improve bls spec tests

This commit is contained in:
Marin Petrunić 2019-09-04 13:35:34 +02:00
parent 359cc185e4
commit 98ec4bb402
8 changed files with 90 additions and 91 deletions

View File

@ -27,8 +27,9 @@
"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 -r ./.babel-register 'test/unit/**/*.test.ts' && nyc report",
"test:spec": "mocha -r ./.babel-register 'test/spec/**/*.test.ts'",
"test:unit": "nyc --cache-dir .nyc_output/.cache -r lcov -e .ts mocha --colors -r ./.babel-register 'test/unit/**/*.test.ts' && nyc report",
"test:spec": "mocha --colors -r ./.babel-register 'test/spec/**/*.test.ts'",
"test:spec-min": "yarn run test:spec",
"test": "yarn test:unit && yarn test:spec",
"coverage": "codecov -F bls",
"benchmark": "node -r ./.babel-register test/benchmarks"

View File

@ -1,21 +1,21 @@
import {join} from "path";
import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util";
import bls from "../../src";
import {BLSSignature} from "../../src/types";
import {BLSSignature} from "@chainsafe/eth2.0-types";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/aggregate_pubkeys/aggregate_pubkeys.yaml"),
bls.aggregatePubkeys,
({input}) => {
const sigs: BLSSignature[] = [];
input.forEach((sig: string) => {
sigs.push(Buffer.from(sig.replace('0x', ''), 'hex'))
});
return [
sigs
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/aggregate_pubkeys/aggregate_pubkeys.yaml"),
bls.aggregatePubkeys,
({input}) => {
const sigs: BLSSignature[] = [];
input.forEach((sig: string) => {
sigs.push(Buffer.from(sig.replace('0x', ''), 'hex'));
});
return [
sigs
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
);

View File

@ -1,22 +1,21 @@
import {join} from "path";
import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util";
import bls from "../../src";
import {G2point} from "../../src/helpers/g2point";
import {BLSPubkey} from "../../src/types";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/aggregate_sigs/aggregate_sigs.yaml"),
bls.aggregateSignatures,
({input}) => {
const pubKeys: BLSPubkey[] = [];
input.forEach((pubKey: string) => {
pubKeys.push(Buffer.from(pubKey.replace('0x', ''), 'hex'))
});
return [
pubKeys
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/aggregate_sigs/aggregate_sigs.yaml"),
bls.aggregateSignatures,
({input}) => {
const pubKeys: BLSPubkey[] = [];
input.forEach((pubKey: string) => {
pubKeys.push(Buffer.from(pubKey.replace('0x', ''), 'hex'));
});
return [
pubKeys
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
);

View File

@ -4,20 +4,20 @@ import {padLeft} from "../../src/helpers/utils";
import {G2point} from "../../src/helpers/g2point";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/msg_hash_g2_compressed/g2_compressed.yaml"),
G2point.hashToG2,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => {
const xReExpected = padLeft(Buffer.from(output[0].replace('0x', ''), 'hex'), 48);
const xImExpected = padLeft(Buffer.from(output[1].replace('0x', ''), 'hex'), 48);
return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex')
},
(result:G2point) => `0x${result.toBytesCompressed().toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/msg_hash_g2_compressed/g2_compressed.yaml"),
G2point.hashToG2,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => {
const xReExpected = padLeft(Buffer.from(output[0].replace('0x', ''), 'hex'), 48);
const xImExpected = padLeft(Buffer.from(output[1].replace('0x', ''), 'hex'), 48);
return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex');
},
(result: G2point) => `0x${result.toBytesCompressed().toString('hex')}`,
() => false,
);

View File

@ -4,25 +4,25 @@ import {padLeft} from "../../src/helpers/utils";
import {G2point} from "../../src/helpers/g2point";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/msg_hash_g2_uncompressed/g2_uncompressed.yaml"),
G2point.hashToG2,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => {
return '0x' + G2point.fromUncompressedInput(
Buffer.from(output[0][0].replace('0x', ''), 'hex'),
Buffer.from(output[0][1].replace('0x', ''), 'hex'),
Buffer.from(output[1][0].replace('0x', ''), 'hex'),
Buffer.from(output[1][1].replace('0x', ''), 'hex'),
Buffer.from(output[2][0].replace('0x', ''), 'hex'),
Buffer.from(output[2][1].replace('0x', ''), 'hex'),
).toBytesCompressed().toString('hex');
},
(result:G2point) => `0x${result.toBytesCompressed().toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/msg_hash_g2_uncompressed/g2_uncompressed.yaml"),
G2point.hashToG2,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => {
return '0x' + G2point.fromUncompressedInput(
Buffer.from(output[0][0].replace('0x', ''), 'hex'),
Buffer.from(output[0][1].replace('0x', ''), 'hex'),
Buffer.from(output[1][0].replace('0x', ''), 'hex'),
Buffer.from(output[1][1].replace('0x', ''), 'hex'),
Buffer.from(output[2][0].replace('0x', ''), 'hex'),
Buffer.from(output[2][1].replace('0x', ''), 'hex'),
).toBytesCompressed().toString('hex');
},
(result: G2point) => `0x${result.toBytesCompressed().toString('hex')}`,
() => false,
);

View File

@ -3,12 +3,12 @@ import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util";
import bls from "../../src";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/priv_to_pub/priv_to_pub.yaml"),
bls.generatePublicKey,
({input}) => {
return [Buffer.from(input.replace('0x', ''), 'hex')];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/priv_to_pub/priv_to_pub.yaml"),
bls.generatePublicKey,
({input}) => {
return [Buffer.from(input.replace('0x', ''), 'hex')];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
);

View File

@ -4,17 +4,17 @@ import bls from "../../src";
import {padLeft} from "../../src/helpers/utils";
describeSpecTest(
join(__dirname, "./spec-tests/tests/bls/sign_msg/sign_msg.yaml"),
bls.sign,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.privkey.replace('0x', ''), 'hex'),
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
join(__dirname, "../../../spec-test-cases/tests/bls/sign_msg/sign_msg.yaml"),
bls.sign,
({input}) => {
const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8);
return [
Buffer.from(input.privkey.replace('0x', ''), 'hex'),
Buffer.from(input.message.replace('0x', ''), 'hex'),
domain
];
},
({output}) => output,
(result) => `0x${result.toString('hex')}`,
() => false,
);

@ -1 +0,0 @@
Subproject commit 7567342c966c4e020f6ab3889f93cedb65ea9bfe