diff --git a/package.json b/package.json index 34da2a1..3488a5f 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "test:unit": "nyc --cache-dir .nyc_output/.cache -r lcov -e .ts mocha -r ./.babel-register 'test/unit/**/*.test.ts' && nyc report", "test:spec": "mocha -r ./.babel-register 'test/spec/**/*.test.ts'", "test": "yarn test:unit && yarn test:spec", - "coverage": "codecov -F bls" + "coverage": "codecov -F bls", + "benchmark": "node -r ./.babel-register test/benchmarks" }, "dependencies": { "@chainsafe/eth2.0-types": "^0.1.0", @@ -49,11 +50,12 @@ "@babel/preset-typescript": "^7.3.3", "@babel/register": "^7.0.0", "@babel/runtime": "^7.4.4", + "@chainsafe/benchmark-utils": "^0.1.0", "@chainsafe/eth2.0-spec-test-util": "^0.2.3", "@types/assert": "^1.4.2", "@types/chai": "^4.1.7", "@types/mocha": "^5.2.5", - "@types/node": "^10.12.17", + "@types/node": "^12.7.2", "@typescript-eslint/eslint-plugin": "^1.3.0", "@typescript-eslint/parser": "^1.3.0", "babel-plugin-rewire-exports": "^1.1.0", diff --git a/test/benchmarks/index.ts b/test/benchmarks/index.ts new file mode 100644 index 0000000..5b03fa5 --- /dev/null +++ b/test/benchmarks/index.ts @@ -0,0 +1,11 @@ +// Import benchmarks +import * as suites from "./suites"; +import {createReportDir, runSuite} from "@chainsafe/benchmark-utils"; +// Create file +const directory: string = createReportDir(); + + +// Run benchmarks +Object.values(suites).forEach((suite) => { + runSuite(suite(directory)); +}); \ No newline at end of file diff --git a/test/benchmarks/suites/index.ts b/test/benchmarks/suites/index.ts new file mode 100644 index 0000000..9af379d --- /dev/null +++ b/test/benchmarks/suites/index.ts @@ -0,0 +1,5 @@ +export {verifyInValidSignatureBenchmark} from './verifyInValidSignature'; +export {verifyValidSignatureBenchmark} from './verifyValidSignature'; +export {verifyValidAggregatedSignature} from './verifyValidAggregatedSignature'; +export {verifyInvalidAggregatedSignature} from './verifyInvalidAggregatedSignature'; +export {aggregateSignaturesBenchmark} from './signatureAggregation'; \ No newline at end of file diff --git a/test/benchmarks/suites/signatureAggregation.ts b/test/benchmarks/suites/signatureAggregation.ts new file mode 100644 index 0000000..ad58d51 --- /dev/null +++ b/test/benchmarks/suites/signatureAggregation.ts @@ -0,0 +1,39 @@ +/* eslint-disable @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires */ + +import {BenchSuite} from "@chainsafe/benchmark-utils"; +import {aggregateSignatures} from "../../../src"; + + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace global { + export let signatures: Buffer[]; + export let aggregateSignatures: Function; +} + +// @ts-ignore +global.require = require; + +global.aggregateSignatures = aggregateSignatures; + +export function aggregateSignaturesBenchmark(dir: string): BenchSuite { + + // Set the function test + const FUNCTION_NAME = "verifyValidSignature"; // PLEASE FILL THIS OUT + + const aggregateSignatures = function (): void { + global.aggregateSignatures(global.signatures); + }; + + return { + testFunctions: [aggregateSignatures], + setup: function() { + global.signatures = []; + const {Keypair} = require("../../../src"); + const {sha256} = require('js-sha256'); + const keypair = Keypair.generate(); + const message = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + global.signatures.push(keypair.privateKey.signMessage(Buffer.from(message), Buffer.alloc(8)).toBytesCompressed()); + }, + file: dir + FUNCTION_NAME + ".txt" + }; +} \ No newline at end of file diff --git a/test/benchmarks/suites/verifyInValidSignature.ts b/test/benchmarks/suites/verifyInValidSignature.ts new file mode 100644 index 0000000..6cd4106 --- /dev/null +++ b/test/benchmarks/suites/verifyInValidSignature.ts @@ -0,0 +1,42 @@ +import {BenchSuite} from "@chainsafe/benchmark-utils"; +import {verify} from "../../../src"; + + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace global { + export let domain: Buffer; + export let message: Buffer; + export let signature: Buffer; + export let publicKey: Buffer; + export let verify: Function; +} + +// @ts-ignore +global.require = require; + +global.domain = Buffer.alloc(8); +global.verify = verify; + +export function verifyInValidSignatureBenchmark(dir: string): BenchSuite { + + // Set the function test + const FUNCTION_NAME = "verifyInValidSignature"; // PLEASE FILL THIS OUT + + const verifyInValidSignature = function (): void { + global.verify(global.publicKey, global.message, global.signature, global.domain); + }; + + return { + testFunctions: [verifyInValidSignature], + setup: function() { + const {Keypair} = require("../../../src"); + const {sha256} = require('js-sha256'); + const keypair = Keypair.generate(); + const keypair2 = Keypair.generate(); + global.publicKey = keypair2.publicKey.toBytesCompressed(); + global.message = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + global.signature = keypair.privateKey.signMessage(Buffer.from(global.message), global.domain).toBytesCompressed(); + }, + file: dir + FUNCTION_NAME + ".txt" + }; +} \ No newline at end of file diff --git a/test/benchmarks/suites/verifyInvalidAggregatedSignature.ts b/test/benchmarks/suites/verifyInvalidAggregatedSignature.ts new file mode 100644 index 0000000..32e4a33 --- /dev/null +++ b/test/benchmarks/suites/verifyInvalidAggregatedSignature.ts @@ -0,0 +1,48 @@ +import {BenchSuite} from "@chainsafe/benchmark-utils"; +import {aggregateSignatures, verifyMultiple} from "../../../src"; + + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace global { + export let domain: Buffer; + export let messages: Buffer[]; + export let signature: Buffer; + export let publicKeys: Buffer[]; + export let verify: Function; +} + +// @ts-ignore +global.require = require; + +global.domain = Buffer.alloc(8); +global.verify = verifyMultiple; + +export function verifyInvalidAggregatedSignature(dir: string): BenchSuite { + + // Set the function test + const FUNCTION_NAME = "verifyInvalidAggregatedSignature"; // PLEASE FILL THIS OUT + + const verifyInvalidAggregatedSignature = function (): void { + global.verify(global.publicKeys, global.messages, global.signature, global.domain); + }; + + return { + testFunctions: [verifyInvalidAggregatedSignature], + setup: function() { + const {Keypair, aggregateSignatures} = require("../../../src"); + const {sha256} = require('js-sha256'); + const signatures = []; + global.publicKeys = []; + const message = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + const message2 = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + for(let i = 0; i < 128; i++) { + const keypair = Keypair.generate(); + global.publicKeys.push(keypair.publicKey.toBytesCompressed()); + signatures.push(keypair.privateKey.signMessage(Buffer.from(message), global.domain).toBytesCompressed()); + } + global.messages = global.publicKeys.map(() => message2); + global.signature = aggregateSignatures(signatures); + }, + file: dir + FUNCTION_NAME + ".txt" + }; +} \ No newline at end of file diff --git a/test/benchmarks/suites/verifyValidAggregatedSignature.ts b/test/benchmarks/suites/verifyValidAggregatedSignature.ts new file mode 100644 index 0000000..b0efb17 --- /dev/null +++ b/test/benchmarks/suites/verifyValidAggregatedSignature.ts @@ -0,0 +1,48 @@ +import {BenchSuite} from "@chainsafe/benchmark-utils"; +import {aggregateSignatures, verifyMultiple} from "../../../src"; + + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace global { + export let domain: Buffer; + export let messages: Buffer[]; + export let signature: Buffer; + export let publicKeys: Buffer[]; + export let verify: Function; +} + +// @ts-ignore +global.require = require; + +global.domain = Buffer.alloc(8); +global.verify = verifyMultiple; + +export function verifyValidAggregatedSignature(dir: string): BenchSuite { + + // Set the function test + const FUNCTION_NAME = "verifyValidAggregatedSignature"; // PLEASE FILL THIS OUT + + const verifyValidAggregatedSignature = function (): void { + global.verify(global.publicKeys, global.messages, global.signature, global.domain); + }; + + return { + testFunctions: [verifyValidAggregatedSignature], + setup: function() { + const {Keypair, aggregateSignatures} = require("../../../src"); + const {sha256} = require('js-sha256'); + const signatures = []; + global.publicKeys = []; + const message = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + for(let i = 0; i < 128; i++) { + const keypair = Keypair.generate(); + global.publicKeys.push(keypair.publicKey.toBytesCompressed()); + signatures.push(keypair.privateKey.signMessage(Buffer.from(message), global.domain).toBytesCompressed()); + } + global.messages = global.publicKeys.map(() => message); + global.signature = aggregateSignatures(signatures); + global.publicKeys.map(() => message); + }, + file: dir + FUNCTION_NAME + ".txt" + }; +} \ No newline at end of file diff --git a/test/benchmarks/suites/verifyValidSignature.ts b/test/benchmarks/suites/verifyValidSignature.ts new file mode 100644 index 0000000..a57d766 --- /dev/null +++ b/test/benchmarks/suites/verifyValidSignature.ts @@ -0,0 +1,43 @@ +/* eslint-disable @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires */ + +import {BenchSuite} from "@chainsafe/benchmark-utils"; +import {verify} from "../../../src"; + + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace global { + export let domain: Buffer; + export let message: Buffer; + export let signature: Buffer; + export let publicKey: Buffer; + export let verify: Function; +} + +// @ts-ignore +global.require = require; + +global.domain = Buffer.alloc(8); +global.verify = verify; + +export function verifyValidSignatureBenchmark(dir: string): BenchSuite { + + // Set the function test + const FUNCTION_NAME = "verifyValidSignature"; // PLEASE FILL THIS OUT + + const verifyValidSignature = function (): void { + global.verify(global.publicKey, global.message, global.signature, global.domain); + }; + + return { + testFunctions: [verifyValidSignature], + setup: function() { + const {Keypair} = require("../../../src"); + const {sha256} = require('js-sha256'); + const keypair = Keypair.generate(); + global.publicKey = keypair.publicKey.toBytesCompressed(); + global.message = Buffer.from(sha256.arrayBuffer(Math.random().toString(36))); + global.signature = keypair.privateKey.signMessage(Buffer.from(global.message), global.domain).toBytesCompressed(); + }, + file: dir + FUNCTION_NAME + ".txt" + }; +} \ No newline at end of file