@motebit/crypto 1.1.0 → 1.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.
- package/dist/artifacts.d.ts +1 -1
- package/dist/artifacts.js +1158 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/credential-anchor.d.ts.map +1 -1
- package/dist/credential-anchor.js +200 -0
- package/dist/credential-anchor.js.map +1 -0
- package/dist/credentials.js +212 -0
- package/dist/credentials.js.map +1 -0
- package/dist/deletion-certificate.d.ts +256 -0
- package/dist/deletion-certificate.d.ts.map +1 -0
- package/dist/deletion-certificate.js +562 -0
- package/dist/deletion-certificate.js.map +1 -0
- package/dist/hardware-attestation.js +400 -0
- package/dist/hardware-attestation.js.map +1 -0
- package/dist/index.d.ts +120 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1070 -5862
- package/dist/index.js.map +1 -0
- package/dist/merkle.d.ts +34 -0
- package/dist/merkle.d.ts.map +1 -0
- package/dist/merkle.js +84 -0
- package/dist/merkle.js.map +1 -0
- package/dist/signing.js +314 -0
- package/dist/signing.js.map +1 -0
- package/dist/skills.d.ts +95 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +228 -0
- package/dist/skills.js.map +1 -0
- package/dist/suite-dispatch.js +189 -3223
- package/dist/suite-dispatch.js.map +1 -0
- package/dist/witness-omission-dispute.d.ts +98 -0
- package/dist/witness-omission-dispute.d.ts.map +1 -0
- package/dist/witness-omission-dispute.js +237 -0
- package/dist/witness-omission-dispute.js.map +1 -0
- package/package.json +2 -2
package/dist/suite-dispatch.js
CHANGED
|
@@ -1,3233 +1,199 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return b === 1n ? M(x, md) : err("no inverse");
|
|
116
|
-
};
|
|
117
|
-
var apoint = (p) => p instanceof Point ? p : err("Point expected");
|
|
118
|
-
var B256 = 2n ** 256n;
|
|
119
|
-
var Point = class _Point {
|
|
120
|
-
static BASE;
|
|
121
|
-
static ZERO;
|
|
122
|
-
X;
|
|
123
|
-
Y;
|
|
124
|
-
Z;
|
|
125
|
-
T;
|
|
126
|
-
constructor(X, Y, Z, T) {
|
|
127
|
-
const max = B256;
|
|
128
|
-
this.X = assertRange(X, 0n, max);
|
|
129
|
-
this.Y = assertRange(Y, 0n, max);
|
|
130
|
-
this.Z = assertRange(Z, 1n, max);
|
|
131
|
-
this.T = assertRange(T, 0n, max);
|
|
132
|
-
Object.freeze(this);
|
|
133
|
-
}
|
|
134
|
-
static CURVE() {
|
|
135
|
-
return ed25519_CURVE;
|
|
136
|
-
}
|
|
137
|
-
static fromAffine(p) {
|
|
138
|
-
return new _Point(p.x, p.y, 1n, modP(p.x * p.y));
|
|
139
|
-
}
|
|
140
|
-
/** RFC8032 5.1.3: Uint8Array to Point. */
|
|
141
|
-
static fromBytes(hex, zip215 = false) {
|
|
142
|
-
const d = _d;
|
|
143
|
-
const normed = u8fr(abytes(hex, L));
|
|
144
|
-
const lastByte = hex[31];
|
|
145
|
-
normed[31] = lastByte & ~128;
|
|
146
|
-
const y = bytesToNumberLE(normed);
|
|
147
|
-
const max = zip215 ? B256 : P;
|
|
148
|
-
assertRange(y, 0n, max);
|
|
149
|
-
const y2 = modP(y * y);
|
|
150
|
-
const u = M(y2 - 1n);
|
|
151
|
-
const v = modP(d * y2 + 1n);
|
|
152
|
-
let { isValid, value: x } = uvRatio(u, v);
|
|
153
|
-
if (!isValid)
|
|
154
|
-
err("bad point: y not sqrt");
|
|
155
|
-
const isXOdd = (x & 1n) === 1n;
|
|
156
|
-
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
157
|
-
if (!zip215 && x === 0n && isLastByteOdd)
|
|
158
|
-
err("bad point: x==0, isLastByteOdd");
|
|
159
|
-
if (isLastByteOdd !== isXOdd)
|
|
160
|
-
x = M(-x);
|
|
161
|
-
return new _Point(x, y, 1n, modP(x * y));
|
|
162
|
-
}
|
|
163
|
-
static fromHex(hex, zip215) {
|
|
164
|
-
return _Point.fromBytes(hexToBytes(hex), zip215);
|
|
165
|
-
}
|
|
166
|
-
get x() {
|
|
167
|
-
return this.toAffine().x;
|
|
168
|
-
}
|
|
169
|
-
get y() {
|
|
170
|
-
return this.toAffine().y;
|
|
171
|
-
}
|
|
172
|
-
/** Checks if the point is valid and on-curve. */
|
|
173
|
-
assertValidity() {
|
|
174
|
-
const a = _a;
|
|
175
|
-
const d = _d;
|
|
176
|
-
const p = this;
|
|
177
|
-
if (p.is0())
|
|
178
|
-
return err("bad point: ZERO");
|
|
179
|
-
const { X, Y, Z, T } = p;
|
|
180
|
-
const X2 = modP(X * X);
|
|
181
|
-
const Y2 = modP(Y * Y);
|
|
182
|
-
const Z2 = modP(Z * Z);
|
|
183
|
-
const Z4 = modP(Z2 * Z2);
|
|
184
|
-
const aX2 = modP(X2 * a);
|
|
185
|
-
const left = modP(Z2 * (aX2 + Y2));
|
|
186
|
-
const right = M(Z4 + modP(d * modP(X2 * Y2)));
|
|
187
|
-
if (left !== right)
|
|
188
|
-
return err("bad point: equation left != right (1)");
|
|
189
|
-
const XY = modP(X * Y);
|
|
190
|
-
const ZT = modP(Z * T);
|
|
191
|
-
if (XY !== ZT)
|
|
192
|
-
return err("bad point: equation left != right (2)");
|
|
193
|
-
return this;
|
|
194
|
-
}
|
|
195
|
-
/** Equality check: compare points P&Q. */
|
|
196
|
-
equals(other) {
|
|
197
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
198
|
-
const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
|
|
199
|
-
const X1Z2 = modP(X1 * Z2);
|
|
200
|
-
const X2Z1 = modP(X2 * Z1);
|
|
201
|
-
const Y1Z2 = modP(Y1 * Z2);
|
|
202
|
-
const Y2Z1 = modP(Y2 * Z1);
|
|
203
|
-
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
204
|
-
}
|
|
205
|
-
is0() {
|
|
206
|
-
return this.equals(I);
|
|
207
|
-
}
|
|
208
|
-
/** Flip point over y coordinate. */
|
|
209
|
-
negate() {
|
|
210
|
-
return new _Point(M(-this.X), this.Y, this.Z, M(-this.T));
|
|
211
|
-
}
|
|
212
|
-
/** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
|
|
213
|
-
double() {
|
|
214
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
215
|
-
const a = _a;
|
|
216
|
-
const A = modP(X1 * X1);
|
|
217
|
-
const B = modP(Y1 * Y1);
|
|
218
|
-
const C2 = modP(2n * Z1 * Z1);
|
|
219
|
-
const D = modP(a * A);
|
|
220
|
-
const x1y1 = M(X1 + Y1);
|
|
221
|
-
const E = M(modP(x1y1 * x1y1) - A - B);
|
|
222
|
-
const G2 = M(D + B);
|
|
223
|
-
const F = M(G2 - C2);
|
|
224
|
-
const H = M(D - B);
|
|
225
|
-
const X3 = modP(E * F);
|
|
226
|
-
const Y3 = modP(G2 * H);
|
|
227
|
-
const T3 = modP(E * H);
|
|
228
|
-
const Z3 = modP(F * G2);
|
|
229
|
-
return new _Point(X3, Y3, Z3, T3);
|
|
230
|
-
}
|
|
231
|
-
/** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
|
|
232
|
-
add(other) {
|
|
233
|
-
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
234
|
-
const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
|
|
235
|
-
const a = _a;
|
|
236
|
-
const d = _d;
|
|
237
|
-
const A = modP(X1 * X2);
|
|
238
|
-
const B = modP(Y1 * Y2);
|
|
239
|
-
const C2 = modP(modP(T1 * d) * T2);
|
|
240
|
-
const D = modP(Z1 * Z2);
|
|
241
|
-
const E = M(modP(M(X1 + Y1) * M(X2 + Y2)) - A - B);
|
|
242
|
-
const F = M(D - C2);
|
|
243
|
-
const G2 = M(D + C2);
|
|
244
|
-
const H = M(B - modP(a * A));
|
|
245
|
-
const X3 = modP(E * F);
|
|
246
|
-
const Y3 = modP(G2 * H);
|
|
247
|
-
const T3 = modP(E * H);
|
|
248
|
-
const Z3 = modP(F * G2);
|
|
249
|
-
return new _Point(X3, Y3, Z3, T3);
|
|
250
|
-
}
|
|
251
|
-
subtract(other) {
|
|
252
|
-
return this.add(apoint(other).negate());
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
|
|
256
|
-
* Uses {@link wNAF} for base point.
|
|
257
|
-
* Uses fake point to mitigate side-channel leakage.
|
|
258
|
-
* @param n scalar by which point is multiplied
|
|
259
|
-
* @param safe safe mode guards against timing attacks; unsafe mode is faster
|
|
260
|
-
*/
|
|
261
|
-
multiply(n, safe = true) {
|
|
262
|
-
if (!safe && (n === 0n || this.is0()))
|
|
263
|
-
return I;
|
|
264
|
-
assertRange(n, 1n, N);
|
|
265
|
-
if (n === 1n)
|
|
266
|
-
return this;
|
|
267
|
-
if (this.equals(G))
|
|
268
|
-
return wNAF(n).p;
|
|
269
|
-
let p = I;
|
|
270
|
-
let f = G;
|
|
271
|
-
for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
|
|
272
|
-
if (n & 1n)
|
|
273
|
-
p = p.add(d);
|
|
274
|
-
else if (safe)
|
|
275
|
-
f = f.add(d);
|
|
276
|
-
}
|
|
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))
|
|
286
|
-
return { x: 0n, y: 1n };
|
|
287
|
-
const iz = invert(Z, P);
|
|
288
|
-
if (modP(Z * iz) !== 1n)
|
|
289
|
-
err("invalid inverse");
|
|
290
|
-
const x = modP(X * iz);
|
|
291
|
-
const y = modP(Y * iz);
|
|
292
|
-
return { x, y };
|
|
293
|
-
}
|
|
294
|
-
toBytes() {
|
|
295
|
-
const { x, y } = this.toAffine();
|
|
296
|
-
const b = numTo32bLE(y);
|
|
297
|
-
b[31] |= x & 1n ? 128 : 0;
|
|
298
|
-
return b;
|
|
299
|
-
}
|
|
300
|
-
toHex() {
|
|
301
|
-
return bytesToHex(this.toBytes());
|
|
302
|
-
}
|
|
303
|
-
clearCofactor() {
|
|
304
|
-
return this.multiply(big(h), false);
|
|
305
|
-
}
|
|
306
|
-
isSmallOrder() {
|
|
307
|
-
return this.clearCofactor().is0();
|
|
308
|
-
}
|
|
309
|
-
isTorsionFree() {
|
|
310
|
-
let p = this.multiply(N / 2n, false).double();
|
|
311
|
-
if (N % 2n)
|
|
312
|
-
p = p.add(this);
|
|
313
|
-
return p.is0();
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
|
|
317
|
-
var I = new Point(0n, 1n, 1n, 0n);
|
|
318
|
-
Point.BASE = G;
|
|
319
|
-
Point.ZERO = I;
|
|
320
|
-
var numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), 64)).reverse();
|
|
321
|
-
var bytesToNumberLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
|
|
322
|
-
var pow2 = (x, power) => {
|
|
323
|
-
let r = x;
|
|
324
|
-
while (power-- > 0n) {
|
|
325
|
-
r = modP(r * r);
|
|
326
|
-
}
|
|
327
|
-
return r;
|
|
328
|
-
};
|
|
329
|
-
var pow_2_252_3 = (x) => {
|
|
330
|
-
const x2 = modP(x * x);
|
|
331
|
-
const b2 = modP(x2 * x);
|
|
332
|
-
const b4 = modP(pow2(b2, 2n) * b2);
|
|
333
|
-
const b5 = modP(pow2(b4, 1n) * x);
|
|
334
|
-
const b10 = modP(pow2(b5, 5n) * b5);
|
|
335
|
-
const b20 = modP(pow2(b10, 10n) * b10);
|
|
336
|
-
const b40 = modP(pow2(b20, 20n) * b20);
|
|
337
|
-
const b80 = modP(pow2(b40, 40n) * b40);
|
|
338
|
-
const b160 = modP(pow2(b80, 80n) * b80);
|
|
339
|
-
const b240 = modP(pow2(b160, 80n) * b80);
|
|
340
|
-
const b250 = modP(pow2(b240, 10n) * b10);
|
|
341
|
-
const pow_p_5_8 = modP(pow2(b250, 2n) * x);
|
|
342
|
-
return { pow_p_5_8, b2 };
|
|
343
|
-
};
|
|
344
|
-
var RM1 = 0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0n;
|
|
345
|
-
var uvRatio = (u, v) => {
|
|
346
|
-
const v3 = modP(v * modP(v * v));
|
|
347
|
-
const v7 = modP(modP(v3 * v3) * v);
|
|
348
|
-
const pow3 = pow_2_252_3(modP(u * v7)).pow_p_5_8;
|
|
349
|
-
let x = modP(u * modP(v3 * pow3));
|
|
350
|
-
const vx2 = modP(v * modP(x * x));
|
|
351
|
-
const root1 = x;
|
|
352
|
-
const root2 = modP(x * RM1);
|
|
353
|
-
const useRoot1 = vx2 === u;
|
|
354
|
-
const useRoot2 = vx2 === M(-u);
|
|
355
|
-
const noRoot = vx2 === M(-u * RM1);
|
|
356
|
-
if (useRoot1)
|
|
357
|
-
x = root1;
|
|
358
|
-
if (useRoot2 || noRoot)
|
|
359
|
-
x = root2;
|
|
360
|
-
if ((M(x) & 1n) === 1n)
|
|
361
|
-
x = M(-x);
|
|
362
|
-
return { isValid: useRoot1 || useRoot2, value: x };
|
|
363
|
-
};
|
|
364
|
-
var modL_LE = (hash) => modN(bytesToNumberLE(hash));
|
|
365
|
-
var sha512a = (...m) => hashes.sha512Async(concatBytes(...m));
|
|
366
|
-
var hash2extK = (hashed) => {
|
|
367
|
-
const head = hashed.slice(0, 32);
|
|
368
|
-
head[0] &= 248;
|
|
369
|
-
head[31] &= 127;
|
|
370
|
-
head[31] |= 64;
|
|
371
|
-
const prefix = hashed.slice(32, 64);
|
|
372
|
-
const scalar = modL_LE(head);
|
|
373
|
-
const point = G.multiply(scalar);
|
|
374
|
-
const pointBytes = point.toBytes();
|
|
375
|
-
return { head, prefix, scalar, point, pointBytes };
|
|
376
|
-
};
|
|
377
|
-
var getExtendedPublicKeyAsync = (secretKey) => sha512a(abytes(secretKey, L)).then(hash2extK);
|
|
378
|
-
var getPublicKeyAsync = (secretKey) => getExtendedPublicKeyAsync(secretKey).then((p) => p.pointBytes);
|
|
379
|
-
var hashFinishA = (res) => sha512a(res.hashable).then(res.finish);
|
|
380
|
-
var _sign = (e, rBytes, msg) => {
|
|
381
|
-
const { pointBytes: P2, scalar: s } = e;
|
|
382
|
-
const r = modL_LE(rBytes);
|
|
383
|
-
const R = G.multiply(r).toBytes();
|
|
384
|
-
const hashable = concatBytes(R, P2, msg);
|
|
385
|
-
const finish = (hashed) => {
|
|
386
|
-
const S = modN(r + modL_LE(hashed) * s);
|
|
387
|
-
return abytes(concatBytes(R, numTo32bLE(S)), 64);
|
|
388
|
-
};
|
|
389
|
-
return { hashable, finish };
|
|
390
|
-
};
|
|
391
|
-
var signAsync = async (message, secretKey) => {
|
|
392
|
-
const m = abytes(message);
|
|
393
|
-
const e = await getExtendedPublicKeyAsync(secretKey);
|
|
394
|
-
const rBytes = await sha512a(e.prefix, m);
|
|
395
|
-
return hashFinishA(_sign(e, rBytes, m));
|
|
396
|
-
};
|
|
397
|
-
var defaultVerifyOpts = { zip215: true };
|
|
398
|
-
var _verify = (sig, msg, publicKey, options = defaultVerifyOpts) => {
|
|
399
|
-
sig = abytes(sig, 64);
|
|
400
|
-
msg = abytes(msg);
|
|
401
|
-
publicKey = abytes(publicKey, L);
|
|
402
|
-
const { zip215 } = options;
|
|
403
|
-
const r = sig.subarray(0, L);
|
|
404
|
-
const s = bytesToNumberLE(sig.subarray(L, L * 2));
|
|
405
|
-
let A, R, SB;
|
|
406
|
-
let hashable = Uint8Array.of();
|
|
407
|
-
let finished = false;
|
|
408
|
-
try {
|
|
409
|
-
A = Point.fromBytes(publicKey, zip215);
|
|
410
|
-
R = Point.fromBytes(r, zip215);
|
|
411
|
-
SB = G.multiply(s, false);
|
|
412
|
-
hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
|
|
413
|
-
finished = true;
|
|
414
|
-
} catch (error) {
|
|
415
|
-
}
|
|
416
|
-
const finish = (hashed) => {
|
|
417
|
-
if (!finished)
|
|
418
|
-
return false;
|
|
419
|
-
if (!zip215 && A.isSmallOrder())
|
|
420
|
-
return false;
|
|
421
|
-
const k = modL_LE(hashed);
|
|
422
|
-
const RkA = R.add(A.multiply(k, false));
|
|
423
|
-
return RkA.subtract(SB).clearCofactor().is0();
|
|
424
|
-
};
|
|
425
|
-
return { hashable, finish };
|
|
426
|
-
};
|
|
427
|
-
var verifyAsync = async (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishA(_verify(signature, message, publicKey, opts));
|
|
428
|
-
var hashes = {
|
|
429
|
-
sha512Async: async (message) => {
|
|
430
|
-
const s = subtle();
|
|
431
|
-
const m = concatBytes(message);
|
|
432
|
-
return u8n(await s.digest("SHA-512", m.buffer));
|
|
433
|
-
},
|
|
434
|
-
sha512: void 0
|
|
435
|
-
};
|
|
436
|
-
var randomSecretKey = (seed = randomBytes(L)) => seed;
|
|
437
|
-
var keygenAsync = async (seed) => {
|
|
438
|
-
const secretKey = randomSecretKey(seed);
|
|
439
|
-
const publicKey = await getPublicKeyAsync(secretKey);
|
|
440
|
-
return { secretKey, publicKey };
|
|
441
|
-
};
|
|
442
|
-
var W = 8;
|
|
443
|
-
var scalarBits = 256;
|
|
444
|
-
var pwindows = Math.ceil(scalarBits / W) + 1;
|
|
445
|
-
var pwindowSize = 2 ** (W - 1);
|
|
446
|
-
var precompute = () => {
|
|
447
|
-
const points = [];
|
|
448
|
-
let p = G;
|
|
449
|
-
let b = p;
|
|
450
|
-
for (let w = 0; w < pwindows; w++) {
|
|
451
|
-
b = p;
|
|
452
|
-
points.push(b);
|
|
453
|
-
for (let i = 1; i < pwindowSize; i++) {
|
|
454
|
-
b = b.add(p);
|
|
455
|
-
points.push(b);
|
|
456
|
-
}
|
|
457
|
-
p = b.double();
|
|
458
|
-
}
|
|
459
|
-
return points;
|
|
460
|
-
};
|
|
461
|
-
var Gpows = void 0;
|
|
462
|
-
var ctneg = (cnd, p) => {
|
|
463
|
-
const n = p.negate();
|
|
464
|
-
return cnd ? n : p;
|
|
465
|
-
};
|
|
466
|
-
var wNAF = (n) => {
|
|
467
|
-
const comp = Gpows || (Gpows = precompute());
|
|
468
|
-
let p = I;
|
|
469
|
-
let f = G;
|
|
470
|
-
const pow_2_w = 2 ** W;
|
|
471
|
-
const maxNum = pow_2_w;
|
|
472
|
-
const mask = big(pow_2_w - 1);
|
|
473
|
-
const shiftBy = big(W);
|
|
474
|
-
for (let w = 0; w < pwindows; w++) {
|
|
475
|
-
let wbits = Number(n & mask);
|
|
476
|
-
n >>= shiftBy;
|
|
477
|
-
if (wbits > pwindowSize) {
|
|
478
|
-
wbits -= maxNum;
|
|
479
|
-
n += 1n;
|
|
480
|
-
}
|
|
481
|
-
const off = w * pwindowSize;
|
|
482
|
-
const offF = off;
|
|
483
|
-
const offP = off + Math.abs(wbits) - 1;
|
|
484
|
-
const isEven = w % 2 !== 0;
|
|
485
|
-
const isNeg = wbits < 0;
|
|
486
|
-
if (wbits === 0) {
|
|
487
|
-
f = f.add(ctneg(isEven, comp[offF]));
|
|
488
|
-
} else {
|
|
489
|
-
p = p.add(ctneg(isNeg, comp[offP]));
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
if (n !== 0n)
|
|
493
|
-
err("invalid wnaf");
|
|
494
|
-
return { p, f };
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/_assert.js
|
|
498
|
-
function isBytes2(a) {
|
|
499
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
500
|
-
}
|
|
501
|
-
function abytes2(b, ...lengths) {
|
|
502
|
-
if (!isBytes2(b))
|
|
503
|
-
throw new Error("Uint8Array expected");
|
|
504
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
505
|
-
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
506
|
-
}
|
|
507
|
-
function aexists(instance, checkFinished = true) {
|
|
508
|
-
if (instance.destroyed)
|
|
509
|
-
throw new Error("Hash instance has been destroyed");
|
|
510
|
-
if (checkFinished && instance.finished)
|
|
511
|
-
throw new Error("Hash#digest() has already been called");
|
|
512
|
-
}
|
|
513
|
-
function aoutput(out, instance) {
|
|
514
|
-
abytes2(out);
|
|
515
|
-
const min = instance.outputLen;
|
|
516
|
-
if (out.length < min) {
|
|
517
|
-
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/utils.js
|
|
522
|
-
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
523
|
-
var rotr = (word, shift) => word << 32 - shift | word >>> shift;
|
|
524
|
-
function utf8ToBytes(str) {
|
|
525
|
-
if (typeof str !== "string")
|
|
526
|
-
throw new Error("utf8ToBytes expected string, got " + typeof str);
|
|
527
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
528
|
-
}
|
|
529
|
-
function toBytes(data) {
|
|
530
|
-
if (typeof data === "string")
|
|
531
|
-
data = utf8ToBytes(data);
|
|
532
|
-
abytes2(data);
|
|
533
|
-
return data;
|
|
534
|
-
}
|
|
535
|
-
var Hash = class {
|
|
536
|
-
// Safe version that clones internal state
|
|
537
|
-
clone() {
|
|
538
|
-
return this._cloneInto();
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
function wrapConstructor(hashCons) {
|
|
542
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
543
|
-
const tmp = hashCons();
|
|
544
|
-
hashC.outputLen = tmp.outputLen;
|
|
545
|
-
hashC.blockLen = tmp.blockLen;
|
|
546
|
-
hashC.create = () => hashCons();
|
|
547
|
-
return hashC;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/_md.js
|
|
551
|
-
function setBigUint64(view, byteOffset, value, isLE) {
|
|
552
|
-
if (typeof view.setBigUint64 === "function")
|
|
553
|
-
return view.setBigUint64(byteOffset, value, isLE);
|
|
554
|
-
const _32n2 = BigInt(32);
|
|
555
|
-
const _u32_max = BigInt(4294967295);
|
|
556
|
-
const wh = Number(value >> _32n2 & _u32_max);
|
|
557
|
-
const wl = Number(value & _u32_max);
|
|
558
|
-
const h2 = isLE ? 4 : 0;
|
|
559
|
-
const l = isLE ? 0 : 4;
|
|
560
|
-
view.setUint32(byteOffset + h2, wh, isLE);
|
|
561
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
|
562
|
-
}
|
|
563
|
-
var Chi = (a, b, c) => a & b ^ ~a & c;
|
|
564
|
-
var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
565
|
-
var HashMD = class extends Hash {
|
|
566
|
-
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
567
|
-
super();
|
|
568
|
-
this.blockLen = blockLen;
|
|
569
|
-
this.outputLen = outputLen;
|
|
570
|
-
this.padOffset = padOffset;
|
|
571
|
-
this.isLE = isLE;
|
|
572
|
-
this.finished = false;
|
|
573
|
-
this.length = 0;
|
|
574
|
-
this.pos = 0;
|
|
575
|
-
this.destroyed = false;
|
|
576
|
-
this.buffer = new Uint8Array(blockLen);
|
|
577
|
-
this.view = createView(this.buffer);
|
|
578
|
-
}
|
|
579
|
-
update(data) {
|
|
580
|
-
aexists(this);
|
|
581
|
-
const { view, buffer, blockLen } = this;
|
|
582
|
-
data = toBytes(data);
|
|
583
|
-
const len = data.length;
|
|
584
|
-
for (let pos = 0; pos < len; ) {
|
|
585
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
586
|
-
if (take === blockLen) {
|
|
587
|
-
const dataView = createView(data);
|
|
588
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
589
|
-
this.process(dataView, pos);
|
|
590
|
-
continue;
|
|
591
|
-
}
|
|
592
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
593
|
-
this.pos += take;
|
|
594
|
-
pos += take;
|
|
595
|
-
if (this.pos === blockLen) {
|
|
596
|
-
this.process(view, 0);
|
|
597
|
-
this.pos = 0;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
this.length += data.length;
|
|
601
|
-
this.roundClean();
|
|
602
|
-
return this;
|
|
603
|
-
}
|
|
604
|
-
digestInto(out) {
|
|
605
|
-
aexists(this);
|
|
606
|
-
aoutput(out, this);
|
|
607
|
-
this.finished = true;
|
|
608
|
-
const { buffer, view, blockLen, isLE } = this;
|
|
609
|
-
let { pos } = this;
|
|
610
|
-
buffer[pos++] = 128;
|
|
611
|
-
this.buffer.subarray(pos).fill(0);
|
|
612
|
-
if (this.padOffset > blockLen - pos) {
|
|
613
|
-
this.process(view, 0);
|
|
614
|
-
pos = 0;
|
|
615
|
-
}
|
|
616
|
-
for (let i = pos; i < blockLen; i++)
|
|
617
|
-
buffer[i] = 0;
|
|
618
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
619
|
-
this.process(view, 0);
|
|
620
|
-
const oview = createView(out);
|
|
621
|
-
const len = this.outputLen;
|
|
622
|
-
if (len % 4)
|
|
623
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
624
|
-
const outLen = len / 4;
|
|
625
|
-
const state = this.get();
|
|
626
|
-
if (outLen > state.length)
|
|
627
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
628
|
-
for (let i = 0; i < outLen; i++)
|
|
629
|
-
oview.setUint32(4 * i, state[i], isLE);
|
|
630
|
-
}
|
|
631
|
-
digest() {
|
|
632
|
-
const { buffer, outputLen } = this;
|
|
633
|
-
this.digestInto(buffer);
|
|
634
|
-
const res = buffer.slice(0, outputLen);
|
|
635
|
-
this.destroy();
|
|
636
|
-
return res;
|
|
637
|
-
}
|
|
638
|
-
_cloneInto(to) {
|
|
639
|
-
to || (to = new this.constructor());
|
|
640
|
-
to.set(...this.get());
|
|
641
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
642
|
-
to.length = length;
|
|
643
|
-
to.pos = pos;
|
|
644
|
-
to.finished = finished;
|
|
645
|
-
to.destroyed = destroyed;
|
|
646
|
-
if (length % blockLen)
|
|
647
|
-
to.buffer.set(buffer);
|
|
648
|
-
return to;
|
|
649
|
-
}
|
|
650
|
-
};
|
|
651
|
-
|
|
652
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/_u64.js
|
|
653
|
-
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
654
|
-
var _32n = /* @__PURE__ */ BigInt(32);
|
|
655
|
-
function fromBig(n, le = false) {
|
|
656
|
-
if (le)
|
|
657
|
-
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
658
|
-
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
659
|
-
}
|
|
660
|
-
function split(lst, le = false) {
|
|
661
|
-
let Ah = new Uint32Array(lst.length);
|
|
662
|
-
let Al = new Uint32Array(lst.length);
|
|
663
|
-
for (let i = 0; i < lst.length; i++) {
|
|
664
|
-
const { h: h2, l } = fromBig(lst[i], le);
|
|
665
|
-
[Ah[i], Al[i]] = [h2, l];
|
|
666
|
-
}
|
|
667
|
-
return [Ah, Al];
|
|
668
|
-
}
|
|
669
|
-
var toBig = (h2, l) => BigInt(h2 >>> 0) << _32n | BigInt(l >>> 0);
|
|
670
|
-
var shrSH = (h2, _l, s) => h2 >>> s;
|
|
671
|
-
var shrSL = (h2, l, s) => h2 << 32 - s | l >>> s;
|
|
672
|
-
var rotrSH = (h2, l, s) => h2 >>> s | l << 32 - s;
|
|
673
|
-
var rotrSL = (h2, l, s) => h2 << 32 - s | l >>> s;
|
|
674
|
-
var rotrBH = (h2, l, s) => h2 << 64 - s | l >>> s - 32;
|
|
675
|
-
var rotrBL = (h2, l, s) => h2 >>> s - 32 | l << 64 - s;
|
|
676
|
-
var rotr32H = (_h, l) => l;
|
|
677
|
-
var rotr32L = (h2, _l) => h2;
|
|
678
|
-
var rotlSH = (h2, l, s) => h2 << s | l >>> 32 - s;
|
|
679
|
-
var rotlSL = (h2, l, s) => l << s | h2 >>> 32 - s;
|
|
680
|
-
var rotlBH = (h2, l, s) => l << s - 32 | h2 >>> 64 - s;
|
|
681
|
-
var rotlBL = (h2, l, s) => h2 << s - 32 | l >>> 64 - s;
|
|
682
|
-
function add(Ah, Al, Bh, Bl) {
|
|
683
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
684
|
-
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
685
|
-
}
|
|
686
|
-
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
687
|
-
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
688
|
-
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
689
|
-
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
690
|
-
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
691
|
-
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
692
|
-
var u64 = {
|
|
693
|
-
fromBig,
|
|
694
|
-
split,
|
|
695
|
-
toBig,
|
|
696
|
-
shrSH,
|
|
697
|
-
shrSL,
|
|
698
|
-
rotrSH,
|
|
699
|
-
rotrSL,
|
|
700
|
-
rotrBH,
|
|
701
|
-
rotrBL,
|
|
702
|
-
rotr32H,
|
|
703
|
-
rotr32L,
|
|
704
|
-
rotlSH,
|
|
705
|
-
rotlSL,
|
|
706
|
-
rotlBH,
|
|
707
|
-
rotlBL,
|
|
708
|
-
add,
|
|
709
|
-
add3L,
|
|
710
|
-
add3H,
|
|
711
|
-
add4L,
|
|
712
|
-
add4H,
|
|
713
|
-
add5H,
|
|
714
|
-
add5L
|
|
715
|
-
};
|
|
716
|
-
var u64_default = u64;
|
|
717
|
-
|
|
718
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/sha512.js
|
|
719
|
-
var [SHA512_Kh, SHA512_Kl] = /* @__PURE__ */ (() => u64_default.split([
|
|
720
|
-
"0x428a2f98d728ae22",
|
|
721
|
-
"0x7137449123ef65cd",
|
|
722
|
-
"0xb5c0fbcfec4d3b2f",
|
|
723
|
-
"0xe9b5dba58189dbbc",
|
|
724
|
-
"0x3956c25bf348b538",
|
|
725
|
-
"0x59f111f1b605d019",
|
|
726
|
-
"0x923f82a4af194f9b",
|
|
727
|
-
"0xab1c5ed5da6d8118",
|
|
728
|
-
"0xd807aa98a3030242",
|
|
729
|
-
"0x12835b0145706fbe",
|
|
730
|
-
"0x243185be4ee4b28c",
|
|
731
|
-
"0x550c7dc3d5ffb4e2",
|
|
732
|
-
"0x72be5d74f27b896f",
|
|
733
|
-
"0x80deb1fe3b1696b1",
|
|
734
|
-
"0x9bdc06a725c71235",
|
|
735
|
-
"0xc19bf174cf692694",
|
|
736
|
-
"0xe49b69c19ef14ad2",
|
|
737
|
-
"0xefbe4786384f25e3",
|
|
738
|
-
"0x0fc19dc68b8cd5b5",
|
|
739
|
-
"0x240ca1cc77ac9c65",
|
|
740
|
-
"0x2de92c6f592b0275",
|
|
741
|
-
"0x4a7484aa6ea6e483",
|
|
742
|
-
"0x5cb0a9dcbd41fbd4",
|
|
743
|
-
"0x76f988da831153b5",
|
|
744
|
-
"0x983e5152ee66dfab",
|
|
745
|
-
"0xa831c66d2db43210",
|
|
746
|
-
"0xb00327c898fb213f",
|
|
747
|
-
"0xbf597fc7beef0ee4",
|
|
748
|
-
"0xc6e00bf33da88fc2",
|
|
749
|
-
"0xd5a79147930aa725",
|
|
750
|
-
"0x06ca6351e003826f",
|
|
751
|
-
"0x142929670a0e6e70",
|
|
752
|
-
"0x27b70a8546d22ffc",
|
|
753
|
-
"0x2e1b21385c26c926",
|
|
754
|
-
"0x4d2c6dfc5ac42aed",
|
|
755
|
-
"0x53380d139d95b3df",
|
|
756
|
-
"0x650a73548baf63de",
|
|
757
|
-
"0x766a0abb3c77b2a8",
|
|
758
|
-
"0x81c2c92e47edaee6",
|
|
759
|
-
"0x92722c851482353b",
|
|
760
|
-
"0xa2bfe8a14cf10364",
|
|
761
|
-
"0xa81a664bbc423001",
|
|
762
|
-
"0xc24b8b70d0f89791",
|
|
763
|
-
"0xc76c51a30654be30",
|
|
764
|
-
"0xd192e819d6ef5218",
|
|
765
|
-
"0xd69906245565a910",
|
|
766
|
-
"0xf40e35855771202a",
|
|
767
|
-
"0x106aa07032bbd1b8",
|
|
768
|
-
"0x19a4c116b8d2d0c8",
|
|
769
|
-
"0x1e376c085141ab53",
|
|
770
|
-
"0x2748774cdf8eeb99",
|
|
771
|
-
"0x34b0bcb5e19b48a8",
|
|
772
|
-
"0x391c0cb3c5c95a63",
|
|
773
|
-
"0x4ed8aa4ae3418acb",
|
|
774
|
-
"0x5b9cca4f7763e373",
|
|
775
|
-
"0x682e6ff3d6b2b8a3",
|
|
776
|
-
"0x748f82ee5defb2fc",
|
|
777
|
-
"0x78a5636f43172f60",
|
|
778
|
-
"0x84c87814a1f0ab72",
|
|
779
|
-
"0x8cc702081a6439ec",
|
|
780
|
-
"0x90befffa23631e28",
|
|
781
|
-
"0xa4506cebde82bde9",
|
|
782
|
-
"0xbef9a3f7b2c67915",
|
|
783
|
-
"0xc67178f2e372532b",
|
|
784
|
-
"0xca273eceea26619c",
|
|
785
|
-
"0xd186b8c721c0c207",
|
|
786
|
-
"0xeada7dd6cde0eb1e",
|
|
787
|
-
"0xf57d4f7fee6ed178",
|
|
788
|
-
"0x06f067aa72176fba",
|
|
789
|
-
"0x0a637dc5a2c898a6",
|
|
790
|
-
"0x113f9804bef90dae",
|
|
791
|
-
"0x1b710b35131c471b",
|
|
792
|
-
"0x28db77f523047d84",
|
|
793
|
-
"0x32caab7b40c72493",
|
|
794
|
-
"0x3c9ebe0a15c9bebc",
|
|
795
|
-
"0x431d67c49c100d4c",
|
|
796
|
-
"0x4cc5d4becb3e42b6",
|
|
797
|
-
"0x597f299cfc657e2a",
|
|
798
|
-
"0x5fcb6fab3ad6faec",
|
|
799
|
-
"0x6c44198c4a475817"
|
|
800
|
-
].map((n) => BigInt(n))))();
|
|
801
|
-
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
802
|
-
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
803
|
-
var SHA512 = class extends HashMD {
|
|
804
|
-
constructor() {
|
|
805
|
-
super(128, 64, 16, false);
|
|
806
|
-
this.Ah = 1779033703 | 0;
|
|
807
|
-
this.Al = 4089235720 | 0;
|
|
808
|
-
this.Bh = 3144134277 | 0;
|
|
809
|
-
this.Bl = 2227873595 | 0;
|
|
810
|
-
this.Ch = 1013904242 | 0;
|
|
811
|
-
this.Cl = 4271175723 | 0;
|
|
812
|
-
this.Dh = 2773480762 | 0;
|
|
813
|
-
this.Dl = 1595750129 | 0;
|
|
814
|
-
this.Eh = 1359893119 | 0;
|
|
815
|
-
this.El = 2917565137 | 0;
|
|
816
|
-
this.Fh = 2600822924 | 0;
|
|
817
|
-
this.Fl = 725511199 | 0;
|
|
818
|
-
this.Gh = 528734635 | 0;
|
|
819
|
-
this.Gl = 4215389547 | 0;
|
|
820
|
-
this.Hh = 1541459225 | 0;
|
|
821
|
-
this.Hl = 327033209 | 0;
|
|
822
|
-
}
|
|
823
|
-
// prettier-ignore
|
|
824
|
-
get() {
|
|
825
|
-
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
826
|
-
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
827
|
-
}
|
|
828
|
-
// prettier-ignore
|
|
829
|
-
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
830
|
-
this.Ah = Ah | 0;
|
|
831
|
-
this.Al = Al | 0;
|
|
832
|
-
this.Bh = Bh | 0;
|
|
833
|
-
this.Bl = Bl | 0;
|
|
834
|
-
this.Ch = Ch | 0;
|
|
835
|
-
this.Cl = Cl | 0;
|
|
836
|
-
this.Dh = Dh | 0;
|
|
837
|
-
this.Dl = Dl | 0;
|
|
838
|
-
this.Eh = Eh | 0;
|
|
839
|
-
this.El = El | 0;
|
|
840
|
-
this.Fh = Fh | 0;
|
|
841
|
-
this.Fl = Fl | 0;
|
|
842
|
-
this.Gh = Gh | 0;
|
|
843
|
-
this.Gl = Gl | 0;
|
|
844
|
-
this.Hh = Hh | 0;
|
|
845
|
-
this.Hl = Hl | 0;
|
|
846
|
-
}
|
|
847
|
-
process(view, offset) {
|
|
848
|
-
for (let i = 0; i < 16; i++, offset += 4) {
|
|
849
|
-
SHA512_W_H[i] = view.getUint32(offset);
|
|
850
|
-
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
851
|
-
}
|
|
852
|
-
for (let i = 16; i < 80; i++) {
|
|
853
|
-
const W15h = SHA512_W_H[i - 15] | 0;
|
|
854
|
-
const W15l = SHA512_W_L[i - 15] | 0;
|
|
855
|
-
const s0h = u64_default.rotrSH(W15h, W15l, 1) ^ u64_default.rotrSH(W15h, W15l, 8) ^ u64_default.shrSH(W15h, W15l, 7);
|
|
856
|
-
const s0l = u64_default.rotrSL(W15h, W15l, 1) ^ u64_default.rotrSL(W15h, W15l, 8) ^ u64_default.shrSL(W15h, W15l, 7);
|
|
857
|
-
const W2h = SHA512_W_H[i - 2] | 0;
|
|
858
|
-
const W2l = SHA512_W_L[i - 2] | 0;
|
|
859
|
-
const s1h = u64_default.rotrSH(W2h, W2l, 19) ^ u64_default.rotrBH(W2h, W2l, 61) ^ u64_default.shrSH(W2h, W2l, 6);
|
|
860
|
-
const s1l = u64_default.rotrSL(W2h, W2l, 19) ^ u64_default.rotrBL(W2h, W2l, 61) ^ u64_default.shrSL(W2h, W2l, 6);
|
|
861
|
-
const SUMl = u64_default.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
862
|
-
const SUMh = u64_default.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
863
|
-
SHA512_W_H[i] = SUMh | 0;
|
|
864
|
-
SHA512_W_L[i] = SUMl | 0;
|
|
865
|
-
}
|
|
866
|
-
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
867
|
-
for (let i = 0; i < 80; i++) {
|
|
868
|
-
const sigma1h = u64_default.rotrSH(Eh, El, 14) ^ u64_default.rotrSH(Eh, El, 18) ^ u64_default.rotrBH(Eh, El, 41);
|
|
869
|
-
const sigma1l = u64_default.rotrSL(Eh, El, 14) ^ u64_default.rotrSL(Eh, El, 18) ^ u64_default.rotrBL(Eh, El, 41);
|
|
870
|
-
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
871
|
-
const CHIl = El & Fl ^ ~El & Gl;
|
|
872
|
-
const T1ll = u64_default.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
873
|
-
const T1h = u64_default.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
874
|
-
const T1l = T1ll | 0;
|
|
875
|
-
const sigma0h = u64_default.rotrSH(Ah, Al, 28) ^ u64_default.rotrBH(Ah, Al, 34) ^ u64_default.rotrBH(Ah, Al, 39);
|
|
876
|
-
const sigma0l = u64_default.rotrSL(Ah, Al, 28) ^ u64_default.rotrBL(Ah, Al, 34) ^ u64_default.rotrBL(Ah, Al, 39);
|
|
877
|
-
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
878
|
-
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
879
|
-
Hh = Gh | 0;
|
|
880
|
-
Hl = Gl | 0;
|
|
881
|
-
Gh = Fh | 0;
|
|
882
|
-
Gl = Fl | 0;
|
|
883
|
-
Fh = Eh | 0;
|
|
884
|
-
Fl = El | 0;
|
|
885
|
-
({ h: Eh, l: El } = u64_default.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
886
|
-
Dh = Ch | 0;
|
|
887
|
-
Dl = Cl | 0;
|
|
888
|
-
Ch = Bh | 0;
|
|
889
|
-
Cl = Bl | 0;
|
|
890
|
-
Bh = Ah | 0;
|
|
891
|
-
Bl = Al | 0;
|
|
892
|
-
const All = u64_default.add3L(T1l, sigma0l, MAJl);
|
|
893
|
-
Ah = u64_default.add3H(All, T1h, sigma0h, MAJh);
|
|
894
|
-
Al = All | 0;
|
|
895
|
-
}
|
|
896
|
-
({ h: Ah, l: Al } = u64_default.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
897
|
-
({ h: Bh, l: Bl } = u64_default.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
898
|
-
({ h: Ch, l: Cl } = u64_default.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
899
|
-
({ h: Dh, l: Dl } = u64_default.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
900
|
-
({ h: Eh, l: El } = u64_default.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
901
|
-
({ h: Fh, l: Fl } = u64_default.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
902
|
-
({ h: Gh, l: Gl } = u64_default.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
903
|
-
({ h: Hh, l: Hl } = u64_default.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
904
|
-
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
905
|
-
}
|
|
906
|
-
roundClean() {
|
|
907
|
-
SHA512_W_H.fill(0);
|
|
908
|
-
SHA512_W_L.fill(0);
|
|
909
|
-
}
|
|
910
|
-
destroy() {
|
|
911
|
-
this.buffer.fill(0);
|
|
912
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
913
|
-
}
|
|
914
|
-
};
|
|
915
|
-
var sha512 = /* @__PURE__ */ wrapConstructor(() => new SHA512());
|
|
916
|
-
|
|
917
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.1/node_modules/@noble/hashes/esm/sha256.js
|
|
918
|
-
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
919
|
-
1116352408,
|
|
920
|
-
1899447441,
|
|
921
|
-
3049323471,
|
|
922
|
-
3921009573,
|
|
923
|
-
961987163,
|
|
924
|
-
1508970993,
|
|
925
|
-
2453635748,
|
|
926
|
-
2870763221,
|
|
927
|
-
3624381080,
|
|
928
|
-
310598401,
|
|
929
|
-
607225278,
|
|
930
|
-
1426881987,
|
|
931
|
-
1925078388,
|
|
932
|
-
2162078206,
|
|
933
|
-
2614888103,
|
|
934
|
-
3248222580,
|
|
935
|
-
3835390401,
|
|
936
|
-
4022224774,
|
|
937
|
-
264347078,
|
|
938
|
-
604807628,
|
|
939
|
-
770255983,
|
|
940
|
-
1249150122,
|
|
941
|
-
1555081692,
|
|
942
|
-
1996064986,
|
|
943
|
-
2554220882,
|
|
944
|
-
2821834349,
|
|
945
|
-
2952996808,
|
|
946
|
-
3210313671,
|
|
947
|
-
3336571891,
|
|
948
|
-
3584528711,
|
|
949
|
-
113926993,
|
|
950
|
-
338241895,
|
|
951
|
-
666307205,
|
|
952
|
-
773529912,
|
|
953
|
-
1294757372,
|
|
954
|
-
1396182291,
|
|
955
|
-
1695183700,
|
|
956
|
-
1986661051,
|
|
957
|
-
2177026350,
|
|
958
|
-
2456956037,
|
|
959
|
-
2730485921,
|
|
960
|
-
2820302411,
|
|
961
|
-
3259730800,
|
|
962
|
-
3345764771,
|
|
963
|
-
3516065817,
|
|
964
|
-
3600352804,
|
|
965
|
-
4094571909,
|
|
966
|
-
275423344,
|
|
967
|
-
430227734,
|
|
968
|
-
506948616,
|
|
969
|
-
659060556,
|
|
970
|
-
883997877,
|
|
971
|
-
958139571,
|
|
972
|
-
1322822218,
|
|
973
|
-
1537002063,
|
|
974
|
-
1747873779,
|
|
975
|
-
1955562222,
|
|
976
|
-
2024104815,
|
|
977
|
-
2227730452,
|
|
978
|
-
2361852424,
|
|
979
|
-
2428436474,
|
|
980
|
-
2756734187,
|
|
981
|
-
3204031479,
|
|
982
|
-
3329325298
|
|
983
|
-
]);
|
|
984
|
-
var SHA256_IV = /* @__PURE__ */ new Uint32Array([
|
|
985
|
-
1779033703,
|
|
986
|
-
3144134277,
|
|
987
|
-
1013904242,
|
|
988
|
-
2773480762,
|
|
989
|
-
1359893119,
|
|
990
|
-
2600822924,
|
|
991
|
-
528734635,
|
|
992
|
-
1541459225
|
|
993
|
-
]);
|
|
994
|
-
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
995
|
-
var SHA256 = class extends HashMD {
|
|
996
|
-
constructor() {
|
|
997
|
-
super(64, 32, 8, false);
|
|
998
|
-
this.A = SHA256_IV[0] | 0;
|
|
999
|
-
this.B = SHA256_IV[1] | 0;
|
|
1000
|
-
this.C = SHA256_IV[2] | 0;
|
|
1001
|
-
this.D = SHA256_IV[3] | 0;
|
|
1002
|
-
this.E = SHA256_IV[4] | 0;
|
|
1003
|
-
this.F = SHA256_IV[5] | 0;
|
|
1004
|
-
this.G = SHA256_IV[6] | 0;
|
|
1005
|
-
this.H = SHA256_IV[7] | 0;
|
|
1006
|
-
}
|
|
1007
|
-
get() {
|
|
1008
|
-
const { A, B, C: C2, D, E, F, G: G2, H } = this;
|
|
1009
|
-
return [A, B, C2, D, E, F, G2, H];
|
|
1010
|
-
}
|
|
1011
|
-
// prettier-ignore
|
|
1012
|
-
set(A, B, C2, D, E, F, G2, H) {
|
|
1013
|
-
this.A = A | 0;
|
|
1014
|
-
this.B = B | 0;
|
|
1015
|
-
this.C = C2 | 0;
|
|
1016
|
-
this.D = D | 0;
|
|
1017
|
-
this.E = E | 0;
|
|
1018
|
-
this.F = F | 0;
|
|
1019
|
-
this.G = G2 | 0;
|
|
1020
|
-
this.H = H | 0;
|
|
1021
|
-
}
|
|
1022
|
-
process(view, offset) {
|
|
1023
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
1024
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
1025
|
-
for (let i = 16; i < 64; i++) {
|
|
1026
|
-
const W15 = SHA256_W[i - 15];
|
|
1027
|
-
const W2 = SHA256_W[i - 2];
|
|
1028
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
1029
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
1030
|
-
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
1031
|
-
}
|
|
1032
|
-
let { A, B, C: C2, D, E, F, G: G2, H } = this;
|
|
1033
|
-
for (let i = 0; i < 64; i++) {
|
|
1034
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
1035
|
-
const T1 = H + sigma1 + Chi(E, F, G2) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
1036
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
1037
|
-
const T2 = sigma0 + Maj(A, B, C2) | 0;
|
|
1038
|
-
H = G2;
|
|
1039
|
-
G2 = F;
|
|
1040
|
-
F = E;
|
|
1041
|
-
E = D + T1 | 0;
|
|
1042
|
-
D = C2;
|
|
1043
|
-
C2 = B;
|
|
1044
|
-
B = A;
|
|
1045
|
-
A = T1 + T2 | 0;
|
|
1046
|
-
}
|
|
1047
|
-
A = A + this.A | 0;
|
|
1048
|
-
B = B + this.B | 0;
|
|
1049
|
-
C2 = C2 + this.C | 0;
|
|
1050
|
-
D = D + this.D | 0;
|
|
1051
|
-
E = E + this.E | 0;
|
|
1052
|
-
F = F + this.F | 0;
|
|
1053
|
-
G2 = G2 + this.G | 0;
|
|
1054
|
-
H = H + this.H | 0;
|
|
1055
|
-
this.set(A, B, C2, D, E, F, G2, H);
|
|
1056
|
-
}
|
|
1057
|
-
roundClean() {
|
|
1058
|
-
SHA256_W.fill(0);
|
|
1059
|
-
}
|
|
1060
|
-
destroy() {
|
|
1061
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1062
|
-
this.buffer.fill(0);
|
|
1063
|
-
}
|
|
1064
|
-
};
|
|
1065
|
-
var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
1066
|
-
|
|
1067
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_assert.js
|
|
1068
|
-
function anumber(n) {
|
|
1069
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
1070
|
-
throw new Error("positive integer expected, got " + n);
|
|
1071
|
-
}
|
|
1072
|
-
function isBytes3(a) {
|
|
1073
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
1074
|
-
}
|
|
1075
|
-
function abytes3(b, ...lengths) {
|
|
1076
|
-
if (!isBytes3(b))
|
|
1077
|
-
throw new Error("Uint8Array expected");
|
|
1078
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
1079
|
-
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
1080
|
-
}
|
|
1081
|
-
function ahash(h2) {
|
|
1082
|
-
if (typeof h2 !== "function" || typeof h2.create !== "function")
|
|
1083
|
-
throw new Error("Hash should be wrapped by utils.wrapConstructor");
|
|
1084
|
-
anumber(h2.outputLen);
|
|
1085
|
-
anumber(h2.blockLen);
|
|
1086
|
-
}
|
|
1087
|
-
function aexists2(instance, checkFinished = true) {
|
|
1088
|
-
if (instance.destroyed)
|
|
1089
|
-
throw new Error("Hash instance has been destroyed");
|
|
1090
|
-
if (checkFinished && instance.finished)
|
|
1091
|
-
throw new Error("Hash#digest() has already been called");
|
|
1092
|
-
}
|
|
1093
|
-
function aoutput2(out, instance) {
|
|
1094
|
-
abytes3(out);
|
|
1095
|
-
const min = instance.outputLen;
|
|
1096
|
-
if (out.length < min) {
|
|
1097
|
-
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/crypto.js
|
|
1102
|
-
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
1103
|
-
|
|
1104
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/utils.js
|
|
1105
|
-
var createView2 = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1106
|
-
var rotr2 = (word, shift) => word << 32 - shift | word >>> shift;
|
|
1107
|
-
function utf8ToBytes2(str) {
|
|
1108
|
-
if (typeof str !== "string")
|
|
1109
|
-
throw new Error("utf8ToBytes expected string, got " + typeof str);
|
|
1110
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
1111
|
-
}
|
|
1112
|
-
function toBytes2(data) {
|
|
1113
|
-
if (typeof data === "string")
|
|
1114
|
-
data = utf8ToBytes2(data);
|
|
1115
|
-
abytes3(data);
|
|
1116
|
-
return data;
|
|
1117
|
-
}
|
|
1118
|
-
function concatBytes2(...arrays) {
|
|
1119
|
-
let sum = 0;
|
|
1120
|
-
for (let i = 0; i < arrays.length; i++) {
|
|
1121
|
-
const a = arrays[i];
|
|
1122
|
-
abytes3(a);
|
|
1123
|
-
sum += a.length;
|
|
1124
|
-
}
|
|
1125
|
-
const res = new Uint8Array(sum);
|
|
1126
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
1127
|
-
const a = arrays[i];
|
|
1128
|
-
res.set(a, pad);
|
|
1129
|
-
pad += a.length;
|
|
1130
|
-
}
|
|
1131
|
-
return res;
|
|
1132
|
-
}
|
|
1133
|
-
var Hash2 = class {
|
|
1134
|
-
// Safe version that clones internal state
|
|
1135
|
-
clone() {
|
|
1136
|
-
return this._cloneInto();
|
|
1137
|
-
}
|
|
1138
|
-
};
|
|
1139
|
-
function wrapConstructor2(hashCons) {
|
|
1140
|
-
const hashC = (msg) => hashCons().update(toBytes2(msg)).digest();
|
|
1141
|
-
const tmp = hashCons();
|
|
1142
|
-
hashC.outputLen = tmp.outputLen;
|
|
1143
|
-
hashC.blockLen = tmp.blockLen;
|
|
1144
|
-
hashC.create = () => hashCons();
|
|
1145
|
-
return hashC;
|
|
1146
|
-
}
|
|
1147
|
-
function randomBytes2(bytesLength = 32) {
|
|
1148
|
-
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
1149
|
-
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
1150
|
-
}
|
|
1151
|
-
if (crypto && typeof crypto.randomBytes === "function") {
|
|
1152
|
-
return crypto.randomBytes(bytesLength);
|
|
1153
|
-
}
|
|
1154
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/_md.js
|
|
1158
|
-
function setBigUint642(view, byteOffset, value, isLE) {
|
|
1159
|
-
if (typeof view.setBigUint64 === "function")
|
|
1160
|
-
return view.setBigUint64(byteOffset, value, isLE);
|
|
1161
|
-
const _32n2 = BigInt(32);
|
|
1162
|
-
const _u32_max = BigInt(4294967295);
|
|
1163
|
-
const wh = Number(value >> _32n2 & _u32_max);
|
|
1164
|
-
const wl = Number(value & _u32_max);
|
|
1165
|
-
const h2 = isLE ? 4 : 0;
|
|
1166
|
-
const l = isLE ? 0 : 4;
|
|
1167
|
-
view.setUint32(byteOffset + h2, wh, isLE);
|
|
1168
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
|
1169
|
-
}
|
|
1170
|
-
var Chi2 = (a, b, c) => a & b ^ ~a & c;
|
|
1171
|
-
var Maj2 = (a, b, c) => a & b ^ a & c ^ b & c;
|
|
1172
|
-
var HashMD2 = class extends Hash2 {
|
|
1173
|
-
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
1174
|
-
super();
|
|
1175
|
-
this.blockLen = blockLen;
|
|
1176
|
-
this.outputLen = outputLen;
|
|
1177
|
-
this.padOffset = padOffset;
|
|
1178
|
-
this.isLE = isLE;
|
|
1179
|
-
this.finished = false;
|
|
1180
|
-
this.length = 0;
|
|
1181
|
-
this.pos = 0;
|
|
1182
|
-
this.destroyed = false;
|
|
1183
|
-
this.buffer = new Uint8Array(blockLen);
|
|
1184
|
-
this.view = createView2(this.buffer);
|
|
1185
|
-
}
|
|
1186
|
-
update(data) {
|
|
1187
|
-
aexists2(this);
|
|
1188
|
-
const { view, buffer, blockLen } = this;
|
|
1189
|
-
data = toBytes2(data);
|
|
1190
|
-
const len = data.length;
|
|
1191
|
-
for (let pos = 0; pos < len; ) {
|
|
1192
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1193
|
-
if (take === blockLen) {
|
|
1194
|
-
const dataView = createView2(data);
|
|
1195
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
1196
|
-
this.process(dataView, pos);
|
|
1197
|
-
continue;
|
|
1198
|
-
}
|
|
1199
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
1200
|
-
this.pos += take;
|
|
1201
|
-
pos += take;
|
|
1202
|
-
if (this.pos === blockLen) {
|
|
1203
|
-
this.process(view, 0);
|
|
1204
|
-
this.pos = 0;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
this.length += data.length;
|
|
1208
|
-
this.roundClean();
|
|
1209
|
-
return this;
|
|
1210
|
-
}
|
|
1211
|
-
digestInto(out) {
|
|
1212
|
-
aexists2(this);
|
|
1213
|
-
aoutput2(out, this);
|
|
1214
|
-
this.finished = true;
|
|
1215
|
-
const { buffer, view, blockLen, isLE } = this;
|
|
1216
|
-
let { pos } = this;
|
|
1217
|
-
buffer[pos++] = 128;
|
|
1218
|
-
this.buffer.subarray(pos).fill(0);
|
|
1219
|
-
if (this.padOffset > blockLen - pos) {
|
|
1220
|
-
this.process(view, 0);
|
|
1221
|
-
pos = 0;
|
|
1222
|
-
}
|
|
1223
|
-
for (let i = pos; i < blockLen; i++)
|
|
1224
|
-
buffer[i] = 0;
|
|
1225
|
-
setBigUint642(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
1226
|
-
this.process(view, 0);
|
|
1227
|
-
const oview = createView2(out);
|
|
1228
|
-
const len = this.outputLen;
|
|
1229
|
-
if (len % 4)
|
|
1230
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
1231
|
-
const outLen = len / 4;
|
|
1232
|
-
const state = this.get();
|
|
1233
|
-
if (outLen > state.length)
|
|
1234
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
1235
|
-
for (let i = 0; i < outLen; i++)
|
|
1236
|
-
oview.setUint32(4 * i, state[i], isLE);
|
|
1237
|
-
}
|
|
1238
|
-
digest() {
|
|
1239
|
-
const { buffer, outputLen } = this;
|
|
1240
|
-
this.digestInto(buffer);
|
|
1241
|
-
const res = buffer.slice(0, outputLen);
|
|
1242
|
-
this.destroy();
|
|
1243
|
-
return res;
|
|
1244
|
-
}
|
|
1245
|
-
_cloneInto(to) {
|
|
1246
|
-
to || (to = new this.constructor());
|
|
1247
|
-
to.set(...this.get());
|
|
1248
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
1249
|
-
to.length = length;
|
|
1250
|
-
to.pos = pos;
|
|
1251
|
-
to.finished = finished;
|
|
1252
|
-
to.destroyed = destroyed;
|
|
1253
|
-
if (length % blockLen)
|
|
1254
|
-
to.buffer.set(buffer);
|
|
1255
|
-
return to;
|
|
1256
|
-
}
|
|
1257
|
-
};
|
|
1258
|
-
|
|
1259
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/sha256.js
|
|
1260
|
-
var SHA256_K2 = /* @__PURE__ */ new Uint32Array([
|
|
1261
|
-
1116352408,
|
|
1262
|
-
1899447441,
|
|
1263
|
-
3049323471,
|
|
1264
|
-
3921009573,
|
|
1265
|
-
961987163,
|
|
1266
|
-
1508970993,
|
|
1267
|
-
2453635748,
|
|
1268
|
-
2870763221,
|
|
1269
|
-
3624381080,
|
|
1270
|
-
310598401,
|
|
1271
|
-
607225278,
|
|
1272
|
-
1426881987,
|
|
1273
|
-
1925078388,
|
|
1274
|
-
2162078206,
|
|
1275
|
-
2614888103,
|
|
1276
|
-
3248222580,
|
|
1277
|
-
3835390401,
|
|
1278
|
-
4022224774,
|
|
1279
|
-
264347078,
|
|
1280
|
-
604807628,
|
|
1281
|
-
770255983,
|
|
1282
|
-
1249150122,
|
|
1283
|
-
1555081692,
|
|
1284
|
-
1996064986,
|
|
1285
|
-
2554220882,
|
|
1286
|
-
2821834349,
|
|
1287
|
-
2952996808,
|
|
1288
|
-
3210313671,
|
|
1289
|
-
3336571891,
|
|
1290
|
-
3584528711,
|
|
1291
|
-
113926993,
|
|
1292
|
-
338241895,
|
|
1293
|
-
666307205,
|
|
1294
|
-
773529912,
|
|
1295
|
-
1294757372,
|
|
1296
|
-
1396182291,
|
|
1297
|
-
1695183700,
|
|
1298
|
-
1986661051,
|
|
1299
|
-
2177026350,
|
|
1300
|
-
2456956037,
|
|
1301
|
-
2730485921,
|
|
1302
|
-
2820302411,
|
|
1303
|
-
3259730800,
|
|
1304
|
-
3345764771,
|
|
1305
|
-
3516065817,
|
|
1306
|
-
3600352804,
|
|
1307
|
-
4094571909,
|
|
1308
|
-
275423344,
|
|
1309
|
-
430227734,
|
|
1310
|
-
506948616,
|
|
1311
|
-
659060556,
|
|
1312
|
-
883997877,
|
|
1313
|
-
958139571,
|
|
1314
|
-
1322822218,
|
|
1315
|
-
1537002063,
|
|
1316
|
-
1747873779,
|
|
1317
|
-
1955562222,
|
|
1318
|
-
2024104815,
|
|
1319
|
-
2227730452,
|
|
1320
|
-
2361852424,
|
|
1321
|
-
2428436474,
|
|
1322
|
-
2756734187,
|
|
1323
|
-
3204031479,
|
|
1324
|
-
3329325298
|
|
1325
|
-
]);
|
|
1326
|
-
var SHA256_IV2 = /* @__PURE__ */ new Uint32Array([
|
|
1327
|
-
1779033703,
|
|
1328
|
-
3144134277,
|
|
1329
|
-
1013904242,
|
|
1330
|
-
2773480762,
|
|
1331
|
-
1359893119,
|
|
1332
|
-
2600822924,
|
|
1333
|
-
528734635,
|
|
1334
|
-
1541459225
|
|
1335
|
-
]);
|
|
1336
|
-
var SHA256_W2 = /* @__PURE__ */ new Uint32Array(64);
|
|
1337
|
-
var SHA2562 = class extends HashMD2 {
|
|
1338
|
-
constructor() {
|
|
1339
|
-
super(64, 32, 8, false);
|
|
1340
|
-
this.A = SHA256_IV2[0] | 0;
|
|
1341
|
-
this.B = SHA256_IV2[1] | 0;
|
|
1342
|
-
this.C = SHA256_IV2[2] | 0;
|
|
1343
|
-
this.D = SHA256_IV2[3] | 0;
|
|
1344
|
-
this.E = SHA256_IV2[4] | 0;
|
|
1345
|
-
this.F = SHA256_IV2[5] | 0;
|
|
1346
|
-
this.G = SHA256_IV2[6] | 0;
|
|
1347
|
-
this.H = SHA256_IV2[7] | 0;
|
|
1348
|
-
}
|
|
1349
|
-
get() {
|
|
1350
|
-
const { A, B, C: C2, D, E, F, G: G2, H } = this;
|
|
1351
|
-
return [A, B, C2, D, E, F, G2, H];
|
|
1352
|
-
}
|
|
1353
|
-
// prettier-ignore
|
|
1354
|
-
set(A, B, C2, D, E, F, G2, H) {
|
|
1355
|
-
this.A = A | 0;
|
|
1356
|
-
this.B = B | 0;
|
|
1357
|
-
this.C = C2 | 0;
|
|
1358
|
-
this.D = D | 0;
|
|
1359
|
-
this.E = E | 0;
|
|
1360
|
-
this.F = F | 0;
|
|
1361
|
-
this.G = G2 | 0;
|
|
1362
|
-
this.H = H | 0;
|
|
1363
|
-
}
|
|
1364
|
-
process(view, offset) {
|
|
1365
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
1366
|
-
SHA256_W2[i] = view.getUint32(offset, false);
|
|
1367
|
-
for (let i = 16; i < 64; i++) {
|
|
1368
|
-
const W15 = SHA256_W2[i - 15];
|
|
1369
|
-
const W2 = SHA256_W2[i - 2];
|
|
1370
|
-
const s0 = rotr2(W15, 7) ^ rotr2(W15, 18) ^ W15 >>> 3;
|
|
1371
|
-
const s1 = rotr2(W2, 17) ^ rotr2(W2, 19) ^ W2 >>> 10;
|
|
1372
|
-
SHA256_W2[i] = s1 + SHA256_W2[i - 7] + s0 + SHA256_W2[i - 16] | 0;
|
|
1373
|
-
}
|
|
1374
|
-
let { A, B, C: C2, D, E, F, G: G2, H } = this;
|
|
1375
|
-
for (let i = 0; i < 64; i++) {
|
|
1376
|
-
const sigma1 = rotr2(E, 6) ^ rotr2(E, 11) ^ rotr2(E, 25);
|
|
1377
|
-
const T1 = H + sigma1 + Chi2(E, F, G2) + SHA256_K2[i] + SHA256_W2[i] | 0;
|
|
1378
|
-
const sigma0 = rotr2(A, 2) ^ rotr2(A, 13) ^ rotr2(A, 22);
|
|
1379
|
-
const T2 = sigma0 + Maj2(A, B, C2) | 0;
|
|
1380
|
-
H = G2;
|
|
1381
|
-
G2 = F;
|
|
1382
|
-
F = E;
|
|
1383
|
-
E = D + T1 | 0;
|
|
1384
|
-
D = C2;
|
|
1385
|
-
C2 = B;
|
|
1386
|
-
B = A;
|
|
1387
|
-
A = T1 + T2 | 0;
|
|
1388
|
-
}
|
|
1389
|
-
A = A + this.A | 0;
|
|
1390
|
-
B = B + this.B | 0;
|
|
1391
|
-
C2 = C2 + this.C | 0;
|
|
1392
|
-
D = D + this.D | 0;
|
|
1393
|
-
E = E + this.E | 0;
|
|
1394
|
-
F = F + this.F | 0;
|
|
1395
|
-
G2 = G2 + this.G | 0;
|
|
1396
|
-
H = H + this.H | 0;
|
|
1397
|
-
this.set(A, B, C2, D, E, F, G2, H);
|
|
1398
|
-
}
|
|
1399
|
-
roundClean() {
|
|
1400
|
-
SHA256_W2.fill(0);
|
|
1401
|
-
}
|
|
1402
|
-
destroy() {
|
|
1403
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1404
|
-
this.buffer.fill(0);
|
|
1405
|
-
}
|
|
1406
|
-
};
|
|
1407
|
-
var sha2562 = /* @__PURE__ */ wrapConstructor2(() => new SHA2562());
|
|
1408
|
-
|
|
1409
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.6.0/node_modules/@noble/hashes/esm/hmac.js
|
|
1410
|
-
var HMAC = class extends Hash2 {
|
|
1411
|
-
constructor(hash, _key) {
|
|
1412
|
-
super();
|
|
1413
|
-
this.finished = false;
|
|
1414
|
-
this.destroyed = false;
|
|
1415
|
-
ahash(hash);
|
|
1416
|
-
const key = toBytes2(_key);
|
|
1417
|
-
this.iHash = hash.create();
|
|
1418
|
-
if (typeof this.iHash.update !== "function")
|
|
1419
|
-
throw new Error("Expected instance of class which extends utils.Hash");
|
|
1420
|
-
this.blockLen = this.iHash.blockLen;
|
|
1421
|
-
this.outputLen = this.iHash.outputLen;
|
|
1422
|
-
const blockLen = this.blockLen;
|
|
1423
|
-
const pad = new Uint8Array(blockLen);
|
|
1424
|
-
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
1425
|
-
for (let i = 0; i < pad.length; i++)
|
|
1426
|
-
pad[i] ^= 54;
|
|
1427
|
-
this.iHash.update(pad);
|
|
1428
|
-
this.oHash = hash.create();
|
|
1429
|
-
for (let i = 0; i < pad.length; i++)
|
|
1430
|
-
pad[i] ^= 54 ^ 92;
|
|
1431
|
-
this.oHash.update(pad);
|
|
1432
|
-
pad.fill(0);
|
|
1433
|
-
}
|
|
1434
|
-
update(buf) {
|
|
1435
|
-
aexists2(this);
|
|
1436
|
-
this.iHash.update(buf);
|
|
1437
|
-
return this;
|
|
1438
|
-
}
|
|
1439
|
-
digestInto(out) {
|
|
1440
|
-
aexists2(this);
|
|
1441
|
-
abytes3(out, this.outputLen);
|
|
1442
|
-
this.finished = true;
|
|
1443
|
-
this.iHash.digestInto(out);
|
|
1444
|
-
this.oHash.update(out);
|
|
1445
|
-
this.oHash.digestInto(out);
|
|
1446
|
-
this.destroy();
|
|
1447
|
-
}
|
|
1448
|
-
digest() {
|
|
1449
|
-
const out = new Uint8Array(this.oHash.outputLen);
|
|
1450
|
-
this.digestInto(out);
|
|
1451
|
-
return out;
|
|
1452
|
-
}
|
|
1453
|
-
_cloneInto(to) {
|
|
1454
|
-
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
1455
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1456
|
-
to = to;
|
|
1457
|
-
to.finished = finished;
|
|
1458
|
-
to.destroyed = destroyed;
|
|
1459
|
-
to.blockLen = blockLen;
|
|
1460
|
-
to.outputLen = outputLen;
|
|
1461
|
-
to.oHash = oHash._cloneInto(to.oHash);
|
|
1462
|
-
to.iHash = iHash._cloneInto(to.iHash);
|
|
1463
|
-
return to;
|
|
1464
|
-
}
|
|
1465
|
-
destroy() {
|
|
1466
|
-
this.destroyed = true;
|
|
1467
|
-
this.oHash.destroy();
|
|
1468
|
-
this.iHash.destroy();
|
|
1469
|
-
}
|
|
1470
|
-
};
|
|
1471
|
-
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
1472
|
-
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
1473
|
-
|
|
1474
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/utils.js
|
|
1475
|
-
var utils_exports = {};
|
|
1476
|
-
__export(utils_exports, {
|
|
1477
|
-
aInRange: () => aInRange,
|
|
1478
|
-
abool: () => abool,
|
|
1479
|
-
abytes: () => abytes4,
|
|
1480
|
-
bitGet: () => bitGet,
|
|
1481
|
-
bitLen: () => bitLen,
|
|
1482
|
-
bitMask: () => bitMask,
|
|
1483
|
-
bitSet: () => bitSet,
|
|
1484
|
-
bytesToHex: () => bytesToHex2,
|
|
1485
|
-
bytesToNumberBE: () => bytesToNumberBE,
|
|
1486
|
-
bytesToNumberLE: () => bytesToNumberLE2,
|
|
1487
|
-
concatBytes: () => concatBytes3,
|
|
1488
|
-
createHmacDrbg: () => createHmacDrbg,
|
|
1489
|
-
ensureBytes: () => ensureBytes,
|
|
1490
|
-
equalBytes: () => equalBytes,
|
|
1491
|
-
hexToBytes: () => hexToBytes2,
|
|
1492
|
-
hexToNumber: () => hexToNumber,
|
|
1493
|
-
inRange: () => inRange,
|
|
1494
|
-
isBytes: () => isBytes4,
|
|
1495
|
-
memoized: () => memoized,
|
|
1496
|
-
notImplemented: () => notImplemented,
|
|
1497
|
-
numberToBytesBE: () => numberToBytesBE,
|
|
1498
|
-
numberToBytesLE: () => numberToBytesLE,
|
|
1499
|
-
numberToHexUnpadded: () => numberToHexUnpadded,
|
|
1500
|
-
numberToVarBytesBE: () => numberToVarBytesBE,
|
|
1501
|
-
utf8ToBytes: () => utf8ToBytes3,
|
|
1502
|
-
validateObject: () => validateObject
|
|
1503
|
-
});
|
|
1504
|
-
var _0n = /* @__PURE__ */ BigInt(0);
|
|
1505
|
-
var _1n = /* @__PURE__ */ BigInt(1);
|
|
1506
|
-
var _2n = /* @__PURE__ */ BigInt(2);
|
|
1507
|
-
function isBytes4(a) {
|
|
1508
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
1509
|
-
}
|
|
1510
|
-
function abytes4(item) {
|
|
1511
|
-
if (!isBytes4(item))
|
|
1512
|
-
throw new Error("Uint8Array expected");
|
|
1513
|
-
}
|
|
1514
|
-
function abool(title, value) {
|
|
1515
|
-
if (typeof value !== "boolean")
|
|
1516
|
-
throw new Error(title + " boolean expected, got " + value);
|
|
1517
|
-
}
|
|
1518
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
1519
|
-
function bytesToHex2(bytes) {
|
|
1520
|
-
abytes4(bytes);
|
|
1521
|
-
let hex = "";
|
|
1522
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
1523
|
-
hex += hexes[bytes[i]];
|
|
1524
|
-
}
|
|
1525
|
-
return hex;
|
|
1526
|
-
}
|
|
1527
|
-
function numberToHexUnpadded(num) {
|
|
1528
|
-
const hex = num.toString(16);
|
|
1529
|
-
return hex.length & 1 ? "0" + hex : hex;
|
|
1530
|
-
}
|
|
1531
|
-
function hexToNumber(hex) {
|
|
1532
|
-
if (typeof hex !== "string")
|
|
1533
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
1534
|
-
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
1535
|
-
}
|
|
1536
|
-
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
1537
|
-
function asciiToBase16(ch) {
|
|
1538
|
-
if (ch >= asciis._0 && ch <= asciis._9)
|
|
1539
|
-
return ch - asciis._0;
|
|
1540
|
-
if (ch >= asciis.A && ch <= asciis.F)
|
|
1541
|
-
return ch - (asciis.A - 10);
|
|
1542
|
-
if (ch >= asciis.a && ch <= asciis.f)
|
|
1543
|
-
return ch - (asciis.a - 10);
|
|
1544
|
-
return;
|
|
1545
|
-
}
|
|
1546
|
-
function hexToBytes2(hex) {
|
|
1547
|
-
if (typeof hex !== "string")
|
|
1548
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
1549
|
-
const hl = hex.length;
|
|
1550
|
-
const al = hl / 2;
|
|
1551
|
-
if (hl % 2)
|
|
1552
|
-
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
1553
|
-
const array = new Uint8Array(al);
|
|
1554
|
-
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
1555
|
-
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
1556
|
-
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
1557
|
-
if (n1 === void 0 || n2 === void 0) {
|
|
1558
|
-
const char = hex[hi] + hex[hi + 1];
|
|
1559
|
-
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
1560
|
-
}
|
|
1561
|
-
array[ai] = n1 * 16 + n2;
|
|
1562
|
-
}
|
|
1563
|
-
return array;
|
|
1564
|
-
}
|
|
1565
|
-
function bytesToNumberBE(bytes) {
|
|
1566
|
-
return hexToNumber(bytesToHex2(bytes));
|
|
1567
|
-
}
|
|
1568
|
-
function bytesToNumberLE2(bytes) {
|
|
1569
|
-
abytes4(bytes);
|
|
1570
|
-
return hexToNumber(bytesToHex2(Uint8Array.from(bytes).reverse()));
|
|
1571
|
-
}
|
|
1572
|
-
function numberToBytesBE(n, len) {
|
|
1573
|
-
return hexToBytes2(n.toString(16).padStart(len * 2, "0"));
|
|
1574
|
-
}
|
|
1575
|
-
function numberToBytesLE(n, len) {
|
|
1576
|
-
return numberToBytesBE(n, len).reverse();
|
|
1577
|
-
}
|
|
1578
|
-
function numberToVarBytesBE(n) {
|
|
1579
|
-
return hexToBytes2(numberToHexUnpadded(n));
|
|
1580
|
-
}
|
|
1581
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
1582
|
-
let res;
|
|
1583
|
-
if (typeof hex === "string") {
|
|
1
|
+
/**
|
|
2
|
+
* Cryptosuite dispatch — the single entry point for signature primitive
|
|
3
|
+
* verification in @motebit/crypto.
|
|
4
|
+
*
|
|
5
|
+
* Every `verify*` function that checks a signed motebit artifact MUST
|
|
6
|
+
* route through `verifyBySuite`. Direct calls to `ed.verifyAsync`,
|
|
7
|
+
* `ed.signAsync`, or any other primitive outside this file are a
|
|
8
|
+
* drift-gate violation — see `scripts/check-suite-dispatch.ts`.
|
|
9
|
+
*
|
|
10
|
+
* Rationale: the suite value on a wire artifact names a complete
|
|
11
|
+
* verification recipe (algorithm + canonicalization + encoding). The
|
|
12
|
+
* dispatcher's job is to map `suite` → primitive. Encoding of the
|
|
13
|
+
* signature and public key stays at the artifact layer (every artifact
|
|
14
|
+
* already has its own encoding convention — see `spec/<artifact>-v1.md`
|
|
15
|
+
* `#### Wire format (foundation law)` subsections); the dispatcher
|
|
16
|
+
* receives already-decoded bytes and returns already-produced bytes.
|
|
17
|
+
* This keeps the Ed25519 switch arm honest: it does one thing, and the
|
|
18
|
+
* PQ switch arms that follow in 2026+ will do the same one thing for
|
|
19
|
+
* ML-DSA / SLH-DSA.
|
|
20
|
+
*
|
|
21
|
+
* Fail-closed throughout:
|
|
22
|
+
* - unknown `SuiteId` → `verifyBySuite` returns `false`, `signBySuite` throws.
|
|
23
|
+
* - unsupported algorithm in the switch (PQ placeholder) → throws with
|
|
24
|
+
* a clear message so the call site doesn't silently succeed.
|
|
25
|
+
* - primitive-level exception → `verifyBySuite` returns `false`.
|
|
26
|
+
*
|
|
27
|
+
* No legacy-no-suite path. A caller that reaches this function without
|
|
28
|
+
* a valid `SuiteId` has already lost; the function enforces that
|
|
29
|
+
* contract at the boundary.
|
|
30
|
+
*/
|
|
31
|
+
// crypto-suite: intentional-primitive-call
|
|
32
|
+
// This file is the one place in @motebit/crypto allowed to import the
|
|
33
|
+
// raw Ed25519 primitives. The `check-suite-dispatch` gate scans every
|
|
34
|
+
// other source file in this package for `ed.verifyAsync` / `ed.signAsync`
|
|
35
|
+
// and fails CI if they appear outside this file.
|
|
36
|
+
import * as ed from "@noble/ed25519";
|
|
37
|
+
import { sha512 } from "@noble/hashes/sha512";
|
|
38
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
39
|
+
// P-256 ECDSA for hardware-attestation receipts (Apple Secure Enclave
|
|
40
|
+
// generates P-256 keys; this is the verifier side). Centralizing the
|
|
41
|
+
// primitive call here keeps the same single-home-for-primitives
|
|
42
|
+
// discipline the Ed25519 path follows.
|
|
43
|
+
import { p256 } from "@noble/curves/p256";
|
|
44
|
+
// @noble/ed25519 v3 requires explicit SHA-512 binding. Idempotent:
|
|
45
|
+
// binding twice is harmless, but some test environments import
|
|
46
|
+
// signing.ts first — the check guards against redundant assignment.
|
|
47
|
+
if (!ed.hashes.sha512) {
|
|
48
|
+
ed.hashes.sha512 = (msg) => sha512(msg);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Verify an already-decoded signature over `canonicalBytes` using the
|
|
52
|
+
* primitive named by `suite`. The caller is responsible for:
|
|
53
|
+
* 1. canonicalization (the bytes are already the signing input per
|
|
54
|
+
* the suite's canonicalization rule);
|
|
55
|
+
* 2. signature and public-key decoding (hex → Uint8Array, base64url
|
|
56
|
+
* → Uint8Array, multibase → Uint8Array per the suite's
|
|
57
|
+
* `signatureEncoding` / `publicKeyEncoding`).
|
|
58
|
+
*
|
|
59
|
+
* Returns `false` on unknown or unsupported suite, on primitive-level
|
|
60
|
+
* exception, and on signature mismatch. Never throws in the Ed25519
|
|
61
|
+
* path. Throws only on PQ suites (placeholder until implementation
|
|
62
|
+
* lands), because a misconfigured dispatcher there would silently
|
|
63
|
+
* pass every verification.
|
|
64
|
+
*/
|
|
65
|
+
export async function verifyBySuite(suite, canonicalBytes, signatureBytes, publicKeyBytes) {
|
|
66
|
+
// Exhaustive switch on the SuiteId literal union. When ML-DSA /
|
|
67
|
+
// SLH-DSA suites land as new `SuiteId` members, TypeScript will
|
|
68
|
+
// refuse to compile this switch until their arms are added.
|
|
69
|
+
switch (suite) {
|
|
70
|
+
case "motebit-jcs-ed25519-b64-v1":
|
|
71
|
+
case "motebit-jcs-ed25519-hex-v1":
|
|
72
|
+
case "motebit-jwt-ed25519-v1":
|
|
73
|
+
case "motebit-concat-ed25519-hex-v1":
|
|
74
|
+
case "eddsa-jcs-2022":
|
|
75
|
+
try {
|
|
76
|
+
return await ed.verifyAsync(signatureBytes, canonicalBytes, publicKeyBytes);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Produce a signature over `canonicalBytes` using the primitive named
|
|
85
|
+
* by `suite`. Mirrors `verifyBySuite`: caller provides already-
|
|
86
|
+
* canonicalized bytes; caller encodes the returned `Uint8Array` per
|
|
87
|
+
* the suite's `signatureEncoding` at the artifact boundary.
|
|
88
|
+
*
|
|
89
|
+
* Throws on unknown or unsupported suite (fail-closed). Signers that
|
|
90
|
+
* catch and swallow this exception would ship unsigned artifacts,
|
|
91
|
+
* which is a worse failure mode than a loud throw.
|
|
92
|
+
*/
|
|
93
|
+
export async function signBySuite(suite, canonicalBytes, privateKeyBytes) {
|
|
94
|
+
switch (suite) {
|
|
95
|
+
case "motebit-jcs-ed25519-b64-v1":
|
|
96
|
+
case "motebit-jcs-ed25519-hex-v1":
|
|
97
|
+
case "motebit-jwt-ed25519-v1":
|
|
98
|
+
case "motebit-concat-ed25519-hex-v1":
|
|
99
|
+
case "eddsa-jcs-2022":
|
|
100
|
+
return ed.signAsync(canonicalBytes, privateKeyBytes);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Lowest-level Ed25519 primitives. These are the functions callers
|
|
105
|
+
* should use when they genuinely need the primitive — for example,
|
|
106
|
+
* generating a keypair at identity bootstrap, or computing a
|
|
107
|
+
* succession-chain signature where the caller has already dispatched
|
|
108
|
+
* by suite. Exported from this file so the drift gate's scan rule is
|
|
109
|
+
* simple: "`ed.*` lives only in `suite-dispatch.ts`."
|
|
110
|
+
*/
|
|
111
|
+
export async function ed25519Sign(message, privateKey) {
|
|
112
|
+
return ed.signAsync(message, privateKey);
|
|
113
|
+
}
|
|
114
|
+
export async function ed25519Verify(signature, message, publicKey) {
|
|
1584
115
|
try {
|
|
1585
|
-
|
|
1586
|
-
} catch (e) {
|
|
1587
|
-
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
1588
|
-
}
|
|
1589
|
-
} else if (isBytes4(hex)) {
|
|
1590
|
-
res = Uint8Array.from(hex);
|
|
1591
|
-
} else {
|
|
1592
|
-
throw new Error(title + " must be hex string or Uint8Array");
|
|
1593
|
-
}
|
|
1594
|
-
const len = res.length;
|
|
1595
|
-
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
1596
|
-
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
1597
|
-
return res;
|
|
1598
|
-
}
|
|
1599
|
-
function concatBytes3(...arrays) {
|
|
1600
|
-
let sum = 0;
|
|
1601
|
-
for (let i = 0; i < arrays.length; i++) {
|
|
1602
|
-
const a = arrays[i];
|
|
1603
|
-
abytes4(a);
|
|
1604
|
-
sum += a.length;
|
|
1605
|
-
}
|
|
1606
|
-
const res = new Uint8Array(sum);
|
|
1607
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
1608
|
-
const a = arrays[i];
|
|
1609
|
-
res.set(a, pad);
|
|
1610
|
-
pad += a.length;
|
|
1611
|
-
}
|
|
1612
|
-
return res;
|
|
1613
|
-
}
|
|
1614
|
-
function equalBytes(a, b) {
|
|
1615
|
-
if (a.length !== b.length)
|
|
1616
|
-
return false;
|
|
1617
|
-
let diff = 0;
|
|
1618
|
-
for (let i = 0; i < a.length; i++)
|
|
1619
|
-
diff |= a[i] ^ b[i];
|
|
1620
|
-
return diff === 0;
|
|
1621
|
-
}
|
|
1622
|
-
function utf8ToBytes3(str) {
|
|
1623
|
-
if (typeof str !== "string")
|
|
1624
|
-
throw new Error("string expected");
|
|
1625
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
1626
|
-
}
|
|
1627
|
-
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
1628
|
-
function inRange(n, min, max) {
|
|
1629
|
-
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
1630
|
-
}
|
|
1631
|
-
function aInRange(title, n, min, max) {
|
|
1632
|
-
if (!inRange(n, min, max))
|
|
1633
|
-
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
1634
|
-
}
|
|
1635
|
-
function bitLen(n) {
|
|
1636
|
-
let len;
|
|
1637
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
1638
|
-
;
|
|
1639
|
-
return len;
|
|
1640
|
-
}
|
|
1641
|
-
function bitGet(n, pos) {
|
|
1642
|
-
return n >> BigInt(pos) & _1n;
|
|
1643
|
-
}
|
|
1644
|
-
function bitSet(n, pos, value) {
|
|
1645
|
-
return n | (value ? _1n : _0n) << BigInt(pos);
|
|
1646
|
-
}
|
|
1647
|
-
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
1648
|
-
var u8n2 = (data) => new Uint8Array(data);
|
|
1649
|
-
var u8fr2 = (arr) => Uint8Array.from(arr);
|
|
1650
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
1651
|
-
if (typeof hashLen !== "number" || hashLen < 2)
|
|
1652
|
-
throw new Error("hashLen must be a number");
|
|
1653
|
-
if (typeof qByteLen !== "number" || qByteLen < 2)
|
|
1654
|
-
throw new Error("qByteLen must be a number");
|
|
1655
|
-
if (typeof hmacFn !== "function")
|
|
1656
|
-
throw new Error("hmacFn must be a function");
|
|
1657
|
-
let v = u8n2(hashLen);
|
|
1658
|
-
let k = u8n2(hashLen);
|
|
1659
|
-
let i = 0;
|
|
1660
|
-
const reset = () => {
|
|
1661
|
-
v.fill(1);
|
|
1662
|
-
k.fill(0);
|
|
1663
|
-
i = 0;
|
|
1664
|
-
};
|
|
1665
|
-
const h2 = (...b) => hmacFn(k, v, ...b);
|
|
1666
|
-
const reseed = (seed = u8n2()) => {
|
|
1667
|
-
k = h2(u8fr2([0]), seed);
|
|
1668
|
-
v = h2();
|
|
1669
|
-
if (seed.length === 0)
|
|
1670
|
-
return;
|
|
1671
|
-
k = h2(u8fr2([1]), seed);
|
|
1672
|
-
v = h2();
|
|
1673
|
-
};
|
|
1674
|
-
const gen = () => {
|
|
1675
|
-
if (i++ >= 1e3)
|
|
1676
|
-
throw new Error("drbg: tried 1000 values");
|
|
1677
|
-
let len = 0;
|
|
1678
|
-
const out = [];
|
|
1679
|
-
while (len < qByteLen) {
|
|
1680
|
-
v = h2();
|
|
1681
|
-
const sl = v.slice();
|
|
1682
|
-
out.push(sl);
|
|
1683
|
-
len += v.length;
|
|
1684
|
-
}
|
|
1685
|
-
return concatBytes3(...out);
|
|
1686
|
-
};
|
|
1687
|
-
const genUntil = (seed, pred) => {
|
|
1688
|
-
reset();
|
|
1689
|
-
reseed(seed);
|
|
1690
|
-
let res = void 0;
|
|
1691
|
-
while (!(res = pred(gen())))
|
|
1692
|
-
reseed();
|
|
1693
|
-
reset();
|
|
1694
|
-
return res;
|
|
1695
|
-
};
|
|
1696
|
-
return genUntil;
|
|
1697
|
-
}
|
|
1698
|
-
var validatorFns = {
|
|
1699
|
-
bigint: (val) => typeof val === "bigint",
|
|
1700
|
-
function: (val) => typeof val === "function",
|
|
1701
|
-
boolean: (val) => typeof val === "boolean",
|
|
1702
|
-
string: (val) => typeof val === "string",
|
|
1703
|
-
stringOrUint8Array: (val) => typeof val === "string" || isBytes4(val),
|
|
1704
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
1705
|
-
array: (val) => Array.isArray(val),
|
|
1706
|
-
field: (val, object) => object.Fp.isValid(val),
|
|
1707
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
1708
|
-
};
|
|
1709
|
-
function validateObject(object, validators, optValidators = {}) {
|
|
1710
|
-
const checkField = (fieldName, type, isOptional) => {
|
|
1711
|
-
const checkVal = validatorFns[type];
|
|
1712
|
-
if (typeof checkVal !== "function")
|
|
1713
|
-
throw new Error("invalid validator function");
|
|
1714
|
-
const val = object[fieldName];
|
|
1715
|
-
if (isOptional && val === void 0)
|
|
1716
|
-
return;
|
|
1717
|
-
if (!checkVal(val, object)) {
|
|
1718
|
-
throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
|
|
1719
|
-
}
|
|
1720
|
-
};
|
|
1721
|
-
for (const [fieldName, type] of Object.entries(validators))
|
|
1722
|
-
checkField(fieldName, type, false);
|
|
1723
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
|
1724
|
-
checkField(fieldName, type, true);
|
|
1725
|
-
return object;
|
|
1726
|
-
}
|
|
1727
|
-
var notImplemented = () => {
|
|
1728
|
-
throw new Error("not implemented");
|
|
1729
|
-
};
|
|
1730
|
-
function memoized(fn) {
|
|
1731
|
-
const map = /* @__PURE__ */ new WeakMap();
|
|
1732
|
-
return (arg, ...args) => {
|
|
1733
|
-
const val = map.get(arg);
|
|
1734
|
-
if (val !== void 0)
|
|
1735
|
-
return val;
|
|
1736
|
-
const computed = fn(arg, ...args);
|
|
1737
|
-
map.set(arg, computed);
|
|
1738
|
-
return computed;
|
|
1739
|
-
};
|
|
1740
|
-
}
|
|
1741
|
-
|
|
1742
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/modular.js
|
|
1743
|
-
var _0n2 = BigInt(0);
|
|
1744
|
-
var _1n2 = BigInt(1);
|
|
1745
|
-
var _2n2 = /* @__PURE__ */ BigInt(2);
|
|
1746
|
-
var _3n = /* @__PURE__ */ BigInt(3);
|
|
1747
|
-
var _4n = /* @__PURE__ */ BigInt(4);
|
|
1748
|
-
var _5n = /* @__PURE__ */ BigInt(5);
|
|
1749
|
-
var _8n = /* @__PURE__ */ BigInt(8);
|
|
1750
|
-
var _9n = /* @__PURE__ */ BigInt(9);
|
|
1751
|
-
var _16n = /* @__PURE__ */ BigInt(16);
|
|
1752
|
-
function mod(a, b) {
|
|
1753
|
-
const result = a % b;
|
|
1754
|
-
return result >= _0n2 ? result : b + result;
|
|
1755
|
-
}
|
|
1756
|
-
function pow(num, power, modulo) {
|
|
1757
|
-
if (power < _0n2)
|
|
1758
|
-
throw new Error("invalid exponent, negatives unsupported");
|
|
1759
|
-
if (modulo <= _0n2)
|
|
1760
|
-
throw new Error("invalid modulus");
|
|
1761
|
-
if (modulo === _1n2)
|
|
1762
|
-
return _0n2;
|
|
1763
|
-
let res = _1n2;
|
|
1764
|
-
while (power > _0n2) {
|
|
1765
|
-
if (power & _1n2)
|
|
1766
|
-
res = res * num % modulo;
|
|
1767
|
-
num = num * num % modulo;
|
|
1768
|
-
power >>= _1n2;
|
|
1769
|
-
}
|
|
1770
|
-
return res;
|
|
1771
|
-
}
|
|
1772
|
-
function invert2(number, modulo) {
|
|
1773
|
-
if (number === _0n2)
|
|
1774
|
-
throw new Error("invert: expected non-zero number");
|
|
1775
|
-
if (modulo <= _0n2)
|
|
1776
|
-
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
1777
|
-
let a = mod(number, modulo);
|
|
1778
|
-
let b = modulo;
|
|
1779
|
-
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
1780
|
-
while (a !== _0n2) {
|
|
1781
|
-
const q = b / a;
|
|
1782
|
-
const r = b % a;
|
|
1783
|
-
const m = x - u * q;
|
|
1784
|
-
const n = y - v * q;
|
|
1785
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
1786
|
-
}
|
|
1787
|
-
const gcd = b;
|
|
1788
|
-
if (gcd !== _1n2)
|
|
1789
|
-
throw new Error("invert: does not exist");
|
|
1790
|
-
return mod(x, modulo);
|
|
1791
|
-
}
|
|
1792
|
-
function tonelliShanks(P2) {
|
|
1793
|
-
const legendreC = (P2 - _1n2) / _2n2;
|
|
1794
|
-
let Q, S, Z;
|
|
1795
|
-
for (Q = P2 - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
1796
|
-
;
|
|
1797
|
-
for (Z = _2n2; Z < P2 && pow(Z, legendreC, P2) !== P2 - _1n2; Z++) {
|
|
1798
|
-
if (Z > 1e3)
|
|
1799
|
-
throw new Error("Cannot find square root: likely non-prime P");
|
|
1800
|
-
}
|
|
1801
|
-
if (S === 1) {
|
|
1802
|
-
const p1div4 = (P2 + _1n2) / _4n;
|
|
1803
|
-
return function tonelliFast(Fp, n) {
|
|
1804
|
-
const root = Fp.pow(n, p1div4);
|
|
1805
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
1806
|
-
throw new Error("Cannot find square root");
|
|
1807
|
-
return root;
|
|
1808
|
-
};
|
|
1809
|
-
}
|
|
1810
|
-
const Q1div2 = (Q + _1n2) / _2n2;
|
|
1811
|
-
return function tonelliSlow(Fp, n) {
|
|
1812
|
-
if (Fp.pow(n, legendreC) === Fp.neg(Fp.ONE))
|
|
1813
|
-
throw new Error("Cannot find square root");
|
|
1814
|
-
let r = S;
|
|
1815
|
-
let g = Fp.pow(Fp.mul(Fp.ONE, Z), Q);
|
|
1816
|
-
let x = Fp.pow(n, Q1div2);
|
|
1817
|
-
let b = Fp.pow(n, Q);
|
|
1818
|
-
while (!Fp.eql(b, Fp.ONE)) {
|
|
1819
|
-
if (Fp.eql(b, Fp.ZERO))
|
|
1820
|
-
return Fp.ZERO;
|
|
1821
|
-
let m = 1;
|
|
1822
|
-
for (let t2 = Fp.sqr(b); m < r; m++) {
|
|
1823
|
-
if (Fp.eql(t2, Fp.ONE))
|
|
1824
|
-
break;
|
|
1825
|
-
t2 = Fp.sqr(t2);
|
|
1826
|
-
}
|
|
1827
|
-
const ge = Fp.pow(g, _1n2 << BigInt(r - m - 1));
|
|
1828
|
-
g = Fp.sqr(ge);
|
|
1829
|
-
x = Fp.mul(x, ge);
|
|
1830
|
-
b = Fp.mul(b, g);
|
|
1831
|
-
r = m;
|
|
1832
|
-
}
|
|
1833
|
-
return x;
|
|
1834
|
-
};
|
|
1835
|
-
}
|
|
1836
|
-
function FpSqrt(P2) {
|
|
1837
|
-
if (P2 % _4n === _3n) {
|
|
1838
|
-
const p1div4 = (P2 + _1n2) / _4n;
|
|
1839
|
-
return function sqrt3mod4(Fp, n) {
|
|
1840
|
-
const root = Fp.pow(n, p1div4);
|
|
1841
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
1842
|
-
throw new Error("Cannot find square root");
|
|
1843
|
-
return root;
|
|
1844
|
-
};
|
|
1845
|
-
}
|
|
1846
|
-
if (P2 % _8n === _5n) {
|
|
1847
|
-
const c1 = (P2 - _5n) / _8n;
|
|
1848
|
-
return function sqrt5mod8(Fp, n) {
|
|
1849
|
-
const n2 = Fp.mul(n, _2n2);
|
|
1850
|
-
const v = Fp.pow(n2, c1);
|
|
1851
|
-
const nv = Fp.mul(n, v);
|
|
1852
|
-
const i = Fp.mul(Fp.mul(nv, _2n2), v);
|
|
1853
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
1854
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
|
1855
|
-
throw new Error("Cannot find square root");
|
|
1856
|
-
return root;
|
|
1857
|
-
};
|
|
1858
|
-
}
|
|
1859
|
-
if (P2 % _16n === _9n) {
|
|
1860
|
-
}
|
|
1861
|
-
return tonelliShanks(P2);
|
|
1862
|
-
}
|
|
1863
|
-
var FIELD_FIELDS = [
|
|
1864
|
-
"create",
|
|
1865
|
-
"isValid",
|
|
1866
|
-
"is0",
|
|
1867
|
-
"neg",
|
|
1868
|
-
"inv",
|
|
1869
|
-
"sqrt",
|
|
1870
|
-
"sqr",
|
|
1871
|
-
"eql",
|
|
1872
|
-
"add",
|
|
1873
|
-
"sub",
|
|
1874
|
-
"mul",
|
|
1875
|
-
"pow",
|
|
1876
|
-
"div",
|
|
1877
|
-
"addN",
|
|
1878
|
-
"subN",
|
|
1879
|
-
"mulN",
|
|
1880
|
-
"sqrN"
|
|
1881
|
-
];
|
|
1882
|
-
function validateField(field) {
|
|
1883
|
-
const initial = {
|
|
1884
|
-
ORDER: "bigint",
|
|
1885
|
-
MASK: "bigint",
|
|
1886
|
-
BYTES: "isSafeInteger",
|
|
1887
|
-
BITS: "isSafeInteger"
|
|
1888
|
-
};
|
|
1889
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
1890
|
-
map[val] = "function";
|
|
1891
|
-
return map;
|
|
1892
|
-
}, initial);
|
|
1893
|
-
return validateObject(field, opts);
|
|
1894
|
-
}
|
|
1895
|
-
function FpPow(f, num, power) {
|
|
1896
|
-
if (power < _0n2)
|
|
1897
|
-
throw new Error("invalid exponent, negatives unsupported");
|
|
1898
|
-
if (power === _0n2)
|
|
1899
|
-
return f.ONE;
|
|
1900
|
-
if (power === _1n2)
|
|
1901
|
-
return num;
|
|
1902
|
-
let p = f.ONE;
|
|
1903
|
-
let d = num;
|
|
1904
|
-
while (power > _0n2) {
|
|
1905
|
-
if (power & _1n2)
|
|
1906
|
-
p = f.mul(p, d);
|
|
1907
|
-
d = f.sqr(d);
|
|
1908
|
-
power >>= _1n2;
|
|
1909
|
-
}
|
|
1910
|
-
return p;
|
|
1911
|
-
}
|
|
1912
|
-
function FpInvertBatch(f, nums) {
|
|
1913
|
-
const tmp = new Array(nums.length);
|
|
1914
|
-
const lastMultiplied = nums.reduce((acc, num, i) => {
|
|
1915
|
-
if (f.is0(num))
|
|
1916
|
-
return acc;
|
|
1917
|
-
tmp[i] = acc;
|
|
1918
|
-
return f.mul(acc, num);
|
|
1919
|
-
}, f.ONE);
|
|
1920
|
-
const inverted = f.inv(lastMultiplied);
|
|
1921
|
-
nums.reduceRight((acc, num, i) => {
|
|
1922
|
-
if (f.is0(num))
|
|
1923
|
-
return acc;
|
|
1924
|
-
tmp[i] = f.mul(acc, tmp[i]);
|
|
1925
|
-
return f.mul(acc, num);
|
|
1926
|
-
}, inverted);
|
|
1927
|
-
return tmp;
|
|
1928
|
-
}
|
|
1929
|
-
function nLength(n, nBitLength) {
|
|
1930
|
-
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
1931
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
1932
|
-
return { nBitLength: _nBitLength, nByteLength };
|
|
1933
|
-
}
|
|
1934
|
-
function Field(ORDER, bitLen2, isLE = false, redef = {}) {
|
|
1935
|
-
if (ORDER <= _0n2)
|
|
1936
|
-
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
1937
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
|
1938
|
-
if (BYTES > 2048)
|
|
1939
|
-
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
1940
|
-
let sqrtP;
|
|
1941
|
-
const f = Object.freeze({
|
|
1942
|
-
ORDER,
|
|
1943
|
-
BITS,
|
|
1944
|
-
BYTES,
|
|
1945
|
-
MASK: bitMask(BITS),
|
|
1946
|
-
ZERO: _0n2,
|
|
1947
|
-
ONE: _1n2,
|
|
1948
|
-
create: (num) => mod(num, ORDER),
|
|
1949
|
-
isValid: (num) => {
|
|
1950
|
-
if (typeof num !== "bigint")
|
|
1951
|
-
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
1952
|
-
return _0n2 <= num && num < ORDER;
|
|
1953
|
-
},
|
|
1954
|
-
is0: (num) => num === _0n2,
|
|
1955
|
-
isOdd: (num) => (num & _1n2) === _1n2,
|
|
1956
|
-
neg: (num) => mod(-num, ORDER),
|
|
1957
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
1958
|
-
sqr: (num) => mod(num * num, ORDER),
|
|
1959
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
1960
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
1961
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
1962
|
-
pow: (num, power) => FpPow(f, num, power),
|
|
1963
|
-
div: (lhs, rhs) => mod(lhs * invert2(rhs, ORDER), ORDER),
|
|
1964
|
-
// Same as above, but doesn't normalize
|
|
1965
|
-
sqrN: (num) => num * num,
|
|
1966
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
1967
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
1968
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
1969
|
-
inv: (num) => invert2(num, ORDER),
|
|
1970
|
-
sqrt: redef.sqrt || ((n) => {
|
|
1971
|
-
if (!sqrtP)
|
|
1972
|
-
sqrtP = FpSqrt(ORDER);
|
|
1973
|
-
return sqrtP(f, n);
|
|
1974
|
-
}),
|
|
1975
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
1976
|
-
// TODO: do we really need constant cmov?
|
|
1977
|
-
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
1978
|
-
cmov: (a, b, c) => c ? b : a,
|
|
1979
|
-
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
1980
|
-
fromBytes: (bytes) => {
|
|
1981
|
-
if (bytes.length !== BYTES)
|
|
1982
|
-
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
1983
|
-
return isLE ? bytesToNumberLE2(bytes) : bytesToNumberBE(bytes);
|
|
1984
|
-
}
|
|
1985
|
-
});
|
|
1986
|
-
return Object.freeze(f);
|
|
1987
|
-
}
|
|
1988
|
-
function getFieldBytesLength(fieldOrder) {
|
|
1989
|
-
if (typeof fieldOrder !== "bigint")
|
|
1990
|
-
throw new Error("field order must be bigint");
|
|
1991
|
-
const bitLength = fieldOrder.toString(2).length;
|
|
1992
|
-
return Math.ceil(bitLength / 8);
|
|
1993
|
-
}
|
|
1994
|
-
function getMinHashLength(fieldOrder) {
|
|
1995
|
-
const length = getFieldBytesLength(fieldOrder);
|
|
1996
|
-
return length + Math.ceil(length / 2);
|
|
1997
|
-
}
|
|
1998
|
-
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
1999
|
-
const len = key.length;
|
|
2000
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
2001
|
-
const minLen = getMinHashLength(fieldOrder);
|
|
2002
|
-
if (len < 16 || len < minLen || len > 1024)
|
|
2003
|
-
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
2004
|
-
const num = isLE ? bytesToNumberBE(key) : bytesToNumberLE2(key);
|
|
2005
|
-
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
2006
|
-
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
2007
|
-
}
|
|
2008
|
-
|
|
2009
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/curve.js
|
|
2010
|
-
var _0n3 = BigInt(0);
|
|
2011
|
-
var _1n3 = BigInt(1);
|
|
2012
|
-
function constTimeNegate(condition, item) {
|
|
2013
|
-
const neg = item.negate();
|
|
2014
|
-
return condition ? neg : item;
|
|
2015
|
-
}
|
|
2016
|
-
function validateW(W2, bits) {
|
|
2017
|
-
if (!Number.isSafeInteger(W2) || W2 <= 0 || W2 > bits)
|
|
2018
|
-
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W2);
|
|
2019
|
-
}
|
|
2020
|
-
function calcWOpts(W2, bits) {
|
|
2021
|
-
validateW(W2, bits);
|
|
2022
|
-
const windows = Math.ceil(bits / W2) + 1;
|
|
2023
|
-
const windowSize = 2 ** (W2 - 1);
|
|
2024
|
-
return { windows, windowSize };
|
|
2025
|
-
}
|
|
2026
|
-
function validateMSMPoints(points, c) {
|
|
2027
|
-
if (!Array.isArray(points))
|
|
2028
|
-
throw new Error("array expected");
|
|
2029
|
-
points.forEach((p, i) => {
|
|
2030
|
-
if (!(p instanceof c))
|
|
2031
|
-
throw new Error("invalid point at index " + i);
|
|
2032
|
-
});
|
|
2033
|
-
}
|
|
2034
|
-
function validateMSMScalars(scalars, field) {
|
|
2035
|
-
if (!Array.isArray(scalars))
|
|
2036
|
-
throw new Error("array of scalars expected");
|
|
2037
|
-
scalars.forEach((s, i) => {
|
|
2038
|
-
if (!field.isValid(s))
|
|
2039
|
-
throw new Error("invalid scalar at index " + i);
|
|
2040
|
-
});
|
|
2041
|
-
}
|
|
2042
|
-
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
2043
|
-
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
2044
|
-
function getW(P2) {
|
|
2045
|
-
return pointWindowSizes.get(P2) || 1;
|
|
2046
|
-
}
|
|
2047
|
-
function wNAF2(c, bits) {
|
|
2048
|
-
return {
|
|
2049
|
-
constTimeNegate,
|
|
2050
|
-
hasPrecomputes(elm) {
|
|
2051
|
-
return getW(elm) !== 1;
|
|
2052
|
-
},
|
|
2053
|
-
// non-const time multiplication ladder
|
|
2054
|
-
unsafeLadder(elm, n, p = c.ZERO) {
|
|
2055
|
-
let d = elm;
|
|
2056
|
-
while (n > _0n3) {
|
|
2057
|
-
if (n & _1n3)
|
|
2058
|
-
p = p.add(d);
|
|
2059
|
-
d = d.double();
|
|
2060
|
-
n >>= _1n3;
|
|
2061
|
-
}
|
|
2062
|
-
return p;
|
|
2063
|
-
},
|
|
2064
|
-
/**
|
|
2065
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
2066
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2067
|
-
* Number of precomputed points depends on the curve size:
|
|
2068
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2069
|
-
* - 𝑊 is the window size
|
|
2070
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
2071
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2072
|
-
* @param elm Point instance
|
|
2073
|
-
* @param W window size
|
|
2074
|
-
* @returns precomputed point tables flattened to a single array
|
|
2075
|
-
*/
|
|
2076
|
-
precomputeWindow(elm, W2) {
|
|
2077
|
-
const { windows, windowSize } = calcWOpts(W2, bits);
|
|
2078
|
-
const points = [];
|
|
2079
|
-
let p = elm;
|
|
2080
|
-
let base = p;
|
|
2081
|
-
for (let window = 0; window < windows; window++) {
|
|
2082
|
-
base = p;
|
|
2083
|
-
points.push(base);
|
|
2084
|
-
for (let i = 1; i < windowSize; i++) {
|
|
2085
|
-
base = base.add(p);
|
|
2086
|
-
points.push(base);
|
|
2087
|
-
}
|
|
2088
|
-
p = base.double();
|
|
2089
|
-
}
|
|
2090
|
-
return points;
|
|
2091
|
-
},
|
|
2092
|
-
/**
|
|
2093
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2094
|
-
* @param W window size
|
|
2095
|
-
* @param precomputes precomputed tables
|
|
2096
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
2097
|
-
* @returns real and fake (for const-time) points
|
|
2098
|
-
*/
|
|
2099
|
-
wNAF(W2, precomputes, n) {
|
|
2100
|
-
const { windows, windowSize } = calcWOpts(W2, bits);
|
|
2101
|
-
let p = c.ZERO;
|
|
2102
|
-
let f = c.BASE;
|
|
2103
|
-
const mask = BigInt(2 ** W2 - 1);
|
|
2104
|
-
const maxNumber = 2 ** W2;
|
|
2105
|
-
const shiftBy = BigInt(W2);
|
|
2106
|
-
for (let window = 0; window < windows; window++) {
|
|
2107
|
-
const offset = window * windowSize;
|
|
2108
|
-
let wbits = Number(n & mask);
|
|
2109
|
-
n >>= shiftBy;
|
|
2110
|
-
if (wbits > windowSize) {
|
|
2111
|
-
wbits -= maxNumber;
|
|
2112
|
-
n += _1n3;
|
|
2113
|
-
}
|
|
2114
|
-
const offset1 = offset;
|
|
2115
|
-
const offset2 = offset + Math.abs(wbits) - 1;
|
|
2116
|
-
const cond1 = window % 2 !== 0;
|
|
2117
|
-
const cond2 = wbits < 0;
|
|
2118
|
-
if (wbits === 0) {
|
|
2119
|
-
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
2120
|
-
} else {
|
|
2121
|
-
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
2122
|
-
}
|
|
2123
|
-
}
|
|
2124
|
-
return { p, f };
|
|
2125
|
-
},
|
|
2126
|
-
/**
|
|
2127
|
-
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2128
|
-
* @param W window size
|
|
2129
|
-
* @param precomputes precomputed tables
|
|
2130
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
2131
|
-
* @param acc accumulator point to add result of multiplication
|
|
2132
|
-
* @returns point
|
|
2133
|
-
*/
|
|
2134
|
-
wNAFUnsafe(W2, precomputes, n, acc = c.ZERO) {
|
|
2135
|
-
const { windows, windowSize } = calcWOpts(W2, bits);
|
|
2136
|
-
const mask = BigInt(2 ** W2 - 1);
|
|
2137
|
-
const maxNumber = 2 ** W2;
|
|
2138
|
-
const shiftBy = BigInt(W2);
|
|
2139
|
-
for (let window = 0; window < windows; window++) {
|
|
2140
|
-
const offset = window * windowSize;
|
|
2141
|
-
if (n === _0n3)
|
|
2142
|
-
break;
|
|
2143
|
-
let wbits = Number(n & mask);
|
|
2144
|
-
n >>= shiftBy;
|
|
2145
|
-
if (wbits > windowSize) {
|
|
2146
|
-
wbits -= maxNumber;
|
|
2147
|
-
n += _1n3;
|
|
2148
|
-
}
|
|
2149
|
-
if (wbits === 0)
|
|
2150
|
-
continue;
|
|
2151
|
-
let curr = precomputes[offset + Math.abs(wbits) - 1];
|
|
2152
|
-
if (wbits < 0)
|
|
2153
|
-
curr = curr.negate();
|
|
2154
|
-
acc = acc.add(curr);
|
|
2155
|
-
}
|
|
2156
|
-
return acc;
|
|
2157
|
-
},
|
|
2158
|
-
getPrecomputes(W2, P2, transform) {
|
|
2159
|
-
let comp = pointPrecomputes.get(P2);
|
|
2160
|
-
if (!comp) {
|
|
2161
|
-
comp = this.precomputeWindow(P2, W2);
|
|
2162
|
-
if (W2 !== 1)
|
|
2163
|
-
pointPrecomputes.set(P2, transform(comp));
|
|
2164
|
-
}
|
|
2165
|
-
return comp;
|
|
2166
|
-
},
|
|
2167
|
-
wNAFCached(P2, n, transform) {
|
|
2168
|
-
const W2 = getW(P2);
|
|
2169
|
-
return this.wNAF(W2, this.getPrecomputes(W2, P2, transform), n);
|
|
2170
|
-
},
|
|
2171
|
-
wNAFCachedUnsafe(P2, n, transform, prev) {
|
|
2172
|
-
const W2 = getW(P2);
|
|
2173
|
-
if (W2 === 1)
|
|
2174
|
-
return this.unsafeLadder(P2, n, prev);
|
|
2175
|
-
return this.wNAFUnsafe(W2, this.getPrecomputes(W2, P2, transform), n, prev);
|
|
2176
|
-
},
|
|
2177
|
-
// We calculate precomputes for elliptic curve point multiplication
|
|
2178
|
-
// using windowed method. This specifies window size and
|
|
2179
|
-
// stores precomputed values. Usually only base point would be precomputed.
|
|
2180
|
-
setWindowSize(P2, W2) {
|
|
2181
|
-
validateW(W2, bits);
|
|
2182
|
-
pointWindowSizes.set(P2, W2);
|
|
2183
|
-
pointPrecomputes.delete(P2);
|
|
2184
|
-
}
|
|
2185
|
-
};
|
|
2186
|
-
}
|
|
2187
|
-
function pippenger(c, fieldN, points, scalars) {
|
|
2188
|
-
validateMSMPoints(points, c);
|
|
2189
|
-
validateMSMScalars(scalars, fieldN);
|
|
2190
|
-
if (points.length !== scalars.length)
|
|
2191
|
-
throw new Error("arrays of points and scalars must have equal length");
|
|
2192
|
-
const zero = c.ZERO;
|
|
2193
|
-
const wbits = bitLen(BigInt(points.length));
|
|
2194
|
-
const windowSize = wbits > 12 ? wbits - 3 : wbits > 4 ? wbits - 2 : wbits ? 2 : 1;
|
|
2195
|
-
const MASK = (1 << windowSize) - 1;
|
|
2196
|
-
const buckets = new Array(MASK + 1).fill(zero);
|
|
2197
|
-
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
2198
|
-
let sum = zero;
|
|
2199
|
-
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
2200
|
-
buckets.fill(zero);
|
|
2201
|
-
for (let j = 0; j < scalars.length; j++) {
|
|
2202
|
-
const scalar = scalars[j];
|
|
2203
|
-
const wbits2 = Number(scalar >> BigInt(i) & BigInt(MASK));
|
|
2204
|
-
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
2205
|
-
}
|
|
2206
|
-
let resI = zero;
|
|
2207
|
-
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
2208
|
-
sumI = sumI.add(buckets[j]);
|
|
2209
|
-
resI = resI.add(sumI);
|
|
2210
|
-
}
|
|
2211
|
-
sum = sum.add(resI);
|
|
2212
|
-
if (i !== 0)
|
|
2213
|
-
for (let j = 0; j < windowSize; j++)
|
|
2214
|
-
sum = sum.double();
|
|
2215
|
-
}
|
|
2216
|
-
return sum;
|
|
2217
|
-
}
|
|
2218
|
-
function validateBasic(curve) {
|
|
2219
|
-
validateField(curve.Fp);
|
|
2220
|
-
validateObject(curve, {
|
|
2221
|
-
n: "bigint",
|
|
2222
|
-
h: "bigint",
|
|
2223
|
-
Gx: "field",
|
|
2224
|
-
Gy: "field"
|
|
2225
|
-
}, {
|
|
2226
|
-
nBitLength: "isSafeInteger",
|
|
2227
|
-
nByteLength: "isSafeInteger"
|
|
2228
|
-
});
|
|
2229
|
-
return Object.freeze({
|
|
2230
|
-
...nLength(curve.n, curve.nBitLength),
|
|
2231
|
-
...curve,
|
|
2232
|
-
...{ p: curve.Fp.ORDER }
|
|
2233
|
-
});
|
|
2234
|
-
}
|
|
2235
|
-
|
|
2236
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/abstract/weierstrass.js
|
|
2237
|
-
function validateSigVerOpts(opts) {
|
|
2238
|
-
if (opts.lowS !== void 0)
|
|
2239
|
-
abool("lowS", opts.lowS);
|
|
2240
|
-
if (opts.prehash !== void 0)
|
|
2241
|
-
abool("prehash", opts.prehash);
|
|
2242
|
-
}
|
|
2243
|
-
function validatePointOpts(curve) {
|
|
2244
|
-
const opts = validateBasic(curve);
|
|
2245
|
-
validateObject(opts, {
|
|
2246
|
-
a: "field",
|
|
2247
|
-
b: "field"
|
|
2248
|
-
}, {
|
|
2249
|
-
allowedPrivateKeyLengths: "array",
|
|
2250
|
-
wrapPrivateKey: "boolean",
|
|
2251
|
-
isTorsionFree: "function",
|
|
2252
|
-
clearCofactor: "function",
|
|
2253
|
-
allowInfinityPoint: "boolean",
|
|
2254
|
-
fromBytes: "function",
|
|
2255
|
-
toBytes: "function"
|
|
2256
|
-
});
|
|
2257
|
-
const { endo, Fp, a } = opts;
|
|
2258
|
-
if (endo) {
|
|
2259
|
-
if (!Fp.eql(a, Fp.ZERO)) {
|
|
2260
|
-
throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");
|
|
116
|
+
return await ed.verifyAsync(signature, message, publicKey);
|
|
2261
117
|
}
|
|
2262
|
-
|
|
2263
|
-
|
|
118
|
+
catch {
|
|
119
|
+
return false;
|
|
2264
120
|
}
|
|
2265
|
-
}
|
|
2266
|
-
return Object.freeze({ ...opts });
|
|
2267
121
|
}
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
if (v.length !== length)
|
|
2324
|
-
throw new E("tlv.decode: wrong value length");
|
|
2325
|
-
return { v, l: data.subarray(pos + length) };
|
|
2326
|
-
}
|
|
2327
|
-
},
|
|
2328
|
-
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
2329
|
-
// since we always use positive integers here. It must always be empty:
|
|
2330
|
-
// - add zero byte if exists
|
|
2331
|
-
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2332
|
-
_int: {
|
|
2333
|
-
encode(num) {
|
|
2334
|
-
const { Err: E } = DER;
|
|
2335
|
-
if (num < _0n4)
|
|
2336
|
-
throw new E("integer: negative integers are not allowed");
|
|
2337
|
-
let hex = numberToHexUnpadded(num);
|
|
2338
|
-
if (Number.parseInt(hex[0], 16) & 8)
|
|
2339
|
-
hex = "00" + hex;
|
|
2340
|
-
if (hex.length & 1)
|
|
2341
|
-
throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
2342
|
-
return hex;
|
|
2343
|
-
},
|
|
2344
|
-
decode(data) {
|
|
2345
|
-
const { Err: E } = DER;
|
|
2346
|
-
if (data[0] & 128)
|
|
2347
|
-
throw new E("invalid signature integer: negative");
|
|
2348
|
-
if (data[0] === 0 && !(data[1] & 128))
|
|
2349
|
-
throw new E("invalid signature integer: unnecessary leading zero");
|
|
2350
|
-
return b2n(data);
|
|
2351
|
-
}
|
|
2352
|
-
},
|
|
2353
|
-
toSig(hex) {
|
|
2354
|
-
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
2355
|
-
const data = typeof hex === "string" ? h2b(hex) : hex;
|
|
2356
|
-
abytes4(data);
|
|
2357
|
-
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
2358
|
-
if (seqLeftBytes.length)
|
|
2359
|
-
throw new E("invalid signature: left bytes after parsing");
|
|
2360
|
-
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
2361
|
-
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
2362
|
-
if (sLeftBytes.length)
|
|
2363
|
-
throw new E("invalid signature: left bytes after parsing");
|
|
2364
|
-
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
2365
|
-
},
|
|
2366
|
-
hexFromSig(sig) {
|
|
2367
|
-
const { _tlv: tlv, _int: int } = DER;
|
|
2368
|
-
const rs = tlv.encode(2, int.encode(sig.r));
|
|
2369
|
-
const ss = tlv.encode(2, int.encode(sig.s));
|
|
2370
|
-
const seq = rs + ss;
|
|
2371
|
-
return tlv.encode(48, seq);
|
|
2372
|
-
}
|
|
2373
|
-
};
|
|
2374
|
-
var _0n4 = BigInt(0);
|
|
2375
|
-
var _1n4 = BigInt(1);
|
|
2376
|
-
var _2n3 = BigInt(2);
|
|
2377
|
-
var _3n2 = BigInt(3);
|
|
2378
|
-
var _4n2 = BigInt(4);
|
|
2379
|
-
function weierstrassPoints(opts) {
|
|
2380
|
-
const CURVE = validatePointOpts(opts);
|
|
2381
|
-
const { Fp } = CURVE;
|
|
2382
|
-
const Fn = Field(CURVE.n, CURVE.nBitLength);
|
|
2383
|
-
const toBytes3 = CURVE.toBytes || ((_c, point, _isCompressed) => {
|
|
2384
|
-
const a = point.toAffine();
|
|
2385
|
-
return concatBytes3(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
|
2386
|
-
});
|
|
2387
|
-
const fromBytes = CURVE.fromBytes || ((bytes) => {
|
|
2388
|
-
const tail = bytes.subarray(1);
|
|
2389
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
2390
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
2391
|
-
return { x, y };
|
|
2392
|
-
});
|
|
2393
|
-
function weierstrassEquation(x) {
|
|
2394
|
-
const { a, b } = CURVE;
|
|
2395
|
-
const x2 = Fp.sqr(x);
|
|
2396
|
-
const x3 = Fp.mul(x2, x);
|
|
2397
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
|
2398
|
-
}
|
|
2399
|
-
if (!Fp.eql(Fp.sqr(CURVE.Gy), weierstrassEquation(CURVE.Gx)))
|
|
2400
|
-
throw new Error("bad generator point: equation left != right");
|
|
2401
|
-
function isWithinCurveOrder(num) {
|
|
2402
|
-
return inRange(num, _1n4, CURVE.n);
|
|
2403
|
-
}
|
|
2404
|
-
function normPrivateKeyToScalar(key) {
|
|
2405
|
-
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N2 } = CURVE;
|
|
2406
|
-
if (lengths && typeof key !== "bigint") {
|
|
2407
|
-
if (isBytes4(key))
|
|
2408
|
-
key = bytesToHex2(key);
|
|
2409
|
-
if (typeof key !== "string" || !lengths.includes(key.length))
|
|
2410
|
-
throw new Error("invalid private key");
|
|
2411
|
-
key = key.padStart(nByteLength * 2, "0");
|
|
2412
|
-
}
|
|
2413
|
-
let num;
|
|
122
|
+
export async function generateEd25519Keypair() {
|
|
123
|
+
const { secretKey, publicKey } = await ed.keygenAsync();
|
|
124
|
+
return { publicKey, privateKey: secretKey };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Derive the matching public key from a private key, dispatched by suite.
|
|
128
|
+
*
|
|
129
|
+
* For Ed25519 suites this is deterministic seed expansion (hash the seed,
|
|
130
|
+
* derive the curve point). Callers that have a private key in hand and
|
|
131
|
+
* need the public key — sovereign delegation paths, identity bootstrap
|
|
132
|
+
* after a seed import, recovery flows — go through here so the noble
|
|
133
|
+
* call doesn't escape the dispatcher.
|
|
134
|
+
*
|
|
135
|
+
* Throws on unknown or unsupported suite (fail-closed). PQ arms will
|
|
136
|
+
* land alongside their `verifyBySuite` / `signBySuite` counterparts.
|
|
137
|
+
*/
|
|
138
|
+
export async function getPublicKeyBySuite(privateKey, suite) {
|
|
139
|
+
switch (suite) {
|
|
140
|
+
case "motebit-jcs-ed25519-b64-v1":
|
|
141
|
+
case "motebit-jcs-ed25519-hex-v1":
|
|
142
|
+
case "motebit-jwt-ed25519-v1":
|
|
143
|
+
case "motebit-concat-ed25519-hex-v1":
|
|
144
|
+
case "eddsa-jcs-2022":
|
|
145
|
+
return ed.getPublicKeyAsync(privateKey);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// ── P-256 ECDSA — hardware-attestation receipts ──────────────────────
|
|
149
|
+
//
|
|
150
|
+
// Apple Secure Enclave generates P-256 keys and produces ECDSA-SHA256
|
|
151
|
+
// signatures over attestation-receipt bytes. The receipt is OPAQUE to
|
|
152
|
+
// motebit's core suite system (it's a side-channel platform blob per
|
|
153
|
+
// `HardwareAttestationClaim.attestation_receipt`'s schema doc) — the
|
|
154
|
+
// verification primitive still lives here so the single-home-for-
|
|
155
|
+
// primitives rule stays intact.
|
|
156
|
+
//
|
|
157
|
+
// Inputs are already-decoded bytes, matching the shape `verifyBySuite`
|
|
158
|
+
// uses. The SE public key is P-256 compressed-point hex (33 bytes
|
|
159
|
+
// decoded); the signature is ECDSA DER-encoded (as the SE emits).
|
|
160
|
+
/**
|
|
161
|
+
* Verify a P-256 ECDSA-SHA256 signature.
|
|
162
|
+
*
|
|
163
|
+
* - `publicKeyCompressedHex` — P-256 public key in compressed-point
|
|
164
|
+
* hex encoding (33 bytes, `02`/`03` prefix). Uncompressed keys
|
|
165
|
+
* (65 bytes, `04` prefix) also accepted — noble handles both.
|
|
166
|
+
* - `messageBytes` — the bytes that were signed. noble internally
|
|
167
|
+
* SHA-256 hashes before verification, so callers pass the
|
|
168
|
+
* un-pre-hashed payload.
|
|
169
|
+
* - `signatureDerBytes` — DER-encoded ECDSA signature as emitted by
|
|
170
|
+
* Apple SE / Security.framework.
|
|
171
|
+
*
|
|
172
|
+
* Returns `false` on any failure (bad key, bad DER, bad signature,
|
|
173
|
+
* mismatch). Never throws — matches the `verifyBySuite` contract so
|
|
174
|
+
* callers don't need a try/catch.
|
|
175
|
+
*/
|
|
176
|
+
export function verifyP256EcdsaSha256(publicKeyCompressedHex, messageBytes, signatureDerBytes) {
|
|
2414
177
|
try {
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
}
|
|
2419
|
-
if (wrapPrivateKey)
|
|
2420
|
-
num = mod(num, N2);
|
|
2421
|
-
aInRange("private key", num, _1n4, N2);
|
|
2422
|
-
return num;
|
|
2423
|
-
}
|
|
2424
|
-
function assertPrjPoint(other) {
|
|
2425
|
-
if (!(other instanceof Point2))
|
|
2426
|
-
throw new Error("ProjectivePoint expected");
|
|
2427
|
-
}
|
|
2428
|
-
const toAffineMemo = memoized((p, iz) => {
|
|
2429
|
-
const { px: x, py: y, pz: z } = p;
|
|
2430
|
-
if (Fp.eql(z, Fp.ONE))
|
|
2431
|
-
return { x, y };
|
|
2432
|
-
const is0 = p.is0();
|
|
2433
|
-
if (iz == null)
|
|
2434
|
-
iz = is0 ? Fp.ONE : Fp.inv(z);
|
|
2435
|
-
const ax = Fp.mul(x, iz);
|
|
2436
|
-
const ay = Fp.mul(y, iz);
|
|
2437
|
-
const zz = Fp.mul(z, iz);
|
|
2438
|
-
if (is0)
|
|
2439
|
-
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
2440
|
-
if (!Fp.eql(zz, Fp.ONE))
|
|
2441
|
-
throw new Error("invZ was invalid");
|
|
2442
|
-
return { x: ax, y: ay };
|
|
2443
|
-
});
|
|
2444
|
-
const assertValidMemo = memoized((p) => {
|
|
2445
|
-
if (p.is0()) {
|
|
2446
|
-
if (CURVE.allowInfinityPoint && !Fp.is0(p.py))
|
|
2447
|
-
return;
|
|
2448
|
-
throw new Error("bad point: ZERO");
|
|
2449
|
-
}
|
|
2450
|
-
const { x, y } = p.toAffine();
|
|
2451
|
-
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
2452
|
-
throw new Error("bad point: x or y not FE");
|
|
2453
|
-
const left = Fp.sqr(y);
|
|
2454
|
-
const right = weierstrassEquation(x);
|
|
2455
|
-
if (!Fp.eql(left, right))
|
|
2456
|
-
throw new Error("bad point: equation left != right");
|
|
2457
|
-
if (!p.isTorsionFree())
|
|
2458
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
2459
|
-
return true;
|
|
2460
|
-
});
|
|
2461
|
-
class Point2 {
|
|
2462
|
-
constructor(px, py, pz) {
|
|
2463
|
-
this.px = px;
|
|
2464
|
-
this.py = py;
|
|
2465
|
-
this.pz = pz;
|
|
2466
|
-
if (px == null || !Fp.isValid(px))
|
|
2467
|
-
throw new Error("x required");
|
|
2468
|
-
if (py == null || !Fp.isValid(py))
|
|
2469
|
-
throw new Error("y required");
|
|
2470
|
-
if (pz == null || !Fp.isValid(pz))
|
|
2471
|
-
throw new Error("z required");
|
|
2472
|
-
Object.freeze(this);
|
|
2473
|
-
}
|
|
2474
|
-
// Does not validate if the point is on-curve.
|
|
2475
|
-
// Use fromHex instead, or call assertValidity() later.
|
|
2476
|
-
static fromAffine(p) {
|
|
2477
|
-
const { x, y } = p || {};
|
|
2478
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
2479
|
-
throw new Error("invalid affine point");
|
|
2480
|
-
if (p instanceof Point2)
|
|
2481
|
-
throw new Error("projective point not allowed");
|
|
2482
|
-
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
|
2483
|
-
if (is0(x) && is0(y))
|
|
2484
|
-
return Point2.ZERO;
|
|
2485
|
-
return new Point2(x, y, Fp.ONE);
|
|
2486
|
-
}
|
|
2487
|
-
get x() {
|
|
2488
|
-
return this.toAffine().x;
|
|
2489
|
-
}
|
|
2490
|
-
get y() {
|
|
2491
|
-
return this.toAffine().y;
|
|
2492
|
-
}
|
|
2493
|
-
/**
|
|
2494
|
-
* Takes a bunch of Projective Points but executes only one
|
|
2495
|
-
* inversion on all of them. Inversion is very slow operation,
|
|
2496
|
-
* so this improves performance massively.
|
|
2497
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
2498
|
-
*/
|
|
2499
|
-
static normalizeZ(points) {
|
|
2500
|
-
const toInv = Fp.invertBatch(points.map((p) => p.pz));
|
|
2501
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point2.fromAffine);
|
|
2502
|
-
}
|
|
2503
|
-
/**
|
|
2504
|
-
* Converts hash string or Uint8Array to Point.
|
|
2505
|
-
* @param hex short/long ECDSA hex
|
|
2506
|
-
*/
|
|
2507
|
-
static fromHex(hex) {
|
|
2508
|
-
const P2 = Point2.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
|
2509
|
-
P2.assertValidity();
|
|
2510
|
-
return P2;
|
|
2511
|
-
}
|
|
2512
|
-
// Multiplies generator point by privateKey.
|
|
2513
|
-
static fromPrivateKey(privateKey) {
|
|
2514
|
-
return Point2.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
|
2515
|
-
}
|
|
2516
|
-
// Multiscalar Multiplication
|
|
2517
|
-
static msm(points, scalars) {
|
|
2518
|
-
return pippenger(Point2, Fn, points, scalars);
|
|
2519
|
-
}
|
|
2520
|
-
// "Private method", don't use it directly
|
|
2521
|
-
_setWindowSize(windowSize) {
|
|
2522
|
-
wnaf.setWindowSize(this, windowSize);
|
|
2523
|
-
}
|
|
2524
|
-
// A point on curve is valid if it conforms to equation.
|
|
2525
|
-
assertValidity() {
|
|
2526
|
-
assertValidMemo(this);
|
|
2527
|
-
}
|
|
2528
|
-
hasEvenY() {
|
|
2529
|
-
const { y } = this.toAffine();
|
|
2530
|
-
if (Fp.isOdd)
|
|
2531
|
-
return !Fp.isOdd(y);
|
|
2532
|
-
throw new Error("Field doesn't support isOdd");
|
|
2533
|
-
}
|
|
2534
|
-
/**
|
|
2535
|
-
* Compare one point to another.
|
|
2536
|
-
*/
|
|
2537
|
-
equals(other) {
|
|
2538
|
-
assertPrjPoint(other);
|
|
2539
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
2540
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
2541
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
2542
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
2543
|
-
return U1 && U2;
|
|
2544
|
-
}
|
|
2545
|
-
/**
|
|
2546
|
-
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
|
2547
|
-
*/
|
|
2548
|
-
negate() {
|
|
2549
|
-
return new Point2(this.px, Fp.neg(this.py), this.pz);
|
|
2550
|
-
}
|
|
2551
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
2552
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2553
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2554
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2555
|
-
double() {
|
|
2556
|
-
const { a, b } = CURVE;
|
|
2557
|
-
const b3 = Fp.mul(b, _3n2);
|
|
2558
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
2559
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2560
|
-
let t0 = Fp.mul(X1, X1);
|
|
2561
|
-
let t1 = Fp.mul(Y1, Y1);
|
|
2562
|
-
let t2 = Fp.mul(Z1, Z1);
|
|
2563
|
-
let t3 = Fp.mul(X1, Y1);
|
|
2564
|
-
t3 = Fp.add(t3, t3);
|
|
2565
|
-
Z3 = Fp.mul(X1, Z1);
|
|
2566
|
-
Z3 = Fp.add(Z3, Z3);
|
|
2567
|
-
X3 = Fp.mul(a, Z3);
|
|
2568
|
-
Y3 = Fp.mul(b3, t2);
|
|
2569
|
-
Y3 = Fp.add(X3, Y3);
|
|
2570
|
-
X3 = Fp.sub(t1, Y3);
|
|
2571
|
-
Y3 = Fp.add(t1, Y3);
|
|
2572
|
-
Y3 = Fp.mul(X3, Y3);
|
|
2573
|
-
X3 = Fp.mul(t3, X3);
|
|
2574
|
-
Z3 = Fp.mul(b3, Z3);
|
|
2575
|
-
t2 = Fp.mul(a, t2);
|
|
2576
|
-
t3 = Fp.sub(t0, t2);
|
|
2577
|
-
t3 = Fp.mul(a, t3);
|
|
2578
|
-
t3 = Fp.add(t3, Z3);
|
|
2579
|
-
Z3 = Fp.add(t0, t0);
|
|
2580
|
-
t0 = Fp.add(Z3, t0);
|
|
2581
|
-
t0 = Fp.add(t0, t2);
|
|
2582
|
-
t0 = Fp.mul(t0, t3);
|
|
2583
|
-
Y3 = Fp.add(Y3, t0);
|
|
2584
|
-
t2 = Fp.mul(Y1, Z1);
|
|
2585
|
-
t2 = Fp.add(t2, t2);
|
|
2586
|
-
t0 = Fp.mul(t2, t3);
|
|
2587
|
-
X3 = Fp.sub(X3, t0);
|
|
2588
|
-
Z3 = Fp.mul(t2, t1);
|
|
2589
|
-
Z3 = Fp.add(Z3, Z3);
|
|
2590
|
-
Z3 = Fp.add(Z3, Z3);
|
|
2591
|
-
return new Point2(X3, Y3, Z3);
|
|
2592
|
-
}
|
|
2593
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
2594
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2595
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
2596
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2597
|
-
add(other) {
|
|
2598
|
-
assertPrjPoint(other);
|
|
2599
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
|
2600
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
|
2601
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2602
|
-
const a = CURVE.a;
|
|
2603
|
-
const b3 = Fp.mul(CURVE.b, _3n2);
|
|
2604
|
-
let t0 = Fp.mul(X1, X2);
|
|
2605
|
-
let t1 = Fp.mul(Y1, Y2);
|
|
2606
|
-
let t2 = Fp.mul(Z1, Z2);
|
|
2607
|
-
let t3 = Fp.add(X1, Y1);
|
|
2608
|
-
let t4 = Fp.add(X2, Y2);
|
|
2609
|
-
t3 = Fp.mul(t3, t4);
|
|
2610
|
-
t4 = Fp.add(t0, t1);
|
|
2611
|
-
t3 = Fp.sub(t3, t4);
|
|
2612
|
-
t4 = Fp.add(X1, Z1);
|
|
2613
|
-
let t5 = Fp.add(X2, Z2);
|
|
2614
|
-
t4 = Fp.mul(t4, t5);
|
|
2615
|
-
t5 = Fp.add(t0, t2);
|
|
2616
|
-
t4 = Fp.sub(t4, t5);
|
|
2617
|
-
t5 = Fp.add(Y1, Z1);
|
|
2618
|
-
X3 = Fp.add(Y2, Z2);
|
|
2619
|
-
t5 = Fp.mul(t5, X3);
|
|
2620
|
-
X3 = Fp.add(t1, t2);
|
|
2621
|
-
t5 = Fp.sub(t5, X3);
|
|
2622
|
-
Z3 = Fp.mul(a, t4);
|
|
2623
|
-
X3 = Fp.mul(b3, t2);
|
|
2624
|
-
Z3 = Fp.add(X3, Z3);
|
|
2625
|
-
X3 = Fp.sub(t1, Z3);
|
|
2626
|
-
Z3 = Fp.add(t1, Z3);
|
|
2627
|
-
Y3 = Fp.mul(X3, Z3);
|
|
2628
|
-
t1 = Fp.add(t0, t0);
|
|
2629
|
-
t1 = Fp.add(t1, t0);
|
|
2630
|
-
t2 = Fp.mul(a, t2);
|
|
2631
|
-
t4 = Fp.mul(b3, t4);
|
|
2632
|
-
t1 = Fp.add(t1, t2);
|
|
2633
|
-
t2 = Fp.sub(t0, t2);
|
|
2634
|
-
t2 = Fp.mul(a, t2);
|
|
2635
|
-
t4 = Fp.add(t4, t2);
|
|
2636
|
-
t0 = Fp.mul(t1, t4);
|
|
2637
|
-
Y3 = Fp.add(Y3, t0);
|
|
2638
|
-
t0 = Fp.mul(t5, t4);
|
|
2639
|
-
X3 = Fp.mul(t3, X3);
|
|
2640
|
-
X3 = Fp.sub(X3, t0);
|
|
2641
|
-
t0 = Fp.mul(t3, t1);
|
|
2642
|
-
Z3 = Fp.mul(t5, Z3);
|
|
2643
|
-
Z3 = Fp.add(Z3, t0);
|
|
2644
|
-
return new Point2(X3, Y3, Z3);
|
|
2645
|
-
}
|
|
2646
|
-
subtract(other) {
|
|
2647
|
-
return this.add(other.negate());
|
|
2648
|
-
}
|
|
2649
|
-
is0() {
|
|
2650
|
-
return this.equals(Point2.ZERO);
|
|
2651
|
-
}
|
|
2652
|
-
wNAF(n) {
|
|
2653
|
-
return wnaf.wNAFCached(this, n, Point2.normalizeZ);
|
|
178
|
+
const digest = sha256(messageBytes);
|
|
179
|
+
const pubKeyBytes = hexToBytes(publicKeyCompressedHex);
|
|
180
|
+
return p256.verify(signatureDerBytes, digest, pubKeyBytes, { prehash: false });
|
|
2654
181
|
}
|
|
2655
|
-
|
|
2656
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
2657
|
-
* It's faster, but should only be used when you don't care about
|
|
2658
|
-
* an exposed private key e.g. sig verification, which works over *public* keys.
|
|
2659
|
-
*/
|
|
2660
|
-
multiplyUnsafe(sc) {
|
|
2661
|
-
const { endo, n: N2 } = CURVE;
|
|
2662
|
-
aInRange("scalar", sc, _0n4, N2);
|
|
2663
|
-
const I2 = Point2.ZERO;
|
|
2664
|
-
if (sc === _0n4)
|
|
2665
|
-
return I2;
|
|
2666
|
-
if (this.is0() || sc === _1n4)
|
|
2667
|
-
return this;
|
|
2668
|
-
if (!endo || wnaf.hasPrecomputes(this))
|
|
2669
|
-
return wnaf.wNAFCachedUnsafe(this, sc, Point2.normalizeZ);
|
|
2670
|
-
let { k1neg, k1, k2neg, k2 } = endo.splitScalar(sc);
|
|
2671
|
-
let k1p = I2;
|
|
2672
|
-
let k2p = I2;
|
|
2673
|
-
let d = this;
|
|
2674
|
-
while (k1 > _0n4 || k2 > _0n4) {
|
|
2675
|
-
if (k1 & _1n4)
|
|
2676
|
-
k1p = k1p.add(d);
|
|
2677
|
-
if (k2 & _1n4)
|
|
2678
|
-
k2p = k2p.add(d);
|
|
2679
|
-
d = d.double();
|
|
2680
|
-
k1 >>= _1n4;
|
|
2681
|
-
k2 >>= _1n4;
|
|
2682
|
-
}
|
|
2683
|
-
if (k1neg)
|
|
2684
|
-
k1p = k1p.negate();
|
|
2685
|
-
if (k2neg)
|
|
2686
|
-
k2p = k2p.negate();
|
|
2687
|
-
k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
2688
|
-
return k1p.add(k2p);
|
|
2689
|
-
}
|
|
2690
|
-
/**
|
|
2691
|
-
* Constant time multiplication.
|
|
2692
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
2693
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
2694
|
-
* Uses precomputes when available.
|
|
2695
|
-
* Uses endomorphism for Koblitz curves.
|
|
2696
|
-
* @param scalar by which the point would be multiplied
|
|
2697
|
-
* @returns New point
|
|
2698
|
-
*/
|
|
2699
|
-
multiply(scalar) {
|
|
2700
|
-
const { endo, n: N2 } = CURVE;
|
|
2701
|
-
aInRange("scalar", scalar, _1n4, N2);
|
|
2702
|
-
let point, fake;
|
|
2703
|
-
if (endo) {
|
|
2704
|
-
const { k1neg, k1, k2neg, k2 } = endo.splitScalar(scalar);
|
|
2705
|
-
let { p: k1p, f: f1p } = this.wNAF(k1);
|
|
2706
|
-
let { p: k2p, f: f2p } = this.wNAF(k2);
|
|
2707
|
-
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
|
2708
|
-
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
|
2709
|
-
k2p = new Point2(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
|
|
2710
|
-
point = k1p.add(k2p);
|
|
2711
|
-
fake = f1p.add(f2p);
|
|
2712
|
-
} else {
|
|
2713
|
-
const { p, f } = this.wNAF(scalar);
|
|
2714
|
-
point = p;
|
|
2715
|
-
fake = f;
|
|
2716
|
-
}
|
|
2717
|
-
return Point2.normalizeZ([point, fake])[0];
|
|
2718
|
-
}
|
|
2719
|
-
/**
|
|
2720
|
-
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
|
2721
|
-
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
|
2722
|
-
* The trick could be useful if both P and Q are not G (not in our case).
|
|
2723
|
-
* @returns non-zero affine point
|
|
2724
|
-
*/
|
|
2725
|
-
multiplyAndAddUnsafe(Q, a, b) {
|
|
2726
|
-
const G2 = Point2.BASE;
|
|
2727
|
-
const mul = (P2, a2) => a2 === _0n4 || a2 === _1n4 || !P2.equals(G2) ? P2.multiplyUnsafe(a2) : P2.multiply(a2);
|
|
2728
|
-
const sum = mul(this, a).add(mul(Q, b));
|
|
2729
|
-
return sum.is0() ? void 0 : sum;
|
|
2730
|
-
}
|
|
2731
|
-
// Converts Projective point to affine (x, y) coordinates.
|
|
2732
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
2733
|
-
// (x, y, z) ∋ (x=x/z, y=y/z)
|
|
2734
|
-
toAffine(iz) {
|
|
2735
|
-
return toAffineMemo(this, iz);
|
|
2736
|
-
}
|
|
2737
|
-
isTorsionFree() {
|
|
2738
|
-
const { h: cofactor, isTorsionFree } = CURVE;
|
|
2739
|
-
if (cofactor === _1n4)
|
|
2740
|
-
return true;
|
|
2741
|
-
if (isTorsionFree)
|
|
2742
|
-
return isTorsionFree(Point2, this);
|
|
2743
|
-
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
|
2744
|
-
}
|
|
2745
|
-
clearCofactor() {
|
|
2746
|
-
const { h: cofactor, clearCofactor } = CURVE;
|
|
2747
|
-
if (cofactor === _1n4)
|
|
2748
|
-
return this;
|
|
2749
|
-
if (clearCofactor)
|
|
2750
|
-
return clearCofactor(Point2, this);
|
|
2751
|
-
return this.multiplyUnsafe(CURVE.h);
|
|
2752
|
-
}
|
|
2753
|
-
toRawBytes(isCompressed = true) {
|
|
2754
|
-
abool("isCompressed", isCompressed);
|
|
2755
|
-
this.assertValidity();
|
|
2756
|
-
return toBytes3(Point2, this, isCompressed);
|
|
2757
|
-
}
|
|
2758
|
-
toHex(isCompressed = true) {
|
|
2759
|
-
abool("isCompressed", isCompressed);
|
|
2760
|
-
return bytesToHex2(this.toRawBytes(isCompressed));
|
|
2761
|
-
}
|
|
2762
|
-
}
|
|
2763
|
-
Point2.BASE = new Point2(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
2764
|
-
Point2.ZERO = new Point2(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
2765
|
-
const _bits = CURVE.nBitLength;
|
|
2766
|
-
const wnaf = wNAF2(Point2, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
|
|
2767
|
-
return {
|
|
2768
|
-
CURVE,
|
|
2769
|
-
ProjectivePoint: Point2,
|
|
2770
|
-
normPrivateKeyToScalar,
|
|
2771
|
-
weierstrassEquation,
|
|
2772
|
-
isWithinCurveOrder
|
|
2773
|
-
};
|
|
2774
|
-
}
|
|
2775
|
-
function validateOpts(curve) {
|
|
2776
|
-
const opts = validateBasic(curve);
|
|
2777
|
-
validateObject(opts, {
|
|
2778
|
-
hash: "hash",
|
|
2779
|
-
hmac: "function",
|
|
2780
|
-
randomBytes: "function"
|
|
2781
|
-
}, {
|
|
2782
|
-
bits2int: "function",
|
|
2783
|
-
bits2int_modN: "function",
|
|
2784
|
-
lowS: "boolean"
|
|
2785
|
-
});
|
|
2786
|
-
return Object.freeze({ lowS: true, ...opts });
|
|
2787
|
-
}
|
|
2788
|
-
function weierstrass(curveDef) {
|
|
2789
|
-
const CURVE = validateOpts(curveDef);
|
|
2790
|
-
const { Fp, n: CURVE_ORDER } = CURVE;
|
|
2791
|
-
const compressedLen = Fp.BYTES + 1;
|
|
2792
|
-
const uncompressedLen = 2 * Fp.BYTES + 1;
|
|
2793
|
-
function modN2(a) {
|
|
2794
|
-
return mod(a, CURVE_ORDER);
|
|
2795
|
-
}
|
|
2796
|
-
function invN(a) {
|
|
2797
|
-
return invert2(a, CURVE_ORDER);
|
|
2798
|
-
}
|
|
2799
|
-
const { ProjectivePoint: Point2, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
|
2800
|
-
...CURVE,
|
|
2801
|
-
toBytes(_c, point, isCompressed) {
|
|
2802
|
-
const a = point.toAffine();
|
|
2803
|
-
const x = Fp.toBytes(a.x);
|
|
2804
|
-
const cat = concatBytes3;
|
|
2805
|
-
abool("isCompressed", isCompressed);
|
|
2806
|
-
if (isCompressed) {
|
|
2807
|
-
return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
|
2808
|
-
} else {
|
|
2809
|
-
return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
|
2810
|
-
}
|
|
2811
|
-
},
|
|
2812
|
-
fromBytes(bytes) {
|
|
2813
|
-
const len = bytes.length;
|
|
2814
|
-
const head = bytes[0];
|
|
2815
|
-
const tail = bytes.subarray(1);
|
|
2816
|
-
if (len === compressedLen && (head === 2 || head === 3)) {
|
|
2817
|
-
const x = bytesToNumberBE(tail);
|
|
2818
|
-
if (!inRange(x, _1n4, Fp.ORDER))
|
|
2819
|
-
throw new Error("Point is not on curve");
|
|
2820
|
-
const y2 = weierstrassEquation(x);
|
|
2821
|
-
let y;
|
|
2822
|
-
try {
|
|
2823
|
-
y = Fp.sqrt(y2);
|
|
2824
|
-
} catch (sqrtError) {
|
|
2825
|
-
const suffix = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
2826
|
-
throw new Error("Point is not on curve" + suffix);
|
|
2827
|
-
}
|
|
2828
|
-
const isYOdd = (y & _1n4) === _1n4;
|
|
2829
|
-
const isHeadOdd = (head & 1) === 1;
|
|
2830
|
-
if (isHeadOdd !== isYOdd)
|
|
2831
|
-
y = Fp.neg(y);
|
|
2832
|
-
return { x, y };
|
|
2833
|
-
} else if (len === uncompressedLen && head === 4) {
|
|
2834
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
|
2835
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
2836
|
-
return { x, y };
|
|
2837
|
-
} else {
|
|
2838
|
-
const cl = compressedLen;
|
|
2839
|
-
const ul = uncompressedLen;
|
|
2840
|
-
throw new Error("invalid Point, expected length of " + cl + ", or uncompressed " + ul + ", got " + len);
|
|
2841
|
-
}
|
|
2842
|
-
}
|
|
2843
|
-
});
|
|
2844
|
-
const numToNByteStr = (num) => bytesToHex2(numberToBytesBE(num, CURVE.nByteLength));
|
|
2845
|
-
function isBiggerThanHalfOrder(number) {
|
|
2846
|
-
const HALF = CURVE_ORDER >> _1n4;
|
|
2847
|
-
return number > HALF;
|
|
2848
|
-
}
|
|
2849
|
-
function normalizeS(s) {
|
|
2850
|
-
return isBiggerThanHalfOrder(s) ? modN2(-s) : s;
|
|
2851
|
-
}
|
|
2852
|
-
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
|
2853
|
-
class Signature {
|
|
2854
|
-
constructor(r, s, recovery) {
|
|
2855
|
-
this.r = r;
|
|
2856
|
-
this.s = s;
|
|
2857
|
-
this.recovery = recovery;
|
|
2858
|
-
this.assertValidity();
|
|
2859
|
-
}
|
|
2860
|
-
// pair (bytes of r, bytes of s)
|
|
2861
|
-
static fromCompact(hex) {
|
|
2862
|
-
const l = CURVE.nByteLength;
|
|
2863
|
-
hex = ensureBytes("compactSignature", hex, l * 2);
|
|
2864
|
-
return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
|
2865
|
-
}
|
|
2866
|
-
// DER encoded ECDSA signature
|
|
2867
|
-
// https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
|
|
2868
|
-
static fromDER(hex) {
|
|
2869
|
-
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
|
2870
|
-
return new Signature(r, s);
|
|
2871
|
-
}
|
|
2872
|
-
assertValidity() {
|
|
2873
|
-
aInRange("r", this.r, _1n4, CURVE_ORDER);
|
|
2874
|
-
aInRange("s", this.s, _1n4, CURVE_ORDER);
|
|
2875
|
-
}
|
|
2876
|
-
addRecoveryBit(recovery) {
|
|
2877
|
-
return new Signature(this.r, this.s, recovery);
|
|
2878
|
-
}
|
|
2879
|
-
recoverPublicKey(msgHash) {
|
|
2880
|
-
const { r, s, recovery: rec } = this;
|
|
2881
|
-
const h2 = bits2int_modN(ensureBytes("msgHash", msgHash));
|
|
2882
|
-
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
|
2883
|
-
throw new Error("recovery id invalid");
|
|
2884
|
-
const radj = rec === 2 || rec === 3 ? r + CURVE.n : r;
|
|
2885
|
-
if (radj >= Fp.ORDER)
|
|
2886
|
-
throw new Error("recovery id 2 or 3 invalid");
|
|
2887
|
-
const prefix = (rec & 1) === 0 ? "02" : "03";
|
|
2888
|
-
const R = Point2.fromHex(prefix + numToNByteStr(radj));
|
|
2889
|
-
const ir = invN(radj);
|
|
2890
|
-
const u1 = modN2(-h2 * ir);
|
|
2891
|
-
const u2 = modN2(s * ir);
|
|
2892
|
-
const Q = Point2.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
|
2893
|
-
if (!Q)
|
|
2894
|
-
throw new Error("point at infinify");
|
|
2895
|
-
Q.assertValidity();
|
|
2896
|
-
return Q;
|
|
2897
|
-
}
|
|
2898
|
-
// Signatures should be low-s, to prevent malleability.
|
|
2899
|
-
hasHighS() {
|
|
2900
|
-
return isBiggerThanHalfOrder(this.s);
|
|
2901
|
-
}
|
|
2902
|
-
normalizeS() {
|
|
2903
|
-
return this.hasHighS() ? new Signature(this.r, modN2(-this.s), this.recovery) : this;
|
|
2904
|
-
}
|
|
2905
|
-
// DER-encoded
|
|
2906
|
-
toDERRawBytes() {
|
|
2907
|
-
return hexToBytes2(this.toDERHex());
|
|
2908
|
-
}
|
|
2909
|
-
toDERHex() {
|
|
2910
|
-
return DER.hexFromSig({ r: this.r, s: this.s });
|
|
2911
|
-
}
|
|
2912
|
-
// padded bytes of r, then padded bytes of s
|
|
2913
|
-
toCompactRawBytes() {
|
|
2914
|
-
return hexToBytes2(this.toCompactHex());
|
|
2915
|
-
}
|
|
2916
|
-
toCompactHex() {
|
|
2917
|
-
return numToNByteStr(this.r) + numToNByteStr(this.s);
|
|
2918
|
-
}
|
|
2919
|
-
}
|
|
2920
|
-
const utils = {
|
|
2921
|
-
isValidPrivateKey(privateKey) {
|
|
2922
|
-
try {
|
|
2923
|
-
normPrivateKeyToScalar(privateKey);
|
|
2924
|
-
return true;
|
|
2925
|
-
} catch (error) {
|
|
182
|
+
catch {
|
|
2926
183
|
return false;
|
|
2927
|
-
}
|
|
2928
|
-
},
|
|
2929
|
-
normPrivateKeyToScalar,
|
|
2930
|
-
/**
|
|
2931
|
-
* Produces cryptographically secure private key from random of size
|
|
2932
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
|
2933
|
-
*/
|
|
2934
|
-
randomPrivateKey: () => {
|
|
2935
|
-
const length = getMinHashLength(CURVE.n);
|
|
2936
|
-
return mapHashToField(CURVE.randomBytes(length), CURVE.n);
|
|
2937
|
-
},
|
|
2938
|
-
/**
|
|
2939
|
-
* Creates precompute table for an arbitrary EC point. Makes point "cached".
|
|
2940
|
-
* Allows to massively speed-up `point.multiply(scalar)`.
|
|
2941
|
-
* @returns cached point
|
|
2942
|
-
* @example
|
|
2943
|
-
* const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
|
|
2944
|
-
* fast.multiply(privKey); // much faster ECDH now
|
|
2945
|
-
*/
|
|
2946
|
-
precompute(windowSize = 8, point = Point2.BASE) {
|
|
2947
|
-
point._setWindowSize(windowSize);
|
|
2948
|
-
point.multiply(BigInt(3));
|
|
2949
|
-
return point;
|
|
2950
|
-
}
|
|
2951
|
-
};
|
|
2952
|
-
function getPublicKey(privateKey, isCompressed = true) {
|
|
2953
|
-
return Point2.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
|
2954
|
-
}
|
|
2955
|
-
function isProbPub(item) {
|
|
2956
|
-
const arr = isBytes4(item);
|
|
2957
|
-
const str = typeof item === "string";
|
|
2958
|
-
const len = (arr || str) && item.length;
|
|
2959
|
-
if (arr)
|
|
2960
|
-
return len === compressedLen || len === uncompressedLen;
|
|
2961
|
-
if (str)
|
|
2962
|
-
return len === 2 * compressedLen || len === 2 * uncompressedLen;
|
|
2963
|
-
if (item instanceof Point2)
|
|
2964
|
-
return true;
|
|
2965
|
-
return false;
|
|
2966
|
-
}
|
|
2967
|
-
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
|
2968
|
-
if (isProbPub(privateA))
|
|
2969
|
-
throw new Error("first arg must be private key");
|
|
2970
|
-
if (!isProbPub(publicB))
|
|
2971
|
-
throw new Error("second arg must be public key");
|
|
2972
|
-
const b = Point2.fromHex(publicB);
|
|
2973
|
-
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
|
2974
|
-
}
|
|
2975
|
-
const bits2int = CURVE.bits2int || function(bytes) {
|
|
2976
|
-
if (bytes.length > 8192)
|
|
2977
|
-
throw new Error("input is too large");
|
|
2978
|
-
const num = bytesToNumberBE(bytes);
|
|
2979
|
-
const delta = bytes.length * 8 - CURVE.nBitLength;
|
|
2980
|
-
return delta > 0 ? num >> BigInt(delta) : num;
|
|
2981
|
-
};
|
|
2982
|
-
const bits2int_modN = CURVE.bits2int_modN || function(bytes) {
|
|
2983
|
-
return modN2(bits2int(bytes));
|
|
2984
|
-
};
|
|
2985
|
-
const ORDER_MASK = bitMask(CURVE.nBitLength);
|
|
2986
|
-
function int2octets(num) {
|
|
2987
|
-
aInRange("num < 2^" + CURVE.nBitLength, num, _0n4, ORDER_MASK);
|
|
2988
|
-
return numberToBytesBE(num, CURVE.nByteLength);
|
|
2989
|
-
}
|
|
2990
|
-
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
|
2991
|
-
if (["recovered", "canonical"].some((k) => k in opts))
|
|
2992
|
-
throw new Error("sign() legacy options not supported");
|
|
2993
|
-
const { hash, randomBytes: randomBytes3 } = CURVE;
|
|
2994
|
-
let { lowS, prehash, extraEntropy: ent } = opts;
|
|
2995
|
-
if (lowS == null)
|
|
2996
|
-
lowS = true;
|
|
2997
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
2998
|
-
validateSigVerOpts(opts);
|
|
2999
|
-
if (prehash)
|
|
3000
|
-
msgHash = ensureBytes("prehashed msgHash", hash(msgHash));
|
|
3001
|
-
const h1int = bits2int_modN(msgHash);
|
|
3002
|
-
const d = normPrivateKeyToScalar(privateKey);
|
|
3003
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3004
|
-
if (ent != null && ent !== false) {
|
|
3005
|
-
const e = ent === true ? randomBytes3(Fp.BYTES) : ent;
|
|
3006
|
-
seedArgs.push(ensureBytes("extraEntropy", e));
|
|
3007
|
-
}
|
|
3008
|
-
const seed = concatBytes3(...seedArgs);
|
|
3009
|
-
const m = h1int;
|
|
3010
|
-
function k2sig(kBytes) {
|
|
3011
|
-
const k = bits2int(kBytes);
|
|
3012
|
-
if (!isWithinCurveOrder(k))
|
|
3013
|
-
return;
|
|
3014
|
-
const ik = invN(k);
|
|
3015
|
-
const q = Point2.BASE.multiply(k).toAffine();
|
|
3016
|
-
const r = modN2(q.x);
|
|
3017
|
-
if (r === _0n4)
|
|
3018
|
-
return;
|
|
3019
|
-
const s = modN2(ik * modN2(m + r * d));
|
|
3020
|
-
if (s === _0n4)
|
|
3021
|
-
return;
|
|
3022
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
3023
|
-
let normS = s;
|
|
3024
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3025
|
-
normS = normalizeS(s);
|
|
3026
|
-
recovery ^= 1;
|
|
3027
|
-
}
|
|
3028
|
-
return new Signature(r, normS, recovery);
|
|
3029
184
|
}
|
|
3030
|
-
return { seed, k2sig };
|
|
3031
|
-
}
|
|
3032
|
-
const defaultSigOpts = { lowS: CURVE.lowS, prehash: false };
|
|
3033
|
-
const defaultVerOpts = { lowS: CURVE.lowS, prehash: false };
|
|
3034
|
-
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
|
3035
|
-
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
|
3036
|
-
const C2 = CURVE;
|
|
3037
|
-
const drbg = createHmacDrbg(C2.hash.outputLen, C2.nByteLength, C2.hmac);
|
|
3038
|
-
return drbg(seed, k2sig);
|
|
3039
|
-
}
|
|
3040
|
-
Point2.BASE._setWindowSize(8);
|
|
3041
|
-
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
3042
|
-
const sg = signature;
|
|
3043
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
|
3044
|
-
publicKey = ensureBytes("publicKey", publicKey);
|
|
3045
|
-
const { lowS, prehash, format } = opts;
|
|
3046
|
-
validateSigVerOpts(opts);
|
|
3047
|
-
if ("strict" in opts)
|
|
3048
|
-
throw new Error("options.strict was renamed to lowS");
|
|
3049
|
-
if (format !== void 0 && format !== "compact" && format !== "der")
|
|
3050
|
-
throw new Error("format must be compact or der");
|
|
3051
|
-
const isHex = typeof sg === "string" || isBytes4(sg);
|
|
3052
|
-
const isObj = !isHex && !format && typeof sg === "object" && sg !== null && typeof sg.r === "bigint" && typeof sg.s === "bigint";
|
|
3053
|
-
if (!isHex && !isObj)
|
|
3054
|
-
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
3055
|
-
let _sig = void 0;
|
|
3056
|
-
let P2;
|
|
3057
|
-
try {
|
|
3058
|
-
if (isObj)
|
|
3059
|
-
_sig = new Signature(sg.r, sg.s);
|
|
3060
|
-
if (isHex) {
|
|
3061
|
-
try {
|
|
3062
|
-
if (format !== "compact")
|
|
3063
|
-
_sig = Signature.fromDER(sg);
|
|
3064
|
-
} catch (derError) {
|
|
3065
|
-
if (!(derError instanceof DER.Err))
|
|
3066
|
-
throw derError;
|
|
3067
|
-
}
|
|
3068
|
-
if (!_sig && format !== "der")
|
|
3069
|
-
_sig = Signature.fromCompact(sg);
|
|
3070
|
-
}
|
|
3071
|
-
P2 = Point2.fromHex(publicKey);
|
|
3072
|
-
} catch (error) {
|
|
3073
|
-
return false;
|
|
3074
|
-
}
|
|
3075
|
-
if (!_sig)
|
|
3076
|
-
return false;
|
|
3077
|
-
if (lowS && _sig.hasHighS())
|
|
3078
|
-
return false;
|
|
3079
|
-
if (prehash)
|
|
3080
|
-
msgHash = CURVE.hash(msgHash);
|
|
3081
|
-
const { r, s } = _sig;
|
|
3082
|
-
const h2 = bits2int_modN(msgHash);
|
|
3083
|
-
const is = invN(s);
|
|
3084
|
-
const u1 = modN2(h2 * is);
|
|
3085
|
-
const u2 = modN2(r * is);
|
|
3086
|
-
const R = Point2.BASE.multiplyAndAddUnsafe(P2, u1, u2)?.toAffine();
|
|
3087
|
-
if (!R)
|
|
3088
|
-
return false;
|
|
3089
|
-
const v = modN2(R.x);
|
|
3090
|
-
return v === r;
|
|
3091
|
-
}
|
|
3092
|
-
return {
|
|
3093
|
-
CURVE,
|
|
3094
|
-
getPublicKey,
|
|
3095
|
-
getSharedSecret,
|
|
3096
|
-
sign,
|
|
3097
|
-
verify,
|
|
3098
|
-
ProjectivePoint: Point2,
|
|
3099
|
-
Signature,
|
|
3100
|
-
utils
|
|
3101
|
-
};
|
|
3102
|
-
}
|
|
3103
|
-
|
|
3104
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/_shortw_utils.js
|
|
3105
|
-
function getHash(hash) {
|
|
3106
|
-
return {
|
|
3107
|
-
hash,
|
|
3108
|
-
hmac: (key, ...msgs) => hmac(hash, key, concatBytes2(...msgs)),
|
|
3109
|
-
randomBytes: randomBytes2
|
|
3110
|
-
};
|
|
3111
|
-
}
|
|
3112
|
-
function createCurve(curveDef, defHash) {
|
|
3113
|
-
const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
|
|
3114
|
-
return Object.freeze({ ...create(defHash), create });
|
|
3115
|
-
}
|
|
3116
|
-
|
|
3117
|
-
// ../../node_modules/.pnpm/@noble+curves@1.7.0/node_modules/@noble/curves/esm/p256.js
|
|
3118
|
-
var Fp256 = Field(BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"));
|
|
3119
|
-
var CURVE_A = Fp256.create(BigInt("-3"));
|
|
3120
|
-
var CURVE_B = BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
|
|
3121
|
-
var p256 = createCurve({
|
|
3122
|
-
a: CURVE_A,
|
|
3123
|
-
// Equation params: a, b
|
|
3124
|
-
b: CURVE_B,
|
|
3125
|
-
Fp: Fp256,
|
|
3126
|
-
// Field: 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
|
|
3127
|
-
// Curve order, total count of valid points in the field
|
|
3128
|
-
n: BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),
|
|
3129
|
-
// Base (generator) point (x, y)
|
|
3130
|
-
Gx: BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),
|
|
3131
|
-
Gy: BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),
|
|
3132
|
-
h: BigInt(1),
|
|
3133
|
-
lowS: false
|
|
3134
|
-
}, sha2562);
|
|
3135
|
-
|
|
3136
|
-
// src/suite-dispatch.ts
|
|
3137
|
-
if (!hashes.sha512) {
|
|
3138
|
-
hashes.sha512 = (msg) => sha512(msg);
|
|
3139
185
|
}
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
}
|
|
3153
|
-
}
|
|
3154
|
-
async function signBySuite(suite, canonicalBytes, privateKeyBytes) {
|
|
3155
|
-
switch (suite) {
|
|
3156
|
-
case "motebit-jcs-ed25519-b64-v1":
|
|
3157
|
-
case "motebit-jcs-ed25519-hex-v1":
|
|
3158
|
-
case "motebit-jwt-ed25519-v1":
|
|
3159
|
-
case "motebit-concat-ed25519-hex-v1":
|
|
3160
|
-
case "eddsa-jcs-2022":
|
|
3161
|
-
return signAsync(canonicalBytes, privateKeyBytes);
|
|
3162
|
-
}
|
|
3163
|
-
}
|
|
3164
|
-
async function ed25519Sign(message, privateKey) {
|
|
3165
|
-
return signAsync(message, privateKey);
|
|
3166
|
-
}
|
|
3167
|
-
async function ed25519Verify(signature, message, publicKey) {
|
|
3168
|
-
try {
|
|
3169
|
-
return await verifyAsync(signature, message, publicKey);
|
|
3170
|
-
} catch {
|
|
3171
|
-
return false;
|
|
3172
|
-
}
|
|
3173
|
-
}
|
|
3174
|
-
async function generateEd25519Keypair() {
|
|
3175
|
-
const { secretKey, publicKey } = await keygenAsync();
|
|
3176
|
-
return { publicKey, privateKey: secretKey };
|
|
3177
|
-
}
|
|
3178
|
-
async function getPublicKeyBySuite(privateKey, suite) {
|
|
3179
|
-
switch (suite) {
|
|
3180
|
-
case "motebit-jcs-ed25519-b64-v1":
|
|
3181
|
-
case "motebit-jcs-ed25519-hex-v1":
|
|
3182
|
-
case "motebit-jwt-ed25519-v1":
|
|
3183
|
-
case "motebit-concat-ed25519-hex-v1":
|
|
3184
|
-
case "eddsa-jcs-2022":
|
|
3185
|
-
return getPublicKeyAsync(privateKey);
|
|
3186
|
-
}
|
|
3187
|
-
}
|
|
3188
|
-
function verifyP256EcdsaSha256(publicKeyCompressedHex, messageBytes, signatureDerBytes) {
|
|
3189
|
-
try {
|
|
3190
|
-
const digest = sha256(messageBytes);
|
|
3191
|
-
const pubKeyBytes = hexToBytes3(publicKeyCompressedHex);
|
|
3192
|
-
return p256.verify(signatureDerBytes, digest, pubKeyBytes, { prehash: false });
|
|
3193
|
-
} catch {
|
|
3194
|
-
return false;
|
|
3195
|
-
}
|
|
3196
|
-
}
|
|
3197
|
-
function hexToBytes3(hex) {
|
|
3198
|
-
const clean = hex.startsWith("0x") || hex.startsWith("0X") ? hex.slice(2) : hex;
|
|
3199
|
-
if (clean.length % 2 !== 0) throw new Error("hex length must be even");
|
|
3200
|
-
const out = new Uint8Array(clean.length / 2);
|
|
3201
|
-
for (let i = 0; i < out.length; i++) {
|
|
3202
|
-
const byte = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
3203
|
-
if (Number.isNaN(byte)) throw new Error(`invalid hex at position ${i * 2}`);
|
|
3204
|
-
out[i] = byte;
|
|
3205
|
-
}
|
|
3206
|
-
return out;
|
|
186
|
+
function hexToBytes(hex) {
|
|
187
|
+
const clean = hex.startsWith("0x") || hex.startsWith("0X") ? hex.slice(2) : hex;
|
|
188
|
+
if (clean.length % 2 !== 0)
|
|
189
|
+
throw new Error("hex length must be even");
|
|
190
|
+
const out = new Uint8Array(clean.length / 2);
|
|
191
|
+
for (let i = 0; i < out.length; i++) {
|
|
192
|
+
const byte = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
193
|
+
if (Number.isNaN(byte))
|
|
194
|
+
throw new Error(`invalid hex at position ${i * 2}`);
|
|
195
|
+
out[i] = byte;
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
3207
198
|
}
|
|
3208
|
-
|
|
3209
|
-
ed25519Sign,
|
|
3210
|
-
ed25519Verify,
|
|
3211
|
-
generateEd25519Keypair,
|
|
3212
|
-
getPublicKeyBySuite,
|
|
3213
|
-
signBySuite,
|
|
3214
|
-
verifyBySuite,
|
|
3215
|
-
verifyP256EcdsaSha256
|
|
3216
|
-
};
|
|
3217
|
-
/*! Bundled license information:
|
|
3218
|
-
|
|
3219
|
-
@noble/ed25519/index.js:
|
|
3220
|
-
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
3221
|
-
|
|
3222
|
-
@noble/hashes/esm/utils.js:
|
|
3223
|
-
@noble/hashes/esm/utils.js:
|
|
3224
|
-
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
3225
|
-
|
|
3226
|
-
@noble/curves/esm/abstract/utils.js:
|
|
3227
|
-
@noble/curves/esm/abstract/modular.js:
|
|
3228
|
-
@noble/curves/esm/abstract/curve.js:
|
|
3229
|
-
@noble/curves/esm/abstract/weierstrass.js:
|
|
3230
|
-
@noble/curves/esm/_shortw_utils.js:
|
|
3231
|
-
@noble/curves/esm/p256.js:
|
|
3232
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
3233
|
-
*/
|
|
199
|
+
//# sourceMappingURL=suite-dispatch.js.map
|