Test blst-ts with spec tests

This commit is contained in:
dapplion 2020-11-13 21:43:20 +00:00
parent 7ace4c55cc
commit aca18fbcda
8 changed files with 194 additions and 0 deletions

View File

@ -40,6 +40,7 @@
}, },
"dependencies": { "dependencies": {
"@chainsafe/bls-keygen": "^0.2.0", "@chainsafe/bls-keygen": "^0.2.0",
"@chainsafe/blst-ts": "file:../blst-ts",
"@chainsafe/eth2-bls-wasm": "^0.5.0", "@chainsafe/eth2-bls-wasm": "^0.5.0",
"assert": "^1.4.1" "assert": "^1.4.1"
}, },

View File

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

View File

@ -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<IAggregateSigsVerifyTestCase, boolean>(
"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,
}
);

View File

@ -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<IAggregateSigsVerifyTestCase, boolean>(
"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,
}
);

View File

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

View File

@ -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<IVerifyTestCase, boolean>(
"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,
}
);

7
test/util.ts Normal file
View File

@ -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")}`;
}

View File

@ -820,6 +820,9 @@
bip39 "^3.0.2" bip39 "^3.0.2"
buffer "^5.4.3" buffer "^5.4.3"
"@chainsafe/blst-ts@file:../blst-ts":
version "1.0.0"
"@chainsafe/eth2-bls-wasm@^0.5.0": "@chainsafe/eth2-bls-wasm@^0.5.0":
version "0.5.0" version "0.5.0"
resolved "https://registry.yarnpkg.com/@chainsafe/eth2-bls-wasm/-/eth2-bls-wasm-0.5.0.tgz#45d0cb8807b569537d1e0099922a9617e0410b3a" resolved "https://registry.yarnpkg.com/@chainsafe/eth2-bls-wasm/-/eth2-bls-wasm-0.5.0.tgz#45d0cb8807b569537d1e0099922a9617e0410b3a"