A Homomorphic Encryption in python
Homomorphic encryption is a form of encryption that allows computation on ciphertexts, generating an encrypted result which, when decrypted, matches the result of the operations as if they had been performed on the plaintext. WIKIPEDIA
ํ๋ฌธ m1, m2์ ๋ํ ์ํธ๋ฌธ c1, c2๊ฐ ์์๋, m1+m2 ๋ฅผ ๊ณ์ฐํ๋ค๊ณ ํ๋ค. ์ด๋, ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก๋ ์ํธ๋ฌธ c1๊ณผ c2๋ฅผ ๋ณตํธํ ํ์ฌ ํ๋ฌธ m1,m2๋ฅผ ์ป๊ณ m1+m1์ ๊ณ์ฐํ ๋ค์ ๋ค์ ์ํธํํ์ฌ m1+m2์ ๊ฒฐ๊ณผ๋ฌผ์ ์ป์ ์ ์๋ค. ํ์ง๋ง Homomorphic Encryption์ ๊ฒฝ์ฐ ์ด๋ฌํ ๊ณผ์ ๋ค์ ๋น๋ฐํค ์์ด ๊ณ์ฐ์ ์ํํ ์ ์๋ค. ์ฆ, ๋ณตํธํ์ ๊ณผ์ ์ ๊ฑฐ์น์ง์๊ณ ๊ณ์ฐ์ ์ํํ ์ ์๋ค.
ํ๋ฌธ์ด 10 + 15 = 25 ์ผ๋, 10๊ณผ 15๋ฅผ ์ํธํ๋ฅผ ์งํํ๊ฒ ๋๋ฉด 10 mod4 = 2, 10 mod7 = 3 ์ 15 mod4 = 3, 15 mod7 = 1 ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๊ฒ ๋๋ค. mod4๋ฅผ ํ ๊ฒฐ๊ณผ์ mod7์ ํ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ฐ ๋ง์ ์ฐ์ฐ์ ์ํํ๊ฒ ๋๋ฉด 5์ 4๊ฐ ๋์จ๋ค. ์ด๋ฅผ mod4 ์ mod 7์ ์ฐ์ฐํ 1๊ณผ 4๋ ํ๋ฌธ์ ์ฐ์ฐํ 25๋ฅผ mod4๊ณผ mod7๋ก ์ํธํํ ๊ฒฐ๊ณผ์ธ 1๊ณผ 4๋ก ์ํธ๋ฌธ์ ์ฐ์ฐํ ๊ฒฐ๊ณผ์ ํ๋ฌธ ์์ฒด๋ฅผ ์ฐ์ฐํ ๊ฒฐ๊ณผ๋ ๋์ผ ํ๊ฒ ๋์จ๋ค.
- Create public and private key pairs for encryption first
public_key, private_key = paillier.generate_paillier_keypair()- Perform encryption with the desired plaintext
secret_number_list = [3.141592653, 300, -4.6e-12]
encrypted_number_list = [public_key.encrypt(x) for x in secret_number_list]
>>>encrypted_number_list
[<phe.paillier.EncryptedNumber object at 0x000001BB6990AC88>,
<phe.paillier.EncryptedNumber object at 0x000001BB6991D630>,
<phe.paillier.EncryptedNumber object at 0x000001BB6991D7F0>]- Perform operations in encrypted state
a = encrypted_number_list[0]-3 # 0.141592653
b = encrypted_number_list[1]+5 # 305
c = encrypted_number_list[2]*-2 # 9.2e-12- Finally decrypt to see the results
>>>private_key.decrypt(a)
0.141592653
>>>private_key.decrypt(b)
305
>>>private_key.decrypt(c)
9.2e-12