Skip to content

Commit

Permalink
wip: test failing for a seemingly decent reason
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed May 9, 2022
1 parent 3aa7203 commit 927537c
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 164 deletions.
18 changes: 9 additions & 9 deletions src/bindings.ts
@@ -1,21 +1,21 @@
import {getBinaryPath} from "./scripts/paths";
// eslint-disable-next-line
export const blst: Blst = require(getBinaryPath());
// export const blst: Blst = await import("./blst/build/blst.js");
// @ts-ignore
export const blst: Blst = require("../blst/build/blst.js");

export interface Blst {
BLS12_381_G1: P1_Affine;
BLS12_381_NEG_G1: P1_Affine;
BLS12_381_G2: P2_Affine;
BLS12_381_NEG_G2: P2_Affine;
// BLS12_381_G1: P1_Affine;
// BLS12_381_NEG_G1: P1_Affine;
// BLS12_381_G2: P2_Affine;
// BLS12_381_NEG_G2: P2_Affine;
SecretKey: SecretKeyConstructor;
P1_Affine: P1_AffineConstructor;
P2_Affine: P2_AffineConstructor;
P1: P1Constructor;
P2: P2Constructor;
PT: PTConstructor;
Pairing: PairingConstructor;
G1(): P1;
G2(): P2;
// G1(): P1;
// G2(): P2;
}

// blst.hpp types
Expand Down
2 changes: 2 additions & 0 deletions test/browser/.gitignore
@@ -0,0 +1,2 @@
bundle.js*
blst.wasm
27 changes: 27 additions & 0 deletions test/browser/index.html
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>

<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>

<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="bundle.js"></script>
<script class="mocha-exec">
// TODO: more robust wait for `Module` to initialize
setTimeout(() => {
mocha.run();
}, 600);
</script>
</body>
</html>
265 changes: 134 additions & 131 deletions test/unit/bindings/P1.test.ts
@@ -1,144 +1,147 @@
import {blst, BLST_ERROR, P1, P1_Affine, P1Constructor} from "../../../src/bindings";
import {expectHex, fromHex, runInstanceTestCases} from "../../utils";

describe("P1", () => {
const sample = {
keygen: "********************************", // Must be at least 32 bytes
p1: fromHex(
"0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f0109301f36856007e1bc875ab1b00dbf47f9ead16c5562d889d8b270002ade81e78d473204fcb51ede8659bce3d95c67903bc"
),
p1Comp: fromHex("8ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093"),
};
// TODO: more robust wait for `Module` to initialize
setTimeout(() => {
describe("P1", () => {
const sample = {
keygen: "********************************", // Must be at least 32 bytes
p1: fromHex(
"0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f0109301f36856007e1bc875ab1b00dbf47f9ead16c5562d889d8b270002ade81e78d473204fcb51ede8659bce3d95c67903bc"
),
p1Comp: fromHex("8ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093"),
};

const sk = new blst.SecretKey();
sk.keygen(sample.keygen);
const p1 = new blst.P1(sk);
const p1Affine = new blst.P1_Affine(p1);
const sk = new blst.SecretKey();
sk.keygen(sample.keygen);
const p1 = new blst.P1(sk);
const p1Affine = new blst.P1_Affine(p1);

const msg = "assertion"; // this what we're signing
const DST = "MY-DST"; // domain separation tag
const msg = "assertion"; // this what we're signing
const DST = "MY-DST"; // domain separation tag

describe("P1", () => {
it("From serialized", () => {
expectHex(new blst.P1(sample.p1).serialize(), sample.p1);
});
it("From compressed", () => {
expectHex(new blst.P1(sample.p1Comp).serialize(), sample.p1);
describe("P1", () => {
it("From serialized", () => {
expectHex(new blst.P1(sample.p1).serialize(), sample.p1);
});
it("From compressed", () => {
expectHex(new blst.P1(sample.p1Comp).serialize(), sample.p1);
});

runInstanceTestCases<P1>(
{
dup: [{args: [], res: new blst.P1(sk)}],
to_affine: [{args: [], res: p1Affine}],
serialize: [{args: [], res: sample.p1}],
compress: [{args: [], res: sample.p1Comp}],
on_curve: [{args: [], res: true}],
in_group: [{args: [], res: true}],
is_inf: [{args: [], res: false}],
is_equal: [{args: [new blst.P1(sk)], res: true}],
// TODO: Skip tests for now
aggregate: [],
sign_with: [
{
args: [sk],
res: "0c3584dae7a2955145df1e792031e13b7bc1df4c033f59275ad3ecd2d6e6abc198bb5b76fa536b1689ddec11b191b25512eb69e2383a632201809a1fb67deede5aabe258726607c97bc5e17b30b4d26a4fd43cb7bb823d36a5c405a8ec1adc88" as any,
},
],
hash_to: [
{
args: [msg, DST, p1.serialize()],
res: "0c52b57688e9659b326258de28f055137fbf0a408aa646f66fd6898afaa0c6b46d6e0974a4741d6b712b4b9b283159a617d60420d044b26613893c96c83029fae39264ba803fb08267db92b260e1878a438bb988826383ed1121ee27df8dc4b0" as any,
},
],
encode_to: [
{
args: [msg, DST, p1.serialize()],
res: "01fa4ac72140c2f714c135fff2bc3e44f19e6aa65f7ed5d1329d4e404c83e0479bba01f212a17341e325b538fd8c673d054fe059092979fcf6cecf2e81b5a35bc2735b329c446820bb3b7f5bd227c284c7e99712f07fbccd4ae08bc27280d502" as any,
},
],
mult: [
{
// BigIng works fine but breaks in older versions of node
args: [new Uint8Array([2])],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
cneg: [
{
args: [true],
res: "0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093180da9943901cad1d5708cb567572d38b760862ec5fc75344030cff30e927d4fab8bb0335f66179a1e30c26a3986a6ef" as any,
},
],
neg: [
{
args: [],
res: "0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093180da9943901cad1d5708cb567572d38b760862ec5fc75344030cff30e927d4fab8bb0335f66179a1e30c26a3986a6ef" as any,
},
],
add: [
{
args: [p1Affine],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
dbl: [
{
args: [],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
},
function getP1() {
return new blst.P1(sk);
}
);
});

runInstanceTestCases<P1>(
{
dup: [{args: [], res: new blst.P1(sk)}],
to_affine: [{args: [], res: p1Affine}],
serialize: [{args: [], res: sample.p1}],
compress: [{args: [], res: sample.p1Comp}],
on_curve: [{args: [], res: true}],
in_group: [{args: [], res: true}],
is_inf: [{args: [], res: false}],
is_equal: [{args: [new blst.P1(sk)], res: true}],
// TODO: Skip tests for now
aggregate: [],
sign_with: [
{
args: [sk],
res: "0c3584dae7a2955145df1e792031e13b7bc1df4c033f59275ad3ecd2d6e6abc198bb5b76fa536b1689ddec11b191b25512eb69e2383a632201809a1fb67deede5aabe258726607c97bc5e17b30b4d26a4fd43cb7bb823d36a5c405a8ec1adc88" as any,
},
],
hash_to: [
{
args: [msg, DST, p1.serialize()],
res: "0c52b57688e9659b326258de28f055137fbf0a408aa646f66fd6898afaa0c6b46d6e0974a4741d6b712b4b9b283159a617d60420d044b26613893c96c83029fae39264ba803fb08267db92b260e1878a438bb988826383ed1121ee27df8dc4b0" as any,
},
],
encode_to: [
{
args: [msg, DST, p1.serialize()],
res: "01fa4ac72140c2f714c135fff2bc3e44f19e6aa65f7ed5d1329d4e404c83e0479bba01f212a17341e325b538fd8c673d054fe059092979fcf6cecf2e81b5a35bc2735b329c446820bb3b7f5bd227c284c7e99712f07fbccd4ae08bc27280d502" as any,
},
],
mult: [
{
// BigIng works fine but breaks in older versions of node
args: [new Uint8Array([2])],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
cneg: [
{
args: [true],
res: "0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093180da9943901cad1d5708cb567572d38b760862ec5fc75344030cff30e927d4fab8bb0335f66179a1e30c26a3986a6ef" as any,
},
],
neg: [
{
args: [],
res: "0ae7e5822ba97ab07877ea318e747499da648b27302414f9d0b9bb7e3646d248be90c9fdaddfdb93485a6e9334f01093180da9943901cad1d5708cb567572d38b760862ec5fc75344030cff30e927d4fab8bb0335f66179a1e30c26a3986a6ef" as any,
},
],
add: [
{
args: [p1Affine],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
dbl: [
{
args: [],
res: "1995e17c244b8be52aae259364646381d9a0d3b4a83c45c40ccfce11952eb259007b07e5bf96a622acb6ae0a8b94063617a8a12cc833156f99c573b4416c21e50fca56be4df635e89f63ae81108becef79ac015f4d5cd33bea4484bf278f2e7c" as any,
},
],
},
function getP1() {
return new blst.P1(sk);
}
);
});
describe("P1_Affine", () => {
const p2 = new blst.P2(sk);
const p2Affine = new blst.P2_Affine(p2);

describe("P1_Affine", () => {
const p2 = new blst.P2(sk);
const p2Affine = new blst.P2_Affine(p2);
it("From serialized", () => {
expectHex(new blst.P1_Affine(sample.p1).serialize(), sample.p1);
});
it("From compressed", () => {
expectHex(new blst.P1_Affine(sample.p1Comp).serialize(), sample.p1);
});

it("From serialized", () => {
expectHex(new blst.P1_Affine(sample.p1).serialize(), sample.p1);
runInstanceTestCases<P1_Affine>(
{
dup: [{args: [], res: new blst.P1_Affine(p1)}],
to_jacobian: [{args: [], res: p1}],
serialize: [{args: [], res: sample.p1}],
compress: [{args: [], res: sample.p1Comp}],
on_curve: [{args: [], res: true}],
in_group: [{args: [], res: true}],
is_inf: [{args: [], res: false}],
is_equal: [{args: [new blst.P1_Affine(p1)], res: true}],
core_verify: [
{
args: [p2Affine, true, msg, DST],
res: BLST_ERROR.BLST_VERIFY_FAIL,
},
],
},
function getP1Affine() {
return new blst.P1_Affine(p1);
}
);
});
it("From compressed", () => {
expectHex(new blst.P1_Affine(sample.p1Comp).serialize(), sample.p1);
});

runInstanceTestCases<P1_Affine>(
{
dup: [{args: [], res: new blst.P1_Affine(p1)}],
to_jacobian: [{args: [], res: p1}],
serialize: [{args: [], res: sample.p1}],
compress: [{args: [], res: sample.p1Comp}],
on_curve: [{args: [], res: true}],
in_group: [{args: [], res: true}],
is_inf: [{args: [], res: false}],
is_equal: [{args: [new blst.P1_Affine(p1)], res: true}],
core_verify: [
{
args: [p2Affine, true, msg, DST],
res: BLST_ERROR.BLST_VERIFY_FAIL,
},
],
},
function getP1Affine() {
return new blst.P1_Affine(p1);
}
);
});

describe("static", () => {
runInstanceTestCases<P1Constructor>(
{
generator: [
{
args: [],
res: "17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" as any,
},
],
},
() => blst.P1
);
describe("static", () => {
runInstanceTestCases<P1Constructor>(
{
generator: [
{
args: [],
res: "17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" as any,
},
],
},
() => blst.P1
);
});
});
});
}, 500);
51 changes: 27 additions & 24 deletions test/unit/bindings/SecretKey.test.ts
@@ -1,28 +1,31 @@
import {blst, SecretKey} from "../../../src/bindings";
import {fromHex, runInstanceTestCases} from "../../utils";

describe("SecretKey", () => {
const sampleHex = fromHex("0000000000000000000000000000000000000000000000000000000000000001");
const zeroHex = fromHex("0000000000000000000000000000000000000000000000000000000000000000");
const info = "info";
// TODO: more robust wait for `Module` to initialize
setTimeout(() => {
describe("SecretKey", () => {
const sampleHex = fromHex("0000000000000000000000000000000000000000000000000000000000000001");
const zeroHex = fromHex("0000000000000000000000000000000000000000000000000000000000000000");
const info = "info";

runInstanceTestCases<SecretKey>(
{
keygen: [
{id: "from string", args: ["randomString".padEnd(32, "0")]},
{id: "from Buffer", args: [sampleHex]},
{id: "with two args", args: [sampleHex, info]},
{id: "from short ikm", args: [new Uint8Array([0])]},
// info param can't be a Uint8Array
// { id: "With two args", args: [sampleHex, zeroHex] },
],
from_bendian: [{args: [sampleHex]}],
from_lendian: [{args: [sampleHex]}],
to_bendian: [{args: [], res: zeroHex}],
to_lendian: [{args: [], res: zeroHex}],
},
function getSecretKey() {
return new blst.SecretKey();
}
);
});
runInstanceTestCases<SecretKey>(
{
keygen: [
{id: "from string", args: ["randomString".padEnd(32, "0")]},
{id: "from Buffer", args: [sampleHex]},
{id: "with two args", args: [sampleHex, info]},
{id: "from short ikm", args: [new Uint8Array([0])]},
// info param can't be a Uint8Array
// { id: "With two args", args: [sampleHex, zeroHex] },
],
from_bendian: [{args: [sampleHex]}],
from_lendian: [{args: [sampleHex]}],
to_bendian: [{args: [], res: zeroHex}],
to_lendian: [{args: [], res: zeroHex}],
},
function getSecretKey() {
return new blst.SecretKey();
}
);
});
}, 500);

0 comments on commit 927537c

Please sign in to comment.