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/noble.ts

138 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

2020-11-30 22:05:19 +00:00
import {runBenchmark} from "./runner";
2020-12-01 08:29:19 +00:00
import {range, randomMessage} from "../test/util";
2020-11-30 22:05:19 +00:00
import {generateRandomSecretKey} from "@chainsafe/bls-keygen";
import * as noble from "noble-bls12-381";
2020-12-02 20:45:29 +00:00
import {aggCount, runsNoble} from "./params";
2020-11-30 22:05:19 +00:00
(async function () {
// verify
2020-12-04 18:54:47 +00:00
await runBenchmark<{pk: noble.PointG1; msg: noble.PointG2; sig: noble.PointG2}, boolean>({
2020-11-30 22:05:19 +00:00
id: `noble verify`,
prepareTest: async () => {
const priv = generateRandomSecretKey();
const msg = randomMessage();
const pk = noble.PointG1.fromPrivateKey(priv);
const sig = noble.PointG2.fromSignature(await noble.sign(msg, priv));
2020-11-30 22:05:19 +00:00
return {
input: {pk, msg: await noble.PointG2.hashToCurve(msg), sig},
2020-11-30 22:05:19 +00:00
resultCheck: (valid: boolean) => valid === true,
};
},
testRunner: async ({pk, msg, sig}) => {
return await noble.verify(sig, msg, pk);
},
2020-12-02 20:45:29 +00:00
runs: runsNoble,
2020-11-30 22:05:19 +00:00
});
// Fast aggregate
2020-12-04 18:54:47 +00:00
await runBenchmark<{pks: noble.PointG1[]; msg: noble.PointG2; sig: noble.PointG2}, boolean>({
2020-12-02 20:45:29 +00:00
id: `noble verifyAggregate (${aggCount})`,
2020-11-30 22:05:19 +00:00
prepareTest: async () => {
const msg = randomMessage();
const dataArr = await Promise.all(
range(aggCount).map(async () => {
const sk = generateRandomSecretKey();
const pk = noble.PointG1.fromPrivateKey(sk);
const sig = noble.PointG2.fromSignature(await noble.sign(msg, sk));
2020-11-30 22:05:19 +00:00
return {pk, sig};
})
);
const pks = dataArr.map((data) => data.pk);
2020-12-04 18:54:47 +00:00
const sig = noble.aggregateSignatures(dataArr.map((data) => data.sig)) as any as noble.PointG2;
2020-11-30 22:05:19 +00:00
return {
2020-12-04 18:54:47 +00:00
input: {pks, msg: await noble.PointG2.hashToCurve(msg), sig},
2020-11-30 22:05:19 +00:00
resultCheck: (valid: boolean) => valid === true,
};
},
testRunner: async ({pks, msg, sig}) => {
2020-12-04 18:54:47 +00:00
const pk = noble.aggregatePublicKeys(pks) as any as noble.PointG1;
2020-11-30 22:05:19 +00:00
return await noble.verify(sig, msg, pk);
},
2020-12-02 20:45:29 +00:00
runs: runsNoble,
2020-11-30 22:05:19 +00:00
});
// // Verify multiple
2020-12-02 20:54:46 +00:00
2020-12-04 18:54:47 +00:00
await runBenchmark<{pks: noble.PointG1[]; msgs: noble.PointG2[]; sig: noble.PointG2}, boolean>({
2020-12-02 20:54:46 +00:00
id: `noble verifyMultiple (${aggCount})`,
prepareTest: async () => {
const dataArr = await Promise.all(
range(aggCount).map(async () => {
const sk = generateRandomSecretKey();
const pk = noble.PointG1.fromPrivateKey(sk);
2020-12-02 20:54:46 +00:00
const msg = randomMessage();
const sig = noble.PointG2.fromSignature(await noble.sign(msg, sk));
return {pk, msg: await noble.PointG2.hashToCurve(msg), sig};
2020-12-02 20:54:46 +00:00
})
);
const pks = dataArr.map((data) => data.pk);
const msgs = dataArr.map((data) => data.msg);
2020-12-04 18:54:47 +00:00
const sig = noble.aggregateSignatures(dataArr.map((data) => data.sig)) as any as noble.PointG2;
2020-12-02 20:54:46 +00:00
return {
input: {pks, msgs, sig},
resultCheck: (valid: boolean) => valid === true,
};
},
testRunner: async ({pks, msgs, sig}) => {
return await noble.verifyBatch(msgs, pks, sig);
},
runs: runsNoble,
});
2020-11-30 22:05:19 +00:00
// Aggregate pubkeys
2020-12-04 18:54:47 +00:00
await runBenchmark<noble.PointG1[], void>({
2020-11-30 22:05:19 +00:00
id: `noble aggregate pubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => noble.PointG1.fromPrivateKey(generateRandomSecretKey())),
};
},
testRunner: async (pks) => {
noble.aggregatePublicKeys(pks);
},
runs: runsNoble,
});
2020-12-04 18:54:47 +00:00
await runBenchmark<noble.PointG2[], void>({
id: `noble aggregate signatures (${aggCount})`,
prepareTest: async () => {
const hashes = range(aggCount).map(() => generateRandomSecretKey()).map(noble.PointG2.hashToCurve);
return {
input: await Promise.all(hashes),
};
},
testRunner: async (sigs) => {
noble.aggregateSignatures(sigs);
},
runs: runsNoble,
});
await runBenchmark<{sk: Uint8Array; msg: noble.PointG2}, void>({
2020-12-03 16:22:28 +00:00
id: `noble sign`,
prepareTest: async () => ({
2020-12-03 09:38:13 +00:00
input: {
sk: generateRandomSecretKey(),
2020-12-04 18:54:47 +00:00
msg: await noble.PointG2.hashToCurve(randomMessage()),
2020-12-03 09:38:13 +00:00
},
2020-12-03 16:22:28 +00:00
}),
testRunner: async ({sk, msg}) => {
2020-12-03 09:38:13 +00:00
await noble.sign(msg, sk);
2020-12-03 16:22:28 +00:00
},
runs: runsNoble,
2020-12-03 09:38:13 +00:00
});
2020-11-30 22:05:19 +00:00
})();