lightphe 0.0.2__tar.gz → 0.0.3__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.2 → lightphe-0.0.3}/PKG-INFO +29 -9
- {lightphe-0.0.2 → lightphe-0.0.3}/README.md +28 -8
- {lightphe-0.0.2/tests → lightphe-0.0.3/lightphe}/__init__.py +11 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/models/Ciphertext.py +4 -1
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe.egg-info/PKG-INFO +29 -9
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe.egg-info/SOURCES.txt +1 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/setup.py +1 -1
- lightphe-0.0.3/tests/test_cloud.py +44 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/LICENSE +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/commons/__init__.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/commons/logger.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/Benaloh.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/DamgardJurik.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/ElGamal.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/EllipticCurveElGamal.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/GoldwasserMicali.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/NaccacheStern.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/OkamotoUchiyama.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/Paillier.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/RSA.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/cryptosystems/__init__.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/elliptic/Weierstrass.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/elliptic/__init__.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/models/Algorithm.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/models/EllipticCurve.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/models/Homomorphic.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe/models/__init__.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe.egg-info/dependency_links.txt +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe.egg-info/requires.txt +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/lightphe.egg-info/top_level.txt +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/setup.cfg +0 -0
- {lightphe-0.0.2/lightphe → lightphe-0.0.3/tests}/__init__.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_benaloh.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_damgard.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_elgamal.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_ellipticcurveelgamal.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_goldwasser.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_naccache.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_okamoto.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_paillier.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_rsa.py +0 -0
- {lightphe-0.0.2 → lightphe-0.0.3}/tests/test_salary.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.3
|
|
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
|
|
@@ -35,7 +35,15 @@ License-File: LICENSE
|
|
|
35
35
|
|
|
36
36
|
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/), [`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/).
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
# Partially vs Fully Homomorphic Encryption
|
|
39
|
+
|
|
40
|
+
Even though fully homomorphic encryption (FHE) has become available in recent times, but when considering the trade-offs, LightPHE emerges as a more efficient and practical choice. If your specific task doesn't demand the full homomorphic capabilities, opting for partial homomorphism with LightPHE is the logical decision.
|
|
41
|
+
|
|
42
|
+
- 🏎️ Notably faster
|
|
43
|
+
- 💻 Demands fewer computational resources
|
|
44
|
+
- 📏 Generating smaller ciphertexts
|
|
45
|
+
- 🧠 Well-suited for memory-constrained environments
|
|
46
|
+
- ⚖️ Strikes a favorable balance for practical use cases
|
|
39
47
|
|
|
40
48
|
# Installation [](https://pypi.org/project/lightphe/)
|
|
41
49
|
|
|
@@ -118,18 +126,30 @@ m2 = 23
|
|
|
118
126
|
c1 = cs.encrypt(m1)
|
|
119
127
|
c2 = cs.encrypt(m2)
|
|
120
128
|
|
|
121
|
-
#
|
|
122
|
-
|
|
129
|
+
# homomorphic addition - private key is not required!
|
|
130
|
+
c3 = c1 + c2
|
|
131
|
+
|
|
132
|
+
# proof of work
|
|
133
|
+
assert cs.decrypt(c3) == m1 + m2
|
|
123
134
|
```
|
|
124
135
|
|
|
125
|
-
|
|
136
|
+
⚡ Notice that once can perform `c1 + c2` here without holding private key. However, just the data owner with private key can perform encryption and decryption. This is the basic definition of homomorphic encryption.
|
|
137
|
+
|
|
138
|
+
Besides, Paillier is supporting multiplying ciphertexts by a known plain constant. Simply put, decryption of scalar multiplication of ciphertext is equivalent to that constant times plaintext as well.
|
|
126
139
|
|
|
127
140
|
```python
|
|
128
|
-
#
|
|
141
|
+
# increasing something 5%
|
|
129
142
|
k = 1.05
|
|
130
|
-
|
|
143
|
+
|
|
144
|
+
# scalar multiplication - private key is not required!
|
|
145
|
+
c4 = k * c1
|
|
146
|
+
|
|
147
|
+
# proof of work
|
|
148
|
+
assert cs.decrypt(c4) == k * m1
|
|
131
149
|
```
|
|
132
150
|
|
|
151
|
+
⚡ Herein, `k * c1` operation can be performed by anyone without holding private key.
|
|
152
|
+
|
|
133
153
|
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.
|
|
134
154
|
|
|
135
155
|
```python
|
|
@@ -142,11 +162,11 @@ assert cs.decrypt(c1) == m1
|
|
|
142
162
|
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.
|
|
143
163
|
|
|
144
164
|
```python
|
|
145
|
-
# pailier is not homomorphic
|
|
165
|
+
# pailier is not multiplicatively homomorphic
|
|
146
166
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the multiplication"):
|
|
147
167
|
c1 * c2
|
|
148
168
|
|
|
149
|
-
# pailier is not homomorphic
|
|
169
|
+
# pailier is not exclusively homomorphic
|
|
150
170
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the exclusive or"):
|
|
151
171
|
c1 ^ c2
|
|
152
172
|
```
|
|
@@ -19,7 +19,15 @@
|
|
|
19
19
|
|
|
20
20
|
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/), [`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/).
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
# Partially vs Fully Homomorphic Encryption
|
|
23
|
+
|
|
24
|
+
Even though fully homomorphic encryption (FHE) has become available in recent times, but when considering the trade-offs, LightPHE emerges as a more efficient and practical choice. If your specific task doesn't demand the full homomorphic capabilities, opting for partial homomorphism with LightPHE is the logical decision.
|
|
25
|
+
|
|
26
|
+
- 🏎️ Notably faster
|
|
27
|
+
- 💻 Demands fewer computational resources
|
|
28
|
+
- 📏 Generating smaller ciphertexts
|
|
29
|
+
- 🧠 Well-suited for memory-constrained environments
|
|
30
|
+
- ⚖️ Strikes a favorable balance for practical use cases
|
|
23
31
|
|
|
24
32
|
# Installation [](https://pypi.org/project/lightphe/)
|
|
25
33
|
|
|
@@ -102,18 +110,30 @@ m2 = 23
|
|
|
102
110
|
c1 = cs.encrypt(m1)
|
|
103
111
|
c2 = cs.encrypt(m2)
|
|
104
112
|
|
|
105
|
-
#
|
|
106
|
-
|
|
113
|
+
# homomorphic addition - private key is not required!
|
|
114
|
+
c3 = c1 + c2
|
|
115
|
+
|
|
116
|
+
# proof of work
|
|
117
|
+
assert cs.decrypt(c3) == m1 + m2
|
|
107
118
|
```
|
|
108
119
|
|
|
109
|
-
|
|
120
|
+
⚡ Notice that once can perform `c1 + c2` here without holding private key. However, just the data owner with private key can perform encryption and decryption. This is the basic definition of homomorphic encryption.
|
|
121
|
+
|
|
122
|
+
Besides, Paillier is supporting multiplying ciphertexts by a known plain constant. Simply put, decryption of scalar multiplication of ciphertext is equivalent to that constant times plaintext as well.
|
|
110
123
|
|
|
111
124
|
```python
|
|
112
|
-
#
|
|
125
|
+
# increasing something 5%
|
|
113
126
|
k = 1.05
|
|
114
|
-
|
|
127
|
+
|
|
128
|
+
# scalar multiplication - private key is not required!
|
|
129
|
+
c4 = k * c1
|
|
130
|
+
|
|
131
|
+
# proof of work
|
|
132
|
+
assert cs.decrypt(c4) == k * m1
|
|
115
133
|
```
|
|
116
134
|
|
|
135
|
+
⚡ Herein, `k * c1` operation can be performed by anyone without holding private key.
|
|
136
|
+
|
|
117
137
|
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.
|
|
118
138
|
|
|
119
139
|
```python
|
|
@@ -126,11 +146,11 @@ assert cs.decrypt(c1) == m1
|
|
|
126
146
|
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.
|
|
127
147
|
|
|
128
148
|
```python
|
|
129
|
-
# pailier is not homomorphic
|
|
149
|
+
# pailier is not multiplicatively homomorphic
|
|
130
150
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the multiplication"):
|
|
131
151
|
c1 * c2
|
|
132
152
|
|
|
133
|
-
# pailier is not homomorphic
|
|
153
|
+
# pailier is not exclusively homomorphic
|
|
134
154
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the exclusive or"):
|
|
135
155
|
c1 ^ c2
|
|
136
156
|
```
|
|
@@ -217,3 +217,14 @@ class LightPHE:
|
|
|
217
217
|
else:
|
|
218
218
|
raise ValueError(f"unimplemented algorithm - {algorithm_name}")
|
|
219
219
|
return key_size
|
|
220
|
+
|
|
221
|
+
def create_ciphertext_obj(self, ciphertext: Union[int, tuple, list]) -> Ciphertext:
|
|
222
|
+
"""
|
|
223
|
+
Ciphertext objects have keys in addition ciphertext itself to perform
|
|
224
|
+
homomorphic operations.
|
|
225
|
+
Args:
|
|
226
|
+
ciphertext (int or tuple or list): ciphertext content
|
|
227
|
+
Returns:
|
|
228
|
+
Ciphertext
|
|
229
|
+
"""
|
|
230
|
+
return Ciphertext(algorithm_name=self.algorithm_name, keys=self.cs.keys, value=ciphertext)
|
|
@@ -49,7 +49,10 @@ class Ciphertext:
|
|
|
49
49
|
self.cs: Homomorphic = cs
|
|
50
50
|
|
|
51
51
|
def __str__(self) -> str:
|
|
52
|
-
return
|
|
52
|
+
return f"Ciphertext({self.value})"
|
|
53
|
+
|
|
54
|
+
def __repr__(self) -> str:
|
|
55
|
+
return f"Ciphertext({self.value})"
|
|
53
56
|
|
|
54
57
|
def __add__(self, other: "Ciphertext") -> "Ciphertext":
|
|
55
58
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: lightphe
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.3
|
|
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
|
|
@@ -35,7 +35,15 @@ License-File: LICENSE
|
|
|
35
35
|
|
|
36
36
|
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/), [`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/).
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
# Partially vs Fully Homomorphic Encryption
|
|
39
|
+
|
|
40
|
+
Even though fully homomorphic encryption (FHE) has become available in recent times, but when considering the trade-offs, LightPHE emerges as a more efficient and practical choice. If your specific task doesn't demand the full homomorphic capabilities, opting for partial homomorphism with LightPHE is the logical decision.
|
|
41
|
+
|
|
42
|
+
- 🏎️ Notably faster
|
|
43
|
+
- 💻 Demands fewer computational resources
|
|
44
|
+
- 📏 Generating smaller ciphertexts
|
|
45
|
+
- 🧠 Well-suited for memory-constrained environments
|
|
46
|
+
- ⚖️ Strikes a favorable balance for practical use cases
|
|
39
47
|
|
|
40
48
|
# Installation [](https://pypi.org/project/lightphe/)
|
|
41
49
|
|
|
@@ -118,18 +126,30 @@ m2 = 23
|
|
|
118
126
|
c1 = cs.encrypt(m1)
|
|
119
127
|
c2 = cs.encrypt(m2)
|
|
120
128
|
|
|
121
|
-
#
|
|
122
|
-
|
|
129
|
+
# homomorphic addition - private key is not required!
|
|
130
|
+
c3 = c1 + c2
|
|
131
|
+
|
|
132
|
+
# proof of work
|
|
133
|
+
assert cs.decrypt(c3) == m1 + m2
|
|
123
134
|
```
|
|
124
135
|
|
|
125
|
-
|
|
136
|
+
⚡ Notice that once can perform `c1 + c2` here without holding private key. However, just the data owner with private key can perform encryption and decryption. This is the basic definition of homomorphic encryption.
|
|
137
|
+
|
|
138
|
+
Besides, Paillier is supporting multiplying ciphertexts by a known plain constant. Simply put, decryption of scalar multiplication of ciphertext is equivalent to that constant times plaintext as well.
|
|
126
139
|
|
|
127
140
|
```python
|
|
128
|
-
#
|
|
141
|
+
# increasing something 5%
|
|
129
142
|
k = 1.05
|
|
130
|
-
|
|
143
|
+
|
|
144
|
+
# scalar multiplication - private key is not required!
|
|
145
|
+
c4 = k * c1
|
|
146
|
+
|
|
147
|
+
# proof of work
|
|
148
|
+
assert cs.decrypt(c4) == k * m1
|
|
131
149
|
```
|
|
132
150
|
|
|
151
|
+
⚡ Herein, `k * c1` operation can be performed by anyone without holding private key.
|
|
152
|
+
|
|
133
153
|
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.
|
|
134
154
|
|
|
135
155
|
```python
|
|
@@ -142,11 +162,11 @@ assert cs.decrypt(c1) == m1
|
|
|
142
162
|
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.
|
|
143
163
|
|
|
144
164
|
```python
|
|
145
|
-
# pailier is not homomorphic
|
|
165
|
+
# pailier is not multiplicatively homomorphic
|
|
146
166
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the multiplication"):
|
|
147
167
|
c1 * c2
|
|
148
168
|
|
|
149
|
-
# pailier is not homomorphic
|
|
169
|
+
# pailier is not exclusively homomorphic
|
|
150
170
|
with pytest.raises(ValueError, match="Paillier is not homomorphic with respect to the exclusive or"):
|
|
151
171
|
c1 ^ c2
|
|
152
172
|
```
|
|
@@ -8,7 +8,7 @@ with open("requirements.txt", "r", encoding="utf-8") as f:
|
|
|
8
8
|
|
|
9
9
|
setuptools.setup(
|
|
10
10
|
name="lightphe",
|
|
11
|
-
version="0.0.
|
|
11
|
+
version="0.0.3",
|
|
12
12
|
author="Sefik Ilkin Serengil",
|
|
13
13
|
author_email="serengil@gmail.com",
|
|
14
14
|
description="A Lightweight Partially Homomorphic Encryption Library for Python",
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from lightphe.commons.logger import Logger
|
|
2
|
+
from lightphe import LightPHE
|
|
3
|
+
|
|
4
|
+
logger = Logger()
|
|
5
|
+
|
|
6
|
+
# pre-generated 20-bit RSA keys
|
|
7
|
+
PRIVATE = {"private_key": {"d": 30365}, "public_key": {"n": 175501, "e": 101753}}
|
|
8
|
+
PUBLIC = {"public_key": {"n": 175501, "e": 101753}}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_encryption():
|
|
12
|
+
cs = LightPHE(algorithm_name="RSA", keys=PRIVATE)
|
|
13
|
+
|
|
14
|
+
# plaintexts
|
|
15
|
+
m1 = 10000
|
|
16
|
+
c1 = cs.encrypt(m1)
|
|
17
|
+
|
|
18
|
+
# m2 = 1.05 # TODO: add support
|
|
19
|
+
m2 = 5
|
|
20
|
+
c2 = cs.encrypt(m2)
|
|
21
|
+
|
|
22
|
+
assert cs.decrypt(c1) == m1
|
|
23
|
+
assert cs.decrypt(c2) == m2
|
|
24
|
+
|
|
25
|
+
logger.info("✅ Cloud encryption tests done")
|
|
26
|
+
|
|
27
|
+
c3_val = homomorphic_operations(c1.value, c2.value)
|
|
28
|
+
c3 = cs.create_ciphertext_obj(c3_val)
|
|
29
|
+
|
|
30
|
+
assert cs.decrypt(c3) == m1 * m2
|
|
31
|
+
|
|
32
|
+
logger.info("✅ Cloud decryption tests done")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def homomorphic_operations(c1: int, c2: int):
|
|
36
|
+
"""
|
|
37
|
+
One can perform homomorphic operations while he/she does not hold the private key
|
|
38
|
+
"""
|
|
39
|
+
cs = LightPHE(algorithm_name="RSA", keys=PUBLIC)
|
|
40
|
+
c1_obj = cs.create_ciphertext_obj(c1)
|
|
41
|
+
c2_obj = cs.create_ciphertext_obj(c2)
|
|
42
|
+
c3 = c1_obj * c2_obj
|
|
43
|
+
logger.info("✅ Cloud homomorphic operation tests done")
|
|
44
|
+
return c3.value
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|