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 encrypted_celsius_to_fahrenheit(public_key: PublicKey, ciphertext: int) -> int: """ Returns an encrypted integer representing 1/10ths of a degree Fahrenheit °F = °C * 1.8 + 32 """ # First multiply the ciphertext by 18. multiplied_by_18 = multiply(public_key, ciphertext, 18) # Now encrypt 320. encrypted_320 = encrypt(public_key, 320) # Finally add them together. The result will need to be divided by 10. return add(public_key, multiplied_by_18, encrypted_320)
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 encrypted_price_calculator( public_key: PublicKey, # a list of (encrypted price, plaintext quantity) tuples encrypted_cart: List[Tuple[int, int]], ) -> int: """ Returns the encrypted sum of multiplying each encrypted price by an unencrypted quantity """ item_subtotals = [ multiply(public_key, price, quantity) for price, quantity in encrypted_cart ] total = encrypt(public_key, 0) for item_subtotal in item_subtotals: total = add(public_key, total, item_subtotal) return total