def decrypt(self, encryptedMessage): tmp = self.reModulo(poly.multPoly(self.f, encryptedMessage), self.D, self.q) centered = poly.cenPoly(tmp, self.q) m1 = poly.multPoly(self.f_p, centered) tmp = self.reModulo(m1, self.D, self.p) return poly.trim(tmp)
def encrypt(self,message,randPol): if self.h!=None: e_tilda=poly.addPoly(poly.multPoly(poly.multPoly([self.p],randPol),self.h),message) e=self.reModulo(e_tilda,self.D,self.q) return e else: print "Cannot Encrypt Message Public Key is not set!" print "Cannot Set Public Key manually or Generate it"
def decryptSQ(self,encryptedMessage): F_p_sq=poly.multPoly(self.f_p,self.f_p) f_sq=poly.multPoly(self.f,self.f) tmp=self.reModulo(poly.multPoly(f_sq,encryptedMessage),self.D,self.q) centered=poly.cenPoly(tmp,self.q) m1=poly.multPoly(F_p_sq,centered) tmp=self.reModulo(m1,self.D,self.p) return poly.trim(tmp)
def encrypt(self,message,randPol): if self.h!=None: e_tilda=poly.addPoly(poly.multPoly(poly.multPoly([self.p],randPol),self.h),message) e=self.reModulo(e_tilda,self.D,self.q) return e else: print("Cannot Encrypt Message Public Key is not set!") print("Cannot Set Public Key manually or Generate it")
def encrypt(self, message, randPol): if self.h != None: e_tilda = poly.addPoly( poly.multPoly(poly.multPoly([self.p], randPol), self.h), message) e = self.reModulo(e_tilda, self.D, self.q) return e else: print("Error: Public Key is not set, ", end='') print("no default value of Public Key is available.")
def verify(self, message, sign): up = self.myhash(message) vp = self.myhash(self.h) u = poly.addPoly(poly.multPoly([self.p], sign), up) v = self.reModulo(poly.multPoly(u, self.h), self.D, self.q) w = self.reModulo(poly.subPoly(v, vp), self.D, self.p) for i in w: if i != 0: return "Error" return False else: continue print "success" return True
def sign(self, message, randPol, b, f_new, g_new, d_new): self.f = f_new self.g = g_new self.d = d_new #compute up,vp up = self.myhash(message) vp = self.myhash(self.h) #compute u1 u1_pre = poly.multPoly([self.p], randPol) u1 = poly.addPoly(u1_pre, up) #compute v1 v1 = self.reModulo(poly.multPoly(u1, self.h), self.D, self.q) #compute a [gcd_g, s_g, t_g] = poly.extEuclidPoly(self.g, self.D) self.g_p = poly.modPoly(s_g, self.p) a_pre = poly.subPoly(vp, v1) a = self.reModulo(poly.multPoly(a_pre, self.g_p), self.D, self.p) #compute v v_pre = poly.multPoly([int(math.pow(-1, b))], poly.multPoly(a, self.g)) v = poly.addPoly(v1, v_pre) #compute sign sign_pre = poly.multPoly([int(math.pow(-1, b))], poly.multPoly(a, self.f)) sign = poly.addPoly(randPol, sign_pre) return sign
def genPublicKey(self, f_new, g_new, d_new): self.f = f_new self.g = g_new self.d = d_new [gcd_f, s_f, t_f] = poly.extEuclidPoly(self.f, self.D) self.f_p = poly.modPoly(s_f, self.p) self.f_q = poly.modPoly(s_f, self.q) self.h = self.reModulo(poly.multPoly(self.f_q, self.g), self.D, self.q) if not self.runTests(): print("Failed!") quit()
def genPublicKey(self, f_new, g_new, d_new): # Using Extended Euclidean Algorithm for Polynomials to get s and t. self.f = f_new self.g = g_new self.d = d_new [gcd_f, s_f, t_f] = poly.extEuclidPoly(self.f, self.D) self.f_p = poly.modPoly(s_f, self.p) self.f_q = poly.modPoly(s_f, self.q) self.h = self.reModulo(poly.multPoly(self.f_q, self.g), self.D, self.q) if not self.runTests(): quit()
def genPublicKey(self, f_new, g_new, d_new): # Using Extended Euclidean Algorithm for Polynomials # to get s and t. Note that the gcd must be 1 self.f = f_new self.g = g_new self.d = d_new pf = poly.multPoly([self.p], self.f) [gcd_f, s_f, t_f] = poly.extEuclidPoly(self.f, self.D) [gcd_pf, s_pf, t_pf] = poly.extEuclidPoly(pf, self.D) self.f_p = poly.modPoly(s_f, self.p) self.f_q = poly.modPoly(s_f, self.q) self.pf_q = poly.modPoly(s_pf, self.q) self.h = self.reModulo(poly.multPoly(self.g, self.pf_q), self.D, self.q) if not self.runTests(): print "Failed!" quit()
def genPublicKey(self,f_new,g_new,d_new): # Using Extended Euclidean Algorithm for Polynomials # to get s and t. Note that the gcd must be 1 self.f=f_new self.g=g_new self.d=d_new [gcd_f,s_f,t_f]=poly.extEuclidPoly(self.f,self.D) self.f_p=poly.modPoly(s_f,self.p) self.f_q=poly.modPoly(s_f,self.q) self.h=self.reModulo(poly.multPoly(self.f_q,self.g),self.D,self.q) if not self.runTests(): print "Failed!" quit()
print "b",b break # TESTING SUBTRACTION FUNCTION a=P.polysub(c1,c2).tolist() b=q.subPoly(c1,c2) if(a!=b): print "Failed on Case:" print "a subtract b" print "a",a print "b",b break # TESTING MULTIPLICATION FUNCTION a=P.polymul(c1,c2).tolist() b=q.multPoly(c1,c2) if(a!=b): print "Failed on Case:" print "a * b" print "a",a print "b",b break # TESINGING DIVISION FUNCTION if q.trim(c2)==[0]: continue try: [a1,a2]=P.polydiv(c1,c2) a1=q.trim(a1.tolist()) a2=q.trim(a2.tolist()) [b1,b2]=q.divPoly(c1,c2)
print "b", b break # TESTING SUBTRACTION FUNCTION a = P.polysub(c1, c2).tolist() b = q.subPoly(c1, c2) if (a != b): print "Failed on Case:" print "a subtract b" print "a", a print "b", b break # TESTING MULTIPLICATION FUNCTION a = P.polymul(c1, c2).tolist() b = q.multPoly(c1, c2) if (a != b): print "Failed on Case:" print "a * b" print "a", a print "b", b break # TESINGING DIVISION FUNCTION if q.trim(c2) == [0]: continue try: [a1, a2] = P.polydiv(c1, c2) a1 = q.trim(a1.tolist()) a2 = q.trim(a2.tolist()) [b1, b2] = q.divPoly(c1, c2)
def decrypt(self,encryptedMessage): tmp=self.reModulo(poly.multPoly(self.f,encryptedMessage),self.D,self.q) centered=poly.cenPoly(tmp,self.q) m1=poly.multPoly(self.f_p,centered) tmp=self.reModulo(m1,self.D,self.p) return poly.trim(tmp)