-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest.py
354 lines (309 loc) · 9.61 KB
/
test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#! /usr/bin/env python3
"""
test.py
Written by Geremy Condra
Licensed under GPLv3
Released 11 October 2009
"""
import unittest
from pypbc import *
stored_params = """type a
q 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791
h 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776
r 730750818665451621361119245571504901405976559617
exp2 159
exp1 107
sign1 1
sign0 1
"""
class TestParameters(unittest.TestCase):
def setUp(self): pass
def test_init(self):
try:
# test n-type generation
params = Parameters(n=3559*3571) # type a1
# not tested because it is *crazy* slow on my old machine...
#params = Parameters(n=3559*3571, short=True) # type f
# test s-type generation
params = Parameters(param_string=stored_params) # type a
# test qr-type generation
params = Parameters(qbits=512, rbits=160) # type a
#params = Parameters(qbits=512, rbits=160, short=True) # type e, may be renamed
except Exception:
fail("Could not instantiate parameters")
def test_bad_init(self):
# here we try to make it screw up by calling it incorrectly
# here, param_string and other values are given
try:
Parameters(param_string=stored_params, short=True)
self.fail()
except: pass
try:
Parameters(param_string=stored_params, qbits=512)
self.fail()
except: pass
try:
Parameters(param_string=stored_params, rbits=512)
self.fail()
except: pass
try:
Parameters(param_string=stored_params, n=512)
self.fail()
except: pass
try:
Parameters(param_string=stored_params, rbits=512)
self.fail()
except: pass
#self.assertRaises(Exception, Parameters, "hello world")
self.assertRaises(Exception, Parameters, 1.0)
pass
class TestPairing(unittest.TestCase):
def setUp(self):
self.params = Parameters(n=3559*3571)
def test_init(self):
try:
Pairing(self.params)
except Exception:
self.fail("Could not instantiate pairing")
def test_bad_init(self):
#self.assertRaises(Exception, Pairing)
self.assertRaises(Exception, Pairing, "hello world")
def test_apply(self):
pairing = Pairing(self.params)
e1 = Element(pairing, G1)
e2 = Element(pairing, G2)
try:
e3 = pairing.apply(e1, e2)
except:
self.fail("could not apply pairing.")
def test_bad_apply(self):
pairing = Pairing(self.params)
e1 = Element(pairing, G1)
e2 = Element(pairing, G2)
self.assertRaises(Exception, pairing.apply)
self.assertRaises(Exception, pairing.apply, e1)
self.assertRaises(Exception, pairing.apply, "hi", 1.5)
class TestElement(unittest.TestCase):
def setUp(self):
self.params = Parameters(n=3559*3571)
self.pairing = Pairing(self.params)
def test_init(self):
try:
self.e1 = Element(self.pairing, G1)
self.e2 = Element.zero(self.pairing, G2)
self.e3 = Element.one(self.pairing, G1)
self.e4 = Element.random(self.pairing, G1)
self.e5 = Element(self.pairing, Zr, value=3559)
self.e6 = Element.from_hash(self.pairing, Zr, "hello world!")
print(self.e6)
except Exception:
self.fail("Could not instantiate element")
def test_str(self):
self.e5 = Element(self.pairing, Zr, value=3559)
self.assertEqual("3559", str(self.e5))
def test_bad_init(self):
self.assertRaises(TypeError, Element)
self.assertRaises(TypeError, Element, self.pairing)
self.assertRaises(TypeError, Element, G1)
self.assertRaises(TypeError, Element, "hidey ho", G1)
self.assertRaises(TypeError, Element, self.pairing, "snerk")
try:
Element.random(self.pairing, GT)
self.fail()
except: pass
def test_add(self):
self.e1 = Element(self.pairing, Zr, value=3)
self.e2 = Element(self.pairing, Zr, value=5)
self.e3 = self.e1 + self.e2
self.assertEqual(str(self.e3), "8")
self.e4 = self.e1 + self.e3
def test_sub(self):
self.e1 = Element(self.pairing, Zr, value=3)
self.e2 = Element(self.pairing, Zr, value=5)
self.e3 = self.e2 - self.e1
self.assertEqual(str(self.e3), "2")
def test_mult(self):
self.e1 = Element(self.pairing, Zr, value=3)
self.e2 = Element(self.pairing, Zr, value=5)
self.e3 = self.e1 * self.e2
self.assertEqual(str(self.e3), "15")
try: self.e3 * 5
except: self.fail()
self.e4 = self.pairing.apply(Element.random(self.pairing, G1), Element.random(self.pairing, G2))
self.e5 = self.pairing.apply(Element.random(self.pairing, G1), Element.random(self.pairing, G2))
try: self.e4 * self.e5
except: self.fail()
def test_pow(self):
self.e1 = Element(self.pairing, Zr, value=3)
self.e2 = Element(self.pairing, Zr, value=2)
self.e3 = pow(self.e1, self.e2)
self.assertEqual(str(self.e3), "9")
try:
self.e1**(1,2)
self.fail()
except:
pass
def test_cmp(self):
self.e1 = Element.random(self.pairing, G1)
self.e2 = Element.random(self.pairing, G1)
self.assertEqual(self.e1 == self.e2, False)
self.e3 = Element(self.pairing, Zr, value=36)
self.e4 = Element(self.pairing, Zr, value=36)
self.assertEqual(self.e3, self.e4)
self.e5 = Element.zero(self.pairing, G1);
self.e6 = Element.one(self.pairing, Zr);
self.assertEqual(self.e5 == 0, True)
self.assertEqual(self.e5 == 1, True)
self.assertEqual(self.e6 == 0, False)
self.assertEqual(self.e6 == 1, True)
self.e7 = Element.random(self.pairing, G2)
self.assertEqual(self.e7 == 0, False)
self.assertEqual(self.e7 == 1, False)
try:
self.e5 < self.e6
self.fail()
self.e5 > self.e6
self.fail()
except: pass
def test_neg(self):
self.e1 = Element.random(self.pairing, Zr)
self.e2 = -self.e1
try:
-Element.one(self.pairing, GT)
self.fail()
except: pass
def test_inv(self):
self.e1 = Element.random(self.pairing, Zr)
self.e2 = ~self.e1
try:
~Element.one(self.pairing, GT)
self.fail()
except: pass
def test_bls(self):
stored_params = """type a
q 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791
h 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776
r 730750818665451621361119245571504901405976559617
exp2 159
exp1 107
sign1 1
sign0 1
"""
# this is a test for the BLS short signature system
params = Parameters(param_string=stored_params)
pairing = Pairing(params)
# build the common parameter g
g = Element.random(pairing, G2)
# build the public and private keys
private_key = Element.random(pairing, Zr)
public_key = Element(pairing, G2, value=g**private_key)
# set the magic hash value
hash_value = Element.from_hash(pairing, G1, "hashofmessage")
# create the signature
signature = hash_value**private_key
# build the temps
temp1 = Element(pairing, GT)
temp2 = Element(pairing, GT)
# fill temp1
temp1 = pairing.apply(signature, g)
#fill temp2
temp2 = pairing.apply(hash_value, public_key)
# and again...
temp1 = pairing.apply(signature, g)
# compare
self.assertEqual(temp1 == temp2, True)
# compare to random signature
rnd = Element.random(pairing, G1)
temp1 = pairing.apply(rnd, g)
# compare
self.assertEqual(temp1 == temp2, False)
def test_auth(self):
# taken from paul miller's PBC::Crypt module tutorial
params = Parameters(qbits=512, rbits=160)
pairing = Pairing(params)
p = Element.random(pairing, G2)
s = Element.random(pairing, Zr)
public_key = p**s
# not actually from a hash. I don't really care.
Q_0 = Element.from_hash(pairing, G1, "node_id=22609")
# ditto
Q_1 = Element.from_hash(pairing, G1, "node_id=9073")
d_0 = Q_0**s
d_1 = Q_1**s
k_01_a = pairing.apply(Q_0, d_1)
k_01_b = pairing.apply(d_0, Q_1)
self.assertEqual(k_01_a, k_01_b)
k_10_a = pairing.apply(Q_1, d_0)
k_10_b = pairing.apply(d_1, Q_0)
self.assertEqual(k_10_a, k_10_b)
if __name__ == '__main__':
# unittest.main()
params = Parameters(qbits=128, rbits=100)
pairing = Pairing(params)
P = Element.random(pairing, G1)
Q = Element.random(pairing, G2)
r = Element.random(pairing, Zr)
e = pairing.apply(P, Q)
print("params =", str(params))
print("pairing =", str(pairing))
print("P =", str(P))
PP = Element(pairing, G1, value=str(P))
print("PP =", str(PP))
assert P == PP
print("Q =", str(Q))
print("r =", str(r))
print("e =", str(e))
ee = Element(pairing, GT, value=str(e))
print("ee =", str(ee))
print("len(P) =", len(P))
print("P[0] = 0x%X" % (P[0]))
print("P[1] = 0x%X" % (P[1]))
assert len(P) == 2
print("len(Q) =", len(Q))
print("Q[0] = 0x%X" % (Q[0]))
print("Q[1] = 0x%X" % (Q[1]))
assert len(P) == 2
try:
print("len(r) =", len(r))
assert False
except TypeError:
print(" failed as expected")
print("int(r) = ", int(r))
print("int(r), hex = 0x%X" % (int(r)))
print("len(e) =", len(e))
print("e[0] = 0x%X" % (e[0]))
print("e[1] = 0x%X" % (e[1]))
assert len(P) == 2
assert e == ee
P0 = Element.zero(pairing, G1)
P1 = Element.one(pairing, G1)
print("P0 =", str(P0))
print("P1 =", str(P1))
Q0 = Element.zero(pairing, G2)
Q1 = Element.one(pairing, G2)
print("Q0 =", str(Q0))
print("Q1 =", str(Q1))
rP = P * r
print("rP =", str(rP))
for j in range (0,2):
if j == 0:
set_point_format_compressed()
else:
set_point_format_uncompressed()
for i in range(0, 100):
R = Element.random(pairing, G1)
RR = Element(pairing, G1, str(R))
S = Element.random(pairing, G1)
e = pairing.apply(P, Q)
ee = Element(pairing, GT, value=str(e))
if i < 10:
print ("R =", str(R))
print ("RR =", str(R))
print ("S =", str(S))
print("e =", str(e))
print("ee =", str(ee))
assert RR == R
assert ee == e
# for i in range(0,1000):
# r = Element.random(pairing, Zr)
# print("i, r =", i, str(r))