-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_bpplus.py
89 lines (75 loc) · 2.64 KB
/
test_bpplus.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import bpplus
from dumb25519 import random_point, random_scalar, Scalar, ScalarVector,PointVector
from random import randrange
import unittest
class TestBPPlus(unittest.TestCase):
def test_complete(self):
N = 4 # bit length
M = [1, 2] # aggregation factor for each proof in a batch
# Produce generators at random; in practice these would be public and reproducible
H = random_point()
G = random_point()
Gi = PointVector([random_point() for _ in range(max(M)*N)])
Hi = PointVector([random_point() for _ in range(max(M)*N)])
params = bpplus.RangeParameters(H,G,N,Gi,Hi)
statements = []
proofs = []
masks = []
for j in range(len(M)):
# Generate the witness
openings = []
for i in range(M[j]):
v = Scalar(randrange(1 << params.N))
r = random_scalar()
openings.append(bpplus.CommitmentOpening(v,r))
if i == 0:
masks.append(r if M[j] == 1 else None)
witness = bpplus.RangeWitness(openings)
# Generate the statement
C = PointVector([params.H*openings[i].v + params.G*openings[i].r for i in range(M[j])])
seed = random_scalar() if M[j] == 1 else None
statement = bpplus.RangeStatement(params,C,seed)
statements.append(statement)
# Prove
proof = bpplus.prove(statement,witness)
proofs.append(proof)
# Verify the entire batch
masks_ = bpplus.verify(statements,proofs)
self.assertEqual(masks,masks_)
def test_bad_batch(self):
N = 4 # bit length
M = [1, 2] # aggregation factor for each proof in a batch
# Produce generators at random; in practice these would be public and reproducible
H = random_point()
G = random_point()
Gi = PointVector([random_point() for _ in range(max(M)*N)])
Hi = PointVector([random_point() for _ in range(max(M)*N)])
params = bpplus.RangeParameters(H,G,N,Gi,Hi)
statements = []
proofs = []
masks = []
for j in range(len(M)):
# Generate the witness
openings = []
for i in range(M[j]):
v = Scalar(randrange(1 << params.N))
r = random_scalar()
openings.append(bpplus.CommitmentOpening(v,r))
if i == 0:
masks.append(r if M[j] == 1 else None)
witness = bpplus.RangeWitness(openings)
# Generate the statement
C = PointVector([params.H*openings[i].v + params.G*openings[i].r for i in range(M[j])])
seed = random_scalar() if M[j] == 1 else None
statement = bpplus.RangeStatement(params,C,seed)
statements.append(statement)
# Prove
proof = bpplus.prove(statement,witness)
proofs.append(proof)
# Make one of the proofs invalid
proofs[0].A = random_point()
# Verify the entire batch
with self.assertRaises(ArithmeticError):
bpplus.verify(statements,proofs)
if __name__ == '__main__':
unittest.main()