@ocap/mcrypto 1.30.24 → 1.30.25

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 (51) hide show
  1. package/esm/crypter/aes-legacy.d.mts +11 -0
  2. package/esm/crypter/aes-legacy.mjs +67 -15
  3. package/esm/crypter/aes.d.mts +14 -2
  4. package/esm/crypter/aes.mjs +69 -3
  5. package/esm/encode.mjs +1 -1
  6. package/esm/hasher/keccak.mjs +7 -2
  7. package/esm/hasher/sha2.d.mts +1 -1
  8. package/esm/hasher/sha2.mjs +26 -11
  9. package/esm/hasher/sha3.d.mts +1 -1
  10. package/esm/hasher/sha3.mjs +21 -6
  11. package/esm/index.d.mts +1 -1
  12. package/esm/signer/ed25519.d.mts +1 -1
  13. package/esm/signer/ed25519.mjs +143 -16
  14. package/esm/signer/ethereum.mjs +7 -3
  15. package/esm/signer/secp256k1.mjs +3 -2
  16. package/esm/webauthn.d.mts +68 -2
  17. package/esm/webauthn.mjs +64 -16
  18. package/lib/_vendor/@noble/curves/LICENSE +21 -0
  19. package/lib/_vendor/@noble/curves/abstract/curve.cjs +373 -0
  20. package/lib/_vendor/@noble/curves/abstract/modular.cjs +616 -0
  21. package/lib/_vendor/@noble/curves/abstract/weierstrass.cjs +1020 -0
  22. package/lib/_vendor/@noble/curves/nist.cjs +91 -0
  23. package/lib/_vendor/@noble/curves/secp256k1.cjs +73 -0
  24. package/lib/_vendor/@noble/curves/utils.cjs +463 -0
  25. package/lib/_vendor/@noble/ed25519/LICENSE +21 -0
  26. package/lib/_vendor/@noble/ed25519/index.cjs +512 -0
  27. package/lib/_vendor/@noble/hashes/LICENSE +21 -0
  28. package/lib/_vendor/@noble/hashes/_md.cjs +230 -0
  29. package/lib/_vendor/@noble/hashes/_u64.cjs +67 -0
  30. package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
  31. package/lib/_vendor/@noble/hashes/sha2.cjs +477 -0
  32. package/lib/_vendor/@noble/hashes/sha3.cjs +329 -0
  33. package/lib/_vendor/@noble/hashes/utils.cjs +410 -0
  34. package/lib/crypter/aes-legacy.cjs +68 -21
  35. package/lib/crypter/aes-legacy.d.cts +11 -0
  36. package/lib/crypter/aes.cjs +69 -2
  37. package/lib/crypter/aes.d.cts +14 -2
  38. package/lib/encode.cjs +1 -1
  39. package/lib/hasher/keccak.cjs +13 -8
  40. package/lib/hasher/sha2.cjs +32 -17
  41. package/lib/hasher/sha2.d.cts +1 -1
  42. package/lib/hasher/sha3.cjs +27 -12
  43. package/lib/hasher/sha3.d.cts +1 -1
  44. package/lib/index.d.cts +1 -1
  45. package/lib/signer/ed25519.cjs +146 -20
  46. package/lib/signer/ed25519.d.cts +1 -1
  47. package/lib/signer/ethereum.cjs +16 -12
  48. package/lib/signer/secp256k1.cjs +11 -10
  49. package/lib/webauthn.cjs +80 -31
  50. package/lib/webauthn.d.cts +68 -2
  51. package/package.json +6 -7
@@ -0,0 +1,512 @@
1
+
2
+ //#region ../../node_modules/@noble/ed25519/index.js
3
+ /*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) */
4
+ /**
5
+ * 5KB JS implementation of ed25519 EdDSA signatures.
6
+ * Compliant with RFC8032, FIPS 186-5 & ZIP215.
7
+ * @module
8
+ * @example
9
+ * ```js
10
+ import * as ed from '@noble/ed25519';
11
+ (async () => {
12
+ const secretKey = ed.utils.randomSecretKey();
13
+ const message = Uint8Array.from([0xab, 0xbc, 0xcd, 0xde]);
14
+ const pubKey = await ed.getPublicKeyAsync(secretKey); // Sync methods are also present
15
+ const signature = await ed.signAsync(message, secretKey);
16
+ const isValid = await ed.verifyAsync(signature, message, pubKey);
17
+ })();
18
+ ```
19
+ */
20
+ /**
21
+ * Curve params. ed25519 is twisted edwards curve. Equation is −x² + y² = -a + dx²y².
22
+ * * P = `2n**255n - 19n` // field over which calculations are done
23
+ * * N = `2n**252n + 27742317777372353535851937790883648493n` // group order, amount of curve points
24
+ * * h = 8 // cofactor
25
+ * * a = `Fp.create(BigInt(-1))` // equation param
26
+ * * d = -121665/121666 a.k.a. `Fp.neg(121665 * Fp.inv(121666))` // equation param
27
+ * * Gx, Gy are coordinates of Generator / base point
28
+ */
29
+ const ed25519_CURVE = {
30
+ p: 57896044618658097711785492504343953926634992332820282019728792003956564819949n,
31
+ n: 7237005577332262213973186563042994240857116359379907606001950938285454250989n,
32
+ h: 8n,
33
+ a: 57896044618658097711785492504343953926634992332820282019728792003956564819948n,
34
+ d: 37095705934669439343138083508754565189542113879843219016388785533085940283555n,
35
+ Gx: 15112221349535400772501151409588531511454012693041857206046113283949847762202n,
36
+ Gy: 46316835694926478169428394003475163141307993866256225615783033603165251855960n
37
+ };
38
+ const { p: P, n: N, Gx, Gy, a: _a, d: _d, h } = ed25519_CURVE;
39
+ const L = 32;
40
+ const captureTrace = (...args) => {
41
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") Error.captureStackTrace(...args);
42
+ };
43
+ const err = (message = "") => {
44
+ const e = new Error(message);
45
+ captureTrace(e, err);
46
+ throw e;
47
+ };
48
+ const isBig = (n) => typeof n === "bigint";
49
+ const isStr = (s) => typeof s === "string";
50
+ const isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
51
+ /** Asserts something is Uint8Array. */
52
+ const abytes = (value, length, title = "") => {
53
+ const bytes = isBytes(value);
54
+ const len = value?.length;
55
+ const needsLen = length !== void 0;
56
+ if (!bytes || needsLen && len !== length) {
57
+ const prefix = title && `"${title}" `;
58
+ const ofLen = needsLen ? ` of length ${length}` : "";
59
+ const got = bytes ? `length=${len}` : `type=${typeof value}`;
60
+ err(prefix + "expected Uint8Array" + ofLen + ", got " + got);
61
+ }
62
+ return value;
63
+ };
64
+ /** create Uint8Array */
65
+ const u8n = (len) => new Uint8Array(len);
66
+ const u8fr = (buf) => Uint8Array.from(buf);
67
+ const padh = (n, pad) => n.toString(16).padStart(pad, "0");
68
+ const bytesToHex = (b) => Array.from(abytes(b)).map((e) => padh(e, 2)).join("");
69
+ const C = {
70
+ _0: 48,
71
+ _9: 57,
72
+ A: 65,
73
+ F: 70,
74
+ a: 97,
75
+ f: 102
76
+ };
77
+ const _ch = (ch) => {
78
+ if (ch >= C._0 && ch <= C._9) return ch - C._0;
79
+ if (ch >= C.A && ch <= C.F) return ch - (C.A - 10);
80
+ if (ch >= C.a && ch <= C.f) return ch - (C.a - 10);
81
+ };
82
+ const hexToBytes = (hex) => {
83
+ const e = "hex invalid";
84
+ if (!isStr(hex)) return err(e);
85
+ const hl = hex.length;
86
+ const al = hl / 2;
87
+ if (hl % 2) return err(e);
88
+ const array = u8n(al);
89
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
90
+ const n1 = _ch(hex.charCodeAt(hi));
91
+ const n2 = _ch(hex.charCodeAt(hi + 1));
92
+ if (n1 === void 0 || n2 === void 0) return err(e);
93
+ array[ai] = n1 * 16 + n2;
94
+ }
95
+ return array;
96
+ };
97
+ const cr = () => globalThis?.crypto;
98
+ const subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined, consider polyfill");
99
+ const concatBytes = (...arrs) => {
100
+ const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
101
+ let pad = 0;
102
+ arrs.forEach((a) => {
103
+ r.set(a, pad);
104
+ pad += a.length;
105
+ });
106
+ return r;
107
+ };
108
+ const big = BigInt;
109
+ const assertRange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
110
+ /** modular division */
111
+ const M = (a, b = P) => {
112
+ const r = a % b;
113
+ return r >= 0n ? r : b + r;
114
+ };
115
+ const P_MASK = (1n << 255n) - 1n;
116
+ const modP = (num) => {
117
+ if (num < 0n) err("negative coordinate");
118
+ let r = (num >> 255n) * 19n + (num & P_MASK);
119
+ r = (r >> 255n) * 19n + (r & P_MASK);
120
+ return r % P;
121
+ };
122
+ const modN = (a) => M(a, N);
123
+ /** Modular inversion using euclidean GCD (non-CT). No negative exponent for now. */
124
+ const invert = (num, md) => {
125
+ if (num === 0n || md <= 0n) err("no inverse n=" + num + " mod=" + md);
126
+ let a = M(num, md), b = md, x = 0n, y = 1n, u = 1n, v = 0n;
127
+ while (a !== 0n) {
128
+ const q = b / a, r = b % a;
129
+ const m = x - u * q, n = y - v * q;
130
+ b = a, a = r, x = u, y = v, u = m, v = n;
131
+ }
132
+ return b === 1n ? M(x, md) : err("no inverse");
133
+ };
134
+ const callHash = (name) => {
135
+ const fn = hashes[name];
136
+ if (typeof fn !== "function") err("hashes." + name + " not set");
137
+ return fn;
138
+ };
139
+ const apoint = (p) => p instanceof Point ? p : err("Point expected");
140
+ const B256 = 2n ** 256n;
141
+ /** Point in XYZT extended coordinates. */
142
+ var Point = class Point {
143
+ static BASE;
144
+ static ZERO;
145
+ X;
146
+ Y;
147
+ Z;
148
+ T;
149
+ constructor(X, Y, Z, T) {
150
+ const max = B256;
151
+ this.X = assertRange(X, 0n, max);
152
+ this.Y = assertRange(Y, 0n, max);
153
+ this.Z = assertRange(Z, 1n, max);
154
+ this.T = assertRange(T, 0n, max);
155
+ Object.freeze(this);
156
+ }
157
+ static CURVE() {
158
+ return ed25519_CURVE;
159
+ }
160
+ static fromAffine(p) {
161
+ return new Point(p.x, p.y, 1n, modP(p.x * p.y));
162
+ }
163
+ /** RFC8032 5.1.3: Uint8Array to Point. */
164
+ static fromBytes(hex, zip215 = false) {
165
+ const d = _d;
166
+ const normed = u8fr(abytes(hex, L));
167
+ const lastByte = hex[31];
168
+ normed[31] = lastByte & -129;
169
+ const y = bytesToNumberLE(normed);
170
+ assertRange(y, 0n, zip215 ? B256 : P);
171
+ const y2 = modP(y * y);
172
+ let { isValid, value: x } = uvRatio(M(y2 - 1n), modP(d * y2 + 1n));
173
+ if (!isValid) err("bad point: y not sqrt");
174
+ const isXOdd = (x & 1n) === 1n;
175
+ const isLastByteOdd = (lastByte & 128) !== 0;
176
+ if (!zip215 && x === 0n && isLastByteOdd) err("bad point: x==0, isLastByteOdd");
177
+ if (isLastByteOdd !== isXOdd) x = M(-x);
178
+ return new Point(x, y, 1n, modP(x * y));
179
+ }
180
+ static fromHex(hex, zip215) {
181
+ return Point.fromBytes(hexToBytes(hex), zip215);
182
+ }
183
+ get x() {
184
+ return this.toAffine().x;
185
+ }
186
+ get y() {
187
+ return this.toAffine().y;
188
+ }
189
+ /** Checks if the point is valid and on-curve. */
190
+ assertValidity() {
191
+ const a = _a;
192
+ const d = _d;
193
+ const p = this;
194
+ if (p.is0()) return err("bad point: ZERO");
195
+ const { X, Y, Z, T } = p;
196
+ const X2 = modP(X * X);
197
+ const Y2 = modP(Y * Y);
198
+ const Z2 = modP(Z * Z);
199
+ const Z4 = modP(Z2 * Z2);
200
+ if (modP(Z2 * (modP(X2 * a) + Y2)) !== M(Z4 + modP(d * modP(X2 * Y2)))) return err("bad point: equation left != right (1)");
201
+ if (modP(X * Y) !== modP(Z * T)) return err("bad point: equation left != right (2)");
202
+ return this;
203
+ }
204
+ /** Equality check: compare points P&Q. */
205
+ equals(other) {
206
+ const { X: X1, Y: Y1, Z: Z1 } = this;
207
+ const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
208
+ const X1Z2 = modP(X1 * Z2);
209
+ const X2Z1 = modP(X2 * Z1);
210
+ const Y1Z2 = modP(Y1 * Z2);
211
+ const Y2Z1 = modP(Y2 * Z1);
212
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
213
+ }
214
+ is0() {
215
+ return this.equals(I);
216
+ }
217
+ /** Flip point over y coordinate. */
218
+ negate() {
219
+ return new Point(M(-this.X), this.Y, this.Z, M(-this.T));
220
+ }
221
+ /** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
222
+ double() {
223
+ const { X: X1, Y: Y1, Z: Z1 } = this;
224
+ const a = _a;
225
+ const A = modP(X1 * X1);
226
+ const B = modP(Y1 * Y1);
227
+ const C$1 = modP(2n * Z1 * Z1);
228
+ const D = modP(a * A);
229
+ const x1y1 = M(X1 + Y1);
230
+ const E = M(modP(x1y1 * x1y1) - A - B);
231
+ const G$1 = M(D + B);
232
+ const F = M(G$1 - C$1);
233
+ const H = M(D - B);
234
+ const X3 = modP(E * F);
235
+ const Y3 = modP(G$1 * H);
236
+ const T3 = modP(E * H);
237
+ return new Point(X3, Y3, modP(F * G$1), T3);
238
+ }
239
+ /** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
240
+ add(other) {
241
+ const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
242
+ const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
243
+ const a = _a;
244
+ const d = _d;
245
+ const A = modP(X1 * X2);
246
+ const B = modP(Y1 * Y2);
247
+ const C$1 = modP(modP(T1 * d) * T2);
248
+ const D = modP(Z1 * Z2);
249
+ const E = M(modP(M(X1 + Y1) * M(X2 + Y2)) - A - B);
250
+ const F = M(D - C$1);
251
+ const G$1 = M(D + C$1);
252
+ const H = M(B - modP(a * A));
253
+ const X3 = modP(E * F);
254
+ const Y3 = modP(G$1 * H);
255
+ const T3 = modP(E * H);
256
+ return new Point(X3, Y3, modP(F * G$1), T3);
257
+ }
258
+ subtract(other) {
259
+ return this.add(apoint(other).negate());
260
+ }
261
+ /**
262
+ * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
263
+ * Uses {@link wNAF} for base point.
264
+ * Uses fake point to mitigate side-channel leakage.
265
+ * @param n scalar by which point is multiplied
266
+ * @param safe safe mode guards against timing attacks; unsafe mode is faster
267
+ */
268
+ multiply(n, safe = true) {
269
+ if (!safe && (n === 0n || this.is0())) return I;
270
+ assertRange(n, 1n, N);
271
+ if (n === 1n) return this;
272
+ if (this.equals(G)) return wNAF(n).p;
273
+ let p = I;
274
+ let f = G;
275
+ for (let d = this; n > 0n; d = d.double(), n >>= 1n) if (n & 1n) p = p.add(d);
276
+ else if (safe) f = f.add(d);
277
+ return p;
278
+ }
279
+ multiplyUnsafe(scalar) {
280
+ return this.multiply(scalar, false);
281
+ }
282
+ /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
283
+ toAffine() {
284
+ const { X, Y, Z } = this;
285
+ if (this.equals(I)) return {
286
+ x: 0n,
287
+ y: 1n
288
+ };
289
+ const iz = invert(Z, P);
290
+ if (modP(Z * iz) !== 1n) err("invalid inverse");
291
+ return {
292
+ x: modP(X * iz),
293
+ y: modP(Y * iz)
294
+ };
295
+ }
296
+ toBytes() {
297
+ const { x, y } = this.toAffine();
298
+ const b = numTo32bLE(y);
299
+ b[31] |= x & 1n ? 128 : 0;
300
+ return b;
301
+ }
302
+ toHex() {
303
+ return bytesToHex(this.toBytes());
304
+ }
305
+ clearCofactor() {
306
+ return this.multiply(big(h), false);
307
+ }
308
+ isSmallOrder() {
309
+ return this.clearCofactor().is0();
310
+ }
311
+ isTorsionFree() {
312
+ let p = this.multiply(N / 2n, false).double();
313
+ if (N % 2n) p = p.add(this);
314
+ return p.is0();
315
+ }
316
+ };
317
+ /** Generator / base point */
318
+ const G = new Point(Gx, Gy, 1n, M(Gx * Gy));
319
+ /** Identity / zero point */
320
+ const I = new Point(0n, 1n, 1n, 0n);
321
+ Point.BASE = G;
322
+ Point.ZERO = I;
323
+ const numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), 64)).reverse();
324
+ const bytesToNumberLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
325
+ const pow2 = (x, power) => {
326
+ let r = x;
327
+ while (power-- > 0n) r = modP(r * r);
328
+ return r;
329
+ };
330
+ const pow_2_252_3 = (x) => {
331
+ const b2 = modP(modP(x * x) * x);
332
+ const b5 = modP(pow2(modP(pow2(b2, 2n) * b2), 1n) * x);
333
+ const b10 = modP(pow2(b5, 5n) * b5);
334
+ const b20 = modP(pow2(b10, 10n) * b10);
335
+ const b40 = modP(pow2(b20, 20n) * b20);
336
+ const b80 = modP(pow2(b40, 40n) * b40);
337
+ return {
338
+ pow_p_5_8: modP(pow2(modP(pow2(modP(pow2(modP(pow2(b80, 80n) * b80), 80n) * b80), 10n) * b10), 2n) * x),
339
+ b2
340
+ };
341
+ };
342
+ const RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
343
+ const uvRatio = (u, v) => {
344
+ const v3 = modP(v * modP(v * v));
345
+ const pow = pow_2_252_3(modP(u * modP(modP(v3 * v3) * v))).pow_p_5_8;
346
+ let x = modP(u * modP(v3 * pow));
347
+ const vx2 = modP(v * modP(x * x));
348
+ const root1 = x;
349
+ const root2 = modP(x * RM1);
350
+ const useRoot1 = vx2 === u;
351
+ const useRoot2 = vx2 === M(-u);
352
+ const noRoot = vx2 === M(-u * RM1);
353
+ if (useRoot1) x = root1;
354
+ if (useRoot2 || noRoot) x = root2;
355
+ if ((M(x) & 1n) === 1n) x = M(-x);
356
+ return {
357
+ isValid: useRoot1 || useRoot2,
358
+ value: x
359
+ };
360
+ };
361
+ const modL_LE = (hash) => modN(bytesToNumberLE(hash));
362
+ const sha512s = (...m) => callHash("sha512")(concatBytes(...m));
363
+ const hash2extK = (hashed) => {
364
+ const head = hashed.slice(0, 32);
365
+ head[0] &= 248;
366
+ head[31] &= 127;
367
+ head[31] |= 64;
368
+ const prefix = hashed.slice(32, 64);
369
+ const scalar = modL_LE(head);
370
+ const point = G.multiply(scalar);
371
+ return {
372
+ head,
373
+ prefix,
374
+ scalar,
375
+ point,
376
+ pointBytes: point.toBytes()
377
+ };
378
+ };
379
+ const getExtendedPublicKey = (secretKey) => hash2extK(sha512s(abytes(secretKey, L)));
380
+ /** Creates 32-byte ed25519 public key from 32-byte secret key. To use, set `hashes.sha512` first. */
381
+ const getPublicKey = (priv) => getExtendedPublicKey(priv).pointBytes;
382
+ const hashFinishS = (res) => res.finish(sha512s(res.hashable));
383
+ const _sign = (e, rBytes, msg) => {
384
+ const { pointBytes: P$1, scalar: s } = e;
385
+ const r = modL_LE(rBytes);
386
+ const R = G.multiply(r).toBytes();
387
+ const hashable = concatBytes(R, P$1, msg);
388
+ const finish = (hashed) => {
389
+ return abytes(concatBytes(R, numTo32bLE(modN(r + modL_LE(hashed) * s))), 64);
390
+ };
391
+ return {
392
+ hashable,
393
+ finish
394
+ };
395
+ };
396
+ /**
397
+ * Signs message using secret key. To use, set `hashes.sha512` first.
398
+ * Follows RFC8032 5.1.6.
399
+ */
400
+ const sign = (message, secretKey) => {
401
+ const m = abytes(message);
402
+ const e = getExtendedPublicKey(secretKey);
403
+ return hashFinishS(_sign(e, sha512s(e.prefix, m), m));
404
+ };
405
+ const defaultVerifyOpts = { zip215: true };
406
+ const _verify = (sig, msg, publicKey, options = defaultVerifyOpts) => {
407
+ sig = abytes(sig, 64);
408
+ msg = abytes(msg);
409
+ publicKey = abytes(publicKey, L);
410
+ const { zip215 } = options;
411
+ const r = sig.subarray(0, L);
412
+ const s = bytesToNumberLE(sig.subarray(L, L * 2));
413
+ let A, R, SB;
414
+ let hashable = Uint8Array.of();
415
+ let finished = false;
416
+ try {
417
+ A = Point.fromBytes(publicKey, zip215);
418
+ R = Point.fromBytes(r, zip215);
419
+ SB = G.multiply(s, false);
420
+ hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
421
+ finished = true;
422
+ } catch (error) {}
423
+ const finish = (hashed) => {
424
+ if (!finished) return false;
425
+ if (!zip215 && A.isSmallOrder()) return false;
426
+ const k = modL_LE(hashed);
427
+ return R.add(A.multiply(k, false)).subtract(SB).clearCofactor().is0();
428
+ };
429
+ return {
430
+ hashable,
431
+ finish
432
+ };
433
+ };
434
+ /** Verifies signature on message and public key. To use, set `hashes.sha512` first. Follows RFC8032 5.1.7. */
435
+ const verify = (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishS(_verify(signature, message, publicKey, opts));
436
+ const hashes = {
437
+ sha512Async: async (message) => {
438
+ const s = subtle();
439
+ const m = concatBytes(message);
440
+ return u8n(await s.digest("SHA-512", m.buffer));
441
+ },
442
+ sha512: void 0
443
+ };
444
+ const W = 8;
445
+ const pwindows = Math.ceil(256 / W) + 1;
446
+ const pwindowSize = 2 ** (W - 1);
447
+ const precompute = () => {
448
+ const points = [];
449
+ let p = G;
450
+ let b = p;
451
+ for (let w = 0; w < pwindows; w++) {
452
+ b = p;
453
+ points.push(b);
454
+ for (let i = 1; i < pwindowSize; i++) {
455
+ b = b.add(p);
456
+ points.push(b);
457
+ }
458
+ p = b.double();
459
+ }
460
+ return points;
461
+ };
462
+ let Gpows = void 0;
463
+ const ctneg = (cnd, p) => {
464
+ const n = p.negate();
465
+ return cnd ? n : p;
466
+ };
467
+ /**
468
+ * Precomputes give 12x faster getPublicKey(), 10x sign(), 2x verify() by
469
+ * caching multiples of G (base point). Cache is stored in 32MB of RAM.
470
+ * Any time `G.multiply` is done, precomputes are used.
471
+ * Not used for getSharedSecret, which instead multiplies random pubkey `P.multiply`.
472
+ *
473
+ * w-ary non-adjacent form (wNAF) precomputation method is 10% slower than windowed method,
474
+ * but takes 2x less RAM. RAM reduction is possible by utilizing `.subtract`.
475
+ *
476
+ * !! Precomputes can be disabled by commenting-out call of the wNAF() inside Point#multiply().
477
+ */
478
+ const wNAF = (n) => {
479
+ const comp = Gpows || (Gpows = precompute());
480
+ let p = I;
481
+ let f = G;
482
+ const pow_2_w = 2 ** W;
483
+ const maxNum = pow_2_w;
484
+ const mask = big(pow_2_w - 1);
485
+ const shiftBy = big(W);
486
+ for (let w = 0; w < pwindows; w++) {
487
+ let wbits = Number(n & mask);
488
+ n >>= shiftBy;
489
+ if (wbits > pwindowSize) {
490
+ wbits -= maxNum;
491
+ n += 1n;
492
+ }
493
+ const off = w * pwindowSize;
494
+ const offF = off;
495
+ const offP = off + Math.abs(wbits) - 1;
496
+ const isEven = w % 2 !== 0;
497
+ const isNeg = wbits < 0;
498
+ if (wbits === 0) f = f.add(ctneg(isEven, comp[offF]));
499
+ else p = p.add(ctneg(isNeg, comp[offP]));
500
+ }
501
+ if (n !== 0n) err("invalid wnaf");
502
+ return {
503
+ p,
504
+ f
505
+ };
506
+ };
507
+
508
+ //#endregion
509
+ exports.getPublicKey = getPublicKey;
510
+ exports.hashes = hashes;
511
+ exports.sign = sign;
512
+ exports.verify = verify;
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Paul Miller (https://paulmillr.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.