def setUp(self): self.tp = ThresholdParameters(3, 5) self.kp = ThresholdCrypto.static_512_key_parameters() self.pk, self.shares = ThresholdCrypto.create_public_key_and_shares_centralized( self.kp, self.tp) self.em = ThresholdCrypto.encrypt_message('Some secret message', self.pk) self.reconstruct_shares = [self.shares[i] for i in [0, 2, 4] ] # choose 3 of 5 key shares self.partial_decryptions = [ ThresholdCrypto.compute_partial_decryption(self.em, share) for share in self.reconstruct_shares ]
def test_key_encryption_decryption_without_enough_shares(self): r = number.getRandomRange(2, self.kp.q) testkey_element = pow(self.kp.g, r, self.kp.p) g_k, c = ThresholdCrypto._encrypt_key_element(testkey_element, self.pk) em = EncryptedMessage(g_k, c, '') reconstruct_shares = [self.shares[i] for i in [0, 4]] # choose 2 of 5 key shares partial_decryptions = [ ThresholdCrypto.compute_partial_decryption(em, share) for share in reconstruct_shares ] rec_testkey_element = ThresholdCrypto._combine_shares( partial_decryptions, em, self.tp, self.kp) self.assertNotEqual(testkey_element, rec_testkey_element)
def test_static_2048key_parameters(self): kp = ThresholdCrypto.static_2048_key_parameters() self.assertEqual(kp.p, 2 * kp.q + 1) # safe prime self.assertEqual(pow(kp.g, kp.q, kp.p), 1) # g generates q order subgroup self.assertNotEqual(pow(kp.g, 2, kp.p), 1)
def test_complete_process_with_enough_shares(self): key_params = ThresholdCrypto.static_512_key_parameters() thresh_params = ThresholdParameters(3, 5) pub_key, key_shares = ThresholdCrypto.create_public_key_and_shares_centralized( key_params, thresh_params) message = 'Some secret message to be encrypted!' encrypted_message = ThresholdCrypto.encrypt_message(message, pub_key) reconstruct_shares = [key_shares[i] for i in [0, 2, 4]] # choose 3 of 5 key shares partial_decryptions = [ ThresholdCrypto.compute_partial_decryption(encrypted_message, share) for share in reconstruct_shares ] decrypted_message = ThresholdCrypto.decrypt_message( partial_decryptions, encrypted_message, thresh_params, key_params) self.assertEqual(message, decrypted_message)
def threshold(k, n): """ generates (k, n) threshold system (k secret keys out of n needed to decrypt the message) returns ThresholdKeys object storing pkT (election public key and n key shares (sk_i)) """ thresh_params = ThresholdParameters(k, n) pub_key, key_shares = ThresholdCrypto.create_public_key_and_shares_centralized( parameters, thresh_params) PK = El(type_G, pub_key.g_a) key_shares = [ El(type_Z, key_shares[i].y, i + 1) for i in range(len(key_shares)) ] threshold_keys = ThresholdKeys(PK, key_shares) return threshold_keys
def test_central_key_generation(self): pk, shares = ThresholdCrypto.create_public_key_and_shares_centralized( self.kp, self.tp) self.assertEqual(pk.key_parameters, self.kp) self.assertEqual(len(shares), self.tp.n)
from threshold_crypto import (ThresholdCrypto, ThresholdParameters) # Generate parameters, public key and shares key_params = ThresholdCrypto.static_2048_key_parameters() thresh_params = ThresholdParameters(3, 5) pub_key, key_shares = ThresholdCrypto.create_public_key_and_shares_centralized( key_params, thresh_params) # encrypt message using the public key message = 'Some secret message to be encrypted!' encrypted_message = ThresholdCrypto.encrypt_message(message, pub_key) # build partial decryptions of three share owners using their shares reconstruct_shares = [key_shares[i] for i in [0, 2, 4]] partial_decryptions = [ ThresholdCrypto.compute_partial_decryption(encrypted_message, share) for share in reconstruct_shares ] # combine these partial decryptions to recover the message decrypted_message = ThresholdCrypto.decrypt_message(partial_decryptions, encrypted_message, thresh_params, key_params) print(decrypted_message)
def elgamal_enc_threshold(pk, message): encrypted = ThresholdCrypto.encrypt_message(pk, message) cipher = ThresholdCipher(encrypted.v, encrypted.c, encrypted.enc) return cipher