@icp-sdk/vetkeys 0.6.0 → 0.7.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/lib/encrypted_maps.es.js +60 -90
- package/dist/lib/index.es.js +18 -3737
- package/dist/lib/key_manager.es.js +7 -6
- package/dist/lib/utils-CiynM__c.mjs +901 -0
- package/package.json +7 -4
- package/dist/lib/actor-CFYb2LZx.mjs +0 -8929
package/dist/lib/index.es.js
CHANGED
|
@@ -1,3739 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
function ce(e) {
|
|
3
|
-
return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
|
|
4
|
-
}
|
|
5
|
-
function Ut(e) {
|
|
6
|
-
if (!Number.isSafeInteger(e) || e < 0)
|
|
7
|
-
throw new Error("positive integer expected, got " + e);
|
|
8
|
-
}
|
|
9
|
-
function Q(e, ...t) {
|
|
10
|
-
if (!ce(e))
|
|
11
|
-
throw new Error("Uint8Array expected");
|
|
12
|
-
if (t.length > 0 && !t.includes(e.length))
|
|
13
|
-
throw new Error("Uint8Array expected of length " + t + ", got length=" + e.length);
|
|
14
|
-
}
|
|
15
|
-
function Ue(e) {
|
|
16
|
-
if (typeof e != "function" || typeof e.create != "function")
|
|
17
|
-
throw new Error("Hash should be wrapped by utils.createHasher");
|
|
18
|
-
Ut(e.outputLen), Ut(e.blockLen);
|
|
19
|
-
}
|
|
20
|
-
function Lt(e, t = !0) {
|
|
21
|
-
if (e.destroyed)
|
|
22
|
-
throw new Error("Hash instance has been destroyed");
|
|
23
|
-
if (t && e.finished)
|
|
24
|
-
throw new Error("Hash#digest() has already been called");
|
|
25
|
-
}
|
|
26
|
-
function xn(e, t) {
|
|
27
|
-
Q(e);
|
|
28
|
-
const r = t.outputLen;
|
|
29
|
-
if (e.length < r)
|
|
30
|
-
throw new Error("digestInto() expects output buffer of length at least " + r);
|
|
31
|
-
}
|
|
32
|
-
function Qn(e) {
|
|
33
|
-
return new Uint32Array(e.buffer, e.byteOffset, Math.floor(e.byteLength / 4));
|
|
34
|
-
}
|
|
35
|
-
function St(...e) {
|
|
36
|
-
for (let t = 0; t < e.length; t++)
|
|
37
|
-
e[t].fill(0);
|
|
38
|
-
}
|
|
39
|
-
function be(e) {
|
|
40
|
-
return new DataView(e.buffer, e.byteOffset, e.byteLength);
|
|
41
|
-
}
|
|
42
|
-
function at(e, t) {
|
|
43
|
-
return e << 32 - t | e >>> t;
|
|
44
|
-
}
|
|
45
|
-
const Jn = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
46
|
-
function tr(e) {
|
|
47
|
-
return e << 24 & 4278190080 | e << 8 & 16711680 | e >>> 8 & 65280 | e >>> 24 & 255;
|
|
48
|
-
}
|
|
49
|
-
function er(e) {
|
|
50
|
-
for (let t = 0; t < e.length; t++)
|
|
51
|
-
e[t] = tr(e[t]);
|
|
52
|
-
return e;
|
|
53
|
-
}
|
|
54
|
-
const Ve = Jn ? (e) => e : er, Bn = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", nr = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0"));
|
|
55
|
-
function Yt(e) {
|
|
56
|
-
if (Q(e), Bn)
|
|
57
|
-
return e.toHex();
|
|
58
|
-
let t = "";
|
|
59
|
-
for (let r = 0; r < e.length; r++)
|
|
60
|
-
t += nr[e[r]];
|
|
61
|
-
return t;
|
|
62
|
-
}
|
|
63
|
-
const ut = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
64
|
-
function Fe(e) {
|
|
65
|
-
if (e >= ut._0 && e <= ut._9)
|
|
66
|
-
return e - ut._0;
|
|
67
|
-
if (e >= ut.A && e <= ut.F)
|
|
68
|
-
return e - (ut.A - 10);
|
|
69
|
-
if (e >= ut.a && e <= ut.f)
|
|
70
|
-
return e - (ut.a - 10);
|
|
71
|
-
}
|
|
72
|
-
function _n(e) {
|
|
73
|
-
if (typeof e != "string")
|
|
74
|
-
throw new Error("hex string expected, got " + typeof e);
|
|
75
|
-
if (Bn)
|
|
76
|
-
return Uint8Array.fromHex(e);
|
|
77
|
-
const t = e.length, r = t / 2;
|
|
78
|
-
if (t % 2)
|
|
79
|
-
throw new Error("hex string expected, got unpadded hex of length " + t);
|
|
80
|
-
const n = new Uint8Array(r);
|
|
81
|
-
for (let s = 0, o = 0; s < r; s++, o += 2) {
|
|
82
|
-
const i = Fe(e.charCodeAt(o)), c = Fe(e.charCodeAt(o + 1));
|
|
83
|
-
if (i === void 0 || c === void 0) {
|
|
84
|
-
const a = e[o] + e[o + 1];
|
|
85
|
-
throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + o);
|
|
86
|
-
}
|
|
87
|
-
n[s] = i * 16 + c;
|
|
88
|
-
}
|
|
89
|
-
return n;
|
|
90
|
-
}
|
|
91
|
-
function $t(e) {
|
|
92
|
-
if (typeof e != "string")
|
|
93
|
-
throw new Error("string expected");
|
|
94
|
-
return new Uint8Array(new TextEncoder().encode(e));
|
|
95
|
-
}
|
|
96
|
-
function vt(e) {
|
|
97
|
-
return typeof e == "string" && (e = $t(e)), Q(e), e;
|
|
98
|
-
}
|
|
99
|
-
function V(...e) {
|
|
100
|
-
let t = 0;
|
|
101
|
-
for (let n = 0; n < e.length; n++) {
|
|
102
|
-
const s = e[n];
|
|
103
|
-
Q(s), t += s.length;
|
|
104
|
-
}
|
|
105
|
-
const r = new Uint8Array(t);
|
|
106
|
-
for (let n = 0, s = 0; n < e.length; n++) {
|
|
107
|
-
const o = e[n];
|
|
108
|
-
r.set(o, s), s += o.length;
|
|
109
|
-
}
|
|
110
|
-
return r;
|
|
111
|
-
}
|
|
112
|
-
class Le {
|
|
113
|
-
}
|
|
114
|
-
function rr(e) {
|
|
115
|
-
const t = (n) => e().update(vt(n)).digest(), r = e();
|
|
116
|
-
return t.outputLen = r.outputLen, t.blockLen = r.blockLen, t.create = () => e(), t;
|
|
117
|
-
}
|
|
118
|
-
function sr(e) {
|
|
119
|
-
const t = (n, s) => e(s).update(vt(n)).digest(), r = e({});
|
|
120
|
-
return t.outputLen = r.outputLen, t.blockLen = r.blockLen, t.create = (n) => e(n), t;
|
|
121
|
-
}
|
|
122
|
-
function or(e = 32) {
|
|
123
|
-
if (Tt && typeof Tt.getRandomValues == "function")
|
|
124
|
-
return Tt.getRandomValues(new Uint8Array(e));
|
|
125
|
-
if (Tt && typeof Tt.randomBytes == "function")
|
|
126
|
-
return Uint8Array.from(Tt.randomBytes(e));
|
|
127
|
-
throw new Error("crypto.getRandomValues must be defined");
|
|
128
|
-
}
|
|
129
|
-
function ir(e, t, r, n) {
|
|
130
|
-
if (typeof e.setBigUint64 == "function")
|
|
131
|
-
return e.setBigUint64(t, r, n);
|
|
132
|
-
const s = BigInt(32), o = BigInt(4294967295), i = Number(r >> s & o), c = Number(r & o), a = n ? 4 : 0, u = n ? 0 : 4;
|
|
133
|
-
e.setUint32(t + a, i, n), e.setUint32(t + u, c, n);
|
|
134
|
-
}
|
|
135
|
-
function cr(e, t, r) {
|
|
136
|
-
return e & t ^ ~e & r;
|
|
137
|
-
}
|
|
138
|
-
function ar(e, t, r) {
|
|
139
|
-
return e & t ^ e & r ^ t & r;
|
|
140
|
-
}
|
|
141
|
-
class fr extends Le {
|
|
142
|
-
constructor(t, r, n, s) {
|
|
143
|
-
super(), this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.blockLen = t, this.outputLen = r, this.padOffset = n, this.isLE = s, this.buffer = new Uint8Array(t), this.view = be(this.buffer);
|
|
144
|
-
}
|
|
145
|
-
update(t) {
|
|
146
|
-
Lt(this), t = vt(t), Q(t);
|
|
147
|
-
const { view: r, buffer: n, blockLen: s } = this, o = t.length;
|
|
148
|
-
for (let i = 0; i < o; ) {
|
|
149
|
-
const c = Math.min(s - this.pos, o - i);
|
|
150
|
-
if (c === s) {
|
|
151
|
-
const a = be(t);
|
|
152
|
-
for (; s <= o - i; i += s)
|
|
153
|
-
this.process(a, i);
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
n.set(t.subarray(i, i + c), this.pos), this.pos += c, i += c, this.pos === s && (this.process(r, 0), this.pos = 0);
|
|
157
|
-
}
|
|
158
|
-
return this.length += t.length, this.roundClean(), this;
|
|
159
|
-
}
|
|
160
|
-
digestInto(t) {
|
|
161
|
-
Lt(this), xn(t, this), this.finished = !0;
|
|
162
|
-
const { buffer: r, view: n, blockLen: s, isLE: o } = this;
|
|
163
|
-
let { pos: i } = this;
|
|
164
|
-
r[i++] = 128, St(this.buffer.subarray(i)), this.padOffset > s - i && (this.process(n, 0), i = 0);
|
|
165
|
-
for (let d = i; d < s; d++)
|
|
166
|
-
r[d] = 0;
|
|
167
|
-
ir(n, s - 8, BigInt(this.length * 8), o), this.process(n, 0);
|
|
168
|
-
const c = be(t), a = this.outputLen;
|
|
169
|
-
if (a % 4)
|
|
170
|
-
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
171
|
-
const u = a / 4, l = this.get();
|
|
172
|
-
if (u > l.length)
|
|
173
|
-
throw new Error("_sha2: outputLen bigger than state");
|
|
174
|
-
for (let d = 0; d < u; d++)
|
|
175
|
-
c.setUint32(4 * d, l[d], o);
|
|
176
|
-
}
|
|
177
|
-
digest() {
|
|
178
|
-
const { buffer: t, outputLen: r } = this;
|
|
179
|
-
this.digestInto(t);
|
|
180
|
-
const n = t.slice(0, r);
|
|
181
|
-
return this.destroy(), n;
|
|
182
|
-
}
|
|
183
|
-
_cloneInto(t) {
|
|
184
|
-
t || (t = new this.constructor()), t.set(...this.get());
|
|
185
|
-
const { blockLen: r, buffer: n, length: s, finished: o, destroyed: i, pos: c } = this;
|
|
186
|
-
return t.destroyed = i, t.finished = o, t.length = s, t.pos = c, s % r && t.buffer.set(n), t;
|
|
187
|
-
}
|
|
188
|
-
clone() {
|
|
189
|
-
return this._cloneInto();
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
const bt = /* @__PURE__ */ Uint32Array.from([
|
|
193
|
-
1779033703,
|
|
194
|
-
3144134277,
|
|
195
|
-
1013904242,
|
|
196
|
-
2773480762,
|
|
197
|
-
1359893119,
|
|
198
|
-
2600822924,
|
|
199
|
-
528734635,
|
|
200
|
-
1541459225
|
|
201
|
-
]), Wt = /* @__PURE__ */ BigInt(2 ** 32 - 1), Ce = /* @__PURE__ */ BigInt(32);
|
|
202
|
-
function ur(e, t = !1) {
|
|
203
|
-
return t ? { h: Number(e & Wt), l: Number(e >> Ce & Wt) } : { h: Number(e >> Ce & Wt) | 0, l: Number(e & Wt) | 0 };
|
|
204
|
-
}
|
|
205
|
-
function dr(e, t = !1) {
|
|
206
|
-
const r = e.length;
|
|
207
|
-
let n = new Uint32Array(r), s = new Uint32Array(r);
|
|
208
|
-
for (let o = 0; o < r; o++) {
|
|
209
|
-
const { h: i, l: c } = ur(e[o], t);
|
|
210
|
-
[n[o], s[o]] = [i, c];
|
|
211
|
-
}
|
|
212
|
-
return [n, s];
|
|
213
|
-
}
|
|
214
|
-
const lr = (e, t, r) => e << r | t >>> 32 - r, br = (e, t, r) => t << r | e >>> 32 - r, hr = (e, t, r) => t << r - 32 | e >>> 64 - r, mr = (e, t, r) => e << r - 32 | t >>> 64 - r, gr = /* @__PURE__ */ Uint32Array.from([
|
|
215
|
-
1116352408,
|
|
216
|
-
1899447441,
|
|
217
|
-
3049323471,
|
|
218
|
-
3921009573,
|
|
219
|
-
961987163,
|
|
220
|
-
1508970993,
|
|
221
|
-
2453635748,
|
|
222
|
-
2870763221,
|
|
223
|
-
3624381080,
|
|
224
|
-
310598401,
|
|
225
|
-
607225278,
|
|
226
|
-
1426881987,
|
|
227
|
-
1925078388,
|
|
228
|
-
2162078206,
|
|
229
|
-
2614888103,
|
|
230
|
-
3248222580,
|
|
231
|
-
3835390401,
|
|
232
|
-
4022224774,
|
|
233
|
-
264347078,
|
|
234
|
-
604807628,
|
|
235
|
-
770255983,
|
|
236
|
-
1249150122,
|
|
237
|
-
1555081692,
|
|
238
|
-
1996064986,
|
|
239
|
-
2554220882,
|
|
240
|
-
2821834349,
|
|
241
|
-
2952996808,
|
|
242
|
-
3210313671,
|
|
243
|
-
3336571891,
|
|
244
|
-
3584528711,
|
|
245
|
-
113926993,
|
|
246
|
-
338241895,
|
|
247
|
-
666307205,
|
|
248
|
-
773529912,
|
|
249
|
-
1294757372,
|
|
250
|
-
1396182291,
|
|
251
|
-
1695183700,
|
|
252
|
-
1986661051,
|
|
253
|
-
2177026350,
|
|
254
|
-
2456956037,
|
|
255
|
-
2730485921,
|
|
256
|
-
2820302411,
|
|
257
|
-
3259730800,
|
|
258
|
-
3345764771,
|
|
259
|
-
3516065817,
|
|
260
|
-
3600352804,
|
|
261
|
-
4094571909,
|
|
262
|
-
275423344,
|
|
263
|
-
430227734,
|
|
264
|
-
506948616,
|
|
265
|
-
659060556,
|
|
266
|
-
883997877,
|
|
267
|
-
958139571,
|
|
268
|
-
1322822218,
|
|
269
|
-
1537002063,
|
|
270
|
-
1747873779,
|
|
271
|
-
1955562222,
|
|
272
|
-
2024104815,
|
|
273
|
-
2227730452,
|
|
274
|
-
2361852424,
|
|
275
|
-
2428436474,
|
|
276
|
-
2756734187,
|
|
277
|
-
3204031479,
|
|
278
|
-
3329325298
|
|
279
|
-
]), ht = /* @__PURE__ */ new Uint32Array(64);
|
|
280
|
-
class yr extends fr {
|
|
281
|
-
constructor(t = 32) {
|
|
282
|
-
super(64, t, 8, !1), this.A = bt[0] | 0, this.B = bt[1] | 0, this.C = bt[2] | 0, this.D = bt[3] | 0, this.E = bt[4] | 0, this.F = bt[5] | 0, this.G = bt[6] | 0, this.H = bt[7] | 0;
|
|
283
|
-
}
|
|
284
|
-
get() {
|
|
285
|
-
const { A: t, B: r, C: n, D: s, E: o, F: i, G: c, H: a } = this;
|
|
286
|
-
return [t, r, n, s, o, i, c, a];
|
|
287
|
-
}
|
|
288
|
-
// prettier-ignore
|
|
289
|
-
set(t, r, n, s, o, i, c, a) {
|
|
290
|
-
this.A = t | 0, this.B = r | 0, this.C = n | 0, this.D = s | 0, this.E = o | 0, this.F = i | 0, this.G = c | 0, this.H = a | 0;
|
|
291
|
-
}
|
|
292
|
-
process(t, r) {
|
|
293
|
-
for (let d = 0; d < 16; d++, r += 4)
|
|
294
|
-
ht[d] = t.getUint32(r, !1);
|
|
295
|
-
for (let d = 16; d < 64; d++) {
|
|
296
|
-
const f = ht[d - 15], b = ht[d - 2], m = at(f, 7) ^ at(f, 18) ^ f >>> 3, g = at(b, 17) ^ at(b, 19) ^ b >>> 10;
|
|
297
|
-
ht[d] = g + ht[d - 7] + m + ht[d - 16] | 0;
|
|
298
|
-
}
|
|
299
|
-
let { A: n, B: s, C: o, D: i, E: c, F: a, G: u, H: l } = this;
|
|
300
|
-
for (let d = 0; d < 64; d++) {
|
|
301
|
-
const f = at(c, 6) ^ at(c, 11) ^ at(c, 25), b = l + f + cr(c, a, u) + gr[d] + ht[d] | 0, g = (at(n, 2) ^ at(n, 13) ^ at(n, 22)) + ar(n, s, o) | 0;
|
|
302
|
-
l = u, u = a, a = c, c = i + b | 0, i = o, o = s, s = n, n = b + g | 0;
|
|
303
|
-
}
|
|
304
|
-
n = n + this.A | 0, s = s + this.B | 0, o = o + this.C | 0, i = i + this.D | 0, c = c + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, l = l + this.H | 0, this.set(n, s, o, i, c, a, u, l);
|
|
305
|
-
}
|
|
306
|
-
roundClean() {
|
|
307
|
-
St(ht);
|
|
308
|
-
}
|
|
309
|
-
destroy() {
|
|
310
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0), St(this.buffer);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
const Sn = /* @__PURE__ */ rr(() => new yr());
|
|
314
|
-
const He = /* @__PURE__ */ BigInt(0), ee = /* @__PURE__ */ BigInt(1);
|
|
315
|
-
function ze(e, t = "") {
|
|
316
|
-
if (typeof e != "boolean") {
|
|
317
|
-
const r = t && `"${t}"`;
|
|
318
|
-
throw new Error(r + "expected boolean, got type=" + typeof e);
|
|
319
|
-
}
|
|
320
|
-
return e;
|
|
321
|
-
}
|
|
322
|
-
function $e(e, t, r = "") {
|
|
323
|
-
const n = ce(e), s = e?.length;
|
|
324
|
-
if (!n || t !== void 0) {
|
|
325
|
-
const i = r && `"${r}" `, c = "", a = n ? `length=${s}` : `type=${typeof e}`;
|
|
326
|
-
throw new Error(i + "expected Uint8Array" + c + ", got " + a);
|
|
327
|
-
}
|
|
328
|
-
return e;
|
|
329
|
-
}
|
|
330
|
-
function vn(e) {
|
|
331
|
-
if (typeof e != "string")
|
|
332
|
-
throw new Error("hex string expected, got " + typeof e);
|
|
333
|
-
return e === "" ? He : BigInt("0x" + e);
|
|
334
|
-
}
|
|
335
|
-
function ft(e) {
|
|
336
|
-
return vn(Yt(e));
|
|
337
|
-
}
|
|
338
|
-
function An(e) {
|
|
339
|
-
return Q(e), vn(Yt(Uint8Array.from(e).reverse()));
|
|
340
|
-
}
|
|
341
|
-
function C(e, t) {
|
|
342
|
-
return _n(e.toString(16).padStart(t * 2, "0"));
|
|
343
|
-
}
|
|
344
|
-
function On(e, t) {
|
|
345
|
-
return C(e, t).reverse();
|
|
346
|
-
}
|
|
347
|
-
function Ht(e, t, r) {
|
|
348
|
-
let n;
|
|
349
|
-
if (typeof t == "string")
|
|
350
|
-
try {
|
|
351
|
-
n = _n(t);
|
|
352
|
-
} catch (o) {
|
|
353
|
-
throw new Error(e + " must be hex string or Uint8Array, cause: " + o);
|
|
354
|
-
}
|
|
355
|
-
else if (ce(t))
|
|
356
|
-
n = Uint8Array.from(t);
|
|
357
|
-
else
|
|
358
|
-
throw new Error(e + " must be hex string or Uint8Array");
|
|
359
|
-
const s = n.length;
|
|
360
|
-
if (typeof r == "number" && s !== r)
|
|
361
|
-
throw new Error(e + " of length " + r + " expected, got " + s);
|
|
362
|
-
return n;
|
|
363
|
-
}
|
|
364
|
-
const he = (e) => typeof e == "bigint" && He <= e;
|
|
365
|
-
function pr(e, t, r) {
|
|
366
|
-
return he(e) && he(t) && he(r) && t <= e && e < r;
|
|
367
|
-
}
|
|
368
|
-
function Vt(e) {
|
|
369
|
-
let t;
|
|
370
|
-
for (t = 0; e > He; e >>= ee, t += 1)
|
|
371
|
-
;
|
|
372
|
-
return t;
|
|
373
|
-
}
|
|
374
|
-
function wr(e, t) {
|
|
375
|
-
return e >> BigInt(t) & ee;
|
|
376
|
-
}
|
|
377
|
-
const Ot = (e) => (ee << BigInt(e)) - ee;
|
|
378
|
-
function Er(e) {
|
|
379
|
-
return typeof e == "function" && Number.isSafeInteger(e.outputLen);
|
|
380
|
-
}
|
|
381
|
-
function ke(e, t, r = {}) {
|
|
382
|
-
if (!e || typeof e != "object")
|
|
383
|
-
throw new Error("expected valid options object");
|
|
384
|
-
function n(s, o, i) {
|
|
385
|
-
const c = e[s];
|
|
386
|
-
if (i && c === void 0)
|
|
387
|
-
return;
|
|
388
|
-
const a = typeof c;
|
|
389
|
-
if (a !== o || c === null)
|
|
390
|
-
throw new Error(`param "${s}" is invalid: expected ${o}, got ${a}`);
|
|
391
|
-
}
|
|
392
|
-
Object.entries(t).forEach(([s, o]) => n(s, o, !1)), Object.entries(r).forEach(([s, o]) => n(s, o, !0));
|
|
393
|
-
}
|
|
394
|
-
const In = () => {
|
|
395
|
-
throw new Error("not implemented");
|
|
396
|
-
};
|
|
397
|
-
function Ae(e) {
|
|
398
|
-
const t = /* @__PURE__ */ new WeakMap();
|
|
399
|
-
return (r, ...n) => {
|
|
400
|
-
const s = t.get(r);
|
|
401
|
-
if (s !== void 0)
|
|
402
|
-
return s;
|
|
403
|
-
const o = e(r, ...n);
|
|
404
|
-
return t.set(r, o), o;
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
const tt = BigInt(0), W = BigInt(1), Et = /* @__PURE__ */ BigInt(2), Nn = /* @__PURE__ */ BigInt(3), Tn = /* @__PURE__ */ BigInt(4), qn = /* @__PURE__ */ BigInt(5), xr = /* @__PURE__ */ BigInt(7), Rn = /* @__PURE__ */ BigInt(8), Br = /* @__PURE__ */ BigInt(9), Gn = /* @__PURE__ */ BigInt(16);
|
|
408
|
-
function et(e, t) {
|
|
409
|
-
const r = e % t;
|
|
410
|
-
return r >= tt ? r : t + r;
|
|
411
|
-
}
|
|
412
|
-
function je(e, t) {
|
|
413
|
-
if (e === tt)
|
|
414
|
-
throw new Error("invert: expected non-zero number");
|
|
415
|
-
if (t <= tt)
|
|
416
|
-
throw new Error("invert: expected positive modulus, got " + t);
|
|
417
|
-
let r = et(e, t), n = t, s = tt, o = W;
|
|
418
|
-
for (; r !== tt; ) {
|
|
419
|
-
const c = n / r, a = n % r, u = s - o * c;
|
|
420
|
-
n = r, r = a, s = o, o = u;
|
|
421
|
-
}
|
|
422
|
-
if (n !== W)
|
|
423
|
-
throw new Error("invert: does not exist");
|
|
424
|
-
return et(s, t);
|
|
425
|
-
}
|
|
426
|
-
function Pe(e, t, r) {
|
|
427
|
-
if (!e.eql(e.sqr(t), r))
|
|
428
|
-
throw new Error("Cannot find square root");
|
|
429
|
-
}
|
|
430
|
-
function Un(e, t) {
|
|
431
|
-
const r = (e.ORDER + W) / Tn, n = e.pow(t, r);
|
|
432
|
-
return Pe(e, n, t), n;
|
|
433
|
-
}
|
|
434
|
-
function _r(e, t) {
|
|
435
|
-
const r = (e.ORDER - qn) / Rn, n = e.mul(t, Et), s = e.pow(n, r), o = e.mul(t, s), i = e.mul(e.mul(o, Et), s), c = e.mul(o, e.sub(i, e.ONE));
|
|
436
|
-
return Pe(e, c, t), c;
|
|
437
|
-
}
|
|
438
|
-
function Sr(e) {
|
|
439
|
-
const t = Pt(e), r = Ln(e), n = r(t, t.neg(t.ONE)), s = r(t, n), o = r(t, t.neg(n)), i = (e + xr) / Gn;
|
|
440
|
-
return (c, a) => {
|
|
441
|
-
let u = c.pow(a, i), l = c.mul(u, n);
|
|
442
|
-
const d = c.mul(u, s), f = c.mul(u, o), b = c.eql(c.sqr(l), a), m = c.eql(c.sqr(d), a);
|
|
443
|
-
u = c.cmov(u, l, b), l = c.cmov(f, d, m);
|
|
444
|
-
const g = c.eql(c.sqr(l), a), w = c.cmov(u, l, g);
|
|
445
|
-
return Pe(c, w, a), w;
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
function Ln(e) {
|
|
449
|
-
if (e < Nn)
|
|
450
|
-
throw new Error("sqrt is not defined for small field");
|
|
451
|
-
let t = e - W, r = 0;
|
|
452
|
-
for (; t % Et === tt; )
|
|
453
|
-
t /= Et, r++;
|
|
454
|
-
let n = Et;
|
|
455
|
-
const s = Pt(e);
|
|
456
|
-
for (; ne(s, n) === 1; )
|
|
457
|
-
if (n++ > 1e3)
|
|
458
|
-
throw new Error("Cannot find square root: probably non-prime P");
|
|
459
|
-
if (r === 1)
|
|
460
|
-
return Un;
|
|
461
|
-
let o = s.pow(n, t);
|
|
462
|
-
const i = (t + W) / Et;
|
|
463
|
-
return function(a, u) {
|
|
464
|
-
if (a.is0(u))
|
|
465
|
-
return u;
|
|
466
|
-
if (ne(a, u) !== 1)
|
|
467
|
-
throw new Error("Cannot find square root");
|
|
468
|
-
let l = r, d = a.mul(a.ONE, o), f = a.pow(u, t), b = a.pow(u, i);
|
|
469
|
-
for (; !a.eql(f, a.ONE); ) {
|
|
470
|
-
if (a.is0(f))
|
|
471
|
-
return a.ZERO;
|
|
472
|
-
let m = 1, g = a.sqr(f);
|
|
473
|
-
for (; !a.eql(g, a.ONE); )
|
|
474
|
-
if (m++, g = a.sqr(g), m === l)
|
|
475
|
-
throw new Error("Cannot find square root");
|
|
476
|
-
const w = W << BigInt(l - m - 1), T = a.pow(d, w);
|
|
477
|
-
l = m, d = a.sqr(T), f = a.mul(f, d), b = a.mul(b, T);
|
|
478
|
-
}
|
|
479
|
-
return b;
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
function vr(e) {
|
|
483
|
-
return e % Tn === Nn ? Un : e % Rn === qn ? _r : e % Gn === Br ? Sr(e) : Ln(e);
|
|
484
|
-
}
|
|
485
|
-
const Ar = [
|
|
486
|
-
"create",
|
|
487
|
-
"isValid",
|
|
488
|
-
"is0",
|
|
489
|
-
"neg",
|
|
490
|
-
"inv",
|
|
491
|
-
"sqrt",
|
|
492
|
-
"sqr",
|
|
493
|
-
"eql",
|
|
494
|
-
"add",
|
|
495
|
-
"sub",
|
|
496
|
-
"mul",
|
|
497
|
-
"pow",
|
|
498
|
-
"div",
|
|
499
|
-
"addN",
|
|
500
|
-
"subN",
|
|
501
|
-
"mulN",
|
|
502
|
-
"sqrN"
|
|
503
|
-
];
|
|
504
|
-
function Hn(e) {
|
|
505
|
-
const t = {
|
|
506
|
-
ORDER: "bigint",
|
|
507
|
-
MASK: "bigint",
|
|
508
|
-
BYTES: "number",
|
|
509
|
-
BITS: "number"
|
|
510
|
-
}, r = Ar.reduce((n, s) => (n[s] = "function", n), t);
|
|
511
|
-
return ke(e, r), e;
|
|
512
|
-
}
|
|
513
|
-
function ae(e, t, r) {
|
|
514
|
-
if (r < tt)
|
|
515
|
-
throw new Error("invalid exponent, negatives unsupported");
|
|
516
|
-
if (r === tt)
|
|
517
|
-
return e.ONE;
|
|
518
|
-
if (r === W)
|
|
519
|
-
return t;
|
|
520
|
-
let n = e.ONE, s = t;
|
|
521
|
-
for (; r > tt; )
|
|
522
|
-
r & W && (n = e.mul(n, s)), s = e.sqr(s), r >>= W;
|
|
523
|
-
return n;
|
|
524
|
-
}
|
|
525
|
-
function It(e, t, r = !1) {
|
|
526
|
-
const n = new Array(t.length).fill(r ? e.ZERO : void 0), s = t.reduce((i, c, a) => e.is0(c) ? i : (n[a] = i, e.mul(i, c)), e.ONE), o = e.inv(s);
|
|
527
|
-
return t.reduceRight((i, c, a) => e.is0(c) ? i : (n[a] = e.mul(i, n[a]), e.mul(i, c)), o), n;
|
|
528
|
-
}
|
|
529
|
-
function ne(e, t) {
|
|
530
|
-
const r = (e.ORDER - W) / Et, n = e.pow(t, r), s = e.eql(n, e.ONE), o = e.eql(n, e.ZERO), i = e.eql(n, e.neg(e.ONE));
|
|
531
|
-
if (!s && !o && !i)
|
|
532
|
-
throw new Error("invalid Legendre symbol result");
|
|
533
|
-
return s ? 1 : o ? 0 : -1;
|
|
534
|
-
}
|
|
535
|
-
function Or(e, t) {
|
|
536
|
-
t !== void 0 && Ut(t);
|
|
537
|
-
const r = t !== void 0 ? t : e.toString(2).length, n = Math.ceil(r / 8);
|
|
538
|
-
return { nBitLength: r, nByteLength: n };
|
|
539
|
-
}
|
|
540
|
-
function Pt(e, t, r = !1, n = {}) {
|
|
541
|
-
if (e <= tt)
|
|
542
|
-
throw new Error("invalid field: expected ORDER > 0, got " + e);
|
|
543
|
-
let s, o, i = !1, c;
|
|
544
|
-
if (typeof t == "object" && t != null) {
|
|
545
|
-
if (n.sqrt || r)
|
|
546
|
-
throw new Error("cannot specify opts in two arguments");
|
|
547
|
-
const f = t;
|
|
548
|
-
f.BITS && (s = f.BITS), f.sqrt && (o = f.sqrt), typeof f.isLE == "boolean" && (r = f.isLE), typeof f.modFromBytes == "boolean" && (i = f.modFromBytes), c = f.allowedLengths;
|
|
549
|
-
} else
|
|
550
|
-
typeof t == "number" && (s = t), n.sqrt && (o = n.sqrt);
|
|
551
|
-
const { nBitLength: a, nByteLength: u } = Or(e, s);
|
|
552
|
-
if (u > 2048)
|
|
553
|
-
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
554
|
-
let l;
|
|
555
|
-
const d = Object.freeze({
|
|
556
|
-
ORDER: e,
|
|
557
|
-
isLE: r,
|
|
558
|
-
BITS: a,
|
|
559
|
-
BYTES: u,
|
|
560
|
-
MASK: Ot(a),
|
|
561
|
-
ZERO: tt,
|
|
562
|
-
ONE: W,
|
|
563
|
-
allowedLengths: c,
|
|
564
|
-
create: (f) => et(f, e),
|
|
565
|
-
isValid: (f) => {
|
|
566
|
-
if (typeof f != "bigint")
|
|
567
|
-
throw new Error("invalid field element: expected bigint, got " + typeof f);
|
|
568
|
-
return tt <= f && f < e;
|
|
569
|
-
},
|
|
570
|
-
is0: (f) => f === tt,
|
|
571
|
-
// is valid and invertible
|
|
572
|
-
isValidNot0: (f) => !d.is0(f) && d.isValid(f),
|
|
573
|
-
isOdd: (f) => (f & W) === W,
|
|
574
|
-
neg: (f) => et(-f, e),
|
|
575
|
-
eql: (f, b) => f === b,
|
|
576
|
-
sqr: (f) => et(f * f, e),
|
|
577
|
-
add: (f, b) => et(f + b, e),
|
|
578
|
-
sub: (f, b) => et(f - b, e),
|
|
579
|
-
mul: (f, b) => et(f * b, e),
|
|
580
|
-
pow: (f, b) => ae(d, f, b),
|
|
581
|
-
div: (f, b) => et(f * je(b, e), e),
|
|
582
|
-
// Same as above, but doesn't normalize
|
|
583
|
-
sqrN: (f) => f * f,
|
|
584
|
-
addN: (f, b) => f + b,
|
|
585
|
-
subN: (f, b) => f - b,
|
|
586
|
-
mulN: (f, b) => f * b,
|
|
587
|
-
inv: (f) => je(f, e),
|
|
588
|
-
sqrt: o || ((f) => (l || (l = vr(e)), l(d, f))),
|
|
589
|
-
toBytes: (f) => r ? On(f, u) : C(f, u),
|
|
590
|
-
fromBytes: (f, b = !0) => {
|
|
591
|
-
if (c) {
|
|
592
|
-
if (!c.includes(f.length) || f.length > u)
|
|
593
|
-
throw new Error("Field.fromBytes: expected " + c + " bytes, got " + f.length);
|
|
594
|
-
const g = new Uint8Array(u);
|
|
595
|
-
g.set(f, r ? 0 : g.length - f.length), f = g;
|
|
596
|
-
}
|
|
597
|
-
if (f.length !== u)
|
|
598
|
-
throw new Error("Field.fromBytes: expected " + u + " bytes, got " + f.length);
|
|
599
|
-
let m = r ? An(f) : ft(f);
|
|
600
|
-
if (i && (m = et(m, e)), !b && !d.isValid(m))
|
|
601
|
-
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
602
|
-
return m;
|
|
603
|
-
},
|
|
604
|
-
// TODO: we don't need it here, move out to separate fn
|
|
605
|
-
invertBatch: (f) => It(d, f),
|
|
606
|
-
// We can't move this out because Fp6, Fp12 implement it
|
|
607
|
-
// and it's unclear what to return in there.
|
|
608
|
-
cmov: (f, b, m) => m ? b : f
|
|
609
|
-
});
|
|
610
|
-
return Object.freeze(d);
|
|
611
|
-
}
|
|
612
|
-
function kn(e) {
|
|
613
|
-
if (typeof e != "bigint")
|
|
614
|
-
throw new Error("field order must be bigint");
|
|
615
|
-
const t = e.toString(2).length;
|
|
616
|
-
return Math.ceil(t / 8);
|
|
617
|
-
}
|
|
618
|
-
function Pn(e) {
|
|
619
|
-
const t = kn(e);
|
|
620
|
-
return t + Math.ceil(t / 2);
|
|
621
|
-
}
|
|
622
|
-
function Ir(e, t, r = !1) {
|
|
623
|
-
const n = e.length, s = kn(t), o = Pn(t);
|
|
624
|
-
if (n < 16 || n < o || n > 1024)
|
|
625
|
-
throw new Error("expected " + o + "-1024 bytes of input, got " + n);
|
|
626
|
-
const i = r ? An(e) : ft(e), c = et(i, t - W) + W;
|
|
627
|
-
return r ? On(c, s) : C(c, s);
|
|
628
|
-
}
|
|
629
|
-
const kt = BigInt(0), xt = BigInt(1);
|
|
630
|
-
function re(e, t) {
|
|
631
|
-
const r = t.negate();
|
|
632
|
-
return e ? r : t;
|
|
633
|
-
}
|
|
634
|
-
function Kt(e, t) {
|
|
635
|
-
const r = It(e.Fp, t.map((n) => n.Z));
|
|
636
|
-
return t.map((n, s) => e.fromAffine(n.toAffine(r[s])));
|
|
637
|
-
}
|
|
638
|
-
function Dn(e, t) {
|
|
639
|
-
if (!Number.isSafeInteger(e) || e <= 0 || e > t)
|
|
640
|
-
throw new Error("invalid window size, expected [1.." + t + "], got W=" + e);
|
|
641
|
-
}
|
|
642
|
-
function me(e, t) {
|
|
643
|
-
Dn(e, t);
|
|
644
|
-
const r = Math.ceil(t / e) + 1, n = 2 ** (e - 1), s = 2 ** e, o = Ot(e), i = BigInt(e);
|
|
645
|
-
return { windows: r, windowSize: n, mask: o, maxNumber: s, shiftBy: i };
|
|
646
|
-
}
|
|
647
|
-
function Xe(e, t, r) {
|
|
648
|
-
const { windowSize: n, mask: s, maxNumber: o, shiftBy: i } = r;
|
|
649
|
-
let c = Number(e & s), a = e >> i;
|
|
650
|
-
c > n && (c -= o, a += xt);
|
|
651
|
-
const u = t * n, l = u + Math.abs(c) - 1, d = c === 0, f = c < 0, b = t % 2 !== 0;
|
|
652
|
-
return { nextN: a, offset: l, isZero: d, isNeg: f, isNegF: b, offsetF: u };
|
|
653
|
-
}
|
|
654
|
-
function Nr(e, t) {
|
|
655
|
-
if (!Array.isArray(e))
|
|
656
|
-
throw new Error("array expected");
|
|
657
|
-
e.forEach((r, n) => {
|
|
658
|
-
if (!(r instanceof t))
|
|
659
|
-
throw new Error("invalid point at index " + n);
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
function Tr(e, t) {
|
|
663
|
-
if (!Array.isArray(e))
|
|
664
|
-
throw new Error("array of scalars expected");
|
|
665
|
-
e.forEach((r, n) => {
|
|
666
|
-
if (!t.isValid(r))
|
|
667
|
-
throw new Error("invalid scalar at index " + n);
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
const ge = /* @__PURE__ */ new WeakMap(), Mn = /* @__PURE__ */ new WeakMap();
|
|
671
|
-
function ye(e) {
|
|
672
|
-
return Mn.get(e) || 1;
|
|
673
|
-
}
|
|
674
|
-
function We(e) {
|
|
675
|
-
if (e !== kt)
|
|
676
|
-
throw new Error("invalid wNAF");
|
|
677
|
-
}
|
|
678
|
-
class qr {
|
|
679
|
-
// Parametrized with a given Point class (not individual point)
|
|
680
|
-
constructor(t, r) {
|
|
681
|
-
this.BASE = t.BASE, this.ZERO = t.ZERO, this.Fn = t.Fn, this.bits = r;
|
|
682
|
-
}
|
|
683
|
-
// non-const time multiplication ladder
|
|
684
|
-
_unsafeLadder(t, r, n = this.ZERO) {
|
|
685
|
-
let s = t;
|
|
686
|
-
for (; r > kt; )
|
|
687
|
-
r & xt && (n = n.add(s)), s = s.double(), r >>= xt;
|
|
688
|
-
return n;
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* Creates a wNAF precomputation window. Used for caching.
|
|
692
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
693
|
-
* Number of precomputed points depends on the curve size:
|
|
694
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
695
|
-
* - 𝑊 is the window size
|
|
696
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
697
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
698
|
-
* @param point Point instance
|
|
699
|
-
* @param W window size
|
|
700
|
-
* @returns precomputed point tables flattened to a single array
|
|
701
|
-
*/
|
|
702
|
-
precomputeWindow(t, r) {
|
|
703
|
-
const { windows: n, windowSize: s } = me(r, this.bits), o = [];
|
|
704
|
-
let i = t, c = i;
|
|
705
|
-
for (let a = 0; a < n; a++) {
|
|
706
|
-
c = i, o.push(c);
|
|
707
|
-
for (let u = 1; u < s; u++)
|
|
708
|
-
c = c.add(i), o.push(c);
|
|
709
|
-
i = c.double();
|
|
710
|
-
}
|
|
711
|
-
return o;
|
|
712
|
-
}
|
|
713
|
-
/**
|
|
714
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
715
|
-
* More compact implementation:
|
|
716
|
-
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
717
|
-
* @returns real and fake (for const-time) points
|
|
718
|
-
*/
|
|
719
|
-
wNAF(t, r, n) {
|
|
720
|
-
if (!this.Fn.isValid(n))
|
|
721
|
-
throw new Error("invalid scalar");
|
|
722
|
-
let s = this.ZERO, o = this.BASE;
|
|
723
|
-
const i = me(t, this.bits);
|
|
724
|
-
for (let c = 0; c < i.windows; c++) {
|
|
725
|
-
const { nextN: a, offset: u, isZero: l, isNeg: d, isNegF: f, offsetF: b } = Xe(n, c, i);
|
|
726
|
-
n = a, l ? o = o.add(re(f, r[b])) : s = s.add(re(d, r[u]));
|
|
727
|
-
}
|
|
728
|
-
return We(n), { p: s, f: o };
|
|
729
|
-
}
|
|
730
|
-
/**
|
|
731
|
-
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
732
|
-
* @param acc accumulator point to add result of multiplication
|
|
733
|
-
* @returns point
|
|
734
|
-
*/
|
|
735
|
-
wNAFUnsafe(t, r, n, s = this.ZERO) {
|
|
736
|
-
const o = me(t, this.bits);
|
|
737
|
-
for (let i = 0; i < o.windows && n !== kt; i++) {
|
|
738
|
-
const { nextN: c, offset: a, isZero: u, isNeg: l } = Xe(n, i, o);
|
|
739
|
-
if (n = c, !u) {
|
|
740
|
-
const d = r[a];
|
|
741
|
-
s = s.add(l ? d.negate() : d);
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
return We(n), s;
|
|
745
|
-
}
|
|
746
|
-
getPrecomputes(t, r, n) {
|
|
747
|
-
let s = ge.get(r);
|
|
748
|
-
return s || (s = this.precomputeWindow(r, t), t !== 1 && (typeof n == "function" && (s = n(s)), ge.set(r, s))), s;
|
|
749
|
-
}
|
|
750
|
-
cached(t, r, n) {
|
|
751
|
-
const s = ye(t);
|
|
752
|
-
return this.wNAF(s, this.getPrecomputes(s, t, n), r);
|
|
753
|
-
}
|
|
754
|
-
unsafe(t, r, n, s) {
|
|
755
|
-
const o = ye(t);
|
|
756
|
-
return o === 1 ? this._unsafeLadder(t, r, s) : this.wNAFUnsafe(o, this.getPrecomputes(o, t, n), r, s);
|
|
757
|
-
}
|
|
758
|
-
// We calculate precomputes for elliptic curve point multiplication
|
|
759
|
-
// using windowed method. This specifies window size and
|
|
760
|
-
// stores precomputed values. Usually only base point would be precomputed.
|
|
761
|
-
createCache(t, r) {
|
|
762
|
-
Dn(r, this.bits), Mn.set(t, r), ge.delete(t);
|
|
763
|
-
}
|
|
764
|
-
hasCache(t) {
|
|
765
|
-
return ye(t) !== 1;
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
function Rr(e, t, r, n) {
|
|
769
|
-
let s = t, o = e.ZERO, i = e.ZERO;
|
|
770
|
-
for (; r > kt || n > kt; )
|
|
771
|
-
r & xt && (o = o.add(s)), n & xt && (i = i.add(s)), s = s.double(), r >>= xt, n >>= xt;
|
|
772
|
-
return { p1: o, p2: i };
|
|
773
|
-
}
|
|
774
|
-
function Gr(e, t, r, n) {
|
|
775
|
-
Nr(r, e), Tr(n, t);
|
|
776
|
-
const s = r.length, o = n.length;
|
|
777
|
-
if (s !== o)
|
|
778
|
-
throw new Error("arrays of points and scalars must have equal length");
|
|
779
|
-
const i = e.ZERO, c = Vt(BigInt(s));
|
|
780
|
-
let a = 1;
|
|
781
|
-
c > 12 ? a = c - 3 : c > 4 ? a = c - 2 : c > 0 && (a = 2);
|
|
782
|
-
const u = Ot(a), l = new Array(Number(u) + 1).fill(i), d = Math.floor((t.BITS - 1) / a) * a;
|
|
783
|
-
let f = i;
|
|
784
|
-
for (let b = d; b >= 0; b -= a) {
|
|
785
|
-
l.fill(i);
|
|
786
|
-
for (let g = 0; g < o; g++) {
|
|
787
|
-
const w = n[g], T = Number(w >> BigInt(b) & u);
|
|
788
|
-
l[T] = l[T].add(r[g]);
|
|
789
|
-
}
|
|
790
|
-
let m = i;
|
|
791
|
-
for (let g = l.length - 1, w = i; g > 0; g--)
|
|
792
|
-
w = w.add(l[g]), m = m.add(w);
|
|
793
|
-
if (f = f.add(m), b !== 0)
|
|
794
|
-
for (let g = 0; g < a; g++)
|
|
795
|
-
f = f.double();
|
|
796
|
-
}
|
|
797
|
-
return f;
|
|
798
|
-
}
|
|
799
|
-
function Qe(e, t, r) {
|
|
800
|
-
if (t) {
|
|
801
|
-
if (t.ORDER !== e)
|
|
802
|
-
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
803
|
-
return Hn(t), t;
|
|
804
|
-
} else
|
|
805
|
-
return Pt(e, { isLE: r });
|
|
806
|
-
}
|
|
807
|
-
function Ur(e, t, r = {}, n) {
|
|
808
|
-
if (n === void 0 && (n = e === "edwards"), !t || typeof t != "object")
|
|
809
|
-
throw new Error(`expected valid ${e} CURVE object`);
|
|
810
|
-
for (const a of ["p", "n", "h"]) {
|
|
811
|
-
const u = t[a];
|
|
812
|
-
if (!(typeof u == "bigint" && u > kt))
|
|
813
|
-
throw new Error(`CURVE.${a} must be positive bigint`);
|
|
814
|
-
}
|
|
815
|
-
const s = Qe(t.p, r.Fp, n), o = Qe(t.n, r.Fn, n), c = ["Gx", "Gy", "a", "b"];
|
|
816
|
-
for (const a of c)
|
|
817
|
-
if (!s.isValid(t[a]))
|
|
818
|
-
throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`);
|
|
819
|
-
return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: s, Fn: o };
|
|
820
|
-
}
|
|
821
|
-
const Lr = ft;
|
|
822
|
-
function gt(e, t) {
|
|
823
|
-
if (Ft(e), Ft(t), e < 0 || e >= 1 << 8 * t)
|
|
824
|
-
throw new Error("invalid I2OSP input: " + e);
|
|
825
|
-
const r = Array.from({ length: t }).fill(0);
|
|
826
|
-
for (let n = t - 1; n >= 0; n--)
|
|
827
|
-
r[n] = e & 255, e >>>= 8;
|
|
828
|
-
return new Uint8Array(r);
|
|
829
|
-
}
|
|
830
|
-
function Hr(e, t) {
|
|
831
|
-
const r = new Uint8Array(e.length);
|
|
832
|
-
for (let n = 0; n < e.length; n++)
|
|
833
|
-
r[n] = e[n] ^ t[n];
|
|
834
|
-
return r;
|
|
835
|
-
}
|
|
836
|
-
function Ft(e) {
|
|
837
|
-
if (!Number.isSafeInteger(e))
|
|
838
|
-
throw new Error("number expected");
|
|
839
|
-
}
|
|
840
|
-
function Zn(e) {
|
|
841
|
-
if (!ce(e) && typeof e != "string")
|
|
842
|
-
throw new Error("DST must be Uint8Array or string");
|
|
843
|
-
return typeof e == "string" ? $t(e) : e;
|
|
844
|
-
}
|
|
845
|
-
function kr(e, t, r, n) {
|
|
846
|
-
Q(e), Ft(r), t = Zn(t), t.length > 255 && (t = n(V($t("H2C-OVERSIZE-DST-"), t)));
|
|
847
|
-
const { outputLen: s, blockLen: o } = n, i = Math.ceil(r / s);
|
|
848
|
-
if (r > 65535 || i > 255)
|
|
849
|
-
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
850
|
-
const c = V(t, gt(t.length, 1)), a = gt(0, o), u = gt(r, 2), l = new Array(i), d = n(V(a, e, u, gt(0, 1), c));
|
|
851
|
-
l[0] = n(V(d, gt(1, 1), c));
|
|
852
|
-
for (let b = 1; b <= i; b++) {
|
|
853
|
-
const m = [Hr(d, l[b - 1]), gt(b + 1, 1), c];
|
|
854
|
-
l[b] = n(V(...m));
|
|
855
|
-
}
|
|
856
|
-
return V(...l).slice(0, r);
|
|
857
|
-
}
|
|
858
|
-
function Pr(e, t, r, n, s) {
|
|
859
|
-
if (Q(e), Ft(r), t = Zn(t), t.length > 255) {
|
|
860
|
-
const o = Math.ceil(2 * n / 8);
|
|
861
|
-
t = s.create({ dkLen: o }).update($t("H2C-OVERSIZE-DST-")).update(t).digest();
|
|
862
|
-
}
|
|
863
|
-
if (r > 65535 || t.length > 255)
|
|
864
|
-
throw new Error("expand_message_xof: invalid lenInBytes");
|
|
865
|
-
return s.create({ dkLen: r }).update(e).update(gt(r, 2)).update(t).update(gt(t.length, 1)).digest();
|
|
866
|
-
}
|
|
867
|
-
function pe(e, t, r) {
|
|
868
|
-
ke(r, {
|
|
869
|
-
p: "bigint",
|
|
870
|
-
m: "number",
|
|
871
|
-
k: "number",
|
|
872
|
-
hash: "function"
|
|
873
|
-
});
|
|
874
|
-
const { p: n, k: s, m: o, hash: i, expand: c, DST: a } = r;
|
|
875
|
-
if (!Er(r.hash))
|
|
876
|
-
throw new Error("expected valid hash");
|
|
877
|
-
Q(e), Ft(t);
|
|
878
|
-
const u = n.toString(2).length, l = Math.ceil((u + s) / 8), d = t * o * l;
|
|
879
|
-
let f;
|
|
880
|
-
if (c === "xmd")
|
|
881
|
-
f = kr(e, a, d, i);
|
|
882
|
-
else if (c === "xof")
|
|
883
|
-
f = Pr(e, a, d, s, i);
|
|
884
|
-
else if (c === "_internal_pass")
|
|
885
|
-
f = e;
|
|
886
|
-
else
|
|
887
|
-
throw new Error('expand must be "xmd" or "xof"');
|
|
888
|
-
const b = new Array(t);
|
|
889
|
-
for (let m = 0; m < t; m++) {
|
|
890
|
-
const g = new Array(o);
|
|
891
|
-
for (let w = 0; w < o; w++) {
|
|
892
|
-
const T = l * (w + m * o), H = f.subarray(T, T + l);
|
|
893
|
-
g[w] = et(Lr(H), n);
|
|
894
|
-
}
|
|
895
|
-
b[m] = g;
|
|
896
|
-
}
|
|
897
|
-
return b;
|
|
898
|
-
}
|
|
899
|
-
function Kn(e, t) {
|
|
900
|
-
const r = t.map((n) => Array.from(n).reverse());
|
|
901
|
-
return (n, s) => {
|
|
902
|
-
const [o, i, c, a] = r.map((d) => d.reduce((f, b) => e.add(e.mul(f, n), b))), [u, l] = It(e, [i, a], !0);
|
|
903
|
-
return n = e.mul(o, u), s = e.mul(s, e.mul(c, l)), { x: n, y: s };
|
|
904
|
-
};
|
|
905
|
-
}
|
|
906
|
-
const Dr = $t("HashToScalar-");
|
|
907
|
-
function Je(e, t, r) {
|
|
908
|
-
if (typeof t != "function")
|
|
909
|
-
throw new Error("mapToCurve() must be defined");
|
|
910
|
-
function n(o) {
|
|
911
|
-
return e.fromAffine(t(o));
|
|
912
|
-
}
|
|
913
|
-
function s(o) {
|
|
914
|
-
const i = o.clearCofactor();
|
|
915
|
-
return i.equals(e.ZERO) ? e.ZERO : (i.assertValidity(), i);
|
|
916
|
-
}
|
|
917
|
-
return {
|
|
918
|
-
defaults: r,
|
|
919
|
-
hashToCurve(o, i) {
|
|
920
|
-
const c = Object.assign({}, r, i), a = pe(o, 2, c), u = n(a[0]), l = n(a[1]);
|
|
921
|
-
return s(u.add(l));
|
|
922
|
-
},
|
|
923
|
-
encodeToCurve(o, i) {
|
|
924
|
-
const c = r.encodeDST ? { DST: r.encodeDST } : {}, a = Object.assign({}, r, c, i), u = pe(o, 1, a), l = n(u[0]);
|
|
925
|
-
return s(l);
|
|
926
|
-
},
|
|
927
|
-
/** See {@link H2CHasher} */
|
|
928
|
-
mapToCurve(o) {
|
|
929
|
-
if (!Array.isArray(o))
|
|
930
|
-
throw new Error("expected array of bigints");
|
|
931
|
-
for (const i of o)
|
|
932
|
-
if (typeof i != "bigint")
|
|
933
|
-
throw new Error("expected array of bigints");
|
|
934
|
-
return s(n(o));
|
|
935
|
-
},
|
|
936
|
-
// hash_to_scalar can produce 0: https://www.rfc-editor.org/errata/eid8393
|
|
937
|
-
// RFC 9380, draft-irtf-cfrg-bbs-signatures-08
|
|
938
|
-
hashToScalar(o, i) {
|
|
939
|
-
const c = e.Fn.ORDER, a = Object.assign({}, r, { p: c, m: 1, DST: Dr }, i);
|
|
940
|
-
return pe(o, 1, a)[0][0];
|
|
941
|
-
}
|
|
942
|
-
};
|
|
943
|
-
}
|
|
944
|
-
class Yn extends Le {
|
|
945
|
-
constructor(t, r) {
|
|
946
|
-
super(), this.finished = !1, this.destroyed = !1, Ue(t);
|
|
947
|
-
const n = vt(r);
|
|
948
|
-
if (this.iHash = t.create(), typeof this.iHash.update != "function")
|
|
949
|
-
throw new Error("Expected instance of class which extends utils.Hash");
|
|
950
|
-
this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
|
|
951
|
-
const s = this.blockLen, o = new Uint8Array(s);
|
|
952
|
-
o.set(n.length > s ? t.create().update(n).digest() : n);
|
|
953
|
-
for (let i = 0; i < o.length; i++)
|
|
954
|
-
o[i] ^= 54;
|
|
955
|
-
this.iHash.update(o), this.oHash = t.create();
|
|
956
|
-
for (let i = 0; i < o.length; i++)
|
|
957
|
-
o[i] ^= 106;
|
|
958
|
-
this.oHash.update(o), St(o);
|
|
959
|
-
}
|
|
960
|
-
update(t) {
|
|
961
|
-
return Lt(this), this.iHash.update(t), this;
|
|
962
|
-
}
|
|
963
|
-
digestInto(t) {
|
|
964
|
-
Lt(this), Q(t, this.outputLen), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
|
|
965
|
-
}
|
|
966
|
-
digest() {
|
|
967
|
-
const t = new Uint8Array(this.oHash.outputLen);
|
|
968
|
-
return this.digestInto(t), t;
|
|
969
|
-
}
|
|
970
|
-
_cloneInto(t) {
|
|
971
|
-
t || (t = Object.create(Object.getPrototypeOf(this), {}));
|
|
972
|
-
const { oHash: r, iHash: n, finished: s, destroyed: o, blockLen: i, outputLen: c } = this;
|
|
973
|
-
return t = t, t.finished = s, t.destroyed = o, t.blockLen = i, t.outputLen = c, t.oHash = r._cloneInto(t.oHash), t.iHash = n._cloneInto(t.iHash), t;
|
|
974
|
-
}
|
|
975
|
-
clone() {
|
|
976
|
-
return this._cloneInto();
|
|
977
|
-
}
|
|
978
|
-
destroy() {
|
|
979
|
-
this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
const De = (e, t, r) => new Yn(e, t).update(r).digest();
|
|
983
|
-
De.create = (e, t) => new Yn(e, t);
|
|
984
|
-
const tn = (e, t) => (e + (e >= 0 ? t : -t) / dt) / t;
|
|
985
|
-
function Mr(e, t, r) {
|
|
986
|
-
const [[n, s], [o, i]] = t, c = tn(i * e, r), a = tn(-s * e, r);
|
|
987
|
-
let u = e - c * n - a * o, l = -c * s - a * i;
|
|
988
|
-
const d = u < Bt, f = l < Bt;
|
|
989
|
-
d && (u = -u), f && (l = -l);
|
|
990
|
-
const b = Ot(Math.ceil(Vt(r) / 2)) + j;
|
|
991
|
-
if (u < Bt || u >= b || l < Bt || l >= b)
|
|
992
|
-
throw new Error("splitScalar (endomorphism): failed, k=" + e);
|
|
993
|
-
return { k1neg: d, k1: u, k2neg: f, k2: l };
|
|
994
|
-
}
|
|
995
|
-
const Bt = BigInt(0), j = BigInt(1), dt = BigInt(2), Rt = BigInt(3), Oe = BigInt(4);
|
|
996
|
-
function se(e, t) {
|
|
997
|
-
const { BYTES: r } = e;
|
|
998
|
-
let n;
|
|
999
|
-
if (typeof t == "bigint")
|
|
1000
|
-
n = t;
|
|
1001
|
-
else {
|
|
1002
|
-
let s = Ht("private key", t);
|
|
1003
|
-
try {
|
|
1004
|
-
n = e.fromBytes(s);
|
|
1005
|
-
} catch {
|
|
1006
|
-
throw new Error(`invalid private key: expected ui8a of size ${r}, got ${typeof t}`);
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
if (!e.isValidNot0(n))
|
|
1010
|
-
throw new Error("invalid private key: out of range [1..N-1]");
|
|
1011
|
-
return n;
|
|
1012
|
-
}
|
|
1013
|
-
function Zr(e, t = {}) {
|
|
1014
|
-
const r = Ur("weierstrass", e, t), { Fp: n, Fn: s } = r;
|
|
1015
|
-
let o = r.CURVE;
|
|
1016
|
-
const { h: i, n: c } = o;
|
|
1017
|
-
ke(t, {}, {
|
|
1018
|
-
allowInfinityPoint: "boolean",
|
|
1019
|
-
clearCofactor: "function",
|
|
1020
|
-
isTorsionFree: "function",
|
|
1021
|
-
fromBytes: "function",
|
|
1022
|
-
toBytes: "function",
|
|
1023
|
-
endo: "object",
|
|
1024
|
-
wrapPrivateKey: "boolean"
|
|
1025
|
-
});
|
|
1026
|
-
const { endo: a } = t;
|
|
1027
|
-
if (a && (!n.is0(o.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises)))
|
|
1028
|
-
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
1029
|
-
const u = Vr(n, s);
|
|
1030
|
-
function l() {
|
|
1031
|
-
if (!n.isOdd)
|
|
1032
|
-
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
1033
|
-
}
|
|
1034
|
-
function d(q, h, y) {
|
|
1035
|
-
const { x: v, y: U } = h.toAffine(), M = n.toBytes(v);
|
|
1036
|
-
if (ze(y, "isCompressed"), y) {
|
|
1037
|
-
l();
|
|
1038
|
-
const p = !n.isOdd(U);
|
|
1039
|
-
return V(Kr(p), M);
|
|
1040
|
-
} else
|
|
1041
|
-
return V(Uint8Array.of(4), M, n.toBytes(U));
|
|
1042
|
-
}
|
|
1043
|
-
function f(q) {
|
|
1044
|
-
$e(q, void 0, "Point");
|
|
1045
|
-
const { publicKey: h, publicKeyUncompressed: y } = u, v = q.length, U = q[0], M = q.subarray(1);
|
|
1046
|
-
if (v === h && (U === 2 || U === 3)) {
|
|
1047
|
-
const p = n.fromBytes(M);
|
|
1048
|
-
if (!n.isValid(p))
|
|
1049
|
-
throw new Error("bad point: is not on curve, wrong x");
|
|
1050
|
-
const E = g(p);
|
|
1051
|
-
let B;
|
|
1052
|
-
try {
|
|
1053
|
-
B = n.sqrt(E);
|
|
1054
|
-
} catch (J) {
|
|
1055
|
-
const F = J instanceof Error ? ": " + J.message : "";
|
|
1056
|
-
throw new Error("bad point: is not on curve, sqrt error" + F);
|
|
1057
|
-
}
|
|
1058
|
-
l();
|
|
1059
|
-
const R = n.isOdd(B);
|
|
1060
|
-
return (U & 1) === 1 !== R && (B = n.neg(B)), { x: p, y: B };
|
|
1061
|
-
} else if (v === y && U === 4) {
|
|
1062
|
-
const p = n.BYTES, E = n.fromBytes(M.subarray(0, p)), B = n.fromBytes(M.subarray(p, p * 2));
|
|
1063
|
-
if (!w(E, B))
|
|
1064
|
-
throw new Error("bad point: is not on curve");
|
|
1065
|
-
return { x: E, y: B };
|
|
1066
|
-
} else
|
|
1067
|
-
throw new Error(`bad point: got length ${v}, expected compressed=${h} or uncompressed=${y}`);
|
|
1068
|
-
}
|
|
1069
|
-
const b = t.toBytes || d, m = t.fromBytes || f;
|
|
1070
|
-
function g(q) {
|
|
1071
|
-
const h = n.sqr(q), y = n.mul(h, q);
|
|
1072
|
-
return n.add(n.add(y, n.mul(q, o.a)), o.b);
|
|
1073
|
-
}
|
|
1074
|
-
function w(q, h) {
|
|
1075
|
-
const y = n.sqr(h), v = g(q);
|
|
1076
|
-
return n.eql(y, v);
|
|
1077
|
-
}
|
|
1078
|
-
if (!w(o.Gx, o.Gy))
|
|
1079
|
-
throw new Error("bad curve params: generator point");
|
|
1080
|
-
const T = n.mul(n.pow(o.a, Rt), Oe), H = n.mul(n.sqr(o.b), BigInt(27));
|
|
1081
|
-
if (n.is0(n.add(T, H)))
|
|
1082
|
-
throw new Error("bad curve params: a or b");
|
|
1083
|
-
function O(q, h, y = !1) {
|
|
1084
|
-
if (!n.isValid(h) || y && n.is0(h))
|
|
1085
|
-
throw new Error(`bad point coordinate ${q}`);
|
|
1086
|
-
return h;
|
|
1087
|
-
}
|
|
1088
|
-
function _(q) {
|
|
1089
|
-
if (!(q instanceof S))
|
|
1090
|
-
throw new Error("ProjectivePoint expected");
|
|
1091
|
-
}
|
|
1092
|
-
function A(q) {
|
|
1093
|
-
if (!a || !a.basises)
|
|
1094
|
-
throw new Error("no endo");
|
|
1095
|
-
return Mr(q, a.basises, s.ORDER);
|
|
1096
|
-
}
|
|
1097
|
-
const I = Ae((q, h) => {
|
|
1098
|
-
const { X: y, Y: v, Z: U } = q;
|
|
1099
|
-
if (n.eql(U, n.ONE))
|
|
1100
|
-
return { x: y, y: v };
|
|
1101
|
-
const M = q.is0();
|
|
1102
|
-
h == null && (h = M ? n.ONE : n.inv(U));
|
|
1103
|
-
const p = n.mul(y, h), E = n.mul(v, h), B = n.mul(U, h);
|
|
1104
|
-
if (M)
|
|
1105
|
-
return { x: n.ZERO, y: n.ZERO };
|
|
1106
|
-
if (!n.eql(B, n.ONE))
|
|
1107
|
-
throw new Error("invZ was invalid");
|
|
1108
|
-
return { x: p, y: E };
|
|
1109
|
-
}), N = Ae((q) => {
|
|
1110
|
-
if (q.is0()) {
|
|
1111
|
-
if (t.allowInfinityPoint && !n.is0(q.Y))
|
|
1112
|
-
return;
|
|
1113
|
-
throw new Error("bad point: ZERO");
|
|
1114
|
-
}
|
|
1115
|
-
const { x: h, y } = q.toAffine();
|
|
1116
|
-
if (!n.isValid(h) || !n.isValid(y))
|
|
1117
|
-
throw new Error("bad point: x or y not field elements");
|
|
1118
|
-
if (!w(h, y))
|
|
1119
|
-
throw new Error("bad point: equation left != right");
|
|
1120
|
-
if (!q.isTorsionFree())
|
|
1121
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
1122
|
-
return !0;
|
|
1123
|
-
});
|
|
1124
|
-
function D(q, h, y, v, U) {
|
|
1125
|
-
return y = new S(n.mul(y.X, q), y.Y, y.Z), h = re(v, h), y = re(U, y), h.add(y);
|
|
1126
|
-
}
|
|
1127
|
-
class S {
|
|
1128
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
1129
|
-
constructor(h, y, v) {
|
|
1130
|
-
this.X = O("x", h), this.Y = O("y", y, !0), this.Z = O("z", v), Object.freeze(this);
|
|
1131
|
-
}
|
|
1132
|
-
static CURVE() {
|
|
1133
|
-
return o;
|
|
1134
|
-
}
|
|
1135
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
1136
|
-
static fromAffine(h) {
|
|
1137
|
-
const { x: y, y: v } = h || {};
|
|
1138
|
-
if (!h || !n.isValid(y) || !n.isValid(v))
|
|
1139
|
-
throw new Error("invalid affine point");
|
|
1140
|
-
if (h instanceof S)
|
|
1141
|
-
throw new Error("projective point not allowed");
|
|
1142
|
-
return n.is0(y) && n.is0(v) ? S.ZERO : new S(y, v, n.ONE);
|
|
1143
|
-
}
|
|
1144
|
-
static fromBytes(h) {
|
|
1145
|
-
const y = S.fromAffine(m($e(h, void 0, "point")));
|
|
1146
|
-
return y.assertValidity(), y;
|
|
1147
|
-
}
|
|
1148
|
-
static fromHex(h) {
|
|
1149
|
-
return S.fromBytes(Ht("pointHex", h));
|
|
1150
|
-
}
|
|
1151
|
-
get x() {
|
|
1152
|
-
return this.toAffine().x;
|
|
1153
|
-
}
|
|
1154
|
-
get y() {
|
|
1155
|
-
return this.toAffine().y;
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
*
|
|
1159
|
-
* @param windowSize
|
|
1160
|
-
* @param isLazy true will defer table computation until the first multiplication
|
|
1161
|
-
* @returns
|
|
1162
|
-
*/
|
|
1163
|
-
precompute(h = 8, y = !0) {
|
|
1164
|
-
return Y.createCache(this, h), y || this.multiply(Rt), this;
|
|
1165
|
-
}
|
|
1166
|
-
// TODO: return `this`
|
|
1167
|
-
/** A point on curve is valid if it conforms to equation. */
|
|
1168
|
-
assertValidity() {
|
|
1169
|
-
N(this);
|
|
1170
|
-
}
|
|
1171
|
-
hasEvenY() {
|
|
1172
|
-
const { y: h } = this.toAffine();
|
|
1173
|
-
if (!n.isOdd)
|
|
1174
|
-
throw new Error("Field doesn't support isOdd");
|
|
1175
|
-
return !n.isOdd(h);
|
|
1176
|
-
}
|
|
1177
|
-
/** Compare one point to another. */
|
|
1178
|
-
equals(h) {
|
|
1179
|
-
_(h);
|
|
1180
|
-
const { X: y, Y: v, Z: U } = this, { X: M, Y: p, Z: E } = h, B = n.eql(n.mul(y, E), n.mul(M, U)), R = n.eql(n.mul(v, E), n.mul(p, U));
|
|
1181
|
-
return B && R;
|
|
1182
|
-
}
|
|
1183
|
-
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
1184
|
-
negate() {
|
|
1185
|
-
return new S(this.X, n.neg(this.Y), this.Z);
|
|
1186
|
-
}
|
|
1187
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
1188
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1189
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
1190
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
1191
|
-
double() {
|
|
1192
|
-
const { a: h, b: y } = o, v = n.mul(y, Rt), { X: U, Y: M, Z: p } = this;
|
|
1193
|
-
let E = n.ZERO, B = n.ZERO, R = n.ZERO, G = n.mul(U, U), J = n.mul(M, M), F = n.mul(p, p), Z = n.mul(U, M);
|
|
1194
|
-
return Z = n.add(Z, Z), R = n.mul(U, p), R = n.add(R, R), E = n.mul(h, R), B = n.mul(v, F), B = n.add(E, B), E = n.sub(J, B), B = n.add(J, B), B = n.mul(E, B), E = n.mul(Z, E), R = n.mul(v, R), F = n.mul(h, F), Z = n.sub(G, F), Z = n.mul(h, Z), Z = n.add(Z, R), R = n.add(G, G), G = n.add(R, G), G = n.add(G, F), G = n.mul(G, Z), B = n.add(B, G), F = n.mul(M, p), F = n.add(F, F), G = n.mul(F, Z), E = n.sub(E, G), R = n.mul(F, J), R = n.add(R, R), R = n.add(R, R), new S(E, B, R);
|
|
1195
|
-
}
|
|
1196
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
1197
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
1198
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
1199
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
1200
|
-
add(h) {
|
|
1201
|
-
_(h);
|
|
1202
|
-
const { X: y, Y: v, Z: U } = this, { X: M, Y: p, Z: E } = h;
|
|
1203
|
-
let B = n.ZERO, R = n.ZERO, G = n.ZERO;
|
|
1204
|
-
const J = o.a, F = n.mul(o.b, Rt);
|
|
1205
|
-
let Z = n.mul(y, M), st = n.mul(v, p), ot = n.mul(U, E), Nt = n.add(y, v), $ = n.add(M, p);
|
|
1206
|
-
Nt = n.mul(Nt, $), $ = n.add(Z, st), Nt = n.sub(Nt, $), $ = n.add(y, U);
|
|
1207
|
-
let ct = n.add(M, E);
|
|
1208
|
-
return $ = n.mul($, ct), ct = n.add(Z, ot), $ = n.sub($, ct), ct = n.add(v, U), B = n.add(p, E), ct = n.mul(ct, B), B = n.add(st, ot), ct = n.sub(ct, B), G = n.mul(J, $), B = n.mul(F, ot), G = n.add(B, G), B = n.sub(st, G), G = n.add(st, G), R = n.mul(B, G), st = n.add(Z, Z), st = n.add(st, Z), ot = n.mul(J, ot), $ = n.mul(F, $), st = n.add(st, ot), ot = n.sub(Z, ot), ot = n.mul(J, ot), $ = n.add($, ot), Z = n.mul(st, $), R = n.add(R, Z), Z = n.mul(ct, $), B = n.mul(Nt, B), B = n.sub(B, Z), Z = n.mul(Nt, st), G = n.mul(ct, G), G = n.add(G, Z), new S(B, R, G);
|
|
1209
|
-
}
|
|
1210
|
-
subtract(h) {
|
|
1211
|
-
return this.add(h.negate());
|
|
1212
|
-
}
|
|
1213
|
-
is0() {
|
|
1214
|
-
return this.equals(S.ZERO);
|
|
1215
|
-
}
|
|
1216
|
-
/**
|
|
1217
|
-
* Constant time multiplication.
|
|
1218
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
1219
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
1220
|
-
* Uses precomputes when available.
|
|
1221
|
-
* Uses endomorphism for Koblitz curves.
|
|
1222
|
-
* @param scalar by which the point would be multiplied
|
|
1223
|
-
* @returns New point
|
|
1224
|
-
*/
|
|
1225
|
-
multiply(h) {
|
|
1226
|
-
const { endo: y } = t;
|
|
1227
|
-
if (!s.isValidNot0(h))
|
|
1228
|
-
throw new Error("invalid scalar: out of range");
|
|
1229
|
-
let v, U;
|
|
1230
|
-
const M = (p) => Y.cached(this, p, (E) => Kt(S, E));
|
|
1231
|
-
if (y) {
|
|
1232
|
-
const { k1neg: p, k1: E, k2neg: B, k2: R } = A(h), { p: G, f: J } = M(E), { p: F, f: Z } = M(R);
|
|
1233
|
-
U = J.add(Z), v = D(y.beta, G, F, p, B);
|
|
1234
|
-
} else {
|
|
1235
|
-
const { p, f: E } = M(h);
|
|
1236
|
-
v = p, U = E;
|
|
1237
|
-
}
|
|
1238
|
-
return Kt(S, [v, U])[0];
|
|
1239
|
-
}
|
|
1240
|
-
/**
|
|
1241
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1242
|
-
* It's faster, but should only be used when you don't care about
|
|
1243
|
-
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
1244
|
-
*/
|
|
1245
|
-
multiplyUnsafe(h) {
|
|
1246
|
-
const { endo: y } = t, v = this;
|
|
1247
|
-
if (!s.isValid(h))
|
|
1248
|
-
throw new Error("invalid scalar: out of range");
|
|
1249
|
-
if (h === Bt || v.is0())
|
|
1250
|
-
return S.ZERO;
|
|
1251
|
-
if (h === j)
|
|
1252
|
-
return v;
|
|
1253
|
-
if (Y.hasCache(this))
|
|
1254
|
-
return this.multiply(h);
|
|
1255
|
-
if (y) {
|
|
1256
|
-
const { k1neg: U, k1: M, k2neg: p, k2: E } = A(h), { p1: B, p2: R } = Rr(S, v, M, E);
|
|
1257
|
-
return D(y.beta, B, R, U, p);
|
|
1258
|
-
} else
|
|
1259
|
-
return Y.unsafe(v, h);
|
|
1260
|
-
}
|
|
1261
|
-
multiplyAndAddUnsafe(h, y, v) {
|
|
1262
|
-
const U = this.multiplyUnsafe(y).add(h.multiplyUnsafe(v));
|
|
1263
|
-
return U.is0() ? void 0 : U;
|
|
1264
|
-
}
|
|
1265
|
-
/**
|
|
1266
|
-
* Converts Projective point to affine (x, y) coordinates.
|
|
1267
|
-
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
1268
|
-
*/
|
|
1269
|
-
toAffine(h) {
|
|
1270
|
-
return I(this, h);
|
|
1271
|
-
}
|
|
1272
|
-
/**
|
|
1273
|
-
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
1274
|
-
* Always torsion-free for cofactor=1 curves.
|
|
1275
|
-
*/
|
|
1276
|
-
isTorsionFree() {
|
|
1277
|
-
const { isTorsionFree: h } = t;
|
|
1278
|
-
return i === j ? !0 : h ? h(S, this) : Y.unsafe(this, c).is0();
|
|
1279
|
-
}
|
|
1280
|
-
clearCofactor() {
|
|
1281
|
-
const { clearCofactor: h } = t;
|
|
1282
|
-
return i === j ? this : h ? h(S, this) : this.multiplyUnsafe(i);
|
|
1283
|
-
}
|
|
1284
|
-
isSmallOrder() {
|
|
1285
|
-
return this.multiplyUnsafe(i).is0();
|
|
1286
|
-
}
|
|
1287
|
-
toBytes(h = !0) {
|
|
1288
|
-
return ze(h, "isCompressed"), this.assertValidity(), b(S, this, h);
|
|
1289
|
-
}
|
|
1290
|
-
toHex(h = !0) {
|
|
1291
|
-
return Yt(this.toBytes(h));
|
|
1292
|
-
}
|
|
1293
|
-
toString() {
|
|
1294
|
-
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1295
|
-
}
|
|
1296
|
-
// TODO: remove
|
|
1297
|
-
get px() {
|
|
1298
|
-
return this.X;
|
|
1299
|
-
}
|
|
1300
|
-
get py() {
|
|
1301
|
-
return this.X;
|
|
1302
|
-
}
|
|
1303
|
-
get pz() {
|
|
1304
|
-
return this.Z;
|
|
1305
|
-
}
|
|
1306
|
-
toRawBytes(h = !0) {
|
|
1307
|
-
return this.toBytes(h);
|
|
1308
|
-
}
|
|
1309
|
-
_setWindowSize(h) {
|
|
1310
|
-
this.precompute(h);
|
|
1311
|
-
}
|
|
1312
|
-
static normalizeZ(h) {
|
|
1313
|
-
return Kt(S, h);
|
|
1314
|
-
}
|
|
1315
|
-
static msm(h, y) {
|
|
1316
|
-
return Gr(S, s, h, y);
|
|
1317
|
-
}
|
|
1318
|
-
static fromPrivateKey(h) {
|
|
1319
|
-
return S.BASE.multiply(se(s, h));
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
S.BASE = new S(o.Gx, o.Gy, n.ONE), S.ZERO = new S(n.ZERO, n.ONE, n.ZERO), S.Fp = n, S.Fn = s;
|
|
1323
|
-
const K = s.BITS, Y = new qr(S, t.endo ? Math.ceil(K / 2) : K);
|
|
1324
|
-
return S.BASE.precompute(8), S;
|
|
1325
|
-
}
|
|
1326
|
-
function Kr(e) {
|
|
1327
|
-
return Uint8Array.of(e ? 2 : 3);
|
|
1328
|
-
}
|
|
1329
|
-
function Yr(e, t) {
|
|
1330
|
-
const r = e.ORDER;
|
|
1331
|
-
let n = Bt;
|
|
1332
|
-
for (let m = r - j; m % dt === Bt; m /= dt)
|
|
1333
|
-
n += j;
|
|
1334
|
-
const s = n, o = dt << s - j - j, i = o * dt, c = (r - j) / i, a = (c - j) / dt, u = i - j, l = o, d = e.pow(t, c), f = e.pow(t, (c + j) / dt);
|
|
1335
|
-
let b = (m, g) => {
|
|
1336
|
-
let w = d, T = e.pow(g, u), H = e.sqr(T);
|
|
1337
|
-
H = e.mul(H, g);
|
|
1338
|
-
let O = e.mul(m, H);
|
|
1339
|
-
O = e.pow(O, a), O = e.mul(O, T), T = e.mul(O, g), H = e.mul(O, m);
|
|
1340
|
-
let _ = e.mul(H, T);
|
|
1341
|
-
O = e.pow(_, l);
|
|
1342
|
-
let A = e.eql(O, e.ONE);
|
|
1343
|
-
T = e.mul(H, f), O = e.mul(_, w), H = e.cmov(T, H, A), _ = e.cmov(O, _, A);
|
|
1344
|
-
for (let I = s; I > j; I--) {
|
|
1345
|
-
let N = I - dt;
|
|
1346
|
-
N = dt << N - j;
|
|
1347
|
-
let D = e.pow(_, N);
|
|
1348
|
-
const S = e.eql(D, e.ONE);
|
|
1349
|
-
T = e.mul(H, w), w = e.mul(w, w), D = e.mul(_, w), H = e.cmov(T, H, S), _ = e.cmov(D, _, S);
|
|
1350
|
-
}
|
|
1351
|
-
return { isValid: A, value: H };
|
|
1352
|
-
};
|
|
1353
|
-
if (e.ORDER % Oe === Rt) {
|
|
1354
|
-
const m = (e.ORDER - Rt) / Oe, g = e.sqrt(e.neg(t));
|
|
1355
|
-
b = (w, T) => {
|
|
1356
|
-
let H = e.sqr(T);
|
|
1357
|
-
const O = e.mul(w, T);
|
|
1358
|
-
H = e.mul(H, O);
|
|
1359
|
-
let _ = e.pow(H, m);
|
|
1360
|
-
_ = e.mul(_, O);
|
|
1361
|
-
const A = e.mul(_, g), I = e.mul(e.sqr(_), T), N = e.eql(I, w);
|
|
1362
|
-
let D = e.cmov(A, _, N);
|
|
1363
|
-
return { isValid: N, value: D };
|
|
1364
|
-
};
|
|
1365
|
-
}
|
|
1366
|
-
return b;
|
|
1367
|
-
}
|
|
1368
|
-
function Vn(e, t) {
|
|
1369
|
-
Hn(e);
|
|
1370
|
-
const { A: r, B: n, Z: s } = t;
|
|
1371
|
-
if (!e.isValid(r) || !e.isValid(n) || !e.isValid(s))
|
|
1372
|
-
throw new Error("mapToCurveSimpleSWU: invalid opts");
|
|
1373
|
-
const o = Yr(e, s);
|
|
1374
|
-
if (!e.isOdd)
|
|
1375
|
-
throw new Error("Field does not have .isOdd()");
|
|
1376
|
-
return (i) => {
|
|
1377
|
-
let c, a, u, l, d, f, b, m;
|
|
1378
|
-
c = e.sqr(i), c = e.mul(c, s), a = e.sqr(c), a = e.add(a, c), u = e.add(a, e.ONE), u = e.mul(u, n), l = e.cmov(s, e.neg(a), !e.eql(a, e.ZERO)), l = e.mul(l, r), a = e.sqr(u), f = e.sqr(l), d = e.mul(f, r), a = e.add(a, d), a = e.mul(a, u), f = e.mul(f, l), d = e.mul(f, n), a = e.add(a, d), b = e.mul(c, u);
|
|
1379
|
-
const { isValid: g, value: w } = o(a, f);
|
|
1380
|
-
m = e.mul(c, i), m = e.mul(m, w), b = e.cmov(b, u, g), m = e.cmov(m, w, g);
|
|
1381
|
-
const T = e.isOdd(i) === e.isOdd(m);
|
|
1382
|
-
m = e.cmov(e.neg(m), m, T);
|
|
1383
|
-
const H = It(e, [l], !0)[0];
|
|
1384
|
-
return b = e.mul(b, H), { x: b, y: m };
|
|
1385
|
-
};
|
|
1386
|
-
}
|
|
1387
|
-
function Vr(e, t) {
|
|
1388
|
-
return {
|
|
1389
|
-
secretKey: t.BYTES,
|
|
1390
|
-
publicKey: 1 + e.BYTES,
|
|
1391
|
-
publicKeyUncompressed: 1 + 2 * e.BYTES,
|
|
1392
|
-
publicKeyHasPrefix: !0,
|
|
1393
|
-
signature: 2 * t.BYTES
|
|
1394
|
-
};
|
|
1395
|
-
}
|
|
1396
|
-
function en(e) {
|
|
1397
|
-
const { CURVE: t, curveOpts: r } = Fr(e), n = Zr(t, r);
|
|
1398
|
-
return zr(e, n);
|
|
1399
|
-
}
|
|
1400
|
-
function Fr(e) {
|
|
1401
|
-
const t = {
|
|
1402
|
-
a: e.a,
|
|
1403
|
-
b: e.b,
|
|
1404
|
-
p: e.Fp.ORDER,
|
|
1405
|
-
n: e.n,
|
|
1406
|
-
h: e.h,
|
|
1407
|
-
Gx: e.Gx,
|
|
1408
|
-
Gy: e.Gy
|
|
1409
|
-
}, r = e.Fp;
|
|
1410
|
-
let n = e.allowedPrivateKeyLengths ? Array.from(new Set(e.allowedPrivateKeyLengths.map((i) => Math.ceil(i / 2)))) : void 0;
|
|
1411
|
-
const s = Pt(t.n, {
|
|
1412
|
-
BITS: e.nBitLength,
|
|
1413
|
-
allowedLengths: n,
|
|
1414
|
-
modFromBytes: e.wrapPrivateKey
|
|
1415
|
-
}), o = {
|
|
1416
|
-
Fp: r,
|
|
1417
|
-
Fn: s,
|
|
1418
|
-
allowInfinityPoint: e.allowInfinityPoint,
|
|
1419
|
-
endo: e.endo,
|
|
1420
|
-
isTorsionFree: e.isTorsionFree,
|
|
1421
|
-
clearCofactor: e.clearCofactor,
|
|
1422
|
-
fromBytes: e.fromBytes,
|
|
1423
|
-
toBytes: e.toBytes
|
|
1424
|
-
};
|
|
1425
|
-
return { CURVE: t, curveOpts: o };
|
|
1426
|
-
}
|
|
1427
|
-
function Cr(e, t, r) {
|
|
1428
|
-
function n(s) {
|
|
1429
|
-
const o = e.sqr(s), i = e.mul(o, s);
|
|
1430
|
-
return e.add(e.add(i, e.mul(s, t)), r);
|
|
1431
|
-
}
|
|
1432
|
-
return n;
|
|
1433
|
-
}
|
|
1434
|
-
function zr(e, t) {
|
|
1435
|
-
const { Fp: r, Fn: n } = t;
|
|
1436
|
-
function s(i) {
|
|
1437
|
-
return pr(i, j, n.ORDER);
|
|
1438
|
-
}
|
|
1439
|
-
const o = Cr(r, e.a, e.b);
|
|
1440
|
-
return Object.assign({}, {
|
|
1441
|
-
CURVE: e,
|
|
1442
|
-
Point: t,
|
|
1443
|
-
ProjectivePoint: t,
|
|
1444
|
-
normPrivateKeyToScalar: (i) => se(n, i),
|
|
1445
|
-
weierstrassEquation: o,
|
|
1446
|
-
isWithinCurveOrder: s
|
|
1447
|
-
});
|
|
1448
|
-
}
|
|
1449
|
-
const $r = BigInt(0), Qt = BigInt(1), nn = BigInt(2), Gt = BigInt(3);
|
|
1450
|
-
function jr(e) {
|
|
1451
|
-
const t = [];
|
|
1452
|
-
for (; e > Qt; e >>= Qt)
|
|
1453
|
-
(e & Qt) === $r ? t.unshift(0) : (e & Gt) === Gt ? (t.unshift(-1), e += Qt) : t.unshift(1);
|
|
1454
|
-
return t;
|
|
1455
|
-
}
|
|
1456
|
-
function we(e) {
|
|
1457
|
-
if (!Array.isArray(e) || e.length === 0)
|
|
1458
|
-
throw new Error("expected non-empty array");
|
|
1459
|
-
}
|
|
1460
|
-
function Xr(e, t, r, n) {
|
|
1461
|
-
const { Fp2: s, Fp12: o } = e, { twistType: i, ateLoopSize: c, xNegative: a, postPrecompute: u } = n;
|
|
1462
|
-
let l;
|
|
1463
|
-
if (i === "multiplicative")
|
|
1464
|
-
l = (O, _, A, I, N, D) => o.mul014(I, O, s.mul(_, N), s.mul(A, D));
|
|
1465
|
-
else if (i === "divisive")
|
|
1466
|
-
l = (O, _, A, I, N, D) => o.mul034(I, s.mul(A, D), s.mul(_, N), O);
|
|
1467
|
-
else
|
|
1468
|
-
throw new Error("bls: unknown twist type");
|
|
1469
|
-
const d = s.div(s.ONE, s.mul(s.ONE, nn));
|
|
1470
|
-
function f(O, _, A, I) {
|
|
1471
|
-
const N = s.sqr(A), D = s.sqr(I), S = s.mulByB(s.mul(D, Gt)), K = s.mul(S, Gt), Y = s.sub(s.sub(s.sqr(s.add(A, I)), D), N), q = s.sub(S, N), h = s.mul(s.sqr(_), Gt), y = s.neg(Y);
|
|
1472
|
-
return O.push([q, h, y]), _ = s.mul(s.mul(s.mul(s.sub(N, K), _), A), d), A = s.sub(s.sqr(s.mul(s.add(N, K), d)), s.mul(s.sqr(S), Gt)), I = s.mul(N, Y), { Rx: _, Ry: A, Rz: I };
|
|
1473
|
-
}
|
|
1474
|
-
function b(O, _, A, I, N, D) {
|
|
1475
|
-
const S = s.sub(A, s.mul(D, I)), K = s.sub(_, s.mul(N, I)), Y = s.sub(s.mul(S, N), s.mul(K, D)), q = s.neg(S), h = K;
|
|
1476
|
-
O.push([Y, q, h]);
|
|
1477
|
-
const y = s.sqr(K), v = s.mul(y, K), U = s.mul(y, _), M = s.add(s.sub(v, s.mul(U, nn)), s.mul(s.sqr(S), I));
|
|
1478
|
-
return _ = s.mul(K, M), A = s.sub(s.mul(s.sub(U, M), S), s.mul(v, A)), I = s.mul(I, v), { Rx: _, Ry: A, Rz: I };
|
|
1479
|
-
}
|
|
1480
|
-
const m = jr(c), g = Ae((O) => {
|
|
1481
|
-
const _ = O, { x: A, y: I } = _.toAffine(), N = A, D = I, S = s.neg(I);
|
|
1482
|
-
let K = N, Y = D, q = s.ONE;
|
|
1483
|
-
const h = [];
|
|
1484
|
-
for (const y of m) {
|
|
1485
|
-
const v = [];
|
|
1486
|
-
({ Rx: K, Ry: Y, Rz: q } = f(v, K, Y, q)), y && ({ Rx: K, Ry: Y, Rz: q } = b(v, K, Y, q, N, y === -1 ? S : D)), h.push(v);
|
|
1487
|
-
}
|
|
1488
|
-
if (u) {
|
|
1489
|
-
const y = h[h.length - 1];
|
|
1490
|
-
u(K, Y, q, N, D, b.bind(null, y));
|
|
1491
|
-
}
|
|
1492
|
-
return h;
|
|
1493
|
-
});
|
|
1494
|
-
function w(O, _ = !1) {
|
|
1495
|
-
let A = o.ONE;
|
|
1496
|
-
if (O.length) {
|
|
1497
|
-
const I = O[0][0].length;
|
|
1498
|
-
for (let N = 0; N < I; N++) {
|
|
1499
|
-
A = o.sqr(A);
|
|
1500
|
-
for (const [D, S, K] of O)
|
|
1501
|
-
for (const [Y, q, h] of D[N])
|
|
1502
|
-
A = l(Y, q, h, A, S, K);
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
return a && (A = o.conjugate(A)), _ ? o.finalExponentiate(A) : A;
|
|
1506
|
-
}
|
|
1507
|
-
function T(O, _ = !0) {
|
|
1508
|
-
const A = [];
|
|
1509
|
-
Kt(t, O.map(({ g1: I }) => I)), Kt(r, O.map(({ g2: I }) => I));
|
|
1510
|
-
for (const { g1: I, g2: N } of O) {
|
|
1511
|
-
if (I.is0() || N.is0())
|
|
1512
|
-
throw new Error("pairing is not available for ZERO point");
|
|
1513
|
-
I.assertValidity(), N.assertValidity();
|
|
1514
|
-
const D = I.toAffine();
|
|
1515
|
-
A.push([g(N), D.x, D.y]);
|
|
1516
|
-
}
|
|
1517
|
-
return w(A, _);
|
|
1518
|
-
}
|
|
1519
|
-
function H(O, _, A = !0) {
|
|
1520
|
-
return T([{ g1: O, g2: _ }], A);
|
|
1521
|
-
}
|
|
1522
|
-
return {
|
|
1523
|
-
Fp12: o,
|
|
1524
|
-
// NOTE: we re-export Fp12 here because pairing results are Fp12!
|
|
1525
|
-
millerLoopBatch: w,
|
|
1526
|
-
pairing: H,
|
|
1527
|
-
pairingBatch: T,
|
|
1528
|
-
calcPairingPrecomputes: g
|
|
1529
|
-
};
|
|
1530
|
-
}
|
|
1531
|
-
function rn(e, t, r, n, s) {
|
|
1532
|
-
const { Fp12: o, pairingBatch: i } = e;
|
|
1533
|
-
function c(d) {
|
|
1534
|
-
return d instanceof t.Point ? d : t.Point.fromHex(d);
|
|
1535
|
-
}
|
|
1536
|
-
function a(d) {
|
|
1537
|
-
return d instanceof r.Point ? d : r.Point.fromHex(d);
|
|
1538
|
-
}
|
|
1539
|
-
function u(d) {
|
|
1540
|
-
if (!(d instanceof r.Point))
|
|
1541
|
-
throw new Error(`expected valid message hashed to ${s ? "G1" : "G2"} curve`);
|
|
1542
|
-
return d;
|
|
1543
|
-
}
|
|
1544
|
-
const l = s ? (d, f) => ({ g1: f, g2: d }) : (d, f) => ({ g1: d, g2: f });
|
|
1545
|
-
return {
|
|
1546
|
-
// P = pk x G
|
|
1547
|
-
getPublicKey(d) {
|
|
1548
|
-
const f = se(t.Point.Fn, d);
|
|
1549
|
-
return t.Point.BASE.multiply(f);
|
|
1550
|
-
},
|
|
1551
|
-
// S = pk x H(m)
|
|
1552
|
-
sign(d, f, b) {
|
|
1553
|
-
if (b != null)
|
|
1554
|
-
throw new Error("sign() expects 2 arguments");
|
|
1555
|
-
const m = se(t.Point.Fn, f);
|
|
1556
|
-
return u(d).assertValidity(), d.multiply(m);
|
|
1557
|
-
},
|
|
1558
|
-
// Checks if pairing of public key & hash is equal to pairing of generator & signature.
|
|
1559
|
-
// e(P, H(m)) == e(G, S)
|
|
1560
|
-
// e(S, G) == e(H(m), P)
|
|
1561
|
-
verify(d, f, b, m) {
|
|
1562
|
-
if (m != null)
|
|
1563
|
-
throw new Error("verify() expects 3 arguments");
|
|
1564
|
-
d = a(d), b = c(b);
|
|
1565
|
-
const g = b.negate(), w = t.Point.BASE, T = u(f), H = d, O = i([l(g, T), l(w, H)]);
|
|
1566
|
-
return o.eql(O, o.ONE);
|
|
1567
|
-
},
|
|
1568
|
-
// https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
|
|
1569
|
-
// e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
|
|
1570
|
-
// TODO: maybe `{message: G2Hex, publicKey: G1Hex}[]` instead?
|
|
1571
|
-
verifyBatch(d, f, b) {
|
|
1572
|
-
if (we(f), b.length !== f.length)
|
|
1573
|
-
throw new Error("amount of public keys and messages should be equal");
|
|
1574
|
-
const m = a(d), g = f, w = b.map(c), T = /* @__PURE__ */ new Map();
|
|
1575
|
-
for (let _ = 0; _ < w.length; _++) {
|
|
1576
|
-
const A = w[_], I = g[_];
|
|
1577
|
-
let N = T.get(I);
|
|
1578
|
-
N === void 0 && (N = [], T.set(I, N)), N.push(A);
|
|
1579
|
-
}
|
|
1580
|
-
const H = [], O = t.Point.BASE;
|
|
1581
|
-
try {
|
|
1582
|
-
for (const [_, A] of T) {
|
|
1583
|
-
const I = A.reduce((N, D) => N.add(D));
|
|
1584
|
-
H.push(l(I, _));
|
|
1585
|
-
}
|
|
1586
|
-
return H.push(l(O.negate(), m)), o.eql(i(H), o.ONE);
|
|
1587
|
-
} catch {
|
|
1588
|
-
return !1;
|
|
1589
|
-
}
|
|
1590
|
-
},
|
|
1591
|
-
// Adds a bunch of public key points together.
|
|
1592
|
-
// pk1 + pk2 + pk3 = pkA
|
|
1593
|
-
aggregatePublicKeys(d) {
|
|
1594
|
-
we(d), d = d.map((b) => c(b));
|
|
1595
|
-
const f = d.reduce((b, m) => b.add(m), t.Point.ZERO);
|
|
1596
|
-
return f.assertValidity(), f;
|
|
1597
|
-
},
|
|
1598
|
-
// Adds a bunch of signature points together.
|
|
1599
|
-
// pk1 + pk2 + pk3 = pkA
|
|
1600
|
-
aggregateSignatures(d) {
|
|
1601
|
-
we(d), d = d.map((b) => a(b));
|
|
1602
|
-
const f = d.reduce((b, m) => b.add(m), r.Point.ZERO);
|
|
1603
|
-
return f.assertValidity(), f;
|
|
1604
|
-
},
|
|
1605
|
-
hash(d, f) {
|
|
1606
|
-
Q(d);
|
|
1607
|
-
const b = f ? { DST: f } : void 0;
|
|
1608
|
-
return r.hashToCurve(d, b);
|
|
1609
|
-
},
|
|
1610
|
-
Signature: n
|
|
1611
|
-
};
|
|
1612
|
-
}
|
|
1613
|
-
function Wr(e) {
|
|
1614
|
-
const { Fp: t, Fr: r, Fp2: n, Fp6: s, Fp12: o } = e.fields, i = en(e.G1), c = Object.assign(i, Je(i.Point, e.G1.mapToCurve, {
|
|
1615
|
-
...e.htfDefaults,
|
|
1616
|
-
...e.G1.htfDefaults
|
|
1617
|
-
})), a = en(e.G2), u = Object.assign(a, Je(a.Point, e.G2.mapToCurve, {
|
|
1618
|
-
...e.htfDefaults,
|
|
1619
|
-
...e.G2.htfDefaults
|
|
1620
|
-
})), l = Xr(e.fields, c.Point, u.Point, {
|
|
1621
|
-
...e.params,
|
|
1622
|
-
postPrecompute: e.postPrecompute
|
|
1623
|
-
}), { millerLoopBatch: d, pairing: f, pairingBatch: b, calcPairingPrecomputes: m } = l, g = rn(l, c, u, e.G2.Signature, !1), w = rn(l, u, c, e.G1.ShortSignature, !0), T = e.randomBytes || or, H = () => {
|
|
1624
|
-
const p = Pn(r.ORDER);
|
|
1625
|
-
return Ir(T(p), r.ORDER);
|
|
1626
|
-
}, O = {
|
|
1627
|
-
randomSecretKey: H,
|
|
1628
|
-
randomPrivateKey: H,
|
|
1629
|
-
calcPairingPrecomputes: m
|
|
1630
|
-
}, { ShortSignature: _ } = e.G1, { Signature: A } = e.G2;
|
|
1631
|
-
function I(p, E) {
|
|
1632
|
-
return p instanceof c.Point ? p : w.hash(Ht("point", p), E?.DST);
|
|
1633
|
-
}
|
|
1634
|
-
function N(p, E) {
|
|
1635
|
-
return p instanceof u.Point ? p : g.hash(Ht("point", p), E?.DST);
|
|
1636
|
-
}
|
|
1637
|
-
function D(p) {
|
|
1638
|
-
return g.getPublicKey(p).toBytes(!0);
|
|
1639
|
-
}
|
|
1640
|
-
function S(p) {
|
|
1641
|
-
return w.getPublicKey(p).toBytes(!0);
|
|
1642
|
-
}
|
|
1643
|
-
function K(p, E, B) {
|
|
1644
|
-
const R = N(p, B), G = g.sign(R, E);
|
|
1645
|
-
return p instanceof u.Point ? G : A.toBytes(G);
|
|
1646
|
-
}
|
|
1647
|
-
function Y(p, E, B) {
|
|
1648
|
-
const R = I(p, B), G = w.sign(R, E);
|
|
1649
|
-
return p instanceof c.Point ? G : _.toBytes(G);
|
|
1650
|
-
}
|
|
1651
|
-
function q(p, E, B, R) {
|
|
1652
|
-
const G = N(E, R);
|
|
1653
|
-
return g.verify(p, G, B);
|
|
1654
|
-
}
|
|
1655
|
-
function h(p, E, B, R) {
|
|
1656
|
-
const G = I(E, R);
|
|
1657
|
-
return w.verify(p, G, B);
|
|
1658
|
-
}
|
|
1659
|
-
function y(p) {
|
|
1660
|
-
const E = g.aggregatePublicKeys(p);
|
|
1661
|
-
return p[0] instanceof c.Point ? E : E.toBytes(!0);
|
|
1662
|
-
}
|
|
1663
|
-
function v(p) {
|
|
1664
|
-
const E = g.aggregateSignatures(p);
|
|
1665
|
-
return p[0] instanceof u.Point ? E : A.toBytes(E);
|
|
1666
|
-
}
|
|
1667
|
-
function U(p) {
|
|
1668
|
-
const E = w.aggregateSignatures(p);
|
|
1669
|
-
return p[0] instanceof c.Point ? E : _.toBytes(E);
|
|
1670
|
-
}
|
|
1671
|
-
function M(p, E, B, R) {
|
|
1672
|
-
const G = E.map((J) => N(J, R));
|
|
1673
|
-
return g.verifyBatch(p, G, B);
|
|
1674
|
-
}
|
|
1675
|
-
return c.Point.BASE.precompute(4), {
|
|
1676
|
-
longSignatures: g,
|
|
1677
|
-
shortSignatures: w,
|
|
1678
|
-
millerLoopBatch: d,
|
|
1679
|
-
pairing: f,
|
|
1680
|
-
pairingBatch: b,
|
|
1681
|
-
verifyBatch: M,
|
|
1682
|
-
fields: {
|
|
1683
|
-
Fr: r,
|
|
1684
|
-
Fp: t,
|
|
1685
|
-
Fp2: n,
|
|
1686
|
-
Fp6: s,
|
|
1687
|
-
Fp12: o
|
|
1688
|
-
},
|
|
1689
|
-
params: {
|
|
1690
|
-
ateLoopSize: e.params.ateLoopSize,
|
|
1691
|
-
twistType: e.params.twistType,
|
|
1692
|
-
// deprecated
|
|
1693
|
-
r: e.params.r,
|
|
1694
|
-
G1b: e.G1.b,
|
|
1695
|
-
G2b: e.G2.b
|
|
1696
|
-
},
|
|
1697
|
-
utils: O,
|
|
1698
|
-
// deprecated
|
|
1699
|
-
getPublicKey: D,
|
|
1700
|
-
getPublicKeyForShortSignatures: S,
|
|
1701
|
-
sign: K,
|
|
1702
|
-
signShortSignature: Y,
|
|
1703
|
-
verify: q,
|
|
1704
|
-
verifyShortSignature: h,
|
|
1705
|
-
aggregatePublicKeys: y,
|
|
1706
|
-
aggregateSignatures: v,
|
|
1707
|
-
aggregateShortSignatures: U,
|
|
1708
|
-
G1: c,
|
|
1709
|
-
G2: u,
|
|
1710
|
-
Signature: A,
|
|
1711
|
-
ShortSignature: _
|
|
1712
|
-
};
|
|
1713
|
-
}
|
|
1714
|
-
const sn = BigInt(0), lt = BigInt(1), X = BigInt(2), on = BigInt(3);
|
|
1715
|
-
function Me(e, t, r, n, s = 1, o) {
|
|
1716
|
-
const i = BigInt(o === void 0 ? n : o), c = r ** BigInt(n), a = [];
|
|
1717
|
-
for (let u = 0; u < s; u++) {
|
|
1718
|
-
const l = BigInt(u + 1), d = [];
|
|
1719
|
-
for (let f = 0, b = lt; f < n; f++) {
|
|
1720
|
-
const m = (l * b - l) / i % c;
|
|
1721
|
-
d.push(e.pow(t, m)), b *= r;
|
|
1722
|
-
}
|
|
1723
|
-
a.push(d);
|
|
1724
|
-
}
|
|
1725
|
-
return a;
|
|
1726
|
-
}
|
|
1727
|
-
function Qr(e, t, r) {
|
|
1728
|
-
const n = t.pow(r, (e.ORDER - lt) / on), s = t.pow(r, (e.ORDER - lt) / X);
|
|
1729
|
-
function o(f, b) {
|
|
1730
|
-
const m = t.mul(t.frobeniusMap(f, 1), n), g = t.mul(t.frobeniusMap(b, 1), s);
|
|
1731
|
-
return [m, g];
|
|
1732
|
-
}
|
|
1733
|
-
const i = t.pow(r, (e.ORDER ** X - lt) / on), c = t.pow(r, (e.ORDER ** X - lt) / X);
|
|
1734
|
-
if (!t.eql(c, t.neg(t.ONE)))
|
|
1735
|
-
throw new Error("psiFrobenius: PSI2_Y!==-1");
|
|
1736
|
-
function a(f, b) {
|
|
1737
|
-
return [t.mul(f, i), t.neg(b)];
|
|
1738
|
-
}
|
|
1739
|
-
const u = (f) => (b, m) => {
|
|
1740
|
-
const g = m.toAffine(), w = f(g.x, g.y);
|
|
1741
|
-
return b.fromAffine({ x: w[0], y: w[1] });
|
|
1742
|
-
}, l = u(o), d = u(a);
|
|
1743
|
-
return { psi: o, psi2: a, G2psi: l, G2psi2: d, PSI_X: n, PSI_Y: s, PSI2_X: i, PSI2_Y: c };
|
|
1744
|
-
}
|
|
1745
|
-
const cn = (e, t) => {
|
|
1746
|
-
if (t.length !== 2)
|
|
1747
|
-
throw new Error("invalid tuple");
|
|
1748
|
-
const r = t.map((n) => e.create(n));
|
|
1749
|
-
return { c0: r[0], c1: r[1] };
|
|
1750
|
-
};
|
|
1751
|
-
class Jr {
|
|
1752
|
-
constructor(t, r = {}) {
|
|
1753
|
-
this.MASK = lt;
|
|
1754
|
-
const n = t.ORDER, s = n * n;
|
|
1755
|
-
this.Fp = t, this.ORDER = s, this.BITS = Vt(s), this.BYTES = Math.ceil(Vt(s) / 8), this.isLE = t.isLE, this.ZERO = { c0: t.ZERO, c1: t.ZERO }, this.ONE = { c0: t.ONE, c1: t.ZERO }, this.Fp_NONRESIDUE = t.create(r.NONRESIDUE || BigInt(-1)), this.Fp_div2 = t.div(t.ONE, X), this.NONRESIDUE = cn(t, r.FP2_NONRESIDUE), this.FROBENIUS_COEFFICIENTS = Me(t, this.Fp_NONRESIDUE, t.ORDER, 2)[0], this.mulByB = r.Fp2mulByB, Object.seal(this);
|
|
1756
|
-
}
|
|
1757
|
-
fromBigTuple(t) {
|
|
1758
|
-
return cn(this.Fp, t);
|
|
1759
|
-
}
|
|
1760
|
-
create(t) {
|
|
1761
|
-
return t;
|
|
1762
|
-
}
|
|
1763
|
-
isValid({ c0: t, c1: r }) {
|
|
1764
|
-
function n(s, o) {
|
|
1765
|
-
return typeof s == "bigint" && sn <= s && s < o;
|
|
1766
|
-
}
|
|
1767
|
-
return n(t, this.ORDER) && n(r, this.ORDER);
|
|
1768
|
-
}
|
|
1769
|
-
is0({ c0: t, c1: r }) {
|
|
1770
|
-
return this.Fp.is0(t) && this.Fp.is0(r);
|
|
1771
|
-
}
|
|
1772
|
-
isValidNot0(t) {
|
|
1773
|
-
return !this.is0(t) && this.isValid(t);
|
|
1774
|
-
}
|
|
1775
|
-
eql({ c0: t, c1: r }, { c0: n, c1: s }) {
|
|
1776
|
-
return this.Fp.eql(t, n) && this.Fp.eql(r, s);
|
|
1777
|
-
}
|
|
1778
|
-
neg({ c0: t, c1: r }) {
|
|
1779
|
-
return { c0: this.Fp.neg(t), c1: this.Fp.neg(r) };
|
|
1780
|
-
}
|
|
1781
|
-
pow(t, r) {
|
|
1782
|
-
return ae(this, t, r);
|
|
1783
|
-
}
|
|
1784
|
-
invertBatch(t) {
|
|
1785
|
-
return It(this, t);
|
|
1786
|
-
}
|
|
1787
|
-
// Normalized
|
|
1788
|
-
add(t, r) {
|
|
1789
|
-
const { c0: n, c1: s } = t, { c0: o, c1: i } = r;
|
|
1790
|
-
return {
|
|
1791
|
-
c0: this.Fp.add(n, o),
|
|
1792
|
-
c1: this.Fp.add(s, i)
|
|
1793
|
-
};
|
|
1794
|
-
}
|
|
1795
|
-
sub({ c0: t, c1: r }, { c0: n, c1: s }) {
|
|
1796
|
-
return {
|
|
1797
|
-
c0: this.Fp.sub(t, n),
|
|
1798
|
-
c1: this.Fp.sub(r, s)
|
|
1799
|
-
};
|
|
1800
|
-
}
|
|
1801
|
-
mul({ c0: t, c1: r }, n) {
|
|
1802
|
-
const { Fp: s } = this;
|
|
1803
|
-
if (typeof n == "bigint")
|
|
1804
|
-
return { c0: s.mul(t, n), c1: s.mul(r, n) };
|
|
1805
|
-
const { c0: o, c1: i } = n;
|
|
1806
|
-
let c = s.mul(t, o), a = s.mul(r, i);
|
|
1807
|
-
const u = s.sub(c, a), l = s.sub(s.mul(s.add(t, r), s.add(o, i)), s.add(c, a));
|
|
1808
|
-
return { c0: u, c1: l };
|
|
1809
|
-
}
|
|
1810
|
-
sqr({ c0: t, c1: r }) {
|
|
1811
|
-
const { Fp: n } = this, s = n.add(t, r), o = n.sub(t, r), i = n.add(t, t);
|
|
1812
|
-
return { c0: n.mul(s, o), c1: n.mul(i, r) };
|
|
1813
|
-
}
|
|
1814
|
-
// NonNormalized stuff
|
|
1815
|
-
addN(t, r) {
|
|
1816
|
-
return this.add(t, r);
|
|
1817
|
-
}
|
|
1818
|
-
subN(t, r) {
|
|
1819
|
-
return this.sub(t, r);
|
|
1820
|
-
}
|
|
1821
|
-
mulN(t, r) {
|
|
1822
|
-
return this.mul(t, r);
|
|
1823
|
-
}
|
|
1824
|
-
sqrN(t) {
|
|
1825
|
-
return this.sqr(t);
|
|
1826
|
-
}
|
|
1827
|
-
// Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
|
|
1828
|
-
div(t, r) {
|
|
1829
|
-
const { Fp: n } = this;
|
|
1830
|
-
return this.mul(t, typeof r == "bigint" ? n.inv(n.create(r)) : this.inv(r));
|
|
1831
|
-
}
|
|
1832
|
-
inv({ c0: t, c1: r }) {
|
|
1833
|
-
const { Fp: n } = this, s = n.inv(n.create(t * t + r * r));
|
|
1834
|
-
return { c0: n.mul(s, n.create(t)), c1: n.mul(s, n.create(-r)) };
|
|
1835
|
-
}
|
|
1836
|
-
sqrt(t) {
|
|
1837
|
-
const { Fp: r } = this, n = this, { c0: s, c1: o } = t;
|
|
1838
|
-
if (r.is0(o))
|
|
1839
|
-
return ne(r, s) === 1 ? n.create({ c0: r.sqrt(s), c1: r.ZERO }) : n.create({ c0: r.ZERO, c1: r.sqrt(r.div(s, this.Fp_NONRESIDUE)) });
|
|
1840
|
-
const i = r.sqrt(r.sub(r.sqr(s), r.mul(r.sqr(o), this.Fp_NONRESIDUE)));
|
|
1841
|
-
let c = r.mul(r.add(i, s), this.Fp_div2);
|
|
1842
|
-
ne(r, c) === -1 && (c = r.sub(c, i));
|
|
1843
|
-
const u = r.sqrt(c), l = n.create({ c0: u, c1: r.div(r.mul(o, this.Fp_div2), u) });
|
|
1844
|
-
if (!n.eql(n.sqr(l), t))
|
|
1845
|
-
throw new Error("Cannot find square root");
|
|
1846
|
-
const d = l, f = n.neg(d), { re: b, im: m } = n.reim(d), { re: g, im: w } = n.reim(f);
|
|
1847
|
-
return m > w || m === w && b > g ? d : f;
|
|
1848
|
-
}
|
|
1849
|
-
// Same as sgn0_m_eq_2 in RFC 9380
|
|
1850
|
-
isOdd(t) {
|
|
1851
|
-
const { re: r, im: n } = this.reim(t), s = r % X, o = r === sn, i = n % X;
|
|
1852
|
-
return BigInt(s || o && i) == lt;
|
|
1853
|
-
}
|
|
1854
|
-
// Bytes util
|
|
1855
|
-
fromBytes(t) {
|
|
1856
|
-
const { Fp: r } = this;
|
|
1857
|
-
if (t.length !== this.BYTES)
|
|
1858
|
-
throw new Error("fromBytes invalid length=" + t.length);
|
|
1859
|
-
return { c0: r.fromBytes(t.subarray(0, r.BYTES)), c1: r.fromBytes(t.subarray(r.BYTES)) };
|
|
1860
|
-
}
|
|
1861
|
-
toBytes({ c0: t, c1: r }) {
|
|
1862
|
-
return V(this.Fp.toBytes(t), this.Fp.toBytes(r));
|
|
1863
|
-
}
|
|
1864
|
-
cmov({ c0: t, c1: r }, { c0: n, c1: s }, o) {
|
|
1865
|
-
return {
|
|
1866
|
-
c0: this.Fp.cmov(t, n, o),
|
|
1867
|
-
c1: this.Fp.cmov(r, s, o)
|
|
1868
|
-
};
|
|
1869
|
-
}
|
|
1870
|
-
reim({ c0: t, c1: r }) {
|
|
1871
|
-
return { re: t, im: r };
|
|
1872
|
-
}
|
|
1873
|
-
Fp4Square(t, r) {
|
|
1874
|
-
const n = this, s = n.sqr(t), o = n.sqr(r);
|
|
1875
|
-
return {
|
|
1876
|
-
first: n.add(n.mulByNonresidue(o), s),
|
|
1877
|
-
// b² * Nonresidue + a²
|
|
1878
|
-
second: n.sub(n.sub(n.sqr(n.add(t, r)), s), o)
|
|
1879
|
-
// (a + b)² - a² - b²
|
|
1880
|
-
};
|
|
1881
|
-
}
|
|
1882
|
-
// multiply by u + 1
|
|
1883
|
-
mulByNonresidue({ c0: t, c1: r }) {
|
|
1884
|
-
return this.mul({ c0: t, c1: r }, this.NONRESIDUE);
|
|
1885
|
-
}
|
|
1886
|
-
frobeniusMap({ c0: t, c1: r }, n) {
|
|
1887
|
-
return {
|
|
1888
|
-
c0: t,
|
|
1889
|
-
c1: this.Fp.mul(r, this.FROBENIUS_COEFFICIENTS[n % 2])
|
|
1890
|
-
};
|
|
1891
|
-
}
|
|
1892
|
-
}
|
|
1893
|
-
class ts {
|
|
1894
|
-
constructor(t) {
|
|
1895
|
-
this.MASK = lt, this.Fp2 = t, this.ORDER = t.ORDER, this.BITS = 3 * t.BITS, this.BYTES = 3 * t.BYTES, this.isLE = t.isLE, this.ZERO = { c0: t.ZERO, c1: t.ZERO, c2: t.ZERO }, this.ONE = { c0: t.ONE, c1: t.ZERO, c2: t.ZERO };
|
|
1896
|
-
const { Fp: r } = t, n = Me(t, t.NONRESIDUE, r.ORDER, 6, 2, 3);
|
|
1897
|
-
this.FROBENIUS_COEFFICIENTS_1 = n[0], this.FROBENIUS_COEFFICIENTS_2 = n[1], Object.seal(this);
|
|
1898
|
-
}
|
|
1899
|
-
add({ c0: t, c1: r, c2: n }, { c0: s, c1: o, c2: i }) {
|
|
1900
|
-
const { Fp2: c } = this;
|
|
1901
|
-
return {
|
|
1902
|
-
c0: c.add(t, s),
|
|
1903
|
-
c1: c.add(r, o),
|
|
1904
|
-
c2: c.add(n, i)
|
|
1905
|
-
};
|
|
1906
|
-
}
|
|
1907
|
-
sub({ c0: t, c1: r, c2: n }, { c0: s, c1: o, c2: i }) {
|
|
1908
|
-
const { Fp2: c } = this;
|
|
1909
|
-
return {
|
|
1910
|
-
c0: c.sub(t, s),
|
|
1911
|
-
c1: c.sub(r, o),
|
|
1912
|
-
c2: c.sub(n, i)
|
|
1913
|
-
};
|
|
1914
|
-
}
|
|
1915
|
-
mul({ c0: t, c1: r, c2: n }, s) {
|
|
1916
|
-
const { Fp2: o } = this;
|
|
1917
|
-
if (typeof s == "bigint")
|
|
1918
|
-
return {
|
|
1919
|
-
c0: o.mul(t, s),
|
|
1920
|
-
c1: o.mul(r, s),
|
|
1921
|
-
c2: o.mul(n, s)
|
|
1922
|
-
};
|
|
1923
|
-
const { c0: i, c1: c, c2: a } = s, u = o.mul(t, i), l = o.mul(r, c), d = o.mul(n, a);
|
|
1924
|
-
return {
|
|
1925
|
-
// t0 + (c1 + c2) * (r1 * r2) - (T1 + T2) * (u + 1)
|
|
1926
|
-
c0: o.add(u, o.mulByNonresidue(o.sub(o.mul(o.add(r, n), o.add(c, a)), o.add(l, d)))),
|
|
1927
|
-
// (c0 + c1) * (r0 + r1) - (T0 + T1) + T2 * (u + 1)
|
|
1928
|
-
c1: o.add(o.sub(o.mul(o.add(t, r), o.add(i, c)), o.add(u, l)), o.mulByNonresidue(d)),
|
|
1929
|
-
// T1 + (c0 + c2) * (r0 + r2) - T0 + T2
|
|
1930
|
-
c2: o.sub(o.add(l, o.mul(o.add(t, n), o.add(i, a))), o.add(u, d))
|
|
1931
|
-
};
|
|
1932
|
-
}
|
|
1933
|
-
sqr({ c0: t, c1: r, c2: n }) {
|
|
1934
|
-
const { Fp2: s } = this;
|
|
1935
|
-
let o = s.sqr(t), i = s.mul(s.mul(t, r), X), c = s.mul(s.mul(r, n), X), a = s.sqr(n);
|
|
1936
|
-
return {
|
|
1937
|
-
c0: s.add(s.mulByNonresidue(c), o),
|
|
1938
|
-
// T3 * (u + 1) + T0
|
|
1939
|
-
c1: s.add(s.mulByNonresidue(a), i),
|
|
1940
|
-
// T4 * (u + 1) + T1
|
|
1941
|
-
// T1 + (c0 - c1 + c2)² + T3 - T0 - T4
|
|
1942
|
-
c2: s.sub(s.sub(s.add(s.add(i, s.sqr(s.add(s.sub(t, r), n))), c), o), a)
|
|
1943
|
-
};
|
|
1944
|
-
}
|
|
1945
|
-
addN(t, r) {
|
|
1946
|
-
return this.add(t, r);
|
|
1947
|
-
}
|
|
1948
|
-
subN(t, r) {
|
|
1949
|
-
return this.sub(t, r);
|
|
1950
|
-
}
|
|
1951
|
-
mulN(t, r) {
|
|
1952
|
-
return this.mul(t, r);
|
|
1953
|
-
}
|
|
1954
|
-
sqrN(t) {
|
|
1955
|
-
return this.sqr(t);
|
|
1956
|
-
}
|
|
1957
|
-
create(t) {
|
|
1958
|
-
return t;
|
|
1959
|
-
}
|
|
1960
|
-
isValid({ c0: t, c1: r, c2: n }) {
|
|
1961
|
-
const { Fp2: s } = this;
|
|
1962
|
-
return s.isValid(t) && s.isValid(r) && s.isValid(n);
|
|
1963
|
-
}
|
|
1964
|
-
is0({ c0: t, c1: r, c2: n }) {
|
|
1965
|
-
const { Fp2: s } = this;
|
|
1966
|
-
return s.is0(t) && s.is0(r) && s.is0(n);
|
|
1967
|
-
}
|
|
1968
|
-
isValidNot0(t) {
|
|
1969
|
-
return !this.is0(t) && this.isValid(t);
|
|
1970
|
-
}
|
|
1971
|
-
neg({ c0: t, c1: r, c2: n }) {
|
|
1972
|
-
const { Fp2: s } = this;
|
|
1973
|
-
return { c0: s.neg(t), c1: s.neg(r), c2: s.neg(n) };
|
|
1974
|
-
}
|
|
1975
|
-
eql({ c0: t, c1: r, c2: n }, { c0: s, c1: o, c2: i }) {
|
|
1976
|
-
const { Fp2: c } = this;
|
|
1977
|
-
return c.eql(t, s) && c.eql(r, o) && c.eql(n, i);
|
|
1978
|
-
}
|
|
1979
|
-
sqrt(t) {
|
|
1980
|
-
return In();
|
|
1981
|
-
}
|
|
1982
|
-
// Do we need division by bigint at all? Should be done via order:
|
|
1983
|
-
div(t, r) {
|
|
1984
|
-
const { Fp2: n } = this, { Fp: s } = n;
|
|
1985
|
-
return this.mul(t, typeof r == "bigint" ? s.inv(s.create(r)) : this.inv(r));
|
|
1986
|
-
}
|
|
1987
|
-
pow(t, r) {
|
|
1988
|
-
return ae(this, t, r);
|
|
1989
|
-
}
|
|
1990
|
-
invertBatch(t) {
|
|
1991
|
-
return It(this, t);
|
|
1992
|
-
}
|
|
1993
|
-
inv({ c0: t, c1: r, c2: n }) {
|
|
1994
|
-
const { Fp2: s } = this;
|
|
1995
|
-
let o = s.sub(s.sqr(t), s.mulByNonresidue(s.mul(n, r))), i = s.sub(s.mulByNonresidue(s.sqr(n)), s.mul(t, r)), c = s.sub(s.sqr(r), s.mul(t, n)), a = s.inv(s.add(s.mulByNonresidue(s.add(s.mul(n, i), s.mul(r, c))), s.mul(t, o)));
|
|
1996
|
-
return { c0: s.mul(a, o), c1: s.mul(a, i), c2: s.mul(a, c) };
|
|
1997
|
-
}
|
|
1998
|
-
// Bytes utils
|
|
1999
|
-
fromBytes(t) {
|
|
2000
|
-
const { Fp2: r } = this;
|
|
2001
|
-
if (t.length !== this.BYTES)
|
|
2002
|
-
throw new Error("fromBytes invalid length=" + t.length);
|
|
2003
|
-
const n = r.BYTES;
|
|
2004
|
-
return {
|
|
2005
|
-
c0: r.fromBytes(t.subarray(0, n)),
|
|
2006
|
-
c1: r.fromBytes(t.subarray(n, n * 2)),
|
|
2007
|
-
c2: r.fromBytes(t.subarray(2 * n))
|
|
2008
|
-
};
|
|
2009
|
-
}
|
|
2010
|
-
toBytes({ c0: t, c1: r, c2: n }) {
|
|
2011
|
-
const { Fp2: s } = this;
|
|
2012
|
-
return V(s.toBytes(t), s.toBytes(r), s.toBytes(n));
|
|
2013
|
-
}
|
|
2014
|
-
cmov({ c0: t, c1: r, c2: n }, { c0: s, c1: o, c2: i }, c) {
|
|
2015
|
-
const { Fp2: a } = this;
|
|
2016
|
-
return {
|
|
2017
|
-
c0: a.cmov(t, s, c),
|
|
2018
|
-
c1: a.cmov(r, o, c),
|
|
2019
|
-
c2: a.cmov(n, i, c)
|
|
2020
|
-
};
|
|
2021
|
-
}
|
|
2022
|
-
fromBigSix(t) {
|
|
2023
|
-
const { Fp2: r } = this;
|
|
2024
|
-
if (!Array.isArray(t) || t.length !== 6)
|
|
2025
|
-
throw new Error("invalid Fp6 usage");
|
|
2026
|
-
return {
|
|
2027
|
-
c0: r.fromBigTuple(t.slice(0, 2)),
|
|
2028
|
-
c1: r.fromBigTuple(t.slice(2, 4)),
|
|
2029
|
-
c2: r.fromBigTuple(t.slice(4, 6))
|
|
2030
|
-
};
|
|
2031
|
-
}
|
|
2032
|
-
frobeniusMap({ c0: t, c1: r, c2: n }, s) {
|
|
2033
|
-
const { Fp2: o } = this;
|
|
2034
|
-
return {
|
|
2035
|
-
c0: o.frobeniusMap(t, s),
|
|
2036
|
-
c1: o.mul(o.frobeniusMap(r, s), this.FROBENIUS_COEFFICIENTS_1[s % 6]),
|
|
2037
|
-
c2: o.mul(o.frobeniusMap(n, s), this.FROBENIUS_COEFFICIENTS_2[s % 6])
|
|
2038
|
-
};
|
|
2039
|
-
}
|
|
2040
|
-
mulByFp2({ c0: t, c1: r, c2: n }, s) {
|
|
2041
|
-
const { Fp2: o } = this;
|
|
2042
|
-
return {
|
|
2043
|
-
c0: o.mul(t, s),
|
|
2044
|
-
c1: o.mul(r, s),
|
|
2045
|
-
c2: o.mul(n, s)
|
|
2046
|
-
};
|
|
2047
|
-
}
|
|
2048
|
-
mulByNonresidue({ c0: t, c1: r, c2: n }) {
|
|
2049
|
-
const { Fp2: s } = this;
|
|
2050
|
-
return { c0: s.mulByNonresidue(n), c1: t, c2: r };
|
|
2051
|
-
}
|
|
2052
|
-
// Sparse multiplication
|
|
2053
|
-
mul1({ c0: t, c1: r, c2: n }, s) {
|
|
2054
|
-
const { Fp2: o } = this;
|
|
2055
|
-
return {
|
|
2056
|
-
c0: o.mulByNonresidue(o.mul(n, s)),
|
|
2057
|
-
c1: o.mul(t, s),
|
|
2058
|
-
c2: o.mul(r, s)
|
|
2059
|
-
};
|
|
2060
|
-
}
|
|
2061
|
-
// Sparse multiplication
|
|
2062
|
-
mul01({ c0: t, c1: r, c2: n }, s, o) {
|
|
2063
|
-
const { Fp2: i } = this;
|
|
2064
|
-
let c = i.mul(t, s), a = i.mul(r, o);
|
|
2065
|
-
return {
|
|
2066
|
-
// ((c1 + c2) * b1 - T1) * (u + 1) + T0
|
|
2067
|
-
c0: i.add(i.mulByNonresidue(i.sub(i.mul(i.add(r, n), o), a)), c),
|
|
2068
|
-
// (b0 + b1) * (c0 + c1) - T0 - T1
|
|
2069
|
-
c1: i.sub(i.sub(i.mul(i.add(s, o), i.add(t, r)), c), a),
|
|
2070
|
-
// (c0 + c2) * b0 - T0 + T1
|
|
2071
|
-
c2: i.add(i.sub(i.mul(i.add(t, n), s), c), a)
|
|
2072
|
-
};
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
class es {
|
|
2076
|
-
constructor(t, r) {
|
|
2077
|
-
this.MASK = lt;
|
|
2078
|
-
const { Fp2: n } = t, { Fp: s } = n;
|
|
2079
|
-
this.Fp6 = t, this.ORDER = n.ORDER, this.BITS = 2 * t.BITS, this.BYTES = 2 * t.BYTES, this.isLE = t.isLE, this.ZERO = { c0: t.ZERO, c1: t.ZERO }, this.ONE = { c0: t.ONE, c1: t.ZERO }, this.FROBENIUS_COEFFICIENTS = Me(n, n.NONRESIDUE, s.ORDER, 12, 1, 6)[0], this.X_LEN = r.X_LEN, this.finalExponentiate = r.Fp12finalExponentiate;
|
|
2080
|
-
}
|
|
2081
|
-
create(t) {
|
|
2082
|
-
return t;
|
|
2083
|
-
}
|
|
2084
|
-
isValid({ c0: t, c1: r }) {
|
|
2085
|
-
const { Fp6: n } = this;
|
|
2086
|
-
return n.isValid(t) && n.isValid(r);
|
|
2087
|
-
}
|
|
2088
|
-
is0({ c0: t, c1: r }) {
|
|
2089
|
-
const { Fp6: n } = this;
|
|
2090
|
-
return n.is0(t) && n.is0(r);
|
|
2091
|
-
}
|
|
2092
|
-
isValidNot0(t) {
|
|
2093
|
-
return !this.is0(t) && this.isValid(t);
|
|
2094
|
-
}
|
|
2095
|
-
neg({ c0: t, c1: r }) {
|
|
2096
|
-
const { Fp6: n } = this;
|
|
2097
|
-
return { c0: n.neg(t), c1: n.neg(r) };
|
|
2098
|
-
}
|
|
2099
|
-
eql({ c0: t, c1: r }, { c0: n, c1: s }) {
|
|
2100
|
-
const { Fp6: o } = this;
|
|
2101
|
-
return o.eql(t, n) && o.eql(r, s);
|
|
2102
|
-
}
|
|
2103
|
-
sqrt(t) {
|
|
2104
|
-
In();
|
|
2105
|
-
}
|
|
2106
|
-
inv({ c0: t, c1: r }) {
|
|
2107
|
-
const { Fp6: n } = this;
|
|
2108
|
-
let s = n.inv(n.sub(n.sqr(t), n.mulByNonresidue(n.sqr(r))));
|
|
2109
|
-
return { c0: n.mul(t, s), c1: n.neg(n.mul(r, s)) };
|
|
2110
|
-
}
|
|
2111
|
-
div(t, r) {
|
|
2112
|
-
const { Fp6: n } = this, { Fp2: s } = n, { Fp: o } = s;
|
|
2113
|
-
return this.mul(t, typeof r == "bigint" ? o.inv(o.create(r)) : this.inv(r));
|
|
2114
|
-
}
|
|
2115
|
-
pow(t, r) {
|
|
2116
|
-
return ae(this, t, r);
|
|
2117
|
-
}
|
|
2118
|
-
invertBatch(t) {
|
|
2119
|
-
return It(this, t);
|
|
2120
|
-
}
|
|
2121
|
-
// Normalized
|
|
2122
|
-
add({ c0: t, c1: r }, { c0: n, c1: s }) {
|
|
2123
|
-
const { Fp6: o } = this;
|
|
2124
|
-
return {
|
|
2125
|
-
c0: o.add(t, n),
|
|
2126
|
-
c1: o.add(r, s)
|
|
2127
|
-
};
|
|
2128
|
-
}
|
|
2129
|
-
sub({ c0: t, c1: r }, { c0: n, c1: s }) {
|
|
2130
|
-
const { Fp6: o } = this;
|
|
2131
|
-
return {
|
|
2132
|
-
c0: o.sub(t, n),
|
|
2133
|
-
c1: o.sub(r, s)
|
|
2134
|
-
};
|
|
2135
|
-
}
|
|
2136
|
-
mul({ c0: t, c1: r }, n) {
|
|
2137
|
-
const { Fp6: s } = this;
|
|
2138
|
-
if (typeof n == "bigint")
|
|
2139
|
-
return { c0: s.mul(t, n), c1: s.mul(r, n) };
|
|
2140
|
-
let { c0: o, c1: i } = n, c = s.mul(t, o), a = s.mul(r, i);
|
|
2141
|
-
return {
|
|
2142
|
-
c0: s.add(c, s.mulByNonresidue(a)),
|
|
2143
|
-
// T1 + T2 * v
|
|
2144
|
-
// (c0 + c1) * (r0 + r1) - (T1 + T2)
|
|
2145
|
-
c1: s.sub(s.mul(s.add(t, r), s.add(o, i)), s.add(c, a))
|
|
2146
|
-
};
|
|
2147
|
-
}
|
|
2148
|
-
sqr({ c0: t, c1: r }) {
|
|
2149
|
-
const { Fp6: n } = this;
|
|
2150
|
-
let s = n.mul(t, r);
|
|
2151
|
-
return {
|
|
2152
|
-
// (c1 * v + c0) * (c0 + c1) - AB - AB * v
|
|
2153
|
-
c0: n.sub(n.sub(n.mul(n.add(n.mulByNonresidue(r), t), n.add(t, r)), s), n.mulByNonresidue(s)),
|
|
2154
|
-
c1: n.add(s, s)
|
|
2155
|
-
};
|
|
2156
|
-
}
|
|
2157
|
-
// NonNormalized stuff
|
|
2158
|
-
addN(t, r) {
|
|
2159
|
-
return this.add(t, r);
|
|
2160
|
-
}
|
|
2161
|
-
subN(t, r) {
|
|
2162
|
-
return this.sub(t, r);
|
|
2163
|
-
}
|
|
2164
|
-
mulN(t, r) {
|
|
2165
|
-
return this.mul(t, r);
|
|
2166
|
-
}
|
|
2167
|
-
sqrN(t) {
|
|
2168
|
-
return this.sqr(t);
|
|
2169
|
-
}
|
|
2170
|
-
// Bytes utils
|
|
2171
|
-
fromBytes(t) {
|
|
2172
|
-
const { Fp6: r } = this;
|
|
2173
|
-
if (t.length !== this.BYTES)
|
|
2174
|
-
throw new Error("fromBytes invalid length=" + t.length);
|
|
2175
|
-
return {
|
|
2176
|
-
c0: r.fromBytes(t.subarray(0, r.BYTES)),
|
|
2177
|
-
c1: r.fromBytes(t.subarray(r.BYTES))
|
|
2178
|
-
};
|
|
2179
|
-
}
|
|
2180
|
-
toBytes({ c0: t, c1: r }) {
|
|
2181
|
-
const { Fp6: n } = this;
|
|
2182
|
-
return V(n.toBytes(t), n.toBytes(r));
|
|
2183
|
-
}
|
|
2184
|
-
cmov({ c0: t, c1: r }, { c0: n, c1: s }, o) {
|
|
2185
|
-
const { Fp6: i } = this;
|
|
2186
|
-
return {
|
|
2187
|
-
c0: i.cmov(t, n, o),
|
|
2188
|
-
c1: i.cmov(r, s, o)
|
|
2189
|
-
};
|
|
2190
|
-
}
|
|
2191
|
-
// Utils
|
|
2192
|
-
// toString() {
|
|
2193
|
-
// return '' + 'Fp12(' + this.c0 + this.c1 + '* w');
|
|
2194
|
-
// },
|
|
2195
|
-
// fromTuple(c: [Fp6, Fp6]) {
|
|
2196
|
-
// return new Fp12(...c);
|
|
2197
|
-
// }
|
|
2198
|
-
fromBigTwelve(t) {
|
|
2199
|
-
const { Fp6: r } = this;
|
|
2200
|
-
return {
|
|
2201
|
-
c0: r.fromBigSix(t.slice(0, 6)),
|
|
2202
|
-
c1: r.fromBigSix(t.slice(6, 12))
|
|
2203
|
-
};
|
|
2204
|
-
}
|
|
2205
|
-
// Raises to q**i -th power
|
|
2206
|
-
frobeniusMap(t, r) {
|
|
2207
|
-
const { Fp6: n } = this, { Fp2: s } = n, { c0: o, c1: i, c2: c } = n.frobeniusMap(t.c1, r), a = this.FROBENIUS_COEFFICIENTS[r % 12];
|
|
2208
|
-
return {
|
|
2209
|
-
c0: n.frobeniusMap(t.c0, r),
|
|
2210
|
-
c1: n.create({
|
|
2211
|
-
c0: s.mul(o, a),
|
|
2212
|
-
c1: s.mul(i, a),
|
|
2213
|
-
c2: s.mul(c, a)
|
|
2214
|
-
})
|
|
2215
|
-
};
|
|
2216
|
-
}
|
|
2217
|
-
mulByFp2({ c0: t, c1: r }, n) {
|
|
2218
|
-
const { Fp6: s } = this;
|
|
2219
|
-
return {
|
|
2220
|
-
c0: s.mulByFp2(t, n),
|
|
2221
|
-
c1: s.mulByFp2(r, n)
|
|
2222
|
-
};
|
|
2223
|
-
}
|
|
2224
|
-
conjugate({ c0: t, c1: r }) {
|
|
2225
|
-
return { c0: t, c1: this.Fp6.neg(r) };
|
|
2226
|
-
}
|
|
2227
|
-
// Sparse multiplication
|
|
2228
|
-
mul014({ c0: t, c1: r }, n, s, o) {
|
|
2229
|
-
const { Fp6: i } = this, { Fp2: c } = i;
|
|
2230
|
-
let a = i.mul01(t, n, s), u = i.mul1(r, o);
|
|
2231
|
-
return {
|
|
2232
|
-
c0: i.add(i.mulByNonresidue(u), a),
|
|
2233
|
-
// T1 * v + T0
|
|
2234
|
-
// (c1 + c0) * [o0, o1+o4] - T0 - T1
|
|
2235
|
-
c1: i.sub(i.sub(i.mul01(i.add(r, t), n, c.add(s, o)), a), u)
|
|
2236
|
-
};
|
|
2237
|
-
}
|
|
2238
|
-
mul034({ c0: t, c1: r }, n, s, o) {
|
|
2239
|
-
const { Fp6: i } = this, { Fp2: c } = i, a = i.create({
|
|
2240
|
-
c0: c.mul(t.c0, n),
|
|
2241
|
-
c1: c.mul(t.c1, n),
|
|
2242
|
-
c2: c.mul(t.c2, n)
|
|
2243
|
-
}), u = i.mul01(r, s, o), l = i.mul01(i.add(t, r), c.add(n, s), o);
|
|
2244
|
-
return {
|
|
2245
|
-
c0: i.add(i.mulByNonresidue(u), a),
|
|
2246
|
-
c1: i.sub(l, i.add(a, u))
|
|
2247
|
-
};
|
|
2248
|
-
}
|
|
2249
|
-
// A cyclotomic group is a subgroup of Fp^n defined by
|
|
2250
|
-
// GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
|
|
2251
|
-
// The result of any pairing is in a cyclotomic subgroup
|
|
2252
|
-
// https://eprint.iacr.org/2009/565.pdf
|
|
2253
|
-
// https://eprint.iacr.org/2010/354.pdf
|
|
2254
|
-
_cyclotomicSquare({ c0: t, c1: r }) {
|
|
2255
|
-
const { Fp6: n } = this, { Fp2: s } = n, { c0: o, c1: i, c2: c } = t, { c0: a, c1: u, c2: l } = r, { first: d, second: f } = s.Fp4Square(o, u), { first: b, second: m } = s.Fp4Square(a, c), { first: g, second: w } = s.Fp4Square(i, l), T = s.mulByNonresidue(w);
|
|
2256
|
-
return {
|
|
2257
|
-
c0: n.create({
|
|
2258
|
-
c0: s.add(s.mul(s.sub(d, o), X), d),
|
|
2259
|
-
// 2 * (T3 - c0c0) + T3
|
|
2260
|
-
c1: s.add(s.mul(s.sub(b, i), X), b),
|
|
2261
|
-
// 2 * (T5 - c0c1) + T5
|
|
2262
|
-
c2: s.add(s.mul(s.sub(g, c), X), g)
|
|
2263
|
-
}),
|
|
2264
|
-
// 2 * (T7 - c0c2) + T7
|
|
2265
|
-
c1: n.create({
|
|
2266
|
-
c0: s.add(s.mul(s.add(T, a), X), T),
|
|
2267
|
-
// 2 * (T9 + c1c0) + T9
|
|
2268
|
-
c1: s.add(s.mul(s.add(f, u), X), f),
|
|
2269
|
-
// 2 * (T4 + c1c1) + T4
|
|
2270
|
-
c2: s.add(s.mul(s.add(m, l), X), m)
|
|
2271
|
-
})
|
|
2272
|
-
};
|
|
2273
|
-
}
|
|
2274
|
-
// https://eprint.iacr.org/2009/565.pdf
|
|
2275
|
-
_cyclotomicExp(t, r) {
|
|
2276
|
-
let n = this.ONE;
|
|
2277
|
-
for (let s = this.X_LEN - 1; s >= 0; s--)
|
|
2278
|
-
n = this._cyclotomicSquare(n), wr(r, s) && (n = this.mul(n, t));
|
|
2279
|
-
return n;
|
|
2280
|
-
}
|
|
2281
|
-
}
|
|
2282
|
-
function ns(e) {
|
|
2283
|
-
const t = Pt(e.ORDER), r = new Jr(t, e), n = new ts(r), s = new es(n, e);
|
|
2284
|
-
return { Fp: t, Fp2: r, Fp6: n, Fp12: s };
|
|
2285
|
-
}
|
|
2286
|
-
const z = BigInt(0), oe = BigInt(1), rt = BigInt(2), fe = BigInt(3), Ct = BigInt(4), yt = BigInt("0xd201000000010000"), rs = Vt(yt), At = {
|
|
2287
|
-
p: BigInt("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"),
|
|
2288
|
-
n: BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"),
|
|
2289
|
-
h: BigInt("0x396c8c005555e1568c00aaab0000aaab"),
|
|
2290
|
-
a: z,
|
|
2291
|
-
b: Ct,
|
|
2292
|
-
Gx: BigInt("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb"),
|
|
2293
|
-
Gy: BigInt("0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1")
|
|
2294
|
-
}, ss = Pt(At.n, {
|
|
2295
|
-
modFromBytes: !0,
|
|
2296
|
-
isLE: !0
|
|
2297
|
-
}), { Fp: x, Fp2: L, Fp6: os, Fp12: k } = ns({
|
|
2298
|
-
ORDER: At.p,
|
|
2299
|
-
X_LEN: rs,
|
|
2300
|
-
// Finite extension field over irreducible polynominal.
|
|
2301
|
-
// Fp(u) / (u² - β) where β = -1
|
|
2302
|
-
FP2_NONRESIDUE: [oe, oe],
|
|
2303
|
-
Fp2mulByB: ({ c0: e, c1: t }) => {
|
|
2304
|
-
const r = x.mul(e, Ct), n = x.mul(t, Ct);
|
|
2305
|
-
return { c0: x.sub(r, n), c1: x.add(r, n) };
|
|
2306
|
-
},
|
|
2307
|
-
Fp12finalExponentiate: (e) => {
|
|
2308
|
-
const t = yt, r = k.div(k.frobeniusMap(e, 6), e), n = k.mul(k.frobeniusMap(r, 2), r), s = k.conjugate(k._cyclotomicExp(n, t)), o = k.mul(k.conjugate(k._cyclotomicSquare(n)), s), i = k.conjugate(k._cyclotomicExp(o, t)), c = k.conjugate(k._cyclotomicExp(i, t)), a = k.mul(k.conjugate(k._cyclotomicExp(c, t)), k._cyclotomicSquare(s)), u = k.conjugate(k._cyclotomicExp(a, t)), l = k.frobeniusMap(k.mul(s, c), 2), d = k.frobeniusMap(k.mul(i, n), 3), f = k.frobeniusMap(k.mul(a, k.conjugate(n)), 1), b = k.mul(k.mul(u, k.conjugate(o)), n);
|
|
2309
|
-
return k.mul(k.mul(k.mul(l, d), f), b);
|
|
2310
|
-
}
|
|
2311
|
-
}), { G2psi: an, G2psi2: is } = Qr(x, L, L.div(L.ONE, L.NONRESIDUE)), Ee = Object.freeze({
|
|
2312
|
-
DST: "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_",
|
|
2313
|
-
encodeDST: "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_",
|
|
2314
|
-
p: x.ORDER,
|
|
2315
|
-
m: 2,
|
|
2316
|
-
k: 128,
|
|
2317
|
-
expand: "xmd",
|
|
2318
|
-
hash: Sn
|
|
2319
|
-
}), Ze = {
|
|
2320
|
-
p: L.ORDER,
|
|
2321
|
-
n: At.n,
|
|
2322
|
-
h: BigInt("0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5"),
|
|
2323
|
-
a: L.ZERO,
|
|
2324
|
-
b: L.fromBigTuple([Ct, Ct]),
|
|
2325
|
-
Gx: L.fromBigTuple([
|
|
2326
|
-
BigInt("0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8"),
|
|
2327
|
-
BigInt("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e")
|
|
2328
|
-
]),
|
|
2329
|
-
Gy: L.fromBigTuple([
|
|
2330
|
-
BigInt("0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801"),
|
|
2331
|
-
BigInt("0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be")
|
|
2332
|
-
])
|
|
2333
|
-
}, ue = jt(x.toBytes(z), { infinity: !0, compressed: !0 });
|
|
2334
|
-
function de(e) {
|
|
2335
|
-
e = e.slice();
|
|
2336
|
-
const t = e[0] & 224, r = !!(t >> 7 & 1), n = !!(t >> 6 & 1), s = !!(t >> 5 & 1);
|
|
2337
|
-
return e[0] &= 31, { compressed: r, infinity: n, sort: s, value: e };
|
|
2338
|
-
}
|
|
2339
|
-
function jt(e, t) {
|
|
2340
|
-
if (e[0] & 224)
|
|
2341
|
-
throw new Error("setMask: non-empty mask");
|
|
2342
|
-
return t.compressed && (e[0] |= 128), t.infinity && (e[0] |= 64), t.sort && (e[0] |= 32), e;
|
|
2343
|
-
}
|
|
2344
|
-
function cs(e, t, r) {
|
|
2345
|
-
const { BYTES: n, ORDER: s } = x, o = t.is0(), { x: i, y: c } = t.toAffine();
|
|
2346
|
-
if (r) {
|
|
2347
|
-
if (o)
|
|
2348
|
-
return ue.slice();
|
|
2349
|
-
const a = !!(c * rt / s);
|
|
2350
|
-
return jt(C(i, n), { compressed: !0, sort: a });
|
|
2351
|
-
} else
|
|
2352
|
-
return o ? V(Uint8Array.of(64), new Uint8Array(2 * n - 1)) : V(C(i, n), C(c, n));
|
|
2353
|
-
}
|
|
2354
|
-
function xe(e) {
|
|
2355
|
-
e.assertValidity();
|
|
2356
|
-
const { BYTES: t, ORDER: r } = x, { x: n, y: s } = e.toAffine();
|
|
2357
|
-
if (e.is0())
|
|
2358
|
-
return ue.slice();
|
|
2359
|
-
const o = !!(s * rt / r);
|
|
2360
|
-
return jt(C(n, t), { compressed: !0, sort: o });
|
|
2361
|
-
}
|
|
2362
|
-
function as(e) {
|
|
2363
|
-
const { compressed: t, infinity: r, sort: n, value: s } = de(e), { BYTES: o, ORDER: i } = x;
|
|
2364
|
-
if (s.length === 48 && t) {
|
|
2365
|
-
const c = ft(s), a = x.create(c & Ot(x.BITS));
|
|
2366
|
-
if (r) {
|
|
2367
|
-
if (a !== z)
|
|
2368
|
-
throw new Error("invalid G1 point: non-empty, at infinity, with compression");
|
|
2369
|
-
return { x: z, y: z };
|
|
2370
|
-
}
|
|
2371
|
-
const u = x.add(x.pow(a, fe), x.create(At.b));
|
|
2372
|
-
let l = x.sqrt(u);
|
|
2373
|
-
if (!l)
|
|
2374
|
-
throw new Error("invalid G1 point: compressed point");
|
|
2375
|
-
return l * rt / i !== BigInt(n) && (l = x.neg(l)), { x: x.create(a), y: x.create(l) };
|
|
2376
|
-
} else if (s.length === 96 && !t) {
|
|
2377
|
-
const c = ft(s.subarray(0, o)), a = ft(s.subarray(o));
|
|
2378
|
-
if (r) {
|
|
2379
|
-
if (c !== z || a !== z)
|
|
2380
|
-
throw new Error("G1: non-empty point at infinity");
|
|
2381
|
-
return P.G1.Point.ZERO.toAffine();
|
|
2382
|
-
}
|
|
2383
|
-
return { x: x.create(c), y: x.create(a) };
|
|
2384
|
-
} else
|
|
2385
|
-
throw new Error("invalid G1 point: expected 48/96 bytes");
|
|
2386
|
-
}
|
|
2387
|
-
function fn(e) {
|
|
2388
|
-
const { infinity: t, sort: r, value: n } = de(Ht("signatureHex", e, 48)), s = x.ORDER, o = P.G1.Point, i = ft(n);
|
|
2389
|
-
if (t)
|
|
2390
|
-
return o.ZERO;
|
|
2391
|
-
const c = x.create(i & Ot(x.BITS)), a = x.add(x.pow(c, fe), x.create(At.b));
|
|
2392
|
-
let u = x.sqrt(a);
|
|
2393
|
-
if (!u)
|
|
2394
|
-
throw new Error("invalid G1 point: compressed");
|
|
2395
|
-
const l = BigInt(r);
|
|
2396
|
-
u * rt / s !== l && (u = x.neg(u));
|
|
2397
|
-
const d = o.fromAffine({ x: c, y: u });
|
|
2398
|
-
return d.assertValidity(), d;
|
|
2399
|
-
}
|
|
2400
|
-
function fs(e, t, r) {
|
|
2401
|
-
const { BYTES: n, ORDER: s } = x, o = t.is0(), { x: i, y: c } = t.toAffine();
|
|
2402
|
-
if (r) {
|
|
2403
|
-
if (o)
|
|
2404
|
-
return V(ue, C(z, n));
|
|
2405
|
-
const a = !!(c.c1 === z ? c.c0 * rt / s : c.c1 * rt / s);
|
|
2406
|
-
return V(jt(C(i.c1, n), { compressed: !0, sort: a }), C(i.c0, n));
|
|
2407
|
-
} else {
|
|
2408
|
-
if (o)
|
|
2409
|
-
return V(Uint8Array.of(64), new Uint8Array(4 * n - 1));
|
|
2410
|
-
const { re: a, im: u } = L.reim(i), { re: l, im: d } = L.reim(c);
|
|
2411
|
-
return V(C(u, n), C(a, n), C(d, n), C(l, n));
|
|
2412
|
-
}
|
|
2413
|
-
}
|
|
2414
|
-
function Be(e) {
|
|
2415
|
-
e.assertValidity();
|
|
2416
|
-
const { BYTES: t } = x;
|
|
2417
|
-
if (e.is0())
|
|
2418
|
-
return V(ue, C(z, t));
|
|
2419
|
-
const { x: r, y: n } = e.toAffine(), { re: s, im: o } = L.reim(r), { re: i, im: c } = L.reim(n), u = !!((c > z ? c * rt : i * rt) / x.ORDER & oe), l = s;
|
|
2420
|
-
return V(jt(C(o, t), { sort: u, compressed: !0 }), C(l, t));
|
|
2421
|
-
}
|
|
2422
|
-
function us(e) {
|
|
2423
|
-
const { BYTES: t, ORDER: r } = x, { compressed: n, infinity: s, sort: o, value: i } = de(e);
|
|
2424
|
-
if (!n && !s && o || // 00100000
|
|
2425
|
-
!n && s && o || // 01100000
|
|
2426
|
-
o && s && n)
|
|
2427
|
-
throw new Error("invalid encoding flag: " + (e[0] & 224));
|
|
2428
|
-
const c = (a, u, l) => ft(a.slice(u, l));
|
|
2429
|
-
if (i.length === 96 && n) {
|
|
2430
|
-
if (s) {
|
|
2431
|
-
if (i.reduce((m, g) => m !== 0 ? g + 1 : g, 0) > 0)
|
|
2432
|
-
throw new Error("invalid G2 point: compressed");
|
|
2433
|
-
return { x: L.ZERO, y: L.ZERO };
|
|
2434
|
-
}
|
|
2435
|
-
const a = c(i, 0, t), u = c(i, t, 2 * t), l = L.create({ c0: x.create(u), c1: x.create(a) }), d = L.add(L.pow(l, fe), Ze.b);
|
|
2436
|
-
let f = L.sqrt(d);
|
|
2437
|
-
const b = f.c1 === z ? f.c0 * rt / r : f.c1 * rt / r ? oe : z;
|
|
2438
|
-
return f = o && b > 0 ? f : L.neg(f), { x: l, y: f };
|
|
2439
|
-
} else if (i.length === 192 && !n) {
|
|
2440
|
-
if (s) {
|
|
2441
|
-
if (i.reduce((f, b) => f !== 0 ? b + 1 : b, 0) > 0)
|
|
2442
|
-
throw new Error("invalid G2 point: uncompressed");
|
|
2443
|
-
return { x: L.ZERO, y: L.ZERO };
|
|
2444
|
-
}
|
|
2445
|
-
const a = c(i, 0 * t, 1 * t), u = c(i, 1 * t, 2 * t), l = c(i, 2 * t, 3 * t), d = c(i, 3 * t, 4 * t);
|
|
2446
|
-
return { x: L.fromBigTuple([u, a]), y: L.fromBigTuple([d, l]) };
|
|
2447
|
-
} else
|
|
2448
|
-
throw new Error("invalid G2 point: expected 96/192 bytes");
|
|
2449
|
-
}
|
|
2450
|
-
function un(e) {
|
|
2451
|
-
const { ORDER: t } = x, { infinity: r, sort: n, value: s } = de(Ht("signatureHex", e)), o = P.G2.Point, i = s.length / 2;
|
|
2452
|
-
if (i !== 48 && i !== 96)
|
|
2453
|
-
throw new Error("invalid compressed signature length, expected 96/192 bytes");
|
|
2454
|
-
const c = ft(s.slice(0, i)), a = ft(s.slice(i));
|
|
2455
|
-
if (r)
|
|
2456
|
-
return o.ZERO;
|
|
2457
|
-
const u = x.create(c & Ot(x.BITS)), l = x.create(a), d = L.create({ c0: l, c1: u }), f = L.add(L.pow(d, fe), Ze.b);
|
|
2458
|
-
let b = L.sqrt(f);
|
|
2459
|
-
if (!b)
|
|
2460
|
-
throw new Error("Failed to find a square root");
|
|
2461
|
-
const { re: m, im: g } = L.reim(b), w = BigInt(n), T = g > z && g * rt / t !== w, H = g === z && m * rt / t !== w;
|
|
2462
|
-
(T || H) && (b = L.neg(b));
|
|
2463
|
-
const O = o.fromAffine({ x: d, y: b });
|
|
2464
|
-
return O.assertValidity(), O;
|
|
2465
|
-
}
|
|
2466
|
-
const P = Wr({
|
|
2467
|
-
// Fields
|
|
2468
|
-
fields: {
|
|
2469
|
-
Fp: x,
|
|
2470
|
-
Fp2: L,
|
|
2471
|
-
Fp6: os,
|
|
2472
|
-
Fp12: k,
|
|
2473
|
-
Fr: ss
|
|
2474
|
-
},
|
|
2475
|
-
// G1: y² = x³ + 4
|
|
2476
|
-
G1: {
|
|
2477
|
-
...At,
|
|
2478
|
-
Fp: x,
|
|
2479
|
-
htfDefaults: { ...Ee, m: 1, DST: "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" },
|
|
2480
|
-
wrapPrivateKey: !0,
|
|
2481
|
-
allowInfinityPoint: !0,
|
|
2482
|
-
// Checks is the point resides in prime-order subgroup.
|
|
2483
|
-
// point.isTorsionFree() should return true for valid points
|
|
2484
|
-
// It returns false for shitty points.
|
|
2485
|
-
// https://eprint.iacr.org/2021/1130.pdf
|
|
2486
|
-
isTorsionFree: (e, t) => {
|
|
2487
|
-
const r = BigInt("0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe"), n = new e(x.mul(t.X, r), t.Y, t.Z);
|
|
2488
|
-
return t.multiplyUnsafe(yt).negate().multiplyUnsafe(yt).equals(n);
|
|
2489
|
-
},
|
|
2490
|
-
// Clear cofactor of G1
|
|
2491
|
-
// https://eprint.iacr.org/2019/403
|
|
2492
|
-
clearCofactor: (e, t) => t.multiplyUnsafe(yt).add(t),
|
|
2493
|
-
mapToCurve: ms,
|
|
2494
|
-
fromBytes: as,
|
|
2495
|
-
toBytes: cs,
|
|
2496
|
-
ShortSignature: {
|
|
2497
|
-
fromBytes(e) {
|
|
2498
|
-
return Q(e), fn(e);
|
|
2499
|
-
},
|
|
2500
|
-
fromHex(e) {
|
|
2501
|
-
return fn(e);
|
|
2502
|
-
},
|
|
2503
|
-
toBytes(e) {
|
|
2504
|
-
return xe(e);
|
|
2505
|
-
},
|
|
2506
|
-
toRawBytes(e) {
|
|
2507
|
-
return xe(e);
|
|
2508
|
-
},
|
|
2509
|
-
toHex(e) {
|
|
2510
|
-
return Yt(xe(e));
|
|
2511
|
-
}
|
|
2512
|
-
}
|
|
2513
|
-
},
|
|
2514
|
-
G2: {
|
|
2515
|
-
...Ze,
|
|
2516
|
-
Fp: L,
|
|
2517
|
-
// https://datatracker.ietf.org/doc/html/rfc9380#name-clearing-the-cofactor
|
|
2518
|
-
// https://datatracker.ietf.org/doc/html/rfc9380#name-cofactor-clearing-for-bls12
|
|
2519
|
-
hEff: BigInt("0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551"),
|
|
2520
|
-
htfDefaults: { ...Ee },
|
|
2521
|
-
wrapPrivateKey: !0,
|
|
2522
|
-
allowInfinityPoint: !0,
|
|
2523
|
-
mapToCurve: gs,
|
|
2524
|
-
// Checks is the point resides in prime-order subgroup.
|
|
2525
|
-
// point.isTorsionFree() should return true for valid points
|
|
2526
|
-
// It returns false for shitty points.
|
|
2527
|
-
// https://eprint.iacr.org/2021/1130.pdf
|
|
2528
|
-
// Older version: https://eprint.iacr.org/2019/814.pdf
|
|
2529
|
-
isTorsionFree: (e, t) => t.multiplyUnsafe(yt).negate().equals(an(e, t)),
|
|
2530
|
-
// Maps the point into the prime-order subgroup G2.
|
|
2531
|
-
// clear_cofactor_bls12381_g2 from RFC 9380.
|
|
2532
|
-
// https://eprint.iacr.org/2017/419.pdf
|
|
2533
|
-
// prettier-ignore
|
|
2534
|
-
clearCofactor: (e, t) => {
|
|
2535
|
-
const r = yt;
|
|
2536
|
-
let n = t.multiplyUnsafe(r).negate(), s = an(e, t), o = t.double();
|
|
2537
|
-
return o = is(e, o), o = o.subtract(s), s = n.add(s), s = s.multiplyUnsafe(r).negate(), o = o.add(s), o = o.subtract(n), o.subtract(t);
|
|
2538
|
-
},
|
|
2539
|
-
fromBytes: us,
|
|
2540
|
-
toBytes: fs,
|
|
2541
|
-
Signature: {
|
|
2542
|
-
fromBytes(e) {
|
|
2543
|
-
return Q(e), un(e);
|
|
2544
|
-
},
|
|
2545
|
-
fromHex(e) {
|
|
2546
|
-
return un(e);
|
|
2547
|
-
},
|
|
2548
|
-
toBytes(e) {
|
|
2549
|
-
return Be(e);
|
|
2550
|
-
},
|
|
2551
|
-
toRawBytes(e) {
|
|
2552
|
-
return Be(e);
|
|
2553
|
-
},
|
|
2554
|
-
toHex(e) {
|
|
2555
|
-
return Yt(Be(e));
|
|
2556
|
-
}
|
|
2557
|
-
}
|
|
2558
|
-
},
|
|
2559
|
-
params: {
|
|
2560
|
-
ateLoopSize: yt,
|
|
2561
|
-
// The BLS parameter x for BLS12-381
|
|
2562
|
-
r: At.n,
|
|
2563
|
-
// order; z⁴ − z² + 1; CURVE.n from other curves
|
|
2564
|
-
xNegative: !0,
|
|
2565
|
-
twistType: "multiplicative"
|
|
2566
|
-
},
|
|
2567
|
-
htfDefaults: Ee
|
|
2568
|
-
}), ds = Kn(L, [
|
|
2569
|
-
// xNum
|
|
2570
|
-
[
|
|
2571
|
-
[
|
|
2572
|
-
"0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6",
|
|
2573
|
-
"0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6"
|
|
2574
|
-
],
|
|
2575
|
-
[
|
|
2576
|
-
"0x0",
|
|
2577
|
-
"0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71a"
|
|
2578
|
-
],
|
|
2579
|
-
[
|
|
2580
|
-
"0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71e",
|
|
2581
|
-
"0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38d"
|
|
2582
|
-
],
|
|
2583
|
-
[
|
|
2584
|
-
"0x171d6541fa38ccfaed6dea691f5fb614cb14b4e7f4e810aa22d6108f142b85757098e38d0f671c7188e2aaaaaaaa5ed1",
|
|
2585
|
-
"0x0"
|
|
2586
|
-
]
|
|
2587
|
-
],
|
|
2588
|
-
// xDen
|
|
2589
|
-
[
|
|
2590
|
-
[
|
|
2591
|
-
"0x0",
|
|
2592
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa63"
|
|
2593
|
-
],
|
|
2594
|
-
[
|
|
2595
|
-
"0xc",
|
|
2596
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa9f"
|
|
2597
|
-
],
|
|
2598
|
-
["0x1", "0x0"]
|
|
2599
|
-
// LAST 1
|
|
2600
|
-
],
|
|
2601
|
-
// yNum
|
|
2602
|
-
[
|
|
2603
|
-
[
|
|
2604
|
-
"0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706",
|
|
2605
|
-
"0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706"
|
|
2606
|
-
],
|
|
2607
|
-
[
|
|
2608
|
-
"0x0",
|
|
2609
|
-
"0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97be"
|
|
2610
|
-
],
|
|
2611
|
-
[
|
|
2612
|
-
"0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71c",
|
|
2613
|
-
"0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38f"
|
|
2614
|
-
],
|
|
2615
|
-
[
|
|
2616
|
-
"0x124c9ad43b6cf79bfbf7043de3811ad0761b0f37a1e26286b0e977c69aa274524e79097a56dc4bd9e1b371c71c718b10",
|
|
2617
|
-
"0x0"
|
|
2618
|
-
]
|
|
2619
|
-
],
|
|
2620
|
-
// yDen
|
|
2621
|
-
[
|
|
2622
|
-
[
|
|
2623
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb",
|
|
2624
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb"
|
|
2625
|
-
],
|
|
2626
|
-
[
|
|
2627
|
-
"0x0",
|
|
2628
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa9d3"
|
|
2629
|
-
],
|
|
2630
|
-
[
|
|
2631
|
-
"0x12",
|
|
2632
|
-
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa99"
|
|
2633
|
-
],
|
|
2634
|
-
["0x1", "0x0"]
|
|
2635
|
-
// LAST 1
|
|
2636
|
-
]
|
|
2637
|
-
].map((e) => e.map((t) => L.fromBigTuple(t.map(BigInt))))), ls = Kn(x, [
|
|
2638
|
-
// xNum
|
|
2639
|
-
[
|
|
2640
|
-
"0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7",
|
|
2641
|
-
"0x17294ed3e943ab2f0588bab22147a81c7c17e75b2f6a8417f565e33c70d1e86b4838f2a6f318c356e834eef1b3cb83bb",
|
|
2642
|
-
"0xd54005db97678ec1d1048c5d10a9a1bce032473295983e56878e501ec68e25c958c3e3d2a09729fe0179f9dac9edcb0",
|
|
2643
|
-
"0x1778e7166fcc6db74e0609d307e55412d7f5e4656a8dbf25f1b33289f1b330835336e25ce3107193c5b388641d9b6861",
|
|
2644
|
-
"0xe99726a3199f4436642b4b3e4118e5499db995a1257fb3f086eeb65982fac18985a286f301e77c451154ce9ac8895d9",
|
|
2645
|
-
"0x1630c3250d7313ff01d1201bf7a74ab5db3cb17dd952799b9ed3ab9097e68f90a0870d2dcae73d19cd13c1c66f652983",
|
|
2646
|
-
"0xd6ed6553fe44d296a3726c38ae652bfb11586264f0f8ce19008e218f9c86b2a8da25128c1052ecaddd7f225a139ed84",
|
|
2647
|
-
"0x17b81e7701abdbe2e8743884d1117e53356de5ab275b4db1a682c62ef0f2753339b7c8f8c8f475af9ccb5618e3f0c88e",
|
|
2648
|
-
"0x80d3cf1f9a78fc47b90b33563be990dc43b756ce79f5574a2c596c928c5d1de4fa295f296b74e956d71986a8497e317",
|
|
2649
|
-
"0x169b1f8e1bcfa7c42e0c37515d138f22dd2ecb803a0c5c99676314baf4bb1b7fa3190b2edc0327797f241067be390c9e",
|
|
2650
|
-
"0x10321da079ce07e272d8ec09d2565b0dfa7dccdde6787f96d50af36003b14866f69b771f8c285decca67df3f1605fb7b",
|
|
2651
|
-
"0x6e08c248e260e70bd1e962381edee3d31d79d7e22c837bc23c0bf1bc24c6b68c24b1b80b64d391fa9c8ba2e8ba2d229"
|
|
2652
|
-
],
|
|
2653
|
-
// xDen
|
|
2654
|
-
[
|
|
2655
|
-
"0x8ca8d548cff19ae18b2e62f4bd3fa6f01d5ef4ba35b48ba9c9588617fc8ac62b558d681be343df8993cf9fa40d21b1c",
|
|
2656
|
-
"0x12561a5deb559c4348b4711298e536367041e8ca0cf0800c0126c2588c48bf5713daa8846cb026e9e5c8276ec82b3bff",
|
|
2657
|
-
"0xb2962fe57a3225e8137e629bff2991f6f89416f5a718cd1fca64e00b11aceacd6a3d0967c94fedcfcc239ba5cb83e19",
|
|
2658
|
-
"0x3425581a58ae2fec83aafef7c40eb545b08243f16b1655154cca8abc28d6fd04976d5243eecf5c4130de8938dc62cd8",
|
|
2659
|
-
"0x13a8e162022914a80a6f1d5f43e7a07dffdfc759a12062bb8d6b44e833b306da9bd29ba81f35781d539d395b3532a21e",
|
|
2660
|
-
"0xe7355f8e4e667b955390f7f0506c6e9395735e9ce9cad4d0a43bcef24b8982f7400d24bc4228f11c02df9a29f6304a5",
|
|
2661
|
-
"0x772caacf16936190f3e0c63e0596721570f5799af53a1894e2e073062aede9cea73b3538f0de06cec2574496ee84a3a",
|
|
2662
|
-
"0x14a7ac2a9d64a8b230b3f5b074cf01996e7f63c21bca68a81996e1cdf9822c580fa5b9489d11e2d311f7d99bbdcc5a5e",
|
|
2663
|
-
"0xa10ecf6ada54f825e920b3dafc7a3cce07f8d1d7161366b74100da67f39883503826692abba43704776ec3a79a1d641",
|
|
2664
|
-
"0x95fc13ab9e92ad4476d6e3eb3a56680f682b4ee96f7d03776df533978f31c1593174e4b4b7865002d6384d168ecdd0a",
|
|
2665
|
-
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
|
|
2666
|
-
// LAST 1
|
|
2667
|
-
],
|
|
2668
|
-
// yNum
|
|
2669
|
-
[
|
|
2670
|
-
"0x90d97c81ba24ee0259d1f094980dcfa11ad138e48a869522b52af6c956543d3cd0c7aee9b3ba3c2be9845719707bb33",
|
|
2671
|
-
"0x134996a104ee5811d51036d776fb46831223e96c254f383d0f906343eb67ad34d6c56711962fa8bfe097e75a2e41c696",
|
|
2672
|
-
"0xcc786baa966e66f4a384c86a3b49942552e2d658a31ce2c344be4b91400da7d26d521628b00523b8dfe240c72de1f6",
|
|
2673
|
-
"0x1f86376e8981c217898751ad8746757d42aa7b90eeb791c09e4a3ec03251cf9de405aba9ec61deca6355c77b0e5f4cb",
|
|
2674
|
-
"0x8cc03fdefe0ff135caf4fe2a21529c4195536fbe3ce50b879833fd221351adc2ee7f8dc099040a841b6daecf2e8fedb",
|
|
2675
|
-
"0x16603fca40634b6a2211e11db8f0a6a074a7d0d4afadb7bd76505c3d3ad5544e203f6326c95a807299b23ab13633a5f0",
|
|
2676
|
-
"0x4ab0b9bcfac1bbcb2c977d027796b3ce75bb8ca2be184cb5231413c4d634f3747a87ac2460f415ec961f8855fe9d6f2",
|
|
2677
|
-
"0x987c8d5333ab86fde9926bd2ca6c674170a05bfe3bdd81ffd038da6c26c842642f64550fedfe935a15e4ca31870fb29",
|
|
2678
|
-
"0x9fc4018bd96684be88c9e221e4da1bb8f3abd16679dc26c1e8b6e6a1f20cabe69d65201c78607a360370e577bdba587",
|
|
2679
|
-
"0xe1bba7a1186bdb5223abde7ada14a23c42a0ca7915af6fe06985e7ed1e4d43b9b3f7055dd4eba6f2bafaaebca731c30",
|
|
2680
|
-
"0x19713e47937cd1be0dfd0b8f1d43fb93cd2fcbcb6caf493fd1183e416389e61031bf3a5cce3fbafce813711ad011c132",
|
|
2681
|
-
"0x18b46a908f36f6deb918c143fed2edcc523559b8aaf0c2462e6bfe7f911f643249d9cdf41b44d606ce07c8a4d0074d8e",
|
|
2682
|
-
"0xb182cac101b9399d155096004f53f447aa7b12a3426b08ec02710e807b4633f06c851c1919211f20d4c04f00b971ef8",
|
|
2683
|
-
"0x245a394ad1eca9b72fc00ae7be315dc757b3b080d4c158013e6632d3c40659cc6cf90ad1c232a6442d9d3f5db980133",
|
|
2684
|
-
"0x5c129645e44cf1102a159f748c4a3fc5e673d81d7e86568d9ab0f5d396a7ce46ba1049b6579afb7866b1e715475224b",
|
|
2685
|
-
"0x15e6be4e990f03ce4ea50b3b42df2eb5cb181d8f84965a3957add4fa95af01b2b665027efec01c7704b456be69c8b604"
|
|
2686
|
-
],
|
|
2687
|
-
// yDen
|
|
2688
|
-
[
|
|
2689
|
-
"0x16112c4c3a9c98b252181140fad0eae9601a6de578980be6eec3232b5be72e7a07f3688ef60c206d01479253b03663c1",
|
|
2690
|
-
"0x1962d75c2381201e1a0cbd6c43c348b885c84ff731c4d59ca4a10356f453e01f78a4260763529e3532f6102c2e49a03d",
|
|
2691
|
-
"0x58df3306640da276faaae7d6e8eb15778c4855551ae7f310c35a5dd279cd2eca6757cd636f96f891e2538b53dbf67f2",
|
|
2692
|
-
"0x16b7d288798e5395f20d23bf89edb4d1d115c5dbddbcd30e123da489e726af41727364f2c28297ada8d26d98445f5416",
|
|
2693
|
-
"0xbe0e079545f43e4b00cc912f8228ddcc6d19c9f0f69bbb0542eda0fc9dec916a20b15dc0fd2ededda39142311a5001d",
|
|
2694
|
-
"0x8d9e5297186db2d9fb266eaac783182b70152c65550d881c5ecd87b6f0f5a6449f38db9dfa9cce202c6477faaf9b7ac",
|
|
2695
|
-
"0x166007c08a99db2fc3ba8734ace9824b5eecfdfa8d0cf8ef5dd365bc400a0051d5fa9c01a58b1fb93d1a1399126a775c",
|
|
2696
|
-
"0x16a3ef08be3ea7ea03bcddfabba6ff6ee5a4375efa1f4fd7feb34fd206357132b920f5b00801dee460ee415a15812ed9",
|
|
2697
|
-
"0x1866c8ed336c61231a1be54fd1d74cc4f9fb0ce4c6af5920abc5750c4bf39b4852cfe2f7bb9248836b233d9d55535d4a",
|
|
2698
|
-
"0x167a55cda70a6e1cea820597d94a84903216f763e13d87bb5308592e7ea7d4fbc7385ea3d529b35e346ef48bb8913f55",
|
|
2699
|
-
"0x4d2f259eea405bd48f010a01ad2911d9c6dd039bb61a6290e591b36e636a5c871a5c29f4f83060400f8b49cba8f6aa8",
|
|
2700
|
-
"0xaccbb67481d033ff5852c1e48c50c477f94ff8aefce42d28c0f9a88cea7913516f968986f7ebbea9684b529e2561092",
|
|
2701
|
-
"0xad6b9514c767fe3c3613144b45f1496543346d98adf02267d5ceef9a00d9b8693000763e3b90ac11e99b138573345cc",
|
|
2702
|
-
"0x2660400eb2e4f3b628bdd0d53cd76f2bf565b94e72927c1cb748df27942480e420517bd8714cc80d1fadc1326ed06f7",
|
|
2703
|
-
"0xe0fa1d816ddc03e6b24255e0d7819c171c40f65e273b853324efcd6356caa205ca2f570f13497804415473a1d634b8f",
|
|
2704
|
-
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
|
|
2705
|
-
// LAST 1
|
|
2706
|
-
]
|
|
2707
|
-
].map((e) => e.map((t) => BigInt(t)))), bs = Vn(x, {
|
|
2708
|
-
A: x.create(BigInt("0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d")),
|
|
2709
|
-
B: x.create(BigInt("0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0")),
|
|
2710
|
-
Z: x.create(BigInt(11))
|
|
2711
|
-
}), hs = Vn(L, {
|
|
2712
|
-
A: L.create({ c0: x.create(z), c1: x.create(BigInt(240)) }),
|
|
2713
|
-
// A' = 240 * I
|
|
2714
|
-
B: L.create({ c0: x.create(BigInt(1012)), c1: x.create(BigInt(1012)) }),
|
|
2715
|
-
// B' = 1012 * (1 + I)
|
|
2716
|
-
Z: L.create({ c0: x.create(BigInt(-2)), c1: x.create(BigInt(-1)) })
|
|
2717
|
-
// Z: -(2 + I)
|
|
2718
|
-
});
|
|
2719
|
-
function ms(e) {
|
|
2720
|
-
const { x: t, y: r } = bs(x.create(e[0]));
|
|
2721
|
-
return ls(t, r);
|
|
2722
|
-
}
|
|
2723
|
-
function gs(e) {
|
|
2724
|
-
const { x: t, y: r } = hs(L.fromBigTuple(e));
|
|
2725
|
-
return ds(t, r);
|
|
2726
|
-
}
|
|
2727
|
-
const ys = BigInt(0), Dt = BigInt(1), ps = BigInt(2), ws = BigInt(7), Es = BigInt(256), xs = BigInt(113), Fn = [], Cn = [], zn = [];
|
|
2728
|
-
for (let e = 0, t = Dt, r = 1, n = 0; e < 24; e++) {
|
|
2729
|
-
[r, n] = [n, (2 * r + 3 * n) % 5], Fn.push(2 * (5 * n + r)), Cn.push((e + 1) * (e + 2) / 2 % 64);
|
|
2730
|
-
let s = ys;
|
|
2731
|
-
for (let o = 0; o < 7; o++)
|
|
2732
|
-
t = (t << Dt ^ (t >> ws) * xs) % Es, t & ps && (s ^= Dt << (Dt << /* @__PURE__ */ BigInt(o)) - Dt);
|
|
2733
|
-
zn.push(s);
|
|
2734
|
-
}
|
|
2735
|
-
const $n = dr(zn, !0), Bs = $n[0], _s = $n[1], dn = (e, t, r) => r > 32 ? hr(e, t, r) : lr(e, t, r), ln = (e, t, r) => r > 32 ? mr(e, t, r) : br(e, t, r);
|
|
2736
|
-
function Ss(e, t = 24) {
|
|
2737
|
-
const r = new Uint32Array(10);
|
|
2738
|
-
for (let n = 24 - t; n < 24; n++) {
|
|
2739
|
-
for (let i = 0; i < 10; i++)
|
|
2740
|
-
r[i] = e[i] ^ e[i + 10] ^ e[i + 20] ^ e[i + 30] ^ e[i + 40];
|
|
2741
|
-
for (let i = 0; i < 10; i += 2) {
|
|
2742
|
-
const c = (i + 8) % 10, a = (i + 2) % 10, u = r[a], l = r[a + 1], d = dn(u, l, 1) ^ r[c], f = ln(u, l, 1) ^ r[c + 1];
|
|
2743
|
-
for (let b = 0; b < 50; b += 10)
|
|
2744
|
-
e[i + b] ^= d, e[i + b + 1] ^= f;
|
|
2745
|
-
}
|
|
2746
|
-
let s = e[2], o = e[3];
|
|
2747
|
-
for (let i = 0; i < 24; i++) {
|
|
2748
|
-
const c = Cn[i], a = dn(s, o, c), u = ln(s, o, c), l = Fn[i];
|
|
2749
|
-
s = e[l], o = e[l + 1], e[l] = a, e[l + 1] = u;
|
|
2750
|
-
}
|
|
2751
|
-
for (let i = 0; i < 50; i += 10) {
|
|
2752
|
-
for (let c = 0; c < 10; c++)
|
|
2753
|
-
r[c] = e[i + c];
|
|
2754
|
-
for (let c = 0; c < 10; c++)
|
|
2755
|
-
e[i + c] ^= ~r[(c + 2) % 10] & r[(c + 4) % 10];
|
|
2756
|
-
}
|
|
2757
|
-
e[0] ^= Bs[n], e[1] ^= _s[n];
|
|
2758
|
-
}
|
|
2759
|
-
St(r);
|
|
2760
|
-
}
|
|
2761
|
-
class Ke extends Le {
|
|
2762
|
-
// NOTE: we accept arguments in bytes instead of bits here.
|
|
2763
|
-
constructor(t, r, n, s = !1, o = 24) {
|
|
2764
|
-
if (super(), this.pos = 0, this.posOut = 0, this.finished = !1, this.destroyed = !1, this.enableXOF = !1, this.blockLen = t, this.suffix = r, this.outputLen = n, this.enableXOF = s, this.rounds = o, Ut(n), !(0 < t && t < 200))
|
|
2765
|
-
throw new Error("only keccak-f1600 function is supported");
|
|
2766
|
-
this.state = new Uint8Array(200), this.state32 = Qn(this.state);
|
|
2767
|
-
}
|
|
2768
|
-
clone() {
|
|
2769
|
-
return this._cloneInto();
|
|
2770
|
-
}
|
|
2771
|
-
keccak() {
|
|
2772
|
-
Ve(this.state32), Ss(this.state32, this.rounds), Ve(this.state32), this.posOut = 0, this.pos = 0;
|
|
2773
|
-
}
|
|
2774
|
-
update(t) {
|
|
2775
|
-
Lt(this), t = vt(t), Q(t);
|
|
2776
|
-
const { blockLen: r, state: n } = this, s = t.length;
|
|
2777
|
-
for (let o = 0; o < s; ) {
|
|
2778
|
-
const i = Math.min(r - this.pos, s - o);
|
|
2779
|
-
for (let c = 0; c < i; c++)
|
|
2780
|
-
n[this.pos++] ^= t[o++];
|
|
2781
|
-
this.pos === r && this.keccak();
|
|
2782
|
-
}
|
|
2783
|
-
return this;
|
|
2784
|
-
}
|
|
2785
|
-
finish() {
|
|
2786
|
-
if (this.finished)
|
|
2787
|
-
return;
|
|
2788
|
-
this.finished = !0;
|
|
2789
|
-
const { state: t, suffix: r, pos: n, blockLen: s } = this;
|
|
2790
|
-
t[n] ^= r, (r & 128) !== 0 && n === s - 1 && this.keccak(), t[s - 1] ^= 128, this.keccak();
|
|
2791
|
-
}
|
|
2792
|
-
writeInto(t) {
|
|
2793
|
-
Lt(this, !1), Q(t), this.finish();
|
|
2794
|
-
const r = this.state, { blockLen: n } = this;
|
|
2795
|
-
for (let s = 0, o = t.length; s < o; ) {
|
|
2796
|
-
this.posOut >= n && this.keccak();
|
|
2797
|
-
const i = Math.min(n - this.posOut, o - s);
|
|
2798
|
-
t.set(r.subarray(this.posOut, this.posOut + i), s), this.posOut += i, s += i;
|
|
2799
|
-
}
|
|
2800
|
-
return t;
|
|
2801
|
-
}
|
|
2802
|
-
xofInto(t) {
|
|
2803
|
-
if (!this.enableXOF)
|
|
2804
|
-
throw new Error("XOF is not possible for this instance");
|
|
2805
|
-
return this.writeInto(t);
|
|
2806
|
-
}
|
|
2807
|
-
xof(t) {
|
|
2808
|
-
return Ut(t), this.xofInto(new Uint8Array(t));
|
|
2809
|
-
}
|
|
2810
|
-
digestInto(t) {
|
|
2811
|
-
if (xn(t, this), this.finished)
|
|
2812
|
-
throw new Error("digest() was already called");
|
|
2813
|
-
return this.writeInto(t), this.destroy(), t;
|
|
2814
|
-
}
|
|
2815
|
-
digest() {
|
|
2816
|
-
return this.digestInto(new Uint8Array(this.outputLen));
|
|
2817
|
-
}
|
|
2818
|
-
destroy() {
|
|
2819
|
-
this.destroyed = !0, St(this.state);
|
|
2820
|
-
}
|
|
2821
|
-
_cloneInto(t) {
|
|
2822
|
-
const { blockLen: r, suffix: n, outputLen: s, rounds: o, enableXOF: i } = this;
|
|
2823
|
-
return t || (t = new Ke(r, n, s, i, o)), t.state32.set(this.state32), t.pos = this.pos, t.posOut = this.posOut, t.finished = this.finished, t.rounds = o, t.suffix = n, t.outputLen = s, t.enableXOF = i, t.destroyed = this.destroyed, t;
|
|
2824
|
-
}
|
|
2825
|
-
}
|
|
2826
|
-
const vs = (e, t, r) => sr((n = {}) => new Ke(t, e, n.dkLen === void 0 ? r : n.dkLen, !0)), As = vs(31, 136, 256 / 8);
|
|
2827
|
-
function Os(e, t, r) {
|
|
2828
|
-
return Ue(e), r === void 0 && (r = new Uint8Array(e.outputLen)), De(e, vt(r), vt(t));
|
|
2829
|
-
}
|
|
2830
|
-
const _e = /* @__PURE__ */ Uint8Array.from([0]), bn = /* @__PURE__ */ Uint8Array.of();
|
|
2831
|
-
function Is(e, t, r, n = 32) {
|
|
2832
|
-
Ue(e), Ut(n);
|
|
2833
|
-
const s = e.outputLen;
|
|
2834
|
-
if (n > 255 * s)
|
|
2835
|
-
throw new Error("Length should be <= 255*HashLen");
|
|
2836
|
-
const o = Math.ceil(n / s);
|
|
2837
|
-
r === void 0 && (r = bn);
|
|
2838
|
-
const i = new Uint8Array(o * s), c = De.create(e, t), a = c._cloneInto(), u = new Uint8Array(c.outputLen);
|
|
2839
|
-
for (let l = 0; l < o; l++)
|
|
2840
|
-
_e[0] = l + 1, a.update(l === 0 ? bn : u).update(r).update(_e).digestInto(u), i.set(u, s * l), c._cloneInto(a);
|
|
2841
|
-
return c.destroy(), a.destroy(), St(u, _e), i.slice(0, n);
|
|
2842
|
-
}
|
|
2843
|
-
const Ns = (e, t, r, n, s) => Is(e, Os(e, t, r), n, s), it = 48, nt = 96;
|
|
2844
|
-
class Ie {
|
|
2845
|
-
#t;
|
|
2846
|
-
#e;
|
|
2847
|
-
/**
|
|
2848
|
-
* Create a random transport secret key
|
|
2849
|
-
*/
|
|
2850
|
-
static random() {
|
|
2851
|
-
return new Ie(P.utils.randomSecretKey());
|
|
2852
|
-
}
|
|
2853
|
-
/**
|
|
2854
|
-
* Deserialize TransportSecretKey from a bytestring
|
|
2855
|
-
*
|
|
2856
|
-
* The passed value would typically be a string previously returned
|
|
2857
|
-
* by calling serialize on a randomly-created TransportSecretKey.
|
|
2858
|
-
*/
|
|
2859
|
-
static deserialize(t) {
|
|
2860
|
-
if (t.length !== 32)
|
|
2861
|
-
throw new Error("Invalid size for transport secret key");
|
|
2862
|
-
return new Ie(t);
|
|
2863
|
-
}
|
|
2864
|
-
/**
|
|
2865
|
-
* Return the encoding of the transport public key; this value is
|
|
2866
|
-
* sent to the IC
|
|
2867
|
-
*/
|
|
2868
|
-
publicKeyBytes() {
|
|
2869
|
-
return this.#e.toBytes(!0);
|
|
2870
|
-
}
|
|
2871
|
-
/**
|
|
2872
|
-
* Return the transport secret key value
|
|
2873
|
-
*
|
|
2874
|
-
* Applications would not normally need to call this
|
|
2875
|
-
*/
|
|
2876
|
-
serialize() {
|
|
2877
|
-
return this.#t;
|
|
2878
|
-
}
|
|
2879
|
-
/**
|
|
2880
|
-
* @internal constructor
|
|
2881
|
-
*/
|
|
2882
|
-
constructor(t) {
|
|
2883
|
-
this.#t = t;
|
|
2884
|
-
const r = P.G1.Point.BASE.multiply(
|
|
2885
|
-
P.G1.Point.Fn.fromBytes(this.#t)
|
|
2886
|
-
);
|
|
2887
|
-
this.#e = r;
|
|
2888
|
-
}
|
|
2889
|
-
}
|
|
2890
|
-
function Us(e) {
|
|
2891
|
-
if (e.length != 48)
|
|
2892
|
-
return !1;
|
|
2893
|
-
try {
|
|
2894
|
-
return P.G1.Point.fromHex(e), !0;
|
|
2895
|
-
} catch {
|
|
2896
|
-
return !1;
|
|
2897
|
-
}
|
|
2898
|
-
}
|
|
2899
|
-
function _t(e) {
|
|
2900
|
-
let t = e.length;
|
|
2901
|
-
const r = new Uint8Array(8 + t);
|
|
2902
|
-
for (let n = 7; n >= 0; n--)
|
|
2903
|
-
r[n] = t & 255, t >>>= 8;
|
|
2904
|
-
return r.set(e, 8), r;
|
|
2905
|
-
}
|
|
2906
|
-
var Ts = /* @__PURE__ */ ((e) => (e.KEY_1 = "key_1", e.TEST_KEY_1 = "test_key_1", e))(Ts || {}), qs = /* @__PURE__ */ ((e) => (e.KEY_1 = "key_1", e.TEST_KEY_1 = "test_key_1", e.DFX_TEST_KEY = "dfx_test_key", e))(qs || {});
|
|
2907
|
-
function Mt(e) {
|
|
2908
|
-
const t = new Uint8Array(e.length / 2);
|
|
2909
|
-
for (let r = 0; r < e.length; r += 2)
|
|
2910
|
-
t[r / 2] = parseInt(e.substring(r, r + 2), 16);
|
|
2911
|
-
return t;
|
|
2912
|
-
}
|
|
2913
|
-
class wt {
|
|
2914
|
-
#t;
|
|
2915
|
-
/**
|
|
2916
|
-
* Read a MasterPublicKey from the bytestring encoding
|
|
2917
|
-
*
|
|
2918
|
-
* Normally the bytes provided here will have been returned by
|
|
2919
|
-
* the `vetkd_public_key` management canister interface.
|
|
2920
|
-
*/
|
|
2921
|
-
static deserialize(t) {
|
|
2922
|
-
return new wt(P.G2.Point.fromHex(t));
|
|
2923
|
-
}
|
|
2924
|
-
/**
|
|
2925
|
-
* Derive a canister master key from the subnet master key
|
|
2926
|
-
*
|
|
2927
|
-
* To create the derived public key in VetKD, a two step derivation is performed. The first step
|
|
2928
|
-
* creates a key that is specific to the canister that is making VetKD requests to the
|
|
2929
|
-
* management canister, sometimes called canister master key.
|
|
2930
|
-
*
|
|
2931
|
-
* This function can be used to compute canister master keys knowing just the subnet master key
|
|
2932
|
-
* plus the canister identity. This avoids having to interact with the IC for performing this
|
|
2933
|
-
* computation.
|
|
2934
|
-
*/
|
|
2935
|
-
deriveCanisterKey(t) {
|
|
2936
|
-
const r = "ic-vetkd-bls12-381-g2-canister-id", n = this.publicKeyBytes(), s = new Uint8Array([
|
|
2937
|
-
..._t(n),
|
|
2938
|
-
..._t(t)
|
|
2939
|
-
]), o = Ye(s, r), i = P.G2.Point.BASE.multiply(o);
|
|
2940
|
-
return new zt(this.#t.add(i));
|
|
2941
|
-
}
|
|
2942
|
-
/**
|
|
2943
|
-
* Return the bytestring encoding of the master public key
|
|
2944
|
-
*/
|
|
2945
|
-
publicKeyBytes() {
|
|
2946
|
-
return this.#t.toBytes(!0);
|
|
2947
|
-
}
|
|
2948
|
-
/**
|
|
2949
|
-
* Return the hardcoded master public key used on IC
|
|
2950
|
-
*
|
|
2951
|
-
* This allows performing public key derivation offline
|
|
2952
|
-
*/
|
|
2953
|
-
static productionKey(t = "key_1") {
|
|
2954
|
-
if (t == "key_1")
|
|
2955
|
-
return wt.deserialize(
|
|
2956
|
-
Mt(
|
|
2957
|
-
"a9caf9ae8af0c7c7272f8a122133e2e0c7c0899b75e502bda9e109ca8193ded3ef042ed96db1125e1bdaad77d8cc60d917e122fe2501c45b96274f43705edf0cfd455bc66c3c060faa2fcd15486e76351edf91fecb993797273bbc8beaa47404"
|
|
2958
|
-
)
|
|
2959
|
-
);
|
|
2960
|
-
if (t == "test_key_1")
|
|
2961
|
-
return wt.deserialize(
|
|
2962
|
-
Mt(
|
|
2963
|
-
"ad86e8ff845912f022a0838a502d763fdea547c9948f8cb20ea7738dd52c1c38dcb4c6ca9ac29f9ac690fc5ad7681cb41922b8dffbd65d94bff141f5fb5b6624eccc03bf850f222052df888cf9b1e47203556d7522271cbb879b2ef4b8c2bfb1"
|
|
2964
|
-
)
|
|
2965
|
-
);
|
|
2966
|
-
throw new Error(
|
|
2967
|
-
"Unknown MasterPublicKeyId value for productionKey"
|
|
2968
|
-
);
|
|
2969
|
-
}
|
|
2970
|
-
/**
|
|
2971
|
-
* Return the hardcoded master public key used in PocketIC
|
|
2972
|
-
*
|
|
2973
|
-
* This allows performing public key derivation offline
|
|
2974
|
-
*/
|
|
2975
|
-
static pocketicKey(t = "key_1") {
|
|
2976
|
-
if (t == "key_1")
|
|
2977
|
-
return wt.deserialize(
|
|
2978
|
-
Mt(
|
|
2979
|
-
"8c800b5cff00463d26e8167369168827f1e48f4d8d60f71dd6a295580f65275b5f5f8e6a792c876b2c72492136530d0710a27522ee63977a76216c3cef9e70bfcb45b88736fc62142e7e0737848ce06cbb1f45a4a6a349b142ae5cf7853561e0"
|
|
2980
|
-
)
|
|
2981
|
-
);
|
|
2982
|
-
if (t == "test_key_1")
|
|
2983
|
-
return wt.deserialize(
|
|
2984
|
-
Mt(
|
|
2985
|
-
"9069b82c7aae418cef27678291e7f2cb1a008a500eceba7199bffca12421b07c158987c6a22618af3d1958738b2835691028801f7663d311799733286c557c8979184bb62cb559a4d582fca7d2e48b860f08ed6641aef66a059ec891889a6218"
|
|
2986
|
-
)
|
|
2987
|
-
);
|
|
2988
|
-
if (t == "dfx_test_key")
|
|
2989
|
-
return wt.deserialize(
|
|
2990
|
-
Mt(
|
|
2991
|
-
"b181c14cf9d04ba45d782c0067a44b0aaa9fc2acf94f1a875f0dae801af4f80339a7e6bf8b09fcf993824c8df3080b3f1409b688ca08cbd44d2cb28db9899f4aa3b5f06b9174240448e10be2f01f9f80079ea5431ce2d11d1c8d1c775333315f"
|
|
2992
|
-
)
|
|
2993
|
-
);
|
|
2994
|
-
throw new Error(
|
|
2995
|
-
"Unknown PocketIcMasterPublicKeyId value for pocketicKey"
|
|
2996
|
-
);
|
|
2997
|
-
}
|
|
2998
|
-
/**
|
|
2999
|
-
* @internal constructor
|
|
3000
|
-
*/
|
|
3001
|
-
constructor(t) {
|
|
3002
|
-
this.#t = t;
|
|
3003
|
-
}
|
|
3004
|
-
}
|
|
3005
|
-
class zt {
|
|
3006
|
-
#t;
|
|
3007
|
-
/**
|
|
3008
|
-
* Read a DerivedPublicKey from the bytestring encoding
|
|
3009
|
-
*
|
|
3010
|
-
* Normally the bytes provided here will have been returned by
|
|
3011
|
-
* the `vetkd_public_key` management canister interface.
|
|
3012
|
-
*/
|
|
3013
|
-
static deserialize(t) {
|
|
3014
|
-
return new zt(P.G2.Point.fromHex(t));
|
|
3015
|
-
}
|
|
3016
|
-
/**
|
|
3017
|
-
* Perform second-stage derivation of a public key
|
|
3018
|
-
*
|
|
3019
|
-
* To create the derived public key in VetKD, a two step derivation is performed. The first step
|
|
3020
|
-
* creates a key that is specific to the canister that is making VetKD requests to the
|
|
3021
|
-
* management canister, sometimes called canister master key. The second step incorporates the
|
|
3022
|
-
* "derivation context" value provided to the `vetkd_public_key` management canister interface.
|
|
3023
|
-
*
|
|
3024
|
-
* If `vetkd_public_key` is invoked with an empty derivation context, it simply returns the
|
|
3025
|
-
* canister master key. Then the second derivation step can be done offline, using this
|
|
3026
|
-
* function. This is useful if you wish to derive multiple keys without having to interact with
|
|
3027
|
-
* the IC each time.
|
|
3028
|
-
*
|
|
3029
|
-
* If `context` is empty, then this simply returns the underlying key. This matches the behavior
|
|
3030
|
-
* of `vetkd_public_key`
|
|
3031
|
-
*/
|
|
3032
|
-
deriveSubKey(t) {
|
|
3033
|
-
if (t.length === 0)
|
|
3034
|
-
return this;
|
|
3035
|
-
{
|
|
3036
|
-
const r = "ic-vetkd-bls12-381-g2-context", n = this.publicKeyBytes(), s = new Uint8Array([
|
|
3037
|
-
..._t(n),
|
|
3038
|
-
..._t(t)
|
|
3039
|
-
]), o = Ye(s, r), i = P.G2.Point.BASE.multiply(o);
|
|
3040
|
-
return new zt(this.getPoint().add(i));
|
|
3041
|
-
}
|
|
3042
|
-
}
|
|
3043
|
-
/**
|
|
3044
|
-
* Return the bytestring encoding of the derived public key
|
|
3045
|
-
*
|
|
3046
|
-
* Applications would not normally need to call this, unless they
|
|
3047
|
-
* are using VetKD for creating a random beacon, in which case
|
|
3048
|
-
* these bytes are used by anyone verifying the beacon.
|
|
3049
|
-
*/
|
|
3050
|
-
publicKeyBytes() {
|
|
3051
|
-
return this.#t.toBytes(!0);
|
|
3052
|
-
}
|
|
3053
|
-
/**
|
|
3054
|
-
* @internal getter returning the point element of the derived public key
|
|
3055
|
-
*
|
|
3056
|
-
* Applications would not normally need to call this
|
|
3057
|
-
*/
|
|
3058
|
-
getPoint() {
|
|
3059
|
-
return this.#t;
|
|
3060
|
-
}
|
|
3061
|
-
/**
|
|
3062
|
-
* @internal constructor
|
|
3063
|
-
*
|
|
3064
|
-
* This is public for typing reasons but there should be no need
|
|
3065
|
-
* for an application to call this.
|
|
3066
|
-
*/
|
|
3067
|
-
constructor(t) {
|
|
3068
|
-
this.#t = t;
|
|
3069
|
-
}
|
|
3070
|
-
}
|
|
3071
|
-
function Ye(e, t) {
|
|
3072
|
-
return P.G2.hashToScalar(e, { DST: t });
|
|
3073
|
-
}
|
|
3074
|
-
function Ne(e) {
|
|
3075
|
-
if (typeof e == "string")
|
|
3076
|
-
return new TextEncoder().encode(e);
|
|
3077
|
-
const t = new Uint8Array(e.length);
|
|
3078
|
-
return t.set(e), t;
|
|
3079
|
-
}
|
|
3080
|
-
function Se(e, t) {
|
|
3081
|
-
const r = new TextEncoder().encode(e), n = Ne(t), s = new Uint8Array(r.length + n.length);
|
|
3082
|
-
return s.set(r, 0), s.set(n, r.length), s;
|
|
3083
|
-
}
|
|
3084
|
-
function Xt(e, t, r) {
|
|
3085
|
-
const n = new Uint8Array();
|
|
3086
|
-
return Ns(Sn, e, n, t, r);
|
|
3087
|
-
}
|
|
3088
|
-
function Rs(e, t) {
|
|
3089
|
-
const r = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_AUG_", n = e.publicKeyBytes(), s = new Uint8Array([...n, ...t]);
|
|
3090
|
-
return P.G1.Point.fromAffine(
|
|
3091
|
-
P.G1.hashToCurve(s, {
|
|
3092
|
-
DST: r
|
|
3093
|
-
}).toAffine()
|
|
3094
|
-
);
|
|
3095
|
-
}
|
|
3096
|
-
function jn(e, t, r) {
|
|
3097
|
-
const n = e.publicKeyBytes(), s = new Uint8Array([...n, ...t]), i = P.shortSignatures.hash(
|
|
3098
|
-
s,
|
|
3099
|
-
"BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_AUG_"
|
|
3100
|
-
);
|
|
3101
|
-
return P.shortSignatures.verify(
|
|
3102
|
-
r,
|
|
3103
|
-
i,
|
|
3104
|
-
e.getPoint()
|
|
3105
|
-
);
|
|
3106
|
-
}
|
|
3107
|
-
class le {
|
|
3108
|
-
#t;
|
|
3109
|
-
#e;
|
|
3110
|
-
/**
|
|
3111
|
-
* Return the VetKey bytes, aka the BLS signature
|
|
3112
|
-
*
|
|
3113
|
-
* Use the raw bytes only if your design makes use of the fact that VetKeys
|
|
3114
|
-
* are BLS signatures (eg for random beacon or threshold BLS signature
|
|
3115
|
-
* generation). If you are using VetKD for key distribution, instead use
|
|
3116
|
-
* deriveSymmetricKey or asHkdfCryptoKey
|
|
3117
|
-
*/
|
|
3118
|
-
signatureBytes() {
|
|
3119
|
-
return this.#e;
|
|
3120
|
-
}
|
|
3121
|
-
/**
|
|
3122
|
-
* Return the serialization of the VetKey
|
|
3123
|
-
*
|
|
3124
|
-
* This is the byte encoding of the unencrypted VetKey.
|
|
3125
|
-
*/
|
|
3126
|
-
serialize() {
|
|
3127
|
-
return this.#e;
|
|
3128
|
-
}
|
|
3129
|
-
/**
|
|
3130
|
-
* Derive a symmetric key of the requested length from the VetKey
|
|
3131
|
-
*
|
|
3132
|
-
* As an alternative to this function consider using asDerivedKeyMaterial,
|
|
3133
|
-
* which uses the WebCrypto API and prevents export of the underlying key.
|
|
3134
|
-
*
|
|
3135
|
-
* The `domainSep` parameter should be a string unique to your application and
|
|
3136
|
-
* also your usage of the resulting key. For example say your application
|
|
3137
|
-
* "my-app" is deriving two keys, one for usage "foo" and the other for
|
|
3138
|
-
* "bar". You might use as domain separators "my-app-foo" and "my-app-bar".
|
|
3139
|
-
*
|
|
3140
|
-
* The returned Uint8Array will be `outputLength` bytes long.
|
|
3141
|
-
*/
|
|
3142
|
-
deriveSymmetricKey(t, r) {
|
|
3143
|
-
return Xt(this.#e, t, r);
|
|
3144
|
-
}
|
|
3145
|
-
/**
|
|
3146
|
-
* Return a DerivedKeyMaterial type which is suitable for further key derivation
|
|
3147
|
-
*/
|
|
3148
|
-
async asDerivedKeyMaterial() {
|
|
3149
|
-
return ie.setup(this.#e);
|
|
3150
|
-
}
|
|
3151
|
-
/**
|
|
3152
|
-
* Deserialize a VetKey from the 48 byte encoding of the BLS signature
|
|
3153
|
-
*
|
|
3154
|
-
* This deserializes the same value as returned by serialize (or signatureBytes)
|
|
3155
|
-
*/
|
|
3156
|
-
static deserialize(t) {
|
|
3157
|
-
return new le(P.G1.Point.fromHex(t));
|
|
3158
|
-
}
|
|
3159
|
-
/**
|
|
3160
|
-
* @internal getter returning the point object of the VetKey
|
|
3161
|
-
*
|
|
3162
|
-
* Applications would not usually need to call this
|
|
3163
|
-
*/
|
|
3164
|
-
getPoint() {
|
|
3165
|
-
return this.#t;
|
|
3166
|
-
}
|
|
3167
|
-
/**
|
|
3168
|
-
* @internal constructor
|
|
3169
|
-
*
|
|
3170
|
-
* This is public for typing reasons but there is no reason for an application
|
|
3171
|
-
* to call this constructor.
|
|
3172
|
-
*/
|
|
3173
|
-
constructor(t) {
|
|
3174
|
-
this.#t = t, this.#e = new Uint8Array(t.toBytes(!0));
|
|
3175
|
-
}
|
|
3176
|
-
}
|
|
3177
|
-
const qt = 12, hn = 2, Te = "IC GCMv2", mn = new TextEncoder().encode(
|
|
3178
|
-
Te
|
|
3179
|
-
), Zt = 8;
|
|
3180
|
-
class ie {
|
|
3181
|
-
#t;
|
|
3182
|
-
#e;
|
|
3183
|
-
/**
|
|
3184
|
-
* @internal constructor
|
|
3185
|
-
*/
|
|
3186
|
-
constructor(t, r) {
|
|
3187
|
-
this.#t = t, this.#e = r;
|
|
3188
|
-
}
|
|
3189
|
-
static async fromCryptoKey(t) {
|
|
3190
|
-
const r = {
|
|
3191
|
-
name: "HKDF",
|
|
3192
|
-
hash: "SHA-256",
|
|
3193
|
-
info: new TextEncoder().encode(
|
|
3194
|
-
"ic-vetkd-bls12-381-g2-derived-key-material"
|
|
3195
|
-
),
|
|
3196
|
-
salt: new Uint8Array()
|
|
3197
|
-
}, n = {
|
|
3198
|
-
name: "AES-GCM",
|
|
3199
|
-
length: 256
|
|
3200
|
-
}, s = await crypto.subtle.deriveKey(
|
|
3201
|
-
r,
|
|
3202
|
-
t,
|
|
3203
|
-
n,
|
|
3204
|
-
!0,
|
|
3205
|
-
// exportable
|
|
3206
|
-
["encrypt"]
|
|
3207
|
-
), o = await crypto.subtle.exportKey(
|
|
3208
|
-
"raw",
|
|
3209
|
-
s
|
|
3210
|
-
), i = await crypto.subtle.importKey(
|
|
3211
|
-
"raw",
|
|
3212
|
-
o,
|
|
3213
|
-
{ name: "HKDF" },
|
|
3214
|
-
!1,
|
|
3215
|
-
["deriveKey", "deriveBits"]
|
|
3216
|
-
);
|
|
3217
|
-
return new ie(i, t);
|
|
3218
|
-
}
|
|
3219
|
-
/**
|
|
3220
|
-
* @internal constructor
|
|
3221
|
-
*/
|
|
3222
|
-
static async setup(t) {
|
|
3223
|
-
const n = await globalThis.crypto.subtle.importKey(
|
|
3224
|
-
"raw",
|
|
3225
|
-
t,
|
|
3226
|
-
"HKDF",
|
|
3227
|
-
!1,
|
|
3228
|
-
["deriveKey", "deriveBits"]
|
|
3229
|
-
);
|
|
3230
|
-
return ie.fromCryptoKey(n);
|
|
3231
|
-
}
|
|
3232
|
-
/**
|
|
3233
|
-
* Return the CryptoKey
|
|
3234
|
-
*/
|
|
3235
|
-
getCryptoKey() {
|
|
3236
|
-
return this.#e;
|
|
3237
|
-
}
|
|
3238
|
-
/**
|
|
3239
|
-
* Return a WebCrypto CryptoKey handle suitable for AES-GCM encryption/decryption
|
|
3240
|
-
*
|
|
3241
|
-
* The key is derived using HKDF with the provided domain separator
|
|
3242
|
-
*
|
|
3243
|
-
* The CryptoKey is not exportable
|
|
3244
|
-
*/
|
|
3245
|
-
async deriveAesGcmCryptoKey(t, r) {
|
|
3246
|
-
const n = {
|
|
3247
|
-
name: "HKDF",
|
|
3248
|
-
hash: "SHA-256",
|
|
3249
|
-
length: 256,
|
|
3250
|
-
info: Se(
|
|
3251
|
-
"ic-vetkd-bls12-381-g2-aes-gcm-v" + r.toString() + "-",
|
|
3252
|
-
t
|
|
3253
|
-
),
|
|
3254
|
-
salt: new Uint8Array()
|
|
3255
|
-
}, s = {
|
|
3256
|
-
name: "AES-GCM",
|
|
3257
|
-
length: 256
|
|
3258
|
-
};
|
|
3259
|
-
return globalThis.crypto.subtle.deriveKey(
|
|
3260
|
-
n,
|
|
3261
|
-
this.#t,
|
|
3262
|
-
s,
|
|
3263
|
-
!1,
|
|
3264
|
-
["encrypt", "decrypt"]
|
|
3265
|
-
);
|
|
3266
|
-
}
|
|
3267
|
-
/**
|
|
3268
|
-
* Encrypt the provided message using AES-GCM and a key derived using HKDF
|
|
3269
|
-
*
|
|
3270
|
-
* The GCM key is derived using HKDF with the provided domain separator
|
|
3271
|
-
*/
|
|
3272
|
-
async encryptMessage(t, r, n) {
|
|
3273
|
-
const s = await this.deriveAesGcmCryptoKey(
|
|
3274
|
-
r,
|
|
3275
|
-
hn
|
|
3276
|
-
), o = globalThis.crypto.getRandomValues(
|
|
3277
|
-
new Uint8Array(qt)
|
|
3278
|
-
), i = Se(Te, n), c = new Uint8Array(
|
|
3279
|
-
await globalThis.crypto.subtle.encrypt(
|
|
3280
|
-
{ name: "AES-GCM", iv: o, additionalData: i },
|
|
3281
|
-
s,
|
|
3282
|
-
Ne(t)
|
|
3283
|
-
)
|
|
3284
|
-
);
|
|
3285
|
-
return new Uint8Array([
|
|
3286
|
-
...mn,
|
|
3287
|
-
...o,
|
|
3288
|
-
...c
|
|
3289
|
-
]);
|
|
3290
|
-
}
|
|
3291
|
-
/**
|
|
3292
|
-
* Decrypt the provided ciphertext using AES-GCM and a key derived using HKDF
|
|
3293
|
-
*
|
|
3294
|
-
* The GCM key is derived using HKDF with the provided domain separator
|
|
3295
|
-
*/
|
|
3296
|
-
async decryptMessage(t, r, n) {
|
|
3297
|
-
const o = Zt + qt + 16;
|
|
3298
|
-
if (t.length < o)
|
|
3299
|
-
throw new Error(
|
|
3300
|
-
"Invalid ciphertext, too short to possibly be valid"
|
|
3301
|
-
);
|
|
3302
|
-
const i = t.slice(0, Zt);
|
|
3303
|
-
if (!qe(i, mn))
|
|
3304
|
-
if (n.length == 0) {
|
|
3305
|
-
const d = t.slice(
|
|
3306
|
-
0,
|
|
3307
|
-
qt
|
|
3308
|
-
), f = t.slice(
|
|
3309
|
-
qt
|
|
3310
|
-
), b = {
|
|
3311
|
-
name: "HKDF",
|
|
3312
|
-
hash: "SHA-256",
|
|
3313
|
-
length: 256,
|
|
3314
|
-
info: Ne(r),
|
|
3315
|
-
salt: new Uint8Array()
|
|
3316
|
-
}, m = {
|
|
3317
|
-
name: "AES-GCM",
|
|
3318
|
-
length: 256
|
|
3319
|
-
}, g = await globalThis.crypto.subtle.deriveKey(
|
|
3320
|
-
b,
|
|
3321
|
-
this.#e,
|
|
3322
|
-
m,
|
|
3323
|
-
!1,
|
|
3324
|
-
["decrypt"]
|
|
3325
|
-
);
|
|
3326
|
-
try {
|
|
3327
|
-
const w = await globalThis.crypto.subtle.decrypt(
|
|
3328
|
-
{ name: "AES-GCM", iv: d },
|
|
3329
|
-
g,
|
|
3330
|
-
f
|
|
3331
|
-
);
|
|
3332
|
-
return new Uint8Array(w);
|
|
3333
|
-
} catch {
|
|
3334
|
-
throw new Error("Decryption failed");
|
|
3335
|
-
}
|
|
3336
|
-
} else
|
|
3337
|
-
throw new Error(
|
|
3338
|
-
"Unknown header for AES-GCM encrypted ciphertext"
|
|
3339
|
-
);
|
|
3340
|
-
const c = Se(Te, n), a = t.slice(
|
|
3341
|
-
Zt,
|
|
3342
|
-
Zt + qt
|
|
3343
|
-
), u = t.slice(
|
|
3344
|
-
Zt + qt
|
|
3345
|
-
), l = await this.deriveAesGcmCryptoKey(
|
|
3346
|
-
r,
|
|
3347
|
-
hn
|
|
3348
|
-
);
|
|
3349
|
-
try {
|
|
3350
|
-
const d = await globalThis.crypto.subtle.decrypt(
|
|
3351
|
-
{ name: "AES-GCM", iv: a, additionalData: c },
|
|
3352
|
-
l,
|
|
3353
|
-
u
|
|
3354
|
-
);
|
|
3355
|
-
return new Uint8Array(d);
|
|
3356
|
-
} catch {
|
|
3357
|
-
throw new Error("Decryption failed");
|
|
3358
|
-
}
|
|
3359
|
-
}
|
|
3360
|
-
}
|
|
3361
|
-
class Xn {
|
|
3362
|
-
#t;
|
|
3363
|
-
#e;
|
|
3364
|
-
#n;
|
|
3365
|
-
/**
|
|
3366
|
-
* Parse an encrypted key returned by the `vetkd_derive_encrypted_key`
|
|
3367
|
-
* managment canister interface
|
|
3368
|
-
*/
|
|
3369
|
-
static deserialize(t) {
|
|
3370
|
-
if (t.length !== it + nt + it)
|
|
3371
|
-
throw new Error("Invalid EncryptedVetKey serialization");
|
|
3372
|
-
const r = P.G1.Point.fromHex(t.subarray(0, it)), n = P.G2.Point.fromHex(
|
|
3373
|
-
t.subarray(it, it + nt)
|
|
3374
|
-
), s = P.G1.Point.fromHex(
|
|
3375
|
-
t.subarray(it + nt)
|
|
3376
|
-
);
|
|
3377
|
-
return new Xn(r, n, s);
|
|
3378
|
-
}
|
|
3379
|
-
/**
|
|
3380
|
-
* Decrypt the encrypted key returning a VetKey
|
|
3381
|
-
*/
|
|
3382
|
-
decryptAndVerify(t, r, n) {
|
|
3383
|
-
const s = P.G1.Point.BASE, o = P.G2.Point.BASE.negate(), i = P.fields.Fp12.ONE, c = P.pairingBatch([
|
|
3384
|
-
{ g1: this.#t, g2: o },
|
|
3385
|
-
{ g1: s, g2: this.#e }
|
|
3386
|
-
]);
|
|
3387
|
-
if (!P.fields.Fp12.eql(c, i))
|
|
3388
|
-
throw new Error("Invalid VetKey");
|
|
3389
|
-
const a = this.#n.subtract(
|
|
3390
|
-
this.#t.multiply(P.G1.Point.Fn.fromBytes(t.serialize()))
|
|
3391
|
-
);
|
|
3392
|
-
if (jn(r, n, a))
|
|
3393
|
-
return new le(a);
|
|
3394
|
-
throw new Error("Invalid VetKey");
|
|
3395
|
-
}
|
|
3396
|
-
/**
|
|
3397
|
-
* @internal constructor
|
|
3398
|
-
*/
|
|
3399
|
-
constructor(t, r, n) {
|
|
3400
|
-
this.#t = t, this.#e = r, this.#n = n;
|
|
3401
|
-
}
|
|
3402
|
-
}
|
|
3403
|
-
const gn = new Uint8Array([
|
|
3404
|
-
73,
|
|
3405
|
-
67,
|
|
3406
|
-
32,
|
|
3407
|
-
73,
|
|
3408
|
-
66,
|
|
3409
|
-
69,
|
|
3410
|
-
0,
|
|
3411
|
-
1
|
|
3412
|
-
]), mt = 8;
|
|
3413
|
-
function yn(e, t, r) {
|
|
3414
|
-
const n = new Uint8Array([...e, ...t, ...r]);
|
|
3415
|
-
return Ye(
|
|
3416
|
-
n,
|
|
3417
|
-
"ic-vetkd-bls12-381-ibe-hash-to-mask"
|
|
3418
|
-
/* HashToMask */
|
|
3419
|
-
);
|
|
3420
|
-
}
|
|
3421
|
-
function Wn(e, t) {
|
|
3422
|
-
if (e.length !== t.length)
|
|
3423
|
-
throw new Error("xorBuf arguments should have the same length");
|
|
3424
|
-
const r = new Uint8Array(e.length);
|
|
3425
|
-
for (let n = 0; n < e.length; n++)
|
|
3426
|
-
r[n] = e[n] ^ t[n];
|
|
3427
|
-
return r;
|
|
3428
|
-
}
|
|
3429
|
-
function pn(e, t) {
|
|
3430
|
-
if (t.length !== 576)
|
|
3431
|
-
throw new Error("Unexpected size for Gt element");
|
|
3432
|
-
const r = Xt(
|
|
3433
|
-
t,
|
|
3434
|
-
"ic-vetkd-bls12-381-ibe-mask-seed",
|
|
3435
|
-
e.length
|
|
3436
|
-
);
|
|
3437
|
-
return Wn(r, e);
|
|
3438
|
-
}
|
|
3439
|
-
function wn(e, t) {
|
|
3440
|
-
const r = "ic-vetkd-bls12-381-ibe-mask-msg-".concat(
|
|
3441
|
-
e.length.toString().padStart(20, "0")
|
|
3442
|
-
), n = Xt(t, r, 32), s = As(n, { dkLen: e.length });
|
|
3443
|
-
return Wn(e, s);
|
|
3444
|
-
}
|
|
3445
|
-
function En(e) {
|
|
3446
|
-
const t = P.fields.Fp12.toBytes(e), r = new Uint8Array(576), n = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
|
|
3447
|
-
for (let s = 0; s < 12; ++s)
|
|
3448
|
-
for (let o = 0; o < 48; ++o)
|
|
3449
|
-
r[48 * s + o] = t[48 * n[s] + o];
|
|
3450
|
-
return r;
|
|
3451
|
-
}
|
|
3452
|
-
function qe(e, t) {
|
|
3453
|
-
if (e.length !== t.length)
|
|
3454
|
-
return !1;
|
|
3455
|
-
let r = 0;
|
|
3456
|
-
for (let n = 0; n < e.length; ++n)
|
|
3457
|
-
r |= e[n] ^ t[n];
|
|
3458
|
-
return r == 0;
|
|
3459
|
-
}
|
|
3460
|
-
class Jt {
|
|
3461
|
-
#t;
|
|
3462
|
-
constructor(t) {
|
|
3463
|
-
this.#t = t;
|
|
3464
|
-
}
|
|
3465
|
-
/**
|
|
3466
|
-
* Create an identity from a byte string
|
|
3467
|
-
*/
|
|
3468
|
-
static fromBytes(t) {
|
|
3469
|
-
return new Jt(t);
|
|
3470
|
-
}
|
|
3471
|
-
/**
|
|
3472
|
-
* Create an identity from a string
|
|
3473
|
-
*/
|
|
3474
|
-
static fromString(t) {
|
|
3475
|
-
return Jt.fromBytes(new TextEncoder().encode(t));
|
|
3476
|
-
}
|
|
3477
|
-
/**
|
|
3478
|
-
* Create an identity from a Principal
|
|
3479
|
-
*/
|
|
3480
|
-
static fromPrincipal(t) {
|
|
3481
|
-
return Jt.fromBytes(t.toUint8Array());
|
|
3482
|
-
}
|
|
3483
|
-
/**
|
|
3484
|
-
* @internal getter returning the encoded
|
|
3485
|
-
*/
|
|
3486
|
-
getBytes() {
|
|
3487
|
-
return this.#t;
|
|
3488
|
-
}
|
|
3489
|
-
}
|
|
3490
|
-
const pt = 32;
|
|
3491
|
-
class te {
|
|
3492
|
-
#t;
|
|
3493
|
-
constructor(t) {
|
|
3494
|
-
if (t.length !== pt)
|
|
3495
|
-
throw new Error("IBE seed must be exactly IBE_SEED_BYTES long");
|
|
3496
|
-
this.#t = t;
|
|
3497
|
-
}
|
|
3498
|
-
/**
|
|
3499
|
-
* Create a seed for IBE encryption from a byte string
|
|
3500
|
-
*
|
|
3501
|
-
* This input should be randomly chosen by a secure random number generator.
|
|
3502
|
-
* If the seed is not securely generated the IBE scheme will be insecure.
|
|
3503
|
-
*
|
|
3504
|
-
* At least 128 bits (16 bytes) must be provided.
|
|
3505
|
-
*
|
|
3506
|
-
* If the input is exactly 256 bits it is used directly. Otherwise the input
|
|
3507
|
-
* is hashed with HKDF to produce a 256 bit seed.
|
|
3508
|
-
*/
|
|
3509
|
-
static fromBytes(t) {
|
|
3510
|
-
if (t.length < 16)
|
|
3511
|
-
throw new Error(
|
|
3512
|
-
"Insufficient input material for IbeSeed derivation"
|
|
3513
|
-
);
|
|
3514
|
-
return t.length == pt ? new te(t) : new te(
|
|
3515
|
-
Xt(
|
|
3516
|
-
t,
|
|
3517
|
-
"ic-vetkd-bls12-381-ibe-hash-seed",
|
|
3518
|
-
pt
|
|
3519
|
-
)
|
|
3520
|
-
);
|
|
3521
|
-
}
|
|
3522
|
-
/**
|
|
3523
|
-
* Create a random seed for IBE encryption
|
|
3524
|
-
*/
|
|
3525
|
-
static random() {
|
|
3526
|
-
return new te(
|
|
3527
|
-
globalThis.crypto.getRandomValues(new Uint8Array(pt))
|
|
3528
|
-
);
|
|
3529
|
-
}
|
|
3530
|
-
/**
|
|
3531
|
-
* @internal getter returning the seed bytes
|
|
3532
|
-
*/
|
|
3533
|
-
getBytes() {
|
|
3534
|
-
return this.#t;
|
|
3535
|
-
}
|
|
3536
|
-
}
|
|
3537
|
-
const ve = mt + pt + nt;
|
|
3538
|
-
class Re {
|
|
3539
|
-
#t;
|
|
3540
|
-
#e;
|
|
3541
|
-
#n;
|
|
3542
|
-
#r;
|
|
3543
|
-
/**
|
|
3544
|
-
* Helper function for determining the size of an IBE ciphertext in bytes.
|
|
3545
|
-
*/
|
|
3546
|
-
static ciphertextSize(t) {
|
|
3547
|
-
if (t < 0)
|
|
3548
|
-
throw new Error(
|
|
3549
|
-
"IbeCiphertext.ciphertextSize argument cannot be negative"
|
|
3550
|
-
);
|
|
3551
|
-
return t + ve;
|
|
3552
|
-
}
|
|
3553
|
-
/**
|
|
3554
|
-
* Helper function for determining the size of an IBE plaintext in bytes.
|
|
3555
|
-
*/
|
|
3556
|
-
static plaintextSize(t) {
|
|
3557
|
-
if (t < ve)
|
|
3558
|
-
throw new Error(
|
|
3559
|
-
"IbeCiphertext.plaintextSize given ciphertext size is too small to be valid"
|
|
3560
|
-
);
|
|
3561
|
-
return t - ve;
|
|
3562
|
-
}
|
|
3563
|
-
/**
|
|
3564
|
-
* Serialize the IBE ciphertext to a bytestring
|
|
3565
|
-
*/
|
|
3566
|
-
serialize() {
|
|
3567
|
-
const t = this.#e.toBytes(!0);
|
|
3568
|
-
return new Uint8Array([
|
|
3569
|
-
...this.#t,
|
|
3570
|
-
...t,
|
|
3571
|
-
...this.#n,
|
|
3572
|
-
...this.#r
|
|
3573
|
-
]);
|
|
3574
|
-
}
|
|
3575
|
-
/**
|
|
3576
|
-
* Deserialize an IBE ciphertext
|
|
3577
|
-
*/
|
|
3578
|
-
static deserialize(t) {
|
|
3579
|
-
if (t.length < mt + nt + pt)
|
|
3580
|
-
throw new Error("Invalid IBE ciphertext");
|
|
3581
|
-
const r = t.subarray(0, mt), n = P.G2.Point.fromHex(
|
|
3582
|
-
t.subarray(mt, mt + nt)
|
|
3583
|
-
), s = t.subarray(
|
|
3584
|
-
mt + nt,
|
|
3585
|
-
mt + nt + pt
|
|
3586
|
-
), o = t.subarray(mt + nt + pt);
|
|
3587
|
-
if (!qe(r, gn))
|
|
3588
|
-
throw new Error("Unexpected header for IBE ciphertext");
|
|
3589
|
-
return new Re(r, n, s, o);
|
|
3590
|
-
}
|
|
3591
|
-
/**
|
|
3592
|
-
* Encrypt a message using IBE, returning the ciphertext
|
|
3593
|
-
*
|
|
3594
|
-
* Any user who is able to retrieve the VetKey for the specified derived public key and
|
|
3595
|
-
* identity will be able to decrypt this message.
|
|
3596
|
-
*
|
|
3597
|
-
* There is no fixed upper bound on the size of the message that can be encrypted using
|
|
3598
|
-
* this scheme. However, internally during the encryption process several heap allocations
|
|
3599
|
-
* are performed which are approximately the same length as the message itself, so
|
|
3600
|
-
* encrypting or decrypting very large messages may result in memory allocation errors.
|
|
3601
|
-
*
|
|
3602
|
-
* If you anticipate using IBE to encrypt very large messages, consider using IBE just to
|
|
3603
|
-
* encrypt a symmetric key, and then using a standard cipher such as AES-GCM to encrypt the
|
|
3604
|
-
* data.
|
|
3605
|
-
*
|
|
3606
|
-
* The seed parameter must be a randomly generated value that was generated just for this
|
|
3607
|
-
* one message. Using it for a second message, or for any other purpose, compromises the
|
|
3608
|
-
* security of the IBE scheme.
|
|
3609
|
-
*/
|
|
3610
|
-
static encrypt(t, r, n, s) {
|
|
3611
|
-
const o = gn, i = yn(o, s.getBytes(), n), c = Rs(t, r.getBytes()), a = P.fields.Fp12.pow(
|
|
3612
|
-
P.pairing(c, t.getPoint()),
|
|
3613
|
-
i
|
|
3614
|
-
), u = P.G2.Point.BASE.multiply(i), l = pn(s.getBytes(), En(a)), d = wn(n, s.getBytes());
|
|
3615
|
-
return new Re(o, u, l, d);
|
|
3616
|
-
}
|
|
3617
|
-
/**
|
|
3618
|
-
* Decrypt an IBE ciphertext, returning the message
|
|
3619
|
-
*
|
|
3620
|
-
* There is no fixed upper bound on the size of the message that can be encrypted using
|
|
3621
|
-
* this scheme. However, internally during the encryption process several heap allocations
|
|
3622
|
-
* are performed which are approximately the same length as the message itself, so
|
|
3623
|
-
* encrypting or decrypting very large messages may result in memory allocation errors.
|
|
3624
|
-
*/
|
|
3625
|
-
decrypt(t) {
|
|
3626
|
-
const r = pn(
|
|
3627
|
-
this.#n,
|
|
3628
|
-
En(P.pairing(t.getPoint(), this.#e))
|
|
3629
|
-
), n = wn(this.#r, r), s = yn(this.#t, r, n);
|
|
3630
|
-
if (qe(
|
|
3631
|
-
P.G2.Point.BASE.multiply(s).toBytes(!0),
|
|
3632
|
-
this.#e.toBytes(!0)
|
|
3633
|
-
))
|
|
3634
|
-
return n;
|
|
3635
|
-
throw new Error("Decryption failed");
|
|
3636
|
-
}
|
|
3637
|
-
/**
|
|
3638
|
-
* Private constructor
|
|
3639
|
-
*/
|
|
3640
|
-
constructor(t, r, n, s) {
|
|
3641
|
-
this.#t = t, this.#e = r, this.#n = n, this.#r = s;
|
|
3642
|
-
}
|
|
3643
|
-
}
|
|
3644
|
-
const Gs = 32;
|
|
3645
|
-
class Ge {
|
|
3646
|
-
#t;
|
|
3647
|
-
#e;
|
|
3648
|
-
#n;
|
|
3649
|
-
#r;
|
|
3650
|
-
static computeVrfHash(t, r, n) {
|
|
3651
|
-
const s = new Uint8Array([
|
|
3652
|
-
..._t(t.serialize()),
|
|
3653
|
-
..._t(r.publicKeyBytes()),
|
|
3654
|
-
..._t(n)
|
|
3655
|
-
]);
|
|
3656
|
-
return Xt(
|
|
3657
|
-
s,
|
|
3658
|
-
"ic-vetkd-bls12-381-g2-vrf",
|
|
3659
|
-
Gs
|
|
3660
|
-
);
|
|
3661
|
-
}
|
|
3662
|
-
/**
|
|
3663
|
-
* Serialize a VrfOutput to a byte string
|
|
3664
|
-
*/
|
|
3665
|
-
serialize() {
|
|
3666
|
-
return new Uint8Array([
|
|
3667
|
-
...this.#t.serialize(),
|
|
3668
|
-
...this.#e.publicKeyBytes(),
|
|
3669
|
-
...this.#n
|
|
3670
|
-
]);
|
|
3671
|
-
}
|
|
3672
|
-
/**
|
|
3673
|
-
* Deserialize and verify a VrfOutput
|
|
3674
|
-
*
|
|
3675
|
-
* Note this verifies the VrfOutput with respect to the derived public key
|
|
3676
|
-
* and VRF input which are included in the struct. It is the responsibility
|
|
3677
|
-
* of the application to examine the return value of `publicKey` and `input`
|
|
3678
|
-
* and ensure these values make sense in the context where this VRF is being
|
|
3679
|
-
* used.
|
|
3680
|
-
*/
|
|
3681
|
-
static deserialize(t) {
|
|
3682
|
-
if (t.length < it + nt)
|
|
3683
|
-
throw new Error(
|
|
3684
|
-
"VrfOutput.deserialize input too short to possibly be valid"
|
|
3685
|
-
);
|
|
3686
|
-
const r = le.deserialize(t.slice(0, it)), n = zt.deserialize(
|
|
3687
|
-
t.slice(it, it + nt)
|
|
3688
|
-
), s = t.slice(it + nt);
|
|
3689
|
-
if (!jn(n, s, r.getPoint()))
|
|
3690
|
-
throw new Error("VrfOutput.deserialize proof is invalid");
|
|
3691
|
-
return new Ge(r, n, s);
|
|
3692
|
-
}
|
|
3693
|
-
/**
|
|
3694
|
-
* Return the public key under which this VRF output was derived
|
|
3695
|
-
*/
|
|
3696
|
-
publicKey() {
|
|
3697
|
-
return this.#e;
|
|
3698
|
-
}
|
|
3699
|
-
/**
|
|
3700
|
-
* Return the input that was used to create this VRF output
|
|
3701
|
-
*/
|
|
3702
|
-
input() {
|
|
3703
|
-
return this.#n;
|
|
3704
|
-
}
|
|
3705
|
-
/**
|
|
3706
|
-
* Return the VRF output
|
|
3707
|
-
*
|
|
3708
|
-
* This is a random-looking value which was provably generated by some party with
|
|
3709
|
-
* access to the VRF secret key.
|
|
3710
|
-
*/
|
|
3711
|
-
output() {
|
|
3712
|
-
return this.#r;
|
|
3713
|
-
}
|
|
3714
|
-
/**
|
|
3715
|
-
* Private constructor
|
|
3716
|
-
*/
|
|
3717
|
-
constructor(t, r, n) {
|
|
3718
|
-
this.#t = t, this.#e = r, this.#n = n, this.#r = Ge.computeVrfHash(t, r, n);
|
|
3719
|
-
}
|
|
3720
|
-
}
|
|
1
|
+
import { D as s, a as r, E as t, I as i, b as y, c, M as d, d as K, P as b, T as l, V as u, e as I, f as o, g as P, h as n, i as p, v as h } from "./utils-CiynM__c.mjs";
|
|
3721
2
|
export {
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3
|
+
s as DerivedKeyMaterial,
|
|
4
|
+
r as DerivedPublicKey,
|
|
5
|
+
t as EncryptedVetKey,
|
|
6
|
+
i as IbeCiphertext,
|
|
7
|
+
y as IbeIdentity,
|
|
8
|
+
c as IbeSeed,
|
|
9
|
+
d as MasterPublicKey,
|
|
10
|
+
K as MasterPublicKeyId,
|
|
11
|
+
b as PocketIcMasterPublicKeyId,
|
|
12
|
+
l as TransportSecretKey,
|
|
13
|
+
u as VetKey,
|
|
14
|
+
I as VrfOutput,
|
|
15
|
+
o as augmentedHashToG1,
|
|
16
|
+
P as deriveSymmetricKey,
|
|
17
|
+
n as hashToScalar,
|
|
18
|
+
p as isValidTransportPublicKey,
|
|
19
|
+
h as verifyBlsSignature
|
|
3739
20
|
};
|