-
Notifications
You must be signed in to change notification settings - Fork 0
/
aone.py
51 lines (44 loc) · 1.32 KB
/
aone.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
48
49
50
51
"""
Paper: "Dynamic Decentralized Functional Encryption" (https://eprint.iacr.org/2020/197)
Function: Rate-1 All-or-Nothing Encapsulation(AoNE)
"""
import random
from py_ecc import (
optimized_bn128 as bn128,
)
class AoNE:
"""AoNE class implementation"""
def keygen(self):
"""
Key Generation
KeyGen() -> (pk,sk_pk)
"""
t = random.randint(0, bn128.field_modulus)
return (bn128.multiply(bn128.G2, t), t)
def encrypt(self, sk, r, x, pk, label):
"""
Encryption
Encrypt(sk_pk, m) -> ct_pk
ct_pk = (c_pk, [r_pk]_2, S_{pk, U_M, l}, U_M, l)
"""
# compute symmetric key K
h_1 = bn128.multiply(bn128.G1, label)
sum_pk = pk[0]
for i in range(1, len(pk)):
sum_pk = bn128.add(sum_pk, pk[i])
k = bn128.pairing(bn128.multiply(sum_pk, r), h_1)
s = bn128.multiply(h_1, sk)
# TODO: encrypt x with k
return k, x, bn128.multiply(bn128.G2, r), s, label
def decrypt(self, r, s):
"""
Decryption
Decrypt(ct) -> x
"""
sum_pk = s[0]
for i in range(1, len(s)):
sum_pk = bn128.add(sum_pk, s[i])
g2 = bn128.multiply(bn128.G2, r)
k = bn128.pairing(g2, sum_pk)
# TODO: decrypt ct with k
return k