-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshamir.test.ts
57 lines (44 loc) · 1.76 KB
/
shamir.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { describe, expect, it } from "bun:test";
import { generateCircuit } from "../bin/generate";
import { createInput, parseOutput, randomIndices } from "../bin/utils";
import constants from "../bin/constants";
const testCases: { K: number; N: number }[] = [
{ K: 3, N: 8 },
{ K: 2, N: 3 },
{ K: 13, N: 45 },
{ K: 23, N: 510 },
];
describe("shamir secret sharing", () => {
testCases.map(({ K, N }) =>
describe(`(k=${K}, n=${N})`, () => {
let secret: bigint;
let evals: [bigint, bigint][] = [];
it("should generate code", async () => {
secret = BigInt(Math.round(Math.random() * 1_000_000_000));
const code = generateCircuit(K, N);
// `leo run split` looks for main.leo so we have to do this
await Bun.write(constants.CODE_TARGET, code);
});
it("should split a secret", async () => {
const proc = Bun.spawn(["leo", "run", "split", `${secret}field`]);
const output = await new Response(proc.stdout).text();
evals = parseOutput(N, output);
expect(evals.length).toBe(N);
});
it("should recover the secret from `k` random points", async () => {
// create unique random points
const randomEvals = randomIndices(K, N).map((p) => evals[p]);
const input = createInput(randomEvals);
await Bun.write(constants.IN_TARGET, input);
const proc = Bun.spawn(["leo", "run", "recover"]);
const output = await new Response(proc.stdout).text();
const parsedOutput = output
.slice(output.lastIndexOf("•") + 1, output.lastIndexOf("Leo ✅ Finished"))
.replaceAll("field", "")
.trim();
const recovered = BigInt(parsedOutput);
expect(recovered).toBe(secret);
});
}),
);
});