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 () {
|
2021-04-05 19:00:49 +00:00
|
|
|
{
|
|
|
|
// verify
|
|
|
|
|
|
|
|
const priv = generateRandomSecretKey();
|
|
|
|
const msg = randomMessage();
|
|
|
|
const pk = noble.PointG1.fromPrivateKey(priv);
|
|
|
|
const sig = noble.PointG2.fromSignature(await noble.sign(msg, priv));
|
|
|
|
|
|
|
|
await runBenchmark<{pk: noble.PointG1; msg: noble.PointG2; sig: noble.PointG2}, boolean>({
|
|
|
|
id: `noble verify`,
|
|
|
|
prepareTest: async () => ({pk, msg: await noble.PointG2.hashToCurve(msg), sig}),
|
|
|
|
testRunner: async ({pk, msg, sig}) => await noble.verify(sig, msg, pk),
|
|
|
|
runs: runsNoble,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Fast aggregate
|
|
|
|
|
|
|
|
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));
|
|
|
|
return {pk, sig};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const pks = dataArr.map((data) => data.pk);
|
|
|
|
const sig = (noble.aggregateSignatures(dataArr.map((data) => data.sig)) as any) as noble.PointG2;
|
|
|
|
|
|
|
|
await runBenchmark({
|
|
|
|
id: `noble verifyAggregate (${aggCount})`,
|
|
|
|
prepareTest: async () => ({pks, msg: await noble.PointG2.hashToCurve(msg), sig}),
|
|
|
|
testRunner: async ({pks, msg, sig}) =>
|
|
|
|
await noble.verify(sig, msg, (noble.aggregatePublicKeys(pks) as any) as noble.PointG1),
|
|
|
|
runs: runsNoble,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify multiple
|
|
|
|
|
|
|
|
const dataArr = await Promise.all(
|
|
|
|
range(aggCount).map(async () => {
|
|
|
|
const sk = generateRandomSecretKey();
|
|
|
|
const pk = noble.PointG1.fromPrivateKey(sk);
|
|
|
|
const msg = randomMessage();
|
|
|
|
const sig = noble.PointG2.fromSignature(await noble.sign(msg, sk));
|
|
|
|
return {pk, msg: await noble.PointG2.hashToCurve(msg), sig};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const pks = dataArr.map((data) => data.pk);
|
|
|
|
const msgs = dataArr.map((data) => data.msg);
|
|
|
|
const sig = (noble.aggregateSignatures(dataArr.map((data) => data.sig)) as any) as noble.PointG2;
|
|
|
|
|
|
|
|
await runBenchmark({
|
|
|
|
id: `noble verifyMultiple (${aggCount})`,
|
|
|
|
prepareTest: async () => ({pks, msgs, sig}),
|
|
|
|
testRunner: async ({pks, msgs, sig}) => await noble.verifyBatch(msgs, pks, sig),
|
|
|
|
runs: runsNoble,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Aggregate pubkeys
|
|
|
|
|
|
|
|
const pubkeys = range(aggCount).map(() => noble.PointG1.fromPrivateKey(generateRandomSecretKey()));
|
|
|
|
|
|
|
|
await runBenchmark({
|
|
|
|
id: `noble aggregate pubkeys (${aggCount})`,
|
|
|
|
prepareTest: () => pubkeys,
|
|
|
|
testRunner: async (pks) => noble.aggregatePublicKeys(pks),
|
|
|
|
runs: runsNoble,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const hashes = await Promise.all(
|
|
|
|
range(aggCount)
|
|
|
|
.map(() => generateRandomSecretKey())
|
|
|
|
.map(noble.PointG2.hashToCurve)
|
|
|
|
);
|
|
|
|
|
|
|
|
await runBenchmark({
|
2020-12-04 18:54:47 +00:00
|
|
|
id: `noble aggregate signatures (${aggCount})`,
|
2021-04-05 19:00:49 +00:00
|
|
|
prepareTest: () => hashes,
|
|
|
|
testRunner: async (sigs) => noble.aggregateSignatures(sigs),
|
2020-12-04 18:54:47 +00:00
|
|
|
runs: runsNoble,
|
|
|
|
});
|
|
|
|
|
2021-04-05 19:00:49 +00:00
|
|
|
const sk = generateRandomSecretKey();
|
|
|
|
const msg = await noble.PointG2.hashToCurve(randomMessage());
|
2020-12-03 16:22:28 +00:00
|
|
|
|
2021-04-05 19:00:49 +00:00
|
|
|
await runBenchmark({
|
|
|
|
id: `noble sign`,
|
|
|
|
prepareTest: () => ({sk, msg}),
|
|
|
|
testRunner: async ({sk, msg}) => 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
|
|
|
})();
|