def generate_quadratic_residue(p: int) -> int: a = number.getRandomRange(0, p - 1) while pow(a, (p - 1) // 2, p) == p - 1: a = number.getRandomRange(0, p - 1) return a
def predict_rating(self, i, k): # predict rating of user i, item k denoted P_i,k enc_obj = ElGamal.construct( (self.p, self.g, self.user_list[i].getPubKey())) encrypted_ratings = self.user_list[i].sendRatings(self.m) sum_of_sims = 0 l = 10 #modified top_l = sorted(range(len(self.simMat[k])), key=lambda i: self.simMat[k][i])[-1 * l:] #modified #print(top_l) #print(self.simMat[k]) for j in range(self.m): if k != j: sum_of_sims += self.simMat[k][j] R_k = self.avg_ratings[k] * 200 #print(R_k) #print(sum_of_sims) #print(R_k*sum_of_sims) a = number.getRandomRange(2, self.p - 1, Random.new().read) first_term = elgEncrypt(enc_obj, pow(self.g, int(R_k * sum_of_sims), self.p), a) result = (1, 1) result = self.addElgamal(result, first_term, self.p) for j in range(self.m): if k != j: a = number.getRandomRange(2, self.p - 1, Random.new().read) R_j = self.avg_ratings[j] * 200 denom = elgEncrypt(enc_obj, pow(self.g, int(R_j), self.p), a) denom_inv = self.inv_pair(denom) inter_result = self.addElgamal(encrypted_ratings[j], denom_inv, self.p) #inter_result = encrypted_ratings[j] inter_result = (inter_result[0]**self.simMat[k][j], inter_result[1]**self.simMat[k][j]) result = self.addElgamal(result, inter_result, self.p) a = number.getRandomRange(2, self.p - 1, Random.new().read) encrypted_denom = elgEncrypt(enc_obj, pow(self.g, sum_of_sims, self.p), a) # send results to user obtain rating predicted_rating = self.user_list[i].calculate_prediction( result, encrypted_denom) result = predicted_rating / 200 #result = self.avg_ratings[k]# only the average if result > 5: result = 5 return result
def genElgamalKey(self, p, g): # generate elgamal keys according to parameters self.p = p self.g = g self.privElgamalKey = number.getRandomRange(2, self.p - 1, Random.new().read) self.pubElgamalKey = pow(g, self.privElgamalKey, self.p)
def ChooseE(n): """ This function for choose E. """ while True: e = Crypto_Number.getRandomRange(2,n) if(Module.gcd(e,n)==1): return e
def average_round1(self, r): k1 = number.getRandomRange(2, self.p - 1, Random.new().read) k2 = number.getRandomRange(2, self.p - 1, Random.new().read) if r in self.ratings: flag = 1 enc_rating = elgEncrypt(self.encryptObj, pow(self.g, self.ratings[r], self.p), k1) enc_flag = elgEncrypt(self.encryptObj, pow(self.g, flag, self.p), k2) else: flag = 0 enc_rating = elgEncrypt(self.encryptObj, pow(self.g, flag, self.p), k1) enc_flag = elgEncrypt(self.encryptObj, pow(self.g, flag, self.p), k2) return (enc_rating, enc_flag ) # corresponds to M_i^(1) = (E(g^r), E(g^f))
def ChooseA(P): """ This function for choose E. """ while True: a = Crypto_Number.getRandomRange(2, P - 1) if (Module.gcd(a, P) == 1): return a
def enc(self, m): assert m < self.n assert m >= 0 r = getRandomRange(0, self.n) assert GCD(r, self.n) == 1 return pow(self.g, m, self.n2) * pow(r, self.n, self.n2) % self.n2
def sim_round1(self, i, j, avg_ratings): k = number.getRandomRange(2, self.p - 1, Random.new().read) if (i in self.ratings) and (j in self.ratings): # multiply two ratings #print(self.ratings[i]*self.ratings[j]) """ term1 = pow(self.g, int(self.ratings[i]*self.ratings[j]*10), self.p) term2 = pow(self.g, int(avg_ratings[i]*avg_ratings[j]*10), self.p) div1 = pow(self.g, int(self.ratings[i]*avg_ratings[j]*10), self.p) div2 = pow(self.g, int(avg_ratings[i]*self.ratings[j]*10), self.p) div1 = inverse(div1, self.p) div2 = inverse(div2, self.p) #pearson coeff test mult_rating = elgEncrypt(self.encryptObj, pow(self.g, term1*term2*div1*div2, self.p), k) """ mult_rating = elgEncrypt( self.encryptObj, pow(self.g, (self.ratings[i]) * (self.ratings[j]), self.p), k) else: # send 0 mult_rating = elgEncrypt(self.encryptObj, pow(self.g, 0, self.p), k) return mult_rating
def sendRatings(self, num_item): enc_obj = ElGamal.construct((self.p, self.g, self.pubElgamalKey)) encrypted_ratings = {} for i in range(num_item): k = number.getRandomRange(2, self.p - 1, Random.new().read) if i in self.ratings: encrypted_ratings[i] = elgEncrypt( enc_obj, pow(self.g, self.ratings[i] * 100, self.p), k) else: temp = 1 sum_rat = 0 rat_cnt = 0 for r in self.ratings: sum_rat += self.ratings[r] rat_cnt += 1 if rat_cnt == 0: user_avg = 0 else: user_avg = sum_rat / rat_cnt encrypted_ratings[i] = elgEncrypt( enc_obj, pow(self.g, int(user_avg * 100), self.p), k) return encrypted_ratings
def randomingroupstar(self, N): while True: r = number.getRandomRange(3, N, self._randfunc) if 1 == GCD(Integer(r), N): return r