@bcts/shamir 1.0.0-alpha.8 → 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/dist/index.d.mts
CHANGED
|
@@ -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.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/error.ts","../src/shamir.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.d.mts","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"}
|
package/dist/index.iife.js
CHANGED
|
@@ -1,20 +1,40 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var bctsShamir = (function(exports, _bcts_crypto) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region src/error.ts
|
|
4
|
+
/**
|
|
5
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
6
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
6
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.
|
|
7
24
|
*/
|
|
8
|
-
let ShamirErrorType = /* @__PURE__ */ function(ShamirErrorType
|
|
9
|
-
ShamirErrorType
|
|
10
|
-
ShamirErrorType
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
ShamirErrorType
|
|
15
|
-
ShamirErrorType
|
|
16
|
-
ShamirErrorType
|
|
17
|
-
|
|
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;
|
|
18
38
|
}({});
|
|
19
39
|
/**
|
|
20
40
|
* Error class for Shamir secret sharing operations.
|
|
@@ -28,21 +48,33 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
28
48
|
}
|
|
29
49
|
static defaultMessage(type) {
|
|
30
50
|
switch (type) {
|
|
31
|
-
case
|
|
32
|
-
case
|
|
33
|
-
case
|
|
34
|
-
case
|
|
35
|
-
case
|
|
36
|
-
case
|
|
37
|
-
case
|
|
38
|
-
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";
|
|
39
59
|
}
|
|
40
60
|
}
|
|
41
61
|
};
|
|
42
|
-
|
|
43
|
-
//#
|
|
44
|
-
|
|
45
|
-
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/hazmat.ts
|
|
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
|
+
/**
|
|
46
78
|
* Convert an array of bytes into a bitsliced representation.
|
|
47
79
|
* Takes the first 32 bytes from x and produces 8 u32 values.
|
|
48
80
|
*
|
|
@@ -50,9 +82,9 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
50
82
|
* @param x - Input array of at least 32 bytes
|
|
51
83
|
*/
|
|
52
84
|
function bitslice(r, x) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
(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);
|
|
56
88
|
for (let arrIdx = 0; arrIdx < 32; arrIdx++) {
|
|
57
89
|
const cur = x[arrIdx];
|
|
58
90
|
for (let bitIdx = 0; bitIdx < 8; bitIdx++) r[bitIdx] |= (cur & 1 << bitIdx) >>> bitIdx << arrIdx;
|
|
@@ -65,9 +97,9 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
65
97
|
* @param x - Input array of 8 u32 values (bitsliced representation)
|
|
66
98
|
*/
|
|
67
99
|
function unbitslice(r, x) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
(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));
|
|
71
103
|
for (let bitIdx = 0; bitIdx < 8; bitIdx++) {
|
|
72
104
|
const cur = x[bitIdx];
|
|
73
105
|
for (let arrIdx = 0; arrIdx < 32; arrIdx++) r[arrIdx] |= (cur & 1 << arrIdx) >>> arrIdx << bitIdx;
|
|
@@ -80,7 +112,7 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
80
112
|
* @param x - Byte value to set in all positions
|
|
81
113
|
*/
|
|
82
114
|
function bitsliceSetall(r, x) {
|
|
83
|
-
|
|
115
|
+
assertContract(r.length === 8, "bitsliceSetall: output must have 8 elements");
|
|
84
116
|
for (let idx = 0; idx < 8; idx++) r[idx] = (x >>> idx & 1) === 1 ? 4294967295 : 0;
|
|
85
117
|
}
|
|
86
118
|
/**
|
|
@@ -91,7 +123,7 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
91
123
|
* @param x - Second operand
|
|
92
124
|
*/
|
|
93
125
|
function gf256Add(r, x) {
|
|
94
|
-
|
|
126
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Add: arrays must have 8 elements");
|
|
95
127
|
for (let i = 0; i < 8; i++) r[i] ^= x[i];
|
|
96
128
|
}
|
|
97
129
|
/**
|
|
@@ -105,7 +137,7 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
105
137
|
* @param b - Second operand (must NOT overlap with r)
|
|
106
138
|
*/
|
|
107
139
|
function gf256Mul(r, a, b) {
|
|
108
|
-
|
|
140
|
+
assertContract(r.length === 8 && a.length === 8 && b.length === 8, "gf256Mul: arrays must have 8 elements");
|
|
109
141
|
const a2 = new Uint32Array(a);
|
|
110
142
|
r[0] = a2[0] & b[0];
|
|
111
143
|
r[1] = a2[1] & b[0];
|
|
@@ -201,7 +233,7 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
201
233
|
* @param x - Value to square
|
|
202
234
|
*/
|
|
203
235
|
function gf256Square(r, x) {
|
|
204
|
-
|
|
236
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Square: arrays must have 8 elements");
|
|
205
237
|
const r14 = x[7];
|
|
206
238
|
const r12 = x[6];
|
|
207
239
|
let r10 = x[5];
|
|
@@ -237,7 +269,7 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
237
269
|
* @param x - Value to invert (will be modified)
|
|
238
270
|
*/
|
|
239
271
|
function gf256Inv(r, x) {
|
|
240
|
-
|
|
272
|
+
assertContract(r.length === 8 && x.length === 8, "gf256Inv: arrays must have 8 elements");
|
|
241
273
|
const y = new Uint32Array(8);
|
|
242
274
|
const z = new Uint32Array(8);
|
|
243
275
|
gf256Square(y, x);
|
|
@@ -252,10 +284,14 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
252
284
|
gf256Mul(r, new Uint32Array(r), z);
|
|
253
285
|
gf256Mul(r, new Uint32Array(r), y);
|
|
254
286
|
}
|
|
255
|
-
|
|
256
|
-
//#
|
|
257
|
-
|
|
258
|
-
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/interpolate.ts
|
|
289
|
+
/**
|
|
290
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
291
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
292
|
+
*
|
|
293
|
+
*/
|
|
294
|
+
/**
|
|
259
295
|
* Calculate the lagrange basis coefficients for the lagrange polynomial
|
|
260
296
|
* defined by the x coordinates xc at the value x.
|
|
261
297
|
*
|
|
@@ -317,8 +353,8 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
317
353
|
*/
|
|
318
354
|
function interpolate(n, xi, yl, yij, x) {
|
|
319
355
|
const y = [];
|
|
320
|
-
for (let i = 0; i < n; i++) y.push(new Uint8Array(
|
|
321
|
-
const values = new Uint8Array(
|
|
356
|
+
for (let i = 0; i < n; i++) y.push(new Uint8Array(32));
|
|
357
|
+
const values = new Uint8Array(32);
|
|
322
358
|
for (let i = 0; i < n; i++) y[i].set(yij[i].subarray(0, yl), 0);
|
|
323
359
|
const lagrange = new Uint8Array(n);
|
|
324
360
|
const ySlice = new Uint32Array(8);
|
|
@@ -335,28 +371,32 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
335
371
|
unbitslice(values, resultSlice);
|
|
336
372
|
const result = new Uint8Array(yl);
|
|
337
373
|
result.set(values.subarray(0, yl), 0);
|
|
338
|
-
(0,
|
|
339
|
-
(0,
|
|
340
|
-
(0,
|
|
341
|
-
(0,
|
|
342
|
-
(0,
|
|
343
|
-
(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);
|
|
344
380
|
return result;
|
|
345
381
|
}
|
|
346
|
-
|
|
347
|
-
//#
|
|
348
|
-
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/shamir.ts
|
|
384
|
+
/**
|
|
385
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
386
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
387
|
+
*
|
|
388
|
+
*/
|
|
349
389
|
const SECRET_INDEX = 255;
|
|
350
390
|
const DIGEST_INDEX = 254;
|
|
351
391
|
function createDigest(randomData, sharedSecret) {
|
|
352
|
-
return (0,
|
|
392
|
+
return (0, _bcts_crypto.hmacSha256)(randomData, sharedSecret);
|
|
353
393
|
}
|
|
354
394
|
function validateParameters(threshold, shareCount, secretLength) {
|
|
355
|
-
if (shareCount >
|
|
356
|
-
else if (threshold < 1 || threshold > shareCount) throw new ShamirError(
|
|
357
|
-
else if (secretLength >
|
|
358
|
-
else if (secretLength <
|
|
359
|
-
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");
|
|
360
400
|
}
|
|
361
401
|
/**
|
|
362
402
|
* Splits a secret into shares using the Shamir secret sharing algorithm.
|
|
@@ -421,9 +461,9 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
421
461
|
const v = interpolate(n, x, secret.length, y, index);
|
|
422
462
|
result[index].set(v);
|
|
423
463
|
}
|
|
424
|
-
(0,
|
|
425
|
-
(0,
|
|
426
|
-
(0,
|
|
464
|
+
(0, _bcts_crypto.memzero)(digest);
|
|
465
|
+
(0, _bcts_crypto.memzero)(x);
|
|
466
|
+
(0, _bcts_crypto.memzeroVecVecU8)(y);
|
|
427
467
|
return result;
|
|
428
468
|
}
|
|
429
469
|
}
|
|
@@ -454,10 +494,10 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
454
494
|
*/
|
|
455
495
|
function recoverSecret(indexes, shares) {
|
|
456
496
|
const threshold = shares.length;
|
|
457
|
-
if (threshold === 0 || indexes.length !== threshold) throw new ShamirError(
|
|
497
|
+
if (threshold === 0 || indexes.length !== threshold) throw new ShamirError("InvalidThreshold");
|
|
458
498
|
const shareLength = shares[0].length;
|
|
459
499
|
validateParameters(threshold, threshold, shareLength);
|
|
460
|
-
if (!shares.every((share) => share.length === shareLength)) throw new ShamirError(
|
|
500
|
+
if (!shares.every((share) => share.length === shareLength)) throw new ShamirError("SharesUnequalLength");
|
|
461
501
|
if (threshold === 1) return new Uint8Array(shares[0]);
|
|
462
502
|
else {
|
|
463
503
|
const indexesU8 = new Uint8Array(indexes);
|
|
@@ -466,16 +506,20 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
466
506
|
const verify = createDigest(digest.subarray(4), secret);
|
|
467
507
|
let valid = true;
|
|
468
508
|
for (let i = 0; i < 4; i++) valid = valid && digest[i] === verify[i];
|
|
469
|
-
(0,
|
|
470
|
-
(0,
|
|
471
|
-
if (!valid) throw new ShamirError(
|
|
509
|
+
(0, _bcts_crypto.memzero)(digest);
|
|
510
|
+
(0, _bcts_crypto.memzero)(verify);
|
|
511
|
+
if (!valid) throw new ShamirError("ChecksumFailure");
|
|
472
512
|
return secret;
|
|
473
513
|
}
|
|
474
514
|
}
|
|
475
|
-
|
|
476
|
-
//#
|
|
477
|
-
|
|
478
|
-
|
|
515
|
+
//#endregion
|
|
516
|
+
//#region src/index.ts
|
|
517
|
+
/**
|
|
518
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
519
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
520
|
+
*
|
|
521
|
+
*/
|
|
522
|
+
/**
|
|
479
523
|
* The minimum length of a secret.
|
|
480
524
|
*/
|
|
481
525
|
const MIN_SECRET_LEN = 16;
|
|
@@ -487,23 +531,15 @@ var BCShamir = (function(exports, __bcts_crypto) {
|
|
|
487
531
|
* The maximum number of shares that can be generated from a secret.
|
|
488
532
|
*/
|
|
489
533
|
const MAX_SHARE_COUNT = 16;
|
|
534
|
+
//#endregion
|
|
535
|
+
exports.MAX_SECRET_LEN = MAX_SECRET_LEN;
|
|
536
|
+
exports.MAX_SHARE_COUNT = MAX_SHARE_COUNT;
|
|
537
|
+
exports.MIN_SECRET_LEN = MIN_SECRET_LEN;
|
|
538
|
+
exports.ShamirError = ShamirError;
|
|
539
|
+
exports.ShamirErrorType = ShamirErrorType;
|
|
540
|
+
exports.recoverSecret = recoverSecret;
|
|
541
|
+
exports.splitSecret = splitSecret;
|
|
542
|
+
return exports;
|
|
543
|
+
})({}, bctsCrypto);
|
|
490
544
|
|
|
491
|
-
//#endregion
|
|
492
|
-
exports.MAX_SECRET_LEN = MAX_SECRET_LEN;
|
|
493
|
-
exports.MAX_SHARE_COUNT = MAX_SHARE_COUNT;
|
|
494
|
-
exports.MIN_SECRET_LEN = MIN_SECRET_LEN;
|
|
495
|
-
exports.ShamirError = ShamirError;
|
|
496
|
-
exports.ShamirErrorType = ShamirErrorType;
|
|
497
|
-
exports.bitslice = bitslice;
|
|
498
|
-
exports.bitsliceSetall = bitsliceSetall;
|
|
499
|
-
exports.gf256Add = gf256Add;
|
|
500
|
-
exports.gf256Inv = gf256Inv;
|
|
501
|
-
exports.gf256Mul = gf256Mul;
|
|
502
|
-
exports.gf256Square = gf256Square;
|
|
503
|
-
exports.interpolate = interpolate;
|
|
504
|
-
exports.recoverSecret = recoverSecret;
|
|
505
|
-
exports.splitSecret = splitSecret;
|
|
506
|
-
exports.unbitslice = unbitslice;
|
|
507
|
-
return exports;
|
|
508
|
-
})({}, BCCrypto);
|
|
509
545
|
//# sourceMappingURL=index.iife.js.map
|