From aca18fbcda95bd1be36bd09ced4742efdccb2228 Mon Sep 17 00:00:00 2001 From: dapplion Date: Fri, 13 Nov 2020 21:43:20 +0000 Subject: [PATCH] Test blst-ts with spec tests --- package.json | 1 + test/spec-blst/aggregate_sigs.test.ts | 29 ++++++++++++++ test/spec-blst/aggregate_sigs_verify.test.ts | 39 ++++++++++++++++++ test/spec-blst/fast_aggregate_verify.test.ts | 42 ++++++++++++++++++++ test/spec-blst/sign.test.ts | 34 ++++++++++++++++ test/spec-blst/verify.test.ts | 39 ++++++++++++++++++ test/util.ts | 7 ++++ yarn.lock | 3 ++ 8 files changed, 194 insertions(+) create mode 100644 test/spec-blst/aggregate_sigs.test.ts create mode 100644 test/spec-blst/aggregate_sigs_verify.test.ts create mode 100644 test/spec-blst/fast_aggregate_verify.test.ts create mode 100644 test/spec-blst/sign.test.ts create mode 100644 test/spec-blst/verify.test.ts create mode 100644 test/util.ts diff --git a/package.json b/package.json index 65ba239..996b109 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ }, "dependencies": { "@chainsafe/bls-keygen": "^0.2.0", + "@chainsafe/blst-ts": "file:../blst-ts", "@chainsafe/eth2-bls-wasm": "^0.5.0", "assert": "^1.4.1" }, diff --git a/test/spec-blst/aggregate_sigs.test.ts b/test/spec-blst/aggregate_sigs.test.ts new file mode 100644 index 0000000..80a4eed --- /dev/null +++ b/test/spec-blst/aggregate_sigs.test.ts @@ -0,0 +1,29 @@ +import path from "path"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; +import {AggregateSignature} from "@chainsafe/blst-ts"; +import {fromHexString, toHexString} from "../util"; + +interface IAggregateSigsTestCase { + data: { + input: string[]; + output: string; + }; +} + +describeDirectorySpecTest( + "BLS - aggregate sigs", + path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate/small"), + (testCase) => { + const signaturesHex = testCase.data.input; + const signaturesBytes = signaturesHex.map(fromHexString); + const aggSig = AggregateSignature.fromSignaturesBytes(signaturesBytes); + const aggSigHex = aggSig.toSignature().toBytes(); + return toHexString(aggSigHex); + }, + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase) => testCase.data.output, + } +); diff --git a/test/spec-blst/aggregate_sigs_verify.test.ts b/test/spec-blst/aggregate_sigs_verify.test.ts new file mode 100644 index 0000000..de4cb74 --- /dev/null +++ b/test/spec-blst/aggregate_sigs_verify.test.ts @@ -0,0 +1,39 @@ +import path from "path"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; +import {aggregateVerify, Signature, PublicKey} from "@chainsafe/blst-ts"; +import {fromHexString} from "../util"; + +interface IAggregateSigsVerifyTestCase { + data: { + input: { + pubkeys: string[]; + messages: string[]; + signature: string; + }; + output: boolean; + }; +} + +describeDirectorySpecTest( + "BLS - aggregate sigs verify", + path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate_verify/small"), + (testCase) => { + const {pubkeys, messages, signature} = testCase.data.input; + + try { + const msgs = messages.map(fromHexString); + const pks = pubkeys.map((pubkey) => PublicKey.fromBytes(fromHexString(pubkey))); + const sig = Signature.fromBytes(fromHexString(signature)); + + return aggregateVerify(msgs, pks, sig); + } catch (e) { + return false; + } + }, + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase) => testCase.data.output, + } +); diff --git a/test/spec-blst/fast_aggregate_verify.test.ts b/test/spec-blst/fast_aggregate_verify.test.ts new file mode 100644 index 0000000..2982a73 --- /dev/null +++ b/test/spec-blst/fast_aggregate_verify.test.ts @@ -0,0 +1,42 @@ +import path from "path"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; +import {fastAggregateVerify, Signature, PublicKey} from "@chainsafe/blst-ts"; +import {fromHexString} from "../util"; + +interface IAggregateSigsVerifyTestCase { + data: { + input: { + pubkeys: string[]; + message: string; + signature: string; + }; + output: boolean; + }; +} + +describeDirectorySpecTest( + "BLS - aggregate sigs verify", + path.join( + __dirname, + "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/fast_aggregate_verify/small" + ), + (testCase) => { + const {pubkeys, message, signature} = testCase.data.input; + + try { + const msg = fromHexString(message); + const pks = pubkeys.map((pubkey) => PublicKey.fromBytes(fromHexString(pubkey))); + const sig = Signature.fromBytes(fromHexString(signature)); + + return fastAggregateVerify(msg, pks, sig); + } catch (e) { + return false; + } + }, + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase) => testCase.data.output, + } +); diff --git a/test/spec-blst/sign.test.ts b/test/spec-blst/sign.test.ts new file mode 100644 index 0000000..8108ad8 --- /dev/null +++ b/test/spec-blst/sign.test.ts @@ -0,0 +1,34 @@ +import path from "path"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; +import {SecretKey} from "@chainsafe/blst-ts"; +import {fromHexString, toHexString} from "../util"; + +interface ISignMessageTestCase { + data: { + input: { + privkey: string; + message: string; + }; + output: string; + }; +} + +describeDirectorySpecTest( + "BLS - sign", + path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/sign/small"), + (testCase) => { + const {privkey, message} = testCase.data.input; + + const sk = SecretKey.fromBytes(fromHexString(privkey)); + const msg = fromHexString(message); + const sig = sk.sign(msg); + + return toHexString(sig.toBytes()); + }, + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase) => testCase.data.output, + } +); diff --git a/test/spec-blst/verify.test.ts b/test/spec-blst/verify.test.ts new file mode 100644 index 0000000..fd914ac --- /dev/null +++ b/test/spec-blst/verify.test.ts @@ -0,0 +1,39 @@ +import path from "path"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util"; +import {verify, Signature, PublicKey} from "@chainsafe/blst-ts"; +import {fromHexString} from "../util"; + +interface IVerifyTestCase { + data: { + input: { + pubkey: string; + message: string; + signature: string; + }; + output: boolean; + }; +} + +describeDirectorySpecTest( + "BLS - verify", + path.join(__dirname, "../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/verify/small"), + (testCase) => { + const {pubkey, message, signature} = testCase.data.input; + + try { + const msg = fromHexString(message); + const pk = PublicKey.fromBytes(fromHexString(pubkey)); + const sig = Signature.fromBytes(fromHexString(signature)); + + return verify(msg, pk, sig); + } catch (e) { + return false; + } + }, + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase) => testCase.data.output, + } +); diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 0000000..50478de --- /dev/null +++ b/test/util.ts @@ -0,0 +1,7 @@ +export function fromHexString(hex: string): Buffer { + return Buffer.from(hex.replace("0x", ""), "hex"); +} + +export function toHexString(bytes: Buffer | Uint8Array): string { + return `0x${Buffer.from(bytes).toString("hex")}`; +} diff --git a/yarn.lock b/yarn.lock index 296f5d6..dd7013f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -820,6 +820,9 @@ bip39 "^3.0.2" buffer "^5.4.3" +"@chainsafe/blst-ts@file:../blst-ts": + version "1.0.0" + "@chainsafe/eth2-bls-wasm@^0.5.0": version "0.5.0" resolved "https://registry.yarnpkg.com/@chainsafe/eth2-bls-wasm/-/eth2-bls-wasm-0.5.0.tgz#45d0cb8807b569537d1e0099922a9617e0410b3a"