Skip to content

Xenia101/Homomorphic-Encryption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

20 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Homomorphic Encryption

A Homomorphic Encryption in python

Homomorphic Encryption๋ž€?

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๋กœ ์•”ํ˜ธ๋ฌธ์„ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ์™€ ํ‰๋ฌธ ์ž์ฒด๋ฅผ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๋Š” ๋™์ผ ํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค.

Example

  1. Create public and private key pairs for encryption first
public_key, private_key = paillier.generate_paillier_keypair()
  1. 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>]
  1. 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
  1. Finally decrypt to see the results
>>>private_key.decrypt(a)
0.141592653
>>>private_key.decrypt(b)
305
>>>private_key.decrypt(c)
9.2e-12

References

Python Paillier Documentation Release 1.4.0

Releases

No releases published

Packages

No packages published

Languages