@mysten/seal 0.4.11 → 0.4.13
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/CHANGELOG.md +15 -0
- package/dist/cjs/decrypt.js +2 -12
- package/dist/cjs/decrypt.js.map +3 -3
- package/dist/cjs/encrypt.js +2 -21
- package/dist/cjs/encrypt.js.map +3 -3
- package/dist/cjs/key-server.js +1 -1
- package/dist/cjs/key-server.js.map +2 -2
- package/dist/cjs/shamir.d.ts +79 -0
- package/dist/cjs/shamir.js +766 -0
- package/dist/cjs/shamir.js.map +7 -0
- package/dist/cjs/utils.d.ts +4 -0
- package/dist/cjs/utils.js +11 -0
- package/dist/cjs/utils.js.map +2 -2
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/decrypt.js +3 -13
- package/dist/esm/decrypt.js.map +2 -2
- package/dist/esm/encrypt.js +2 -21
- package/dist/esm/encrypt.js.map +2 -2
- package/dist/esm/key-server.js +1 -1
- package/dist/esm/key-server.js.map +2 -2
- package/dist/esm/shamir.d.ts +79 -0
- package/dist/esm/shamir.js +746 -0
- package/dist/esm/shamir.js.map +7 -0
- package/dist/esm/utils.d.ts +4 -0
- package/dist/esm/utils.js +11 -0
- package/dist/esm/utils.js.map +2 -2
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var shamir_exports = {};
|
|
20
|
+
__export(shamir_exports, {
|
|
21
|
+
GF256: () => GF256,
|
|
22
|
+
Polynomial: () => Polynomial,
|
|
23
|
+
combine: () => combine,
|
|
24
|
+
interpolate: () => interpolate,
|
|
25
|
+
split: () => split
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(shamir_exports);
|
|
28
|
+
var import_utils = require("./utils.js");
|
|
29
|
+
const GF256_SIZE = 256;
|
|
30
|
+
class GF256 {
|
|
31
|
+
constructor(value) {
|
|
32
|
+
if (value < 0 || value >= GF256_SIZE) {
|
|
33
|
+
throw new Error(`Invalid value ${value} for GF256`);
|
|
34
|
+
}
|
|
35
|
+
this.value = value;
|
|
36
|
+
}
|
|
37
|
+
log() {
|
|
38
|
+
if (this.value === 0) {
|
|
39
|
+
throw new Error("Invalid value");
|
|
40
|
+
}
|
|
41
|
+
return LOG[this.value - 1];
|
|
42
|
+
}
|
|
43
|
+
static exp(x) {
|
|
44
|
+
return new GF256(EXP[x % (GF256_SIZE - 1)]);
|
|
45
|
+
}
|
|
46
|
+
add(other) {
|
|
47
|
+
return new GF256(this.value ^ other.value);
|
|
48
|
+
}
|
|
49
|
+
sub(other) {
|
|
50
|
+
return this.add(other);
|
|
51
|
+
}
|
|
52
|
+
neg() {
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
mul(other) {
|
|
56
|
+
if (this.value === 0 || other.value === 0) {
|
|
57
|
+
return new GF256(0);
|
|
58
|
+
}
|
|
59
|
+
return GF256.exp(this.log() + other.log());
|
|
60
|
+
}
|
|
61
|
+
div(other) {
|
|
62
|
+
return this.mul(GF256.exp(GF256_SIZE - other.log() - 1));
|
|
63
|
+
}
|
|
64
|
+
equals(other) {
|
|
65
|
+
return this.value === other.value;
|
|
66
|
+
}
|
|
67
|
+
static zero() {
|
|
68
|
+
return new GF256(0);
|
|
69
|
+
}
|
|
70
|
+
static one() {
|
|
71
|
+
return new GF256(1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const EXP = [
|
|
75
|
+
1,
|
|
76
|
+
3,
|
|
77
|
+
5,
|
|
78
|
+
15,
|
|
79
|
+
17,
|
|
80
|
+
51,
|
|
81
|
+
85,
|
|
82
|
+
255,
|
|
83
|
+
26,
|
|
84
|
+
46,
|
|
85
|
+
114,
|
|
86
|
+
150,
|
|
87
|
+
161,
|
|
88
|
+
248,
|
|
89
|
+
19,
|
|
90
|
+
53,
|
|
91
|
+
95,
|
|
92
|
+
225,
|
|
93
|
+
56,
|
|
94
|
+
72,
|
|
95
|
+
216,
|
|
96
|
+
115,
|
|
97
|
+
149,
|
|
98
|
+
164,
|
|
99
|
+
247,
|
|
100
|
+
2,
|
|
101
|
+
6,
|
|
102
|
+
10,
|
|
103
|
+
30,
|
|
104
|
+
34,
|
|
105
|
+
102,
|
|
106
|
+
170,
|
|
107
|
+
229,
|
|
108
|
+
52,
|
|
109
|
+
92,
|
|
110
|
+
228,
|
|
111
|
+
55,
|
|
112
|
+
89,
|
|
113
|
+
235,
|
|
114
|
+
38,
|
|
115
|
+
106,
|
|
116
|
+
190,
|
|
117
|
+
217,
|
|
118
|
+
112,
|
|
119
|
+
144,
|
|
120
|
+
171,
|
|
121
|
+
230,
|
|
122
|
+
49,
|
|
123
|
+
83,
|
|
124
|
+
245,
|
|
125
|
+
4,
|
|
126
|
+
12,
|
|
127
|
+
20,
|
|
128
|
+
60,
|
|
129
|
+
68,
|
|
130
|
+
204,
|
|
131
|
+
79,
|
|
132
|
+
209,
|
|
133
|
+
104,
|
|
134
|
+
184,
|
|
135
|
+
211,
|
|
136
|
+
110,
|
|
137
|
+
178,
|
|
138
|
+
205,
|
|
139
|
+
76,
|
|
140
|
+
212,
|
|
141
|
+
103,
|
|
142
|
+
169,
|
|
143
|
+
224,
|
|
144
|
+
59,
|
|
145
|
+
77,
|
|
146
|
+
215,
|
|
147
|
+
98,
|
|
148
|
+
166,
|
|
149
|
+
241,
|
|
150
|
+
8,
|
|
151
|
+
24,
|
|
152
|
+
40,
|
|
153
|
+
120,
|
|
154
|
+
136,
|
|
155
|
+
131,
|
|
156
|
+
158,
|
|
157
|
+
185,
|
|
158
|
+
208,
|
|
159
|
+
107,
|
|
160
|
+
189,
|
|
161
|
+
220,
|
|
162
|
+
127,
|
|
163
|
+
129,
|
|
164
|
+
152,
|
|
165
|
+
179,
|
|
166
|
+
206,
|
|
167
|
+
73,
|
|
168
|
+
219,
|
|
169
|
+
118,
|
|
170
|
+
154,
|
|
171
|
+
181,
|
|
172
|
+
196,
|
|
173
|
+
87,
|
|
174
|
+
249,
|
|
175
|
+
16,
|
|
176
|
+
48,
|
|
177
|
+
80,
|
|
178
|
+
240,
|
|
179
|
+
11,
|
|
180
|
+
29,
|
|
181
|
+
39,
|
|
182
|
+
105,
|
|
183
|
+
187,
|
|
184
|
+
214,
|
|
185
|
+
97,
|
|
186
|
+
163,
|
|
187
|
+
254,
|
|
188
|
+
25,
|
|
189
|
+
43,
|
|
190
|
+
125,
|
|
191
|
+
135,
|
|
192
|
+
146,
|
|
193
|
+
173,
|
|
194
|
+
236,
|
|
195
|
+
47,
|
|
196
|
+
113,
|
|
197
|
+
147,
|
|
198
|
+
174,
|
|
199
|
+
233,
|
|
200
|
+
32,
|
|
201
|
+
96,
|
|
202
|
+
160,
|
|
203
|
+
251,
|
|
204
|
+
22,
|
|
205
|
+
58,
|
|
206
|
+
78,
|
|
207
|
+
210,
|
|
208
|
+
109,
|
|
209
|
+
183,
|
|
210
|
+
194,
|
|
211
|
+
93,
|
|
212
|
+
231,
|
|
213
|
+
50,
|
|
214
|
+
86,
|
|
215
|
+
250,
|
|
216
|
+
21,
|
|
217
|
+
63,
|
|
218
|
+
65,
|
|
219
|
+
195,
|
|
220
|
+
94,
|
|
221
|
+
226,
|
|
222
|
+
61,
|
|
223
|
+
71,
|
|
224
|
+
201,
|
|
225
|
+
64,
|
|
226
|
+
192,
|
|
227
|
+
91,
|
|
228
|
+
237,
|
|
229
|
+
44,
|
|
230
|
+
116,
|
|
231
|
+
156,
|
|
232
|
+
191,
|
|
233
|
+
218,
|
|
234
|
+
117,
|
|
235
|
+
159,
|
|
236
|
+
186,
|
|
237
|
+
213,
|
|
238
|
+
100,
|
|
239
|
+
172,
|
|
240
|
+
239,
|
|
241
|
+
42,
|
|
242
|
+
126,
|
|
243
|
+
130,
|
|
244
|
+
157,
|
|
245
|
+
188,
|
|
246
|
+
223,
|
|
247
|
+
122,
|
|
248
|
+
142,
|
|
249
|
+
137,
|
|
250
|
+
128,
|
|
251
|
+
155,
|
|
252
|
+
182,
|
|
253
|
+
193,
|
|
254
|
+
88,
|
|
255
|
+
232,
|
|
256
|
+
35,
|
|
257
|
+
101,
|
|
258
|
+
175,
|
|
259
|
+
234,
|
|
260
|
+
37,
|
|
261
|
+
111,
|
|
262
|
+
177,
|
|
263
|
+
200,
|
|
264
|
+
67,
|
|
265
|
+
197,
|
|
266
|
+
84,
|
|
267
|
+
252,
|
|
268
|
+
31,
|
|
269
|
+
33,
|
|
270
|
+
99,
|
|
271
|
+
165,
|
|
272
|
+
244,
|
|
273
|
+
7,
|
|
274
|
+
9,
|
|
275
|
+
27,
|
|
276
|
+
45,
|
|
277
|
+
119,
|
|
278
|
+
153,
|
|
279
|
+
176,
|
|
280
|
+
203,
|
|
281
|
+
70,
|
|
282
|
+
202,
|
|
283
|
+
69,
|
|
284
|
+
207,
|
|
285
|
+
74,
|
|
286
|
+
222,
|
|
287
|
+
121,
|
|
288
|
+
139,
|
|
289
|
+
134,
|
|
290
|
+
145,
|
|
291
|
+
168,
|
|
292
|
+
227,
|
|
293
|
+
62,
|
|
294
|
+
66,
|
|
295
|
+
198,
|
|
296
|
+
81,
|
|
297
|
+
243,
|
|
298
|
+
14,
|
|
299
|
+
18,
|
|
300
|
+
54,
|
|
301
|
+
90,
|
|
302
|
+
238,
|
|
303
|
+
41,
|
|
304
|
+
123,
|
|
305
|
+
141,
|
|
306
|
+
140,
|
|
307
|
+
143,
|
|
308
|
+
138,
|
|
309
|
+
133,
|
|
310
|
+
148,
|
|
311
|
+
167,
|
|
312
|
+
242,
|
|
313
|
+
13,
|
|
314
|
+
23,
|
|
315
|
+
57,
|
|
316
|
+
75,
|
|
317
|
+
221,
|
|
318
|
+
124,
|
|
319
|
+
132,
|
|
320
|
+
151,
|
|
321
|
+
162,
|
|
322
|
+
253,
|
|
323
|
+
28,
|
|
324
|
+
36,
|
|
325
|
+
108,
|
|
326
|
+
180,
|
|
327
|
+
199,
|
|
328
|
+
82,
|
|
329
|
+
246
|
|
330
|
+
];
|
|
331
|
+
const LOG = [
|
|
332
|
+
0,
|
|
333
|
+
25,
|
|
334
|
+
1,
|
|
335
|
+
50,
|
|
336
|
+
2,
|
|
337
|
+
26,
|
|
338
|
+
198,
|
|
339
|
+
75,
|
|
340
|
+
199,
|
|
341
|
+
27,
|
|
342
|
+
104,
|
|
343
|
+
51,
|
|
344
|
+
238,
|
|
345
|
+
223,
|
|
346
|
+
3,
|
|
347
|
+
100,
|
|
348
|
+
4,
|
|
349
|
+
224,
|
|
350
|
+
14,
|
|
351
|
+
52,
|
|
352
|
+
141,
|
|
353
|
+
129,
|
|
354
|
+
239,
|
|
355
|
+
76,
|
|
356
|
+
113,
|
|
357
|
+
8,
|
|
358
|
+
200,
|
|
359
|
+
248,
|
|
360
|
+
105,
|
|
361
|
+
28,
|
|
362
|
+
193,
|
|
363
|
+
125,
|
|
364
|
+
194,
|
|
365
|
+
29,
|
|
366
|
+
181,
|
|
367
|
+
249,
|
|
368
|
+
185,
|
|
369
|
+
39,
|
|
370
|
+
106,
|
|
371
|
+
77,
|
|
372
|
+
228,
|
|
373
|
+
166,
|
|
374
|
+
114,
|
|
375
|
+
154,
|
|
376
|
+
201,
|
|
377
|
+
9,
|
|
378
|
+
120,
|
|
379
|
+
101,
|
|
380
|
+
47,
|
|
381
|
+
138,
|
|
382
|
+
5,
|
|
383
|
+
33,
|
|
384
|
+
15,
|
|
385
|
+
225,
|
|
386
|
+
36,
|
|
387
|
+
18,
|
|
388
|
+
240,
|
|
389
|
+
130,
|
|
390
|
+
69,
|
|
391
|
+
53,
|
|
392
|
+
147,
|
|
393
|
+
218,
|
|
394
|
+
142,
|
|
395
|
+
150,
|
|
396
|
+
143,
|
|
397
|
+
219,
|
|
398
|
+
189,
|
|
399
|
+
54,
|
|
400
|
+
208,
|
|
401
|
+
206,
|
|
402
|
+
148,
|
|
403
|
+
19,
|
|
404
|
+
92,
|
|
405
|
+
210,
|
|
406
|
+
241,
|
|
407
|
+
64,
|
|
408
|
+
70,
|
|
409
|
+
131,
|
|
410
|
+
56,
|
|
411
|
+
102,
|
|
412
|
+
221,
|
|
413
|
+
253,
|
|
414
|
+
48,
|
|
415
|
+
191,
|
|
416
|
+
6,
|
|
417
|
+
139,
|
|
418
|
+
98,
|
|
419
|
+
179,
|
|
420
|
+
37,
|
|
421
|
+
226,
|
|
422
|
+
152,
|
|
423
|
+
34,
|
|
424
|
+
136,
|
|
425
|
+
145,
|
|
426
|
+
16,
|
|
427
|
+
126,
|
|
428
|
+
110,
|
|
429
|
+
72,
|
|
430
|
+
195,
|
|
431
|
+
163,
|
|
432
|
+
182,
|
|
433
|
+
30,
|
|
434
|
+
66,
|
|
435
|
+
58,
|
|
436
|
+
107,
|
|
437
|
+
40,
|
|
438
|
+
84,
|
|
439
|
+
250,
|
|
440
|
+
133,
|
|
441
|
+
61,
|
|
442
|
+
186,
|
|
443
|
+
43,
|
|
444
|
+
121,
|
|
445
|
+
10,
|
|
446
|
+
21,
|
|
447
|
+
155,
|
|
448
|
+
159,
|
|
449
|
+
94,
|
|
450
|
+
202,
|
|
451
|
+
78,
|
|
452
|
+
212,
|
|
453
|
+
172,
|
|
454
|
+
229,
|
|
455
|
+
243,
|
|
456
|
+
115,
|
|
457
|
+
167,
|
|
458
|
+
87,
|
|
459
|
+
175,
|
|
460
|
+
88,
|
|
461
|
+
168,
|
|
462
|
+
80,
|
|
463
|
+
244,
|
|
464
|
+
234,
|
|
465
|
+
214,
|
|
466
|
+
116,
|
|
467
|
+
79,
|
|
468
|
+
174,
|
|
469
|
+
233,
|
|
470
|
+
213,
|
|
471
|
+
231,
|
|
472
|
+
230,
|
|
473
|
+
173,
|
|
474
|
+
232,
|
|
475
|
+
44,
|
|
476
|
+
215,
|
|
477
|
+
117,
|
|
478
|
+
122,
|
|
479
|
+
235,
|
|
480
|
+
22,
|
|
481
|
+
11,
|
|
482
|
+
245,
|
|
483
|
+
89,
|
|
484
|
+
203,
|
|
485
|
+
95,
|
|
486
|
+
176,
|
|
487
|
+
156,
|
|
488
|
+
169,
|
|
489
|
+
81,
|
|
490
|
+
160,
|
|
491
|
+
127,
|
|
492
|
+
12,
|
|
493
|
+
246,
|
|
494
|
+
111,
|
|
495
|
+
23,
|
|
496
|
+
196,
|
|
497
|
+
73,
|
|
498
|
+
236,
|
|
499
|
+
216,
|
|
500
|
+
67,
|
|
501
|
+
31,
|
|
502
|
+
45,
|
|
503
|
+
164,
|
|
504
|
+
118,
|
|
505
|
+
123,
|
|
506
|
+
183,
|
|
507
|
+
204,
|
|
508
|
+
187,
|
|
509
|
+
62,
|
|
510
|
+
90,
|
|
511
|
+
251,
|
|
512
|
+
96,
|
|
513
|
+
177,
|
|
514
|
+
134,
|
|
515
|
+
59,
|
|
516
|
+
82,
|
|
517
|
+
161,
|
|
518
|
+
108,
|
|
519
|
+
170,
|
|
520
|
+
85,
|
|
521
|
+
41,
|
|
522
|
+
157,
|
|
523
|
+
151,
|
|
524
|
+
178,
|
|
525
|
+
135,
|
|
526
|
+
144,
|
|
527
|
+
97,
|
|
528
|
+
190,
|
|
529
|
+
220,
|
|
530
|
+
252,
|
|
531
|
+
188,
|
|
532
|
+
149,
|
|
533
|
+
207,
|
|
534
|
+
205,
|
|
535
|
+
55,
|
|
536
|
+
63,
|
|
537
|
+
91,
|
|
538
|
+
209,
|
|
539
|
+
83,
|
|
540
|
+
57,
|
|
541
|
+
132,
|
|
542
|
+
60,
|
|
543
|
+
65,
|
|
544
|
+
162,
|
|
545
|
+
109,
|
|
546
|
+
71,
|
|
547
|
+
20,
|
|
548
|
+
42,
|
|
549
|
+
158,
|
|
550
|
+
93,
|
|
551
|
+
86,
|
|
552
|
+
242,
|
|
553
|
+
211,
|
|
554
|
+
171,
|
|
555
|
+
68,
|
|
556
|
+
17,
|
|
557
|
+
146,
|
|
558
|
+
217,
|
|
559
|
+
35,
|
|
560
|
+
32,
|
|
561
|
+
46,
|
|
562
|
+
137,
|
|
563
|
+
180,
|
|
564
|
+
124,
|
|
565
|
+
184,
|
|
566
|
+
38,
|
|
567
|
+
119,
|
|
568
|
+
153,
|
|
569
|
+
227,
|
|
570
|
+
165,
|
|
571
|
+
103,
|
|
572
|
+
74,
|
|
573
|
+
237,
|
|
574
|
+
222,
|
|
575
|
+
197,
|
|
576
|
+
49,
|
|
577
|
+
254,
|
|
578
|
+
24,
|
|
579
|
+
13,
|
|
580
|
+
99,
|
|
581
|
+
140,
|
|
582
|
+
128,
|
|
583
|
+
192,
|
|
584
|
+
247,
|
|
585
|
+
112,
|
|
586
|
+
7
|
|
587
|
+
];
|
|
588
|
+
class Polynomial {
|
|
589
|
+
/**
|
|
590
|
+
* Construct a new Polynomial over [GF256] from the given coefficients.
|
|
591
|
+
* The first coefficient is the constant term.
|
|
592
|
+
*/
|
|
593
|
+
constructor(coefficients) {
|
|
594
|
+
this.coefficients = coefficients.slice();
|
|
595
|
+
while (this.coefficients.length > 0 && this.coefficients[this.coefficients.length - 1].value === 0) {
|
|
596
|
+
this.coefficients.pop();
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
static fromBytes(bytes) {
|
|
600
|
+
return new Polynomial(Array.from(bytes, (b) => new GF256(b)));
|
|
601
|
+
}
|
|
602
|
+
degree() {
|
|
603
|
+
if (this.coefficients.length === 0) {
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
return this.coefficients.length - 1;
|
|
607
|
+
}
|
|
608
|
+
getCoefficient(index) {
|
|
609
|
+
if (index >= this.coefficients.length) {
|
|
610
|
+
return GF256.zero();
|
|
611
|
+
}
|
|
612
|
+
return this.coefficients[index];
|
|
613
|
+
}
|
|
614
|
+
add(other) {
|
|
615
|
+
const degree = Math.max(this.degree(), other.degree());
|
|
616
|
+
return new Polynomial(
|
|
617
|
+
Array.from(
|
|
618
|
+
{ length: degree + 1 },
|
|
619
|
+
(_, i) => this.getCoefficient(i).add(other.getCoefficient(i))
|
|
620
|
+
)
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
mul(other) {
|
|
624
|
+
const degree = this.degree() + other.degree();
|
|
625
|
+
return new Polynomial(
|
|
626
|
+
Array.from({ length: degree + 1 }, (_, i) => {
|
|
627
|
+
let sum = GF256.zero();
|
|
628
|
+
for (let j = 0; j <= i; j++) {
|
|
629
|
+
if (j <= this.degree() && i - j <= other.degree()) {
|
|
630
|
+
sum = sum.add(this.coefficients[j].mul(other.coefficients[i - j]));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return sum;
|
|
634
|
+
})
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
/** The polynomial s * this. */
|
|
638
|
+
scale(s) {
|
|
639
|
+
return new Polynomial(this.coefficients.map((c) => c.mul(s)));
|
|
640
|
+
}
|
|
641
|
+
div(s) {
|
|
642
|
+
return this.scale(new GF256(1).div(s));
|
|
643
|
+
}
|
|
644
|
+
/** The polynomial x + c. */
|
|
645
|
+
static monic_linear(c) {
|
|
646
|
+
return new Polynomial([c, GF256.one()]);
|
|
647
|
+
}
|
|
648
|
+
static zero() {
|
|
649
|
+
return new Polynomial([]);
|
|
650
|
+
}
|
|
651
|
+
static one() {
|
|
652
|
+
return new Polynomial([GF256.one()]);
|
|
653
|
+
}
|
|
654
|
+
/** Given a set of coordinates, interpolate a polynomial. */
|
|
655
|
+
static interpolate(coordinates) {
|
|
656
|
+
if (coordinates.length < 1) {
|
|
657
|
+
throw new Error("At least one coefficient is required");
|
|
658
|
+
}
|
|
659
|
+
if ((0, import_utils.hasDuplicates)(coordinates.map(({ x }) => x.value))) {
|
|
660
|
+
throw new Error("Coefficients must have unique x values");
|
|
661
|
+
}
|
|
662
|
+
return coordinates.reduce(
|
|
663
|
+
(sum, { x: x_j, y: y_j }, j) => sum.add(
|
|
664
|
+
coordinates.filter((_, i) => i !== j).reduce(
|
|
665
|
+
(product, { x: x_i }) => product.mul(Polynomial.monic_linear(x_i.neg()).div(x_j.sub(x_i))),
|
|
666
|
+
Polynomial.one()
|
|
667
|
+
).scale(y_j)
|
|
668
|
+
),
|
|
669
|
+
Polynomial.zero()
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
/** Given a set of coordinates, interpolate a polynomial and evaluate it at x = 0. */
|
|
673
|
+
static combine(coordinates) {
|
|
674
|
+
if (coordinates.length < 1) {
|
|
675
|
+
throw new Error("At least one coefficient is required");
|
|
676
|
+
}
|
|
677
|
+
if ((0, import_utils.hasDuplicates)(coordinates.map(({ x }) => x.value))) {
|
|
678
|
+
throw new Error("Coefficients must have unique x values");
|
|
679
|
+
}
|
|
680
|
+
const quotient = coordinates.reduce((sum, { x: x_j, y: y_j }, j) => {
|
|
681
|
+
const denominator = x_j.mul(
|
|
682
|
+
coordinates.filter((_, i) => i !== j).reduce((product, { x: x_i }) => product.mul(x_i.sub(x_j)), GF256.one())
|
|
683
|
+
);
|
|
684
|
+
return sum.add(y_j.div(denominator));
|
|
685
|
+
}, GF256.zero());
|
|
686
|
+
const xProduct = coordinates.reduce((product, { x }) => product.mul(x), GF256.one());
|
|
687
|
+
return xProduct.mul(quotient);
|
|
688
|
+
}
|
|
689
|
+
/** Evaluate the polynomial at x. */
|
|
690
|
+
evaluate(x) {
|
|
691
|
+
return this.coefficients.toReversed().reduce((sum, coefficient) => sum.mul(x).add(coefficient), GF256.zero());
|
|
692
|
+
}
|
|
693
|
+
equals(other) {
|
|
694
|
+
if (this.degree() !== other.degree()) {
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
return this.coefficients.every((c, i) => c.equals(other.coefficients[i]));
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
function toInternalShare(share) {
|
|
701
|
+
return {
|
|
702
|
+
index: new GF256(share.index),
|
|
703
|
+
share: Array.from(share.share, (byte) => new GF256(byte))
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
function toShare(internalShare) {
|
|
707
|
+
return {
|
|
708
|
+
index: internalShare.index.value,
|
|
709
|
+
share: new Uint8Array(internalShare.share.map((byte) => byte.value))
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
function samplePolynomial(constant, degree) {
|
|
713
|
+
const randomCoefficients = new Uint8Array(degree);
|
|
714
|
+
crypto.getRandomValues(randomCoefficients);
|
|
715
|
+
return Polynomial.fromBytes(new Uint8Array([constant.value, ...randomCoefficients]));
|
|
716
|
+
}
|
|
717
|
+
function split(secret, threshold, total) {
|
|
718
|
+
if (threshold > total || threshold < 1 || total > GF256_SIZE) {
|
|
719
|
+
throw new Error(`Invalid threshold ${threshold} or total ${total}`);
|
|
720
|
+
}
|
|
721
|
+
const polynomials = Array.from(secret, (s) => samplePolynomial(new GF256(s), threshold - 1));
|
|
722
|
+
return Array.from({ length: total }, (_, i) => {
|
|
723
|
+
const index = new GF256(i + 1);
|
|
724
|
+
const share = polynomials.map((p) => p.evaluate(index));
|
|
725
|
+
return toShare({ index, share });
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
function validateShares(shares) {
|
|
729
|
+
if (shares.length < 1) {
|
|
730
|
+
throw new Error("At least one share is required");
|
|
731
|
+
}
|
|
732
|
+
if (!(0, import_utils.allEqual)(shares.map(({ share }) => share.length))) {
|
|
733
|
+
throw new Error("All shares must have the same length");
|
|
734
|
+
}
|
|
735
|
+
if ((0, import_utils.hasDuplicates)(shares.map(({ index }) => index))) {
|
|
736
|
+
throw new Error("Shares must have unique indices");
|
|
737
|
+
}
|
|
738
|
+
const internalShares = shares.map(toInternalShare);
|
|
739
|
+
const length = internalShares[0].share.length;
|
|
740
|
+
return { internalShares, length };
|
|
741
|
+
}
|
|
742
|
+
function combine(shares) {
|
|
743
|
+
const { internalShares, length } = validateShares(shares);
|
|
744
|
+
return new Uint8Array(
|
|
745
|
+
Array.from(
|
|
746
|
+
{ length },
|
|
747
|
+
(_, i) => Polynomial.combine(
|
|
748
|
+
internalShares.map(({ index, share }) => ({
|
|
749
|
+
x: index,
|
|
750
|
+
y: share[i]
|
|
751
|
+
}))
|
|
752
|
+
).value
|
|
753
|
+
)
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
function interpolate(shares) {
|
|
757
|
+
const { internalShares, length } = validateShares(shares);
|
|
758
|
+
const polynomials = Array.from(
|
|
759
|
+
{ length },
|
|
760
|
+
(_, i) => Polynomial.interpolate(internalShares.map(({ index, share }) => ({ x: index, y: share[i] })))
|
|
761
|
+
);
|
|
762
|
+
return (x) => {
|
|
763
|
+
return new Uint8Array(polynomials.map((p) => p.evaluate(new GF256(x)).value));
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
//# sourceMappingURL=shamir.js.map
|