-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dsum.py
47 lines (40 loc) · 1 KB
/
test_dsum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Test DSum class
"""
import random
from py_ecc import (
optimized_bn128 as bn128,
)
from dsum import DSum
def test_main():
"""
Test DSum class
"""
user_num = 5
pk = []
sk = []
r = [random.randint(0, bn128.field_modulus) for i in range(user_num)]
x = [random.randint(0, bn128.field_modulus) for i in range(user_num)]
k_pk = []
r_pk_2 = []
s_pk = []
dsum = DSum()
# Key generation
for i in range(user_num):
tmp_pk, tmp_sk = dsum.keygen()
pk.append(tmp_pk)
sk.append(tmp_sk)
# All users encrypt data
c_pks = []
for i in range(user_num):
k, c_pk, r2, s, l = dsum.encrypt(
sk[i], r[i], x[i], pk[:i]+pk[i+1:], 12345)
k_pk.append(k)
r_pk_2.append(r2)
s_pk.append(s)
c_pks.append(c_pk)
# Decrypt each ciphertext
for i in range(user_num):
assert dsum.decrypt(r[i], s_pk[:i]+s_pk[i+1:]) == k_pk[i]
assert (sum(x) == sum(c_pks))
print("Test Finish!")