@bcts/sskr 1.0.0-alpha.10
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 +48 -0
- package/README.md +15 -0
- package/dist/index.cjs +504 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +240 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +240 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.iife.js +506 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.mjs +490 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +75 -0
- package/src/encoding.ts +326 -0
- package/src/error.ts +82 -0
- package/src/index.ts +60 -0
- package/src/secret.ts +90 -0
- package/src/share.ts +62 -0
- package/src/spec.ts +162 -0
package/src/encoding.ts
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// Ported from bc-sskr-rust/src/encoding.rs
|
|
2
|
+
|
|
3
|
+
import type { RandomNumberGenerator } from "@bcts/rand";
|
|
4
|
+
import { SecureRandomNumberGenerator } from "@bcts/rand";
|
|
5
|
+
import { splitSecret, recoverSecret, ShamirError } from "@bcts/shamir";
|
|
6
|
+
|
|
7
|
+
import { SSKRError, SSKRErrorType } from "./error.js";
|
|
8
|
+
import { Secret } from "./secret.js";
|
|
9
|
+
import type { Spec } from "./spec.js";
|
|
10
|
+
import { SSKRShare } from "./share.js";
|
|
11
|
+
import { METADATA_SIZE_BYTES } from "./index.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Generates SSKR shares for the given Spec and Secret.
|
|
15
|
+
*
|
|
16
|
+
* @param spec - The Spec instance that defines the group and member thresholds.
|
|
17
|
+
* @param masterSecret - The Secret instance to be split into shares.
|
|
18
|
+
* @returns A vector of groups, each containing a vector of shares,
|
|
19
|
+
* each of which is a Uint8Array.
|
|
20
|
+
*/
|
|
21
|
+
export function sskrGenerate(spec: Spec, masterSecret: Secret): Uint8Array[][] {
|
|
22
|
+
const rng = new SecureRandomNumberGenerator();
|
|
23
|
+
return sskrGenerateUsing(spec, masterSecret, rng);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Generates SSKR shares for the given Spec and Secret using the provided
|
|
28
|
+
* random number generator.
|
|
29
|
+
*
|
|
30
|
+
* @param spec - The Spec instance that defines the group and member thresholds.
|
|
31
|
+
* @param masterSecret - The Secret instance to be split into shares.
|
|
32
|
+
* @param randomGenerator - The random number generator to use for generating
|
|
33
|
+
* shares.
|
|
34
|
+
* @returns A vector of groups, each containing a vector of shares,
|
|
35
|
+
* each of which is a Uint8Array.
|
|
36
|
+
*/
|
|
37
|
+
export function sskrGenerateUsing(
|
|
38
|
+
spec: Spec,
|
|
39
|
+
masterSecret: Secret,
|
|
40
|
+
randomGenerator: RandomNumberGenerator,
|
|
41
|
+
): Uint8Array[][] {
|
|
42
|
+
const groupsShares = generateShares(spec, masterSecret, randomGenerator);
|
|
43
|
+
|
|
44
|
+
const result: Uint8Array[][] = groupsShares.map((group) => group.map(serializeShare));
|
|
45
|
+
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Combines the given SSKR shares into a Secret.
|
|
51
|
+
*
|
|
52
|
+
* @param shares - A array of SSKR shares to be combined.
|
|
53
|
+
* @returns The reconstructed Secret.
|
|
54
|
+
* @throws SSKRError if the shares do not meet the necessary quorum of groups
|
|
55
|
+
* and member shares within each group.
|
|
56
|
+
*/
|
|
57
|
+
export function sskrCombine(shares: Uint8Array[]): Secret {
|
|
58
|
+
const sskrShares: SSKRShare[] = [];
|
|
59
|
+
|
|
60
|
+
for (const share of shares) {
|
|
61
|
+
const sskrShare = deserializeShare(share);
|
|
62
|
+
sskrShares.push(sskrShare);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return combineShares(sskrShares);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function serializeShare(share: SSKRShare): Uint8Array {
|
|
69
|
+
// pack the id, group and member data into 5 bytes:
|
|
70
|
+
// 76543210 76543210 76543210
|
|
71
|
+
// 76543210 76543210
|
|
72
|
+
// ----------------====----====----====----
|
|
73
|
+
// identifier: 16
|
|
74
|
+
// group-threshold: 4
|
|
75
|
+
// group-count: 4
|
|
76
|
+
// group-index: 4
|
|
77
|
+
// member-threshold: 4
|
|
78
|
+
// reserved (MUST be zero): 4
|
|
79
|
+
// member-index: 4
|
|
80
|
+
|
|
81
|
+
const valueData = share.value().getData();
|
|
82
|
+
const result = new Uint8Array(valueData.length + METADATA_SIZE_BYTES);
|
|
83
|
+
|
|
84
|
+
const id = share.identifier();
|
|
85
|
+
const gt = (share.groupThreshold() - 1) & 0xf;
|
|
86
|
+
const gc = (share.groupCount() - 1) & 0xf;
|
|
87
|
+
const gi = share.groupIndex() & 0xf;
|
|
88
|
+
const mt = (share.memberThreshold() - 1) & 0xf;
|
|
89
|
+
const mi = share.memberIndex() & 0xf;
|
|
90
|
+
|
|
91
|
+
const id1 = id >> 8;
|
|
92
|
+
const id2 = id & 0xff;
|
|
93
|
+
|
|
94
|
+
result[0] = id1;
|
|
95
|
+
result[1] = id2;
|
|
96
|
+
result[2] = (gt << 4) | gc;
|
|
97
|
+
result[3] = (gi << 4) | mt;
|
|
98
|
+
result[4] = mi;
|
|
99
|
+
result.set(valueData, METADATA_SIZE_BYTES);
|
|
100
|
+
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function deserializeShare(source: Uint8Array): SSKRShare {
|
|
105
|
+
if (source.length < METADATA_SIZE_BYTES) {
|
|
106
|
+
throw new SSKRError(SSKRErrorType.ShareLengthInvalid);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const groupThreshold = (source[2] >> 4) + 1;
|
|
110
|
+
const groupCount = (source[2] & 0xf) + 1;
|
|
111
|
+
|
|
112
|
+
if (groupThreshold > groupCount) {
|
|
113
|
+
throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const identifier = (source[0] << 8) | source[1];
|
|
117
|
+
const groupIndex = source[3] >> 4;
|
|
118
|
+
const memberThreshold = (source[3] & 0xf) + 1;
|
|
119
|
+
const reserved = source[4] >> 4;
|
|
120
|
+
if (reserved !== 0) {
|
|
121
|
+
throw new SSKRError(SSKRErrorType.ShareReservedBitsInvalid);
|
|
122
|
+
}
|
|
123
|
+
const memberIndex = source[4] & 0xf;
|
|
124
|
+
const value = Secret.new(source.subarray(METADATA_SIZE_BYTES));
|
|
125
|
+
|
|
126
|
+
return new SSKRShare(
|
|
127
|
+
identifier,
|
|
128
|
+
groupIndex,
|
|
129
|
+
groupThreshold,
|
|
130
|
+
groupCount,
|
|
131
|
+
memberIndex,
|
|
132
|
+
memberThreshold,
|
|
133
|
+
value,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function generateShares(
|
|
138
|
+
spec: Spec,
|
|
139
|
+
masterSecret: Secret,
|
|
140
|
+
randomGenerator: RandomNumberGenerator,
|
|
141
|
+
): SSKRShare[][] {
|
|
142
|
+
// assign a random identifier
|
|
143
|
+
const identifierBytes = new Uint8Array(2);
|
|
144
|
+
randomGenerator.fillRandomData(identifierBytes);
|
|
145
|
+
const identifier = (identifierBytes[0] << 8) | identifierBytes[1];
|
|
146
|
+
|
|
147
|
+
const groupsShares: SSKRShare[][] = [];
|
|
148
|
+
|
|
149
|
+
let groupSecrets: Uint8Array[];
|
|
150
|
+
try {
|
|
151
|
+
groupSecrets = splitSecret(
|
|
152
|
+
spec.groupThreshold(),
|
|
153
|
+
spec.groupCount(),
|
|
154
|
+
masterSecret.getData(),
|
|
155
|
+
randomGenerator,
|
|
156
|
+
);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
if (e instanceof ShamirError) {
|
|
159
|
+
throw SSKRError.fromShamirError(e);
|
|
160
|
+
}
|
|
161
|
+
throw e;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {
|
|
165
|
+
const group = spec.groups()[groupIndex];
|
|
166
|
+
const groupSecret = groupSecrets[groupIndex];
|
|
167
|
+
|
|
168
|
+
let memberSecrets: Uint8Array[];
|
|
169
|
+
try {
|
|
170
|
+
memberSecrets = splitSecret(
|
|
171
|
+
group.memberThreshold(),
|
|
172
|
+
group.memberCount(),
|
|
173
|
+
groupSecret,
|
|
174
|
+
randomGenerator,
|
|
175
|
+
);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
if (e instanceof ShamirError) {
|
|
178
|
+
throw SSKRError.fromShamirError(e);
|
|
179
|
+
}
|
|
180
|
+
throw e;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const memberSSKRShares: SSKRShare[] = memberSecrets.map((memberSecret, memberIndex) => {
|
|
184
|
+
const secret = Secret.new(memberSecret);
|
|
185
|
+
return new SSKRShare(
|
|
186
|
+
identifier,
|
|
187
|
+
groupIndex,
|
|
188
|
+
spec.groupThreshold(),
|
|
189
|
+
spec.groupCount(),
|
|
190
|
+
memberIndex,
|
|
191
|
+
group.memberThreshold(),
|
|
192
|
+
secret,
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
groupsShares.push(memberSSKRShares);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return groupsShares;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface Group {
|
|
203
|
+
groupIndex: number;
|
|
204
|
+
memberThreshold: number;
|
|
205
|
+
memberIndexes: number[];
|
|
206
|
+
memberShares: Secret[];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function combineShares(shares: SSKRShare[]): Secret {
|
|
210
|
+
let identifier = 0;
|
|
211
|
+
let groupThreshold = 0;
|
|
212
|
+
let groupCount = 0;
|
|
213
|
+
|
|
214
|
+
if (shares.length === 0) {
|
|
215
|
+
throw new SSKRError(SSKRErrorType.SharesEmpty);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let nextGroup = 0;
|
|
219
|
+
const groups: Group[] = [];
|
|
220
|
+
let secretLen = 0;
|
|
221
|
+
|
|
222
|
+
for (let i = 0; i < shares.length; i++) {
|
|
223
|
+
const share = shares[i];
|
|
224
|
+
|
|
225
|
+
if (i === 0) {
|
|
226
|
+
// on the first one, establish expected values for common metadata
|
|
227
|
+
identifier = share.identifier();
|
|
228
|
+
groupCount = share.groupCount();
|
|
229
|
+
groupThreshold = share.groupThreshold();
|
|
230
|
+
secretLen = share.value().len();
|
|
231
|
+
} else {
|
|
232
|
+
// on subsequent shares, check that common metadata matches
|
|
233
|
+
if (
|
|
234
|
+
share.identifier() !== identifier ||
|
|
235
|
+
share.groupThreshold() !== groupThreshold ||
|
|
236
|
+
share.groupCount() !== groupCount ||
|
|
237
|
+
share.value().len() !== secretLen
|
|
238
|
+
) {
|
|
239
|
+
throw new SSKRError(SSKRErrorType.ShareSetInvalid);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// sort shares into member groups
|
|
244
|
+
let groupFound = false;
|
|
245
|
+
for (const group of groups) {
|
|
246
|
+
if (share.groupIndex() === group.groupIndex) {
|
|
247
|
+
groupFound = true;
|
|
248
|
+
if (share.memberThreshold() !== group.memberThreshold) {
|
|
249
|
+
throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);
|
|
250
|
+
}
|
|
251
|
+
for (const memberIndex of group.memberIndexes) {
|
|
252
|
+
if (share.memberIndex() === memberIndex) {
|
|
253
|
+
throw new SSKRError(SSKRErrorType.DuplicateMemberIndex);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (group.memberIndexes.length < group.memberThreshold) {
|
|
257
|
+
group.memberIndexes.push(share.memberIndex());
|
|
258
|
+
group.memberShares.push(share.value().clone());
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (!groupFound) {
|
|
264
|
+
const g: Group = {
|
|
265
|
+
groupIndex: share.groupIndex(),
|
|
266
|
+
memberThreshold: share.memberThreshold(),
|
|
267
|
+
memberIndexes: [share.memberIndex()],
|
|
268
|
+
memberShares: [share.value().clone()],
|
|
269
|
+
};
|
|
270
|
+
groups.push(g);
|
|
271
|
+
nextGroup++;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Check that we have enough groups to recover the master secret
|
|
276
|
+
if (nextGroup < groupThreshold) {
|
|
277
|
+
throw new SSKRError(SSKRErrorType.NotEnoughGroups);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Here, all of the shares are unpacked into member groups. Now we go
|
|
281
|
+
// through each group and recover the group secret, and then use the
|
|
282
|
+
// result to recover the master secret
|
|
283
|
+
const masterIndexes: number[] = [];
|
|
284
|
+
const masterShares: Uint8Array[] = [];
|
|
285
|
+
|
|
286
|
+
for (const group of groups) {
|
|
287
|
+
// Only attempt to recover the group secret if we have enough shares
|
|
288
|
+
if (group.memberIndexes.length < group.memberThreshold) {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Recover the group secret
|
|
293
|
+
try {
|
|
294
|
+
const memberSharesData = group.memberShares.map((s) => s.getData());
|
|
295
|
+
const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);
|
|
296
|
+
masterIndexes.push(group.groupIndex);
|
|
297
|
+
masterShares.push(groupSecret);
|
|
298
|
+
} catch {
|
|
299
|
+
// If we can't recover this group, just skip it
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Stop if we have enough groups to recover the master secret
|
|
304
|
+
if (masterIndexes.length === groupThreshold) {
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// If we don't have enough groups to recover the master secret, return an error
|
|
310
|
+
if (masterIndexes.length < groupThreshold) {
|
|
311
|
+
throw new SSKRError(SSKRErrorType.NotEnoughGroups);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Recover the master secret
|
|
315
|
+
let masterSecretData: Uint8Array;
|
|
316
|
+
try {
|
|
317
|
+
masterSecretData = recoverSecret(masterIndexes, masterShares);
|
|
318
|
+
} catch (e) {
|
|
319
|
+
if (e instanceof ShamirError) {
|
|
320
|
+
throw SSKRError.fromShamirError(e);
|
|
321
|
+
}
|
|
322
|
+
throw e;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return Secret.new(masterSecretData);
|
|
326
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Ported from bc-sskr-rust/src/error.rs
|
|
2
|
+
|
|
3
|
+
import type { ShamirError } from "@bcts/shamir";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Error types for SSKR operations.
|
|
7
|
+
*/
|
|
8
|
+
export enum SSKRErrorType {
|
|
9
|
+
DuplicateMemberIndex = "DuplicateMemberIndex",
|
|
10
|
+
GroupSpecInvalid = "GroupSpecInvalid",
|
|
11
|
+
GroupCountInvalid = "GroupCountInvalid",
|
|
12
|
+
GroupThresholdInvalid = "GroupThresholdInvalid",
|
|
13
|
+
MemberCountInvalid = "MemberCountInvalid",
|
|
14
|
+
MemberThresholdInvalid = "MemberThresholdInvalid",
|
|
15
|
+
NotEnoughGroups = "NotEnoughGroups",
|
|
16
|
+
SecretLengthNotEven = "SecretLengthNotEven",
|
|
17
|
+
SecretTooLong = "SecretTooLong",
|
|
18
|
+
SecretTooShort = "SecretTooShort",
|
|
19
|
+
ShareLengthInvalid = "ShareLengthInvalid",
|
|
20
|
+
ShareReservedBitsInvalid = "ShareReservedBitsInvalid",
|
|
21
|
+
SharesEmpty = "SharesEmpty",
|
|
22
|
+
ShareSetInvalid = "ShareSetInvalid",
|
|
23
|
+
ShamirError = "ShamirError",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Error class for SSKR operations.
|
|
28
|
+
*/
|
|
29
|
+
export class SSKRError extends Error {
|
|
30
|
+
readonly type: SSKRErrorType;
|
|
31
|
+
readonly shamirError?: ShamirError | undefined;
|
|
32
|
+
|
|
33
|
+
constructor(type: SSKRErrorType, message?: string, shamirError?: ShamirError) {
|
|
34
|
+
super(message ?? SSKRError.defaultMessage(type, shamirError));
|
|
35
|
+
this.type = type;
|
|
36
|
+
this.shamirError = shamirError;
|
|
37
|
+
this.name = "SSKRError";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private static defaultMessage(type: SSKRErrorType, shamirError?: ShamirError): string {
|
|
41
|
+
switch (type) {
|
|
42
|
+
case SSKRErrorType.DuplicateMemberIndex:
|
|
43
|
+
return "When combining shares, the provided shares contained a duplicate member index";
|
|
44
|
+
case SSKRErrorType.GroupSpecInvalid:
|
|
45
|
+
return "Invalid group specification";
|
|
46
|
+
case SSKRErrorType.GroupCountInvalid:
|
|
47
|
+
return "When creating a split spec, the group count is invalid";
|
|
48
|
+
case SSKRErrorType.GroupThresholdInvalid:
|
|
49
|
+
return "SSKR group threshold is invalid";
|
|
50
|
+
case SSKRErrorType.MemberCountInvalid:
|
|
51
|
+
return "SSKR member count is invalid";
|
|
52
|
+
case SSKRErrorType.MemberThresholdInvalid:
|
|
53
|
+
return "SSKR member threshold is invalid";
|
|
54
|
+
case SSKRErrorType.NotEnoughGroups:
|
|
55
|
+
return "SSKR shares did not contain enough groups";
|
|
56
|
+
case SSKRErrorType.SecretLengthNotEven:
|
|
57
|
+
return "SSKR secret is not of even length";
|
|
58
|
+
case SSKRErrorType.SecretTooLong:
|
|
59
|
+
return "SSKR secret is too long";
|
|
60
|
+
case SSKRErrorType.SecretTooShort:
|
|
61
|
+
return "SSKR secret is too short";
|
|
62
|
+
case SSKRErrorType.ShareLengthInvalid:
|
|
63
|
+
return "SSKR shares did not contain enough serialized bytes";
|
|
64
|
+
case SSKRErrorType.ShareReservedBitsInvalid:
|
|
65
|
+
return "SSKR shares contained invalid reserved bits";
|
|
66
|
+
case SSKRErrorType.SharesEmpty:
|
|
67
|
+
return "SSKR shares were empty";
|
|
68
|
+
case SSKRErrorType.ShareSetInvalid:
|
|
69
|
+
return "SSKR shares were invalid";
|
|
70
|
+
case SSKRErrorType.ShamirError:
|
|
71
|
+
return shamirError != null
|
|
72
|
+
? `SSKR Shamir error: ${shamirError.message}`
|
|
73
|
+
: "SSKR Shamir error";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static fromShamirError(error: ShamirError): SSKRError {
|
|
78
|
+
return new SSKRError(SSKRErrorType.ShamirError, undefined, error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type SSKRResult<T> = T;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Blockchain Commons Sharded Secret Key Reconstruction (SSKR)
|
|
2
|
+
// Ported from bc-sskr-rust
|
|
3
|
+
//
|
|
4
|
+
// Sharded Secret Key Reconstruction (SSKR) is a protocol for splitting a
|
|
5
|
+
// secret into a set of shares across one or more groups, such that the
|
|
6
|
+
// secret can be reconstructed from any combination of shares totaling or
|
|
7
|
+
// exceeding a threshold number of shares within each group and across all
|
|
8
|
+
// groups. SSKR is a generalization of Shamir's Secret Sharing (SSS) that
|
|
9
|
+
// allows for multiple groups and multiple thresholds.
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
MIN_SECRET_LEN as SHAMIR_MIN_SECRET_LEN,
|
|
13
|
+
MAX_SECRET_LEN as SHAMIR_MAX_SECRET_LEN,
|
|
14
|
+
MAX_SHARE_COUNT as SHAMIR_MAX_SHARE_COUNT,
|
|
15
|
+
} from "@bcts/shamir";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The minimum length of a secret.
|
|
19
|
+
*/
|
|
20
|
+
export const MIN_SECRET_LEN = SHAMIR_MIN_SECRET_LEN;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The maximum length of a secret.
|
|
24
|
+
*/
|
|
25
|
+
export const MAX_SECRET_LEN = SHAMIR_MAX_SECRET_LEN;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The maximum number of shares that can be generated from a secret.
|
|
29
|
+
*/
|
|
30
|
+
export const MAX_SHARE_COUNT = SHAMIR_MAX_SHARE_COUNT;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The maximum number of groups in a split.
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_GROUPS_COUNT = MAX_SHARE_COUNT;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The number of bytes used to encode the metadata for a share.
|
|
39
|
+
*/
|
|
40
|
+
export const METADATA_SIZE_BYTES = 5;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The minimum number of bytes required to encode a share.
|
|
44
|
+
*/
|
|
45
|
+
export const MIN_SERIALIZE_SIZE_BYTES = METADATA_SIZE_BYTES + MIN_SECRET_LEN;
|
|
46
|
+
|
|
47
|
+
// Error types
|
|
48
|
+
export { SSKRError, SSKRErrorType, type SSKRResult } from "./error.js";
|
|
49
|
+
|
|
50
|
+
// Secret
|
|
51
|
+
export { Secret } from "./secret.js";
|
|
52
|
+
|
|
53
|
+
// Specifications
|
|
54
|
+
export { GroupSpec, Spec } from "./spec.js";
|
|
55
|
+
|
|
56
|
+
// Share
|
|
57
|
+
export { SSKRShare } from "./share.js";
|
|
58
|
+
|
|
59
|
+
// Encoding/Decoding
|
|
60
|
+
export { sskrGenerate, sskrGenerateUsing, sskrCombine } from "./encoding.js";
|
package/src/secret.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Ported from bc-sskr-rust/src/secret.rs
|
|
2
|
+
|
|
3
|
+
import { MIN_SECRET_LEN, MAX_SECRET_LEN } from "./index.js";
|
|
4
|
+
import { SSKRError, SSKRErrorType } from "./error.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A secret to be split into shares.
|
|
8
|
+
*/
|
|
9
|
+
export class Secret {
|
|
10
|
+
private readonly data: Uint8Array;
|
|
11
|
+
|
|
12
|
+
private constructor(data: Uint8Array) {
|
|
13
|
+
this.data = data;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new Secret instance with the given data.
|
|
18
|
+
*
|
|
19
|
+
* @param data - The secret data to be split into shares.
|
|
20
|
+
* @returns A new Secret instance.
|
|
21
|
+
* @throws SSKRError if the length of the secret is less than
|
|
22
|
+
* MIN_SECRET_LEN, greater than MAX_SECRET_LEN, or not even.
|
|
23
|
+
*/
|
|
24
|
+
static new(data: Uint8Array | string): Secret {
|
|
25
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
26
|
+
const len = bytes.length;
|
|
27
|
+
|
|
28
|
+
if (len < MIN_SECRET_LEN) {
|
|
29
|
+
throw new SSKRError(SSKRErrorType.SecretTooShort);
|
|
30
|
+
}
|
|
31
|
+
if (len > MAX_SECRET_LEN) {
|
|
32
|
+
throw new SSKRError(SSKRErrorType.SecretTooLong);
|
|
33
|
+
}
|
|
34
|
+
if ((len & 1) !== 0) {
|
|
35
|
+
throw new SSKRError(SSKRErrorType.SecretLengthNotEven);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return new Secret(new Uint8Array(bytes));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the length of the secret.
|
|
43
|
+
*/
|
|
44
|
+
len(): number {
|
|
45
|
+
return this.data.length;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns true if the secret is empty.
|
|
50
|
+
*/
|
|
51
|
+
isEmpty(): boolean {
|
|
52
|
+
return this.len() === 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns a reference to the secret data.
|
|
57
|
+
*/
|
|
58
|
+
getData(): Uint8Array {
|
|
59
|
+
return this.data;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the secret data as a Uint8Array.
|
|
64
|
+
*/
|
|
65
|
+
asRef(): Uint8Array {
|
|
66
|
+
return this.data;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check equality with another Secret.
|
|
71
|
+
*/
|
|
72
|
+
equals(other: Secret): boolean {
|
|
73
|
+
if (this.data.length !== other.data.length) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
for (let i = 0; i < this.data.length; i++) {
|
|
77
|
+
if (this.data[i] !== other.data[i]) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Clone the secret.
|
|
86
|
+
*/
|
|
87
|
+
clone(): Secret {
|
|
88
|
+
return new Secret(new Uint8Array(this.data));
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/share.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Ported from bc-sskr-rust/src/share.rs
|
|
2
|
+
|
|
3
|
+
import type { Secret } from "./secret.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A share in the SSKR scheme.
|
|
7
|
+
*/
|
|
8
|
+
export class SSKRShare {
|
|
9
|
+
private readonly _identifier: number;
|
|
10
|
+
private readonly _groupIndex: number;
|
|
11
|
+
private readonly _groupThreshold: number;
|
|
12
|
+
private readonly _groupCount: number;
|
|
13
|
+
private readonly _memberIndex: number;
|
|
14
|
+
private readonly _memberThreshold: number;
|
|
15
|
+
private readonly _value: Secret;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
identifier: number,
|
|
19
|
+
groupIndex: number,
|
|
20
|
+
groupThreshold: number,
|
|
21
|
+
groupCount: number,
|
|
22
|
+
memberIndex: number,
|
|
23
|
+
memberThreshold: number,
|
|
24
|
+
value: Secret,
|
|
25
|
+
) {
|
|
26
|
+
this._identifier = identifier;
|
|
27
|
+
this._groupIndex = groupIndex;
|
|
28
|
+
this._groupThreshold = groupThreshold;
|
|
29
|
+
this._groupCount = groupCount;
|
|
30
|
+
this._memberIndex = memberIndex;
|
|
31
|
+
this._memberThreshold = memberThreshold;
|
|
32
|
+
this._value = value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
identifier(): number {
|
|
36
|
+
return this._identifier;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
groupIndex(): number {
|
|
40
|
+
return this._groupIndex;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
groupThreshold(): number {
|
|
44
|
+
return this._groupThreshold;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
groupCount(): number {
|
|
48
|
+
return this._groupCount;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
memberIndex(): number {
|
|
52
|
+
return this._memberIndex;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
memberThreshold(): number {
|
|
56
|
+
return this._memberThreshold;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
value(): Secret {
|
|
60
|
+
return this._value;
|
|
61
|
+
}
|
|
62
|
+
}
|