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