def multiplicative_inverse(e, phi):
    """
    Euclid's extended algorithm for finding the multiplicative inverse of two numbers
    """
    d, next_d, temp_phi = 0, 1, phi
    while e > 0:
        quotient = temp_phi // e
        d, next_d = next_d, d - quotient * next_d
        temp_phi, e = e, temp_phi - quotient * e
    if temp_phi > 1:
        raise ValueError('e is not invertible by modulo phi.')
    if d < 0:
        d += phi
    return d