@bcts/shamir 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.
@@ -0,0 +1,179 @@
1
+ import { RandomNumberGenerator } from "@bcts/rand";
2
+
3
+ //#region src/error.d.ts
4
+ /**
5
+ * Error types for Shamir secret sharing operations.
6
+ */
7
+ declare enum ShamirErrorType {
8
+ SecretTooLong = "SecretTooLong",
9
+ TooManyShares = "TooManyShares",
10
+ InterpolationFailure = "InterpolationFailure",
11
+ ChecksumFailure = "ChecksumFailure",
12
+ SecretTooShort = "SecretTooShort",
13
+ SecretNotEvenLen = "SecretNotEvenLen",
14
+ InvalidThreshold = "InvalidThreshold",
15
+ SharesUnequalLength = "SharesUnequalLength",
16
+ }
17
+ /**
18
+ * Error class for Shamir secret sharing operations.
19
+ */
20
+ declare class ShamirError extends Error {
21
+ readonly type: ShamirErrorType;
22
+ constructor(type: ShamirErrorType, message?: string);
23
+ private static defaultMessage;
24
+ }
25
+ type ShamirResult<T> = T;
26
+ //#endregion
27
+ //#region src/shamir.d.ts
28
+ /**
29
+ * Splits a secret into shares using the Shamir secret sharing algorithm.
30
+ *
31
+ * @param threshold - The minimum number of shares required to reconstruct the
32
+ * secret. Must be greater than or equal to 1 and less than or equal to
33
+ * shareCount.
34
+ * @param shareCount - The total number of shares to generate. Must be at least
35
+ * threshold and less than or equal to MAX_SHARE_COUNT.
36
+ * @param secret - A Uint8Array containing the secret to be split. Must be at
37
+ * least MIN_SECRET_LEN bytes long and at most MAX_SECRET_LEN bytes long.
38
+ * The length must be an even number.
39
+ * @param randomGenerator - An implementation of the RandomNumberGenerator
40
+ * interface, used to generate random data.
41
+ * @returns An array of Uint8Array representing the shares of the secret.
42
+ * @throws ShamirError if parameters are invalid
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * import { splitSecret } from "@bcts/shamir";
47
+ * import { SecureRandomNumberGenerator } from "@bcts/rand";
48
+ *
49
+ * const threshold = 2;
50
+ * const shareCount = 3;
51
+ * const secret = new TextEncoder().encode("my secret belongs to me.");
52
+ * const rng = new SecureRandomNumberGenerator();
53
+ *
54
+ * const shares = splitSecret(threshold, shareCount, secret, rng);
55
+ * console.log(shares.length); // 3
56
+ * ```
57
+ */
58
+ declare function splitSecret(threshold: number, shareCount: number, secret: Uint8Array, randomGenerator: RandomNumberGenerator): Uint8Array[];
59
+ /**
60
+ * Recovers the secret from the given shares using the Shamir secret sharing
61
+ * algorithm.
62
+ *
63
+ * @param indexes - An array of indexes of the shares to be used for recovering
64
+ * the secret. These are the indexes of the shares returned by splitSecret.
65
+ * @param shares - An array of shares of the secret matching the indexes in
66
+ * indexes. These are the shares returned by splitSecret.
67
+ * @returns A Uint8Array representing the recovered secret.
68
+ * @throws ShamirError if parameters are invalid or checksum verification fails
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * import { recoverSecret } from "@bcts/shamir";
73
+ *
74
+ * const indexes = [0, 2];
75
+ * const shares = [
76
+ * new Uint8Array([47, 165, 102, 232, ...]),
77
+ * new Uint8Array([221, 174, 116, 201, ...]),
78
+ * ];
79
+ *
80
+ * const secret = recoverSecret(indexes, shares);
81
+ * console.log(new TextDecoder().decode(secret)); // "my secret belongs to me."
82
+ * ```
83
+ */
84
+ declare function recoverSecret(indexes: number[], shares: Uint8Array[]): Uint8Array;
85
+ //#endregion
86
+ //#region src/hazmat.d.ts
87
+ /**
88
+ * Convert an array of bytes into a bitsliced representation.
89
+ * Takes the first 32 bytes from x and produces 8 u32 values.
90
+ *
91
+ * @param r - Output array of 8 u32 values (bitsliced representation)
92
+ * @param x - Input array of at least 32 bytes
93
+ */
94
+ declare function bitslice(r: Uint32Array, x: Uint8Array): void;
95
+ /**
96
+ * Convert a bitsliced representation back to bytes.
97
+ *
98
+ * @param r - Output array of at least 32 bytes
99
+ * @param x - Input array of 8 u32 values (bitsliced representation)
100
+ */
101
+ declare function unbitslice(r: Uint8Array, x: Uint32Array): void;
102
+ /**
103
+ * Set all 32 positions in a bitsliced array to the same byte value.
104
+ *
105
+ * @param r - Output array of 8 u32 values
106
+ * @param x - Byte value to set in all positions
107
+ */
108
+ declare function bitsliceSetall(r: Uint32Array, x: number): void;
109
+ /**
110
+ * Add (XOR) r with x and store the result in r.
111
+ * In GF(2^8), addition is XOR.
112
+ *
113
+ * @param r - First operand and result
114
+ * @param x - Second operand
115
+ */
116
+ declare function gf256Add(r: Uint32Array, x: Uint32Array): void;
117
+ /**
118
+ * Safely multiply two bitsliced polynomials in GF(2^8) reduced by
119
+ * x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r
120
+ * and b will produce an incorrect result! If you need to square a polynomial
121
+ * use gf256Square instead.
122
+ *
123
+ * @param r - Result array (8 u32 values)
124
+ * @param a - First operand (may overlap with r)
125
+ * @param b - Second operand (must NOT overlap with r)
126
+ */
127
+ declare function gf256Mul(r: Uint32Array, a: Uint32Array, b: Uint32Array): void;
128
+ /**
129
+ * Square x in GF(2^8) and write the result to r.
130
+ * r and x may overlap.
131
+ *
132
+ * @param r - Result array (8 u32 values)
133
+ * @param x - Value to square
134
+ */
135
+ declare function gf256Square(r: Uint32Array, x: Uint32Array): void;
136
+ /**
137
+ * Invert x in GF(2^8) and write the result to r.
138
+ *
139
+ * @param r - Result array (8 u32 values)
140
+ * @param x - Value to invert (will be modified)
141
+ */
142
+ declare function gf256Inv(r: Uint32Array, x: Uint32Array): void;
143
+ //#endregion
144
+ //#region src/interpolate.d.ts
145
+ /**
146
+ * Safely interpolate the polynomial going through
147
+ * the points (x0 [y0_0 y0_1 y0_2 ... y0_31]) , (x1 [y1_0 ...]), ...
148
+ *
149
+ * where
150
+ * xi points to [x0 x1 ... xn-1 ]
151
+ * y contains an array of pointers to 32-bit arrays of y values
152
+ * y contains [y0 y1 y2 ... yn-1]
153
+ * and each of the yi arrays contain [yi_0 yi_i ... yi_31].
154
+ *
155
+ * @param n - Number of points to interpolate
156
+ * @param xi - x coordinates for points (array of length n)
157
+ * @param yl - Length of y coordinate arrays
158
+ * @param yij - Array of n arrays of length yl
159
+ * @param x - Coordinate to interpolate at
160
+ * @returns The interpolated result of length yl
161
+ */
162
+ declare function interpolate(n: number, xi: Uint8Array, yl: number, yij: Uint8Array[], x: number): Uint8Array;
163
+ //#endregion
164
+ //#region src/index.d.ts
165
+ /**
166
+ * The minimum length of a secret.
167
+ */
168
+ declare const MIN_SECRET_LEN = 16;
169
+ /**
170
+ * The maximum length of a secret.
171
+ */
172
+ declare const MAX_SECRET_LEN = 32;
173
+ /**
174
+ * The maximum number of shares that can be generated from a secret.
175
+ */
176
+ declare const MAX_SHARE_COUNT = 16;
177
+ //#endregion
178
+ export { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN, ShamirError, ShamirErrorType, type ShamirResult, bitslice, bitsliceSetall, gf256Add, gf256Inv, gf256Mul, gf256Square, interpolate, recoverSecret, splitSecret, unbitslice };
179
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/error.ts","../src/shamir.ts","../src/hazmat.ts","../src/interpolate.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;AAKY,aAAA,eAAA;EAcC,aAAA,GAAY,eAAA;EACR,aAAA,GAAA,eAAA;EAEG,oBAAA,GAAA,sBAAA;EAHa,eAAA,GAAA,iBAAA;EAAK,cAAA,GAAA,gBAAA;EA+B1B,gBAAY,GAAA,kBAAO;;;;ACU/B;;;AAKG,cD9CU,WAAA,SAAoB,KAAA,CC8C9B;EAAU,SAAA,IAAA,ED7CI,eC6CJ;EAkFG,WAAA,CAAA,IAAa,ED7HT,eC6HqC,EAAA,OAAyB,CAAV,EAAA,MAAU;;;KDjGtE,kBAAkB;;;;;AA7C9B;AAcA;;;;;AA+BA;;;;ACUA;;;;;AAuFA;;;;ACvIA;AAyBA;AAyBA;AAsBA;AAoBA;;;;;AA6GgB,iBDzJA,WAAA,CCyJe,SAAgB,EAAA,MAAA,EAAW,U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ts
4
+ /**
5
+ * Error types for Shamir secret sharing operations.
6
+ */
7
+ declare enum ShamirErrorType {
8
+ SecretTooLong = "SecretTooLong",
9
+ TooManyShares = "TooManyShares",
10
+ InterpolationFailure = "InterpolationFailure",
11
+ ChecksumFailure = "ChecksumFailure",
12
+ SecretTooShort = "SecretTooShort",
13
+ SecretNotEvenLen = "SecretNotEvenLen",
14
+ InvalidThreshold = "InvalidThreshold",
15
+ SharesUnequalLength = "SharesUnequalLength",
16
+ }
17
+ /**
18
+ * Error class for Shamir secret sharing operations.
19
+ */
20
+ declare class ShamirError extends Error {
21
+ readonly type: ShamirErrorType;
22
+ constructor(type: ShamirErrorType, message?: string);
23
+ private static defaultMessage;
24
+ }
25
+ type ShamirResult<T> = T;
26
+ //#endregion
27
+ //#region src/shamir.d.ts
28
+ /**
29
+ * Splits a secret into shares using the Shamir secret sharing algorithm.
30
+ *
31
+ * @param threshold - The minimum number of shares required to reconstruct the
32
+ * secret. Must be greater than or equal to 1 and less than or equal to
33
+ * shareCount.
34
+ * @param shareCount - The total number of shares to generate. Must be at least
35
+ * threshold and less than or equal to MAX_SHARE_COUNT.
36
+ * @param secret - A Uint8Array containing the secret to be split. Must be at
37
+ * least MIN_SECRET_LEN bytes long and at most MAX_SECRET_LEN bytes long.
38
+ * The length must be an even number.
39
+ * @param randomGenerator - An implementation of the RandomNumberGenerator
40
+ * interface, used to generate random data.
41
+ * @returns An array of Uint8Array representing the shares of the secret.
42
+ * @throws ShamirError if parameters are invalid
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * import { splitSecret } from "@bcts/shamir";
47
+ * import { SecureRandomNumberGenerator } from "@bcts/rand";
48
+ *
49
+ * const threshold = 2;
50
+ * const shareCount = 3;
51
+ * const secret = new TextEncoder().encode("my secret belongs to me.");
52
+ * const rng = new SecureRandomNumberGenerator();
53
+ *
54
+ * const shares = splitSecret(threshold, shareCount, secret, rng);
55
+ * console.log(shares.length); // 3
56
+ * ```
57
+ */
58
+ declare function splitSecret(threshold: number, shareCount: number, secret: Uint8Array, randomGenerator: RandomNumberGenerator): Uint8Array[];
59
+ /**
60
+ * Recovers the secret from the given shares using the Shamir secret sharing
61
+ * algorithm.
62
+ *
63
+ * @param indexes - An array of indexes of the shares to be used for recovering
64
+ * the secret. These are the indexes of the shares returned by splitSecret.
65
+ * @param shares - An array of shares of the secret matching the indexes in
66
+ * indexes. These are the shares returned by splitSecret.
67
+ * @returns A Uint8Array representing the recovered secret.
68
+ * @throws ShamirError if parameters are invalid or checksum verification fails
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * import { recoverSecret } from "@bcts/shamir";
73
+ *
74
+ * const indexes = [0, 2];
75
+ * const shares = [
76
+ * new Uint8Array([47, 165, 102, 232, ...]),
77
+ * new Uint8Array([221, 174, 116, 201, ...]),
78
+ * ];
79
+ *
80
+ * const secret = recoverSecret(indexes, shares);
81
+ * console.log(new TextDecoder().decode(secret)); // "my secret belongs to me."
82
+ * ```
83
+ */
84
+ declare function recoverSecret(indexes: number[], shares: Uint8Array[]): Uint8Array;
85
+ //#endregion
86
+ //#region src/hazmat.d.ts
87
+ /**
88
+ * Convert an array of bytes into a bitsliced representation.
89
+ * Takes the first 32 bytes from x and produces 8 u32 values.
90
+ *
91
+ * @param r - Output array of 8 u32 values (bitsliced representation)
92
+ * @param x - Input array of at least 32 bytes
93
+ */
94
+ declare function bitslice(r: Uint32Array, x: Uint8Array): void;
95
+ /**
96
+ * Convert a bitsliced representation back to bytes.
97
+ *
98
+ * @param r - Output array of at least 32 bytes
99
+ * @param x - Input array of 8 u32 values (bitsliced representation)
100
+ */
101
+ declare function unbitslice(r: Uint8Array, x: Uint32Array): void;
102
+ /**
103
+ * Set all 32 positions in a bitsliced array to the same byte value.
104
+ *
105
+ * @param r - Output array of 8 u32 values
106
+ * @param x - Byte value to set in all positions
107
+ */
108
+ declare function bitsliceSetall(r: Uint32Array, x: number): void;
109
+ /**
110
+ * Add (XOR) r with x and store the result in r.
111
+ * In GF(2^8), addition is XOR.
112
+ *
113
+ * @param r - First operand and result
114
+ * @param x - Second operand
115
+ */
116
+ declare function gf256Add(r: Uint32Array, x: Uint32Array): void;
117
+ /**
118
+ * Safely multiply two bitsliced polynomials in GF(2^8) reduced by
119
+ * x^8 + x^4 + x^3 + x + 1. r and a may overlap, but overlapping of r
120
+ * and b will produce an incorrect result! If you need to square a polynomial
121
+ * use gf256Square instead.
122
+ *
123
+ * @param r - Result array (8 u32 values)
124
+ * @param a - First operand (may overlap with r)
125
+ * @param b - Second operand (must NOT overlap with r)
126
+ */
127
+ declare function gf256Mul(r: Uint32Array, a: Uint32Array, b: Uint32Array): void;
128
+ /**
129
+ * Square x in GF(2^8) and write the result to r.
130
+ * r and x may overlap.
131
+ *
132
+ * @param r - Result array (8 u32 values)
133
+ * @param x - Value to square
134
+ */
135
+ declare function gf256Square(r: Uint32Array, x: Uint32Array): void;
136
+ /**
137
+ * Invert x in GF(2^8) and write the result to r.
138
+ *
139
+ * @param r - Result array (8 u32 values)
140
+ * @param x - Value to invert (will be modified)
141
+ */
142
+ declare function gf256Inv(r: Uint32Array, x: Uint32Array): void;
143
+ //#endregion
144
+ //#region src/interpolate.d.ts
145
+ /**
146
+ * Safely interpolate the polynomial going through
147
+ * the points (x0 [y0_0 y0_1 y0_2 ... y0_31]) , (x1 [y1_0 ...]), ...
148
+ *
149
+ * where
150
+ * xi points to [x0 x1 ... xn-1 ]
151
+ * y contains an array of pointers to 32-bit arrays of y values
152
+ * y contains [y0 y1 y2 ... yn-1]
153
+ * and each of the yi arrays contain [yi_0 yi_i ... yi_31].
154
+ *
155
+ * @param n - Number of points to interpolate
156
+ * @param xi - x coordinates for points (array of length n)
157
+ * @param yl - Length of y coordinate arrays
158
+ * @param yij - Array of n arrays of length yl
159
+ * @param x - Coordinate to interpolate at
160
+ * @returns The interpolated result of length yl
161
+ */
162
+ declare function interpolate(n: number, xi: Uint8Array, yl: number, yij: Uint8Array[], x: number): Uint8Array;
163
+ //#endregion
164
+ //#region src/index.d.ts
165
+ /**
166
+ * The minimum length of a secret.
167
+ */
168
+ declare const MIN_SECRET_LEN = 16;
169
+ /**
170
+ * The maximum length of a secret.
171
+ */
172
+ declare const MAX_SECRET_LEN = 32;
173
+ /**
174
+ * The maximum number of shares that can be generated from a secret.
175
+ */
176
+ declare const MAX_SHARE_COUNT = 16;
177
+ //#endregion
178
+ export { MAX_SECRET_LEN, MAX_SHARE_COUNT, MIN_SECRET_LEN, ShamirError, ShamirErrorType, type ShamirResult, bitslice, bitsliceSetall, gf256Add, gf256Inv, gf256Mul, gf256Square, interpolate, recoverSecret, splitSecret, unbitslice };
179
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/error.ts","../src/shamir.ts","../src/hazmat.ts","../src/interpolate.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;AAKY,aAAA,eAAA;EAcC,aAAA,GAAY,eAAA;EACR,aAAA,GAAA,eAAA;EAEG,oBAAA,GAAA,sBAAA;EAHa,eAAA,GAAA,iBAAA;EAAK,cAAA,GAAA,gBAAA;EA+B1B,gBAAY,GAAA,kBAAO;;;;ACU/B;;;AAKG,cD9CU,WAAA,SAAoB,KAAA,CC8C9B;EAAU,SAAA,IAAA,ED7CI,eC6CJ;EAkFG,WAAA,CAAA,IAAa,ED7HT,eC6HqC,EAAA,OAAyB,CAAV,EAAA,MAAU;;;KDjGtE,kBAAkB;;;;;AA7C9B;AAcA;;;;;AA+BA;;;;ACUA;;;;;AAuFA;;;;ACvIA;AAyBA;AAyBA;AAsBA;AAoBA;;;;;AA6GgB,iBDzJA,WAAA,CCyJe,SAAgB,EAAA,MAAA,EAAW,UAAA,EAAA,MAAA,EAAA,MAAA,EDtJhD,UCsJgD,EAAA,eAAA,EDrJvC,qBCqJuC,CAAA,EDpJvD,UCoJuD,EAAA;AA+C1D;;;;AC3JA;;;;;;;;AC3FA;AAKA;AAKA;;;;;;;;;;;iBH2HgB,aAAA,4BA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