This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/test/unit/multithread/naive/naive.test.ts

66 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import {expect} from "chai";
2022-04-14 17:16:06 +00:00
import {IBls} from "../../../../src/types.js";
import type {PublicKey, Signature} from "../../../../src/types.js";
import {BlsMultiThreadNaive} from "./index.js";
export function runMultithreadTests(bls: IBls): void {
describe("bls pool naive", function () {
2021-04-05 00:22:29 +00:00
// Starting all threads may take a while due to ts-node compilation
this.timeout(20 * 1000);
const n = 16;
let pool: BlsMultiThreadNaive;
before("Create pool and warm-up wallets", async function () {
pool = new BlsMultiThreadNaive(bls.implementation);
});
after("Destroy pool", async function () {
await pool.destroy();
});
2021-04-05 15:52:21 +00:00
describe("1 msg, 1 pk", function () {
const msg = Buffer.from("sample-msg");
const sk = bls.SecretKey.fromKeygen(Buffer.alloc(32, 1));
const pk = sk.toPublicKey();
const sig = sk.sign(msg);
2021-04-05 15:52:21 +00:00
it("verify", async function () {
if (bls.implementation === "herumi") this.skip();
2021-04-05 00:22:29 +00:00
const validArr = await Promise.all(
Array.from({length: 32}, (i) => i).map(async () => pool.verify(pk, msg, sig))
);
for (const [i, valid] of validArr.entries()) {
expect(valid).to.equal(true, `Invalid ${i}`);
}
});
});
2021-04-05 15:52:21 +00:00
describe("N msgs, N pks", function () {
const sets: {publicKey: PublicKey; message: Uint8Array; signature: Signature}[] = [];
for (let i = 0; i < n; i++) {
const message = Buffer.alloc(32, i);
const sk = bls.SecretKey.fromKeygen(Buffer.alloc(32, i));
sets.push({message, publicKey: sk.toPublicKey(), signature: sk.sign(message)});
}
2021-04-05 15:52:21 +00:00
it("verify", async function () {
if (bls.implementation === "herumi") this.skip();
const validArr = await Promise.all(sets.map((s) => pool.verify(s.publicKey, s.message, s.signature)));
for (const [i, valid] of validArr.entries()) {
expect(valid).to.equal(true, `Invalid ${i}`);
}
});
2021-04-05 15:52:21 +00:00
it("verifyMultipleAggregateSignatures", async function () {
if (bls.implementation === "herumi") this.skip();
const valid = await pool.verifyMultipleAggregateSignatures(sets);
expect(valid).to.equal(true);
});
});
});
}