From 0a21bfb8cd8deb5ddccd9c2338567240edcec487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Sun, 29 Sep 2019 10:47:17 +0200 Subject: [PATCH] bls uses spec tests cases from npm module --- package.json | 2 +- test/spec/aggregate_pubkeys.test.ts | 11 ++++--- test/spec/aggregate_sigs.test.ts | 7 +++-- test/spec/msg_hash_compressed.test.ts | 21 +++++++------ test/spec/msg_hash_uncompressed.test.ts | 29 +++++++++--------- test/spec/priv_to_public.test.ts | 39 +++++++++++++------------ test/spec/sign_message.test.ts | 11 ++++--- 7 files changed, 69 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 7b14308..34be5a4 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "lint:fix": "eslint --ext .ts src/ --fix", "pretest": "yarn check-types", "prepublishOnly": "yarn build", - "test:unit": "nyc --cache-dir .nyc_output/.cache -r lcov -e .ts mocha --colors -r ./.babel-register 'test/unit/**/*.test.ts' && nyc report", + "test:unit": "nyc --cache-dir .nyc_output/.cache -r lcov -e .ts mocha --colors -r ts-node/register 'test/unit/**/*.test.ts' && nyc report", "test:spec": "mocha --colors -r ts-node/register 'test/spec/**/*.test.ts'", "test:spec-min": "yarn run test:spec", "test": "yarn test:unit && yarn test:spec", diff --git a/test/spec/aggregate_pubkeys.test.ts b/test/spec/aggregate_pubkeys.test.ts index 76d7b03..753471d 100644 --- a/test/spec/aggregate_pubkeys.test.ts +++ b/test/spec/aggregate_pubkeys.test.ts @@ -2,21 +2,24 @@ import bls from "../../src"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; import path from "path"; -interface AggregatePubKeysTestCase { +interface IAggregatePubKeysTestCase { data: { input: string[]; output: string; }; } -describeDirectorySpecTest( +describeDirectorySpecTest( "aggregate pubkeys", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/aggregate_pubkeys/small"), + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate_pubkeys/small" + ), (testCase => { const result = bls.aggregatePubkeys(testCase.data.input.map(pubKey => { return Buffer.from(pubKey.replace("0x", ""), "hex"); })); - return `0x${result.toString('hex')}`; + return `0x${result.toString("hex")}`; }), { inputTypes: { diff --git a/test/spec/aggregate_sigs.test.ts b/test/spec/aggregate_sigs.test.ts index 4df88df..62c84aa 100644 --- a/test/spec/aggregate_sigs.test.ts +++ b/test/spec/aggregate_sigs.test.ts @@ -11,12 +11,15 @@ interface AggregateSigsTestCase { describeDirectorySpecTest( "aggregate sigs", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/aggregate_sigs/small"), + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/aggregate_sigs/small" + ), (testCase => { const result = bls.aggregateSignatures(testCase.data.input.map(pubKey => { return Buffer.from(pubKey.replace("0x", ""), "hex"); })); - return `0x${result.toString('hex')}`; + return `0x${result.toString("hex")}`; }), { inputTypes: { diff --git a/test/spec/msg_hash_compressed.test.ts b/test/spec/msg_hash_compressed.test.ts index 31b81a7..ef758dd 100644 --- a/test/spec/msg_hash_compressed.test.ts +++ b/test/spec/msg_hash_compressed.test.ts @@ -3,7 +3,7 @@ import {padLeft} from "../../src/helpers/utils"; import {G2point} from "../../src/helpers/g2point"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; -interface MsgHHashCOmpressed { +interface IMsgHHashCOmpressed { data: { input: { message: string; @@ -13,23 +13,26 @@ interface MsgHHashCOmpressed { }; } -describeDirectorySpecTest( +describeDirectorySpecTest( "msg_hash_compressed", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_compressed/small"), + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/msg_hash_compressed/small" + ), (testCase => { - const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); - const input = Buffer.from(testCase.data.input.message.replace('0x', ''), "hex"); + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace("0x", ""), "hex"), 8); + const input = Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"); const result = G2point.hashToG2(input, domain); - return `0x${result.toBytesCompressed().toString('hex')}`; + return `0x${result.toBytesCompressed().toString("hex")}`; }), { inputTypes: { data: InputType.YAML, }, getExpected: (testCase => { - const xReExpected = padLeft(Buffer.from(testCase.data.output[0].replace('0x', ''), 'hex'), 48); - const xImExpected = padLeft(Buffer.from(testCase.data.output[1].replace('0x', ''), 'hex'), 48); - return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex'); + const xReExpected = padLeft(Buffer.from(testCase.data.output[0].replace("0x", ""), "hex"), 48); + const xImExpected = padLeft(Buffer.from(testCase.data.output[1].replace("0x", ""), "hex"), 48); + return "0x" + Buffer.concat([xReExpected, xImExpected]).toString("hex"); }) } ); \ No newline at end of file diff --git a/test/spec/msg_hash_uncompressed.test.ts b/test/spec/msg_hash_uncompressed.test.ts index b589909..4da8e81 100644 --- a/test/spec/msg_hash_uncompressed.test.ts +++ b/test/spec/msg_hash_uncompressed.test.ts @@ -3,7 +3,7 @@ import {padLeft} from "../../src/helpers/utils"; import {G2point} from "../../src/helpers/g2point"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; -interface MsgHHashUnCompressed { +interface IMsgHHashUnCompressed { data: { input: { message: string; @@ -13,28 +13,31 @@ interface MsgHHashUnCompressed { }; } -describeDirectorySpecTest( +describeDirectorySpecTest( "msg_hash_uncompressed", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_uncompressed/small"), + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/msg_hash_uncompressed/small" + ), (testCase => { - const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace("0x", ""), "hex"), 8); const input = Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"); const result = G2point.hashToG2(input, domain); - return `0x${result.toBytesCompressed().toString('hex')}`; + return `0x${result.toBytesCompressed().toString("hex")}`; }), { inputTypes: { data: InputType.YAML, }, getExpected: (testCase => { - return '0x' + G2point.fromUncompressedInput( - Buffer.from(testCase.data.output[0][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[0][1].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[1][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[1][1].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[2][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[2][1].replace('0x', ''), 'hex'), - ).toBytesCompressed().toString('hex'); + return "0x" + G2point.fromUncompressedInput( + Buffer.from(testCase.data.output[0][0].replace("0x", ""), "hex"), + Buffer.from(testCase.data.output[0][1].replace("0x", ""), "hex"), + Buffer.from(testCase.data.output[1][0].replace("0x", ""), "hex"), + Buffer.from(testCase.data.output[1][1].replace("0x", ""), "hex"), + Buffer.from(testCase.data.output[2][0].replace("0x", ""), "hex"), + Buffer.from(testCase.data.output[2][1].replace("0x", ""), "hex"), + ).toBytesCompressed().toString("hex"); }) } ); \ No newline at end of file diff --git a/test/spec/priv_to_public.test.ts b/test/spec/priv_to_public.test.ts index edd6c0d..dfb3218 100644 --- a/test/spec/priv_to_public.test.ts +++ b/test/spec/priv_to_public.test.ts @@ -2,24 +2,27 @@ import bls from "../../src"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; import path from "path"; -interface PrivToPubTestCase { - data: { - input: string; - output: string; - }; +interface IPrivToPubTestCase { + data: { + input: string; + output: string; + }; } -describeDirectorySpecTest( - "priv_to_pub", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/priv_to_pub/small"), - (testCase => { - const result = bls.generatePublicKey(Buffer.from(testCase.data.input.replace('0x', ''), 'hex')); - return `0x${result.toString('hex')}`; - }), - { - inputTypes: { - data: InputType.YAML, - }, - getExpected: (testCase => testCase.data.output) - } +describeDirectorySpecTest( + "priv_to_pub", + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/priv_to_pub/small" + ), + (testCase => { + const result = bls.generatePublicKey(Buffer.from(testCase.data.input.replace("0x", ""), "hex")); + return `0x${result.toString("hex")}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } ); diff --git a/test/spec/sign_message.test.ts b/test/spec/sign_message.test.ts index 5aebb9e..3ca0154 100644 --- a/test/spec/sign_message.test.ts +++ b/test/spec/sign_message.test.ts @@ -3,7 +3,7 @@ import bls from "../../src"; import {padLeft} from "../../src/helpers/utils"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; -interface SignMessageTestCase { +interface ISignMessageTestCase { data: { input: { privkey: string; @@ -14,14 +14,17 @@ interface SignMessageTestCase { }; } -describeDirectorySpecTest( +describeDirectorySpecTest( "priv_to_pub", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/sign_msg/small"), + path.join( + __dirname, + "../../../../node_modules/@chainsafe/eth2-spec-tests/tests/general/phase0/bls/sign_msg/small" + ), (testCase => { const signature = bls.sign( Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"), Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"), - padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8) + padLeft(Buffer.from(testCase.data.input.domain.replace("0x", ""), "hex"), 8) ); return `0x${signature.toString("hex")}`; }),