@bcts/sskr 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 +131 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -25
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +46 -25
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +163 -118
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +118 -72
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -17
- package/src/encoding.ts +19 -4
- package/src/error.ts +15 -1
- package/src/index.ts +6 -3
- package/src/secret.ts +18 -0
- package/src/share.ts +6 -0
- package/src/spec.ts +39 -5
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,27 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
let
|
|
3
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _bcts_shamir = require("@bcts/shamir");
|
|
3
|
+
let _bcts_rand = require("@bcts/rand");
|
|
4
4
|
//#region src/error.ts
|
|
5
5
|
/**
|
|
6
6
|
* Error types for SSKR operations.
|
|
7
7
|
*/
|
|
8
|
-
let SSKRErrorType = /* @__PURE__ */ function(SSKRErrorType
|
|
9
|
-
SSKRErrorType
|
|
10
|
-
SSKRErrorType
|
|
11
|
-
SSKRErrorType
|
|
12
|
-
SSKRErrorType
|
|
13
|
-
SSKRErrorType
|
|
14
|
-
SSKRErrorType
|
|
15
|
-
SSKRErrorType
|
|
16
|
-
SSKRErrorType
|
|
17
|
-
SSKRErrorType
|
|
18
|
-
SSKRErrorType
|
|
19
|
-
SSKRErrorType
|
|
20
|
-
SSKRErrorType
|
|
21
|
-
SSKRErrorType
|
|
22
|
-
SSKRErrorType
|
|
23
|
-
SSKRErrorType
|
|
24
|
-
return SSKRErrorType
|
|
8
|
+
let SSKRErrorType = /* @__PURE__ */ function(SSKRErrorType) {
|
|
9
|
+
SSKRErrorType["DuplicateMemberIndex"] = "DuplicateMemberIndex";
|
|
10
|
+
SSKRErrorType["GroupSpecInvalid"] = "GroupSpecInvalid";
|
|
11
|
+
SSKRErrorType["GroupCountInvalid"] = "GroupCountInvalid";
|
|
12
|
+
SSKRErrorType["GroupThresholdInvalid"] = "GroupThresholdInvalid";
|
|
13
|
+
SSKRErrorType["MemberCountInvalid"] = "MemberCountInvalid";
|
|
14
|
+
SSKRErrorType["MemberThresholdInvalid"] = "MemberThresholdInvalid";
|
|
15
|
+
SSKRErrorType["NotEnoughGroups"] = "NotEnoughGroups";
|
|
16
|
+
SSKRErrorType["SecretLengthNotEven"] = "SecretLengthNotEven";
|
|
17
|
+
SSKRErrorType["SecretTooLong"] = "SecretTooLong";
|
|
18
|
+
SSKRErrorType["SecretTooShort"] = "SecretTooShort";
|
|
19
|
+
SSKRErrorType["ShareLengthInvalid"] = "ShareLengthInvalid";
|
|
20
|
+
SSKRErrorType["ShareReservedBitsInvalid"] = "ShareReservedBitsInvalid";
|
|
21
|
+
SSKRErrorType["SharesEmpty"] = "SharesEmpty";
|
|
22
|
+
SSKRErrorType["ShareSetInvalid"] = "ShareSetInvalid";
|
|
23
|
+
SSKRErrorType["ShamirError"] = "ShamirError";
|
|
24
|
+
return SSKRErrorType;
|
|
25
25
|
}({});
|
|
26
26
|
/**
|
|
27
27
|
* Error class for SSKR operations.
|
|
@@ -37,31 +37,35 @@ var SSKRError = class SSKRError extends Error {
|
|
|
37
37
|
}
|
|
38
38
|
static defaultMessage(type, shamirError) {
|
|
39
39
|
switch (type) {
|
|
40
|
-
case
|
|
41
|
-
case
|
|
42
|
-
case
|
|
43
|
-
case
|
|
44
|
-
case
|
|
45
|
-
case
|
|
46
|
-
case
|
|
47
|
-
case
|
|
48
|
-
case
|
|
49
|
-
case
|
|
50
|
-
case
|
|
51
|
-
case
|
|
52
|
-
case
|
|
53
|
-
case
|
|
54
|
-
case
|
|
40
|
+
case "DuplicateMemberIndex": return "When combining shares, the provided shares contained a duplicate member index";
|
|
41
|
+
case "GroupSpecInvalid": return "Invalid group specification.";
|
|
42
|
+
case "GroupCountInvalid": return "When creating a split spec, the group count is invalid";
|
|
43
|
+
case "GroupThresholdInvalid": return "SSKR group threshold is invalid";
|
|
44
|
+
case "MemberCountInvalid": return "SSKR member count is invalid";
|
|
45
|
+
case "MemberThresholdInvalid": return "SSKR member threshold is invalid";
|
|
46
|
+
case "NotEnoughGroups": return "SSKR shares did not contain enough groups";
|
|
47
|
+
case "SecretLengthNotEven": return "SSKR secret is not of even length";
|
|
48
|
+
case "SecretTooLong": return "SSKR secret is too long";
|
|
49
|
+
case "SecretTooShort": return "SSKR secret is too short";
|
|
50
|
+
case "ShareLengthInvalid": return "SSKR shares did not contain enough serialized bytes";
|
|
51
|
+
case "ShareReservedBitsInvalid": return "SSKR shares contained invalid reserved bits";
|
|
52
|
+
case "SharesEmpty": return "SSKR shares were empty";
|
|
53
|
+
case "ShareSetInvalid": return "SSKR shares were invalid";
|
|
54
|
+
case "ShamirError": return shamirError != null ? `SSKR Shamir error: ${shamirError.message}` : "SSKR Shamir error";
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
static fromShamirError(error) {
|
|
58
|
-
return new SSKRError(
|
|
58
|
+
return new SSKRError("ShamirError", void 0, error);
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
|
-
|
|
62
61
|
//#endregion
|
|
63
62
|
//#region src/secret.ts
|
|
64
63
|
/**
|
|
64
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
65
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
65
69
|
* A secret to be split into shares.
|
|
66
70
|
*/
|
|
67
71
|
var Secret = class Secret {
|
|
@@ -80,9 +84,9 @@ var Secret = class Secret {
|
|
|
80
84
|
static new(data) {
|
|
81
85
|
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
82
86
|
const len = bytes.length;
|
|
83
|
-
if (len < MIN_SECRET_LEN) throw new SSKRError(
|
|
84
|
-
if (len > MAX_SECRET_LEN) throw new SSKRError(
|
|
85
|
-
if ((len & 1) !== 0) throw new SSKRError(
|
|
87
|
+
if (len < MIN_SECRET_LEN) throw new SSKRError("SecretTooShort");
|
|
88
|
+
if (len > MAX_SECRET_LEN) throw new SSKRError("SecretTooLong");
|
|
89
|
+
if ((len & 1) !== 0) throw new SSKRError("SecretLengthNotEven");
|
|
86
90
|
return new Secret(new Uint8Array(bytes));
|
|
87
91
|
}
|
|
88
92
|
/**
|
|
@@ -99,12 +103,24 @@ var Secret = class Secret {
|
|
|
99
103
|
}
|
|
100
104
|
/**
|
|
101
105
|
* Returns a reference to the secret data.
|
|
106
|
+
*
|
|
107
|
+
* Mirrors Rust's `Secret::data(&self) -> &[u8]`
|
|
108
|
+
* (`bc-sskr-rust/src/secret.rs:43`).
|
|
102
109
|
*/
|
|
103
110
|
getData() {
|
|
104
111
|
return this.data;
|
|
105
112
|
}
|
|
106
113
|
/**
|
|
107
114
|
* Returns the secret data as a Uint8Array.
|
|
115
|
+
*
|
|
116
|
+
* Mirrors Rust's `impl AsRef<[u8]> for Secret`
|
|
117
|
+
* (`bc-sskr-rust/src/secret.rs:46-49`). In Rust, `as_ref()` is
|
|
118
|
+
* provided via the `AsRef<[u8]>` trait, which lets the `Secret` flow
|
|
119
|
+
* naturally through any API expecting `impl AsRef<[u8]>`. TypeScript
|
|
120
|
+
* has no equivalent of that trait, so we expose the same backing
|
|
121
|
+
* buffer through both {@link getData} (the field accessor) and
|
|
122
|
+
* `asRef` (the trait-style accessor) for ergonomic parity. Callers
|
|
123
|
+
* may pick whichever name reads better at the call site.
|
|
108
124
|
*/
|
|
109
125
|
asRef() {
|
|
110
126
|
return this.data;
|
|
@@ -124,10 +140,30 @@ var Secret = class Secret {
|
|
|
124
140
|
return new Secret(new Uint8Array(this.data));
|
|
125
141
|
}
|
|
126
142
|
};
|
|
127
|
-
|
|
128
143
|
//#endregion
|
|
129
144
|
//#region src/spec.ts
|
|
130
145
|
/**
|
|
146
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
147
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
148
|
+
*
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Strictly parses a non-negative integer string the way Rust's
|
|
152
|
+
* `<usize as FromStr>::from_str` does (`bc-sskr-rust/src/spec.rs:102, 108`).
|
|
153
|
+
*
|
|
154
|
+
* Accepts an optional leading `+` followed by one or more decimal digits.
|
|
155
|
+
* Rejects whitespace, decimals (`"2.5"`), trailing characters (`"2x"`),
|
|
156
|
+
* the `-` sign, and the empty string. Returns `undefined` on rejection so
|
|
157
|
+
* callers surface the appropriate `GroupSpecInvalid` error in the same
|
|
158
|
+
* spot Rust's `?` would.
|
|
159
|
+
*/
|
|
160
|
+
const STRICT_USIZE_RE = /^\+?\d+$/;
|
|
161
|
+
function parseStrictUsize(s) {
|
|
162
|
+
if (!STRICT_USIZE_RE.test(s)) return void 0;
|
|
163
|
+
const n = Number(s);
|
|
164
|
+
return Number.isSafeInteger(n) ? n : void 0;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
131
167
|
* A specification for a group of shares within an SSKR split.
|
|
132
168
|
*/
|
|
133
169
|
var GroupSpec = class GroupSpec {
|
|
@@ -149,9 +185,9 @@ var GroupSpec = class GroupSpec {
|
|
|
149
185
|
* greater than the member count.
|
|
150
186
|
*/
|
|
151
187
|
static new(memberThreshold, memberCount) {
|
|
152
|
-
if (memberCount === 0) throw new SSKRError(
|
|
153
|
-
if (memberCount >
|
|
154
|
-
if (memberThreshold > memberCount) throw new SSKRError(
|
|
188
|
+
if (memberCount === 0) throw new SSKRError("MemberCountInvalid");
|
|
189
|
+
if (memberCount > _bcts_shamir.MAX_SHARE_COUNT) throw new SSKRError("MemberCountInvalid");
|
|
190
|
+
if (memberThreshold > memberCount) throw new SSKRError("MemberThresholdInvalid");
|
|
155
191
|
return new GroupSpec(memberThreshold, memberCount);
|
|
156
192
|
}
|
|
157
193
|
/**
|
|
@@ -168,16 +204,24 @@ var GroupSpec = class GroupSpec {
|
|
|
168
204
|
}
|
|
169
205
|
/**
|
|
170
206
|
* Parses a group specification from a string.
|
|
171
|
-
* Format: "M-of-N" where M is the threshold and N is the count.
|
|
207
|
+
* Format: `"M-of-N"` where `M` is the threshold and `N` is the count.
|
|
208
|
+
*
|
|
209
|
+
* Mirrors Rust's `GroupSpec::parse` (`bc-sskr-rust/src/spec.rs:97-112`),
|
|
210
|
+
* which calls `parts[0].parse::<usize>()`. Rust's `usize::FromStr` is
|
|
211
|
+
* strict: it rejects whitespace, decimals, trailing characters, and the
|
|
212
|
+
* `-` sign, accepting only an optional `+` prefix followed by digits.
|
|
213
|
+
* We mirror that with the regex `^\+?\d+$` so strings like `"2.5"`,
|
|
214
|
+
* `"2x"`, `" 2"`, `"-2"`, and `""` all surface
|
|
215
|
+
* {@link SSKRErrorType.GroupSpecInvalid} on both sides.
|
|
172
216
|
*/
|
|
173
217
|
static parse(s) {
|
|
174
218
|
const parts = s.split("-");
|
|
175
|
-
if (parts.length !== 3) throw new SSKRError(
|
|
176
|
-
const memberThreshold =
|
|
177
|
-
if (
|
|
178
|
-
if (parts[1] !== "of") throw new SSKRError(
|
|
179
|
-
const memberCount =
|
|
180
|
-
if (
|
|
219
|
+
if (parts.length !== 3) throw new SSKRError("GroupSpecInvalid");
|
|
220
|
+
const memberThreshold = parseStrictUsize(parts[0]);
|
|
221
|
+
if (memberThreshold === void 0) throw new SSKRError("GroupSpecInvalid");
|
|
222
|
+
if (parts[1] !== "of") throw new SSKRError("GroupSpecInvalid");
|
|
223
|
+
const memberCount = parseStrictUsize(parts[2]);
|
|
224
|
+
if (memberCount === void 0) throw new SSKRError("GroupSpecInvalid");
|
|
181
225
|
return GroupSpec.new(memberThreshold, memberCount);
|
|
182
226
|
}
|
|
183
227
|
/**
|
|
@@ -216,9 +260,9 @@ var Spec = class Spec {
|
|
|
216
260
|
* greater than the maximum share count.
|
|
217
261
|
*/
|
|
218
262
|
static new(groupThreshold, groups) {
|
|
219
|
-
if (groupThreshold === 0) throw new SSKRError(
|
|
220
|
-
if (groupThreshold > groups.length) throw new SSKRError(
|
|
221
|
-
if (groups.length >
|
|
263
|
+
if (groupThreshold === 0) throw new SSKRError("GroupThresholdInvalid");
|
|
264
|
+
if (groupThreshold > groups.length) throw new SSKRError("GroupThresholdInvalid");
|
|
265
|
+
if (groups.length > _bcts_shamir.MAX_SHARE_COUNT) throw new SSKRError("GroupCountInvalid");
|
|
222
266
|
return new Spec(groupThreshold, groups);
|
|
223
267
|
}
|
|
224
268
|
/**
|
|
@@ -246,7 +290,6 @@ var Spec = class Spec {
|
|
|
246
290
|
return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);
|
|
247
291
|
}
|
|
248
292
|
};
|
|
249
|
-
|
|
250
293
|
//#endregion
|
|
251
294
|
//#region src/share.ts
|
|
252
295
|
/**
|
|
@@ -291,7 +334,6 @@ var SSKRShare = class {
|
|
|
291
334
|
return this._value;
|
|
292
335
|
}
|
|
293
336
|
};
|
|
294
|
-
|
|
295
337
|
//#endregion
|
|
296
338
|
//#region src/encoding.ts
|
|
297
339
|
/**
|
|
@@ -303,7 +345,7 @@ var SSKRShare = class {
|
|
|
303
345
|
* each of which is a Uint8Array.
|
|
304
346
|
*/
|
|
305
347
|
function sskrGenerate(spec, masterSecret) {
|
|
306
|
-
return sskrGenerateUsing(spec, masterSecret, new
|
|
348
|
+
return sskrGenerateUsing(spec, masterSecret, new _bcts_rand.SecureRandomNumberGenerator());
|
|
307
349
|
}
|
|
308
350
|
/**
|
|
309
351
|
* Generates SSKR shares for the given Spec and Secret using the provided
|
|
@@ -337,7 +379,7 @@ function sskrCombine(shares) {
|
|
|
337
379
|
}
|
|
338
380
|
function serializeShare(share) {
|
|
339
381
|
const valueData = share.value().getData();
|
|
340
|
-
const result = new Uint8Array(valueData.length +
|
|
382
|
+
const result = new Uint8Array(valueData.length + 5);
|
|
341
383
|
const id = share.identifier();
|
|
342
384
|
const gt = share.groupThreshold() - 1 & 15;
|
|
343
385
|
const gc = share.groupCount() - 1 & 15;
|
|
@@ -351,19 +393,19 @@ function serializeShare(share) {
|
|
|
351
393
|
result[2] = gt << 4 | gc;
|
|
352
394
|
result[3] = gi << 4 | mt;
|
|
353
395
|
result[4] = mi;
|
|
354
|
-
result.set(valueData,
|
|
396
|
+
result.set(valueData, 5);
|
|
355
397
|
return result;
|
|
356
398
|
}
|
|
357
399
|
function deserializeShare(source) {
|
|
358
|
-
if (source.length <
|
|
400
|
+
if (source.length < 5) throw new SSKRError("ShareLengthInvalid");
|
|
359
401
|
const groupThreshold = (source[2] >> 4) + 1;
|
|
360
402
|
const groupCount = (source[2] & 15) + 1;
|
|
361
|
-
if (groupThreshold > groupCount) throw new SSKRError(
|
|
403
|
+
if (groupThreshold > groupCount) throw new SSKRError("GroupThresholdInvalid");
|
|
362
404
|
const identifier = source[0] << 8 | source[1];
|
|
363
405
|
const groupIndex = source[3] >> 4;
|
|
364
406
|
const memberThreshold = (source[3] & 15) + 1;
|
|
365
|
-
if (source[4] >> 4 !== 0) throw new SSKRError(
|
|
366
|
-
return new SSKRShare(identifier, groupIndex, groupThreshold, groupCount, source[4] & 15, memberThreshold, Secret.new(source.subarray(
|
|
407
|
+
if (source[4] >> 4 !== 0) throw new SSKRError("ShareReservedBitsInvalid");
|
|
408
|
+
return new SSKRShare(identifier, groupIndex, groupThreshold, groupCount, source[4] & 15, memberThreshold, Secret.new(source.subarray(5)));
|
|
367
409
|
}
|
|
368
410
|
function generateShares(spec, masterSecret, randomGenerator) {
|
|
369
411
|
const identifierBytes = new Uint8Array(2);
|
|
@@ -372,9 +414,9 @@ function generateShares(spec, masterSecret, randomGenerator) {
|
|
|
372
414
|
const groupsShares = [];
|
|
373
415
|
let groupSecrets;
|
|
374
416
|
try {
|
|
375
|
-
groupSecrets = (0,
|
|
417
|
+
groupSecrets = (0, _bcts_shamir.splitSecret)(spec.groupThreshold(), spec.groupCount(), masterSecret.getData(), randomGenerator);
|
|
376
418
|
} catch (e) {
|
|
377
|
-
if (e instanceof
|
|
419
|
+
if (e instanceof _bcts_shamir.ShamirError) throw SSKRError.fromShamirError(e);
|
|
378
420
|
throw e;
|
|
379
421
|
}
|
|
380
422
|
for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {
|
|
@@ -382,9 +424,9 @@ function generateShares(spec, masterSecret, randomGenerator) {
|
|
|
382
424
|
const groupSecret = groupSecrets[groupIndex];
|
|
383
425
|
let memberSecrets;
|
|
384
426
|
try {
|
|
385
|
-
memberSecrets = (0,
|
|
427
|
+
memberSecrets = (0, _bcts_shamir.splitSecret)(group.memberThreshold(), group.memberCount(), groupSecret, randomGenerator);
|
|
386
428
|
} catch (e) {
|
|
387
|
-
if (e instanceof
|
|
429
|
+
if (e instanceof _bcts_shamir.ShamirError) throw SSKRError.fromShamirError(e);
|
|
388
430
|
throw e;
|
|
389
431
|
}
|
|
390
432
|
const memberSSKRShares = memberSecrets.map((memberSecret, memberIndex) => {
|
|
@@ -399,7 +441,7 @@ function combineShares(shares) {
|
|
|
399
441
|
let identifier = 0;
|
|
400
442
|
let groupThreshold = 0;
|
|
401
443
|
let groupCount = 0;
|
|
402
|
-
if (shares.length === 0) throw new SSKRError(
|
|
444
|
+
if (shares.length === 0) throw new SSKRError("SharesEmpty");
|
|
403
445
|
let nextGroup = 0;
|
|
404
446
|
const groups = [];
|
|
405
447
|
let secretLen = 0;
|
|
@@ -410,12 +452,12 @@ function combineShares(shares) {
|
|
|
410
452
|
groupCount = share.groupCount();
|
|
411
453
|
groupThreshold = share.groupThreshold();
|
|
412
454
|
secretLen = share.value().len();
|
|
413
|
-
} else if (share.identifier() !== identifier || share.groupThreshold() !== groupThreshold || share.groupCount() !== groupCount || share.value().len() !== secretLen) throw new SSKRError(
|
|
455
|
+
} else if (share.identifier() !== identifier || share.groupThreshold() !== groupThreshold || share.groupCount() !== groupCount || share.value().len() !== secretLen) throw new SSKRError("ShareSetInvalid");
|
|
414
456
|
let groupFound = false;
|
|
415
457
|
for (const group of groups) if (share.groupIndex() === group.groupIndex) {
|
|
416
458
|
groupFound = true;
|
|
417
|
-
if (share.memberThreshold() !== group.memberThreshold) throw new SSKRError(
|
|
418
|
-
for (const memberIndex of group.memberIndexes) if (share.memberIndex() === memberIndex) throw new SSKRError(
|
|
459
|
+
if (share.memberThreshold() !== group.memberThreshold) throw new SSKRError("MemberThresholdInvalid");
|
|
460
|
+
for (const memberIndex of group.memberIndexes) if (share.memberIndex() === memberIndex) throw new SSKRError("DuplicateMemberIndex");
|
|
419
461
|
if (group.memberIndexes.length < group.memberThreshold) {
|
|
420
462
|
group.memberIndexes.push(share.memberIndex());
|
|
421
463
|
group.memberShares.push(share.value().clone());
|
|
@@ -432,46 +474,51 @@ function combineShares(shares) {
|
|
|
432
474
|
nextGroup++;
|
|
433
475
|
}
|
|
434
476
|
}
|
|
435
|
-
if (nextGroup < groupThreshold) throw new SSKRError(
|
|
477
|
+
if (nextGroup < groupThreshold) throw new SSKRError("NotEnoughGroups");
|
|
436
478
|
const masterIndexes = [];
|
|
437
479
|
const masterShares = [];
|
|
438
480
|
for (const group of groups) {
|
|
439
481
|
if (group.memberIndexes.length < group.memberThreshold) continue;
|
|
440
482
|
try {
|
|
441
483
|
const memberSharesData = group.memberShares.map((s) => s.getData());
|
|
442
|
-
const groupSecret = (0,
|
|
484
|
+
const groupSecret = (0, _bcts_shamir.recoverSecret)(group.memberIndexes, memberSharesData);
|
|
443
485
|
masterIndexes.push(group.groupIndex);
|
|
444
486
|
masterShares.push(groupSecret);
|
|
445
|
-
} catch {
|
|
446
|
-
continue;
|
|
487
|
+
} catch (e) {
|
|
488
|
+
if (e instanceof _bcts_shamir.ShamirError) continue;
|
|
489
|
+
throw e;
|
|
447
490
|
}
|
|
448
491
|
if (masterIndexes.length === groupThreshold) break;
|
|
449
492
|
}
|
|
450
|
-
if (masterIndexes.length < groupThreshold) throw new SSKRError(
|
|
493
|
+
if (masterIndexes.length < groupThreshold) throw new SSKRError("NotEnoughGroups");
|
|
451
494
|
let masterSecretData;
|
|
452
495
|
try {
|
|
453
|
-
masterSecretData = (0,
|
|
496
|
+
masterSecretData = (0, _bcts_shamir.recoverSecret)(masterIndexes, masterShares);
|
|
454
497
|
} catch (e) {
|
|
455
|
-
if (e instanceof
|
|
498
|
+
if (e instanceof _bcts_shamir.ShamirError) throw SSKRError.fromShamirError(e);
|
|
456
499
|
throw e;
|
|
457
500
|
}
|
|
458
501
|
return Secret.new(masterSecretData);
|
|
459
502
|
}
|
|
460
|
-
|
|
461
503
|
//#endregion
|
|
462
504
|
//#region src/index.ts
|
|
463
505
|
/**
|
|
506
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
507
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
508
|
+
*
|
|
509
|
+
*/
|
|
510
|
+
/**
|
|
464
511
|
* The minimum length of a secret.
|
|
465
512
|
*/
|
|
466
|
-
const MIN_SECRET_LEN =
|
|
513
|
+
const MIN_SECRET_LEN = _bcts_shamir.MIN_SECRET_LEN;
|
|
467
514
|
/**
|
|
468
515
|
* The maximum length of a secret.
|
|
469
516
|
*/
|
|
470
|
-
const MAX_SECRET_LEN =
|
|
517
|
+
const MAX_SECRET_LEN = _bcts_shamir.MAX_SECRET_LEN;
|
|
471
518
|
/**
|
|
472
519
|
* The maximum number of shares that can be generated from a secret.
|
|
473
520
|
*/
|
|
474
|
-
const MAX_SHARE_COUNT =
|
|
521
|
+
const MAX_SHARE_COUNT = _bcts_shamir.MAX_SHARE_COUNT;
|
|
475
522
|
/**
|
|
476
523
|
* The maximum number of groups in a split.
|
|
477
524
|
*/
|
|
@@ -483,8 +530,7 @@ const METADATA_SIZE_BYTES = 5;
|
|
|
483
530
|
/**
|
|
484
531
|
* The minimum number of bytes required to encode a share.
|
|
485
532
|
*/
|
|
486
|
-
const MIN_SERIALIZE_SIZE_BYTES =
|
|
487
|
-
|
|
533
|
+
const MIN_SERIALIZE_SIZE_BYTES = 5 + MIN_SECRET_LEN;
|
|
488
534
|
//#endregion
|
|
489
535
|
exports.GroupSpec = GroupSpec;
|
|
490
536
|
exports.MAX_GROUPS_COUNT = MAX_GROUPS_COUNT;
|
|
@@ -495,10 +541,10 @@ exports.MIN_SECRET_LEN = MIN_SECRET_LEN;
|
|
|
495
541
|
exports.MIN_SERIALIZE_SIZE_BYTES = MIN_SERIALIZE_SIZE_BYTES;
|
|
496
542
|
exports.SSKRError = SSKRError;
|
|
497
543
|
exports.SSKRErrorType = SSKRErrorType;
|
|
498
|
-
exports.SSKRShare = SSKRShare;
|
|
499
544
|
exports.Secret = Secret;
|
|
500
545
|
exports.Spec = Spec;
|
|
501
546
|
exports.sskrCombine = sskrCombine;
|
|
502
547
|
exports.sskrGenerate = sskrGenerate;
|
|
503
548
|
exports.sskrGenerateUsing = sskrGenerateUsing;
|
|
549
|
+
|
|
504
550
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["MAX_SHARE_COUNT","SecureRandomNumberGenerator","sskrShares: SSKRShare[]","groupsShares: SSKRShare[][]","groupSecrets: Uint8Array[]","ShamirError","memberSecrets: Uint8Array[]","memberSSKRShares: SSKRShare[]","groups: Group[]","g: Group","masterIndexes: number[]","masterShares: Uint8Array[]","masterSecretData: Uint8Array","SHAMIR_MIN_SECRET_LEN","SHAMIR_MAX_SECRET_LEN","SHAMIR_MAX_SHARE_COUNT"],"sources":["../src/error.ts","../src/secret.ts","../src/spec.ts","../src/share.ts","../src/encoding.ts","../src/index.ts"],"sourcesContent":["// Ported from bc-sskr-rust/src/error.rs\n\nimport type { ShamirError } from \"@bcts/shamir\";\n\n/**\n * Error types for SSKR operations.\n */\nexport enum SSKRErrorType {\n DuplicateMemberIndex = \"DuplicateMemberIndex\",\n GroupSpecInvalid = \"GroupSpecInvalid\",\n GroupCountInvalid = \"GroupCountInvalid\",\n GroupThresholdInvalid = \"GroupThresholdInvalid\",\n MemberCountInvalid = \"MemberCountInvalid\",\n MemberThresholdInvalid = \"MemberThresholdInvalid\",\n NotEnoughGroups = \"NotEnoughGroups\",\n SecretLengthNotEven = \"SecretLengthNotEven\",\n SecretTooLong = \"SecretTooLong\",\n SecretTooShort = \"SecretTooShort\",\n ShareLengthInvalid = \"ShareLengthInvalid\",\n ShareReservedBitsInvalid = \"ShareReservedBitsInvalid\",\n SharesEmpty = \"SharesEmpty\",\n ShareSetInvalid = \"ShareSetInvalid\",\n ShamirError = \"ShamirError\",\n}\n\n/**\n * Error class for SSKR operations.\n */\nexport class SSKRError extends Error {\n readonly type: SSKRErrorType;\n readonly shamirError?: ShamirError | undefined;\n\n constructor(type: SSKRErrorType, message?: string, shamirError?: ShamirError) {\n super(message ?? SSKRError.defaultMessage(type, shamirError));\n this.type = type;\n this.shamirError = shamirError;\n this.name = \"SSKRError\";\n }\n\n private static defaultMessage(type: SSKRErrorType, shamirError?: ShamirError): string {\n switch (type) {\n case SSKRErrorType.DuplicateMemberIndex:\n return \"When combining shares, the provided shares contained a duplicate member index\";\n case SSKRErrorType.GroupSpecInvalid:\n return \"Invalid group specification\";\n case SSKRErrorType.GroupCountInvalid:\n return \"When creating a split spec, the group count is invalid\";\n case SSKRErrorType.GroupThresholdInvalid:\n return \"SSKR group threshold is invalid\";\n case SSKRErrorType.MemberCountInvalid:\n return \"SSKR member count is invalid\";\n case SSKRErrorType.MemberThresholdInvalid:\n return \"SSKR member threshold is invalid\";\n case SSKRErrorType.NotEnoughGroups:\n return \"SSKR shares did not contain enough groups\";\n case SSKRErrorType.SecretLengthNotEven:\n return \"SSKR secret is not of even length\";\n case SSKRErrorType.SecretTooLong:\n return \"SSKR secret is too long\";\n case SSKRErrorType.SecretTooShort:\n return \"SSKR secret is too short\";\n case SSKRErrorType.ShareLengthInvalid:\n return \"SSKR shares did not contain enough serialized bytes\";\n case SSKRErrorType.ShareReservedBitsInvalid:\n return \"SSKR shares contained invalid reserved bits\";\n case SSKRErrorType.SharesEmpty:\n return \"SSKR shares were empty\";\n case SSKRErrorType.ShareSetInvalid:\n return \"SSKR shares were invalid\";\n case SSKRErrorType.ShamirError:\n return shamirError != null\n ? `SSKR Shamir error: ${shamirError.message}`\n : \"SSKR Shamir error\";\n }\n }\n\n static fromShamirError(error: ShamirError): SSKRError {\n return new SSKRError(SSKRErrorType.ShamirError, undefined, error);\n }\n}\n\nexport type SSKRResult<T> = T;\n","// Ported from bc-sskr-rust/src/secret.rs\n\nimport { MIN_SECRET_LEN, MAX_SECRET_LEN } from \"./index.js\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A secret to be split into shares.\n */\nexport class Secret {\n private readonly data: Uint8Array;\n\n private constructor(data: Uint8Array) {\n this.data = data;\n }\n\n /**\n * Creates a new Secret instance with the given data.\n *\n * @param data - The secret data to be split into shares.\n * @returns A new Secret instance.\n * @throws SSKRError if the length of the secret is less than\n * MIN_SECRET_LEN, greater than MAX_SECRET_LEN, or not even.\n */\n static new(data: Uint8Array | string): Secret {\n const bytes = typeof data === \"string\" ? new TextEncoder().encode(data) : data;\n const len = bytes.length;\n\n if (len < MIN_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooShort);\n }\n if (len > MAX_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooLong);\n }\n if ((len & 1) !== 0) {\n throw new SSKRError(SSKRErrorType.SecretLengthNotEven);\n }\n\n return new Secret(new Uint8Array(bytes));\n }\n\n /**\n * Returns the length of the secret.\n */\n len(): number {\n return this.data.length;\n }\n\n /**\n * Returns true if the secret is empty.\n */\n isEmpty(): boolean {\n return this.len() === 0;\n }\n\n /**\n * Returns a reference to the secret data.\n */\n getData(): Uint8Array {\n return this.data;\n }\n\n /**\n * Returns the secret data as a Uint8Array.\n */\n asRef(): Uint8Array {\n return this.data;\n }\n\n /**\n * Check equality with another Secret.\n */\n equals(other: Secret): boolean {\n if (this.data.length !== other.data.length) {\n return false;\n }\n for (let i = 0; i < this.data.length; i++) {\n if (this.data[i] !== other.data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Clone the secret.\n */\n clone(): Secret {\n return new Secret(new Uint8Array(this.data));\n }\n}\n","// Ported from bc-sskr-rust/src/spec.rs\n\nimport { MAX_SHARE_COUNT } from \"@bcts/shamir\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A specification for a group of shares within an SSKR split.\n */\nexport class GroupSpec {\n private readonly _memberThreshold: number;\n private readonly _memberCount: number;\n\n private constructor(memberThreshold: number, memberCount: number) {\n this._memberThreshold = memberThreshold;\n this._memberCount = memberCount;\n }\n\n /**\n * Creates a new GroupSpec instance with the given member threshold and count.\n *\n * @param memberThreshold - The minimum number of member shares required to\n * reconstruct the secret within the group.\n * @param memberCount - The total number of member shares in the group.\n * @returns A new GroupSpec instance.\n * @throws SSKRError if the member count is zero, if the member count is\n * greater than the maximum share count, or if the member threshold is\n * greater than the member count.\n */\n static new(memberThreshold: number, memberCount: number): GroupSpec {\n if (memberCount === 0) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberCount > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberThreshold > memberCount) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n return new GroupSpec(memberThreshold, memberCount);\n }\n\n /**\n * Returns the member share threshold for this group.\n */\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n /**\n * Returns the number of member shares in this group.\n */\n memberCount(): number {\n return this._memberCount;\n }\n\n /**\n * Parses a group specification from a string.\n * Format: \"M-of-N\" where M is the threshold and N is the count.\n */\n static parse(s: string): GroupSpec {\n const parts = s.split(\"-\");\n if (parts.length !== 3) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberThreshold = parseInt(parts[0], 10);\n if (isNaN(memberThreshold)) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n if (parts[1] !== \"of\") {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberCount = parseInt(parts[2], 10);\n if (isNaN(memberCount)) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n return GroupSpec.new(memberThreshold, memberCount);\n }\n\n /**\n * Creates a default GroupSpec (1-of-1).\n */\n static default(): GroupSpec {\n return GroupSpec.new(1, 1);\n }\n\n /**\n * Returns a string representation of the group spec.\n */\n toString(): string {\n return `${this._memberThreshold}-of-${this._memberCount}`;\n }\n}\n\n/**\n * A specification for an SSKR split.\n */\nexport class Spec {\n private readonly _groupThreshold: number;\n private readonly _groups: GroupSpec[];\n\n private constructor(groupThreshold: number, groups: GroupSpec[]) {\n this._groupThreshold = groupThreshold;\n this._groups = groups;\n }\n\n /**\n * Creates a new Spec instance with the given group threshold and groups.\n *\n * @param groupThreshold - The minimum number of groups required to\n * reconstruct the secret.\n * @param groups - The list of GroupSpec instances that define the groups\n * and their members.\n * @returns A new Spec instance.\n * @throws SSKRError if the group threshold is zero, if the group threshold\n * is greater than the number of groups, or if the number of groups is\n * greater than the maximum share count.\n */\n static new(groupThreshold: number, groups: GroupSpec[]): Spec {\n if (groupThreshold === 0) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groupThreshold > groups.length) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groups.length > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.GroupCountInvalid);\n }\n return new Spec(groupThreshold, groups);\n }\n\n /**\n * Returns the group threshold.\n */\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n /**\n * Returns a slice of the group specifications.\n */\n groups(): GroupSpec[] {\n return this._groups;\n }\n\n /**\n * Returns the number of groups.\n */\n groupCount(): number {\n return this._groups.length;\n }\n\n /**\n * Returns the total number of shares across all groups.\n */\n shareCount(): number {\n return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);\n }\n}\n","// Ported from bc-sskr-rust/src/share.rs\n\nimport type { Secret } from \"./secret.js\";\n\n/**\n * A share in the SSKR scheme.\n */\nexport class SSKRShare {\n private readonly _identifier: number;\n private readonly _groupIndex: number;\n private readonly _groupThreshold: number;\n private readonly _groupCount: number;\n private readonly _memberIndex: number;\n private readonly _memberThreshold: number;\n private readonly _value: Secret;\n\n constructor(\n identifier: number,\n groupIndex: number,\n groupThreshold: number,\n groupCount: number,\n memberIndex: number,\n memberThreshold: number,\n value: Secret,\n ) {\n this._identifier = identifier;\n this._groupIndex = groupIndex;\n this._groupThreshold = groupThreshold;\n this._groupCount = groupCount;\n this._memberIndex = memberIndex;\n this._memberThreshold = memberThreshold;\n this._value = value;\n }\n\n identifier(): number {\n return this._identifier;\n }\n\n groupIndex(): number {\n return this._groupIndex;\n }\n\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n groupCount(): number {\n return this._groupCount;\n }\n\n memberIndex(): number {\n return this._memberIndex;\n }\n\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n value(): Secret {\n return this._value;\n }\n}\n","// Ported from bc-sskr-rust/src/encoding.rs\n\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\nimport { SecureRandomNumberGenerator } from \"@bcts/rand\";\nimport { splitSecret, recoverSecret, ShamirError } from \"@bcts/shamir\";\n\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\nimport { Secret } from \"./secret.js\";\nimport type { Spec } from \"./spec.js\";\nimport { SSKRShare } from \"./share.js\";\nimport { METADATA_SIZE_BYTES } from \"./index.js\";\n\n/**\n * Generates SSKR shares for the given Spec and Secret.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerate(spec: Spec, masterSecret: Secret): Uint8Array[][] {\n const rng = new SecureRandomNumberGenerator();\n return sskrGenerateUsing(spec, masterSecret, rng);\n}\n\n/**\n * Generates SSKR shares for the given Spec and Secret using the provided\n * random number generator.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @param randomGenerator - The random number generator to use for generating\n * shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerateUsing(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[][] {\n const groupsShares = generateShares(spec, masterSecret, randomGenerator);\n\n const result: Uint8Array[][] = groupsShares.map((group) => group.map(serializeShare));\n\n return result;\n}\n\n/**\n * Combines the given SSKR shares into a Secret.\n *\n * @param shares - A array of SSKR shares to be combined.\n * @returns The reconstructed Secret.\n * @throws SSKRError if the shares do not meet the necessary quorum of groups\n * and member shares within each group.\n */\nexport function sskrCombine(shares: Uint8Array[]): Secret {\n const sskrShares: SSKRShare[] = [];\n\n for (const share of shares) {\n const sskrShare = deserializeShare(share);\n sskrShares.push(sskrShare);\n }\n\n return combineShares(sskrShares);\n}\n\nfunction serializeShare(share: SSKRShare): Uint8Array {\n // pack the id, group and member data into 5 bytes:\n // 76543210 76543210 76543210\n // 76543210 76543210\n // ----------------====----====----====----\n // identifier: 16\n // group-threshold: 4\n // group-count: 4\n // group-index: 4\n // member-threshold: 4\n // reserved (MUST be zero): 4\n // member-index: 4\n\n const valueData = share.value().getData();\n const result = new Uint8Array(valueData.length + METADATA_SIZE_BYTES);\n\n const id = share.identifier();\n const gt = (share.groupThreshold() - 1) & 0xf;\n const gc = (share.groupCount() - 1) & 0xf;\n const gi = share.groupIndex() & 0xf;\n const mt = (share.memberThreshold() - 1) & 0xf;\n const mi = share.memberIndex() & 0xf;\n\n const id1 = id >> 8;\n const id2 = id & 0xff;\n\n result[0] = id1;\n result[1] = id2;\n result[2] = (gt << 4) | gc;\n result[3] = (gi << 4) | mt;\n result[4] = mi;\n result.set(valueData, METADATA_SIZE_BYTES);\n\n return result;\n}\n\nfunction deserializeShare(source: Uint8Array): SSKRShare {\n if (source.length < METADATA_SIZE_BYTES) {\n throw new SSKRError(SSKRErrorType.ShareLengthInvalid);\n }\n\n const groupThreshold = (source[2] >> 4) + 1;\n const groupCount = (source[2] & 0xf) + 1;\n\n if (groupThreshold > groupCount) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n\n const identifier = (source[0] << 8) | source[1];\n const groupIndex = source[3] >> 4;\n const memberThreshold = (source[3] & 0xf) + 1;\n const reserved = source[4] >> 4;\n if (reserved !== 0) {\n throw new SSKRError(SSKRErrorType.ShareReservedBitsInvalid);\n }\n const memberIndex = source[4] & 0xf;\n const value = Secret.new(source.subarray(METADATA_SIZE_BYTES));\n\n return new SSKRShare(\n identifier,\n groupIndex,\n groupThreshold,\n groupCount,\n memberIndex,\n memberThreshold,\n value,\n );\n}\n\nfunction generateShares(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): SSKRShare[][] {\n // assign a random identifier\n const identifierBytes = new Uint8Array(2);\n randomGenerator.fillRandomData(identifierBytes);\n const identifier = (identifierBytes[0] << 8) | identifierBytes[1];\n\n const groupsShares: SSKRShare[][] = [];\n\n let groupSecrets: Uint8Array[];\n try {\n groupSecrets = splitSecret(\n spec.groupThreshold(),\n spec.groupCount(),\n masterSecret.getData(),\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {\n const group = spec.groups()[groupIndex];\n const groupSecret = groupSecrets[groupIndex];\n\n let memberSecrets: Uint8Array[];\n try {\n memberSecrets = splitSecret(\n group.memberThreshold(),\n group.memberCount(),\n groupSecret,\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n const memberSSKRShares: SSKRShare[] = memberSecrets.map((memberSecret, memberIndex) => {\n const secret = Secret.new(memberSecret);\n return new SSKRShare(\n identifier,\n groupIndex,\n spec.groupThreshold(),\n spec.groupCount(),\n memberIndex,\n group.memberThreshold(),\n secret,\n );\n });\n\n groupsShares.push(memberSSKRShares);\n }\n\n return groupsShares;\n}\n\ninterface Group {\n groupIndex: number;\n memberThreshold: number;\n memberIndexes: number[];\n memberShares: Secret[];\n}\n\nfunction combineShares(shares: SSKRShare[]): Secret {\n let identifier = 0;\n let groupThreshold = 0;\n let groupCount = 0;\n\n if (shares.length === 0) {\n throw new SSKRError(SSKRErrorType.SharesEmpty);\n }\n\n let nextGroup = 0;\n const groups: Group[] = [];\n let secretLen = 0;\n\n for (let i = 0; i < shares.length; i++) {\n const share = shares[i];\n\n if (i === 0) {\n // on the first one, establish expected values for common metadata\n identifier = share.identifier();\n groupCount = share.groupCount();\n groupThreshold = share.groupThreshold();\n secretLen = share.value().len();\n } else {\n // on subsequent shares, check that common metadata matches\n if (\n share.identifier() !== identifier ||\n share.groupThreshold() !== groupThreshold ||\n share.groupCount() !== groupCount ||\n share.value().len() !== secretLen\n ) {\n throw new SSKRError(SSKRErrorType.ShareSetInvalid);\n }\n }\n\n // sort shares into member groups\n let groupFound = false;\n for (const group of groups) {\n if (share.groupIndex() === group.groupIndex) {\n groupFound = true;\n if (share.memberThreshold() !== group.memberThreshold) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n for (const memberIndex of group.memberIndexes) {\n if (share.memberIndex() === memberIndex) {\n throw new SSKRError(SSKRErrorType.DuplicateMemberIndex);\n }\n }\n if (group.memberIndexes.length < group.memberThreshold) {\n group.memberIndexes.push(share.memberIndex());\n group.memberShares.push(share.value().clone());\n }\n }\n }\n\n if (!groupFound) {\n const g: Group = {\n groupIndex: share.groupIndex(),\n memberThreshold: share.memberThreshold(),\n memberIndexes: [share.memberIndex()],\n memberShares: [share.value().clone()],\n };\n groups.push(g);\n nextGroup++;\n }\n }\n\n // Check that we have enough groups to recover the master secret\n if (nextGroup < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Here, all of the shares are unpacked into member groups. Now we go\n // through each group and recover the group secret, and then use the\n // result to recover the master secret\n const masterIndexes: number[] = [];\n const masterShares: Uint8Array[] = [];\n\n for (const group of groups) {\n // Only attempt to recover the group secret if we have enough shares\n if (group.memberIndexes.length < group.memberThreshold) {\n continue;\n }\n\n // Recover the group secret\n try {\n const memberSharesData = group.memberShares.map((s) => s.getData());\n const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);\n masterIndexes.push(group.groupIndex);\n masterShares.push(groupSecret);\n } catch {\n // If we can't recover this group, just skip it\n continue;\n }\n\n // Stop if we have enough groups to recover the master secret\n if (masterIndexes.length === groupThreshold) {\n break;\n }\n }\n\n // If we don't have enough groups to recover the master secret, return an error\n if (masterIndexes.length < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Recover the master secret\n let masterSecretData: Uint8Array;\n try {\n masterSecretData = recoverSecret(masterIndexes, masterShares);\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n return Secret.new(masterSecretData);\n}\n","// Blockchain Commons Sharded Secret Key Reconstruction (SSKR)\n// Ported from bc-sskr-rust\n//\n// Sharded Secret Key Reconstruction (SSKR) is a protocol for splitting a\n// secret into a set of shares across one or more groups, such that the\n// secret can be reconstructed from any combination of shares totaling or\n// exceeding a threshold number of shares within each group and across all\n// groups. SSKR is a generalization of Shamir's Secret Sharing (SSS) that\n// allows for multiple groups and multiple thresholds.\n\nimport {\n MIN_SECRET_LEN as SHAMIR_MIN_SECRET_LEN,\n MAX_SECRET_LEN as SHAMIR_MAX_SECRET_LEN,\n MAX_SHARE_COUNT as SHAMIR_MAX_SHARE_COUNT,\n} from \"@bcts/shamir\";\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = SHAMIR_MIN_SECRET_LEN;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = SHAMIR_MAX_SECRET_LEN;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = SHAMIR_MAX_SHARE_COUNT;\n\n/**\n * The maximum number of groups in a split.\n */\nexport const MAX_GROUPS_COUNT = MAX_SHARE_COUNT;\n\n/**\n * The number of bytes used to encode the metadata for a share.\n */\nexport const METADATA_SIZE_BYTES = 5;\n\n/**\n * The minimum number of bytes required to encode a share.\n */\nexport const MIN_SERIALIZE_SIZE_BYTES = METADATA_SIZE_BYTES + MIN_SECRET_LEN;\n\n// Error types\nexport { SSKRError, SSKRErrorType, type SSKRResult } from \"./error.js\";\n\n// Secret\nexport { Secret } from \"./secret.js\";\n\n// Specifications\nexport { GroupSpec, Spec } from \"./spec.js\";\n\n// Share\nexport { SSKRShare } from \"./share.js\";\n\n// Encoding/Decoding\nexport { sskrGenerate, sskrGenerateUsing, sskrCombine } from \"./encoding.js\";\n"],"mappings":";;;;;;;AAOA,IAAY,0DAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;AAMF,IAAa,YAAb,MAAa,kBAAkB,MAAM;CACnC,AAAS;CACT,AAAS;CAET,YAAY,MAAqB,SAAkB,aAA2B;AAC5E,QAAM,WAAW,UAAU,eAAe,MAAM,YAAY,CAAC;AAC7D,OAAK,OAAO;AACZ,OAAK,cAAc;AACnB,OAAK,OAAO;;CAGd,OAAe,eAAe,MAAqB,aAAmC;AACpF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0B,EAAE;CAClC,MAAMC,eAA6B,EAAE;AAErC,MAAK,MAAM,SAAS,QAAQ;AAE1B,MAAI,MAAM,cAAc,SAAS,MAAM,gBACrC;AAIF,MAAI;GACF,MAAM,mBAAmB,MAAM,aAAa,KAAK,MAAM,EAAE,SAAS,CAAC;GACnE,MAAM,+CAA4B,MAAM,eAAe,iBAAiB;AACxE,iBAAc,KAAK,MAAM,WAAW;AACpC,gBAAa,KAAK,YAAY;UACxB;AAEN;;AAIF,MAAI,cAAc,WAAW,eAC3B;;AAKJ,KAAI,cAAc,SAAS,eACzB,OAAM,IAAI,UAAU,cAAc,gBAAgB;CAIpD,IAAIC;AACJ,KAAI;AACF,sDAAiC,eAAe,aAAa;UACtD,GAAG;AACV,MAAI,aAAaP,0BACf,OAAM,UAAU,gBAAgB,EAAE;AAEpC,QAAM;;AAGR,QAAO,OAAO,IAAI,iBAAiB;;;;;;;;ACjTrC,MAAa,iBAAiBQ;;;;AAK9B,MAAa,iBAAiBC;;;;AAK9B,MAAa,kBAAkBC;;;;AAK/B,MAAa,mBAAmB;;;;AAKhC,MAAa,sBAAsB;;;;AAKnC,MAAa,2BAA2B,sBAAsB"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["MAX_SHARE_COUNT","SecureRandomNumberGenerator","ShamirError","SHAMIR_MIN_SECRET_LEN","SHAMIR_MAX_SECRET_LEN","SHAMIR_MAX_SHARE_COUNT"],"sources":["../src/error.ts","../src/secret.ts","../src/spec.ts","../src/share.ts","../src/encoding.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-sskr-rust/src/error.rs\n\nimport type { ShamirError } from \"@bcts/shamir\";\n\n/**\n * Error types for SSKR operations.\n */\nexport enum SSKRErrorType {\n DuplicateMemberIndex = \"DuplicateMemberIndex\",\n GroupSpecInvalid = \"GroupSpecInvalid\",\n GroupCountInvalid = \"GroupCountInvalid\",\n GroupThresholdInvalid = \"GroupThresholdInvalid\",\n MemberCountInvalid = \"MemberCountInvalid\",\n MemberThresholdInvalid = \"MemberThresholdInvalid\",\n NotEnoughGroups = \"NotEnoughGroups\",\n SecretLengthNotEven = \"SecretLengthNotEven\",\n SecretTooLong = \"SecretTooLong\",\n SecretTooShort = \"SecretTooShort\",\n ShareLengthInvalid = \"ShareLengthInvalid\",\n ShareReservedBitsInvalid = \"ShareReservedBitsInvalid\",\n SharesEmpty = \"SharesEmpty\",\n ShareSetInvalid = \"ShareSetInvalid\",\n ShamirError = \"ShamirError\",\n}\n\n/**\n * Error class for SSKR operations.\n */\nexport class SSKRError extends Error {\n readonly type: SSKRErrorType;\n readonly shamirError?: ShamirError | undefined;\n\n constructor(type: SSKRErrorType, message?: string, shamirError?: ShamirError) {\n super(message ?? SSKRError.defaultMessage(type, shamirError));\n this.type = type;\n this.shamirError = shamirError;\n this.name = \"SSKRError\";\n }\n\n private static defaultMessage(type: SSKRErrorType, shamirError?: ShamirError): string {\n switch (type) {\n case SSKRErrorType.DuplicateMemberIndex:\n return \"When combining shares, the provided shares contained a duplicate member index\";\n case SSKRErrorType.GroupSpecInvalid:\n return \"Invalid group specification.\";\n case SSKRErrorType.GroupCountInvalid:\n return \"When creating a split spec, the group count is invalid\";\n case SSKRErrorType.GroupThresholdInvalid:\n return \"SSKR group threshold is invalid\";\n case SSKRErrorType.MemberCountInvalid:\n return \"SSKR member count is invalid\";\n case SSKRErrorType.MemberThresholdInvalid:\n return \"SSKR member threshold is invalid\";\n case SSKRErrorType.NotEnoughGroups:\n return \"SSKR shares did not contain enough groups\";\n case SSKRErrorType.SecretLengthNotEven:\n return \"SSKR secret is not of even length\";\n case SSKRErrorType.SecretTooLong:\n return \"SSKR secret is too long\";\n case SSKRErrorType.SecretTooShort:\n return \"SSKR secret is too short\";\n case SSKRErrorType.ShareLengthInvalid:\n return \"SSKR shares did not contain enough serialized bytes\";\n case SSKRErrorType.ShareReservedBitsInvalid:\n return \"SSKR shares contained invalid reserved bits\";\n case SSKRErrorType.SharesEmpty:\n return \"SSKR shares were empty\";\n case SSKRErrorType.ShareSetInvalid:\n return \"SSKR shares were invalid\";\n case SSKRErrorType.ShamirError:\n return shamirError != null\n ? `SSKR Shamir error: ${shamirError.message}`\n : \"SSKR Shamir error\";\n }\n }\n\n static fromShamirError(error: ShamirError): SSKRError {\n return new SSKRError(SSKRErrorType.ShamirError, undefined, error);\n }\n}\n\n/**\n * Mirrors Rust's `Result<T, Error>` (`bc-sskr-rust/src/error.rs:54`).\n *\n * The TypeScript port surfaces failures by throwing {@link SSKRError}\n * rather than returning a sum type, so this alias is a no-op\n * (`SSKRResult<T>` ≡ `T`). It is kept so signatures published in\n * `@bcts/sskr` remain visually parallel to their Rust counterparts.\n */\nexport type SSKRResult<T> = T;\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/secret.rs\n\nimport { MIN_SECRET_LEN, MAX_SECRET_LEN } from \"./index.js\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A secret to be split into shares.\n */\nexport class Secret {\n private readonly data: Uint8Array;\n\n private constructor(data: Uint8Array) {\n this.data = data;\n }\n\n /**\n * Creates a new Secret instance with the given data.\n *\n * @param data - The secret data to be split into shares.\n * @returns A new Secret instance.\n * @throws SSKRError if the length of the secret is less than\n * MIN_SECRET_LEN, greater than MAX_SECRET_LEN, or not even.\n */\n static new(data: Uint8Array | string): Secret {\n const bytes = typeof data === \"string\" ? new TextEncoder().encode(data) : data;\n const len = bytes.length;\n\n if (len < MIN_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooShort);\n }\n if (len > MAX_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooLong);\n }\n if ((len & 1) !== 0) {\n throw new SSKRError(SSKRErrorType.SecretLengthNotEven);\n }\n\n return new Secret(new Uint8Array(bytes));\n }\n\n /**\n * Returns the length of the secret.\n */\n len(): number {\n return this.data.length;\n }\n\n /**\n * Returns true if the secret is empty.\n */\n isEmpty(): boolean {\n return this.len() === 0;\n }\n\n /**\n * Returns a reference to the secret data.\n *\n * Mirrors Rust's `Secret::data(&self) -> &[u8]`\n * (`bc-sskr-rust/src/secret.rs:43`).\n */\n getData(): Uint8Array {\n return this.data;\n }\n\n /**\n * Returns the secret data as a Uint8Array.\n *\n * Mirrors Rust's `impl AsRef<[u8]> for Secret`\n * (`bc-sskr-rust/src/secret.rs:46-49`). In Rust, `as_ref()` is\n * provided via the `AsRef<[u8]>` trait, which lets the `Secret` flow\n * naturally through any API expecting `impl AsRef<[u8]>`. TypeScript\n * has no equivalent of that trait, so we expose the same backing\n * buffer through both {@link getData} (the field accessor) and\n * `asRef` (the trait-style accessor) for ergonomic parity. Callers\n * may pick whichever name reads better at the call site.\n */\n asRef(): Uint8Array {\n return this.data;\n }\n\n /**\n * Check equality with another Secret.\n */\n equals(other: Secret): boolean {\n if (this.data.length !== other.data.length) {\n return false;\n }\n for (let i = 0; i < this.data.length; i++) {\n if (this.data[i] !== other.data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Clone the secret.\n */\n clone(): Secret {\n return new Secret(new Uint8Array(this.data));\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/spec.rs\n\nimport { MAX_SHARE_COUNT } from \"@bcts/shamir\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * Strictly parses a non-negative integer string the way Rust's\n * `<usize as FromStr>::from_str` does (`bc-sskr-rust/src/spec.rs:102, 108`).\n *\n * Accepts an optional leading `+` followed by one or more decimal digits.\n * Rejects whitespace, decimals (`\"2.5\"`), trailing characters (`\"2x\"`),\n * the `-` sign, and the empty string. Returns `undefined` on rejection so\n * callers surface the appropriate `GroupSpecInvalid` error in the same\n * spot Rust's `?` would.\n */\nconst STRICT_USIZE_RE = /^\\+?\\d+$/;\nfunction parseStrictUsize(s: string): number | undefined {\n if (!STRICT_USIZE_RE.test(s)) return undefined;\n const n = Number(s);\n // `Number.isSafeInteger` rejects values outside ±2^53; usize on\n // 64-bit Rust covers a larger range but for SSKR's `member_count`\n // (capped at 16) this is far beyond any meaningful input.\n return Number.isSafeInteger(n) ? n : undefined;\n}\n\n/**\n * A specification for a group of shares within an SSKR split.\n */\nexport class GroupSpec {\n private readonly _memberThreshold: number;\n private readonly _memberCount: number;\n\n private constructor(memberThreshold: number, memberCount: number) {\n this._memberThreshold = memberThreshold;\n this._memberCount = memberCount;\n }\n\n /**\n * Creates a new GroupSpec instance with the given member threshold and count.\n *\n * @param memberThreshold - The minimum number of member shares required to\n * reconstruct the secret within the group.\n * @param memberCount - The total number of member shares in the group.\n * @returns A new GroupSpec instance.\n * @throws SSKRError if the member count is zero, if the member count is\n * greater than the maximum share count, or if the member threshold is\n * greater than the member count.\n */\n static new(memberThreshold: number, memberCount: number): GroupSpec {\n if (memberCount === 0) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberCount > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberThreshold > memberCount) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n return new GroupSpec(memberThreshold, memberCount);\n }\n\n /**\n * Returns the member share threshold for this group.\n */\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n /**\n * Returns the number of member shares in this group.\n */\n memberCount(): number {\n return this._memberCount;\n }\n\n /**\n * Parses a group specification from a string.\n * Format: `\"M-of-N\"` where `M` is the threshold and `N` is the count.\n *\n * Mirrors Rust's `GroupSpec::parse` (`bc-sskr-rust/src/spec.rs:97-112`),\n * which calls `parts[0].parse::<usize>()`. Rust's `usize::FromStr` is\n * strict: it rejects whitespace, decimals, trailing characters, and the\n * `-` sign, accepting only an optional `+` prefix followed by digits.\n * We mirror that with the regex `^\\+?\\d+$` so strings like `\"2.5\"`,\n * `\"2x\"`, `\" 2\"`, `\"-2\"`, and `\"\"` all surface\n * {@link SSKRErrorType.GroupSpecInvalid} on both sides.\n */\n static parse(s: string): GroupSpec {\n const parts = s.split(\"-\");\n if (parts.length !== 3) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberThreshold = parseStrictUsize(parts[0]);\n if (memberThreshold === undefined) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n if (parts[1] !== \"of\") {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberCount = parseStrictUsize(parts[2]);\n if (memberCount === undefined) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n return GroupSpec.new(memberThreshold, memberCount);\n }\n\n /**\n * Creates a default GroupSpec (1-of-1).\n */\n static default(): GroupSpec {\n return GroupSpec.new(1, 1);\n }\n\n /**\n * Returns a string representation of the group spec.\n */\n toString(): string {\n return `${this._memberThreshold}-of-${this._memberCount}`;\n }\n}\n\n/**\n * A specification for an SSKR split.\n */\nexport class Spec {\n private readonly _groupThreshold: number;\n private readonly _groups: GroupSpec[];\n\n private constructor(groupThreshold: number, groups: GroupSpec[]) {\n this._groupThreshold = groupThreshold;\n this._groups = groups;\n }\n\n /**\n * Creates a new Spec instance with the given group threshold and groups.\n *\n * @param groupThreshold - The minimum number of groups required to\n * reconstruct the secret.\n * @param groups - The list of GroupSpec instances that define the groups\n * and their members.\n * @returns A new Spec instance.\n * @throws SSKRError if the group threshold is zero, if the group threshold\n * is greater than the number of groups, or if the number of groups is\n * greater than the maximum share count.\n */\n static new(groupThreshold: number, groups: GroupSpec[]): Spec {\n if (groupThreshold === 0) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groupThreshold > groups.length) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groups.length > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.GroupCountInvalid);\n }\n return new Spec(groupThreshold, groups);\n }\n\n /**\n * Returns the group threshold.\n */\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n /**\n * Returns a slice of the group specifications.\n */\n groups(): GroupSpec[] {\n return this._groups;\n }\n\n /**\n * Returns the number of groups.\n */\n groupCount(): number {\n return this._groups.length;\n }\n\n /**\n * Returns the total number of shares across all groups.\n */\n shareCount(): number {\n return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/share.rs\n\nimport type { Secret } from \"./secret.js\";\n\n/**\n * A share in the SSKR scheme.\n */\nexport class SSKRShare {\n private readonly _identifier: number;\n private readonly _groupIndex: number;\n private readonly _groupThreshold: number;\n private readonly _groupCount: number;\n private readonly _memberIndex: number;\n private readonly _memberThreshold: number;\n private readonly _value: Secret;\n\n constructor(\n identifier: number,\n groupIndex: number,\n groupThreshold: number,\n groupCount: number,\n memberIndex: number,\n memberThreshold: number,\n value: Secret,\n ) {\n this._identifier = identifier;\n this._groupIndex = groupIndex;\n this._groupThreshold = groupThreshold;\n this._groupCount = groupCount;\n this._memberIndex = memberIndex;\n this._memberThreshold = memberThreshold;\n this._value = value;\n }\n\n identifier(): number {\n return this._identifier;\n }\n\n groupIndex(): number {\n return this._groupIndex;\n }\n\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n groupCount(): number {\n return this._groupCount;\n }\n\n memberIndex(): number {\n return this._memberIndex;\n }\n\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n value(): Secret {\n return this._value;\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/encoding.rs\n\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\nimport { SecureRandomNumberGenerator } from \"@bcts/rand\";\nimport { splitSecret, recoverSecret, ShamirError } from \"@bcts/shamir\";\n\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\nimport { Secret } from \"./secret.js\";\nimport type { Spec } from \"./spec.js\";\nimport { SSKRShare } from \"./share.js\";\nimport { METADATA_SIZE_BYTES } from \"./index.js\";\n\n/**\n * Generates SSKR shares for the given Spec and Secret.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerate(spec: Spec, masterSecret: Secret): Uint8Array[][] {\n const rng = new SecureRandomNumberGenerator();\n return sskrGenerateUsing(spec, masterSecret, rng);\n}\n\n/**\n * Generates SSKR shares for the given Spec and Secret using the provided\n * random number generator.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @param randomGenerator - The random number generator to use for generating\n * shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerateUsing(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[][] {\n const groupsShares = generateShares(spec, masterSecret, randomGenerator);\n\n const result: Uint8Array[][] = groupsShares.map((group) => group.map(serializeShare));\n\n return result;\n}\n\n/**\n * Combines the given SSKR shares into a Secret.\n *\n * @param shares - A array of SSKR shares to be combined.\n * @returns The reconstructed Secret.\n * @throws SSKRError if the shares do not meet the necessary quorum of groups\n * and member shares within each group.\n */\nexport function sskrCombine(shares: Uint8Array[]): Secret {\n const sskrShares: SSKRShare[] = [];\n\n for (const share of shares) {\n const sskrShare = deserializeShare(share);\n sskrShares.push(sskrShare);\n }\n\n return combineShares(sskrShares);\n}\n\nfunction serializeShare(share: SSKRShare): Uint8Array {\n // pack the id, group and member data into 5 bytes:\n // 76543210 76543210 76543210\n // 76543210 76543210\n // ----------------====----====----====----\n // identifier: 16\n // group-threshold: 4\n // group-count: 4\n // group-index: 4\n // member-threshold: 4\n // reserved (MUST be zero): 4\n // member-index: 4\n\n const valueData = share.value().getData();\n const result = new Uint8Array(valueData.length + METADATA_SIZE_BYTES);\n\n const id = share.identifier();\n const gt = (share.groupThreshold() - 1) & 0xf;\n const gc = (share.groupCount() - 1) & 0xf;\n const gi = share.groupIndex() & 0xf;\n const mt = (share.memberThreshold() - 1) & 0xf;\n const mi = share.memberIndex() & 0xf;\n\n const id1 = id >> 8;\n const id2 = id & 0xff;\n\n result[0] = id1;\n result[1] = id2;\n result[2] = (gt << 4) | gc;\n result[3] = (gi << 4) | mt;\n result[4] = mi;\n result.set(valueData, METADATA_SIZE_BYTES);\n\n return result;\n}\n\nfunction deserializeShare(source: Uint8Array): SSKRShare {\n if (source.length < METADATA_SIZE_BYTES) {\n throw new SSKRError(SSKRErrorType.ShareLengthInvalid);\n }\n\n const groupThreshold = (source[2] >> 4) + 1;\n const groupCount = (source[2] & 0xf) + 1;\n\n if (groupThreshold > groupCount) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n\n const identifier = (source[0] << 8) | source[1];\n const groupIndex = source[3] >> 4;\n const memberThreshold = (source[3] & 0xf) + 1;\n const reserved = source[4] >> 4;\n if (reserved !== 0) {\n throw new SSKRError(SSKRErrorType.ShareReservedBitsInvalid);\n }\n const memberIndex = source[4] & 0xf;\n const value = Secret.new(source.subarray(METADATA_SIZE_BYTES));\n\n return new SSKRShare(\n identifier,\n groupIndex,\n groupThreshold,\n groupCount,\n memberIndex,\n memberThreshold,\n value,\n );\n}\n\nfunction generateShares(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): SSKRShare[][] {\n // assign a random identifier\n const identifierBytes = new Uint8Array(2);\n randomGenerator.fillRandomData(identifierBytes);\n const identifier = (identifierBytes[0] << 8) | identifierBytes[1];\n\n const groupsShares: SSKRShare[][] = [];\n\n let groupSecrets: Uint8Array[];\n try {\n groupSecrets = splitSecret(\n spec.groupThreshold(),\n spec.groupCount(),\n masterSecret.getData(),\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {\n const group = spec.groups()[groupIndex];\n const groupSecret = groupSecrets[groupIndex];\n\n let memberSecrets: Uint8Array[];\n try {\n memberSecrets = splitSecret(\n group.memberThreshold(),\n group.memberCount(),\n groupSecret,\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n const memberSSKRShares: SSKRShare[] = memberSecrets.map((memberSecret, memberIndex) => {\n const secret = Secret.new(memberSecret);\n return new SSKRShare(\n identifier,\n groupIndex,\n spec.groupThreshold(),\n spec.groupCount(),\n memberIndex,\n group.memberThreshold(),\n secret,\n );\n });\n\n groupsShares.push(memberSSKRShares);\n }\n\n return groupsShares;\n}\n\ninterface Group {\n groupIndex: number;\n memberThreshold: number;\n memberIndexes: number[];\n memberShares: Secret[];\n}\n\nfunction combineShares(shares: SSKRShare[]): Secret {\n let identifier = 0;\n let groupThreshold = 0;\n let groupCount = 0;\n\n if (shares.length === 0) {\n throw new SSKRError(SSKRErrorType.SharesEmpty);\n }\n\n let nextGroup = 0;\n const groups: Group[] = [];\n let secretLen = 0;\n\n for (let i = 0; i < shares.length; i++) {\n const share = shares[i];\n\n if (i === 0) {\n // on the first one, establish expected values for common metadata\n identifier = share.identifier();\n groupCount = share.groupCount();\n groupThreshold = share.groupThreshold();\n secretLen = share.value().len();\n } else {\n // on subsequent shares, check that common metadata matches\n if (\n share.identifier() !== identifier ||\n share.groupThreshold() !== groupThreshold ||\n share.groupCount() !== groupCount ||\n share.value().len() !== secretLen\n ) {\n throw new SSKRError(SSKRErrorType.ShareSetInvalid);\n }\n }\n\n // sort shares into member groups\n let groupFound = false;\n for (const group of groups) {\n if (share.groupIndex() === group.groupIndex) {\n groupFound = true;\n if (share.memberThreshold() !== group.memberThreshold) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n for (const memberIndex of group.memberIndexes) {\n if (share.memberIndex() === memberIndex) {\n throw new SSKRError(SSKRErrorType.DuplicateMemberIndex);\n }\n }\n if (group.memberIndexes.length < group.memberThreshold) {\n group.memberIndexes.push(share.memberIndex());\n group.memberShares.push(share.value().clone());\n }\n }\n }\n\n if (!groupFound) {\n const g: Group = {\n groupIndex: share.groupIndex(),\n memberThreshold: share.memberThreshold(),\n memberIndexes: [share.memberIndex()],\n memberShares: [share.value().clone()],\n };\n groups.push(g);\n nextGroup++;\n }\n }\n\n // Check that we have enough groups to recover the master secret\n if (nextGroup < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Here, all of the shares are unpacked into member groups. Now we go\n // through each group and recover the group secret, and then use the\n // result to recover the master secret\n const masterIndexes: number[] = [];\n const masterShares: Uint8Array[] = [];\n\n for (const group of groups) {\n // Only attempt to recover the group secret if we have enough shares\n if (group.memberIndexes.length < group.memberThreshold) {\n continue;\n }\n\n // Recover the group secret. Mirrors Rust's `if let Ok(group_secret)\n // = recover_secret(...)` (`bc-sskr-rust/src/encoding.rs:289-294`):\n // we silently skip groups whose Shamir recovery fails (the\n // seedtool-cli #6 fix), but **rethrow any non-Shamir error** so\n // unexpected exceptions don't get swallowed — Rust's match-by-Result\n // already enforces that distinction at the type level.\n try {\n const memberSharesData = group.memberShares.map((s) => s.getData());\n const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);\n masterIndexes.push(group.groupIndex);\n masterShares.push(groupSecret);\n } catch (e) {\n if (e instanceof ShamirError) {\n // Equivalent to Rust's \"Err arm\" of `if let Ok` — the seedtool\n // #6 fix demands we keep iterating to other groups.\n continue;\n }\n throw e;\n }\n\n // Stop if we have enough groups to recover the master secret\n if (masterIndexes.length === groupThreshold) {\n break;\n }\n }\n\n // If we don't have enough groups to recover the master secret, return an error\n if (masterIndexes.length < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Recover the master secret\n let masterSecretData: Uint8Array;\n try {\n masterSecretData = recoverSecret(masterIndexes, masterShares);\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n return Secret.new(masterSecretData);\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Blockchain Commons Sharded Secret Key Reconstruction (SSKR)\n// Ported from bc-sskr-rust\n//\n// Sharded Secret Key Reconstruction (SSKR) is a protocol for splitting a\n// secret into a set of shares across one or more groups, such that the\n// secret can be reconstructed from any combination of shares totaling or\n// exceeding a threshold number of shares within each group and across all\n// groups. SSKR is a generalization of Shamir's Secret Sharing (SSS) that\n// allows for multiple groups and multiple thresholds.\n\nimport {\n MIN_SECRET_LEN as SHAMIR_MIN_SECRET_LEN,\n MAX_SECRET_LEN as SHAMIR_MAX_SECRET_LEN,\n MAX_SHARE_COUNT as SHAMIR_MAX_SHARE_COUNT,\n} from \"@bcts/shamir\";\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = SHAMIR_MIN_SECRET_LEN;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = SHAMIR_MAX_SECRET_LEN;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = SHAMIR_MAX_SHARE_COUNT;\n\n/**\n * The maximum number of groups in a split.\n */\nexport const MAX_GROUPS_COUNT = MAX_SHARE_COUNT;\n\n/**\n * The number of bytes used to encode the metadata for a share.\n */\nexport const METADATA_SIZE_BYTES = 5;\n\n/**\n * The minimum number of bytes required to encode a share.\n */\nexport const MIN_SERIALIZE_SIZE_BYTES = METADATA_SIZE_BYTES + MIN_SECRET_LEN;\n\n// Error types\nexport { SSKRError, SSKRErrorType, type SSKRResult } from \"./error.js\";\n\n// Secret\nexport { Secret } from \"./secret.js\";\n\n// Specifications\nexport { GroupSpec, Spec } from \"./spec.js\";\n\n// Encoding/Decoding\nexport { sskrGenerate, sskrGenerateUsing, sskrCombine } from \"./encoding.js\";\n"],"mappings":";;;;;;;AAaA,IAAY,gBAAL,yBAAA,eAAA;AACL,eAAA,0BAAA;AACA,eAAA,sBAAA;AACA,eAAA,uBAAA;AACA,eAAA,2BAAA;AACA,eAAA,wBAAA;AACA,eAAA,4BAAA;AACA,eAAA,qBAAA;AACA,eAAA,yBAAA;AACA,eAAA,mBAAA;AACA,eAAA,oBAAA;AACA,eAAA,wBAAA;AACA,eAAA,8BAAA;AACA,eAAA,iBAAA;AACA,eAAA,qBAAA;AACA,eAAA,iBAAA;;KACD;;;;AAKD,IAAa,YAAb,MAAa,kBAAkB,MAAM;CACnC;CACA;CAEA,YAAY,MAAqB,SAAkB,aAA2B;AAC5E,QAAM,WAAW,UAAU,eAAe,MAAM,YAAY,CAAC;AAC7D,OAAK,OAAO;AACZ,OAAK,cAAc;AACnB,OAAK,OAAO;;CAGd,OAAe,eAAe,MAAqB,aAAmC;AACpF,UAAQ,MAAR;GACE,KAAA,uBACE,QAAO;GACT,KAAA,mBACE,QAAO;GACT,KAAA,oBACE,QAAO;GACT,KAAA,wBACE,QAAO;GACT,KAAA,qBACE,QAAO;GACT,KAAA,yBACE,QAAO;GACT,KAAA,kBACE,QAAO;GACT,KAAA,sBACE,QAAO;GACT,KAAA,gBACE,QAAO;GACT,KAAA,iBACE,QAAO;GACT,KAAA,qBACE,QAAO;GACT,KAAA,2BACE,QAAO;GACT,KAAA,cACE,QAAO;GACT,KAAA,kBACE,QAAO;GACT,KAAA,cACE,QAAO,eAAe,OAClB,sBAAsB,YAAY,YAClC;;;CAIV,OAAO,gBAAgB,OAA+B;AACpD,SAAO,IAAI,UAAA,eAAqC,KAAA,GAAW,MAAM;;;;;;;;;;;;;ACrErE,IAAa,SAAb,MAAa,OAAO;CAClB;CAEA,YAAoB,MAAkB;AACpC,OAAK,OAAO;;;;;;;;;;CAWd,OAAO,IAAI,MAAmC;EAC5C,M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