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/benchmark/index.ts

167 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-11-13 23:09:13 +00:00
import {runBenchmark} from "./runner";
2020-12-01 08:29:19 +00:00
import {runForAllImplementations} from "../test/switch";
2020-12-03 09:38:13 +00:00
import {PublicKey, Signature, SecretKey} from "../src/interface";
2020-12-01 08:29:19 +00:00
import {range, randomMessage} from "../test/util";
2020-12-02 20:45:29 +00:00
import {aggCount, runs} from "./params";
2020-11-13 23:09:13 +00:00
2020-11-30 22:05:19 +00:00
(async function () {
await runForAllImplementations(async (bls, implementation) => {
// verify
2020-11-13 23:09:13 +00:00
2020-11-30 23:53:40 +00:00
await runBenchmark<{pk: PublicKey; msg: Uint8Array; sig: Signature}, boolean>({
2020-11-30 22:05:19 +00:00
id: `${implementation} verify`,
2020-11-13 23:09:13 +00:00
2020-11-30 22:05:19 +00:00
prepareTest: () => {
const msg = randomMessage();
2020-11-30 18:01:13 +00:00
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
2020-11-25 16:09:44 +00:00
const sig = sk.sign(msg);
2020-11-30 22:05:19 +00:00
return {
input: {pk, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pk, msg, sig}) => {
return sig.verify(pk, msg);
},
2020-12-02 20:45:29 +00:00
runs,
2020-11-30 22:05:19 +00:00
});
// Fast aggregate
2020-11-30 23:53:40 +00:00
await runBenchmark<{pks: PublicKey[]; msg: Uint8Array; sig: Signature}, boolean>({
2020-12-02 20:45:29 +00:00
id: `${implementation} verifyAggregate (${aggCount})`,
2020-11-30 22:05:19 +00:00
prepareTest: () => {
const msg = randomMessage();
const dataArr = range(aggCount).map(() => {
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
return {pk, sig};
});
const pks = dataArr.map((data) => data.pk);
const sig = bls.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);
},
2020-12-02 20:45:29 +00:00
runs,
2020-11-30 22:05:19 +00:00
});
2020-12-02 20:54:46 +00:00
// Verify multiple
await runBenchmark<{pks: PublicKey[]; msgs: Uint8Array[]; sig: Signature}, boolean>({
id: `${implementation} verifyMultiple (${aggCount})`,
prepareTest: () => {
const dataArr = range(aggCount).map(() => {
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const msg = randomMessage();
const sig = sk.sign(msg);
return {pk, msg, sig};
});
const pks = dataArr.map((data) => data.pk);
const msgs = dataArr.map((data) => data.msg);
const sig = bls.Signature.aggregate(dataArr.map((data) => data.sig));
return {
input: {pks, msgs, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pks, msgs, sig}) => {
return sig.verifyMultiple(pks, msgs);
},
runs,
});
2020-12-02 23:21:48 +00:00
// Verify multiple signatures
2021-04-05 16:03:49 +00:00
await runBenchmark({
2020-12-02 23:21:48 +00:00
id: `${implementation} verifyMultipleSignatures (${aggCount})`,
prepareTest: () => {
2021-04-05 16:03:49 +00:00
const sets = range(aggCount).map(() => {
2020-12-02 23:21:48 +00:00
const sk = bls.SecretKey.fromKeygen();
const pk = sk.toPublicKey();
const msg = randomMessage();
const sig = sk.sign(msg);
2021-04-05 16:03:49 +00:00
return {publicKey: pk, message: msg, signature: sig};
2020-12-02 23:21:48 +00:00
});
return {
2021-04-05 16:03:49 +00:00
input: sets,
2020-12-02 23:21:48 +00:00
resultCheck: (valid) => valid === true,
};
},
2021-04-05 16:03:49 +00:00
testRunner: (sets) => {
return bls.Signature.verifyMultipleSignatures(sets);
2020-12-02 23:21:48 +00:00
},
runs,
});
2020-11-30 22:05:19 +00:00
// Aggregate pubkeys
2020-11-30 23:53:40 +00:00
await runBenchmark<PublicKey[], void>({
2020-11-30 22:05:19 +00:00
id: `${implementation} aggregate pubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => bls.SecretKey.fromKeygen().toPublicKey()),
};
},
testRunner: (pks) => {
bls.PublicKey.aggregate(pks);
},
2020-12-02 20:45:29 +00:00
runs,
2020-11-30 22:05:19 +00:00
});
// Aggregate sigs
2020-11-30 23:53:40 +00:00
await runBenchmark<Signature[], void>({
2020-11-30 22:05:19 +00:00
id: `${implementation} aggregate signatures (${aggCount})`,
prepareTest: () => {
const msg = randomMessage();
const sigs = range(aggCount).map(() => {
const sk = bls.SecretKey.fromKeygen();
return sk.sign(msg);
});
return {
input: sigs,
};
},
testRunner: (sigs) => {
bls.Signature.aggregate(sigs);
},
2020-12-02 20:45:29 +00:00
runs,
2020-11-30 22:05:19 +00:00
});
2020-12-03 09:38:13 +00:00
// Sign
await runBenchmark<{sk: SecretKey; msg: Uint8Array}, void>({
id: `${implementation} sign`,
prepareTest: () => ({
input: {
sk: bls.SecretKey.fromKeygen(),
msg: randomMessage(),
},
}),
testRunner: ({sk, msg}) => {
sk.sign(msg);
},
runs,
});
2020-11-13 23:09:13 +00:00
});
2020-11-30 22:05:19 +00:00
})();