@solana/webcrypto-ed25519-polyfill 2.0.0-experimental.fc4e943 → 2.0.0-experimental.fcff844
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/index.browser.cjs +595 -22
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +174 -21
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +563 -1392
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +174 -21
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +604 -19
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +179 -19
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +3 -18
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/secrets.d.ts +5 -4
- package/dist/types/secrets.d.ts.map +1 -0
- package/package.json +10 -9
|
@@ -1,1387 +1,420 @@
|
|
|
1
1
|
(function () {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
// ../../node_modules/.pnpm/@noble+
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
bytes(out);
|
|
19
|
-
const min = instance.outputLen;
|
|
20
|
-
if (out.length < min) {
|
|
21
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.3.2/node_modules/@noble/hashes/esm/crypto.js
|
|
26
|
-
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
27
|
-
|
|
28
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.3.2/node_modules/@noble/hashes/esm/utils.js
|
|
29
|
-
var u8a = (a) => a instanceof Uint8Array;
|
|
30
|
-
var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
31
|
-
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
32
|
-
if (!isLE)
|
|
33
|
-
throw new Error("Non little-endian hardware is not supported");
|
|
34
|
-
function utf8ToBytes(str) {
|
|
35
|
-
if (typeof str !== "string")
|
|
36
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
37
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
38
|
-
}
|
|
39
|
-
function toBytes(data) {
|
|
40
|
-
if (typeof data === "string")
|
|
41
|
-
data = utf8ToBytes(data);
|
|
42
|
-
if (!u8a(data))
|
|
43
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
44
|
-
return data;
|
|
45
|
-
}
|
|
46
|
-
var Hash = class {
|
|
47
|
-
// Safe version that clones internal state
|
|
48
|
-
clone() {
|
|
49
|
-
return this._cloneInto();
|
|
50
|
-
}
|
|
4
|
+
// ../../node_modules/.pnpm/@noble+ed25519@2.0.0/node_modules/@noble/ed25519/index.js
|
|
5
|
+
var P = 2n ** 255n - 19n;
|
|
6
|
+
var N = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
7
|
+
var Gx = 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an;
|
|
8
|
+
var Gy = 0x6666666666666666666666666666666666666666666666666666666666666658n;
|
|
9
|
+
var CURVE = {
|
|
10
|
+
a: -1n,
|
|
11
|
+
d: 37095705934669439343138083508754565189542113879843219016388785533085940283555n,
|
|
12
|
+
p: P,
|
|
13
|
+
n: N,
|
|
14
|
+
h: 8,
|
|
15
|
+
Gx,
|
|
16
|
+
Gy
|
|
17
|
+
// field prime, curve (group) order, cofactor
|
|
51
18
|
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const tmp = hashCons();
|
|
55
|
-
hashC.outputLen = tmp.outputLen;
|
|
56
|
-
hashC.blockLen = tmp.blockLen;
|
|
57
|
-
hashC.create = () => hashCons();
|
|
58
|
-
return hashC;
|
|
59
|
-
}
|
|
60
|
-
function randomBytes(bytesLength = 32) {
|
|
61
|
-
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
62
|
-
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
63
|
-
}
|
|
64
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ../../node_modules/.pnpm/@noble+hashes@1.3.2/node_modules/@noble/hashes/esm/_sha2.js
|
|
68
|
-
function setBigUint64(view, byteOffset, value, isLE2) {
|
|
69
|
-
if (typeof view.setBigUint64 === "function")
|
|
70
|
-
return view.setBigUint64(byteOffset, value, isLE2);
|
|
71
|
-
const _32n2 = BigInt(32);
|
|
72
|
-
const _u32_max = BigInt(4294967295);
|
|
73
|
-
const wh = Number(value >> _32n2 & _u32_max);
|
|
74
|
-
const wl = Number(value & _u32_max);
|
|
75
|
-
const h = isLE2 ? 4 : 0;
|
|
76
|
-
const l = isLE2 ? 0 : 4;
|
|
77
|
-
view.setUint32(byteOffset + h, wh, isLE2);
|
|
78
|
-
view.setUint32(byteOffset + l, wl, isLE2);
|
|
79
|
-
}
|
|
80
|
-
var SHA2 = class extends Hash {
|
|
81
|
-
constructor(blockLen, outputLen, padOffset, isLE2) {
|
|
82
|
-
super();
|
|
83
|
-
this.blockLen = blockLen;
|
|
84
|
-
this.outputLen = outputLen;
|
|
85
|
-
this.padOffset = padOffset;
|
|
86
|
-
this.isLE = isLE2;
|
|
87
|
-
this.finished = false;
|
|
88
|
-
this.length = 0;
|
|
89
|
-
this.pos = 0;
|
|
90
|
-
this.destroyed = false;
|
|
91
|
-
this.buffer = new Uint8Array(blockLen);
|
|
92
|
-
this.view = createView(this.buffer);
|
|
93
|
-
}
|
|
94
|
-
update(data) {
|
|
95
|
-
exists(this);
|
|
96
|
-
const { view, buffer, blockLen } = this;
|
|
97
|
-
data = toBytes(data);
|
|
98
|
-
const len = data.length;
|
|
99
|
-
for (let pos = 0; pos < len; ) {
|
|
100
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
101
|
-
if (take === blockLen) {
|
|
102
|
-
const dataView = createView(data);
|
|
103
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
104
|
-
this.process(dataView, pos);
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
108
|
-
this.pos += take;
|
|
109
|
-
pos += take;
|
|
110
|
-
if (this.pos === blockLen) {
|
|
111
|
-
this.process(view, 0);
|
|
112
|
-
this.pos = 0;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
this.length += data.length;
|
|
116
|
-
this.roundClean();
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
digestInto(out) {
|
|
120
|
-
exists(this);
|
|
121
|
-
output(out, this);
|
|
122
|
-
this.finished = true;
|
|
123
|
-
const { buffer, view, blockLen, isLE: isLE2 } = this;
|
|
124
|
-
let { pos } = this;
|
|
125
|
-
buffer[pos++] = 128;
|
|
126
|
-
this.buffer.subarray(pos).fill(0);
|
|
127
|
-
if (this.padOffset > blockLen - pos) {
|
|
128
|
-
this.process(view, 0);
|
|
129
|
-
pos = 0;
|
|
130
|
-
}
|
|
131
|
-
for (let i = pos; i < blockLen; i++)
|
|
132
|
-
buffer[i] = 0;
|
|
133
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE2);
|
|
134
|
-
this.process(view, 0);
|
|
135
|
-
const oview = createView(out);
|
|
136
|
-
const len = this.outputLen;
|
|
137
|
-
if (len % 4)
|
|
138
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
139
|
-
const outLen = len / 4;
|
|
140
|
-
const state = this.get();
|
|
141
|
-
if (outLen > state.length)
|
|
142
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
143
|
-
for (let i = 0; i < outLen; i++)
|
|
144
|
-
oview.setUint32(4 * i, state[i], isLE2);
|
|
145
|
-
}
|
|
146
|
-
digest() {
|
|
147
|
-
const { buffer, outputLen } = this;
|
|
148
|
-
this.digestInto(buffer);
|
|
149
|
-
const res = buffer.slice(0, outputLen);
|
|
150
|
-
this.destroy();
|
|
151
|
-
return res;
|
|
152
|
-
}
|
|
153
|
-
_cloneInto(to) {
|
|
154
|
-
to || (to = new this.constructor());
|
|
155
|
-
to.set(...this.get());
|
|
156
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
157
|
-
to.length = length;
|
|
158
|
-
to.pos = pos;
|
|
159
|
-
to.finished = finished;
|
|
160
|
-
to.destroyed = destroyed;
|
|
161
|
-
if (length % blockLen)
|
|
162
|
-
to.buffer.set(buffer);
|
|
163
|
-
return to;
|
|
164
|
-
}
|
|
19
|
+
var err = (m = "") => {
|
|
20
|
+
throw new Error(m);
|
|
165
21
|
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
let Ah = new Uint32Array(lst.length);
|
|
177
|
-
let Al = new Uint32Array(lst.length);
|
|
178
|
-
for (let i = 0; i < lst.length; i++) {
|
|
179
|
-
const { h, l } = fromBig(lst[i], le);
|
|
180
|
-
[Ah[i], Al[i]] = [h, l];
|
|
181
|
-
}
|
|
182
|
-
return [Ah, Al];
|
|
183
|
-
}
|
|
184
|
-
var toBig = (h, l) => BigInt(h >>> 0) << _32n | BigInt(l >>> 0);
|
|
185
|
-
var shrSH = (h, _l, s) => h >>> s;
|
|
186
|
-
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
187
|
-
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
188
|
-
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
189
|
-
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
190
|
-
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
191
|
-
var rotr32H = (_h, l) => l;
|
|
192
|
-
var rotr32L = (h, _l) => h;
|
|
193
|
-
var rotlSH = (h, l, s) => h << s | l >>> 32 - s;
|
|
194
|
-
var rotlSL = (h, l, s) => l << s | h >>> 32 - s;
|
|
195
|
-
var rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
|
|
196
|
-
var rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
|
|
197
|
-
function add(Ah, Al, Bh, Bl) {
|
|
198
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
199
|
-
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
200
|
-
}
|
|
201
|
-
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
202
|
-
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
203
|
-
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
204
|
-
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
205
|
-
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
206
|
-
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
207
|
-
var u64 = {
|
|
208
|
-
fromBig,
|
|
209
|
-
split,
|
|
210
|
-
toBig,
|
|
211
|
-
shrSH,
|
|
212
|
-
shrSL,
|
|
213
|
-
rotrSH,
|
|
214
|
-
rotrSL,
|
|
215
|
-
rotrBH,
|
|
216
|
-
rotrBL,
|
|
217
|
-
rotr32H,
|
|
218
|
-
rotr32L,
|
|
219
|
-
rotlSH,
|
|
220
|
-
rotlSL,
|
|
221
|
-
rotlBH,
|
|
222
|
-
rotlBL,
|
|
223
|
-
add,
|
|
224
|
-
add3L,
|
|
225
|
-
add3H,
|
|
226
|
-
add4L,
|
|
227
|
-
add4H,
|
|
228
|
-
add5H,
|
|
229
|
-
add5L
|
|
22
|
+
var str = (s) => typeof s === "string";
|
|
23
|
+
var au8 = (a, l) => (
|
|
24
|
+
// is Uint8Array (of specific length)
|
|
25
|
+
!(a instanceof Uint8Array) || typeof l === "number" && l > 0 && a.length !== l ? err("Uint8Array expected") : a
|
|
26
|
+
);
|
|
27
|
+
var u8n = (data) => new Uint8Array(data);
|
|
28
|
+
var toU8 = (a, len) => au8(str(a) ? h2b(a) : u8n(a), len);
|
|
29
|
+
var mod = (a, b = P) => {
|
|
30
|
+
let r = a % b;
|
|
31
|
+
return r >= 0n ? r : b + r;
|
|
230
32
|
};
|
|
231
|
-
var
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
this.
|
|
330
|
-
|
|
331
|
-
this.
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
this
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
this.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
this.
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
this.
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
SHA512_W_L[i] = SUMl | 0;
|
|
380
|
-
}
|
|
381
|
-
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
382
|
-
for (let i = 0; i < 80; i++) {
|
|
383
|
-
const sigma1h = u64_default.rotrSH(Eh, El, 14) ^ u64_default.rotrSH(Eh, El, 18) ^ u64_default.rotrBH(Eh, El, 41);
|
|
384
|
-
const sigma1l = u64_default.rotrSL(Eh, El, 14) ^ u64_default.rotrSL(Eh, El, 18) ^ u64_default.rotrBL(Eh, El, 41);
|
|
385
|
-
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
386
|
-
const CHIl = El & Fl ^ ~El & Gl;
|
|
387
|
-
const T1ll = u64_default.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
388
|
-
const T1h = u64_default.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
389
|
-
const T1l = T1ll | 0;
|
|
390
|
-
const sigma0h = u64_default.rotrSH(Ah, Al, 28) ^ u64_default.rotrBH(Ah, Al, 34) ^ u64_default.rotrBH(Ah, Al, 39);
|
|
391
|
-
const sigma0l = u64_default.rotrSL(Ah, Al, 28) ^ u64_default.rotrBL(Ah, Al, 34) ^ u64_default.rotrBL(Ah, Al, 39);
|
|
392
|
-
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
393
|
-
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
394
|
-
Hh = Gh | 0;
|
|
395
|
-
Hl = Gl | 0;
|
|
396
|
-
Gh = Fh | 0;
|
|
397
|
-
Gl = Fl | 0;
|
|
398
|
-
Fh = Eh | 0;
|
|
399
|
-
Fl = El | 0;
|
|
400
|
-
({ h: Eh, l: El } = u64_default.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
401
|
-
Dh = Ch | 0;
|
|
402
|
-
Dl = Cl | 0;
|
|
403
|
-
Ch = Bh | 0;
|
|
404
|
-
Cl = Bl | 0;
|
|
405
|
-
Bh = Ah | 0;
|
|
406
|
-
Bl = Al | 0;
|
|
407
|
-
const All = u64_default.add3L(T1l, sigma0l, MAJl);
|
|
408
|
-
Ah = u64_default.add3H(All, T1h, sigma0h, MAJh);
|
|
409
|
-
Al = All | 0;
|
|
410
|
-
}
|
|
411
|
-
({ h: Ah, l: Al } = u64_default.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
412
|
-
({ h: Bh, l: Bl } = u64_default.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
413
|
-
({ h: Ch, l: Cl } = u64_default.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
414
|
-
({ h: Dh, l: Dl } = u64_default.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
415
|
-
({ h: Eh, l: El } = u64_default.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
416
|
-
({ h: Fh, l: Fl } = u64_default.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
417
|
-
({ h: Gh, l: Gl } = u64_default.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
418
|
-
({ h: Hh, l: Hl } = u64_default.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
419
|
-
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
420
|
-
}
|
|
421
|
-
roundClean() {
|
|
422
|
-
SHA512_W_H.fill(0);
|
|
423
|
-
SHA512_W_L.fill(0);
|
|
424
|
-
}
|
|
425
|
-
destroy() {
|
|
426
|
-
this.buffer.fill(0);
|
|
427
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
428
|
-
}
|
|
33
|
+
var isPoint = (p) => p instanceof Point ? p : err("Point expected");
|
|
34
|
+
var Gpows = void 0;
|
|
35
|
+
var Point = class _Point {
|
|
36
|
+
constructor(ex, ey, ez, et) {
|
|
37
|
+
this.ex = ex;
|
|
38
|
+
this.ey = ey;
|
|
39
|
+
this.ez = ez;
|
|
40
|
+
this.et = et;
|
|
41
|
+
}
|
|
42
|
+
static fromAffine(p) {
|
|
43
|
+
return new _Point(p.x, p.y, 1n, mod(p.x * p.y));
|
|
44
|
+
}
|
|
45
|
+
static fromHex(hex, strict = true) {
|
|
46
|
+
const { d } = CURVE;
|
|
47
|
+
hex = toU8(hex, 32);
|
|
48
|
+
const normed = hex.slice();
|
|
49
|
+
normed[31] = hex[31] & ~128;
|
|
50
|
+
const y = b2n_LE(normed);
|
|
51
|
+
if (y === 0n) ; else {
|
|
52
|
+
if (strict && !(0n < y && y < P))
|
|
53
|
+
err("bad y coord 1");
|
|
54
|
+
if (!strict && !(0n < y && y < 2n ** 256n))
|
|
55
|
+
err("bad y coord 2");
|
|
56
|
+
}
|
|
57
|
+
const y2 = mod(y * y);
|
|
58
|
+
const u = mod(y2 - 1n);
|
|
59
|
+
const v = mod(d * y2 + 1n);
|
|
60
|
+
let { isValid, value: x } = uvRatio(u, v);
|
|
61
|
+
if (!isValid)
|
|
62
|
+
err("bad y coordinate 3");
|
|
63
|
+
const isXOdd = (x & 1n) === 1n;
|
|
64
|
+
const isHeadOdd = (hex[31] & 128) !== 0;
|
|
65
|
+
if (isHeadOdd !== isXOdd)
|
|
66
|
+
x = mod(-x);
|
|
67
|
+
return new _Point(x, y, 1n, mod(x * y));
|
|
68
|
+
}
|
|
69
|
+
get x() {
|
|
70
|
+
return this.toAffine().x;
|
|
71
|
+
}
|
|
72
|
+
// .x, .y will call expensive toAffine.
|
|
73
|
+
get y() {
|
|
74
|
+
return this.toAffine().y;
|
|
75
|
+
}
|
|
76
|
+
// Should be used with care.
|
|
77
|
+
equals(other) {
|
|
78
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
79
|
+
const { ex: X2, ey: Y2, ez: Z2 } = isPoint(other);
|
|
80
|
+
const X1Z2 = mod(X1 * Z2), X2Z1 = mod(X2 * Z1);
|
|
81
|
+
const Y1Z2 = mod(Y1 * Z2), Y2Z1 = mod(Y2 * Z1);
|
|
82
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
83
|
+
}
|
|
84
|
+
is0() {
|
|
85
|
+
return this.equals(I);
|
|
86
|
+
}
|
|
87
|
+
negate() {
|
|
88
|
+
return new _Point(mod(-this.ex), this.ey, this.ez, mod(-this.et));
|
|
89
|
+
}
|
|
90
|
+
double() {
|
|
91
|
+
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
92
|
+
const { a } = CURVE;
|
|
93
|
+
const A = mod(X1 * X1);
|
|
94
|
+
const B = mod(Y1 * Y1);
|
|
95
|
+
const C = mod(2n * mod(Z1 * Z1));
|
|
96
|
+
const D = mod(a * A);
|
|
97
|
+
const x1y1 = X1 + Y1;
|
|
98
|
+
const E = mod(mod(x1y1 * x1y1) - A - B);
|
|
99
|
+
const G2 = D + B;
|
|
100
|
+
const F = G2 - C;
|
|
101
|
+
const H = D - B;
|
|
102
|
+
const X3 = mod(E * F);
|
|
103
|
+
const Y3 = mod(G2 * H);
|
|
104
|
+
const T3 = mod(E * H);
|
|
105
|
+
const Z3 = mod(F * G2);
|
|
106
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
107
|
+
}
|
|
108
|
+
add(other) {
|
|
109
|
+
const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
|
|
110
|
+
const { ex: X2, ey: Y2, ez: Z2, et: T2 } = isPoint(other);
|
|
111
|
+
const { a, d } = CURVE;
|
|
112
|
+
const A = mod(X1 * X2);
|
|
113
|
+
const B = mod(Y1 * Y2);
|
|
114
|
+
const C = mod(T1 * d * T2);
|
|
115
|
+
const D = mod(Z1 * Z2);
|
|
116
|
+
const E = mod((X1 + Y1) * (X2 + Y2) - A - B);
|
|
117
|
+
const F = mod(D - C);
|
|
118
|
+
const G2 = mod(D + C);
|
|
119
|
+
const H = mod(B - a * A);
|
|
120
|
+
const X3 = mod(E * F);
|
|
121
|
+
const Y3 = mod(G2 * H);
|
|
122
|
+
const T3 = mod(E * H);
|
|
123
|
+
const Z3 = mod(F * G2);
|
|
124
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
125
|
+
}
|
|
126
|
+
mul(n, safe = true) {
|
|
127
|
+
if (n === 0n)
|
|
128
|
+
return safe === true ? err("cannot multiply by 0") : I;
|
|
129
|
+
if (!(typeof n === "bigint" && 0n < n && n < N))
|
|
130
|
+
err("invalid scalar, must be < L");
|
|
131
|
+
if (!safe && this.is0() || n === 1n)
|
|
132
|
+
return this;
|
|
133
|
+
if (this.equals(G))
|
|
134
|
+
return wNAF(n).p;
|
|
135
|
+
let p = I, f = G;
|
|
136
|
+
for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
|
|
137
|
+
if (n & 1n)
|
|
138
|
+
p = p.add(d);
|
|
139
|
+
else if (safe)
|
|
140
|
+
f = f.add(d);
|
|
141
|
+
}
|
|
142
|
+
return p;
|
|
143
|
+
}
|
|
144
|
+
multiply(scalar) {
|
|
145
|
+
return this.mul(scalar);
|
|
146
|
+
}
|
|
147
|
+
// Aliases for compatibilty
|
|
148
|
+
clearCofactor() {
|
|
149
|
+
return this.mul(BigInt(CURVE.h), false);
|
|
150
|
+
}
|
|
151
|
+
// multiply by cofactor
|
|
152
|
+
isSmallOrder() {
|
|
153
|
+
return this.clearCofactor().is0();
|
|
154
|
+
}
|
|
155
|
+
// check if P is small order
|
|
156
|
+
isTorsionFree() {
|
|
157
|
+
let p = this.mul(N / 2n, false).double();
|
|
158
|
+
if (N % 2n)
|
|
159
|
+
p = p.add(this);
|
|
160
|
+
return p.is0();
|
|
161
|
+
}
|
|
162
|
+
toAffine() {
|
|
163
|
+
const { ex: x, ey: y, ez: z } = this;
|
|
164
|
+
if (this.is0())
|
|
165
|
+
return { x: 0n, y: 0n };
|
|
166
|
+
const iz = invert(z);
|
|
167
|
+
if (mod(z * iz) !== 1n)
|
|
168
|
+
err("invalid inverse");
|
|
169
|
+
return { x: mod(x * iz), y: mod(y * iz) };
|
|
170
|
+
}
|
|
171
|
+
toRawBytes() {
|
|
172
|
+
const { x, y } = this.toAffine();
|
|
173
|
+
const b = n2b_32LE(y);
|
|
174
|
+
b[31] |= x & 1n ? 128 : 0;
|
|
175
|
+
return b;
|
|
176
|
+
}
|
|
177
|
+
toHex() {
|
|
178
|
+
return b2h(this.toRawBytes());
|
|
179
|
+
}
|
|
180
|
+
// encode to hex string
|
|
429
181
|
};
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
var
|
|
435
|
-
var
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
let hex = "";
|
|
442
|
-
for (let i = 0; i < bytes2.length; i++) {
|
|
443
|
-
hex += hexes[bytes2[i]];
|
|
444
|
-
}
|
|
445
|
-
return hex;
|
|
446
|
-
}
|
|
447
|
-
function hexToNumber(hex) {
|
|
448
|
-
if (typeof hex !== "string")
|
|
449
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
450
|
-
return BigInt(hex === "" ? "0" : `0x${hex}`);
|
|
451
|
-
}
|
|
452
|
-
function hexToBytes(hex) {
|
|
453
|
-
if (typeof hex !== "string")
|
|
454
|
-
throw new Error("hex string expected, got " + typeof hex);
|
|
455
|
-
const len = hex.length;
|
|
456
|
-
if (len % 2)
|
|
457
|
-
throw new Error("padded hex string expected, got unpadded hex of length " + len);
|
|
458
|
-
const array = new Uint8Array(len / 2);
|
|
459
|
-
for (let i = 0; i < array.length; i++) {
|
|
182
|
+
Point.BASE = new Point(Gx, Gy, 1n, mod(Gx * Gy));
|
|
183
|
+
Point.ZERO = new Point(0n, 1n, 1n, 0n);
|
|
184
|
+
var { BASE: G, ZERO: I } = Point;
|
|
185
|
+
var padh = (num, pad) => num.toString(16).padStart(pad, "0");
|
|
186
|
+
var b2h = (b) => Array.from(b).map((e) => padh(e, 2)).join("");
|
|
187
|
+
var h2b = (hex) => {
|
|
188
|
+
const l = hex.length;
|
|
189
|
+
if (!str(hex) || l % 2)
|
|
190
|
+
err("hex invalid 1");
|
|
191
|
+
const arr = u8n(l / 2);
|
|
192
|
+
for (let i = 0; i < arr.length; i++) {
|
|
460
193
|
const j = i * 2;
|
|
461
|
-
const
|
|
462
|
-
const
|
|
463
|
-
if (Number.isNaN(
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
}
|
|
467
|
-
return array;
|
|
468
|
-
}
|
|
469
|
-
function bytesToNumberBE(bytes2) {
|
|
470
|
-
return hexToNumber(bytesToHex(bytes2));
|
|
471
|
-
}
|
|
472
|
-
function bytesToNumberLE(bytes2) {
|
|
473
|
-
if (!u8a2(bytes2))
|
|
474
|
-
throw new Error("Uint8Array expected");
|
|
475
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes2).reverse()));
|
|
476
|
-
}
|
|
477
|
-
function numberToBytesBE(n, len) {
|
|
478
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
479
|
-
}
|
|
480
|
-
function numberToBytesLE(n, len) {
|
|
481
|
-
return numberToBytesBE(n, len).reverse();
|
|
482
|
-
}
|
|
483
|
-
function ensureBytes(title, hex, expectedLength) {
|
|
484
|
-
let res;
|
|
485
|
-
if (typeof hex === "string") {
|
|
486
|
-
try {
|
|
487
|
-
res = hexToBytes(hex);
|
|
488
|
-
} catch (e) {
|
|
489
|
-
throw new Error(`${title} must be valid hex string, got "${hex}". Cause: ${e}`);
|
|
490
|
-
}
|
|
491
|
-
} else if (u8a2(hex)) {
|
|
492
|
-
res = Uint8Array.from(hex);
|
|
493
|
-
} else {
|
|
494
|
-
throw new Error(`${title} must be hex string or Uint8Array`);
|
|
194
|
+
const h = hex.slice(j, j + 2);
|
|
195
|
+
const b = Number.parseInt(h, 16);
|
|
196
|
+
if (Number.isNaN(b) || b < 0)
|
|
197
|
+
err("hex invalid 2");
|
|
198
|
+
arr[i] = b;
|
|
495
199
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
|
200
|
+
return arr;
|
|
201
|
+
};
|
|
202
|
+
var n2b_32LE = (num) => h2b(padh(num, 32 * 2)).reverse();
|
|
203
|
+
var b2n_LE = (b) => BigInt("0x" + b2h(u8n(au8(b)).reverse()));
|
|
204
|
+
var concatB = (...arrs) => {
|
|
205
|
+
const r = u8n(arrs.reduce((sum, a) => sum + au8(a).length, 0));
|
|
503
206
|
let pad = 0;
|
|
504
|
-
|
|
505
|
-
if (!u8a2(a))
|
|
506
|
-
throw new Error("Uint8Array expected");
|
|
207
|
+
arrs.forEach((a) => {
|
|
507
208
|
r.set(a, pad);
|
|
508
209
|
pad += a.length;
|
|
509
210
|
});
|
|
510
211
|
return r;
|
|
511
|
-
}
|
|
512
|
-
var bitMask = (n) => (_2n << BigInt(n - 1)) - _1n;
|
|
513
|
-
var validatorFns = {
|
|
514
|
-
bigint: (val) => typeof val === "bigint",
|
|
515
|
-
function: (val) => typeof val === "function",
|
|
516
|
-
boolean: (val) => typeof val === "boolean",
|
|
517
|
-
string: (val) => typeof val === "string",
|
|
518
|
-
stringOrUint8Array: (val) => typeof val === "string" || val instanceof Uint8Array,
|
|
519
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
520
|
-
array: (val) => Array.isArray(val),
|
|
521
|
-
field: (val, object) => object.Fp.isValid(val),
|
|
522
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
523
212
|
};
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
const
|
|
530
|
-
if (isOptional && val === void 0)
|
|
531
|
-
return;
|
|
532
|
-
if (!checkVal(val, object)) {
|
|
533
|
-
throw new Error(`Invalid param ${String(fieldName)}=${val} (${typeof val}), expected ${type}`);
|
|
534
|
-
}
|
|
535
|
-
};
|
|
536
|
-
for (const [fieldName, type] of Object.entries(validators))
|
|
537
|
-
checkField(fieldName, type, false);
|
|
538
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
|
539
|
-
checkField(fieldName, type, true);
|
|
540
|
-
return object;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
// ../../node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/esm/abstract/modular.js
|
|
544
|
-
var _0n2 = BigInt(0);
|
|
545
|
-
var _1n2 = BigInt(1);
|
|
546
|
-
var _2n2 = BigInt(2);
|
|
547
|
-
var _3n = BigInt(3);
|
|
548
|
-
var _4n = BigInt(4);
|
|
549
|
-
var _5n = BigInt(5);
|
|
550
|
-
var _8n = BigInt(8);
|
|
551
|
-
BigInt(9);
|
|
552
|
-
BigInt(16);
|
|
553
|
-
function mod(a, b) {
|
|
554
|
-
const result = a % b;
|
|
555
|
-
return result >= _0n2 ? result : b + result;
|
|
556
|
-
}
|
|
557
|
-
function pow(num, power, modulo) {
|
|
558
|
-
if (modulo <= _0n2 || power < _0n2)
|
|
559
|
-
throw new Error("Expected power/modulo > 0");
|
|
560
|
-
if (modulo === _1n2)
|
|
561
|
-
return _0n2;
|
|
562
|
-
let res = _1n2;
|
|
563
|
-
while (power > _0n2) {
|
|
564
|
-
if (power & _1n2)
|
|
565
|
-
res = res * num % modulo;
|
|
566
|
-
num = num * num % modulo;
|
|
567
|
-
power >>= _1n2;
|
|
568
|
-
}
|
|
569
|
-
return res;
|
|
570
|
-
}
|
|
571
|
-
function pow2(x, power, modulo) {
|
|
572
|
-
let res = x;
|
|
573
|
-
while (power-- > _0n2) {
|
|
574
|
-
res *= res;
|
|
575
|
-
res %= modulo;
|
|
576
|
-
}
|
|
577
|
-
return res;
|
|
578
|
-
}
|
|
579
|
-
function invert(number, modulo) {
|
|
580
|
-
if (number === _0n2 || modulo <= _0n2) {
|
|
581
|
-
throw new Error(`invert: expected positive integers, got n=${number} mod=${modulo}`);
|
|
582
|
-
}
|
|
583
|
-
let a = mod(number, modulo);
|
|
584
|
-
let b = modulo;
|
|
585
|
-
let x = _0n2, u = _1n2;
|
|
586
|
-
while (a !== _0n2) {
|
|
587
|
-
const q = b / a;
|
|
588
|
-
const r = b % a;
|
|
213
|
+
var invert = (num, md = P) => {
|
|
214
|
+
if (num === 0n || md <= 0n)
|
|
215
|
+
err("no inverse n=" + num + " mod=" + md);
|
|
216
|
+
let a = mod(num, md), b = md, x = 0n, u = 1n;
|
|
217
|
+
while (a !== 0n) {
|
|
218
|
+
const q = b / a, r = b % a;
|
|
589
219
|
const m = x - u * q;
|
|
590
220
|
b = a, a = r, x = u, u = m;
|
|
591
221
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
let Q, S, Z;
|
|
600
|
-
for (Q = P - _1n2, S = 0; Q % _2n2 === _0n2; Q /= _2n2, S++)
|
|
601
|
-
;
|
|
602
|
-
for (Z = _2n2; Z < P && pow(Z, legendreC, P) !== P - _1n2; Z++)
|
|
603
|
-
;
|
|
604
|
-
if (S === 1) {
|
|
605
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
606
|
-
return function tonelliFast(Fp2, n) {
|
|
607
|
-
const root = Fp2.pow(n, p1div4);
|
|
608
|
-
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
609
|
-
throw new Error("Cannot find square root");
|
|
610
|
-
return root;
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
const Q1div2 = (Q + _1n2) / _2n2;
|
|
614
|
-
return function tonelliSlow(Fp2, n) {
|
|
615
|
-
if (Fp2.pow(n, legendreC) === Fp2.neg(Fp2.ONE))
|
|
616
|
-
throw new Error("Cannot find square root");
|
|
617
|
-
let r = S;
|
|
618
|
-
let g = Fp2.pow(Fp2.mul(Fp2.ONE, Z), Q);
|
|
619
|
-
let x = Fp2.pow(n, Q1div2);
|
|
620
|
-
let b = Fp2.pow(n, Q);
|
|
621
|
-
while (!Fp2.eql(b, Fp2.ONE)) {
|
|
622
|
-
if (Fp2.eql(b, Fp2.ZERO))
|
|
623
|
-
return Fp2.ZERO;
|
|
624
|
-
let m = 1;
|
|
625
|
-
for (let t2 = Fp2.sqr(b); m < r; m++) {
|
|
626
|
-
if (Fp2.eql(t2, Fp2.ONE))
|
|
627
|
-
break;
|
|
628
|
-
t2 = Fp2.sqr(t2);
|
|
629
|
-
}
|
|
630
|
-
const ge = Fp2.pow(g, _1n2 << BigInt(r - m - 1));
|
|
631
|
-
g = Fp2.sqr(ge);
|
|
632
|
-
x = Fp2.mul(x, ge);
|
|
633
|
-
b = Fp2.mul(b, g);
|
|
634
|
-
r = m;
|
|
635
|
-
}
|
|
636
|
-
return x;
|
|
637
|
-
};
|
|
638
|
-
}
|
|
639
|
-
function FpSqrt(P) {
|
|
640
|
-
if (P % _4n === _3n) {
|
|
641
|
-
const p1div4 = (P + _1n2) / _4n;
|
|
642
|
-
return function sqrt3mod4(Fp2, n) {
|
|
643
|
-
const root = Fp2.pow(n, p1div4);
|
|
644
|
-
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
645
|
-
throw new Error("Cannot find square root");
|
|
646
|
-
return root;
|
|
647
|
-
};
|
|
648
|
-
}
|
|
649
|
-
if (P % _8n === _5n) {
|
|
650
|
-
const c1 = (P - _5n) / _8n;
|
|
651
|
-
return function sqrt5mod8(Fp2, n) {
|
|
652
|
-
const n2 = Fp2.mul(n, _2n2);
|
|
653
|
-
const v = Fp2.pow(n2, c1);
|
|
654
|
-
const nv = Fp2.mul(n, v);
|
|
655
|
-
const i = Fp2.mul(Fp2.mul(nv, _2n2), v);
|
|
656
|
-
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
657
|
-
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
658
|
-
throw new Error("Cannot find square root");
|
|
659
|
-
return root;
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
return tonelliShanks(P);
|
|
663
|
-
}
|
|
664
|
-
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
665
|
-
var FIELD_FIELDS = [
|
|
666
|
-
"create",
|
|
667
|
-
"isValid",
|
|
668
|
-
"is0",
|
|
669
|
-
"neg",
|
|
670
|
-
"inv",
|
|
671
|
-
"sqrt",
|
|
672
|
-
"sqr",
|
|
673
|
-
"eql",
|
|
674
|
-
"add",
|
|
675
|
-
"sub",
|
|
676
|
-
"mul",
|
|
677
|
-
"pow",
|
|
678
|
-
"div",
|
|
679
|
-
"addN",
|
|
680
|
-
"subN",
|
|
681
|
-
"mulN",
|
|
682
|
-
"sqrN"
|
|
683
|
-
];
|
|
684
|
-
function validateField(field) {
|
|
685
|
-
const initial = {
|
|
686
|
-
ORDER: "bigint",
|
|
687
|
-
MASK: "bigint",
|
|
688
|
-
BYTES: "isSafeInteger",
|
|
689
|
-
BITS: "isSafeInteger"
|
|
690
|
-
};
|
|
691
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
692
|
-
map[val] = "function";
|
|
693
|
-
return map;
|
|
694
|
-
}, initial);
|
|
695
|
-
return validateObject(field, opts);
|
|
696
|
-
}
|
|
697
|
-
function FpPow(f, num, power) {
|
|
698
|
-
if (power < _0n2)
|
|
699
|
-
throw new Error("Expected power > 0");
|
|
700
|
-
if (power === _0n2)
|
|
701
|
-
return f.ONE;
|
|
702
|
-
if (power === _1n2)
|
|
703
|
-
return num;
|
|
704
|
-
let p = f.ONE;
|
|
705
|
-
let d = num;
|
|
706
|
-
while (power > _0n2) {
|
|
707
|
-
if (power & _1n2)
|
|
708
|
-
p = f.mul(p, d);
|
|
709
|
-
d = f.sqr(d);
|
|
710
|
-
power >>= _1n2;
|
|
711
|
-
}
|
|
712
|
-
return p;
|
|
713
|
-
}
|
|
714
|
-
function FpInvertBatch(f, nums) {
|
|
715
|
-
const tmp = new Array(nums.length);
|
|
716
|
-
const lastMultiplied = nums.reduce((acc, num, i) => {
|
|
717
|
-
if (f.is0(num))
|
|
718
|
-
return acc;
|
|
719
|
-
tmp[i] = acc;
|
|
720
|
-
return f.mul(acc, num);
|
|
721
|
-
}, f.ONE);
|
|
722
|
-
const inverted = f.inv(lastMultiplied);
|
|
723
|
-
nums.reduceRight((acc, num, i) => {
|
|
724
|
-
if (f.is0(num))
|
|
725
|
-
return acc;
|
|
726
|
-
tmp[i] = f.mul(acc, tmp[i]);
|
|
727
|
-
return f.mul(acc, num);
|
|
728
|
-
}, inverted);
|
|
729
|
-
return tmp;
|
|
730
|
-
}
|
|
731
|
-
function nLength(n, nBitLength) {
|
|
732
|
-
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
733
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
734
|
-
return { nBitLength: _nBitLength, nByteLength };
|
|
735
|
-
}
|
|
736
|
-
function Field(ORDER, bitLen, isLE2 = false, redef = {}) {
|
|
737
|
-
if (ORDER <= _0n2)
|
|
738
|
-
throw new Error(`Expected Field ORDER > 0, got ${ORDER}`);
|
|
739
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen);
|
|
740
|
-
if (BYTES > 2048)
|
|
741
|
-
throw new Error("Field lengths over 2048 bytes are not supported");
|
|
742
|
-
const sqrtP = FpSqrt(ORDER);
|
|
743
|
-
const f = Object.freeze({
|
|
744
|
-
ORDER,
|
|
745
|
-
BITS,
|
|
746
|
-
BYTES,
|
|
747
|
-
MASK: bitMask(BITS),
|
|
748
|
-
ZERO: _0n2,
|
|
749
|
-
ONE: _1n2,
|
|
750
|
-
create: (num) => mod(num, ORDER),
|
|
751
|
-
isValid: (num) => {
|
|
752
|
-
if (typeof num !== "bigint")
|
|
753
|
-
throw new Error(`Invalid field element: expected bigint, got ${typeof num}`);
|
|
754
|
-
return _0n2 <= num && num < ORDER;
|
|
755
|
-
},
|
|
756
|
-
is0: (num) => num === _0n2,
|
|
757
|
-
isOdd: (num) => (num & _1n2) === _1n2,
|
|
758
|
-
neg: (num) => mod(-num, ORDER),
|
|
759
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
760
|
-
sqr: (num) => mod(num * num, ORDER),
|
|
761
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
762
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
763
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
764
|
-
pow: (num, power) => FpPow(f, num, power),
|
|
765
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
766
|
-
// Same as above, but doesn't normalize
|
|
767
|
-
sqrN: (num) => num * num,
|
|
768
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
769
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
770
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
771
|
-
inv: (num) => invert(num, ORDER),
|
|
772
|
-
sqrt: redef.sqrt || ((n) => sqrtP(f, n)),
|
|
773
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
774
|
-
// TODO: do we really need constant cmov?
|
|
775
|
-
// We don't have const-time bigints anyway, so probably will be not very useful
|
|
776
|
-
cmov: (a, b, c) => c ? b : a,
|
|
777
|
-
toBytes: (num) => isLE2 ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
778
|
-
fromBytes: (bytes2) => {
|
|
779
|
-
if (bytes2.length !== BYTES)
|
|
780
|
-
throw new Error(`Fp.fromBytes: expected ${BYTES}, got ${bytes2.length}`);
|
|
781
|
-
return isLE2 ? bytesToNumberLE(bytes2) : bytesToNumberBE(bytes2);
|
|
782
|
-
}
|
|
783
|
-
});
|
|
784
|
-
return Object.freeze(f);
|
|
785
|
-
}
|
|
786
|
-
function FpSqrtEven(Fp2, elm) {
|
|
787
|
-
if (!Fp2.isOdd)
|
|
788
|
-
throw new Error(`Field doesn't have isOdd`);
|
|
789
|
-
const root = Fp2.sqrt(elm);
|
|
790
|
-
return Fp2.isOdd(root) ? Fp2.neg(root) : root;
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
// ../../node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/esm/abstract/curve.js
|
|
794
|
-
var _0n3 = BigInt(0);
|
|
795
|
-
var _1n3 = BigInt(1);
|
|
796
|
-
function wNAF(c, bits) {
|
|
797
|
-
const constTimeNegate = (condition, item) => {
|
|
798
|
-
const neg = item.negate();
|
|
799
|
-
return condition ? neg : item;
|
|
800
|
-
};
|
|
801
|
-
const opts = (W) => {
|
|
802
|
-
const windows = Math.ceil(bits / W) + 1;
|
|
803
|
-
const windowSize = 2 ** (W - 1);
|
|
804
|
-
return { windows, windowSize };
|
|
805
|
-
};
|
|
806
|
-
return {
|
|
807
|
-
constTimeNegate,
|
|
808
|
-
// non-const time multiplication ladder
|
|
809
|
-
unsafeLadder(elm, n) {
|
|
810
|
-
let p = c.ZERO;
|
|
811
|
-
let d = elm;
|
|
812
|
-
while (n > _0n3) {
|
|
813
|
-
if (n & _1n3)
|
|
814
|
-
p = p.add(d);
|
|
815
|
-
d = d.double();
|
|
816
|
-
n >>= _1n3;
|
|
817
|
-
}
|
|
818
|
-
return p;
|
|
819
|
-
},
|
|
820
|
-
/**
|
|
821
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
822
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
823
|
-
* Number of precomputed points depends on the curve size:
|
|
824
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
825
|
-
* - 𝑊 is the window size
|
|
826
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
827
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
828
|
-
* @returns precomputed point tables flattened to a single array
|
|
829
|
-
*/
|
|
830
|
-
precomputeWindow(elm, W) {
|
|
831
|
-
const { windows, windowSize } = opts(W);
|
|
832
|
-
const points = [];
|
|
833
|
-
let p = elm;
|
|
834
|
-
let base = p;
|
|
835
|
-
for (let window = 0; window < windows; window++) {
|
|
836
|
-
base = p;
|
|
837
|
-
points.push(base);
|
|
838
|
-
for (let i = 1; i < windowSize; i++) {
|
|
839
|
-
base = base.add(p);
|
|
840
|
-
points.push(base);
|
|
841
|
-
}
|
|
842
|
-
p = base.double();
|
|
843
|
-
}
|
|
844
|
-
return points;
|
|
845
|
-
},
|
|
846
|
-
/**
|
|
847
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
848
|
-
* @param W window size
|
|
849
|
-
* @param precomputes precomputed tables
|
|
850
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
851
|
-
* @returns real and fake (for const-time) points
|
|
852
|
-
*/
|
|
853
|
-
wNAF(W, precomputes, n) {
|
|
854
|
-
const { windows, windowSize } = opts(W);
|
|
855
|
-
let p = c.ZERO;
|
|
856
|
-
let f = c.BASE;
|
|
857
|
-
const mask = BigInt(2 ** W - 1);
|
|
858
|
-
const maxNumber = 2 ** W;
|
|
859
|
-
const shiftBy = BigInt(W);
|
|
860
|
-
for (let window = 0; window < windows; window++) {
|
|
861
|
-
const offset = window * windowSize;
|
|
862
|
-
let wbits = Number(n & mask);
|
|
863
|
-
n >>= shiftBy;
|
|
864
|
-
if (wbits > windowSize) {
|
|
865
|
-
wbits -= maxNumber;
|
|
866
|
-
n += _1n3;
|
|
867
|
-
}
|
|
868
|
-
const offset1 = offset;
|
|
869
|
-
const offset2 = offset + Math.abs(wbits) - 1;
|
|
870
|
-
const cond1 = window % 2 !== 0;
|
|
871
|
-
const cond2 = wbits < 0;
|
|
872
|
-
if (wbits === 0) {
|
|
873
|
-
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
874
|
-
} else {
|
|
875
|
-
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
return { p, f };
|
|
879
|
-
},
|
|
880
|
-
wNAFCached(P, precomputesMap, n, transform) {
|
|
881
|
-
const W = P._WINDOW_SIZE || 1;
|
|
882
|
-
let comp = precomputesMap.get(P);
|
|
883
|
-
if (!comp) {
|
|
884
|
-
comp = this.precomputeWindow(P, W);
|
|
885
|
-
if (W !== 1) {
|
|
886
|
-
precomputesMap.set(P, transform(comp));
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
return this.wNAF(W, comp, n);
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
}
|
|
893
|
-
function validateBasic(curve) {
|
|
894
|
-
validateField(curve.Fp);
|
|
895
|
-
validateObject(curve, {
|
|
896
|
-
n: "bigint",
|
|
897
|
-
h: "bigint",
|
|
898
|
-
Gx: "field",
|
|
899
|
-
Gy: "field"
|
|
900
|
-
}, {
|
|
901
|
-
nBitLength: "isSafeInteger",
|
|
902
|
-
nByteLength: "isSafeInteger"
|
|
903
|
-
});
|
|
904
|
-
return Object.freeze({
|
|
905
|
-
...nLength(curve.n, curve.nBitLength),
|
|
906
|
-
...curve,
|
|
907
|
-
...{ p: curve.Fp.ORDER }
|
|
908
|
-
});
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
// ../../node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/esm/abstract/edwards.js
|
|
912
|
-
var _0n4 = BigInt(0);
|
|
913
|
-
var _1n4 = BigInt(1);
|
|
914
|
-
var _2n3 = BigInt(2);
|
|
915
|
-
var _8n2 = BigInt(8);
|
|
916
|
-
var VERIFY_DEFAULT = { zip215: true };
|
|
917
|
-
function validateOpts(curve) {
|
|
918
|
-
const opts = validateBasic(curve);
|
|
919
|
-
validateObject(curve, {
|
|
920
|
-
hash: "function",
|
|
921
|
-
a: "bigint",
|
|
922
|
-
d: "bigint",
|
|
923
|
-
randomBytes: "function"
|
|
924
|
-
}, {
|
|
925
|
-
adjustScalarBytes: "function",
|
|
926
|
-
domain: "function",
|
|
927
|
-
uvRatio: "function",
|
|
928
|
-
mapToCurve: "function"
|
|
929
|
-
});
|
|
930
|
-
return Object.freeze({ ...opts });
|
|
931
|
-
}
|
|
932
|
-
function twistedEdwards(curveDef) {
|
|
933
|
-
const CURVE = validateOpts(curveDef);
|
|
934
|
-
const { Fp: Fp2, n: CURVE_ORDER, prehash, hash: cHash, randomBytes: randomBytes2, nByteLength, h: cofactor } = CURVE;
|
|
935
|
-
const MASK = _2n3 << BigInt(nByteLength * 8) - _1n4;
|
|
936
|
-
const modP = Fp2.create;
|
|
937
|
-
const uvRatio2 = CURVE.uvRatio || ((u, v) => {
|
|
938
|
-
try {
|
|
939
|
-
return { isValid: true, value: Fp2.sqrt(u * Fp2.inv(v)) };
|
|
940
|
-
} catch (e) {
|
|
941
|
-
return { isValid: false, value: _0n4 };
|
|
942
|
-
}
|
|
943
|
-
});
|
|
944
|
-
const adjustScalarBytes2 = CURVE.adjustScalarBytes || ((bytes2) => bytes2);
|
|
945
|
-
const domain = CURVE.domain || ((data, ctx, phflag) => {
|
|
946
|
-
if (ctx.length || phflag)
|
|
947
|
-
throw new Error("Contexts/pre-hash are not supported");
|
|
948
|
-
return data;
|
|
949
|
-
});
|
|
950
|
-
const inBig = (n) => typeof n === "bigint" && _0n4 < n;
|
|
951
|
-
const inRange = (n, max) => inBig(n) && inBig(max) && n < max;
|
|
952
|
-
const in0MaskRange = (n) => n === _0n4 || inRange(n, MASK);
|
|
953
|
-
function assertInRange(n, max) {
|
|
954
|
-
if (inRange(n, max))
|
|
955
|
-
return n;
|
|
956
|
-
throw new Error(`Expected valid scalar < ${max}, got ${typeof n} ${n}`);
|
|
957
|
-
}
|
|
958
|
-
function assertGE0(n) {
|
|
959
|
-
return n === _0n4 ? n : assertInRange(n, CURVE_ORDER);
|
|
960
|
-
}
|
|
961
|
-
const pointPrecomputes = /* @__PURE__ */ new Map();
|
|
962
|
-
function isPoint(other) {
|
|
963
|
-
if (!(other instanceof Point))
|
|
964
|
-
throw new Error("ExtendedPoint expected");
|
|
965
|
-
}
|
|
966
|
-
class Point {
|
|
967
|
-
constructor(ex, ey, ez, et) {
|
|
968
|
-
this.ex = ex;
|
|
969
|
-
this.ey = ey;
|
|
970
|
-
this.ez = ez;
|
|
971
|
-
this.et = et;
|
|
972
|
-
if (!in0MaskRange(ex))
|
|
973
|
-
throw new Error("x required");
|
|
974
|
-
if (!in0MaskRange(ey))
|
|
975
|
-
throw new Error("y required");
|
|
976
|
-
if (!in0MaskRange(ez))
|
|
977
|
-
throw new Error("z required");
|
|
978
|
-
if (!in0MaskRange(et))
|
|
979
|
-
throw new Error("t required");
|
|
980
|
-
}
|
|
981
|
-
get x() {
|
|
982
|
-
return this.toAffine().x;
|
|
983
|
-
}
|
|
984
|
-
get y() {
|
|
985
|
-
return this.toAffine().y;
|
|
986
|
-
}
|
|
987
|
-
static fromAffine(p) {
|
|
988
|
-
if (p instanceof Point)
|
|
989
|
-
throw new Error("extended point not allowed");
|
|
990
|
-
const { x, y } = p || {};
|
|
991
|
-
if (!in0MaskRange(x) || !in0MaskRange(y))
|
|
992
|
-
throw new Error("invalid affine point");
|
|
993
|
-
return new Point(x, y, _1n4, modP(x * y));
|
|
994
|
-
}
|
|
995
|
-
static normalizeZ(points) {
|
|
996
|
-
const toInv = Fp2.invertBatch(points.map((p) => p.ez));
|
|
997
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
|
998
|
-
}
|
|
999
|
-
// "Private method", don't use it directly
|
|
1000
|
-
_setWindowSize(windowSize) {
|
|
1001
|
-
this._WINDOW_SIZE = windowSize;
|
|
1002
|
-
pointPrecomputes.delete(this);
|
|
1003
|
-
}
|
|
1004
|
-
// Not required for fromHex(), which always creates valid points.
|
|
1005
|
-
// Could be useful for fromAffine().
|
|
1006
|
-
assertValidity() {
|
|
1007
|
-
const { a, d } = CURVE;
|
|
1008
|
-
if (this.is0())
|
|
1009
|
-
throw new Error("bad point: ZERO");
|
|
1010
|
-
const { ex: X, ey: Y, ez: Z, et: T } = this;
|
|
1011
|
-
const X2 = modP(X * X);
|
|
1012
|
-
const Y2 = modP(Y * Y);
|
|
1013
|
-
const Z2 = modP(Z * Z);
|
|
1014
|
-
const Z4 = modP(Z2 * Z2);
|
|
1015
|
-
const aX2 = modP(X2 * a);
|
|
1016
|
-
const left = modP(Z2 * modP(aX2 + Y2));
|
|
1017
|
-
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
1018
|
-
if (left !== right)
|
|
1019
|
-
throw new Error("bad point: equation left != right (1)");
|
|
1020
|
-
const XY = modP(X * Y);
|
|
1021
|
-
const ZT = modP(Z * T);
|
|
1022
|
-
if (XY !== ZT)
|
|
1023
|
-
throw new Error("bad point: equation left != right (2)");
|
|
1024
|
-
}
|
|
1025
|
-
// Compare one point to another.
|
|
1026
|
-
equals(other) {
|
|
1027
|
-
isPoint(other);
|
|
1028
|
-
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
1029
|
-
const { ex: X2, ey: Y2, ez: Z2 } = other;
|
|
1030
|
-
const X1Z2 = modP(X1 * Z2);
|
|
1031
|
-
const X2Z1 = modP(X2 * Z1);
|
|
1032
|
-
const Y1Z2 = modP(Y1 * Z2);
|
|
1033
|
-
const Y2Z1 = modP(Y2 * Z1);
|
|
1034
|
-
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
1035
|
-
}
|
|
1036
|
-
is0() {
|
|
1037
|
-
return this.equals(Point.ZERO);
|
|
1038
|
-
}
|
|
1039
|
-
negate() {
|
|
1040
|
-
return new Point(modP(-this.ex), this.ey, this.ez, modP(-this.et));
|
|
1041
|
-
}
|
|
1042
|
-
// Fast algo for doubling Extended Point.
|
|
1043
|
-
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1044
|
-
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1045
|
-
double() {
|
|
1046
|
-
const { a } = CURVE;
|
|
1047
|
-
const { ex: X1, ey: Y1, ez: Z1 } = this;
|
|
1048
|
-
const A = modP(X1 * X1);
|
|
1049
|
-
const B = modP(Y1 * Y1);
|
|
1050
|
-
const C = modP(_2n3 * modP(Z1 * Z1));
|
|
1051
|
-
const D = modP(a * A);
|
|
1052
|
-
const x1y1 = X1 + Y1;
|
|
1053
|
-
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
1054
|
-
const G2 = D + B;
|
|
1055
|
-
const F = G2 - C;
|
|
1056
|
-
const H = D - B;
|
|
1057
|
-
const X3 = modP(E * F);
|
|
1058
|
-
const Y3 = modP(G2 * H);
|
|
1059
|
-
const T3 = modP(E * H);
|
|
1060
|
-
const Z3 = modP(F * G2);
|
|
1061
|
-
return new Point(X3, Y3, Z3, T3);
|
|
1062
|
-
}
|
|
1063
|
-
// Fast algo for adding 2 Extended Points.
|
|
1064
|
-
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
1065
|
-
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1066
|
-
add(other) {
|
|
1067
|
-
isPoint(other);
|
|
1068
|
-
const { a, d } = CURVE;
|
|
1069
|
-
const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
|
|
1070
|
-
const { ex: X2, ey: Y2, ez: Z2, et: T2 } = other;
|
|
1071
|
-
if (a === BigInt(-1)) {
|
|
1072
|
-
const A2 = modP((Y1 - X1) * (Y2 + X2));
|
|
1073
|
-
const B2 = modP((Y1 + X1) * (Y2 - X2));
|
|
1074
|
-
const F2 = modP(B2 - A2);
|
|
1075
|
-
if (F2 === _0n4)
|
|
1076
|
-
return this.double();
|
|
1077
|
-
const C2 = modP(Z1 * _2n3 * T2);
|
|
1078
|
-
const D2 = modP(T1 * _2n3 * Z2);
|
|
1079
|
-
const E2 = D2 + C2;
|
|
1080
|
-
const G3 = B2 + A2;
|
|
1081
|
-
const H2 = D2 - C2;
|
|
1082
|
-
const X32 = modP(E2 * F2);
|
|
1083
|
-
const Y32 = modP(G3 * H2);
|
|
1084
|
-
const T32 = modP(E2 * H2);
|
|
1085
|
-
const Z32 = modP(F2 * G3);
|
|
1086
|
-
return new Point(X32, Y32, Z32, T32);
|
|
1087
|
-
}
|
|
1088
|
-
const A = modP(X1 * X2);
|
|
1089
|
-
const B = modP(Y1 * Y2);
|
|
1090
|
-
const C = modP(T1 * d * T2);
|
|
1091
|
-
const D = modP(Z1 * Z2);
|
|
1092
|
-
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
1093
|
-
const F = D - C;
|
|
1094
|
-
const G2 = D + C;
|
|
1095
|
-
const H = modP(B - a * A);
|
|
1096
|
-
const X3 = modP(E * F);
|
|
1097
|
-
const Y3 = modP(G2 * H);
|
|
1098
|
-
const T3 = modP(E * H);
|
|
1099
|
-
const Z3 = modP(F * G2);
|
|
1100
|
-
return new Point(X3, Y3, Z3, T3);
|
|
1101
|
-
}
|
|
1102
|
-
subtract(other) {
|
|
1103
|
-
return this.add(other.negate());
|
|
1104
|
-
}
|
|
1105
|
-
wNAF(n) {
|
|
1106
|
-
return wnaf.wNAFCached(this, pointPrecomputes, n, Point.normalizeZ);
|
|
1107
|
-
}
|
|
1108
|
-
// Constant-time multiplication.
|
|
1109
|
-
multiply(scalar) {
|
|
1110
|
-
const { p, f } = this.wNAF(assertInRange(scalar, CURVE_ORDER));
|
|
1111
|
-
return Point.normalizeZ([p, f])[0];
|
|
1112
|
-
}
|
|
1113
|
-
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1114
|
-
// It's faster, but should only be used when you don't care about
|
|
1115
|
-
// an exposed private key e.g. sig verification.
|
|
1116
|
-
// Does NOT allow scalars higher than CURVE.n.
|
|
1117
|
-
multiplyUnsafe(scalar) {
|
|
1118
|
-
let n = assertGE0(scalar);
|
|
1119
|
-
if (n === _0n4)
|
|
1120
|
-
return I;
|
|
1121
|
-
if (this.equals(I) || n === _1n4)
|
|
1122
|
-
return this;
|
|
1123
|
-
if (this.equals(G))
|
|
1124
|
-
return this.wNAF(n).p;
|
|
1125
|
-
return wnaf.unsafeLadder(this, n);
|
|
1126
|
-
}
|
|
1127
|
-
// Checks if point is of small order.
|
|
1128
|
-
// If you add something to small order point, you will have "dirty"
|
|
1129
|
-
// point with torsion component.
|
|
1130
|
-
// Multiplies point by cofactor and checks if the result is 0.
|
|
1131
|
-
isSmallOrder() {
|
|
1132
|
-
return this.multiplyUnsafe(cofactor).is0();
|
|
1133
|
-
}
|
|
1134
|
-
// Multiplies point by curve order and checks if the result is 0.
|
|
1135
|
-
// Returns `false` is the point is dirty.
|
|
1136
|
-
isTorsionFree() {
|
|
1137
|
-
return wnaf.unsafeLadder(this, CURVE_ORDER).is0();
|
|
1138
|
-
}
|
|
1139
|
-
// Converts Extended point to default (x, y) coordinates.
|
|
1140
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1141
|
-
toAffine(iz) {
|
|
1142
|
-
const { ex: x, ey: y, ez: z } = this;
|
|
1143
|
-
const is0 = this.is0();
|
|
1144
|
-
if (iz == null)
|
|
1145
|
-
iz = is0 ? _8n2 : Fp2.inv(z);
|
|
1146
|
-
const ax = modP(x * iz);
|
|
1147
|
-
const ay = modP(y * iz);
|
|
1148
|
-
const zz = modP(z * iz);
|
|
1149
|
-
if (is0)
|
|
1150
|
-
return { x: _0n4, y: _1n4 };
|
|
1151
|
-
if (zz !== _1n4)
|
|
1152
|
-
throw new Error("invZ was invalid");
|
|
1153
|
-
return { x: ax, y: ay };
|
|
1154
|
-
}
|
|
1155
|
-
clearCofactor() {
|
|
1156
|
-
const { h: cofactor2 } = CURVE;
|
|
1157
|
-
if (cofactor2 === _1n4)
|
|
1158
|
-
return this;
|
|
1159
|
-
return this.multiplyUnsafe(cofactor2);
|
|
1160
|
-
}
|
|
1161
|
-
// Converts hash string or Uint8Array to Point.
|
|
1162
|
-
// Uses algo from RFC8032 5.1.3.
|
|
1163
|
-
static fromHex(hex, zip215 = false) {
|
|
1164
|
-
const { d, a } = CURVE;
|
|
1165
|
-
const len = Fp2.BYTES;
|
|
1166
|
-
hex = ensureBytes("pointHex", hex, len);
|
|
1167
|
-
const normed = hex.slice();
|
|
1168
|
-
const lastByte = hex[len - 1];
|
|
1169
|
-
normed[len - 1] = lastByte & ~128;
|
|
1170
|
-
const y = bytesToNumberLE(normed);
|
|
1171
|
-
if (y === _0n4) ; else {
|
|
1172
|
-
if (zip215)
|
|
1173
|
-
assertInRange(y, MASK);
|
|
1174
|
-
else
|
|
1175
|
-
assertInRange(y, Fp2.ORDER);
|
|
1176
|
-
}
|
|
1177
|
-
const y2 = modP(y * y);
|
|
1178
|
-
const u = modP(y2 - _1n4);
|
|
1179
|
-
const v = modP(d * y2 - a);
|
|
1180
|
-
let { isValid, value: x } = uvRatio2(u, v);
|
|
1181
|
-
if (!isValid)
|
|
1182
|
-
throw new Error("Point.fromHex: invalid y coordinate");
|
|
1183
|
-
const isXOdd = (x & _1n4) === _1n4;
|
|
1184
|
-
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1185
|
-
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
1186
|
-
throw new Error("Point.fromHex: x=0 and x_0=1");
|
|
1187
|
-
if (isLastByteOdd !== isXOdd)
|
|
1188
|
-
x = modP(-x);
|
|
1189
|
-
return Point.fromAffine({ x, y });
|
|
1190
|
-
}
|
|
1191
|
-
static fromPrivateKey(privKey) {
|
|
1192
|
-
return getExtendedPublicKey(privKey).point;
|
|
1193
|
-
}
|
|
1194
|
-
toRawBytes() {
|
|
1195
|
-
const { x, y } = this.toAffine();
|
|
1196
|
-
const bytes2 = numberToBytesLE(y, Fp2.BYTES);
|
|
1197
|
-
bytes2[bytes2.length - 1] |= x & _1n4 ? 128 : 0;
|
|
1198
|
-
return bytes2;
|
|
1199
|
-
}
|
|
1200
|
-
toHex() {
|
|
1201
|
-
return bytesToHex(this.toRawBytes());
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
1205
|
-
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
1206
|
-
const { BASE: G, ZERO: I } = Point;
|
|
1207
|
-
const wnaf = wNAF(Point, nByteLength * 8);
|
|
1208
|
-
function modN(a) {
|
|
1209
|
-
return mod(a, CURVE_ORDER);
|
|
1210
|
-
}
|
|
1211
|
-
function modN_LE(hash) {
|
|
1212
|
-
return modN(bytesToNumberLE(hash));
|
|
1213
|
-
}
|
|
1214
|
-
function getExtendedPublicKey(key) {
|
|
1215
|
-
const len = nByteLength;
|
|
1216
|
-
key = ensureBytes("private key", key, len);
|
|
1217
|
-
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
1218
|
-
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
1219
|
-
const prefix = hashed.slice(len, 2 * len);
|
|
1220
|
-
const scalar = modN_LE(head);
|
|
1221
|
-
const point = G.multiply(scalar);
|
|
1222
|
-
const pointBytes = point.toRawBytes();
|
|
1223
|
-
return { head, prefix, scalar, point, pointBytes };
|
|
1224
|
-
}
|
|
1225
|
-
function getPublicKey(privKey) {
|
|
1226
|
-
return getExtendedPublicKey(privKey).pointBytes;
|
|
1227
|
-
}
|
|
1228
|
-
function hashDomainToScalar(context = new Uint8Array(), ...msgs) {
|
|
1229
|
-
const msg = concatBytes2(...msgs);
|
|
1230
|
-
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
1231
|
-
}
|
|
1232
|
-
function sign(msg, privKey, options = {}) {
|
|
1233
|
-
msg = ensureBytes("message", msg);
|
|
1234
|
-
if (prehash)
|
|
1235
|
-
msg = prehash(msg);
|
|
1236
|
-
const { prefix, scalar, pointBytes } = getExtendedPublicKey(privKey);
|
|
1237
|
-
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
1238
|
-
const R = G.multiply(r).toRawBytes();
|
|
1239
|
-
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
1240
|
-
const s = modN(r + k * scalar);
|
|
1241
|
-
assertGE0(s);
|
|
1242
|
-
const res = concatBytes2(R, numberToBytesLE(s, Fp2.BYTES));
|
|
1243
|
-
return ensureBytes("result", res, nByteLength * 2);
|
|
1244
|
-
}
|
|
1245
|
-
const verifyOpts = VERIFY_DEFAULT;
|
|
1246
|
-
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
1247
|
-
const { context, zip215 } = options;
|
|
1248
|
-
const len = Fp2.BYTES;
|
|
1249
|
-
sig = ensureBytes("signature", sig, 2 * len);
|
|
1250
|
-
msg = ensureBytes("message", msg);
|
|
1251
|
-
if (prehash)
|
|
1252
|
-
msg = prehash(msg);
|
|
1253
|
-
const s = bytesToNumberLE(sig.slice(len, 2 * len));
|
|
1254
|
-
let A, R, SB;
|
|
1255
|
-
try {
|
|
1256
|
-
A = Point.fromHex(publicKey, zip215);
|
|
1257
|
-
R = Point.fromHex(sig.slice(0, len), zip215);
|
|
1258
|
-
SB = G.multiplyUnsafe(s);
|
|
1259
|
-
} catch (error) {
|
|
1260
|
-
return false;
|
|
1261
|
-
}
|
|
1262
|
-
if (!zip215 && A.isSmallOrder())
|
|
1263
|
-
return false;
|
|
1264
|
-
const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg);
|
|
1265
|
-
const RkA = R.add(A.multiplyUnsafe(k));
|
|
1266
|
-
return RkA.subtract(SB).clearCofactor().equals(Point.ZERO);
|
|
222
|
+
return b === 1n ? mod(x, md) : err("no inverse");
|
|
223
|
+
};
|
|
224
|
+
var pow2 = (x, power) => {
|
|
225
|
+
let r = x;
|
|
226
|
+
while (power-- > 0n) {
|
|
227
|
+
r *= r;
|
|
228
|
+
r %= P;
|
|
1267
229
|
}
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
// ed25519 private keys are uniform 32b. No need to check for modulo bias, like in secp256k1.
|
|
1272
|
-
randomPrivateKey: () => randomBytes2(Fp2.BYTES),
|
|
1273
|
-
/**
|
|
1274
|
-
* We're doing scalar multiplication (used in getPublicKey etc) with precomputed BASE_POINT
|
|
1275
|
-
* values. This slows down first getPublicKey() by milliseconds (see Speed section),
|
|
1276
|
-
* but allows to speed-up subsequent getPublicKey() calls up to 20x.
|
|
1277
|
-
* @param windowSize 2, 4, 8, 16
|
|
1278
|
-
*/
|
|
1279
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
|
1280
|
-
point._setWindowSize(windowSize);
|
|
1281
|
-
point.multiply(BigInt(3));
|
|
1282
|
-
return point;
|
|
1283
|
-
}
|
|
1284
|
-
};
|
|
1285
|
-
return {
|
|
1286
|
-
CURVE,
|
|
1287
|
-
getPublicKey,
|
|
1288
|
-
sign,
|
|
1289
|
-
verify,
|
|
1290
|
-
ExtendedPoint: Point,
|
|
1291
|
-
utils
|
|
1292
|
-
};
|
|
1293
|
-
}
|
|
1294
|
-
|
|
1295
|
-
// ../../node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/esm/ed25519.js
|
|
1296
|
-
var ED25519_P = BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949");
|
|
1297
|
-
var ED25519_SQRT_M1 = BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
1298
|
-
BigInt(0);
|
|
1299
|
-
var _1n5 = BigInt(1);
|
|
1300
|
-
var _2n4 = BigInt(2);
|
|
1301
|
-
var _5n2 = BigInt(5);
|
|
1302
|
-
var _10n = BigInt(10);
|
|
1303
|
-
var _20n = BigInt(20);
|
|
1304
|
-
var _40n = BigInt(40);
|
|
1305
|
-
var _80n = BigInt(80);
|
|
1306
|
-
function ed25519_pow_2_252_3(x) {
|
|
1307
|
-
const P = ED25519_P;
|
|
230
|
+
return r;
|
|
231
|
+
};
|
|
232
|
+
var pow_2_252_3 = (x) => {
|
|
1308
233
|
const x2 = x * x % P;
|
|
1309
234
|
const b2 = x2 * x % P;
|
|
1310
|
-
const b4 = pow2(b2,
|
|
1311
|
-
const b5 = pow2(b4,
|
|
1312
|
-
const b10 = pow2(b5,
|
|
1313
|
-
const b20 = pow2(b10,
|
|
1314
|
-
const b40 = pow2(b20,
|
|
1315
|
-
const b80 = pow2(b40,
|
|
1316
|
-
const b160 = pow2(b80,
|
|
1317
|
-
const b240 = pow2(b160,
|
|
1318
|
-
const b250 = pow2(b240,
|
|
1319
|
-
const pow_p_5_8 = pow2(b250,
|
|
235
|
+
const b4 = pow2(b2, 2n) * b2 % P;
|
|
236
|
+
const b5 = pow2(b4, 1n) * x % P;
|
|
237
|
+
const b10 = pow2(b5, 5n) * b5 % P;
|
|
238
|
+
const b20 = pow2(b10, 10n) * b10 % P;
|
|
239
|
+
const b40 = pow2(b20, 20n) * b20 % P;
|
|
240
|
+
const b80 = pow2(b40, 40n) * b40 % P;
|
|
241
|
+
const b160 = pow2(b80, 80n) * b80 % P;
|
|
242
|
+
const b240 = pow2(b160, 80n) * b80 % P;
|
|
243
|
+
const b250 = pow2(b240, 10n) * b10 % P;
|
|
244
|
+
const pow_p_5_8 = pow2(b250, 2n) * x % P;
|
|
1320
245
|
return { pow_p_5_8, b2 };
|
|
1321
|
-
}
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
const P = ED25519_P;
|
|
1330
|
-
const v3 = mod(v * v * v, P);
|
|
1331
|
-
const v7 = mod(v3 * v3 * v, P);
|
|
1332
|
-
const pow3 = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
1333
|
-
let x = mod(u * v3 * pow3, P);
|
|
1334
|
-
const vx2 = mod(v * x * x, P);
|
|
246
|
+
};
|
|
247
|
+
var RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
248
|
+
var uvRatio = (u, v) => {
|
|
249
|
+
const v3 = mod(v * v * v);
|
|
250
|
+
const v7 = mod(v3 * v3 * v);
|
|
251
|
+
const pow = pow_2_252_3(u * v7).pow_p_5_8;
|
|
252
|
+
let x = mod(u * v3 * pow);
|
|
253
|
+
const vx2 = mod(v * x * x);
|
|
1335
254
|
const root1 = x;
|
|
1336
|
-
const root2 = mod(x *
|
|
255
|
+
const root2 = mod(x * RM1);
|
|
1337
256
|
const useRoot1 = vx2 === u;
|
|
1338
|
-
const useRoot2 = vx2 === mod(-u
|
|
1339
|
-
const noRoot = vx2 === mod(-u *
|
|
257
|
+
const useRoot2 = vx2 === mod(-u);
|
|
258
|
+
const noRoot = vx2 === mod(-u * RM1);
|
|
1340
259
|
if (useRoot1)
|
|
1341
260
|
x = root1;
|
|
1342
261
|
if (useRoot2 || noRoot)
|
|
1343
262
|
x = root2;
|
|
1344
|
-
if (
|
|
1345
|
-
x = mod(-x
|
|
263
|
+
if ((mod(x) & 1n) === 1n)
|
|
264
|
+
x = mod(-x);
|
|
1346
265
|
return { isValid: useRoot1 || useRoot2, value: x };
|
|
1347
|
-
}
|
|
1348
|
-
var Fp = Field(ED25519_P, void 0, true);
|
|
1349
|
-
var ed25519Defaults = {
|
|
1350
|
-
// Param: a
|
|
1351
|
-
a: BigInt(-1),
|
|
1352
|
-
// d is equal to -121665/121666 over finite field.
|
|
1353
|
-
// Negative number is P - number, and division is invert(number, P)
|
|
1354
|
-
d: BigInt("37095705934669439343138083508754565189542113879843219016388785533085940283555"),
|
|
1355
|
-
// Finite field 𝔽p over which we'll do calculations; 2n**255n - 19n
|
|
1356
|
-
Fp,
|
|
1357
|
-
// Subgroup order: how many points curve has
|
|
1358
|
-
// 2n**252n + 27742317777372353535851937790883648493n;
|
|
1359
|
-
n: BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989"),
|
|
1360
|
-
// Cofactor
|
|
1361
|
-
h: BigInt(8),
|
|
1362
|
-
// Base point (x, y) aka generator point
|
|
1363
|
-
Gx: BigInt("15112221349535400772501151409588531511454012693041857206046113283949847762202"),
|
|
1364
|
-
Gy: BigInt("46316835694926478169428394003475163141307993866256225615783033603165251855960"),
|
|
1365
|
-
hash: sha512,
|
|
1366
|
-
randomBytes,
|
|
1367
|
-
adjustScalarBytes,
|
|
1368
|
-
// dom2
|
|
1369
|
-
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
1370
|
-
// Constant-time, u/√v
|
|
1371
|
-
uvRatio
|
|
1372
266
|
};
|
|
1373
|
-
var
|
|
1374
|
-
var
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
267
|
+
var modL_LE = (hash) => mod(b2n_LE(hash), N);
|
|
268
|
+
var _shaS;
|
|
269
|
+
var sha512a = (...m) => etc.sha512Async(...m);
|
|
270
|
+
var sha512s = (...m) => (
|
|
271
|
+
// Sync SHA512, not set by default
|
|
272
|
+
typeof _shaS === "function" ? _shaS(...m) : err("etc.sha512Sync not set")
|
|
273
|
+
);
|
|
274
|
+
var hash2extK = (hashed) => {
|
|
275
|
+
const head = hashed.slice(0, 32);
|
|
276
|
+
head[0] &= 248;
|
|
277
|
+
head[31] &= 127;
|
|
278
|
+
head[31] |= 64;
|
|
279
|
+
const prefix = hashed.slice(32, 64);
|
|
280
|
+
const scalar = modL_LE(head);
|
|
281
|
+
const point = G.mul(scalar);
|
|
282
|
+
const pointBytes = point.toRawBytes();
|
|
283
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
284
|
+
};
|
|
285
|
+
var getExtendedPublicKeyAsync = (priv) => sha512a(toU8(priv, 32)).then(hash2extK);
|
|
286
|
+
var getExtendedPublicKey = (priv) => hash2extK(sha512s(toU8(priv, 32)));
|
|
287
|
+
var getPublicKeyAsync = (priv) => getExtendedPublicKeyAsync(priv).then((p) => p.pointBytes);
|
|
288
|
+
function hashFinish(asynchronous, res) {
|
|
289
|
+
if (asynchronous)
|
|
290
|
+
return sha512a(res.hashable).then(res.finish);
|
|
291
|
+
return res.finish(sha512s(res.hashable));
|
|
292
|
+
}
|
|
293
|
+
var _sign = (e, rBytes, msg) => {
|
|
294
|
+
const { pointBytes: P2, scalar: s } = e;
|
|
295
|
+
const r = modL_LE(rBytes);
|
|
296
|
+
const R = G.mul(r).toRawBytes();
|
|
297
|
+
const hashable = concatB(R, P2, msg);
|
|
298
|
+
const finish = (hashed) => {
|
|
299
|
+
const S = mod(r + modL_LE(hashed) * s, N);
|
|
300
|
+
return au8(concatB(R, n2b_32LE(S)), 64);
|
|
301
|
+
};
|
|
302
|
+
return { hashable, finish };
|
|
303
|
+
};
|
|
304
|
+
var signAsync = async (msg, privKey) => {
|
|
305
|
+
const m = toU8(msg);
|
|
306
|
+
const e = await getExtendedPublicKeyAsync(privKey);
|
|
307
|
+
const rBytes = await sha512a(e.prefix, m);
|
|
308
|
+
return hashFinish(true, _sign(e, rBytes, m));
|
|
309
|
+
};
|
|
310
|
+
var _verify = (sig, msg, pub) => {
|
|
311
|
+
msg = toU8(msg);
|
|
312
|
+
sig = toU8(sig, 64);
|
|
313
|
+
const A = Point.fromHex(pub, false);
|
|
314
|
+
const R = Point.fromHex(sig.slice(0, 32), false);
|
|
315
|
+
const s = b2n_LE(sig.slice(32, 64));
|
|
316
|
+
const SB = G.mul(s, false);
|
|
317
|
+
const hashable = concatB(R.toRawBytes(), A.toRawBytes(), msg);
|
|
318
|
+
const finish = (hashed) => {
|
|
319
|
+
const k = modL_LE(hashed);
|
|
320
|
+
const RkA = R.add(A.mul(k, false));
|
|
321
|
+
return RkA.add(SB.negate()).clearCofactor().is0();
|
|
322
|
+
};
|
|
323
|
+
return { hashable, finish };
|
|
324
|
+
};
|
|
325
|
+
var verifyAsync = async (s, m, p) => hashFinish(true, _verify(s, m, p));
|
|
326
|
+
var cr = () => (
|
|
327
|
+
// We support: 1) browsers 2) node.js 19+
|
|
328
|
+
typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0
|
|
329
|
+
);
|
|
330
|
+
var etc = {
|
|
331
|
+
bytesToHex: b2h,
|
|
332
|
+
hexToBytes: h2b,
|
|
333
|
+
concatBytes: concatB,
|
|
334
|
+
mod,
|
|
335
|
+
invert,
|
|
336
|
+
randomBytes: (len) => {
|
|
337
|
+
const crypto = cr();
|
|
338
|
+
if (!crypto)
|
|
339
|
+
err("crypto.getRandomValues must be defined");
|
|
340
|
+
return crypto.getRandomValues(u8n(len));
|
|
341
|
+
},
|
|
342
|
+
sha512Async: async (...messages) => {
|
|
343
|
+
const crypto = cr();
|
|
344
|
+
if (!crypto)
|
|
345
|
+
err("crypto.subtle or etc.sha512Async must be defined");
|
|
346
|
+
const m = concatB(...messages);
|
|
347
|
+
return u8n(await crypto.subtle.digest("SHA-512", m.buffer));
|
|
348
|
+
},
|
|
349
|
+
sha512Sync: void 0
|
|
350
|
+
// Actual logic below
|
|
351
|
+
};
|
|
352
|
+
Object.defineProperties(etc, { sha512Sync: {
|
|
353
|
+
configurable: false,
|
|
354
|
+
get() {
|
|
355
|
+
return _shaS;
|
|
356
|
+
},
|
|
357
|
+
set(f) {
|
|
358
|
+
if (!_shaS)
|
|
359
|
+
_shaS = f;
|
|
360
|
+
}
|
|
361
|
+
} });
|
|
362
|
+
var utils = {
|
|
363
|
+
getExtendedPublicKeyAsync,
|
|
364
|
+
getExtendedPublicKey,
|
|
365
|
+
randomPrivateKey: () => etc.randomBytes(32),
|
|
366
|
+
precompute(w = 8, p = G) {
|
|
367
|
+
p.multiply(3n);
|
|
368
|
+
return p;
|
|
369
|
+
}
|
|
370
|
+
// no-op
|
|
371
|
+
};
|
|
372
|
+
var W = 8;
|
|
373
|
+
var precompute = () => {
|
|
374
|
+
const points = [];
|
|
375
|
+
const windows = 256 / W + 1;
|
|
376
|
+
let p = G, b = p;
|
|
377
|
+
for (let w = 0; w < windows; w++) {
|
|
378
|
+
b = p;
|
|
379
|
+
points.push(b);
|
|
380
|
+
for (let i = 1; i < 2 ** (W - 1); i++) {
|
|
381
|
+
b = b.add(p);
|
|
382
|
+
points.push(b);
|
|
383
|
+
}
|
|
384
|
+
p = b.double();
|
|
385
|
+
}
|
|
386
|
+
return points;
|
|
387
|
+
};
|
|
388
|
+
var wNAF = (n) => {
|
|
389
|
+
const comp = Gpows || (Gpows = precompute());
|
|
390
|
+
const neg = (cnd, p2) => {
|
|
391
|
+
let n2 = p2.negate();
|
|
392
|
+
return cnd ? n2 : p2;
|
|
393
|
+
};
|
|
394
|
+
let p = I, f = G;
|
|
395
|
+
const windows = 1 + 256 / W;
|
|
396
|
+
const wsize = 2 ** (W - 1);
|
|
397
|
+
const mask = BigInt(2 ** W - 1);
|
|
398
|
+
const maxNum = 2 ** W;
|
|
399
|
+
const shiftBy = BigInt(W);
|
|
400
|
+
for (let w = 0; w < windows; w++) {
|
|
401
|
+
const off = w * wsize;
|
|
402
|
+
let wbits = Number(n & mask);
|
|
403
|
+
n >>= shiftBy;
|
|
404
|
+
if (wbits > wsize) {
|
|
405
|
+
wbits -= maxNum;
|
|
406
|
+
n += 1n;
|
|
407
|
+
}
|
|
408
|
+
const off1 = off, off2 = off + Math.abs(wbits) - 1;
|
|
409
|
+
const cnd1 = w % 2 !== 0, cnd2 = wbits < 0;
|
|
410
|
+
if (wbits === 0) {
|
|
411
|
+
f = f.add(neg(cnd1, comp[off1]));
|
|
412
|
+
} else {
|
|
413
|
+
p = p.add(neg(cnd2, comp[off2]));
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return { p, f };
|
|
417
|
+
};
|
|
1385
418
|
|
|
1386
419
|
// src/secrets.ts
|
|
1387
420
|
var PROHIBITED_KEY_USAGES = /* @__PURE__ */ new Set([
|
|
@@ -1392,15 +425,62 @@
|
|
|
1392
425
|
"unwrapKey",
|
|
1393
426
|
"wrapKey"
|
|
1394
427
|
]);
|
|
428
|
+
var ED25519_PKCS8_HEADER = (
|
|
429
|
+
// prettier-ignore
|
|
430
|
+
[
|
|
431
|
+
/**
|
|
432
|
+
* PKCS#8 header
|
|
433
|
+
*/
|
|
434
|
+
48,
|
|
435
|
+
// ASN.1 sequence tag
|
|
436
|
+
46,
|
|
437
|
+
// Length of sequence (46 more bytes)
|
|
438
|
+
2,
|
|
439
|
+
// ASN.1 integer tag
|
|
440
|
+
1,
|
|
441
|
+
// Length of integer
|
|
442
|
+
0,
|
|
443
|
+
// Version number
|
|
444
|
+
48,
|
|
445
|
+
// ASN.1 sequence tag
|
|
446
|
+
5,
|
|
447
|
+
// Length of sequence
|
|
448
|
+
6,
|
|
449
|
+
// ASN.1 object identifier tag
|
|
450
|
+
3,
|
|
451
|
+
// Length of object identifier
|
|
452
|
+
// Edwards curve algorithms identifier https://oid-rep.orange-labs.fr/get/1.3.101.112
|
|
453
|
+
43,
|
|
454
|
+
// iso(1) / identified-organization(3) (The first node is multiplied by the decimal 40 and the result is added to the value of the second node)
|
|
455
|
+
101,
|
|
456
|
+
// thawte(101)
|
|
457
|
+
// Ed25519 identifier
|
|
458
|
+
112,
|
|
459
|
+
// id-Ed25519(112)
|
|
460
|
+
/**
|
|
461
|
+
* Private key payload
|
|
462
|
+
*/
|
|
463
|
+
4,
|
|
464
|
+
// ASN.1 octet string tag
|
|
465
|
+
34,
|
|
466
|
+
// String length (34 more bytes)
|
|
467
|
+
// Private key bytes as octet string
|
|
468
|
+
4,
|
|
469
|
+
// ASN.1 octet string tag
|
|
470
|
+
32
|
|
471
|
+
// String length (32 bytes)
|
|
472
|
+
]
|
|
473
|
+
);
|
|
1395
474
|
function bufferSourceToUint8Array(data) {
|
|
1396
475
|
return data instanceof Uint8Array ? data : new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
|
|
1397
476
|
}
|
|
1398
477
|
var storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT;
|
|
1399
|
-
|
|
478
|
+
var publicKeyBytesStore;
|
|
479
|
+
function createKeyPairFromBytes(bytes, extractable, keyUsages) {
|
|
1400
480
|
const keyPair = createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages);
|
|
1401
481
|
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
1402
|
-
cache.set(keyPair.privateKey,
|
|
1403
|
-
cache.set(keyPair.publicKey,
|
|
482
|
+
cache.set(keyPair.privateKey, bytes);
|
|
483
|
+
cache.set(keyPair.publicKey, bytes);
|
|
1404
484
|
return keyPair;
|
|
1405
485
|
}
|
|
1406
486
|
function createKeyPair_INTERNAL_ONLY_DO_NOT_EXPORT(extractable, keyUsages) {
|
|
@@ -1432,58 +512,117 @@
|
|
|
1432
512
|
});
|
|
1433
513
|
}
|
|
1434
514
|
function getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key) {
|
|
1435
|
-
const secretKeyBytes = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT
|
|
515
|
+
const secretKeyBytes = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT == null ? void 0 : storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT.get(key);
|
|
1436
516
|
if (secretKeyBytes === void 0) {
|
|
1437
517
|
throw new Error("Could not find secret key material associated with this `CryptoKey`");
|
|
1438
518
|
}
|
|
1439
519
|
return secretKeyBytes;
|
|
1440
520
|
}
|
|
1441
|
-
function
|
|
521
|
+
async function getPublicKeyBytes(key) {
|
|
522
|
+
const publicKeyStore = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
|
|
523
|
+
const fromPublicStore = publicKeyStore.get(key);
|
|
524
|
+
if (fromPublicStore)
|
|
525
|
+
return fromPublicStore;
|
|
526
|
+
const publicKeyBytes = await getPublicKeyAsync(getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key));
|
|
527
|
+
publicKeyStore.set(key, publicKeyBytes);
|
|
528
|
+
return publicKeyBytes;
|
|
529
|
+
}
|
|
530
|
+
async function exportKeyPolyfill(format, key) {
|
|
531
|
+
if (key.extractable === false) {
|
|
532
|
+
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
533
|
+
}
|
|
1442
534
|
switch (format) {
|
|
1443
535
|
case "raw": {
|
|
1444
|
-
if (key.extractable === false) {
|
|
1445
|
-
throw new DOMException("key is not extractable", "InvalidAccessException");
|
|
1446
|
-
}
|
|
1447
536
|
if (key.type !== "public") {
|
|
1448
537
|
throw new DOMException(`Unable to export a raw Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
1449
538
|
}
|
|
1450
|
-
const publicKeyBytes =
|
|
539
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
1451
540
|
return publicKeyBytes;
|
|
1452
541
|
}
|
|
542
|
+
case "pkcs8": {
|
|
543
|
+
if (key.type !== "private") {
|
|
544
|
+
throw new DOMException(`Unable to export a pkcs8 Ed25519 ${key.type} key`, "InvalidAccessError");
|
|
545
|
+
}
|
|
546
|
+
const secretKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
547
|
+
return new Uint8Array([...ED25519_PKCS8_HEADER, ...secretKeyBytes]);
|
|
548
|
+
}
|
|
1453
549
|
default:
|
|
1454
550
|
throw new Error(`Exporting polyfilled Ed25519 keys in the "${format}" format is unimplemented`);
|
|
1455
551
|
}
|
|
1456
552
|
}
|
|
1457
553
|
function generateKeyPolyfill(extractable, keyUsages) {
|
|
1458
|
-
const privateKeyBytes =
|
|
554
|
+
const privateKeyBytes = utils.randomPrivateKey();
|
|
1459
555
|
const keyPair = createKeyPairFromBytes(privateKeyBytes, extractable, keyUsages);
|
|
1460
556
|
return keyPair;
|
|
1461
557
|
}
|
|
1462
558
|
function isPolyfilledKey(key) {
|
|
1463
|
-
return !!storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT
|
|
559
|
+
return !!(storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT == null ? void 0 : storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT.has(key)) || !!(publicKeyBytesStore == null ? void 0 : publicKeyBytesStore.has(key));
|
|
1464
560
|
}
|
|
1465
|
-
function signPolyfill(key, data) {
|
|
561
|
+
async function signPolyfill(key, data) {
|
|
1466
562
|
if (key.type !== "private" || !key.usages.includes("sign")) {
|
|
1467
563
|
throw new DOMException("Unable to use this key to sign", "InvalidAccessError");
|
|
1468
564
|
}
|
|
1469
565
|
const privateKeyBytes = getSecretKeyBytes_INTERNAL_ONLY_DO_NOT_EXPORT(key);
|
|
1470
566
|
const payload = bufferSourceToUint8Array(data);
|
|
1471
|
-
const signature =
|
|
567
|
+
const signature = await signAsync(payload, privateKeyBytes);
|
|
1472
568
|
return signature;
|
|
1473
569
|
}
|
|
1474
|
-
function verifyPolyfill(key, signature, data) {
|
|
570
|
+
async function verifyPolyfill(key, signature, data) {
|
|
1475
571
|
if (key.type !== "public" || !key.usages.includes("verify")) {
|
|
1476
572
|
throw new DOMException("Unable to use this key to verify", "InvalidAccessError");
|
|
1477
573
|
}
|
|
1478
|
-
const publicKeyBytes =
|
|
574
|
+
const publicKeyBytes = await getPublicKeyBytes(key);
|
|
1479
575
|
try {
|
|
1480
|
-
return
|
|
576
|
+
return await verifyAsync(bufferSourceToUint8Array(signature), bufferSourceToUint8Array(data), publicKeyBytes);
|
|
1481
577
|
} catch {
|
|
1482
578
|
return false;
|
|
1483
579
|
}
|
|
1484
580
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
581
|
+
function importKeyPolyfill(format, keyData, extractable, keyUsages) {
|
|
582
|
+
const bytes = bufferSourceToUint8Array(keyData);
|
|
583
|
+
if (format === "raw") {
|
|
584
|
+
if (keyUsages.some((usage) => usage === "sign" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
585
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
586
|
+
}
|
|
587
|
+
if (bytes.length !== 32) {
|
|
588
|
+
throw new DOMException("Ed25519 raw keys must be exactly 32-bytes", "DataError");
|
|
589
|
+
}
|
|
590
|
+
const publicKey = {
|
|
591
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
592
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
593
|
+
extractable,
|
|
594
|
+
type: "public",
|
|
595
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "verify"))
|
|
596
|
+
};
|
|
597
|
+
const cache = publicKeyBytesStore || (publicKeyBytesStore = /* @__PURE__ */ new WeakMap());
|
|
598
|
+
cache.set(publicKey, bytes);
|
|
599
|
+
return publicKey;
|
|
600
|
+
}
|
|
601
|
+
if (format === "pkcs8") {
|
|
602
|
+
if (keyUsages.some((usage) => usage === "verify" || PROHIBITED_KEY_USAGES.has(usage))) {
|
|
603
|
+
throw new DOMException("Unsupported key usage for a Ed25519 key", "SyntaxError");
|
|
604
|
+
}
|
|
605
|
+
if (bytes.length !== 48) {
|
|
606
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
607
|
+
}
|
|
608
|
+
const header = bytes.slice(0, 16);
|
|
609
|
+
if (!header.every((val, i) => val === ED25519_PKCS8_HEADER[i])) {
|
|
610
|
+
throw new DOMException("Invalid keyData", "DataError");
|
|
611
|
+
}
|
|
612
|
+
const secretKeyBytes = bytes.slice(16);
|
|
613
|
+
const privateKey = {
|
|
614
|
+
[Symbol.toStringTag]: "CryptoKey",
|
|
615
|
+
algorithm: Object.freeze({ name: "Ed25519" }),
|
|
616
|
+
extractable,
|
|
617
|
+
type: "private",
|
|
618
|
+
usages: Object.freeze(keyUsages.filter((usage) => usage === "sign"))
|
|
619
|
+
};
|
|
620
|
+
const cache = storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT || (storageKeyBySecretKey_INTERNAL_ONLY_DO_NOT_EXPORT = /* @__PURE__ */ new WeakMap());
|
|
621
|
+
cache.set(privateKey, secretKeyBytes);
|
|
622
|
+
return privateKey;
|
|
623
|
+
}
|
|
624
|
+
throw new Error(`Importing Ed25519 keys in the "${format}" format is unimplemented`);
|
|
625
|
+
}
|
|
1487
626
|
if (globalThis.isSecureContext) {
|
|
1488
627
|
const originalCryptoObject = globalThis.crypto || (globalThis.crypto = {});
|
|
1489
628
|
const originalSubtleCrypto = originalCryptoObject.subtle || (originalCryptoObject.subtle = {});
|
|
@@ -1491,7 +630,7 @@
|
|
|
1491
630
|
originalSubtleCrypto.exportKey = async (...args) => {
|
|
1492
631
|
const [_, key] = args;
|
|
1493
632
|
if (isPolyfilledKey(key)) {
|
|
1494
|
-
return exportKeyPolyfill(...args);
|
|
633
|
+
return await exportKeyPolyfill(...args);
|
|
1495
634
|
} else if (originalExportKey) {
|
|
1496
635
|
return await originalExportKey.apply(originalSubtleCrypto, args);
|
|
1497
636
|
} else {
|
|
@@ -1550,7 +689,7 @@
|
|
|
1550
689
|
const [_, key] = args;
|
|
1551
690
|
if (isPolyfilledKey(key)) {
|
|
1552
691
|
const [_2, ...rest] = args;
|
|
1553
|
-
return signPolyfill(...rest);
|
|
692
|
+
return await signPolyfill(...rest);
|
|
1554
693
|
} else if (originalSign) {
|
|
1555
694
|
return await originalSign.apply(originalSubtleCrypto, args);
|
|
1556
695
|
} else {
|
|
@@ -1562,33 +701,65 @@
|
|
|
1562
701
|
const [_, key] = args;
|
|
1563
702
|
if (isPolyfilledKey(key)) {
|
|
1564
703
|
const [_2, ...rest] = args;
|
|
1565
|
-
return verifyPolyfill(...rest);
|
|
704
|
+
return await verifyPolyfill(...rest);
|
|
1566
705
|
} else if (originalVerify) {
|
|
1567
706
|
return await originalVerify.apply(originalSubtleCrypto, args);
|
|
1568
707
|
} else {
|
|
1569
708
|
throw new TypeError("No native `verify` function exists to handle this call");
|
|
1570
709
|
}
|
|
1571
710
|
};
|
|
711
|
+
const originalImportKey = originalSubtleCrypto.importKey;
|
|
712
|
+
let originalImportKeySupportsEd25519;
|
|
713
|
+
originalSubtleCrypto.importKey = async (...args) => {
|
|
714
|
+
const [format, keyData, algorithm] = args;
|
|
715
|
+
if (algorithm !== "Ed25519") {
|
|
716
|
+
if (originalImportKey) {
|
|
717
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
718
|
+
} else {
|
|
719
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
let optimisticallyImportedKey;
|
|
723
|
+
if (originalImportKeySupportsEd25519 === void 0) {
|
|
724
|
+
originalImportKeySupportsEd25519 = new Promise((resolve) => {
|
|
725
|
+
if (!originalImportKey) {
|
|
726
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
originalImportKey.apply(originalSubtleCrypto, args).then((key) => {
|
|
730
|
+
{
|
|
731
|
+
console.warn(
|
|
732
|
+
"`@solana/webcrypto-ed25519-polyfill` was included in an environment that supports Ed25519 key manipulation natively. Falling back to the native implementation. Consider including this polyfill only in environments where Ed25519 is not supported."
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
if (originalSubtleCrypto.importKey !== originalImportKey) {
|
|
736
|
+
originalSubtleCrypto.importKey = originalImportKey;
|
|
737
|
+
}
|
|
738
|
+
optimisticallyImportedKey = key;
|
|
739
|
+
resolve(originalImportKeySupportsEd25519 = true);
|
|
740
|
+
}).catch(() => {
|
|
741
|
+
resolve(originalImportKeySupportsEd25519 = false);
|
|
742
|
+
});
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
if (typeof originalImportKey === "boolean" ? originalImportKeySupportsEd25519 : await originalImportKeySupportsEd25519) {
|
|
746
|
+
if (optimisticallyImportedKey) {
|
|
747
|
+
return optimisticallyImportedKey;
|
|
748
|
+
} else if (originalImportKey) {
|
|
749
|
+
return await originalImportKey.apply(originalSubtleCrypto, args);
|
|
750
|
+
} else {
|
|
751
|
+
throw new TypeError("No native `importKey` function exists to handle this call");
|
|
752
|
+
}
|
|
753
|
+
} else {
|
|
754
|
+
const [_format, _keyData, _algorithm, extractable, keyUsages] = args;
|
|
755
|
+
return importKeyPolyfill(format, keyData, extractable, keyUsages);
|
|
756
|
+
}
|
|
757
|
+
};
|
|
1572
758
|
}
|
|
1573
759
|
/*! Bundled license information:
|
|
1574
760
|
|
|
1575
|
-
@noble/
|
|
1576
|
-
(*! noble-
|
|
1577
|
-
|
|
1578
|
-
@noble/curves/esm/abstract/utils.js:
|
|
1579
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
1580
|
-
|
|
1581
|
-
@noble/curves/esm/abstract/modular.js:
|
|
1582
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
1583
|
-
|
|
1584
|
-
@noble/curves/esm/abstract/curve.js:
|
|
1585
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
1586
|
-
|
|
1587
|
-
@noble/curves/esm/abstract/edwards.js:
|
|
1588
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
1589
|
-
|
|
1590
|
-
@noble/curves/esm/ed25519.js:
|
|
1591
|
-
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
761
|
+
@noble/ed25519/index.js:
|
|
762
|
+
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
1592
763
|
*/
|
|
1593
764
|
|
|
1594
765
|
})();
|