Fix type errors in benchmark runner

Lint issues
This commit is contained in:
dapplion 2020-11-20 19:35:32 +00:00
parent 70ccbfbe5b
commit 990258dbd9
13 changed files with 73 additions and 123 deletions

View File

@ -22,8 +22,8 @@ export interface IBls {
verifyAggregate(publicKeys: Uint8Array[], messageHash: Uint8Array, signature: Uint8Array): boolean;
verifyMultiple(publicKeys: Uint8Array[], messageHashes: Uint8Array[], signature: Uint8Array): boolean;
initBLS: () => Promise<void>;
destroy: () => void;
initBLS(): Promise<void>;
destroy(): void;
}
export interface IKeypair {
@ -45,7 +45,6 @@ export interface ISignature {
}
export interface IPrivateKey {
value: any;
toPublicKey(): IPublicKey;
signMessage(message: Uint8Array): ISignature;
toBytes(): Buffer;

View File

@ -1,95 +1,49 @@
import crypto from "crypto";
import * as blst from "@chainsafe/blst";
import * as herumi from "../../src";
import {runBenchmark} from "./runner";
import {runForAllImplementations} from "../switch";
import {IPublicKey, ISignature} from "../../src/interface";
(async function () {
await herumi.initBLS();
runForAllImplementations(async (bls, implementation) => {
await bls.initBLS();
const aggCount = 30;
// verify
runBenchmark<{pk: blst.PublicKey; msg: Uint8Array; sig: blst.Signature}, boolean>({
id: "BLST verify",
runBenchmark<{pk: IPublicKey; msg: Uint8Array; sig: ISignature}, boolean>({
id: `${implementation} verify`,
prepareTest: () => {
const msg = randomMsg();
const sk = blst.SecretKey.fromKeygen(crypto.randomBytes(32));
const sk = bls.PrivateKey.fromKeygen();
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
const sig = sk.signMessage(msg);
return {
input: {pk, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pk, msg, sig}) => {
return blst.verify(msg, pk, sig);
},
});
runBenchmark<{pk: herumi.PublicKey; msg: Uint8Array; sig: herumi.Signature}, boolean>({
id: "HERUMI verify",
prepareTest: () => {
const msg = randomMsg();
const keypair = herumi.generateKeyPair();
const pk = keypair.publicKey;
const sig = keypair.privateKey.signMessage(msg);
return {
input: {pk, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pk, msg, sig}) => {
return pk.verifyMessage(sig, msg);
return sig.verify(pk, msg);
},
});
// Fast aggregate
runBenchmark<{pks: blst.AggregatePublicKey[]; msg: Uint8Array; sig: blst.Signature}, boolean>({
id: "BLST fastAggregateVerify",
runBenchmark<{pks: IPublicKey[]; msg: Uint8Array; sig: ISignature}, boolean>({
id: `${implementation} verifyAggregate`,
prepareTest: () => {
const msg = randomMsg();
const dataArr = range(aggCount).map(() => {
const sk = blst.SecretKey.fromKeygen(crypto.randomBytes(32));
const pk = sk.toAggregatePublicKey();
const sig = sk.sign(msg);
const sk = bls.PrivateKey.fromKeygen();
const pk = sk.toPublicKey();
const sig = sk.signMessage(msg);
return {pk, sig};
});
const pks = dataArr.map((data) => data.pk);
const aggSig = blst.AggregateSignature.fromSignatures(dataArr.map((data) => data.sig));
const sig = aggSig.toSignature();
return {
input: {pks, msg, sig},
resultCheck: (valid) => valid === true,
};
},
testRunner: ({pks, msg, sig}) => {
return blst.fastAggregateVerify(msg, pks, sig);
},
});
runBenchmark<{pks: herumi.PublicKey[]; msg: Uint8Array; sig: herumi.Signature}, boolean>({
id: "HERUMI fastAggregateVerify",
prepareTest: () => {
const msg = randomMsg();
const dataArr = range(aggCount).map(() => {
const keypair = herumi.generateKeyPair();
const pk = keypair.publicKey;
const sig = keypair.privateKey.signMessage(msg);
return {pk, sig};
});
const pks = dataArr.map((data) => data.pk);
const sig = herumi.Signature.aggregate(dataArr.map((data) => data.sig));
const sig = bls.Signature.aggregate(dataArr.map((data) => data.sig));
return {
input: {pks, msg, sig},
@ -101,50 +55,41 @@ import {runBenchmark} from "./runner";
},
});
// Aggregate pubkeys
runBenchmark<IPublicKey[], void>({
id: `${implementation} aggregate pubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => bls.PrivateKey.fromKeygen().toPublicKey()),
};
},
testRunner: (pks) => {
bls.PublicKey.aggregate(pks);
},
});
// Aggregate sigs
runBenchmark<blst.PublicKey[], void>({
id: `BLST aggregatePubkeys (${aggCount})`,
runBenchmark<ISignature[], void>({
id: `${implementation} aggregate signatures (${aggCount})`,
prepareTest: () => {
const msg = randomMsg();
const sigs = range(aggCount).map(() => {
const sk = bls.PrivateKey.fromKeygen();
return sk.signMessage(msg);
});
return {
input: range(aggCount).map(() => blst.SecretKey.fromKeygen(crypto.randomBytes(32)).toPublicKey()),
input: sigs,
};
},
testRunner: (pks) => {
blst.AggregatePublicKey.fromPublicKeys(pks);
testRunner: (sigs) => {
bls.Signature.aggregate(sigs);
},
});
runBenchmark<blst.AggregatePublicKey[], void>({
id: `BLST aggregatePubkeys as jacobian (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => {
const pk = blst.SecretKey.fromKeygen(crypto.randomBytes(32)).toPublicKey();
return blst.AggregatePublicKey.fromPublicKey(pk);
}),
};
},
testRunner: (pks) => {
blst.aggregatePubkeys(pks);
},
});
runBenchmark<herumi.PublicKey[], void>({
id: `HERUMI aggregatePubkeys (${aggCount})`,
prepareTest: () => {
return {
input: range(aggCount).map(() => herumi.generateKeyPair().publicKey),
};
},
testRunner: (pks) => {
herumi.PublicKey.aggregate(pks);
},
});
})();
});
function range(n: number): number[] {
const nums: number[] = [];

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
interface IAggregateSigsTestCase {
data: {
@ -11,7 +11,7 @@ interface IAggregateSigsTestCase {
};
}
forEachImplementation((bls) => {
describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsTestCase, string>(
"bls/aggregate/small",
path.join(SPEC_TESTS_DIR, "general/phase0/bls/aggregate/small"),

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
interface IAggregateSigsVerifyTestCase {
data: {
@ -15,7 +15,7 @@ interface IAggregateSigsVerifyTestCase {
};
}
forEachImplementation((bls) => {
describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>(
"bls/aggregate_verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/bls/aggregate_verify/small"),

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
interface IAggregateSigsVerifyTestCase {
data: {
@ -15,7 +15,7 @@ interface IAggregateSigsVerifyTestCase {
};
}
forEachImplementation((bls) => {
describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>(
"bls/fast_aggregate_verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/bls/fast_aggregate_verify/small"),

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
interface ISignMessageTestCase {
data: {
@ -14,7 +14,7 @@ interface ISignMessageTestCase {
};
}
forEachImplementation((bls) => {
describeForAllImplementations((bls) => {
describeDirectorySpecTest<ISignMessageTestCase, string>(
"bls/sign/small",
path.join(SPEC_TESTS_DIR, "general/phase0/bls/sign/small"),

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
interface IVerifyTestCase {
data: {
@ -15,7 +15,7 @@ interface IVerifyTestCase {
};
}
forEachImplementation((bls) => {
describeForAllImplementations((bls) => {
describeDirectorySpecTest<IVerifyTestCase, boolean>(
"bls/verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/bls/verify/small"),

View File

@ -14,19 +14,23 @@ export function getBls(implementation: Implementation): IBls {
}
}
export function forEachImplementation(
implementations: Implementation[],
callback: (bls: ReturnType<typeof getBls>, implementation: Implementation) => void
): void {
for (const implementation of implementations) {
describe(implementation, () => {
export async function runForAllImplementations(
callback: (bls: IBls, implementation: Implementation) => Promise<void> | void
): Promise<void> {
for (const implementation of ["blst", "herumi"] as Implementation[]) {
const bls = getBls(implementation);
await callback(bls, implementation);
}
}
export function describeForAllImplementations(callback: (bls: IBls) => void): void {
runForAllImplementations((bls, implementation) => {
describe(implementation, () => {
before(async () => {
await bls.initBLS();
});
callback(bls, implementation);
callback(bls);
});
});
}
}

View File

@ -2,7 +2,7 @@ import {expect} from "chai";
import {IBls} from "../../src/interface";
import {getN, randomMessage} from "../util";
export function runIndexTests(bls: IBls) {
export function runIndexTests(bls: IBls): void {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function getRandomData() {
const sk = bls.PrivateKey.fromKeygen();

View File

@ -1,7 +1,7 @@
import {expect} from "chai";
import {IBls} from "../../src/interface";
export function runPrivateKeyTests(bls: IBls) {
export function runPrivateKeyTests(bls: IBls): void {
describe("PrivateKey", () => {
it("should generate fromKeygen private key", () => {
const privateKey1 = bls.PrivateKey.fromKeygen();

View File

@ -1,7 +1,7 @@
import {expect} from "chai";
import {IBls} from "../../src/interface";
export function runPublicKeyTests(bls: IBls) {
export function runPublicKeyTests(bls: IBls): void {
describe("PublicKey", () => {
const publicKey =
"0xb6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311";

View File

@ -2,10 +2,10 @@ import {runPrivateKeyTests} from "./privateKey.test";
import {runPublicKeyTests} from "./publicKey.test";
// import {runKeypairTests} from "./keypair.test";
import {runIndexTests} from "./index.test";
import {forEachImplementation} from "../switch";
import {describeForAllImplementations} from "../switch";
// Import test's bls lib lazily to prevent breaking test with Karma
forEachImplementation(["blst", "herumi"], (bls) => {
describeForAllImplementations((bls) => {
runPrivateKeyTests(bls);
runPublicKeyTests(bls);
// runKeypairTests(bls);

View File

@ -9,6 +9,8 @@ import {runIndexTests} from "./index.test";
// Error: BLST bindings loader should only run in a NodeJS context: process.platform
describe("herumi", () => {
before(async () => {
// For consistency with describeForAllImplementations
// eslint-disable-next-line import/no-named-as-default-member
await herumi.initBLS();
});