Skip to content

Commit 7404aa0

Browse files
mess up more
1 parent 33cc37f commit 7404aa0

File tree

4 files changed

+360
-51
lines changed

4 files changed

+360
-51
lines changed

scripts/benchmark.py

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,97 @@
77
G2,
88
add,
99
multiply,
10-
pairing,
10+
pairing as _pairing,
1111
)
1212
from py_ecc import bls
13+
from constants import (
14+
data_path,
15+
msg,
16+
domain,
17+
P_G1,
18+
P_G2,
19+
Q_G1,
20+
Q_G2,
21+
)
22+
import json
23+
from eth_utils import (
24+
is_hex,
25+
decode_hex,
26+
)
27+
import timeit
28+
29+
30+
def _is_hex(value):
31+
return isinstance(value, str) and is_hex(value)
32+
33+
34+
def convert(value):
35+
if isinstance(value, list) and _is_hex(value[0]):
36+
return [decode_hex(v) for v in value]
37+
elif _is_hex(value):
38+
return decode_hex(value)
39+
else:
40+
return value
1341

14-
LARGE_NUMBER = 10000
1542

16-
N_VALIDATORS = 100
17-
print("Gen priv keys")
18-
privkeys = [secrets.randbelow(q) for i in range(N_VALIDATORS)]
19-
print("Gen pub keys")
20-
pubkeys = [bls.privtopub(key) for key in privkeys]
21-
msg = b'ab'*32
22-
domain = 5566
23-
print("Signing")
24-
sigs = [bls.sign(message_hash=msg, privkey=key, domain=domain) for key in privkeys]
25-
print("Agg sigs")
26-
agg_sigs = bls.aggregate_signatures(sigs)
27-
print("Agg keys")
28-
agg_keys = bls.aggregate_pubkeys(pubkeys)
43+
with open(data_path) as f:
44+
d = {k: convert(v) for k, v in json.load(f).items()}
2945

30-
P_G1 = multiply(G1, 100)
31-
Q_G1 = multiply(G1, 5566)
46+
pubkeys = d["pubkeys"]
47+
sigs = d["sigs"]
48+
agg_sigs = d["agg_sigs"]
49+
agg_keys = d["agg_keys"]
3250

33-
P_G2 = multiply(G2, 100)
34-
Q_G2 = multiply(G2, 5566)
3551

52+
def bench(func, seconds=2, repeat=3):
53+
stmt = "{0}()".format(func.__name__)
54+
setup = "from __main__ import {0}".format(func.__name__)
55+
timer = timeit.Timer(stmt, setup=setup)
56+
for _ in range(repeat):
57+
total_time = 0
58+
count = 0
59+
while total_time < seconds:
60+
total_time += timer.timeit(1)
61+
count += 1
62+
yield total_time / count, count
3663

37-
def profile(fn):
3864

39-
a = time()
40-
n_sample = fn()
41-
total_time = time() - a
42-
avg_time = total_time/n_sample
43-
print(f"{fn.__name__} avg {avg_time} seconds")
65+
def report(func):
66+
results = "\t".join(
67+
"{0}\tsecs / {1}\ttimes".format(seconds, count)
68+
for seconds, count in bench(func)
69+
)
70+
print(func.__name__, "\t", results)
4471

4572

4673
def adding_G1():
47-
for i in range(LARGE_NUMBER):
48-
add(P_G1, Q_G1)
49-
return LARGE_NUMBER
74+
return add(P_G1, Q_G1)
5075

5176

5277
def adding_G2():
53-
for i in range(LARGE_NUMBER):
54-
add(P_G2, Q_G2)
55-
return LARGE_NUMBER
78+
return add(P_G2, Q_G2)
5679

5780

58-
def _pairing():
59-
ln = int(LARGE_NUMBER/1000)
60-
for i in range(ln):
61-
pairing(P_G2, Q_G1, final_exponentiate=False)
62-
return ln
81+
def pairing():
82+
return _pairing(P_G2, Q_G1, final_exponentiate=False)
83+
6384

6485
def aggregate_keys():
65-
agg_keys = bls.aggregate_pubkeys(pubkeys)
66-
return 1
86+
return bls.aggregate_pubkeys(pubkeys)
87+
88+
89+
def aggregate_sigs():
90+
return bls.aggregate_signatures(sigs)
91+
6792

6893
def bls_verify():
69-
for i in range(10):
70-
bls.verify(msg, agg_keys, agg_sigs, domain)
71-
return 10
94+
return bls.verify(msg, agg_keys, agg_sigs, domain)
95+
7296

7397
if __name__ == '__main__':
74-
profile(adding_G1)
75-
profile(adding_G2)
76-
profile(_pairing)
77-
profile(aggregate_keys)
78-
profile(bls_verify)
79-
# adding_G1 avg 3.800830841064453e-05 seconds
80-
# adding_G2 avg 0.00018580918312072753 seconds
81-
# _pairing avg 0.12307929992675781 seconds
82-
# aggregate_keys avg 0.041207075119018555 seconds
83-
# bls_verify avg 1.0701230764389038 seconds
98+
report(adding_G1)
99+
report(adding_G2)
100+
report(pairing)
101+
report(aggregate_keys)
102+
report(aggregate_sigs)
103+
report(bls_verify)

scripts/constants.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pathlib import Path
2+
import os
3+
from py_ecc.optimized_bls12_381 import (
4+
field_modulus as q,
5+
G1,
6+
G2,
7+
add,
8+
multiply,
9+
pairing,
10+
)
11+
import secrets
12+
13+
14+
data_path = Path(os.path.dirname(__file__)) / "fixtures" / "data.json"
15+
16+
17+
N_VALIDATORS = 100
18+
privkeys = [secrets.randbelow(q) for i in range(N_VALIDATORS)]
19+
20+
msg = b'\xab'*32
21+
domain = 5566
22+
P_G1 = multiply(G1, 100)
23+
Q_G1 = multiply(G1, 5566)
24+
25+
P_G2 = multiply(G2, 100)
26+
Q_G2 = multiply(G2, 5566)

0 commit comments

Comments
 (0)