@kangentic/protocol 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capabilities/verbs.d.ts +10 -1
- package/dist/events/event.d.ts +4 -1
- package/dist/index.cjs +1193 -1180
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +1189 -1180
- package/dist/wire/framing.d.ts +30 -1
- package/dist/wire/messages.d.ts +15 -0
- package/dist/wire/payloads.d.ts +34 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -41,6 +41,8 @@ __export(index_exports, {
|
|
|
41
41
|
PUSH_ENVELOPE_MAX_FUTURE_SKEW_MS: () => PUSH_ENVELOPE_MAX_FUTURE_SKEW_MS,
|
|
42
42
|
SecretstreamState: () => SecretstreamState,
|
|
43
43
|
SessionFrameKind: () => SessionFrameKind,
|
|
44
|
+
UNSUPPORTED_VERB_ERROR_CODE: () => UNSUPPORTED_VERB_ERROR_CODE,
|
|
45
|
+
UnsupportedVerbError: () => UnsupportedVerbError,
|
|
44
46
|
X25519_KEY_LENGTH: () => X25519_KEY_LENGTH,
|
|
45
47
|
bytesToHex: () => bytesToHex,
|
|
46
48
|
capabilitySetFromArray: () => capabilitySetFromArray,
|
|
@@ -77,6 +79,7 @@ __export(index_exports, {
|
|
|
77
79
|
isRosterEntryExpired: () => isRosterEntryExpired,
|
|
78
80
|
isSecureRelayAddress: () => isSecureRelayAddress,
|
|
79
81
|
isTodoRole: () => isTodoRole,
|
|
82
|
+
isUnsupportedVerbError: () => isUnsupportedVerbError,
|
|
80
83
|
openPairingConfirm: () => openPairingConfirm,
|
|
81
84
|
openPushEnvelope: () => openPushEnvelope,
|
|
82
85
|
parseActivityEventPayload: () => parseActivityEventPayload,
|
|
@@ -94,6 +97,7 @@ __export(index_exports, {
|
|
|
94
97
|
parseSessionEventWire: () => parseSessionEventWire,
|
|
95
98
|
parseSessionSummaryWire: () => parseSessionSummaryWire,
|
|
96
99
|
parseSessionUsageWire: () => parseSessionUsageWire,
|
|
100
|
+
parseStartSessionResponsePayload: () => parseStartSessionResponsePayload,
|
|
97
101
|
parseTerminalDimensionsWire: () => parseTerminalDimensionsWire,
|
|
98
102
|
parseTranscriptEntriesWire: () => parseTranscriptEntriesWire,
|
|
99
103
|
parseTranscriptEventPayload: () => parseTranscriptEventPayload,
|
|
@@ -116,64 +120,116 @@ function encodeProtocolVersion(version = PROTOCOL_VERSION) {
|
|
|
116
120
|
return new TextEncoder().encode(`kangentic-bridge-v${version}`);
|
|
117
121
|
}
|
|
118
122
|
|
|
123
|
+
// ../../node_modules/@noble/hashes/_u64.js
|
|
124
|
+
var U32_MASK64 = /* @__PURE__ */ (() => BigInt(2 ** 32 - 1))();
|
|
125
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
126
|
+
function fromBig(n, le = false) {
|
|
127
|
+
if (le)
|
|
128
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
129
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
130
|
+
}
|
|
131
|
+
function split(lst, le = false) {
|
|
132
|
+
const len = lst.length;
|
|
133
|
+
let Ah = new Uint32Array(len);
|
|
134
|
+
let Al = new Uint32Array(len);
|
|
135
|
+
for (let i2 = 0; i2 < len; i2++) {
|
|
136
|
+
const { h, l } = fromBig(lst[i2], le);
|
|
137
|
+
[Ah[i2], Al[i2]] = [h, l];
|
|
138
|
+
}
|
|
139
|
+
return [Ah, Al];
|
|
140
|
+
}
|
|
141
|
+
var fromNumH = (n) => n / 2 ** 32 | 0;
|
|
142
|
+
var fromNumL = (n) => n >>> 0;
|
|
143
|
+
function setU64FromNum(view, byteOffset, n, isLE3) {
|
|
144
|
+
const h = fromNumH(n);
|
|
145
|
+
const l = fromNumL(n);
|
|
146
|
+
view.setUint32(byteOffset, isLE3 ? l : h, isLE3);
|
|
147
|
+
view.setUint32(byteOffset + 4, isLE3 ? h : l, isLE3);
|
|
148
|
+
}
|
|
149
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
150
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
151
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
152
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
153
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
154
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
155
|
+
function add(Ah, Al, Bh, Bl) {
|
|
156
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
157
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
158
|
+
}
|
|
159
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
160
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
161
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
162
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
163
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
164
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
165
|
+
|
|
119
166
|
// ../../node_modules/@noble/hashes/utils.js
|
|
120
167
|
function isBytes(a) {
|
|
121
168
|
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
122
169
|
}
|
|
170
|
+
var atitle = (title) => title ? `"${title}" ` : "";
|
|
123
171
|
function anumber(n, title = "") {
|
|
124
|
-
if (typeof n !== "number")
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const prefix = title && `"${title}" `;
|
|
130
|
-
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
131
|
-
}
|
|
172
|
+
if (typeof n !== "number")
|
|
173
|
+
throw new TypeError(atitle(title) + "expected number, got " + typeof n);
|
|
174
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
175
|
+
throw new RangeError(atitle(title) + "expected integer >= 0, got " + n);
|
|
176
|
+
return n;
|
|
132
177
|
}
|
|
133
178
|
function abytes(value, length, title = "") {
|
|
179
|
+
if (isBytes(value) && (length === void 0 || value.length === length))
|
|
180
|
+
return value;
|
|
181
|
+
if (length !== void 0)
|
|
182
|
+
anumber(length, "length");
|
|
134
183
|
const bytes = isBytes(value);
|
|
135
|
-
const
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
throw new RangeError(message);
|
|
145
|
-
}
|
|
146
|
-
return value;
|
|
184
|
+
const ofLen = length !== void 0 ? ` of length ${length}` : "";
|
|
185
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
186
|
+
const message = atitle(title) + "expected Uint8Array" + ofLen + ", got " + got;
|
|
187
|
+
if (!bytes)
|
|
188
|
+
throw new TypeError(message);
|
|
189
|
+
throw new RangeError(message);
|
|
190
|
+
}
|
|
191
|
+
function copyBytes(bytes) {
|
|
192
|
+
return Uint8Array.from(abytes(bytes));
|
|
147
193
|
}
|
|
148
194
|
function ahash(h) {
|
|
149
195
|
if (typeof h !== "function" || typeof h.create !== "function")
|
|
150
|
-
throw new TypeError("
|
|
196
|
+
throw new TypeError("expected hash wrapped by utils.createHasher");
|
|
151
197
|
anumber(h.outputLen);
|
|
152
198
|
anumber(h.blockLen);
|
|
153
|
-
if (h.outputLen < 1)
|
|
154
|
-
throw new Error(
|
|
155
|
-
if (h.blockLen < 1)
|
|
156
|
-
throw new Error('"blockLen" must be >= 1');
|
|
199
|
+
if (h.outputLen < 1 || h.blockLen < 1)
|
|
200
|
+
throw new Error("hash blockLen / outputLen must be >= 1");
|
|
157
201
|
}
|
|
202
|
+
var aobject = (value, label) => {
|
|
203
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
204
|
+
throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
|
|
205
|
+
};
|
|
206
|
+
var aopts = (value, label) => {
|
|
207
|
+
aobject(value, label);
|
|
208
|
+
const proto = Object.getPrototypeOf(value);
|
|
209
|
+
if (proto !== Object.prototype && proto !== null)
|
|
210
|
+
throw new TypeError(`"${label}" expected plain object`);
|
|
211
|
+
if (Object.hasOwn(value, "__proto__"))
|
|
212
|
+
throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
213
|
+
};
|
|
158
214
|
function aexists(instance, checkFinished = true) {
|
|
159
215
|
if (instance.destroyed)
|
|
160
|
-
throw new Error("
|
|
216
|
+
throw new Error("hash was destroyed");
|
|
161
217
|
if (checkFinished && instance.finished)
|
|
162
|
-
throw new Error("
|
|
218
|
+
throw new Error("digest() was already called");
|
|
163
219
|
}
|
|
164
220
|
function aoutput(out, instance) {
|
|
165
|
-
abytes(out, void 0, "
|
|
221
|
+
abytes(out, void 0, "output");
|
|
166
222
|
const min = instance.outputLen;
|
|
167
|
-
if (out.length
|
|
168
|
-
throw new RangeError('"
|
|
223
|
+
if (!(out.length >= min)) {
|
|
224
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
169
225
|
}
|
|
170
226
|
}
|
|
171
227
|
function u32(arr) {
|
|
172
228
|
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
173
229
|
}
|
|
174
230
|
function clean(...arrays) {
|
|
175
|
-
for (let
|
|
176
|
-
arrays[
|
|
231
|
+
for (let i2 = 0; i2 < arrays.length; i2++) {
|
|
232
|
+
arrays[i2].fill(0);
|
|
177
233
|
}
|
|
178
234
|
}
|
|
179
235
|
function createView(arr) {
|
|
@@ -188,8 +244,8 @@ function byteSwap(word) {
|
|
|
188
244
|
}
|
|
189
245
|
var swap8IfBE = isLE ? (n) => n : (n) => byteSwap(n) >>> 0;
|
|
190
246
|
function byteSwap32(arr) {
|
|
191
|
-
for (let
|
|
192
|
-
arr[
|
|
247
|
+
for (let i2 = 0; i2 < arr.length; i2++) {
|
|
248
|
+
arr[i2] = byteSwap(arr[i2]);
|
|
193
249
|
}
|
|
194
250
|
return arr;
|
|
195
251
|
}
|
|
@@ -198,26 +254,19 @@ var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
|
198
254
|
// @ts-ignore
|
|
199
255
|
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
200
256
|
))();
|
|
201
|
-
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_,
|
|
257
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i2) => i2.toString(16).padStart(2, "0"));
|
|
202
258
|
function bytesToHex(bytes) {
|
|
203
259
|
abytes(bytes);
|
|
204
260
|
if (hasHexBuiltin)
|
|
205
261
|
return bytes.toHex();
|
|
206
262
|
let hex = "";
|
|
207
|
-
for (let
|
|
208
|
-
hex += hexes[bytes[
|
|
263
|
+
for (let i2 = 0; i2 < bytes.length; i2++) {
|
|
264
|
+
hex += hexes[bytes[i2]];
|
|
209
265
|
}
|
|
210
266
|
return hex;
|
|
211
267
|
}
|
|
212
|
-
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
213
268
|
function asciiToBase16(ch) {
|
|
214
|
-
|
|
215
|
-
return ch - asciis._0;
|
|
216
|
-
if (ch >= asciis.A && ch <= asciis.F)
|
|
217
|
-
return ch - (asciis.A - 10);
|
|
218
|
-
if (ch >= asciis.a && ch <= asciis.f)
|
|
219
|
-
return ch - (asciis.a - 10);
|
|
220
|
-
return;
|
|
269
|
+
return ch >= 48 && ch <= 57 ? ch - 48 : ch >= 65 && ch <= 70 ? ch - (65 - 10) : ch >= 97 && ch <= 102 ? ch - (97 - 10) : void 0;
|
|
221
270
|
}
|
|
222
271
|
function hexToBytes(hex) {
|
|
223
272
|
if (typeof hex !== "string")
|
|
@@ -249,20 +298,30 @@ function hexToBytes(hex) {
|
|
|
249
298
|
}
|
|
250
299
|
function concatBytes(...arrays) {
|
|
251
300
|
let sum = 0;
|
|
252
|
-
for (let
|
|
253
|
-
const a = arrays[
|
|
301
|
+
for (let i2 = 0; i2 < arrays.length; i2++) {
|
|
302
|
+
const a = arrays[i2];
|
|
254
303
|
abytes(a);
|
|
255
304
|
sum += a.length;
|
|
256
305
|
}
|
|
257
306
|
const res = new Uint8Array(sum);
|
|
258
|
-
for (let
|
|
259
|
-
const a = arrays[
|
|
307
|
+
for (let i2 = 0, pad = 0; i2 < arrays.length; i2++) {
|
|
308
|
+
const a = arrays[i2];
|
|
260
309
|
res.set(a, pad);
|
|
261
310
|
pad += a.length;
|
|
262
311
|
}
|
|
263
312
|
return res;
|
|
264
313
|
}
|
|
314
|
+
function checkOpts(defaults, opts, title = "opts") {
|
|
315
|
+
aopts(defaults, "defaults");
|
|
316
|
+
if (opts !== void 0)
|
|
317
|
+
aopts(opts, title);
|
|
318
|
+
const merged = Object.assign(/* @__PURE__ */ Object.create(null), defaults, opts);
|
|
319
|
+
return merged;
|
|
320
|
+
}
|
|
265
321
|
function createHasher(hashCons, info = {}) {
|
|
322
|
+
if (typeof hashCons !== "function")
|
|
323
|
+
throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
324
|
+
info = checkOpts({}, info, "info");
|
|
266
325
|
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
267
326
|
const tmp = hashCons(void 0);
|
|
268
327
|
hashC.outputLen = tmp.outputLen;
|
|
@@ -314,24 +373,28 @@ var HashMD = class {
|
|
|
314
373
|
abytes(data);
|
|
315
374
|
const { view, buffer, blockLen } = this;
|
|
316
375
|
const len = data.length;
|
|
376
|
+
let processed = false;
|
|
317
377
|
for (let pos = 0; pos < len; ) {
|
|
318
378
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
319
379
|
if (take === blockLen) {
|
|
320
380
|
const dataView = createView(data);
|
|
321
381
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
322
382
|
this.process(dataView, pos);
|
|
383
|
+
processed = true;
|
|
323
384
|
continue;
|
|
324
385
|
}
|
|
325
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
386
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
326
387
|
this.pos += take;
|
|
327
388
|
pos += take;
|
|
328
389
|
if (this.pos === blockLen) {
|
|
329
390
|
this.process(view, 0);
|
|
330
391
|
this.pos = 0;
|
|
392
|
+
processed = true;
|
|
331
393
|
}
|
|
332
394
|
}
|
|
333
395
|
this.length += data.length;
|
|
334
|
-
|
|
396
|
+
if (processed)
|
|
397
|
+
this.roundClean();
|
|
335
398
|
return this;
|
|
336
399
|
}
|
|
337
400
|
digestInto(out) {
|
|
@@ -341,25 +404,22 @@ var HashMD = class {
|
|
|
341
404
|
const { buffer, view, blockLen, isLE: isLE3 } = this;
|
|
342
405
|
let { pos } = this;
|
|
343
406
|
buffer[pos++] = 128;
|
|
344
|
-
|
|
407
|
+
buffer.fill(0, pos);
|
|
345
408
|
if (this.padOffset > blockLen - pos) {
|
|
346
409
|
this.process(view, 0);
|
|
347
|
-
|
|
410
|
+
buffer.fill(0);
|
|
348
411
|
}
|
|
349
|
-
|
|
350
|
-
buffer[i] = 0;
|
|
351
|
-
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE3);
|
|
412
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE3);
|
|
352
413
|
this.process(view, 0);
|
|
353
|
-
|
|
414
|
+
this.roundClean();
|
|
415
|
+
const oview = out === buffer ? view : createView(out);
|
|
354
416
|
const len = this.outputLen;
|
|
355
|
-
if (len % 4)
|
|
356
|
-
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
357
417
|
const outLen = len / 4;
|
|
358
418
|
const state = this.get();
|
|
359
|
-
if (outLen > state.length)
|
|
360
|
-
throw new Error("
|
|
361
|
-
for (let
|
|
362
|
-
oview.setUint32(4 *
|
|
419
|
+
if (len % 4 || outLen > state.length)
|
|
420
|
+
throw new Error("invalid outputLen");
|
|
421
|
+
for (let i2 = 0; i2 < outLen; i2++)
|
|
422
|
+
oview.setUint32(4 * i2, state[i2], isLE3);
|
|
363
423
|
}
|
|
364
424
|
digest() {
|
|
365
425
|
const { buffer, outputLen } = this;
|
|
@@ -368,15 +428,13 @@ var HashMD = class {
|
|
|
368
428
|
this.destroy();
|
|
369
429
|
return res;
|
|
370
430
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
to.set(...this.get());
|
|
374
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
431
|
+
_cloneIntoMeta(to) {
|
|
432
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
375
433
|
to.destroyed = destroyed;
|
|
376
434
|
to.finished = finished;
|
|
377
435
|
to.length = length;
|
|
378
436
|
to.pos = pos;
|
|
379
|
-
if (
|
|
437
|
+
if (pos)
|
|
380
438
|
to.buffer.set(buffer);
|
|
381
439
|
return to;
|
|
382
440
|
}
|
|
@@ -413,41 +471,6 @@ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
|
413
471
|
327033209
|
|
414
472
|
]);
|
|
415
473
|
|
|
416
|
-
// ../../node_modules/@noble/hashes/_u64.js
|
|
417
|
-
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
418
|
-
var _32n = /* @__PURE__ */ BigInt(32);
|
|
419
|
-
function fromBig(n, le = false) {
|
|
420
|
-
if (le)
|
|
421
|
-
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
422
|
-
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
423
|
-
}
|
|
424
|
-
function split(lst, le = false) {
|
|
425
|
-
const len = lst.length;
|
|
426
|
-
let Ah = new Uint32Array(len);
|
|
427
|
-
let Al = new Uint32Array(len);
|
|
428
|
-
for (let i = 0; i < len; i++) {
|
|
429
|
-
const { h, l } = fromBig(lst[i], le);
|
|
430
|
-
[Ah[i], Al[i]] = [h, l];
|
|
431
|
-
}
|
|
432
|
-
return [Ah, Al];
|
|
433
|
-
}
|
|
434
|
-
var shrSH = (h, _l, s) => h >>> s;
|
|
435
|
-
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
436
|
-
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
437
|
-
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
438
|
-
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
439
|
-
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
440
|
-
function add(Ah, Al, Bh, Bl) {
|
|
441
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
442
|
-
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
443
|
-
}
|
|
444
|
-
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
445
|
-
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
446
|
-
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
447
|
-
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
448
|
-
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
449
|
-
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
450
|
-
|
|
451
474
|
// ../../node_modules/@noble/hashes/sha2.js
|
|
452
475
|
var K512 = /* @__PURE__ */ (() => split([
|
|
453
476
|
"0x428a2f98d728ae22",
|
|
@@ -536,8 +559,45 @@ var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
|
536
559
|
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
537
560
|
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
538
561
|
var SHA2_64B = class extends HashMD {
|
|
539
|
-
|
|
562
|
+
// We cannot use array here since array allows indexing by variable
|
|
563
|
+
// which means optimizer/compiler cannot use registers.
|
|
564
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
565
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
566
|
+
// V8's field representation and slows hashing down (measured on sha256).
|
|
567
|
+
Ah = 0;
|
|
568
|
+
Al = 0;
|
|
569
|
+
Bh = 0;
|
|
570
|
+
Bl = 0;
|
|
571
|
+
Ch = 0;
|
|
572
|
+
Cl = 0;
|
|
573
|
+
Dh = 0;
|
|
574
|
+
Dl = 0;
|
|
575
|
+
Eh = 0;
|
|
576
|
+
El = 0;
|
|
577
|
+
Fh = 0;
|
|
578
|
+
Fl = 0;
|
|
579
|
+
Gh = 0;
|
|
580
|
+
Gl = 0;
|
|
581
|
+
Hh = 0;
|
|
582
|
+
Hl = 0;
|
|
583
|
+
constructor(outputLen, IV) {
|
|
540
584
|
super(128, outputLen, 16, false);
|
|
585
|
+
this.Ah = IV[0] | 0;
|
|
586
|
+
this.Al = IV[1] | 0;
|
|
587
|
+
this.Bh = IV[2] | 0;
|
|
588
|
+
this.Bl = IV[3] | 0;
|
|
589
|
+
this.Ch = IV[4] | 0;
|
|
590
|
+
this.Cl = IV[5] | 0;
|
|
591
|
+
this.Dh = IV[6] | 0;
|
|
592
|
+
this.Dl = IV[7] | 0;
|
|
593
|
+
this.Eh = IV[8] | 0;
|
|
594
|
+
this.El = IV[9] | 0;
|
|
595
|
+
this.Fh = IV[10] | 0;
|
|
596
|
+
this.Fl = IV[11] | 0;
|
|
597
|
+
this.Gh = IV[12] | 0;
|
|
598
|
+
this.Gl = IV[13] | 0;
|
|
599
|
+
this.Hh = IV[14] | 0;
|
|
600
|
+
this.Hl = IV[15] | 0;
|
|
541
601
|
}
|
|
542
602
|
// prettier-ignore
|
|
543
603
|
get() {
|
|
@@ -563,33 +623,37 @@ var SHA2_64B = class extends HashMD {
|
|
|
563
623
|
this.Hh = Hh | 0;
|
|
564
624
|
this.Hl = Hl | 0;
|
|
565
625
|
}
|
|
626
|
+
_cloneInto(to) {
|
|
627
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
628
|
+
return this._cloneIntoMeta(to);
|
|
629
|
+
}
|
|
566
630
|
process(view, offset) {
|
|
567
|
-
for (let
|
|
568
|
-
SHA512_W_H[
|
|
569
|
-
SHA512_W_L[
|
|
631
|
+
for (let i2 = 0; i2 < 16; i2++, offset += 4) {
|
|
632
|
+
SHA512_W_H[i2] = view.getUint32(offset);
|
|
633
|
+
SHA512_W_L[i2] = view.getUint32(offset += 4);
|
|
570
634
|
}
|
|
571
|
-
for (let
|
|
572
|
-
const W15h = SHA512_W_H[
|
|
573
|
-
const W15l = SHA512_W_L[
|
|
635
|
+
for (let i2 = 16; i2 < 80; i2++) {
|
|
636
|
+
const W15h = SHA512_W_H[i2 - 15] | 0;
|
|
637
|
+
const W15l = SHA512_W_L[i2 - 15] | 0;
|
|
574
638
|
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
575
639
|
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
576
|
-
const W2h = SHA512_W_H[
|
|
577
|
-
const W2l = SHA512_W_L[
|
|
640
|
+
const W2h = SHA512_W_H[i2 - 2] | 0;
|
|
641
|
+
const W2l = SHA512_W_L[i2 - 2] | 0;
|
|
578
642
|
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
579
643
|
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
580
|
-
const SUMl = add4L(s0l, s1l, SHA512_W_L[
|
|
581
|
-
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[
|
|
582
|
-
SHA512_W_H[
|
|
583
|
-
SHA512_W_L[
|
|
644
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i2 - 7], SHA512_W_L[i2 - 16]);
|
|
645
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i2 - 7], SHA512_W_H[i2 - 16]);
|
|
646
|
+
SHA512_W_H[i2] = SUMh | 0;
|
|
647
|
+
SHA512_W_L[i2] = SUMl | 0;
|
|
584
648
|
}
|
|
585
649
|
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
586
|
-
for (let
|
|
650
|
+
for (let i2 = 0; i2 < 80; i2++) {
|
|
587
651
|
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
588
652
|
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
589
653
|
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
590
654
|
const CHIl = El & Fl ^ ~El & Gl;
|
|
591
|
-
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[
|
|
592
|
-
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[
|
|
655
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i2], SHA512_W_L[i2]);
|
|
656
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i2], SHA512_W_H[i2]);
|
|
593
657
|
const T1l = T1ll | 0;
|
|
594
658
|
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
595
659
|
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
@@ -632,24 +696,8 @@ var SHA2_64B = class extends HashMD {
|
|
|
632
696
|
}
|
|
633
697
|
};
|
|
634
698
|
var _SHA512 = class extends SHA2_64B {
|
|
635
|
-
Ah = SHA512_IV[0] | 0;
|
|
636
|
-
Al = SHA512_IV[1] | 0;
|
|
637
|
-
Bh = SHA512_IV[2] | 0;
|
|
638
|
-
Bl = SHA512_IV[3] | 0;
|
|
639
|
-
Ch = SHA512_IV[4] | 0;
|
|
640
|
-
Cl = SHA512_IV[5] | 0;
|
|
641
|
-
Dh = SHA512_IV[6] | 0;
|
|
642
|
-
Dl = SHA512_IV[7] | 0;
|
|
643
|
-
Eh = SHA512_IV[8] | 0;
|
|
644
|
-
El = SHA512_IV[9] | 0;
|
|
645
|
-
Fh = SHA512_IV[10] | 0;
|
|
646
|
-
Fl = SHA512_IV[11] | 0;
|
|
647
|
-
Gh = SHA512_IV[12] | 0;
|
|
648
|
-
Gl = SHA512_IV[13] | 0;
|
|
649
|
-
Hh = SHA512_IV[14] | 0;
|
|
650
|
-
Hl = SHA512_IV[15] | 0;
|
|
651
699
|
constructor() {
|
|
652
|
-
super(64);
|
|
700
|
+
super(64, SHA512_IV);
|
|
653
701
|
}
|
|
654
702
|
};
|
|
655
703
|
var sha512 = /* @__PURE__ */ createHasher(
|
|
@@ -658,8 +706,26 @@ var sha512 = /* @__PURE__ */ createHasher(
|
|
|
658
706
|
);
|
|
659
707
|
|
|
660
708
|
// ../../node_modules/@noble/curves/utils.js
|
|
709
|
+
function aarray(item, title, inner = () => {
|
|
710
|
+
}) {
|
|
711
|
+
if (!Array.isArray(item))
|
|
712
|
+
throw new TypeError(`"${title}" expected array, got type=${typeof item}`);
|
|
713
|
+
for (let i2 = 0; i2 < item.length; i2++)
|
|
714
|
+
inner(item[i2], `${title}[${i2}]`);
|
|
715
|
+
return item;
|
|
716
|
+
}
|
|
661
717
|
var abytes2 = (value, length, title) => abytes(value, length, title);
|
|
662
718
|
var anumber2 = anumber;
|
|
719
|
+
function aobject2(value, title = "object") {
|
|
720
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
721
|
+
throw new TypeError(title === "object" ? "expected valid options object" : `"${title}" expected object, got type=${typeof value}`);
|
|
722
|
+
return value;
|
|
723
|
+
}
|
|
724
|
+
function afunction(value, title) {
|
|
725
|
+
if (typeof value !== "function")
|
|
726
|
+
throw new TypeError(`"${title}" is invalid: expected function, got ${typeof value}`);
|
|
727
|
+
return value;
|
|
728
|
+
}
|
|
663
729
|
var bytesToHex2 = bytesToHex;
|
|
664
730
|
var concatBytes2 = (...arrays) => concatBytes(...arrays);
|
|
665
731
|
var hexToBytes2 = (hex) => hexToBytes(hex);
|
|
@@ -667,11 +733,10 @@ var isBytes2 = isBytes;
|
|
|
667
733
|
var randomBytes2 = (bytesLength) => randomBytes(bytesLength);
|
|
668
734
|
var _0n = /* @__PURE__ */ BigInt(0);
|
|
669
735
|
var _1n = /* @__PURE__ */ BigInt(1);
|
|
736
|
+
var atitle2 = (title) => title ? `"${title}" ` : "";
|
|
670
737
|
function abool(value, title = "") {
|
|
671
|
-
if (typeof value !== "boolean")
|
|
672
|
-
|
|
673
|
-
throw new TypeError(prefix + "expected boolean, got type=" + typeof value);
|
|
674
|
-
}
|
|
738
|
+
if (typeof value !== "boolean")
|
|
739
|
+
throw new TypeError(atitle2(title) + "expected boolean, got type=" + typeof value);
|
|
675
740
|
return value;
|
|
676
741
|
}
|
|
677
742
|
function abignumber(n) {
|
|
@@ -701,46 +766,28 @@ function bytesToNumberBE(bytes) {
|
|
|
701
766
|
return hexToNumber(bytesToHex(bytes));
|
|
702
767
|
}
|
|
703
768
|
function bytesToNumberLE(bytes) {
|
|
704
|
-
return hexToNumber(bytesToHex(
|
|
769
|
+
return hexToNumber(bytesToHex(copyBytes2(abytes(bytes)).reverse()));
|
|
705
770
|
}
|
|
706
771
|
function numberToBytesBE(n, len) {
|
|
707
772
|
anumber(len);
|
|
708
773
|
if (len === 0)
|
|
709
|
-
throw new
|
|
774
|
+
throw new Error("zero output length is invalid");
|
|
710
775
|
n = abignumber(n);
|
|
776
|
+
const expectedLen = len * 2;
|
|
711
777
|
const hex = n.toString(16);
|
|
712
|
-
if (hex.length >
|
|
713
|
-
throw new RangeError("number too large");
|
|
714
|
-
return hexToBytes(hex.padStart(
|
|
778
|
+
if (hex.length > expectedLen)
|
|
779
|
+
throw new RangeError("number is too large");
|
|
780
|
+
return hexToBytes(hex.padStart(expectedLen, "0"));
|
|
715
781
|
}
|
|
716
782
|
function numberToBytesLE(n, len) {
|
|
717
783
|
return numberToBytesBE(n, len).reverse();
|
|
718
784
|
}
|
|
719
|
-
function
|
|
720
|
-
a = abytes2(a);
|
|
721
|
-
b = abytes2(b);
|
|
722
|
-
if (a.length !== b.length)
|
|
723
|
-
return false;
|
|
724
|
-
let diff = 0;
|
|
725
|
-
for (let i = 0; i < a.length; i++)
|
|
726
|
-
diff |= a[i] ^ b[i];
|
|
727
|
-
return diff === 0;
|
|
728
|
-
}
|
|
729
|
-
function copyBytes(bytes) {
|
|
785
|
+
function copyBytes2(bytes) {
|
|
730
786
|
return Uint8Array.from(abytes2(bytes));
|
|
731
787
|
}
|
|
732
|
-
function
|
|
733
|
-
|
|
734
|
-
throw new TypeError("ascii string expected, got " + typeof ascii);
|
|
735
|
-
return Uint8Array.from(ascii, (c, i) => {
|
|
736
|
-
const charCode = c.charCodeAt(0);
|
|
737
|
-
if (c.length !== 1 || charCode > 127) {
|
|
738
|
-
throw new RangeError(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
739
|
-
}
|
|
740
|
-
return charCode;
|
|
741
|
-
});
|
|
788
|
+
function isPosBig(n) {
|
|
789
|
+
return typeof n === "bigint" && _0n <= n;
|
|
742
790
|
}
|
|
743
|
-
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
744
791
|
function inRange(n, min, max2) {
|
|
745
792
|
return isPosBig(n) && isPosBig(min) && isPosBig(max2) && min <= n && n < max2;
|
|
746
793
|
}
|
|
@@ -751,32 +798,32 @@ function aInRange(title, n, min, max2) {
|
|
|
751
798
|
function bitLen(n) {
|
|
752
799
|
if (n < _0n)
|
|
753
800
|
throw new Error("expected non-negative bigint, got " + n);
|
|
754
|
-
|
|
755
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
756
|
-
;
|
|
757
|
-
return len;
|
|
801
|
+
return n === _0n ? 0 : n.toString(2).length;
|
|
758
802
|
}
|
|
759
|
-
var bitMask = (n) =>
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
803
|
+
var bitMask = (n) => {
|
|
804
|
+
asafenumber(n, "n");
|
|
805
|
+
return (_1n << BigInt(n)) - _1n;
|
|
806
|
+
};
|
|
807
|
+
function validateObject(object, fields = {}, optFields = {}, title = "object") {
|
|
808
|
+
aobject2(object, title);
|
|
809
|
+
aobject2(fields, "fields");
|
|
810
|
+
aobject2(optFields, "optFields");
|
|
763
811
|
function checkField(fieldName, expectedType, isOpt) {
|
|
764
|
-
|
|
765
|
-
throw new TypeError(`param "${fieldName}" is invalid: expected own property`);
|
|
812
|
+
const label = title === "object" ? `param "${String(fieldName)}"` : `"${title}.${String(fieldName)}"`;
|
|
766
813
|
const val = object[fieldName];
|
|
814
|
+
if (!Object.hasOwn(object, fieldName) && (isOpt ? val !== void 0 : expectedType !== "function")) {
|
|
815
|
+
throw new TypeError(`${label} is invalid: expected own property`);
|
|
816
|
+
}
|
|
767
817
|
if (isOpt && val === void 0)
|
|
768
818
|
return;
|
|
769
819
|
const current = typeof val;
|
|
770
820
|
if (current !== expectedType || val === null)
|
|
771
|
-
throw new TypeError(
|
|
821
|
+
throw new TypeError(`${label} is invalid: expected ${expectedType}, got ${current}`);
|
|
772
822
|
}
|
|
773
823
|
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
774
824
|
iter(fields, false);
|
|
775
825
|
iter(optFields, true);
|
|
776
826
|
}
|
|
777
|
-
var notImplemented = () => {
|
|
778
|
-
throw new Error("not implemented");
|
|
779
|
-
};
|
|
780
827
|
|
|
781
828
|
// ../../node_modules/@noble/curves/abstract/modular.js
|
|
782
829
|
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
@@ -788,17 +835,67 @@ var _5n = /* @__PURE__ */ BigInt(5);
|
|
|
788
835
|
var _7n = /* @__PURE__ */ BigInt(7);
|
|
789
836
|
var _8n = /* @__PURE__ */ BigInt(8);
|
|
790
837
|
var _9n = /* @__PURE__ */ BigInt(9);
|
|
838
|
+
var _15n = /* @__PURE__ */ BigInt(15);
|
|
791
839
|
var _16n = /* @__PURE__ */ BigInt(16);
|
|
840
|
+
var POW_WINDOWED_MIN = /* @__PURE__ */ BigInt("0x10000000000000000");
|
|
792
841
|
function mod(a, b) {
|
|
793
842
|
if (b <= _0n2)
|
|
794
843
|
throw new Error("mod: expected positive modulus, got " + b);
|
|
795
844
|
const result = a % b;
|
|
796
845
|
return result >= _0n2 ? result : b + result;
|
|
797
846
|
}
|
|
798
|
-
function
|
|
847
|
+
function pow(num, power, modulo) {
|
|
848
|
+
if (modulo <= _1n2)
|
|
849
|
+
throw new Error("pow: expected modulus > 1, got " + modulo);
|
|
850
|
+
if (typeof power !== "bigint")
|
|
851
|
+
throw new TypeError("invalid exponent: expected bigint, got " + typeof power);
|
|
852
|
+
if (power < _0n2)
|
|
853
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
854
|
+
if (power === _0n2)
|
|
855
|
+
return _1n2;
|
|
856
|
+
if (power === _1n2)
|
|
857
|
+
return num;
|
|
858
|
+
let d = num % modulo;
|
|
859
|
+
if (d < _0n2)
|
|
860
|
+
d += modulo;
|
|
861
|
+
if (power < POW_WINDOWED_MIN) {
|
|
862
|
+
let p2 = _1n2;
|
|
863
|
+
while (power > _0n2) {
|
|
864
|
+
if (power & _1n2)
|
|
865
|
+
p2 = p2 * d % modulo;
|
|
866
|
+
d = d * d % modulo;
|
|
867
|
+
power >>= _1n2;
|
|
868
|
+
}
|
|
869
|
+
return p2;
|
|
870
|
+
}
|
|
871
|
+
const digits = [];
|
|
872
|
+
while (power > _0n2) {
|
|
873
|
+
digits.push(Number(power & _15n));
|
|
874
|
+
power >>= _4n;
|
|
875
|
+
}
|
|
876
|
+
const table = new Array(16);
|
|
877
|
+
table[0] = _1n2;
|
|
878
|
+
table[1] = d;
|
|
879
|
+
for (let i2 = 2; i2 < 16; i2++)
|
|
880
|
+
table[i2] = table[i2 - 1] * d % modulo;
|
|
881
|
+
let p = table[digits[digits.length - 1]];
|
|
882
|
+
for (let w = digits.length - 2; w >= 0; w--) {
|
|
883
|
+
p = p * p % modulo;
|
|
884
|
+
p = p * p % modulo;
|
|
885
|
+
p = p * p % modulo;
|
|
886
|
+
p = p * p % modulo;
|
|
887
|
+
const digit = digits[w];
|
|
888
|
+
if (digit !== 0)
|
|
889
|
+
p = p * table[digit] % modulo;
|
|
890
|
+
}
|
|
891
|
+
return p;
|
|
892
|
+
}
|
|
893
|
+
function pow2(x2, power, modulo) {
|
|
894
|
+
if (modulo <= _1n2)
|
|
895
|
+
throw new Error("pow2: expected modulus > 1, got " + modulo);
|
|
799
896
|
if (power < _0n2)
|
|
800
897
|
throw new Error("pow2: expected non-negative exponent, got " + power);
|
|
801
|
-
let res =
|
|
898
|
+
let res = x2;
|
|
802
899
|
while (power-- > _0n2) {
|
|
803
900
|
res *= res;
|
|
804
901
|
res %= modulo;
|
|
@@ -808,28 +905,31 @@ function pow2(x, power, modulo) {
|
|
|
808
905
|
function invert(number, modulo) {
|
|
809
906
|
if (number === _0n2)
|
|
810
907
|
throw new Error("invert: expected non-zero number");
|
|
811
|
-
if (modulo <=
|
|
812
|
-
throw new Error("invert: expected
|
|
908
|
+
if (modulo <= _1n2)
|
|
909
|
+
throw new Error("invert: expected modulus > 1, got " + modulo);
|
|
813
910
|
let a = mod(number, modulo);
|
|
814
911
|
let b = modulo;
|
|
815
|
-
let
|
|
912
|
+
let x2 = _0n2, u = _1n2;
|
|
816
913
|
while (a !== _0n2) {
|
|
817
914
|
const q = b / a;
|
|
818
915
|
const r = b - a * q;
|
|
819
|
-
const m =
|
|
820
|
-
|
|
821
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
916
|
+
const m = x2 - u * q;
|
|
917
|
+
b = a, a = r, x2 = u, u = m;
|
|
822
918
|
}
|
|
823
919
|
const gcd = b;
|
|
824
920
|
if (gcd !== _1n2)
|
|
825
921
|
throw new Error("invert: does not exist");
|
|
826
|
-
return mod(
|
|
922
|
+
return mod(x2, modulo);
|
|
827
923
|
}
|
|
828
924
|
function assertIsSquare(Fp2, root, n) {
|
|
829
925
|
const F = Fp2;
|
|
830
926
|
if (!F.eql(F.sqr(root), n))
|
|
831
927
|
throw new Error("Cannot find square root");
|
|
832
928
|
}
|
|
929
|
+
function aoddModulus(order, fnName) {
|
|
930
|
+
if ((order & _1n2) === _0n2)
|
|
931
|
+
throw new Error(fnName + ": expected odd modulus, got " + order);
|
|
932
|
+
}
|
|
833
933
|
function sqrt3mod4(Fp2, n) {
|
|
834
934
|
const F = Fp2;
|
|
835
935
|
const p1div4 = (F.ORDER + _1n2) / _4n;
|
|
@@ -843,8 +943,8 @@ function sqrt5mod8(Fp2, n) {
|
|
|
843
943
|
const n2 = F.mul(n, _2n);
|
|
844
944
|
const v = F.pow(n2, p5div8);
|
|
845
945
|
const nv = F.mul(n, v);
|
|
846
|
-
const
|
|
847
|
-
const root = F.mul(nv, F.sub(
|
|
946
|
+
const i2 = F.mul(F.mul(nv, _2n), v);
|
|
947
|
+
const root = F.mul(nv, F.sub(i2, F.ONE));
|
|
848
948
|
assertIsSquare(F, root, n);
|
|
849
949
|
return root;
|
|
850
950
|
}
|
|
@@ -874,6 +974,7 @@ function sqrt9mod16(P) {
|
|
|
874
974
|
function tonelliShanks(P) {
|
|
875
975
|
if (P < _3n)
|
|
876
976
|
throw new Error("sqrt is not defined for small field");
|
|
977
|
+
aoddModulus(P, "tonelliShanks");
|
|
877
978
|
let Q = P - _1n2;
|
|
878
979
|
let S = 0;
|
|
879
980
|
while (Q % _2n === _0n2) {
|
|
@@ -902,18 +1003,18 @@ function tonelliShanks(P) {
|
|
|
902
1003
|
let R = F.pow(n, Q1div2);
|
|
903
1004
|
while (!F.eql(t, F.ONE)) {
|
|
904
1005
|
if (F.is0(t))
|
|
905
|
-
|
|
906
|
-
let
|
|
1006
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
1007
|
+
let i2 = 1;
|
|
907
1008
|
let t_tmp = F.sqr(t);
|
|
908
1009
|
while (!F.eql(t_tmp, F.ONE)) {
|
|
909
|
-
|
|
1010
|
+
i2++;
|
|
910
1011
|
t_tmp = F.sqr(t_tmp);
|
|
911
|
-
if (
|
|
1012
|
+
if (i2 === M)
|
|
912
1013
|
throw new Error("Cannot find square root");
|
|
913
1014
|
}
|
|
914
|
-
const exponent = _1n2 << BigInt(M -
|
|
1015
|
+
const exponent = _1n2 << BigInt(M - i2 - 1);
|
|
915
1016
|
const b = F.pow(c, exponent);
|
|
916
|
-
M =
|
|
1017
|
+
M = i2;
|
|
917
1018
|
c = F.sqr(b);
|
|
918
1019
|
t = F.mul(t, c);
|
|
919
1020
|
R = F.mul(R, b);
|
|
@@ -922,6 +1023,7 @@ function tonelliShanks(P) {
|
|
|
922
1023
|
};
|
|
923
1024
|
}
|
|
924
1025
|
function FpSqrt(P) {
|
|
1026
|
+
aoddModulus(P, "Fp.sqrt");
|
|
925
1027
|
if (P % _4n === _3n)
|
|
926
1028
|
return sqrt3mod4;
|
|
927
1029
|
if (P % _8n === _5n)
|
|
@@ -951,62 +1053,44 @@ var FIELD_FIELDS = [
|
|
|
951
1053
|
"sqrN"
|
|
952
1054
|
];
|
|
953
1055
|
function validateField(field) {
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
BITS: "number"
|
|
958
|
-
};
|
|
959
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
960
|
-
map[val] = "function";
|
|
961
|
-
return map;
|
|
962
|
-
}, initial);
|
|
963
|
-
validateObject(field, opts);
|
|
1056
|
+
aobject2(field, "field");
|
|
1057
|
+
if (typeof field.ORDER !== "bigint")
|
|
1058
|
+
throw new TypeError('param "ORDER" is invalid: expected bigint, got ' + typeof field.ORDER);
|
|
964
1059
|
asafenumber(field.BYTES, "BYTES");
|
|
965
1060
|
asafenumber(field.BITS, "BITS");
|
|
1061
|
+
for (const name of FIELD_FIELDS)
|
|
1062
|
+
afunction(field[name], "field." + name);
|
|
966
1063
|
if (field.BYTES < 1 || field.BITS < 1)
|
|
967
1064
|
throw new Error("invalid field: expected BYTES/BITS > 0");
|
|
968
1065
|
if (field.ORDER <= _1n2)
|
|
969
1066
|
throw new Error("invalid field: expected ORDER > 1, got " + field.ORDER);
|
|
970
1067
|
return field;
|
|
971
1068
|
}
|
|
972
|
-
function FpPow(Fp2, num, power) {
|
|
973
|
-
const F = Fp2;
|
|
974
|
-
if (power < _0n2)
|
|
975
|
-
throw new Error("invalid exponent, negatives unsupported");
|
|
976
|
-
if (power === _0n2)
|
|
977
|
-
return F.ONE;
|
|
978
|
-
if (power === _1n2)
|
|
979
|
-
return num;
|
|
980
|
-
let p = F.ONE;
|
|
981
|
-
let d = num;
|
|
982
|
-
while (power > _0n2) {
|
|
983
|
-
if (power & _1n2)
|
|
984
|
-
p = F.mul(p, d);
|
|
985
|
-
d = F.sqr(d);
|
|
986
|
-
power >>= _1n2;
|
|
987
|
-
}
|
|
988
|
-
return p;
|
|
989
|
-
}
|
|
990
1069
|
function FpInvertBatch(Fp2, nums, passZero = false) {
|
|
1070
|
+
validateField(Fp2);
|
|
1071
|
+
aarray(nums, "nums");
|
|
1072
|
+
abool(passZero, "passZero");
|
|
991
1073
|
const F = Fp2;
|
|
992
1074
|
const inverted = new Array(nums.length).fill(passZero ? F.ZERO : void 0);
|
|
993
|
-
const multipliedAcc = nums.reduce((acc, num,
|
|
1075
|
+
const multipliedAcc = nums.reduce((acc, num, i2) => {
|
|
994
1076
|
if (F.is0(num))
|
|
995
1077
|
return acc;
|
|
996
|
-
inverted[
|
|
1078
|
+
inverted[i2] = acc;
|
|
997
1079
|
return F.mul(acc, num);
|
|
998
1080
|
}, F.ONE);
|
|
999
1081
|
const invertedAcc = F.inv(multipliedAcc);
|
|
1000
|
-
nums.reduceRight((acc, num,
|
|
1082
|
+
nums.reduceRight((acc, num, i2) => {
|
|
1001
1083
|
if (F.is0(num))
|
|
1002
1084
|
return acc;
|
|
1003
|
-
inverted[
|
|
1085
|
+
inverted[i2] = F.mul(acc, inverted[i2]);
|
|
1004
1086
|
return F.mul(acc, num);
|
|
1005
1087
|
}, invertedAcc);
|
|
1006
1088
|
return inverted;
|
|
1007
1089
|
}
|
|
1008
1090
|
function FpLegendre(Fp2, n) {
|
|
1091
|
+
validateField(Fp2);
|
|
1009
1092
|
const F = Fp2;
|
|
1093
|
+
aoddModulus(F.ORDER, "FpLegendre");
|
|
1010
1094
|
const p1mod2 = (F.ORDER - _1n2) / _2n;
|
|
1011
1095
|
const powered = F.pow(n, p1mod2);
|
|
1012
1096
|
const yes = F.eql(powered, F.ONE);
|
|
@@ -1025,7 +1109,7 @@ function nLength(n, nBitLength) {
|
|
|
1025
1109
|
throw new Error("invalid n length: expected positive bit length, got " + nBitLength);
|
|
1026
1110
|
const bits2 = bitLen(n);
|
|
1027
1111
|
if (nBitLength !== void 0 && nBitLength < bits2)
|
|
1028
|
-
throw new Error(`invalid n length: expected
|
|
1112
|
+
throw new Error(`invalid n length: expected nBitLength (${nBitLength}) >= bitLen(n) (${bits2})`);
|
|
1029
1113
|
const _nBitLength = nBitLength !== void 0 ? nBitLength : bits2;
|
|
1030
1114
|
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
1031
1115
|
return { nBitLength: _nBitLength, nByteLength };
|
|
@@ -1102,7 +1186,7 @@ var _Field = class {
|
|
|
1102
1186
|
return mod(lhs * rhs, this.ORDER);
|
|
1103
1187
|
}
|
|
1104
1188
|
pow(num, power) {
|
|
1105
|
-
return
|
|
1189
|
+
return pow(num, power, this.ORDER);
|
|
1106
1190
|
}
|
|
1107
1191
|
div(lhs, rhs) {
|
|
1108
1192
|
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
@@ -1156,7 +1240,7 @@ var _Field = class {
|
|
|
1156
1240
|
}
|
|
1157
1241
|
// TODO: we don't need it here, move out to separate fn
|
|
1158
1242
|
invertBatch(lst) {
|
|
1159
|
-
return FpInvertBatch(this, lst);
|
|
1243
|
+
return FpInvertBatch(this, lst, true);
|
|
1160
1244
|
}
|
|
1161
1245
|
// We can't move this out because Fp6, Fp12 implement it
|
|
1162
1246
|
// and it's unclear what to return in there.
|
|
@@ -1165,192 +1249,357 @@ var _Field = class {
|
|
|
1165
1249
|
return condition ? b : a;
|
|
1166
1250
|
}
|
|
1167
1251
|
};
|
|
1168
|
-
Object.freeze(_Field.prototype);
|
|
1169
1252
|
function Field(ORDER, opts = {}) {
|
|
1253
|
+
Object.freeze(_Field.prototype);
|
|
1170
1254
|
return new _Field(ORDER, opts);
|
|
1171
1255
|
}
|
|
1172
1256
|
|
|
1173
1257
|
// ../../node_modules/@noble/curves/abstract/curve.js
|
|
1174
1258
|
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
1175
1259
|
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1260
|
+
var _4n2 = /* @__PURE__ */ BigInt(4);
|
|
1261
|
+
var BLIND_BYTES = 16;
|
|
1262
|
+
var BLIND_BITS = 128;
|
|
1263
|
+
var FW_WINDOW = 5;
|
|
1264
|
+
var TABLE_BYTES_MAX = /* @__PURE__ */ (() => 2 ** 31)();
|
|
1265
|
+
function validatePointCons(Point) {
|
|
1266
|
+
const pc = Point;
|
|
1267
|
+
if (typeof pc !== "function")
|
|
1268
|
+
throw new TypeError('"Point" expected constructor, got type=' + typeof Point);
|
|
1269
|
+
afunction(pc.fromAffine, "Point.fromAffine");
|
|
1270
|
+
afunction(pc.fromBytes, "Point.fromBytes");
|
|
1271
|
+
afunction(pc.fromHex, "Point.fromHex");
|
|
1272
|
+
aobject2(pc.BASE, "Point.BASE");
|
|
1273
|
+
aobject2(pc.ZERO, "Point.ZERO");
|
|
1274
|
+
validateField(pc.Fp);
|
|
1275
|
+
validateField(pc.Fn);
|
|
1179
1276
|
}
|
|
1180
1277
|
function normalizeZ(c, points) {
|
|
1278
|
+
validatePointCons(c);
|
|
1279
|
+
validateMSMPoints(points, c);
|
|
1181
1280
|
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
1182
|
-
return points.map((p,
|
|
1183
|
-
}
|
|
1184
|
-
function validateW(W, bits2) {
|
|
1185
|
-
if (!Number.isSafeInteger(W) || W
|
|
1186
|
-
throw new Error("invalid window size, expected [
|
|
1187
|
-
}
|
|
1188
|
-
function
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1281
|
+
return points.map((p, i2) => c.fromAffine(p.toAffine(invertedZs[i2])));
|
|
1282
|
+
}
|
|
1283
|
+
function validateW(W, bits2, min = 1) {
|
|
1284
|
+
if (!Number.isSafeInteger(W) || W < min || W > bits2)
|
|
1285
|
+
throw new Error("invalid window size, expected [" + min + ".." + bits2 + "], got W=" + W);
|
|
1286
|
+
}
|
|
1287
|
+
function validateTableBytes(numPoints, fpBytes) {
|
|
1288
|
+
const bytes = numPoints * (4 * fpBytes + 128);
|
|
1289
|
+
if (bytes > TABLE_BYTES_MAX)
|
|
1290
|
+
throw new Error("invalid window size: table would need ~" + Math.ceil(bytes / 2 ** 20) + " MiB, max " + TABLE_BYTES_MAX / 2 ** 20 + " MiB");
|
|
1291
|
+
}
|
|
1292
|
+
function probeRandomBytes(randomBytes5, length) {
|
|
1293
|
+
if (randomBytes5 === void 0)
|
|
1294
|
+
return void 0;
|
|
1295
|
+
afunction(randomBytes5, "randomBytes");
|
|
1296
|
+
try {
|
|
1297
|
+
const probe = randomBytes5(length);
|
|
1298
|
+
if (!isBytes2(probe) || probe.length !== length)
|
|
1299
|
+
return void 0;
|
|
1300
|
+
} catch {
|
|
1301
|
+
return void 0;
|
|
1302
|
+
}
|
|
1303
|
+
return randomBytes5;
|
|
1304
|
+
}
|
|
1305
|
+
function validateMSMPoints(points, c) {
|
|
1306
|
+
aarray(points, "points");
|
|
1307
|
+
points.forEach((p, i2) => {
|
|
1308
|
+
if (!(p instanceof c))
|
|
1309
|
+
throw new Error("invalid point at index " + i2);
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
function validateMSMScalars(scalars, field, maxScalar) {
|
|
1313
|
+
if (!Array.isArray(scalars))
|
|
1314
|
+
throw new Error("array of scalars expected");
|
|
1315
|
+
scalars.forEach((s, i2) => {
|
|
1316
|
+
const ok = maxScalar === void 0 ? field.isValid(s) : isPosBig(s) && s < maxScalar;
|
|
1317
|
+
if (!ok)
|
|
1318
|
+
throw new Error("invalid scalar at index " + i2);
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1214
1321
|
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
1215
|
-
function
|
|
1322
|
+
function getWindowSize(P) {
|
|
1216
1323
|
return pointWindowSizes.get(P) || 1;
|
|
1217
1324
|
}
|
|
1218
|
-
function
|
|
1325
|
+
function oddMultiples(p, size) {
|
|
1326
|
+
const dbl = p.double();
|
|
1327
|
+
const t = [p];
|
|
1328
|
+
for (let j = 1; j < size; j++)
|
|
1329
|
+
t.push(t[j - 1].add(dbl));
|
|
1330
|
+
return t;
|
|
1331
|
+
}
|
|
1332
|
+
function wnafDigits(n, W) {
|
|
1333
|
+
const size = 2 ** W;
|
|
1334
|
+
const half = size / 2;
|
|
1335
|
+
const mask = BigInt(size - 1);
|
|
1336
|
+
const d = [];
|
|
1337
|
+
while (n > _0n3) {
|
|
1338
|
+
let w = 0;
|
|
1339
|
+
if (n & _1n3) {
|
|
1340
|
+
w = Number(n & mask);
|
|
1341
|
+
if (w >= half)
|
|
1342
|
+
w -= size;
|
|
1343
|
+
n -= BigInt(w);
|
|
1344
|
+
}
|
|
1345
|
+
d.push(w);
|
|
1346
|
+
n >>= _1n3;
|
|
1347
|
+
}
|
|
1348
|
+
return d;
|
|
1349
|
+
}
|
|
1350
|
+
function signedWindowDigits(n, W, windows) {
|
|
1351
|
+
const size = 2 ** W;
|
|
1352
|
+
const half = size / 2;
|
|
1353
|
+
const mask = BigInt(size - 1);
|
|
1354
|
+
const shiftBy = BigInt(W);
|
|
1355
|
+
const d = [];
|
|
1356
|
+
for (let w = 0; w < windows; w++) {
|
|
1357
|
+
let v = Number(n & mask);
|
|
1358
|
+
n >>= shiftBy;
|
|
1359
|
+
if (v > half) {
|
|
1360
|
+
v -= size;
|
|
1361
|
+
n += _1n3;
|
|
1362
|
+
}
|
|
1363
|
+
d.push(v);
|
|
1364
|
+
}
|
|
1219
1365
|
if (n !== _0n3)
|
|
1220
|
-
throw new Error("invalid
|
|
1366
|
+
throw new Error("invalid wnaf");
|
|
1367
|
+
return d;
|
|
1368
|
+
}
|
|
1369
|
+
function wnafWalk(zero, tables, digits) {
|
|
1370
|
+
let max2 = 0;
|
|
1371
|
+
for (const d of digits)
|
|
1372
|
+
max2 = Math.max(max2, d.length);
|
|
1373
|
+
let acc = zero;
|
|
1374
|
+
for (let bit = max2 - 1; bit >= 0; bit--) {
|
|
1375
|
+
if (bit !== max2 - 1)
|
|
1376
|
+
acc = acc.double();
|
|
1377
|
+
for (let i2 = 0; i2 < digits.length; i2++) {
|
|
1378
|
+
const w = digits[i2][bit];
|
|
1379
|
+
if (w) {
|
|
1380
|
+
const item = tables[i2][Math.abs(w) - 1 >> 1];
|
|
1381
|
+
acc = acc.add(w < 0 ? item.negate() : item);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
return acc;
|
|
1221
1386
|
}
|
|
1222
|
-
var
|
|
1387
|
+
var ScalarMultiplier = class {
|
|
1388
|
+
Point;
|
|
1223
1389
|
BASE;
|
|
1224
1390
|
ZERO;
|
|
1225
|
-
|
|
1391
|
+
randomBytes;
|
|
1392
|
+
wnafPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
1393
|
+
baseCanBeBlinded;
|
|
1226
1394
|
bits;
|
|
1227
1395
|
// Parametrized with a given Point class (not individual point)
|
|
1228
|
-
constructor(Point,
|
|
1396
|
+
constructor(Point, randomBytes5) {
|
|
1397
|
+
validatePointCons(Point);
|
|
1398
|
+
this.randomBytes = probeRandomBytes(randomBytes5, BLIND_BYTES);
|
|
1399
|
+
this.Point = Point;
|
|
1229
1400
|
this.BASE = Point.BASE;
|
|
1230
1401
|
this.ZERO = Point.ZERO;
|
|
1231
|
-
this.
|
|
1232
|
-
this.bits = bits2;
|
|
1233
|
-
}
|
|
1234
|
-
// non-const time multiplication ladder
|
|
1235
|
-
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
1236
|
-
let d = elm;
|
|
1237
|
-
while (n > _0n3) {
|
|
1238
|
-
if (n & _1n3)
|
|
1239
|
-
p = p.add(d);
|
|
1240
|
-
d = d.double();
|
|
1241
|
-
n >>= _1n3;
|
|
1242
|
-
}
|
|
1243
|
-
return p;
|
|
1402
|
+
this.bits = Point.Fn.BITS;
|
|
1244
1403
|
}
|
|
1245
1404
|
/**
|
|
1246
|
-
* Creates a wNAF precomputation window
|
|
1247
|
-
*
|
|
1248
|
-
*
|
|
1249
|
-
*
|
|
1250
|
-
* -
|
|
1251
|
-
* - 𝑛 is the bitlength of the curve order.
|
|
1252
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
1405
|
+
* Creates a signed fixed-window wNAF precomputation table: for every window w, the
|
|
1406
|
+
* multiples `[1..2^(W−1)]⋅2^(w⋅W)⋅P`, flattened. All doublings are baked into the table,
|
|
1407
|
+
* so cached multiplication is additions-only. `windows = ceil(bits/W) + 1`: the extra
|
|
1408
|
+
* window absorbs the final carry of signed-digit recoding.
|
|
1409
|
+
* For a 256-bit curve and W=6, the table is 44⋅32 = 1408 points.
|
|
1253
1410
|
* @param point - Point instance
|
|
1254
1411
|
* @param W - window size
|
|
1255
|
-
* @
|
|
1412
|
+
* @param bits - scalar bitlength the table must cover
|
|
1256
1413
|
*/
|
|
1257
|
-
|
|
1258
|
-
const
|
|
1259
|
-
const
|
|
1260
|
-
|
|
1261
|
-
let base =
|
|
1262
|
-
for (let
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
points.push(base);
|
|
1414
|
+
buildWnafTable(point, W, bits2) {
|
|
1415
|
+
const windows = Math.ceil(bits2 / W) + 1;
|
|
1416
|
+
const half = 2 ** (W - 1);
|
|
1417
|
+
const comp = [];
|
|
1418
|
+
let base = point;
|
|
1419
|
+
for (let w = 0; w < windows; w++) {
|
|
1420
|
+
let acc = base;
|
|
1421
|
+
for (let i2 = 0; i2 < half; i2++) {
|
|
1422
|
+
comp.push(acc);
|
|
1423
|
+
acc = acc.add(base);
|
|
1268
1424
|
}
|
|
1269
|
-
|
|
1425
|
+
base = comp[comp.length - 1].double();
|
|
1270
1426
|
}
|
|
1271
|
-
return
|
|
1427
|
+
return { W, bits: bits2, windows, comp };
|
|
1272
1428
|
}
|
|
1273
1429
|
/**
|
|
1274
|
-
* Implements ec multiplication using precomputed
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1430
|
+
* Implements ec multiplication using precomputed signed fixed-window wNAF tables.
|
|
1431
|
+
* Constant-time: fixed window count with one table addition per window — zero digits feed
|
|
1432
|
+
* the fake accumulator — and no doublings; the lookup scans the whole window slice.
|
|
1433
|
+
* Scalar bounds are validated by the public entry points ({@link ScalarMultiplier.mulCT},
|
|
1434
|
+
* {@link ScalarMultiplier.mulCTBlinded}, {@link ScalarMultiplier.mulUnsafe});
|
|
1435
|
+
* signedWindowDigits throws if `n` exceeds the table.
|
|
1277
1436
|
* @returns real and fake (for const-time) points
|
|
1278
1437
|
*/
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1438
|
+
wnafCachedCT(precomputes, n) {
|
|
1439
|
+
const { W, windows, comp } = precomputes;
|
|
1440
|
+
const half = 2 ** (W - 1);
|
|
1441
|
+
const digits = signedWindowDigits(n, W, windows);
|
|
1282
1442
|
let p = this.ZERO;
|
|
1283
1443
|
let f = this.BASE;
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
const
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1444
|
+
for (let w = 0; w < windows; w++) {
|
|
1445
|
+
const digit = digits[w];
|
|
1446
|
+
const start = w * half;
|
|
1447
|
+
const idx = Math.abs(digit) - 1;
|
|
1448
|
+
let sel = comp[start];
|
|
1449
|
+
for (let i2 = 1; i2 < half; i2++)
|
|
1450
|
+
sel = i2 === idx ? comp[start + i2] : sel;
|
|
1451
|
+
const neg = sel.negate();
|
|
1452
|
+
if (digit === 0)
|
|
1453
|
+
f = f.add(comp[start]);
|
|
1454
|
+
else
|
|
1455
|
+
p = p.add(digit < 0 ? neg : sel);
|
|
1293
1456
|
}
|
|
1294
|
-
assert0(n);
|
|
1295
1457
|
return { p, f };
|
|
1296
1458
|
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
1304
|
-
const wo = calcWOpts(W, this.bits);
|
|
1305
|
-
for (let window = 0; window < wo.windows; window++) {
|
|
1306
|
-
if (n === _0n3)
|
|
1307
|
-
break;
|
|
1308
|
-
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1309
|
-
n = nextN;
|
|
1310
|
-
if (isZero) {
|
|
1311
|
-
continue;
|
|
1312
|
-
} else {
|
|
1313
|
-
const item = precomputes[offset];
|
|
1314
|
-
acc = acc.add(isNeg ? item.negate() : item);
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
assert0(n);
|
|
1318
|
-
return acc;
|
|
1319
|
-
}
|
|
1320
|
-
getPrecomputes(W, point, transform) {
|
|
1321
|
-
let comp = pointPrecomputes.get(point);
|
|
1459
|
+
// Cache key is point identity plus (W, bits); at most two entries exist per point (public-width
|
|
1460
|
+
// `Fn.BITS` and blinded `Fn.BITS + BLIND_BITS`). Callers must not reuse the same point with
|
|
1461
|
+
// incompatible `transform(...)` layouts and expect a separate cache entry.
|
|
1462
|
+
getWnafPrecomputes(W, point, bits2, transform) {
|
|
1463
|
+
let entries = this.wnafPrecomputes.get(point);
|
|
1464
|
+
let comp = entries?.find((entry) => entry.W === W && entry.bits === bits2);
|
|
1322
1465
|
if (!comp) {
|
|
1323
|
-
comp = this.
|
|
1324
|
-
if (
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1466
|
+
comp = this.buildWnafTable(point, W, bits2);
|
|
1467
|
+
if (typeof transform === "function")
|
|
1468
|
+
comp = { ...comp, comp: transform(comp.comp) };
|
|
1469
|
+
if (!entries) {
|
|
1470
|
+
entries = [];
|
|
1471
|
+
this.wnafPrecomputes.set(point, entries);
|
|
1328
1472
|
}
|
|
1473
|
+
entries.push(comp);
|
|
1329
1474
|
}
|
|
1330
1475
|
return comp;
|
|
1331
1476
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1477
|
+
assertPoint(point) {
|
|
1478
|
+
if (!(point instanceof this.Point))
|
|
1479
|
+
throw new TypeError('"point" expected Point instance, got type=' + typeof point);
|
|
1480
|
+
}
|
|
1481
|
+
// Shared prologue of the constant-time entry points. Rejects scalar 0: in key/signature-style
|
|
1482
|
+
// callers a zero scalar means broken upstream plumbing, and concrete Points already reject it.
|
|
1483
|
+
// Uses inRange instead of Fn.isValidNot0: validateField() only certifies the arithmetic subset.
|
|
1484
|
+
validateMulInput(point, scalar) {
|
|
1485
|
+
this.assertPoint(point);
|
|
1486
|
+
if (!inRange(scalar, _1n3, this.Point.Fn.ORDER))
|
|
1487
|
+
throw new Error("invalid scalar");
|
|
1335
1488
|
}
|
|
1336
|
-
|
|
1337
|
-
|
|
1489
|
+
// Constant-time dispatch shared by mulCT / mulCTBlinded. Un-precomputed points (W===1, e.g.
|
|
1490
|
+
// ECDH peer keys) skip building a throwaway cached table in favor of a small fixed-window
|
|
1491
|
+
// multiply. `n` must be < 2^bits.
|
|
1492
|
+
runCT(point, n, bits2, transform) {
|
|
1493
|
+
const W = getWindowSize(point);
|
|
1338
1494
|
if (W === 1)
|
|
1339
|
-
return this.
|
|
1340
|
-
return this.
|
|
1495
|
+
return this.fixedWindowCT(point, n, bits2);
|
|
1496
|
+
return this.wnafCachedCT(this.getWnafPrecomputes(W, point, bits2, transform), n);
|
|
1497
|
+
}
|
|
1498
|
+
mulCT(point, scalar, transform) {
|
|
1499
|
+
this.validateMulInput(point, scalar);
|
|
1500
|
+
return this.runCT(point, scalar, this.bits, transform);
|
|
1501
|
+
}
|
|
1502
|
+
mulCTBlinded(point, scalar, transform) {
|
|
1503
|
+
this.validateMulInput(point, scalar);
|
|
1504
|
+
if (this.randomBytes === void 0)
|
|
1505
|
+
throw new Error("randomBytes is required for scalar blinding");
|
|
1506
|
+
const bits2 = this.Point.Fn.BITS + BLIND_BITS;
|
|
1507
|
+
const blind = this.randomBytes(BLIND_BYTES);
|
|
1508
|
+
if (!isBytes2(blind) || blind.length !== BLIND_BYTES)
|
|
1509
|
+
throw new Error("randomBytes returned invalid byte array");
|
|
1510
|
+
blind[0] = blind[0] & 63 | 128;
|
|
1511
|
+
const n = scalar + bytesToNumberBE(blind) * this.Point.Fn.ORDER;
|
|
1512
|
+
return this.runCT(point, n, bits2, transform);
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Constant-time multiplication `n*point` for an un-precomputed point, via a small fixed window.
|
|
1516
|
+
* A cached wNAF table only pays off when reused; a flat 2^FW_WINDOW table (`size-1` adds) is
|
|
1517
|
+
* far cheaper to build for a single use. The point-operation sequence is independent of `n`:
|
|
1518
|
+
* build the table, then per window exactly FW_WINDOW doublings, a data-oblivious scan over
|
|
1519
|
+
* every table entry, and one addition (adds the identity when the window digit is 0 — never
|
|
1520
|
+
* skipped).
|
|
1521
|
+
*
|
|
1522
|
+
* `n` must be `< 2^bits`. Assumes complete addition (adding the identity costs the same as any
|
|
1523
|
+
* add), which holds for the Weierstrass/Edwards point types used here. The table is left in
|
|
1524
|
+
* projective form (no normalizeZ): normalizing this small a table costs more than the
|
|
1525
|
+
* mixed-add savings it would buy for a single multiply.
|
|
1526
|
+
* @returns real point `p`; `f` duplicates it only to match {@link wnafCachedCT}'s return shape
|
|
1527
|
+
* (this path needs no fake accumulator — its op-count is already scalar-independent).
|
|
1528
|
+
*/
|
|
1529
|
+
fixedWindowCT(point, n, bits2) {
|
|
1530
|
+
const W = FW_WINDOW;
|
|
1531
|
+
const size = 1 << W;
|
|
1532
|
+
const mask = bitMask(W);
|
|
1533
|
+
const table = new Array(size);
|
|
1534
|
+
table[0] = this.ZERO;
|
|
1535
|
+
for (let i2 = 1; i2 < size; i2++)
|
|
1536
|
+
table[i2] = table[i2 - 1].add(point);
|
|
1537
|
+
const windows = Math.ceil(bits2 / W);
|
|
1538
|
+
let acc = this.ZERO;
|
|
1539
|
+
for (let window = windows - 1; window >= 0; window--) {
|
|
1540
|
+
if (window !== windows - 1)
|
|
1541
|
+
for (let d = 0; d < W; d++)
|
|
1542
|
+
acc = acc.double();
|
|
1543
|
+
const digit = Number(n >> BigInt(window * W) & mask);
|
|
1544
|
+
let sel = table[0];
|
|
1545
|
+
for (let i2 = 1; i2 < size; i2++)
|
|
1546
|
+
sel = i2 === digit ? table[i2] : sel;
|
|
1547
|
+
acc = acc.add(sel);
|
|
1548
|
+
}
|
|
1549
|
+
return { p: acc, f: acc };
|
|
1341
1550
|
}
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1551
|
+
shouldBlind(point, cofactor) {
|
|
1552
|
+
if (this.randomBytes === void 0)
|
|
1553
|
+
return false;
|
|
1554
|
+
if (cofactor === _1n3)
|
|
1555
|
+
return true;
|
|
1556
|
+
if (point !== this.BASE)
|
|
1557
|
+
return false;
|
|
1558
|
+
if (this.baseCanBeBlinded === void 0)
|
|
1559
|
+
this.baseCanBeBlinded = this.mulUnsafe(this.BASE, this.Point.Fn.ORDER).is0();
|
|
1560
|
+
return this.baseCanBeBlinded;
|
|
1561
|
+
}
|
|
1562
|
+
mulSecret(point, scalar, cofactor, transform) {
|
|
1563
|
+
return this.shouldBlind(point, cofactor) ? this.mulCTBlinded(point, scalar, transform) : this.mulCT(point, scalar, transform);
|
|
1564
|
+
}
|
|
1565
|
+
mulUnsafe(point, scalar, transform) {
|
|
1566
|
+
this.assertPoint(point);
|
|
1567
|
+
if (!isPosBig(scalar))
|
|
1568
|
+
throw new Error("invalid scalar");
|
|
1569
|
+
const W = getWindowSize(point);
|
|
1570
|
+
if (W === 1 || scalar >= this.Point.Fn.ORDER)
|
|
1571
|
+
return mulAddUnsafe(this.Point, [point], [scalar], true);
|
|
1572
|
+
const precomputes = this.getWnafPrecomputes(W, point, this.bits, transform);
|
|
1573
|
+
return this.wnafCachedCT(precomputes, scalar).p;
|
|
1574
|
+
}
|
|
1575
|
+
// Remembers the window size used for precomputed wNAF multiplication of the given point
|
|
1576
|
+
// and drops any previously built tables. Usually only the base point is precomputed.
|
|
1577
|
+
// W=1 resets the point to the un-precomputed (table-less) paths.
|
|
1578
|
+
// W is additionally capped so tables stay under ~2 GiB ({@link TABLE_BYTES_MAX}).
|
|
1579
|
+
setWindowSize(point, W) {
|
|
1580
|
+
this.assertPoint(point);
|
|
1346
1581
|
validateW(W, this.bits);
|
|
1347
|
-
|
|
1348
|
-
|
|
1582
|
+
const windows = Math.ceil((this.bits + BLIND_BITS) / W) + 1;
|
|
1583
|
+
validateTableBytes(windows * 2 ** (W - 1), this.Point.Fp.BYTES);
|
|
1584
|
+
pointWindowSizes.set(point, W);
|
|
1585
|
+
this.wnafPrecomputes.delete(point);
|
|
1349
1586
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1587
|
+
// True when a window size is set: tables themselves are built lazily on first multiply.
|
|
1588
|
+
hasWindowSize(point) {
|
|
1589
|
+
return getWindowSize(point) !== 1;
|
|
1352
1590
|
}
|
|
1353
1591
|
};
|
|
1592
|
+
function mulAddUnsafe(c, points, scalars, allowOversized = false) {
|
|
1593
|
+
validatePointCons(c);
|
|
1594
|
+
validateMSMPoints(points, c);
|
|
1595
|
+
abool(allowOversized, "allowOversized");
|
|
1596
|
+
validateMSMScalars(scalars, c.Fn, allowOversized ? c.Fn.ORDER ** _4n2 : void 0);
|
|
1597
|
+
if (points.length !== scalars.length)
|
|
1598
|
+
throw new Error("arrays of points and scalars must have equal length");
|
|
1599
|
+
const tables = points.map((p) => oddMultiples(p, 4));
|
|
1600
|
+
const digits = scalars.map((n) => wnafDigits(n, 4));
|
|
1601
|
+
return wnafWalk(c.ZERO, tables, digits);
|
|
1602
|
+
}
|
|
1354
1603
|
function createField(order, field, isLE3) {
|
|
1355
1604
|
if (field) {
|
|
1356
1605
|
if (field.ORDER !== order)
|
|
@@ -1362,17 +1611,20 @@ function createField(order, field, isLE3) {
|
|
|
1362
1611
|
}
|
|
1363
1612
|
}
|
|
1364
1613
|
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
1614
|
+
if (type !== "weierstrass" && type !== "edwards")
|
|
1615
|
+
throw new Error('expected curve type "weierstrass" or "edwards"');
|
|
1365
1616
|
if (FpFnLE === void 0)
|
|
1366
1617
|
FpFnLE = type === "edwards";
|
|
1367
1618
|
if (!CURVE || typeof CURVE !== "object")
|
|
1368
1619
|
throw new Error(`expected valid ${type} CURVE object`);
|
|
1620
|
+
validateObject(curveOpts);
|
|
1369
1621
|
for (const p of ["p", "n", "h"]) {
|
|
1370
1622
|
const val = CURVE[p];
|
|
1371
|
-
if (!(
|
|
1623
|
+
if (!(isPosBig(val) && val !== _0n3))
|
|
1372
1624
|
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
1373
1625
|
}
|
|
1374
1626
|
const Fp2 = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
1375
|
-
const
|
|
1627
|
+
const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
1376
1628
|
const _b2 = type === "weierstrass" ? "b" : "d";
|
|
1377
1629
|
const params = ["Gx", "Gy", "a", _b2];
|
|
1378
1630
|
for (const p of params) {
|
|
@@ -1380,7 +1632,7 @@ function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
|
1380
1632
|
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
1381
1633
|
}
|
|
1382
1634
|
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
1383
|
-
return { CURVE, Fp: Fp2, Fn
|
|
1635
|
+
return { CURVE, Fp: Fp2, Fn };
|
|
1384
1636
|
}
|
|
1385
1637
|
function createKeygen(randomSecretKey, getPublicKey) {
|
|
1386
1638
|
return function keygen(seed) {
|
|
@@ -1393,23 +1645,34 @@ function createKeygen(randomSecretKey, getPublicKey) {
|
|
|
1393
1645
|
var _0n4 = /* @__PURE__ */ BigInt(0);
|
|
1394
1646
|
var _1n4 = /* @__PURE__ */ BigInt(1);
|
|
1395
1647
|
var _2n2 = /* @__PURE__ */ BigInt(2);
|
|
1648
|
+
var _4n3 = /* @__PURE__ */ BigInt(4);
|
|
1396
1649
|
var _8n2 = /* @__PURE__ */ BigInt(8);
|
|
1397
|
-
function isEdValidXY(Fp2, CURVE,
|
|
1398
|
-
const
|
|
1650
|
+
function isEdValidXY(Fp2, CURVE, x2, y) {
|
|
1651
|
+
const x22 = Fp2.sqr(x2);
|
|
1399
1652
|
const y2 = Fp2.sqr(y);
|
|
1400
|
-
const left = Fp2.add(Fp2.mul(CURVE.a,
|
|
1401
|
-
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(
|
|
1653
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x22), y2);
|
|
1654
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x22, y2)));
|
|
1402
1655
|
return Fp2.eql(left, right);
|
|
1403
1656
|
}
|
|
1404
1657
|
function edwards(params, extraOpts = {}) {
|
|
1658
|
+
validateObject(extraOpts, {}, {}, "extraOpts");
|
|
1405
1659
|
const opts = extraOpts;
|
|
1406
1660
|
const validated = createCurveFields("edwards", params, opts, opts.FpFnLE);
|
|
1407
|
-
const { Fp: Fp2, Fn
|
|
1661
|
+
const { Fp: Fp2, Fn } = validated;
|
|
1408
1662
|
let CURVE = validated.CURVE;
|
|
1409
1663
|
const { h: cofactor } = CURVE;
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1664
|
+
if (FpLegendre(Fp2, CURVE.a) !== 1)
|
|
1665
|
+
throw new Error("edwards: CURVE.a must be a square in Fp for complete addition formulas");
|
|
1666
|
+
if (FpLegendre(Fp2, CURVE.d) !== -1)
|
|
1667
|
+
throw new Error("edwards: CURVE.d must be a non-square in Fp for complete addition formulas");
|
|
1668
|
+
validateObject(opts, {}, { uvRatio: "function", randomBytes: "function" });
|
|
1669
|
+
const randomBytes5 = opts.randomBytes === void 0 ? randomBytes2 : opts.randomBytes;
|
|
1670
|
+
const MASK = _2n2 << BigInt(Fp2.BYTES * 8) - _1n4;
|
|
1671
|
+
function isOdd(n) {
|
|
1672
|
+
if (!Fp2.isOdd)
|
|
1673
|
+
throw new Error("Field does not have .isOdd()");
|
|
1674
|
+
return Fp2.isOdd(n);
|
|
1675
|
+
}
|
|
1413
1676
|
const uvRatio2 = opts.uvRatio === void 0 ? (u, v) => {
|
|
1414
1677
|
try {
|
|
1415
1678
|
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
@@ -1419,6 +1682,7 @@ function edwards(params, extraOpts = {}) {
|
|
|
1419
1682
|
} : opts.uvRatio;
|
|
1420
1683
|
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
1421
1684
|
throw new Error("bad curve params: generator point");
|
|
1685
|
+
const mulA = Fp2.eql(CURVE.a, Fp2.neg(Fp2.ONE)) ? (x2) => Fp2.neg(x2) : Fp2.eql(CURVE.a, Fp2.ONE) ? (x2) => x2 : (x2) => Fp2.mul(CURVE.a, x2);
|
|
1422
1686
|
function acoord(title, n, banZero = false) {
|
|
1423
1687
|
const min = banZero ? _1n4 : _0n4;
|
|
1424
1688
|
aInRange("coordinate " + title, n, min, MASK);
|
|
@@ -1429,15 +1693,10 @@ function edwards(params, extraOpts = {}) {
|
|
|
1429
1693
|
throw new Error("EdwardsPoint expected");
|
|
1430
1694
|
}
|
|
1431
1695
|
class Point {
|
|
1432
|
-
|
|
1433
|
-
static
|
|
1434
|
-
// zero / infinity / identity point
|
|
1435
|
-
static ZERO = new Point(_0n4, _1n4, _1n4, _0n4);
|
|
1436
|
-
// 0, 1, 1, 0
|
|
1437
|
-
// math field
|
|
1696
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp2.ONE, Fp2.mul(CURVE.Gx, CURVE.Gy));
|
|
1697
|
+
static ZERO = new Point(Fp2.ZERO, Fp2.ONE, Fp2.ONE, Fp2.ZERO);
|
|
1438
1698
|
static Fp = Fp2;
|
|
1439
|
-
|
|
1440
|
-
static Fn = Fn2;
|
|
1699
|
+
static Fn = Fn;
|
|
1441
1700
|
X;
|
|
1442
1701
|
Y;
|
|
1443
1702
|
Z;
|
|
@@ -1460,36 +1719,36 @@ function edwards(params, extraOpts = {}) {
|
|
|
1460
1719
|
static fromAffine(p) {
|
|
1461
1720
|
if (p instanceof Point)
|
|
1462
1721
|
throw new Error("extended point not allowed");
|
|
1463
|
-
const { x, y } = p || {};
|
|
1464
|
-
acoord("x",
|
|
1722
|
+
const { x: x2, y } = p || {};
|
|
1723
|
+
acoord("x", x2);
|
|
1465
1724
|
acoord("y", y);
|
|
1466
|
-
return new Point(
|
|
1725
|
+
return new Point(x2, y, Fp2.ONE, Fp2.mul(x2, y));
|
|
1467
1726
|
}
|
|
1468
1727
|
// Uses algo from RFC8032 5.1.3.
|
|
1469
1728
|
static fromBytes(bytes, zip215 = false) {
|
|
1470
1729
|
const len = Fp2.BYTES;
|
|
1471
1730
|
const { a, d } = CURVE;
|
|
1472
|
-
bytes =
|
|
1731
|
+
bytes = copyBytes2(abytes2(bytes, len, "point"));
|
|
1473
1732
|
abool(zip215, "zip215");
|
|
1474
|
-
const normed =
|
|
1733
|
+
const normed = copyBytes2(bytes);
|
|
1475
1734
|
const lastByte = bytes[len - 1];
|
|
1476
1735
|
normed[len - 1] = lastByte & ~128;
|
|
1477
1736
|
const y = bytesToNumberLE(normed);
|
|
1478
1737
|
const max2 = zip215 ? MASK : Fp2.ORDER;
|
|
1479
1738
|
aInRange("point.y", y, _0n4, max2);
|
|
1480
|
-
const y2 =
|
|
1481
|
-
const u =
|
|
1482
|
-
const v =
|
|
1483
|
-
let { isValid, value:
|
|
1739
|
+
const y2 = Fp2.sqr(y);
|
|
1740
|
+
const u = Fp2.sub(y2, Fp2.ONE);
|
|
1741
|
+
const v = Fp2.sub(Fp2.mulN(d, y2), a);
|
|
1742
|
+
let { isValid, value: x2 } = uvRatio2(u, v);
|
|
1484
1743
|
if (!isValid)
|
|
1485
1744
|
throw new Error("bad point: invalid y coordinate");
|
|
1486
|
-
const isXOdd = (
|
|
1745
|
+
const isXOdd = isOdd(x2);
|
|
1487
1746
|
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
1488
|
-
if (!zip215 &&
|
|
1747
|
+
if (!zip215 && Fp2.is0(x2) && isLastByteOdd)
|
|
1489
1748
|
throw new Error("bad point: x=0 and x_0=1");
|
|
1490
1749
|
if (isLastByteOdd !== isXOdd)
|
|
1491
|
-
|
|
1492
|
-
return Point.fromAffine({ x, y });
|
|
1750
|
+
x2 = Fp2.neg(x2);
|
|
1751
|
+
return Point.fromAffine({ x: x2, y });
|
|
1493
1752
|
}
|
|
1494
1753
|
static fromHex(hex, zip215 = false) {
|
|
1495
1754
|
return Point.fromBytes(hexToBytes2(hex), zip215);
|
|
@@ -1500,8 +1759,8 @@ function edwards(params, extraOpts = {}) {
|
|
|
1500
1759
|
get y() {
|
|
1501
1760
|
return this.toAffine().y;
|
|
1502
1761
|
}
|
|
1503
|
-
precompute(windowSize =
|
|
1504
|
-
wnaf.
|
|
1762
|
+
precompute(windowSize = 6, isLazy = true) {
|
|
1763
|
+
wnaf.setWindowSize(this, windowSize);
|
|
1505
1764
|
if (!isLazy)
|
|
1506
1765
|
this.multiply(_2n2);
|
|
1507
1766
|
return this;
|
|
@@ -1513,18 +1772,18 @@ function edwards(params, extraOpts = {}) {
|
|
|
1513
1772
|
if (p.is0())
|
|
1514
1773
|
throw new Error("bad point: ZERO");
|
|
1515
1774
|
const { X, Y, Z, T } = p;
|
|
1516
|
-
const X2 =
|
|
1517
|
-
const Y2 =
|
|
1518
|
-
const Z2 =
|
|
1519
|
-
const Z4 =
|
|
1520
|
-
const aX2 =
|
|
1521
|
-
const left =
|
|
1522
|
-
const right =
|
|
1523
|
-
if (left
|
|
1775
|
+
const X2 = Fp2.sqr(X);
|
|
1776
|
+
const Y2 = Fp2.sqr(Y);
|
|
1777
|
+
const Z2 = Fp2.sqr(Z);
|
|
1778
|
+
const Z4 = Fp2.sqr(Z2);
|
|
1779
|
+
const aX2 = Fp2.mul(X2, a);
|
|
1780
|
+
const left = Fp2.mul(Fp2.add(aX2, Y2), Z2);
|
|
1781
|
+
const right = Fp2.add(Z4, Fp2.mul(d, Fp2.mul(X2, Y2)));
|
|
1782
|
+
if (!Fp2.eql(left, right))
|
|
1524
1783
|
throw new Error("bad point: equation left != right (1)");
|
|
1525
|
-
const XY =
|
|
1526
|
-
const ZT =
|
|
1527
|
-
if (XY
|
|
1784
|
+
const XY = Fp2.mul(X, Y);
|
|
1785
|
+
const ZT = Fp2.mul(Z, T);
|
|
1786
|
+
if (!Fp2.eql(XY, ZT))
|
|
1528
1787
|
throw new Error("bad point: equation left != right (2)");
|
|
1529
1788
|
}
|
|
1530
1789
|
// Compare one point to another.
|
|
@@ -1532,37 +1791,36 @@ function edwards(params, extraOpts = {}) {
|
|
|
1532
1791
|
aedpoint(other);
|
|
1533
1792
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1534
1793
|
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
1535
|
-
const X1Z2 =
|
|
1536
|
-
const X2Z1 =
|
|
1537
|
-
const Y1Z2 =
|
|
1538
|
-
const Y2Z1 =
|
|
1539
|
-
return X1Z2
|
|
1794
|
+
const X1Z2 = Fp2.mul(X1, Z2);
|
|
1795
|
+
const X2Z1 = Fp2.mul(X2, Z1);
|
|
1796
|
+
const Y1Z2 = Fp2.mul(Y1, Z2);
|
|
1797
|
+
const Y2Z1 = Fp2.mul(Y2, Z1);
|
|
1798
|
+
return Fp2.eql(X1Z2, X2Z1) && Fp2.eql(Y1Z2, Y2Z1);
|
|
1540
1799
|
}
|
|
1541
1800
|
is0() {
|
|
1542
1801
|
return this.equals(Point.ZERO);
|
|
1543
1802
|
}
|
|
1544
1803
|
negate() {
|
|
1545
|
-
return new Point(
|
|
1804
|
+
return new Point(Fp2.neg(this.X), this.Y, this.Z, Fp2.neg(this.T));
|
|
1546
1805
|
}
|
|
1547
1806
|
// Fast algo for doubling Extended Point.
|
|
1548
1807
|
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
1549
1808
|
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
1550
1809
|
double() {
|
|
1551
|
-
const { a } = CURVE;
|
|
1552
1810
|
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
1553
|
-
const A =
|
|
1554
|
-
const B =
|
|
1555
|
-
const C =
|
|
1556
|
-
const D =
|
|
1557
|
-
const x1y1 = X1
|
|
1558
|
-
const E =
|
|
1559
|
-
const G = D
|
|
1560
|
-
const F = G
|
|
1561
|
-
const H = D
|
|
1562
|
-
const X3 =
|
|
1563
|
-
const Y3 =
|
|
1564
|
-
const T3 =
|
|
1565
|
-
const Z3 =
|
|
1811
|
+
const A = Fp2.sqr(X1);
|
|
1812
|
+
const B = Fp2.sqr(Y1);
|
|
1813
|
+
const C = Fp2.mul(Fp2.sqr(Z1), _2n2);
|
|
1814
|
+
const D = mulA(A);
|
|
1815
|
+
const x1y1 = Fp2.addN(X1, Y1);
|
|
1816
|
+
const E = Fp2.sub(Fp2.subN(Fp2.sqr(x1y1), A), B);
|
|
1817
|
+
const G = Fp2.addN(D, B);
|
|
1818
|
+
const F = Fp2.subN(G, C);
|
|
1819
|
+
const H = Fp2.subN(D, B);
|
|
1820
|
+
const X3 = Fp2.mul(E, F);
|
|
1821
|
+
const Y3 = Fp2.mul(G, H);
|
|
1822
|
+
const T3 = Fp2.mul(E, H);
|
|
1823
|
+
const Z3 = Fp2.mul(F, G);
|
|
1566
1824
|
return new Point(X3, Y3, Z3, T3);
|
|
1567
1825
|
}
|
|
1568
1826
|
// Fast algo for adding 2 Extended Points.
|
|
@@ -1570,21 +1828,21 @@ function edwards(params, extraOpts = {}) {
|
|
|
1570
1828
|
// Cost: 9M + 1*a + 1*d + 7add.
|
|
1571
1829
|
add(other) {
|
|
1572
1830
|
aedpoint(other);
|
|
1573
|
-
const {
|
|
1831
|
+
const { d } = CURVE;
|
|
1574
1832
|
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
1575
1833
|
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
1576
|
-
const A =
|
|
1577
|
-
const B =
|
|
1578
|
-
const C =
|
|
1579
|
-
const D =
|
|
1580
|
-
const E =
|
|
1581
|
-
const F = D
|
|
1582
|
-
const G = D
|
|
1583
|
-
const H =
|
|
1584
|
-
const X3 =
|
|
1585
|
-
const Y3 =
|
|
1586
|
-
const T3 =
|
|
1587
|
-
const Z3 =
|
|
1834
|
+
const A = Fp2.mul(X1, X2);
|
|
1835
|
+
const B = Fp2.mul(Y1, Y2);
|
|
1836
|
+
const C = Fp2.mul(Fp2.mulN(T1, d), T2);
|
|
1837
|
+
const D = Fp2.mul(Z1, Z2);
|
|
1838
|
+
const E = Fp2.sub(Fp2.subN(Fp2.mulN(Fp2.addN(X1, Y1), Fp2.addN(X2, Y2)), A), B);
|
|
1839
|
+
const F = Fp2.subN(D, C);
|
|
1840
|
+
const G = Fp2.addN(D, C);
|
|
1841
|
+
const H = Fp2.sub(B, mulA(A));
|
|
1842
|
+
const X3 = Fp2.mul(E, F);
|
|
1843
|
+
const Y3 = Fp2.mul(G, H);
|
|
1844
|
+
const T3 = Fp2.mul(E, H);
|
|
1845
|
+
const Z3 = Fp2.mul(F, G);
|
|
1588
1846
|
return new Point(X3, Y3, Z3, T3);
|
|
1589
1847
|
}
|
|
1590
1848
|
subtract(other) {
|
|
@@ -1593,10 +1851,10 @@ function edwards(params, extraOpts = {}) {
|
|
|
1593
1851
|
}
|
|
1594
1852
|
// Constant-time multiplication.
|
|
1595
1853
|
multiply(scalar) {
|
|
1596
|
-
if (!
|
|
1854
|
+
if (!Fn.isValidNot0(scalar))
|
|
1597
1855
|
throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
1598
|
-
const { p, f } = wnaf.
|
|
1599
|
-
return
|
|
1856
|
+
const { p, f } = wnaf.mulSecret(this, scalar, cofactor, normalize);
|
|
1857
|
+
return normalize([p, f])[0];
|
|
1600
1858
|
}
|
|
1601
1859
|
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
1602
1860
|
// It's faster, but should only be used when you don't care about
|
|
@@ -1604,13 +1862,13 @@ function edwards(params, extraOpts = {}) {
|
|
|
1604
1862
|
// Keeps the same subgroup-scalar contract: 0 is allowed for public-scalar callers, but
|
|
1605
1863
|
// n and larger values are rejected instead of being reduced mod n to the identity point.
|
|
1606
1864
|
multiplyUnsafe(scalar) {
|
|
1607
|
-
if (!
|
|
1865
|
+
if (!Fn.isValid(scalar))
|
|
1608
1866
|
throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
1609
1867
|
if (scalar === _0n4)
|
|
1610
1868
|
return Point.ZERO;
|
|
1611
1869
|
if (this.is0() || scalar === _1n4)
|
|
1612
1870
|
return this;
|
|
1613
|
-
return wnaf.
|
|
1871
|
+
return wnaf.mulUnsafe(this, scalar, normalize);
|
|
1614
1872
|
}
|
|
1615
1873
|
// Checks if point is of small order.
|
|
1616
1874
|
// If you add something to small order point, you will have "dirty"
|
|
@@ -1622,35 +1880,43 @@ function edwards(params, extraOpts = {}) {
|
|
|
1622
1880
|
// Multiplies point by curve order and checks if the result is 0.
|
|
1623
1881
|
// Returns `false` is the point is dirty.
|
|
1624
1882
|
isTorsionFree() {
|
|
1625
|
-
return wnaf.
|
|
1883
|
+
return wnaf.mulUnsafe(this, CURVE.n).is0();
|
|
1626
1884
|
}
|
|
1627
1885
|
// Converts Extended point to default (x, y) coordinates.
|
|
1628
1886
|
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
1629
1887
|
toAffine(invertedZ) {
|
|
1630
1888
|
const p = this;
|
|
1631
1889
|
let iz = invertedZ;
|
|
1890
|
+
if (iz != null && typeof iz !== "bigint")
|
|
1891
|
+
throw new TypeError('"invertedZ" expected bigint, got type=' + typeof iz);
|
|
1632
1892
|
const { X, Y, Z } = p;
|
|
1633
1893
|
const is0 = p.is0();
|
|
1634
1894
|
if (iz == null)
|
|
1635
|
-
iz = is0 ? _8n2 : Fp2.inv(Z);
|
|
1636
|
-
const
|
|
1637
|
-
const y =
|
|
1895
|
+
iz = is0 ? Fp2.create(_8n2) : Fp2.inv(Z);
|
|
1896
|
+
const x2 = Fp2.mul(X, iz);
|
|
1897
|
+
const y = Fp2.mul(Y, iz);
|
|
1638
1898
|
const zz = Fp2.mul(Z, iz);
|
|
1639
1899
|
if (is0)
|
|
1640
|
-
return { x:
|
|
1641
|
-
if (zz
|
|
1900
|
+
return { x: Fp2.ZERO, y: Fp2.ONE };
|
|
1901
|
+
if (!Fp2.eql(zz, Fp2.ONE))
|
|
1642
1902
|
throw new Error("invZ was invalid");
|
|
1643
|
-
return { x, y };
|
|
1903
|
+
return { x: x2, y };
|
|
1644
1904
|
}
|
|
1645
1905
|
clearCofactor() {
|
|
1646
1906
|
if (cofactor === _1n4)
|
|
1647
1907
|
return this;
|
|
1908
|
+
if (cofactor === _2n2)
|
|
1909
|
+
return this.double();
|
|
1910
|
+
if (cofactor === _4n3)
|
|
1911
|
+
return this.double().double();
|
|
1912
|
+
if (cofactor === _8n2)
|
|
1913
|
+
return this.double().double().double();
|
|
1648
1914
|
return this.multiplyUnsafe(cofactor);
|
|
1649
1915
|
}
|
|
1650
1916
|
toBytes() {
|
|
1651
|
-
const { x, y } = this.toAffine();
|
|
1917
|
+
const { x: x2, y } = this.toAffine();
|
|
1652
1918
|
const bytes = Fp2.toBytes(y);
|
|
1653
|
-
bytes[bytes.length - 1] |=
|
|
1919
|
+
bytes[bytes.length - 1] |= isOdd(x2) ? 128 : 0;
|
|
1654
1920
|
return bytes;
|
|
1655
1921
|
}
|
|
1656
1922
|
toHex() {
|
|
@@ -1660,94 +1926,16 @@ function edwards(params, extraOpts = {}) {
|
|
|
1660
1926
|
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
1661
1927
|
}
|
|
1662
1928
|
}
|
|
1663
|
-
const
|
|
1664
|
-
|
|
1665
|
-
|
|
1929
|
+
const normalize = (points) => normalizeZ(Point, points);
|
|
1930
|
+
const wnaf = new ScalarMultiplier(Point, randomBytes5);
|
|
1931
|
+
if (wnaf.bits >= 6)
|
|
1932
|
+
Point.BASE.precompute(6);
|
|
1666
1933
|
Object.freeze(Point.prototype);
|
|
1667
1934
|
Object.freeze(Point);
|
|
1668
1935
|
return Point;
|
|
1669
1936
|
}
|
|
1670
|
-
var PrimeEdwardsPoint = class {
|
|
1671
|
-
static BASE;
|
|
1672
|
-
static ZERO;
|
|
1673
|
-
static Fp;
|
|
1674
|
-
static Fn;
|
|
1675
|
-
ep;
|
|
1676
|
-
/**
|
|
1677
|
-
* Wrap one internal Edwards representative directly.
|
|
1678
|
-
* This is not a canonical encoding boundary: alternate Edwards
|
|
1679
|
-
* representatives may still describe the same abstract wrapper element.
|
|
1680
|
-
*/
|
|
1681
|
-
constructor(ep) {
|
|
1682
|
-
this.ep = ep;
|
|
1683
|
-
}
|
|
1684
|
-
// Static methods that must be implemented by subclasses
|
|
1685
|
-
static fromBytes(_bytes) {
|
|
1686
|
-
notImplemented();
|
|
1687
|
-
}
|
|
1688
|
-
static fromHex(_hex) {
|
|
1689
|
-
notImplemented();
|
|
1690
|
-
}
|
|
1691
|
-
get x() {
|
|
1692
|
-
return this.toAffine().x;
|
|
1693
|
-
}
|
|
1694
|
-
get y() {
|
|
1695
|
-
return this.toAffine().y;
|
|
1696
|
-
}
|
|
1697
|
-
// Common implementations
|
|
1698
|
-
clearCofactor() {
|
|
1699
|
-
return this;
|
|
1700
|
-
}
|
|
1701
|
-
assertValidity() {
|
|
1702
|
-
this.ep.assertValidity();
|
|
1703
|
-
}
|
|
1704
|
-
/**
|
|
1705
|
-
* Return affine coordinates of the current internal Edwards representative.
|
|
1706
|
-
* This is a convenience helper, not a canonical Ristretto/Decaf encoding.
|
|
1707
|
-
* Equal abstract elements may expose different `x` / `y`; use
|
|
1708
|
-
* `toBytes()` / `fromBytes()` for canonical roundtrips.
|
|
1709
|
-
*/
|
|
1710
|
-
toAffine(invertedZ) {
|
|
1711
|
-
return this.ep.toAffine(invertedZ);
|
|
1712
|
-
}
|
|
1713
|
-
toHex() {
|
|
1714
|
-
return bytesToHex2(this.toBytes());
|
|
1715
|
-
}
|
|
1716
|
-
toString() {
|
|
1717
|
-
return this.toHex();
|
|
1718
|
-
}
|
|
1719
|
-
isTorsionFree() {
|
|
1720
|
-
return true;
|
|
1721
|
-
}
|
|
1722
|
-
isSmallOrder() {
|
|
1723
|
-
return false;
|
|
1724
|
-
}
|
|
1725
|
-
add(other) {
|
|
1726
|
-
this.assertSame(other);
|
|
1727
|
-
return this.init(this.ep.add(other.ep));
|
|
1728
|
-
}
|
|
1729
|
-
subtract(other) {
|
|
1730
|
-
this.assertSame(other);
|
|
1731
|
-
return this.init(this.ep.subtract(other.ep));
|
|
1732
|
-
}
|
|
1733
|
-
multiply(scalar) {
|
|
1734
|
-
return this.init(this.ep.multiply(scalar));
|
|
1735
|
-
}
|
|
1736
|
-
multiplyUnsafe(scalar) {
|
|
1737
|
-
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
1738
|
-
}
|
|
1739
|
-
double() {
|
|
1740
|
-
return this.init(this.ep.double());
|
|
1741
|
-
}
|
|
1742
|
-
negate() {
|
|
1743
|
-
return this.init(this.ep.negate());
|
|
1744
|
-
}
|
|
1745
|
-
precompute(windowSize, isLazy) {
|
|
1746
|
-
this.ep.precompute(windowSize, isLazy);
|
|
1747
|
-
return this;
|
|
1748
|
-
}
|
|
1749
|
-
};
|
|
1750
1937
|
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
1938
|
+
validatePointCons(Point);
|
|
1751
1939
|
if (typeof cHash !== "function")
|
|
1752
1940
|
throw new Error('"hash" function param is required');
|
|
1753
1941
|
const hash = cHash;
|
|
@@ -1758,10 +1946,12 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1758
1946
|
domain: "function",
|
|
1759
1947
|
prehash: "function",
|
|
1760
1948
|
zip215: "boolean",
|
|
1761
|
-
mapToCurve: "function"
|
|
1949
|
+
mapToCurve: "function",
|
|
1950
|
+
toMontgomery: "function",
|
|
1951
|
+
toMontgomerySecret: "function"
|
|
1762
1952
|
});
|
|
1763
1953
|
const { prehash } = opts;
|
|
1764
|
-
const { BASE, Fp: Fp2, Fn
|
|
1954
|
+
const { BASE, Fp: Fp2, Fn } = Point;
|
|
1765
1955
|
const outputLen = hash.outputLen;
|
|
1766
1956
|
const expectedLen = 2 * Fp2.BYTES;
|
|
1767
1957
|
if (outputLen !== void 0) {
|
|
@@ -1770,6 +1960,8 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1770
1960
|
throw new Error(`hash.outputLen must be ${expectedLen}, got ${outputLen}`);
|
|
1771
1961
|
}
|
|
1772
1962
|
const randomBytes5 = opts.randomBytes === void 0 ? randomBytes2 : opts.randomBytes;
|
|
1963
|
+
const toMontgomery2 = opts.toMontgomery;
|
|
1964
|
+
const toMontgomerySecret2 = opts.toMontgomerySecret;
|
|
1773
1965
|
const adjustScalarBytes2 = opts.adjustScalarBytes === void 0 ? (bytes) => bytes : opts.adjustScalarBytes;
|
|
1774
1966
|
const domain = opts.domain === void 0 ? (data, ctx, phflag) => {
|
|
1775
1967
|
abool(phflag, "phflag");
|
|
@@ -1778,7 +1970,7 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1778
1970
|
return data;
|
|
1779
1971
|
} : opts.domain;
|
|
1780
1972
|
function modN_LE(hash2) {
|
|
1781
|
-
return
|
|
1973
|
+
return Fn.create(bytesToNumberLE(hash2));
|
|
1782
1974
|
}
|
|
1783
1975
|
function getPrivateScalar(key) {
|
|
1784
1976
|
const len = lengths.secretKey;
|
|
@@ -1803,23 +1995,25 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1803
1995
|
return modN_LE(hash(domain(msg, abytes2(context, void 0, "context"), !!prehash)));
|
|
1804
1996
|
}
|
|
1805
1997
|
function sign(msg, secretKey, options = {}) {
|
|
1806
|
-
|
|
1998
|
+
validateObject(options, {}, {}, "options");
|
|
1999
|
+
msg = copyBytes2(abytes2(msg, void 0, "message"));
|
|
1807
2000
|
if (prehash)
|
|
1808
2001
|
msg = prehash(msg);
|
|
1809
2002
|
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
1810
2003
|
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
1811
2004
|
const R = BASE.multiply(r).toBytes();
|
|
1812
2005
|
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
1813
|
-
const s =
|
|
1814
|
-
if (!
|
|
2006
|
+
const s = Fn.create(r + k * scalar);
|
|
2007
|
+
if (!Fn.isValid(s))
|
|
1815
2008
|
throw new Error("sign failed: invalid s");
|
|
1816
|
-
const rs = concatBytes2(R,
|
|
2009
|
+
const rs = concatBytes2(R, Fn.toBytes(s));
|
|
1817
2010
|
return abytes2(rs, lengths.signature, "result");
|
|
1818
2011
|
}
|
|
1819
2012
|
const verifyOpts = {
|
|
1820
2013
|
zip215: opts.zip215
|
|
1821
2014
|
};
|
|
1822
2015
|
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
2016
|
+
validateObject(options);
|
|
1823
2017
|
const { context } = options;
|
|
1824
2018
|
const zip215 = options.zip215 === void 0 ? !!verifyOpts.zip215 : options.zip215;
|
|
1825
2019
|
const len = lengths.signature;
|
|
@@ -1873,29 +2067,16 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1873
2067
|
randomSecretKey,
|
|
1874
2068
|
isValidSecretKey,
|
|
1875
2069
|
isValidPublicKey,
|
|
1876
|
-
/**
|
|
1877
|
-
* Converts ed public key to x public key. Uses formula:
|
|
1878
|
-
* - ed25519:
|
|
1879
|
-
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
1880
|
-
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
1881
|
-
* - ed448:
|
|
1882
|
-
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
1883
|
-
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
1884
|
-
*/
|
|
2070
|
+
/** Converts an Edwards public key to a companion Montgomery public key. */
|
|
1885
2071
|
toMontgomery(publicKey) {
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
if (!is25519 && size !== 57)
|
|
1890
|
-
throw new Error("only defined for 25519 and 448");
|
|
1891
|
-
const u = is25519 ? Fp2.div(_1n4 + y, _1n4 - y) : Fp2.div(y - _1n4, y + _1n4);
|
|
1892
|
-
return Fp2.toBytes(u);
|
|
2072
|
+
if (toMontgomery2 === void 0)
|
|
2073
|
+
throw new Error("Montgomery conversion is not supported for this curve");
|
|
2074
|
+
return toMontgomery2(Point.fromBytes(publicKey));
|
|
1893
2075
|
},
|
|
1894
2076
|
toMontgomerySecret(secretKey) {
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
2077
|
+
if (toMontgomerySecret2 === void 0)
|
|
2078
|
+
throw new Error("Montgomery conversion is not supported for this curve");
|
|
2079
|
+
return toMontgomerySecret2(secretKey);
|
|
1899
2080
|
}
|
|
1900
2081
|
};
|
|
1901
2082
|
Object.freeze(lengths);
|
|
@@ -1911,65 +2092,22 @@ function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
|
1911
2092
|
});
|
|
1912
2093
|
}
|
|
1913
2094
|
|
|
1914
|
-
// ../../node_modules/@noble/curves/abstract/hash-to-curve.js
|
|
1915
|
-
function i2osp(value, length) {
|
|
1916
|
-
asafenumber(value);
|
|
1917
|
-
asafenumber(length);
|
|
1918
|
-
if (length < 0 || length > 4)
|
|
1919
|
-
throw new Error("invalid I2OSP length: " + length);
|
|
1920
|
-
if (value < 0 || value > 2 ** (8 * length) - 1)
|
|
1921
|
-
throw new Error("invalid I2OSP input: " + value);
|
|
1922
|
-
const res = Array.from({ length }).fill(0);
|
|
1923
|
-
for (let i = length - 1; i >= 0; i--) {
|
|
1924
|
-
res[i] = value & 255;
|
|
1925
|
-
value >>>= 8;
|
|
1926
|
-
}
|
|
1927
|
-
return new Uint8Array(res);
|
|
1928
|
-
}
|
|
1929
|
-
function strxor(a, b) {
|
|
1930
|
-
const arr = new Uint8Array(a.length);
|
|
1931
|
-
for (let i = 0; i < a.length; i++) {
|
|
1932
|
-
arr[i] = a[i] ^ b[i];
|
|
1933
|
-
}
|
|
1934
|
-
return arr;
|
|
1935
|
-
}
|
|
1936
|
-
function normDST(DST) {
|
|
1937
|
-
if (!isBytes2(DST) && typeof DST !== "string")
|
|
1938
|
-
throw new Error("DST must be Uint8Array or ascii string");
|
|
1939
|
-
const dst = typeof DST === "string" ? asciiToBytes(DST) : DST;
|
|
1940
|
-
if (dst.length === 0)
|
|
1941
|
-
throw new Error("DST must be non-empty");
|
|
1942
|
-
return dst;
|
|
1943
|
-
}
|
|
1944
|
-
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
1945
|
-
abytes2(msg);
|
|
1946
|
-
asafenumber(lenInBytes);
|
|
1947
|
-
DST = normDST(DST);
|
|
1948
|
-
if (DST.length > 255)
|
|
1949
|
-
DST = H(concatBytes2(asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
1950
|
-
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
1951
|
-
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
1952
|
-
if (lenInBytes > 65535 || ell > 255)
|
|
1953
|
-
throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
1954
|
-
const DST_prime = concatBytes2(DST, i2osp(DST.length, 1));
|
|
1955
|
-
const Z_pad = new Uint8Array(r_in_bytes);
|
|
1956
|
-
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
1957
|
-
const b = new Array(ell);
|
|
1958
|
-
const b_0 = H(concatBytes2(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
1959
|
-
b[0] = H(concatBytes2(b_0, i2osp(1, 1), DST_prime));
|
|
1960
|
-
for (let i = 1; i < ell; i++) {
|
|
1961
|
-
const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
|
|
1962
|
-
b[i] = H(concatBytes2(...args));
|
|
1963
|
-
}
|
|
1964
|
-
const pseudo_random_bytes = concatBytes2(...b);
|
|
1965
|
-
return pseudo_random_bytes.slice(0, lenInBytes);
|
|
1966
|
-
}
|
|
1967
|
-
var _DST_scalar = "HashToScalar-";
|
|
1968
|
-
|
|
1969
2095
|
// ../../node_modules/@noble/curves/abstract/montgomery.js
|
|
1970
|
-
var _0n5 = BigInt(0);
|
|
1971
|
-
var _1n5 = BigInt(1);
|
|
1972
|
-
var _2n3 = BigInt(2);
|
|
2096
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
2097
|
+
var _1n5 = /* @__PURE__ */ BigInt(1);
|
|
2098
|
+
var _2n3 = /* @__PURE__ */ BigInt(2);
|
|
2099
|
+
function cmask(P, swap) {
|
|
2100
|
+
return P + swap - (swap >> _1n5 << _1n5);
|
|
2101
|
+
}
|
|
2102
|
+
function cswap(P) {
|
|
2103
|
+
const offset = BigInt(6) * P;
|
|
2104
|
+
return (mask, x_2, x_3) => {
|
|
2105
|
+
const sum = x_2 + x_3;
|
|
2106
|
+
const d = offset + x_3 - x_2;
|
|
2107
|
+
const a = (d * mask + x_2) % P;
|
|
2108
|
+
return { x_2: a, x_3: sum - a };
|
|
2109
|
+
};
|
|
2110
|
+
}
|
|
1973
2111
|
function validateOpts(curve) {
|
|
1974
2112
|
validateObject(curve, {
|
|
1975
2113
|
P: "bigint",
|
|
@@ -1977,23 +2115,26 @@ function validateOpts(curve) {
|
|
|
1977
2115
|
adjustScalarBytes: "function",
|
|
1978
2116
|
powPminus2: "function"
|
|
1979
2117
|
}, {
|
|
1980
|
-
randomBytes: "function"
|
|
2118
|
+
randomBytes: "function",
|
|
2119
|
+
scalarMultBase: "function"
|
|
1981
2120
|
});
|
|
1982
2121
|
return Object.freeze({ ...curve });
|
|
1983
2122
|
}
|
|
1984
2123
|
function montgomery(curveDef) {
|
|
1985
2124
|
const CURVE = validateOpts(curveDef);
|
|
1986
2125
|
const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
|
|
2126
|
+
const mulBaseHook = CURVE.scalarMultBase;
|
|
1987
2127
|
const is25519 = type === "x25519";
|
|
1988
2128
|
if (!is25519 && type !== "x448")
|
|
1989
2129
|
throw new Error("invalid type");
|
|
1990
2130
|
const randomBytes_ = rand === void 0 ? randomBytes2 : rand;
|
|
1991
2131
|
const montgomeryBits = is25519 ? 255 : 448;
|
|
2132
|
+
const swap = cswap(P);
|
|
1992
2133
|
const fieldLen = is25519 ? 32 : 56;
|
|
1993
2134
|
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
1994
2135
|
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
1995
2136
|
const minScalar = is25519 ? _2n3 ** BigInt(254) : _2n3 ** BigInt(447);
|
|
1996
|
-
const maxAdded = is25519 ? BigInt(8) * _2n3 ** BigInt(251) - _1n5 : BigInt(4) * _2n3 ** BigInt(445) - _1n5;
|
|
2137
|
+
const maxAdded = is25519 ? BigInt(8) * (_2n3 ** BigInt(251) - _1n5) : BigInt(4) * (_2n3 ** BigInt(445) - _1n5);
|
|
1997
2138
|
const maxScalar = minScalar + maxAdded + _1n5;
|
|
1998
2139
|
const modP = (n) => mod(n, P);
|
|
1999
2140
|
const GuBytes = encodeU(Gu);
|
|
@@ -2001,31 +2142,42 @@ function montgomery(curveDef) {
|
|
|
2001
2142
|
return numberToBytesLE(modP(u), fieldLen);
|
|
2002
2143
|
}
|
|
2003
2144
|
function decodeU(u) {
|
|
2004
|
-
const _u =
|
|
2145
|
+
const _u = copyBytes2(abytes2(u, fieldLen, "uCoordinate"));
|
|
2005
2146
|
if (is25519)
|
|
2006
2147
|
_u[31] &= 127;
|
|
2007
2148
|
return modP(bytesToNumberLE(_u));
|
|
2008
2149
|
}
|
|
2009
2150
|
function decodeScalar(scalar) {
|
|
2010
|
-
return bytesToNumberLE(adjustScalarBytes2(
|
|
2011
|
-
}
|
|
2151
|
+
return bytesToNumberLE(adjustScalarBytes2(copyBytes2(abytes2(scalar, fieldLen, "scalar"))));
|
|
2152
|
+
}
|
|
2153
|
+
const lowOrderU = new Set(is25519 ? [
|
|
2154
|
+
_0n5,
|
|
2155
|
+
_1n5,
|
|
2156
|
+
P - _1n5,
|
|
2157
|
+
BigInt("325606250916557431795983626356110631294008115727848805560023387167927233504"),
|
|
2158
|
+
BigInt("39382357235489614581723060781553021112529911719440698176882885853963445705823")
|
|
2159
|
+
] : [_0n5, _1n5, P - _1n5]);
|
|
2012
2160
|
function scalarMult(scalar, u) {
|
|
2013
|
-
const
|
|
2161
|
+
const pointU = decodeU(u);
|
|
2162
|
+
if (lowOrderU.has(pointU))
|
|
2163
|
+
throw new Error("invalid private or public key received");
|
|
2164
|
+
const pu = montgomeryLadder(pointU, decodeScalar(scalar));
|
|
2014
2165
|
if (pu === _0n5)
|
|
2015
2166
|
throw new Error("invalid private or public key received");
|
|
2016
2167
|
return encodeU(pu);
|
|
2017
2168
|
}
|
|
2018
2169
|
function scalarMultBase(scalar) {
|
|
2019
|
-
|
|
2170
|
+
if (mulBaseHook === void 0)
|
|
2171
|
+
return scalarMult(scalar, GuBytes);
|
|
2172
|
+
const k = decodeScalar(scalar);
|
|
2173
|
+
aInRange("scalar", k, minScalar, maxScalar);
|
|
2174
|
+
const pu = modP(mulBaseHook(k));
|
|
2175
|
+
if (pu === _0n5)
|
|
2176
|
+
throw new Error("invalid private or public key received");
|
|
2177
|
+
return encodeU(pu);
|
|
2020
2178
|
}
|
|
2021
2179
|
const getPublicKey = scalarMultBase;
|
|
2022
2180
|
const getSharedSecret = scalarMult;
|
|
2023
|
-
function cswap(swap, x_2, x_3) {
|
|
2024
|
-
const dummy = modP(swap * (x_2 - x_3));
|
|
2025
|
-
x_2 = modP(x_2 - dummy);
|
|
2026
|
-
x_3 = modP(x_3 + dummy);
|
|
2027
|
-
return { x_2, x_3 };
|
|
2028
|
-
}
|
|
2029
2181
|
function montgomeryLadder(u, scalar) {
|
|
2030
2182
|
aInRange("u", u, _0n5, P);
|
|
2031
2183
|
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
@@ -2035,13 +2187,11 @@ function montgomery(curveDef) {
|
|
|
2035
2187
|
let z_2 = _0n5;
|
|
2036
2188
|
let x_3 = u;
|
|
2037
2189
|
let z_3 = _1n5;
|
|
2038
|
-
|
|
2190
|
+
const kx = k ^ k >> _1n5;
|
|
2039
2191
|
for (let t = BigInt(montgomeryBits - 1); t >= _0n5; t--) {
|
|
2040
|
-
const
|
|
2041
|
-
swap
|
|
2042
|
-
({ x_2, x_3 } =
|
|
2043
|
-
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
2044
|
-
swap = k_t;
|
|
2192
|
+
const mask2 = cmask(P, kx >> t);
|
|
2193
|
+
({ x_2, x_3 } = swap(mask2, x_2, x_3));
|
|
2194
|
+
({ x_2: z_2, x_3: z_3 } = swap(mask2, z_2, z_3));
|
|
2045
2195
|
const A = x_2 + z_2;
|
|
2046
2196
|
const AA = modP(A * A);
|
|
2047
2197
|
const B = x_2 - z_2;
|
|
@@ -2058,8 +2208,9 @@ function montgomery(curveDef) {
|
|
|
2058
2208
|
x_2 = modP(AA * BB);
|
|
2059
2209
|
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
2060
2210
|
}
|
|
2061
|
-
|
|
2062
|
-
({ x_2
|
|
2211
|
+
const mask = cmask(P, k);
|
|
2212
|
+
({ x_2, x_3 } = swap(mask, x_2, x_3));
|
|
2213
|
+
({ x_2: z_2, x_3: z_3 } = swap(mask, z_2, z_3));
|
|
2063
2214
|
const z2 = powPminus2(z_2);
|
|
2064
2215
|
return modP(x_2 * z2);
|
|
2065
2216
|
}
|
|
@@ -2105,13 +2256,13 @@ var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
|
2105
2256
|
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
2106
2257
|
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
2107
2258
|
}))();
|
|
2108
|
-
function ed25519_pow_2_252_3(
|
|
2259
|
+
function ed25519_pow_2_252_3(x2) {
|
|
2109
2260
|
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
2110
2261
|
const P = ed25519_CURVE_p;
|
|
2111
|
-
const
|
|
2112
|
-
const b2 =
|
|
2262
|
+
const x22 = x2 * x2 % P;
|
|
2263
|
+
const b2 = x22 * x2 % P;
|
|
2113
2264
|
const b4 = pow2(b2, _2n4, P) * b2 % P;
|
|
2114
|
-
const b5 = pow2(b4, _1n6, P) *
|
|
2265
|
+
const b5 = pow2(b4, _1n6, P) * x2 % P;
|
|
2115
2266
|
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
2116
2267
|
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
2117
2268
|
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
@@ -2119,7 +2270,7 @@ function ed25519_pow_2_252_3(x) {
|
|
|
2119
2270
|
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
2120
2271
|
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
2121
2272
|
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
2122
|
-
const pow_p_5_8 = pow2(b250, _2n4, P) *
|
|
2273
|
+
const pow_p_5_8 = pow2(b250, _2n4, P) * x2 % P;
|
|
2123
2274
|
return { pow_p_5_8, b2 };
|
|
2124
2275
|
}
|
|
2125
2276
|
function adjustScalarBytes(bytes) {
|
|
@@ -2133,233 +2284,60 @@ function uvRatio(u, v) {
|
|
|
2133
2284
|
const P = ed25519_CURVE_p;
|
|
2134
2285
|
const v3 = mod(v * v * v, P);
|
|
2135
2286
|
const v7 = mod(v3 * v3 * v, P);
|
|
2136
|
-
const
|
|
2137
|
-
let
|
|
2138
|
-
const vx2 = mod(v *
|
|
2139
|
-
const root1 =
|
|
2140
|
-
const root2 = mod(
|
|
2287
|
+
const pow3 = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
2288
|
+
let x2 = mod(u * v3 * pow3, P);
|
|
2289
|
+
const vx2 = mod(v * x2 * x2, P);
|
|
2290
|
+
const root1 = x2;
|
|
2291
|
+
const root2 = mod(x2 * ED25519_SQRT_M1, P);
|
|
2141
2292
|
const useRoot1 = vx2 === u;
|
|
2142
2293
|
const useRoot2 = vx2 === mod(-u, P);
|
|
2143
2294
|
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
2144
2295
|
if (useRoot1)
|
|
2145
|
-
|
|
2296
|
+
x2 = root1;
|
|
2146
2297
|
if (useRoot2 || noRoot)
|
|
2147
|
-
|
|
2148
|
-
if (isNegativeLE(
|
|
2149
|
-
|
|
2150
|
-
return { isValid: useRoot1 || useRoot2, value:
|
|
2298
|
+
x2 = root2;
|
|
2299
|
+
if (isNegativeLE(x2, P))
|
|
2300
|
+
x2 = mod(-x2, P);
|
|
2301
|
+
return { isValid: useRoot1 || useRoot2, value: x2 };
|
|
2151
2302
|
}
|
|
2152
2303
|
var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
2153
2304
|
var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
2154
|
-
|
|
2305
|
+
function toMontgomery(point) {
|
|
2306
|
+
const { y } = point;
|
|
2307
|
+
return Fp.toBytes(Fp.div(_1n6 + y, _1n6 - y));
|
|
2308
|
+
}
|
|
2309
|
+
function toMontgomerySecret(secretKey) {
|
|
2310
|
+
const size = ed25519_Point.Fp.BYTES;
|
|
2311
|
+
abytes(secretKey, size);
|
|
2312
|
+
return adjustScalarBytes(sha512(secretKey.subarray(0, size))).subarray(0, size);
|
|
2313
|
+
}
|
|
2155
2314
|
function ed(opts) {
|
|
2156
|
-
return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes, zip215: true }, opts));
|
|
2315
|
+
return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes, toMontgomery, toMontgomerySecret, zip215: true }, opts));
|
|
2157
2316
|
}
|
|
2158
2317
|
var ed25519 = /* @__PURE__ */ ed({});
|
|
2159
2318
|
var x25519 = /* @__PURE__ */ (() => {
|
|
2160
2319
|
const P = ed25519_CURVE_p;
|
|
2320
|
+
const powPminus2 = (x2) => {
|
|
2321
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x2);
|
|
2322
|
+
return mod(pow2(pow_p_5_8, _3n2, P) * b2, P);
|
|
2323
|
+
};
|
|
2161
2324
|
return montgomery({
|
|
2162
2325
|
P,
|
|
2163
2326
|
type: "x25519",
|
|
2164
|
-
powPminus2
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2327
|
+
powPminus2,
|
|
2328
|
+
adjustScalarBytes,
|
|
2329
|
+
// ~3x faster fixed-base: [k]B on the birationally-equivalent Edwards curve using cached
|
|
2330
|
+
// base tables, mapped back via u = (1+y)/(1-y) = (Z+Y)/(Z-Y) with one Fermat inversion.
|
|
2331
|
+
// Same construction as libsodium's crypto_scalarmult_curve25519_base.
|
|
2332
|
+
scalarMultBase: (k) => {
|
|
2333
|
+
const kn = mod(k, ed25519_Point.Fn.ORDER);
|
|
2334
|
+
if (kn === _0n6)
|
|
2335
|
+
return _0n6;
|
|
2336
|
+
const p = ed25519_Point.BASE.multiply(kn);
|
|
2337
|
+
return mod((p.Z + p.Y) * powPminus2(mod(p.Z - p.Y, P)), P);
|
|
2338
|
+
}
|
|
2169
2339
|
});
|
|
2170
2340
|
})();
|
|
2171
|
-
var SQRT_M1 = ED25519_SQRT_M1;
|
|
2172
|
-
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
2173
|
-
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
2174
|
-
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
2175
|
-
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
2176
|
-
var invertSqrt = (number) => uvRatio(_1n6, number);
|
|
2177
|
-
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
2178
|
-
var bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
2179
|
-
function calcElligatorRistrettoMap(r0) {
|
|
2180
|
-
const { d } = ed25519_CURVE;
|
|
2181
|
-
const P = ed25519_CURVE_p;
|
|
2182
|
-
const mod2 = (n) => Fp.create(n);
|
|
2183
|
-
const r = mod2(SQRT_M1 * r0 * r0);
|
|
2184
|
-
const Ns = mod2((r + _1n6) * ONE_MINUS_D_SQ);
|
|
2185
|
-
let c = BigInt(-1);
|
|
2186
|
-
const D = mod2((c - d * r) * mod2(r + d));
|
|
2187
|
-
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
2188
|
-
let s_ = mod2(s * r0);
|
|
2189
|
-
if (!isNegativeLE(s_, P))
|
|
2190
|
-
s_ = mod2(-s_);
|
|
2191
|
-
if (!Ns_D_is_sq)
|
|
2192
|
-
s = s_;
|
|
2193
|
-
if (!Ns_D_is_sq)
|
|
2194
|
-
c = r;
|
|
2195
|
-
const Nt = mod2(c * (r - _1n6) * D_MINUS_ONE_SQ - D);
|
|
2196
|
-
const s2 = s * s;
|
|
2197
|
-
const W0 = mod2((s + s) * D);
|
|
2198
|
-
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
2199
|
-
const W2 = mod2(_1n6 - s2);
|
|
2200
|
-
const W3 = mod2(_1n6 + s2);
|
|
2201
|
-
return new ed25519_Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
2202
|
-
}
|
|
2203
|
-
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
2204
|
-
// Do NOT change syntax: the following gymnastics is done,
|
|
2205
|
-
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
2206
|
-
// prettier-ignore
|
|
2207
|
-
static BASE = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))();
|
|
2208
|
-
// prettier-ignore
|
|
2209
|
-
static ZERO = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))();
|
|
2210
|
-
// prettier-ignore
|
|
2211
|
-
static Fp = /* @__PURE__ */ (() => Fp)();
|
|
2212
|
-
// prettier-ignore
|
|
2213
|
-
static Fn = /* @__PURE__ */ (() => Fn)();
|
|
2214
|
-
constructor(ep) {
|
|
2215
|
-
super(ep);
|
|
2216
|
-
}
|
|
2217
|
-
/**
|
|
2218
|
-
* Create one Ristretto255 point from affine Edwards coordinates.
|
|
2219
|
-
* This wraps the internal Edwards representative directly and is not a
|
|
2220
|
-
* canonical ristretto255 decoding path.
|
|
2221
|
-
* Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
|
|
2222
|
-
*/
|
|
2223
|
-
static fromAffine(ap) {
|
|
2224
|
-
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
2225
|
-
}
|
|
2226
|
-
assertSame(other) {
|
|
2227
|
-
if (!(other instanceof __RistrettoPoint))
|
|
2228
|
-
throw new Error("RistrettoPoint expected");
|
|
2229
|
-
}
|
|
2230
|
-
init(ep) {
|
|
2231
|
-
return new __RistrettoPoint(ep);
|
|
2232
|
-
}
|
|
2233
|
-
static fromBytes(bytes) {
|
|
2234
|
-
abytes(bytes, 32);
|
|
2235
|
-
const { a, d } = ed25519_CURVE;
|
|
2236
|
-
const P = ed25519_CURVE_p;
|
|
2237
|
-
const mod2 = (n) => Fp.create(n);
|
|
2238
|
-
const s = bytes255ToNumberLE(bytes);
|
|
2239
|
-
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
2240
|
-
throw new Error("invalid ristretto255 encoding 1");
|
|
2241
|
-
const s2 = mod2(s * s);
|
|
2242
|
-
const u1 = mod2(_1n6 + a * s2);
|
|
2243
|
-
const u2 = mod2(_1n6 - a * s2);
|
|
2244
|
-
const u1_2 = mod2(u1 * u1);
|
|
2245
|
-
const u2_2 = mod2(u2 * u2);
|
|
2246
|
-
const v = mod2(a * d * u1_2 - u2_2);
|
|
2247
|
-
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
2248
|
-
const Dx = mod2(I * u2);
|
|
2249
|
-
const Dy = mod2(I * Dx * v);
|
|
2250
|
-
let x = mod2((s + s) * Dx);
|
|
2251
|
-
if (isNegativeLE(x, P))
|
|
2252
|
-
x = mod2(-x);
|
|
2253
|
-
const y = mod2(u1 * Dy);
|
|
2254
|
-
const t = mod2(x * y);
|
|
2255
|
-
if (!isValid || isNegativeLE(t, P) || y === _0n6)
|
|
2256
|
-
throw new Error("invalid ristretto255 encoding 2");
|
|
2257
|
-
return new __RistrettoPoint(new ed25519_Point(x, y, _1n6, t));
|
|
2258
|
-
}
|
|
2259
|
-
/**
|
|
2260
|
-
* Converts ristretto-encoded string to ristretto point.
|
|
2261
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
2262
|
-
* @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
2263
|
-
*/
|
|
2264
|
-
static fromHex(hex) {
|
|
2265
|
-
return __RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
2266
|
-
}
|
|
2267
|
-
/**
|
|
2268
|
-
* Encodes ristretto point to Uint8Array.
|
|
2269
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
2270
|
-
*/
|
|
2271
|
-
toBytes() {
|
|
2272
|
-
let { X, Y, Z, T } = this.ep;
|
|
2273
|
-
const P = ed25519_CURVE_p;
|
|
2274
|
-
const mod2 = (n) => Fp.create(n);
|
|
2275
|
-
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
2276
|
-
const u2 = mod2(X * Y);
|
|
2277
|
-
const u2sq = mod2(u2 * u2);
|
|
2278
|
-
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
2279
|
-
const D1 = mod2(invsqrt * u1);
|
|
2280
|
-
const D2 = mod2(invsqrt * u2);
|
|
2281
|
-
const zInv = mod2(D1 * D2 * T);
|
|
2282
|
-
let D;
|
|
2283
|
-
if (isNegativeLE(T * zInv, P)) {
|
|
2284
|
-
let _x = mod2(Y * SQRT_M1);
|
|
2285
|
-
let _y = mod2(X * SQRT_M1);
|
|
2286
|
-
X = _x;
|
|
2287
|
-
Y = _y;
|
|
2288
|
-
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
2289
|
-
} else {
|
|
2290
|
-
D = D2;
|
|
2291
|
-
}
|
|
2292
|
-
if (isNegativeLE(X * zInv, P))
|
|
2293
|
-
Y = mod2(-Y);
|
|
2294
|
-
let s = mod2((Z - Y) * D);
|
|
2295
|
-
if (isNegativeLE(s, P))
|
|
2296
|
-
s = mod2(-s);
|
|
2297
|
-
return Fp.toBytes(s);
|
|
2298
|
-
}
|
|
2299
|
-
/**
|
|
2300
|
-
* Compares two Ristretto points.
|
|
2301
|
-
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
2302
|
-
*/
|
|
2303
|
-
equals(other) {
|
|
2304
|
-
this.assertSame(other);
|
|
2305
|
-
const { X: X1, Y: Y1 } = this.ep;
|
|
2306
|
-
const { X: X2, Y: Y2 } = other.ep;
|
|
2307
|
-
const mod2 = (n) => Fp.create(n);
|
|
2308
|
-
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
2309
|
-
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
2310
|
-
return one || two;
|
|
2311
|
-
}
|
|
2312
|
-
is0() {
|
|
2313
|
-
return this.equals(__RistrettoPoint.ZERO);
|
|
2314
|
-
}
|
|
2315
|
-
};
|
|
2316
|
-
Object.freeze(_RistrettoPoint.BASE);
|
|
2317
|
-
Object.freeze(_RistrettoPoint.ZERO);
|
|
2318
|
-
Object.freeze(_RistrettoPoint.prototype);
|
|
2319
|
-
Object.freeze(_RistrettoPoint);
|
|
2320
|
-
var ristretto255_hasher = Object.freeze({
|
|
2321
|
-
Point: _RistrettoPoint,
|
|
2322
|
-
/**
|
|
2323
|
-
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
2324
|
-
* * There are no test vectors
|
|
2325
|
-
* * encodeToCurve / mapToCurve is undefined
|
|
2326
|
-
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
2327
|
-
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
2328
|
-
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
2329
|
-
(os2ip -> bytes255ToNumberLE)
|
|
2330
|
-
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
2331
|
-
* * hashToScalar is undefined in RFC9380 for ristretto, so we use the OPRF
|
|
2332
|
-
version here. Using `bytes255ToNumblerLE` will create a different result
|
|
2333
|
-
if we use `bytes255ToNumberLE` as os2ip
|
|
2334
|
-
* * current version is closest to spec.
|
|
2335
|
-
*/
|
|
2336
|
-
hashToCurve(msg, options) {
|
|
2337
|
-
const DST = options?.DST === void 0 ? "ristretto255_XMD:SHA-512_R255MAP_RO_" : options.DST;
|
|
2338
|
-
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
2339
|
-
return ristretto255_hasher.deriveToCurve(xmd);
|
|
2340
|
-
},
|
|
2341
|
-
hashToScalar(msg, options = { DST: _DST_scalar }) {
|
|
2342
|
-
const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
|
|
2343
|
-
return Fn.create(bytesToNumberLE(xmd));
|
|
2344
|
-
},
|
|
2345
|
-
/**
|
|
2346
|
-
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
2347
|
-
* Converts 64 uniform random bytes into a curve point.
|
|
2348
|
-
*
|
|
2349
|
-
* WARNING: This represents an older hash-to-curve construction from before
|
|
2350
|
-
* RFC 9380 was finalized.
|
|
2351
|
-
* It was later reused as a component in the newer
|
|
2352
|
-
* `hash_to_ristretto255` function defined in RFC 9380.
|
|
2353
|
-
*/
|
|
2354
|
-
deriveToCurve(bytes) {
|
|
2355
|
-
abytes(bytes, 64);
|
|
2356
|
-
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
2357
|
-
const R1 = calcElligatorRistrettoMap(r1);
|
|
2358
|
-
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
2359
|
-
const R2 = calcElligatorRistrettoMap(r2);
|
|
2360
|
-
return new _RistrettoPoint(R1.add(R2));
|
|
2361
|
-
}
|
|
2362
|
-
});
|
|
2363
2341
|
|
|
2364
2342
|
// ../../node_modules/@noble/hashes/_blake.js
|
|
2365
2343
|
var BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
@@ -2621,15 +2599,15 @@ var BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
|
2621
2599
|
1,
|
|
2622
2600
|
9
|
|
2623
2601
|
]);
|
|
2624
|
-
function G1s(a, b, c, d,
|
|
2625
|
-
a = a + b +
|
|
2602
|
+
function G1s(a, b, c, d, x2) {
|
|
2603
|
+
a = a + b + x2 | 0;
|
|
2626
2604
|
d = rotr(d ^ a, 16);
|
|
2627
2605
|
c = c + d | 0;
|
|
2628
2606
|
b = rotr(b ^ c, 12);
|
|
2629
2607
|
return { a, b, c, d };
|
|
2630
2608
|
}
|
|
2631
|
-
function G2s(a, b, c, d,
|
|
2632
|
-
a = a + b +
|
|
2609
|
+
function G2s(a, b, c, d, x2) {
|
|
2610
|
+
a = a + b + x2 | 0;
|
|
2633
2611
|
d = rotr(d ^ a, 8);
|
|
2634
2612
|
c = c + d | 0;
|
|
2635
2613
|
b = rotr(b ^ c, 7);
|
|
@@ -2640,7 +2618,7 @@ function G2s(a, b, c, d, x) {
|
|
|
2640
2618
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
|
2641
2619
|
anumber(keyLen);
|
|
2642
2620
|
if (outputLen <= 0 || outputLen > keyLen)
|
|
2643
|
-
throw new Error("
|
|
2621
|
+
throw new Error('"dkLen" must be 1..' + keyLen + ", got " + outputLen);
|
|
2644
2622
|
const { key, salt, personalization } = opts;
|
|
2645
2623
|
if (key !== void 0 && (key.length < 1 || key.length > keyLen))
|
|
2646
2624
|
throw new Error('"key" expected to be undefined or of length=1..' + keyLen);
|
|
@@ -2693,7 +2671,7 @@ var _BLAKE2 = class {
|
|
|
2693
2671
|
swap32IfBE(data32);
|
|
2694
2672
|
continue;
|
|
2695
2673
|
}
|
|
2696
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
2674
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
2697
2675
|
this.pos += take;
|
|
2698
2676
|
this.length += take;
|
|
2699
2677
|
pos += take;
|
|
@@ -2703,26 +2681,26 @@ var _BLAKE2 = class {
|
|
|
2703
2681
|
digestInto(out) {
|
|
2704
2682
|
aexists(this);
|
|
2705
2683
|
aoutput(out, this);
|
|
2684
|
+
if (out.byteOffset & 3)
|
|
2685
|
+
throw new RangeError('"output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
|
|
2706
2686
|
const { pos, buffer32 } = this;
|
|
2707
2687
|
this.finished = true;
|
|
2708
|
-
|
|
2688
|
+
this.buffer.fill(0, pos);
|
|
2709
2689
|
swap32IfBE(buffer32);
|
|
2710
2690
|
this.compress(buffer32, 0, true);
|
|
2711
2691
|
swap32IfBE(buffer32);
|
|
2712
|
-
if (out.byteOffset & 3)
|
|
2713
|
-
throw new RangeError('"digestInto() output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
|
|
2714
2692
|
const state = this.get();
|
|
2715
|
-
const out32 = u32(out);
|
|
2693
|
+
const out32 = out === this.buffer ? buffer32 : u32(out);
|
|
2716
2694
|
const full = Math.floor(this.outputLen / 4);
|
|
2717
|
-
for (let
|
|
2718
|
-
out32[
|
|
2695
|
+
for (let i2 = 0; i2 < full; i2++)
|
|
2696
|
+
out32[i2] = swap8IfBE(state[i2]);
|
|
2719
2697
|
const tail = this.outputLen % 4;
|
|
2720
2698
|
if (!tail)
|
|
2721
2699
|
return;
|
|
2722
2700
|
const off = full * 4;
|
|
2723
2701
|
const word = state[full];
|
|
2724
|
-
for (let
|
|
2725
|
-
out[off +
|
|
2702
|
+
for (let i2 = 0; i2 < tail; i2++)
|
|
2703
|
+
out[off + i2] = word >>> 8 * i2;
|
|
2726
2704
|
}
|
|
2727
2705
|
digest() {
|
|
2728
2706
|
const { buffer, outputLen } = this;
|
|
@@ -2747,9 +2725,9 @@ var _BLAKE2 = class {
|
|
|
2747
2725
|
return this._cloneInto();
|
|
2748
2726
|
}
|
|
2749
2727
|
};
|
|
2750
|
-
function
|
|
2728
|
+
function _compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
|
|
2751
2729
|
let j = 0;
|
|
2752
|
-
for (let
|
|
2730
|
+
for (let i2 = 0; i2 < rounds; i2++) {
|
|
2753
2731
|
({ a: v0, b: v4, c: v8, d: v12 } = G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
|
2754
2732
|
({ a: v0, b: v4, c: v8, d: v12 } = G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
|
2755
2733
|
({ a: v1, b: v5, c: v9, d: v13 } = G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
|
@@ -2781,6 +2759,7 @@ var _BLAKE2s = class extends _BLAKE2 {
|
|
|
2781
2759
|
v6 = B2S_IV[6] | 0;
|
|
2782
2760
|
v7 = B2S_IV[7] | 0;
|
|
2783
2761
|
constructor(opts = {}) {
|
|
2762
|
+
opts = checkOpts({}, opts);
|
|
2784
2763
|
const olen = opts.dkLen === void 0 ? 32 : opts.dkLen;
|
|
2785
2764
|
super(64, olen);
|
|
2786
2765
|
checkBlake2Opts(olen, opts, 32, 8, 8);
|
|
@@ -2793,13 +2772,13 @@ var _BLAKE2s = class extends _BLAKE2 {
|
|
|
2793
2772
|
this.v0 ^= this.outputLen | keyLength << 8 | 1 << 16 | 1 << 24;
|
|
2794
2773
|
if (salt !== void 0) {
|
|
2795
2774
|
abytes(salt, void 0, "salt");
|
|
2796
|
-
const slt = u32(salt);
|
|
2775
|
+
const slt = u32(copyBytes(salt));
|
|
2797
2776
|
this.v4 ^= swap8IfBE(slt[0]);
|
|
2798
2777
|
this.v5 ^= swap8IfBE(slt[1]);
|
|
2799
2778
|
}
|
|
2800
2779
|
if (personalization !== void 0) {
|
|
2801
2780
|
abytes(personalization, void 0, "personalization");
|
|
2802
|
-
const pers = u32(personalization);
|
|
2781
|
+
const pers = u32(copyBytes(personalization));
|
|
2803
2782
|
this.v6 ^= swap8IfBE(pers[0]);
|
|
2804
2783
|
this.v7 ^= swap8IfBE(pers[1]);
|
|
2805
2784
|
}
|
|
@@ -2807,6 +2786,7 @@ var _BLAKE2s = class extends _BLAKE2 {
|
|
|
2807
2786
|
const tmp = new Uint8Array(this.blockLen);
|
|
2808
2787
|
tmp.set(key);
|
|
2809
2788
|
this.update(tmp);
|
|
2789
|
+
clean(tmp);
|
|
2810
2790
|
}
|
|
2811
2791
|
}
|
|
2812
2792
|
get() {
|
|
@@ -2825,8 +2805,9 @@ var _BLAKE2s = class extends _BLAKE2 {
|
|
|
2825
2805
|
this.v7 = v7 | 0;
|
|
2826
2806
|
}
|
|
2827
2807
|
compress(msg, offset, isLast) {
|
|
2828
|
-
const
|
|
2829
|
-
const
|
|
2808
|
+
const l = fromNumL(this.length);
|
|
2809
|
+
const h = fromNumH(this.length);
|
|
2810
|
+
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = _compress(BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
|
|
2830
2811
|
this.v0 ^= v0 ^ v8;
|
|
2831
2812
|
this.v1 ^= v1 ^ v9;
|
|
2832
2813
|
this.v2 ^= v2 ^ v10;
|
|
@@ -2858,18 +2839,18 @@ var _HMAC = class {
|
|
|
2858
2839
|
abytes(key, void 0, "key");
|
|
2859
2840
|
this.iHash = hash.create();
|
|
2860
2841
|
if (typeof this.iHash.update !== "function")
|
|
2861
|
-
throw new Error("
|
|
2842
|
+
throw new Error("expected Hash instance");
|
|
2862
2843
|
this.blockLen = this.iHash.blockLen;
|
|
2863
2844
|
this.outputLen = this.iHash.outputLen;
|
|
2864
2845
|
const blockLen = this.blockLen;
|
|
2865
2846
|
const pad = new Uint8Array(blockLen);
|
|
2866
2847
|
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
2867
|
-
for (let
|
|
2868
|
-
pad[
|
|
2848
|
+
for (let i2 = 0; i2 < pad.length; i2++)
|
|
2849
|
+
pad[i2] ^= 54;
|
|
2869
2850
|
this.iHash.update(pad);
|
|
2870
2851
|
this.oHash = hash.create();
|
|
2871
|
-
for (let
|
|
2872
|
-
pad[
|
|
2852
|
+
for (let i2 = 0; i2 < pad.length; i2++)
|
|
2853
|
+
pad[i2] ^= 54 ^ 92;
|
|
2873
2854
|
this.oHash.update(pad);
|
|
2874
2855
|
clean(pad);
|
|
2875
2856
|
}
|
|
@@ -2895,12 +2876,13 @@ var _HMAC = class {
|
|
|
2895
2876
|
}
|
|
2896
2877
|
_cloneInto(to) {
|
|
2897
2878
|
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
2898
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
2879
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen, canXOF } = this;
|
|
2899
2880
|
to = to;
|
|
2900
2881
|
to.finished = finished;
|
|
2901
2882
|
to.destroyed = destroyed;
|
|
2902
2883
|
to.blockLen = blockLen;
|
|
2903
2884
|
to.outputLen = outputLen;
|
|
2885
|
+
to.canXOF = canXOF;
|
|
2904
2886
|
to.oHash = oHash._cloneInto(to.oHash);
|
|
2905
2887
|
to.iHash = iHash._cloneInto(to.iHash);
|
|
2906
2888
|
return to;
|
|
@@ -2921,15 +2903,9 @@ var hmac = /* @__PURE__ */ (() => {
|
|
|
2921
2903
|
})();
|
|
2922
2904
|
|
|
2923
2905
|
// ../../node_modules/@noble/hashes/hkdf.js
|
|
2924
|
-
function extract(hash, ikm, salt) {
|
|
2925
|
-
ahash(hash);
|
|
2926
|
-
if (salt === void 0)
|
|
2927
|
-
salt = new Uint8Array(hash.outputLen);
|
|
2928
|
-
return hmac(hash, salt, ikm);
|
|
2929
|
-
}
|
|
2930
2906
|
var HKDF_COUNTER = /* @__PURE__ */ Uint8Array.of(0);
|
|
2931
2907
|
var EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
|
2932
|
-
function expand(hash, prk, info, length = 32) {
|
|
2908
|
+
function expand(hash, prk, info, length = 32, _recycled) {
|
|
2933
2909
|
ahash(hash);
|
|
2934
2910
|
anumber(length, "length");
|
|
2935
2911
|
abytes(prk, void 0, "prk");
|
|
@@ -2943,99 +2919,144 @@ function expand(hash, prk, info, length = 32) {
|
|
|
2943
2919
|
info = EMPTY_BUFFER;
|
|
2944
2920
|
else
|
|
2945
2921
|
abytes(info, void 0, "info");
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2922
|
+
if (!blocks) {
|
|
2923
|
+
if (_recycled)
|
|
2924
|
+
clean(prk);
|
|
2925
|
+
return new Uint8Array();
|
|
2926
|
+
}
|
|
2927
|
+
const okm = _recycled && blocks === 1 ? prk : new Uint8Array(blocks * olen);
|
|
2928
|
+
const { iHash, oHash } = hmac.create(hash, prk);
|
|
2929
|
+
const T = _recycled ? prk : new Uint8Array(olen);
|
|
2930
|
+
const worker = blocks > 1 ? _recycled?.iHash || hash.create() : void 0;
|
|
2931
|
+
for (let counter = 0; counter < blocks - 1; counter++) {
|
|
2951
2932
|
HKDF_COUNTER[0] = counter + 1;
|
|
2952
|
-
|
|
2933
|
+
const iWork = iHash._cloneInto(worker);
|
|
2934
|
+
if (counter)
|
|
2935
|
+
iWork.update(T);
|
|
2936
|
+
iWork.update(info).update(HKDF_COUNTER).digestInto(T);
|
|
2937
|
+
oHash._cloneInto(worker).update(T).digestInto(T);
|
|
2953
2938
|
okm.set(T, olen * counter);
|
|
2954
|
-
HMAC._cloneInto(HMACTmp);
|
|
2955
2939
|
}
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2940
|
+
HKDF_COUNTER[0] = blocks;
|
|
2941
|
+
if (blocks > 1)
|
|
2942
|
+
iHash.update(T);
|
|
2943
|
+
iHash.update(info).update(HKDF_COUNTER).digestInto(T);
|
|
2944
|
+
oHash.update(T).digestInto(T);
|
|
2945
|
+
okm.set(T, olen * (blocks - 1));
|
|
2946
|
+
iHash.destroy();
|
|
2947
|
+
oHash.destroy();
|
|
2948
|
+
worker?.destroy();
|
|
2949
|
+
if (T !== okm)
|
|
2950
|
+
clean(T);
|
|
2951
|
+
clean(HKDF_COUNTER);
|
|
2952
|
+
if (length === okm.length)
|
|
2953
|
+
return okm;
|
|
2954
|
+
const res = okm.slice(0, length);
|
|
2955
|
+
clean(okm);
|
|
2956
|
+
return res;
|
|
2960
2957
|
}
|
|
2961
|
-
var hkdf = (hash, ikm, salt, info, length) =>
|
|
2958
|
+
var hkdf = (hash, ikm, salt, info, length) => {
|
|
2959
|
+
ahash(hash);
|
|
2960
|
+
if (salt === void 0)
|
|
2961
|
+
salt = new Uint8Array(hash.outputLen);
|
|
2962
|
+
const HMAC = hmac.create(hash, salt).update(ikm);
|
|
2963
|
+
return expand(hash, HMAC.digest(), info, length, HMAC);
|
|
2964
|
+
};
|
|
2962
2965
|
|
|
2963
2966
|
// ../../node_modules/@noble/ciphers/utils.js
|
|
2964
2967
|
function isBytes3(a) {
|
|
2965
2968
|
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
2966
2969
|
}
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
+
var atitle3 = (title) => title ? `"${title}" ` : "";
|
|
2971
|
+
function abool2(value, title = "") {
|
|
2972
|
+
if (typeof value !== "boolean")
|
|
2973
|
+
throw new TypeError(atitle3(title) + "expected boolean, got type=" + typeof value);
|
|
2974
|
+
return value;
|
|
2970
2975
|
}
|
|
2971
|
-
function anumber3(n) {
|
|
2976
|
+
function anumber3(n, title = "") {
|
|
2972
2977
|
if (typeof n !== "number")
|
|
2973
|
-
throw new TypeError("number
|
|
2978
|
+
throw new TypeError(atitle3(title) + "expected number, got " + typeof n);
|
|
2974
2979
|
if (!Number.isSafeInteger(n) || n < 0)
|
|
2975
|
-
throw new RangeError("
|
|
2980
|
+
throw new RangeError(atitle3(title) + "expected integer >= 0, got " + n);
|
|
2981
|
+
return n;
|
|
2976
2982
|
}
|
|
2977
2983
|
function abytes3(value, length, title = "") {
|
|
2984
|
+
if (isBytes3(value) && (length === void 0 || value.length === length))
|
|
2985
|
+
return value;
|
|
2986
|
+
if (length !== void 0)
|
|
2987
|
+
anumber3(length, "length");
|
|
2978
2988
|
const bytes = isBytes3(value);
|
|
2979
|
-
const
|
|
2980
|
-
const
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
throw new
|
|
2989
|
-
|
|
2990
|
-
return value;
|
|
2991
|
-
}
|
|
2989
|
+
const ofLen = length !== void 0 ? ` of length ${length}` : "";
|
|
2990
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
2991
|
+
const message = atitle3(title) + "expected Uint8Array" + ofLen + ", got " + got;
|
|
2992
|
+
if (!bytes)
|
|
2993
|
+
throw new TypeError(message);
|
|
2994
|
+
throw new RangeError(message);
|
|
2995
|
+
}
|
|
2996
|
+
var aobject3 = (value, label) => {
|
|
2997
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
2998
|
+
throw new TypeError(label === "object" ? "expected valid options object" : `"${label}" expected object, got type=${typeof value}`);
|
|
2999
|
+
};
|
|
2992
3000
|
function aexists2(instance, checkFinished = true) {
|
|
2993
3001
|
if (instance.destroyed)
|
|
2994
|
-
throw new Error("
|
|
3002
|
+
throw new Error("hash was destroyed");
|
|
2995
3003
|
if (checkFinished && instance.finished)
|
|
2996
|
-
throw new Error("
|
|
3004
|
+
throw new Error("digest() was already called");
|
|
2997
3005
|
}
|
|
2998
|
-
function aoutput2(out, instance
|
|
3006
|
+
function aoutput2(out, instance) {
|
|
2999
3007
|
abytes3(out, void 0, "output");
|
|
3000
3008
|
const min = instance.outputLen;
|
|
3001
|
-
if (out.length
|
|
3002
|
-
throw new RangeError("
|
|
3009
|
+
if (!(out.length >= min)) {
|
|
3010
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
3003
3011
|
}
|
|
3004
|
-
if (onlyAligned && !isAligned32(out))
|
|
3005
|
-
throw new Error("invalid output, must be aligned");
|
|
3006
3012
|
}
|
|
3007
3013
|
function u322(arr) {
|
|
3008
3014
|
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
3009
3015
|
}
|
|
3010
3016
|
function clean2(...arrays) {
|
|
3011
|
-
for (let
|
|
3012
|
-
arrays[
|
|
3017
|
+
for (let i2 = 0; i2 < arrays.length; i2++) {
|
|
3018
|
+
arrays[i2].fill(0);
|
|
3013
3019
|
}
|
|
3014
3020
|
}
|
|
3015
3021
|
function createView2(arr) {
|
|
3016
3022
|
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
3017
3023
|
}
|
|
3018
3024
|
var isLE2 = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3025
|
+
function byteSwap2(word) {
|
|
3026
|
+
return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
|
|
3027
|
+
}
|
|
3028
|
+
function byteSwap322(arr) {
|
|
3029
|
+
for (let i2 = 0; i2 < arr.length; i2++) {
|
|
3030
|
+
arr[i2] = byteSwap2(arr[i2]);
|
|
3031
|
+
}
|
|
3024
3032
|
return arr;
|
|
3025
|
-
}
|
|
3033
|
+
}
|
|
3026
3034
|
var swap32IfBE2 = isLE2 ? (u) => u : byteSwap322;
|
|
3027
|
-
function
|
|
3028
|
-
if (
|
|
3029
|
-
|
|
3035
|
+
function overlapBytes(a, b) {
|
|
3036
|
+
if (!a.byteLength || !b.byteLength)
|
|
3037
|
+
return false;
|
|
3038
|
+
return a.buffer === b.buffer && // best we can do, may fail with an obscure Proxy
|
|
3039
|
+
a.byteOffset < b.byteOffset + b.byteLength && // a starts before b end
|
|
3040
|
+
b.byteOffset < a.byteOffset + a.byteLength;
|
|
3041
|
+
}
|
|
3042
|
+
function complexOverlapBytes(input, output) {
|
|
3043
|
+
if (overlapBytes(input, output) && input.byteOffset < output.byteOffset)
|
|
3044
|
+
throw new Error("complex overlap of input and output is not supported");
|
|
3045
|
+
}
|
|
3046
|
+
function checkOpts2(defaults, opts) {
|
|
3047
|
+
aobject3(defaults, "defaults");
|
|
3048
|
+
aobject3(opts, "opts");
|
|
3030
3049
|
const merged = Object.assign(defaults, opts);
|
|
3031
3050
|
return merged;
|
|
3032
3051
|
}
|
|
3033
|
-
function
|
|
3052
|
+
function equalBytes(a, b) {
|
|
3053
|
+
a = abytes3(a);
|
|
3054
|
+
b = abytes3(b);
|
|
3034
3055
|
if (a.length !== b.length)
|
|
3035
3056
|
return false;
|
|
3036
3057
|
let diff = 0;
|
|
3037
|
-
for (let
|
|
3038
|
-
diff |= a[
|
|
3058
|
+
for (let i2 = 0; i2 < a.length; i2++)
|
|
3059
|
+
diff |= a[i2] ^ b[i2];
|
|
3039
3060
|
return diff === 0;
|
|
3040
3061
|
}
|
|
3041
3062
|
function wrapMacConstructor(keyLen, macCons, fromMsg) {
|
|
@@ -3056,8 +3077,14 @@ var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
|
|
|
3056
3077
|
abytes3(nonce, params.varSizeNonce ? void 0 : params.nonceLength, "nonce");
|
|
3057
3078
|
}
|
|
3058
3079
|
const tagl = params.tagLength;
|
|
3059
|
-
|
|
3060
|
-
|
|
3080
|
+
const aadStart = params.nonceLength !== void 0 ? 1 : 0;
|
|
3081
|
+
if (!params.withAAD) {
|
|
3082
|
+
for (let i2 = aadStart; i2 < args.length; i2++)
|
|
3083
|
+
if (isBytes3(args[i2]))
|
|
3084
|
+
throw new Error("AAD not supported");
|
|
3085
|
+
}
|
|
3086
|
+
if (params.withAAD && args[aadStart] !== void 0)
|
|
3087
|
+
abytes3(args[aadStart], void 0, "AAD");
|
|
3061
3088
|
const cipher = constructor(key, ...args);
|
|
3062
3089
|
const checkOutput = (fnLength, output) => {
|
|
3063
3090
|
if (output !== void 0) {
|
|
@@ -3072,14 +3099,14 @@ var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
|
|
|
3072
3099
|
if (called)
|
|
3073
3100
|
throw new Error("cannot encrypt() twice with same key + nonce");
|
|
3074
3101
|
called = true;
|
|
3075
|
-
abytes3(data);
|
|
3102
|
+
abytes3(data, void 0, "data");
|
|
3076
3103
|
checkOutput(cipher.encrypt.length, output);
|
|
3077
3104
|
return cipher.encrypt(data, output);
|
|
3078
3105
|
},
|
|
3079
3106
|
decrypt(data, output) {
|
|
3080
|
-
abytes3(data);
|
|
3107
|
+
abytes3(data, void 0, "data");
|
|
3081
3108
|
if (tagl && data.length < tagl)
|
|
3082
|
-
throw new Error('"ciphertext" expected length
|
|
3109
|
+
throw new Error('"ciphertext" expected length >= tagLength=' + tagl);
|
|
3083
3110
|
checkOutput(cipher.decrypt.length, output);
|
|
3084
3111
|
return cipher.decrypt(data, output);
|
|
3085
3112
|
}
|
|
@@ -3092,9 +3119,7 @@ var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
|
|
|
3092
3119
|
function getOutput(expectedLength, out, onlyAligned = true) {
|
|
3093
3120
|
if (out === void 0)
|
|
3094
3121
|
return new Uint8Array(expectedLength);
|
|
3095
|
-
abytes3(out,
|
|
3096
|
-
if (out.length !== expectedLength)
|
|
3097
|
-
throw new Error('"output" expected Uint8Array of length ' + expectedLength + ", got: " + out.length);
|
|
3122
|
+
abytes3(out, expectedLength, "output");
|
|
3098
3123
|
if (onlyAligned && !isAligned32(out))
|
|
3099
3124
|
throw new Error("invalid output, must be aligned");
|
|
3100
3125
|
return out;
|
|
@@ -3112,7 +3137,7 @@ function u64Lengths(dataLength, aadLength, isLE3) {
|
|
|
3112
3137
|
function isAligned32(bytes) {
|
|
3113
3138
|
return bytes.byteOffset % 4 === 0;
|
|
3114
3139
|
}
|
|
3115
|
-
function
|
|
3140
|
+
function copyBytes3(bytes) {
|
|
3116
3141
|
return Uint8Array.from(abytes3(bytes));
|
|
3117
3142
|
}
|
|
3118
3143
|
|
|
@@ -3173,7 +3198,7 @@ function runCipher(core, sigma, key, nonce, data, output, counter, rounds) {
|
|
|
3173
3198
|
}
|
|
3174
3199
|
}
|
|
3175
3200
|
function createCipher(core, opts) {
|
|
3176
|
-
const { allowShortKeys, extendNonceFn, counterLength, counterRight, rounds } =
|
|
3201
|
+
const { allowShortKeys, extendNonceFn, counterLength, counterRight, rounds } = checkOpts2({ allowShortKeys: false, counterLength: 8, counterRight: false, rounds: 20 }, opts);
|
|
3177
3202
|
if (typeof core !== "function")
|
|
3178
3203
|
throw new Error("core must be a function");
|
|
3179
3204
|
anumber3(counterLength);
|
|
@@ -3185,7 +3210,10 @@ function createCipher(core, opts) {
|
|
|
3185
3210
|
abytes3(nonce, void 0, "nonce");
|
|
3186
3211
|
abytes3(data, void 0, "data");
|
|
3187
3212
|
const len = data.length;
|
|
3213
|
+
const hasOutput = output !== void 0;
|
|
3188
3214
|
output = getOutput(len, output, false);
|
|
3215
|
+
if (hasOutput)
|
|
3216
|
+
complexOverlapBytes(data, output);
|
|
3189
3217
|
anumber3(counter);
|
|
3190
3218
|
if (counter < 0 || counter >= MAX_COUNTER)
|
|
3191
3219
|
throw new Error("arx: counter overflow");
|
|
@@ -3194,7 +3222,7 @@ function createCipher(core, opts) {
|
|
|
3194
3222
|
let k;
|
|
3195
3223
|
let sigma;
|
|
3196
3224
|
if (l === 32) {
|
|
3197
|
-
toClean.push(k =
|
|
3225
|
+
toClean.push(k = copyBytes3(key));
|
|
3198
3226
|
sigma = sigma32_32;
|
|
3199
3227
|
} else if (l === 16 && allowShortKeys) {
|
|
3200
3228
|
k = new Uint8Array(32);
|
|
@@ -3207,11 +3235,11 @@ function createCipher(core, opts) {
|
|
|
3207
3235
|
throw new Error("invalid key size");
|
|
3208
3236
|
}
|
|
3209
3237
|
if (!isLE2 || !isAligned32(nonce))
|
|
3210
|
-
toClean.push(nonce =
|
|
3238
|
+
toClean.push(nonce = copyBytes3(nonce));
|
|
3211
3239
|
let k32 = u322(k);
|
|
3212
3240
|
if (extendNonceFn) {
|
|
3213
3241
|
if (nonce.length !== 24)
|
|
3214
|
-
throw new Error(
|
|
3242
|
+
throw new Error("arx: extended nonce must be 24 bytes");
|
|
3215
3243
|
const n16 = nonce.subarray(0, 16);
|
|
3216
3244
|
if (isLE2)
|
|
3217
3245
|
extendNonceFn(sigma, k32, u322(n16), k32);
|
|
@@ -3244,8 +3272,8 @@ function createCipher(core, opts) {
|
|
|
3244
3272
|
}
|
|
3245
3273
|
|
|
3246
3274
|
// ../../node_modules/@noble/ciphers/_poly1305.js
|
|
3247
|
-
function u8to16(a,
|
|
3248
|
-
return a[
|
|
3275
|
+
function u8to16(a, i2) {
|
|
3276
|
+
return a[i2++] & 255 | (a[i2++] & 255) << 8;
|
|
3249
3277
|
}
|
|
3250
3278
|
var Poly1305 = class {
|
|
3251
3279
|
blockLen = 16;
|
|
@@ -3260,7 +3288,7 @@ var Poly1305 = class {
|
|
|
3260
3288
|
destroyed = false;
|
|
3261
3289
|
// Can be speed-up using BigUint64Array, at the cost of complexity
|
|
3262
3290
|
constructor(key) {
|
|
3263
|
-
key =
|
|
3291
|
+
key = copyBytes3(abytes3(key, 32, "key"));
|
|
3264
3292
|
const t0 = u8to16(key, 0);
|
|
3265
3293
|
const t1 = u8to16(key, 2);
|
|
3266
3294
|
const t2 = u8to16(key, 4);
|
|
@@ -3279,8 +3307,8 @@ var Poly1305 = class {
|
|
|
3279
3307
|
this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
3280
3308
|
this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
3281
3309
|
this.r[9] = t7 >>> 5 & 127;
|
|
3282
|
-
for (let
|
|
3283
|
-
this.pad[
|
|
3310
|
+
for (let i2 = 0; i2 < 8; i2++)
|
|
3311
|
+
this.pad[i2] = u8to16(key, 16 + 2 * i2);
|
|
3284
3312
|
}
|
|
3285
3313
|
process(data, offset, isLast = false) {
|
|
3286
3314
|
const hibit = isLast ? 0 : 1 << 11;
|
|
@@ -3395,10 +3423,10 @@ var Poly1305 = class {
|
|
|
3395
3423
|
const g = new Uint16Array(10);
|
|
3396
3424
|
let c = h[1] >>> 13;
|
|
3397
3425
|
h[1] &= 8191;
|
|
3398
|
-
for (let
|
|
3399
|
-
h[
|
|
3400
|
-
c = h[
|
|
3401
|
-
h[
|
|
3426
|
+
for (let i2 = 2; i2 < 10; i2++) {
|
|
3427
|
+
h[i2] += c;
|
|
3428
|
+
c = h[i2] >>> 13;
|
|
3429
|
+
h[i2] &= 8191;
|
|
3402
3430
|
}
|
|
3403
3431
|
h[0] += c * 5;
|
|
3404
3432
|
c = h[0] >>> 13;
|
|
@@ -3410,18 +3438,18 @@ var Poly1305 = class {
|
|
|
3410
3438
|
g[0] = h[0] + 5;
|
|
3411
3439
|
c = g[0] >>> 13;
|
|
3412
3440
|
g[0] &= 8191;
|
|
3413
|
-
for (let
|
|
3414
|
-
g[
|
|
3415
|
-
c = g[
|
|
3416
|
-
g[
|
|
3441
|
+
for (let i2 = 1; i2 < 10; i2++) {
|
|
3442
|
+
g[i2] = h[i2] + c;
|
|
3443
|
+
c = g[i2] >>> 13;
|
|
3444
|
+
g[i2] &= 8191;
|
|
3417
3445
|
}
|
|
3418
3446
|
g[9] -= 1 << 13;
|
|
3419
3447
|
let mask = (c ^ 1) - 1;
|
|
3420
|
-
for (let
|
|
3421
|
-
g[
|
|
3448
|
+
for (let i2 = 0; i2 < 10; i2++)
|
|
3449
|
+
g[i2] &= mask;
|
|
3422
3450
|
mask = ~mask;
|
|
3423
|
-
for (let
|
|
3424
|
-
h[
|
|
3451
|
+
for (let i2 = 0; i2 < 10; i2++)
|
|
3452
|
+
h[i2] = h[i2] & mask | g[i2];
|
|
3425
3453
|
h[0] = (h[0] | h[1] << 13) & 65535;
|
|
3426
3454
|
h[1] = (h[1] >>> 3 | h[2] << 10) & 65535;
|
|
3427
3455
|
h[2] = (h[2] >>> 6 | h[3] << 7) & 65535;
|
|
@@ -3432,16 +3460,16 @@ var Poly1305 = class {
|
|
|
3432
3460
|
h[7] = (h[8] >>> 8 | h[9] << 5) & 65535;
|
|
3433
3461
|
let f = h[0] + pad[0];
|
|
3434
3462
|
h[0] = f & 65535;
|
|
3435
|
-
for (let
|
|
3436
|
-
f = (h[
|
|
3437
|
-
h[
|
|
3463
|
+
for (let i2 = 1; i2 < 8; i2++) {
|
|
3464
|
+
f = (h[i2] + pad[i2] | 0) + (f >>> 16) | 0;
|
|
3465
|
+
h[i2] = f & 65535;
|
|
3438
3466
|
}
|
|
3439
3467
|
clean2(g);
|
|
3440
3468
|
}
|
|
3441
3469
|
update(data) {
|
|
3442
3470
|
aexists2(this);
|
|
3443
3471
|
abytes3(data);
|
|
3444
|
-
data =
|
|
3472
|
+
data = copyBytes3(data);
|
|
3445
3473
|
const { buffer, blockLen } = this;
|
|
3446
3474
|
const len = data.length;
|
|
3447
3475
|
for (let pos = 0; pos < len; ) {
|
|
@@ -3479,9 +3507,9 @@ var Poly1305 = class {
|
|
|
3479
3507
|
}
|
|
3480
3508
|
this.finalize();
|
|
3481
3509
|
let opos = 0;
|
|
3482
|
-
for (let
|
|
3483
|
-
out[opos++] = h[
|
|
3484
|
-
out[opos++] = h[
|
|
3510
|
+
for (let i2 = 0; i2 < 8; i2++) {
|
|
3511
|
+
out[opos++] = h[i2] >>> 0;
|
|
3512
|
+
out[opos++] = h[i2] >>> 8;
|
|
3485
3513
|
}
|
|
3486
3514
|
}
|
|
3487
3515
|
digest() {
|
|
@@ -3582,84 +3610,25 @@ function chachaCore(s, k, n, out, cnt, rounds = 20) {
|
|
|
3582
3610
|
out[oi++] = y14 + x14 | 0;
|
|
3583
3611
|
out[oi++] = y15 + x15 | 0;
|
|
3584
3612
|
}
|
|
3585
|
-
function hchacha(s, k,
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
x04 = rotl(x04 ^ x08, 12);
|
|
3592
|
-
x00 = x00 + x04 | 0;
|
|
3593
|
-
x12 = rotl(x12 ^ x00, 8);
|
|
3594
|
-
x08 = x08 + x12 | 0;
|
|
3595
|
-
x04 = rotl(x04 ^ x08, 7);
|
|
3596
|
-
x01 = x01 + x05 | 0;
|
|
3597
|
-
x13 = rotl(x13 ^ x01, 16);
|
|
3598
|
-
x09 = x09 + x13 | 0;
|
|
3599
|
-
x05 = rotl(x05 ^ x09, 12);
|
|
3600
|
-
x01 = x01 + x05 | 0;
|
|
3601
|
-
x13 = rotl(x13 ^ x01, 8);
|
|
3602
|
-
x09 = x09 + x13 | 0;
|
|
3603
|
-
x05 = rotl(x05 ^ x09, 7);
|
|
3604
|
-
x02 = x02 + x06 | 0;
|
|
3605
|
-
x14 = rotl(x14 ^ x02, 16);
|
|
3606
|
-
x10 = x10 + x14 | 0;
|
|
3607
|
-
x06 = rotl(x06 ^ x10, 12);
|
|
3608
|
-
x02 = x02 + x06 | 0;
|
|
3609
|
-
x14 = rotl(x14 ^ x02, 8);
|
|
3610
|
-
x10 = x10 + x14 | 0;
|
|
3611
|
-
x06 = rotl(x06 ^ x10, 7);
|
|
3612
|
-
x03 = x03 + x07 | 0;
|
|
3613
|
-
x15 = rotl(x15 ^ x03, 16);
|
|
3614
|
-
x11 = x11 + x15 | 0;
|
|
3615
|
-
x07 = rotl(x07 ^ x11, 12);
|
|
3616
|
-
x03 = x03 + x07 | 0;
|
|
3617
|
-
x15 = rotl(x15 ^ x03, 8);
|
|
3618
|
-
x11 = x11 + x15 | 0;
|
|
3619
|
-
x07 = rotl(x07 ^ x11, 7);
|
|
3620
|
-
x00 = x00 + x05 | 0;
|
|
3621
|
-
x15 = rotl(x15 ^ x00, 16);
|
|
3622
|
-
x10 = x10 + x15 | 0;
|
|
3623
|
-
x05 = rotl(x05 ^ x10, 12);
|
|
3624
|
-
x00 = x00 + x05 | 0;
|
|
3625
|
-
x15 = rotl(x15 ^ x00, 8);
|
|
3626
|
-
x10 = x10 + x15 | 0;
|
|
3627
|
-
x05 = rotl(x05 ^ x10, 7);
|
|
3628
|
-
x01 = x01 + x06 | 0;
|
|
3629
|
-
x12 = rotl(x12 ^ x01, 16);
|
|
3630
|
-
x11 = x11 + x12 | 0;
|
|
3631
|
-
x06 = rotl(x06 ^ x11, 12);
|
|
3632
|
-
x01 = x01 + x06 | 0;
|
|
3633
|
-
x12 = rotl(x12 ^ x01, 8);
|
|
3634
|
-
x11 = x11 + x12 | 0;
|
|
3635
|
-
x06 = rotl(x06 ^ x11, 7);
|
|
3636
|
-
x02 = x02 + x07 | 0;
|
|
3637
|
-
x13 = rotl(x13 ^ x02, 16);
|
|
3638
|
-
x08 = x08 + x13 | 0;
|
|
3639
|
-
x07 = rotl(x07 ^ x08, 12);
|
|
3640
|
-
x02 = x02 + x07 | 0;
|
|
3641
|
-
x13 = rotl(x13 ^ x02, 8);
|
|
3642
|
-
x08 = x08 + x13 | 0;
|
|
3643
|
-
x07 = rotl(x07 ^ x08, 7);
|
|
3644
|
-
x03 = x03 + x04 | 0;
|
|
3645
|
-
x14 = rotl(x14 ^ x03, 16);
|
|
3646
|
-
x09 = x09 + x14 | 0;
|
|
3647
|
-
x04 = rotl(x04 ^ x09, 12);
|
|
3648
|
-
x03 = x03 + x04 | 0;
|
|
3649
|
-
x14 = rotl(x14 ^ x03, 8);
|
|
3650
|
-
x09 = x09 + x14 | 0;
|
|
3651
|
-
x04 = rotl(x04 ^ x09, 7);
|
|
3652
|
-
}
|
|
3613
|
+
function hchacha(s, k, i2, out) {
|
|
3614
|
+
const s2 = isLE2 ? s : swap32IfBE2(s.slice(0, 4));
|
|
3615
|
+
const k2 = isLE2 ? k : swap32IfBE2(k.slice(0, 8));
|
|
3616
|
+
const i22 = isLE2 ? i2 : swap32IfBE2(i2.slice(0, 4));
|
|
3617
|
+
const t = new Uint32Array(16);
|
|
3618
|
+
chachaCore(s2, k2, i22.subarray(1), t, i22[0]);
|
|
3653
3619
|
let oi = 0;
|
|
3654
|
-
out[oi++] =
|
|
3655
|
-
out[oi++] =
|
|
3656
|
-
out[oi++] =
|
|
3657
|
-
out[oi++] =
|
|
3658
|
-
out[oi++] =
|
|
3659
|
-
out[oi++] =
|
|
3660
|
-
out[oi++] =
|
|
3661
|
-
out[oi++] =
|
|
3620
|
+
out[oi++] = t[0] - s2[0] | 0;
|
|
3621
|
+
out[oi++] = t[1] - s2[1] | 0;
|
|
3622
|
+
out[oi++] = t[2] - s2[2] | 0;
|
|
3623
|
+
out[oi++] = t[3] - s2[3] | 0;
|
|
3624
|
+
out[oi++] = t[12] - i22[0] | 0;
|
|
3625
|
+
out[oi++] = t[13] - i22[1] | 0;
|
|
3626
|
+
out[oi++] = t[14] - i22[2] | 0;
|
|
3627
|
+
out[oi++] = t[15] - i22[3] | 0;
|
|
3662
3628
|
swap32IfBE2(out);
|
|
3629
|
+
if (!isLE2)
|
|
3630
|
+
clean2(s2, k2, i22);
|
|
3631
|
+
clean2(t);
|
|
3663
3632
|
}
|
|
3664
3633
|
var chacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
3665
3634
|
counterRight: false,
|
|
@@ -3713,7 +3682,7 @@ var _poly1305_aead = (xorStream) => (key, nonce, AAD) => {
|
|
|
3713
3682
|
const data = ciphertext.subarray(0, -tagLength);
|
|
3714
3683
|
const passedTag = ciphertext.subarray(-tagLength);
|
|
3715
3684
|
const tag = computeTag(xorStream, key, nonce, data, AAD);
|
|
3716
|
-
if (!
|
|
3685
|
+
if (!equalBytes(passedTag, tag)) {
|
|
3717
3686
|
clean2(tag);
|
|
3718
3687
|
throw new Error("invalid tag");
|
|
3719
3688
|
}
|
|
@@ -3725,11 +3694,11 @@ var _poly1305_aead = (xorStream) => (key, nonce, AAD) => {
|
|
|
3725
3694
|
};
|
|
3726
3695
|
};
|
|
3727
3696
|
var chacha20poly1305 = /* @__PURE__ */ wrapCipher(
|
|
3728
|
-
{ blockSize: 64, nonceLength: 12, tagLength: 16 },
|
|
3697
|
+
{ blockSize: 64, nonceLength: 12, tagLength: 16, withAAD: true },
|
|
3729
3698
|
/* @__PURE__ */ _poly1305_aead(chacha20)
|
|
3730
3699
|
);
|
|
3731
3700
|
var xchacha20poly1305 = /* @__PURE__ */ wrapCipher(
|
|
3732
|
-
{ blockSize: 64, nonceLength: 24, tagLength: 16 },
|
|
3701
|
+
{ blockSize: 64, nonceLength: 24, tagLength: 16, withAAD: true },
|
|
3733
3702
|
/* @__PURE__ */ _poly1305_aead(xchacha20)
|
|
3734
3703
|
);
|
|
3735
3704
|
|
|
@@ -3780,8 +3749,8 @@ function hashBlake2s(data) {
|
|
|
3780
3749
|
function noiseHkdf(chainingKey, inputKeyMaterial, numOutputs) {
|
|
3781
3750
|
const output = hkdf(blake2s, inputKeyMaterial, chainingKey, new Uint8Array(0), numOutputs * HASH_LENGTH);
|
|
3782
3751
|
const chunks = [];
|
|
3783
|
-
for (let
|
|
3784
|
-
chunks.push(output.subarray(
|
|
3752
|
+
for (let i2 = 0; i2 < numOutputs; i2++) {
|
|
3753
|
+
chunks.push(output.subarray(i2 * HASH_LENGTH, (i2 + 1) * HASH_LENGTH));
|
|
3785
3754
|
}
|
|
3786
3755
|
return chunks;
|
|
3787
3756
|
}
|
|
@@ -3816,7 +3785,7 @@ function xaeadDecrypt(key, nonce, associatedData, ciphertext) {
|
|
|
3816
3785
|
const cipher = xchacha20poly1305(key, nonce, associatedData);
|
|
3817
3786
|
return cipher.decrypt(ciphertext);
|
|
3818
3787
|
}
|
|
3819
|
-
function
|
|
3788
|
+
function concatBytes3(...arrays) {
|
|
3820
3789
|
const total = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
3821
3790
|
const result = new Uint8Array(total);
|
|
3822
3791
|
let offset = 0;
|
|
@@ -3863,7 +3832,7 @@ var SymmetricState = class {
|
|
|
3863
3832
|
hash;
|
|
3864
3833
|
constructor(protocolName) {
|
|
3865
3834
|
if (protocolName.length <= HASH_LENGTH) {
|
|
3866
|
-
this.hash =
|
|
3835
|
+
this.hash = concatBytes3(protocolName, new Uint8Array(HASH_LENGTH - protocolName.length));
|
|
3867
3836
|
} else {
|
|
3868
3837
|
this.hash = hashBlake2s(protocolName);
|
|
3869
3838
|
}
|
|
@@ -3876,7 +3845,7 @@ var SymmetricState = class {
|
|
|
3876
3845
|
this.cipherState.initializeKey(tempKey);
|
|
3877
3846
|
}
|
|
3878
3847
|
mixHash(data) {
|
|
3879
|
-
this.hash = hashBlake2s(
|
|
3848
|
+
this.hash = hashBlake2s(concatBytes3(this.hash, data));
|
|
3880
3849
|
}
|
|
3881
3850
|
/** Processes a "psk" token: MixKeyAndHash(psk) per Noise section 9. */
|
|
3882
3851
|
mixKeyAndHash(inputKeyMaterial) {
|
|
@@ -4036,7 +4005,7 @@ var HandshakeState = class {
|
|
|
4036
4005
|
}
|
|
4037
4006
|
}
|
|
4038
4007
|
buffers.push(this.symmetricState.encryptAndHash(payload));
|
|
4039
|
-
return { message:
|
|
4008
|
+
return { message: concatBytes3(...buffers), split: this.maybeSplit() };
|
|
4040
4009
|
}
|
|
4041
4010
|
readMessage(message) {
|
|
4042
4011
|
const tokens = this.nextTokens();
|
|
@@ -4166,7 +4135,7 @@ var SecretstreamState = class {
|
|
|
4166
4135
|
const associatedData = Uint8Array.of(tag);
|
|
4167
4136
|
const ciphertext = xaeadEncrypt(this.key, this.nonceForCounter(this.counter), associatedData, plaintext);
|
|
4168
4137
|
this.counter += 1n;
|
|
4169
|
-
return
|
|
4138
|
+
return concatBytes3(associatedData, ciphertext);
|
|
4170
4139
|
}
|
|
4171
4140
|
/**
|
|
4172
4141
|
* Decrypts one frame and advances this direction's counter. Throws if
|
|
@@ -4276,8 +4245,8 @@ function deriveShortAuthenticationString(handshakeHash) {
|
|
|
4276
4245
|
const digitsValue = digitsRaw % 1e6;
|
|
4277
4246
|
const digits = digitsValue.toString(10).padStart(6, "0");
|
|
4278
4247
|
const emoji = [];
|
|
4279
|
-
for (let
|
|
4280
|
-
emoji.push(SAS_EMOJI_TABLE[bytes[4 +
|
|
4248
|
+
for (let i2 = 0; i2 < 5; i2++) {
|
|
4249
|
+
emoji.push(SAS_EMOJI_TABLE[bytes[4 + i2] % SAS_EMOJI_TABLE.length]);
|
|
4281
4250
|
}
|
|
4282
4251
|
return { digits, emoji };
|
|
4283
4252
|
}
|
|
@@ -4286,7 +4255,7 @@ function deriveShortAuthenticationString(handshakeHash) {
|
|
|
4286
4255
|
var SESSION_SLOT_LABEL = "kangentic-session-slot-v1";
|
|
4287
4256
|
var SESSION_SLOT_LENGTH = 16;
|
|
4288
4257
|
function deriveSessionSlotId(desktopStaticPublicKey, phoneStaticPublicKey) {
|
|
4289
|
-
const material =
|
|
4258
|
+
const material = concatBytes3(desktopStaticPublicKey, phoneStaticPublicKey);
|
|
4290
4259
|
return bytesToHex(deriveLabeledKey(material, SESSION_SLOT_LABEL, SESSION_SLOT_LENGTH));
|
|
4291
4260
|
}
|
|
4292
4261
|
var PAIRING_SLOT_LABEL = "kangentic-pairing-slot-v1";
|
|
@@ -4382,13 +4351,13 @@ var fdeb = new u8([
|
|
|
4382
4351
|
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
|
|
4383
4352
|
var freb = function(eb, start) {
|
|
4384
4353
|
var b = new u16(31);
|
|
4385
|
-
for (var
|
|
4386
|
-
b[
|
|
4354
|
+
for (var i2 = 0; i2 < 31; ++i2) {
|
|
4355
|
+
b[i2] = start += 1 << eb[i2 - 1];
|
|
4387
4356
|
}
|
|
4388
4357
|
var r = new i32(b[30]);
|
|
4389
|
-
for (var
|
|
4390
|
-
for (var j = b[
|
|
4391
|
-
r[j] = j - b[
|
|
4358
|
+
for (var i2 = 1; i2 < 30; ++i2) {
|
|
4359
|
+
for (var j = b[i2]; j < b[i2 + 1]; ++j) {
|
|
4360
|
+
r[j] = j - b[i2] << 5 | i2;
|
|
4392
4361
|
}
|
|
4393
4362
|
}
|
|
4394
4363
|
return { b, r };
|
|
@@ -4411,25 +4380,25 @@ var x;
|
|
|
4411
4380
|
var i;
|
|
4412
4381
|
var hMap = (function(cd, mb, r) {
|
|
4413
4382
|
var s = cd.length;
|
|
4414
|
-
var
|
|
4383
|
+
var i2 = 0;
|
|
4415
4384
|
var l = new u16(mb);
|
|
4416
|
-
for (;
|
|
4417
|
-
if (cd[
|
|
4418
|
-
++l[cd[
|
|
4385
|
+
for (; i2 < s; ++i2) {
|
|
4386
|
+
if (cd[i2])
|
|
4387
|
+
++l[cd[i2] - 1];
|
|
4419
4388
|
}
|
|
4420
4389
|
var le = new u16(mb);
|
|
4421
|
-
for (
|
|
4422
|
-
le[
|
|
4390
|
+
for (i2 = 1; i2 < mb; ++i2) {
|
|
4391
|
+
le[i2] = le[i2 - 1] + l[i2 - 1] << 1;
|
|
4423
4392
|
}
|
|
4424
4393
|
var co;
|
|
4425
4394
|
if (r) {
|
|
4426
4395
|
co = new u16(1 << mb);
|
|
4427
4396
|
var rvb = 15 - mb;
|
|
4428
|
-
for (
|
|
4429
|
-
if (cd[
|
|
4430
|
-
var sv =
|
|
4431
|
-
var r_1 = mb - cd[
|
|
4432
|
-
var v = le[cd[
|
|
4397
|
+
for (i2 = 0; i2 < s; ++i2) {
|
|
4398
|
+
if (cd[i2]) {
|
|
4399
|
+
var sv = i2 << 4 | cd[i2];
|
|
4400
|
+
var r_1 = mb - cd[i2];
|
|
4401
|
+
var v = le[cd[i2] - 1]++ << r_1;
|
|
4433
4402
|
for (var m = v | (1 << r_1) - 1; v <= m; ++v) {
|
|
4434
4403
|
co[rev[v] >> rvb] = sv;
|
|
4435
4404
|
}
|
|
@@ -4437,9 +4406,9 @@ var hMap = (function(cd, mb, r) {
|
|
|
4437
4406
|
}
|
|
4438
4407
|
} else {
|
|
4439
4408
|
co = new u16(s);
|
|
4440
|
-
for (
|
|
4441
|
-
if (cd[
|
|
4442
|
-
co[
|
|
4409
|
+
for (i2 = 0; i2 < s; ++i2) {
|
|
4410
|
+
if (cd[i2]) {
|
|
4411
|
+
co[i2] = rev[le[cd[i2] - 1]++] >> 15 - cd[i2];
|
|
4443
4412
|
}
|
|
4444
4413
|
}
|
|
4445
4414
|
}
|
|
@@ -4468,9 +4437,9 @@ var fdm = /* @__PURE__ */ hMap(fdt, 5, 0);
|
|
|
4468
4437
|
var fdrm = /* @__PURE__ */ hMap(fdt, 5, 1);
|
|
4469
4438
|
var max = function(a) {
|
|
4470
4439
|
var m = a[0];
|
|
4471
|
-
for (var
|
|
4472
|
-
if (a[
|
|
4473
|
-
m = a[
|
|
4440
|
+
for (var i2 = 1; i2 < a.length; ++i2) {
|
|
4441
|
+
if (a[i2] > m)
|
|
4442
|
+
m = a[i2];
|
|
4474
4443
|
}
|
|
4475
4444
|
return m;
|
|
4476
4445
|
};
|
|
@@ -4563,28 +4532,28 @@ var inflt = function(dat, st, buf, dict) {
|
|
|
4563
4532
|
pos += 14;
|
|
4564
4533
|
var ldt = new u8(tl);
|
|
4565
4534
|
var clt = new u8(19);
|
|
4566
|
-
for (var
|
|
4567
|
-
clt[clim[
|
|
4535
|
+
for (var i2 = 0; i2 < hcLen; ++i2) {
|
|
4536
|
+
clt[clim[i2]] = bits(dat, pos + i2 * 3, 7);
|
|
4568
4537
|
}
|
|
4569
4538
|
pos += hcLen * 3;
|
|
4570
4539
|
var clb = max(clt), clbmsk = (1 << clb) - 1;
|
|
4571
4540
|
var clm = hMap(clt, clb, 1);
|
|
4572
|
-
for (var
|
|
4541
|
+
for (var i2 = 0; i2 < tl; ) {
|
|
4573
4542
|
var r = clm[bits(dat, pos, clbmsk)];
|
|
4574
4543
|
pos += r & 15;
|
|
4575
4544
|
var s = r >> 4;
|
|
4576
4545
|
if (s < 16) {
|
|
4577
|
-
ldt[
|
|
4546
|
+
ldt[i2++] = s;
|
|
4578
4547
|
} else {
|
|
4579
4548
|
var c = 0, n = 0;
|
|
4580
4549
|
if (s == 16)
|
|
4581
|
-
n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[
|
|
4550
|
+
n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i2 - 1];
|
|
4582
4551
|
else if (s == 17)
|
|
4583
4552
|
n = 3 + bits(dat, pos, 7), pos += 3;
|
|
4584
4553
|
else if (s == 18)
|
|
4585
4554
|
n = 11 + bits(dat, pos, 127), pos += 7;
|
|
4586
4555
|
while (n--)
|
|
4587
|
-
ldt[
|
|
4556
|
+
ldt[i2++] = c;
|
|
4588
4557
|
}
|
|
4589
4558
|
}
|
|
4590
4559
|
var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
|
|
@@ -4622,8 +4591,8 @@ var inflt = function(dat, st, buf, dict) {
|
|
|
4622
4591
|
} else {
|
|
4623
4592
|
var add2 = sym - 254;
|
|
4624
4593
|
if (sym > 264) {
|
|
4625
|
-
var
|
|
4626
|
-
add2 = bits(dat, pos, (1 << b) - 1) + fl[
|
|
4594
|
+
var i2 = sym - 257, b = fleb[i2];
|
|
4595
|
+
add2 = bits(dat, pos, (1 << b) - 1) + fl[i2];
|
|
4627
4596
|
pos += b;
|
|
4628
4597
|
}
|
|
4629
4598
|
var d = dm[bits16(dat, pos) & dms], dsym = d >> 4;
|
|
@@ -4675,9 +4644,9 @@ var wbits16 = function(d, p, v) {
|
|
|
4675
4644
|
};
|
|
4676
4645
|
var hTree = function(d, mb) {
|
|
4677
4646
|
var t = [];
|
|
4678
|
-
for (var
|
|
4679
|
-
if (d[
|
|
4680
|
-
t.push({ s:
|
|
4647
|
+
for (var i2 = 0; i2 < d.length; ++i2) {
|
|
4648
|
+
if (d[i2])
|
|
4649
|
+
t.push({ s: i2, f: d[i2] });
|
|
4681
4650
|
}
|
|
4682
4651
|
var s = t.length;
|
|
4683
4652
|
var t2 = t.slice();
|
|
@@ -4692,28 +4661,28 @@ var hTree = function(d, mb) {
|
|
|
4692
4661
|
return a.f - b.f;
|
|
4693
4662
|
});
|
|
4694
4663
|
t.push({ s: -1, f: 25001 });
|
|
4695
|
-
var l = t[0], r = t[1], i0 = 0, i1 = 1,
|
|
4664
|
+
var l = t[0], r = t[1], i0 = 0, i1 = 1, i22 = 2;
|
|
4696
4665
|
t[0] = { s: -1, f: l.f + r.f, l, r };
|
|
4697
4666
|
while (i1 != s - 1) {
|
|
4698
|
-
l = t[t[i0].f < t[
|
|
4699
|
-
r = t[i0 != i1 && t[i0].f < t[
|
|
4667
|
+
l = t[t[i0].f < t[i22].f ? i0++ : i22++];
|
|
4668
|
+
r = t[i0 != i1 && t[i0].f < t[i22].f ? i0++ : i22++];
|
|
4700
4669
|
t[i1++] = { s: -1, f: l.f + r.f, l, r };
|
|
4701
4670
|
}
|
|
4702
4671
|
var maxSym = t2[0].s;
|
|
4703
|
-
for (var
|
|
4704
|
-
if (t2[
|
|
4705
|
-
maxSym = t2[
|
|
4672
|
+
for (var i2 = 1; i2 < s; ++i2) {
|
|
4673
|
+
if (t2[i2].s > maxSym)
|
|
4674
|
+
maxSym = t2[i2].s;
|
|
4706
4675
|
}
|
|
4707
4676
|
var tr = new u16(maxSym + 1);
|
|
4708
4677
|
var mbt = ln(t[i1 - 1], tr, 0);
|
|
4709
4678
|
if (mbt > mb) {
|
|
4710
|
-
var
|
|
4679
|
+
var i2 = 0, dt = 0;
|
|
4711
4680
|
var lft = mbt - mb, cst = 1 << lft;
|
|
4712
4681
|
t2.sort(function(a, b) {
|
|
4713
4682
|
return tr[b.s] - tr[a.s] || a.f - b.f;
|
|
4714
4683
|
});
|
|
4715
|
-
for (;
|
|
4716
|
-
var i2_1 = t2[
|
|
4684
|
+
for (; i2 < s; ++i2) {
|
|
4685
|
+
var i2_1 = t2[i2].s;
|
|
4717
4686
|
if (tr[i2_1] > mb) {
|
|
4718
4687
|
dt += cst - (1 << mbt - tr[i2_1]);
|
|
4719
4688
|
tr[i2_1] = mb;
|
|
@@ -4722,14 +4691,14 @@ var hTree = function(d, mb) {
|
|
|
4722
4691
|
}
|
|
4723
4692
|
dt >>= lft;
|
|
4724
4693
|
while (dt > 0) {
|
|
4725
|
-
var i2_2 = t2[
|
|
4694
|
+
var i2_2 = t2[i2].s;
|
|
4726
4695
|
if (tr[i2_2] < mb)
|
|
4727
4696
|
dt -= 1 << mb - tr[i2_2]++ - 1;
|
|
4728
4697
|
else
|
|
4729
|
-
++
|
|
4698
|
+
++i2;
|
|
4730
4699
|
}
|
|
4731
|
-
for (;
|
|
4732
|
-
var i2_3 = t2[
|
|
4700
|
+
for (; i2 >= 0 && dt; --i2) {
|
|
4701
|
+
var i2_3 = t2[i2].s;
|
|
4733
4702
|
if (tr[i2_3] == mb) {
|
|
4734
4703
|
--tr[i2_3];
|
|
4735
4704
|
++dt;
|
|
@@ -4751,8 +4720,8 @@ var lc = function(c) {
|
|
|
4751
4720
|
var w = function(v) {
|
|
4752
4721
|
cl[cli++] = v;
|
|
4753
4722
|
};
|
|
4754
|
-
for (var
|
|
4755
|
-
if (c[
|
|
4723
|
+
for (var i2 = 1; i2 <= s; ++i2) {
|
|
4724
|
+
if (c[i2] == cln && i2 != s)
|
|
4756
4725
|
++cls;
|
|
4757
4726
|
else {
|
|
4758
4727
|
if (!cln && cls > 2) {
|
|
@@ -4772,15 +4741,15 @@ var lc = function(c) {
|
|
|
4772
4741
|
while (cls--)
|
|
4773
4742
|
w(cln);
|
|
4774
4743
|
cls = 1;
|
|
4775
|
-
cln = c[
|
|
4744
|
+
cln = c[i2];
|
|
4776
4745
|
}
|
|
4777
4746
|
}
|
|
4778
4747
|
return { c: cl.subarray(0, cli), n: s };
|
|
4779
4748
|
};
|
|
4780
4749
|
var clen = function(cf, cl) {
|
|
4781
4750
|
var l = 0;
|
|
4782
|
-
for (var
|
|
4783
|
-
l += cf[
|
|
4751
|
+
for (var i2 = 0; i2 < cl.length; ++i2)
|
|
4752
|
+
l += cf[i2] * cl[i2];
|
|
4784
4753
|
return l;
|
|
4785
4754
|
};
|
|
4786
4755
|
var wfblk = function(out, pos, dat) {
|
|
@@ -4790,8 +4759,8 @@ var wfblk = function(out, pos, dat) {
|
|
|
4790
4759
|
out[o + 1] = s >> 8;
|
|
4791
4760
|
out[o + 2] = out[o] ^ 255;
|
|
4792
4761
|
out[o + 3] = out[o + 1] ^ 255;
|
|
4793
|
-
for (var
|
|
4794
|
-
out[o +
|
|
4762
|
+
for (var i2 = 0; i2 < s; ++i2)
|
|
4763
|
+
out[o + i2 + 4] = dat[i2];
|
|
4795
4764
|
return (o + 4 + s) * 8;
|
|
4796
4765
|
};
|
|
4797
4766
|
var wblk = function(dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
|
|
@@ -4802,10 +4771,10 @@ var wblk = function(dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
|
|
|
4802
4771
|
var _c = lc(dlt), lclt = _c.c, nlc = _c.n;
|
|
4803
4772
|
var _d = lc(ddt), lcdt = _d.c, ndc = _d.n;
|
|
4804
4773
|
var lcfreq = new u16(19);
|
|
4805
|
-
for (var
|
|
4806
|
-
++lcfreq[lclt[
|
|
4807
|
-
for (var
|
|
4808
|
-
++lcfreq[lcdt[
|
|
4774
|
+
for (var i2 = 0; i2 < lclt.length; ++i2)
|
|
4775
|
+
++lcfreq[lclt[i2] & 31];
|
|
4776
|
+
for (var i2 = 0; i2 < lcdt.length; ++i2)
|
|
4777
|
+
++lcfreq[lcdt[i2] & 31];
|
|
4809
4778
|
var _e = hTree(lcfreq, 7), lct = _e.t, mlcb = _e.l;
|
|
4810
4779
|
var nlcc = 19;
|
|
4811
4780
|
for (; nlcc > 4 && !lct[clim[nlcc - 1]]; --nlcc)
|
|
@@ -4824,24 +4793,24 @@ var wblk = function(dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
|
|
|
4824
4793
|
wbits(out, p + 5, ndc - 1);
|
|
4825
4794
|
wbits(out, p + 10, nlcc - 4);
|
|
4826
4795
|
p += 14;
|
|
4827
|
-
for (var
|
|
4828
|
-
wbits(out, p + 3 *
|
|
4796
|
+
for (var i2 = 0; i2 < nlcc; ++i2)
|
|
4797
|
+
wbits(out, p + 3 * i2, lct[clim[i2]]);
|
|
4829
4798
|
p += 3 * nlcc;
|
|
4830
4799
|
var lcts = [lclt, lcdt];
|
|
4831
4800
|
for (var it = 0; it < 2; ++it) {
|
|
4832
4801
|
var clct = lcts[it];
|
|
4833
|
-
for (var
|
|
4834
|
-
var len = clct[
|
|
4802
|
+
for (var i2 = 0; i2 < clct.length; ++i2) {
|
|
4803
|
+
var len = clct[i2] & 31;
|
|
4835
4804
|
wbits(out, p, llm[len]), p += lct[len];
|
|
4836
4805
|
if (len > 15)
|
|
4837
|
-
wbits(out, p, clct[
|
|
4806
|
+
wbits(out, p, clct[i2] >> 5 & 127), p += clct[i2] >> 12;
|
|
4838
4807
|
}
|
|
4839
4808
|
}
|
|
4840
4809
|
} else {
|
|
4841
4810
|
lm = flm, ll = flt, dm = fdm, dl = fdt;
|
|
4842
4811
|
}
|
|
4843
|
-
for (var
|
|
4844
|
-
var sym = syms[
|
|
4812
|
+
for (var i2 = 0; i2 < li; ++i2) {
|
|
4813
|
+
var sym = syms[i2];
|
|
4845
4814
|
if (sym > 255) {
|
|
4846
4815
|
var len = sym >> 18 & 31;
|
|
4847
4816
|
wbits16(out, p, lm[len + 257]), p += ll[len + 257];
|
|
@@ -4874,36 +4843,36 @@ var dflt = function(dat, lvl, plvl, pre, post, st) {
|
|
|
4874
4843
|
var msk_1 = (1 << plvl) - 1;
|
|
4875
4844
|
var prev = st.p || new u16(32768), head = st.h || new u16(msk_1 + 1);
|
|
4876
4845
|
var bs1_1 = Math.ceil(plvl / 3), bs2_1 = 2 * bs1_1;
|
|
4877
|
-
var hsh = function(
|
|
4878
|
-
return (dat[
|
|
4846
|
+
var hsh = function(i3) {
|
|
4847
|
+
return (dat[i3] ^ dat[i3 + 1] << bs1_1 ^ dat[i3 + 2] << bs2_1) & msk_1;
|
|
4879
4848
|
};
|
|
4880
4849
|
var syms = new i32(25e3);
|
|
4881
4850
|
var lf = new u16(288), df = new u16(32);
|
|
4882
|
-
var lc_1 = 0, eb = 0,
|
|
4883
|
-
for (;
|
|
4884
|
-
var hv = hsh(
|
|
4885
|
-
var imod =
|
|
4851
|
+
var lc_1 = 0, eb = 0, i2 = st.i || 0, li = 0, wi = st.w || 0, bs = 0;
|
|
4852
|
+
for (; i2 + 2 < s; ++i2) {
|
|
4853
|
+
var hv = hsh(i2);
|
|
4854
|
+
var imod = i2 & 32767, pimod = head[hv];
|
|
4886
4855
|
prev[imod] = pimod;
|
|
4887
4856
|
head[hv] = imod;
|
|
4888
|
-
if (wi <=
|
|
4889
|
-
var rem = s -
|
|
4857
|
+
if (wi <= i2) {
|
|
4858
|
+
var rem = s - i2;
|
|
4890
4859
|
if ((lc_1 > 7e3 || li > 24576) && (rem > 423 || !lst)) {
|
|
4891
|
-
pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs,
|
|
4892
|
-
li = lc_1 = eb = 0, bs =
|
|
4860
|
+
pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs, i2 - bs, pos);
|
|
4861
|
+
li = lc_1 = eb = 0, bs = i2;
|
|
4893
4862
|
for (var j = 0; j < 286; ++j)
|
|
4894
4863
|
lf[j] = 0;
|
|
4895
4864
|
for (var j = 0; j < 30; ++j)
|
|
4896
4865
|
df[j] = 0;
|
|
4897
4866
|
}
|
|
4898
4867
|
var l = 2, d = 0, ch_1 = c, dif = imod - pimod & 32767;
|
|
4899
|
-
if (rem > 2 && hv == hsh(
|
|
4868
|
+
if (rem > 2 && hv == hsh(i2 - dif)) {
|
|
4900
4869
|
var maxn = Math.min(n, rem) - 1;
|
|
4901
|
-
var maxd = Math.min(32767,
|
|
4870
|
+
var maxd = Math.min(32767, i2);
|
|
4902
4871
|
var ml = Math.min(258, rem);
|
|
4903
4872
|
while (dif <= maxd && --ch_1 && imod != pimod) {
|
|
4904
|
-
if (dat[
|
|
4873
|
+
if (dat[i2 + l] == dat[i2 + l - dif]) {
|
|
4905
4874
|
var nl = 0;
|
|
4906
|
-
for (; nl < ml && dat[
|
|
4875
|
+
for (; nl < ml && dat[i2 + nl] == dat[i2 + nl - dif]; ++nl)
|
|
4907
4876
|
;
|
|
4908
4877
|
if (nl > l) {
|
|
4909
4878
|
l = nl, d = dif;
|
|
@@ -4912,7 +4881,7 @@ var dflt = function(dat, lvl, plvl, pre, post, st) {
|
|
|
4912
4881
|
var mmd = Math.min(dif, nl - 2);
|
|
4913
4882
|
var md = 0;
|
|
4914
4883
|
for (var j = 0; j < mmd; ++j) {
|
|
4915
|
-
var ti =
|
|
4884
|
+
var ti = i2 - dif + j & 32767;
|
|
4916
4885
|
var pti = prev[ti];
|
|
4917
4886
|
var cd = ti - pti & 32767;
|
|
4918
4887
|
if (cd > md)
|
|
@@ -4930,32 +4899,32 @@ var dflt = function(dat, lvl, plvl, pre, post, st) {
|
|
|
4930
4899
|
eb += fleb[lin] + fdeb[din];
|
|
4931
4900
|
++lf[257 + lin];
|
|
4932
4901
|
++df[din];
|
|
4933
|
-
wi =
|
|
4902
|
+
wi = i2 + l;
|
|
4934
4903
|
++lc_1;
|
|
4935
4904
|
} else {
|
|
4936
|
-
syms[li++] = dat[
|
|
4937
|
-
++lf[dat[
|
|
4905
|
+
syms[li++] = dat[i2];
|
|
4906
|
+
++lf[dat[i2]];
|
|
4938
4907
|
}
|
|
4939
4908
|
}
|
|
4940
4909
|
}
|
|
4941
|
-
for (
|
|
4942
|
-
syms[li++] = dat[
|
|
4943
|
-
++lf[dat[
|
|
4910
|
+
for (i2 = Math.max(i2, wi); i2 < s; ++i2) {
|
|
4911
|
+
syms[li++] = dat[i2];
|
|
4912
|
+
++lf[dat[i2]];
|
|
4944
4913
|
}
|
|
4945
|
-
pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs,
|
|
4914
|
+
pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs, i2 - bs, pos);
|
|
4946
4915
|
if (!lst) {
|
|
4947
4916
|
st.r = pos & 7 | w[pos / 8 | 0] << 3;
|
|
4948
4917
|
pos -= 7;
|
|
4949
|
-
st.h = head, st.p = prev, st.i =
|
|
4918
|
+
st.h = head, st.p = prev, st.i = i2, st.w = wi;
|
|
4950
4919
|
}
|
|
4951
4920
|
} else {
|
|
4952
|
-
for (var
|
|
4953
|
-
var e =
|
|
4921
|
+
for (var i2 = st.w || 0; i2 < s + lst; i2 += 65535) {
|
|
4922
|
+
var e = i2 + 65535;
|
|
4954
4923
|
if (e >= s) {
|
|
4955
4924
|
w[pos / 8 | 0] = lst;
|
|
4956
4925
|
e = s;
|
|
4957
4926
|
}
|
|
4958
|
-
pos = wfblk(w, pos + 1, dat.subarray(
|
|
4927
|
+
pos = wfblk(w, pos + 1, dat.subarray(i2, e));
|
|
4959
4928
|
}
|
|
4960
4929
|
st.i = s;
|
|
4961
4930
|
}
|
|
@@ -5000,7 +4969,8 @@ var CAPABILITY_VERBS = [
|
|
|
5000
4969
|
"interactive-terminal",
|
|
5001
4970
|
"board-tool-read",
|
|
5002
4971
|
"board-tool-write",
|
|
5003
|
-
"register-push"
|
|
4972
|
+
"register-push",
|
|
4973
|
+
"start-session"
|
|
5004
4974
|
];
|
|
5005
4975
|
function isCapabilityVerb(value) {
|
|
5006
4976
|
return CAPABILITY_VERBS.includes(value);
|
|
@@ -5036,6 +5006,23 @@ var COMPRESSION_THRESHOLD = 4 * 1024;
|
|
|
5036
5006
|
var FRAME_FORMAT_DEFLATE = 1;
|
|
5037
5007
|
var JSON_OPEN_BRACE = 123;
|
|
5038
5008
|
var DEFLATE_HEADER_LENGTH = 5;
|
|
5009
|
+
var UNSUPPORTED_VERB_ERROR_CODE = "unsupported-verb";
|
|
5010
|
+
var UNSUPPORTED_VERB_ERROR_NAME = "UnsupportedVerbError";
|
|
5011
|
+
var UnsupportedVerbError = class extends Error {
|
|
5012
|
+
requestId;
|
|
5013
|
+
verb;
|
|
5014
|
+
constructor(requestId, verb) {
|
|
5015
|
+
super(`capability-request has an unsupported "verb": ${verb}`);
|
|
5016
|
+
this.name = UNSUPPORTED_VERB_ERROR_NAME;
|
|
5017
|
+
this.requestId = requestId;
|
|
5018
|
+
this.verb = verb;
|
|
5019
|
+
}
|
|
5020
|
+
};
|
|
5021
|
+
function isUnsupportedVerbError(error) {
|
|
5022
|
+
if (!(error instanceof Error) || error.name !== UNSUPPORTED_VERB_ERROR_NAME) return false;
|
|
5023
|
+
const candidate = error;
|
|
5024
|
+
return typeof candidate.requestId === "string" && typeof candidate.verb === "string";
|
|
5025
|
+
}
|
|
5039
5026
|
function encodeMessage(message) {
|
|
5040
5027
|
const json = new TextEncoder().encode(JSON.stringify(message));
|
|
5041
5028
|
if (json.length > MAX_DECODED_LENGTH) {
|
|
@@ -5114,8 +5101,9 @@ function validateBridgeMessage(value) {
|
|
|
5114
5101
|
return { type: "heartbeat" };
|
|
5115
5102
|
case "capability-request": {
|
|
5116
5103
|
if (typeof value.requestId !== "string") throw new Error('capability-request missing "requestId"');
|
|
5117
|
-
if (typeof value.verb !== "string"
|
|
5104
|
+
if (typeof value.verb !== "string") throw new Error('capability-request has an invalid "verb"');
|
|
5118
5105
|
if (!isJsonValue(value.payload)) throw new Error('capability-request has a non-JSON "payload"');
|
|
5106
|
+
if (!isCapabilityVerb(value.verb)) throw new UnsupportedVerbError(value.requestId, value.verb);
|
|
5119
5107
|
return { type: "capability-request", requestId: value.requestId, verb: value.verb, payload: value.payload };
|
|
5120
5108
|
}
|
|
5121
5109
|
case "capability-response": {
|
|
@@ -5123,12 +5111,14 @@ function validateBridgeMessage(value) {
|
|
|
5123
5111
|
if (typeof value.ok !== "boolean") throw new Error('capability-response missing "ok"');
|
|
5124
5112
|
if (value.payload !== void 0 && !isJsonValue(value.payload)) throw new Error('capability-response has a non-JSON "payload"');
|
|
5125
5113
|
if (value.error !== void 0 && typeof value.error !== "string") throw new Error('capability-response has a non-string "error"');
|
|
5114
|
+
if (value.code !== void 0 && typeof value.code !== "string") throw new Error('capability-response has a non-string "code"');
|
|
5126
5115
|
return {
|
|
5127
5116
|
type: "capability-response",
|
|
5128
5117
|
requestId: value.requestId,
|
|
5129
5118
|
ok: value.ok,
|
|
5130
5119
|
payload: value.payload,
|
|
5131
|
-
error: value.error
|
|
5120
|
+
error: value.error,
|
|
5121
|
+
code: value.code
|
|
5132
5122
|
};
|
|
5133
5123
|
}
|
|
5134
5124
|
case "event": {
|
|
@@ -5163,20 +5153,20 @@ function unwrapSessionFrame(frame) {
|
|
|
5163
5153
|
// src/wire/base64url.ts
|
|
5164
5154
|
var ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
5165
5155
|
var DECODE_MAP = {};
|
|
5166
|
-
for (let
|
|
5156
|
+
for (let i2 = 0; i2 < ALPHABET.length; i2++) DECODE_MAP[ALPHABET[i2]] = i2;
|
|
5167
5157
|
function base64UrlEncode(bytes) {
|
|
5168
5158
|
let result = "";
|
|
5169
|
-
let
|
|
5170
|
-
for (;
|
|
5171
|
-
const chunk = bytes[
|
|
5159
|
+
let i2 = 0;
|
|
5160
|
+
for (; i2 + 2 < bytes.length; i2 += 3) {
|
|
5161
|
+
const chunk = bytes[i2] << 16 | bytes[i2 + 1] << 8 | bytes[i2 + 2];
|
|
5172
5162
|
result += ALPHABET[chunk >> 18 & 63] + ALPHABET[chunk >> 12 & 63] + ALPHABET[chunk >> 6 & 63] + ALPHABET[chunk & 63];
|
|
5173
5163
|
}
|
|
5174
|
-
const remaining = bytes.length -
|
|
5164
|
+
const remaining = bytes.length - i2;
|
|
5175
5165
|
if (remaining === 1) {
|
|
5176
|
-
const chunk = bytes[
|
|
5166
|
+
const chunk = bytes[i2] << 16;
|
|
5177
5167
|
result += ALPHABET[chunk >> 18 & 63] + ALPHABET[chunk >> 12 & 63];
|
|
5178
5168
|
} else if (remaining === 2) {
|
|
5179
|
-
const chunk = bytes[
|
|
5169
|
+
const chunk = bytes[i2] << 16 | bytes[i2 + 1] << 8;
|
|
5180
5170
|
result += ALPHABET[chunk >> 18 & 63] + ALPHABET[chunk >> 12 & 63] + ALPHABET[chunk >> 6 & 63];
|
|
5181
5171
|
}
|
|
5182
5172
|
return result;
|
|
@@ -5233,7 +5223,7 @@ function sealPushEnvelope(pushKey, recipientStaticPublicKey, plaintext) {
|
|
|
5233
5223
|
const nonce = randomBytes4(XCHACHA_NONCE_LENGTH);
|
|
5234
5224
|
const encodedPlaintext = new TextEncoder().encode(JSON.stringify(plaintext));
|
|
5235
5225
|
const ciphertext = xaeadEncrypt(pushKey, nonce, recipientStaticPublicKey, encodedPlaintext);
|
|
5236
|
-
return base64UrlEncode(
|
|
5226
|
+
return base64UrlEncode(concatBytes3(nonce, ciphertext));
|
|
5237
5227
|
}
|
|
5238
5228
|
function openPushEnvelope(pushKey, recipientStaticPublicKey, blob) {
|
|
5239
5229
|
const bytes = base64UrlDecode(blob);
|
|
@@ -5756,6 +5746,23 @@ function parseMoveTaskRequestPayload(payload) {
|
|
|
5756
5746
|
projectId: payload.projectId
|
|
5757
5747
|
};
|
|
5758
5748
|
}
|
|
5749
|
+
function parseStartSessionRequestPayload(payload) {
|
|
5750
|
+
if (!isRecord(payload)) throw new Error("start-session payload must be an object");
|
|
5751
|
+
if (typeof payload.taskId !== "string") throw new Error('start-session payload missing "taskId"');
|
|
5752
|
+
if (typeof payload.projectId !== "string") throw new Error('start-session payload missing "projectId"');
|
|
5753
|
+
return {
|
|
5754
|
+
taskId: payload.taskId,
|
|
5755
|
+
projectId: payload.projectId
|
|
5756
|
+
};
|
|
5757
|
+
}
|
|
5758
|
+
function parseStartSessionResponsePayload(payload) {
|
|
5759
|
+
if (!isRecord(payload)) throw new Error("start-session response must be an object");
|
|
5760
|
+
if (typeof payload.ok !== "boolean") throw new Error('start-session response missing "ok"');
|
|
5761
|
+
if (payload.outcome !== "starting" && payload.outcome !== "live") {
|
|
5762
|
+
throw new Error('start-session response has an invalid "outcome"');
|
|
5763
|
+
}
|
|
5764
|
+
return { ok: payload.ok, outcome: payload.outcome };
|
|
5765
|
+
}
|
|
5759
5766
|
function parseAnswerPermissionPromptRequestPayload(payload) {
|
|
5760
5767
|
if (!isRecord(payload)) throw new Error("answer-permission-prompt payload must be an object");
|
|
5761
5768
|
if (typeof payload.sessionId !== "string") throw new Error('answer-permission-prompt payload missing "sessionId"');
|
|
@@ -5851,6 +5858,8 @@ function parseCapabilityRequestPayload(verb, payload) {
|
|
|
5851
5858
|
return parseBoardToolRequestPayload(payload);
|
|
5852
5859
|
case "register-push":
|
|
5853
5860
|
return parseRegisterPushRequestPayload(payload);
|
|
5861
|
+
case "start-session":
|
|
5862
|
+
return parseStartSessionRequestPayload(payload);
|
|
5854
5863
|
default: {
|
|
5855
5864
|
const exhaustiveCheck = verb;
|
|
5856
5865
|
throw new Error(`Unknown capability verb: ${String(exhaustiveCheck)}`);
|
|
@@ -5916,7 +5925,7 @@ function encodePairingQrPayload(payload) {
|
|
|
5916
5925
|
}
|
|
5917
5926
|
const expiresAtBytes = new TextEncoder().encode(payload.expiresAt);
|
|
5918
5927
|
const protocolVersionBytes = new TextEncoder().encode(payload.protocolVersion);
|
|
5919
|
-
const body =
|
|
5928
|
+
const body = concatBytes3(
|
|
5920
5929
|
Uint8Array.of(PAYLOAD_VERSION),
|
|
5921
5930
|
payload.desktopStaticPublicKey,
|
|
5922
5931
|
payload.pairingToken,
|
|
@@ -5996,12 +6005,12 @@ function openPairingConfirm(initiatorToResponder, frame) {
|
|
|
5996
6005
|
function uint32LengthPrefixed(bytes) {
|
|
5997
6006
|
const prefix = new Uint8Array(4);
|
|
5998
6007
|
new DataView(prefix.buffer).setUint32(0, bytes.length, false);
|
|
5999
|
-
return
|
|
6008
|
+
return concatBytes3(prefix, bytes);
|
|
6000
6009
|
}
|
|
6001
6010
|
function encodeRosterEntryForSigning(entry) {
|
|
6002
6011
|
const encoder = new TextEncoder();
|
|
6003
6012
|
const sortedCapabilities = entry.capabilities.slice().sort().join(",");
|
|
6004
|
-
return
|
|
6013
|
+
return concatBytes3(
|
|
6005
6014
|
uint32LengthPrefixed(encoder.encode(entry.deviceId)),
|
|
6006
6015
|
uint32LengthPrefixed(entry.staticPublicKey),
|
|
6007
6016
|
uint32LengthPrefixed(encoder.encode(entry.displayName)),
|
|
@@ -6192,6 +6201,8 @@ function isBridgeEvent(value) {
|
|
|
6192
6201
|
PUSH_ENVELOPE_MAX_FUTURE_SKEW_MS,
|
|
6193
6202
|
SecretstreamState,
|
|
6194
6203
|
SessionFrameKind,
|
|
6204
|
+
UNSUPPORTED_VERB_ERROR_CODE,
|
|
6205
|
+
UnsupportedVerbError,
|
|
6195
6206
|
X25519_KEY_LENGTH,
|
|
6196
6207
|
bytesToHex,
|
|
6197
6208
|
capabilitySetFromArray,
|
|
@@ -6228,6 +6239,7 @@ function isBridgeEvent(value) {
|
|
|
6228
6239
|
isRosterEntryExpired,
|
|
6229
6240
|
isSecureRelayAddress,
|
|
6230
6241
|
isTodoRole,
|
|
6242
|
+
isUnsupportedVerbError,
|
|
6231
6243
|
openPairingConfirm,
|
|
6232
6244
|
openPushEnvelope,
|
|
6233
6245
|
parseActivityEventPayload,
|
|
@@ -6245,6 +6257,7 @@ function isBridgeEvent(value) {
|
|
|
6245
6257
|
parseSessionEventWire,
|
|
6246
6258
|
parseSessionSummaryWire,
|
|
6247
6259
|
parseSessionUsageWire,
|
|
6260
|
+
parseStartSessionResponsePayload,
|
|
6248
6261
|
parseTerminalDimensionsWire,
|
|
6249
6262
|
parseTranscriptEntriesWire,
|
|
6250
6263
|
parseTranscriptEventPayload,
|