|
1 | 1 | import * as shamir from "../src/shamir.js";
|
2 |
| -import { it } from "node:test"; |
| 2 | +import { describe, it } from "node:test"; |
3 | 3 | import { strict as assert } from "node:assert";
|
4 | 4 |
|
| 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 | + |
5 | 16 | 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 | + |
6 | 33 | const shareHolders = 36;
|
7 | 34 | const neededParts = 3;
|
8 | 35 |
|
9 | 36 | const parts = shamir.split(key.buffer, shareHolders, neededParts);
|
10 | 37 |
|
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 |
| - |
20 | 38 | it("should not give the whole key to any shareholders", () => {
|
21 | 39 | const byte = key[0];
|
22 | 40 |
|
|
0 commit comments