Skip to content

Commit c03a91e

Browse files
authored
tests(crypto): increase coverage (#79)
1 parent c600389 commit c03a91e

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

packages/crypto/test/shamir.test.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import * as shamir from "../src/shamir.js";
2-
import { it } from "node:test";
2+
import { describe, it } from "node:test";
33
import { strict as assert } from "node:assert";
44

5+
it("should reconstruct a key", () => {
6+
assert.strictEqual(
7+
Buffer.from(shamir.reconstruct([
8+
Buffer.from([0xef, 0x05, 0x70, 0x4a, 0xf2, 0xb2, 0xcd, 0x02]),
9+
Buffer.from([0x62, 0x1e, 0x41, 0x63, 0xfa, 0x5e, 0x0b, 0x1c]),
10+
Buffer.from([0xc4, 0xc8, 0x3c, 0x22, 0x53, 0x05, 0x62, 0x0a]),
11+
])).toString("hex"),
12+
Buffer.from("caritat").toString("hex"),
13+
);
14+
});
15+
516
const key = crypto.getRandomValues(new Uint8Array(256));
17+
18+
describe("should reconstruct single byte with enough shareholders", () => {
19+
const byte = key[0];
20+
for (let shareHolders = 2; shareHolders < 256; shareHolders++) {
21+
for (let neededParts = 1; neededParts < shareHolders; neededParts++) {
22+
it(`Should destruct/reconstruct a key with ${shareHolders} share holders needing ${neededParts} parts`, () => {
23+
const points = Array.from(
24+
shamir.generatePoints(byte, shareHolders, neededParts),
25+
);
26+
const reconstructed = shamir.reconstructByte(points);
27+
assert.strictEqual(reconstructed, byte);
28+
});
29+
}
30+
}
31+
});
32+
633
const shareHolders = 36;
734
const neededParts = 3;
835

936
const parts = shamir.split(key.buffer, shareHolders, neededParts);
1037

11-
it("should reconstruct single byte with enough shareholders", () => {
12-
const byte = key[0];
13-
const points = Array.from(
14-
shamir.generatePoints(byte, shareHolders, neededParts),
15-
);
16-
const reconstructed = shamir.reconstructByte(points);
17-
assert.strictEqual(reconstructed, byte);
18-
});
19-
2038
it("should not give the whole key to any shareholders", () => {
2139
const byte = key[0];
2240

0 commit comments

Comments
 (0)