def power_function(): print("Testing -- power_mod function --") print("Using numpy.random generating {0} numbers".format(testnumber)) for n in range(testnumber): a = rd.randint(minimum, maximum) b = rd.randint(minimum, maximum) c = rd.randint(minimum, maximum) tf1 = nmtf.power_mod(tf.constant(a), tf.constant(b), tf.constant(c)) nm1 = nm.power_mod(a, b, c) print("Test {0}------------------------------".format(n + 1)) print("nmtf.power_mod({0}, {1}, {2}) = ({3}, {4})".format( a, b, c, tf1[0].eval(), tf1[1].eval())) print(" nm.power_mod({0}, {1}, {2}) = {3}".format(a, b, c, nm1)) print("")
def dlog(p, g, h, B=2**20): """ Calculate discrete logarithm. :param p: Prime p. :param g: Any element in Z_star_p => h=g^x for 0 <= x <= B. :param h: Value h=g^x for 0 <= x <= B. :param B: x value space; 0 <= x <= B. :return: x: discrete log modulo p if found, None otherwise. """ l_hash = dict() print('[+] Calculating l-value table...') for x1 in tqdm(range(B)): g_pow_x1 = numbthy.power_mod(g, x1, p) inv_g_pow_x1 = numbthy.invmod(g_pow_x1, p) l_val = h * inv_g_pow_x1 l_val = l_val % p l_hash[l_val] = x1 print('[+] Looking up r-value in l-value table...') x0, x1 = 0, 0 g_pow_B = numbthy.power_mod(g, B, p) for x0 in tqdm(range(B)): r_val = numbthy.power_mod(g_pow_B, x0, p) try: x1 = l_hash[r_val] print('[+] Found!!') break except KeyError: pass if x0 != 0 and x1 != 0: x = x0 * B + x1 else: x = None return x
def create_blocks_2(self, number=0): """ Construction 1.2 Obtain blocks that create super set of one parallel class. :param number: iteration (offset) number according to Galois Field """ # first obtain alpha (primitive element in Galois Field) alpha = 0 for a in range(self.q): if is_primitive_root(a, self.q): alpha = a break # split points to groups groups = { 1: self.points[:self.q], 2: self.points[self.q:self.q * 2], 3: self.points[self.q * 2:] } get = lambda index, group: groups[group][self.add_mod(index, number)] # references A^0 self.blocks = {'a_': (groups[1][number], groups[2][number], groups[3][number])} # references B_i,j for i in range(0, self.t): for j in (1, 2, 3): self.blocks['b' + str(i) + ',' + str(j)] = \ (get(power_mod(alpha, i, self.q), j), get(power_mod(alpha, i + 2 * self.t, self.q), j), get(power_mod(alpha, i + 4 * self.t, self.q), j)) # references A_i for i in range(0, 6 * self.t): self.blocks['a' + str(i)] = \ (get(power_mod(alpha, i, self.q), 1), get(power_mod(alpha, i + 2 * self.t, self.q), 2), get(power_mod(alpha, i + 4 * self.t, self.q), 3))
#x0 = 2 #x1 = 1 #p=1073676287 #g=1010343267 #h=857348958 #B=pow(2, 10) #1026831 (*this is the solution*) #x0 = 1002; #x1 = 783; print datetime.datetime.now() gi = numbthy.inverse_mod(g, p) gPowB = numbthy.power_mod(g, B, p) print "p", p print "g", g print "h", h print "B", B print "inverse g", gi print "gPowB", gPowB left = {} powX1 = h % p left[powX1] = 0 x1 = 1 while (x1 < B):
def create_blocks_1(self, number=0): """ Construction 1.1 Obtain blocks that create one parallel class. :param number: iteration (offset) number according to Galois Field """ # first obtain alpha (primitive element in Galois Field) and m (by specified equation) alpha, m = 0, 0 for a in range(self.q): if is_primitive_root(a, self.q): for _m in range(self.q): # let me satisfy the equation --> 2 * alpha^m = alpha^t + 1 right = self.add_mod(power_mod(a, self.t, self.q), 1) _a = power_mod(a, _m, self.q) left = self.add_mod(_a, _a) if left == right: alpha, m = a, _m break # split points to groups groups = { 1: self.points[:self.q], 2: self.points[self.q:], } get = lambda index, group: groups[group][self.add_mod(index, number)] # references A^0 self.blocks = {'a_': (groups[1][number], groups[2][number], groups[2][-1])} # references A_i, B_i for i in range(0, self.t): self.blocks['a' + str(i)] = \ (get(power_mod(alpha, i + m + self.t, self.q), 2), get(power_mod(alpha, i + m + 3 * self.t, self.q), 2), get(power_mod(alpha, i + m + 5 * self.t, self.q), 2)) self.blocks['b' + str(i)] = \ (get(power_mod(alpha, i, self.q), 1), get(power_mod(alpha, i + self.t, self.q), 1), get(power_mod(alpha, i + m, self.q), 2)) # references B_i for i in range(2 * self.t, 3 * self.t): self.blocks['b' + str(i)] = \ (get(power_mod(alpha, i, self.q), 1), get(power_mod(alpha, i + self.t, self.q), 1), get(power_mod(alpha, i + m, self.q), 2)) # references B_i for i in range(4 * self.t, 5 * self.t): self.blocks['b' + str(i)] = \ (get(power_mod(alpha, i, self.q), 1), get(power_mod(alpha, i + self.t, self.q), 1), get(power_mod(alpha, i + m, self.q), 2))
def test_power_mod(self): for testcase in ((2, 5, 13, 6), (2, -21, 31, 16), (-2, -21, 31, 15), (0, 5, 31, 0), (5, 0, 31, 1)): self.assertEqual( numbthy.power_mod(testcase[0], testcase[1], testcase[2]), testcase[3])
texterat="p={0}, q={1}, n={2}, t of n={3}, d={4}, and e={5}" print(texterat.format(varp,varq,varn,totientn,vard,vare)) #prints variables if why=='n': varp=int(input('Enter p: ')) varq=int(input('Enter q: ')) varn=varp*varq totientn=(varp-1)*(varq-1) vard=int(input('Enter d: ')) vare=int(input('Enter e: ')) texterat="p={0}, q={1}, n={2}, t of n={3}, d={4}, and e={5}" print(texterat.format(varp,varq,varn,totientn,vard,vare)) check=13 varc=power_mod(check,vare,varn) varb=power_mod(varc,vard,varn) if check==varb: print("Variables confirmed") endop='t' elif check!=varb: print("Variables denied") endop='q' #checks variables associations = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,:;'\"/\\<>(){}[]-=_+?!" choice=input("Enter e to encrypt, d to decrypt, or q to quit: ") while choice!='q' and endop!='q': if choice=="e": conn=1 mes=input("Input string to be encrypted: ")
def f2(p, g, h, x1): denom = power_mod(g, x1, p) return (h * inverse_mod(denom, p)) % p
def f1(p, g, x0, b): return power_mod(g, (x0 * b), p)
def DecryptCipher(CipherVal, EncExp, p1, q1): DecExp = inverse_mod(EncExp, (p1 - 1) * (q1 - 1)) MsgVal = power_mod(CipherVal, DecExp, p1 * q1) return MsgVal
from numbthy import inverse_mod, power_mod p = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171 g = 11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568 h = 3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333 table = [] B = 2**20 for x1 in range(B + 1): denominator = inverse_mod(power_mod(g, x1, p), p) table.append((h * denominator) % p) for x0 in range(B + 1): power = B * x0 trial = power_mod(g, power, p) print 'Try ' + str(x0) try: x1 = table.index(trial) print x0, x1 print x0 * B + x1 break except ValueError: continue