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