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

View File

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

View File

@ -2,7 +2,7 @@ import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils"; import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params"; import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch"; import {describeForAllImplementations} from "../switch";
interface IAggregateSigsTestCase { interface IAggregateSigsTestCase {
data: { data: {
@ -11,7 +11,7 @@ interface IAggregateSigsTestCase {
}; };
} }
forEachImplementation((bls) => { describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsTestCase, string>( describeDirectorySpecTest<IAggregateSigsTestCase, string>(
"bls/aggregate/small", "bls/aggregate/small",
path.join(SPEC_TESTS_DIR, "general/phase0/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 {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils"; import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params"; import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch"; import {describeForAllImplementations} from "../switch";
interface IAggregateSigsVerifyTestCase { interface IAggregateSigsVerifyTestCase {
data: { data: {
@ -15,7 +15,7 @@ interface IAggregateSigsVerifyTestCase {
}; };
} }
forEachImplementation((bls) => { describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>( describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>(
"bls/aggregate_verify/small", "bls/aggregate_verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/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 {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils"; import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params"; import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch"; import {describeForAllImplementations} from "../switch";
interface IAggregateSigsVerifyTestCase { interface IAggregateSigsVerifyTestCase {
data: { data: {
@ -15,7 +15,7 @@ interface IAggregateSigsVerifyTestCase {
}; };
} }
forEachImplementation((bls) => { describeForAllImplementations((bls) => {
describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>( describeDirectorySpecTest<IAggregateSigsVerifyTestCase, boolean>(
"bls/fast_aggregate_verify/small", "bls/fast_aggregate_verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/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 {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils"; import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params"; import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch"; import {describeForAllImplementations} from "../switch";
interface ISignMessageTestCase { interface ISignMessageTestCase {
data: { data: {
@ -14,7 +14,7 @@ interface ISignMessageTestCase {
}; };
} }
forEachImplementation((bls) => { describeForAllImplementations((bls) => {
describeDirectorySpecTest<ISignMessageTestCase, string>( describeDirectorySpecTest<ISignMessageTestCase, string>(
"bls/sign/small", "bls/sign/small",
path.join(SPEC_TESTS_DIR, "general/phase0/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 {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils"; import {hexToBytes} from "../../src/helpers/utils";
import {SPEC_TESTS_DIR} from "../params"; import {SPEC_TESTS_DIR} from "../params";
import {forEachImplementation} from "../switch"; import {describeForAllImplementations} from "../switch";
interface IVerifyTestCase { interface IVerifyTestCase {
data: { data: {
@ -15,7 +15,7 @@ interface IVerifyTestCase {
}; };
} }
forEachImplementation((bls) => { describeForAllImplementations((bls) => {
describeDirectorySpecTest<IVerifyTestCase, boolean>( describeDirectorySpecTest<IVerifyTestCase, boolean>(
"bls/verify/small", "bls/verify/small",
path.join(SPEC_TESTS_DIR, "general/phase0/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( export async function runForAllImplementations(
implementations: Implementation[], callback: (bls: IBls, implementation: Implementation) => Promise<void> | void
callback: (bls: ReturnType<typeof getBls>, implementation: Implementation) => void ): Promise<void> {
): void { for (const implementation of ["blst", "herumi"] as Implementation[]) {
for (const implementation of implementations) {
describe(implementation, () => {
const bls = getBls(implementation); const bls = getBls(implementation);
await callback(bls, implementation);
}
}
export function describeForAllImplementations(callback: (bls: IBls) => void): void {
runForAllImplementations((bls, implementation) => {
describe(implementation, () => {
before(async () => { before(async () => {
await bls.initBLS(); await bls.initBLS();
}); });
callback(bls, implementation); callback(bls);
});
}); });
}
} }

View File

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

View File

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

View File

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

View File

@ -2,10 +2,10 @@ import {runPrivateKeyTests} from "./privateKey.test";
import {runPublicKeyTests} from "./publicKey.test"; import {runPublicKeyTests} from "./publicKey.test";
// import {runKeypairTests} from "./keypair.test"; // import {runKeypairTests} from "./keypair.test";
import {runIndexTests} from "./index.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 // Import test's bls lib lazily to prevent breaking test with Karma
forEachImplementation(["blst", "herumi"], (bls) => { describeForAllImplementations((bls) => {
runPrivateKeyTests(bls); runPrivateKeyTests(bls);
runPublicKeyTests(bls); runPublicKeyTests(bls);
// runKeypairTests(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 // Error: BLST bindings loader should only run in a NodeJS context: process.platform
describe("herumi", () => { describe("herumi", () => {
before(async () => { before(async () => {
// For consistency with describeForAllImplementations
// eslint-disable-next-line import/no-named-as-default-member
await herumi.initBLS(); await herumi.initBLS();
}); });