Esempio n. 1
0
    def Gen_response(self,slist,split_ID,hksplit_ID):#t代表多项式的项数,n代表分片的数量
        pairing = global_variable.get_pairing()
        g1 = global_variable.get_g1()
        self.__response = gmpy2.mpz(0)
        coef_hk = dict()
        for i in range(len(slist)-2):
            temp = slist[i]
            temp1 = hksplit_ID[len(slist)-1-i]
            temp2 = gmpy2.mul(temp , temp1)
            temp2 = gmpy2.f_mod(temp2,q)
            coef_hk[i+2] = temp2
        #存m个f(i)+a[i]连乘的结果
        fk_ID = (np.polyval(slist,split_ID))
        fk_ID = fk_ID % q
        for l in coef_hk:
            coefhk = coef_hk[l]
            fk_ID = gmpy2.sub(fk_ID , coefhk)
            fk_ID = fk_ID % q
        self.__response = fk_ID

        
        self.__comm_response_ID = Element(pairing, G1)
        self.__comm_response_ID = g1 ** int(self.__response)
        return self.__comm_response_ID,self.__response
Esempio n. 2
0
    def __Gen_vk(self, t, n, k):
        #获取全局变量
        pairing = global_variable.get_pairing()
        g1 = global_variable.get_g1()

        #生成秘密 secret 的承诺
        secret_k = dict()
        secret = self.__f[0]
        temp = secret
        secret_k[1] = secret
        for i in range(k):
            if i >= 1:
                temp = gmpy2.mul(temp, secret)
                temp = gmpy2.f_mod(temp, q)
                secret_k[i + 1] = temp
        commitment_s_i = Element(pairing, G1)
        self.__commitment_s = dict()
        for i in secret_k:
            commitment_s_i = g1**int(secret_k[i])
            self.__commitment_s[i] = commitment_s_i
        #print ("commitment_s = \n",self.__commitment_s)

        #生成 f(x) 的承诺
        commitment_fx_coef = self.__comm(self.__f, 1, t, pairing, g1)
        print("the commitment of fx = \n", commitment_fx_coef)
        self.__commitment_fx_joint = str(commitment_fx_coef[0])
        for i in range(t - 1):
            self.__commitment_fx_joint = self.__commitment_fx_joint + str(
                commitment_fx_coef[i + 1])
        #print("the joint of fx = \n",self.__commitment_fx_joint)

        #生成 r
        self.__r = 0
        r = sha256()
        r.update(self.__commitment_fx_joint.encode("utf8"))
        dr = r.hexdigest()
        self.__r = int(dr, 16)
        self.__r = gmpy2.mpz(self.__r)
        self.__r = gmpy2.f_mod(self.__r, q)
        #print("r = \n",self.__r)

        #生成 f(r) 和 f(r)^k 的承诺
        self.__commitment_fr_k = dict()
        fr = self.__fun(self.__f, 1, t, self.__r)
        comm_fr = Element(pairing, G1)
        comm_fr = g1**int(fr)
        self.__commitment_fr_k[1] = comm_fr
        fr_i = fr
        for i in range(k - 1):
            fr_i = fr_i * fr
            comm_fr_i = Element(pairing, G1)
            comm_fr_i = g1**int(fr_i)
            self.__commitment_fr_k[i + 2] = comm_fr_i
        print("commitment_fr_k = \n", self.__commitment_fr_k)

        #生成 ck(x) 的承诺
        commitment_ck_coef = dict()
        self.__commitment_ck_joint = dict()
        for i in self.__ck:
            commitment_ck_i = list()
            ck_i = self.__ck[i]
            commitment_ck_i = self.__comm(ck_i, 1, t, pairing, g1)
            commitment_ck_coef[i] = commitment_ck_i
        #print("commitment_ck_coef = \n", commitment_ck_coef)
        for i in commitment_ck_coef:
            comm_ci_coef = commitment_ck_coef[i]
            str_comm_ci_coef = str(comm_ci_coef[0])
            str_comm_ci_coef = str_comm_ci_coef[:130]
            for j in range(t - 1):
                str_comm_ci_coef = str_comm_ci_coef + str(comm_ci_coef[j + 1])
            self.__commitment_ck_joint[i] = str_comm_ci_coef
        #print("commitment_ck_joint = \n",self.__commitment_ck_joint)

        #生成 hk(x) 的承诺
        commitment_hk_coef = dict()  #用于存储基点与hk各个系数的点乘结果
        self.__commitment_hk_joint = dict()  #用于存储拼接后的hk的承诺
        for i in self.__hk:
            commitment_hk_i = list()
            hk_i = self.__hk[i]
            commitment_hk_i = self.__comm(hk_i, i, t, pairing, g1)
            commitment_hk_coef[i] = commitment_hk_i
        #print("commitment_hk_coef = \n",commitment_hk_coef)
        for i in commitment_hk_coef:
            comm_hi_coef = commitment_hk_coef[i]
            str_comm_hi_coef = str(comm_hi_coef[0])
            str_comm_hi_coef = str_comm_hi_coef[:130]
            for j in range(i * t - i):
                str_comm_hi_coef = str_comm_hi_coef + str(comm_hi_coef[j + 1])
            self.__commitment_hk_joint[i] = str_comm_hi_coef
        #print("commitment_hk_joint = \n",self.__commitment_hk_joint)

        #生成 f(x) 分片 f(i) 的承诺,用于安全传输
        self.__commitment_split = dict()
        for i in self.__split:
            comm_split = Element(pairing, G1)
            comm_split = g1**int(self.__split[i])
            self.__commitment_split[i] = comm_split
        #print("commitment_split = \n",self.__commitment_split)

        #生成 hk(x) 分片 hk(i) 的承诺,用于安全传输
        self.__commitment_hk_split = dict()
        for i in self.__hksplit:
            hksplit_key = self.__hksplit[i]
            comm_hksplit_key = dict()
            for key in hksplit_key:
                comm_hksplit = Element(pairing, G1)
                comm_hksplit = g1**int(hksplit_key[key])
                comm_hksplit_key[key] = comm_hksplit
            self.__commitment_hk_split[i] = comm_hksplit_key
Esempio n. 3
0
    def __verify_com(self,k,t,commitment_s,commitment_fx_joint,commitment_fr_k,commitment_ck_joint,commitment_hk_joint):
        #获取全局变量
        pairing = global_variable.get_pairing()
        g1 = global_variable.get_g1()
        
        #用来记录验证成功的次数
        success_num = 0
        
        #验证秘密 secret 的承诺
        count = 0
        temp1 = Element(pairing, G1)
        temp2 = Element(pairing, G1)
        for i in range(k):
            if i >= 1:
                temp1 = pairing.apply(commitment_s[1], commitment_s[i])
                temp2 = pairing.apply(commitment_s[i+1], g1)
                if (temp1 == temp2):
                    count = count + 1
                    print("the verify for commitment of secret is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^")
                else:
                    print("the verify for commitment of secret is:>~< >~< >~< >~< >~< >~<fail!>~< >~< >~< >~< >~< >~<")
        if count == k-1:
            success_num = success_num + 1
        
        #生成 r'
        r = 0
        rt = sha256()
        rt.update(commitment_fx_joint.encode("utf8"))
        dr = rt.hexdigest()
        r = int(dr,16)
        r = gmpy2.mpz(r)
        r = gmpy2.f_mod(r,q)
        print("r' = \n",r)
                   
        #处理以字符串形式拼接起来的承诺 CM_fx,并生成CM_fr'
        temp = commitment_fx_joint
        str_fx = re.findall(r'.{130}', temp)
        #print("str_fx = \n",str_fx)
        gfr_list = list()
        for i in range(len(str_fx)):
            temp = Element(pairing,G1,value = str_fx[i])
            ri = gmpy2.powmod(r,i,q)
            temp = temp ** int(ri)
            gfr_list.append(temp)
        #print("gfr_list = \n",gfr_list)
        gfr = Element(pairing,G1)
        for i in range(len(gfr_list)):
            gfr = gfr * gfr_list[i]

        #验证秘密 fk(r) 的承诺
        count = 0
        temp3 = Element(pairing, G1)
        temp4 = Element(pairing, G1)
        for j in range(k):
            if j >= 1:
                temp3 = pairing.apply(gfr, commitment_fr_k[j])
                temp4 = pairing.apply(commitment_fr_k[j+1], g1)
                if (temp3 == temp4):
                    count = count + 1
                    print("the verify for commitment of fk(r) is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^")
                else:
                    print("the verify for commitment of fk(r) is:>~< >~< >~< >~< >~< >~<fail!>~< >~< >~< >~< >~< >~<")
        if count == k-1:
            success_num = success_num + 1
        
        #处理以字符串形式拼接起来的承诺 CM_ck
        com_ck = dict() 
        for key in commitment_ck_joint:
            com_cki = list()
            temp = commitment_ck_joint[key]
            str_ck = re.findall(r'.{130}', temp)
            #print("str_ck = \n",str_ck)
            for i in range(len(str_ck)):
                temp1 = Element(pairing,G1,value = str_ck[i])
                com_cki.append(temp1)
            com_ck[key] = com_cki
        #print("com_ck = \n",com_ck)
        
        #生成 ck(r') 的承诺
        commitment_ck_r = dict()
        temp1 = list()
        for key in com_ck:
            comm_ck_r = list()
            temp1 = com_ck[key]
            temp2 = Element(pairing,G1)
            for i in range(t):
                temp2 = temp1[i] ** int(r ** i)
                comm_ck_r.append(temp2)
            temp3 = Element(pairing,G1)
            temp3 = comm_ck_r[0]
            for j in range(t-1):
                temp3 = temp3 * comm_ck_r[j+1]
            commitment_ck_r[key] = temp3
        #print("commitment_ck_r = \n",commitment_ck_r)    
    
        #生成 hk(r') 的承诺
        com_hk = dict()
        for key in commitment_hk_joint:
            com_hki = list()
            temp = commitment_hk_joint[key]
            str_hk = re.findall(r'.{130}', temp)
            for i in range(len(str_hk)):
                temp1 = Element(pairing,G1,value = str_hk[i])
                com_hki.append(temp1)
            com_hk[key] = com_hki
        #print("com_hk = \n",com_hk)
        
        temp1 = Element(pairing, G1)
        commitment_hk_r = dict()
        for key in com_hk:
            temp3 = list()
            temp2 = com_hk[key]
            for j in range(key*t-key+1):
                temp1 = temp2[j]**int(r**j)
                temp3.append(temp1)
            temp4 = Element(pairing, G1)
            temp4 = temp3[0]
            for k in range(key*t-key):
                temp4 = temp4 * temp3[k+1]
            commitment_hk_r[key] = temp4
        #print("commitment_hk_r = \n",commitment_hk_r)
    
        commitment_hk_r1 = dict()
        temp = Element(pairing, G1)
        for i in commitment_ck_r:
            temp = commitment_fr_k[i]-commitment_ck_r[i]-commitment_s[i]
            commitment_hk_r1[i] = temp
        #print ("commitment_hk_r1 = \n",commitment_hk_r1)
        
        #验证秘密 hk(r) 的承诺
        count = 0
        for key in commitment_hk_r1:
            if commitment_hk_r1[key] == commitment_hk_r[key]:
                count = count + 1
                print("the verify for commitment of hk(r) is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^")
            else:
                print("the verify for commitment of hk(r) is:>~< >~< >~< >~< >~< >~<fail!>~< >~< >~< >~< >~< >~<")
        if count == len(commitment_hk_r1):
            success_num = success_num + 1
            
        #汇总验证结果
        if success_num == 3:
            return True
Esempio n. 4
0
    def __verify_core_share(self,k,t,commitment_split,commitment_hk_split,commitment_fx_joint,commitment_hk_joint,ID):      
        #获取全局变量
        pairing = global_variable.get_pairing()

        #用来记录验证成功的次数
        success_num = 0
        
        #验证分片 fi 的承诺
        count = 0
        com_fx_coef = list()
        temp = commitment_fx_joint
        str_fx = re.findall(r'.{130}', temp)
        for i in range(len(str_fx)):
            temp = Element(pairing,G1,value = str_fx[i])
            com_fx_coef.append(temp)
        #print("com_fx_coef = \n",com_fx_coef)
        
        commitment_fID = Element(pairing, G1)
        comm_fi = Element(pairing, G1)  
        temp2 = Element(pairing, G1)
        commitment_fxcoef_ID = list()
        for j in range(t): 
            temp2 = com_fx_coef[j] ** int(ID**j)
            commitment_fxcoef_ID.append(temp2)
        #print("commitment_fxcoef_ID = ",commitment_fxcoef_ID)
        comm_fi = commitment_fxcoef_ID[0]
        for m in range(t-1):
            comm_fi = comm_fi * commitment_fxcoef_ID[m+1]
        commitment_fID = comm_fi
        #print("commitment_fID = ",commitment_fID)
        if commitment_fID == commitment_split[ID]:     
            count = count + 1
            print("the verify for commitment of fi is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^")
        else:
            print("the verify for commitment of fi is:>~< >~< >~< >~< >~< >~<fail!>~< >~< >~< >~< >~< >~<")
        if count == len(commitment_fID):
            success_num = success_num + 1
            
        #验证分片 hki 的承诺
        count = 0
        temp1 = Element(pairing, G1)
        temp4 = Element(pairing, G1)
        commitment_hk_i = dict()
        com_hk = dict()
        for key in commitment_hk_joint:
            com_hki = list()
            temp = commitment_hk_joint[key]
            str_hk = re.findall(r'.{130}', temp)
            for i in range(len(str_hk)):
                temp1 = Element(pairing,G1,value = str_hk[i])
                com_hki.append(temp1)
            com_hk[key] = com_hki
        #print("com_hk = ",com_hk)
        
        for key in com_hk:
            com_hk_key = com_hk[key]
            comm_hk_ii = list()
            for j in range(key*t-key+1):
                temp1 = com_hk_key[j]**int(ID**j)
                comm_hk_ii.append(temp1)
            temp4 = comm_hk_ii[0]
            for k in range(key*t-key):
                temp4 = temp4 * comm_hk_ii[k+1]
            commitment_hk_i[key] = temp4
        print("commitment_hk_i = ",commitment_hk_i)

        if commitment_hk_split[ID] == commitment_hk_i:
            count = count + 1
            print("the verify for commitment of hksplit is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^")
        else:
            print("the verify for commitment of hksplit is:>~< >~< >~< >~< >~< >~<fail!>~< >~< >~< >~< >~< >~<")
        if count == len(commitment_hk_split):
            success_num = success_num + 1
        
        if success_num == 2:
            return True
Esempio n. 5
0
    def verify_response_comm(self, slist, t, ID, commitment_fx_joint,
                             commitment_s, commitment_cx_joint,
                             comm_response_ID):
        pairing = global_variable.get_pairing()
        g1 = global_variable.get_g1()

        self.__response_coef_comm = dict()
        commitment_cx = dict()  # 存储ck(ID)的承诺
        commitment_fx = dict()  # 存储f(ID)^k的承诺
        com_ck = dict()  # 存ck(x)系数的承诺
        for key in commitment_cx_joint:
            if key <= (len(slist) - 1):
                com_cki = list()  # 中间变量
                temp = commitment_cx_joint[key]
                str_ck = re.findall(r'.{130}', temp)
                for i in range(len(str_ck)):
                    temp1 = Element(pairing, G1, value=str_ck[i])
                    com_cki.append(temp1)
                com_ck[key] = com_cki
                com_ck_key = com_ck[key]
                comm_ck_coef_i = list()
                for j in range(t):
                    temp2 = com_ck_key[j]**(int(ID)**j)
                    comm_ck_coef_i.append(temp2)
                temp3 = comm_ck_coef_i[0]
                for j in range(t - 1):
                    temp3 = temp3 * comm_ck_coef_i[j + 1]
                commitment_cx[key] = temp3
        temp = commitment_fx_joint
        str_fx = re.findall(r'.{130}', temp)
        gfr_list = list()
        for i in range(len(str_fx)):
            temp1 = Element(pairing, G1, value=str_fx[i])
            gfr_list.append(temp1)
        comm_fk_coef_i = list()
        for j in range(t):
            temp2 = gfr_list[j]**(int(ID)**j)
            comm_fk_coef_i.append(temp2)
        temp3 = comm_fk_coef_i[0]
        for j in range(t - 1):
            temp3 = temp3 * comm_fk_coef_i[j + 1]
        commitment_fx = temp3
        commck_mul_commsk = dict()  # 存储ck(ID)的承诺*s^k的承诺
        for key in commitment_cx:
            temp1 = commitment_cx[key] * commitment_s[key]
            commck_mul_commsk[key] = temp1
        if (len(slist) - 1) > 1:
            response_commcksk = dict()
            for i in range(len(slist) - 1):
                if i + 2 <= len(slist) - 1:
                    temp1 = commck_mul_commsk[i + 2]
                    temp = temp1**int(slist[len(slist) - i - 3])
                    response_commcksk[i + 2] = temp
            temp2 = response_commcksk[2]
            for i in response_commcksk:
                if i != 2:
                    temp3 = response_commcksk[i]
                    temp2 = temp2 * temp3
            self.__response_coef_comm = temp2 * \
                (commitment_fx**int(slist[len(slist)-2])
                 )*(g1**int(slist[len(slist)-1]))
        else:
            self.__response_coef_comm = (commitment_fx**int(
                slist[len(slist) - 2])) * (g1**int(slist[len(slist) - 1]))
            #print("response_coef_comm = ",self.__response_coef_comm)

        if comm_response_ID == self.__response_coef_comm:
            print(
                "the verify for response is:^-^ ^-^ ^-^ ^-^ ^-^ ^-^success!^-^ ^-^ ^-^ ^-^ ^-^ ^-^"
            )
        else:
            print(
                "the verify for response is:>~< >~< >~< >~< >~< >~<fali!>~< >~< >~< >~< >~< >~<"
            )