@bcts/shamir 1.0.0-alpha.5

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 ADDED
@@ -0,0 +1,48 @@
1
+ Copyright © 2023 Blockchain Commons, LLC
2
+ Copyright © 2025 Leonardo Amoroso Custodio
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ Subject to the terms and conditions of this license, each copyright holder and
15
+ contributor hereby grants to those receiving rights under this license a
16
+ perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
17
+ (except for failure to satisfy the conditions of this license) patent license to
18
+ make, have made, use, offer to sell, sell, import, and otherwise transfer this
19
+ software, where such license applies only to those patent claims, already
20
+ acquired or hereafter acquired, licensable by such copyright holder or
21
+ contributor that are necessarily infringed by:
22
+
23
+ (a) their Contribution(s) (the licensed copyrights of copyright holders and
24
+ non-copyrightable additions of contributors, in source or binary form)
25
+ alone; or
26
+
27
+ (b) combination of their Contribution(s) with the work of authorship to
28
+ which such Contribution(s) was added by such copyright holder or
29
+ contributor, if, at the time the Contribution is added, such addition causes
30
+ such combination to be necessarily infringed. The patent license shall not
31
+ apply to any other combinations which include the Contribution.
32
+
33
+ Except as expressly stated above, no rights or licenses from any copyright
34
+ holder or contributor is granted under this license, whether expressly, by
35
+ implication, estoppel or otherwise.
36
+
37
+ DISCLAIMER
38
+
39
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
40
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
43
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
47
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Blockchain Commons Shamir Secret Sharing for TypeScript
2
+
3
+ > Disclaimer: This package is under active development and APIs may change.
4
+
5
+ ## Introduction
6
+
7
+ This is a pure TypeScript implementation of [Shamir's Secret Sharing (SSS)](https://en.wikipedia.org/wiki/Shamir%27s_secret_sharing), a cryptographic technique in which a _secret_ is divided into parts, called _shares_, in such a way that a _threshold_ of several shares are needed to reconstruct the secret. The shares are distributed in a way that makes it impossible for an attacker to know anything about the secret without having a threshold of shares. If the number of shares is less than the threshold, then no information about the secret is revealed.
8
+
9
+ ## Rust Reference Implementation
10
+
11
+ This TypeScript implementation is based on [bc-shamir-rust](https://github.com/BlockchainCommons/bc-shamir-rust) **v0.13.0** ([commit](https://github.com/BlockchainCommons/bc-shamir-rust/tree/fcf8deb2b0f51566635a7de89ae6d8d6628921c7)).
package/dist/index.cjs ADDED
@@ -0,0 +1,506 @@
1
+ let __bcts_crypto = require("@bcts/crypto");
2
+
3
+ //#region src/error.ts
4
+ /**
5
+ * Error types for Shamir secret sharing operations.
6
+ */
7
+ let ShamirErrorType = /* @__PURE__ */ function(ShamirErrorType$1) {
8
+ ShamirErrorType$1["SecretTooLong"] = "SecretTooLong";
9
+ ShamirErrorType$1["TooManyShares"] = "TooManyShares";
10
+ ShamirErrorType$1["InterpolationFailure"] = "InterpolationFailure";
11
+ ShamirErrorType$1["ChecksumFailure"] = "ChecksumFailure";
12
+ ShamirErrorType$1["SecretTooShort"] = "SecretTooShort";
13
+ ShamirErrorType$1["SecretNotEvenLen"] = "SecretNotEvenLen";
14
+ ShamirErrorType$1["InvalidThreshold"] = "InvalidThreshold";
15
+ ShamirErrorType$1["SharesUnequalLength"] = "SharesUnequalLength";
16
+ return ShamirErrorType$1;
17
+ }({});
18
+ /**
19
+ * Error class for Shamir secret sharing operations.
20
+ */
21
+ var ShamirError = class ShamirError extends Error {
22
+ type;
23
+ constructor(type, message) {
24
+ super(message ?? ShamirError.defaultMessage(type));
25
+ this.type = type;
26
+ this.name = "ShamirError";
27
+ }
28
+ static defaultMessage(type) {
29
+ switch (type) {
30
+ case ShamirErrorType.SecretTooLong: return "secret is too long";
31
+ case ShamirErrorType.TooManyShares: return "too many shares";
32
+ case ShamirErrorType.InterpolationFailure: return "interpolation failed";
33
+ case ShamirErrorType.ChecksumFailure: return "checksum failure";
34
+ case ShamirErrorType.SecretTooShort: return "secret is too short";
35
+ case ShamirErrorType.SecretNotEvenLen: return "secret is not of even length";
36
+ case ShamirErrorType.InvalidThreshold: return "invalid threshold";
37
+ case ShamirErrorType.SharesUnequalLength: return "shares have unequal length";
38
+ }
39
+ }
40
+ };
41
+
42
+ //#endregion
43
+ //#region src/hazmat.ts
44
+ /**
45
+ * Convert an array of bytes into a bitsliced representation.
46
+ * Takes the first 32 bytes from x and produces 8 u32 values.
47
+ *
48
+ * @param r - Output array of 8 u32 values (bitsliced representation)
49
+ * @param x - Input array of at least 32 bytes
50
+ */
51
+ function bitslice(r, x) {
52
+ if (x.length < 32) throw new Error("bitslice: input must be at least 32 bytes");
53
+ if (r.length !== 8) throw new Error("bitslice: output must have 8 elements");
54
+ (0, __bcts_crypto.memzero)(r);
55
+ for (let arrIdx = 0; arrIdx < 32; arrIdx++) {
56
+ const cur = x[arrIdx];
57
+ for (let bitIdx = 0; bitIdx < 8; bitIdx++) r[bitIdx] |= (cur & 1 << bitIdx) >>> bitIdx << arrIdx;
58
+ }
59
+ }
60
+ /**
61
+ * Convert a bitsliced representation back to bytes.
62
+ *
63
+ * @param r - Output array of at least 32 bytes
64
+ * @param x - Input array of 8 u32 values (bitsliced representation)
65
+ */
66
+ function unbitslice(r, x) {
67
+ if (r.length < 32) throw new Error("unbitslice: output must be at least 32 bytes");
68
+ if (x.length !== 8) throw new Error("unbitslice: input must have 8 elements");
69
+ (0, __bcts_crypto.memzero)(r.subarray(0, 32));
70
+ for (let bitIdx = 0; bitIdx < 8; bitIdx++) {
71
+ const cur = x[bitIdx];
72
+ for (let arrIdx = 0; arrIdx < 32; arrIdx++) r[arrIdx] |= (cur & 1 << arrIdx) >>> arrIdx << bitIdx;
73
+ }
74
+ }
75
+ /**
76
+ * Set all 32 positions in a bitsliced array to the same byte value.
77
+ *
78
+ * @param r - Output array of 8 u32 values
79
+ * @param x - Byte value to set in all positions
80
+ */
81
+ function bitsliceSetall(r, x) {
82
+ if (r.length !== 8) throw new Error("bitsliceSetall: output must have 8 elements");
83
+ for (let idx = 0; idx < 8; idx++) r[idx] = (x >>> idx & 1) === 1 ? 4294967295 : 0;
84
+ }
85
+ /**
86
+ * Add (XOR) r with x and store the result in r.
87
+ * In GF(2^8), addition is XOR.
88
+ *
89
+ * @param r - First operand and result
90
+ * @param x - Second operand
91
+ */
92
+ function gf256Add(r, x) {
93
+ if (r.length !== 8 || x.length !== 8) throw new Error("gf256Add: arrays must have 8 elements");
94
+ for (let i = 0; i < 8; i++) r[i] ^= x[i];
95
+ }
96
+ /**
97
+ * Safely multiply two bitsliced polynomials in GF(2^8) reduced by
98
+ * x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r
99
+ * and b will produce an incorrect result! If you need to square a polynomial
100
+ * use gf256Square instead.
101
+ *
102
+ * @param r - Result array (8 u32 values)
103
+ * @param a - First operand (may overlap with r)
104
+ * @param b - Second operand (must NOT overlap with r)
105
+ */
106
+ function gf256Mul(r, a, b) {
107
+ if (r.length !== 8 || a.length !== 8 || b.length !== 8) throw new Error("gf256Mul: arrays must have 8 elements");
108
+ const a2 = new Uint32Array(a);
109
+ r[0] = a2[0] & b[0];
110
+ r[1] = a2[1] & b[0];
111
+ r[2] = a2[2] & b[0];
112
+ r[3] = a2[3] & b[0];
113
+ r[4] = a2[4] & b[0];
114
+ r[5] = a2[5] & b[0];
115
+ r[6] = a2[6] & b[0];
116
+ r[7] = a2[7] & b[0];
117
+ a2[0] ^= a2[7];
118
+ a2[2] ^= a2[7];
119
+ a2[3] ^= a2[7];
120
+ r[0] ^= a2[7] & b[1];
121
+ r[1] ^= a2[0] & b[1];
122
+ r[2] ^= a2[1] & b[1];
123
+ r[3] ^= a2[2] & b[1];
124
+ r[4] ^= a2[3] & b[1];
125
+ r[5] ^= a2[4] & b[1];
126
+ r[6] ^= a2[5] & b[1];
127
+ r[7] ^= a2[6] & b[1];
128
+ a2[7] ^= a2[6];
129
+ a2[1] ^= a2[6];
130
+ a2[2] ^= a2[6];
131
+ r[0] ^= a2[6] & b[2];
132
+ r[1] ^= a2[7] & b[2];
133
+ r[2] ^= a2[0] & b[2];
134
+ r[3] ^= a2[1] & b[2];
135
+ r[4] ^= a2[2] & b[2];
136
+ r[5] ^= a2[3] & b[2];
137
+ r[6] ^= a2[4] & b[2];
138
+ r[7] ^= a2[5] & b[2];
139
+ a2[6] ^= a2[5];
140
+ a2[0] ^= a2[5];
141
+ a2[1] ^= a2[5];
142
+ r[0] ^= a2[5] & b[3];
143
+ r[1] ^= a2[6] & b[3];
144
+ r[2] ^= a2[7] & b[3];
145
+ r[3] ^= a2[0] & b[3];
146
+ r[4] ^= a2[1] & b[3];
147
+ r[5] ^= a2[2] & b[3];
148
+ r[6] ^= a2[3] & b[3];
149
+ r[7] ^= a2[4] & b[3];
150
+ a2[5] ^= a2[4];
151
+ a2[7] ^= a2[4];
152
+ a2[0] ^= a2[4];
153
+ r[0] ^= a2[4] & b[4];
154
+ r[1] ^= a2[5] & b[4];
155
+ r[2] ^= a2[6] & b[4];
156
+ r[3] ^= a2[7] & b[4];
157
+ r[4] ^= a2[0] & b[4];
158
+ r[5] ^= a2[1] & b[4];
159
+ r[6] ^= a2[2] & b[4];
160
+ r[7] ^= a2[3] & b[4];
161
+ a2[4] ^= a2[3];
162
+ a2[6] ^= a2[3];
163
+ a2[7] ^= a2[3];
164
+ r[0] ^= a2[3] & b[5];
165
+ r[1] ^= a2[4] & b[5];
166
+ r[2] ^= a2[5] & b[5];
167
+ r[3] ^= a2[6] & b[5];
168
+ r[4] ^= a2[7] & b[5];
169
+ r[5] ^= a2[0] & b[5];
170
+ r[6] ^= a2[1] & b[5];
171
+ r[7] ^= a2[2] & b[5];
172
+ a2[3] ^= a2[2];
173
+ a2[5] ^= a2[2];
174
+ a2[6] ^= a2[2];
175
+ r[0] ^= a2[2] & b[6];
176
+ r[1] ^= a2[3] & b[6];
177
+ r[2] ^= a2[4] & b[6];
178
+ r[3] ^= a2[5] & b[6];
179
+ r[4] ^= a2[6] & b[6];
180
+ r[5] ^= a2[7] & b[6];
181
+ r[6] ^= a2[0] & b[6];
182
+ r[7] ^= a2[1] & b[6];
183
+ a2[2] ^= a2[1];
184
+ a2[4] ^= a2[1];
185
+ a2[5] ^= a2[1];
186
+ r[0] ^= a2[1] & b[7];
187
+ r[1] ^= a2[2] & b[7];
188
+ r[2] ^= a2[3] & b[7];
189
+ r[3] ^= a2[4] & b[7];
190
+ r[4] ^= a2[5] & b[7];
191
+ r[5] ^= a2[6] & b[7];
192
+ r[6] ^= a2[7] & b[7];
193
+ r[7] ^= a2[0] & b[7];
194
+ }
195
+ /**
196
+ * Square x in GF(2^8) and write the result to r.
197
+ * r and x may overlap.
198
+ *
199
+ * @param r - Result array (8 u32 values)
200
+ * @param x - Value to square
201
+ */
202
+ function gf256Square(r, x) {
203
+ if (r.length !== 8 || x.length !== 8) throw new Error("gf256Square: arrays must have 8 elements");
204
+ const r14 = x[7];
205
+ const r12 = x[6];
206
+ let r10 = x[5];
207
+ let r8 = x[4];
208
+ r[6] = x[3];
209
+ r[4] = x[2];
210
+ r[2] = x[1];
211
+ r[0] = x[0];
212
+ r[7] = r14;
213
+ r[6] ^= r14;
214
+ r10 ^= r14;
215
+ r[4] ^= r12;
216
+ r[5] = r12;
217
+ r[7] ^= r12;
218
+ r8 ^= r12;
219
+ r[2] ^= r10;
220
+ r[3] = r10;
221
+ r[5] ^= r10;
222
+ r[6] ^= r10;
223
+ r[1] = r14;
224
+ r[2] ^= r14;
225
+ r[4] ^= r14;
226
+ r[5] ^= r14;
227
+ r[0] ^= r8;
228
+ r[1] ^= r8;
229
+ r[3] ^= r8;
230
+ r[4] ^= r8;
231
+ }
232
+ /**
233
+ * Invert x in GF(2^8) and write the result to r.
234
+ *
235
+ * @param r - Result array (8 u32 values)
236
+ * @param x - Value to invert (will be modified)
237
+ */
238
+ function gf256Inv(r, x) {
239
+ if (r.length !== 8 || x.length !== 8) throw new Error("gf256Inv: arrays must have 8 elements");
240
+ const y = new Uint32Array(8);
241
+ const z = new Uint32Array(8);
242
+ gf256Square(y, x);
243
+ gf256Square(y, new Uint32Array(y));
244
+ gf256Square(r, y);
245
+ gf256Mul(z, r, x);
246
+ gf256Square(r, new Uint32Array(r));
247
+ gf256Mul(r, new Uint32Array(r), z);
248
+ gf256Square(r, new Uint32Array(r));
249
+ gf256Square(z, r);
250
+ gf256Square(z, new Uint32Array(z));
251
+ gf256Mul(r, new Uint32Array(r), z);
252
+ gf256Mul(r, new Uint32Array(r), y);
253
+ }
254
+
255
+ //#endregion
256
+ //#region src/interpolate.ts
257
+ /**
258
+ * Calculate the lagrange basis coefficients for the lagrange polynomial
259
+ * defined by the x coordinates xc at the value x.
260
+ *
261
+ * After the function runs, the values array should hold data satisfying:
262
+ * --- (x-xc[j])
263
+ * values[i] = | | -------------
264
+ * j != i (xc[i]-xc[j])
265
+ *
266
+ * @param values - Output array for the lagrange basis values
267
+ * @param n - Number of points (length of the xc array, 0 < n <= 32)
268
+ * @param xc - Array of x components to use as interpolating points
269
+ * @param x - x coordinate to evaluate lagrange polynomials at
270
+ */
271
+ function hazmatLagrangeBasis(values, n, xc, x) {
272
+ const xx = new Uint8Array(48);
273
+ const xSlice = new Uint32Array(8);
274
+ const lxi = [];
275
+ for (let i = 0; i < n; i++) lxi.push(new Uint32Array(8));
276
+ const numerator = new Uint32Array(8);
277
+ const denominator = new Uint32Array(8);
278
+ const temp = new Uint32Array(8);
279
+ xx.set(xc.subarray(0, n), 0);
280
+ for (let i = 0; i < n; i++) {
281
+ bitslice(lxi[i], xx.subarray(i));
282
+ xx[i + n] = xx[i];
283
+ }
284
+ bitsliceSetall(xSlice, x);
285
+ bitsliceSetall(numerator, 1);
286
+ bitsliceSetall(denominator, 1);
287
+ for (let i = 1; i < n; i++) {
288
+ temp.set(xSlice);
289
+ gf256Add(temp, lxi[i]);
290
+ gf256Mul(numerator, new Uint32Array(numerator), temp);
291
+ temp.set(lxi[0]);
292
+ gf256Add(temp, lxi[i]);
293
+ gf256Mul(denominator, new Uint32Array(denominator), temp);
294
+ }
295
+ gf256Inv(temp, denominator);
296
+ gf256Mul(numerator, new Uint32Array(numerator), temp);
297
+ unbitslice(xx, numerator);
298
+ values.set(xx.subarray(0, n), 0);
299
+ }
300
+ /**
301
+ * Safely interpolate the polynomial going through
302
+ * the points (x0 [y0_0 y0_1 y0_2 ... y0_31]) , (x1 [y1_0 ...]), ...
303
+ *
304
+ * where
305
+ * xi points to [x0 x1 ... xn-1 ]
306
+ * y contains an array of pointers to 32-bit arrays of y values
307
+ * y contains [y0 y1 y2 ... yn-1]
308
+ * and each of the yi arrays contain [yi_0 yi_i ... yi_31].
309
+ *
310
+ * @param n - Number of points to interpolate
311
+ * @param xi - x coordinates for points (array of length n)
312
+ * @param yl - Length of y coordinate arrays
313
+ * @param yij - Array of n arrays of length yl
314
+ * @param x - Coordinate to interpolate at
315
+ * @returns The interpolated result of length yl
316
+ */
317
+ function interpolate(n, xi, yl, yij, x) {
318
+ const y = [];
319
+ for (let i = 0; i < n; i++) y.push(new Uint8Array(MAX_SECRET_LEN));
320
+ const values = new Uint8Array(MAX_SECRET_LEN);
321
+ for (let i = 0; i < n; i++) y[i].set(yij[i].subarray(0, yl), 0);
322
+ const lagrange = new Uint8Array(n);
323
+ const ySlice = new Uint32Array(8);
324
+ const resultSlice = new Uint32Array(8);
325
+ const temp = new Uint32Array(8);
326
+ hazmatLagrangeBasis(lagrange, n, xi, x);
327
+ bitsliceSetall(resultSlice, 0);
328
+ for (let i = 0; i < n; i++) {
329
+ bitslice(ySlice, y[i]);
330
+ bitsliceSetall(temp, lagrange[i]);
331
+ gf256Mul(temp, new Uint32Array(temp), ySlice);
332
+ gf256Add(resultSlice, temp);
333
+ }
334
+ unbitslice(values, resultSlice);
335
+ const result = new Uint8Array(yl);
336
+ result.set(values.subarray(0, yl), 0);
337
+ (0, __bcts_crypto.memzero)(lagrange);
338
+ (0, __bcts_crypto.memzero)(ySlice);
339
+ (0, __bcts_crypto.memzero)(resultSlice);
340
+ (0, __bcts_crypto.memzero)(temp);
341
+ (0, __bcts_crypto.memzeroVecVecU8)(y);
342
+ (0, __bcts_crypto.memzero)(values);
343
+ return result;
344
+ }
345
+
346
+ //#endregion
347
+ //#region src/shamir.ts
348
+ const SECRET_INDEX = 255;
349
+ const DIGEST_INDEX = 254;
350
+ function createDigest(randomData, sharedSecret) {
351
+ return (0, __bcts_crypto.hmacSha256)(randomData, sharedSecret);
352
+ }
353
+ function validateParameters(threshold, shareCount, secretLength) {
354
+ if (shareCount > MAX_SHARE_COUNT) throw new ShamirError(ShamirErrorType.TooManyShares);
355
+ else if (threshold < 1 || threshold > shareCount) throw new ShamirError(ShamirErrorType.InvalidThreshold);
356
+ else if (secretLength > MAX_SECRET_LEN) throw new ShamirError(ShamirErrorType.SecretTooLong);
357
+ else if (secretLength < MIN_SECRET_LEN) throw new ShamirError(ShamirErrorType.SecretTooShort);
358
+ else if ((secretLength & 1) !== 0) throw new ShamirError(ShamirErrorType.SecretNotEvenLen);
359
+ }
360
+ /**
361
+ * Splits a secret into shares using the Shamir secret sharing algorithm.
362
+ *
363
+ * @param threshold - The minimum number of shares required to reconstruct the
364
+ * secret. Must be greater than or equal to 1 and less than or equal to
365
+ * shareCount.
366
+ * @param shareCount - The total number of shares to generate. Must be at least
367
+ * threshold and less than or equal to MAX_SHARE_COUNT.
368
+ * @param secret - A Uint8Array containing the secret to be split. Must be at
369
+ * least MIN_SECRET_LEN bytes long and at most MAX_SECRET_LEN bytes long.
370
+ * The length must be an even number.
371
+ * @param randomGenerator - An implementation of the RandomNumberGenerator
372
+ * interface, used to generate random data.
373
+ * @returns An array of Uint8Array representing the shares of the secret.
374
+ * @throws ShamirError if parameters are invalid
375
+ *
376
+ * @example
377
+ * ```typescript
378
+ * import { splitSecret } from "@bcts/shamir";
379
+ * import { SecureRandomNumberGenerator } from "@bcts/rand";
380
+ *
381
+ * const threshold = 2;
382
+ * const shareCount = 3;
383
+ * const secret = new TextEncoder().encode("my secret belongs to me.");
384
+ * const rng = new SecureRandomNumberGenerator();
385
+ *
386
+ * const shares = splitSecret(threshold, shareCount, secret, rng);
387
+ * console.log(shares.length); // 3
388
+ * ```
389
+ */
390
+ function splitSecret(threshold, shareCount, secret, randomGenerator) {
391
+ validateParameters(threshold, shareCount, secret.length);
392
+ if (threshold === 1) {
393
+ const result = [];
394
+ for (let i = 0; i < shareCount; i++) result.push(new Uint8Array(secret));
395
+ return result;
396
+ } else {
397
+ const x = new Uint8Array(shareCount);
398
+ const y = [];
399
+ for (let i = 0; i < shareCount; i++) y.push(new Uint8Array(secret.length));
400
+ let n = 0;
401
+ const result = [];
402
+ for (let i = 0; i < shareCount; i++) result.push(new Uint8Array(secret.length));
403
+ for (let index = 0; index < threshold - 2; index++) {
404
+ randomGenerator.fillRandomData(result[index]);
405
+ x[n] = index;
406
+ y[n].set(result[index]);
407
+ n++;
408
+ }
409
+ const digest = new Uint8Array(secret.length);
410
+ randomGenerator.fillRandomData(digest.subarray(4));
411
+ const d = createDigest(digest.subarray(4), secret);
412
+ digest.set(d.subarray(0, 4), 0);
413
+ x[n] = DIGEST_INDEX;
414
+ y[n].set(digest);
415
+ n++;
416
+ x[n] = SECRET_INDEX;
417
+ y[n].set(secret);
418
+ n++;
419
+ for (let index = threshold - 2; index < shareCount; index++) {
420
+ const v = interpolate(n, x, secret.length, y, index);
421
+ result[index].set(v);
422
+ }
423
+ (0, __bcts_crypto.memzero)(digest);
424
+ (0, __bcts_crypto.memzero)(x);
425
+ (0, __bcts_crypto.memzeroVecVecU8)(y);
426
+ return result;
427
+ }
428
+ }
429
+ /**
430
+ * Recovers the secret from the given shares using the Shamir secret sharing
431
+ * algorithm.
432
+ *
433
+ * @param indexes - An array of indexes of the shares to be used for recovering
434
+ * the secret. These are the indexes of the shares returned by splitSecret.
435
+ * @param shares - An array of shares of the secret matching the indexes in
436
+ * indexes. These are the shares returned by splitSecret.
437
+ * @returns A Uint8Array representing the recovered secret.
438
+ * @throws ShamirError if parameters are invalid or checksum verification fails
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * import { recoverSecret } from "@bcts/shamir";
443
+ *
444
+ * const indexes = [0, 2];
445
+ * const shares = [
446
+ * new Uint8Array([47, 165, 102, 232, ...]),
447
+ * new Uint8Array([221, 174, 116, 201, ...]),
448
+ * ];
449
+ *
450
+ * const secret = recoverSecret(indexes, shares);
451
+ * console.log(new TextDecoder().decode(secret)); // "my secret belongs to me."
452
+ * ```
453
+ */
454
+ function recoverSecret(indexes, shares) {
455
+ const threshold = shares.length;
456
+ if (threshold === 0 || indexes.length !== threshold) throw new ShamirError(ShamirErrorType.InvalidThreshold);
457
+ const shareLength = shares[0].length;
458
+ validateParameters(threshold, threshold, shareLength);
459
+ if (!shares.every((share) => share.length === shareLength)) throw new ShamirError(ShamirErrorType.SharesUnequalLength);
460
+ if (threshold === 1) return new Uint8Array(shares[0]);
461
+ else {
462
+ const indexesU8 = new Uint8Array(indexes);
463
+ const digest = interpolate(threshold, indexesU8, shareLength, shares, DIGEST_INDEX);
464
+ const secret = interpolate(threshold, indexesU8, shareLength, shares, SECRET_INDEX);
465
+ const verify = createDigest(digest.subarray(4), secret);
466
+ let valid = true;
467
+ for (let i = 0; i < 4; i++) valid = valid && digest[i] === verify[i];
468
+ (0, __bcts_crypto.memzero)(digest);
469
+ (0, __bcts_crypto.memzero)(verify);
470
+ if (!valid) throw new ShamirError(ShamirErrorType.ChecksumFailure);
471
+ return secret;
472
+ }
473
+ }
474
+
475
+ //#endregion
476
+ //#region src/index.ts
477
+ /**
478
+ * The minimum length of a secret.
479
+ */
480
+ const MIN_SECRET_LEN = 16;
481
+ /**
482
+ * The maximum length of a secret.
483
+ */
484
+ const MAX_SECRET_LEN = 32;
485
+ /**
486
+ * The maximum number of shares that can be generated from a secret.
487
+ */
488
+ const MAX_SHARE_COUNT = 16;
489
+
490
+ //#endregion
491
+ exports.MAX_SECRET_LEN = MAX_SECRET_LEN;
492
+ exports.MAX_SHARE_COUNT = MAX_SHARE_COUNT;
493
+ exports.MIN_SECRET_LEN = MIN_SECRET_LEN;
494
+ exports.ShamirError = ShamirError;
495
+ 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
+ exports.recoverSecret = recoverSecret;
504
+ exports.splitSecret = splitSecret;
505
+ exports.unbitslice = unbitslice;
506
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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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