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.
- {lightphe-0.0.11 → lightphe-0.0.22}/PKG-INFO +112 -170
- lightphe-0.0.22/README.md +258 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/__init__.py +193 -94
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/phe_utils.py +4 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/Benaloh.py +56 -30
- lightphe-0.0.22/lightphe/cryptosystems/BonehGohNissim.py +350 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/DamgardJurik.py +1 -7
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/ElGamal.py +1 -4
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/EllipticCurveElGamal.py +36 -36
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/GoldwasserMicali.py +91 -44
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/NaccacheStern.py +107 -100
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/OkamotoUchiyama.py +5 -9
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/Paillier.py +1 -7
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/RSA.py +41 -37
- lightphe-0.0.22/lightphe/cryptosystems/SanderYoungYung.py +225 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/models/Algorithm.py +2 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/models/Ciphertext.py +26 -2
- lightphe-0.0.22/lightphe/models/Homomorphic.py +98 -0
- lightphe-0.0.22/lightphe/models/Tensor.py +389 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/PKG-INFO +112 -170
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/SOURCES.txt +7 -8
- lightphe-0.0.22/lightphe.egg-info/requires.txt +3 -0
- lightphe-0.0.22/requirements.txt +3 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/setup.py +1 -1
- lightphe-0.0.22/tests/test_benaloh.py +39 -0
- lightphe-0.0.22/tests/test_bonehgohnissim.py +180 -0
- lightphe-0.0.22/tests/test_ciphertexts.py +31 -0
- lightphe-0.0.22/tests/test_damgard.py +34 -0
- lightphe-0.0.22/tests/test_elgamal.py +64 -0
- lightphe-0.0.22/tests/test_ellipticcurveelgamal.py +148 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_exporting_keys.py +1 -1
- lightphe-0.0.22/tests/test_goldwasser.py +40 -0
- lightphe-0.0.22/tests/test_keys.py +62 -0
- lightphe-0.0.22/tests/test_large_keys.py +77 -0
- lightphe-0.0.22/tests/test_naccache.py +108 -0
- lightphe-0.0.22/tests/test_okamoto.py +34 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_paillier.py +2 -36
- {lightphe-0.0.11 → lightphe-0.0.22}/tests/test_rsa.py +1 -31
- lightphe-0.0.22/tests/test_salary.py +24 -0
- lightphe-0.0.22/tests/test_sanderyoungyung.py +71 -0
- lightphe-0.0.22/tests/test_tensors.py +351 -0
- lightphe-0.0.11/README.md +0 -316
- lightphe-0.0.11/lightphe/commons/binary_operations.py +0 -147
- lightphe-0.0.11/lightphe/elliptic_curve_forms/edwards.py +0 -89
- lightphe-0.0.11/lightphe/elliptic_curve_forms/koblitz.py +0 -166
- lightphe-0.0.11/lightphe/elliptic_curve_forms/weierstrass.py +0 -120
- lightphe-0.0.11/lightphe/models/Curve.py +0 -29
- lightphe-0.0.11/lightphe/models/EllipticCurve.py +0 -160
- lightphe-0.0.11/lightphe/models/Homomorphic.py +0 -52
- lightphe-0.0.11/lightphe/models/Tensor.py +0 -203
- lightphe-0.0.11/lightphe.egg-info/requires.txt +0 -2
- lightphe-0.0.11/requirements.txt +0 -2
- lightphe-0.0.11/tests/__init__.py +0 -0
- lightphe-0.0.11/tests/test_benaloh.py +0 -63
- lightphe-0.0.11/tests/test_damgard.py +0 -63
- lightphe-0.0.11/tests/test_elgamal.py +0 -139
- lightphe-0.0.11/tests/test_ellipticcurve.py +0 -75
- lightphe-0.0.11/tests/test_ellipticcurveelgamal.py +0 -263
- lightphe-0.0.11/tests/test_goldwasser.py +0 -64
- lightphe-0.0.11/tests/test_naccache.py +0 -94
- lightphe-0.0.11/tests/test_okamoto.py +0 -61
- lightphe-0.0.11/tests/test_salary.py +0 -53
- lightphe-0.0.11/tests/test_tensors.py +0 -174
- {lightphe-0.0.11 → lightphe-0.0.22}/LICENSE +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/__init__.py +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/commons/logger.py +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe/cryptosystems/__init__.py +0 -0
- {lightphe-0.0.11/lightphe/elliptic_curve_forms → lightphe-0.0.22/lightphe/models}/__init__.py +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/dependency_links.txt +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/lightphe.egg-info/top_level.txt +0 -0
- {lightphe-0.0.11 → lightphe-0.0.22}/setup.cfg +0 -0
- {lightphe-0.0.11/lightphe/models → lightphe-0.0.22/tests}/__init__.py +0 -0
- {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.
|
|
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
|
[](https://github.com/serengil/LightPHE/actions/workflows/tests.yml)
|
|
24
24
|
[](https://github.com/serengil/LightPHE/blob/master/LICENSE)
|
|
25
25
|
[](https://arxiv.org/abs/2408.05219)
|
|
26
|
-
<!-- [](https://arxiv.org/abs/2408.05219) -->
|
|
27
26
|
|
|
28
27
|
[](https://sefiks.com)
|
|
29
28
|
[](https://www.youtube.com/@sefiks?sub_confirmation=1)
|
|
30
29
|
[](https://twitter.com/intent/user?screen_name=serengil)
|
|
31
30
|
|
|
32
|
-
[](https://www.patreon.com/serengil?repo=lightphe)
|
|
33
32
|
[](https://github.com/sponsors/serengil)
|
|
34
33
|
[](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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
116
|
+
# define plaintexts
|
|
117
|
+
m1 = 10000 # base salary in usd
|
|
118
|
+
m2 = 500 # wage increase in usd
|
|
155
119
|
|
|
156
|
-
|
|
120
|
+
# encrypt plaintexts - private key is not required.
|
|
121
|
+
c1 = cs.encrypt(m1)
|
|
122
|
+
c2 = cs.encrypt(m2)
|
|
157
123
|
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
127
|
+
# homomorphic scalar multiplication - private key is not required
|
|
128
|
+
k = 1.05 # increase something 5%
|
|
129
|
+
c4 = k * c1
|
|
199
130
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
assert
|
|
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
|
-
|
|
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
|
-
|
|
138
|
+
```python
|
|
139
|
+
# build a multiplicatively homomorphic cryptosystem
|
|
140
|
+
cs = LightPHE(algorithm_name = "RSA")
|
|
208
141
|
|
|
209
|
-
|
|
142
|
+
# define plaintexts
|
|
143
|
+
m1 = 17
|
|
144
|
+
m2 = 21
|
|
210
145
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
146
|
+
# encrypt plaintexts - private key is not required.
|
|
147
|
+
c1 = cs.encrypt(m1)
|
|
148
|
+
c2 = cs.encrypt(m2)
|
|
214
149
|
|
|
215
|
-
#
|
|
216
|
-
|
|
150
|
+
# homomorphic multiplication
|
|
151
|
+
c3 = c1 * c2
|
|
217
152
|
|
|
218
|
-
#
|
|
219
|
-
assert
|
|
153
|
+
# decryption - private key is required
|
|
154
|
+
assert cs.decrypt(c3) == m1 * m2
|
|
220
155
|
```
|
|
221
156
|
|
|
222
157
|
### Ciphertext Regeneration
|
|
223
158
|
|
|
224
|
-
|
|
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 =
|
|
162
|
+
c1_prime = cs.regenerate_ciphertext(c1)
|
|
228
163
|
assert c1_prime.value != c1.value
|
|
229
|
-
assert
|
|
230
|
-
assert
|
|
164
|
+
assert cs.decrypt(c1_prime) == m1
|
|
165
|
+
assert cs.decrypt(c1) == m1
|
|
231
166
|
```
|
|
232
167
|
|
|
233
|
-
###
|
|
168
|
+
### Elliptic Curve Cryptography
|
|
234
169
|
|
|
235
|
-
|
|
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
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
189
|
+
### Vector Embeddings
|
|
254
190
|
|
|
255
|
-
|
|
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
|
-
|
|
193
|
+
```python
|
|
194
|
+
# build an additively homomorphic cryptosystem (e.g. Paillier)
|
|
195
|
+
cs = LightPHE("Paillier")
|
|
259
196
|
|
|
260
|
-
#
|
|
261
|
-
|
|
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
|
-
#
|
|
264
|
-
|
|
202
|
+
# encrypt embeddings
|
|
203
|
+
c1, c2 = cs.encrypt(t1), cs.encrypt(t2)
|
|
265
204
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
_5G = _3G + _2G
|
|
205
|
+
# perform addition of two encrypted embeddings
|
|
206
|
+
c4 = c1 + c2
|
|
269
207
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
```
|
|
208
|
+
# perform scalar multiplication on an embedding
|
|
209
|
+
c5 = 3 * c1
|
|
273
210
|
|
|
274
|
-
|
|
211
|
+
# perform element-wise multiplication between an encrypted embedding and plain embedding
|
|
212
|
+
c6 = c1 * t3
|
|
275
213
|
|
|
276
|
-
|
|
277
|
-
|
|
214
|
+
# encrypted dot product (likewise cosine similarity)
|
|
215
|
+
c7 = c1 @ t3
|
|
278
216
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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.
|
|
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%"
|
|
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%"
|
|
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
|
-
@
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
|