@blockcast/mmt-container 2.2.0-main.ea9223dc11de → 2.2.0-main.ec45bee1b9cc
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 +125 -2
- package/dist/__tests__/ad-creative-signer.d.ts +11 -0
- package/dist/__tests__/ad-creative-signer.d.ts.map +1 -0
- package/dist/__tests__/test-utils.d.ts +1 -0
- package/dist/__tests__/test-utils.d.ts.map +1 -1
- package/dist/ad-beacon.d.ts +39 -0
- package/dist/ad-beacon.d.ts.map +1 -0
- package/dist/ad-beacon.js +23 -0
- package/dist/ad-beacon.js.map +7 -0
- package/dist/ad-creative-record.d.ts +65 -0
- package/dist/ad-creative-record.d.ts.map +1 -0
- package/dist/ad-creative-record.js +2281 -0
- package/dist/ad-creative-record.js.map +7 -0
- package/dist/ad-creative-timeline.d.ts +103 -0
- package/dist/ad-creative-timeline.d.ts.map +1 -0
- package/dist/ad-creative-timeline.js +2550 -0
- package/dist/ad-creative-timeline.js.map +7 -0
- package/dist/block-policy.d.ts +128 -14
- package/dist/block-policy.d.ts.map +1 -1
- package/dist/block-policy.js +472 -27
- package/dist/block-policy.js.map +2 -2
- package/dist/browser-runtime.js +1039 -120
- package/dist/browser-runtime.js.map +4 -4
- package/dist/carp-catalog.d.ts +1 -0
- package/dist/carp-catalog.d.ts.map +1 -1
- package/dist/carp-catalog.js +34 -0
- package/dist/carp-catalog.js.map +4 -4
- package/dist/catalog-fec-params.js.map +1 -1
- package/dist/catalog-network-source.d.ts +1 -17
- package/dist/catalog-network-source.d.ts.map +1 -1
- package/dist/catalog-network-source.js +25 -19
- package/dist/catalog-network-source.js.map +3 -3
- package/dist/cmaf-block-assembler.d.ts +10 -2
- package/dist/cmaf-block-assembler.d.ts.map +1 -1
- package/dist/cmaf-block-assembler.js +30 -8
- package/dist/cmaf-block-assembler.js.map +3 -3
- package/dist/codec-utils.d.ts.map +1 -1
- package/dist/codec-utils.js +16 -3
- package/dist/codec-utils.js.map +2 -2
- package/dist/conceal-timeline.d.ts +24 -1
- package/dist/conceal-timeline.d.ts.map +1 -1
- package/dist/conceal-timeline.js +201 -4
- package/dist/conceal-timeline.js.map +3 -3
- package/dist/datagram-leg-deadline.d.ts +4 -0
- package/dist/datagram-leg-deadline.d.ts.map +1 -1
- package/dist/datagram-leg-deadline.js.map +2 -2
- package/dist/event-timeline-mmtp.d.ts +17 -0
- package/dist/event-timeline-mmtp.d.ts.map +1 -0
- package/dist/event-timeline-mmtp.js +3117 -0
- package/dist/event-timeline-mmtp.js.map +7 -0
- package/dist/fec-manager.d.ts +24 -3
- package/dist/fec-manager.d.ts.map +1 -1
- package/dist/fec-manager.js +531 -38
- package/dist/fec-manager.js.map +2 -2
- package/dist/feed-gate.d.ts +114 -0
- package/dist/feed-gate.d.ts.map +1 -0
- package/dist/feed-gate.js +175 -0
- package/dist/feed-gate.js.map +7 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +2 -2
- package/dist/iso-bmff-timing.d.ts +16 -0
- package/dist/iso-bmff-timing.d.ts.map +1 -1
- package/dist/iso-bmff-timing.js +28 -0
- package/dist/iso-bmff-timing.js.map +2 -2
- package/dist/mfu-reassembler.d.ts +2 -0
- package/dist/mfu-reassembler.d.ts.map +1 -1
- package/dist/mfu-reassembler.js +4 -2
- package/dist/mfu-reassembler.js.map +2 -2
- package/dist/mmtp-container-parser.d.ts +12 -0
- package/dist/mmtp-container-parser.d.ts.map +1 -1
- package/dist/mmtp-container-parser.js +73 -3
- package/dist/mmtp-container-parser.js.map +2 -2
- package/dist/mmtp-container-parser.no-modules.js +103 -25
- package/dist/mmtp-container-parser.no-modules.js.map +4 -4
- package/dist/mmtp-decode-pipeline.js +73 -3
- package/dist/mmtp-decode-pipeline.js.map +2 -2
- package/dist/mmtp-router.d.ts +20 -2
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +150 -23
- package/dist/mmtp-router.js.map +3 -3
- package/dist/multicast-auth-catalog.js.map +1 -1
- package/dist/multicast-input.js.map +1 -1
- package/dist/multicast-manager.d.ts +23 -2
- package/dist/multicast-manager.d.ts.map +1 -1
- package/dist/multicast-manager.js +749 -87
- package/dist/multicast-manager.js.map +3 -3
- package/dist/multicast-transport.d.ts +8 -0
- package/dist/multicast-transport.d.ts.map +1 -1
- package/dist/multicast-transport.js +56 -23
- package/dist/multicast-transport.js.map +2 -2
- package/dist/no-modules.js +98 -22
- package/dist/no-modules.js.map +4 -4
- package/dist/repair-fetch.d.ts +15 -7
- package/dist/repair-fetch.d.ts.map +1 -1
- package/dist/repair-fetch.js +34 -7
- package/dist/repair-fetch.js.map +2 -2
- package/dist/wasm-mfu-reassembler.js.map +2 -2
- package/dist/wasm-mmtp-container-parser.js +73 -3
- package/dist/wasm-mmtp-container-parser.js.map +2 -2
- package/package.json +17 -5
- package/schemas/catalog.schema.json +7 -2
- package/src/ad-beacon.ts +54 -0
- package/src/ad-creative-record.ts +276 -0
- package/src/ad-creative-timeline.ts +410 -0
- package/src/block-policy.ts +673 -42
- package/src/carp-catalog.ts +9 -0
- package/src/catalog-network-source.ts +3 -37
- package/src/cmaf-block-assembler.ts +17 -0
- package/src/codec-utils.ts +19 -3
- package/src/conceal-timeline.ts +60 -5
- package/src/datagram-leg-deadline.ts +4 -0
- package/src/event-timeline-mmtp.ts +55 -0
- package/src/fec-manager.ts +84 -12
- package/src/feed-gate.ts +295 -0
- package/src/index.ts +21 -0
- package/src/iso-bmff-timing.ts +33 -0
- package/src/mfu-reassembler.ts +4 -0
- package/src/mmtp-container-parser.ts +51 -5
- package/src/mmtp-router.ts +116 -4
- package/src/multicast-manager.ts +31 -0
- package/src/multicast-transport.ts +27 -2
- package/src/repair-fetch.ts +70 -19
- package/test-vectors/catalog/README.md +1 -0
- package/test-vectors/catalog/negative/event-timeline-empty-event-type.json +41 -0
- package/test-vectors/catalog/positive/event-timeline-ad-creative.json +41 -0
- package/test-vectors/catalog/positive/event-timeline-wallclock-no-depends.json +38 -0
- package/test-vectors/catalog/positive/multicast-event-timeline.json +81 -0
- package/test-vectors/catalog/section-codes.json +1 -0
- package/test-vectors/frame-needs-v1.json +295 -0
- package/test-vectors/repair-policy-v1.json +98 -18
|
@@ -0,0 +1,2281 @@
|
|
|
1
|
+
// node_modules/@noble/hashes/esm/crypto.js
|
|
2
|
+
var crypto = typeof globalThis === "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
3
|
+
|
|
4
|
+
// node_modules/@noble/hashes/esm/utils.js
|
|
5
|
+
function isBytes(a) {
|
|
6
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
7
|
+
}
|
|
8
|
+
function anumber(n) {
|
|
9
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
10
|
+
throw new Error("positive integer expected, got " + n);
|
|
11
|
+
}
|
|
12
|
+
function abytes(b, ...lengths) {
|
|
13
|
+
if (!isBytes(b))
|
|
14
|
+
throw new Error("Uint8Array expected");
|
|
15
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
16
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
17
|
+
}
|
|
18
|
+
function aexists(instance, checkFinished = true) {
|
|
19
|
+
if (instance.destroyed)
|
|
20
|
+
throw new Error("Hash instance has been destroyed");
|
|
21
|
+
if (checkFinished && instance.finished)
|
|
22
|
+
throw new Error("Hash#digest() has already been called");
|
|
23
|
+
}
|
|
24
|
+
function aoutput(out, instance) {
|
|
25
|
+
abytes(out);
|
|
26
|
+
const min = instance.outputLen;
|
|
27
|
+
if (out.length < min) {
|
|
28
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function clean(...arrays) {
|
|
32
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
33
|
+
arrays[i].fill(0);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function createView(arr) {
|
|
37
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
38
|
+
}
|
|
39
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
42
|
+
))();
|
|
43
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
44
|
+
function bytesToHex(bytes) {
|
|
45
|
+
abytes(bytes);
|
|
46
|
+
if (hasHexBuiltin)
|
|
47
|
+
return bytes.toHex();
|
|
48
|
+
let hex = "";
|
|
49
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
50
|
+
hex += hexes[bytes[i]];
|
|
51
|
+
}
|
|
52
|
+
return hex;
|
|
53
|
+
}
|
|
54
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
55
|
+
function asciiToBase16(ch) {
|
|
56
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
57
|
+
return ch - asciis._0;
|
|
58
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
59
|
+
return ch - (asciis.A - 10);
|
|
60
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
61
|
+
return ch - (asciis.a - 10);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
function hexToBytes(hex) {
|
|
65
|
+
if (typeof hex !== "string")
|
|
66
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
67
|
+
if (hasHexBuiltin)
|
|
68
|
+
return Uint8Array.fromHex(hex);
|
|
69
|
+
const hl = hex.length;
|
|
70
|
+
const al = hl / 2;
|
|
71
|
+
if (hl % 2)
|
|
72
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
73
|
+
const array = new Uint8Array(al);
|
|
74
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
75
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
76
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
77
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
78
|
+
const char = hex[hi] + hex[hi + 1];
|
|
79
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
80
|
+
}
|
|
81
|
+
array[ai] = n1 * 16 + n2;
|
|
82
|
+
}
|
|
83
|
+
return array;
|
|
84
|
+
}
|
|
85
|
+
function utf8ToBytes(str) {
|
|
86
|
+
if (typeof str !== "string")
|
|
87
|
+
throw new Error("string expected");
|
|
88
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
89
|
+
}
|
|
90
|
+
function toBytes(data) {
|
|
91
|
+
if (typeof data === "string")
|
|
92
|
+
data = utf8ToBytes(data);
|
|
93
|
+
abytes(data);
|
|
94
|
+
return data;
|
|
95
|
+
}
|
|
96
|
+
function concatBytes(...arrays) {
|
|
97
|
+
let sum = 0;
|
|
98
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
99
|
+
const a = arrays[i];
|
|
100
|
+
abytes(a);
|
|
101
|
+
sum += a.length;
|
|
102
|
+
}
|
|
103
|
+
const res = new Uint8Array(sum);
|
|
104
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
105
|
+
const a = arrays[i];
|
|
106
|
+
res.set(a, pad);
|
|
107
|
+
pad += a.length;
|
|
108
|
+
}
|
|
109
|
+
return res;
|
|
110
|
+
}
|
|
111
|
+
var Hash = class {
|
|
112
|
+
};
|
|
113
|
+
function createHasher(hashCons) {
|
|
114
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
115
|
+
const tmp = hashCons();
|
|
116
|
+
hashC.outputLen = tmp.outputLen;
|
|
117
|
+
hashC.blockLen = tmp.blockLen;
|
|
118
|
+
hashC.create = () => hashCons();
|
|
119
|
+
return hashC;
|
|
120
|
+
}
|
|
121
|
+
function randomBytes(bytesLength = 32) {
|
|
122
|
+
if (crypto && typeof crypto.getRandomValues === "function") {
|
|
123
|
+
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
124
|
+
}
|
|
125
|
+
if (crypto && typeof crypto.randomBytes === "function") {
|
|
126
|
+
return Uint8Array.from(crypto.randomBytes(bytesLength));
|
|
127
|
+
}
|
|
128
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// node_modules/@noble/hashes/esm/_md.js
|
|
132
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
133
|
+
if (typeof view.setBigUint64 === "function")
|
|
134
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
135
|
+
const _32n2 = BigInt(32);
|
|
136
|
+
const _u32_max = BigInt(4294967295);
|
|
137
|
+
const wh = Number(value >> _32n2 & _u32_max);
|
|
138
|
+
const wl = Number(value & _u32_max);
|
|
139
|
+
const h = isLE ? 4 : 0;
|
|
140
|
+
const l = isLE ? 0 : 4;
|
|
141
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
142
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
143
|
+
}
|
|
144
|
+
var HashMD = class extends Hash {
|
|
145
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
146
|
+
super();
|
|
147
|
+
this.finished = false;
|
|
148
|
+
this.length = 0;
|
|
149
|
+
this.pos = 0;
|
|
150
|
+
this.destroyed = false;
|
|
151
|
+
this.blockLen = blockLen;
|
|
152
|
+
this.outputLen = outputLen;
|
|
153
|
+
this.padOffset = padOffset;
|
|
154
|
+
this.isLE = isLE;
|
|
155
|
+
this.buffer = new Uint8Array(blockLen);
|
|
156
|
+
this.view = createView(this.buffer);
|
|
157
|
+
}
|
|
158
|
+
update(data) {
|
|
159
|
+
aexists(this);
|
|
160
|
+
data = toBytes(data);
|
|
161
|
+
abytes(data);
|
|
162
|
+
const { view, buffer, blockLen } = this;
|
|
163
|
+
const len = data.length;
|
|
164
|
+
for (let pos = 0; pos < len; ) {
|
|
165
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
166
|
+
if (take === blockLen) {
|
|
167
|
+
const dataView = createView(data);
|
|
168
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
169
|
+
this.process(dataView, pos);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
173
|
+
this.pos += take;
|
|
174
|
+
pos += take;
|
|
175
|
+
if (this.pos === blockLen) {
|
|
176
|
+
this.process(view, 0);
|
|
177
|
+
this.pos = 0;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
this.length += data.length;
|
|
181
|
+
this.roundClean();
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
digestInto(out) {
|
|
185
|
+
aexists(this);
|
|
186
|
+
aoutput(out, this);
|
|
187
|
+
this.finished = true;
|
|
188
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
189
|
+
let { pos } = this;
|
|
190
|
+
buffer[pos++] = 128;
|
|
191
|
+
clean(this.buffer.subarray(pos));
|
|
192
|
+
if (this.padOffset > blockLen - pos) {
|
|
193
|
+
this.process(view, 0);
|
|
194
|
+
pos = 0;
|
|
195
|
+
}
|
|
196
|
+
for (let i = pos; i < blockLen; i++)
|
|
197
|
+
buffer[i] = 0;
|
|
198
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
199
|
+
this.process(view, 0);
|
|
200
|
+
const oview = createView(out);
|
|
201
|
+
const len = this.outputLen;
|
|
202
|
+
if (len % 4)
|
|
203
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
204
|
+
const outLen = len / 4;
|
|
205
|
+
const state = this.get();
|
|
206
|
+
if (outLen > state.length)
|
|
207
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
208
|
+
for (let i = 0; i < outLen; i++)
|
|
209
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
210
|
+
}
|
|
211
|
+
digest() {
|
|
212
|
+
const { buffer, outputLen } = this;
|
|
213
|
+
this.digestInto(buffer);
|
|
214
|
+
const res = buffer.slice(0, outputLen);
|
|
215
|
+
this.destroy();
|
|
216
|
+
return res;
|
|
217
|
+
}
|
|
218
|
+
_cloneInto(to) {
|
|
219
|
+
to || (to = new this.constructor());
|
|
220
|
+
to.set(...this.get());
|
|
221
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
222
|
+
to.destroyed = destroyed;
|
|
223
|
+
to.finished = finished;
|
|
224
|
+
to.length = length;
|
|
225
|
+
to.pos = pos;
|
|
226
|
+
if (length % blockLen)
|
|
227
|
+
to.buffer.set(buffer);
|
|
228
|
+
return to;
|
|
229
|
+
}
|
|
230
|
+
clone() {
|
|
231
|
+
return this._cloneInto();
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
235
|
+
1779033703,
|
|
236
|
+
4089235720,
|
|
237
|
+
3144134277,
|
|
238
|
+
2227873595,
|
|
239
|
+
1013904242,
|
|
240
|
+
4271175723,
|
|
241
|
+
2773480762,
|
|
242
|
+
1595750129,
|
|
243
|
+
1359893119,
|
|
244
|
+
2917565137,
|
|
245
|
+
2600822924,
|
|
246
|
+
725511199,
|
|
247
|
+
528734635,
|
|
248
|
+
4215389547,
|
|
249
|
+
1541459225,
|
|
250
|
+
327033209
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
// node_modules/@noble/hashes/esm/_u64.js
|
|
254
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
255
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
256
|
+
function fromBig(n, le = false) {
|
|
257
|
+
if (le)
|
|
258
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
259
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
260
|
+
}
|
|
261
|
+
function split(lst, le = false) {
|
|
262
|
+
const len = lst.length;
|
|
263
|
+
let Ah = new Uint32Array(len);
|
|
264
|
+
let Al = new Uint32Array(len);
|
|
265
|
+
for (let i = 0; i < len; i++) {
|
|
266
|
+
const { h, l } = fromBig(lst[i], le);
|
|
267
|
+
[Ah[i], Al[i]] = [h, l];
|
|
268
|
+
}
|
|
269
|
+
return [Ah, Al];
|
|
270
|
+
}
|
|
271
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
272
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
273
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
274
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
275
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
276
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
277
|
+
function add(Ah, Al, Bh, Bl) {
|
|
278
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
279
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
280
|
+
}
|
|
281
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
282
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
283
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
284
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
285
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
286
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
287
|
+
|
|
288
|
+
// node_modules/@noble/hashes/esm/sha2.js
|
|
289
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
290
|
+
"0x428a2f98d728ae22",
|
|
291
|
+
"0x7137449123ef65cd",
|
|
292
|
+
"0xb5c0fbcfec4d3b2f",
|
|
293
|
+
"0xe9b5dba58189dbbc",
|
|
294
|
+
"0x3956c25bf348b538",
|
|
295
|
+
"0x59f111f1b605d019",
|
|
296
|
+
"0x923f82a4af194f9b",
|
|
297
|
+
"0xab1c5ed5da6d8118",
|
|
298
|
+
"0xd807aa98a3030242",
|
|
299
|
+
"0x12835b0145706fbe",
|
|
300
|
+
"0x243185be4ee4b28c",
|
|
301
|
+
"0x550c7dc3d5ffb4e2",
|
|
302
|
+
"0x72be5d74f27b896f",
|
|
303
|
+
"0x80deb1fe3b1696b1",
|
|
304
|
+
"0x9bdc06a725c71235",
|
|
305
|
+
"0xc19bf174cf692694",
|
|
306
|
+
"0xe49b69c19ef14ad2",
|
|
307
|
+
"0xefbe4786384f25e3",
|
|
308
|
+
"0x0fc19dc68b8cd5b5",
|
|
309
|
+
"0x240ca1cc77ac9c65",
|
|
310
|
+
"0x2de92c6f592b0275",
|
|
311
|
+
"0x4a7484aa6ea6e483",
|
|
312
|
+
"0x5cb0a9dcbd41fbd4",
|
|
313
|
+
"0x76f988da831153b5",
|
|
314
|
+
"0x983e5152ee66dfab",
|
|
315
|
+
"0xa831c66d2db43210",
|
|
316
|
+
"0xb00327c898fb213f",
|
|
317
|
+
"0xbf597fc7beef0ee4",
|
|
318
|
+
"0xc6e00bf33da88fc2",
|
|
319
|
+
"0xd5a79147930aa725",
|
|
320
|
+
"0x06ca6351e003826f",
|
|
321
|
+
"0x142929670a0e6e70",
|
|
322
|
+
"0x27b70a8546d22ffc",
|
|
323
|
+
"0x2e1b21385c26c926",
|
|
324
|
+
"0x4d2c6dfc5ac42aed",
|
|
325
|
+
"0x53380d139d95b3df",
|
|
326
|
+
"0x650a73548baf63de",
|
|
327
|
+
"0x766a0abb3c77b2a8",
|
|
328
|
+
"0x81c2c92e47edaee6",
|
|
329
|
+
"0x92722c851482353b",
|
|
330
|
+
"0xa2bfe8a14cf10364",
|
|
331
|
+
"0xa81a664bbc423001",
|
|
332
|
+
"0xc24b8b70d0f89791",
|
|
333
|
+
"0xc76c51a30654be30",
|
|
334
|
+
"0xd192e819d6ef5218",
|
|
335
|
+
"0xd69906245565a910",
|
|
336
|
+
"0xf40e35855771202a",
|
|
337
|
+
"0x106aa07032bbd1b8",
|
|
338
|
+
"0x19a4c116b8d2d0c8",
|
|
339
|
+
"0x1e376c085141ab53",
|
|
340
|
+
"0x2748774cdf8eeb99",
|
|
341
|
+
"0x34b0bcb5e19b48a8",
|
|
342
|
+
"0x391c0cb3c5c95a63",
|
|
343
|
+
"0x4ed8aa4ae3418acb",
|
|
344
|
+
"0x5b9cca4f7763e373",
|
|
345
|
+
"0x682e6ff3d6b2b8a3",
|
|
346
|
+
"0x748f82ee5defb2fc",
|
|
347
|
+
"0x78a5636f43172f60",
|
|
348
|
+
"0x84c87814a1f0ab72",
|
|
349
|
+
"0x8cc702081a6439ec",
|
|
350
|
+
"0x90befffa23631e28",
|
|
351
|
+
"0xa4506cebde82bde9",
|
|
352
|
+
"0xbef9a3f7b2c67915",
|
|
353
|
+
"0xc67178f2e372532b",
|
|
354
|
+
"0xca273eceea26619c",
|
|
355
|
+
"0xd186b8c721c0c207",
|
|
356
|
+
"0xeada7dd6cde0eb1e",
|
|
357
|
+
"0xf57d4f7fee6ed178",
|
|
358
|
+
"0x06f067aa72176fba",
|
|
359
|
+
"0x0a637dc5a2c898a6",
|
|
360
|
+
"0x113f9804bef90dae",
|
|
361
|
+
"0x1b710b35131c471b",
|
|
362
|
+
"0x28db77f523047d84",
|
|
363
|
+
"0x32caab7b40c72493",
|
|
364
|
+
"0x3c9ebe0a15c9bebc",
|
|
365
|
+
"0x431d67c49c100d4c",
|
|
366
|
+
"0x4cc5d4becb3e42b6",
|
|
367
|
+
"0x597f299cfc657e2a",
|
|
368
|
+
"0x5fcb6fab3ad6faec",
|
|
369
|
+
"0x6c44198c4a475817"
|
|
370
|
+
].map((n) => BigInt(n))))();
|
|
371
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
372
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
373
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
374
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
375
|
+
var SHA512 = class extends HashMD {
|
|
376
|
+
constructor(outputLen = 64) {
|
|
377
|
+
super(128, outputLen, 16, false);
|
|
378
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
379
|
+
this.Al = SHA512_IV[1] | 0;
|
|
380
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
381
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
382
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
383
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
384
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
385
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
386
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
387
|
+
this.El = SHA512_IV[9] | 0;
|
|
388
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
389
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
390
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
391
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
392
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
393
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
394
|
+
}
|
|
395
|
+
// prettier-ignore
|
|
396
|
+
get() {
|
|
397
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
398
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
399
|
+
}
|
|
400
|
+
// prettier-ignore
|
|
401
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
402
|
+
this.Ah = Ah | 0;
|
|
403
|
+
this.Al = Al | 0;
|
|
404
|
+
this.Bh = Bh | 0;
|
|
405
|
+
this.Bl = Bl | 0;
|
|
406
|
+
this.Ch = Ch | 0;
|
|
407
|
+
this.Cl = Cl | 0;
|
|
408
|
+
this.Dh = Dh | 0;
|
|
409
|
+
this.Dl = Dl | 0;
|
|
410
|
+
this.Eh = Eh | 0;
|
|
411
|
+
this.El = El | 0;
|
|
412
|
+
this.Fh = Fh | 0;
|
|
413
|
+
this.Fl = Fl | 0;
|
|
414
|
+
this.Gh = Gh | 0;
|
|
415
|
+
this.Gl = Gl | 0;
|
|
416
|
+
this.Hh = Hh | 0;
|
|
417
|
+
this.Hl = Hl | 0;
|
|
418
|
+
}
|
|
419
|
+
process(view, offset) {
|
|
420
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
421
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
422
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
423
|
+
}
|
|
424
|
+
for (let i = 16; i < 80; i++) {
|
|
425
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
426
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
427
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
428
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
429
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
430
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
431
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
432
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
433
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
434
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
435
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
436
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
437
|
+
}
|
|
438
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
439
|
+
for (let i = 0; i < 80; i++) {
|
|
440
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
441
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
442
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
443
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
444
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
445
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
446
|
+
const T1l = T1ll | 0;
|
|
447
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
448
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
449
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
450
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
451
|
+
Hh = Gh | 0;
|
|
452
|
+
Hl = Gl | 0;
|
|
453
|
+
Gh = Fh | 0;
|
|
454
|
+
Gl = Fl | 0;
|
|
455
|
+
Fh = Eh | 0;
|
|
456
|
+
Fl = El | 0;
|
|
457
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
458
|
+
Dh = Ch | 0;
|
|
459
|
+
Dl = Cl | 0;
|
|
460
|
+
Ch = Bh | 0;
|
|
461
|
+
Cl = Bl | 0;
|
|
462
|
+
Bh = Ah | 0;
|
|
463
|
+
Bl = Al | 0;
|
|
464
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
465
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
466
|
+
Al = All | 0;
|
|
467
|
+
}
|
|
468
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
469
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
470
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
471
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
472
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
473
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
474
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
475
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
476
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
477
|
+
}
|
|
478
|
+
roundClean() {
|
|
479
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
480
|
+
}
|
|
481
|
+
destroy() {
|
|
482
|
+
clean(this.buffer);
|
|
483
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
var sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
487
|
+
|
|
488
|
+
// node_modules/@noble/curves/esm/utils.js
|
|
489
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
490
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
491
|
+
function _abool2(value, title = "") {
|
|
492
|
+
if (typeof value !== "boolean") {
|
|
493
|
+
const prefix = title && `"${title}"`;
|
|
494
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
495
|
+
}
|
|
496
|
+
return value;
|
|
497
|
+
}
|
|
498
|
+
function _abytes2(value, length, title = "") {
|
|
499
|
+
const bytes = isBytes(value);
|
|
500
|
+
const len = value?.length;
|
|
501
|
+
const needsLen = length !== void 0;
|
|
502
|
+
if (!bytes || needsLen && len !== length) {
|
|
503
|
+
const prefix = title && `"${title}" `;
|
|
504
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
505
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
506
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
507
|
+
}
|
|
508
|
+
return value;
|
|
509
|
+
}
|
|
510
|
+
function hexToNumber(hex) {
|
|
511
|
+
if (typeof hex !== "string")
|
|
512
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
513
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
514
|
+
}
|
|
515
|
+
function bytesToNumberBE(bytes) {
|
|
516
|
+
return hexToNumber(bytesToHex(bytes));
|
|
517
|
+
}
|
|
518
|
+
function bytesToNumberLE(bytes) {
|
|
519
|
+
abytes(bytes);
|
|
520
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
521
|
+
}
|
|
522
|
+
function numberToBytesBE(n, len) {
|
|
523
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
524
|
+
}
|
|
525
|
+
function numberToBytesLE(n, len) {
|
|
526
|
+
return numberToBytesBE(n, len).reverse();
|
|
527
|
+
}
|
|
528
|
+
function ensureBytes(title, hex, expectedLength) {
|
|
529
|
+
let res;
|
|
530
|
+
if (typeof hex === "string") {
|
|
531
|
+
try {
|
|
532
|
+
res = hexToBytes(hex);
|
|
533
|
+
} catch (e) {
|
|
534
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
535
|
+
}
|
|
536
|
+
} else if (isBytes(hex)) {
|
|
537
|
+
res = Uint8Array.from(hex);
|
|
538
|
+
} else {
|
|
539
|
+
throw new Error(title + " must be hex string or Uint8Array");
|
|
540
|
+
}
|
|
541
|
+
const len = res.length;
|
|
542
|
+
if (typeof expectedLength === "number" && len !== expectedLength)
|
|
543
|
+
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
544
|
+
return res;
|
|
545
|
+
}
|
|
546
|
+
function equalBytes(a, b) {
|
|
547
|
+
if (a.length !== b.length)
|
|
548
|
+
return false;
|
|
549
|
+
let diff = 0;
|
|
550
|
+
for (let i = 0; i < a.length; i++)
|
|
551
|
+
diff |= a[i] ^ b[i];
|
|
552
|
+
return diff === 0;
|
|
553
|
+
}
|
|
554
|
+
function copyBytes(bytes) {
|
|
555
|
+
return Uint8Array.from(bytes);
|
|
556
|
+
}
|
|
557
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
558
|
+
function inRange(n, min, max) {
|
|
559
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
560
|
+
}
|
|
561
|
+
function aInRange(title, n, min, max) {
|
|
562
|
+
if (!inRange(n, min, max))
|
|
563
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
564
|
+
}
|
|
565
|
+
function bitLen(n) {
|
|
566
|
+
let len;
|
|
567
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
568
|
+
;
|
|
569
|
+
return len;
|
|
570
|
+
}
|
|
571
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
572
|
+
function _validateObject(object, fields, optFields = {}) {
|
|
573
|
+
if (!object || typeof object !== "object")
|
|
574
|
+
throw new Error("expected valid options object");
|
|
575
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
576
|
+
const val = object[fieldName];
|
|
577
|
+
if (isOpt && val === void 0)
|
|
578
|
+
return;
|
|
579
|
+
const current = typeof val;
|
|
580
|
+
if (current !== expectedType || val === null)
|
|
581
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
582
|
+
}
|
|
583
|
+
Object.entries(fields).forEach(([k, v]) => checkField(k, v, false));
|
|
584
|
+
Object.entries(optFields).forEach(([k, v]) => checkField(k, v, true));
|
|
585
|
+
}
|
|
586
|
+
var notImplemented = () => {
|
|
587
|
+
throw new Error("not implemented");
|
|
588
|
+
};
|
|
589
|
+
function memoized(fn) {
|
|
590
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
591
|
+
return (arg, ...args) => {
|
|
592
|
+
const val = map.get(arg);
|
|
593
|
+
if (val !== void 0)
|
|
594
|
+
return val;
|
|
595
|
+
const computed = fn(arg, ...args);
|
|
596
|
+
map.set(arg, computed);
|
|
597
|
+
return computed;
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// node_modules/@noble/curves/esm/abstract/modular.js
|
|
602
|
+
var _0n2 = BigInt(0);
|
|
603
|
+
var _1n2 = BigInt(1);
|
|
604
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
605
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
606
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
607
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
608
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
609
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
610
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
611
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
612
|
+
function mod(a, b) {
|
|
613
|
+
const result = a % b;
|
|
614
|
+
return result >= _0n2 ? result : b + result;
|
|
615
|
+
}
|
|
616
|
+
function pow2(x, power, modulo) {
|
|
617
|
+
let res = x;
|
|
618
|
+
while (power-- > _0n2) {
|
|
619
|
+
res *= res;
|
|
620
|
+
res %= modulo;
|
|
621
|
+
}
|
|
622
|
+
return res;
|
|
623
|
+
}
|
|
624
|
+
function invert(number, modulo) {
|
|
625
|
+
if (number === _0n2)
|
|
626
|
+
throw new Error("invert: expected non-zero number");
|
|
627
|
+
if (modulo <= _0n2)
|
|
628
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
629
|
+
let a = mod(number, modulo);
|
|
630
|
+
let b = modulo;
|
|
631
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
632
|
+
while (a !== _0n2) {
|
|
633
|
+
const q = b / a;
|
|
634
|
+
const r = b % a;
|
|
635
|
+
const m = x - u * q;
|
|
636
|
+
const n = y - v * q;
|
|
637
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
638
|
+
}
|
|
639
|
+
const gcd = b;
|
|
640
|
+
if (gcd !== _1n2)
|
|
641
|
+
throw new Error("invert: does not exist");
|
|
642
|
+
return mod(x, modulo);
|
|
643
|
+
}
|
|
644
|
+
function assertIsSquare(Fp2, root, n) {
|
|
645
|
+
if (!Fp2.eql(Fp2.sqr(root), n))
|
|
646
|
+
throw new Error("Cannot find square root");
|
|
647
|
+
}
|
|
648
|
+
function sqrt3mod4(Fp2, n) {
|
|
649
|
+
const p1div4 = (Fp2.ORDER + _1n2) / _4n;
|
|
650
|
+
const root = Fp2.pow(n, p1div4);
|
|
651
|
+
assertIsSquare(Fp2, root, n);
|
|
652
|
+
return root;
|
|
653
|
+
}
|
|
654
|
+
function sqrt5mod8(Fp2, n) {
|
|
655
|
+
const p5div8 = (Fp2.ORDER - _5n) / _8n;
|
|
656
|
+
const n2 = Fp2.mul(n, _2n);
|
|
657
|
+
const v = Fp2.pow(n2, p5div8);
|
|
658
|
+
const nv = Fp2.mul(n, v);
|
|
659
|
+
const i = Fp2.mul(Fp2.mul(nv, _2n), v);
|
|
660
|
+
const root = Fp2.mul(nv, Fp2.sub(i, Fp2.ONE));
|
|
661
|
+
assertIsSquare(Fp2, root, n);
|
|
662
|
+
return root;
|
|
663
|
+
}
|
|
664
|
+
function sqrt9mod16(P) {
|
|
665
|
+
const Fp_ = Field(P);
|
|
666
|
+
const tn = tonelliShanks(P);
|
|
667
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
668
|
+
const c2 = tn(Fp_, c1);
|
|
669
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
670
|
+
const c4 = (P + _7n) / _16n;
|
|
671
|
+
return (Fp2, n) => {
|
|
672
|
+
let tv1 = Fp2.pow(n, c4);
|
|
673
|
+
let tv2 = Fp2.mul(tv1, c1);
|
|
674
|
+
const tv3 = Fp2.mul(tv1, c2);
|
|
675
|
+
const tv4 = Fp2.mul(tv1, c3);
|
|
676
|
+
const e1 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
677
|
+
const e2 = Fp2.eql(Fp2.sqr(tv3), n);
|
|
678
|
+
tv1 = Fp2.cmov(tv1, tv2, e1);
|
|
679
|
+
tv2 = Fp2.cmov(tv4, tv3, e2);
|
|
680
|
+
const e3 = Fp2.eql(Fp2.sqr(tv2), n);
|
|
681
|
+
const root = Fp2.cmov(tv1, tv2, e3);
|
|
682
|
+
assertIsSquare(Fp2, root, n);
|
|
683
|
+
return root;
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
function tonelliShanks(P) {
|
|
687
|
+
if (P < _3n)
|
|
688
|
+
throw new Error("sqrt is not defined for small field");
|
|
689
|
+
let Q = P - _1n2;
|
|
690
|
+
let S = 0;
|
|
691
|
+
while (Q % _2n === _0n2) {
|
|
692
|
+
Q /= _2n;
|
|
693
|
+
S++;
|
|
694
|
+
}
|
|
695
|
+
let Z = _2n;
|
|
696
|
+
const _Fp = Field(P);
|
|
697
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
698
|
+
if (Z++ > 1e3)
|
|
699
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
700
|
+
}
|
|
701
|
+
if (S === 1)
|
|
702
|
+
return sqrt3mod4;
|
|
703
|
+
let cc = _Fp.pow(Z, Q);
|
|
704
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
705
|
+
return function tonelliSlow(Fp2, n) {
|
|
706
|
+
if (Fp2.is0(n))
|
|
707
|
+
return n;
|
|
708
|
+
if (FpLegendre(Fp2, n) !== 1)
|
|
709
|
+
throw new Error("Cannot find square root");
|
|
710
|
+
let M = S;
|
|
711
|
+
let c = Fp2.mul(Fp2.ONE, cc);
|
|
712
|
+
let t = Fp2.pow(n, Q);
|
|
713
|
+
let R = Fp2.pow(n, Q1div2);
|
|
714
|
+
while (!Fp2.eql(t, Fp2.ONE)) {
|
|
715
|
+
if (Fp2.is0(t))
|
|
716
|
+
return Fp2.ZERO;
|
|
717
|
+
let i = 1;
|
|
718
|
+
let t_tmp = Fp2.sqr(t);
|
|
719
|
+
while (!Fp2.eql(t_tmp, Fp2.ONE)) {
|
|
720
|
+
i++;
|
|
721
|
+
t_tmp = Fp2.sqr(t_tmp);
|
|
722
|
+
if (i === M)
|
|
723
|
+
throw new Error("Cannot find square root");
|
|
724
|
+
}
|
|
725
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
726
|
+
const b = Fp2.pow(c, exponent);
|
|
727
|
+
M = i;
|
|
728
|
+
c = Fp2.sqr(b);
|
|
729
|
+
t = Fp2.mul(t, c);
|
|
730
|
+
R = Fp2.mul(R, b);
|
|
731
|
+
}
|
|
732
|
+
return R;
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function FpSqrt(P) {
|
|
736
|
+
if (P % _4n === _3n)
|
|
737
|
+
return sqrt3mod4;
|
|
738
|
+
if (P % _8n === _5n)
|
|
739
|
+
return sqrt5mod8;
|
|
740
|
+
if (P % _16n === _9n)
|
|
741
|
+
return sqrt9mod16(P);
|
|
742
|
+
return tonelliShanks(P);
|
|
743
|
+
}
|
|
744
|
+
var isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n2) === _1n2;
|
|
745
|
+
var FIELD_FIELDS = [
|
|
746
|
+
"create",
|
|
747
|
+
"isValid",
|
|
748
|
+
"is0",
|
|
749
|
+
"neg",
|
|
750
|
+
"inv",
|
|
751
|
+
"sqrt",
|
|
752
|
+
"sqr",
|
|
753
|
+
"eql",
|
|
754
|
+
"add",
|
|
755
|
+
"sub",
|
|
756
|
+
"mul",
|
|
757
|
+
"pow",
|
|
758
|
+
"div",
|
|
759
|
+
"addN",
|
|
760
|
+
"subN",
|
|
761
|
+
"mulN",
|
|
762
|
+
"sqrN"
|
|
763
|
+
];
|
|
764
|
+
function validateField(field) {
|
|
765
|
+
const initial = {
|
|
766
|
+
ORDER: "bigint",
|
|
767
|
+
MASK: "bigint",
|
|
768
|
+
BYTES: "number",
|
|
769
|
+
BITS: "number"
|
|
770
|
+
};
|
|
771
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
772
|
+
map[val] = "function";
|
|
773
|
+
return map;
|
|
774
|
+
}, initial);
|
|
775
|
+
_validateObject(field, opts);
|
|
776
|
+
return field;
|
|
777
|
+
}
|
|
778
|
+
function FpPow(Fp2, num, power) {
|
|
779
|
+
if (power < _0n2)
|
|
780
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
781
|
+
if (power === _0n2)
|
|
782
|
+
return Fp2.ONE;
|
|
783
|
+
if (power === _1n2)
|
|
784
|
+
return num;
|
|
785
|
+
let p = Fp2.ONE;
|
|
786
|
+
let d = num;
|
|
787
|
+
while (power > _0n2) {
|
|
788
|
+
if (power & _1n2)
|
|
789
|
+
p = Fp2.mul(p, d);
|
|
790
|
+
d = Fp2.sqr(d);
|
|
791
|
+
power >>= _1n2;
|
|
792
|
+
}
|
|
793
|
+
return p;
|
|
794
|
+
}
|
|
795
|
+
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
796
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp2.ZERO : void 0);
|
|
797
|
+
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
798
|
+
if (Fp2.is0(num))
|
|
799
|
+
return acc;
|
|
800
|
+
inverted[i] = acc;
|
|
801
|
+
return Fp2.mul(acc, num);
|
|
802
|
+
}, Fp2.ONE);
|
|
803
|
+
const invertedAcc = Fp2.inv(multipliedAcc);
|
|
804
|
+
nums.reduceRight((acc, num, i) => {
|
|
805
|
+
if (Fp2.is0(num))
|
|
806
|
+
return acc;
|
|
807
|
+
inverted[i] = Fp2.mul(acc, inverted[i]);
|
|
808
|
+
return Fp2.mul(acc, num);
|
|
809
|
+
}, invertedAcc);
|
|
810
|
+
return inverted;
|
|
811
|
+
}
|
|
812
|
+
function FpLegendre(Fp2, n) {
|
|
813
|
+
const p1mod2 = (Fp2.ORDER - _1n2) / _2n;
|
|
814
|
+
const powered = Fp2.pow(n, p1mod2);
|
|
815
|
+
const yes = Fp2.eql(powered, Fp2.ONE);
|
|
816
|
+
const zero = Fp2.eql(powered, Fp2.ZERO);
|
|
817
|
+
const no = Fp2.eql(powered, Fp2.neg(Fp2.ONE));
|
|
818
|
+
if (!yes && !zero && !no)
|
|
819
|
+
throw new Error("invalid Legendre symbol result");
|
|
820
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
821
|
+
}
|
|
822
|
+
function nLength(n, nBitLength) {
|
|
823
|
+
if (nBitLength !== void 0)
|
|
824
|
+
anumber(nBitLength);
|
|
825
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
826
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
827
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
828
|
+
}
|
|
829
|
+
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
830
|
+
if (ORDER <= _0n2)
|
|
831
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
832
|
+
let _nbitLength = void 0;
|
|
833
|
+
let _sqrt = void 0;
|
|
834
|
+
let modFromBytes = false;
|
|
835
|
+
let allowedLengths = void 0;
|
|
836
|
+
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
837
|
+
if (opts.sqrt || isLE)
|
|
838
|
+
throw new Error("cannot specify opts in two arguments");
|
|
839
|
+
const _opts = bitLenOrOpts;
|
|
840
|
+
if (_opts.BITS)
|
|
841
|
+
_nbitLength = _opts.BITS;
|
|
842
|
+
if (_opts.sqrt)
|
|
843
|
+
_sqrt = _opts.sqrt;
|
|
844
|
+
if (typeof _opts.isLE === "boolean")
|
|
845
|
+
isLE = _opts.isLE;
|
|
846
|
+
if (typeof _opts.modFromBytes === "boolean")
|
|
847
|
+
modFromBytes = _opts.modFromBytes;
|
|
848
|
+
allowedLengths = _opts.allowedLengths;
|
|
849
|
+
} else {
|
|
850
|
+
if (typeof bitLenOrOpts === "number")
|
|
851
|
+
_nbitLength = bitLenOrOpts;
|
|
852
|
+
if (opts.sqrt)
|
|
853
|
+
_sqrt = opts.sqrt;
|
|
854
|
+
}
|
|
855
|
+
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
856
|
+
if (BYTES > 2048)
|
|
857
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
858
|
+
let sqrtP;
|
|
859
|
+
const f = Object.freeze({
|
|
860
|
+
ORDER,
|
|
861
|
+
isLE,
|
|
862
|
+
BITS,
|
|
863
|
+
BYTES,
|
|
864
|
+
MASK: bitMask(BITS),
|
|
865
|
+
ZERO: _0n2,
|
|
866
|
+
ONE: _1n2,
|
|
867
|
+
allowedLengths,
|
|
868
|
+
create: (num) => mod(num, ORDER),
|
|
869
|
+
isValid: (num) => {
|
|
870
|
+
if (typeof num !== "bigint")
|
|
871
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
872
|
+
return _0n2 <= num && num < ORDER;
|
|
873
|
+
},
|
|
874
|
+
is0: (num) => num === _0n2,
|
|
875
|
+
// is valid and invertible
|
|
876
|
+
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
877
|
+
isOdd: (num) => (num & _1n2) === _1n2,
|
|
878
|
+
neg: (num) => mod(-num, ORDER),
|
|
879
|
+
eql: (lhs, rhs) => lhs === rhs,
|
|
880
|
+
sqr: (num) => mod(num * num, ORDER),
|
|
881
|
+
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
882
|
+
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
883
|
+
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
884
|
+
pow: (num, power) => FpPow(f, num, power),
|
|
885
|
+
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
886
|
+
// Same as above, but doesn't normalize
|
|
887
|
+
sqrN: (num) => num * num,
|
|
888
|
+
addN: (lhs, rhs) => lhs + rhs,
|
|
889
|
+
subN: (lhs, rhs) => lhs - rhs,
|
|
890
|
+
mulN: (lhs, rhs) => lhs * rhs,
|
|
891
|
+
inv: (num) => invert(num, ORDER),
|
|
892
|
+
sqrt: _sqrt || ((n) => {
|
|
893
|
+
if (!sqrtP)
|
|
894
|
+
sqrtP = FpSqrt(ORDER);
|
|
895
|
+
return sqrtP(f, n);
|
|
896
|
+
}),
|
|
897
|
+
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
898
|
+
fromBytes: (bytes, skipValidation = true) => {
|
|
899
|
+
if (allowedLengths) {
|
|
900
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
901
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
902
|
+
}
|
|
903
|
+
const padded = new Uint8Array(BYTES);
|
|
904
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
905
|
+
bytes = padded;
|
|
906
|
+
}
|
|
907
|
+
if (bytes.length !== BYTES)
|
|
908
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
909
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
910
|
+
if (modFromBytes)
|
|
911
|
+
scalar = mod(scalar, ORDER);
|
|
912
|
+
if (!skipValidation) {
|
|
913
|
+
if (!f.isValid(scalar))
|
|
914
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
915
|
+
}
|
|
916
|
+
return scalar;
|
|
917
|
+
},
|
|
918
|
+
// TODO: we don't need it here, move out to separate fn
|
|
919
|
+
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
920
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
921
|
+
// and it's unclear what to return in there.
|
|
922
|
+
cmov: (a, b, c) => c ? b : a
|
|
923
|
+
});
|
|
924
|
+
return Object.freeze(f);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// node_modules/@noble/curves/esm/abstract/curve.js
|
|
928
|
+
var _0n3 = BigInt(0);
|
|
929
|
+
var _1n3 = BigInt(1);
|
|
930
|
+
function negateCt(condition, item) {
|
|
931
|
+
const neg = item.negate();
|
|
932
|
+
return condition ? neg : item;
|
|
933
|
+
}
|
|
934
|
+
function normalizeZ(c, points) {
|
|
935
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
936
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
937
|
+
}
|
|
938
|
+
function validateW(W, bits) {
|
|
939
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
940
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
941
|
+
}
|
|
942
|
+
function calcWOpts(W, scalarBits) {
|
|
943
|
+
validateW(W, scalarBits);
|
|
944
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
945
|
+
const windowSize = 2 ** (W - 1);
|
|
946
|
+
const maxNumber = 2 ** W;
|
|
947
|
+
const mask = bitMask(W);
|
|
948
|
+
const shiftBy = BigInt(W);
|
|
949
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
950
|
+
}
|
|
951
|
+
function calcOffsets(n, window, wOpts) {
|
|
952
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
953
|
+
let wbits = Number(n & mask);
|
|
954
|
+
let nextN = n >> shiftBy;
|
|
955
|
+
if (wbits > windowSize) {
|
|
956
|
+
wbits -= maxNumber;
|
|
957
|
+
nextN += _1n3;
|
|
958
|
+
}
|
|
959
|
+
const offsetStart = window * windowSize;
|
|
960
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
961
|
+
const isZero = wbits === 0;
|
|
962
|
+
const isNeg = wbits < 0;
|
|
963
|
+
const isNegF = window % 2 !== 0;
|
|
964
|
+
const offsetF = offsetStart;
|
|
965
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
966
|
+
}
|
|
967
|
+
function validateMSMPoints(points, c) {
|
|
968
|
+
if (!Array.isArray(points))
|
|
969
|
+
throw new Error("array expected");
|
|
970
|
+
points.forEach((p, i) => {
|
|
971
|
+
if (!(p instanceof c))
|
|
972
|
+
throw new Error("invalid point at index " + i);
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
function validateMSMScalars(scalars, field) {
|
|
976
|
+
if (!Array.isArray(scalars))
|
|
977
|
+
throw new Error("array of scalars expected");
|
|
978
|
+
scalars.forEach((s, i) => {
|
|
979
|
+
if (!field.isValid(s))
|
|
980
|
+
throw new Error("invalid scalar at index " + i);
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
984
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
985
|
+
function getW(P) {
|
|
986
|
+
return pointWindowSizes.get(P) || 1;
|
|
987
|
+
}
|
|
988
|
+
function assert0(n) {
|
|
989
|
+
if (n !== _0n3)
|
|
990
|
+
throw new Error("invalid wNAF");
|
|
991
|
+
}
|
|
992
|
+
var wNAF = class {
|
|
993
|
+
// Parametrized with a given Point class (not individual point)
|
|
994
|
+
constructor(Point, bits) {
|
|
995
|
+
this.BASE = Point.BASE;
|
|
996
|
+
this.ZERO = Point.ZERO;
|
|
997
|
+
this.Fn = Point.Fn;
|
|
998
|
+
this.bits = bits;
|
|
999
|
+
}
|
|
1000
|
+
// non-const time multiplication ladder
|
|
1001
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
1002
|
+
let d = elm;
|
|
1003
|
+
while (n > _0n3) {
|
|
1004
|
+
if (n & _1n3)
|
|
1005
|
+
p = p.add(d);
|
|
1006
|
+
d = d.double();
|
|
1007
|
+
n >>= _1n3;
|
|
1008
|
+
}
|
|
1009
|
+
return p;
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
1013
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
1014
|
+
* Number of precomputed points depends on the curve size:
|
|
1015
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
1016
|
+
* - 𝑊 is the window size
|
|
1017
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
1018
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
1019
|
+
* @param point Point instance
|
|
1020
|
+
* @param W window size
|
|
1021
|
+
* @returns precomputed point tables flattened to a single array
|
|
1022
|
+
*/
|
|
1023
|
+
precomputeWindow(point, W) {
|
|
1024
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
1025
|
+
const points = [];
|
|
1026
|
+
let p = point;
|
|
1027
|
+
let base = p;
|
|
1028
|
+
for (let window = 0; window < windows; window++) {
|
|
1029
|
+
base = p;
|
|
1030
|
+
points.push(base);
|
|
1031
|
+
for (let i = 1; i < windowSize; i++) {
|
|
1032
|
+
base = base.add(p);
|
|
1033
|
+
points.push(base);
|
|
1034
|
+
}
|
|
1035
|
+
p = base.double();
|
|
1036
|
+
}
|
|
1037
|
+
return points;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1041
|
+
* More compact implementation:
|
|
1042
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
1043
|
+
* @returns real and fake (for const-time) points
|
|
1044
|
+
*/
|
|
1045
|
+
wNAF(W, precomputes, n) {
|
|
1046
|
+
if (!this.Fn.isValid(n))
|
|
1047
|
+
throw new Error("invalid scalar");
|
|
1048
|
+
let p = this.ZERO;
|
|
1049
|
+
let f = this.BASE;
|
|
1050
|
+
const wo = calcWOpts(W, this.bits);
|
|
1051
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1052
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
1053
|
+
n = nextN;
|
|
1054
|
+
if (isZero) {
|
|
1055
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
1056
|
+
} else {
|
|
1057
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
assert0(n);
|
|
1061
|
+
return { p, f };
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1065
|
+
* @param acc accumulator point to add result of multiplication
|
|
1066
|
+
* @returns point
|
|
1067
|
+
*/
|
|
1068
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
1069
|
+
const wo = calcWOpts(W, this.bits);
|
|
1070
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1071
|
+
if (n === _0n3)
|
|
1072
|
+
break;
|
|
1073
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1074
|
+
n = nextN;
|
|
1075
|
+
if (isZero) {
|
|
1076
|
+
continue;
|
|
1077
|
+
} else {
|
|
1078
|
+
const item = precomputes[offset];
|
|
1079
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
assert0(n);
|
|
1083
|
+
return acc;
|
|
1084
|
+
}
|
|
1085
|
+
getPrecomputes(W, point, transform) {
|
|
1086
|
+
let comp = pointPrecomputes.get(point);
|
|
1087
|
+
if (!comp) {
|
|
1088
|
+
comp = this.precomputeWindow(point, W);
|
|
1089
|
+
if (W !== 1) {
|
|
1090
|
+
if (typeof transform === "function")
|
|
1091
|
+
comp = transform(comp);
|
|
1092
|
+
pointPrecomputes.set(point, comp);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return comp;
|
|
1096
|
+
}
|
|
1097
|
+
cached(point, scalar, transform) {
|
|
1098
|
+
const W = getW(point);
|
|
1099
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
1100
|
+
}
|
|
1101
|
+
unsafe(point, scalar, transform, prev) {
|
|
1102
|
+
const W = getW(point);
|
|
1103
|
+
if (W === 1)
|
|
1104
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
1105
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
1106
|
+
}
|
|
1107
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
1108
|
+
// using windowed method. This specifies window size and
|
|
1109
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
1110
|
+
createCache(P, W) {
|
|
1111
|
+
validateW(W, this.bits);
|
|
1112
|
+
pointWindowSizes.set(P, W);
|
|
1113
|
+
pointPrecomputes.delete(P);
|
|
1114
|
+
}
|
|
1115
|
+
hasCache(elm) {
|
|
1116
|
+
return getW(elm) !== 1;
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
1120
|
+
validateMSMPoints(points, c);
|
|
1121
|
+
validateMSMScalars(scalars, fieldN);
|
|
1122
|
+
const plength = points.length;
|
|
1123
|
+
const slength = scalars.length;
|
|
1124
|
+
if (plength !== slength)
|
|
1125
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
1126
|
+
const zero = c.ZERO;
|
|
1127
|
+
const wbits = bitLen(BigInt(plength));
|
|
1128
|
+
let windowSize = 1;
|
|
1129
|
+
if (wbits > 12)
|
|
1130
|
+
windowSize = wbits - 3;
|
|
1131
|
+
else if (wbits > 4)
|
|
1132
|
+
windowSize = wbits - 2;
|
|
1133
|
+
else if (wbits > 0)
|
|
1134
|
+
windowSize = 2;
|
|
1135
|
+
const MASK = bitMask(windowSize);
|
|
1136
|
+
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
|
1137
|
+
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
1138
|
+
let sum = zero;
|
|
1139
|
+
for (let i = lastBits; i >= 0; i -= windowSize) {
|
|
1140
|
+
buckets.fill(zero);
|
|
1141
|
+
for (let j = 0; j < slength; j++) {
|
|
1142
|
+
const scalar = scalars[j];
|
|
1143
|
+
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
|
1144
|
+
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
|
1145
|
+
}
|
|
1146
|
+
let resI = zero;
|
|
1147
|
+
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
1148
|
+
sumI = sumI.add(buckets[j]);
|
|
1149
|
+
resI = resI.add(sumI);
|
|
1150
|
+
}
|
|
1151
|
+
sum = sum.add(resI);
|
|
1152
|
+
if (i !== 0)
|
|
1153
|
+
for (let j = 0; j < windowSize; j++)
|
|
1154
|
+
sum = sum.double();
|
|
1155
|
+
}
|
|
1156
|
+
return sum;
|
|
1157
|
+
}
|
|
1158
|
+
function createField(order, field, isLE) {
|
|
1159
|
+
if (field) {
|
|
1160
|
+
if (field.ORDER !== order)
|
|
1161
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
1162
|
+
validateField(field);
|
|
1163
|
+
return field;
|
|
1164
|
+
} else {
|
|
1165
|
+
return Field(order, { isLE });
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
1169
|
+
if (FpFnLE === void 0)
|
|
1170
|
+
FpFnLE = type === "edwards";
|
|
1171
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
1172
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
1173
|
+
for (const p of ["p", "n", "h"]) {
|
|
1174
|
+
const val = CURVE[p];
|
|
1175
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
1176
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
1177
|
+
}
|
|
1178
|
+
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
1179
|
+
const Fn2 = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
1180
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
1181
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
1182
|
+
for (const p of params) {
|
|
1183
|
+
if (!Fp2.isValid(CURVE[p]))
|
|
1184
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
1185
|
+
}
|
|
1186
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
1187
|
+
return { CURVE, Fp: Fp2, Fn: Fn2 };
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// node_modules/@noble/curves/esm/abstract/edwards.js
|
|
1191
|
+
var _0n4 = BigInt(0);
|
|
1192
|
+
var _1n4 = BigInt(1);
|
|
1193
|
+
var _2n2 = BigInt(2);
|
|
1194
|
+
var _8n2 = BigInt(8);
|
|
1195
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
1196
|
+
const x2 = Fp2.sqr(x);
|
|
1197
|
+
const y2 = Fp2.sqr(y);
|
|
1198
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
1199
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
1200
|
+
return Fp2.eql(left, right);
|
|
1201
|
+
}
|
|
1202
|
+
function edwards(params, extraOpts = {}) {
|
|
1203
|
+
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
1204
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
1205
|
+
let CURVE = validated.CURVE;
|
|
1206
|
+
const { h: cofactor } = CURVE;
|
|
1207
|
+
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
1208
|
+
const MASK = _2n2 << BigInt(Fn2.BYTES * 8) - _1n4;
|
|
1209
|
+
const modP = (n) => Fp2.create(n);
|
|
1210
|
+
const uvRatio2 = extraOpts.uvRatio || ((u, v) => {
|
|
1211
|
+
try {
|
|
1212
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
1213
|
+
} catch (e) {
|
|
1214
|
+
return { isValid: false, value: _0n4 };
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
1218
|
+
throw new Error("bad curve params: generator point");
|
|
1219
|
+
function acoord(title, n, banZero = false) {
|
|
1220
|
+
const min = banZero ? _1n4 : _0n4;
|
|
1221
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
1222
|
+
return n;
|
|
1223
|
+
}
|
|
1224
|
+
function aextpoint(other) {
|
|
1225
|
+
if (!(other instanceof Point))
|
|
1226
|
+
throw new Error("ExtendedPoint expected");
|
|
1227
|
+
}
|
|
1228
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
1229
|
+
const { X, Y, Z } = p;
|
|
1230
|
+
const is0 = p.is0();
|
|
1231
|
+
if (iz == null)
|
|
1232
|
+
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
1233
|
+
const x = modP(X * iz);
|
|
1234
|
+
const y = modP(Y * iz);
|
|
1235
|
+
const zz = Fp2.mul(Z, iz);
|
|
1236
|
+
if (is0)
|
|
1237
|
+
return { x: _0n4, y: _1n4 };
|
|
1238
|
+
if (zz !== _1n4)
|
|
1239
|
+
throw new Error("invZ was invalid");
|
|
1240
|
+
return { x, y };
|
|
1241
|
+
});
|
|
1242
|
+
const assertValidMemo = memoized((p) => {
|
|
1243
|
+
const { a, d } = CURVE;
|
|
1244
|
+
if (p.is0())
|
|
1245
|
+
throw new Error("bad point: ZERO");
|
|
1246
|
+
const { X, Y, Z, T } = p;
|
|
1247
|
+
const X2 = modP(X * X);
|
|
1248
|
+
const Y2 = modP(Y * Y);
|
|
1249
|
+
const Z2 = modP(Z * Z);
|
|
1250
|
+
const Z4 = modP(Z2 * Z2);
|
|
1251
|
+
const aX2 = modP(X2 * a);
|
|
1252
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
1253
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
1254
|
+
if (left !== right)
|
|
1255
|
+
throw new Error("bad point: equation left != right (1)");
|
|
1256
|
+
const XY = modP(X * Y);
|
|
1257
|
+
const ZT = modP(Z * T);
|
|
1258
|
+
if (XY !== ZT)
|
|
1259
|
+
throw new Error("bad point: equation left != right (2)");
|
|
1260
|
+
return true;
|
|
1261
|
+
});
|
|
1262
|
+
class Point {
|
|
1263
|
+
constructor(X, Y, Z, T) {
|
|
1264
|
+
this.X = acoord("x", X);
|
|
1265
|
+
this.Y = acoord("y", Y);
|
|
1266
|
+
this.Z = acoord("z", Z, true);
|
|
1267
|
+
this.T = acoord("t", T);
|
|
1268
|
+
Object.freeze(this);
|
|
1269
|
+
}
|
|
1270
|
+
static CURVE() {
|
|
1271
|
+
return CURVE;
|
|
1272
|
+
}
|
|
1273
|
+
static fromAffine(p) {
|
|
1274
|
+
if (p instanceof Point)
|
|
1275
|
+
throw new Error("extended point not allowed");
|
|
1276
|
+
const { x, y } = p || {};
|
|
1277
|
+
acoord("x", x);
|
|
1278
|
+
acoord("y", y);
|
|
1279
|
+
return new Point(x, y, _1n4, modP(x * y));
|
|
1280
|
+
}
|
|
1281
|
+
// Uses algo from RFC8032 5.1.3.
|
|
1282
|
+
static fromBytes(bytes, zip215 = false) {
|
|
1283
|
+
const len = Fp2.BYTES;
|
|
1284
|
+
const { a, d } = CURVE;
|
|
1285
|
+
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
1286
|
+
_abool2(zip215, "zip215");
|
|
1287
|
+
const normed = copyBytes(bytes);
|
|
1288
|
+
const lastByte = bytes[len - 1];
|
|
1289
|
+
normed[len - 1] = lastByte & ~128;
|
|
1290
|
+
const y = bytesToNumberLE(normed);
|
|
1291
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
1292
|
+
aInRange("point.y", y, _0n4, max);
|
|
1293
|
+
const y2 = modP(y * y);
|
|
1294
|
+
const u = modP(y2 - _1n4);
|
|
1295
|
+
const v = modP(d * y2 - a);
|
|
1296
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
1297
|
+
if (!isValid)
|
|
1298
|
+
throw new Error("bad point: invalid y coordinate");
|
|
1299
|
+
const isXOdd = (x & _1n4) === _1n4;
|
|
1300
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1301
|
+
if (!zip215 && x === _0n4 && isLastByteOdd)
|
|
1302
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
1303
|
+
if (isLastByteOdd !== isXOdd)
|
|
1304
|
+
x = modP(-x);
|
|
1305
|
+
return Point.fromAffine({ x, y });
|
|
1306
|
+
}
|
|
1307
|
+
static fromHex(bytes, zip215 = false) {
|
|
1308
|
+
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
1309
|
+
}
|
|
1310
|
+
get x() {
|
|
1311
|
+
return this.toAffine().x;
|
|
1312
|
+
}
|
|
1313
|
+
get y() {
|
|
1314
|
+
return this.toAffine().y;
|
|
1315
|
+
}
|
|
1316
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
1317
|
+
wnaf.createCache(this, windowSize);
|
|
1318
|
+
if (!isLazy)
|
|
1319
|
+
this.multiply(_2n2);
|
|
1320
|
+
return this;
|
|
1321
|
+
}
|
|
1322
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
1323
|
+
assertValidity() {
|
|
1324
|
+
assertValidMemo(this);
|
|
1325
|
+
}
|
|
1326
|
+
// Compare one point to another.
|
|
1327
|
+
equals(other) {
|
|
1328
|
+
aextpoint(other);
|
|
1329
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1330
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1331
|
+
const X1Z2 = modP(X1 * Z2);
|
|
1332
|
+
const X2Z1 = modP(X2 * Z1);
|
|
1333
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
1334
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
1335
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
1336
|
+
}
|
|
1337
|
+
is0() {
|
|
1338
|
+
return this.equals(Point.ZERO);
|
|
1339
|
+
}
|
|
1340
|
+
negate() {
|
|
1341
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
1342
|
+
}
|
|
1343
|
+
// Fast algo for doubling Extended Point.
|
|
1344
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1345
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1346
|
+
double() {
|
|
1347
|
+
const { a } = CURVE;
|
|
1348
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1349
|
+
const A = modP(X1 * X1);
|
|
1350
|
+
const B = modP(Y1 * Y1);
|
|
1351
|
+
const C = modP(_2n2 * modP(Z1 * Z1));
|
|
1352
|
+
const D = modP(a * A);
|
|
1353
|
+
const x1y1 = X1 + Y1;
|
|
1354
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
1355
|
+
const G = D + B;
|
|
1356
|
+
const F = G - C;
|
|
1357
|
+
const H = D - B;
|
|
1358
|
+
const X3 = modP(E * F);
|
|
1359
|
+
const Y3 = modP(G * H);
|
|
1360
|
+
const T3 = modP(E * H);
|
|
1361
|
+
const Z3 = modP(F * G);
|
|
1362
|
+
return new Point(X3, Y3, Z3, T3);
|
|
1363
|
+
}
|
|
1364
|
+
// Fast algo for adding 2 Extended Points.
|
|
1365
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
1366
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1367
|
+
add(other) {
|
|
1368
|
+
aextpoint(other);
|
|
1369
|
+
const { a, d } = CURVE;
|
|
1370
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1371
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1372
|
+
const A = modP(X1 * X2);
|
|
1373
|
+
const B = modP(Y1 * Y2);
|
|
1374
|
+
const C = modP(T1 * d * T2);
|
|
1375
|
+
const D = modP(Z1 * Z2);
|
|
1376
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
1377
|
+
const F = D - C;
|
|
1378
|
+
const G = D + C;
|
|
1379
|
+
const H = modP(B - a * A);
|
|
1380
|
+
const X3 = modP(E * F);
|
|
1381
|
+
const Y3 = modP(G * H);
|
|
1382
|
+
const T3 = modP(E * H);
|
|
1383
|
+
const Z3 = modP(F * G);
|
|
1384
|
+
return new Point(X3, Y3, Z3, T3);
|
|
1385
|
+
}
|
|
1386
|
+
subtract(other) {
|
|
1387
|
+
return this.add(other.negate());
|
|
1388
|
+
}
|
|
1389
|
+
// Constant-time multiplication.
|
|
1390
|
+
multiply(scalar) {
|
|
1391
|
+
if (!Fn2.isValidNot0(scalar))
|
|
1392
|
+
throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
1393
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
1394
|
+
return normalizeZ(Point, [p, f])[0];
|
|
1395
|
+
}
|
|
1396
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1397
|
+
// It's faster, but should only be used when you don't care about
|
|
1398
|
+
// an exposed private key e.g. sig verification.
|
|
1399
|
+
// Does NOT allow scalars higher than CURVE.n.
|
|
1400
|
+
// Accepts optional accumulator to merge with multiply (important for sparse scalars)
|
|
1401
|
+
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
1402
|
+
if (!Fn2.isValid(scalar))
|
|
1403
|
+
throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
1404
|
+
if (scalar === _0n4)
|
|
1405
|
+
return Point.ZERO;
|
|
1406
|
+
if (this.is0() || scalar === _1n4)
|
|
1407
|
+
return this;
|
|
1408
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
1409
|
+
}
|
|
1410
|
+
// Checks if point is of small order.
|
|
1411
|
+
// If you add something to small order point, you will have "dirty"
|
|
1412
|
+
// point with torsion component.
|
|
1413
|
+
// Multiplies point by cofactor and checks if the result is 0.
|
|
1414
|
+
isSmallOrder() {
|
|
1415
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
1416
|
+
}
|
|
1417
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
1418
|
+
// Returns `false` is the point is dirty.
|
|
1419
|
+
isTorsionFree() {
|
|
1420
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
1421
|
+
}
|
|
1422
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
1423
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1424
|
+
toAffine(invertedZ) {
|
|
1425
|
+
return toAffineMemo(this, invertedZ);
|
|
1426
|
+
}
|
|
1427
|
+
clearCofactor() {
|
|
1428
|
+
if (cofactor === _1n4)
|
|
1429
|
+
return this;
|
|
1430
|
+
return this.multiplyUnsafe(cofactor);
|
|
1431
|
+
}
|
|
1432
|
+
toBytes() {
|
|
1433
|
+
const { x, y } = this.toAffine();
|
|
1434
|
+
const bytes = Fp2.toBytes(y);
|
|
1435
|
+
bytes[bytes.length - 1] |= x & _1n4 ? 128 : 0;
|
|
1436
|
+
return bytes;
|
|
1437
|
+
}
|
|
1438
|
+
toHex() {
|
|
1439
|
+
return bytesToHex(this.toBytes());
|
|
1440
|
+
}
|
|
1441
|
+
toString() {
|
|
1442
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1443
|
+
}
|
|
1444
|
+
// TODO: remove
|
|
1445
|
+
get ex() {
|
|
1446
|
+
return this.X;
|
|
1447
|
+
}
|
|
1448
|
+
get ey() {
|
|
1449
|
+
return this.Y;
|
|
1450
|
+
}
|
|
1451
|
+
get ez() {
|
|
1452
|
+
return this.Z;
|
|
1453
|
+
}
|
|
1454
|
+
get et() {
|
|
1455
|
+
return this.T;
|
|
1456
|
+
}
|
|
1457
|
+
static normalizeZ(points) {
|
|
1458
|
+
return normalizeZ(Point, points);
|
|
1459
|
+
}
|
|
1460
|
+
static msm(points, scalars) {
|
|
1461
|
+
return pippenger(Point, Fn2, points, scalars);
|
|
1462
|
+
}
|
|
1463
|
+
_setWindowSize(windowSize) {
|
|
1464
|
+
this.precompute(windowSize);
|
|
1465
|
+
}
|
|
1466
|
+
toRawBytes() {
|
|
1467
|
+
return this.toBytes();
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n4, modP(CURVE.Gx * CURVE.Gy));
|
|
1471
|
+
Point.ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
1472
|
+
Point.Fp = Fp2;
|
|
1473
|
+
Point.Fn = Fn2;
|
|
1474
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
1475
|
+
Point.BASE.precompute(8);
|
|
1476
|
+
return Point;
|
|
1477
|
+
}
|
|
1478
|
+
var PrimeEdwardsPoint = class {
|
|
1479
|
+
constructor(ep) {
|
|
1480
|
+
this.ep = ep;
|
|
1481
|
+
}
|
|
1482
|
+
// Static methods that must be implemented by subclasses
|
|
1483
|
+
static fromBytes(_bytes) {
|
|
1484
|
+
notImplemented();
|
|
1485
|
+
}
|
|
1486
|
+
static fromHex(_hex) {
|
|
1487
|
+
notImplemented();
|
|
1488
|
+
}
|
|
1489
|
+
get x() {
|
|
1490
|
+
return this.toAffine().x;
|
|
1491
|
+
}
|
|
1492
|
+
get y() {
|
|
1493
|
+
return this.toAffine().y;
|
|
1494
|
+
}
|
|
1495
|
+
// Common implementations
|
|
1496
|
+
clearCofactor() {
|
|
1497
|
+
return this;
|
|
1498
|
+
}
|
|
1499
|
+
assertValidity() {
|
|
1500
|
+
this.ep.assertValidity();
|
|
1501
|
+
}
|
|
1502
|
+
toAffine(invertedZ) {
|
|
1503
|
+
return this.ep.toAffine(invertedZ);
|
|
1504
|
+
}
|
|
1505
|
+
toHex() {
|
|
1506
|
+
return bytesToHex(this.toBytes());
|
|
1507
|
+
}
|
|
1508
|
+
toString() {
|
|
1509
|
+
return this.toHex();
|
|
1510
|
+
}
|
|
1511
|
+
isTorsionFree() {
|
|
1512
|
+
return true;
|
|
1513
|
+
}
|
|
1514
|
+
isSmallOrder() {
|
|
1515
|
+
return false;
|
|
1516
|
+
}
|
|
1517
|
+
add(other) {
|
|
1518
|
+
this.assertSame(other);
|
|
1519
|
+
return this.init(this.ep.add(other.ep));
|
|
1520
|
+
}
|
|
1521
|
+
subtract(other) {
|
|
1522
|
+
this.assertSame(other);
|
|
1523
|
+
return this.init(this.ep.subtract(other.ep));
|
|
1524
|
+
}
|
|
1525
|
+
multiply(scalar) {
|
|
1526
|
+
return this.init(this.ep.multiply(scalar));
|
|
1527
|
+
}
|
|
1528
|
+
multiplyUnsafe(scalar) {
|
|
1529
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
1530
|
+
}
|
|
1531
|
+
double() {
|
|
1532
|
+
return this.init(this.ep.double());
|
|
1533
|
+
}
|
|
1534
|
+
negate() {
|
|
1535
|
+
return this.init(this.ep.negate());
|
|
1536
|
+
}
|
|
1537
|
+
precompute(windowSize, isLazy) {
|
|
1538
|
+
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
1539
|
+
}
|
|
1540
|
+
/** @deprecated use `toBytes` */
|
|
1541
|
+
toRawBytes() {
|
|
1542
|
+
return this.toBytes();
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
1546
|
+
if (typeof cHash !== "function")
|
|
1547
|
+
throw new Error('"hash" function param is required');
|
|
1548
|
+
_validateObject(eddsaOpts, {}, {
|
|
1549
|
+
adjustScalarBytes: "function",
|
|
1550
|
+
randomBytes: "function",
|
|
1551
|
+
domain: "function",
|
|
1552
|
+
prehash: "function",
|
|
1553
|
+
mapToCurve: "function"
|
|
1554
|
+
});
|
|
1555
|
+
const { prehash } = eddsaOpts;
|
|
1556
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
1557
|
+
const randomBytes2 = eddsaOpts.randomBytes || randomBytes;
|
|
1558
|
+
const adjustScalarBytes2 = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
1559
|
+
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
1560
|
+
_abool2(phflag, "phflag");
|
|
1561
|
+
if (ctx.length || phflag)
|
|
1562
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
1563
|
+
return data;
|
|
1564
|
+
});
|
|
1565
|
+
function modN_LE(hash) {
|
|
1566
|
+
return Fn2.create(bytesToNumberLE(hash));
|
|
1567
|
+
}
|
|
1568
|
+
function getPrivateScalar(key) {
|
|
1569
|
+
const len = lengths.secretKey;
|
|
1570
|
+
key = ensureBytes("private key", key, len);
|
|
1571
|
+
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
1572
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
1573
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
1574
|
+
const scalar = modN_LE(head);
|
|
1575
|
+
return { head, prefix, scalar };
|
|
1576
|
+
}
|
|
1577
|
+
function getExtendedPublicKey(secretKey) {
|
|
1578
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
1579
|
+
const point = BASE.multiply(scalar);
|
|
1580
|
+
const pointBytes = point.toBytes();
|
|
1581
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
1582
|
+
}
|
|
1583
|
+
function getPublicKey(secretKey) {
|
|
1584
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
1585
|
+
}
|
|
1586
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
1587
|
+
const msg = concatBytes(...msgs);
|
|
1588
|
+
return modN_LE(cHash(domain(msg, ensureBytes("context", context), !!prehash)));
|
|
1589
|
+
}
|
|
1590
|
+
function sign(msg, secretKey, options = {}) {
|
|
1591
|
+
msg = ensureBytes("message", msg);
|
|
1592
|
+
if (prehash)
|
|
1593
|
+
msg = prehash(msg);
|
|
1594
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
1595
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
1596
|
+
const R = BASE.multiply(r).toBytes();
|
|
1597
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
1598
|
+
const s = Fn2.create(r + k * scalar);
|
|
1599
|
+
if (!Fn2.isValid(s))
|
|
1600
|
+
throw new Error("sign failed: invalid s");
|
|
1601
|
+
const rs = concatBytes(R, Fn2.toBytes(s));
|
|
1602
|
+
return _abytes2(rs, lengths.signature, "result");
|
|
1603
|
+
}
|
|
1604
|
+
const verifyOpts = { zip215: true };
|
|
1605
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
1606
|
+
const { context, zip215 } = options;
|
|
1607
|
+
const len = lengths.signature;
|
|
1608
|
+
sig = ensureBytes("signature", sig, len);
|
|
1609
|
+
msg = ensureBytes("message", msg);
|
|
1610
|
+
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
1611
|
+
if (zip215 !== void 0)
|
|
1612
|
+
_abool2(zip215, "zip215");
|
|
1613
|
+
if (prehash)
|
|
1614
|
+
msg = prehash(msg);
|
|
1615
|
+
const mid = len / 2;
|
|
1616
|
+
const r = sig.subarray(0, mid);
|
|
1617
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
1618
|
+
let A, R, SB;
|
|
1619
|
+
try {
|
|
1620
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
1621
|
+
R = Point.fromBytes(r, zip215);
|
|
1622
|
+
SB = BASE.multiplyUnsafe(s);
|
|
1623
|
+
} catch (error) {
|
|
1624
|
+
return false;
|
|
1625
|
+
}
|
|
1626
|
+
if (!zip215 && A.isSmallOrder())
|
|
1627
|
+
return false;
|
|
1628
|
+
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
1629
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
1630
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
1631
|
+
}
|
|
1632
|
+
const _size = Fp2.BYTES;
|
|
1633
|
+
const lengths = {
|
|
1634
|
+
secretKey: _size,
|
|
1635
|
+
publicKey: _size,
|
|
1636
|
+
signature: 2 * _size,
|
|
1637
|
+
seed: _size
|
|
1638
|
+
};
|
|
1639
|
+
function randomSecretKey(seed = randomBytes2(lengths.seed)) {
|
|
1640
|
+
return _abytes2(seed, lengths.seed, "seed");
|
|
1641
|
+
}
|
|
1642
|
+
function keygen(seed) {
|
|
1643
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
1644
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
1645
|
+
}
|
|
1646
|
+
function isValidSecretKey(key) {
|
|
1647
|
+
return isBytes(key) && key.length === Fn2.BYTES;
|
|
1648
|
+
}
|
|
1649
|
+
function isValidPublicKey(key, zip215) {
|
|
1650
|
+
try {
|
|
1651
|
+
return !!Point.fromBytes(key, zip215);
|
|
1652
|
+
} catch (error) {
|
|
1653
|
+
return false;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
const utils = {
|
|
1657
|
+
getExtendedPublicKey,
|
|
1658
|
+
randomSecretKey,
|
|
1659
|
+
isValidSecretKey,
|
|
1660
|
+
isValidPublicKey,
|
|
1661
|
+
/**
|
|
1662
|
+
* Converts ed public key to x public key. Uses formula:
|
|
1663
|
+
* - ed25519:
|
|
1664
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
1665
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
1666
|
+
* - ed448:
|
|
1667
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
1668
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
1669
|
+
*/
|
|
1670
|
+
toMontgomery(publicKey) {
|
|
1671
|
+
const { y } = Point.fromBytes(publicKey);
|
|
1672
|
+
const size = lengths.publicKey;
|
|
1673
|
+
const is25519 = size === 32;
|
|
1674
|
+
if (!is25519 && size !== 57)
|
|
1675
|
+
throw new Error("only defined for 25519 and 448");
|
|
1676
|
+
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
1677
|
+
return Fp2.toBytes(u);
|
|
1678
|
+
},
|
|
1679
|
+
toMontgomerySecret(secretKey) {
|
|
1680
|
+
const size = lengths.secretKey;
|
|
1681
|
+
_abytes2(secretKey, size);
|
|
1682
|
+
const hashed = cHash(secretKey.subarray(0, size));
|
|
1683
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
1684
|
+
},
|
|
1685
|
+
/** @deprecated */
|
|
1686
|
+
randomPrivateKey: randomSecretKey,
|
|
1687
|
+
/** @deprecated */
|
|
1688
|
+
precompute(windowSize = 8, point = Point.BASE) {
|
|
1689
|
+
return point.precompute(windowSize, false);
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
return Object.freeze({
|
|
1693
|
+
keygen,
|
|
1694
|
+
getPublicKey,
|
|
1695
|
+
sign,
|
|
1696
|
+
verify,
|
|
1697
|
+
utils,
|
|
1698
|
+
Point,
|
|
1699
|
+
lengths
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
function _eddsa_legacy_opts_to_new(c) {
|
|
1703
|
+
const CURVE = {
|
|
1704
|
+
a: c.a,
|
|
1705
|
+
d: c.d,
|
|
1706
|
+
p: c.Fp.ORDER,
|
|
1707
|
+
n: c.n,
|
|
1708
|
+
h: c.h,
|
|
1709
|
+
Gx: c.Gx,
|
|
1710
|
+
Gy: c.Gy
|
|
1711
|
+
};
|
|
1712
|
+
const Fp2 = c.Fp;
|
|
1713
|
+
const Fn2 = Field(CURVE.n, c.nBitLength, true);
|
|
1714
|
+
const curveOpts = { Fp: Fp2, Fn: Fn2, uvRatio: c.uvRatio };
|
|
1715
|
+
const eddsaOpts = {
|
|
1716
|
+
randomBytes: c.randomBytes,
|
|
1717
|
+
adjustScalarBytes: c.adjustScalarBytes,
|
|
1718
|
+
domain: c.domain,
|
|
1719
|
+
prehash: c.prehash,
|
|
1720
|
+
mapToCurve: c.mapToCurve
|
|
1721
|
+
};
|
|
1722
|
+
return { CURVE, curveOpts, hash: c.hash, eddsaOpts };
|
|
1723
|
+
}
|
|
1724
|
+
function _eddsa_new_output_to_legacy(c, eddsa2) {
|
|
1725
|
+
const Point = eddsa2.Point;
|
|
1726
|
+
const legacy = Object.assign({}, eddsa2, {
|
|
1727
|
+
ExtendedPoint: Point,
|
|
1728
|
+
CURVE: c,
|
|
1729
|
+
nBitLength: Point.Fn.BITS,
|
|
1730
|
+
nByteLength: Point.Fn.BYTES
|
|
1731
|
+
});
|
|
1732
|
+
return legacy;
|
|
1733
|
+
}
|
|
1734
|
+
function twistedEdwards(c) {
|
|
1735
|
+
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
1736
|
+
const Point = edwards(CURVE, curveOpts);
|
|
1737
|
+
const EDDSA = eddsa(Point, hash, eddsaOpts);
|
|
1738
|
+
return _eddsa_new_output_to_legacy(c, EDDSA);
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
// node_modules/@noble/curves/esm/ed25519.js
|
|
1742
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
1743
|
+
var _1n5 = BigInt(1);
|
|
1744
|
+
var _2n3 = BigInt(2);
|
|
1745
|
+
var _3n2 = BigInt(3);
|
|
1746
|
+
var _5n2 = BigInt(5);
|
|
1747
|
+
var _8n3 = BigInt(8);
|
|
1748
|
+
var ed25519_CURVE_p = BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
1749
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
1750
|
+
p: ed25519_CURVE_p,
|
|
1751
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
1752
|
+
h: _8n3,
|
|
1753
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
1754
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
1755
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
1756
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
1757
|
+
}))();
|
|
1758
|
+
function ed25519_pow_2_252_3(x) {
|
|
1759
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
1760
|
+
const P = ed25519_CURVE_p;
|
|
1761
|
+
const x2 = x * x % P;
|
|
1762
|
+
const b2 = x2 * x % P;
|
|
1763
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
1764
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
1765
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
1766
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
1767
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
1768
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
1769
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
1770
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
1771
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
1772
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
1773
|
+
return { pow_p_5_8, b2 };
|
|
1774
|
+
}
|
|
1775
|
+
function adjustScalarBytes(bytes) {
|
|
1776
|
+
bytes[0] &= 248;
|
|
1777
|
+
bytes[31] &= 127;
|
|
1778
|
+
bytes[31] |= 64;
|
|
1779
|
+
return bytes;
|
|
1780
|
+
}
|
|
1781
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
1782
|
+
function uvRatio(u, v) {
|
|
1783
|
+
const P = ed25519_CURVE_p;
|
|
1784
|
+
const v3 = mod(v * v * v, P);
|
|
1785
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
1786
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
1787
|
+
let x = mod(u * v3 * pow, P);
|
|
1788
|
+
const vx2 = mod(v * x * x, P);
|
|
1789
|
+
const root1 = x;
|
|
1790
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
1791
|
+
const useRoot1 = vx2 === u;
|
|
1792
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
1793
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
1794
|
+
if (useRoot1)
|
|
1795
|
+
x = root1;
|
|
1796
|
+
if (useRoot2 || noRoot)
|
|
1797
|
+
x = root2;
|
|
1798
|
+
if (isNegativeLE(x, P))
|
|
1799
|
+
x = mod(-x, P);
|
|
1800
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
1801
|
+
}
|
|
1802
|
+
var Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, { isLE: true }))();
|
|
1803
|
+
var Fn = /* @__PURE__ */ (() => Field(ed25519_CURVE.n, { isLE: true }))();
|
|
1804
|
+
var ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
1805
|
+
...ed25519_CURVE,
|
|
1806
|
+
Fp,
|
|
1807
|
+
hash: sha512,
|
|
1808
|
+
adjustScalarBytes,
|
|
1809
|
+
// dom2
|
|
1810
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
1811
|
+
// Constant-time, u/√v
|
|
1812
|
+
uvRatio
|
|
1813
|
+
}))();
|
|
1814
|
+
var ed25519 = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
1815
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
1816
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
1817
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
1818
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
1819
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
1820
|
+
var invertSqrt = (number) => uvRatio(_1n5, number);
|
|
1821
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
1822
|
+
var bytes255ToNumberLE = (bytes) => ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
1823
|
+
function calcElligatorRistrettoMap(r0) {
|
|
1824
|
+
const { d } = ed25519_CURVE;
|
|
1825
|
+
const P = ed25519_CURVE_p;
|
|
1826
|
+
const mod2 = (n) => Fp.create(n);
|
|
1827
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
1828
|
+
const Ns = mod2((r + _1n5) * ONE_MINUS_D_SQ);
|
|
1829
|
+
let c = BigInt(-1);
|
|
1830
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
1831
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
1832
|
+
let s_ = mod2(s * r0);
|
|
1833
|
+
if (!isNegativeLE(s_, P))
|
|
1834
|
+
s_ = mod2(-s_);
|
|
1835
|
+
if (!Ns_D_is_sq)
|
|
1836
|
+
s = s_;
|
|
1837
|
+
if (!Ns_D_is_sq)
|
|
1838
|
+
c = r;
|
|
1839
|
+
const Nt = mod2(c * (r - _1n5) * D_MINUS_ONE_SQ - D);
|
|
1840
|
+
const s2 = s * s;
|
|
1841
|
+
const W0 = mod2((s + s) * D);
|
|
1842
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
1843
|
+
const W2 = mod2(_1n5 - s2);
|
|
1844
|
+
const W3 = mod2(_1n5 + s2);
|
|
1845
|
+
return new ed25519.Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
1846
|
+
}
|
|
1847
|
+
function ristretto255_map(bytes) {
|
|
1848
|
+
abytes(bytes, 64);
|
|
1849
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
1850
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
1851
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
1852
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
1853
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
1854
|
+
}
|
|
1855
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
1856
|
+
constructor(ep) {
|
|
1857
|
+
super(ep);
|
|
1858
|
+
}
|
|
1859
|
+
static fromAffine(ap) {
|
|
1860
|
+
return new __RistrettoPoint(ed25519.Point.fromAffine(ap));
|
|
1861
|
+
}
|
|
1862
|
+
assertSame(other) {
|
|
1863
|
+
if (!(other instanceof __RistrettoPoint))
|
|
1864
|
+
throw new Error("RistrettoPoint expected");
|
|
1865
|
+
}
|
|
1866
|
+
init(ep) {
|
|
1867
|
+
return new __RistrettoPoint(ep);
|
|
1868
|
+
}
|
|
1869
|
+
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
1870
|
+
static hashToCurve(hex) {
|
|
1871
|
+
return ristretto255_map(ensureBytes("ristrettoHash", hex, 64));
|
|
1872
|
+
}
|
|
1873
|
+
static fromBytes(bytes) {
|
|
1874
|
+
abytes(bytes, 32);
|
|
1875
|
+
const { a, d } = ed25519_CURVE;
|
|
1876
|
+
const P = ed25519_CURVE_p;
|
|
1877
|
+
const mod2 = (n) => Fp.create(n);
|
|
1878
|
+
const s = bytes255ToNumberLE(bytes);
|
|
1879
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
1880
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
1881
|
+
const s2 = mod2(s * s);
|
|
1882
|
+
const u1 = mod2(_1n5 + a * s2);
|
|
1883
|
+
const u2 = mod2(_1n5 - a * s2);
|
|
1884
|
+
const u1_2 = mod2(u1 * u1);
|
|
1885
|
+
const u2_2 = mod2(u2 * u2);
|
|
1886
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
1887
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
1888
|
+
const Dx = mod2(I * u2);
|
|
1889
|
+
const Dy = mod2(I * Dx * v);
|
|
1890
|
+
let x = mod2((s + s) * Dx);
|
|
1891
|
+
if (isNegativeLE(x, P))
|
|
1892
|
+
x = mod2(-x);
|
|
1893
|
+
const y = mod2(u1 * Dy);
|
|
1894
|
+
const t = mod2(x * y);
|
|
1895
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n5)
|
|
1896
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
1897
|
+
return new __RistrettoPoint(new ed25519.Point(x, y, _1n5, t));
|
|
1898
|
+
}
|
|
1899
|
+
/**
|
|
1900
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
1901
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
1902
|
+
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
1903
|
+
*/
|
|
1904
|
+
static fromHex(hex) {
|
|
1905
|
+
return __RistrettoPoint.fromBytes(ensureBytes("ristrettoHex", hex, 32));
|
|
1906
|
+
}
|
|
1907
|
+
static msm(points, scalars) {
|
|
1908
|
+
return pippenger(__RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1911
|
+
* Encodes ristretto point to Uint8Array.
|
|
1912
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
1913
|
+
*/
|
|
1914
|
+
toBytes() {
|
|
1915
|
+
let { X, Y, Z, T } = this.ep;
|
|
1916
|
+
const P = ed25519_CURVE_p;
|
|
1917
|
+
const mod2 = (n) => Fp.create(n);
|
|
1918
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
1919
|
+
const u2 = mod2(X * Y);
|
|
1920
|
+
const u2sq = mod2(u2 * u2);
|
|
1921
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
1922
|
+
const D1 = mod2(invsqrt * u1);
|
|
1923
|
+
const D2 = mod2(invsqrt * u2);
|
|
1924
|
+
const zInv = mod2(D1 * D2 * T);
|
|
1925
|
+
let D;
|
|
1926
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
1927
|
+
let _x = mod2(Y * SQRT_M1);
|
|
1928
|
+
let _y = mod2(X * SQRT_M1);
|
|
1929
|
+
X = _x;
|
|
1930
|
+
Y = _y;
|
|
1931
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
1932
|
+
} else {
|
|
1933
|
+
D = D2;
|
|
1934
|
+
}
|
|
1935
|
+
if (isNegativeLE(X * zInv, P))
|
|
1936
|
+
Y = mod2(-Y);
|
|
1937
|
+
let s = mod2((Z - Y) * D);
|
|
1938
|
+
if (isNegativeLE(s, P))
|
|
1939
|
+
s = mod2(-s);
|
|
1940
|
+
return Fp.toBytes(s);
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Compares two Ristretto points.
|
|
1944
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
1945
|
+
*/
|
|
1946
|
+
equals(other) {
|
|
1947
|
+
this.assertSame(other);
|
|
1948
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
1949
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
1950
|
+
const mod2 = (n) => Fp.create(n);
|
|
1951
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
1952
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
1953
|
+
return one || two;
|
|
1954
|
+
}
|
|
1955
|
+
is0() {
|
|
1956
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
1957
|
+
}
|
|
1958
|
+
};
|
|
1959
|
+
_RistrettoPoint.BASE = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.BASE))();
|
|
1960
|
+
_RistrettoPoint.ZERO = /* @__PURE__ */ (() => new _RistrettoPoint(ed25519.Point.ZERO))();
|
|
1961
|
+
_RistrettoPoint.Fp = /* @__PURE__ */ (() => Fp)();
|
|
1962
|
+
_RistrettoPoint.Fn = /* @__PURE__ */ (() => Fn)();
|
|
1963
|
+
|
|
1964
|
+
// ../mmt-base/dist/mmtp.js
|
|
1965
|
+
var MPU_HEADER_SIZE = 8;
|
|
1966
|
+
var MPU_LENGTH_FIELD_SIZE = 2;
|
|
1967
|
+
var MPU_HEADER_REMAINDER_SIZE = MPU_HEADER_SIZE - MPU_LENGTH_FIELD_SIZE;
|
|
1968
|
+
|
|
1969
|
+
// ../mmt-base/dist/ntp-short.js
|
|
1970
|
+
var NTP_SHORT_BITS = 32;
|
|
1971
|
+
var NTP_SHORT_MODULUS = 2 ** NTP_SHORT_BITS;
|
|
1972
|
+
var NTP_SHORT_HALF_RANGE = 2 ** (NTP_SHORT_BITS - 1);
|
|
1973
|
+
|
|
1974
|
+
// ../mmt-base/dist/fec-block-profile.js
|
|
1975
|
+
var FEC_BLOCK_PROFILES = Object.freeze([
|
|
1976
|
+
"iso-ssbg1-v1"
|
|
1977
|
+
]);
|
|
1978
|
+
|
|
1979
|
+
// ../mmt-base/dist/fec-bindings.js
|
|
1980
|
+
var RUNTIME_FEC_RECEIVER = Object.freeze({
|
|
1981
|
+
schemes: Object.freeze(["raptorq", "reed-solomon"]),
|
|
1982
|
+
blockProfiles: Object.freeze(["iso-ssbg1-v1"]),
|
|
1983
|
+
overlays: true
|
|
1984
|
+
});
|
|
1985
|
+
|
|
1986
|
+
// ../mmt-base/dist/mmtp-siblings.js
|
|
1987
|
+
var MMTP_MODES = Object.freeze(["mfu", "mfu-datagram"]);
|
|
1988
|
+
var MMTP_SIBLING_HARD_FIELDS = Object.freeze([
|
|
1989
|
+
"namespace",
|
|
1990
|
+
"timescale",
|
|
1991
|
+
"presentationAnchorTicks",
|
|
1992
|
+
"groupDuration"
|
|
1993
|
+
]);
|
|
1994
|
+
var MMTP_SIBLING_RENDITION_FIELDS = Object.freeze([
|
|
1995
|
+
"codec",
|
|
1996
|
+
"width",
|
|
1997
|
+
"height",
|
|
1998
|
+
"framerate",
|
|
1999
|
+
"bitrate",
|
|
2000
|
+
"samplerate",
|
|
2001
|
+
"channelConfig",
|
|
2002
|
+
"lang"
|
|
2003
|
+
]);
|
|
2004
|
+
var MMTP_SIBLING_SOFT_FIELDS = Object.freeze([
|
|
2005
|
+
"initRef",
|
|
2006
|
+
"keyframeIntervalMs",
|
|
2007
|
+
"keyframeIntervalTicks",
|
|
2008
|
+
"isLive",
|
|
2009
|
+
"altGroup"
|
|
2010
|
+
]);
|
|
2011
|
+
|
|
2012
|
+
// ../mmt-base/dist/datagram-leg-size.js
|
|
2013
|
+
var DATAGRAM_LEG_UDP_FLOOR = 1200;
|
|
2014
|
+
var DATAGRAM_LEG_QUIC_WT_FRAMING = 48;
|
|
2015
|
+
var DATAGRAM_LEG_OBJECT_HEADER = 13;
|
|
2016
|
+
var DATAGRAM_LEG_RELAY_ROOM = 16;
|
|
2017
|
+
var DATAGRAM_LEG_DATAGRAM_MAX = DATAGRAM_LEG_UDP_FLOOR - DATAGRAM_LEG_QUIC_WT_FRAMING;
|
|
2018
|
+
var DATAGRAM_LEG_BODY_MAX = DATAGRAM_LEG_DATAGRAM_MAX - DATAGRAM_LEG_OBJECT_HEADER - DATAGRAM_LEG_RELAY_ROOM;
|
|
2019
|
+
|
|
2020
|
+
// ../mmt-manifest-verify/dist/bc-provenance-v2.js
|
|
2021
|
+
var BCPV2_HEADER_LENGTH = 68;
|
|
2022
|
+
var BCPV2_SIGNATURE_LENGTH = 64;
|
|
2023
|
+
var BCPV2_MIN_RECORD_LENGTH = BCPV2_HEADER_LENGTH + 32 + BCPV2_SIGNATURE_LENGTH;
|
|
2024
|
+
var text = new TextEncoder();
|
|
2025
|
+
var MAGIC = Uint8Array.from([77, 72, 77, 50]);
|
|
2026
|
+
var SIGNING_DOMAIN = text.encode("mcast-manifest-v2\0");
|
|
2027
|
+
|
|
2028
|
+
// src/multicast-auth-catalog.ts
|
|
2029
|
+
var RAW_ED25519_KEY_LENGTH = 32;
|
|
2030
|
+
var BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
2031
|
+
function encodeBase64Url(bytes) {
|
|
2032
|
+
let output = "";
|
|
2033
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
2034
|
+
const b0 = bytes[i];
|
|
2035
|
+
const b1 = bytes[i + 1];
|
|
2036
|
+
const b2 = bytes[i + 2];
|
|
2037
|
+
output += BASE64URL_ALPHABET[b0 >> 2];
|
|
2038
|
+
output += BASE64URL_ALPHABET[(b0 & 3) << 4 | (b1 === void 0 ? 0 : b1 >> 4)];
|
|
2039
|
+
if (b1 === void 0) break;
|
|
2040
|
+
output += BASE64URL_ALPHABET[(b1 & 15) << 2 | (b2 === void 0 ? 0 : b2 >> 6)];
|
|
2041
|
+
if (b2 === void 0) break;
|
|
2042
|
+
output += BASE64URL_ALPHABET[b2 & 63];
|
|
2043
|
+
}
|
|
2044
|
+
return output;
|
|
2045
|
+
}
|
|
2046
|
+
var BASE64URL_DECODE_TABLE = (() => {
|
|
2047
|
+
const table = new Int8Array(128).fill(-1);
|
|
2048
|
+
for (let i = 0; i < BASE64URL_ALPHABET.length; i++) {
|
|
2049
|
+
table[BASE64URL_ALPHABET.charCodeAt(i)] = i;
|
|
2050
|
+
}
|
|
2051
|
+
return table;
|
|
2052
|
+
})();
|
|
2053
|
+
function decodeBase64Url(text2) {
|
|
2054
|
+
if (text2.length % 4 === 1) {
|
|
2055
|
+
throw new Error(`[multicast-auth-catalog] invalid unpadded base64url length for "${text2}"`);
|
|
2056
|
+
}
|
|
2057
|
+
const bytes = [];
|
|
2058
|
+
let buffer = 0;
|
|
2059
|
+
let bits = 0;
|
|
2060
|
+
for (let i = 0; i < text2.length; i++) {
|
|
2061
|
+
const code = text2.charCodeAt(i);
|
|
2062
|
+
const value = code < 128 ? BASE64URL_DECODE_TABLE[code] : -1;
|
|
2063
|
+
if (value === void 0 || value < 0) {
|
|
2064
|
+
throw new Error(`[multicast-auth-catalog] invalid base64url character in "${text2}"`);
|
|
2065
|
+
}
|
|
2066
|
+
buffer = buffer << 6 | value;
|
|
2067
|
+
bits += 6;
|
|
2068
|
+
if (bits >= 8) {
|
|
2069
|
+
bits -= 8;
|
|
2070
|
+
bytes.push(buffer >> bits & 255);
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
const decoded = Uint8Array.from(bytes);
|
|
2074
|
+
if (encodeBase64Url(decoded) !== text2) {
|
|
2075
|
+
throw new Error(`[multicast-auth-catalog] non-canonical base64url encoding "${text2}"`);
|
|
2076
|
+
}
|
|
2077
|
+
return decoded;
|
|
2078
|
+
}
|
|
2079
|
+
function decodeEd25519PublicKey(publicKey) {
|
|
2080
|
+
const bytes = decodeBase64Url(publicKey);
|
|
2081
|
+
if (bytes.byteLength !== RAW_ED25519_KEY_LENGTH) {
|
|
2082
|
+
throw new Error(
|
|
2083
|
+
`[multicast-auth-catalog] public key must decode to ${RAW_ED25519_KEY_LENGTH} raw bytes, got ${bytes.byteLength}`
|
|
2084
|
+
);
|
|
2085
|
+
}
|
|
2086
|
+
return bytes;
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
// src/ad-creative-record.ts
|
|
2090
|
+
var DETACHED_JWS = /^([A-Za-z0-9_-]+)\.\.([A-Za-z0-9_-]+)$/;
|
|
2091
|
+
var UTF8 = new TextDecoder("utf-8", { fatal: true });
|
|
2092
|
+
var ENCODER = new TextEncoder();
|
|
2093
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2094
|
+
function importAdCreativeJwks(jwks, kids) {
|
|
2095
|
+
const keys = /* @__PURE__ */ new Map();
|
|
2096
|
+
const repeated = /* @__PURE__ */ new Set();
|
|
2097
|
+
const list = isObject(jwks) ? jwks["keys"] : void 0;
|
|
2098
|
+
for (const jwk of Array.isArray(list) ? list : []) {
|
|
2099
|
+
if (!isObject(jwk)) continue;
|
|
2100
|
+
const kid = jwk["kid"];
|
|
2101
|
+
const x = jwk["x"];
|
|
2102
|
+
if (jwk["kty"] !== "OKP" || jwk["crv"] !== "Ed25519" || typeof kid !== "string" || typeof x !== "string" || "d" in jwk) continue;
|
|
2103
|
+
if (kids !== void 0 && !kids.includes(kid)) continue;
|
|
2104
|
+
if (keys.has(kid)) {
|
|
2105
|
+
repeated.add(kid);
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
try {
|
|
2109
|
+
keys.set(kid, decodeEd25519PublicKey(x));
|
|
2110
|
+
} catch {
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
for (const kid of repeated) keys.delete(kid);
|
|
2114
|
+
return keys;
|
|
2115
|
+
}
|
|
2116
|
+
function canonicalJson(value) {
|
|
2117
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
2118
|
+
if (isObject(value)) {
|
|
2119
|
+
return `{${Object.keys(value).sort().map((k) => `${JSON.stringify(k)}:${canonicalJson(value[k])}`).join(",")}}`;
|
|
2120
|
+
}
|
|
2121
|
+
if (typeof value === "number" && !Number.isFinite(value)) throw new RangeError("RFC 8785 has no non-finite numbers");
|
|
2122
|
+
return JSON.stringify(value);
|
|
2123
|
+
}
|
|
2124
|
+
function checkJws(sig, canonical, keys) {
|
|
2125
|
+
if (sig === void 0) return "unsigned";
|
|
2126
|
+
const parts = typeof sig === "string" ? DETACHED_JWS.exec(sig) : null;
|
|
2127
|
+
if (!parts) return "sig";
|
|
2128
|
+
const protectedB64 = parts[1];
|
|
2129
|
+
let header;
|
|
2130
|
+
let signature;
|
|
2131
|
+
try {
|
|
2132
|
+
header = JSON.parse(UTF8.decode(decodeBase64Url(protectedB64)));
|
|
2133
|
+
signature = decodeBase64Url(parts[2]);
|
|
2134
|
+
} catch {
|
|
2135
|
+
return "sig";
|
|
2136
|
+
}
|
|
2137
|
+
if (!isObject(header) || signature.length !== 64) return "sig";
|
|
2138
|
+
const crit = header["crit"];
|
|
2139
|
+
if (header["b64"] !== false || !Array.isArray(crit) || crit.length !== 1 || crit[0] !== "b64") return "sig";
|
|
2140
|
+
if (header["alg"] !== "EdDSA" && header["alg"] !== "Ed25519") return "alg";
|
|
2141
|
+
const kid = header["kid"];
|
|
2142
|
+
const key = typeof kid === "string" ? keys.get(kid) : void 0;
|
|
2143
|
+
if (!key) return "kid";
|
|
2144
|
+
try {
|
|
2145
|
+
const input = ENCODER.encode(`${protectedB64}.${canonical}`);
|
|
2146
|
+
return ed25519.verify(signature, input, key, { zip215: false }) ? "verified" : "signature";
|
|
2147
|
+
} catch {
|
|
2148
|
+
return "signature";
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
function verifyAdCreativeSig(record, keys) {
|
|
2152
|
+
const unsigned = { ...record };
|
|
2153
|
+
delete unsigned["sig"];
|
|
2154
|
+
return checkJws(record["sig"], canonicalJson(unsigned), keys);
|
|
2155
|
+
}
|
|
2156
|
+
var AD_CREATIVE_EVENT_TYPE = "urn:svta:advertising-wg:ad-creative-signaling";
|
|
2157
|
+
var AD_OBJECT_MAX_BYTES = 65536;
|
|
2158
|
+
var AD_OBJECT_MAX_RECORDS = 64;
|
|
2159
|
+
var AD_JSON_MAX_DEPTH = 16;
|
|
2160
|
+
var INDEX_NAMES = ["m", "t", "l", "M", "T", "L"];
|
|
2161
|
+
var ENVELOPE_TYPES = /* @__PURE__ */ new Set(["pod", "slot", "trackingEvent", "verification"]);
|
|
2162
|
+
var DEFINED_FEATURES = /* @__PURE__ */ new Set(["remoteFields"]);
|
|
2163
|
+
var SUPPORTED_FEATURES = /* @__PURE__ */ new Set();
|
|
2164
|
+
var isMs = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
2165
|
+
function depthWithin(value, limit) {
|
|
2166
|
+
if (typeof value !== "object" || value === null) return true;
|
|
2167
|
+
if (limit === 0) return false;
|
|
2168
|
+
return (Array.isArray(value) ? value : Object.values(value)).every((child) => depthWithin(child, limit - 1));
|
|
2169
|
+
}
|
|
2170
|
+
function indexOf(record) {
|
|
2171
|
+
const present = INDEX_NAMES.filter((name) => name in record);
|
|
2172
|
+
if (present.length !== 1) return void 0;
|
|
2173
|
+
const value = record[present[0]];
|
|
2174
|
+
const kind = present[0].toLowerCase();
|
|
2175
|
+
if (kind === "l") {
|
|
2176
|
+
return Array.isArray(value) && value.length === 2 && value.every((n) => Number.isSafeInteger(n) && n >= 0) ? { kind: "l", group: value[0], object: value[1] } : void 0;
|
|
2177
|
+
}
|
|
2178
|
+
return isMs(value) ? { kind, ms: value } : void 0;
|
|
2179
|
+
}
|
|
2180
|
+
var keyOf = (index) => index.kind === "l" ? `l:${index.group},${index.object}` : `${index.kind}:${index.ms}`;
|
|
2181
|
+
function envelopeOf(data) {
|
|
2182
|
+
const type = data["type"];
|
|
2183
|
+
const payload = data["payload"];
|
|
2184
|
+
const features = data["features"];
|
|
2185
|
+
if (data["version"] !== 2 || typeof type !== "string" || !ENVELOPE_TYPES.has(type)) return void 0;
|
|
2186
|
+
if (!Array.isArray(payload) || payload.length === 0 || !payload.every(isObject)) return void 0;
|
|
2187
|
+
if (features !== void 0 && !(isObject(features) && Object.entries(features).every(([name, on]) => typeof on === "boolean" && (!on || DEFINED_FEATURES.has(name))))) {
|
|
2188
|
+
return void 0;
|
|
2189
|
+
}
|
|
2190
|
+
return data;
|
|
2191
|
+
}
|
|
2192
|
+
var isHttpsUrl = (value) => {
|
|
2193
|
+
if (typeof value !== "string") return false;
|
|
2194
|
+
try {
|
|
2195
|
+
return new URL(value).protocol === "https:";
|
|
2196
|
+
} catch {
|
|
2197
|
+
return false;
|
|
2198
|
+
}
|
|
2199
|
+
};
|
|
2200
|
+
var allHttps = (value) => typeof value === "string" ? isHttpsUrl(value) : typeof value !== "object" || value === null || (Array.isArray(value) ? value : Object.values(value)).every(allHttps);
|
|
2201
|
+
function httpsOnly(value) {
|
|
2202
|
+
if (Array.isArray(value)) return value.every(httpsOnly);
|
|
2203
|
+
if (!isObject(value)) return true;
|
|
2204
|
+
return Object.entries(value).every(([name, child]) => {
|
|
2205
|
+
if (name === "$remote") return allHttps(child);
|
|
2206
|
+
if (name === "urls" && !(Array.isArray(child) && child.every(isHttpsUrl))) return false;
|
|
2207
|
+
if (name === "resource" && !isHttpsUrl(child)) return false;
|
|
2208
|
+
return httpsOnly(child);
|
|
2209
|
+
});
|
|
2210
|
+
}
|
|
2211
|
+
function parseAdCreativeRecord(value, keys) {
|
|
2212
|
+
if (!isObject(value)) return { verdict: "shape" };
|
|
2213
|
+
if (!depthWithin(value, AD_JSON_MAX_DEPTH)) return { verdict: "depth" };
|
|
2214
|
+
const index = indexOf(value);
|
|
2215
|
+
if (!index) return { verdict: "index" };
|
|
2216
|
+
const key = keyOf(index);
|
|
2217
|
+
const data = value["data"];
|
|
2218
|
+
if (!isObject(data)) return { verdict: "data", key };
|
|
2219
|
+
const envelope = envelopeOf(data);
|
|
2220
|
+
if (!envelope) return { verdict: "envelope", key };
|
|
2221
|
+
if (!httpsOnly(data)) return { verdict: "url", key };
|
|
2222
|
+
if (Object.entries(envelope.features ?? {}).some(([name, on]) => on && !SUPPORTED_FEATURES.has(name))) {
|
|
2223
|
+
return { verdict: "feature", key };
|
|
2224
|
+
}
|
|
2225
|
+
if (envelope.type === "pod" && envelope.payload.some((pod) => pod["start"] !== void 0 && pod["start"] !== 0)) {
|
|
2226
|
+
return { verdict: "timing", key };
|
|
2227
|
+
}
|
|
2228
|
+
const unsigned = { ...value };
|
|
2229
|
+
delete unsigned["sig"];
|
|
2230
|
+
let canonical;
|
|
2231
|
+
try {
|
|
2232
|
+
canonical = canonicalJson(unsigned);
|
|
2233
|
+
} catch {
|
|
2234
|
+
return { verdict: "number", key };
|
|
2235
|
+
}
|
|
2236
|
+
const record = { index, key, data: envelope, canonical };
|
|
2237
|
+
const verdict = checkJws(value["sig"], record.canonical, keys);
|
|
2238
|
+
return verdict === "verified" || verdict === "unsigned" || verdict === "kid" ? { verdict, key, record } : { verdict, key };
|
|
2239
|
+
}
|
|
2240
|
+
function parseAdCreativeObject(bytes, keys) {
|
|
2241
|
+
if (bytes.byteLength > AD_OBJECT_MAX_BYTES) return { verdict: "size", records: [] };
|
|
2242
|
+
let text2;
|
|
2243
|
+
try {
|
|
2244
|
+
text2 = UTF8.decode(bytes);
|
|
2245
|
+
} catch {
|
|
2246
|
+
return { verdict: "utf8", records: [] };
|
|
2247
|
+
}
|
|
2248
|
+
let doc;
|
|
2249
|
+
try {
|
|
2250
|
+
doc = JSON.parse(text2);
|
|
2251
|
+
} catch {
|
|
2252
|
+
return { verdict: "json", records: [] };
|
|
2253
|
+
}
|
|
2254
|
+
if (!Array.isArray(doc)) return { verdict: "not-array", records: [] };
|
|
2255
|
+
if (doc.length > AD_OBJECT_MAX_RECORDS) return { verdict: "count", records: [] };
|
|
2256
|
+
return { verdict: "accepted", records: doc.map((record) => parseAdCreativeRecord(record, keys)) };
|
|
2257
|
+
}
|
|
2258
|
+
export {
|
|
2259
|
+
AD_CREATIVE_EVENT_TYPE,
|
|
2260
|
+
AD_JSON_MAX_DEPTH,
|
|
2261
|
+
AD_OBJECT_MAX_BYTES,
|
|
2262
|
+
AD_OBJECT_MAX_RECORDS,
|
|
2263
|
+
canonicalJson,
|
|
2264
|
+
importAdCreativeJwks,
|
|
2265
|
+
parseAdCreativeObject,
|
|
2266
|
+
parseAdCreativeRecord,
|
|
2267
|
+
verifyAdCreativeSig
|
|
2268
|
+
};
|
|
2269
|
+
/*! Bundled license information:
|
|
2270
|
+
|
|
2271
|
+
@noble/hashes/esm/utils.js:
|
|
2272
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2273
|
+
|
|
2274
|
+
@noble/curves/esm/utils.js:
|
|
2275
|
+
@noble/curves/esm/abstract/modular.js:
|
|
2276
|
+
@noble/curves/esm/abstract/curve.js:
|
|
2277
|
+
@noble/curves/esm/abstract/edwards.js:
|
|
2278
|
+
@noble/curves/esm/ed25519.js:
|
|
2279
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
2280
|
+
*/
|
|
2281
|
+
//# sourceMappingURL=ad-creative-record.js.map
|