pryvx 2.4.9__py3-none-any.whl → 2.6.0__py3-none-any.whl
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.
- pryvx/__init__.py +2 -1
- pryvx/phe.py +6 -0
- pryvx/rtphe.py +147 -0
- pryvx/smpc.py +19 -44
- {pryvx-2.4.9.dist-info → pryvx-2.6.0.dist-info}/METADATA +1 -1
- {pryvx-2.4.9.dist-info → pryvx-2.6.0.dist-info}/RECORD +9 -8
- {pryvx-2.4.9.dist-info → pryvx-2.6.0.dist-info}/LICENSE +0 -0
- {pryvx-2.4.9.dist-info → pryvx-2.6.0.dist-info}/WHEEL +0 -0
- {pryvx-2.4.9.dist-info → pryvx-2.6.0.dist-info}/top_level.txt +0 -0
pryvx/__init__.py
CHANGED
pryvx/phe.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import random
|
2
2
|
from sympy import mod_inverse, isprime, nextprime
|
3
3
|
import math
|
4
|
+
import hashlib
|
4
5
|
|
5
6
|
def generate_prime(bits=512):
|
6
7
|
while True:
|
@@ -29,6 +30,11 @@ class PHE:
|
|
29
30
|
@staticmethod
|
30
31
|
def encode(value, scale_factor=1000):
|
31
32
|
return int(round(value * scale_factor))
|
33
|
+
|
34
|
+
@staticmethod
|
35
|
+
def hash_to_bigint(data: str) -> int:
|
36
|
+
digest = hashlib.sha256(data.encode()).hexdigest()
|
37
|
+
return int(digest, 16)
|
32
38
|
|
33
39
|
@staticmethod
|
34
40
|
def decode(value, scale_factor=1000):
|
pryvx/rtphe.py
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
import random
|
2
|
+
from sympy import mod_inverse, isprime, nextprime
|
3
|
+
import math
|
4
|
+
import hashlib
|
5
|
+
import secrets
|
6
|
+
|
7
|
+
def prove_dec(pub, c, m, r):
|
8
|
+
"""
|
9
|
+
Non‑interactive PPK:
|
10
|
+
a = g^u · w^n
|
11
|
+
e = H(c || a) (Fiat–Shamir)
|
12
|
+
z = u + e·m (mod n)
|
13
|
+
w' = w · r^e (mod n)
|
14
|
+
π = (a, z, w')
|
15
|
+
"""
|
16
|
+
n, g = pub ; n2 = n*n
|
17
|
+
u = secrets.randbelow(n)
|
18
|
+
w = secrets.randbelow(n)
|
19
|
+
a = (pow(g, u, n2) * pow(w, n, n2)) % n2
|
20
|
+
|
21
|
+
e = int.from_bytes(
|
22
|
+
hashlib.sha256(str(c).encode() + str(a).encode()).digest(),
|
23
|
+
'big') % n
|
24
|
+
|
25
|
+
z = (u + e * m) % n
|
26
|
+
w_ = (w * pow(r, e, n)) % n
|
27
|
+
return a, z, w_
|
28
|
+
|
29
|
+
def verify_dec(pub, c, m, proof):
|
30
|
+
n, g = pub ; n2 = n*n
|
31
|
+
a, z, w_ = proof
|
32
|
+
e = int.from_bytes(
|
33
|
+
hashlib.sha256(str(c).encode() + str(a).encode()).digest(),
|
34
|
+
'big') % n
|
35
|
+
left = (pow(g, z, n2) * pow(w_, n, n2)) % n2
|
36
|
+
right = (pow(c, e, n2) * a) % n2
|
37
|
+
return left == right
|
38
|
+
|
39
|
+
def generate_prime(bits=512):
|
40
|
+
while True:
|
41
|
+
p = nextprime(random.getrandbits(bits))
|
42
|
+
if isprime(p):
|
43
|
+
return p
|
44
|
+
|
45
|
+
def lcm(a, b):
|
46
|
+
return abs(a * b) // math.gcd(a, b)
|
47
|
+
|
48
|
+
class RTPHE:
|
49
|
+
|
50
|
+
@staticmethod
|
51
|
+
def keygen(bits=64):
|
52
|
+
p = generate_prime(bits)
|
53
|
+
q = generate_prime(bits)
|
54
|
+
n = p * q
|
55
|
+
lambda_n = lcm(p - 1, q - 1)
|
56
|
+
g = n + 1
|
57
|
+
mu = mod_inverse((pow(g, lambda_n, n * n) - 1) // n, n)
|
58
|
+
public_key = (n, g)
|
59
|
+
private_key = (lambda_n, mu)
|
60
|
+
return public_key, private_key
|
61
|
+
|
62
|
+
@staticmethod
|
63
|
+
def encrypt(public_key, plaintext, r=None):
|
64
|
+
n, g = public_key
|
65
|
+
if r is None:
|
66
|
+
r = random.randint(1, n - 1)
|
67
|
+
while math.gcd(r, n) != 1:
|
68
|
+
r = random.randint(1, n - 1)
|
69
|
+
n2 = n * n
|
70
|
+
c = (pow(g, plaintext, n2) * pow(r, n, n2)) % n2
|
71
|
+
return c
|
72
|
+
|
73
|
+
@staticmethod
|
74
|
+
def encrypt_with_randomness(public_key, plaintext):
|
75
|
+
r = random.randint(1, public_key[0] - 1)
|
76
|
+
while math.gcd(r, public_key[0]) != 1:
|
77
|
+
r = random.randint(1, public_key[0] - 1)
|
78
|
+
c = RTPHE.encrypt(public_key, plaintext, r)
|
79
|
+
return c, r
|
80
|
+
|
81
|
+
@staticmethod
|
82
|
+
def decrypt(public_key, private_key, ciphertext):
|
83
|
+
n, g = public_key
|
84
|
+
lambda_n, mu = private_key
|
85
|
+
n2 = n * n
|
86
|
+
x = pow(ciphertext, lambda_n, n2)
|
87
|
+
Lx = (x - 1) // n
|
88
|
+
plaintext = (Lx * mu) % n
|
89
|
+
|
90
|
+
if plaintext > n // 2:
|
91
|
+
plaintext -= n
|
92
|
+
return plaintext
|
93
|
+
|
94
|
+
@staticmethod
|
95
|
+
def homomorphic_add(c1, c2, public_key):
|
96
|
+
n2 = public_key[0] ** 2
|
97
|
+
return (c1 * c2) % n2
|
98
|
+
|
99
|
+
@staticmethod
|
100
|
+
def homomorphic_add_plaintext(ciphertext, plaintext, public_key):
|
101
|
+
n, g = public_key
|
102
|
+
n2 = n * n
|
103
|
+
c_plain = pow(g, plaintext, n2)
|
104
|
+
return (ciphertext * c_plain) % n2
|
105
|
+
|
106
|
+
@staticmethod
|
107
|
+
def homomorphic_sub(c1, c2, public_key):
|
108
|
+
n2 = public_key[0] ** 2
|
109
|
+
c2_inv = mod_inverse(c2, n2)
|
110
|
+
return (c1 * c2_inv) % n2
|
111
|
+
|
112
|
+
@staticmethod
|
113
|
+
def homomorphic_sub_plaintext(ciphertext, plaintext, public_key):
|
114
|
+
n, g = public_key
|
115
|
+
n2 = n * n
|
116
|
+
c_plain_neg = pow(g, -plaintext % n, n2)
|
117
|
+
return (ciphertext * c_plain_neg) % n2
|
118
|
+
|
119
|
+
@staticmethod
|
120
|
+
def homomorphic_scalar_mult(ciphertext, scalar, public_key):
|
121
|
+
n2 = public_key[0] ** 2
|
122
|
+
return pow(ciphertext, scalar, n2)
|
123
|
+
|
124
|
+
@staticmethod
|
125
|
+
def homomorphic_div(ciphertext, divisor, public_key):
|
126
|
+
n = public_key[0]
|
127
|
+
divisor_inv = mod_inverse(divisor, n)
|
128
|
+
return RTPHE.homomorphic_scalar_mult(ciphertext, divisor_inv, public_key)
|
129
|
+
|
130
|
+
# ========== 🔐 RANDOMNESS TRACKING HELPERS ==========
|
131
|
+
|
132
|
+
@staticmethod
|
133
|
+
def track_randomness_add(r1, r2, n):
|
134
|
+
return (r1 * r2) % n
|
135
|
+
|
136
|
+
@staticmethod
|
137
|
+
def track_randomness_sub(r1, r2, n):
|
138
|
+
return (r1 * mod_inverse(r2, n)) % n
|
139
|
+
|
140
|
+
@staticmethod
|
141
|
+
def track_randomness_scalar_mult(r, scalar, n):
|
142
|
+
return pow(r, scalar, n)
|
143
|
+
|
144
|
+
@staticmethod
|
145
|
+
def track_randomness_scalar_div(r, divisor, n):
|
146
|
+
divisor_inv = mod_inverse(divisor, n)
|
147
|
+
return pow(r, divisor_inv, n)
|
pryvx/smpc.py
CHANGED
@@ -1,59 +1,34 @@
|
|
1
1
|
import random
|
2
|
-
|
2
|
+
import hashlib
|
3
|
+
|
3
4
|
def getAdditiveShares(secret, N, fieldSize):
|
4
|
-
shares = [random.randrange(fieldSize) for
|
5
|
+
shares = [random.randrange(fieldSize) for _ in range(N-1)]
|
5
6
|
shares.append((secret - sum(shares)) % fieldSize )
|
6
7
|
return shares
|
7
|
-
|
8
|
-
def reconstructSecret(shares, fieldSize):
|
9
|
-
return sum(shares) % fieldSize
|
10
|
-
|
11
8
|
|
12
9
|
class SMPC:
|
13
10
|
def __init__(self) -> None:
|
14
|
-
self.BASE = 10
|
15
|
-
self.PRECISION_INTEGRAL = 8
|
16
|
-
self.PRECISION_FRACTIONAL = 8
|
17
11
|
self.fieldSize = 293973345475167247070445277780365744413
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
def
|
23
|
-
upscaled
|
24
|
-
field_element = upscaled % self.fieldSize
|
25
|
-
return field_element
|
13
|
+
def encode(self, secret, scale_factor=1000):
|
14
|
+
return int(secret * scale_factor)
|
15
|
+
|
16
|
+
def decode(self, upscaled, scale_factor=1000):
|
17
|
+
return upscaled / scale_factor
|
26
18
|
|
27
|
-
def
|
28
|
-
|
29
|
-
rational = upscaled / self.BASE**self.PRECISION_FRACTIONAL
|
30
|
-
return rational
|
19
|
+
def get_field_size(self):
|
20
|
+
return self.fieldSize
|
31
21
|
|
32
|
-
def get_secret_shares(self,
|
33
|
-
|
34
|
-
shares = getAdditiveShares(self.encode(m), N, self.fieldSize)
|
22
|
+
def get_secret_shares(self, secret, N):
|
23
|
+
shares = getAdditiveShares(secret, N, self.fieldSize)
|
35
24
|
return shares
|
36
25
|
|
37
|
-
def
|
38
|
-
|
39
|
-
return
|
40
|
-
|
41
|
-
def multiplication(self):
|
42
|
-
pass
|
43
|
-
|
44
|
-
def difference(self, shares_a, shares_b):
|
45
|
-
difference_shares = [(a - b) % self.fieldSize for a, b in zip(shares_a, shares_b)]
|
46
|
-
return self.decode(reconstructSecret(difference_shares, self.fieldSize))
|
26
|
+
def hash_to_bigint(self, data: str) -> int:
|
27
|
+
digest = hashlib.sha256(data.encode()).hexdigest()
|
28
|
+
return int(digest, 16)
|
47
29
|
|
48
|
-
def
|
49
|
-
|
50
|
-
reconstructed_difference = self.decode(reconstructSecret(difference_shares, self.fieldSize))
|
30
|
+
def reconstruct(self, share):
|
31
|
+
return share % self.fieldSize
|
51
32
|
|
52
|
-
|
53
|
-
|
54
|
-
elif reconstructed_difference > 0:
|
55
|
-
comparison_result = "a is greater than b"
|
56
|
-
else:
|
57
|
-
comparison_result = "a is less than b"
|
58
|
-
|
59
|
-
return comparison_result
|
33
|
+
def additive_reconstruct(self, shares):
|
34
|
+
return sum(shares) % self.fieldSize
|
@@ -1,16 +1,17 @@
|
|
1
|
-
pryvx/__init__.py,sha256=
|
1
|
+
pryvx/__init__.py,sha256=awMTuiYTjZjhLEhLBYUA4-12DmjStBC_ChUKY7rGbUY,137
|
2
2
|
pryvx/fl_client.py,sha256=9MFIlpdLbNemhiXvYPhJlJGZLdA2U8AzfxGuuNHlIaE,1301
|
3
3
|
pryvx/fl_ensemble.py,sha256=lvgvd-zJDSBh-RyMAjTyJZwu0eFiUNXfCU7J2Vk7g0g,715
|
4
4
|
pryvx/fl_server.py,sha256=LJdJkj2BJdXrMczy1qZBHN_fY-ZveH-vHvljwwgDv8k,2039
|
5
5
|
pryvx/gdp.py,sha256=ToLtWNvBKtSs-3Na1pGe_J6dWvBxXhV2raUJkNtKMg4,925
|
6
6
|
pryvx/ldp.py,sha256=WxBQLnZ_b2wNV1rlN7rtKXaQ5waUPPvnRus-4tUnCB8,810
|
7
|
-
pryvx/phe.py,sha256=
|
7
|
+
pryvx/phe.py,sha256=q4MhHVZaRacW_-iIJD3iveogh_xiEcQiAcRVpGH-MyI,3150
|
8
8
|
pryvx/pryvx_pb2.py,sha256=HlwY1zRVFReIaK8WZ8opJ1kd3_PQWSJEx-PZImWYlCM,1621
|
9
9
|
pryvx/pryvx_pb2_grpc.py,sha256=yn4mThL3XY8glPvZ9b1Fmj_c8JtAs1pTNp3m0JV9d3s,3425
|
10
10
|
pryvx/psi.py,sha256=V0BUJwYfiFWhKtEL27q45yxY7U-I7B4olKQhaGbgN50,1346
|
11
|
-
pryvx/
|
12
|
-
pryvx
|
13
|
-
pryvx-2.
|
14
|
-
pryvx-2.
|
15
|
-
pryvx-2.
|
16
|
-
pryvx-2.
|
11
|
+
pryvx/rtphe.py,sha256=e7mKCJfhtvJa-aGpJOrYDsKbEm_zi46sXR1ezrkv264,4293
|
12
|
+
pryvx/smpc.py,sha256=MDHo53sOtz9hdRzKIlzp7qtAsLcBRyMkcaiq0t-ENMg,1051
|
13
|
+
pryvx-2.6.0.dist-info/LICENSE,sha256=pbIMXbaorAIVW-fDch2tvtZRkVA3mz-UnXQqeCf4LDg,1086
|
14
|
+
pryvx-2.6.0.dist-info/METADATA,sha256=wHm2cVmPRlzkz_GSelhNcfSFccCDlI1O2SdZ-ZDQLGM,1066
|
15
|
+
pryvx-2.6.0.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
16
|
+
pryvx-2.6.0.dist-info/top_level.txt,sha256=1iHoeevu_FoFjdPg8HyMlgvHNUm1--9QEpFMaJEa4hw,6
|
17
|
+
pryvx-2.6.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|