2020-11-13 23:09:13 +00:00
|
|
|
import {runBenchmark} from "./runner";
|
2020-11-20 19:35:32 +00:00
|
|
|
import {runForAllImplementations} from "../switch";
|
|
|
|
import {IPublicKey, ISignature} from "../../src/interface";
|
2020-11-28 19:27:59 +00:00
|
|
|
import {randomBytes} from "../../src/helpers";
|
2020-11-13 23:09:13 +00:00
|
|
|
|
2020-11-29 22:23:48 +00:00
|
|
|
runForAllImplementations((bls, implementation) => {
|
2020-11-13 23:09:13 +00:00
|
|
|
const aggCount = 30;
|
|
|
|
|
|
|
|
// verify
|
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
runBenchmark<{pk: IPublicKey; msg: Uint8Array; sig: ISignature}, boolean>({
|
|
|
|
id: `${implementation} verify`,
|
2020-11-13 23:09:13 +00:00
|
|
|
|
|
|
|
prepareTest: () => {
|
|
|
|
const msg = randomMsg();
|
2020-11-30 18:01:13 +00:00
|
|
|
const sk = bls.SecretKey.fromKeygen();
|
2020-11-13 23:09:13 +00:00
|
|
|
const pk = sk.toPublicKey();
|
2020-11-25 16:09:44 +00:00
|
|
|
const sig = sk.sign(msg);
|
2020-11-13 23:09:13 +00:00
|
|
|
return {
|
|
|
|
input: {pk, msg, sig},
|
|
|
|
resultCheck: (valid) => valid === true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
testRunner: ({pk, msg, sig}) => {
|
2020-11-20 19:35:32 +00:00
|
|
|
return sig.verify(pk, msg);
|
2020-11-13 23:09:13 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fast aggregate
|
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
runBenchmark<{pks: IPublicKey[]; msg: Uint8Array; sig: ISignature}, boolean>({
|
|
|
|
id: `${implementation} verifyAggregate`,
|
2020-11-13 23:09:13 +00:00
|
|
|
|
|
|
|
prepareTest: () => {
|
|
|
|
const msg = randomMsg();
|
|
|
|
const dataArr = range(aggCount).map(() => {
|
2020-11-30 18:01:13 +00:00
|
|
|
const sk = bls.SecretKey.fromKeygen();
|
2020-11-20 19:35:32 +00:00
|
|
|
const pk = sk.toPublicKey();
|
2020-11-25 16:09:44 +00:00
|
|
|
const sig = sk.sign(msg);
|
2020-11-13 23:09:13 +00:00
|
|
|
return {pk, sig};
|
|
|
|
});
|
|
|
|
|
|
|
|
const pks = dataArr.map((data) => data.pk);
|
2020-11-20 19:35:32 +00:00
|
|
|
const sig = bls.Signature.aggregate(dataArr.map((data) => data.sig));
|
2020-11-13 23:09:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
input: {pks, msg, sig},
|
|
|
|
resultCheck: (valid) => valid === true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
testRunner: ({pks, msg, sig}) => {
|
|
|
|
return sig.verifyAggregate(pks, msg);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
// Aggregate pubkeys
|
2020-11-13 23:09:13 +00:00
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
runBenchmark<IPublicKey[], void>({
|
|
|
|
id: `${implementation} aggregate pubkeys (${aggCount})`,
|
2020-11-13 23:09:13 +00:00
|
|
|
|
|
|
|
prepareTest: () => {
|
|
|
|
return {
|
2020-11-30 18:01:13 +00:00
|
|
|
input: range(aggCount).map(() => bls.SecretKey.fromKeygen().toPublicKey()),
|
2020-11-13 23:09:13 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
testRunner: (pks) => {
|
2020-11-20 19:35:32 +00:00
|
|
|
bls.PublicKey.aggregate(pks);
|
2020-11-13 23:09:13 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
// Aggregate sigs
|
2020-11-14 22:25:22 +00:00
|
|
|
|
2020-11-20 19:35:32 +00:00
|
|
|
runBenchmark<ISignature[], void>({
|
|
|
|
id: `${implementation} aggregate signatures (${aggCount})`,
|
2020-11-13 23:09:13 +00:00
|
|
|
|
|
|
|
prepareTest: () => {
|
2020-11-20 19:35:32 +00:00
|
|
|
const msg = randomMsg();
|
|
|
|
const sigs = range(aggCount).map(() => {
|
2020-11-30 18:01:13 +00:00
|
|
|
const sk = bls.SecretKey.fromKeygen();
|
2020-11-25 16:09:44 +00:00
|
|
|
return sk.sign(msg);
|
2020-11-20 19:35:32 +00:00
|
|
|
});
|
2020-11-13 23:09:13 +00:00
|
|
|
return {
|
2020-11-20 19:35:32 +00:00
|
|
|
input: sigs,
|
2020-11-13 23:09:13 +00:00
|
|
|
};
|
|
|
|
},
|
2020-11-20 19:35:32 +00:00
|
|
|
testRunner: (sigs) => {
|
|
|
|
bls.Signature.aggregate(sigs);
|
2020-11-13 23:09:13 +00:00
|
|
|
},
|
|
|
|
});
|
2020-11-20 19:35:32 +00:00
|
|
|
});
|
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 {
|
2020-11-28 19:30:56 +00:00
|
|
|
return randomBytes(32);
|
2020-11-13 23:09:13 +00:00
|
|
|
}
|