bls uses spec tests cases from npm module

This commit is contained in:
Marin Petrunić 2019-09-29 10:47:17 +02:00
parent 2725e3a518
commit 0a21bfb8cd
7 changed files with 69 additions and 51 deletions

View File

@ -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",

View File

@ -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<AggregatePubKeysTestCase, string>(
describeDirectorySpecTest<IAggregatePubKeysTestCase, string>(
"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: {

View File

@ -11,12 +11,15 @@ interface AggregateSigsTestCase {
describeDirectorySpecTest<AggregateSigsTestCase, string>(
"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: {

View File

@ -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<MsgHHashCOmpressed, string>(
describeDirectorySpecTest<IMsgHHashCOmpressed, string>(
"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");
})
}
);

View File

@ -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<MsgHHashUnCompressed, string>(
describeDirectorySpecTest<IMsgHHashUnCompressed, string>(
"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");
})
}
);

View File

@ -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<PrivToPubTestCase, string>(
"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<IPrivToPubTestCase, string>(
"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)
}
);

View File

@ -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<SignMessageTestCase, string>(
describeDirectorySpecTest<ISignMessageTestCase, string>(
"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")}`;
}),