lightphe 0.0.11__tar.gz → 0.0.22__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. {lightphe-0.0.11 → lightphe-0.0.22}/PKG-INFO +112 -170
  2. lightphe-0.0.22/README.md +258 -0
  3. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/__init__.py +193 -94
  4. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/phe_utils.py +4 -0
  5. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/Benaloh.py +56 -30
  6. lightphe-0.0.22/lightphe/cryptosystems/BonehGohNissim.py +350 -0
  7. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/DamgardJurik.py +1 -7
  8. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/ElGamal.py +1 -4
  9. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/EllipticCurveElGamal.py +36 -36
  10. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/GoldwasserMicali.py +91 -44
  11. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/NaccacheStern.py +107 -100
  12. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/OkamotoUchiyama.py +5 -9
  13. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/Paillier.py +1 -7
  14. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/RSA.py +41 -37
  15. lightphe-0.0.22/lightphe/cryptosystems/SanderYoungYung.py +225 -0
  16. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/models/Algorithm.py +2 -0
  17. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/models/Ciphertext.py +26 -2
  18. lightphe-0.0.22/lightphe/models/Homomorphic.py +98 -0
  19. lightphe-0.0.22/lightphe/models/Tensor.py +389 -0
  20. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/PKG-INFO +112 -170
  21. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/SOURCES.txt +7 -8
  22. lightphe-0.0.22/lightphe.egg-info/requires.txt +3 -0
  23. lightphe-0.0.22/requirements.txt +3 -0
  24. {lightphe-0.0.11 → lightphe-0.0.22}/setup.py +1 -1
  25. lightphe-0.0.22/tests/test_benaloh.py +39 -0
  26. lightphe-0.0.22/tests/test_bonehgohnissim.py +180 -0
  27. lightphe-0.0.22/tests/test_ciphertexts.py +31 -0
  28. lightphe-0.0.22/tests/test_damgard.py +34 -0
  29. lightphe-0.0.22/tests/test_elgamal.py +64 -0
  30. lightphe-0.0.22/tests/test_ellipticcurveelgamal.py +148 -0
  31. {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_exporting_keys.py +1 -1
  32. lightphe-0.0.22/tests/test_goldwasser.py +40 -0
  33. lightphe-0.0.22/tests/test_keys.py +62 -0
  34. lightphe-0.0.22/tests/test_large_keys.py +77 -0
  35. lightphe-0.0.22/tests/test_naccache.py +108 -0
  36. lightphe-0.0.22/tests/test_okamoto.py +34 -0
  37. {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_paillier.py +2 -36
  38. {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_rsa.py +1 -31
  39. lightphe-0.0.22/tests/test_salary.py +24 -0
  40. lightphe-0.0.22/tests/test_sanderyoungyung.py +71 -0
  41. lightphe-0.0.22/tests/test_tensors.py +351 -0
  42. lightphe-0.0.11/README.md +0 -316
  43. lightphe-0.0.11/lightphe/commons/binary_operations.py +0 -147
  44. lightphe-0.0.11/lightphe/elliptic_curve_forms/edwards.py +0 -89
  45. lightphe-0.0.11/lightphe/elliptic_curve_forms/koblitz.py +0 -166
  46. lightphe-0.0.11/lightphe/elliptic_curve_forms/weierstrass.py +0 -120
  47. lightphe-0.0.11/lightphe/models/Curve.py +0 -29
  48. lightphe-0.0.11/lightphe/models/EllipticCurve.py +0 -160
  49. lightphe-0.0.11/lightphe/models/Homomorphic.py +0 -52
  50. lightphe-0.0.11/lightphe/models/Tensor.py +0 -203
  51. lightphe-0.0.11/lightphe.egg-info/requires.txt +0 -2
  52. lightphe-0.0.11/requirements.txt +0 -2
  53. lightphe-0.0.11/tests/__init__.py +0 -0
  54. lightphe-0.0.11/tests/test_benaloh.py +0 -63
  55. lightphe-0.0.11/tests/test_damgard.py +0 -63
  56. lightphe-0.0.11/tests/test_elgamal.py +0 -139
  57. lightphe-0.0.11/tests/test_ellipticcurve.py +0 -75
  58. lightphe-0.0.11/tests/test_ellipticcurveelgamal.py +0 -263
  59. lightphe-0.0.11/tests/test_goldwasser.py +0 -64
  60. lightphe-0.0.11/tests/test_naccache.py +0 -94
  61. lightphe-0.0.11/tests/test_okamoto.py +0 -61
  62. lightphe-0.0.11/tests/test_salary.py +0 -53
  63. lightphe-0.0.11/tests/test_tensors.py +0 -174
  64. {lightphe-0.0.11 → lightphe-0.0.22}/LICENSE +0 -0
  65. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/__init__.py +0 -0
  66. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/logger.py +0 -0
  67. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/__init__.py +0 -0
  68. {lightphe-0.0.11/lightphe/elliptic_curve_forms → lightphe-0.0.22/lightphe/models}/__init__.py +0 -0
  69. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/dependency_links.txt +0 -0
  70. {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/top_level.txt +0 -0
  71. {lightphe-0.0.11 → lightphe-0.0.22}/setup.cfg +0 -0
  72. {lightphe-0.0.11/lightphe/models → lightphe-0.0.22/tests}/__init__.py +0 -0
  73. {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_cloud.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lightphe
3
- Version: 0.0.11
3
+ Version: 0.0.22
4
4
  Summary: A Lightweight Partially Homomorphic Encryption Library for Python
5
5
  Home-page: https://github.com/serengil/LightPHE
6
6
  Author: Sefik Ilkin Serengil
@@ -23,13 +23,12 @@ License-File: LICENSE
23
23
  [![Tests](https://github.com/serengil/LightPHE/actions/workflows/tests.yml/badge.svg)](https://github.com/serengil/LightPHE/actions/workflows/tests.yml)
24
24
  [![License](http://img.shields.io/:license-MIT-green.svg?style=flat)](https://github.com/serengil/LightPHE/blob/master/LICENSE)
25
25
  [![arXiv](https://img.shields.io/badge/arXiv-2408.05219-b31b1b.svg?logo=arXiv)](https://arxiv.org/abs/2408.05219)
26
- <!-- [![DOI](http://img.shields.io/:DOI-10.48550/arXiv.2408.05219-blue.svg?style=flat)](https://arxiv.org/abs/2408.05219) -->
27
26
 
28
27
  [![Blog](https://img.shields.io/:blog-sefiks.com-blue.svg?style=flat&logo=wordpress)](https://sefiks.com)
29
28
  [![YouTube](https://img.shields.io/:youtube-@sefiks-red.svg?style=flat&logo=youtube)](https://www.youtube.com/@sefiks?sub_confirmation=1)
30
29
  [![Twitter](https://img.shields.io/:follow-@serengil-blue.svg?style=flat&logo=x)](https://twitter.com/intent/user?screen_name=serengil)
31
30
 
32
- [![Support me on Patreon](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dserengil%26type%3Dpatrons&style=flat)](https://www.patreon.com/serengil?repo=lightphe)
31
+ [![Patreon](https://img.shields.io/:become-patron-f96854.svg?style=flat&logo=patreon)](https://www.patreon.com/serengil?repo=lightphe)
33
32
  [![GitHub Sponsors](https://img.shields.io/github/sponsors/serengil?logo=GitHub&color=lightgray)](https://github.com/sponsors/serengil)
34
33
  [![Buy Me a Coffee](https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee)](https://buymeacoffee.com/serengil)
35
34
 
@@ -37,7 +36,7 @@ License-File: LICENSE
37
36
 
38
37
  <p align="center"><img src="https://raw.githubusercontent.com/serengil/LightPHE/master/icons/phi.png" width="200" height="240"></p>
39
38
 
40
- LightPHE is a lightweight partially homomorphic encryption library for python. It is a hybrid homomoprhic encryption library wrapping many schemes such as [`RSA`](https://sefiks.com/2023/03/06/a-step-by-step-partially-homomorphic-encryption-example-with-rsa-in-python/), [`ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/), [`Exponential ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/), [`Elliptic Curve ElGamal`](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/) ([`Weierstrass`](https://sefiks.com/2016/03/13/the-math-behind-elliptic-curve-cryptography/), [`Koblitz`](sefiks.com/2016/03/13/the-math-behind-elliptic-curves-over-binary-field/) and [`Edwards`](https://sefiks.com/2018/12/19/a-gentle-introduction-to-edwards-curves/) forms), [`Paillier`](https://sefiks.com/2023/04/03/a-step-by-step-partially-homomorphic-encryption-example-with-paillier-in-python/), [`Damgard-Jurik`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-damgard-jurik-in-python/), [`Okamoto–Uchiyama`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-okamoto-uchiyama-in-python/), [`Benaloh`](https://sefiks.com/2023/10/06/a-step-by-step-partially-homomorphic-encryption-example-with-benaloh-in-python-from-scratch/), [`Naccache–Stern`](https://sefiks.com/2023/10/26/a-step-by-step-partially-homomorphic-encryption-example-with-naccache-stern-in-python/), [`Goldwasser–Micali`](https://sefiks.com/2023/10/27/a-step-by-step-partially-homomorphic-encryption-example-with-goldwasser-micali-in-python/).
39
+ LightPHE is a lightweight partially homomorphic encryption library for python. It is a hybrid homomoprhic encryption library wrapping many schemes such as [`RSA`](https://sefiks.com/2023/03/06/a-step-by-step-partially-homomorphic-encryption-example-with-rsa-in-python/), [`ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/), [`Exponential ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/), [`Elliptic Curve ElGamal`](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/) ([`Weierstrass`](https://sefiks.com/2016/03/13/the-math-behind-elliptic-curve-cryptography/), [`Koblitz`](sefiks.com/2016/03/13/the-math-behind-elliptic-curves-over-binary-field/) and [`Edwards`](https://sefiks.com/2018/12/19/a-gentle-introduction-to-edwards-curves/) forms), [`Paillier`](https://sefiks.com/2023/04/03/a-step-by-step-partially-homomorphic-encryption-example-with-paillier-in-python/), [`Damgard-Jurik`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-damgard-jurik-in-python/), [`Okamoto–Uchiyama`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-okamoto-uchiyama-in-python/), [`Benaloh`](https://sefiks.com/2023/10/06/a-step-by-step-partially-homomorphic-encryption-example-with-benaloh-in-python-from-scratch/), [`Naccache–Stern`](https://sefiks.com/2023/10/26/a-step-by-step-partially-homomorphic-encryption-example-with-naccache-stern-in-python/), [`Goldwasser–Micali`](https://sefiks.com/2023/10/27/a-step-by-step-partially-homomorphic-encryption-example-with-goldwasser-micali-in-python/), [`Sander-Young-Yung`](https://sefiks.com/2026/04/02/a-step-by-step-partially-homomorphic-sander-young-yung-example-in-python/), [`Boneh-Goh-Nissim`](https://sefiks.com/2026/04/02/a-step-by-step-somewhat-homomorphic-encryption-example-with-boneh-goh-nissim-in-python/).
41
40
 
42
41
  # Partially vs Fully Homomorphic Encryption
43
42
 
@@ -68,18 +67,20 @@ from lightphe import LightPHE
68
67
 
69
68
  In summary, LightPHE is covering following algorithms and these are partially homomorphic with respect to the operations mentioned in the following table.
70
69
 
71
- | Algorithm | Multiplicatively<br>Homomorphic | Additively<br>Homomorphic | Multiplication with a Plain Constant | Exclusively<br>Homomorphic | Regeneration<br>of Ciphertext |
72
- | --- | --- | --- | --- | --- | --- |
73
- | RSA | ✅ | ❌ | ❌ | ❌ | ❌ |
74
- | ElGamal | ✅ | ❌ | ❌ | ❌ | ✅ |
75
- | Exponential ElGamal | ❌ | ✅ | ✅ | ❌ | ✅ |
76
- | Elliptic Curve ElGamal | ❌ | ✅ | ✅ | ❌ | ❌ |
77
- | Paillier | ❌ | ✅ | ✅ | ❌ | ✅ |
78
- | Damgard-Jurik | ❌ | ✅ | ✅ | ❌ | ✅ |
79
- | Benaloh | ❌ | ✅ | ✅ | ❌ | ✅ |
80
- | Naccache-Stern | ❌ | ✅ | ✅ | ❌ | ✅ |
81
- | Okamoto-Uchiyama | ❌ | ✅ | ✅ | ❌ | ✅ |
82
- | Goldwasser-Micali | ❌ | ❌ | ❌ | ✅ | ❌ |
70
+ | Algorithm | Multiplicatively<br>Homomorphic | Additively<br>Homomorphic | Scalar Multiplication | Bitwise-XOR Homomorphic | Bitwise-AND Homomorphic | Regeneration<br>of Ciphertext |
71
+ | --- | --- | --- | --- | --- | --- | --- |
72
+ | RSA | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
73
+ | ElGamal | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
74
+ | Exponential ElGamal | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
75
+ | Elliptic Curve ElGamal | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
76
+ | Paillier | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
77
+ | Damgard-Jurik | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
78
+ | Benaloh | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
79
+ | Naccache-Stern | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
80
+ | Okamoto-Uchiyama | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ |
81
+ | Goldwasser-Micali | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ |
82
+ | Sander-Young-Yung | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
83
+ | Boneh-Goh-Nissim | 1️⃣ | ✅ | ✅ | ❌ | ❌ | ✅ |
83
84
 
84
85
  # Building cryptosystem
85
86
 
@@ -96,203 +97,135 @@ algorithms = [
96
97
  "Benaloh",
97
98
  "Naccache-Stern",
98
99
  "Goldwasser-Micali",
99
- "EllipticCurve-ElGamal"
100
+ "EllipticCurve-ElGamal",
101
+ "Sander-Young-Yung",
102
+ "Boneh-Goh-Nissim",
100
103
  ]
101
104
 
102
- phe = LightPHE(algorithm_name = algorithms[0])
103
- ```
104
-
105
- # Encryption & Decryption
106
-
107
- Once you built your cryptosystem, you will be able to encrypt and decrypt messages with the built cryptosystem.
108
-
109
- ```python
110
- # define plaintext
111
- m = 17
112
-
113
- # calculate ciphertext
114
- c = phe.encrypt(m)
115
-
116
- # proof of work
117
- assert phe.decrypt(c) == m
105
+ cs = LightPHE(algorithm_name = algorithms[0])
118
106
  ```
119
107
 
120
108
  # Homomorphic Operations
121
109
 
122
- Once you have the ciphertext, you will be able to perform homomorphic operations on encrypted data without holding private key. For instance, Paillier is homomorphic with respect to the addition. In other words, decryption of the addition of two ciphertexts is equivalent to addition of plaintexts.
123
-
124
- ### On-Prem Encryption
125
-
126
- This code snippet illustrates how to generate a random public-private key pair using the Paillier and encrypt a plaintext pair. The resulting ciphertext pair, c1 and c2, along with the public key, is then sent from the on-premises environment to the cloud.
110
+ The following example demonstrates a simple workflow using LightPHE with an additively homomorphic cryptosystem (e.g. Paillier). First, we build the cryptosystem on-premises and define two plaintext values. Next, we encrypt the plaintexts, which can be done either on-premises or in the cloud, and does not require the private key. Homomorphic operations, such as addition and scalar multiplication, can then be performed on the ciphertexts—these operations can be offloaded to the cloud, leveraging its computational power without revealing the private key or the plaintext. Finally, the results are decrypted on-premises using the private key, verifying that the homomorphic operations on encrypted data produce the expected plaintext results.
127
111
 
128
112
  ```python
129
- def on_premise() -> Tuple[int, int, dict]:
130
- """
131
- Executes on-premise operations: initializes a cryptosystem by generating
132
- a random public-private key pair, then encrypts two plaintext values.
133
-
134
- Returns:
135
- result (tuple): A tuple containing:
136
- - c1 (int): The first ciphertext
137
- - c2 (int): The second ciphertext
138
- - public_key (dict): The public key for the cryptosystem
139
- """
140
- # generate a random private-public key pair
141
- phe = LightPHE(algorithm_name = "Paillier")
142
-
143
- # define plaintexts
144
- m1 = 10000 # base salary in usd
145
- m2 = 500 # wage increase in usd
146
-
147
- # calculate ciphertexts
148
- c1 = phe.encrypt(m1).value
149
- c2 = phe.encrypt(m2).value
150
-
151
- return (c1, c2, phe.cs.public_key)
152
- ```
113
+ # build an additively homomorphic cryptosystem
114
+ cs = LightPHE(algorithm_name = "Paillier")
153
115
 
154
- ### Homomorphic Operations on Cloud
116
+ # define plaintexts
117
+ m1 = 10000 # base salary in usd
118
+ m2 = 500 # wage increase in usd
155
119
 
156
- This code snippet demonstrates how to perform homomorphic addition on the cloud side without using the private key. However, the cloud is unable to decrypt c3 itself, even though it is the one that calculated it.
120
+ # encrypt plaintexts - private key is not required.
121
+ c1 = cs.encrypt(m1)
122
+ c2 = cs.encrypt(m2)
157
123
 
158
- ```python
159
- def cloud(c1: int, c2: int, public_key: dict) -> int:
160
- """
161
- Performs cloud-side operations: reconstructs a cryptosystem using the
162
- provided public key and executes a homomorphic addition on two ciphertexts.
163
-
164
- Args:
165
- c1 (int): The first ciphertext
166
- c2 (int): The second ciphertext
167
- public_key (dict): The public key of an existing cryptosystem
168
- Retunrs:
169
- c3 (int): The resulting ciphertext after homomorphic addition
170
- """
171
- # restore cryptosystem with just the public key
172
- phe = LightPHE(algorithm_name = "Paillier", keys = public_key)
173
-
174
- # cast c1 and c2 to ciphertext objects
175
- c1 = phe.create_ciphertext_obj(c1)
176
- c2 = phe.create_ciphertext_obj(c2)
177
-
178
- # confirm that cloud cannot decrypt c1
179
- with pytest.raises(ValueError, match="You must have private key"):
180
- phe.decrypt(c1)
181
-
182
- # confirm that cloud cannot decrypt c2
183
- with pytest.raises(ValueError, match="You must have private key"):
184
- phe.decrypt(c2)
185
-
186
- # perform homomorphic addition
187
- c3 = c1 + c2
188
-
189
- # confirm that cloud cannot decrypt c3
190
- with pytest.raises(ValueError, match="You must have private key"):
191
- phe.decrypt(c3)
192
-
193
- return c3.value
194
- ```
124
+ # homomorphic addition - private key is not required
125
+ c3 = c1 + c2
195
126
 
196
- ### On-Prem Decryption And Proof of Work
197
-
198
- This code snippet demonstrates the proof of work. Even though c3 was calculated in the cloud by adding c1 and c2, on-premises can validate that its decryption must be equal to the addition of plaintexts m1 and m2.
127
+ # homomorphic scalar multiplication - private key is not required
128
+ k = 1.05 # increase something 5%
129
+ c4 = k * c1
199
130
 
200
- ```python
201
- # proof of work - private key required
202
- assert phe.decrypt(c3) == m1 + m2
131
+ # decryption - private key is required
132
+ assert cs.decrypt(c3) == m1 + m2
133
+ assert cs.decrypt(c4) == k * m1
203
134
  ```
204
135
 
205
- In this homomorphic pipeline, the cloud's computational power was utilized to calculate c3, but it can only be decrypted by the on-premises side. Additionally, while we performed the encryption on the on-premises side, this is not strictly necessary; only the public key is required for encryption. Therefore, encryption can also be performed on the non-premises side. This approach is particularly convenient when collecting data from multiple edge devices and storing all of it in the cloud simultaneously.
136
+ On the other hand, if you adopt a multiplicatively homomorphic cryptosystem (e.g. RSA or ElGamal), you can multiply ciphertexts without revealing the private key or the plaintext.
206
137
 
207
- ### Scalar Multiplication
138
+ ```python
139
+ # build a multiplicatively homomorphic cryptosystem
140
+ cs = LightPHE(algorithm_name = "RSA")
208
141
 
209
- Besides, Paillier is supporting multiplying ciphertexts by a known plain constant. This code snippet demonstrates how to perform scalar multiplication on encrypted data using Paillier homomorphic encryption with the LightPHE library.
142
+ # define plaintexts
143
+ m1 = 17
144
+ m2 = 21
210
145
 
211
- ```python
212
- # increasing something 5%
213
- k = 1.05
146
+ # encrypt plaintexts - private key is not required.
147
+ c1 = cs.encrypt(m1)
148
+ c2 = cs.encrypt(m2)
214
149
 
215
- # scalar multiplication - cloud (private key is not required)
216
- c4 = k * c1
150
+ # homomorphic multiplication
151
+ c3 = c1 * c2
217
152
 
218
- # proof of work on-prem - private key is required
219
- assert phe.decrypt(c4) == k * m1
153
+ # decryption - private key is required
154
+ assert cs.decrypt(c3) == m1 * m2
220
155
  ```
221
156
 
222
157
  ### Ciphertext Regeneration
223
158
 
224
- Similar to the most of additively homomorphic algorithms, Paillier lets you to regenerate ciphertext while you are not breaking its plaintext restoration. You may consider to do this re-generation many times to have stronger ciphertexts.
159
+ The most of additively homomorphic algorithms allow you to regenerate ciphertext while you are not breaking its plaintext restoration. You may consider to do this re-generation many times to have stronger ciphertexts.
225
160
 
226
161
  ```python
227
- c1_prime = phe.regenerate_ciphertext(c1)
162
+ c1_prime = cs.regenerate_ciphertext(c1)
228
163
  assert c1_prime.value != c1.value
229
- assert phe.decrypt(c1_prime) == m1
230
- assert phe.decrypt(c1) == m1
164
+ assert cs.decrypt(c1_prime) == m1
165
+ assert cs.decrypt(c1) == m1
231
166
  ```
232
167
 
233
- ### Unsupported Operations
168
+ ### Elliptic Curve Cryptography
234
169
 
235
- Finally, if you try to perform an operation that algorithm does not support, then an exception will be thrown. For instance, Paillier is not homomorphic with respect to the multiplication or xor. To put it simply, you cannot multiply two ciphertexts. If you enforce this calculation, you will have an exception.
170
+ ECC is a powerful public-key cryptosystem based on the algebraic structure of elliptic curves over finite fields. The library supports 3 elliptic curve forms (weierstrass (default), edwards and koblitz) and 100+ standard elliptic curve configurations.
236
171
 
237
- ```python
238
- # pailier is not multiplicatively homomorphic
239
- with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the multiplication"):
240
- c1 * c2
172
+ In LightPHE, the [Elliptic Curve ElGamal](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/) scheme is implemented, offering a secure and efficient homomorphic encryption option.
241
173
 
242
- # pailier is not exclusively homomorphic
243
- with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the exclusive or"):
244
- c1 ^ c2
174
+ ```python
175
+ forms = ["weierstrass", "edwards", "koblitz"]
176
+ phe = LightPHE(
177
+ algorithm_name="EllipticCurve-ElGamal",
178
+ form="edwards",
179
+ # curve="ed448", # optinally you can specify the curve for given form
180
+ )
245
181
  ```
246
182
 
247
- However, if you tried to multiply ciphertexts with RSA, or xor ciphertexts with Goldwasser-Micali, these will be succeeded because those cryptosystems support those homomorphic operations.
183
+ One of the crucial factors that define the security level of an elliptic curve cryptosystem is the order of the curve. The order of a curve is the number of points on the curve, and it directly influences the strength of the encryption. A higher order typically corresponds to a stronger cryptosystem, making it more resistant to cryptographic attacks.
248
184
 
249
- ### Elliptic Curve Cryptography
185
+ Each curve in LightPHE has a specific order, which is carefully chosen to balance performance and security. By selecting an elliptic curve with a larger order, you increase the security of your cryptographic system, but this may come with a trade-off in computational efficiency. Therefore, choosing the appropriate curve order is a crucial decision based on your application’s security and performance requirements.
250
186
 
251
- ECC is a powerful public-key cryptosystem based on the algebraic structure of elliptic curves over finite fields. The library supports 3 elliptic curve forms (weierstrass, edwards and koblitz) and 100+ standard elliptic curve configurations.
187
+ See [`curves`](https://github.com/serengil/LightECC#supported-curves) page for a list of all supported forms, curves and their details.
252
188
 
253
- Building an elliptic curve with LightPHE is very straightforward. Once the curve is initialized, you can perform operations such as point addition and scalar multiplication.
189
+ ### Vector Embeddings
254
190
 
255
- ```python
256
- from lightphe import ECC
191
+ LightPHE supports homomorphic encryption on vector embeddings. This is useful in privacy-preserving machine learning, secure aggregation, and confidential data processing.
257
192
 
258
- forms = ["weierstrass", "edwards", "koblitz"]
193
+ ```python
194
+ # build an additively homomorphic cryptosystem (e.g. Paillier)
195
+ cs = LightPHE("Paillier")
259
196
 
260
- # construct an elliptic curve
261
- ed = ECC(form_name="edwards")
197
+ # define plain embeddings
198
+ t1 = [1.005, 2.05, 3.6, 4, 4.02, 3.5]
199
+ t2 = [5, 6.2, 7.5, 8.02, 8.02, 4.5]
200
+ t3 = [1.03, 2.04, 3.05, 7.02, 2.01, 1.06]
262
201
 
263
- # Base Point
264
- G = ed.G
202
+ # encrypt embeddings
203
+ c1, c2 = cs.encrypt(t1), cs.encrypt(t2)
265
204
 
266
- _2G = G + G
267
- _3G = _2G + G
268
- _5G = _3G + _2G
205
+ # perform addition of two encrypted embeddings
206
+ c4 = c1 + c2
269
207
 
270
- _20G = 4 * G
271
- _19G = _20G - G
272
- ```
208
+ # perform scalar multiplication on an embedding
209
+ c5 = 3 * c1
273
210
 
274
- In LightPHE, the [Elliptic Curve ElGamal](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/) scheme is implemented, offering a secure and efficient homomorphic encryption option.
211
+ # perform element-wise multiplication between an encrypted embedding and plain embedding
212
+ c6 = c1 * t3
275
213
 
276
- ```python
277
- forms = ["weierstass", "edwards", "koblitz"]
214
+ # encrypted dot product (likewise cosine similarity)
215
+ c7 = c1 @ t3
278
216
 
279
- phe = LightPHE(
280
- algorithm_name="EllipticCurve-ElGamal",
281
- form=forms[1],
282
- )
217
+ # proof of work
218
+ assert np.allclose(cs.decrypt(c4), [a + b for a, b in zip(t1, t2)], rtol=1e-2)
219
+ assert np.allclose(cs.decrypt(c5), [a * 3 for a in t1], rtol=1e-2)
220
+ assert np.allclose(cs.decrypt(c6), [a * b for a, b in zip(t1, t3)], rtol=1e-2)
221
+ assert np.allclose(cs.decrypt(c7)[0], sum([a * b for a, b in zip(t1, t3)]), rtol=1e-2)
283
222
  ```
284
223
 
285
- One of the crucial factors that define the security level of an elliptic curve cryptosystem is the order of the curve. The order of a curve is the number of points on the curve, and it directly influences the strength of the encryption. A higher order typically corresponds to a stronger cryptosystem, making it more resistant to cryptographic attacks.
286
-
287
- Each curve in LightPHE has a specific order, which is carefully chosen to balance performance and security. By selecting an elliptic curve with a larger order, you increase the security of your cryptographic system, but this may come with a trade-off in computational efficiency. Therefore, choosing the appropriate curve order is a crucial decision based on your application’s security and performance requirements.
288
-
289
- See [`curves`](https://github.com/serengil/LightPHE/tree/master/lightphe/elliptic_curve_forms) page for a list of all supported forms, curves and their details.
290
-
291
224
  # Contributing
292
225
 
293
226
  All PRs are more than welcome! If you are planning to contribute a large patch, please create an issue first to get any upfront questions or design decisions out of the way first.
294
227
 
295
- You should be able run `make test` and `make lint` commands successfully before committing. Once a PR is created, GitHub test workflow will be run automatically and unit test results will be available in [GitHub actions](https://github.com/serengil/LightPHE/actions/workflows/tests.yml) before approval. Besides, workflow will evaluate the code with pylint as well.
228
+ You should be able run `make test` and `make lint` commands successfully before committing. Once a PR is created, GitHub test workflow will be run automatically and unit test results will be available in [GitHub actions](https://github.com/serengil/LightPHE/actions/workflows/tests.yml) before approval.
296
229
 
297
230
  # Support
298
231
 
@@ -301,11 +234,11 @@ There are many ways to support a project - starring⭐️ the GitHub repo is jus
301
234
  You can also support this work on [Patreon](https://www.patreon.com/serengil?repo=lightphe), [GitHub Sponsors](https://github.com/sponsors/serengil) or [Buy Me a Coffee](https://buymeacoffee.com/serengil).
302
235
 
303
236
  <a href="https://www.patreon.com/serengil?repo=lightphe">
304
- <img src="https://raw.githubusercontent.com/serengil/LightPHE/master/icons/patreon.png" width="30%" height="30%">
237
+ <img src="https://raw.githubusercontent.com/serengil/LightPHE/master/icons/patreon.png" width="30%">
305
238
  </a>
306
239
 
307
240
  <a href="https://buymeacoffee.com/serengil">
308
- <img src="https://raw.githubusercontent.com/serengil/LightPHE/master/icons/bmc-button.png" width="25%" height="25%">
241
+ <img src="https://raw.githubusercontent.com/serengil/LightPHE/master/icons/bmc-button.png" width="25%">
309
242
  </a>
310
243
 
311
244
  Also, your company's logo will be shown on README on GitHub if you become sponsor in gold, silver or bronze tiers.
@@ -315,13 +248,22 @@ Also, your company's logo will be shown on README on GitHub if you become sponso
315
248
  Please cite LightPHE in your publications if it helps your research. Here is its BibTex entry:
316
249
 
317
250
  ```BibTeX
318
- @misc{serengil2024lightphe,
319
- title = {LightPHE: Integrating Partially Homomorphic Encryption into Python with Extensive Cloud Environment Evaluations},
320
- author = {Serengil, Sefik Ilkin and Ozpinar, Alper},
321
- year = {2024},
322
- publisher = {arXiv},
323
- url = {https://arxiv.org/abs/2408.05219},
324
- doi = {10.48550/arXiv.2408.05219}
251
+ @article{serengil2024lightphe,
252
+ title={LightPHE: Integrating Partially Homomorphic Encryption into Python with Extensive Cloud Environment Evaluations},
253
+ author={Serengil, Sefik Ilkin and Ozpinar, Alper},
254
+ journal={arXiv preprint arXiv:2408.05219},
255
+ note={doi: 10.48550/arXiv.2408.05219. [Online]. Available: \url{https://arxiv.org/abs/2408.05219}},
256
+ year={2025}
257
+ }
258
+ ```
259
+
260
+ ```BibTeX
261
+ @article{serengil2025vectorsimilarity,
262
+ title={Encrypted Vector Similarity Computations Using Partially Homomorphic Encryption: Applications and Performance Analysis},
263
+ author={Serengil, Sefik and Ozpinar, Alper},
264
+ journal={arXiv preprint arXiv:2503.05850},
265
+ note={doi: 10.48550/arXiv.2503.05850. [Online]. Available: \url{https://arxiv.org/abs/2503.05850}},
266
+ year={2025}
325
267
  }
326
268
  ```
327
269