Пример #1
0
    C = HW.MultiPrecisionAdd(A, B, "subtract")

    print "A                = ", hex(A)  # 514-bits
    print "B                = ", hex(B)  # 514-bits
    print "A - B            = ", hex(C)  # 515-bits

#####################################################

if operation == 3:
    print "Test Vector for Montgomery Multiplication\n"

    A = helpers.getRandomInt(512)
    B = helpers.getRandomInt(512)
    M = helpers.getModulus(512)
    C = HW.MontMul_512(A, B, M)
    D = (A * B * helpers.Modinv(2**512, M)) % M
    e = C - D

    print "A                = ", hex(A)  # 512-bits
    print "B                = ", hex(B)  # 512-bits
    print "M                = ", hex(M)  # 512-bits
    print "(A*B*R^-1) mod M = ", hex(C)  # 512-bits
    print "(A*B*R^-1) mod M = ", hex(D)  # 512-bits
    print "error            = ", hex(e)

#####################################################

if operation == 4:

    print "Test Vector for Montgomery Exponentiation\n"
Пример #2
0
def MontMul_1024(A, B, M):
    # Returns (A*B*Modinv(R,M)) mod M
    R = 2**1024
    return (A * B * helpers.Modinv(R, M)) % M
Пример #3
0
#####################################################

if operation == 3:
    print "Test Vector for Montgomery Multiplication\n"

    A = helpers.getRandomInt(512)
    B = helpers.getRandomInt(512)
    N = helpers.getModulus(512)

    # A = 0xa84ff2f71071936d568335f4e31da1c104c831dc18d7b9199f5d96b9df7315bd0fa8db7a6201cf9ae0842c7f6797a025684296de2089f536c18b7a583c7a9fc5
    # B = 0xb9cf554dbc2f7d876274c0895b10c21a0322d9435a2cd1af43a483a61f7cfb92f984df1a0d9357bc796f8e582427a609d99348f8079de7731fc8a31b3eea6c6e
    # M = 0xef449a8c29c1266af559bdb8d0c42c042b9a46f619b28d7094369f2842ebe42175eb00442338301d1a509aef69043c1dee3bc1f3a06da74e54d094bc7e4ec49b

    C = HW.MontMul_512(A, B, M)
    D = (A * B * helpers.Modinv(2**512, M)) % M
    e = C - D

    print "A                = ", hex(A)  # 512-bits
    print "B                = ", hex(B)  # 512-bits
    print "M                = ", hex(M)  # 512-bits
    print "(A*B*R^-1) mod M = ", hex(C)  # 512-bits
    print "(A*B*R^-1) mod M = ", hex(D)  # 512-bits
    print "error            = ", hex(e)

    helpers.print2array(A, 'a', 32)
    helpers.print2array(B, 'b', 1)
    helpers.print2array(N, 'n', 32)
    helpers.print2array(N_prime, 'n_prime', 32)
    helpers.print2array((r * r) % N, 'r2m', 32)
    helpers.print2array(r % N, 'rm', 32)