@noble/curves 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +214 -122
  2. package/abstract/bls.d.ts +299 -16
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +82 -22
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +274 -27
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +177 -23
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +166 -30
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +221 -86
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.d.ts +322 -10
  15. package/abstract/fft.d.ts.map +1 -1
  16. package/abstract/fft.js +154 -12
  17. package/abstract/fft.js.map +1 -1
  18. package/abstract/frost.d.ts +293 -0
  19. package/abstract/frost.d.ts.map +1 -0
  20. package/abstract/frost.js +704 -0
  21. package/abstract/frost.js.map +1 -0
  22. package/abstract/hash-to-curve.d.ts +173 -24
  23. package/abstract/hash-to-curve.d.ts.map +1 -1
  24. package/abstract/hash-to-curve.js +170 -31
  25. package/abstract/hash-to-curve.js.map +1 -1
  26. package/abstract/modular.d.ts +429 -37
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +414 -119
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +83 -12
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +32 -7
  33. package/abstract/montgomery.js.map +1 -1
  34. package/abstract/oprf.d.ts +164 -91
  35. package/abstract/oprf.d.ts.map +1 -1
  36. package/abstract/oprf.js +88 -29
  37. package/abstract/oprf.js.map +1 -1
  38. package/abstract/poseidon.d.ts +138 -7
  39. package/abstract/poseidon.d.ts.map +1 -1
  40. package/abstract/poseidon.js +178 -15
  41. package/abstract/poseidon.js.map +1 -1
  42. package/abstract/tower.d.ts +122 -3
  43. package/abstract/tower.d.ts.map +1 -1
  44. package/abstract/tower.js +323 -139
  45. package/abstract/tower.js.map +1 -1
  46. package/abstract/weierstrass.d.ts +339 -76
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +395 -205
  49. package/abstract/weierstrass.js.map +1 -1
  50. package/bls12-381.d.ts +16 -2
  51. package/bls12-381.d.ts.map +1 -1
  52. package/bls12-381.js +199 -209
  53. package/bls12-381.js.map +1 -1
  54. package/bn254.d.ts +11 -2
  55. package/bn254.d.ts.map +1 -1
  56. package/bn254.js +93 -38
  57. package/bn254.js.map +1 -1
  58. package/ed25519.d.ts +125 -14
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +202 -40
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +108 -14
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +194 -42
  65. package/ed448.js.map +1 -1
  66. package/index.js +7 -1
  67. package/index.js.map +1 -1
  68. package/misc.d.ts +106 -7
  69. package/misc.d.ts.map +1 -1
  70. package/misc.js +141 -32
  71. package/misc.js.map +1 -1
  72. package/nist.d.ts +112 -11
  73. package/nist.d.ts.map +1 -1
  74. package/nist.js +139 -17
  75. package/nist.js.map +1 -1
  76. package/package.json +11 -6
  77. package/secp256k1.d.ts +92 -15
  78. package/secp256k1.d.ts.map +1 -1
  79. package/secp256k1.js +211 -28
  80. package/secp256k1.js.map +1 -1
  81. package/src/abstract/bls.ts +350 -67
  82. package/src/abstract/curve.ts +327 -44
  83. package/src/abstract/edwards.ts +367 -143
  84. package/src/abstract/fft.ts +369 -36
  85. package/src/abstract/frost.ts +1092 -0
  86. package/src/abstract/hash-to-curve.ts +255 -56
  87. package/src/abstract/modular.ts +591 -144
  88. package/src/abstract/montgomery.ts +114 -30
  89. package/src/abstract/oprf.ts +383 -194
  90. package/src/abstract/poseidon.ts +235 -35
  91. package/src/abstract/tower.ts +428 -159
  92. package/src/abstract/weierstrass.ts +710 -312
  93. package/src/bls12-381.ts +239 -236
  94. package/src/bn254.ts +107 -46
  95. package/src/ed25519.ts +227 -55
  96. package/src/ed448.ts +227 -57
  97. package/src/index.ts +7 -1
  98. package/src/misc.ts +154 -35
  99. package/src/nist.ts +143 -20
  100. package/src/secp256k1.ts +284 -41
  101. package/src/utils.ts +583 -81
  102. package/src/webcrypto.ts +302 -73
  103. package/utils.d.ts +457 -24
  104. package/utils.d.ts.map +1 -1
  105. package/utils.js +410 -53
  106. package/utils.js.map +1 -1
  107. package/webcrypto.d.ts +167 -25
  108. package/webcrypto.d.ts.map +1 -1
  109. package/webcrypto.js +165 -58
  110. package/webcrypto.js.map +1 -1
package/utils.js CHANGED
@@ -3,66 +3,299 @@
3
3
  * @module
4
4
  */
5
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
6
- import { abytes as abytes_, anumber, bytesToHex as bytesToHex_, concatBytes as concatBytes_, hexToBytes as hexToBytes_, } from '@noble/hashes/utils.js';
7
- export { abytes, anumber, bytesToHex, concatBytes, hexToBytes, isBytes, randomBytes, } from '@noble/hashes/utils.js';
6
+ import { abytes as abytes_, anumber as anumber_, bytesToHex as bytesToHex_, concatBytes as concatBytes_, hexToBytes as hexToBytes_, isBytes as isBytes_, randomBytes as randomBytes_, } from '@noble/hashes/utils.js';
7
+ /**
8
+ * Validates that a value is a byte array.
9
+ * @param value - Value to validate.
10
+ * @param length - Optional exact byte length.
11
+ * @param title - Optional field name.
12
+ * @returns Original byte array.
13
+ * @example
14
+ * Reject non-byte input before passing data into curve code.
15
+ *
16
+ * ```ts
17
+ * abytes(new Uint8Array(1));
18
+ * ```
19
+ */
20
+ export const abytes = (value, length, title) => abytes_(value, length, title);
21
+ /**
22
+ * Validates that a value is a non-negative safe integer.
23
+ * @param n - Value to validate.
24
+ * @param title - Optional field name.
25
+ * @example
26
+ * Validate a numeric length before allocating buffers.
27
+ *
28
+ * ```ts
29
+ * anumber(1);
30
+ * ```
31
+ */
32
+ export const anumber = anumber_;
33
+ /**
34
+ * Encodes bytes as lowercase hex.
35
+ * @param bytes - Bytes to encode.
36
+ * @returns Lowercase hex string.
37
+ * @example
38
+ * Serialize bytes as hex for logging or fixtures.
39
+ *
40
+ * ```ts
41
+ * bytesToHex(Uint8Array.of(1, 2, 3));
42
+ * ```
43
+ */
44
+ export const bytesToHex = bytesToHex_;
45
+ /**
46
+ * Concatenates byte arrays.
47
+ * @param arrays - Byte arrays to join.
48
+ * @returns Concatenated bytes.
49
+ * @example
50
+ * Join domain-separated chunks into one buffer.
51
+ *
52
+ * ```ts
53
+ * concatBytes(Uint8Array.of(1), Uint8Array.of(2));
54
+ * ```
55
+ */
56
+ export const concatBytes = (...arrays) => concatBytes_(...arrays);
57
+ /**
58
+ * Decodes lowercase or uppercase hex into bytes.
59
+ * @param hex - Hex string to decode.
60
+ * @returns Decoded bytes.
61
+ * @example
62
+ * Parse fixture hex into bytes before hashing.
63
+ *
64
+ * ```ts
65
+ * hexToBytes('0102');
66
+ * ```
67
+ */
68
+ export const hexToBytes = (hex) => hexToBytes_(hex);
69
+ /**
70
+ * Checks whether a value is a Uint8Array.
71
+ * @param a - Value to inspect.
72
+ * @returns `true` when `a` is a Uint8Array.
73
+ * @example
74
+ * Branch on byte input before decoding it.
75
+ *
76
+ * ```ts
77
+ * isBytes(new Uint8Array(1));
78
+ * ```
79
+ */
80
+ export const isBytes = isBytes_;
81
+ /**
82
+ * Reads random bytes from the platform CSPRNG.
83
+ * @param bytesLength - Number of random bytes to read.
84
+ * @returns Fresh random bytes.
85
+ * @example
86
+ * Generate a random seed for a keypair.
87
+ *
88
+ * ```ts
89
+ * randomBytes(2);
90
+ * ```
91
+ */
92
+ export const randomBytes = (bytesLength) => randomBytes_(bytesLength);
8
93
  const _0n = /* @__PURE__ */ BigInt(0);
9
94
  const _1n = /* @__PURE__ */ BigInt(1);
95
+ /**
96
+ * Validates that a flag is boolean.
97
+ * @param value - Value to validate.
98
+ * @param title - Optional field name.
99
+ * @returns Original value.
100
+ * @throws On wrong argument types. {@link TypeError}
101
+ * @example
102
+ * Reject non-boolean option flags early.
103
+ *
104
+ * ```ts
105
+ * abool(true);
106
+ * ```
107
+ */
10
108
  export function abool(value, title = '') {
11
109
  if (typeof value !== 'boolean') {
12
110
  const prefix = title && `"${title}" `;
13
- throw new Error(prefix + 'expected boolean, got type=' + typeof value);
111
+ throw new TypeError(prefix + 'expected boolean, got type=' + typeof value);
14
112
  }
15
113
  return value;
16
114
  }
17
- // Used in weierstrass, der
18
- function abignumber(n) {
115
+ /**
116
+ * Validates that a value is a non-negative bigint or safe integer.
117
+ * @param n - Value to validate.
118
+ * @returns The same validated value.
119
+ * @throws On wrong argument ranges or values. {@link RangeError}
120
+ * @example
121
+ * Validate one integer-like value before serializing it.
122
+ *
123
+ * ```ts
124
+ * abignumber(1n);
125
+ * ```
126
+ */
127
+ export function abignumber(n) {
19
128
  if (typeof n === 'bigint') {
20
129
  if (!isPosBig(n))
21
- throw new Error('positive bigint expected, got ' + n);
130
+ throw new RangeError('positive bigint expected, got ' + n);
22
131
  }
23
132
  else
24
133
  anumber(n);
25
134
  return n;
26
135
  }
136
+ /**
137
+ * Validates that a value is a safe integer.
138
+ * @param value - Integer to validate.
139
+ * @param title - Optional field name.
140
+ * @throws On wrong argument types. {@link TypeError}
141
+ * @throws On wrong argument ranges or values. {@link RangeError}
142
+ * @example
143
+ * Validate a window size before scalar arithmetic uses it.
144
+ *
145
+ * ```ts
146
+ * asafenumber(1);
147
+ * ```
148
+ */
27
149
  export function asafenumber(value, title = '') {
150
+ if (typeof value !== 'number') {
151
+ const prefix = title && `"${title}" `;
152
+ throw new TypeError(prefix + 'expected number, got type=' + typeof value);
153
+ }
28
154
  if (!Number.isSafeInteger(value)) {
29
155
  const prefix = title && `"${title}" `;
30
- throw new Error(prefix + 'expected safe integer, got type=' + typeof value);
156
+ throw new RangeError(prefix + 'expected safe integer, got ' + value);
31
157
  }
32
158
  }
159
+ /**
160
+ * Encodes a bigint into even-length big-endian hex.
161
+ * The historical "unpadded" name only means "no fixed-width field padding"; odd-length hex still
162
+ * gets one leading zero nibble so the result always represents whole bytes.
163
+ * @param num - Number to encode.
164
+ * @returns Big-endian hex string.
165
+ * @throws On wrong argument ranges or values. {@link RangeError}
166
+ * @example
167
+ * Encode a scalar into hex without a `0x` prefix.
168
+ *
169
+ * ```ts
170
+ * numberToHexUnpadded(255n);
171
+ * ```
172
+ */
33
173
  export function numberToHexUnpadded(num) {
34
174
  const hex = abignumber(num).toString(16);
35
175
  return hex.length & 1 ? '0' + hex : hex;
36
176
  }
177
+ /**
178
+ * Parses a big-endian hex string into bigint.
179
+ * Accepts odd-length hex through the native `BigInt('0x' + hex)` parser and currently surfaces the
180
+ * same native `SyntaxError` for malformed hex instead of wrapping it in a library-specific error.
181
+ * @param hex - Hex string without `0x`.
182
+ * @returns Parsed bigint value.
183
+ * @throws On wrong argument types. {@link TypeError}
184
+ * @example
185
+ * Parse a scalar from fixture hex.
186
+ *
187
+ * ```ts
188
+ * hexToNumber('ff');
189
+ * ```
190
+ */
37
191
  export function hexToNumber(hex) {
38
192
  if (typeof hex !== 'string')
39
- throw new Error('hex string expected, got ' + typeof hex);
193
+ throw new TypeError('hex string expected, got ' + typeof hex);
40
194
  return hex === '' ? _0n : BigInt('0x' + hex); // Big Endian
41
195
  }
42
196
  // BE: Big Endian, LE: Little Endian
197
+ /**
198
+ * Parses big-endian bytes into bigint.
199
+ * @param bytes - Bytes in big-endian order.
200
+ * @returns Parsed bigint value.
201
+ * @throws On wrong argument types. {@link TypeError}
202
+ * @example
203
+ * Read a scalar encoded in network byte order.
204
+ *
205
+ * ```ts
206
+ * bytesToNumberBE(Uint8Array.of(1, 0));
207
+ * ```
208
+ */
43
209
  export function bytesToNumberBE(bytes) {
44
210
  return hexToNumber(bytesToHex_(bytes));
45
211
  }
212
+ /**
213
+ * Parses little-endian bytes into bigint.
214
+ * @param bytes - Bytes in little-endian order.
215
+ * @returns Parsed bigint value.
216
+ * @throws On wrong argument types. {@link TypeError}
217
+ * @example
218
+ * Read a scalar encoded in little-endian form.
219
+ *
220
+ * ```ts
221
+ * bytesToNumberLE(Uint8Array.of(1, 0));
222
+ * ```
223
+ */
46
224
  export function bytesToNumberLE(bytes) {
47
225
  return hexToNumber(bytesToHex_(copyBytes(abytes_(bytes)).reverse()));
48
226
  }
227
+ /**
228
+ * Encodes a bigint into fixed-length big-endian bytes.
229
+ * @param n - Number to encode.
230
+ * @param len - Output length in bytes. Must be greater than zero.
231
+ * @returns Big-endian byte array.
232
+ * @throws On wrong argument ranges or values. {@link RangeError}
233
+ * @example
234
+ * Serialize a scalar into a 32-byte field element.
235
+ *
236
+ * ```ts
237
+ * numberToBytesBE(255n, 2);
238
+ * ```
239
+ */
49
240
  export function numberToBytesBE(n, len) {
50
- anumber(len);
241
+ anumber_(len);
242
+ if (len === 0)
243
+ throw new RangeError('zero length');
51
244
  n = abignumber(n);
52
- const res = hexToBytes_(n.toString(16).padStart(len * 2, '0'));
53
- if (res.length !== len)
54
- throw new Error('number too large');
55
- return res;
245
+ const hex = n.toString(16);
246
+ // Detect overflow before hex parsing so oversized values don't leak the shared odd-hex error.
247
+ if (hex.length > len * 2)
248
+ throw new RangeError('number too large');
249
+ return hexToBytes_(hex.padStart(len * 2, '0'));
56
250
  }
251
+ /**
252
+ * Encodes a bigint into fixed-length little-endian bytes.
253
+ * @param n - Number to encode.
254
+ * @param len - Output length in bytes.
255
+ * @returns Little-endian byte array.
256
+ * @throws On wrong argument ranges or values. {@link RangeError}
257
+ * @example
258
+ * Serialize a scalar for little-endian protocols.
259
+ *
260
+ * ```ts
261
+ * numberToBytesLE(255n, 2);
262
+ * ```
263
+ */
57
264
  export function numberToBytesLE(n, len) {
58
265
  return numberToBytesBE(n, len).reverse();
59
266
  }
60
267
  // Unpadded, rarely used
268
+ /**
269
+ * Encodes a bigint into variable-length big-endian bytes.
270
+ * @param n - Number to encode.
271
+ * @returns Variable-length big-endian bytes.
272
+ * @throws On wrong argument ranges or values. {@link RangeError}
273
+ * @example
274
+ * Serialize a bigint without fixed-width padding.
275
+ *
276
+ * ```ts
277
+ * numberToVarBytesBE(255n);
278
+ * ```
279
+ */
61
280
  export function numberToVarBytesBE(n) {
62
281
  return hexToBytes_(numberToHexUnpadded(abignumber(n)));
63
282
  }
64
283
  // Compares 2 u8a-s in kinda constant time
284
+ /**
285
+ * Compares two byte arrays in constant-ish time.
286
+ * @param a - Left byte array.
287
+ * @param b - Right byte array.
288
+ * @returns `true` when bytes match.
289
+ * @example
290
+ * Compare two encoded points without early exit.
291
+ *
292
+ * ```ts
293
+ * equalBytes(Uint8Array.of(1), Uint8Array.of(1));
294
+ * ```
295
+ */
65
296
  export function equalBytes(a, b) {
297
+ a = abytes(a);
298
+ b = abytes(b);
66
299
  if (a.length !== b.length)
67
300
  return false;
68
301
  let diff = 0;
@@ -73,33 +306,78 @@ export function equalBytes(a, b) {
73
306
  /**
74
307
  * Copies Uint8Array. We can't use u8a.slice(), because u8a can be Buffer,
75
308
  * and Buffer#slice creates mutable copy. Never use Buffers!
309
+ * @param bytes - Bytes to copy.
310
+ * @returns Detached copy.
311
+ * @example
312
+ * Make an isolated copy before mutating serialized bytes.
313
+ *
314
+ * ```ts
315
+ * copyBytes(Uint8Array.of(1, 2, 3));
316
+ * ```
76
317
  */
77
318
  export function copyBytes(bytes) {
78
- return Uint8Array.from(bytes);
319
+ // `Uint8Array.from(...)` would also accept arrays / other typed arrays. Keep this helper strict
320
+ // because callers use it at byte-validation boundaries before mutating the detached copy.
321
+ return Uint8Array.from(abytes(bytes));
79
322
  }
80
323
  /**
81
324
  * Decodes 7-bit ASCII string to Uint8Array, throws on non-ascii symbols
82
325
  * Should be safe to use for things expected to be ASCII.
83
326
  * Returns exact same result as `TextEncoder` for ASCII or throws.
327
+ * @param ascii - ASCII input text.
328
+ * @returns Encoded bytes.
329
+ * @throws On wrong argument types. {@link TypeError}
330
+ * @example
331
+ * Encode an ASCII domain-separation tag.
332
+ *
333
+ * ```ts
334
+ * asciiToBytes('ABC');
335
+ * ```
84
336
  */
85
337
  export function asciiToBytes(ascii) {
338
+ if (typeof ascii !== 'string')
339
+ throw new TypeError('ascii string expected, got ' + typeof ascii);
86
340
  return Uint8Array.from(ascii, (c, i) => {
87
341
  const charCode = c.charCodeAt(0);
88
342
  if (c.length !== 1 || charCode > 127) {
89
- throw new Error(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
343
+ throw new RangeError(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
90
344
  }
91
345
  return charCode;
92
346
  });
93
347
  }
94
- // Is positive bigint
348
+ // Historical name: this accepts non-negative bigints, including zero.
95
349
  const isPosBig = (n) => typeof n === 'bigint' && _0n <= n;
350
+ /**
351
+ * Checks whether a bigint lies inside a half-open range.
352
+ * @param n - Candidate value.
353
+ * @param min - Inclusive lower bound.
354
+ * @param max - Exclusive upper bound.
355
+ * @returns `true` when the value is inside the range.
356
+ * @example
357
+ * Check whether a candidate scalar fits the field order.
358
+ *
359
+ * ```ts
360
+ * inRange(2n, 1n, 3n);
361
+ * ```
362
+ */
96
363
  export function inRange(n, min, max) {
97
364
  return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
98
365
  }
99
366
  /**
100
- * Asserts min <= n < max. NOTE: It's < max and not <= max.
367
+ * Asserts `min <= n < max`. NOTE: upper bound is exclusive.
368
+ * @param title - Value label for error messages.
369
+ * @param n - Candidate value.
370
+ * @param min - Inclusive lower bound.
371
+ * @param max - Exclusive upper bound.
372
+ * Wrong-type inputs are not separated from out-of-range values here: they still flow through the
373
+ * shared `RangeError` path because this is only a throwing wrapper around `inRange(...)`.
374
+ * @throws On wrong argument ranges or values. {@link RangeError}
101
375
  * @example
102
- * aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
376
+ * Assert that a bigint stays within one half-open range.
377
+ *
378
+ * ```ts
379
+ * aInRange('x', 2n, 1n, 256n);
380
+ * ```
103
381
  */
104
382
  export function aInRange(title, n, min, max) {
105
383
  // Why min <= n < max and not a (min < n < max) OR b (min <= n <= max)?
@@ -108,15 +386,28 @@ export function aInRange(title, n, min, max) {
108
386
  // - b would commonly require subtraction: `inRange('x', x, 0n, P - 1n)`
109
387
  // - our way is the cleanest: `inRange('x', x, 0n, P)
110
388
  if (!inRange(n, min, max))
111
- throw new Error('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);
389
+ throw new RangeError('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);
112
390
  }
113
391
  // Bit operations
114
392
  /**
115
393
  * Calculates amount of bits in a bigint.
116
394
  * Same as `n.toString(2).length`
117
395
  * TODO: merge with nLength in modular
396
+ * @param n - Value to inspect.
397
+ * @returns Bit length.
398
+ * @throws If the value is negative. {@link Error}
399
+ * @example
400
+ * Measure the bit length of a scalar before serialization.
401
+ *
402
+ * ```ts
403
+ * bitLen(8n);
404
+ * ```
118
405
  */
119
406
  export function bitLen(n) {
407
+ // Size callers in this repo only use non-negative orders / scalars, so negative inputs are a
408
+ // contract bug and must not silently collapse to zero bits.
409
+ if (n < _0n)
410
+ throw new Error('expected non-negative bigint, got ' + n);
120
411
  let len;
121
412
  for (len = 0; n > _0n; n >>= _1n, len += 1)
122
413
  ;
@@ -126,48 +417,99 @@ export function bitLen(n) {
126
417
  * Gets single bit at position.
127
418
  * NOTE: first bit position is 0 (same as arrays)
128
419
  * Same as `!!+Array.from(n.toString(2)).reverse()[pos]`
420
+ * @param n - Source value.
421
+ * @param pos - Bit position. Negative positions are passed through to raw
422
+ * bigint shift semantics; because the mask is built as `1n << pos`,
423
+ * they currently collapse to `0n` and make the helper a no-op.
424
+ * @returns Bit as bigint.
425
+ * @example
426
+ * Gets single bit at position.
427
+ *
428
+ * ```ts
429
+ * bitGet(5n, 0);
430
+ * ```
129
431
  */
130
432
  export function bitGet(n, pos) {
131
433
  return (n >> BigInt(pos)) & _1n;
132
434
  }
133
435
  /**
134
436
  * Sets single bit at position.
437
+ * @param n - Source value.
438
+ * @param pos - Bit position. Negative positions are passed through to raw bigint shift semantics,
439
+ * so they currently behave like left shifts.
440
+ * @param value - Whether the bit should be set.
441
+ * @returns Updated bigint.
442
+ * @example
443
+ * Sets single bit at position.
444
+ *
445
+ * ```ts
446
+ * bitSet(0n, 1, true);
447
+ * ```
135
448
  */
136
449
  export function bitSet(n, pos, value) {
137
- return n | ((value ? _1n : _0n) << BigInt(pos));
450
+ const mask = _1n << BigInt(pos);
451
+ // Clearing needs AND-not here; OR with zero leaves an already-set bit untouched.
452
+ return value ? n | mask : n & ~mask;
138
453
  }
139
454
  /**
140
455
  * Calculate mask for N bits. Not using ** operator with bigints because of old engines.
141
456
  * Same as BigInt(`0b${Array(i).fill('1').join('')}`)
457
+ * @param n - Number of bits. Negative widths are currently passed through to raw bigint shift
458
+ * semantics and therefore produce `-1n`.
459
+ * @returns Bitmask value.
460
+ * @example
461
+ * Calculate mask for N bits.
462
+ *
463
+ * ```ts
464
+ * bitMask(4);
465
+ * ```
142
466
  */
143
467
  export const bitMask = (n) => (_1n << BigInt(n)) - _1n;
144
468
  /**
145
469
  * Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
146
- * @returns function that will call DRBG until 2nd arg returns something meaningful
470
+ * @param hashLen - Hash output size in bytes. Callers are expected to pass a positive length; `0`
471
+ * is not rejected here and would make the internal generate loop non-progressing.
472
+ * @param qByteLen - Requested output size in bytes. Callers are expected to pass a positive length.
473
+ * @param hmacFn - HMAC implementation.
474
+ * @returns Function that will call DRBG until the predicate returns anything
475
+ * other than `undefined`.
476
+ * @throws On wrong argument types. {@link TypeError}
147
477
  * @example
148
- * const drbg = createHmacDRBG<Key>(32, 32, hmac);
149
- * drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
478
+ * Build a deterministic nonce generator for RFC6979-style signing.
479
+ *
480
+ * ```ts
481
+ * import { createHmacDrbg } from '@noble/curves/utils.js';
482
+ * import { hmac } from '@noble/hashes/hmac.js';
483
+ * import { sha256 } from '@noble/hashes/sha2.js';
484
+ * const drbg = createHmacDrbg(32, 32, (key, msg) => hmac(sha256, key, msg));
485
+ * const seed = new Uint8Array(32);
486
+ * drbg(seed, (bytes) => bytes);
487
+ * ```
150
488
  */
151
489
  export function createHmacDrbg(hashLen, qByteLen, hmacFn) {
152
- anumber(hashLen, 'hashLen');
153
- anumber(qByteLen, 'qByteLen');
490
+ anumber_(hashLen, 'hashLen');
491
+ anumber_(qByteLen, 'qByteLen');
154
492
  if (typeof hmacFn !== 'function')
155
- throw new Error('hmacFn must be a function');
156
- const u8n = (len) => new Uint8Array(len); // creates Uint8Array
493
+ throw new TypeError('hmacFn must be a function');
494
+ // creates Uint8Array
495
+ const u8n = (len) => new Uint8Array(len);
157
496
  const NULL = Uint8Array.of();
158
497
  const byte0 = Uint8Array.of(0x00);
159
498
  const byte1 = Uint8Array.of(0x01);
160
499
  const _maxDrbgIters = 1000;
161
- // Step B, Step C: set hashLen to 8*ceil(hlen/8)
162
- let v = u8n(hashLen); // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
163
- let k = u8n(hashLen); // Steps B and C of RFC6979 3.2: set hashLen, in our case always same
500
+ // Step B, Step C: set hashLen to 8*ceil(hlen/8).
501
+ // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 signatures.
502
+ let v = u8n(hashLen);
503
+ // Steps B and C of RFC6979 3.2.
504
+ let k = u8n(hashLen);
164
505
  let i = 0; // Iterations counter, will throw when over 1000
165
506
  const reset = () => {
166
507
  v.fill(1);
167
508
  k.fill(0);
168
509
  i = 0;
169
510
  };
170
- const h = (...msgs) => hmacFn(k, concatBytes_(v, ...msgs)); // hmac(k)(v, ...values)
511
+ // hmac(k)(v, ...values)
512
+ const h = (...msgs) => hmacFn(k, concatBytes(v, ...msgs));
171
513
  const reseed = (seed = NULL) => {
172
514
  // HMAC-DRBG reseed() function. Steps D-G
173
515
  k = h(byte0, seed); // k = hmac(k || v || 0x00 || seed)
@@ -189,53 +531,68 @@ export function createHmacDrbg(hashLen, qByteLen, hmacFn) {
189
531
  out.push(sl);
190
532
  len += v.length;
191
533
  }
192
- return concatBytes_(...out);
534
+ return concatBytes(...out);
193
535
  };
194
536
  const genUntil = (seed, pred) => {
195
537
  reset();
196
538
  reseed(seed); // Steps D-G
197
- let res = undefined; // Step H: grind until k is in [1..n-1]
198
- while (!(res = pred(gen())))
539
+ let res = undefined; // Step H: grind until the predicate accepts a candidate.
540
+ // Falsy values like 0 are valid outputs.
541
+ while ((res = pred(gen())) === undefined)
199
542
  reseed();
200
543
  reset();
201
544
  return res;
202
545
  };
203
546
  return genUntil;
204
547
  }
548
+ /**
549
+ * Validates declared required and optional field types on a plain object.
550
+ * Extra keys are intentionally ignored because many callers validate only the subset they use from
551
+ * richer option bags or runtime objects.
552
+ * @param object - Object to validate.
553
+ * @param fields - Required field types.
554
+ * @param optFields - Optional field types.
555
+ * @throws On wrong argument types. {@link TypeError}
556
+ * @example
557
+ * Check user options before building a curve helper.
558
+ *
559
+ * ```ts
560
+ * validateObject({ flag: true }, { flag: 'boolean' });
561
+ * ```
562
+ */
205
563
  export function validateObject(object, fields = {}, optFields = {}) {
206
- if (!object || typeof object !== 'object')
207
- throw new Error('expected valid options object');
564
+ if (Object.prototype.toString.call(object) !== '[object Object]')
565
+ throw new TypeError('expected valid options object');
208
566
  function checkField(fieldName, expectedType, isOpt) {
567
+ // Config/data fields must be explicit own properties, but runtime objects such as Field
568
+ // instances intentionally satisfy required method slots via their shared prototype.
569
+ if (!isOpt && expectedType !== 'function' && !Object.hasOwn(object, fieldName))
570
+ throw new TypeError(`param "${fieldName}" is invalid: expected own property`);
209
571
  const val = object[fieldName];
210
572
  if (isOpt && val === undefined)
211
573
  return;
212
574
  const current = typeof val;
213
575
  if (current !== expectedType || val === null)
214
- throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
576
+ throw new TypeError(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
215
577
  }
216
578
  const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
217
579
  iter(fields, false);
218
580
  iter(optFields, true);
219
581
  }
220
582
  /**
221
- * throws not implemented error
583
+ * Throws not implemented error.
584
+ * @returns Never returns.
585
+ * @throws If the unfinished code path is reached. {@link Error}
586
+ * @example
587
+ * Surface the placeholder error from an unfinished code path.
588
+ *
589
+ * ```ts
590
+ * try {
591
+ * notImplemented();
592
+ * } catch {}
593
+ * ```
222
594
  */
223
595
  export const notImplemented = () => {
224
596
  throw new Error('not implemented');
225
597
  };
226
- /**
227
- * Memoizes (caches) computation result.
228
- * Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
229
- */
230
- export function memoized(fn) {
231
- const map = new WeakMap();
232
- return (arg, ...args) => {
233
- const val = map.get(arg);
234
- if (val !== undefined)
235
- return val;
236
- const computed = fn(arg, ...args);
237
- map.set(arg, computed);
238
- return computed;
239
- };
240
- }
241
598
  //# sourceMappingURL=utils.js.map
package/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,sEAAsE;AACtE,OAAO,EACL,MAAM,IAAI,OAAO,EACjB,OAAO,EACP,UAAU,IAAI,WAAW,EACzB,WAAW,IAAI,YAAY,EAC3B,UAAU,IAAI,WAAW,GAC1B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,MAAM,EACN,OAAO,EACP,UAAU,EACV,WAAW,EACX,UAAU,EACV,OAAO,EACP,WAAW,GACZ,MAAM,wBAAwB,CAAC;AAChC,MAAM,GAAG,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,GAAG,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAStC,MAAM,UAAU,KAAK,CAAC,KAAc,EAAE,QAAgB,EAAE;IACtD,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;Q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