research/zksnark/bn128_pairing_test.py

30 lines
1007 B
Python
Raw Normal View History

2017-02-20 11:32:38 -05:00
from optimized_pairing import pairing, neg, G2, G1, multiply, FQ12, curve_order
# from bn128_pairing import pairing, neg, G2, G1, multiply, FQ12, curve_order
2017-02-20 11:32:38 -05:00
import time
2016-12-24 20:01:01 -05:00
2017-02-20 11:32:38 -05:00
a = time.time()
print('Starting tests')
2016-12-24 20:01:01 -05:00
p1 = pairing(G2, G1)
pn1 = pairing(G2, neg(G1))
assert p1 * pn1 == FQ12.one()
2017-02-07 08:39:28 -05:00
print('Pairing check against negative in G1 passed')
2016-12-24 20:01:01 -05:00
np1 = pairing(neg(G2), G1)
assert p1 * np1 == FQ12.one()
assert pn1 == np1
2017-02-07 08:39:28 -05:00
print('Pairing check against negative in G2 passed')
2016-12-24 20:01:01 -05:00
assert p1 ** curve_order == FQ12.one()
2017-02-07 08:39:28 -05:00
print('Pairing output has correct order')
2016-12-24 20:01:01 -05:00
p2 = pairing(G2, multiply(G1, 2))
assert p1 * p1 == p2
2017-02-07 08:39:28 -05:00
print('Pairing bilinearity in G1 passed')
2016-12-24 20:01:01 -05:00
assert p1 != p2 and p1 != np1 and p2 != np1
2017-02-07 08:39:28 -05:00
print('Pairing is non-degenerate')
2016-12-24 20:01:01 -05:00
po2 = pairing(multiply(G2, 2), G1)
assert p1 * p1 == po2
2017-02-07 08:39:28 -05:00
print('Pairing bilinearity in G2 passed')
2016-12-24 20:01:01 -05:00
p3 = pairing(multiply(G2, 27), multiply(G1, 37))
po3 = pairing(G2, multiply(G1, 999))
assert p3 == po3
2017-02-07 08:39:28 -05:00
print('Composite check passed')
2017-02-20 11:32:38 -05:00
print('Total time: %.3f' % (time.time() - a))