dash/python-bindings/benchmark.py
Kittywhiskers Van Gogh 3bc4b00e69 Squashed 'src/dashbls/' content from commit 66ee820fbc
git-subtree-dir: src/dashbls
git-subtree-split: 66ee820fbc9e3b97370db8c164904af48327a124
2022-12-30 00:59:17 +05:30

67 lines
1.5 KiB
Python

# flake8: noqa: E501
import time
import secrets
from blspy import (
AugSchemeMPL,
G1Element,
G2Element,
PrivateKey,
)
def startStopwatch():
return time.perf_counter()
def endStopwatch(test_name, start, numIters):
end_time = time.perf_counter()
duration = end_time - start
print("\n%s\nTotal: %d runs in %0.1f ms\nAvg: %f"
% (test_name, numIters, duration * 1000, duration * 1000 / numIters))
def batch_verification():
numIters = 100000
sig_bytes = []
pk_bytes = []
ms = []
for i in range(numIters):
message = b"%d" % i
sk: PrivateKey = AugSchemeMPL.key_gen(secrets.token_bytes(32))
pk: G1Element = sk.get_g1()
sig: G2Element = AugSchemeMPL.sign(sk, message)
sig_bytes.append(bytes(sig))
pk_bytes.append(bytes(pk))
ms.append(message)
pks = []
start = startStopwatch();
for pk in pk_bytes:
pks.append(G1Element.from_bytes(pk))
endStopwatch("Public key validation", start, numIters);
sigs = []
start = startStopwatch()
for sig in sig_bytes:
sigs.append(G2Element.from_bytes(sig))
endStopwatch("Signature validation", start, numIters);
start = startStopwatch()
aggSig = AugSchemeMPL.aggregate(sigs)
endStopwatch("Aggregation", start, numIters);
start = startStopwatch()
ok = AugSchemeMPL.aggregate_verify(pks, ms, aggSig);
endStopwatch("Batch verification", start, numIters);
if not ok:
print("aggregate_verification failed!")
sys.exit(1)
batch_verification()