@flickfyi/flkr 0.0.0 → 0.0.106
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/node_modules/@flickfyi/core/dist/index.js +2 -2
- package/node_modules/@flickfyi/http-message-sig/dist/index.js +11 -11
- package/node_modules/@flickfyi/identity/dist/{base-B-urclbM.js → base-B1Vicu5t.js} +1 -1
- package/node_modules/@flickfyi/identity/dist/biscuit.js +1 -1
- package/node_modules/@flickfyi/identity/dist/{curve-CpcHwjN8.js → curve-BVH9bh0Y.js} +7 -7
- package/node_modules/@flickfyi/identity/dist/{cwt-DySP8lzM.js → cwt-CfubTfiF.js} +25 -25
- package/node_modules/@flickfyi/identity/dist/cwt.js +1 -1
- package/node_modules/@flickfyi/identity/dist/index.js +3 -3
- package/node_modules/@flickfyi/identity/dist/{nist-DAZq4YJ7.js → nist-C8Poq91M.js} +4 -4
- package/node_modules/@flickfyi/identity/dist/signing.js +2 -2
- package/node_modules/@flickfyi/lens/dist/index.js +1 -1
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/README.md +1 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.abi115.glibc.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.abi115.musl.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.napi.glibc.node +0 -0
- package/node_modules/@msgpackr-extract/msgpackr-extract-linux-x64/node.napi.musl.node +0 -0
- package/node_modules/@msgpackr-extract/{msgpackr-extract-darwin-arm64 → msgpackr-extract-linux-x64}/package.json +4 -4
- package/package.json +1 -1
- package/node_modules/@flickfyi/identity/dist/base-ClGB6hXM.js +0 -354
- package/node_modules/@flickfyi/identity/dist/curve-DALK-Fi3.js +0 -2768
- package/node_modules/@flickfyi/identity/dist/cwt-tfd4xMR_.js +0 -3397
- package/node_modules/@flickfyi/identity/dist/nist-luaoKQ1s.js +0 -1194
- package/node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64/README.md +0 -1
- package/node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64/node.napi.glibc.node +0 -0
- /package/node_modules/@msgpackr-extract/{msgpackr-extract-darwin-arm64 → msgpackr-extract-linux-x64}/index.js +0 -0
|
@@ -1,2768 +0,0 @@
|
|
|
1
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/hashes/2.4.0/a02ec2573a7b07204dfaf0508ab31b11f6a9bd74c18f15e8ab2933e00265fdfc/node_modules/@noble/hashes/_u64.js
|
|
2
|
-
var U32_MASK64 = /* @__PURE__ */ (() => BigInt(2 ** 32 - 1))();
|
|
3
|
-
var _32n = /* @__PURE__ */ BigInt(32);
|
|
4
|
-
function fromBig(n, le = false) {
|
|
5
|
-
if (le) return {
|
|
6
|
-
h: Number(n & U32_MASK64),
|
|
7
|
-
l: Number(n >> _32n & U32_MASK64)
|
|
8
|
-
};
|
|
9
|
-
return {
|
|
10
|
-
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
11
|
-
l: Number(n & U32_MASK64) | 0
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
function split(lst, le = false) {
|
|
15
|
-
const len = lst.length;
|
|
16
|
-
let Ah = new Uint32Array(len);
|
|
17
|
-
let Al = new Uint32Array(len);
|
|
18
|
-
for (let i = 0; i < len; i++) {
|
|
19
|
-
const { h, l } = fromBig(lst[i], le);
|
|
20
|
-
[Ah[i], Al[i]] = [h, l];
|
|
21
|
-
}
|
|
22
|
-
return [Ah, Al];
|
|
23
|
-
}
|
|
24
|
-
var fromNumH = (n) => n / 2 ** 32 | 0;
|
|
25
|
-
var fromNumL = (n) => n >>> 0;
|
|
26
|
-
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
27
|
-
const h = fromNumH(n);
|
|
28
|
-
const l = fromNumL(n);
|
|
29
|
-
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
30
|
-
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
31
|
-
}
|
|
32
|
-
var shrSH = (h, _l, s) => h >>> s;
|
|
33
|
-
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
34
|
-
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
35
|
-
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
36
|
-
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
37
|
-
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
38
|
-
function add(Ah, Al, Bh, Bl) {
|
|
39
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
40
|
-
return {
|
|
41
|
-
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
42
|
-
l: l | 0
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
46
|
-
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
47
|
-
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
48
|
-
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
49
|
-
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
50
|
-
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
51
|
-
//#endregion
|
|
52
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/hashes/2.4.0/a02ec2573a7b07204dfaf0508ab31b11f6a9bd74c18f15e8ab2933e00265fdfc/node_modules/@noble/hashes/utils.js
|
|
53
|
-
/**
|
|
54
|
-
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
55
|
-
* @param a - value to test
|
|
56
|
-
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
57
|
-
* @example
|
|
58
|
-
* Check whether a value is a Uint8Array-compatible view.
|
|
59
|
-
* ```ts
|
|
60
|
-
* isBytes(new Uint8Array([1, 2, 3]));
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
function isBytes$1(a) {
|
|
64
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
65
|
-
}
|
|
66
|
-
var atitle$1 = (title) => title ? `"${title}" ` : "";
|
|
67
|
-
/**
|
|
68
|
-
* Asserts something is a non-negative integer.
|
|
69
|
-
* @param n - number to validate
|
|
70
|
-
* @param title - label included in thrown errors
|
|
71
|
-
* @returns The validated number.
|
|
72
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
73
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
74
|
-
* @example
|
|
75
|
-
* Validate a non-negative integer option.
|
|
76
|
-
* ```ts
|
|
77
|
-
* anumber(32, 'length');
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
function anumber$1(n, title = "") {
|
|
81
|
-
if (typeof n !== "number") throw new TypeError(atitle$1(title) + "expected number, got " + typeof n);
|
|
82
|
-
if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(atitle$1(title) + "expected integer >= 0, got " + n);
|
|
83
|
-
return n;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Asserts something is Uint8Array.
|
|
87
|
-
* @param value - value to validate
|
|
88
|
-
* @param length - optional exact length constraint
|
|
89
|
-
* @param title - label included in thrown errors
|
|
90
|
-
* @returns The validated byte array.
|
|
91
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
92
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
93
|
-
* @example
|
|
94
|
-
* Validate that a value is a byte array.
|
|
95
|
-
* ```ts
|
|
96
|
-
* abytes(new Uint8Array([1, 2, 3]));
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
function abytes$1(value, length, title = "") {
|
|
100
|
-
if (isBytes$1(value) && (length === void 0 || value.length === length)) return value;
|
|
101
|
-
if (length !== void 0) anumber$1(length, "length");
|
|
102
|
-
const bytes = isBytes$1(value);
|
|
103
|
-
const ofLen = length !== void 0 ? ` of length ${length}` : "";
|
|
104
|
-
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
105
|
-
const message = atitle$1(title) + "expected Uint8Array" + ofLen + ", got " + got;
|
|
106
|
-
if (!bytes) throw new TypeError(message);
|
|
107
|
-
throw new RangeError(message);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Asserts something is a wrapped hash constructor.
|
|
111
|
-
* @param h - hash constructor to validate
|
|
112
|
-
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
113
|
-
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
114
|
-
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
115
|
-
* @example
|
|
116
|
-
* Validate a callable hash wrapper.
|
|
117
|
-
* ```ts
|
|
118
|
-
* import { ahash } from '@noble/hashes/utils.js';
|
|
119
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
120
|
-
* ahash(sha256);
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
function ahash(h) {
|
|
124
|
-
if (typeof h !== "function" || typeof h.create !== "function") throw new TypeError("expected hash wrapped by utils.createHasher");
|
|
125
|
-
anumber$1(h.outputLen);
|
|
126
|
-
anumber$1(h.blockLen);
|
|
127
|
-
if (h.outputLen < 1 || h.blockLen < 1) throw new Error("hash blockLen / outputLen must be >= 1");
|
|
128
|
-
}
|
|
129
|
-
var aobject$1 = (value, label) => {
|
|
130
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
|
|
131
|
-
};
|
|
132
|
-
var aopts = (value, label) => {
|
|
133
|
-
aobject$1(value, label);
|
|
134
|
-
const proto = Object.getPrototypeOf(value);
|
|
135
|
-
if (proto !== Object.prototype && proto !== null) throw new TypeError(`"${label}" expected plain object`);
|
|
136
|
-
if (Object.hasOwn(value, "__proto__")) throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
137
|
-
};
|
|
138
|
-
/**
|
|
139
|
-
* Asserts a hash instance has not been destroyed or finished.
|
|
140
|
-
* @param instance - hash instance to validate
|
|
141
|
-
* @param checkFinished - whether to reject finalized instances
|
|
142
|
-
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
143
|
-
* @example
|
|
144
|
-
* Validate that a hash instance is still usable.
|
|
145
|
-
* ```ts
|
|
146
|
-
* import { aexists } from '@noble/hashes/utils.js';
|
|
147
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
148
|
-
* const hash = sha256.create();
|
|
149
|
-
* aexists(hash);
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
function aexists(instance, checkFinished = true) {
|
|
153
|
-
if (instance.destroyed) throw new Error("hash was destroyed");
|
|
154
|
-
if (checkFinished && instance.finished) throw new Error("digest() was already called");
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Asserts output is a sufficiently-sized byte array.
|
|
158
|
-
* @param out - destination buffer
|
|
159
|
-
* @param instance - hash instance providing output length
|
|
160
|
-
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
161
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
162
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
163
|
-
* @example
|
|
164
|
-
* Validate a caller-provided digest buffer.
|
|
165
|
-
* ```ts
|
|
166
|
-
* import { aoutput } from '@noble/hashes/utils.js';
|
|
167
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
168
|
-
* const hash = sha256.create();
|
|
169
|
-
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
function aoutput(out, instance) {
|
|
173
|
-
abytes$1(out, void 0, "output");
|
|
174
|
-
const min = instance.outputLen;
|
|
175
|
-
if (!(out.length >= min)) throw new RangeError("\"output\" expected length >= " + min);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
179
|
-
* @param arrays - arrays to overwrite with zeros
|
|
180
|
-
* @example
|
|
181
|
-
* Zeroize sensitive buffers in place.
|
|
182
|
-
* ```ts
|
|
183
|
-
* clean(new Uint8Array([1, 2, 3]));
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
function clean(...arrays) {
|
|
187
|
-
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Creates a DataView for byte-level manipulation.
|
|
191
|
-
* @param arr - source typed array
|
|
192
|
-
* @returns DataView over the same buffer region.
|
|
193
|
-
* @example
|
|
194
|
-
* Create a DataView over an existing buffer.
|
|
195
|
-
* ```ts
|
|
196
|
-
* createView(new Uint8Array(4));
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
function createView(arr) {
|
|
200
|
-
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Rotate-right operation for uint32 values.
|
|
204
|
-
* @param word - source word
|
|
205
|
-
* @param shift - shift amount in bits
|
|
206
|
-
* @returns Rotated word.
|
|
207
|
-
* @example
|
|
208
|
-
* Rotate a 32-bit word to the right.
|
|
209
|
-
* ```ts
|
|
210
|
-
* rotr(0x12345678, 8);
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
function rotr(word, shift) {
|
|
214
|
-
return word << 32 - shift | word >>> shift;
|
|
215
|
-
}
|
|
216
|
-
var hasHexBuiltin = /* @__PURE__ */ (() => typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function")();
|
|
217
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
218
|
-
/**
|
|
219
|
-
* Convert byte array to hex string.
|
|
220
|
-
* Uses the built-in function when available and assumes it matches the tested
|
|
221
|
-
* fallback semantics.
|
|
222
|
-
* @param bytes - bytes to encode
|
|
223
|
-
* @returns Lowercase hexadecimal string.
|
|
224
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
225
|
-
* @example
|
|
226
|
-
* Convert bytes to lowercase hexadecimal.
|
|
227
|
-
* ```ts
|
|
228
|
-
* bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])); // 'cafe0123'
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
function bytesToHex$1(bytes) {
|
|
232
|
-
abytes$1(bytes);
|
|
233
|
-
if (hasHexBuiltin) return bytes.toHex();
|
|
234
|
-
let hex = "";
|
|
235
|
-
for (let i = 0; i < bytes.length; i++) hex += hexes[bytes[i]];
|
|
236
|
-
return hex;
|
|
237
|
-
}
|
|
238
|
-
function asciiToBase16(ch) {
|
|
239
|
-
return ch >= 48 && ch <= 57 ? ch - 48 : ch >= 65 && ch <= 70 ? ch - 55 : ch >= 97 && ch <= 102 ? ch - 87 : void 0;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Convert hex string to byte array. Uses built-in function, when available.
|
|
243
|
-
* @param hex - hexadecimal string to decode
|
|
244
|
-
* @returns Decoded bytes.
|
|
245
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
246
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
247
|
-
* @example
|
|
248
|
-
* Decode lowercase hexadecimal into bytes.
|
|
249
|
-
* ```ts
|
|
250
|
-
* hexToBytes('cafe0123'); // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
251
|
-
* ```
|
|
252
|
-
*/
|
|
253
|
-
function hexToBytes$1(hex) {
|
|
254
|
-
if (typeof hex !== "string") throw new TypeError("hex string expected, got " + typeof hex);
|
|
255
|
-
if (hasHexBuiltin) try {
|
|
256
|
-
return Uint8Array.fromHex(hex);
|
|
257
|
-
} catch (error) {
|
|
258
|
-
if (error instanceof SyntaxError) throw new RangeError(error.message);
|
|
259
|
-
throw error;
|
|
260
|
-
}
|
|
261
|
-
const hl = hex.length;
|
|
262
|
-
const al = hl / 2;
|
|
263
|
-
if (hl % 2) throw new RangeError("hex string expected, got unpadded hex of length " + hl);
|
|
264
|
-
const array = new Uint8Array(al);
|
|
265
|
-
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
266
|
-
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
267
|
-
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
268
|
-
if (n1 === void 0 || n2 === void 0) {
|
|
269
|
-
const char = hex[hi] + hex[hi + 1];
|
|
270
|
-
throw new RangeError("hex string expected, got non-hex character \"" + char + "\" at index " + hi);
|
|
271
|
-
}
|
|
272
|
-
array[ai] = n1 * 16 + n2;
|
|
273
|
-
}
|
|
274
|
-
return array;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Copies several Uint8Arrays into one.
|
|
278
|
-
* @param arrays - arrays to concatenate
|
|
279
|
-
* @returns Concatenated byte array.
|
|
280
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
281
|
-
* @example
|
|
282
|
-
* Concatenate multiple byte arrays.
|
|
283
|
-
* ```ts
|
|
284
|
-
* concatBytes(new Uint8Array([1]), new Uint8Array([2]));
|
|
285
|
-
* ```
|
|
286
|
-
*/
|
|
287
|
-
function concatBytes$1(...arrays) {
|
|
288
|
-
let sum = 0;
|
|
289
|
-
for (let i = 0; i < arrays.length; i++) {
|
|
290
|
-
const a = arrays[i];
|
|
291
|
-
abytes$1(a);
|
|
292
|
-
sum += a.length;
|
|
293
|
-
}
|
|
294
|
-
const res = new Uint8Array(sum);
|
|
295
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
296
|
-
const a = arrays[i];
|
|
297
|
-
res.set(a, pad);
|
|
298
|
-
pad += a.length;
|
|
299
|
-
}
|
|
300
|
-
return res;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Merges default options and passed options.
|
|
304
|
-
* @param defaults - base option object
|
|
305
|
-
* @param opts - user overrides
|
|
306
|
-
* @param title - label included in thrown override errors
|
|
307
|
-
* @returns Fresh merged option object with a null prototype.
|
|
308
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
309
|
-
* @example
|
|
310
|
-
* Merge user overrides onto default options.
|
|
311
|
-
* ```ts
|
|
312
|
-
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
313
|
-
* ```
|
|
314
|
-
*/
|
|
315
|
-
function checkOpts(defaults, opts, title = "opts") {
|
|
316
|
-
aopts(defaults, "defaults");
|
|
317
|
-
if (opts !== void 0) aopts(opts, title);
|
|
318
|
-
return Object.assign(Object.create(null), defaults, opts);
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Creates a callable hash function from a stateful class constructor.
|
|
322
|
-
* @param hashCons - hash constructor or factory
|
|
323
|
-
* @param info - optional metadata such as DER OID
|
|
324
|
-
* @returns Frozen callable hash wrapper with `.create()`.
|
|
325
|
-
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
326
|
-
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
327
|
-
* init time.
|
|
328
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
329
|
-
* @example
|
|
330
|
-
* Wrap a stateful hash constructor into a callable helper.
|
|
331
|
-
* ```ts
|
|
332
|
-
* import { createHasher } from '@noble/hashes/utils.js';
|
|
333
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
334
|
-
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
335
|
-
* wrapped(new Uint8Array([1]));
|
|
336
|
-
* ```
|
|
337
|
-
*/
|
|
338
|
-
function createHasher(hashCons, info = {}) {
|
|
339
|
-
if (typeof hashCons !== "function") throw new TypeError("\"hashCons\" expected function, got type=" + typeof hashCons);
|
|
340
|
-
info = checkOpts({}, info, "info");
|
|
341
|
-
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
342
|
-
const tmp = hashCons(void 0);
|
|
343
|
-
hashC.outputLen = tmp.outputLen;
|
|
344
|
-
hashC.blockLen = tmp.blockLen;
|
|
345
|
-
hashC.canXOF = tmp.canXOF;
|
|
346
|
-
hashC.create = (opts) => hashCons(opts);
|
|
347
|
-
Object.assign(hashC, info);
|
|
348
|
-
return Object.freeze(hashC);
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Cryptographically secure PRNG backed by `crypto.getRandomValues`.
|
|
352
|
-
* @param bytesLength - number of random bytes to generate
|
|
353
|
-
* @returns Random bytes.
|
|
354
|
-
* The platform `getRandomValues()` implementation still defines any
|
|
355
|
-
* single-call length cap, and this helper rejects oversize requests
|
|
356
|
-
* with a stable library `RangeError` instead of host-specific errors.
|
|
357
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
358
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
359
|
-
* @throws If the current runtime does not provide `crypto.getRandomValues`. {@link Error}
|
|
360
|
-
* @example
|
|
361
|
-
* Generate a fresh random key or nonce.
|
|
362
|
-
* ```ts
|
|
363
|
-
* const key = randomBytes(16);
|
|
364
|
-
* ```
|
|
365
|
-
*/
|
|
366
|
-
function randomBytes$1(bytesLength = 32) {
|
|
367
|
-
anumber$1(bytesLength, "bytesLength");
|
|
368
|
-
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
369
|
-
if (typeof cr?.getRandomValues !== "function") throw new Error("crypto.getRandomValues must be defined");
|
|
370
|
-
if (bytesLength > 65536) throw new RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
371
|
-
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
375
|
-
* @param suffix - final OID byte for the selected hash.
|
|
376
|
-
* The helper accepts any byte even though only the documented NIST hash
|
|
377
|
-
* suffixes are meaningful downstream.
|
|
378
|
-
* @returns Object containing the DER-encoded OID.
|
|
379
|
-
* @example
|
|
380
|
-
* Build OID metadata for a NIST hash.
|
|
381
|
-
* ```ts
|
|
382
|
-
* oidNist(0x01);
|
|
383
|
-
* ```
|
|
384
|
-
*/
|
|
385
|
-
var oidNist = (suffix) => ({ oid: Uint8Array.from([
|
|
386
|
-
6,
|
|
387
|
-
9,
|
|
388
|
-
96,
|
|
389
|
-
134,
|
|
390
|
-
72,
|
|
391
|
-
1,
|
|
392
|
-
101,
|
|
393
|
-
3,
|
|
394
|
-
4,
|
|
395
|
-
2,
|
|
396
|
-
suffix
|
|
397
|
-
]) });
|
|
398
|
-
//#endregion
|
|
399
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/hashes/2.4.0/a02ec2573a7b07204dfaf0508ab31b11f6a9bd74c18f15e8ab2933e00265fdfc/node_modules/@noble/hashes/_md.js
|
|
400
|
-
/**
|
|
401
|
-
* Internal Merkle-Damgard hash utils.
|
|
402
|
-
* @module
|
|
403
|
-
*/
|
|
404
|
-
/**
|
|
405
|
-
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
406
|
-
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
407
|
-
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
408
|
-
* set the same bit.
|
|
409
|
-
* @param a - selector word
|
|
410
|
-
* @param b - word chosen when selector bit is set
|
|
411
|
-
* @param c - word chosen when selector bit is clear
|
|
412
|
-
* @returns Mixed 32-bit word.
|
|
413
|
-
* @example
|
|
414
|
-
* Combine three words with the shared 32-bit choice primitive.
|
|
415
|
-
* ```ts
|
|
416
|
-
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
417
|
-
* ```
|
|
418
|
-
*/
|
|
419
|
-
function Chi(a, b, c) {
|
|
420
|
-
return a & b ^ ~a & c;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
424
|
-
* Returns bits shared by at least two inputs.
|
|
425
|
-
* @param a - first input word
|
|
426
|
-
* @param b - second input word
|
|
427
|
-
* @param c - third input word
|
|
428
|
-
* @returns Mixed 32-bit word.
|
|
429
|
-
* @example
|
|
430
|
-
* Combine three words with the shared 32-bit majority primitive.
|
|
431
|
-
* ```ts
|
|
432
|
-
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
433
|
-
* ```
|
|
434
|
-
*/
|
|
435
|
-
function Maj(a, b, c) {
|
|
436
|
-
return a & b ^ a & c ^ b & c;
|
|
437
|
-
}
|
|
438
|
-
/**
|
|
439
|
-
* Merkle-Damgard hash construction base class.
|
|
440
|
-
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
441
|
-
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
442
|
-
* strings with partial-byte tails.
|
|
443
|
-
* @param blockLen - internal block size in bytes
|
|
444
|
-
* @param outputLen - digest size in bytes
|
|
445
|
-
* @param padOffset - trailing length field size in bytes
|
|
446
|
-
* @param isLE - whether length and state words are encoded in little-endian
|
|
447
|
-
* @example
|
|
448
|
-
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
449
|
-
* ```ts
|
|
450
|
-
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
451
|
-
* const hash = new _SHA1();
|
|
452
|
-
* hash.update(new Uint8Array([97, 98, 99]));
|
|
453
|
-
* hash.digest();
|
|
454
|
-
* ```
|
|
455
|
-
*/
|
|
456
|
-
var HashMD = class {
|
|
457
|
-
blockLen;
|
|
458
|
-
outputLen;
|
|
459
|
-
canXOF = false;
|
|
460
|
-
padOffset;
|
|
461
|
-
isLE;
|
|
462
|
-
buffer;
|
|
463
|
-
view;
|
|
464
|
-
finished = false;
|
|
465
|
-
length = 0;
|
|
466
|
-
pos = 0;
|
|
467
|
-
destroyed = false;
|
|
468
|
-
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
469
|
-
this.blockLen = blockLen;
|
|
470
|
-
this.outputLen = outputLen;
|
|
471
|
-
this.padOffset = padOffset;
|
|
472
|
-
this.isLE = isLE;
|
|
473
|
-
this.buffer = new Uint8Array(blockLen);
|
|
474
|
-
this.view = createView(this.buffer);
|
|
475
|
-
}
|
|
476
|
-
update(data) {
|
|
477
|
-
aexists(this);
|
|
478
|
-
abytes$1(data);
|
|
479
|
-
const { view, buffer, blockLen } = this;
|
|
480
|
-
const len = data.length;
|
|
481
|
-
let processed = false;
|
|
482
|
-
for (let pos = 0; pos < len;) {
|
|
483
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
484
|
-
if (take === blockLen) {
|
|
485
|
-
const dataView = createView(data);
|
|
486
|
-
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
487
|
-
processed = true;
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
491
|
-
this.pos += take;
|
|
492
|
-
pos += take;
|
|
493
|
-
if (this.pos === blockLen) {
|
|
494
|
-
this.process(view, 0);
|
|
495
|
-
this.pos = 0;
|
|
496
|
-
processed = true;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
this.length += data.length;
|
|
500
|
-
if (processed) this.roundClean();
|
|
501
|
-
return this;
|
|
502
|
-
}
|
|
503
|
-
digestInto(out) {
|
|
504
|
-
aexists(this);
|
|
505
|
-
aoutput(out, this);
|
|
506
|
-
this.finished = true;
|
|
507
|
-
const { buffer, view, blockLen, isLE } = this;
|
|
508
|
-
let { pos } = this;
|
|
509
|
-
buffer[pos++] = 128;
|
|
510
|
-
buffer.fill(0, pos);
|
|
511
|
-
if (this.padOffset > blockLen - pos) {
|
|
512
|
-
this.process(view, 0);
|
|
513
|
-
buffer.fill(0);
|
|
514
|
-
}
|
|
515
|
-
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
516
|
-
this.process(view, 0);
|
|
517
|
-
this.roundClean();
|
|
518
|
-
const oview = out === buffer ? view : createView(out);
|
|
519
|
-
const len = this.outputLen;
|
|
520
|
-
const outLen = len / 4;
|
|
521
|
-
const state = this.get();
|
|
522
|
-
if (len % 4 || outLen > state.length) throw new Error("invalid outputLen");
|
|
523
|
-
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
524
|
-
}
|
|
525
|
-
digest() {
|
|
526
|
-
const { buffer, outputLen } = this;
|
|
527
|
-
this.digestInto(buffer);
|
|
528
|
-
const res = buffer.slice(0, outputLen);
|
|
529
|
-
this.destroy();
|
|
530
|
-
return res;
|
|
531
|
-
}
|
|
532
|
-
_cloneIntoMeta(to) {
|
|
533
|
-
const { buffer, length, finished, destroyed, pos } = this;
|
|
534
|
-
to.destroyed = destroyed;
|
|
535
|
-
to.finished = finished;
|
|
536
|
-
to.length = length;
|
|
537
|
-
to.pos = pos;
|
|
538
|
-
if (pos) to.buffer.set(buffer);
|
|
539
|
-
return to;
|
|
540
|
-
}
|
|
541
|
-
clone() {
|
|
542
|
-
return this._cloneInto();
|
|
543
|
-
}
|
|
544
|
-
};
|
|
545
|
-
/**
|
|
546
|
-
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
547
|
-
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
548
|
-
*/
|
|
549
|
-
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
550
|
-
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
551
|
-
* it as read-only because constructors copy words from it by index. */
|
|
552
|
-
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
553
|
-
1779033703,
|
|
554
|
-
3144134277,
|
|
555
|
-
1013904242,
|
|
556
|
-
2773480762,
|
|
557
|
-
1359893119,
|
|
558
|
-
2600822924,
|
|
559
|
-
528734635,
|
|
560
|
-
1541459225
|
|
561
|
-
]);
|
|
562
|
-
/** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
|
|
563
|
-
* big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
|
|
564
|
-
* eight prime numbers. Exported as a shared table; callers must treat it as read-only because
|
|
565
|
-
* constructors copy halves from it by index. */
|
|
566
|
-
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
567
|
-
1779033703,
|
|
568
|
-
4089235720,
|
|
569
|
-
3144134277,
|
|
570
|
-
2227873595,
|
|
571
|
-
1013904242,
|
|
572
|
-
4271175723,
|
|
573
|
-
2773480762,
|
|
574
|
-
1595750129,
|
|
575
|
-
1359893119,
|
|
576
|
-
2917565137,
|
|
577
|
-
2600822924,
|
|
578
|
-
725511199,
|
|
579
|
-
528734635,
|
|
580
|
-
4215389547,
|
|
581
|
-
1541459225,
|
|
582
|
-
327033209
|
|
583
|
-
]);
|
|
584
|
-
//#endregion
|
|
585
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/hashes/2.4.0/a02ec2573a7b07204dfaf0508ab31b11f6a9bd74c18f15e8ab2933e00265fdfc/node_modules/@noble/hashes/sha2.js
|
|
586
|
-
/**
|
|
587
|
-
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
588
|
-
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
589
|
-
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
590
|
-
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
591
|
-
* @module
|
|
592
|
-
*/
|
|
593
|
-
/**
|
|
594
|
-
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
595
|
-
* of the cube roots of the first 64 primes (2..311).
|
|
596
|
-
*/
|
|
597
|
-
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
598
|
-
1116352408,
|
|
599
|
-
1899447441,
|
|
600
|
-
3049323471,
|
|
601
|
-
3921009573,
|
|
602
|
-
961987163,
|
|
603
|
-
1508970993,
|
|
604
|
-
2453635748,
|
|
605
|
-
2870763221,
|
|
606
|
-
3624381080,
|
|
607
|
-
310598401,
|
|
608
|
-
607225278,
|
|
609
|
-
1426881987,
|
|
610
|
-
1925078388,
|
|
611
|
-
2162078206,
|
|
612
|
-
2614888103,
|
|
613
|
-
3248222580,
|
|
614
|
-
3835390401,
|
|
615
|
-
4022224774,
|
|
616
|
-
264347078,
|
|
617
|
-
604807628,
|
|
618
|
-
770255983,
|
|
619
|
-
1249150122,
|
|
620
|
-
1555081692,
|
|
621
|
-
1996064986,
|
|
622
|
-
2554220882,
|
|
623
|
-
2821834349,
|
|
624
|
-
2952996808,
|
|
625
|
-
3210313671,
|
|
626
|
-
3336571891,
|
|
627
|
-
3584528711,
|
|
628
|
-
113926993,
|
|
629
|
-
338241895,
|
|
630
|
-
666307205,
|
|
631
|
-
773529912,
|
|
632
|
-
1294757372,
|
|
633
|
-
1396182291,
|
|
634
|
-
1695183700,
|
|
635
|
-
1986661051,
|
|
636
|
-
2177026350,
|
|
637
|
-
2456956037,
|
|
638
|
-
2730485921,
|
|
639
|
-
2820302411,
|
|
640
|
-
3259730800,
|
|
641
|
-
3345764771,
|
|
642
|
-
3516065817,
|
|
643
|
-
3600352804,
|
|
644
|
-
4094571909,
|
|
645
|
-
275423344,
|
|
646
|
-
430227734,
|
|
647
|
-
506948616,
|
|
648
|
-
659060556,
|
|
649
|
-
883997877,
|
|
650
|
-
958139571,
|
|
651
|
-
1322822218,
|
|
652
|
-
1537002063,
|
|
653
|
-
1747873779,
|
|
654
|
-
1955562222,
|
|
655
|
-
2024104815,
|
|
656
|
-
2227730452,
|
|
657
|
-
2361852424,
|
|
658
|
-
2428436474,
|
|
659
|
-
2756734187,
|
|
660
|
-
3204031479,
|
|
661
|
-
3329325298
|
|
662
|
-
]);
|
|
663
|
-
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
664
|
-
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
665
|
-
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
666
|
-
var SHA2_32B = class extends HashMD {
|
|
667
|
-
A = 0;
|
|
668
|
-
B = 0;
|
|
669
|
-
C = 0;
|
|
670
|
-
D = 0;
|
|
671
|
-
E = 0;
|
|
672
|
-
F = 0;
|
|
673
|
-
G = 0;
|
|
674
|
-
H = 0;
|
|
675
|
-
constructor(outputLen, IV) {
|
|
676
|
-
super(64, outputLen, 8, false);
|
|
677
|
-
this.A = IV[0] | 0;
|
|
678
|
-
this.B = IV[1] | 0;
|
|
679
|
-
this.C = IV[2] | 0;
|
|
680
|
-
this.D = IV[3] | 0;
|
|
681
|
-
this.E = IV[4] | 0;
|
|
682
|
-
this.F = IV[5] | 0;
|
|
683
|
-
this.G = IV[6] | 0;
|
|
684
|
-
this.H = IV[7] | 0;
|
|
685
|
-
}
|
|
686
|
-
get() {
|
|
687
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
688
|
-
return [
|
|
689
|
-
A,
|
|
690
|
-
B,
|
|
691
|
-
C,
|
|
692
|
-
D,
|
|
693
|
-
E,
|
|
694
|
-
F,
|
|
695
|
-
G,
|
|
696
|
-
H
|
|
697
|
-
];
|
|
698
|
-
}
|
|
699
|
-
set(A, B, C, D, E, F, G, H) {
|
|
700
|
-
this.A = A | 0;
|
|
701
|
-
this.B = B | 0;
|
|
702
|
-
this.C = C | 0;
|
|
703
|
-
this.D = D | 0;
|
|
704
|
-
this.E = E | 0;
|
|
705
|
-
this.F = F | 0;
|
|
706
|
-
this.G = G | 0;
|
|
707
|
-
this.H = H | 0;
|
|
708
|
-
}
|
|
709
|
-
_cloneInto(to) {
|
|
710
|
-
(to ||= new this.constructor()).set(...this.get());
|
|
711
|
-
return this._cloneIntoMeta(to);
|
|
712
|
-
}
|
|
713
|
-
process(view, offset) {
|
|
714
|
-
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
715
|
-
for (let i = 16; i < 64; i++) {
|
|
716
|
-
const W15 = SHA256_W[i - 15];
|
|
717
|
-
const W2 = SHA256_W[i - 2];
|
|
718
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
719
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
720
|
-
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
721
|
-
}
|
|
722
|
-
let { A, B, C, D, E, F, G, H } = this;
|
|
723
|
-
for (let i = 0; i < 64; i++) {
|
|
724
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
725
|
-
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
726
|
-
const T2 = (rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22)) + Maj(A, B, C) | 0;
|
|
727
|
-
H = G;
|
|
728
|
-
G = F;
|
|
729
|
-
F = E;
|
|
730
|
-
E = D + T1 | 0;
|
|
731
|
-
D = C;
|
|
732
|
-
C = B;
|
|
733
|
-
B = A;
|
|
734
|
-
A = T1 + T2 | 0;
|
|
735
|
-
}
|
|
736
|
-
A = A + this.A | 0;
|
|
737
|
-
B = B + this.B | 0;
|
|
738
|
-
C = C + this.C | 0;
|
|
739
|
-
D = D + this.D | 0;
|
|
740
|
-
E = E + this.E | 0;
|
|
741
|
-
F = F + this.F | 0;
|
|
742
|
-
G = G + this.G | 0;
|
|
743
|
-
H = H + this.H | 0;
|
|
744
|
-
this.set(A, B, C, D, E, F, G, H);
|
|
745
|
-
}
|
|
746
|
-
roundClean() {
|
|
747
|
-
clean(SHA256_W);
|
|
748
|
-
}
|
|
749
|
-
destroy() {
|
|
750
|
-
this.destroyed = true;
|
|
751
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
752
|
-
clean(this.buffer);
|
|
753
|
-
}
|
|
754
|
-
};
|
|
755
|
-
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
756
|
-
var _SHA256 = class extends SHA2_32B {
|
|
757
|
-
constructor() {
|
|
758
|
-
super(32, SHA256_IV);
|
|
759
|
-
}
|
|
760
|
-
};
|
|
761
|
-
var K512 = /* @__PURE__ */ (() => split([
|
|
762
|
-
"0x428a2f98d728ae22",
|
|
763
|
-
"0x7137449123ef65cd",
|
|
764
|
-
"0xb5c0fbcfec4d3b2f",
|
|
765
|
-
"0xe9b5dba58189dbbc",
|
|
766
|
-
"0x3956c25bf348b538",
|
|
767
|
-
"0x59f111f1b605d019",
|
|
768
|
-
"0x923f82a4af194f9b",
|
|
769
|
-
"0xab1c5ed5da6d8118",
|
|
770
|
-
"0xd807aa98a3030242",
|
|
771
|
-
"0x12835b0145706fbe",
|
|
772
|
-
"0x243185be4ee4b28c",
|
|
773
|
-
"0x550c7dc3d5ffb4e2",
|
|
774
|
-
"0x72be5d74f27b896f",
|
|
775
|
-
"0x80deb1fe3b1696b1",
|
|
776
|
-
"0x9bdc06a725c71235",
|
|
777
|
-
"0xc19bf174cf692694",
|
|
778
|
-
"0xe49b69c19ef14ad2",
|
|
779
|
-
"0xefbe4786384f25e3",
|
|
780
|
-
"0x0fc19dc68b8cd5b5",
|
|
781
|
-
"0x240ca1cc77ac9c65",
|
|
782
|
-
"0x2de92c6f592b0275",
|
|
783
|
-
"0x4a7484aa6ea6e483",
|
|
784
|
-
"0x5cb0a9dcbd41fbd4",
|
|
785
|
-
"0x76f988da831153b5",
|
|
786
|
-
"0x983e5152ee66dfab",
|
|
787
|
-
"0xa831c66d2db43210",
|
|
788
|
-
"0xb00327c898fb213f",
|
|
789
|
-
"0xbf597fc7beef0ee4",
|
|
790
|
-
"0xc6e00bf33da88fc2",
|
|
791
|
-
"0xd5a79147930aa725",
|
|
792
|
-
"0x06ca6351e003826f",
|
|
793
|
-
"0x142929670a0e6e70",
|
|
794
|
-
"0x27b70a8546d22ffc",
|
|
795
|
-
"0x2e1b21385c26c926",
|
|
796
|
-
"0x4d2c6dfc5ac42aed",
|
|
797
|
-
"0x53380d139d95b3df",
|
|
798
|
-
"0x650a73548baf63de",
|
|
799
|
-
"0x766a0abb3c77b2a8",
|
|
800
|
-
"0x81c2c92e47edaee6",
|
|
801
|
-
"0x92722c851482353b",
|
|
802
|
-
"0xa2bfe8a14cf10364",
|
|
803
|
-
"0xa81a664bbc423001",
|
|
804
|
-
"0xc24b8b70d0f89791",
|
|
805
|
-
"0xc76c51a30654be30",
|
|
806
|
-
"0xd192e819d6ef5218",
|
|
807
|
-
"0xd69906245565a910",
|
|
808
|
-
"0xf40e35855771202a",
|
|
809
|
-
"0x106aa07032bbd1b8",
|
|
810
|
-
"0x19a4c116b8d2d0c8",
|
|
811
|
-
"0x1e376c085141ab53",
|
|
812
|
-
"0x2748774cdf8eeb99",
|
|
813
|
-
"0x34b0bcb5e19b48a8",
|
|
814
|
-
"0x391c0cb3c5c95a63",
|
|
815
|
-
"0x4ed8aa4ae3418acb",
|
|
816
|
-
"0x5b9cca4f7763e373",
|
|
817
|
-
"0x682e6ff3d6b2b8a3",
|
|
818
|
-
"0x748f82ee5defb2fc",
|
|
819
|
-
"0x78a5636f43172f60",
|
|
820
|
-
"0x84c87814a1f0ab72",
|
|
821
|
-
"0x8cc702081a6439ec",
|
|
822
|
-
"0x90befffa23631e28",
|
|
823
|
-
"0xa4506cebde82bde9",
|
|
824
|
-
"0xbef9a3f7b2c67915",
|
|
825
|
-
"0xc67178f2e372532b",
|
|
826
|
-
"0xca273eceea26619c",
|
|
827
|
-
"0xd186b8c721c0c207",
|
|
828
|
-
"0xeada7dd6cde0eb1e",
|
|
829
|
-
"0xf57d4f7fee6ed178",
|
|
830
|
-
"0x06f067aa72176fba",
|
|
831
|
-
"0x0a637dc5a2c898a6",
|
|
832
|
-
"0x113f9804bef90dae",
|
|
833
|
-
"0x1b710b35131c471b",
|
|
834
|
-
"0x28db77f523047d84",
|
|
835
|
-
"0x32caab7b40c72493",
|
|
836
|
-
"0x3c9ebe0a15c9bebc",
|
|
837
|
-
"0x431d67c49c100d4c",
|
|
838
|
-
"0x4cc5d4becb3e42b6",
|
|
839
|
-
"0x597f299cfc657e2a",
|
|
840
|
-
"0x5fcb6fab3ad6faec",
|
|
841
|
-
"0x6c44198c4a475817"
|
|
842
|
-
].map((n) => BigInt(n))))();
|
|
843
|
-
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
844
|
-
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
845
|
-
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
846
|
-
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
847
|
-
/** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
|
|
848
|
-
var SHA2_64B = class extends HashMD {
|
|
849
|
-
Ah = 0;
|
|
850
|
-
Al = 0;
|
|
851
|
-
Bh = 0;
|
|
852
|
-
Bl = 0;
|
|
853
|
-
Ch = 0;
|
|
854
|
-
Cl = 0;
|
|
855
|
-
Dh = 0;
|
|
856
|
-
Dl = 0;
|
|
857
|
-
Eh = 0;
|
|
858
|
-
El = 0;
|
|
859
|
-
Fh = 0;
|
|
860
|
-
Fl = 0;
|
|
861
|
-
Gh = 0;
|
|
862
|
-
Gl = 0;
|
|
863
|
-
Hh = 0;
|
|
864
|
-
Hl = 0;
|
|
865
|
-
constructor(outputLen, IV) {
|
|
866
|
-
super(128, outputLen, 16, false);
|
|
867
|
-
this.Ah = IV[0] | 0;
|
|
868
|
-
this.Al = IV[1] | 0;
|
|
869
|
-
this.Bh = IV[2] | 0;
|
|
870
|
-
this.Bl = IV[3] | 0;
|
|
871
|
-
this.Ch = IV[4] | 0;
|
|
872
|
-
this.Cl = IV[5] | 0;
|
|
873
|
-
this.Dh = IV[6] | 0;
|
|
874
|
-
this.Dl = IV[7] | 0;
|
|
875
|
-
this.Eh = IV[8] | 0;
|
|
876
|
-
this.El = IV[9] | 0;
|
|
877
|
-
this.Fh = IV[10] | 0;
|
|
878
|
-
this.Fl = IV[11] | 0;
|
|
879
|
-
this.Gh = IV[12] | 0;
|
|
880
|
-
this.Gl = IV[13] | 0;
|
|
881
|
-
this.Hh = IV[14] | 0;
|
|
882
|
-
this.Hl = IV[15] | 0;
|
|
883
|
-
}
|
|
884
|
-
get() {
|
|
885
|
-
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
886
|
-
return [
|
|
887
|
-
Ah,
|
|
888
|
-
Al,
|
|
889
|
-
Bh,
|
|
890
|
-
Bl,
|
|
891
|
-
Ch,
|
|
892
|
-
Cl,
|
|
893
|
-
Dh,
|
|
894
|
-
Dl,
|
|
895
|
-
Eh,
|
|
896
|
-
El,
|
|
897
|
-
Fh,
|
|
898
|
-
Fl,
|
|
899
|
-
Gh,
|
|
900
|
-
Gl,
|
|
901
|
-
Hh,
|
|
902
|
-
Hl
|
|
903
|
-
];
|
|
904
|
-
}
|
|
905
|
-
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
906
|
-
this.Ah = Ah | 0;
|
|
907
|
-
this.Al = Al | 0;
|
|
908
|
-
this.Bh = Bh | 0;
|
|
909
|
-
this.Bl = Bl | 0;
|
|
910
|
-
this.Ch = Ch | 0;
|
|
911
|
-
this.Cl = Cl | 0;
|
|
912
|
-
this.Dh = Dh | 0;
|
|
913
|
-
this.Dl = Dl | 0;
|
|
914
|
-
this.Eh = Eh | 0;
|
|
915
|
-
this.El = El | 0;
|
|
916
|
-
this.Fh = Fh | 0;
|
|
917
|
-
this.Fl = Fl | 0;
|
|
918
|
-
this.Gh = Gh | 0;
|
|
919
|
-
this.Gl = Gl | 0;
|
|
920
|
-
this.Hh = Hh | 0;
|
|
921
|
-
this.Hl = Hl | 0;
|
|
922
|
-
}
|
|
923
|
-
_cloneInto(to) {
|
|
924
|
-
(to ||= new this.constructor()).set(...this.get());
|
|
925
|
-
return this._cloneIntoMeta(to);
|
|
926
|
-
}
|
|
927
|
-
process(view, offset) {
|
|
928
|
-
for (let i = 0; i < 16; i++, offset += 4) {
|
|
929
|
-
SHA512_W_H[i] = view.getUint32(offset);
|
|
930
|
-
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
931
|
-
}
|
|
932
|
-
for (let i = 16; i < 80; i++) {
|
|
933
|
-
const W15h = SHA512_W_H[i - 15] | 0;
|
|
934
|
-
const W15l = SHA512_W_L[i - 15] | 0;
|
|
935
|
-
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
936
|
-
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
937
|
-
const W2h = SHA512_W_H[i - 2] | 0;
|
|
938
|
-
const W2l = SHA512_W_L[i - 2] | 0;
|
|
939
|
-
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
940
|
-
const SUMl = add4L(s0l, rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6), SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
941
|
-
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
942
|
-
SHA512_W_H[i] = SUMh | 0;
|
|
943
|
-
SHA512_W_L[i] = SUMl | 0;
|
|
944
|
-
}
|
|
945
|
-
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
946
|
-
for (let i = 0; i < 80; i++) {
|
|
947
|
-
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
948
|
-
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
949
|
-
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
950
|
-
const CHIl = El & Fl ^ ~El & Gl;
|
|
951
|
-
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
952
|
-
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
953
|
-
const T1l = T1ll | 0;
|
|
954
|
-
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
955
|
-
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
956
|
-
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
957
|
-
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
958
|
-
Hh = Gh | 0;
|
|
959
|
-
Hl = Gl | 0;
|
|
960
|
-
Gh = Fh | 0;
|
|
961
|
-
Gl = Fl | 0;
|
|
962
|
-
Fh = Eh | 0;
|
|
963
|
-
Fl = El | 0;
|
|
964
|
-
({h: Eh, l: El} = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
965
|
-
Dh = Ch | 0;
|
|
966
|
-
Dl = Cl | 0;
|
|
967
|
-
Ch = Bh | 0;
|
|
968
|
-
Cl = Bl | 0;
|
|
969
|
-
Bh = Ah | 0;
|
|
970
|
-
Bl = Al | 0;
|
|
971
|
-
const All = add3L(T1l, sigma0l, MAJl);
|
|
972
|
-
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
973
|
-
Al = All | 0;
|
|
974
|
-
}
|
|
975
|
-
({h: Ah, l: Al} = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
976
|
-
({h: Bh, l: Bl} = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
977
|
-
({h: Ch, l: Cl} = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
978
|
-
({h: Dh, l: Dl} = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
979
|
-
({h: Eh, l: El} = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
980
|
-
({h: Fh, l: Fl} = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
981
|
-
({h: Gh, l: Gl} = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
982
|
-
({h: Hh, l: Hl} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
983
|
-
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
984
|
-
}
|
|
985
|
-
roundClean() {
|
|
986
|
-
clean(SHA512_W_H, SHA512_W_L);
|
|
987
|
-
}
|
|
988
|
-
destroy() {
|
|
989
|
-
this.destroyed = true;
|
|
990
|
-
clean(this.buffer);
|
|
991
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
992
|
-
}
|
|
993
|
-
};
|
|
994
|
-
/** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
995
|
-
var _SHA512 = class extends SHA2_64B {
|
|
996
|
-
constructor() {
|
|
997
|
-
super(64, SHA512_IV);
|
|
998
|
-
}
|
|
999
|
-
};
|
|
1000
|
-
/**
|
|
1001
|
-
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
1002
|
-
*
|
|
1003
|
-
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
1004
|
-
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1005
|
-
* - Each sha256 hash is executing 2^18 bit operations.
|
|
1006
|
-
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
1007
|
-
* @param msg - message bytes to hash
|
|
1008
|
-
* @param opts - Reserved hash options.
|
|
1009
|
-
* @returns Digest bytes.
|
|
1010
|
-
* @example
|
|
1011
|
-
* Hash a message with SHA2-256.
|
|
1012
|
-
* ```ts
|
|
1013
|
-
* sha256(new Uint8Array([97, 98, 99]));
|
|
1014
|
-
* ```
|
|
1015
|
-
*/
|
|
1016
|
-
var sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(), /* @__PURE__ */ oidNist(1));
|
|
1017
|
-
/**
|
|
1018
|
-
* SHA2-512 hash function from RFC 4634.
|
|
1019
|
-
* @param msg - message bytes to hash
|
|
1020
|
-
* @param opts - Reserved hash options.
|
|
1021
|
-
* @returns Digest bytes.
|
|
1022
|
-
* @example
|
|
1023
|
-
* Hash a message with SHA2-512.
|
|
1024
|
-
* ```ts
|
|
1025
|
-
* sha512(new Uint8Array([97, 98, 99]));
|
|
1026
|
-
* ```
|
|
1027
|
-
*/
|
|
1028
|
-
var sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(), /* @__PURE__ */ oidNist(3));
|
|
1029
|
-
//#endregion
|
|
1030
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/curves/2.4.0/766129a921e8dfd2ef1de557b88b41133e3334d958e354fac1f5d17f98661627/node_modules/@noble/curves/utils.js
|
|
1031
|
-
/**
|
|
1032
|
-
* Hex, bytes and number utilities.
|
|
1033
|
-
* @module
|
|
1034
|
-
*/
|
|
1035
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1036
|
-
/**
|
|
1037
|
-
* Validates that a value is an array, optionally validating each element.
|
|
1038
|
-
* @param item - Value to validate.
|
|
1039
|
-
* @param title - Label included in thrown errors.
|
|
1040
|
-
* @param inner - Optional per-element validator, called with the element and its label.
|
|
1041
|
-
* @returns The validated array.
|
|
1042
|
-
* @example
|
|
1043
|
-
* Validate an array of points before batch processing.
|
|
1044
|
-
*
|
|
1045
|
-
* ```ts
|
|
1046
|
-
* aarray([1n, 2n], 'scalars');
|
|
1047
|
-
* ```
|
|
1048
|
-
*/
|
|
1049
|
-
function aarray(item, title, inner = () => {}) {
|
|
1050
|
-
if (!Array.isArray(item)) throw new TypeError(`"${title}" expected array, got type=${typeof item}`);
|
|
1051
|
-
for (let i = 0; i < item.length; i++) inner(item[i], `${title}[${i}]`);
|
|
1052
|
-
return item;
|
|
1053
|
-
}
|
|
1054
|
-
/**
|
|
1055
|
-
* Validates that a value is a byte array.
|
|
1056
|
-
* @param value - Value to validate.
|
|
1057
|
-
* @param length - Optional exact byte length.
|
|
1058
|
-
* @param title - Optional field name.
|
|
1059
|
-
* @returns Original byte array.
|
|
1060
|
-
* @example
|
|
1061
|
-
* Reject non-byte input before passing data into curve code.
|
|
1062
|
-
*
|
|
1063
|
-
* ```ts
|
|
1064
|
-
* abytes(new Uint8Array(1));
|
|
1065
|
-
* ```
|
|
1066
|
-
*/
|
|
1067
|
-
var abytes = (value, length, title) => abytes$1(value, length, title);
|
|
1068
|
-
/**
|
|
1069
|
-
* Validates that a value is a non-negative safe integer.
|
|
1070
|
-
* @param n - Value to validate.
|
|
1071
|
-
* @param title - Optional field name.
|
|
1072
|
-
* @returns The validated number.
|
|
1073
|
-
* @example
|
|
1074
|
-
* Validate a numeric length before allocating buffers.
|
|
1075
|
-
*
|
|
1076
|
-
* ```ts
|
|
1077
|
-
* anumber(1);
|
|
1078
|
-
* ```
|
|
1079
|
-
*/
|
|
1080
|
-
var anumber = anumber$1;
|
|
1081
|
-
/**
|
|
1082
|
-
* Asserts something is a string.
|
|
1083
|
-
* @param value - Value to validate.
|
|
1084
|
-
* @param title - Label included in thrown errors.
|
|
1085
|
-
* @returns The validated string.
|
|
1086
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1087
|
-
* @example
|
|
1088
|
-
* Validate a label string.
|
|
1089
|
-
*
|
|
1090
|
-
* ```ts
|
|
1091
|
-
* astring('example', 'label');
|
|
1092
|
-
* ```
|
|
1093
|
-
*/
|
|
1094
|
-
function astring(value, title = "") {
|
|
1095
|
-
if (typeof value !== "string") {
|
|
1096
|
-
const prefix = title && `"${title}" `;
|
|
1097
|
-
throw new TypeError(prefix + "expected string, got type=" + typeof value);
|
|
1098
|
-
}
|
|
1099
|
-
return value;
|
|
1100
|
-
}
|
|
1101
|
-
/**
|
|
1102
|
-
* Asserts something is a plain object-ish value, not null or array.
|
|
1103
|
-
* @param value - Value to validate.
|
|
1104
|
-
* @param title - Label included in thrown errors.
|
|
1105
|
-
* @returns The validated object.
|
|
1106
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1107
|
-
* @example
|
|
1108
|
-
* Validate an options object before checking fields.
|
|
1109
|
-
*
|
|
1110
|
-
* ```ts
|
|
1111
|
-
* aobject({ flag: true });
|
|
1112
|
-
* ```
|
|
1113
|
-
*/
|
|
1114
|
-
function aobject(value, title = "object") {
|
|
1115
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError(title === "object" ? "expected valid options object" : `"${title}" expected object, got type=${typeof value}`);
|
|
1116
|
-
return value;
|
|
1117
|
-
}
|
|
1118
|
-
/**
|
|
1119
|
-
* Asserts something is a function.
|
|
1120
|
-
* @param value - Value to validate.
|
|
1121
|
-
* @param title - Label included in thrown errors.
|
|
1122
|
-
* @returns The validated function.
|
|
1123
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1124
|
-
* @example
|
|
1125
|
-
* Validate a required method before calling it.
|
|
1126
|
-
*
|
|
1127
|
-
* ```ts
|
|
1128
|
-
* afunction(() => true, 'predicate');
|
|
1129
|
-
* ```
|
|
1130
|
-
*/
|
|
1131
|
-
function afunction(value, title) {
|
|
1132
|
-
if (typeof value !== "function") throw new TypeError(`"${title}" is invalid: expected function, got ${typeof value}`);
|
|
1133
|
-
return value;
|
|
1134
|
-
}
|
|
1135
|
-
/**
|
|
1136
|
-
* Encodes bytes as lowercase hex.
|
|
1137
|
-
* @param bytes - Bytes to encode.
|
|
1138
|
-
* @returns Lowercase hex string.
|
|
1139
|
-
* @example
|
|
1140
|
-
* Serialize bytes as hex for logging or fixtures.
|
|
1141
|
-
*
|
|
1142
|
-
* ```ts
|
|
1143
|
-
* bytesToHex(Uint8Array.of(1, 2, 3));
|
|
1144
|
-
* ```
|
|
1145
|
-
*/
|
|
1146
|
-
var bytesToHex = bytesToHex$1;
|
|
1147
|
-
/**
|
|
1148
|
-
* Concatenates byte arrays.
|
|
1149
|
-
* @param arrays - Byte arrays to join.
|
|
1150
|
-
* @returns Concatenated bytes.
|
|
1151
|
-
* @example
|
|
1152
|
-
* Join domain-separated chunks into one buffer.
|
|
1153
|
-
*
|
|
1154
|
-
* ```ts
|
|
1155
|
-
* concatBytes(Uint8Array.of(1), Uint8Array.of(2));
|
|
1156
|
-
* ```
|
|
1157
|
-
*/
|
|
1158
|
-
var concatBytes = (...arrays) => concatBytes$1(...arrays);
|
|
1159
|
-
/**
|
|
1160
|
-
* Decodes lowercase or uppercase hex into bytes.
|
|
1161
|
-
* @param hex - Hex string to decode.
|
|
1162
|
-
* @returns Decoded bytes.
|
|
1163
|
-
* @example
|
|
1164
|
-
* Parse fixture hex into bytes before hashing.
|
|
1165
|
-
*
|
|
1166
|
-
* ```ts
|
|
1167
|
-
* hexToBytes('0102');
|
|
1168
|
-
* ```
|
|
1169
|
-
*/
|
|
1170
|
-
var hexToBytes = (hex) => hexToBytes$1(hex);
|
|
1171
|
-
/**
|
|
1172
|
-
* Checks whether a value is a Uint8Array.
|
|
1173
|
-
* @param a - Value to inspect.
|
|
1174
|
-
* @returns `true` when `a` is a Uint8Array.
|
|
1175
|
-
* @example
|
|
1176
|
-
* Branch on byte input before decoding it.
|
|
1177
|
-
*
|
|
1178
|
-
* ```ts
|
|
1179
|
-
* isBytes(new Uint8Array(1));
|
|
1180
|
-
* ```
|
|
1181
|
-
*/
|
|
1182
|
-
var isBytes = isBytes$1;
|
|
1183
|
-
/**
|
|
1184
|
-
* Reads random bytes from the platform CSPRNG.
|
|
1185
|
-
* @param bytesLength - Number of random bytes to read.
|
|
1186
|
-
* @returns Fresh random bytes.
|
|
1187
|
-
* @example
|
|
1188
|
-
* Generate a random seed for a keypair.
|
|
1189
|
-
*
|
|
1190
|
-
* ```ts
|
|
1191
|
-
* randomBytes(2);
|
|
1192
|
-
* ```
|
|
1193
|
-
*/
|
|
1194
|
-
var randomBytes = (bytesLength) => randomBytes$1(bytesLength);
|
|
1195
|
-
var _0n$2 = /* @__PURE__ */ BigInt(0);
|
|
1196
|
-
var _1n$2 = /* @__PURE__ */ BigInt(1);
|
|
1197
|
-
var atitle = (title) => title ? `"${title}" ` : "";
|
|
1198
|
-
/**
|
|
1199
|
-
* Validates that a flag is boolean.
|
|
1200
|
-
* @param value - Value to validate.
|
|
1201
|
-
* @param title - Optional field name.
|
|
1202
|
-
* @returns Original value.
|
|
1203
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1204
|
-
* @example
|
|
1205
|
-
* Reject non-boolean option flags early.
|
|
1206
|
-
*
|
|
1207
|
-
* ```ts
|
|
1208
|
-
* abool(true);
|
|
1209
|
-
* ```
|
|
1210
|
-
*/
|
|
1211
|
-
function abool(value, title = "") {
|
|
1212
|
-
if (typeof value !== "boolean") throw new TypeError(atitle(title) + "expected boolean, got type=" + typeof value);
|
|
1213
|
-
return value;
|
|
1214
|
-
}
|
|
1215
|
-
/**
|
|
1216
|
-
* Validates that a value is a non-negative bigint or safe integer.
|
|
1217
|
-
* @param n - Value to validate.
|
|
1218
|
-
* @returns The same validated value.
|
|
1219
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1220
|
-
* @example
|
|
1221
|
-
* Validate one integer-like value before serializing it.
|
|
1222
|
-
*
|
|
1223
|
-
* ```ts
|
|
1224
|
-
* abignumber(1n);
|
|
1225
|
-
* ```
|
|
1226
|
-
*/
|
|
1227
|
-
function abignumber(n) {
|
|
1228
|
-
if (typeof n === "bigint") {
|
|
1229
|
-
if (!isPosBig(n)) throw new RangeError("positive bigint expected, got " + n);
|
|
1230
|
-
} else anumber(n);
|
|
1231
|
-
return n;
|
|
1232
|
-
}
|
|
1233
|
-
/**
|
|
1234
|
-
* Validates that a value is a safe integer.
|
|
1235
|
-
* @param value - Integer to validate.
|
|
1236
|
-
* @param title - Optional field name.
|
|
1237
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1238
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1239
|
-
* @example
|
|
1240
|
-
* Validate a window size before scalar arithmetic uses it.
|
|
1241
|
-
*
|
|
1242
|
-
* ```ts
|
|
1243
|
-
* asafenumber(1);
|
|
1244
|
-
* ```
|
|
1245
|
-
*/
|
|
1246
|
-
function asafenumber(value, title = "") {
|
|
1247
|
-
if (typeof value !== "number") {
|
|
1248
|
-
const prefix = title && `"${title}" `;
|
|
1249
|
-
throw new TypeError(prefix + "expected number, got type=" + typeof value);
|
|
1250
|
-
}
|
|
1251
|
-
if (!Number.isSafeInteger(value)) {
|
|
1252
|
-
const prefix = title && `"${title}" `;
|
|
1253
|
-
throw new RangeError(prefix + "expected safe integer, got " + value);
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Encodes a bigint into even-length big-endian hex.
|
|
1258
|
-
* The historical "unpadded" name only means "no fixed-width field padding"; odd-length hex still
|
|
1259
|
-
* gets one leading zero nibble so the result always represents whole bytes.
|
|
1260
|
-
* @param num - Number to encode.
|
|
1261
|
-
* @returns Big-endian hex string.
|
|
1262
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1263
|
-
* @example
|
|
1264
|
-
* Encode a scalar into hex without a `0x` prefix.
|
|
1265
|
-
*
|
|
1266
|
-
* ```ts
|
|
1267
|
-
* numberToHexUnpadded(255n);
|
|
1268
|
-
* ```
|
|
1269
|
-
*/
|
|
1270
|
-
function numberToHexUnpadded(num) {
|
|
1271
|
-
const hex = abignumber(num).toString(16);
|
|
1272
|
-
return hex.length & 1 ? "0" + hex : hex;
|
|
1273
|
-
}
|
|
1274
|
-
/**
|
|
1275
|
-
* Parses a big-endian hex string into bigint.
|
|
1276
|
-
* Accepts odd-length hex through the native `BigInt('0x' + hex)` parser and currently surfaces the
|
|
1277
|
-
* same native `SyntaxError` for malformed hex instead of wrapping it in a library-specific error.
|
|
1278
|
-
* @param hex - Hex string without `0x`.
|
|
1279
|
-
* @returns Parsed bigint value.
|
|
1280
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1281
|
-
* @example
|
|
1282
|
-
* Parse a scalar from fixture hex.
|
|
1283
|
-
*
|
|
1284
|
-
* ```ts
|
|
1285
|
-
* hexToNumber('ff');
|
|
1286
|
-
* ```
|
|
1287
|
-
*/
|
|
1288
|
-
function hexToNumber(hex) {
|
|
1289
|
-
if (typeof hex !== "string") throw new TypeError("hex string expected, got " + typeof hex);
|
|
1290
|
-
return hex === "" ? _0n$2 : BigInt("0x" + hex);
|
|
1291
|
-
}
|
|
1292
|
-
/**
|
|
1293
|
-
* Parses big-endian bytes into bigint.
|
|
1294
|
-
* @param bytes - Bytes in big-endian order.
|
|
1295
|
-
* @returns Parsed bigint value.
|
|
1296
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1297
|
-
* @example
|
|
1298
|
-
* Read a scalar encoded in network byte order.
|
|
1299
|
-
*
|
|
1300
|
-
* ```ts
|
|
1301
|
-
* bytesToNumberBE(Uint8Array.of(1, 0));
|
|
1302
|
-
* ```
|
|
1303
|
-
*/
|
|
1304
|
-
function bytesToNumberBE(bytes) {
|
|
1305
|
-
return hexToNumber(bytesToHex$1(bytes));
|
|
1306
|
-
}
|
|
1307
|
-
/**
|
|
1308
|
-
* Parses little-endian bytes into bigint.
|
|
1309
|
-
* @param bytes - Bytes in little-endian order.
|
|
1310
|
-
* @returns Parsed bigint value.
|
|
1311
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1312
|
-
* @example
|
|
1313
|
-
* Read a scalar encoded in little-endian form.
|
|
1314
|
-
*
|
|
1315
|
-
* ```ts
|
|
1316
|
-
* bytesToNumberLE(Uint8Array.of(1, 0));
|
|
1317
|
-
* ```
|
|
1318
|
-
*/
|
|
1319
|
-
function bytesToNumberLE(bytes) {
|
|
1320
|
-
return hexToNumber(bytesToHex$1(copyBytes(abytes$1(bytes)).reverse()));
|
|
1321
|
-
}
|
|
1322
|
-
/**
|
|
1323
|
-
* Encodes a bigint into fixed-length big-endian bytes.
|
|
1324
|
-
* @param n - Number to encode.
|
|
1325
|
-
* @param len - Output length in bytes. Must be greater than zero.
|
|
1326
|
-
* @returns Big-endian byte array.
|
|
1327
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1328
|
-
* @throws If a documented runtime validation or state check fails. {@link Error}
|
|
1329
|
-
* @example
|
|
1330
|
-
* Serialize a scalar into a 32-byte field element.
|
|
1331
|
-
*
|
|
1332
|
-
* ```ts
|
|
1333
|
-
* numberToBytesBE(255n, 2);
|
|
1334
|
-
* ```
|
|
1335
|
-
*/
|
|
1336
|
-
function numberToBytesBE(n, len) {
|
|
1337
|
-
anumber$1(len);
|
|
1338
|
-
if (len === 0) throw new Error("zero output length is invalid");
|
|
1339
|
-
n = abignumber(n);
|
|
1340
|
-
const expectedLen = len * 2;
|
|
1341
|
-
const hex = n.toString(16);
|
|
1342
|
-
if (hex.length > expectedLen) throw new RangeError("number is too large");
|
|
1343
|
-
return hexToBytes$1(hex.padStart(expectedLen, "0"));
|
|
1344
|
-
}
|
|
1345
|
-
/**
|
|
1346
|
-
* Encodes a bigint into fixed-length little-endian bytes.
|
|
1347
|
-
* @param n - Number to encode.
|
|
1348
|
-
* @param len - Output length in bytes.
|
|
1349
|
-
* @returns Little-endian byte array.
|
|
1350
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1351
|
-
* @throws If a documented runtime validation or state check fails. {@link Error}
|
|
1352
|
-
* @example
|
|
1353
|
-
* Serialize a scalar for little-endian protocols.
|
|
1354
|
-
*
|
|
1355
|
-
* ```ts
|
|
1356
|
-
* numberToBytesLE(255n, 2);
|
|
1357
|
-
* ```
|
|
1358
|
-
*/
|
|
1359
|
-
function numberToBytesLE(n, len) {
|
|
1360
|
-
return numberToBytesBE(n, len).reverse();
|
|
1361
|
-
}
|
|
1362
|
-
/**
|
|
1363
|
-
* Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,
|
|
1364
|
-
* and Buffer#slice creates mutable copy. Never use Buffers!
|
|
1365
|
-
* @param bytes - Bytes to copy.
|
|
1366
|
-
* @returns Detached copy.
|
|
1367
|
-
* @example
|
|
1368
|
-
* Make an isolated copy before mutating serialized bytes.
|
|
1369
|
-
*
|
|
1370
|
-
* ```ts
|
|
1371
|
-
* copyBytes(Uint8Array.of(1, 2, 3));
|
|
1372
|
-
* ```
|
|
1373
|
-
*/
|
|
1374
|
-
function copyBytes(bytes) {
|
|
1375
|
-
return Uint8Array.from(abytes(bytes));
|
|
1376
|
-
}
|
|
1377
|
-
/**
|
|
1378
|
-
* Checks whether n is non-negative bigint. Historical name.
|
|
1379
|
-
* @param n - candidate value
|
|
1380
|
-
* @returns `true` when the value is bigint and 0 or larger
|
|
1381
|
-
* @example
|
|
1382
|
-
* Check a candidate scalar before range validation.
|
|
1383
|
-
*
|
|
1384
|
-
* ```ts
|
|
1385
|
-
* isPosBig(2n);
|
|
1386
|
-
* ```
|
|
1387
|
-
*/
|
|
1388
|
-
function isPosBig(n) {
|
|
1389
|
-
return typeof n === "bigint" && _0n$2 <= n;
|
|
1390
|
-
}
|
|
1391
|
-
/**
|
|
1392
|
-
* Checks whether a bigint lies inside a half-open range.
|
|
1393
|
-
* @param n - Candidate value.
|
|
1394
|
-
* @param min - Inclusive lower bound.
|
|
1395
|
-
* @param max - Exclusive upper bound.
|
|
1396
|
-
* @returns `true` when the value is inside the range.
|
|
1397
|
-
* @example
|
|
1398
|
-
* Check whether a candidate scalar fits the field order.
|
|
1399
|
-
*
|
|
1400
|
-
* ```ts
|
|
1401
|
-
* inRange(2n, 1n, 3n);
|
|
1402
|
-
* ```
|
|
1403
|
-
*/
|
|
1404
|
-
function inRange(n, min, max) {
|
|
1405
|
-
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
1406
|
-
}
|
|
1407
|
-
/**
|
|
1408
|
-
* Asserts `min <= n < max`. NOTE: upper bound is exclusive.
|
|
1409
|
-
* @param title - Value label for error messages.
|
|
1410
|
-
* @param n - Candidate value.
|
|
1411
|
-
* @param min - Inclusive lower bound.
|
|
1412
|
-
* @param max - Exclusive upper bound.
|
|
1413
|
-
* Wrong-type inputs are not separated from out-of-range values here: they still flow through the
|
|
1414
|
-
* shared `RangeError` path because this is only a throwing wrapper around `inRange(...)`.
|
|
1415
|
-
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1416
|
-
* @example
|
|
1417
|
-
* Assert that a bigint stays within one half-open range.
|
|
1418
|
-
*
|
|
1419
|
-
* ```ts
|
|
1420
|
-
* aInRange('x', 2n, 1n, 256n);
|
|
1421
|
-
* ```
|
|
1422
|
-
*/
|
|
1423
|
-
function aInRange(title, n, min, max) {
|
|
1424
|
-
if (!inRange(n, min, max)) throw new RangeError("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
1425
|
-
}
|
|
1426
|
-
/**
|
|
1427
|
-
* Calculates amount of bits in a bigint.
|
|
1428
|
-
* Same as `n.toString(2).length`
|
|
1429
|
-
* TODO: merge with nLength in modular
|
|
1430
|
-
* @param n - Value to inspect.
|
|
1431
|
-
* @returns Bit length.
|
|
1432
|
-
* @throws If the value is negative. {@link Error}
|
|
1433
|
-
* @example
|
|
1434
|
-
* Measure the bit length of a scalar before serialization.
|
|
1435
|
-
*
|
|
1436
|
-
* ```ts
|
|
1437
|
-
* bitLen(8n);
|
|
1438
|
-
* ```
|
|
1439
|
-
*/
|
|
1440
|
-
function bitLen(n) {
|
|
1441
|
-
if (n < _0n$2) throw new Error("expected non-negative bigint, got " + n);
|
|
1442
|
-
return n === _0n$2 ? 0 : n.toString(2).length;
|
|
1443
|
-
}
|
|
1444
|
-
/**
|
|
1445
|
-
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
1446
|
-
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
1447
|
-
* @param n - Number of bits. Negative widths are currently passed through to raw bigint shift
|
|
1448
|
-
* semantics and therefore produce `-1n`.
|
|
1449
|
-
* @returns Bitmask value.
|
|
1450
|
-
* @example
|
|
1451
|
-
* Calculate mask for N bits.
|
|
1452
|
-
*
|
|
1453
|
-
* ```ts
|
|
1454
|
-
* bitMask(4);
|
|
1455
|
-
* ```
|
|
1456
|
-
*/
|
|
1457
|
-
var bitMask = (n) => {
|
|
1458
|
-
asafenumber(n, "n");
|
|
1459
|
-
return (_1n$2 << BigInt(n)) - _1n$2;
|
|
1460
|
-
};
|
|
1461
|
-
/**
|
|
1462
|
-
* Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
1463
|
-
* @param hashLen - Hash output size in bytes. Callers are expected to pass a positive length; `0`
|
|
1464
|
-
* is not rejected here and would make the internal generate loop non-progressing.
|
|
1465
|
-
* @param qByteLen - Requested output size in bytes. Callers are expected to pass a positive length.
|
|
1466
|
-
* @param hmacFn - HMAC implementation.
|
|
1467
|
-
* @returns Function that will call DRBG until the predicate returns anything
|
|
1468
|
-
* other than `undefined`.
|
|
1469
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1470
|
-
* @example
|
|
1471
|
-
* Build a deterministic nonce generator for RFC6979-style signing.
|
|
1472
|
-
*
|
|
1473
|
-
* ```ts
|
|
1474
|
-
* import { createHmacDrbg } from '@noble/curves/utils.js';
|
|
1475
|
-
* import { hmac } from '@noble/hashes/hmac.js';
|
|
1476
|
-
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
1477
|
-
* const hmacFn = (key: Uint8Array, msg: Uint8Array) => hmac(sha256, key, msg);
|
|
1478
|
-
* const drbg = createHmacDrbg(32, 32, hmacFn);
|
|
1479
|
-
* const seed = new Uint8Array(32);
|
|
1480
|
-
* drbg(seed, (bytes) => bytes);
|
|
1481
|
-
* ```
|
|
1482
|
-
*/
|
|
1483
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
1484
|
-
anumber$1(hashLen, "hashLen");
|
|
1485
|
-
anumber$1(qByteLen, "qByteLen");
|
|
1486
|
-
if (typeof hmacFn !== "function") throw new TypeError("hmacFn must be a function");
|
|
1487
|
-
const u8n = (len) => new Uint8Array(len);
|
|
1488
|
-
const NULL = Uint8Array.of();
|
|
1489
|
-
const byte0 = Uint8Array.of(0);
|
|
1490
|
-
const byte1 = Uint8Array.of(1);
|
|
1491
|
-
const _maxDrbgIters = 1e3;
|
|
1492
|
-
let v = u8n(hashLen);
|
|
1493
|
-
let k = u8n(hashLen);
|
|
1494
|
-
let i = 0;
|
|
1495
|
-
const reset = () => {
|
|
1496
|
-
v.fill(1);
|
|
1497
|
-
k.fill(0);
|
|
1498
|
-
i = 0;
|
|
1499
|
-
};
|
|
1500
|
-
const h = (...msgs) => hmacFn(k, concatBytes(v, ...msgs));
|
|
1501
|
-
const reseed = (seed = NULL) => {
|
|
1502
|
-
k = h(byte0, seed);
|
|
1503
|
-
v = h();
|
|
1504
|
-
if (seed.length === 0) return;
|
|
1505
|
-
k = h(byte1, seed);
|
|
1506
|
-
v = h();
|
|
1507
|
-
};
|
|
1508
|
-
const gen = () => {
|
|
1509
|
-
if (i++ >= _maxDrbgIters) throw new Error("drbg: tried max amount of iterations");
|
|
1510
|
-
let len = 0;
|
|
1511
|
-
const out = [];
|
|
1512
|
-
while (len < qByteLen) {
|
|
1513
|
-
v = h();
|
|
1514
|
-
const sl = v.slice();
|
|
1515
|
-
out.push(sl);
|
|
1516
|
-
len += v.length;
|
|
1517
|
-
}
|
|
1518
|
-
return concatBytes(...out);
|
|
1519
|
-
};
|
|
1520
|
-
const genUntil = (seed, pred) => {
|
|
1521
|
-
reset();
|
|
1522
|
-
reseed(seed);
|
|
1523
|
-
let res = void 0;
|
|
1524
|
-
while ((res = pred(gen())) === void 0) reseed();
|
|
1525
|
-
reset();
|
|
1526
|
-
return res;
|
|
1527
|
-
};
|
|
1528
|
-
return genUntil;
|
|
1529
|
-
}
|
|
1530
|
-
/**
|
|
1531
|
-
* Validates declared required and optional field types on a plain object.
|
|
1532
|
-
* Extra keys are intentionally ignored because many callers validate only the subset they use from
|
|
1533
|
-
* richer option bags or runtime objects.
|
|
1534
|
-
* This walks field schemas and formats detailed errors, so avoid it on hot paths; use direct
|
|
1535
|
-
* one-line guards such as `aobject()`, `afunction()`, `abool()`, or `asafenumber()` instead.
|
|
1536
|
-
* @param object - Object to validate.
|
|
1537
|
-
* @param fields - Required field types.
|
|
1538
|
-
* @param optFields - Optional field types.
|
|
1539
|
-
* @param title - Object label included in thrown errors.
|
|
1540
|
-
* @throws On wrong argument types. {@link TypeError}
|
|
1541
|
-
* @example
|
|
1542
|
-
* Check user options before building a curve helper.
|
|
1543
|
-
*
|
|
1544
|
-
* ```ts
|
|
1545
|
-
* validateObject({ flag: true }, { flag: 'boolean' });
|
|
1546
|
-
* ```
|
|
1547
|
-
*/
|
|
1548
|
-
function validateObject(object, fields = {}, optFields = {}, title = "object") {
|
|
1549
|
-
aobject(object, title);
|
|
1550
|
-
aobject(fields, "fields");
|
|
1551
|
-
aobject(optFields, "optFields");
|
|
1552
|
-
function checkField(fieldName, expectedType, isOpt) {
|
|
1553
|
-
const label = title === "object" ? `param "${String(fieldName)}"` : `"${title}.${String(fieldName)}"`;
|
|
1554
|
-
const val = object[fieldName];
|
|
1555
|
-
if (!Object.hasOwn(object, fieldName) && (isOpt ? val !== void 0 : expectedType !== "function")) throw new TypeError(`${label} is invalid: expected own property`);
|
|
1556
|
-
if (isOpt && val === void 0) return;
|
|
1557
|
-
const current = typeof val;
|
|
1558
|
-
if (current !== expectedType || val === null) throw new TypeError(`${label} is invalid: expected ${expectedType}, got ${current}`);
|
|
1559
|
-
}
|
|
1560
|
-
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
1561
|
-
iter(fields, false);
|
|
1562
|
-
iter(optFields, true);
|
|
1563
|
-
}
|
|
1564
|
-
//#endregion
|
|
1565
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/curves/2.4.0/766129a921e8dfd2ef1de557b88b41133e3334d958e354fac1f5d17f98661627/node_modules/@noble/curves/abstract/modular.js
|
|
1566
|
-
/**
|
|
1567
|
-
* Utils for modular division and fields.
|
|
1568
|
-
* Field over 11 is a finite (Galois) field is integer number operations `mod 11`.
|
|
1569
|
-
* There is no division: it is replaced by modular multiplicative inverse.
|
|
1570
|
-
* @module
|
|
1571
|
-
*/
|
|
1572
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1573
|
-
var _0n$1 = /* @__PURE__ */ BigInt(0);
|
|
1574
|
-
var _1n$1 = /* @__PURE__ */ BigInt(1);
|
|
1575
|
-
var _2n = /* @__PURE__ */ BigInt(2);
|
|
1576
|
-
var _3n = /* @__PURE__ */ BigInt(3);
|
|
1577
|
-
var _4n$1 = /* @__PURE__ */ BigInt(4);
|
|
1578
|
-
var _5n = /* @__PURE__ */ BigInt(5);
|
|
1579
|
-
var _7n = /* @__PURE__ */ BigInt(7);
|
|
1580
|
-
var _8n = /* @__PURE__ */ BigInt(8);
|
|
1581
|
-
var _9n = /* @__PURE__ */ BigInt(9);
|
|
1582
|
-
var _15n = /* @__PURE__ */ BigInt(15);
|
|
1583
|
-
var _16n = /* @__PURE__ */ BigInt(16);
|
|
1584
|
-
var POW_WINDOWED_MIN = /* @__PURE__ */ BigInt("0x10000000000000000");
|
|
1585
|
-
/**
|
|
1586
|
-
* @param a - Dividend value.
|
|
1587
|
-
* @param b - Positive modulus.
|
|
1588
|
-
* @returns Reduced value in `[0, b)` only when `b` is positive.
|
|
1589
|
-
* @throws If the modulus is not positive. {@link Error}
|
|
1590
|
-
* @example
|
|
1591
|
-
* Normalize a bigint into one field residue.
|
|
1592
|
-
*
|
|
1593
|
-
* ```ts
|
|
1594
|
-
* mod(-1n, 5n);
|
|
1595
|
-
* ```
|
|
1596
|
-
*/
|
|
1597
|
-
function mod(a, b) {
|
|
1598
|
-
if (b <= _0n$1) throw new Error("mod: expected positive modulus, got " + b);
|
|
1599
|
-
const result = a % b;
|
|
1600
|
-
return result >= _0n$1 ? result : b + result;
|
|
1601
|
-
}
|
|
1602
|
-
/**
|
|
1603
|
-
* Efficiently raise num to a power with modular reduction.
|
|
1604
|
-
* Unsafe in some contexts: uses ladder, so can expose bigint bits.
|
|
1605
|
-
* Low-level helper: callers that need canonical residues must pass a valid `num` for the chosen
|
|
1606
|
-
* modulus instead of relying on the `power===0/1` fast paths to normalize it.
|
|
1607
|
-
* @param num - Base value.
|
|
1608
|
-
* @param power - Exponent value.
|
|
1609
|
-
* @param modulo - Reduction modulus.
|
|
1610
|
-
* @returns Modular exponentiation result.
|
|
1611
|
-
* @throws If the modulus or exponent is invalid. {@link Error}
|
|
1612
|
-
* @example
|
|
1613
|
-
* Raise one bigint to a modular power.
|
|
1614
|
-
*
|
|
1615
|
-
* ```ts
|
|
1616
|
-
* pow(2n, 6n, 11n) // 64n % 11n == 9n
|
|
1617
|
-
* ```
|
|
1618
|
-
*/
|
|
1619
|
-
function pow(num, power, modulo) {
|
|
1620
|
-
if (modulo <= _1n$1) throw new Error("pow: expected modulus > 1, got " + modulo);
|
|
1621
|
-
if (typeof power !== "bigint") throw new TypeError("invalid exponent: expected bigint, got " + typeof power);
|
|
1622
|
-
if (power < _0n$1) throw new Error("invalid exponent, negatives unsupported");
|
|
1623
|
-
if (power === _0n$1) return _1n$1;
|
|
1624
|
-
if (power === _1n$1) return num;
|
|
1625
|
-
let d = num % modulo;
|
|
1626
|
-
if (d < _0n$1) d += modulo;
|
|
1627
|
-
if (power < POW_WINDOWED_MIN) {
|
|
1628
|
-
let p = _1n$1;
|
|
1629
|
-
while (power > _0n$1) {
|
|
1630
|
-
if (power & _1n$1) p = p * d % modulo;
|
|
1631
|
-
d = d * d % modulo;
|
|
1632
|
-
power >>= _1n$1;
|
|
1633
|
-
}
|
|
1634
|
-
return p;
|
|
1635
|
-
}
|
|
1636
|
-
const digits = [];
|
|
1637
|
-
while (power > _0n$1) {
|
|
1638
|
-
digits.push(Number(power & _15n));
|
|
1639
|
-
power >>= _4n$1;
|
|
1640
|
-
}
|
|
1641
|
-
const table = new Array(16);
|
|
1642
|
-
table[0] = _1n$1;
|
|
1643
|
-
table[1] = d;
|
|
1644
|
-
for (let i = 2; i < 16; i++) table[i] = table[i - 1] * d % modulo;
|
|
1645
|
-
let p = table[digits[digits.length - 1]];
|
|
1646
|
-
for (let w = digits.length - 2; w >= 0; w--) {
|
|
1647
|
-
p = p * p % modulo;
|
|
1648
|
-
p = p * p % modulo;
|
|
1649
|
-
p = p * p % modulo;
|
|
1650
|
-
p = p * p % modulo;
|
|
1651
|
-
const digit = digits[w];
|
|
1652
|
-
if (digit !== 0) p = p * table[digit] % modulo;
|
|
1653
|
-
}
|
|
1654
|
-
return p;
|
|
1655
|
-
}
|
|
1656
|
-
/**
|
|
1657
|
-
* Does `x^(2^power)` mod p. `pow2(30, 4)` == `30^(2^4)`.
|
|
1658
|
-
* Low-level helper: callers that need canonical residues must pass a valid `x` for the chosen
|
|
1659
|
-
* modulus; the `power===0` fast path intentionally returns the input unchanged.
|
|
1660
|
-
* @param x - Base value.
|
|
1661
|
-
* @param power - Number of squarings.
|
|
1662
|
-
* @param modulo - Reduction modulus.
|
|
1663
|
-
* @returns Repeated-squaring result.
|
|
1664
|
-
* @throws If the exponent is negative. {@link Error}
|
|
1665
|
-
* @example
|
|
1666
|
-
* Apply repeated squaring inside one field.
|
|
1667
|
-
*
|
|
1668
|
-
* ```ts
|
|
1669
|
-
* pow2(3n, 2n, 11n);
|
|
1670
|
-
* ```
|
|
1671
|
-
*/
|
|
1672
|
-
function pow2(x, power, modulo) {
|
|
1673
|
-
if (modulo <= _1n$1) throw new Error("pow2: expected modulus > 1, got " + modulo);
|
|
1674
|
-
if (power < _0n$1) throw new Error("pow2: expected non-negative exponent, got " + power);
|
|
1675
|
-
let res = x;
|
|
1676
|
-
while (power-- > _0n$1) {
|
|
1677
|
-
res *= res;
|
|
1678
|
-
res %= modulo;
|
|
1679
|
-
}
|
|
1680
|
-
return res;
|
|
1681
|
-
}
|
|
1682
|
-
/**
|
|
1683
|
-
* Inverses number over modulo.
|
|
1684
|
-
* Implemented using the {@link https://brilliant.org/wiki/extended-euclidean-algorithm/ | extended Euclidean algorithm}.
|
|
1685
|
-
* @param number - Value to invert.
|
|
1686
|
-
* @param modulo - Modulus greater than 1.
|
|
1687
|
-
* @returns Multiplicative inverse.
|
|
1688
|
-
* @throws If the modulus is invalid or the inverse does not exist. {@link Error}
|
|
1689
|
-
* @example
|
|
1690
|
-
* Compute one modular inverse with the extended Euclidean algorithm.
|
|
1691
|
-
*
|
|
1692
|
-
* ```ts
|
|
1693
|
-
* invert(3n, 11n);
|
|
1694
|
-
* ```
|
|
1695
|
-
*/
|
|
1696
|
-
function invert(number, modulo) {
|
|
1697
|
-
if (number === _0n$1) throw new Error("invert: expected non-zero number");
|
|
1698
|
-
if (modulo <= _1n$1) throw new Error("invert: expected modulus > 1, got " + modulo);
|
|
1699
|
-
let a = mod(number, modulo);
|
|
1700
|
-
let b = modulo;
|
|
1701
|
-
let x = _0n$1, u = _1n$1;
|
|
1702
|
-
while (a !== _0n$1) {
|
|
1703
|
-
const q = b / a;
|
|
1704
|
-
const r = b - a * q;
|
|
1705
|
-
const m = x - u * q;
|
|
1706
|
-
b = a, a = r, x = u, u = m;
|
|
1707
|
-
}
|
|
1708
|
-
if (b !== _1n$1) throw new Error("invert: does not exist");
|
|
1709
|
-
return mod(x, modulo);
|
|
1710
|
-
}
|
|
1711
|
-
/**
|
|
1712
|
-
* Inverses number over modulo using Fermat's little theorem: `a^(p-2) ≡ a⁻¹ (mod p)`.
|
|
1713
|
-
*
|
|
1714
|
-
* Unlike {@link invert} (extended Euclidean), the exponent `p-2` is a public constant, so the
|
|
1715
|
-
* underlying square-and-multiply has the same control flow for every secret `a`: there is no
|
|
1716
|
-
* data-dependent branching or loop count that could leak `a` through timing (e.g. Minerva-style
|
|
1717
|
-
* ECDSA nonce-inversion attacks). This is only "algorithmically" constant-time — JS bigint
|
|
1718
|
-
* multiplication/reduction is still value-dependent — and it is roughly 4x slower than
|
|
1719
|
-
* {@link invert}.
|
|
1720
|
-
*
|
|
1721
|
-
* REQUIRES a prime modulus; Fermat's theorem does not hold otherwise. The result is verified to be
|
|
1722
|
-
* a real inverse, so a non-prime modulus (or a non-invertible input) fails closed with an error
|
|
1723
|
-
* instead of returning a wrong value.
|
|
1724
|
-
* @param a - Value to invert.
|
|
1725
|
-
* @param prime - Prime modulus.
|
|
1726
|
-
* @returns Multiplicative inverse in `[1, prime)`.
|
|
1727
|
-
* @throws If the modulus is below 2, the input reduces to zero, or the inverse does not exist.
|
|
1728
|
-
* {@link Error}
|
|
1729
|
-
* @example
|
|
1730
|
-
* Compute one modular inverse without secret-dependent branching.
|
|
1731
|
-
*
|
|
1732
|
-
* ```ts
|
|
1733
|
-
* invertCt(3n, 11n); // 4n, since 3 * 4 = 12 ≡ 1 (mod 11)
|
|
1734
|
-
* ```
|
|
1735
|
-
*/
|
|
1736
|
-
function invertCt(a, prime) {
|
|
1737
|
-
if (prime <= _1n$1) throw new Error("invertCt: expected prime modulus > 1, got " + prime);
|
|
1738
|
-
const an = mod(a, prime);
|
|
1739
|
-
if (an === _0n$1) throw new Error("invertCt: expected non-zero number");
|
|
1740
|
-
const inverse = pow(an, prime - _2n, prime);
|
|
1741
|
-
if (mod(an * inverse, prime) !== _1n$1) throw new Error("invertCt: does not exist");
|
|
1742
|
-
return inverse;
|
|
1743
|
-
}
|
|
1744
|
-
function assertIsSquare(Fp, root, n) {
|
|
1745
|
-
const F = Fp;
|
|
1746
|
-
if (!F.eql(F.sqr(root), n)) throw new Error("Cannot find square root");
|
|
1747
|
-
}
|
|
1748
|
-
function aoddModulus(order, fnName) {
|
|
1749
|
-
if ((order & _1n$1) === _0n$1) throw new Error(fnName + ": expected odd modulus, got " + order);
|
|
1750
|
-
}
|
|
1751
|
-
function sqrt3mod4(Fp, n) {
|
|
1752
|
-
const F = Fp;
|
|
1753
|
-
const p1div4 = (F.ORDER + _1n$1) / _4n$1;
|
|
1754
|
-
const root = F.pow(n, p1div4);
|
|
1755
|
-
assertIsSquare(F, root, n);
|
|
1756
|
-
return root;
|
|
1757
|
-
}
|
|
1758
|
-
function sqrt5mod8(Fp, n) {
|
|
1759
|
-
const F = Fp;
|
|
1760
|
-
const p5div8 = (F.ORDER - _5n) / _8n;
|
|
1761
|
-
const n2 = F.mul(n, _2n);
|
|
1762
|
-
const v = F.pow(n2, p5div8);
|
|
1763
|
-
const nv = F.mul(n, v);
|
|
1764
|
-
const i = F.mul(F.mul(nv, _2n), v);
|
|
1765
|
-
const root = F.mul(nv, F.sub(i, F.ONE));
|
|
1766
|
-
assertIsSquare(F, root, n);
|
|
1767
|
-
return root;
|
|
1768
|
-
}
|
|
1769
|
-
function sqrt9mod16(P) {
|
|
1770
|
-
const Fp_ = Field(P);
|
|
1771
|
-
const tn = tonelliShanks(P);
|
|
1772
|
-
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
1773
|
-
const c2 = tn(Fp_, c1);
|
|
1774
|
-
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
1775
|
-
const c4 = (P + _7n) / _16n;
|
|
1776
|
-
return ((Fp, n) => {
|
|
1777
|
-
const F = Fp;
|
|
1778
|
-
let tv1 = F.pow(n, c4);
|
|
1779
|
-
let tv2 = F.mul(tv1, c1);
|
|
1780
|
-
const tv3 = F.mul(tv1, c2);
|
|
1781
|
-
const tv4 = F.mul(tv1, c3);
|
|
1782
|
-
const e1 = F.eql(F.sqr(tv2), n);
|
|
1783
|
-
const e2 = F.eql(F.sqr(tv3), n);
|
|
1784
|
-
tv1 = F.cmov(tv1, tv2, e1);
|
|
1785
|
-
tv2 = F.cmov(tv4, tv3, e2);
|
|
1786
|
-
const e3 = F.eql(F.sqr(tv2), n);
|
|
1787
|
-
const root = F.cmov(tv1, tv2, e3);
|
|
1788
|
-
assertIsSquare(F, root, n);
|
|
1789
|
-
return root;
|
|
1790
|
-
});
|
|
1791
|
-
}
|
|
1792
|
-
/**
|
|
1793
|
-
* Tonelli-Shanks square root search algorithm.
|
|
1794
|
-
* This implementation is variable-time: it searches data-dependently for the first non-residue `Z`
|
|
1795
|
-
* and for the smallest `i` in the main loop, unlike RFC 9380 Appendix I.4's constant-time shape.
|
|
1796
|
-
* 1. {@link https://eprint.iacr.org/2012/685.pdf | eprint 2012/685}, page 12
|
|
1797
|
-
* 2. Square Roots from 1; 24, 51, 10 to Dan Shanks
|
|
1798
|
-
* @param P - field order
|
|
1799
|
-
* @returns function that takes field Fp (created from P) and number n
|
|
1800
|
-
* @throws If the field is too small, non-prime, or the square root does not exist. {@link Error}
|
|
1801
|
-
* @example
|
|
1802
|
-
* Construct a square-root helper for primes that need Tonelli-Shanks.
|
|
1803
|
-
*
|
|
1804
|
-
* ```ts
|
|
1805
|
-
* import { Field, tonelliShanks } from '@noble/curves/abstract/modular.js';
|
|
1806
|
-
* const Fp = Field(17n);
|
|
1807
|
-
* const sqrt = tonelliShanks(17n)(Fp, 4n);
|
|
1808
|
-
* ```
|
|
1809
|
-
*/
|
|
1810
|
-
function tonelliShanks(P) {
|
|
1811
|
-
if (P < _3n) throw new Error("sqrt is not defined for small field");
|
|
1812
|
-
aoddModulus(P, "tonelliShanks");
|
|
1813
|
-
let Q = P - _1n$1;
|
|
1814
|
-
let S = 0;
|
|
1815
|
-
while (Q % _2n === _0n$1) {
|
|
1816
|
-
Q /= _2n;
|
|
1817
|
-
S++;
|
|
1818
|
-
}
|
|
1819
|
-
let Z = _2n;
|
|
1820
|
-
const _Fp = Field(P);
|
|
1821
|
-
while (FpLegendre(_Fp, Z) === 1) if (Z++ > 1e3) throw new Error("Cannot find square root: probably non-prime P");
|
|
1822
|
-
if (S === 1) return sqrt3mod4;
|
|
1823
|
-
let cc = _Fp.pow(Z, Q);
|
|
1824
|
-
const Q1div2 = (Q + _1n$1) / _2n;
|
|
1825
|
-
return function tonelliSlow(Fp, n) {
|
|
1826
|
-
const F = Fp;
|
|
1827
|
-
if (F.is0(n)) return n;
|
|
1828
|
-
if (FpLegendre(F, n) !== 1) throw new Error("Cannot find square root");
|
|
1829
|
-
let M = S;
|
|
1830
|
-
let c = F.mul(F.ONE, cc);
|
|
1831
|
-
let t = F.pow(n, Q);
|
|
1832
|
-
let R = F.pow(n, Q1div2);
|
|
1833
|
-
while (!F.eql(t, F.ONE)) {
|
|
1834
|
-
if (F.is0(t)) throw new Error("Cannot find square root: probably non-prime P");
|
|
1835
|
-
let i = 1;
|
|
1836
|
-
let t_tmp = F.sqr(t);
|
|
1837
|
-
while (!F.eql(t_tmp, F.ONE)) {
|
|
1838
|
-
i++;
|
|
1839
|
-
t_tmp = F.sqr(t_tmp);
|
|
1840
|
-
if (i === M) throw new Error("Cannot find square root");
|
|
1841
|
-
}
|
|
1842
|
-
const exponent = _1n$1 << BigInt(M - i - 1);
|
|
1843
|
-
const b = F.pow(c, exponent);
|
|
1844
|
-
M = i;
|
|
1845
|
-
c = F.sqr(b);
|
|
1846
|
-
t = F.mul(t, c);
|
|
1847
|
-
R = F.mul(R, b);
|
|
1848
|
-
}
|
|
1849
|
-
return R;
|
|
1850
|
-
};
|
|
1851
|
-
}
|
|
1852
|
-
/**
|
|
1853
|
-
* Square root for a finite field. Will try optimized versions first:
|
|
1854
|
-
*
|
|
1855
|
-
* 1. P ≡ 3 (mod 4)
|
|
1856
|
-
* 2. P ≡ 5 (mod 8)
|
|
1857
|
-
* 3. P ≡ 9 (mod 16)
|
|
1858
|
-
* 4. Tonelli-Shanks algorithm
|
|
1859
|
-
*
|
|
1860
|
-
* Different algorithms can give different roots, it is up to user to decide which one they want.
|
|
1861
|
-
* For example there is FpSqrtOdd/FpSqrtEven to choose a root by oddness
|
|
1862
|
-
* (used for hash-to-curve).
|
|
1863
|
-
* @param P - Field order.
|
|
1864
|
-
* @returns Square-root helper. The generic fallback inherits Tonelli-Shanks' variable-time
|
|
1865
|
-
* behavior and this selector assumes prime-field-style integer moduli.
|
|
1866
|
-
* @throws If the field is unsupported or the square root does not exist. {@link Error}
|
|
1867
|
-
* @example
|
|
1868
|
-
* Choose the square-root helper appropriate for one field modulus.
|
|
1869
|
-
*
|
|
1870
|
-
* ```ts
|
|
1871
|
-
* import { Field, FpSqrt } from '@noble/curves/abstract/modular.js';
|
|
1872
|
-
* const Fp = Field(17n);
|
|
1873
|
-
* const sqrt = FpSqrt(17n)(Fp, 4n);
|
|
1874
|
-
* ```
|
|
1875
|
-
*/
|
|
1876
|
-
function FpSqrt(P) {
|
|
1877
|
-
aoddModulus(P, "Fp.sqrt");
|
|
1878
|
-
if (P % _4n$1 === _3n) return sqrt3mod4;
|
|
1879
|
-
if (P % _8n === _5n) return sqrt5mod8;
|
|
1880
|
-
if (P % _16n === _9n) return sqrt9mod16(P);
|
|
1881
|
-
return tonelliShanks(P);
|
|
1882
|
-
}
|
|
1883
|
-
/**
|
|
1884
|
-
* @param num - Value to inspect.
|
|
1885
|
-
* @param modulo - Field modulus.
|
|
1886
|
-
* @returns `true` when the least-significant little-endian bit is set.
|
|
1887
|
-
* @throws If the modulus is invalid for `mod(...)`. {@link Error}
|
|
1888
|
-
* @example
|
|
1889
|
-
* Inspect the low bit used by little-endian sign conventions.
|
|
1890
|
-
*
|
|
1891
|
-
* ```ts
|
|
1892
|
-
* isNegativeLE(3n, 11n);
|
|
1893
|
-
* ```
|
|
1894
|
-
*/
|
|
1895
|
-
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n$1) === _1n$1;
|
|
1896
|
-
var FIELD_FIELDS = [
|
|
1897
|
-
"create",
|
|
1898
|
-
"isValid",
|
|
1899
|
-
"is0",
|
|
1900
|
-
"neg",
|
|
1901
|
-
"inv",
|
|
1902
|
-
"sqrt",
|
|
1903
|
-
"sqr",
|
|
1904
|
-
"eql",
|
|
1905
|
-
"add",
|
|
1906
|
-
"sub",
|
|
1907
|
-
"mul",
|
|
1908
|
-
"pow",
|
|
1909
|
-
"div",
|
|
1910
|
-
"addN",
|
|
1911
|
-
"subN",
|
|
1912
|
-
"mulN",
|
|
1913
|
-
"sqrN"
|
|
1914
|
-
];
|
|
1915
|
-
/**
|
|
1916
|
-
* @param field - Field implementation.
|
|
1917
|
-
* @returns Validated field. This only checks the arithmetic subset needed by generic helpers; it
|
|
1918
|
-
* does not guarantee full runtime-method coverage for serialization, batching, `cmov`, or
|
|
1919
|
-
* field-specific extras beyond positive `BYTES` / `BITS`.
|
|
1920
|
-
* @throws If the field shape or numeric metadata are invalid. {@link Error}
|
|
1921
|
-
* @example
|
|
1922
|
-
* Check that a field implementation exposes the operations curve code expects.
|
|
1923
|
-
*
|
|
1924
|
-
* ```ts
|
|
1925
|
-
* import { Field, validateField } from '@noble/curves/abstract/modular.js';
|
|
1926
|
-
* const Fp = validateField(Field(17n));
|
|
1927
|
-
* ```
|
|
1928
|
-
*/
|
|
1929
|
-
function validateField(field) {
|
|
1930
|
-
aobject(field, "field");
|
|
1931
|
-
if (typeof field.ORDER !== "bigint") throw new TypeError("param \"ORDER\" is invalid: expected bigint, got " + typeof field.ORDER);
|
|
1932
|
-
asafenumber(field.BYTES, "BYTES");
|
|
1933
|
-
asafenumber(field.BITS, "BITS");
|
|
1934
|
-
for (const name of FIELD_FIELDS) afunction(field[name], "field." + name);
|
|
1935
|
-
if (field.BYTES < 1 || field.BITS < 1) throw new Error("invalid field: expected BYTES/BITS > 0");
|
|
1936
|
-
if (field.ORDER <= _1n$1) throw new Error("invalid field: expected ORDER > 1, got " + field.ORDER);
|
|
1937
|
-
return field;
|
|
1938
|
-
}
|
|
1939
|
-
function FpInvertBatch(Fp, nums, passZero = false) {
|
|
1940
|
-
validateField(Fp);
|
|
1941
|
-
aarray(nums, "nums");
|
|
1942
|
-
abool(passZero, "passZero");
|
|
1943
|
-
const F = Fp;
|
|
1944
|
-
const inverted = new Array(nums.length).fill(passZero ? F.ZERO : void 0);
|
|
1945
|
-
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
1946
|
-
if (F.is0(num)) return acc;
|
|
1947
|
-
inverted[i] = acc;
|
|
1948
|
-
return F.mul(acc, num);
|
|
1949
|
-
}, F.ONE);
|
|
1950
|
-
const invertedAcc = F.inv(multipliedAcc);
|
|
1951
|
-
nums.reduceRight((acc, num, i) => {
|
|
1952
|
-
if (F.is0(num)) return acc;
|
|
1953
|
-
inverted[i] = F.mul(acc, inverted[i]);
|
|
1954
|
-
return F.mul(acc, num);
|
|
1955
|
-
}, invertedAcc);
|
|
1956
|
-
return inverted;
|
|
1957
|
-
}
|
|
1958
|
-
/**
|
|
1959
|
-
* Legendre symbol.
|
|
1960
|
-
* Legendre constant is used to calculate Legendre symbol (a | p)
|
|
1961
|
-
* which denotes the value of a^((p-1)/2) (mod p).
|
|
1962
|
-
*
|
|
1963
|
-
* * (a | p) ≡ 1 if a is a square (mod p), quadratic residue
|
|
1964
|
-
* * (a | p) ≡ -1 if a is not a square (mod p), quadratic non residue
|
|
1965
|
-
* * (a | p) ≡ 0 if a ≡ 0 (mod p)
|
|
1966
|
-
* @param Fp - Field implementation.
|
|
1967
|
-
* @param n - Value to inspect.
|
|
1968
|
-
* @returns Legendre symbol.
|
|
1969
|
-
* @throws If the powered value does not match a valid Legendre symbol. {@link Error}
|
|
1970
|
-
* @example
|
|
1971
|
-
* Compute the Legendre symbol of one field element.
|
|
1972
|
-
*
|
|
1973
|
-
* ```ts
|
|
1974
|
-
* import { Field, FpLegendre } from '@noble/curves/abstract/modular.js';
|
|
1975
|
-
* const Fp = Field(17n);
|
|
1976
|
-
* const symbol = FpLegendre(Fp, 4n);
|
|
1977
|
-
* ```
|
|
1978
|
-
*/
|
|
1979
|
-
function FpLegendre(Fp, n) {
|
|
1980
|
-
validateField(Fp);
|
|
1981
|
-
const F = Fp;
|
|
1982
|
-
aoddModulus(F.ORDER, "FpLegendre");
|
|
1983
|
-
const p1mod2 = (F.ORDER - _1n$1) / _2n;
|
|
1984
|
-
const powered = F.pow(n, p1mod2);
|
|
1985
|
-
const yes = F.eql(powered, F.ONE);
|
|
1986
|
-
const zero = F.eql(powered, F.ZERO);
|
|
1987
|
-
const no = F.eql(powered, F.neg(F.ONE));
|
|
1988
|
-
if (!yes && !zero && !no) throw new Error("invalid Legendre symbol result");
|
|
1989
|
-
return yes ? 1 : zero ? 0 : -1;
|
|
1990
|
-
}
|
|
1991
|
-
/**
|
|
1992
|
-
* @param n - Curve order. Callers are expected to pass a positive order.
|
|
1993
|
-
* @param nBitLength - Optional cached bit length. Callers are expected to pass a positive cached
|
|
1994
|
-
* value when overriding the derived bit length.
|
|
1995
|
-
* @returns Byte and bit lengths.
|
|
1996
|
-
* @throws If the order or cached bit length is invalid. {@link Error}
|
|
1997
|
-
* @example
|
|
1998
|
-
* Measure the encoding sizes needed for one modulus.
|
|
1999
|
-
*
|
|
2000
|
-
* ```ts
|
|
2001
|
-
* nLength(255n);
|
|
2002
|
-
* ```
|
|
2003
|
-
*/
|
|
2004
|
-
function nLength(n, nBitLength) {
|
|
2005
|
-
if (nBitLength !== void 0) anumber(nBitLength);
|
|
2006
|
-
if (n <= _0n$1) throw new Error("invalid n length: expected positive n, got " + n);
|
|
2007
|
-
if (nBitLength !== void 0 && nBitLength < 1) throw new Error("invalid n length: expected positive bit length, got " + nBitLength);
|
|
2008
|
-
const bits = bitLen(n);
|
|
2009
|
-
if (nBitLength !== void 0 && nBitLength < bits) throw new Error(`invalid n length: expected nBitLength (${nBitLength}) >= bitLen(n) (${bits})`);
|
|
2010
|
-
const _nBitLength = nBitLength !== void 0 ? nBitLength : bits;
|
|
2011
|
-
return {
|
|
2012
|
-
nBitLength: _nBitLength,
|
|
2013
|
-
nByteLength: Math.ceil(_nBitLength / 8)
|
|
2014
|
-
};
|
|
2015
|
-
}
|
|
2016
|
-
var FIELD_SQRT = /* @__PURE__ */ new WeakMap();
|
|
2017
|
-
var _Field = class {
|
|
2018
|
-
ORDER;
|
|
2019
|
-
BITS;
|
|
2020
|
-
BYTES;
|
|
2021
|
-
isLE;
|
|
2022
|
-
ZERO = _0n$1;
|
|
2023
|
-
ONE = _1n$1;
|
|
2024
|
-
_lengths;
|
|
2025
|
-
_mod;
|
|
2026
|
-
constructor(ORDER, opts = {}) {
|
|
2027
|
-
if (ORDER <= _1n$1) throw new Error("invalid field: expected ORDER > 1, got " + ORDER);
|
|
2028
|
-
let _nbitLength = void 0;
|
|
2029
|
-
this.isLE = false;
|
|
2030
|
-
if (opts != null && typeof opts === "object") {
|
|
2031
|
-
if (typeof opts.BITS === "number") _nbitLength = opts.BITS;
|
|
2032
|
-
if (typeof opts.sqrt === "function") Object.defineProperty(this, "sqrt", {
|
|
2033
|
-
value: opts.sqrt,
|
|
2034
|
-
enumerable: true
|
|
2035
|
-
});
|
|
2036
|
-
if (typeof opts.isLE === "boolean") this.isLE = opts.isLE;
|
|
2037
|
-
if (opts.allowedLengths) this._lengths = Object.freeze(opts.allowedLengths.slice());
|
|
2038
|
-
if (typeof opts.modFromBytes === "boolean") this._mod = opts.modFromBytes;
|
|
2039
|
-
}
|
|
2040
|
-
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
2041
|
-
if (nByteLength > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
2042
|
-
this.ORDER = ORDER;
|
|
2043
|
-
this.BITS = nBitLength;
|
|
2044
|
-
this.BYTES = nByteLength;
|
|
2045
|
-
Object.freeze(this);
|
|
2046
|
-
}
|
|
2047
|
-
create(num) {
|
|
2048
|
-
return mod(num, this.ORDER);
|
|
2049
|
-
}
|
|
2050
|
-
isValid(num) {
|
|
2051
|
-
if (typeof num !== "bigint") throw new TypeError("invalid field element: expected bigint, got " + typeof num);
|
|
2052
|
-
return _0n$1 <= num && num < this.ORDER;
|
|
2053
|
-
}
|
|
2054
|
-
is0(num) {
|
|
2055
|
-
return num === _0n$1;
|
|
2056
|
-
}
|
|
2057
|
-
isValidNot0(num) {
|
|
2058
|
-
return !this.is0(num) && this.isValid(num);
|
|
2059
|
-
}
|
|
2060
|
-
isOdd(num) {
|
|
2061
|
-
return (num & _1n$1) === _1n$1;
|
|
2062
|
-
}
|
|
2063
|
-
neg(num) {
|
|
2064
|
-
return mod(-num, this.ORDER);
|
|
2065
|
-
}
|
|
2066
|
-
eql(lhs, rhs) {
|
|
2067
|
-
return lhs === rhs;
|
|
2068
|
-
}
|
|
2069
|
-
sqr(num) {
|
|
2070
|
-
return mod(num * num, this.ORDER);
|
|
2071
|
-
}
|
|
2072
|
-
add(lhs, rhs) {
|
|
2073
|
-
return mod(lhs + rhs, this.ORDER);
|
|
2074
|
-
}
|
|
2075
|
-
sub(lhs, rhs) {
|
|
2076
|
-
return mod(lhs - rhs, this.ORDER);
|
|
2077
|
-
}
|
|
2078
|
-
mul(lhs, rhs) {
|
|
2079
|
-
return mod(lhs * rhs, this.ORDER);
|
|
2080
|
-
}
|
|
2081
|
-
pow(num, power) {
|
|
2082
|
-
return pow(num, power, this.ORDER);
|
|
2083
|
-
}
|
|
2084
|
-
div(lhs, rhs) {
|
|
2085
|
-
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
2086
|
-
}
|
|
2087
|
-
sqrN(num) {
|
|
2088
|
-
return num * num;
|
|
2089
|
-
}
|
|
2090
|
-
addN(lhs, rhs) {
|
|
2091
|
-
return lhs + rhs;
|
|
2092
|
-
}
|
|
2093
|
-
subN(lhs, rhs) {
|
|
2094
|
-
return lhs - rhs;
|
|
2095
|
-
}
|
|
2096
|
-
mulN(lhs, rhs) {
|
|
2097
|
-
return lhs * rhs;
|
|
2098
|
-
}
|
|
2099
|
-
inv(num) {
|
|
2100
|
-
return invert(num, this.ORDER);
|
|
2101
|
-
}
|
|
2102
|
-
sqrt(num) {
|
|
2103
|
-
let sqrt = FIELD_SQRT.get(this);
|
|
2104
|
-
if (!sqrt) FIELD_SQRT.set(this, sqrt = FpSqrt(this.ORDER));
|
|
2105
|
-
return sqrt(this, num);
|
|
2106
|
-
}
|
|
2107
|
-
toBytes(num) {
|
|
2108
|
-
return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
|
|
2109
|
-
}
|
|
2110
|
-
fromBytes(bytes, skipValidation = false) {
|
|
2111
|
-
abytes(bytes);
|
|
2112
|
-
const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
2113
|
-
if (allowedLengths) {
|
|
2114
|
-
if (bytes.length < 1 || !allowedLengths.includes(bytes.length) || bytes.length > BYTES) throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
2115
|
-
const padded = new Uint8Array(BYTES);
|
|
2116
|
-
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
2117
|
-
bytes = padded;
|
|
2118
|
-
}
|
|
2119
|
-
if (bytes.length !== BYTES) throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
2120
|
-
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
2121
|
-
if (modFromBytes) scalar = mod(scalar, ORDER);
|
|
2122
|
-
if (!skipValidation) {
|
|
2123
|
-
if (!this.isValid(scalar)) throw new Error("invalid field element: outside of range 0..ORDER");
|
|
2124
|
-
}
|
|
2125
|
-
return scalar;
|
|
2126
|
-
}
|
|
2127
|
-
invertBatch(lst) {
|
|
2128
|
-
return FpInvertBatch(this, lst, true);
|
|
2129
|
-
}
|
|
2130
|
-
cmov(a, b, condition) {
|
|
2131
|
-
abool(condition, "condition");
|
|
2132
|
-
return condition ? b : a;
|
|
2133
|
-
}
|
|
2134
|
-
};
|
|
2135
|
-
/**
|
|
2136
|
-
* Creates a finite field. Major performance optimizations:
|
|
2137
|
-
* * 1. Denormalized operations like mulN instead of mul.
|
|
2138
|
-
* * 2. Identical object shape: never add or remove keys.
|
|
2139
|
-
* * 3. Frozen stable object shape; the lazy sqrt cache lives in a module-level `WeakMap`.
|
|
2140
|
-
* Fragile: always run a benchmark on a change.
|
|
2141
|
-
* Security note: operations and low-level serializers like `toBytes` don't check `isValid` for
|
|
2142
|
-
* all elements for performance and protocol-flexibility reasons; callers are responsible for
|
|
2143
|
-
* supplying valid elements when they need canonical field behavior.
|
|
2144
|
-
* This is low-level code, please make sure you know what you're doing.
|
|
2145
|
-
*
|
|
2146
|
-
* Note about field properties:
|
|
2147
|
-
* * CHARACTERISTIC p = prime number, number of elements in main subgroup.
|
|
2148
|
-
* * ORDER q = similar to cofactor in curves, may be composite `q = p^m`.
|
|
2149
|
-
*
|
|
2150
|
-
* @param ORDER - field order, probably prime, or could be composite
|
|
2151
|
-
* @param opts - Field options such as bit length or endianness. See {@link FieldOpts}.
|
|
2152
|
-
* @returns Frozen field instance with a stable object shape. This wrapper forwards `opts` straight
|
|
2153
|
-
* into `_Field`, so it inherits `_Field`'s assumptions about cached sizes and `allowedLengths`.
|
|
2154
|
-
* @example
|
|
2155
|
-
* Construct one prime field with optional overrides.
|
|
2156
|
-
*
|
|
2157
|
-
* ```ts
|
|
2158
|
-
* Field(11n);
|
|
2159
|
-
* ```
|
|
2160
|
-
*/
|
|
2161
|
-
function Field(ORDER, opts = {}) {
|
|
2162
|
-
Object.freeze(_Field.prototype);
|
|
2163
|
-
return new _Field(ORDER, opts);
|
|
2164
|
-
}
|
|
2165
|
-
/**
|
|
2166
|
-
* Returns total number of bytes consumed by the field element.
|
|
2167
|
-
* For example, 32 bytes for usual 256-bit weierstrass curve.
|
|
2168
|
-
* @param fieldOrder - number of field elements, usually CURVE.n. Callers are expected to pass an
|
|
2169
|
-
* order greater than 1.
|
|
2170
|
-
* @returns byte length of field
|
|
2171
|
-
* @throws If the field order is not a bigint. {@link Error}
|
|
2172
|
-
* @example
|
|
2173
|
-
* Read the fixed-width byte length of one field.
|
|
2174
|
-
*
|
|
2175
|
-
* ```ts
|
|
2176
|
-
* getFieldBytesLength(255n);
|
|
2177
|
-
* ```
|
|
2178
|
-
*/
|
|
2179
|
-
function getFieldBytesLength(fieldOrder) {
|
|
2180
|
-
if (typeof fieldOrder !== "bigint") throw new Error("field order must be bigint");
|
|
2181
|
-
if (fieldOrder <= _1n$1) throw new Error("field order must be greater than 1");
|
|
2182
|
-
const bitLength = bitLen(fieldOrder - _1n$1);
|
|
2183
|
-
return Math.ceil(bitLength / 8);
|
|
2184
|
-
}
|
|
2185
|
-
/**
|
|
2186
|
-
* Returns minimal amount of bytes that can be safely reduced
|
|
2187
|
-
* by field order.
|
|
2188
|
-
* Should be 2^-128 for 128-bit curve such as P256.
|
|
2189
|
-
* This is the reduction / modulo-bias lower bound; higher-level helpers may still impose a larger
|
|
2190
|
-
* absolute floor for policy reasons.
|
|
2191
|
-
* @param fieldOrder - number of field elements greater than 1, usually CURVE.n.
|
|
2192
|
-
* @returns byte length of target hash
|
|
2193
|
-
* @throws If the field order is invalid. {@link Error}
|
|
2194
|
-
* @example
|
|
2195
|
-
* Compute the minimum hash length needed for field reduction.
|
|
2196
|
-
*
|
|
2197
|
-
* ```ts
|
|
2198
|
-
* getMinHashLength(255n);
|
|
2199
|
-
* ```
|
|
2200
|
-
*/
|
|
2201
|
-
function getMinHashLength(fieldOrder) {
|
|
2202
|
-
const length = getFieldBytesLength(fieldOrder);
|
|
2203
|
-
return length + Math.ceil(length / 2);
|
|
2204
|
-
}
|
|
2205
|
-
/**
|
|
2206
|
-
* "Constant-time" private key generation utility.
|
|
2207
|
-
* Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF
|
|
2208
|
-
* and convert them into private scalar, with the modulo bias being negligible.
|
|
2209
|
-
* Needs at least 48 bytes of input for 32-byte private key. The implementation also keeps a hard
|
|
2210
|
-
* 16-byte minimum even when `getMinHashLength(...)` is smaller, so toy-small inputs do not look
|
|
2211
|
-
* accidentally acceptable for real scalar derivation.
|
|
2212
|
-
* See {@link https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/ | Kudelski's modulo-bias guide},
|
|
2213
|
-
* {@link https://csrc.nist.gov/publications/detail/fips/186/5/final | FIPS 186-5 appendix A.2}, and
|
|
2214
|
-
* {@link https://www.rfc-editor.org/rfc/rfc9380#section-5 | RFC 9380 section 5}. Unlike RFC 9380
|
|
2215
|
-
* `hash_to_field`, this helper intentionally maps into the non-zero private-scalar range `1..n-1`.
|
|
2216
|
-
* @param key - Uniform input bytes.
|
|
2217
|
-
* @param fieldOrder - Size of subgroup.
|
|
2218
|
-
* @param isLE - interpret hash bytes as LE num
|
|
2219
|
-
* @returns valid private scalar
|
|
2220
|
-
* @throws If the hash length or field order is invalid for scalar reduction. {@link Error}
|
|
2221
|
-
* @example
|
|
2222
|
-
* Map hash output into a private scalar range.
|
|
2223
|
-
*
|
|
2224
|
-
* ```ts
|
|
2225
|
-
* mapHashToField(new Uint8Array(48).fill(1), 255n);
|
|
2226
|
-
* ```
|
|
2227
|
-
*/
|
|
2228
|
-
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
2229
|
-
abytes(key);
|
|
2230
|
-
const len = key.length;
|
|
2231
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
2232
|
-
const minLen = Math.max(getMinHashLength(fieldOrder), 16);
|
|
2233
|
-
if (len < minLen || len > 1024) throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
2234
|
-
const reduced = mod(isLE ? bytesToNumberLE(key) : bytesToNumberBE(key), fieldOrder - _1n$1) + _1n$1;
|
|
2235
|
-
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
2236
|
-
}
|
|
2237
|
-
//#endregion
|
|
2238
|
-
//#region ../../../../../.pnpm-store/v11/links/@noble/curves/2.4.0/766129a921e8dfd2ef1de557b88b41133e3334d958e354fac1f5d17f98661627/node_modules/@noble/curves/abstract/curve.js
|
|
2239
|
-
/**
|
|
2240
|
-
* Methods for elliptic curve multiplication by scalars.
|
|
2241
|
-
* Contains wNAF-based ScalarMultiplier, pippenger.
|
|
2242
|
-
* @module
|
|
2243
|
-
*/
|
|
2244
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2245
|
-
var _0n = /* @__PURE__ */ BigInt(0);
|
|
2246
|
-
var _1n = /* @__PURE__ */ BigInt(1);
|
|
2247
|
-
var _4n = /* @__PURE__ */ BigInt(4);
|
|
2248
|
-
var BLIND_BYTES = 16;
|
|
2249
|
-
var BLIND_BITS = 128;
|
|
2250
|
-
var FW_WINDOW = 5;
|
|
2251
|
-
var TABLE_BYTES_MAX = /* @__PURE__ */ (() => 2 ** 31)();
|
|
2252
|
-
/**
|
|
2253
|
-
* Validates the static surface of a point constructor.
|
|
2254
|
-
* This is only a cheap sanity check for the constructor hooks and fields consumed by generic
|
|
2255
|
-
* factories; it does not certify `BASE`/`ZERO` semantics or prove the curve implementation itself.
|
|
2256
|
-
* @param Point - Runtime point constructor.
|
|
2257
|
-
* @throws On missing constructor hooks or malformed field metadata. {@link TypeError}
|
|
2258
|
-
* @example
|
|
2259
|
-
* Check that one point constructor exposes the static hooks generic helpers need.
|
|
2260
|
-
*
|
|
2261
|
-
* ```ts
|
|
2262
|
-
* import { ed25519 } from '@noble/curves/ed25519.js';
|
|
2263
|
-
* import { validatePointCons } from '@noble/curves/abstract/curve.js';
|
|
2264
|
-
* validatePointCons(ed25519.Point);
|
|
2265
|
-
* ```
|
|
2266
|
-
*/
|
|
2267
|
-
function validatePointCons(Point) {
|
|
2268
|
-
const pc = Point;
|
|
2269
|
-
if (typeof pc !== "function") throw new TypeError("\"Point\" expected constructor, got type=" + typeof Point);
|
|
2270
|
-
afunction(pc.fromAffine, "Point.fromAffine");
|
|
2271
|
-
afunction(pc.fromBytes, "Point.fromBytes");
|
|
2272
|
-
afunction(pc.fromHex, "Point.fromHex");
|
|
2273
|
-
aobject(pc.BASE, "Point.BASE");
|
|
2274
|
-
aobject(pc.ZERO, "Point.ZERO");
|
|
2275
|
-
validateField(pc.Fp);
|
|
2276
|
-
validateField(pc.Fn);
|
|
2277
|
-
}
|
|
2278
|
-
/**
|
|
2279
|
-
* Takes a bunch of Projective Points but executes only one
|
|
2280
|
-
* inversion on all of them. Inversion is very slow operation,
|
|
2281
|
-
* so this improves performance massively.
|
|
2282
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
2283
|
-
* Input points are left unchanged; the normalized points are returned as fresh instances.
|
|
2284
|
-
* @param c - Point constructor.
|
|
2285
|
-
* @param points - Projective points.
|
|
2286
|
-
* @returns Fresh projective points reconstructed from normalized affine coordinates.
|
|
2287
|
-
* @example
|
|
2288
|
-
* Batch-normalize projective points with a single shared inversion.
|
|
2289
|
-
*
|
|
2290
|
-
* ```ts
|
|
2291
|
-
* import { normalizeZ } from '@noble/curves/abstract/curve.js';
|
|
2292
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
2293
|
-
* const points = normalizeZ(p256.Point, [p256.Point.BASE, p256.Point.BASE.double()]);
|
|
2294
|
-
* ```
|
|
2295
|
-
*/
|
|
2296
|
-
function normalizeZ(c, points) {
|
|
2297
|
-
validatePointCons(c);
|
|
2298
|
-
validateMSMPoints(points, c);
|
|
2299
|
-
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
2300
|
-
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
2301
|
-
}
|
|
2302
|
-
function validateW(W, bits, min = 1) {
|
|
2303
|
-
if (!Number.isSafeInteger(W) || W < min || W > bits) throw new Error("invalid window size, expected [" + min + ".." + bits + "], got W=" + W);
|
|
2304
|
-
}
|
|
2305
|
-
function validateTableBytes(numPoints, fpBytes) {
|
|
2306
|
-
const bytes = numPoints * (4 * fpBytes + 128);
|
|
2307
|
-
if (bytes > TABLE_BYTES_MAX) throw new Error("invalid window size: table would need ~" + Math.ceil(bytes / 2 ** 20) + " MiB, max " + TABLE_BYTES_MAX / 2 ** 20 + " MiB");
|
|
2308
|
-
}
|
|
2309
|
-
/**
|
|
2310
|
-
* Probes an RNG once, at construction time: returns `undefined` when it is unavailable —
|
|
2311
|
-
* throws or returns malformed bytes — so callers can downgrade to their unblinded /
|
|
2312
|
-
* deterministic constant-time fallback. Blinding is defense-in-depth (DPA/template
|
|
2313
|
-
* hardening), not a correctness or key-secrecy requirement, so availability-based
|
|
2314
|
-
* downgrade is acceptable.
|
|
2315
|
-
*
|
|
2316
|
-
* The downgrade decision is deliberately static. After a successful probe the RNG becomes
|
|
2317
|
-
* part of the trusted contract: later misbehavior must fail closed in per-call validation
|
|
2318
|
-
* (throw), never downgrade — a dynamic fallback would let a tampered RNG silently strip
|
|
2319
|
-
* blinding on demand. A probe can only ever classify broken environments, not adversarial
|
|
2320
|
-
* RNGs: a stateful RNG can always behave while probed and misbehave later.
|
|
2321
|
-
* @param randomBytes - RNG to probe, or `undefined` when the environment provides none.
|
|
2322
|
-
* @param length - Byte length requested from the probe call.
|
|
2323
|
-
* @returns The RNG when the probe produced `length` valid bytes; `undefined` otherwise.
|
|
2324
|
-
* @example
|
|
2325
|
-
* Probe an RNG once before enabling scalar blinding.
|
|
2326
|
-
*
|
|
2327
|
-
* ```ts
|
|
2328
|
-
* import { probeRandomBytes } from '@noble/curves/abstract/curve.js';
|
|
2329
|
-
* import { randomBytes } from '@noble/hashes/utils.js';
|
|
2330
|
-
* const rng = probeRandomBytes(randomBytes, 16);
|
|
2331
|
-
* ```
|
|
2332
|
-
*/
|
|
2333
|
-
function probeRandomBytes(randomBytes, length) {
|
|
2334
|
-
if (randomBytes === void 0) return void 0;
|
|
2335
|
-
afunction(randomBytes, "randomBytes");
|
|
2336
|
-
try {
|
|
2337
|
-
const probe = randomBytes(length);
|
|
2338
|
-
if (!isBytes(probe) || probe.length !== length) return void 0;
|
|
2339
|
-
} catch {
|
|
2340
|
-
return;
|
|
2341
|
-
}
|
|
2342
|
-
return randomBytes;
|
|
2343
|
-
}
|
|
2344
|
-
function validateMSMPoints(points, c) {
|
|
2345
|
-
aarray(points, "points");
|
|
2346
|
-
points.forEach((p, i) => {
|
|
2347
|
-
if (!(p instanceof c)) throw new Error("invalid point at index " + i);
|
|
2348
|
-
});
|
|
2349
|
-
}
|
|
2350
|
-
function validateMSMScalars(scalars, field, maxScalar) {
|
|
2351
|
-
if (!Array.isArray(scalars)) throw new Error("array of scalars expected");
|
|
2352
|
-
scalars.forEach((s, i) => {
|
|
2353
|
-
if (!(maxScalar === void 0 ? field.isValid(s) : isPosBig(s) && s < maxScalar)) throw new Error("invalid scalar at index " + i);
|
|
2354
|
-
});
|
|
2355
|
-
}
|
|
2356
|
-
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
2357
|
-
function getWindowSize(P) {
|
|
2358
|
-
return pointWindowSizes.get(P) || 1;
|
|
2359
|
-
}
|
|
2360
|
-
/** Table of odd multiples [1P, 3P, ..., (2⋅size−1)P]; width-W wNAF uses size = 2^(W−2). */
|
|
2361
|
-
function oddMultiples(p, size) {
|
|
2362
|
-
const dbl = p.double();
|
|
2363
|
-
const t = [p];
|
|
2364
|
-
for (let j = 1; j < size; j++) t.push(t[j - 1].add(dbl));
|
|
2365
|
-
return t;
|
|
2366
|
-
}
|
|
2367
|
-
/**
|
|
2368
|
-
* Width-W wNAF signed-digit recoding (W >= 2), LSB-first: digits are 0 or odd with
|
|
2369
|
-
* |digit| < 2^(W−1); nonzero density ~1/(W+1) (a nonzero digit is followed by W−1 zeros).
|
|
2370
|
-
*/
|
|
2371
|
-
function wnafDigits(n, W) {
|
|
2372
|
-
const size = 2 ** W;
|
|
2373
|
-
const half = size / 2;
|
|
2374
|
-
const mask = BigInt(size - 1);
|
|
2375
|
-
const d = [];
|
|
2376
|
-
while (n > _0n) {
|
|
2377
|
-
let w = 0;
|
|
2378
|
-
if (n & _1n) {
|
|
2379
|
-
w = Number(n & mask);
|
|
2380
|
-
if (w >= half) w -= size;
|
|
2381
|
-
n -= BigInt(w);
|
|
2382
|
-
}
|
|
2383
|
-
d.push(w);
|
|
2384
|
-
n >>= _1n;
|
|
2385
|
-
}
|
|
2386
|
-
return d;
|
|
2387
|
-
}
|
|
2388
|
-
/**
|
|
2389
|
-
* Fixed-position signed-window recoding for precomputed wNAF: `n = Σ digits[w]⋅2^(w⋅W)` with
|
|
2390
|
-
* digits in `[−2^(W−1)+1, 2^(W−1)]`. Digit count is fixed by `windows` (callers reserve one
|
|
2391
|
-
* extra window for the final carry), so recoding length does not depend on the scalar.
|
|
2392
|
-
*/
|
|
2393
|
-
function signedWindowDigits(n, W, windows) {
|
|
2394
|
-
const size = 2 ** W;
|
|
2395
|
-
const half = size / 2;
|
|
2396
|
-
const mask = BigInt(size - 1);
|
|
2397
|
-
const shiftBy = BigInt(W);
|
|
2398
|
-
const d = [];
|
|
2399
|
-
for (let w = 0; w < windows; w++) {
|
|
2400
|
-
let v = Number(n & mask);
|
|
2401
|
-
n >>= shiftBy;
|
|
2402
|
-
if (v > half) {
|
|
2403
|
-
v -= size;
|
|
2404
|
-
n += _1n;
|
|
2405
|
-
}
|
|
2406
|
-
d.push(v);
|
|
2407
|
-
}
|
|
2408
|
-
if (n !== _0n) throw new Error("invalid wnaf");
|
|
2409
|
-
return d;
|
|
2410
|
-
}
|
|
2411
|
-
/**
|
|
2412
|
-
* Shared vartime walk over per-scalar wNAF digit streams: one doubling of a single shared
|
|
2413
|
-
* accumulator per bit position of the longest recoding, one signed table addition per
|
|
2414
|
-
* nonzero digit. `tables[i]` must hold the odd multiples of the i-th point.
|
|
2415
|
-
*/
|
|
2416
|
-
function wnafWalk(zero, tables, digits) {
|
|
2417
|
-
let max = 0;
|
|
2418
|
-
for (const d of digits) max = Math.max(max, d.length);
|
|
2419
|
-
let acc = zero;
|
|
2420
|
-
for (let bit = max - 1; bit >= 0; bit--) {
|
|
2421
|
-
if (bit !== max - 1) acc = acc.double();
|
|
2422
|
-
for (let i = 0; i < digits.length; i++) {
|
|
2423
|
-
const w = digits[i][bit];
|
|
2424
|
-
if (w) {
|
|
2425
|
-
const item = tables[i][Math.abs(w) - 1 >> 1];
|
|
2426
|
-
acc = acc.add(w < 0 ? item.negate() : item);
|
|
2427
|
-
}
|
|
2428
|
-
}
|
|
2429
|
-
}
|
|
2430
|
-
return acc;
|
|
2431
|
-
}
|
|
2432
|
-
/**
|
|
2433
|
-
* Elliptic curve multiplication of Point by scalar.
|
|
2434
|
-
* Routes between cached-table, fixed-window, and one-shot wNAF paths; entry points validate
|
|
2435
|
-
* their own scalars (`mulCT`/`mulCTBlinded`: `1 <= s < Fn.ORDER`; `mulUnsafe`: up to the
|
|
2436
|
-
* `Fn.ORDER^4` DoS cap via {@link mulAddUnsafe}).
|
|
2437
|
-
* Table generation is expensive and happens on first call of `multiply()`
|
|
2438
|
-
* (or eagerly via `precompute(W, false)`). By default, `BASE` point is precomputed.
|
|
2439
|
-
*
|
|
2440
|
-
* Cached algorithm is signed fixed-window wNAF:
|
|
2441
|
-
* - table stores, for every window w, the multiples `[1..2^(W−1)]⋅2^(w⋅W)⋅P` — all doublings
|
|
2442
|
-
* are baked in, so a multiplication is exactly one table addition per window
|
|
2443
|
-
* - window count is fixed (`ceil(bits/W) + 1`), so the point-operation count is scalar-independent
|
|
2444
|
-
* (basis of the constant-time path)
|
|
2445
|
-
* - for a 256-bit curve and W=6: 44⋅32 = 1408 table points, 44 additions per multiply
|
|
2446
|
-
* - secret scalars are additionally blinded (see {@link ScalarMultiplier.mulCTBlinded}), which
|
|
2447
|
-
* widens tables by 128 bits
|
|
2448
|
-
* @param Point - Point constructor.
|
|
2449
|
-
* @param randomBytes - RNG used for scalar blinding; required by the blinded secret path.
|
|
2450
|
-
* @example
|
|
2451
|
-
* Elliptic curve multiplication of Point by scalar.
|
|
2452
|
-
*
|
|
2453
|
-
* ```ts
|
|
2454
|
-
* import { ScalarMultiplier } from '@noble/curves/abstract/curve.js';
|
|
2455
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
2456
|
-
* const mul = new ScalarMultiplier(p256.Point);
|
|
2457
|
-
* ```
|
|
2458
|
-
*/
|
|
2459
|
-
var ScalarMultiplier = class {
|
|
2460
|
-
Point;
|
|
2461
|
-
BASE;
|
|
2462
|
-
ZERO;
|
|
2463
|
-
randomBytes;
|
|
2464
|
-
wnafPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
2465
|
-
baseCanBeBlinded;
|
|
2466
|
-
bits;
|
|
2467
|
-
constructor(Point, randomBytes) {
|
|
2468
|
-
validatePointCons(Point);
|
|
2469
|
-
this.randomBytes = probeRandomBytes(randomBytes, BLIND_BYTES);
|
|
2470
|
-
this.Point = Point;
|
|
2471
|
-
this.BASE = Point.BASE;
|
|
2472
|
-
this.ZERO = Point.ZERO;
|
|
2473
|
-
this.bits = Point.Fn.BITS;
|
|
2474
|
-
}
|
|
2475
|
-
/**
|
|
2476
|
-
* Creates a signed fixed-window wNAF precomputation table: for every window w, the
|
|
2477
|
-
* multiples `[1..2^(W−1)]⋅2^(w⋅W)⋅P`, flattened. All doublings are baked into the table,
|
|
2478
|
-
* so cached multiplication is additions-only. `windows = ceil(bits/W) + 1`: the extra
|
|
2479
|
-
* window absorbs the final carry of signed-digit recoding.
|
|
2480
|
-
* For a 256-bit curve and W=6, the table is 44⋅32 = 1408 points.
|
|
2481
|
-
* @param point - Point instance
|
|
2482
|
-
* @param W - window size
|
|
2483
|
-
* @param bits - scalar bitlength the table must cover
|
|
2484
|
-
*/
|
|
2485
|
-
buildWnafTable(point, W, bits) {
|
|
2486
|
-
const windows = Math.ceil(bits / W) + 1;
|
|
2487
|
-
const half = 2 ** (W - 1);
|
|
2488
|
-
const comp = [];
|
|
2489
|
-
let base = point;
|
|
2490
|
-
for (let w = 0; w < windows; w++) {
|
|
2491
|
-
let acc = base;
|
|
2492
|
-
for (let i = 0; i < half; i++) {
|
|
2493
|
-
comp.push(acc);
|
|
2494
|
-
acc = acc.add(base);
|
|
2495
|
-
}
|
|
2496
|
-
base = comp[comp.length - 1].double();
|
|
2497
|
-
}
|
|
2498
|
-
return {
|
|
2499
|
-
W,
|
|
2500
|
-
bits,
|
|
2501
|
-
windows,
|
|
2502
|
-
comp
|
|
2503
|
-
};
|
|
2504
|
-
}
|
|
2505
|
-
/**
|
|
2506
|
-
* Implements ec multiplication using precomputed signed fixed-window wNAF tables.
|
|
2507
|
-
* Constant-time: fixed window count with one table addition per window — zero digits feed
|
|
2508
|
-
* the fake accumulator — and no doublings; the lookup scans the whole window slice.
|
|
2509
|
-
* Scalar bounds are validated by the public entry points ({@link ScalarMultiplier.mulCT},
|
|
2510
|
-
* {@link ScalarMultiplier.mulCTBlinded}, {@link ScalarMultiplier.mulUnsafe});
|
|
2511
|
-
* signedWindowDigits throws if `n` exceeds the table.
|
|
2512
|
-
* @returns real and fake (for const-time) points
|
|
2513
|
-
*/
|
|
2514
|
-
wnafCachedCT(precomputes, n) {
|
|
2515
|
-
const { W, windows, comp } = precomputes;
|
|
2516
|
-
const half = 2 ** (W - 1);
|
|
2517
|
-
const digits = signedWindowDigits(n, W, windows);
|
|
2518
|
-
let p = this.ZERO;
|
|
2519
|
-
let f = this.BASE;
|
|
2520
|
-
for (let w = 0; w < windows; w++) {
|
|
2521
|
-
const digit = digits[w];
|
|
2522
|
-
const start = w * half;
|
|
2523
|
-
const idx = Math.abs(digit) - 1;
|
|
2524
|
-
let sel = comp[start];
|
|
2525
|
-
for (let i = 1; i < half; i++) sel = i === idx ? comp[start + i] : sel;
|
|
2526
|
-
const neg = sel.negate();
|
|
2527
|
-
if (digit === 0) f = f.add(comp[start]);
|
|
2528
|
-
else p = p.add(digit < 0 ? neg : sel);
|
|
2529
|
-
}
|
|
2530
|
-
return {
|
|
2531
|
-
p,
|
|
2532
|
-
f
|
|
2533
|
-
};
|
|
2534
|
-
}
|
|
2535
|
-
getWnafPrecomputes(W, point, bits, transform) {
|
|
2536
|
-
let entries = this.wnafPrecomputes.get(point);
|
|
2537
|
-
let comp = entries?.find((entry) => entry.W === W && entry.bits === bits);
|
|
2538
|
-
if (!comp) {
|
|
2539
|
-
comp = this.buildWnafTable(point, W, bits);
|
|
2540
|
-
if (typeof transform === "function") comp = {
|
|
2541
|
-
...comp,
|
|
2542
|
-
comp: transform(comp.comp)
|
|
2543
|
-
};
|
|
2544
|
-
if (!entries) {
|
|
2545
|
-
entries = [];
|
|
2546
|
-
this.wnafPrecomputes.set(point, entries);
|
|
2547
|
-
}
|
|
2548
|
-
entries.push(comp);
|
|
2549
|
-
}
|
|
2550
|
-
return comp;
|
|
2551
|
-
}
|
|
2552
|
-
assertPoint(point) {
|
|
2553
|
-
if (!(point instanceof this.Point)) throw new TypeError("\"point\" expected Point instance, got type=" + typeof point);
|
|
2554
|
-
}
|
|
2555
|
-
validateMulInput(point, scalar) {
|
|
2556
|
-
this.assertPoint(point);
|
|
2557
|
-
if (!inRange(scalar, _1n, this.Point.Fn.ORDER)) throw new Error("invalid scalar");
|
|
2558
|
-
}
|
|
2559
|
-
runCT(point, n, bits, transform) {
|
|
2560
|
-
const W = getWindowSize(point);
|
|
2561
|
-
if (W === 1) return this.fixedWindowCT(point, n, bits);
|
|
2562
|
-
return this.wnafCachedCT(this.getWnafPrecomputes(W, point, bits, transform), n);
|
|
2563
|
-
}
|
|
2564
|
-
mulCT(point, scalar, transform) {
|
|
2565
|
-
this.validateMulInput(point, scalar);
|
|
2566
|
-
return this.runCT(point, scalar, this.bits, transform);
|
|
2567
|
-
}
|
|
2568
|
-
mulCTBlinded(point, scalar, transform) {
|
|
2569
|
-
this.validateMulInput(point, scalar);
|
|
2570
|
-
if (this.randomBytes === void 0) throw new Error("randomBytes is required for scalar blinding");
|
|
2571
|
-
const bits = this.Point.Fn.BITS + BLIND_BITS;
|
|
2572
|
-
const blind = this.randomBytes(BLIND_BYTES);
|
|
2573
|
-
if (!isBytes(blind) || blind.length !== BLIND_BYTES) throw new Error("randomBytes returned invalid byte array");
|
|
2574
|
-
blind[0] = blind[0] & 63 | 128;
|
|
2575
|
-
const n = scalar + bytesToNumberBE(blind) * this.Point.Fn.ORDER;
|
|
2576
|
-
return this.runCT(point, n, bits, transform);
|
|
2577
|
-
}
|
|
2578
|
-
/**
|
|
2579
|
-
* Constant-time multiplication `n*point` for an un-precomputed point, via a small fixed window.
|
|
2580
|
-
* A cached wNAF table only pays off when reused; a flat 2^FW_WINDOW table (`size-1` adds) is
|
|
2581
|
-
* far cheaper to build for a single use. The point-operation sequence is independent of `n`:
|
|
2582
|
-
* build the table, then per window exactly FW_WINDOW doublings, a data-oblivious scan over
|
|
2583
|
-
* every table entry, and one addition (adds the identity when the window digit is 0 — never
|
|
2584
|
-
* skipped).
|
|
2585
|
-
*
|
|
2586
|
-
* `n` must be `< 2^bits`. Assumes complete addition (adding the identity costs the same as any
|
|
2587
|
-
* add), which holds for the Weierstrass/Edwards point types used here. The table is left in
|
|
2588
|
-
* projective form (no normalizeZ): normalizing this small a table costs more than the
|
|
2589
|
-
* mixed-add savings it would buy for a single multiply.
|
|
2590
|
-
* @returns real point `p`; `f` duplicates it only to match {@link wnafCachedCT}'s return shape
|
|
2591
|
-
* (this path needs no fake accumulator — its op-count is already scalar-independent).
|
|
2592
|
-
*/
|
|
2593
|
-
fixedWindowCT(point, n, bits) {
|
|
2594
|
-
const W = FW_WINDOW;
|
|
2595
|
-
const size = 32;
|
|
2596
|
-
const mask = bitMask(W);
|
|
2597
|
-
const table = new Array(size);
|
|
2598
|
-
table[0] = this.ZERO;
|
|
2599
|
-
for (let i = 1; i < size; i++) table[i] = table[i - 1].add(point);
|
|
2600
|
-
const windows = Math.ceil(bits / W);
|
|
2601
|
-
let acc = this.ZERO;
|
|
2602
|
-
for (let window = windows - 1; window >= 0; window--) {
|
|
2603
|
-
if (window !== windows - 1) for (let d = 0; d < W; d++) acc = acc.double();
|
|
2604
|
-
const digit = Number(n >> BigInt(window * W) & mask);
|
|
2605
|
-
let sel = table[0];
|
|
2606
|
-
for (let i = 1; i < size; i++) sel = i === digit ? table[i] : sel;
|
|
2607
|
-
acc = acc.add(sel);
|
|
2608
|
-
}
|
|
2609
|
-
return {
|
|
2610
|
-
p: acc,
|
|
2611
|
-
f: acc
|
|
2612
|
-
};
|
|
2613
|
-
}
|
|
2614
|
-
shouldBlind(point, cofactor) {
|
|
2615
|
-
if (this.randomBytes === void 0) return false;
|
|
2616
|
-
if (cofactor === _1n) return true;
|
|
2617
|
-
if (point !== this.BASE) return false;
|
|
2618
|
-
if (this.baseCanBeBlinded === void 0) this.baseCanBeBlinded = this.mulUnsafe(this.BASE, this.Point.Fn.ORDER).is0();
|
|
2619
|
-
return this.baseCanBeBlinded;
|
|
2620
|
-
}
|
|
2621
|
-
mulSecret(point, scalar, cofactor, transform) {
|
|
2622
|
-
return this.shouldBlind(point, cofactor) ? this.mulCTBlinded(point, scalar, transform) : this.mulCT(point, scalar, transform);
|
|
2623
|
-
}
|
|
2624
|
-
mulUnsafe(point, scalar, transform) {
|
|
2625
|
-
this.assertPoint(point);
|
|
2626
|
-
if (!isPosBig(scalar)) throw new Error("invalid scalar");
|
|
2627
|
-
const W = getWindowSize(point);
|
|
2628
|
-
if (W === 1 || scalar >= this.Point.Fn.ORDER) return mulAddUnsafe(this.Point, [point], [scalar], true);
|
|
2629
|
-
const precomputes = this.getWnafPrecomputes(W, point, this.bits, transform);
|
|
2630
|
-
return this.wnafCachedCT(precomputes, scalar).p;
|
|
2631
|
-
}
|
|
2632
|
-
setWindowSize(point, W) {
|
|
2633
|
-
this.assertPoint(point);
|
|
2634
|
-
validateW(W, this.bits);
|
|
2635
|
-
validateTableBytes((Math.ceil((this.bits + BLIND_BITS) / W) + 1) * 2 ** (W - 1), this.Point.Fp.BYTES);
|
|
2636
|
-
pointWindowSizes.set(point, W);
|
|
2637
|
-
this.wnafPrecomputes.delete(point);
|
|
2638
|
-
}
|
|
2639
|
-
hasWindowSize(point) {
|
|
2640
|
-
return getWindowSize(point) !== 1;
|
|
2641
|
-
}
|
|
2642
|
-
};
|
|
2643
|
-
/**
|
|
2644
|
-
* Combined multi-scalar multiplication `Σ scalars[i]⋅points[i]` via interleaved width-4 wNAF
|
|
2645
|
-
* (Strauss–Shamir). Every input gets its own table of odd multiples `[1P, 3P, 5P, 7P]` and
|
|
2646
|
-
* signed-digit recoding, but all walks share one doubling chain, so total cost is
|
|
2647
|
-
* `~bits` doublings + `L⋅bits/5` additions instead of `L⋅bits` doublings for separate
|
|
2648
|
-
* multiplications. Intended for the 2-4 point shapes of signature verification
|
|
2649
|
-
* (`R = u1⋅G + u2⋅P`); use {@link pippenger} for larger batches.
|
|
2650
|
-
*
|
|
2651
|
-
* Not constant-time: only for public inputs. Scalars must satisfy `0 <= s < Fn.ORDER`;
|
|
2652
|
-
* fold negative signs into the points before calling.
|
|
2653
|
-
* @param c - Point constructor.
|
|
2654
|
-
* @param points - Array of curve points.
|
|
2655
|
-
* @param scalars - Array of non-negative scalars, same length as points.
|
|
2656
|
-
* @param allowOversized - Replace the `s < Fn.ORDER` scalar check with a `Fn.ORDER^4` DoS cap.
|
|
2657
|
-
* Off by default. For scalars that must NOT be reduced mod ORDER: torsion checks
|
|
2658
|
-
* (`Fn.ORDER⋅P ≟ O`) and cofactor-clearing multiples. Walk length grows with `bitLen(s)`.
|
|
2659
|
-
* @returns Combined multiplication result; identity for empty input.
|
|
2660
|
-
* @throws If the point set or scalar set is invalid. {@link Error}
|
|
2661
|
-
* @example
|
|
2662
|
-
* Combined multi-scalar multiplication via Strauss–Shamir.
|
|
2663
|
-
*
|
|
2664
|
-
* ```ts
|
|
2665
|
-
* import { mulAddUnsafe } from '@noble/curves/abstract/curve.js';
|
|
2666
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
2667
|
-
* const G = p256.Point.BASE;
|
|
2668
|
-
* const R = mulAddUnsafe(p256.Point, [G, G.double()], [2n, 3n]); // 2⋅G + 3⋅(2⋅G)
|
|
2669
|
-
* ```
|
|
2670
|
-
*/
|
|
2671
|
-
function mulAddUnsafe(c, points, scalars, allowOversized = false) {
|
|
2672
|
-
validatePointCons(c);
|
|
2673
|
-
validateMSMPoints(points, c);
|
|
2674
|
-
abool(allowOversized, "allowOversized");
|
|
2675
|
-
validateMSMScalars(scalars, c.Fn, allowOversized ? c.Fn.ORDER ** _4n : void 0);
|
|
2676
|
-
if (points.length !== scalars.length) throw new Error("arrays of points and scalars must have equal length");
|
|
2677
|
-
const tables = points.map((p) => oddMultiples(p, 4));
|
|
2678
|
-
const digits = scalars.map((n) => wnafDigits(n, 4));
|
|
2679
|
-
return wnafWalk(c.ZERO, tables, digits);
|
|
2680
|
-
}
|
|
2681
|
-
function createField(order, field, isLE) {
|
|
2682
|
-
if (field) {
|
|
2683
|
-
if (field.ORDER !== order) throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
2684
|
-
validateField(field);
|
|
2685
|
-
return field;
|
|
2686
|
-
} else return Field(order, { isLE });
|
|
2687
|
-
}
|
|
2688
|
-
/**
|
|
2689
|
-
* Validates basic CURVE shape and field membership, then creates fields.
|
|
2690
|
-
* This does not prove that the generator is on-curve, that subgroup/order data are consistent, or
|
|
2691
|
-
* that the curve equation itself is otherwise sane.
|
|
2692
|
-
* @param type - Curve family.
|
|
2693
|
-
* @param CURVE - Curve parameters.
|
|
2694
|
-
* @param curveOpts - Optional field overrides. See {@link FpFn}:
|
|
2695
|
-
* - `Fp` (optional): Optional base-field override.
|
|
2696
|
-
* - `Fn` (optional): Optional scalar-field override.
|
|
2697
|
-
* @param FpFnLE - Whether field encoding is little-endian.
|
|
2698
|
-
* @returns Frozen curve parameters and fields.
|
|
2699
|
-
* @throws If the curve parameters or field overrides are invalid. {@link Error}
|
|
2700
|
-
* @example
|
|
2701
|
-
* Build curve fields from raw constants before constructing a curve instance.
|
|
2702
|
-
*
|
|
2703
|
-
* ```ts
|
|
2704
|
-
* const curve = createCurveFields('weierstrass', {
|
|
2705
|
-
* p: 17n,
|
|
2706
|
-
* n: 19n,
|
|
2707
|
-
* h: 1n,
|
|
2708
|
-
* a: 2n,
|
|
2709
|
-
* b: 2n,
|
|
2710
|
-
* Gx: 5n,
|
|
2711
|
-
* Gy: 1n,
|
|
2712
|
-
* });
|
|
2713
|
-
* ```
|
|
2714
|
-
*/
|
|
2715
|
-
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
2716
|
-
if (type !== "weierstrass" && type !== "edwards") throw new Error("expected curve type \"weierstrass\" or \"edwards\"");
|
|
2717
|
-
if (FpFnLE === void 0) FpFnLE = type === "edwards";
|
|
2718
|
-
if (!CURVE || typeof CURVE !== "object") throw new Error(`expected valid ${type} CURVE object`);
|
|
2719
|
-
validateObject(curveOpts);
|
|
2720
|
-
for (const p of [
|
|
2721
|
-
"p",
|
|
2722
|
-
"n",
|
|
2723
|
-
"h"
|
|
2724
|
-
]) {
|
|
2725
|
-
const val = CURVE[p];
|
|
2726
|
-
if (!(isPosBig(val) && val !== _0n)) throw new Error(`CURVE.${p} must be positive bigint`);
|
|
2727
|
-
}
|
|
2728
|
-
const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
2729
|
-
const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
2730
|
-
const params = [
|
|
2731
|
-
"Gx",
|
|
2732
|
-
"Gy",
|
|
2733
|
-
"a",
|
|
2734
|
-
type === "weierstrass" ? "b" : "d"
|
|
2735
|
-
];
|
|
2736
|
-
for (const p of params) if (!Fp.isValid(CURVE[p])) throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
2737
|
-
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
2738
|
-
return {
|
|
2739
|
-
CURVE,
|
|
2740
|
-
Fp,
|
|
2741
|
-
Fn
|
|
2742
|
-
};
|
|
2743
|
-
}
|
|
2744
|
-
/**
|
|
2745
|
-
* @param randomSecretKey - Secret-key generator.
|
|
2746
|
-
* @param getPublicKey - Public-key derivation helper.
|
|
2747
|
-
* @returns Keypair generator.
|
|
2748
|
-
* @example
|
|
2749
|
-
* Build a `keygen()` helper from existing secret-key and public-key primitives.
|
|
2750
|
-
*
|
|
2751
|
-
* ```ts
|
|
2752
|
-
* import { createKeygen } from '@noble/curves/abstract/curve.js';
|
|
2753
|
-
* import { p256 } from '@noble/curves/nist.js';
|
|
2754
|
-
* const keygen = createKeygen(p256.utils.randomSecretKey, p256.getPublicKey);
|
|
2755
|
-
* const pair = keygen();
|
|
2756
|
-
* ```
|
|
2757
|
-
*/
|
|
2758
|
-
function createKeygen(randomSecretKey, getPublicKey) {
|
|
2759
|
-
return function keygen(seed) {
|
|
2760
|
-
const secretKey = randomSecretKey(seed);
|
|
2761
|
-
return {
|
|
2762
|
-
secretKey,
|
|
2763
|
-
publicKey: getPublicKey(secretKey)
|
|
2764
|
-
};
|
|
2765
|
-
};
|
|
2766
|
-
}
|
|
2767
|
-
//#endregion
|
|
2768
|
-
export { isBytes as A, clean as B, bytesToHex as C, copyBytes as D, concatBytes as E, sha512 as F, randomBytes$1 as H, abytes$1 as I, aexists as L, randomBytes as M, validateObject as N, createHmacDrbg as O, sha256 as P, ahash as R, bitMask as S, bytesToNumberLE as T, concatBytes$1 as V, abool as _, normalizeZ as a, astring as b, FpLegendre as c, isNegativeLE as d, mapHashToField as f, abignumber as g, aInRange as h, mulAddUnsafe as i, numberToHexUnpadded as j, hexToBytes as k, getMinHashLength as l, pow2 as m, createCurveFields as n, probeRandomBytes as o, mod as p, createKeygen as r, validatePointCons as s, ScalarMultiplier as t, invertCt as u, abytes as v, bytesToNumberBE as w, bitLen as x, asafenumber as y, aoutput as z };
|