def MontExp(X, E, N): # Returns (X^E) mod N # R = 2**1024 # RN = R % N # R2N = (R*R) % N; # A = RN; # X_tilde = MontMul(X,R2N,N) # t = helpers.bitlen(E) # for i in range(0,t): # A = MontMul(A,A,N) # if helpers.bit(E,t-i-1) == 1: # A = MontMul(A,X_tilde,M) # A = MontMul(A,1,N) R = 2**1024 RN = R % N R2N = (R * R) % N A = RN X_tilde = MontMul_2bW(X, R2N, N) t = helpers.bitlen(E) for i in range(0, t): A = MontMul_2bW(A, A, N) if helpers.bit(E, t - i - 1) == 1: A = MontMul_2bW(A, X_tilde, N) A = MontMul_2bW(A, 1, N) return A
def MontExp_512(X, E, M): # Returns (X^E) mod M R = 2**512 R2 = (R * R) % M A = R % M X_tilde = MontMul_512(X, R2, M) t = helpers.bitlen(E) for i in range(0, t): A = MontMul_512(A, A, M) if helpers.bit(E, t - i - 1) == 1: A = MontMul_512(A, X_tilde, M) A = MontMul_512(A, 1, M) return A
def MontExp_1024(X, E, M): # Returns (X^E) mod M R = 2**1024 R2 = (R * R) % M import pdb pdb.set_trace() A = R % M X_tilde = MontMul_1024(X, R2, M) t = helpers.bitlen(E) for i in range(0, t): A = MontMul_1024(A, A, M) if helpers.bit(E, t - i - 1) == 1: A = MontMul_1024(A, X_tilde, M) A = MontMul_1024(A, 1, M) return A
def MontExp_1024_print(X, E, M): # Returns (X^E) mod M R = 2**1024 R2 = (R * R) % M A = R % M X_tilde = MontMul_1024(X, R2, M) t = helpers.bitlen(E) for i in range(0, 15): print "-- Iteration ", str(i) A = MontMul_1024(A, A, M) print "\n@ A* A -- ", str(i), " : ", hex(A) if helpers.bit(E, t - i - 1) == 1: A = MontMul_1024(A, X_tilde, M) print "\n@ xtilde * A -- ", str(i), " : ", hex(A) A = MontMul_1024(A, 1, M) return A
def MontExp_512(X, E, M): # Returns (X^E) mod M R = 2**512 R2 = (R * R) % M A = R % M X_tilde = MontMul_512(X, R2, M) t = helpers.bitlen(E) for i in range(0, 20): A = MontMul_512(A, A, M) if helpers.bit(E, t - i - 1) == 1: A = MontMul_512(A, X_tilde, M) #if i >= 505: print "uint32_t A_test = {" + helpers.WriteConstants(A, 16) + "};" print " " A = MontMul_512(A, 1, M) return A