コード例 #1
0
ファイル: ntrusign.py プロジェクト: 68823613/QS-BlockChain
    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
コード例 #2
0
ファイル: ntru.py プロジェクト: intangere/Essence
	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"
コード例 #3
0
	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")
コード例 #4
0
 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.")
コード例 #5
0
ファイル: ntrusign.py プロジェクト: 68823613/QS-BlockChain
    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
コード例 #6
0
ファイル: testPoly.py プロジェクト: Sapphirine/ntru
rand.seed(2)
print "TESING 10000 CASES..."
for k in range(10000):
	order1=rand.randint(0,10)
	order2=rand.randint(0,10)
	c1=[0]*(order1+1)
	c2=[0]*(order2+1)
	for i in range(len(c1)):
		c1[i]=rand.randint(-5,5)
	for i in range(len(c2)):
		c2[i]=rand.randint(-5,5)

	# TESTING ADDITION FUNCTION
	a= P.polyadd(c1,c2).tolist()	
	b= q.addPoly(c1,c2)
	if(a!=b):
		print "Failed on Case:"
		print "a add b"
		print "a",a
		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
コード例 #7
0
rand.seed(2)
print "TESING 10000 CASES..."
for k in range(10000):
    order1 = rand.randint(0, 10)
    order2 = rand.randint(0, 10)
    c1 = [0] * (order1 + 1)
    c2 = [0] * (order2 + 1)
    for i in range(len(c1)):
        c1[i] = rand.randint(-5, 5)
    for i in range(len(c2)):
        c2[i] = rand.randint(-5, 5)

    # TESTING ADDITION FUNCTION
    a = P.polyadd(c1, c2).tolist()
    b = q.addPoly(c1, c2)
    if (a != b):
        print "Failed on Case:"
        print "a add b"
        print "a", a
        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