def test_proof_eval(iters=1): print "test_proof_eval" keys1 = generate_keys() n1 = keys1['pub'] p1, q1 = keys1['priv'] keys2 = generate_keys() n2 = keys2['pub'] p2, q2 = keys2['priv'] print "test honest model" for i in range(iters): print "i =", i v1 = mpz(random.randint(0, 2**INT_LEN - 1)) C1 = encrypt_gm(v1, n1) v2 = mpz(random.randint(0, 2**INT_LEN - 1)) C2 = encrypt_gm(v2, n2) C12 = encrypt_gm(v1, n2) P_eval, plaintext_and_coins = proof_eval(C1, C2, C12, v1, n1, n2) eval_res = verify_eval(P_eval, plaintext_and_coins, n1, n2) assert (eval_res != None) assert ((v2 <= v1) == compare_leq_honest(eval_res, (p2, q2))) # flip one bit # Doesn't work... """ v1x = v1 ^ (1 << random.randint(0, 30)) C12x = encrypt_gm(v1x, n2) P_eval_x1, plaintext_and_coins_x1 = proof_eval(C1, C2, C12, v1x, n1, n2) P_eval_x2, plaintext_and_coins_x2 = proof_eval(C1, C2, C12x, v1, n1, n2) P_eval_x3, plaintext_and_coins_x3 = proof_eval(C1, C2, C12x, v1x, n1, n2) assert( verify_eval(P_eval_x1, plaintext_and_coins_x1, n1, n2) == None ) assert( verify_eval(P_eval_x2, plaintext_and_coins_x2, n1, n2) == None ) assert( verify_eval(P_eval_x3, plaintext_and_coins_x3, n1, n2) == None ) """ # end for print "test_proof_eval pass"
def test_gm_enc_dec(iters = 1): print "test_gm_enc_dec:" keys = generate_keys() n = keys['pub'] p, q = keys['priv'] for i in range(iters): num = mpz(random.randint(0, 2**INT_LEN-1)) cipher = encrypt_gm(num, n) # ReEncryption for j in range(3): cipher = [c * encrypt_gm(0, n)[0] % n for c in cipher ] decrypted = decrypt_gm(cipher, (p,q)) assert(decrypted != None) assert(decrypted == num) print "test_gm_enc_dec pass"
def compare_eval(self, sid2): cipher1 = self.blockchain.get_bid(self.sid) cipher2 = self.blockchain.get_bid(sid2) n1 = self.blockchain.get_pubkey(self.sid) n2 = self.blockchain.get_pubkey(sid2) cipher12 = encrypt_gm(self.price, n2) P_eval, plaintext_and_coins = proof_eval(cipher1, cipher2, \ cipher12, self.price, \ n1, n2) self.judge.compare_proof_eval(self.sid, sid2, \ P_eval, plaintext_and_coins)
def test_gm_eval_honest(iters=1): print "test_gm_eval_honest" keys = generate_keys() n = keys['pub'] priv_key = keys['priv'] for i in range(iters): v1 = mpz(random.randint(0, 2**INT_LEN - 1)) v2 = mpz(random.randint(0, 2**INT_LEN - 1)) print 'i=', i, 'v1=', v1, 'v2=', v2 cipher2 = encrypt_gm(v2, n) eval_res = gm_eval_honest(v1, cipher2, n) assert ((v2 <= v1) == compare_leq_honest(eval_res, priv_key)) print "test_gm_eval_honest pass"
def gm_eval_honest(number1, cipher2, pub_key2): assert(len(cipher2) == INT_LEN) n = pub_key2 cipher1 = encrypt_gm(number1, n) neg_cipher1 = map(lambda x: x * (n-1) % n, cipher1) c_neg_xor = dot_mod(neg_cipher1, cipher2, n) cipher1_and = embed_and(cipher1, pub_key2) cipher2_and = embed_and(cipher2, pub_key2) neg_cipher1_and = embed_and(neg_cipher1, pub_key2) c_neg_xor_and = embed_and(c_neg_xor, pub_key2) res = [ ] for l in range(INT_LEN): temp = dot_mod(cipher2_and[l], neg_cipher1_and[l], n) for u in range(l): temp = dot_mod(temp, c_neg_xor_and[u], n) res.append(temp) random.shuffle(res) return res
def commit_bid(self, price): self.price = price price_enc = encrypt_gm(price, self.n) self.blockchain.bid(self.sid, price_enc)
def upload_share(self, share, to_sid): n_i = self.blockchain.get_pubkey(to_sid) secret = encrypt_gm(share, n_i) self.blockchain.upload_keyshare(self.sid, to_sid, secret)