def command(self, command): if command[:5] == "start": if command[6:10] == "poly": self.cryptor = poly() self.cryptor.mode = 'dual' self.cryptor.setKeys(command[11:]) if command[6:9] == "des": self.cryptor = DES() self.cryptor.mode = 'dual' self.cryptor.setKeys(command[10:26]) if command[6:10] == "3des": self.cryptor = TDES() self.cryptor.mode = 'dual' self.cryptor.setKeys(command[11:27], command[28:44], command[45:61]) if command[6:9] == "aes": self.cryptor = AES() self.cryptor.mode = 'dual' self.cryptor.setKeys(command[10:42]) if command[:7] == "encrypt": if command[8:12] == "poly": self.cryptor = poly() self.cryptor.mode = 'enc' self.cryptor.setEnKey(command[13:]) if command[:7] == "decrypt": if command[8:12] == "poly": self.cryptor = poly() self.cryptor.mode = 'dec' self.cryptor.setDeKey(command[13:])
def test_eval2seq(self): for i in range(100): poly = Polynomial.random(2) points = poly.points(*range(1, 3)) for i in range(20): x = randint(-100, 100) self.assertEqual(eval2seq(points, poly[-1], x), poly(x))
def test_generate2(self): for i in range(100): poly = Polynomial.random(2) points = poly.points(1, 2) generator = generate2(points, poly[-1]) for i in range(3, 10): self.assertEqual(next(generator), poly(i))
def test_generate(self): for degree in range(2, 5): for i in range(100): poly = Polynomial.random(degree) points = poly.points(*range(degree)) generator = generate(points, poly[-1]) for i in range(degree, degree + 10): self.assertEqual(next(generator), poly(i))
def test_eval2(self): for i in range(100): poly = Polynomial.random(2) x0 = randint(1, 100) xn = randint(1, 100) if x0 == xn: continue points = poly.points(x0, xn) for i in range(20): x = randint(-100, 100) if x in (x0, xn): continue self.assertEqual(eval2(points, poly[-1], x), poly(x))
def lift(x, p): """ Function returns the polynomial coefficients lie between -p/2 and p/2 (centered lift) lift(poly([1, 2, 3, 4, 5, 6]), 5) >>> poly([1, 2, -2, -1, 0, 1]) """ a = [] for i in x: if i>p//2: a.append(i - p) else: a.append(i) return poly(a)
def L(d1, d2, N): """ Function to create a polynomial degree N-1 that d1 coefficients equal 1, d2 coefficients equal -1, the rest 0. >>> L(1, 1, 5) poly([1, -1, 0, 0, 0]) """ l = [1]*d1 + [-1]*d2 + [0]*(N-d1-d2) out = [] for i in range(N): j = random.randrange(0, N-i) out.append(l.pop(j)) return poly(out)
def ntrumul(f, g, N): """ f = poly([2, 5]) g = poly([4, 3]) ntrumul(f, g, 3) == f*g % poly([-1, 0, 0, 1]) >>> True where f*g % poly([-1, 0, 0, 1]) means f*g mod (X^3 - 1) """ fg = [] for j in range(N): s = 0 for i in range(N): s = s + f[i]*g[(-i+j)%N] fg.append(s) return poly(fg)
def rr_factor(Q, D, m): p = [-1] deg = [-1] t = 1 u = 0 coeff = GF([0], m) f = GF([], m) if len(Q) == 1: for r in poly(Q[0], reverse = True).roots(): f.append([r]) #[[0]] + f ? return f else: t, p, deg, coeff, f = rr_dfs(Q, D, u, t, p, deg, coeff, f, m) adjust_answers(f, m) return f
def tryReverseOp(a, b, op): crutch = {type(N(0)): 1, type(Z(0)): 2, type(Q(0)): 3, type(poly(0)): 4 } # print( type( a ), type( b ) ) # print( type(N(0)), type(Z(0)), type(Q(0)), type(poly(0)) ) # print( a, op, b, ": ", type( a ), type( b ) ) # print( self, "__add__", other, ": ", type( self ), type( other ) ) try: if crutch[type(a)] < crutch[type(b)]: return eval('type(b)(a)' + op + 'b') else: # print( str( a ) + op + str( eval( "type(a)(b)" ) ) ) return eval("a" + op + 'type(a)(b)') except: print(a, op, b, ": ", type(a), type(b)) raise RuntimeError
def __init__(self,x,n,k,b=256): self.field=rand_prime(b) #the field #self.field=x self.secret=x #the secret we want to share self.k=k #the threshold self.n=n #number of shares c=[] #the polynomial if type(self.secret) in (long,int): pass elif type(self.secret) is str: #if secret is string transform to long b=bituse(self.secret) self.secret=b.long() #self.field=b.long() c.append(self.secret) #append secret as free coeficient for i in range(1,k): #generate random polynomial coeficients c.append(randrange(2,self.field)) self.P=poly(c) #create polynomial
def __init__(self, p, poly, root): PAdic.__init__(self, p) self.root = root self.poly = poly self.deriv = derivative(poly) # argument checks for the algorithm to work if poly(root) % p: raise ValueError("%d is not a root of %s modulo %d" % (root, poly, p)) if self.deriv(root) % p == 0: raise ValueError("Polynomial %s is not separable modulo %d" % (poly, p)) # take care of trailing zeros digit = self.root self.val = str(digit) self.exp = self.p while digit == 0: self.order += 1 digit = self._nextdigit()
for bb in b: print(bb) print("orthonormating...") bo = findorthonormal(*b) for bb in bo: print(bb) print(bo[-1].q) print(np.array(scalarmat(*bo))) print(maxdiffident(scalarmat(*bo))) exit() x = poly(1, 2, 3) print(x * x) print(x * 3) print(3 * x) exit() print(x) X = x.int() print(X) print(x.intfrom(-1, 1))
def genPublickey(f, g, N=5, p=3, q=128): fq = polyinv(f, poly([-1]+[0]*(N-1)+[1]))%q h = ntrumul(fq, g, N)%q return h
def ppolytrans(pp, a): qq = ppoly() for i in range(len(pp.intv)): qq.intv.append((pp.intv[i][0]+a, pp.intv[i][1]+a)) qq.poly.append(polycomp(pp.poly[i], poly([1, -a]))) return qq
def autogen_poly(max_order=15, max_exponent=5, min_exponent=-3, verbose=False): rand_order = random() * max_order first = True root_list = [] if verbose: print('Poly Auto-Gen') print('Poly Order:', rand_order) while True: # check for double/triple root double_check = random() triple_check = random() if double_check < double_limit: num_roots = 2 if verbose: print('Double Root') elif triple_check < triple_limit: num_roots = 3 if verbose: print('Triple Root') else: num_roots = 1 if verbose: print('Single Root') # real part significand = random() * 9.999999 * 2 - 9.999999 exponent = random() * (max_exponent - min_exponent) + min_exponent realpart = significand * 10**exponent imag_check = random() if imag_check > imag_limit: # imag part significand = random() * 9.999999 * 2 - 9.999999 exponent = random() * (max_exponent - min_exponent) + min_exponent imagpart = significand * 10**exponent else: imagpart = 0 if verbose: print('Root Generated:', realpart + 1j * imagpart) if abs(imagpart) > 0: temp_poly = poly([realpart + 1j * imagpart, 1]) * poly( [realpart - 1j * imagpart, 1]) else: temp_poly = poly([realpart + 0j, 1]) if verbose: print('Temp Poly:', temp_poly.coeff_list) temp_poly2 = poly([1]) for i in range(num_roots): if verbose: print('Temp Poly2:', temp_poly2.coeff_list) temp_poly2 = temp_poly * temp_poly2 root_list.append(-realpart - 1j * imagpart) if abs(imagpart) > 0: root_list.append(-realpart + 1j * imagpart) if verbose: print('Temp Poly2:', temp_poly2.coeff_list) if first: out_poly = temp_poly2 first = False else: out_poly = out_poly * temp_poly2 if verbose: print('Out Poly:', out_poly.coeff_list) if out_poly.order > rand_order: break return (out_poly, root_list)
def P(msg): return poly(msg, m, reverse=True)
from poly import * p0 = poly([1, 2, 3, 0, 0]) p1 = poly([2, 4, 6, 8]) p2 = p0 + p1 print('p0 order:', p0.order) print('p1 order:', p1.order) print('p2 order:', p2.order) print(p2.coeff_list) p3 = poly([5, 1]) p4 = poly([-5, 1]) p5 = p3 * p4 print('p5 order:', p5.order) print(p5.coeff_list) roots = p5.find_roots() print(roots) p6 = poly([10, 1]) p7 = p5 * p6 roots = p7.find_roots() print(roots) (nr_root, c_flag, count) = p7.newton_raphson(initial_guess=14 + 1j * 10) print('Newton Raphson Convergence:', c_flag)
def dec(e, f, N=5, p=3, q=128): a = lift(ntrumul(f, e, N)%q, q) fp = polyinv(f, poly([-1]+[0]*(N-1)+[1]))%p return lift(ntrumul(fp, a, N)%p, p)
from poly import * poly1 = poly([(100 + 30j), 1]) * poly([(100 - 30j), 1]) * poly([ (2091 + 4039j), 1 ]) * poly([(2091 - 4039j), 1]) * poly([0, 1]) * poly([0, 1]) * poly([-400, 1]) # Complete poly2 = poly([(0.0061652364526995915 + 0j), (-0.15703803937517294 + 0j), 1]) poly2_roots = [(0.07851901968758647 + 0j), (0.07851901968758647 + 0j)] poly3 = poly([(-2.0329929352016895e+46 + 0j), (4.6074776066966756e+45 + 0j), (-2.8149275313239648e+44 + 0j), (1.7232900636595777e+41 + 0j), (1.753470178488559e+38 + 0j), (1.5162837879777032e+35 + 0j), (3.690172287974137e+32 + 0j), (-1.5610651329014487e+29 + 0j), (-2.5453582435211037e+26 + 0j), (-1.914824175192864e+23 + 0j), (-1.999356052571492e+20 + 0j), (-4.189609058991948e+16 + 0j), (13250777879930.385 + 0j), (3497366848.737716 + 0j), (3792.3283982909375 + 0j), 1]) poly3_roots = [(737.5504550420529 + 0j), (-1258.043481242107 - 0.1795066596232439j), (-1258.043481242107 + 0.1795066596232439j), (0.008727877200109596 - 1052.5593274967691j), (0.008727877200109596 + 1052.5593274967691j), (3977.3286200634852 + 0j), (8.245570792597876 + 2.2311916758674357j), (8.245570792597876 - 2.2311916758674357j), (838.3018391845926 + 0j), (0.35867508670257586 - 1250.9229528604917j), (0.35867508670257586 + 1250.9229528604917j), (-0.09183723360664127 + 59239.25413075491j), (-0.09183723360664127 - 59239.25413075491j),