def count_vote(tally, pk, sk, base): tally = paillier.decrypt(pk, sk, tally) count = [] while tally: count.append(tally % base) tally = tally / base return count
def count_vote(tally, pk, sk, base): tally = paillier.decrypt(pk, sk, tally) count = [] while tally: count.append(tally%base) tally = tally/base return count
def test_decrypt(): for i in range(5): priv, pub = paillier.generate_keypair(64) for j in range(5): pt = long(random.randint(0, 1000000)) ct = paillier.encrypt(pub, pt) assert pt == paillier.decrypt(priv, pub, ct)
def compute_GT(self, sk, pk, T): for t in T: d = paillier.decrypt(sk, pk, t) if d == 1: return 1 elif d == -1: return 0 return 0
def test_encrypt_and_decrypt(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) plaintext = 123 ciphertext = encrypt(public_key, plaintext) assert ciphertext != plaintext decrypted = decrypt(private_key, public_key, ciphertext) assert decrypted == plaintext
def test_e_mul_const(): for i in range(5): priv, pub = paillier.generate_keypair(128) for j in range(5): a = long(random.randint(0, 1000000)) c = paillier.encrypt(pub, a) for n in range(0, 11): cs = paillier.e_mul_const(pub, c, n) s = paillier.decrypt(priv, pub, cs) assert a * n == s
def test_e_add(): for i in range(5): priv, pub = paillier.generate_keypair(128) for j in range(5): a = long(random.randint(0, 1000000)) b = long(random.randint(0, 1000000)) ca, cb = paillier.encrypt(pub, a), paillier.encrypt(pub, b) cs = paillier.e_add(pub, ca, cb) s = paillier.decrypt(priv, pub, cs) assert a + b == s
def test_multiply(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) a = 123 b = 25 expected = (123 * 25) % public_key.n ciphertext_a = encrypt(public_key, a) encrypted_result = multiply(public_key, ciphertext_a, b) result = decrypt(private_key, public_key, encrypted_result) assert result == expected
def getValue(pr, pb, x): """ Returns the decryptred value of x in floating point type """ ret = paillier.decrypt(pr, pb, x.mantissa) for _ in range(abs(x.exponent)): if x.exponent > 0: ret = ret * 10 else: ret = ret / 10 return ret
def test_multiply_by_one(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) a = 123 b = 1 expected = 123 ciphertext_a = encrypt(public_key, a) naive_encrypted_result = ciphertext_a encrypted_result = multiply(public_key, ciphertext_a, b) assert encrypted_result != naive_encrypted_result result = decrypt(private_key, public_key, encrypted_result) assert result == expected
def test_add(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) a = 123 b = 37 expected = (123 + 37) % public_key.n ciphertext_a = encrypt(public_key, a) ciphertext_b = encrypt(public_key, b) encrypted_result = add(public_key, ciphertext_a, ciphertext_b) result = decrypt(private_key, public_key, encrypted_result) assert result == expected
def test_encrypted_celsius_to_fahrenheit(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) temperature_in_celsius = 23 temperature_in_fahrenheit = 73.4 encrypted_input = encrypt(public_key, temperature_in_celsius) encrypted_output = encrypted_celsius_to_fahrenheit(public_key, encrypted_input) decrypted_output = decrypt(private_key, public_key, encrypted_output) # Note that the Paillier cryptosystem only deals with integers and cannot handle # encrypted division because it is only a partial homomorphic encryption scheme scaled_output = decrypted_output / 10 assert scaled_output == temperature_in_fahrenheit
def compute_GT(self, sk, pk, C): scheme = self.cipher_suite.scheme if scheme == "Paillier": for c in C: if paillier.decrypt(sk, pk, c) == 0: return 1 # x > y else: return 0 # x <= y elif scheme == "ElGamal": for c in C: if elgamal.decrypt(sk, c) == 1: return 1 # x > y else: return 0 # x <= y
def alice_round_3(pi2, r, r2, y2, mup, mu, alpha, zeta, zkpb, ka_pub, ka_priv, kb_pub): r = r % dsa.Q c = r2 d = dsa.G w1 = dsa.G w2 = y2 m1 = mup m2 = mu m3 = alpha m4 = zeta if not zkp.pi2_verify(pi2, c, d, w1, w2, m1, m2, m3, m4, zkpb, ka_pub, kb_pub): print "Error PI2 proof" return False s = paillier.decrypt(mu, ka_priv) % dsa.Q return r, s
def test_encrypted_price_calculator(): private_key, public_key = create_key_pair(bit_length=TEST_BIT_LENGTH) cart = [ # (price, quantity) (2000, 1), (120, 5), (1999, 3), ] expected_price = 8597 # Note that the items are somewhat anonymized by the removal of the highly # identifying price information, but the plaintext quantity could still provide # information which could deanonymize the item data encrypted_cart = [(encrypt(public_key, price), quantity) for price, quantity in cart] encrypted_price = encrypted_price_calculator(public_key, encrypted_cart) decrypted_price = decrypt(private_key, public_key, encrypted_price) assert decrypted_price == expected_price
def alice_round_3(pi2, r, r2, y2, mup, mu, alpha, zeta, zkp, ka_priv, kb_pub): n, p, q, g, lmdba, mupaillier = ka_priv ka_pub = (n, g) rf = r[0] % ecdsa.n c = r2 d = ecdsa.G w1 = ecdsa.G w2 = y2 m1 = mup m2 = mu m3 = alpha m4 = zeta if not eczkp.pi2_verify(pi2, c, d, w1, w2, m1, m2, m3, m4, zkp, ka_pub, kb_pub): print "Error: zkp 2 failed" exit(1) s = paillier.decrypt(mu, ka_priv) % ecdsa.n if s == 0: print("signature failed, retry") exit(1) return rf, s
def central_server_oblivious(x): """ :param x: A 2-D array each column correspond to the result from one subsystem :return: x_mean : 1-D array the mean value across subsystems enc_time: float the average time for each subsystem to encrypt its result collect_time: float the time for the central server to compute the average dec_time: float the average time for each subsystem to decrypt the average send back from the central server. """ # Get dimension num_subsys = x.shape[0] num_feature = x.shape[1] # cx intermediate encrypted version of x cx = [] cxn = [] # x_mean : a list, this is the returned value x_mean = [] x_meann = [] # private key and public key, the private key is a pair of prime numbers, each about 512 bits sk, pk = paillier.generate_keypair(512) t0 = time.clock() for i in range(0, num_subsys): for j in range(0, num_feature): if x[i,j] < 0: x[i,j] = -x[i,j] cxn.append(paillier.encrypt(pk, x[i,j])) cx.append(1) else: cxn.append(1) cx.append(paillier.encrypt(pk,x[i,j])) t1 = time.clock() enc_time = t1 - t0 # enc_time, encryption time for all sub-system, the final reported time is for each subsystem, i.e. enc_time/num_subsys t0 = time.clock() for i in range(0, num_subsys): for j in range(0, num_feature): if j == 0: x_mean.append(cx[i*num_feature+j]) x_meann.append(cxn[i*num_feature+j]) else: x_mean[i] = paillier.e_add(pk, x_mean[i],cx[i*num_feature+j]) x_meann[i] = paillier.e_add(pk, x_meann[i],cxn[i*num_feature+j]) t1 = time.clock() collect_time = t1 - t0 # collect_time : time for averaging feature values among users t0 = time.clock() x_mean = [paillier.decrypt(sk,pk,x)/num_feature for x in x_mean] x_meann = [paillier.decrypt(sk,pk,x)/num_feature for x in x_meann] t1 = time.clock() dec_time = t1 - t0 # dec_time : decryption time x_mean = [x_mean[i] - x_meann[i] for i in range(0,len(x_mean))] return (x_mean, enc_time/num_subsys, collect_time, dec_time)
temp_hash = sha.sha256(str(temp_key.n)) registered_voters_public_keys[temp_hash] = temp_key c += 1 if GLOBAL_VERBOSE: print "RSA Public Key Hash Dictionary:" print registered_voters_public_keys print "\n\n" candidates = {} Base = util.ballot_base(len(registered_voters_public_keys)) for i in range(len(util.candidates_list)): candidates[int(2 ** (Base * i))] = util.candidates_list[i] private_key = paillier.PAILLIER_Private("keys/private/homomorphic.private") d = paillier.decrypt(long(open("votes.txt" , "r").read()), private_key) result = paillier.tally(candidates.keys(), d) winners = [] win = max(result.values()) for key in sorted(result.keys()): for k in candidates.keys(): if key == k: if result[key] == win: winners.append(candidates[k]) print "Candidate", candidates[k], "has", result[key], "votes" break print "\n-------------------------------------------------------------------------------" print " "
import paillier as p priv, pub = p.generate_keypair(128) x = p.encrypt(pub, 250) y = p.encrypt(pub, 300) z = p.e_add(pub, x, y) b = p.e_sub(pub, x, y) a = p.decrypt(priv, pub, z) c = p.decrypt(priv, pub, b) print(x) print(y) print(z) print(a) print(c)
v2 = (paillier.encrypt(keys['n'], keys['g'], 72, vm_a)) v3 = (paillier.encrypt(keys['n'], keys['g'], 73, vm_a)) v4 = (paillier.encrypt(keys['n'], keys['g'], 74, vm_b)) v5 = (paillier.encrypt(keys['n'], keys['g'], 75, vm_b)) v6 = (paillier.encrypt(keys['n'], keys['g'], 76, vm_b)) v7 = (paillier.encrypt(keys['n'], keys['g'], 77, vm_b)) # tally the votes utilising pailliers homomorphic addition property # multiply all ciphertexts then mod n**2 print("Votes plaintext:", v1, v2, v3, v4, v5, v6, v7) ciphertext_total = (v1 * v2 * v3 * v4 * v5 * v6 * v7) % keys['n']**2 print("Totalled votes (c):", ciphertext_total) # now VA decrypts the total plaintext_total = paillier.decrypt(keys['ƛ'], keys['μ'], keys['n'], ciphertext_total) print("Decrypted plaintext total:", plaintext_total) # convert plaintext to binary and slice off the python "0b" prefix binary_total = bin(plaintext_total)[2:] # account for missing zeros, will happen if theres a low amount of voters diff = COUNTER_SIZE - (len(binary_total)) if diff != 0: binary_total = "0" * diff + binary_total print("Binary total:", binary_total) # split the bits in half split = int(COUNTER_SIZE / 2) candidate_a_votes = binary_total[:split] candidate_b_votes = binary_total[-split:]