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
|
|
|
|
|
|
|
|
await runBenchmark<{pk: Uint8Array; msg: Uint8Array; sig: Uint8Array}, boolean>({
|
|
|
|
id: `noble verify`,
|
|
|
|
|
|
|
|
prepareTest: async () => {
|
|
|
|
const priv = generateRandomSecretKey();
|
|
|
|
const msg = randomMessage();
|
|
|
|
const sig = await noble.sign(msg, priv);
|
|
|
|
const pk = noble.getPublicKey(priv);
|
|
|
|
|
|
|
|
return {
|
|
|
|
input: {pk, msg, sig},
|
|
|
|
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
|
|
|
|
|
|
|
|
await runBenchmark<{pks: Uint8Array[]; msg: Uint8Array; sig: Uint8Array}, 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.getPublicKey(sk);
|
|
|
|
const sig = await noble.sign(msg, sk);
|
|
|
|
return {pk, sig};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const pks = dataArr.map((data) => data.pk);
|
|
|
|
const sig = noble.aggregateSignatures(dataArr.map((data) => data.sig));
|
|
|
|
|
|
|
|
return {
|
|
|
|
input: {pks, msg, sig},
|
|
|
|
resultCheck: (valid: boolean) => valid === true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
testRunner: async ({pks, msg, sig}) => {
|
|
|
|
const pk = noble.aggregatePublicKeys(pks);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-12-02 20:54:46 +00:00
|
|
|
// Verify multiple
|
|
|
|
|
|
|
|
await runBenchmark<{pks: Uint8Array[]; msgs: Uint8Array[]; sig: Uint8Array}, boolean>({
|
|
|
|
id: `noble verifyMultiple (${aggCount})`,
|
|
|
|
|
|
|
|
prepareTest: async () => {
|
|
|
|
const dataArr = await Promise.all(
|
|
|
|
range(aggCount).map(async () => {
|
|
|
|
const sk = generateRandomSecretKey();
|
|
|
|
const pk = noble.getPublicKey(sk);
|
|
|
|
const msg = randomMessage();
|
|
|
|
const sig = await noble.sign(msg, sk);
|
|
|
|
return {pk, 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));
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
await runBenchmark<Uint8Array[], void>({
|
|
|
|
id: `noble aggregate pubkeys (${aggCount})`,
|
|
|
|
|
|
|
|
prepareTest: () => {
|
|
|
|
return {
|
|
|
|
input: range(aggCount).map(() => noble.getPublicKey(generateRandomSecretKey())),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
testRunner: async (pks) => {
|
|
|
|
noble.aggregatePublicKeys(pks);
|
|
|
|
},
|
2020-12-02 20:45:29 +00:00
|
|
|
runs: runsNoble,
|
2020-11-30 22:05:19 +00:00
|
|
|
});
|
2020-12-03 09:38:13 +00:00
|
|
|
|
|
|
|
// Sign
|
|
|
|
|
|
|
|
await runBenchmark<{sk: Uint8Array; msg: Uint8Array}, void>({
|
|
|
|
id: `noble sign`,
|
|
|
|
|
|
|
|
prepareTest: async () => ({
|
|
|
|
input: {
|
|
|
|
sk: generateRandomSecretKey(),
|
|
|
|
msg: randomMessage(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
testRunner: async ({sk, msg}) => {
|
|
|
|
await noble.sign(msg, sk);
|
|
|
|
},
|
|
|
|
runs: runsNoble,
|
|
|
|
});
|
2020-11-30 22:05:19 +00:00
|
|
|
})();
|