math/sigma_test.sage
sigma_test.sageRaw
1import unittest, operator
2load("sigma.sage")
3
4# Tests for Sigma protocol & OR proofs
5
6
7# ethereum elliptic curve
8p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
9a = 0
10b = 7
11F = GF(p)
12E = EllipticCurve(F, [a,b])
13GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
14GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
15g = E(GX,GY)
16n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
17h = 1
18q = g.order()
19assert is_prime(p)
20assert is_prime(q)
21
22class TestSigmaProtocol(unittest.TestCase):
23 def test_interactive(self):
24 alice = Prover_interactive(F, g)
25
26 # Alice generates witness w & statement X
27 X = alice.new_key()
28 assert X == alice.g * int(alice.w)
29
30 # Alice generates the commitment A
31 A = alice.new_commitment()
32 assert A == alice.g * int(alice.a)
33
34 # Bob generates the challenge (and stores A)
35 bob = Verifier_interactive(F, g)
36 c = bob.new_challenge(A)
37
38 # Alice generates the proof
39 z = alice.gen_proof(c)
40
41 # Bob verifies the proof
42 assert bob.verify(X, z)
43
44 # check with the generic_verify function
45 assert generic_verify(g, X, A, c, z)
46
47 def test_non_interactive(self):
48 alice = Prover(F, g)
49
50 # Alice generates witness w & statement X
51 X = alice.new_key()
52 assert X == alice.g * int(alice.w)
53
54 # Alice generates the proof
55 A, z = alice.gen_proof(X)
56
57 # Bob generates the challenge
58 bob = Verifier(F, g)
59
60 # Bob verifies the proof
61 assert bob.verify(X, A, z)
62
63 # check with the generic_verify function
64 c = hash_two_points(A, X)
65 assert generic_verify(g, X, A, c, z)
66
67 def test_simulator(self):
68 sim = Simulator(F, g)
69
70 # set a public key X, for which we don't know w
71 unknown_w = 3
72 X = g * unknown_w
73
74 # simulate for X
75 A, c, z = sim.simulate(X)
76
77 # verify the simulated proof
78 assert generic_verify(g, X, A, c, z)
79
80class TestORProof(unittest.TestCase):
81 def test_2_parties(self):
82 # set a public key X, for which we don't know w
83 unknown_w = 3
84 X_1 = g * unknown_w
85
86 alice = ORProver_2parties(F, g)
87
88 # Alice generates key pair
89 X = alice.new_key()
90 Xs = [X, X_1]
91 # Alice generates commitments (internally running the simulator)
92 As = alice.gen_commitments(Xs)
93
94 # Bob generates the challenge (and stores As)
95 bob = ORVerifier_2parties(F, g)
96 s = bob.new_challenge(As)
97
98 # Alice generates the ORproof
99 cs, zs = alice.gen_proof(s)
100
101 # Bob verifies the proofs
102 bob.verify(Xs, cs, zs)
103
104 def test_n_parties(self):
105 # set n public keys X, for which we don't know w
106 Xs = []
107 for i in range(10):
108 X_i = g * i
109 Xs.append(X_i)
110
111 alice = ORProver(F, g)
112
113 # Alice generates key pair
114 X = alice.new_key()
115 Xs.insert(0, X) # add X at the begining of Xs array
116
117 # Alice generates commitments (internally running the simulator)
118 As = alice.gen_commitments(Xs)
119
120 # Bob generates the challenge (and stores As)
121 bob = ORVerifier(F, g)
122 s = bob.new_challenge(As)
123
124 # Alice generates the ORproof
125 cs, zs = alice.gen_proof(s)
126
127 # Bob verifies the proofs
128 bob.verify(Xs, cs, zs)
129
130if __name__ == '__main__':
131 unittest.main()