def test_simple_elgamal_encryption_decryption(self): nonce = ONE_MOD_Q secret_key = TWO_MOD_Q keypair = get_optional(elgamal_keypair_from_secret(secret_key)) public_key = keypair.public_key self.assertLess(public_key.to_int(), P) elem = g_pow_p(ZERO_MOD_Q) self.assertEqual(elem, ONE_MOD_P) # g^0 == 1 ciphertext = get_optional(elgamal_encrypt(0, nonce, keypair.public_key)) self.assertEqual(G, ciphertext.alpha.to_int()) self.assertEqual( pow(ciphertext.alpha.to_int(), secret_key.to_int(), P), pow(public_key.to_int(), nonce.to_int(), P), ) self.assertEqual( ciphertext.beta.to_int(), pow(public_key.to_int(), nonce.to_int(), P), ) plaintext = ciphertext.decrypt(keypair.secret_key) self.assertEqual(0, plaintext)
def test_elgamal_generated_keypairs_are_within_range( self, keypair: ElGamalKeyPair): self.assertLess(keypair.public_key.to_int(), P) self.assertLess(keypair.secret_key.to_int(), G) self.assertEqual(g_pow_p(keypair.secret_key), keypair.public_key)
def test_cached_one(self): plaintext = int_to_q_unchecked(1) ciphertext = g_pow_p(plaintext) plaintext_again = discrete_log(ciphertext) self.assertEqual(1, plaintext_again)
def test_simple_powers(self): gp = int_to_p(G) self.assertEqual(gp, g_pow_p(ONE_MOD_Q)) self.assertEqual(ONE_MOD_P, g_pow_p(ZERO_MOD_Q))
def test_cached(self, exp: int): plaintext = get_optional(int_to_q(exp)) exp_plaintext = g_pow_p(plaintext) plaintext_again = discrete_log(exp_plaintext) self.assertEqual(exp, plaintext_again)