def modinv(x, m): gcd, a, b = egcd(x, m) assert gcd == 1 if a < 0: a += m assert (a * x) % m == 1 return a
def montmul(x, y, m): #print 'montmul(%s, %s, %s)' % (dhex(x), dhex(y), dhex(m)) A = 0 gcd, m_prime, _ = dumbegcd.egcd(m, WORD_MAX) assert gcd == 1 m_prime = -m_prime dump(' x', x) dump(' y', y) dump(' m', m) for i in range(words(m)): u_i = ((word(A, 0) + word(x, i) * word(y, 0)) * m_prime) % WORD_MAX A = (A + word(x, i) * y + u_i * m) >> R_shift if A >= m: A -= m dump(' result-m', A) return A
def egcd_b(a, b): return egcd(a, b)[2]
def egcd_a(a, b): return egcd(a, b)[1]
def egcd_v(a, b): return egcd(a, b)[0]
def egcd_b(a, b): return egcd(a, b)[2] def gcd_eq_zero(x, m):
def egcd_a(a, b): return egcd(a, b)[1] def egcd_b(a, b): return egcd(a, b)[2]
def egcd_v(a, b): return egcd(a, b)[0] def egcd_a(a, b): return egcd(a, b)[1]