-
Notifications
You must be signed in to change notification settings - Fork 21
/
bls.py
51 lines (32 loc) · 961 Bytes
/
bls.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
#!/usr/bin/python
import bn256
"""
Demonstrate the bn256 module using the BLS short signature scheme
"""
def bls_keygen():
k,g = bn256.g2_random()
return (k,g)
def bls_sign(privkey, msg):
pt = bn256.g1_hash_to_point(msg)
assert pt.is_on_curve()
return bn256.g1_compress(pt.scalar_mul(privkey))
def bls_verify(pubkey, msg, csig):
sig = bn256.g1_uncompress(csig)
assert type(pubkey) == bn256.curve_twist
assert type(sig) == bn256.curve_point
msg_pt = bn256.g1_hash_to_point(msg)
assert msg_pt.is_on_curve()
v1 = bn256.optimal_ate(pubkey, msg_pt)
v2 = bn256.optimal_ate(bn256.twist_G, sig)
return v1 == v2
def test():
(priv,pub) = bls_keygen()
import time
for i in range(1000):
msg = ("message @ %f" % time.time()).encode("utf-8")
print(msg)
sig = bls_sign(priv, msg)
print("sig",sig)
ok = bls_verify(pub, msg, sig)
assert ok
test()