This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/test/benchmark/index.ts

158 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-11-13 23:09:13 +00:00
import crypto from "crypto";
2020-11-19 13:22:41 +00:00
import * as blst from "@chainsafe/blst";
2020-11-13 23:09:13 +00:00
import * as herumi from "../../src";
import {runBenchmark} from "./runner";
(async function () {
await herumi.initBLS();
const aggCount = 30;
// verify
runBenchmark<{pk: blst.PublicKey; msg: Uint8Array; sig: blst.Signature}, boolean>({
id: "BLST verify",
prepareTest: () => {
const msg = randomMsg();
const sk = blst.SecretKey.fromKeygen(crypto.randomBytes(32));
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
return {
input: {pk, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pk, msg, sig}) => {
return blst.verify(msg, pk, sig);
},
});
runBenchmark<{pk: herumi.PublicKey; msg: Uint8Array; sig: herumi.Signature}, boolean>({
id: "HERUMI verify",
prepareTest: () => {
const msg = randomMsg();
const keypair = herumi.generateKeyPair();
const pk = keypair.publicKey;
const sig = keypair.privateKey.signMessage(msg);
return {
input: {pk, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pk, msg, sig}) => {
return pk.verifyMessage(sig, msg);
},
});
// Fast aggregate
2020-11-19 13:22:41 +00:00
runBenchmark<{pks: blst.AggregatePublicKey[]; msg: Uint8Array; sig: blst.Signature}, boolean>({
2020-11-13 23:09:13 +00:00
id: "BLST fastAggregateVerify",
prepareTest: () => {
const msg = randomMsg();
const dataArr = range(aggCount).map(() => {
const sk = blst.SecretKey.fromKeygen(crypto.randomBytes(32));
2020-11-19 13:22:41 +00:00
const pk = sk.toAggregatePublicKey();
2020-11-13 23:09:13 +00:00
const sig = sk.sign(msg);
return {pk, sig};
});
const pks = dataArr.map((data) => data.pk);
const aggSig = blst.AggregateSignature.fromSignatures(dataArr.map((data) => data.sig));
const sig = aggSig.toSignature();
return {
input: {pks, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pks, msg, sig}) => {
return blst.fastAggregateVerify(msg, pks, sig);
},
});
runBenchmark<{pks: herumi.PublicKey[]; msg: Uint8Array; sig: herumi.Signature}, boolean>({
id: "HERUMI fastAggregateVerify",
prepareTest: () => {
const msg = randomMsg();
const dataArr = range(aggCount).map(() => {
const keypair = herumi.generateKeyPair();
const pk = keypair.publicKey;
const sig = keypair.privateKey.signMessage(msg);
return {pk, sig};
});
const pks = dataArr.map((data) => data.pk);
const sig = herumi.Signature.aggregate(dataArr.map((data) => data.sig));
return {
input: {pks, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pks, msg, sig}) => {
return sig.verifyAggregate(pks, msg);
},
});
// Aggregate sigs
runBenchmark<blst.PublicKey[], void>({
id: `BLST aggregatePubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => blst.SecretKey.fromKeygen(crypto.randomBytes(32)).toPublicKey()),
};
},
testRunner: (pks) => {
blst.AggregatePublicKey.fromPublicKeys(pks);
},
});
2020-11-14 22:25:22 +00:00
runBenchmark<blst.AggregatePublicKey[], void>({
id: `BLST aggregatePubkeys as jacobian (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => {
const pk = blst.SecretKey.fromKeygen(crypto.randomBytes(32)).toPublicKey();
return blst.AggregatePublicKey.fromPublicKey(pk);
}),
};
},
testRunner: (pks) => {
2020-11-19 13:22:41 +00:00
blst.aggregatePubkeys(pks);
2020-11-14 22:25:22 +00:00
},
});
2020-11-13 23:09:13 +00:00
runBenchmark<herumi.PublicKey[], void>({
id: `HERUMI aggregatePubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => herumi.generateKeyPair().publicKey),
};
},
testRunner: (pks) => {
2020-11-19 00:23:34 +00:00
herumi.PublicKey.aggregate(pks);
2020-11-13 23:09:13 +00:00
},
});
})();
function range(n: number): number[] {
const nums: number[] = [];
for (let i = 0; i < n; i++) nums.push(i);
return nums;
}
function randomMsg(): Uint8Array {
return Uint8Array.from(crypto.randomBytes(32));
}