Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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
Пример #5
0
def egcd_b(a, b):
    return egcd(a, b)[2]
Пример #6
0
def egcd_a(a, b):
    return egcd(a, b)[1]
Пример #7
0
def egcd_v(a, b):
    return egcd(a, b)[0]
Пример #8
0
def egcd_b(a, b): return egcd(a, b)[2]

def gcd_eq_zero(x, m):
Пример #9
0
def egcd_a(a, b): return egcd(a, b)[1]
def egcd_b(a, b): return egcd(a, b)[2]
Пример #10
0
def egcd_v(a, b): return egcd(a, b)[0]
def egcd_a(a, b): return egcd(a, b)[1]