@bcts/shamir 1.0.0-alpha.9 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -2
- package/dist/index.cjs +103 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +34 -78
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +34 -78
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +125 -89
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +88 -44
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -17
- package/src/error.ts +30 -0
- package/src/hazmat.ts +40 -28
- package/src/index.ts +6 -14
- package/src/interpolate.ts +6 -0
- package/src/shamir.ts +6 -0
package/LICENSE
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
Copyright © 2023 Blockchain Commons, LLC
|
|
2
|
-
Copyright © 2025
|
|
1
|
+
Copyright © 2023-2026 Blockchain Commons, LLC
|
|
2
|
+
Copyright © 2025-2026 Parity Technologies
|
|
3
|
+
|
|
3
4
|
|
|
4
5
|
Redistribution and use in source and binary forms, with or without modification,
|
|
5
6
|
are permitted provided that the following conditions are met:
|
package/dist/index.cjs
CHANGED
|
@@ -1,19 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _bcts_crypto = require("@bcts/crypto");
|
|
3
3
|
//#region src/error.ts
|
|
4
4
|
/**
|
|
5
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
6
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
5
10
|
* Error types for Shamir secret sharing operations.
|
|
11
|
+
*
|
|
12
|
+
* Each variant mirrors a corresponding `Error::*` enum in
|
|
13
|
+
* `bc-shamir-rust/src/error.rs` with the same trigger conditions and the
|
|
14
|
+
* same default `Display` strings.
|
|
15
|
+
*
|
|
16
|
+
* Note on `InterpolationFailure`: this variant is **reserved but
|
|
17
|
+
* unreachable** in both the Rust and TypeScript implementations.
|
|
18
|
+
* `interpolate()` in `interpolate.ts` never actually returns / throws an
|
|
19
|
+
* interpolation failure today — the Lagrange-basis math always succeeds
|
|
20
|
+
* for any well-formed input. The variant is kept for forward
|
|
21
|
+
* compatibility (e.g. should a future revision add input validation that
|
|
22
|
+
* could reject pathological cases) and to keep the TS error type a 1:1
|
|
23
|
+
* mirror of Rust's `Error` enum.
|
|
6
24
|
*/
|
|
7
|
-
let ShamirErrorType = /* @__PURE__ */ function(ShamirErrorType
|
|
8
|
-
ShamirErrorType
|
|
9
|
-
ShamirErrorType
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
ShamirErrorType
|
|
14
|
-
ShamirErrorType
|
|
15
|
-
ShamirErrorType
|
|
16
|
-
|
|
25
|
+
let ShamirErrorType = /* @__PURE__ */ function(ShamirErrorType) {
|
|
26
|
+
ShamirErrorType["SecretTooLong"] = "SecretTooLong";
|
|
27
|
+
ShamirErrorType["TooManyShares"] = "TooManyShares";
|
|
28
|
+
/**
|
|
29
|
+
* Reserved / unreachable in both Rust and TS today. See enum doc above.
|
|
30
|
+
*/
|
|
31
|
+
ShamirErrorType["InterpolationFailure"] = "InterpolationFailure";
|
|
32
|
+
ShamirErrorType["ChecksumFailure"] = "ChecksumFailure";
|
|
33
|
+
ShamirErrorType["SecretTooShort"] = "SecretTooShort";
|
|
34
|
+
ShamirErrorType["SecretNotEvenLen"] = "SecretNotEvenLen";
|
|
35
|
+
ShamirErrorType["InvalidThreshold"] = "InvalidThreshold";
|
|
36
|
+
ShamirErrorType["SharesUnequalLength"] = "SharesUnequalLength";
|
|
37
|
+
return ShamirErrorType;
|
|
17
38
|
}({});
|
|
18
39
|
/**
|
|
19
40
|
* Error class for Shamir secret sharing operations.
|
|
@@ -27,21 +48,33 @@ var ShamirError = class ShamirError extends Error {
|
|
|
27
48
|
}
|
|
28
49
|
static defaultMessage(type) {
|
|
29
50
|
switch (type) {
|
|
30
|
-
case
|
|
31
|
-
case
|
|
32
|
-
case
|
|
33
|
-
case
|
|
34
|
-
case
|
|
35
|
-
case
|
|
36
|
-
case
|
|
37
|
-
case
|
|
51
|
+
case "SecretTooLong": return "secret is too long";
|
|
52
|
+
case "TooManyShares": return "too many shares";
|
|
53
|
+
case "InterpolationFailure": return "interpolation failed";
|
|
54
|
+
case "ChecksumFailure": return "checksum failure";
|
|
55
|
+
case "SecretTooShort": return "secret is too short";
|
|
56
|
+
case "SecretNotEvenLen": return "secret is not of even length";
|
|
57
|
+
case "InvalidThreshold": return "invalid threshold";
|
|
58
|
+
case "SharesUnequalLength": return "shares have unequal length";
|
|
38
59
|
}
|
|
39
60
|
}
|
|
40
61
|
};
|
|
41
|
-
|
|
42
62
|
//#endregion
|
|
43
63
|
//#region src/hazmat.ts
|
|
44
64
|
/**
|
|
65
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
66
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
/**
|
|
70
|
+
* Internal contract guard. Mirrors a Rust `assert!(condition, message)`
|
|
71
|
+
* panic on the boundary between hazmat helpers — kept as a bare `Error`
|
|
72
|
+
* so it cannot be confused with a `ShamirError` from the public API.
|
|
73
|
+
*/
|
|
74
|
+
function assertContract(condition, message) {
|
|
75
|
+
if (!condition) throw new Error(message);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
45
78
|
* Convert an array of bytes into a bitsliced representation.
|
|
46
79
|
* Takes the first 32 bytes from x and produces 8 u32 values.
|
|
47
80
|
*
|
|
@@ -49,9 +82,9 @@ var ShamirError = class ShamirError extends Error {
|
|
|
49
82
|
* @param x - Input array of at least 32 bytes
|
|
50
83
|
*/
|
|
51
84
|
function bitslice(r, x) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
(0,
|
|
85
|
+
assertContract(x.length >= 32, "bitslice: input must be at least 32 bytes");
|
|
86
|
+
assertContract(r.length === 8, "bitslice: output must have 8 elements");
|
|
87
|
+
(0, _bcts_crypto.memzero)(r);
|
|
55
88
|
for (let arrIdx = 0; arrIdx < 32; arrIdx++) {
|
|
56
89
|
const cur = x[arrIdx];
|
|
57
90
|
for (let bitIdx = 0; bitIdx < 8; bitIdx++) r[bitIdx] |= (cur & 1 << bitIdx) >>> bitIdx << arrIdx;
|
|
@@ -64,9 +97,9 @@ function bitslice(r, x) {
|
|
|
64
97
|
* @param x - Input array of 8 u32 values (bitsliced representation)
|
|
65
98
|
*/
|
|
66
99
|
function unbitslice(r, x) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
(0,
|
|
100
|
+
assertContract(r.length >= 32, "unbitslice: output must be at least 32 bytes");
|
|
101
|
+
assertContract(x.length === 8, "unbitslice: input must have 8 elements");
|
|
102
|
+
(0, _bcts_crypto.memzero)(r.subarray(0, 32));
|
|
70
103
|
for (let bitIdx = 0; bitIdx < 8; bitIdx++) {
|
|
71
104
|
const cur = x[bitIdx];
|
|
72
105
|
for (let arrIdx = 0; arrIdx < 32; arrIdx++) r[arrIdx] |= (cur & 1 << arrIdx) >>> arrIdx << bitIdx;
|
|
@@ -79,7 +112,7 @@ function unbitslice(r, x) {
|
|
|
79
112
|
* @param x - Byte value to set in all positions
|
|
80
113
|
*/
|
|
81
114
|
function bitsliceSetall(r, x) {
|
|
82
|
-
|
|
115
|
+
assertContract(r.length === 8, "bitsliceSetall: output must have 8 elements");
|
|
83
116
|
for (let idx = 0; idx < 8; idx++) r[idx] = (x >>> idx & 1) === 1 ? 4294967295 : 0;
|
|
84
117
|
}
|
|
85
118
|
/**
|
|
@@ -90,7 +123,7 @@ function bitsliceSetall(r, x) {
|
|
|
90
123
|
* @param x - Second operand
|
|
91
124
|
*/
|
|
92
125
|
function gf256Add(r, x) {
|
|
93
|
-
|
|
126
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Add: arrays must have 8 elements");
|
|
94
127
|
for (let i = 0; i < 8; i++) r[i] ^= x[i];
|
|
95
128
|
}
|
|
96
129
|
/**
|
|
@@ -104,7 +137,7 @@ function gf256Add(r, x) {
|
|
|
104
137
|
* @param b - Second operand (must NOT overlap with r)
|
|
105
138
|
*/
|
|
106
139
|
function gf256Mul(r, a, b) {
|
|
107
|
-
|
|
140
|
+
assertContract(r.length === 8 && a.length === 8 && b.length === 8, "gf256Mul: arrays must have 8 elements");
|
|
108
141
|
const a2 = new Uint32Array(a);
|
|
109
142
|
r[0] = a2[0] & b[0];
|
|
110
143
|
r[1] = a2[1] & b[0];
|
|
@@ -200,7 +233,7 @@ function gf256Mul(r, a, b) {
|
|
|
200
233
|
* @param x - Value to square
|
|
201
234
|
*/
|
|
202
235
|
function gf256Square(r, x) {
|
|
203
|
-
|
|
236
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Square: arrays must have 8 elements");
|
|
204
237
|
const r14 = x[7];
|
|
205
238
|
const r12 = x[6];
|
|
206
239
|
let r10 = x[5];
|
|
@@ -236,7 +269,7 @@ function gf256Square(r, x) {
|
|
|
236
269
|
* @param x - Value to invert (will be modified)
|
|
237
270
|
*/
|
|
238
271
|
function gf256Inv(r, x) {
|
|
239
|
-
|
|
272
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Inv: arrays must have 8 elements");
|
|
240
273
|
const y = new Uint32Array(8);
|
|
241
274
|
const z = new Uint32Array(8);
|
|
242
275
|
gf256Square(y, x);
|
|
@@ -251,10 +284,14 @@ function gf256Inv(r, x) {
|
|
|
251
284
|
gf256Mul(r, new Uint32Array(r), z);
|
|
252
285
|
gf256Mul(r, new Uint32Array(r), y);
|
|
253
286
|
}
|
|
254
|
-
|
|
255
287
|
//#endregion
|
|
256
288
|
//#region src/interpolate.ts
|
|
257
289
|
/**
|
|
290
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
291
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
292
|
+
*
|
|
293
|
+
*/
|
|
294
|
+
/**
|
|
258
295
|
* Calculate the lagrange basis coefficients for the lagrange polynomial
|
|
259
296
|
* defined by the x coordinates xc at the value x.
|
|
260
297
|
*
|
|
@@ -316,8 +353,8 @@ function hazmatLagrangeBasis(values, n, xc, x) {
|
|
|
316
353
|
*/
|
|
317
354
|
function interpolate(n, xi, yl, yij, x) {
|
|
318
355
|
const y = [];
|
|
319
|
-
for (let i = 0; i < n; i++) y.push(new Uint8Array(
|
|
320
|
-
const values = new Uint8Array(
|
|
356
|
+
for (let i = 0; i < n; i++) y.push(new Uint8Array(32));
|
|
357
|
+
const values = new Uint8Array(32);
|
|
321
358
|
for (let i = 0; i < n; i++) y[i].set(yij[i].subarray(0, yl), 0);
|
|
322
359
|
const lagrange = new Uint8Array(n);
|
|
323
360
|
const ySlice = new Uint32Array(8);
|
|
@@ -334,28 +371,32 @@ function interpolate(n, xi, yl, yij, x) {
|
|
|
334
371
|
unbitslice(values, resultSlice);
|
|
335
372
|
const result = new Uint8Array(yl);
|
|
336
373
|
result.set(values.subarray(0, yl), 0);
|
|
337
|
-
(0,
|
|
338
|
-
(0,
|
|
339
|
-
(0,
|
|
340
|
-
(0,
|
|
341
|
-
(0,
|
|
342
|
-
(0,
|
|
374
|
+
(0, _bcts_crypto.memzero)(lagrange);
|
|
375
|
+
(0, _bcts_crypto.memzero)(ySlice);
|
|
376
|
+
(0, _bcts_crypto.memzero)(resultSlice);
|
|
377
|
+
(0, _bcts_crypto.memzero)(temp);
|
|
378
|
+
(0, _bcts_crypto.memzeroVecVecU8)(y);
|
|
379
|
+
(0, _bcts_crypto.memzero)(values);
|
|
343
380
|
return result;
|
|
344
381
|
}
|
|
345
|
-
|
|
346
382
|
//#endregion
|
|
347
383
|
//#region src/shamir.ts
|
|
384
|
+
/**
|
|
385
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
386
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
387
|
+
*
|
|
388
|
+
*/
|
|
348
389
|
const SECRET_INDEX = 255;
|
|
349
390
|
const DIGEST_INDEX = 254;
|
|
350
391
|
function createDigest(randomData, sharedSecret) {
|
|
351
|
-
return (0,
|
|
392
|
+
return (0, _bcts_crypto.hmacSha256)(randomData, sharedSecret);
|
|
352
393
|
}
|
|
353
394
|
function validateParameters(threshold, shareCount, secretLength) {
|
|
354
|
-
if (shareCount >
|
|
355
|
-
else if (threshold < 1 || threshold > shareCount) throw new ShamirError(
|
|
356
|
-
else if (secretLength >
|
|
357
|
-
else if (secretLength <
|
|
358
|
-
else if ((secretLength & 1) !== 0) throw new ShamirError(
|
|
395
|
+
if (shareCount > 16) throw new ShamirError("TooManyShares");
|
|
396
|
+
else if (threshold < 1 || threshold > shareCount) throw new ShamirError("InvalidThreshold");
|
|
397
|
+
else if (secretLength > 32) throw new ShamirError("SecretTooLong");
|
|
398
|
+
else if (secretLength < 16) throw new ShamirError("SecretTooShort");
|
|
399
|
+
else if ((secretLength & 1) !== 0) throw new ShamirError("SecretNotEvenLen");
|
|
359
400
|
}
|
|
360
401
|
/**
|
|
361
402
|
* Splits a secret into shares using the Shamir secret sharing algorithm.
|
|
@@ -420,9 +461,9 @@ function splitSecret(threshold, shareCount, secret, randomGenerator) {
|
|
|
420
461
|
const v = interpolate(n, x, secret.length, y, index);
|
|
421
462
|
result[index].set(v);
|
|
422
463
|
}
|
|
423
|
-
(0,
|
|
424
|
-
(0,
|
|
425
|
-
(0,
|
|
464
|
+
(0, _bcts_crypto.memzero)(digest);
|
|
465
|
+
(0, _bcts_crypto.memzero)(x);
|
|
466
|
+
(0, _bcts_crypto.memzeroVecVecU8)(y);
|
|
426
467
|
return result;
|
|
427
468
|
}
|
|
428
469
|
}
|
|
@@ -453,10 +494,10 @@ function splitSecret(threshold, shareCount, secret, randomGenerator) {
|
|
|
453
494
|
*/
|
|
454
495
|
function recoverSecret(indexes, shares) {
|
|
455
496
|
const threshold = shares.length;
|
|
456
|
-
if (threshold === 0 || indexes.length !== threshold) throw new ShamirError(
|
|
497
|
+
if (threshold === 0 || indexes.length !== threshold) throw new ShamirError("InvalidThreshold");
|
|
457
498
|
const shareLength = shares[0].length;
|
|
458
499
|
validateParameters(threshold, threshold, shareLength);
|
|
459
|
-
if (!shares.every((share) => share.length === shareLength)) throw new ShamirError(
|
|
500
|
+
if (!shares.every((share) => share.length === shareLength)) throw new ShamirError("SharesUnequalLength");
|
|
460
501
|
if (threshold === 1) return new Uint8Array(shares[0]);
|
|
461
502
|
else {
|
|
462
503
|
const indexesU8 = new Uint8Array(indexes);
|
|
@@ -465,16 +506,20 @@ function recoverSecret(indexes, shares) {
|
|
|
465
506
|
const verify = createDigest(digest.subarray(4), secret);
|
|
466
507
|
let valid = true;
|
|
467
508
|
for (let i = 0; i < 4; i++) valid = valid && digest[i] === verify[i];
|
|
468
|
-
(0,
|
|
469
|
-
(0,
|
|
470
|
-
if (!valid) throw new ShamirError(
|
|
509
|
+
(0, _bcts_crypto.memzero)(digest);
|
|
510
|
+
(0, _bcts_crypto.memzero)(verify);
|
|
511
|
+
if (!valid) throw new ShamirError("ChecksumFailure");
|
|
471
512
|
return secret;
|
|
472
513
|
}
|
|
473
514
|
}
|
|
474
|
-
|
|
475
515
|
//#endregion
|
|
476
516
|
//#region src/index.ts
|
|
477
517
|
/**
|
|
518
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
519
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
520
|
+
*
|
|
521
|
+
*/
|
|
522
|
+
/**
|
|
478
523
|
* The minimum length of a secret.
|
|
479
524
|
*/
|
|
480
525
|
const MIN_SECRET_LEN = 16;
|
|
@@ -486,21 +531,13 @@ const MAX_SECRET_LEN = 32;
|
|
|
486
531
|
* The maximum number of shares that can be generated from a secret.
|
|
487
532
|
*/
|
|
488
533
|
const MAX_SHARE_COUNT = 16;
|
|
489
|
-
|
|
490
534
|
//#endregion
|
|
491
535
|
exports.MAX_SECRET_LEN = MAX_SECRET_LEN;
|
|
492
536
|
exports.MAX_SHARE_COUNT = MAX_SHARE_COUNT;
|
|
493
537
|
exports.MIN_SECRET_LEN = MIN_SECRET_LEN;
|
|
494
538
|
exports.ShamirError = ShamirError;
|
|
495
539
|
exports.ShamirErrorType = ShamirErrorType;
|
|
496
|
-
exports.bitslice = bitslice;
|
|
497
|
-
exports.bitsliceSetall = bitsliceSetall;
|
|
498
|
-
exports.gf256Add = gf256Add;
|
|
499
|
-
exports.gf256Inv = gf256Inv;
|
|
500
|
-
exports.gf256Mul = gf256Mul;
|
|
501
|
-
exports.gf256Square = gf256Square;
|
|
502
|
-
exports.interpolate = interpolate;
|
|
503
540
|
exports.recoverSecret = recoverSecret;
|
|
504
541
|
exports.splitSecret = splitSecret;
|
|
505
|
-
|
|
542
|
+
|
|
506
543
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["lxi: Uint32Array[]","y: Uint8Array[]","result: Uint8Array[]","y: Uint8Array[]"],"sources":["../src/error.ts","../src/hazmat.ts","../src/interpolate.ts","../src/shamir.ts","../src/index.ts"],"sourcesContent":["// Ported from bc-shamir-rust/src/error.rs\n\n/**\n * Error types for Shamir secret sharing operations.\n */\nexport enum ShamirErrorType {\n SecretTooLong = \"SecretTooLong\",\n TooManyShares = \"TooManyShares\",\n InterpolationFailure = \"InterpolationFailure\",\n ChecksumFailure = \"ChecksumFailure\",\n SecretTooShort = \"SecretTooShort\",\n SecretNotEvenLen = \"SecretNotEvenLen\",\n InvalidThreshold = \"InvalidThreshold\",\n SharesUnequalLength = \"SharesUnequalLength\",\n}\n\n/**\n * Error class for Shamir secret sharing operations.\n */\nexport class ShamirError extends Error {\n readonly type: ShamirErrorType;\n\n constructor(type: ShamirErrorType, message?: string) {\n super(message ?? ShamirError.defaultMessage(type));\n this.type = type;\n this.name = \"ShamirError\";\n }\n\n private static defaultMessage(type: ShamirErrorType): string {\n switch (type) {\n case ShamirErrorType.SecretTooLong:\n return \"secret is too long\";\n case ShamirErrorType.TooManyShares:\n return \"too many shares\";\n case ShamirErrorType.InterpolationFailure:\n return \"interpolation failed\";\n case ShamirErrorType.ChecksumFailure:\n return \"checksum failure\";\n case ShamirErrorType.SecretTooShort:\n return \"secret is too short\";\n case ShamirErrorType.SecretNotEvenLen:\n return \"secret is not of even length\";\n case ShamirErrorType.InvalidThreshold:\n return \"invalid threshold\";\n case ShamirErrorType.SharesUnequalLength:\n return \"shares have unequal length\";\n }\n }\n}\n\nexport type ShamirResult<T> = T;\n","// Ported from bc-shamir-rust/src/hazmat.rs\n// GF(2^8) bitsliced polynomial operations for Shamir secret sharing\n\nimport { memzero } from \"@bcts/crypto\";\n\n/**\n * Convert an array of bytes into a bitsliced representation.\n * Takes the first 32 bytes from x and produces 8 u32 values.\n *\n * @param r - Output array of 8 u32 values (bitsliced representation)\n * @param x - Input array of at least 32 bytes\n */\nexport function bitslice(r: Uint32Array, x: Uint8Array): void {\n if (x.length < 32) {\n throw new Error(\"bitslice: input must be at least 32 bytes\");\n }\n if (r.length !== 8) {\n throw new Error(\"bitslice: output must have 8 elements\");\n }\n\n memzero(r);\n\n for (let arrIdx = 0; arrIdx < 32; arrIdx++) {\n const cur = x[arrIdx];\n for (let bitIdx = 0; bitIdx < 8; bitIdx++) {\n // r[bitIdx] |= ((cur & (1 << bitIdx)) >> bitIdx) << arrIdx\n r[bitIdx] |= ((cur & (1 << bitIdx)) >>> bitIdx) << arrIdx;\n }\n }\n}\n\n/**\n * Convert a bitsliced representation back to bytes.\n *\n * @param r - Output array of at least 32 bytes\n * @param x - Input array of 8 u32 values (bitsliced representation)\n */\nexport function unbitslice(r: Uint8Array, x: Uint32Array): void {\n if (r.length < 32) {\n throw new Error(\"unbitslice: output must be at least 32 bytes\");\n }\n if (x.length !== 8) {\n throw new Error(\"unbitslice: input must have 8 elements\");\n }\n\n memzero(r.subarray(0, 32));\n\n for (let bitIdx = 0; bitIdx < 8; bitIdx++) {\n const cur = x[bitIdx];\n for (let arrIdx = 0; arrIdx < 32; arrIdx++) {\n // r[arrIdx] |= ((cur & (1 << arrIdx)) >> arrIdx) << bitIdx\n r[arrIdx] |= ((cur & (1 << arrIdx)) >>> arrIdx) << bitIdx;\n }\n }\n}\n\n/**\n * Set all 32 positions in a bitsliced array to the same byte value.\n *\n * @param r - Output array of 8 u32 values\n * @param x - Byte value to set in all positions\n */\nexport function bitsliceSetall(r: Uint32Array, x: number): void {\n if (r.length !== 8) {\n throw new Error(\"bitsliceSetall: output must have 8 elements\");\n }\n\n for (let idx = 0; idx < 8; idx++) {\n // JavaScript needs special handling for the arithmetic right shift\n // This mirrors: *r = (((((x as u32) & (1u32.wrapping_shl(idx as u32)))\n // .wrapping_shl(31 - idx as u32)) as i32)\n // .wrapping_shr(31)) as u32;\n const bit = (x >>> idx) & 1;\n r[idx] = bit === 1 ? 0xffffffff : 0;\n }\n}\n\n/**\n * Add (XOR) r with x and store the result in r.\n * In GF(2^8), addition is XOR.\n *\n * @param r - First operand and result\n * @param x - Second operand\n */\nexport function gf256Add(r: Uint32Array, x: Uint32Array): void {\n if (r.length !== 8 || x.length !== 8) {\n throw new Error(\"gf256Add: arrays must have 8 elements\");\n }\n\n for (let i = 0; i < 8; i++) {\n r[i] ^= x[i];\n }\n}\n\n/**\n * Safely multiply two bitsliced polynomials in GF(2^8) reduced by\n * x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r\n * and b will produce an incorrect result! If you need to square a polynomial\n * use gf256Square instead.\n *\n * @param r - Result array (8 u32 values)\n * @param a - First operand (may overlap with r)\n * @param b - Second operand (must NOT overlap with r)\n */\nexport function gf256Mul(r: Uint32Array, a: Uint32Array, b: Uint32Array): void {\n if (r.length !== 8 || a.length !== 8 || b.length !== 8) {\n throw new Error(\"gf256Mul: arrays must have 8 elements\");\n }\n\n // Russian Peasant multiplication on two bitsliced polynomials\n const a2 = new Uint32Array(a);\n\n r[0] = a2[0] & b[0];\n r[1] = a2[1] & b[0];\n r[2] = a2[2] & b[0];\n r[3] = a2[3] & b[0];\n r[4] = a2[4] & b[0];\n r[5] = a2[5] & b[0];\n r[6] = a2[6] & b[0];\n r[7] = a2[7] & b[0];\n a2[0] ^= a2[7]; // reduce\n a2[2] ^= a2[7];\n a2[3] ^= a2[7];\n\n r[0] ^= a2[7] & b[1]; // add\n r[1] ^= a2[0] & b[1];\n r[2] ^= a2[1] & b[1];\n r[3] ^= a2[2] & b[1];\n r[4] ^= a2[3] & b[1];\n r[5] ^= a2[4] & b[1];\n r[6] ^= a2[5] & b[1];\n r[7] ^= a2[6] & b[1];\n a2[7] ^= a2[6]; // reduce\n a2[1] ^= a2[6];\n a2[2] ^= a2[6];\n\n r[0] ^= a2[6] & b[2]; // add\n r[1] ^= a2[7] & b[2];\n r[2] ^= a2[0] & b[2];\n r[3] ^= a2[1] & b[2];\n r[4] ^= a2[2] & b[2];\n r[5] ^= a2[3] & b[2];\n r[6] ^= a2[4] & b[2];\n r[7] ^= a2[5] & b[2];\n a2[6] ^= a2[5]; // reduce\n a2[0] ^= a2[5];\n a2[1] ^= a2[5];\n\n r[0] ^= a2[5] & b[3]; // add\n r[1] ^= a2[6] & b[3];\n r[2] ^= a2[7] & b[3];\n r[3] ^= a2[0] & b[3];\n r[4] ^= a2[1] & b[3];\n r[5] ^= a2[2] & b[3];\n r[6] ^= a2[3] & b[3];\n r[7] ^= a2[4] & b[3];\n a2[5] ^= a2[4]; // reduce\n a2[7] ^= a2[4];\n a2[0] ^= a2[4];\n\n r[0] ^= a2[4] & b[4]; // add\n r[1] ^= a2[5] & b[4];\n r[2] ^= a2[6] & b[4];\n r[3] ^= a2[7] & b[4];\n r[4] ^= a2[0] & b[4];\n r[5] ^= a2[1] & b[4];\n r[6] ^= a2[2] & b[4];\n r[7] ^= a2[3] & b[4];\n a2[4] ^= a2[3]; // reduce\n a2[6] ^= a2[3];\n a2[7] ^= a2[3];\n\n r[0] ^= a2[3] & b[5]; // add\n r[1] ^= a2[4] & b[5];\n r[2] ^= a2[5] & b[5];\n r[3] ^= a2[6] & b[5];\n r[4] ^= a2[7] & b[5];\n r[5] ^= a2[0] & b[5];\n r[6] ^= a2[1] & b[5];\n r[7] ^= a2[2] & b[5];\n a2[3] ^= a2[2]; // reduce\n a2[5] ^= a2[2];\n a2[6] ^= a2[2];\n\n r[0] ^= a2[2] & b[6]; // add\n r[1] ^= a2[3] & b[6];\n r[2] ^= a2[4] & b[6];\n r[3] ^= a2[5] & b[6];\n r[4] ^= a2[6] & b[6];\n r[5] ^= a2[7] & b[6];\n r[6] ^= a2[0] & b[6];\n r[7] ^= a2[1] & b[6];\n a2[2] ^= a2[1]; // reduce\n a2[4] ^= a2[1];\n a2[5] ^= a2[1];\n\n r[0] ^= a2[1] & b[7]; // add\n r[1] ^= a2[2] & b[7];\n r[2] ^= a2[3] & b[7];\n r[3] ^= a2[4] & b[7];\n r[4] ^= a2[5] & b[7];\n r[5] ^= a2[6] & b[7];\n r[6] ^= a2[7] & b[7];\n r[7] ^= a2[0] & b[7];\n}\n\n/**\n * Square x in GF(2^8) and write the result to r.\n * r and x may overlap.\n *\n * @param r - Result array (8 u32 values)\n * @param x - Value to square\n */\nexport function gf256Square(r: Uint32Array, x: Uint32Array): void {\n if (r.length !== 8 || x.length !== 8) {\n throw new Error(\"gf256Square: arrays must have 8 elements\");\n }\n\n // Use the Freshman's Dream rule to square the polynomial\n // Assignments are done from 7 downto 0, because this allows\n // in-place operation (e.g. gf256Square(r, r))\n const r14 = x[7];\n const r12 = x[6];\n let r10 = x[5];\n let r8 = x[4];\n r[6] = x[3];\n r[4] = x[2];\n r[2] = x[1];\n r[0] = x[0];\n\n // Reduce with x^8 + x^4 + x^3 + x + 1 until order is less than 8\n r[7] = r14; // r[7] was 0\n r[6] ^= r14;\n r10 ^= r14;\n // Skip, because r13 is always 0\n r[4] ^= r12;\n r[5] = r12; // r[5] was 0\n r[7] ^= r12;\n r8 ^= r12;\n // Skip, because r11 is always 0\n r[2] ^= r10;\n r[3] = r10; // r[3] was 0\n r[5] ^= r10;\n r[6] ^= r10;\n r[1] = r14; // r[1] was 0\n r[2] ^= r14; // Substitute r9 by r14 because they will always be equal\n r[4] ^= r14;\n r[5] ^= r14;\n r[0] ^= r8;\n r[1] ^= r8;\n r[3] ^= r8;\n r[4] ^= r8;\n}\n\n/**\n * Invert x in GF(2^8) and write the result to r.\n *\n * @param r - Result array (8 u32 values)\n * @param x - Value to invert (will be modified)\n */\nexport function gf256Inv(r: Uint32Array, x: Uint32Array): void {\n if (r.length !== 8 || x.length !== 8) {\n throw new Error(\"gf256Inv: arrays must have 8 elements\");\n }\n\n const y = new Uint32Array(8);\n const z = new Uint32Array(8);\n\n gf256Square(y, x); // y = x^2\n const y2 = new Uint32Array(y);\n gf256Square(y, y2); // y = x^4\n gf256Square(r, y); // r = x^8\n gf256Mul(z, r, x); // z = x^9\n const r2a = new Uint32Array(r);\n gf256Square(r, r2a); // r = x^16\n const r2b = new Uint32Array(r);\n gf256Mul(r, r2b, z); // r = x^25\n const r2c = new Uint32Array(r);\n gf256Square(r, r2c); // r = x^50\n gf256Square(z, r); // z = x^100\n const z2 = new Uint32Array(z);\n gf256Square(z, z2); // z = x^200\n const r2d = new Uint32Array(r);\n gf256Mul(r, r2d, z); // r = x^250\n const r2e = new Uint32Array(r);\n gf256Mul(r, r2e, y); // r = x^254\n}\n","// Ported from bc-shamir-rust/src/interpolate.rs\n\nimport { memzero, memzeroVecVecU8 } from \"@bcts/crypto\";\nimport { MAX_SECRET_LEN } from \"./index.js\";\nimport { bitslice, bitsliceSetall, gf256Add, gf256Inv, gf256Mul, unbitslice } from \"./hazmat.js\";\n\n/**\n * Calculate the lagrange basis coefficients for the lagrange polynomial\n * defined by the x coordinates xc at the value x.\n *\n * After the function runs, the values array should hold data satisfying:\n * --- (x-xc[j])\n * values[i] = | | -------------\n * j != i (xc[i]-xc[j])\n *\n * @param values - Output array for the lagrange basis values\n * @param n - Number of points (length of the xc array, 0 < n <= 32)\n * @param xc - Array of x components to use as interpolating points\n * @param x - x coordinate to evaluate lagrange polynomials at\n */\nfunction hazmatLagrangeBasis(values: Uint8Array, n: number, xc: Uint8Array, x: number): void {\n // call the contents of xc [ x0 x1 x2 ... xn-1 ]\n const xx = new Uint8Array(32 + 16);\n const xSlice = new Uint32Array(8);\n const lxi: Uint32Array[] = [];\n for (let i = 0; i < n; i++) {\n lxi.push(new Uint32Array(8));\n }\n const numerator = new Uint32Array(8);\n const denominator = new Uint32Array(8);\n const temp = new Uint32Array(8);\n\n xx.set(xc.subarray(0, n), 0);\n\n // xx now contains bitsliced [ x0 x1 x2 ... xn-1 0 0 0 ... ]\n for (let i = 0; i < n; i++) {\n // lxi = bitsliced [ xi xi+1 xi+2 ... xi-1 0 0 0 ]\n bitslice(lxi[i], xx.subarray(i));\n xx[i + n] = xx[i];\n }\n\n bitsliceSetall(xSlice, x);\n bitsliceSetall(numerator, 1);\n bitsliceSetall(denominator, 1);\n\n for (let i = 1; i < n; i++) {\n temp.set(xSlice);\n gf256Add(temp, lxi[i]);\n // temp = [ x-xi+i x-xi+2 x-xi+3 ... x-xi x x x]\n const numerator2 = new Uint32Array(numerator);\n gf256Mul(numerator, numerator2, temp);\n\n temp.set(lxi[0]);\n gf256Add(temp, lxi[i]);\n // temp = [x0-xi+1 x1-xi+1 x2-xi+2 ... xn-x0 0 0 0]\n const denominator2 = new Uint32Array(denominator);\n gf256Mul(denominator, denominator2, temp);\n }\n\n // At this stage the numerator contains\n // [ num0 num1 num2 ... numn 0 0 0]\n //\n // where numi = prod(j, j!=i, x-xj )\n //\n // and the denominator contains\n // [ d0 d1 d2 ... dn 0 0 0]\n //\n // where di = prod(j, j!=i, xi-xj)\n\n gf256Inv(temp, denominator);\n\n // gf256_inv uses exponentiation to calculate inverse, so the zeros end up\n // remaining zeros.\n\n // tmp = [ 1/d0 1/d1 1/d2 ... 1/dn 0 0 0]\n\n const numerator2 = new Uint32Array(numerator);\n gf256Mul(numerator, numerator2, temp);\n\n // numerator now contains [ l_n_0(x) l_n_1(x) ... l_n_n-1(x) 0 0 0]\n // use the xx array to unpack it\n\n unbitslice(xx, numerator);\n\n // copy results to output array\n values.set(xx.subarray(0, n), 0);\n}\n\n/**\n * Safely interpolate the polynomial going through\n * the points (x0 [y0_0 y0_1 y0_2 ... y0_31]) , (x1 [y1_0 ...]), ...\n *\n * where\n * xi points to [x0 x1 ... xn-1 ]\n * y contains an array of pointers to 32-bit arrays of y values\n * y contains [y0 y1 y2 ... yn-1]\n * and each of the yi arrays contain [yi_0 yi_i ... yi_31].\n *\n * @param n - Number of points to interpolate\n * @param xi - x coordinates for points (array of length n)\n * @param yl - Length of y coordinate arrays\n * @param yij - Array of n arrays of length yl\n * @param x - Coordinate to interpolate at\n * @returns The interpolated result of length yl\n */\nexport function interpolate(\n n: number,\n xi: Uint8Array,\n yl: number,\n yij: Uint8Array[],\n x: number,\n): Uint8Array {\n // The hazmat gf256 implementation needs the y-coordinate data\n // to be in 32-byte blocks\n const y: Uint8Array[] = [];\n for (let i = 0; i < n; i++) {\n y.push(new Uint8Array(MAX_SECRET_LEN));\n }\n const values = new Uint8Array(MAX_SECRET_LEN);\n\n for (let i = 0; i < n; i++) {\n y[i].set(yij[i].subarray(0, yl), 0);\n }\n\n const lagrange = new Uint8Array(n);\n const ySlice = new Uint32Array(8);\n const resultSlice = new Uint32Array(8);\n const temp = new Uint32Array(8);\n\n hazmatLagrangeBasis(lagrange, n, xi, x);\n\n bitsliceSetall(resultSlice, 0);\n\n for (let i = 0; i < n; i++) {\n bitslice(ySlice, y[i]);\n bitsliceSetall(temp, lagrange[i]);\n const temp2 = new Uint32Array(temp);\n gf256Mul(temp, temp2, ySlice);\n gf256Add(resultSlice, temp);\n }\n\n unbitslice(values, resultSlice);\n\n // the calling code is only expecting yl bytes back\n const result = new Uint8Array(yl);\n result.set(values.subarray(0, yl), 0);\n\n // clean up stack\n memzero(lagrange);\n memzero(ySlice);\n memzero(resultSlice);\n memzero(temp);\n memzeroVecVecU8(y);\n memzero(values);\n\n return result;\n}\n","// Ported from bc-shamir-rust/src/shamir.rs\n\nimport { hmacSha256, memzero, memzeroVecVecU8 } from \"@bcts/crypto\";\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\n\nimport { ShamirError, ShamirErrorType } from \"./error.js\";\nimport { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN } from \"./index.js\";\nimport { interpolate } from \"./interpolate.js\";\n\nconst SECRET_INDEX = 255;\nconst DIGEST_INDEX = 254;\n\nfunction createDigest(randomData: Uint8Array, sharedSecret: Uint8Array): Uint8Array {\n return hmacSha256(randomData, sharedSecret);\n}\n\nfunction validateParameters(threshold: number, shareCount: number, secretLength: number): void {\n if (shareCount > MAX_SHARE_COUNT) {\n throw new ShamirError(ShamirErrorType.TooManyShares);\n } else if (threshold < 1 || threshold > shareCount) {\n throw new ShamirError(ShamirErrorType.InvalidThreshold);\n } else if (secretLength > MAX_SECRET_LEN) {\n throw new ShamirError(ShamirErrorType.SecretTooLong);\n } else if (secretLength < MIN_SECRET_LEN) {\n throw new ShamirError(ShamirErrorType.SecretTooShort);\n } else if ((secretLength & 1) !== 0) {\n throw new ShamirError(ShamirErrorType.SecretNotEvenLen);\n }\n}\n\n/**\n * Splits a secret into shares using the Shamir secret sharing algorithm.\n *\n * @param threshold - The minimum number of shares required to reconstruct the\n * secret. Must be greater than or equal to 1 and less than or equal to\n * shareCount.\n * @param shareCount - The total number of shares to generate. Must be at least\n * threshold and less than or equal to MAX_SHARE_COUNT.\n * @param secret - A Uint8Array containing the secret to be split. Must be at\n * least MIN_SECRET_LEN bytes long and at most MAX_SECRET_LEN bytes long.\n * The length must be an even number.\n * @param randomGenerator - An implementation of the RandomNumberGenerator\n * interface, used to generate random data.\n * @returns An array of Uint8Array representing the shares of the secret.\n * @throws ShamirError if parameters are invalid\n *\n * @example\n * ```typescript\n * import { splitSecret } from \"@bcts/shamir\";\n * import { SecureRandomNumberGenerator } from \"@bcts/rand\";\n *\n * const threshold = 2;\n * const shareCount = 3;\n * const secret = new TextEncoder().encode(\"my secret belongs to me.\");\n * const rng = new SecureRandomNumberGenerator();\n *\n * const shares = splitSecret(threshold, shareCount, secret, rng);\n * console.log(shares.length); // 3\n * ```\n */\nexport function splitSecret(\n threshold: number,\n shareCount: number,\n secret: Uint8Array,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[] {\n validateParameters(threshold, shareCount, secret.length);\n\n if (threshold === 1) {\n // just return shareCount copies of the secret\n const result: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n result.push(new Uint8Array(secret));\n }\n return result;\n } else {\n const x = new Uint8Array(shareCount);\n const y: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n y.push(new Uint8Array(secret.length));\n }\n let n = 0;\n const result: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n result.push(new Uint8Array(secret.length));\n }\n\n for (let index = 0; index < threshold - 2; index++) {\n randomGenerator.fillRandomData(result[index]);\n x[n] = index;\n y[n].set(result[index]);\n n++;\n }\n\n // generate secret_length - 4 bytes worth of random data\n const digest = new Uint8Array(secret.length);\n randomGenerator.fillRandomData(digest.subarray(4));\n // put 4 bytes of digest at the top of the digest array\n const d = createDigest(digest.subarray(4), secret);\n digest.set(d.subarray(0, 4), 0);\n x[n] = DIGEST_INDEX;\n y[n].set(digest);\n n++;\n\n x[n] = SECRET_INDEX;\n y[n].set(secret);\n n++;\n\n for (let index = threshold - 2; index < shareCount; index++) {\n const v = interpolate(n, x, secret.length, y, index);\n result[index].set(v);\n }\n\n // clean up stack\n memzero(digest);\n memzero(x);\n memzeroVecVecU8(y);\n\n return result;\n }\n}\n\n/**\n * Recovers the secret from the given shares using the Shamir secret sharing\n * algorithm.\n *\n * @param indexes - An array of indexes of the shares to be used for recovering\n * the secret. These are the indexes of the shares returned by splitSecret.\n * @param shares - An array of shares of the secret matching the indexes in\n * indexes. These are the shares returned by splitSecret.\n * @returns A Uint8Array representing the recovered secret.\n * @throws ShamirError if parameters are invalid or checksum verification fails\n *\n * @example\n * ```typescript\n * import { recoverSecret } from \"@bcts/shamir\";\n *\n * const indexes = [0, 2];\n * const shares = [\n * new Uint8Array([47, 165, 102, 232, ...]),\n * new Uint8Array([221, 174, 116, 201, ...]),\n * ];\n *\n * const secret = recoverSecret(indexes, shares);\n * console.log(new TextDecoder().decode(secret)); // \"my secret belongs to me.\"\n * ```\n */\nexport function recoverSecret(indexes: number[], shares: Uint8Array[]): Uint8Array {\n const threshold = shares.length;\n if (threshold === 0 || indexes.length !== threshold) {\n throw new ShamirError(ShamirErrorType.InvalidThreshold);\n }\n\n const shareLength = shares[0].length;\n validateParameters(threshold, threshold, shareLength);\n\n const allSameLength = shares.every((share) => share.length === shareLength);\n if (!allSameLength) {\n throw new ShamirError(ShamirErrorType.SharesUnequalLength);\n }\n\n if (threshold === 1) {\n return new Uint8Array(shares[0]);\n } else {\n const indexesU8 = new Uint8Array(indexes);\n\n const digest = interpolate(threshold, indexesU8, shareLength, shares, DIGEST_INDEX);\n const secret = interpolate(threshold, indexesU8, shareLength, shares, SECRET_INDEX);\n const verify = createDigest(digest.subarray(4), secret);\n\n let valid = true;\n for (let i = 0; i < 4; i++) {\n valid = valid && digest[i] === verify[i];\n }\n\n memzero(digest);\n memzero(verify);\n\n if (!valid) {\n throw new ShamirError(ShamirErrorType.ChecksumFailure);\n }\n\n return secret;\n }\n}\n","// Blockchain Commons Shamir Secret Sharing\n// Ported from bc-shamir-rust\n//\n// This is a pure TypeScript implementation of Shamir's Secret Sharing (SSS),\n// a cryptographic technique in which a secret is divided into parts, called\n// shares, in such a way that a threshold of several shares are needed to\n// reconstruct the secret. The shares are distributed in a way that makes it\n// impossible for an attacker to know anything about the secret without having\n// a threshold of shares. If the number of shares is less than the threshold,\n// then no information about the secret is revealed.\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = 16;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = 32;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = 16;\n\n// Error types\nexport { ShamirError, ShamirErrorType, type ShamirResult } from \"./error.js\";\n\n// Main functions\nexport { splitSecret, recoverSecret } from \"./shamir.js\";\n\n// Low-level operations (hazmat)\nexport {\n bitslice,\n unbitslice,\n bitsliceSetall,\n gf256Add,\n gf256Mul,\n gf256Square,\n gf256Inv,\n} from \"./hazmat.js\";\n\n// Interpolation\nexport { interpolate } from \"./interpolate.js\";\n"],"mappings":";;;;;;AAKA,IAAY,8DAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;AAMF,IAAa,cAAb,MAAa,oBAAoB,MAAM;CACrC,AAAS;CAET,YAAY,MAAuB,SAAkB;AACnD,QAAM,WAAW,YAAY,eAAe,KAAK,CAAC;AAClD,OAAK,OAAO;AACZ,OAAK,OAAO;;CAGd,OAAe,eAAe,MAA+B;AAC3D,UAAQ,MAAR;GACE,KAAK,gBAAgB,cACnB,QAAO;GACT,KAAK,gBAAgB,cACnB,QAAO;GACT,KAAK,gBAAgB,qBACnB,QAAO;GACT,KAAK,gBAAgB,gBACnB,QAAO;GACT,KAAK,gBAAgB,eACnB,QAAO;GACT,KAAK,gBAAgB,iBACnB,QAAO;GACT,KAAK,gBAAgB,iBACnB,QAAO;GACT,KAAK,gBAAgB,oBACnB,QAAO;;;;;;;;;;;;;;ACjCf,SAAgB,SAAS,GAAgB,GAAqB;AAC5D,KAAI,EAAE,SAAS,GACb,OAAM,IAAI,MAAM,4CAA4C;AAE9D,KAAI,EAAE,WAAW,EACf,OAAM,IAAI,MAAM,wCAAwC;AAG1D,4BAAQ,EAAE;AAEV,MAAK,IAAI,SAAS,GAAG,SAAS,IAAI,UAAU;EAC1C,MAAM,MAAM,EAAE;AACd,OAAK,IAAI,SAAS,GAAG,SAAS,GAAG,SAE/B,GAAE,YAAa,MAAO,KAAK,YAAa,UAAW;;;;;;;;;AAWzD,SAAgB,WAAW,GAAe,GAAsB;AAC9D,KAAI,EAAE,SAAS,GACb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AAgB,cAAc,SAAmB,QAAkC;CACjF,MAAM,YAAY,OAAO;AACzB,KAAI,cAAc,KAAK,QAAQ,WAAW,UACxC,OAAM,IAAI,YAAY,gBAAgB,iBAAiB;CAGzD,MAAM,cAAc,OAAO,GAAG;AAC9B,oBAAmB,WAAW,WAAW,YAAY;AAGrD,KAAI,CADkB,OAAO,OAAO,UAAU,MAAM,WAAW,YAAY,CAEzE,OAAM,IAAI,YAAY,gBAAgB,oBAAoB;AAG5D,KAAI,cAAc,EAChB,QAAO,IAAI,WAAW,OAAO,GAAG;MAC3B;EACL,MAAM,YAAY,IAAI,WAAW,QAAQ;EAEzC,MAAM,SAAS,YAAY,WAAW,WAAW,aAAa,QAAQ,aAAa;EACnF,MAAM,SAAS,YAAY,WAAW,WAAW,aAAa,QAAQ,aAAa;EACnF,MAAM,SAAS,aAAa,OAAO,SAAS,EAAE,EAAE,OAAO;EAEvD,IAAI,QAAQ;AACZ,OAAK,IAAI,IAAI,GAAG,IAAI,GAAG,IACrB,SAAQ,SAAS,OAAO,OAAO,OAAO;AAGxC,6BAAQ,OAAO;AACf,6BAAQ,OAAO;AAEf,MAAI,CAAC,MACH,OAAM,IAAI,YAAY,gBAAgB,gBAAgB;AAGxD,SAAO;;;;;;;;;ACxKX,MAAa,iBAAiB;;;;AAK9B,MAAa,iBAAiB;;;;AAK9B,MAAa,kBAAkB"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/error.ts","../src/hazmat.ts","../src/interpolate.ts","../src/shamir.ts","../src/index.ts"],"sourcesContent":["/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-shamir-rust/src/error.rs\n\n/**\n * Error types for Shamir secret sharing operations.\n *\n * Each variant mirrors a corresponding `Error::*` enum in\n * `bc-shamir-rust/src/error.rs` with the same trigger conditions and the\n * same default `Display` strings.\n *\n * Note on `InterpolationFailure`: this variant is **reserved but\n * unreachable** in both the Rust and TypeScript implementations.\n * `interpolate()` in `interpolate.ts` never actually returns / throws an\n * interpolation failure today — the Lagrange-basis math always succeeds\n * for any well-formed input. The variant is kept for forward\n * compatibility (e.g. should a future revision add input validation that\n * could reject pathological cases) and to keep the TS error type a 1:1\n * mirror of Rust's `Error` enum.\n */\nexport enum ShamirErrorType {\n SecretTooLong = \"SecretTooLong\",\n TooManyShares = \"TooManyShares\",\n /**\n * Reserved / unreachable in both Rust and TS today. See enum doc above.\n */\n InterpolationFailure = \"InterpolationFailure\",\n ChecksumFailure = \"ChecksumFailure\",\n SecretTooShort = \"SecretTooShort\",\n SecretNotEvenLen = \"SecretNotEvenLen\",\n InvalidThreshold = \"InvalidThreshold\",\n SharesUnequalLength = \"SharesUnequalLength\",\n}\n\n/**\n * Error class for Shamir secret sharing operations.\n */\nexport class ShamirError extends Error {\n readonly type: ShamirErrorType;\n\n constructor(type: ShamirErrorType, message?: string) {\n super(message ?? ShamirError.defaultMessage(type));\n this.type = type;\n this.name = \"ShamirError\";\n }\n\n private static defaultMessage(type: ShamirErrorType): string {\n switch (type) {\n case ShamirErrorType.SecretTooLong:\n return \"secret is too long\";\n case ShamirErrorType.TooManyShares:\n return \"too many shares\";\n case ShamirErrorType.InterpolationFailure:\n return \"interpolation failed\";\n case ShamirErrorType.ChecksumFailure:\n return \"checksum failure\";\n case ShamirErrorType.SecretTooShort:\n return \"secret is too short\";\n case ShamirErrorType.SecretNotEvenLen:\n return \"secret is not of even length\";\n case ShamirErrorType.InvalidThreshold:\n return \"invalid threshold\";\n case ShamirErrorType.SharesUnequalLength:\n return \"shares have unequal length\";\n }\n }\n}\n\n/**\n * Mirrors Rust's `Result<T, Error>` for API parity.\n *\n * The TypeScript port surfaces failures by throwing `ShamirError`\n * instances rather than returning a sum type, so this alias is a no-op\n * (`ShamirResult<T>` ≡ `T`). It is kept so signatures published in\n * `@bcts/shamir` remain visually parallel to their Rust counterparts.\n */\nexport type ShamirResult<T> = T;\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-shamir-rust/src/hazmat.rs\n// GF(2^8) bitsliced polynomial operations for Shamir secret sharing.\n//\n// **Defensive arity guards.** Each helper here checks its array-length\n// preconditions (`r.length === 8`, `x.length >= 32`, …) and throws a\n// plain `Error` on violation. These guards mirror Rust's slice-indexing\n// `panic!` semantics — they signal an internal *contract violation*\n// (programmer error), not an end-user input error, and are deliberately\n// **not** elevated to `ShamirError`: that type carries variants for\n// recoverable, user-facing failures of the public `splitSecret` /\n// `recoverSecret` APIs, and has no variant matching \"wrong array arity\".\n// The public Shamir API never triggers these guards in normal use.\n\nimport { memzero } from \"@bcts/crypto\";\n\n/**\n * Internal contract guard. Mirrors a Rust `assert!(condition, message)`\n * panic on the boundary between hazmat helpers — kept as a bare `Error`\n * so it cannot be confused with a `ShamirError` from the public API.\n */\nfunction assertContract(condition: boolean, message: string): void {\n if (!condition) {\n throw new Error(message);\n }\n}\n\n/**\n * Convert an array of bytes into a bitsliced representation.\n * Takes the first 32 bytes from x and produces 8 u32 values.\n *\n * @param r - Output array of 8 u32 values (bitsliced representation)\n * @param x - Input array of at least 32 bytes\n */\nexport function bitslice(r: Uint32Array, x: Uint8Array): void {\n assertContract(x.length >= 32, \"bitslice: input must be at least 32 bytes\");\n assertContract(r.length === 8, \"bitslice: output must have 8 elements\");\n\n memzero(r);\n\n for (let arrIdx = 0; arrIdx < 32; arrIdx++) {\n const cur = x[arrIdx];\n for (let bitIdx = 0; bitIdx < 8; bitIdx++) {\n // r[bitIdx] |= ((cur & (1 << bitIdx)) >> bitIdx) << arrIdx\n r[bitIdx] |= ((cur & (1 << bitIdx)) >>> bitIdx) << arrIdx;\n }\n }\n}\n\n/**\n * Convert a bitsliced representation back to bytes.\n *\n * @param r - Output array of at least 32 bytes\n * @param x - Input array of 8 u32 values (bitsliced representation)\n */\nexport function unbitslice(r: Uint8Array, x: Uint32Array): void {\n assertContract(r.length >= 32, \"unbitslice: output must be at least 32 bytes\");\n assertContract(x.length === 8, \"unbitslice: input must have 8 elements\");\n\n memzero(r.subarray(0, 32));\n\n for (let bitIdx = 0; bitIdx < 8; bitIdx++) {\n const cur = x[bitIdx];\n for (let arrIdx = 0; arrIdx < 32; arrIdx++) {\n // r[arrIdx] |= ((cur & (1 << arrIdx)) >> arrIdx) << bitIdx\n r[arrIdx] |= ((cur & (1 << arrIdx)) >>> arrIdx) << bitIdx;\n }\n }\n}\n\n/**\n * Set all 32 positions in a bitsliced array to the same byte value.\n *\n * @param r - Output array of 8 u32 values\n * @param x - Byte value to set in all positions\n */\nexport function bitsliceSetall(r: Uint32Array, x: number): void {\n assertContract(r.length === 8, \"bitsliceSetall: output must have 8 elements\");\n\n for (let idx = 0; idx < 8; idx++) {\n // JavaScript needs special handling for the arithmetic right shift\n // This mirrors: *r = (((((x as u32) & (1u32.wrapping_shl(idx as u32)))\n // .wrapping_shl(31 - idx as u32)) as i32)\n // .wrapping_shr(31)) as u32;\n const bit = (x >>> idx) & 1;\n r[idx] = bit === 1 ? 0xffffffff : 0;\n }\n}\n\n/**\n * Add (XOR) r with x and store the result in r.\n * In GF(2^8), addition is XOR.\n *\n * @param r - First operand and result\n * @param x - Second operand\n */\nexport function gf256Add(r: Uint32Array, x: Uint32Array): void {\n assertContract(r.length === 8 && x.length === 8, \"gf256Add: arrays must have 8 elements\");\n\n for (let i = 0; i < 8; i++) {\n r[i] ^= x[i];\n }\n}\n\n/**\n * Safely multiply two bitsliced polynomials in GF(2^8) reduced by\n * x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r\n * and b will produce an incorrect result! If you need to square a polynomial\n * use gf256Square instead.\n *\n * @param r - Result array (8 u32 values)\n * @param a - First operand (may overlap with r)\n * @param b - Second operand (must NOT overlap with r)\n */\nexport function gf256Mul(r: Uint32Array, a: Uint32Array, b: Uint32Array): void {\n assertContract(\n r.length === 8 && a.length === 8 && b.length === 8,\n \"gf256Mul: arrays must have 8 elements\",\n );\n\n // Russian Peasant multiplication on two bitsliced polynomials\n const a2 = new Uint32Array(a);\n\n r[0] = a2[0] & b[0];\n r[1] = a2[1] & b[0];\n r[2] = a2[2] & b[0];\n r[3] = a2[3] & b[0];\n r[4] = a2[4] & b[0];\n r[5] = a2[5] & b[0];\n r[6] = a2[6] & b[0];\n r[7] = a2[7] & b[0];\n a2[0] ^= a2[7]; // reduce\n a2[2] ^= a2[7];\n a2[3] ^= a2[7];\n\n r[0] ^= a2[7] & b[1]; // add\n r[1] ^= a2[0] & b[1];\n r[2] ^= a2[1] & b[1];\n r[3] ^= a2[2] & b[1];\n r[4] ^= a2[3] & b[1];\n r[5] ^= a2[4] & b[1];\n r[6] ^= a2[5] & b[1];\n r[7] ^= a2[6] & b[1];\n a2[7] ^= a2[6]; // reduce\n a2[1] ^= a2[6];\n a2[2] ^= a2[6];\n\n r[0] ^= a2[6] & b[2]; // add\n r[1] ^= a2[7] & b[2];\n r[2] ^= a2[0] & b[2];\n r[3] ^= a2[1] & b[2];\n r[4] ^= a2[2] & b[2];\n r[5] ^= a2[3] & b[2];\n r[6] ^= a2[4] & b[2];\n r[7] ^= a2[5] & b[2];\n a2[6] ^= a2[5]; // reduce\n a2[0] ^= a2[5];\n a2[1] ^= a2[5];\n\n r[0] ^= a2[5] & b[3]; // add\n r[1] ^= a2[6] & b[3];\n r[2] ^= a2[7] & b[3];\n r[3] ^= a2[0] & b[3];\n r[4] ^= a2[1] & b[3];\n r[5] ^= a2[2] & b[3];\n r[6] ^= a2[3] & b[3];\n r[7] ^= a2[4] & b[3];\n a2[5] ^= a2[4]; // reduce\n a2[7] ^= a2[4];\n a2[0] ^= a2[4];\n\n r[0] ^= a2[4] & b[4]; // add\n r[1] ^= a2[5] & b[4];\n r[2] ^= a2[6] & b[4];\n r[3] ^= a2[7] & b[4];\n r[4] ^= a2[0] & b[4];\n r[5] ^= a2[1] & b[4];\n r[6] ^= a2[2] & b[4];\n r[7] ^= a2[3] & b[4];\n a2[4] ^= a2[3]; // reduce\n a2[6] ^= a2[3];\n a2[7] ^= a2[3];\n\n r[0] ^= a2[3] & b[5]; // add\n r[1] ^= a2[4] & b[5];\n r[2] ^= a2[5] & b[5];\n r[3] ^= a2[6] & b[5];\n r[4] ^= a2[7] & b[5];\n r[5] ^= a2[0] & b[5];\n r[6] ^= a2[1] & b[5];\n r[7] ^= a2[2] & b[5];\n a2[3] ^= a2[2]; // reduce\n a2[5] ^= a2[2];\n a2[6] ^= a2[2];\n\n r[0] ^= a2[2] & b[6]; // add\n r[1] ^= a2[3] & b[6];\n r[2] ^= a2[4] & b[6];\n r[3] ^= a2[5] & b[6];\n r[4] ^= a2[6] & b[6];\n r[5] ^= a2[7] & b[6];\n r[6] ^= a2[0] & b[6];\n r[7] ^= a2[1] & b[6];\n a2[2] ^= a2[1]; // reduce\n a2[4] ^= a2[1];\n a2[5] ^= a2[1];\n\n r[0] ^= a2[1] & b[7]; // add\n r[1] ^= a2[2] & b[7];\n r[2] ^= a2[3] & b[7];\n r[3] ^= a2[4] & b[7];\n r[4] ^= a2[5] & b[7];\n r[5] ^= a2[6] & b[7];\n r[6] ^= a2[7] & b[7];\n r[7] ^= a2[0] & b[7];\n}\n\n/**\n * Square x in GF(2^8) and write the result to r.\n * r and x may overlap.\n *\n * @param r - Result array (8 u32 values)\n * @param x - Value to square\n */\nexport function gf256Square(r: Uint32Array, x: Uint32Array): void {\n assertContract(r.length === 8 && x.length === 8, \"gf256Square: arrays must have 8 elements\");\n\n // Use the Freshman's Dream rule to square the polynomial\n // Assignments are done from 7 downto 0, because this allows\n // in-place operation (e.g. gf256Square(r, r))\n const r14 = x[7];\n const r12 = x[6];\n let r10 = x[5];\n let r8 = x[4];\n r[6] = x[3];\n r[4] = x[2];\n r[2] = x[1];\n r[0] = x[0];\n\n // Reduce with x^8 + x^4 + x^3 + x + 1 until order is less than 8\n r[7] = r14; // r[7] was 0\n r[6] ^= r14;\n r10 ^= r14;\n // Skip, because r13 is always 0\n r[4] ^= r12;\n r[5] = r12; // r[5] was 0\n r[7] ^= r12;\n r8 ^= r12;\n // Skip, because r11 is always 0\n r[2] ^= r10;\n r[3] = r10; // r[3] was 0\n r[5] ^= r10;\n r[6] ^= r10;\n r[1] = r14; // r[1] was 0\n r[2] ^= r14; // Substitute r9 by r14 because they will always be equal\n r[4] ^= r14;\n r[5] ^= r14;\n r[0] ^= r8;\n r[1] ^= r8;\n r[3] ^= r8;\n r[4] ^= r8;\n}\n\n/**\n * Invert x in GF(2^8) and write the result to r.\n *\n * @param r - Result array (8 u32 values)\n * @param x - Value to invert (will be modified)\n */\nexport function gf256Inv(r: Uint32Array, x: Uint32Array): void {\n assertContract(r.length === 8 && x.length === 8, \"gf256Inv: arrays must have 8 elements\");\n\n const y = new Uint32Array(8);\n const z = new Uint32Array(8);\n\n gf256Square(y, x); // y = x^2\n const y2 = new Uint32Array(y);\n gf256Square(y, y2); // y = x^4\n gf256Square(r, y); // r = x^8\n gf256Mul(z, r, x); // z = x^9\n const r2a = new Uint32Array(r);\n gf256Square(r, r2a); // r = x^16\n const r2b = new Uint32Array(r);\n gf256Mul(r, r2b, z); // r = x^25\n const r2c = new Uint32Array(r);\n gf256Square(r, r2c); // r = x^50\n gf256Square(z, r); // z = x^100\n const z2 = new Uint32Array(z);\n gf256Square(z, z2); // z = x^200\n const r2d = new Uint32Array(r);\n gf256Mul(r, r2d, z); // r = x^250\n const r2e = new Uint32Array(r);\n gf256Mul(r, r2e, y); // r = x^254\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-shamir-rust/src/interpolate.rs\n\nimport { memzero, memzeroVecVecU8 } from \"@bcts/crypto\";\nimport { MAX_SECRET_LEN } from \"./index.js\";\nimport { bitslice, bitsliceSetall, gf256Add, gf256Inv, gf256Mul, unbitslice } from \"./hazmat.js\";\n\n/**\n * Calculate the lagrange basis coefficients for the lagrange polynomial\n * defined by the x coordinates xc at the value x.\n *\n * After the function runs, the values array should hold data satisfying:\n * --- (x-xc[j])\n * values[i] = | | -------------\n * j != i (xc[i]-xc[j])\n *\n * @param values - Output array for the lagrange basis values\n * @param n - Number of points (length of the xc array, 0 < n <= 32)\n * @param xc - Array of x components to use as interpolating points\n * @param x - x coordinate to evaluate lagrange polynomials at\n */\nfunction hazmatLagrangeBasis(values: Uint8Array, n: number, xc: Uint8Array, x: number): void {\n // call the contents of xc [ x0 x1 x2 ... xn-1 ]\n const xx = new Uint8Array(32 + 16);\n const xSlice = new Uint32Array(8);\n const lxi: Uint32Array[] = [];\n for (let i = 0; i < n; i++) {\n lxi.push(new Uint32Array(8));\n }\n const numerator = new Uint32Array(8);\n const denominator = new Uint32Array(8);\n const temp = new Uint32Array(8);\n\n xx.set(xc.subarray(0, n), 0);\n\n // xx now contains bitsliced [ x0 x1 x2 ... xn-1 0 0 0 ... ]\n for (let i = 0; i < n; i++) {\n // lxi = bitsliced [ xi xi+1 xi+2 ... xi-1 0 0 0 ]\n bitslice(lxi[i], xx.subarray(i));\n xx[i + n] = xx[i];\n }\n\n bitsliceSetall(xSlice, x);\n bitsliceSetall(numerator, 1);\n bitsliceSetall(denominator, 1);\n\n for (let i = 1; i < n; i++) {\n temp.set(xSlice);\n gf256Add(temp, lxi[i]);\n // temp = [ x-xi+i x-xi+2 x-xi+3 ... x-xi x x x]\n const numerator2 = new Uint32Array(numerator);\n gf256Mul(numerator, numerator2, temp);\n\n temp.set(lxi[0]);\n gf256Add(temp, lxi[i]);\n // temp = [x0-xi+1 x1-xi+1 x2-xi+2 ... xn-x0 0 0 0]\n const denominator2 = new Uint32Array(denominator);\n gf256Mul(denominator, denominator2, temp);\n }\n\n // At this stage the numerator contains\n // [ num0 num1 num2 ... numn 0 0 0]\n //\n // where numi = prod(j, j!=i, x-xj )\n //\n // and the denominator contains\n // [ d0 d1 d2 ... dn 0 0 0]\n //\n // where di = prod(j, j!=i, xi-xj)\n\n gf256Inv(temp, denominator);\n\n // gf256_inv uses exponentiation to calculate inverse, so the zeros end up\n // remaining zeros.\n\n // tmp = [ 1/d0 1/d1 1/d2 ... 1/dn 0 0 0]\n\n const numerator2 = new Uint32Array(numerator);\n gf256Mul(numerator, numerator2, temp);\n\n // numerator now contains [ l_n_0(x) l_n_1(x) ... l_n_n-1(x) 0 0 0]\n // use the xx array to unpack it\n\n unbitslice(xx, numerator);\n\n // copy results to output array\n values.set(xx.subarray(0, n), 0);\n}\n\n/**\n * Safely interpolate the polynomial going through\n * the points (x0 [y0_0 y0_1 y0_2 ... y0_31]) , (x1 [y1_0 ...]), ...\n *\n * where\n * xi points to [x0 x1 ... xn-1 ]\n * y contains an array of pointers to 32-bit arrays of y values\n * y contains [y0 y1 y2 ... yn-1]\n * and each of the yi arrays contain [yi_0 yi_i ... yi_31].\n *\n * @param n - Number of points to interpolate\n * @param xi - x coordinates for points (array of length n)\n * @param yl - Length of y coordinate arrays\n * @param yij - Array of n arrays of length yl\n * @param x - Coordinate to interpolate at\n * @returns The interpolated result of length yl\n */\nexport function interpolate(\n n: number,\n xi: Uint8Array,\n yl: number,\n yij: Uint8Array[],\n x: number,\n): Uint8Array {\n // The hazmat gf256 implementation needs the y-coordinate data\n // to be in 32-byte blocks\n const y: Uint8Array[] = [];\n for (let i = 0; i < n; i++) {\n y.push(new Uint8Array(MAX_SECRET_LEN));\n }\n const values = new Uint8Array(MAX_SECRET_LEN);\n\n for (let i = 0; i < n; i++) {\n y[i].set(yij[i].subarray(0, yl), 0);\n }\n\n const lagrange = new Uint8Array(n);\n const ySlice = new Uint32Array(8);\n const resultSlice = new Uint32Array(8);\n const temp = new Uint32Array(8);\n\n hazmatLagrangeBasis(lagrange, n, xi, x);\n\n bitsliceSetall(resultSlice, 0);\n\n for (let i = 0; i < n; i++) {\n bitslice(ySlice, y[i]);\n bitsliceSetall(temp, lagrange[i]);\n const temp2 = new Uint32Array(temp);\n gf256Mul(temp, temp2, ySlice);\n gf256Add(resultSlice, temp);\n }\n\n unbitslice(values, resultSlice);\n\n // the calling code is only expecting yl bytes back\n const result = new Uint8Array(yl);\n result.set(values.subarray(0, yl), 0);\n\n // clean up stack\n memzero(lagrange);\n memzero(ySlice);\n memzero(resultSlice);\n memzero(temp);\n memzeroVecVecU8(y);\n memzero(values);\n\n return result;\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-shamir-rust/src/shamir.rs\n\nimport { hmacSha256, memzero, memzeroVecVecU8 } from \"@bcts/crypto\";\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\n\nimport { ShamirError, ShamirErrorType } from \"./error.js\";\nimport { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN } from \"./index.js\";\nimport { interpolate } from \"./interpolate.js\";\n\nconst SECRET_INDEX = 255;\nconst DIGEST_INDEX = 254;\n\nfunction createDigest(randomData: Uint8Array, sharedSecret: Uint8Array): Uint8Array {\n return hmacSha256(randomData, sharedSecret);\n}\n\nfunction validateParameters(threshold: number, shareCount: number, secretLength: number): void {\n if (shareCount > MAX_SHARE_COUNT) {\n throw new ShamirError(ShamirErrorType.TooManyShares);\n } else if (threshold < 1 || threshold > shareCount) {\n throw new ShamirError(ShamirErrorType.InvalidThreshold);\n } else if (secretLength > MAX_SECRET_LEN) {\n throw new ShamirError(ShamirErrorType.SecretTooLong);\n } else if (secretLength < MIN_SECRET_LEN) {\n throw new ShamirError(ShamirErrorType.SecretTooShort);\n } else if ((secretLength & 1) !== 0) {\n throw new ShamirError(ShamirErrorType.SecretNotEvenLen);\n }\n}\n\n/**\n * Splits a secret into shares using the Shamir secret sharing algorithm.\n *\n * @param threshold - The minimum number of shares required to reconstruct the\n * secret. Must be greater than or equal to 1 and less than or equal to\n * shareCount.\n * @param shareCount - The total number of shares to generate. Must be at least\n * threshold and less than or equal to MAX_SHARE_COUNT.\n * @param secret - A Uint8Array containing the secret to be split. Must be at\n * least MIN_SECRET_LEN bytes long and at most MAX_SECRET_LEN bytes long.\n * The length must be an even number.\n * @param randomGenerator - An implementation of the RandomNumberGenerator\n * interface, used to generate random data.\n * @returns An array of Uint8Array representing the shares of the secret.\n * @throws ShamirError if parameters are invalid\n *\n * @example\n * ```typescript\n * import { splitSecret } from \"@bcts/shamir\";\n * import { SecureRandomNumberGenerator } from \"@bcts/rand\";\n *\n * const threshold = 2;\n * const shareCount = 3;\n * const secret = new TextEncoder().encode(\"my secret belongs to me.\");\n * const rng = new SecureRandomNumberGenerator();\n *\n * const shares = splitSecret(threshold, shareCount, secret, rng);\n * console.log(shares.length); // 3\n * ```\n */\nexport function splitSecret(\n threshold: number,\n shareCount: number,\n secret: Uint8Array,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[] {\n validateParameters(threshold, shareCount, secret.length);\n\n if (threshold === 1) {\n // just return shareCount copies of the secret\n const result: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n result.push(new Uint8Array(secret));\n }\n return result;\n } else {\n const x = new Uint8Array(shareCount);\n const y: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n y.push(new Uint8Array(secret.length));\n }\n let n = 0;\n const result: Uint8Array[] = [];\n for (let i = 0; i < shareCount; i++) {\n result.push(new Uint8Array(secret.length));\n }\n\n for (let index = 0; index < threshold - 2; index++) {\n randomGenerator.fillRandomData(result[index]);\n x[n] = index;\n y[n].set(result[index]);\n n++;\n }\n\n // generate secret_length - 4 bytes worth of random data\n const digest = new Uint8Array(secret.length);\n randomGenerator.fillRandomData(digest.subarray(4));\n // put 4 bytes of digest at the top of the digest array\n const d = createDigest(digest.subarray(4), secret);\n digest.set(d.subarray(0, 4), 0);\n x[n] = DIGEST_INDEX;\n y[n].set(digest);\n n++;\n\n x[n] = SECRET_INDEX;\n y[n].set(secret);\n n++;\n\n for (let index = threshold - 2; index < shareCount; index++) {\n const v = interpolate(n, x, secret.length, y, index);\n result[index].set(v);\n }\n\n // clean up stack\n memzero(digest);\n memzero(x);\n memzeroVecVecU8(y);\n\n return result;\n }\n}\n\n/**\n * Recovers the secret from the given shares using the Shamir secret sharing\n * algorithm.\n *\n * @param indexes - An array of indexes of the shares to be used for recovering\n * the secret. These are the indexes of the shares returned by splitSecret.\n * @param shares - An array of shares of the secret matching the indexes in\n * indexes. These are the shares returned by splitSecret.\n * @returns A Uint8Array representing the recovered secret.\n * @throws ShamirError if parameters are invalid or checksum verification fails\n *\n * @example\n * ```typescript\n * import { recoverSecret } from \"@bcts/shamir\";\n *\n * const indexes = [0, 2];\n * const shares = [\n * new Uint8Array([47, 165, 102, 232, ...]),\n * new Uint8Array([221, 174, 116, 201, ...]),\n * ];\n *\n * const secret = recoverSecret(indexes, shares);\n * console.log(new TextDecoder().decode(secret)); // \"my secret belongs to me.\"\n * ```\n */\nexport function recoverSecret(indexes: number[], shares: Uint8Array[]): Uint8Array {\n const threshold = shares.length;\n if (threshold === 0 || indexes.length !== threshold) {\n throw new ShamirError(ShamirErrorType.InvalidThreshold);\n }\n\n const shareLength = shares[0].length;\n validateParameters(threshold, threshold, shareLength);\n\n const allSameLength = shares.every((share) => share.length === shareLength);\n if (!allSameLength) {\n throw new ShamirError(ShamirErrorType.SharesUnequalLength);\n }\n\n if (threshold === 1) {\n return new Uint8Array(shares[0]);\n } else {\n const indexesU8 = new Uint8Array(indexes);\n\n const digest = interpolate(threshold, indexesU8, shareLength, shares, DIGEST_INDEX);\n const secret = interpolate(threshold, indexesU8, shareLength, shares, SECRET_INDEX);\n const verify = createDigest(digest.subarray(4), secret);\n\n let valid = true;\n for (let i = 0; i < 4; i++) {\n valid = valid && digest[i] === verify[i];\n }\n\n memzero(digest);\n memzero(verify);\n\n if (!valid) {\n throw new ShamirError(ShamirErrorType.ChecksumFailure);\n }\n\n return secret;\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Blockchain Commons Shamir Secret Sharing\n// Ported from bc-shamir-rust\n//\n// This is a pure TypeScript implementation of Shamir's Secret Sharing (SSS),\n// a cryptographic technique in which a secret is divided into parts, called\n// shares, in such a way that a threshold of several shares are needed to\n// reconstruct the secret. The shares are distributed in a way that makes it\n// impossible for an attacker to know anything about the secret without having\n// a threshold of shares. If the number of shares is less than the threshold,\n// then no information about the secret is revealed.\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = 16;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = 32;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = 16;\n\n// Error types\nexport { ShamirError, ShamirErrorType, type ShamirResult } from \"./error.js\";\n\n// Main functions\nexport { splitSecret, recoverSecret } from \"./shamir.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,IAAY,kBAAL,yBAAA,iBAAA;AACL,iBAAA,mBAAA;AACA,iBAAA,mBAAA;;;;AAIA,iBAAA,0BAAA;AACA,iBAAA,qBAAA;AACA,iBAAA,oBAAA;AACA,iBAAA,sBAAA;AACA,iBAAA,sBAAA;AACA,iBAAA,yBAAA;;KACD;;;;AAKD,IAAa,cAAb,MAAa,oBAAoB,MAAM;CACrC;CAEA,YAAY,MAAuB,SAAkB;AACnD,QAAM,WAAW,YAAY,eAAe,KAAK,CAAC;AAClD,OAAK,OAAO;AACZ,OAAK,OAAO;;CAGd,OAAe,eAAe,MAA+B;AAC3D,UAAQ,MAAR;GACE,KAAA,gBACE,QAAO;GACT,KAAA,gBACE,QAAO;GACT,KAAA,uBACE,QAAO;GACT,KAAA,kBACE,QAAO;GACT,KAAA,iBACE,QAAO;GACT,KAAA,mBACE,QAAO;GACT,KAAA,mBACE,QAAO;GACT,KAAA,sBACE,QAAO;;;;;;;;;;;;;;;;ACzCf,SAAS,eAAe,WAAoB,SAAuB;AACjE,KAAI,CAAC,UACH,OAAM,IAAI,MAAM,QAAQ;;;;;;;;;AAW5B,SAAgB,SAAS,GAAgB,GAAqB;AAC5D,gBAAe,EAAE,UAAU,IAAI,4CAA4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GED
|
@@ -1,18 +1,39 @@
|
|
|
1
1
|
import { RandomNumberGenerator } from "@bcts/rand";
|
|
2
2
|
|
|
3
3
|
//#region src/error.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
6
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
4
9
|
/**
|
|
5
10
|
* Error types for Shamir secret sharing operations.
|
|
11
|
+
*
|
|
12
|
+
* Each variant mirrors a corresponding `Error::*` enum in
|
|
13
|
+
* `bc-shamir-rust/src/error.rs` with the same trigger conditions and the
|
|
14
|
+
* same default `Display` strings.
|
|
15
|
+
*
|
|
16
|
+
* Note on `InterpolationFailure`: this variant is **reserved but
|
|
17
|
+
* unreachable** in both the Rust and TypeScript implementations.
|
|
18
|
+
* `interpolate()` in `interpolate.ts` never actually returns / throws an
|
|
19
|
+
* interpolation failure today — the Lagrange-basis math always succeeds
|
|
20
|
+
* for any well-formed input. The variant is kept for forward
|
|
21
|
+
* compatibility (e.g. should a future revision add input validation that
|
|
22
|
+
* could reject pathological cases) and to keep the TS error type a 1:1
|
|
23
|
+
* mirror of Rust's `Error` enum.
|
|
6
24
|
*/
|
|
7
25
|
declare enum ShamirErrorType {
|
|
8
26
|
SecretTooLong = "SecretTooLong",
|
|
9
27
|
TooManyShares = "TooManyShares",
|
|
28
|
+
/**
|
|
29
|
+
* Reserved / unreachable in both Rust and TS today. See enum doc above.
|
|
30
|
+
*/
|
|
10
31
|
InterpolationFailure = "InterpolationFailure",
|
|
11
32
|
ChecksumFailure = "ChecksumFailure",
|
|
12
33
|
SecretTooShort = "SecretTooShort",
|
|
13
34
|
SecretNotEvenLen = "SecretNotEvenLen",
|
|
14
35
|
InvalidThreshold = "InvalidThreshold",
|
|
15
|
-
SharesUnequalLength = "SharesUnequalLength"
|
|
36
|
+
SharesUnequalLength = "SharesUnequalLength"
|
|
16
37
|
}
|
|
17
38
|
/**
|
|
18
39
|
* Error class for Shamir secret sharing operations.
|
|
@@ -22,6 +43,14 @@ declare class ShamirError extends Error {
|
|
|
22
43
|
constructor(type: ShamirErrorType, message?: string);
|
|
23
44
|
private static defaultMessage;
|
|
24
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Mirrors Rust's `Result<T, Error>` for API parity.
|
|
48
|
+
*
|
|
49
|
+
* The TypeScript port surfaces failures by throwing `ShamirError`
|
|
50
|
+
* instances rather than returning a sum type, so this alias is a no-op
|
|
51
|
+
* (`ShamirResult<T>` ≡ `T`). It is kept so signatures published in
|
|
52
|
+
* `@bcts/shamir` remain visually parallel to their Rust counterparts.
|
|
53
|
+
*/
|
|
25
54
|
type ShamirResult<T> = T;
|
|
26
55
|
//#endregion
|
|
27
56
|
//#region src/shamir.d.ts
|
|
@@ -83,85 +112,12 @@ declare function splitSecret(threshold: number, shareCount: number, secret: Uint
|
|
|
83
112
|
*/
|
|
84
113
|
declare function recoverSecret(indexes: number[], shares: Uint8Array[]): Uint8Array;
|
|
85
114
|
//#endregion
|
|
86
|
-
//#region src/
|
|
87
|
-
/**
|
|
88
|
-
* Convert an array of bytes into a bitsliced representation.
|
|
89
|
-
* Takes the first 32 bytes from x and produces 8 u32 values.
|
|
90
|
-
*
|
|
91
|
-
* @param r - Output array of 8 u32 values (bitsliced representation)
|
|
92
|
-
* @param x - Input array of at least 32 bytes
|
|
93
|
-
*/
|
|
94
|
-
declare function bitslice(r: Uint32Array, x: Uint8Array): void;
|
|
95
|
-
/**
|
|
96
|
-
* Convert a bitsliced representation back to bytes.
|
|
97
|
-
*
|
|
98
|
-
* @param r - Output array of at least 32 bytes
|
|
99
|
-
* @param x - Input array of 8 u32 values (bitsliced representation)
|
|
100
|
-
*/
|
|
101
|
-
declare function unbitslice(r: Uint8Array, x: Uint32Array): void;
|
|
102
|
-
/**
|
|
103
|
-
* Set all 32 positions in a bitsliced array to the same byte value.
|
|
104
|
-
*
|
|
105
|
-
* @param r - Output array of 8 u32 values
|
|
106
|
-
* @param x - Byte value to set in all positions
|
|
107
|
-
*/
|
|
108
|
-
declare function bitsliceSetall(r: Uint32Array, x: number): void;
|
|
109
|
-
/**
|
|
110
|
-
* Add (XOR) r with x and store the result in r.
|
|
111
|
-
* In GF(2^8), addition is XOR.
|
|
112
|
-
*
|
|
113
|
-
* @param r - First operand and result
|
|
114
|
-
* @param x - Second operand
|
|
115
|
-
*/
|
|
116
|
-
declare function gf256Add(r: Uint32Array, x: Uint32Array): void;
|
|
117
|
-
/**
|
|
118
|
-
* Safely multiply two bitsliced polynomials in GF(2^8) reduced by
|
|
119
|
-
* x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r
|
|
120
|
-
* and b will produce an incorrect result! If you need to square a polynomial
|
|
121
|
-
* use gf256Square instead.
|
|
122
|
-
*
|
|
123
|
-
* @param r - Result array (8 u32 values)
|
|
124
|
-
* @param a - First operand (may overlap with r)
|
|
125
|
-
* @param b - Second operand (must NOT overlap with r)
|
|
126
|
-
*/
|
|
127
|
-
declare function gf256Mul(r: Uint32Array, a: Uint32Array, b: Uint32Array): void;
|
|
128
|
-
/**
|
|
129
|
-
* Square x in GF(2^8) and write the result to r.
|
|
130
|
-
* r and x may overlap.
|
|
131
|
-
*
|
|
132
|
-
* @param r - Result array (8 u32 values)
|
|
133
|
-
* @param x - Value to square
|
|
134
|
-
*/
|
|
135
|
-
declare function gf256Square(r: Uint32Array, x: Uint32Array): void;
|
|
136
|
-
/**
|
|
137
|
-
* Invert x in GF(2^8) and write the result to r.
|
|
138
|
-
*
|
|
139
|
-
* @param r - Result array (8 u32 values)
|
|
140
|
-
* @param x - Value to invert (will be modified)
|
|
141
|
-
*/
|
|
142
|
-
declare function gf256Inv(r: Uint32Array, x: Uint32Array): void;
|
|
143
|
-
//#endregion
|
|
144
|
-
//#region src/interpolate.d.ts
|
|
115
|
+
//#region src/index.d.ts
|
|
145
116
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* where
|
|
150
|
-
* xi points to [x0 x1 ... xn-1 ]
|
|
151
|
-
* y contains an array of pointers to 32-bit arrays of y values
|
|
152
|
-
* y contains [y0 y1 y2 ... yn-1]
|
|
153
|
-
* and each of the yi arrays contain [yi_0 yi_i ... yi_31].
|
|
117
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
118
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
154
119
|
*
|
|
155
|
-
* @param n - Number of points to interpolate
|
|
156
|
-
* @param xi - x coordinates for points (array of length n)
|
|
157
|
-
* @param yl - Length of y coordinate arrays
|
|
158
|
-
* @param yij - Array of n arrays of length yl
|
|
159
|
-
* @param x - Coordinate to interpolate at
|
|
160
|
-
* @returns The interpolated result of length yl
|
|
161
120
|
*/
|
|
162
|
-
declare function interpolate(n: number, xi: Uint8Array, yl: number, yij: Uint8Array[], x: number): Uint8Array;
|
|
163
|
-
//#endregion
|
|
164
|
-
//#region src/index.d.ts
|
|
165
121
|
/**
|
|
166
122
|
* The minimum length of a secret.
|
|
167
123
|
*/
|
|
@@ -175,5 +131,5 @@ declare const MAX_SECRET_LEN = 32;
|
|
|
175
131
|
*/
|
|
176
132
|
declare const MAX_SHARE_COUNT = 16;
|
|
177
133
|
//#endregion
|
|
178
|
-
export { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN, ShamirError, ShamirErrorType, type ShamirResult,
|
|
134
|
+
export { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN, ShamirError, ShamirErrorType, type ShamirResult, recoverSecret, splitSecret };
|
|
179
135
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/error.ts","../src/shamir.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/error.ts","../src/shamir.ts","../src/index.ts"],"mappings":";;;;;;AAwBA;;;;;;;;;;;;;;AAiBA;;;;aAjBY,eAAA;EACV,aAAA;EACA,aAAA;EAeoC;;;EAXpC,oBAAA;EACA,eAAA;EACA,cAAA;EACA,gBAAA;EACA,gBAAA;EACA,mBAAA;AAAA;;AA6CF;;cAvCa,WAAA,SAAoB,KAAA;EAAA,SACtB,IAAA,EAAM,eAAA;cAEH,IAAA,EAAM,eAAA,EAAiB,OAAA;EAAA,eAMpB,cAAA;AAAA;;ACgBjB;;;;;;;KDcY,YAAA,MAAkB,CAAA;;;;;;;;;;;;;;AAvC9B;;;;;;;;;;;;;;;;;;AAuCA;iBCdgB,WAAA,CACd,SAAA,UACA,UAAA,UACA,MAAA,EAAQ,UAAA,EACR,eAAA,EAAiB,qBAAA,GAChB,UAAA;;;;;;;AALH;;;;;;;;;;;;;;;;;AAuFA;;iBAAgB,aAAA,CAAc,OAAA,YAAmB,MAAA,EAAQ,UAAA,KAAe,UAAA;;;;;;ADjIxE;;;;;cEJa,cAAA;;;;cAKA,cAAA;;;;cAKA,eAAA"}
|