@dvmkit/dvm-cli 0.0.0 → 0.1.4
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/LICENSE +67 -0
- package/NOTICE +2 -0
- package/README.md +28 -3
- package/dist/_esm-M2HUAA3B.js +883 -0
- package/dist/_esm-NRGECLJ7.js +35 -0
- package/dist/accounts-S747UW3A.js +73 -0
- package/dist/ccip-XZ5F5J6R.js +18 -0
- package/dist/chains-PINFXYUE.js +1460 -0
- package/dist/channel-store-GOV7TEDN.js +62 -0
- package/dist/chunk-3PPOU55E.js +312 -0
- package/dist/chunk-3TBULV3F.js +551 -0
- package/dist/chunk-4AQ4G7L5.js +1447 -0
- package/dist/chunk-5BUV73FO.js +2351 -0
- package/dist/chunk-6AJ7H2VX.js +4387 -0
- package/dist/chunk-6UGZAFIR.js +1485 -0
- package/dist/chunk-7UPZZGC5.js +30063 -0
- package/dist/chunk-ACNNU5RY.js +5485 -0
- package/dist/chunk-BZNZWRBB.js +189 -0
- package/dist/chunk-EY5K4BM3.js +1780 -0
- package/dist/chunk-G3RLVJF5.js +19 -0
- package/dist/chunk-GIDSTROG.js +16614 -0
- package/dist/chunk-HPRIQEJS.js +258 -0
- package/dist/chunk-IC6OXFYW.js +6484 -0
- package/dist/chunk-IYHTITNZ.js +45 -0
- package/dist/chunk-KC4UPK44.js +113 -0
- package/dist/chunk-MMJR6N3J.js +2426 -0
- package/dist/chunk-MTBCEXJ6.js +4602 -0
- package/dist/chunk-ONQMRWY3.js +83 -0
- package/dist/chunk-PHZHPRQE.js +1181 -0
- package/dist/chunk-RA24J4QM.js +3717 -0
- package/dist/chunk-RWBYCM47.js +339 -0
- package/dist/chunk-SLDVIXCS.js +21369 -0
- package/dist/chunk-TO47XRK7.js +24890 -0
- package/dist/chunk-U5TNSUYW.js +1757 -0
- package/dist/chunk-UU6QMSTQ.js +171 -0
- package/dist/chunk-UXQCP26E.js +2691 -0
- package/dist/chunk-XGABM4H5.js +3339 -0
- package/dist/chunk-YZUBKHHA.js +15436 -0
- package/dist/chunk-ZELLQI7F.js +248 -0
- package/dist/chunk-ZGXCK43G.js +9868 -0
- package/dist/client-D3XFLWOK.js +52 -0
- package/dist/dist-7MMV3EZA.js +46 -0
- package/dist/dvm.js +33650 -0
- package/dist/lifecycle-5RLM5JMN.js +29 -0
- package/dist/mine.wasm-DNBKYCTQ.js +12 -0
- package/dist/pocket-refill-GDYSTECR.js +133 -0
- package/dist/secp256k1-4YWQEMGF.js +19 -0
- package/dist/tempo-lifecycle-DHZPQTSB.js +31 -0
- package/dist/tempo-wallet-5FEOI4CF.js +35 -0
- package/dist/wallet-G3VOUCL4.js +30 -0
- package/dist/x402-VNFA3DBY.js +91 -0
- package/package.json +71 -6
- package/skills/dvm/SKILL.md +71 -0
|
@@ -0,0 +1,3339 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire as __dvmCreateRequire } from "node:module";
|
|
3
|
+
const require = __dvmCreateRequire(import.meta.url);
|
|
4
|
+
import {
|
|
5
|
+
CONFIG_AUDIT_FILE,
|
|
6
|
+
CONFIG_BACKUP_FILE,
|
|
7
|
+
CONFIG_FILE,
|
|
8
|
+
CONFIG_PINNED_FILE,
|
|
9
|
+
DvmError,
|
|
10
|
+
ensureConfigDir,
|
|
11
|
+
require_proper_lockfile
|
|
12
|
+
} from "./chunk-U5TNSUYW.js";
|
|
13
|
+
import {
|
|
14
|
+
__toESM
|
|
15
|
+
} from "./chunk-IYHTITNZ.js";
|
|
16
|
+
|
|
17
|
+
// src/lib/config-mutation.ts
|
|
18
|
+
import {
|
|
19
|
+
appendFileSync,
|
|
20
|
+
chmodSync,
|
|
21
|
+
existsSync,
|
|
22
|
+
readFileSync,
|
|
23
|
+
renameSync,
|
|
24
|
+
statSync,
|
|
25
|
+
unlinkSync,
|
|
26
|
+
writeFileSync
|
|
27
|
+
} from "fs";
|
|
28
|
+
var PROTECTED_CONFIG_FIELDS = ["nwcUri", "defaultIdentity", "tempo", "x402"];
|
|
29
|
+
var ProtectedFieldAuthorization = class {
|
|
30
|
+
/** Targets this authorization covers. A field also covers its own leaves. */
|
|
31
|
+
targets;
|
|
32
|
+
/** Prefer {@link authorizeProtectedRemoval}; this exists for it to call. */
|
|
33
|
+
constructor(targets) {
|
|
34
|
+
this.targets = [...targets];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* True when `target` is one this authorization names. Naming a field covers
|
|
38
|
+
* the leaves under it; naming a leaf covers only that leaf, so a command
|
|
39
|
+
* entitled to clear `tempo.apiKey` still cannot take the whole `tempo` block.
|
|
40
|
+
*/
|
|
41
|
+
covers(target) {
|
|
42
|
+
if (this.targets.includes(target)) return true;
|
|
43
|
+
const field = target.split(".")[0];
|
|
44
|
+
return target !== field && this.targets.includes(field);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function authorizeProtectedRemoval(...targets) {
|
|
48
|
+
return new ProtectedFieldAuthorization(targets);
|
|
49
|
+
}
|
|
50
|
+
function planConfigReset(config) {
|
|
51
|
+
const present = Object.keys(config);
|
|
52
|
+
const resettable = resettableConfigFields();
|
|
53
|
+
const clearable = new Set(resettable);
|
|
54
|
+
return {
|
|
55
|
+
clear: resettable.filter((field) => present.includes(field)),
|
|
56
|
+
preserved: present.filter((field) => !clearable.has(field))
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function applyConfigPatch(base, patch) {
|
|
60
|
+
if (!isPlainObject(patch))
|
|
61
|
+
throw patchShapeError("A change must be an object with `set` and/or `unset`.");
|
|
62
|
+
if (patch.set !== void 0 && !isPlainObject(patch.set)) {
|
|
63
|
+
throw patchShapeError("A change's `set` must be an object of fields to write.");
|
|
64
|
+
}
|
|
65
|
+
if (patch.unset !== void 0 && !Array.isArray(patch.unset)) {
|
|
66
|
+
throw patchShapeError("A change's `unset` must be an array of field names.");
|
|
67
|
+
}
|
|
68
|
+
const set = patch.set ?? {};
|
|
69
|
+
const unset = patch.unset ?? [];
|
|
70
|
+
for (const [field, value] of Object.entries(set)) {
|
|
71
|
+
if (value !== void 0) continue;
|
|
72
|
+
throw new DvmError(
|
|
73
|
+
"config_patch_invalid",
|
|
74
|
+
`Config field '${field}' was declared with no value.`,
|
|
75
|
+
"Give the field a value, or name it in the change's `unset` list to remove it. An undefined value is how a field gets dropped without anyone deciding to drop it."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
for (const field of unset) {
|
|
79
|
+
if (typeof field !== "string" || field.length === 0) {
|
|
80
|
+
throw patchShapeError("Every entry in a change's `unset` must be a field name.");
|
|
81
|
+
}
|
|
82
|
+
if (!Object.hasOwn(set, field)) continue;
|
|
83
|
+
throw new DvmError(
|
|
84
|
+
"config_patch_invalid",
|
|
85
|
+
`Config field '${field}' is both written and removed by the same change.`,
|
|
86
|
+
"Declare each field once: in `set` with its new value, or in `unset` to remove it."
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const merged = { ...base, ...set };
|
|
90
|
+
const removing = new Set(unset);
|
|
91
|
+
return Object.fromEntries(
|
|
92
|
+
Object.entries(merged).filter(([field]) => !removing.has(field))
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
function deepFreezeConfig(config) {
|
|
96
|
+
freezeDeep(config);
|
|
97
|
+
return config;
|
|
98
|
+
}
|
|
99
|
+
function protectedRemovals(before, after) {
|
|
100
|
+
const previous = before;
|
|
101
|
+
const next = after;
|
|
102
|
+
const removed = [];
|
|
103
|
+
for (const field of PROTECTED_CONFIG_FIELDS) {
|
|
104
|
+
const held = previous[field];
|
|
105
|
+
if (!isPresentValue(held)) continue;
|
|
106
|
+
const kept = next[field];
|
|
107
|
+
if (!isPresentValue(kept)) {
|
|
108
|
+
removed.push(field);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const spec = PROTECTED_FIELDS[field];
|
|
112
|
+
if (spec.credentialLeaves.length === 0) continue;
|
|
113
|
+
if (!isPlainObject(held)) continue;
|
|
114
|
+
if (!isPlainObject(kept)) {
|
|
115
|
+
removed.push(field);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
for (const leaf of spec.credentialLeaves) {
|
|
119
|
+
if (!isPresentValue(held[leaf]) || isPresentValue(kept[leaf])) continue;
|
|
120
|
+
removed.push(`${field}.${leaf}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return removed;
|
|
124
|
+
}
|
|
125
|
+
function supersededProtectedSecrets(before, after) {
|
|
126
|
+
const previous = before;
|
|
127
|
+
const next = after;
|
|
128
|
+
const superseded = [];
|
|
129
|
+
for (const field of PROTECTED_CONFIG_FIELDS) {
|
|
130
|
+
const spec = PROTECTED_FIELDS[field];
|
|
131
|
+
if (!spec.secret) continue;
|
|
132
|
+
const held = previous[field];
|
|
133
|
+
const kept = next[field];
|
|
134
|
+
if (spec.credentialLeaves.length === 0) {
|
|
135
|
+
if (isPresentValue(held) && isPresentValue(kept) && held !== kept) superseded.push(field);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (!isPlainObject(held) || !isPlainObject(kept)) continue;
|
|
139
|
+
for (const leaf of spec.credentialLeaves) {
|
|
140
|
+
const was = held[leaf];
|
|
141
|
+
const now = kept[leaf];
|
|
142
|
+
if (!isPresentValue(was) || !isPresentValue(now) || was === now) continue;
|
|
143
|
+
superseded.push(`${field}.${leaf}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return superseded;
|
|
147
|
+
}
|
|
148
|
+
function changedConfigPaths(before, after) {
|
|
149
|
+
const found = /* @__PURE__ */ new Set();
|
|
150
|
+
collectChangedPaths(
|
|
151
|
+
before,
|
|
152
|
+
after,
|
|
153
|
+
CONFIG_PATH_SHAPE,
|
|
154
|
+
"",
|
|
155
|
+
found
|
|
156
|
+
);
|
|
157
|
+
return [...found].sort();
|
|
158
|
+
}
|
|
159
|
+
function assertValidConfig(config, fields) {
|
|
160
|
+
if (!isPlainObject(config)) {
|
|
161
|
+
throw new DvmError(
|
|
162
|
+
"config_invalid",
|
|
163
|
+
"The caller configuration is not a JSON object.",
|
|
164
|
+
`Expected an object at ${CONFIG_FILE}.${rollbackSuffix()}`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const record = config;
|
|
168
|
+
const scope = fields ? new Set(fields) : null;
|
|
169
|
+
for (const [field, kind] of Object.entries(CONFIG_FIELD_TYPES)) {
|
|
170
|
+
if (scope && !scope.has(field)) continue;
|
|
171
|
+
const value = record[field];
|
|
172
|
+
if (value === void 0) continue;
|
|
173
|
+
if (!matchesKind(value, kind)) throw invalidFieldError(field, kind);
|
|
174
|
+
}
|
|
175
|
+
for (const [path, kind] of Object.entries(CONFIG_LEAF_TYPES)) {
|
|
176
|
+
const [field, leaf] = path.split(".");
|
|
177
|
+
if (scope && !scope.has(field)) continue;
|
|
178
|
+
const owner = record[field];
|
|
179
|
+
if (!isPlainObject(owner)) continue;
|
|
180
|
+
const value = owner[leaf];
|
|
181
|
+
if (value === void 0 || !matchesKind(value, kind)) throw invalidFieldError(path, kind);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function readRawConfig() {
|
|
185
|
+
if (!existsSync(CONFIG_FILE)) return null;
|
|
186
|
+
try {
|
|
187
|
+
return JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
|
|
188
|
+
} catch {
|
|
189
|
+
throw new DvmError(
|
|
190
|
+
"config_corrupt",
|
|
191
|
+
"Config file is corrupt.",
|
|
192
|
+
// Not "delete it": those bytes are the only copy on this machine of the
|
|
193
|
+
// NWC connection string and both private keys, however badly they parse.
|
|
194
|
+
`Run 'dvm config rollback' to restore a stored copy, or move ${CONFIG_FILE} aside yourself and run 'dvm init'.${rollbackSuffix()}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function writeConfigFile(config) {
|
|
199
|
+
writeJsonAtomic(CONFIG_FILE, config);
|
|
200
|
+
}
|
|
201
|
+
var CONFIG_SNAPSHOT_SLOTS = ["backup", "pinned"];
|
|
202
|
+
function configSnapshotPath(slot) {
|
|
203
|
+
return slot === "pinned" ? CONFIG_PINNED_FILE : CONFIG_BACKUP_FILE;
|
|
204
|
+
}
|
|
205
|
+
function describeConfigBackup(slot = "backup") {
|
|
206
|
+
const path = configSnapshotPath(slot);
|
|
207
|
+
try {
|
|
208
|
+
if (!existsSync(path)) return null;
|
|
209
|
+
const stat = statSync(path);
|
|
210
|
+
const posix = process.platform !== "win32";
|
|
211
|
+
const mode = stat.mode & 511;
|
|
212
|
+
return {
|
|
213
|
+
path,
|
|
214
|
+
slot,
|
|
215
|
+
bytes: stat.size,
|
|
216
|
+
modifiedAt: stat.mtime.toISOString(),
|
|
217
|
+
modeOctal: posix ? toOctal(mode) : null,
|
|
218
|
+
ownerOnly: !posix || (mode & 63) === 0
|
|
219
|
+
};
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function readConfigBackup(slot = "backup") {
|
|
225
|
+
const path = configSnapshotPath(slot);
|
|
226
|
+
if (!existsSync(path)) return null;
|
|
227
|
+
try {
|
|
228
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
229
|
+
return isPlainObject(parsed) ? parsed : null;
|
|
230
|
+
} catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function retainConfigBackup(slot = "backup") {
|
|
235
|
+
if (!existsSync(CONFIG_FILE)) return false;
|
|
236
|
+
ensureConfigDir();
|
|
237
|
+
const path = configSnapshotPath(slot);
|
|
238
|
+
const tmp = `${path}.tmp`;
|
|
239
|
+
writeFileSync(tmp, readFileSync(CONFIG_FILE), { mode: 384 });
|
|
240
|
+
chmodSync(tmp, 384);
|
|
241
|
+
commitTmp(tmp, path);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
function writeConfigBackup(config, slot = "backup") {
|
|
245
|
+
writeJsonAtomic(configSnapshotPath(slot), config);
|
|
246
|
+
}
|
|
247
|
+
function removeConfigBackup(slot = "backup") {
|
|
248
|
+
const path = configSnapshotPath(slot);
|
|
249
|
+
if (!existsSync(path)) return false;
|
|
250
|
+
unlinkSync(path);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
function protectedTargetValue(config, target) {
|
|
254
|
+
const [field, named] = target.split(".");
|
|
255
|
+
const held = config[field];
|
|
256
|
+
const leaves = PROTECTED_FIELDS[field].credentialLeaves;
|
|
257
|
+
const leaf = named ?? (leaves.length > 0 ? leaves[0] : void 0);
|
|
258
|
+
if (leaf === void 0) return held;
|
|
259
|
+
return isPlainObject(held) ? held[leaf] : void 0;
|
|
260
|
+
}
|
|
261
|
+
function protectedValuesMatch(target, left, right) {
|
|
262
|
+
const normalized = comparableProtectedValue(target, left);
|
|
263
|
+
return normalized !== void 0 && normalized === comparableProtectedValue(target, right);
|
|
264
|
+
}
|
|
265
|
+
function isSecretProtectedTarget(target) {
|
|
266
|
+
return PROTECTED_FIELDS[target.split(".")[0]].secret;
|
|
267
|
+
}
|
|
268
|
+
function withoutProtectedTargets(config, targets) {
|
|
269
|
+
const dropping = /* @__PURE__ */ new Set();
|
|
270
|
+
for (const target of targets) {
|
|
271
|
+
const field = target.split(".")[0];
|
|
272
|
+
dropping.add(field);
|
|
273
|
+
for (const alias of PROTECTED_FIELDS[field].aliases) dropping.add(alias);
|
|
274
|
+
}
|
|
275
|
+
return Object.fromEntries(
|
|
276
|
+
Object.entries(config).filter(([field]) => !dropping.has(field))
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
function appendConfigAudit(record) {
|
|
280
|
+
try {
|
|
281
|
+
ensureConfigDir();
|
|
282
|
+
appendFileSync(CONFIG_AUDIT_FILE, JSON.stringify(record) + "\n", { mode: 384 });
|
|
283
|
+
chmodSync(CONFIG_AUDIT_FILE, 384);
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function protectedRemovalError(targets, reason) {
|
|
288
|
+
const named = targets.join(", ");
|
|
289
|
+
const one = targets.length === 1;
|
|
290
|
+
return new DvmError(
|
|
291
|
+
"config_protected_field",
|
|
292
|
+
`The '${reason}' change would remove ${named} from the local configuration.`,
|
|
293
|
+
`Nothing was written. ${named} ${one ? "is a credential or identity pointer" : "are credentials or identity pointers"} nothing else on this machine records, so only a command that names ${one ? "it" : "them"} explicitly may remove ${one ? "it" : "them"} \u2014 'dvm wallet disconnect', 'dvm wallet tempo-disconnect' and 'dvm wallet x402-disconnect' are those commands.`,
|
|
294
|
+
{ fields: targets }
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
var PROTECTED_FIELDS = {
|
|
298
|
+
nwcUri: { aliases: [], credentialLeaves: [], secret: true, hexCredential: false },
|
|
299
|
+
// A pointer at a key, not a key: worth protecting from removal, and never
|
|
300
|
+
// worth stripping out of a stored copy — that is recovery lost for nothing.
|
|
301
|
+
defaultIdentity: { aliases: [], credentialLeaves: [], secret: false, hexCredential: false },
|
|
302
|
+
// `mpp` is the pre-an internal review name; a caller who has not run a mutating command
|
|
303
|
+
// since the rename still has their private key under it.
|
|
304
|
+
tempo: { aliases: ["mpp"], credentialLeaves: ["apiKey"], secret: true, hexCredential: true },
|
|
305
|
+
x402: { aliases: [], credentialLeaves: ["privateKey"], secret: true, hexCredential: true }
|
|
306
|
+
};
|
|
307
|
+
var CONFIG_PATH_SHAPE = {
|
|
308
|
+
float: { statedBudget: {}, connectedAt: {}, alias: {}, methods: {} },
|
|
309
|
+
x402: { privateKey: {}, network: {} },
|
|
310
|
+
tempo: { method: {}, accountId: {}, apiKey: {} },
|
|
311
|
+
credit: {
|
|
312
|
+
targetJobs: {},
|
|
313
|
+
posture: {},
|
|
314
|
+
tier0Cap: {},
|
|
315
|
+
tier1Cap: {},
|
|
316
|
+
pocketTarget: {},
|
|
317
|
+
fundingRail: {},
|
|
318
|
+
perDvm: { "*": { targetJobs: {}, posture: {}, fundingRail: {}, dvmId: {}, builderPubkey: {} } },
|
|
319
|
+
trustOverrides: { "*": {} }
|
|
320
|
+
},
|
|
321
|
+
feedback: { posture: {} }
|
|
322
|
+
};
|
|
323
|
+
var CONFIG_FIELD_TYPES = {
|
|
324
|
+
nwcUri: "string",
|
|
325
|
+
float: "object",
|
|
326
|
+
defaultBudget: "string",
|
|
327
|
+
autoPayThreshold: "string",
|
|
328
|
+
defaultMaxPayments: "number",
|
|
329
|
+
cashuMints: "string[]",
|
|
330
|
+
x402: "object",
|
|
331
|
+
tempo: "object",
|
|
332
|
+
defaultRail: "string",
|
|
333
|
+
defaultIdentity: "string",
|
|
334
|
+
credit: "object",
|
|
335
|
+
feedback: "object"
|
|
336
|
+
};
|
|
337
|
+
var CONFIG_LEAF_TYPES = {
|
|
338
|
+
"float.connectedAt": "string",
|
|
339
|
+
"x402.privateKey": "string",
|
|
340
|
+
"tempo.method": "string"
|
|
341
|
+
};
|
|
342
|
+
function collectChangedPaths(before, after, shape, prefix, found) {
|
|
343
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
344
|
+
const held = before[key];
|
|
345
|
+
const kept = after[key];
|
|
346
|
+
if (deepEqual(held, kept)) continue;
|
|
347
|
+
const known = shape !== void 0 && Object.hasOwn(shape, key);
|
|
348
|
+
const wildcard = !known && shape !== void 0 && Object.hasOwn(shape, "*");
|
|
349
|
+
const child = known ? shape[key] : wildcard ? shape["*"] : void 0;
|
|
350
|
+
const path = prefix ? `${prefix}.${wildcard ? "*" : key}` : key;
|
|
351
|
+
const descendable = child !== void 0 && Object.keys(child).length > 0 && (held === void 0 || isPlainObject(held)) && (kept === void 0 || isPlainObject(kept));
|
|
352
|
+
if (descendable) {
|
|
353
|
+
collectChangedPaths(
|
|
354
|
+
isPlainObject(held) ? held : {},
|
|
355
|
+
isPlainObject(kept) ? kept : {},
|
|
356
|
+
child,
|
|
357
|
+
path,
|
|
358
|
+
found
|
|
359
|
+
);
|
|
360
|
+
} else {
|
|
361
|
+
found.add(path);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function resettableConfigFields() {
|
|
366
|
+
const guarded = new Set(PROTECTED_CONFIG_FIELDS);
|
|
367
|
+
return Object.keys(CONFIG_FIELD_TYPES).filter((field) => !guarded.has(field));
|
|
368
|
+
}
|
|
369
|
+
function invalidFieldError(path, kind) {
|
|
370
|
+
return new DvmError(
|
|
371
|
+
"config_invalid",
|
|
372
|
+
`Config field '${path}' must be ${describeKind(kind)}.`,
|
|
373
|
+
`Nothing was written. Correct the field in ${CONFIG_FILE}, or run 'dvm doctor' for the full picture.${rollbackSuffix()}`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
function describeKind(kind) {
|
|
377
|
+
switch (kind) {
|
|
378
|
+
case "string":
|
|
379
|
+
return "a string";
|
|
380
|
+
case "number":
|
|
381
|
+
return "a number";
|
|
382
|
+
case "object":
|
|
383
|
+
return "an object";
|
|
384
|
+
case "string[]":
|
|
385
|
+
return "an array of strings";
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
function matchesKind(value, kind) {
|
|
389
|
+
switch (kind) {
|
|
390
|
+
case "string":
|
|
391
|
+
return typeof value === "string";
|
|
392
|
+
case "number":
|
|
393
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
394
|
+
case "object":
|
|
395
|
+
return isPlainObject(value);
|
|
396
|
+
case "string[]":
|
|
397
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function rollbackSuffix() {
|
|
401
|
+
const stored = describeConfigBackup() ?? describeConfigBackup("pinned");
|
|
402
|
+
return stored === null ? "" : ` A stored copy of the previous configuration is at ${stored.path}.`;
|
|
403
|
+
}
|
|
404
|
+
function writeJsonAtomic(path, value) {
|
|
405
|
+
ensureConfigDir();
|
|
406
|
+
const tmp = `${path}.tmp`;
|
|
407
|
+
writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 384 });
|
|
408
|
+
chmodSync(tmp, 384);
|
|
409
|
+
commitTmp(tmp, path);
|
|
410
|
+
}
|
|
411
|
+
function commitTmp(tmp, path) {
|
|
412
|
+
try {
|
|
413
|
+
renameSync(tmp, path);
|
|
414
|
+
} catch (err) {
|
|
415
|
+
try {
|
|
416
|
+
unlinkSync(tmp);
|
|
417
|
+
} catch {
|
|
418
|
+
}
|
|
419
|
+
throw err;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
function deepEqual(a, b) {
|
|
423
|
+
if (a === b) return true;
|
|
424
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
425
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
|
426
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
427
|
+
}
|
|
428
|
+
if (!isPlainObject(a) || !isPlainObject(b)) return false;
|
|
429
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
430
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
431
|
+
}
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
function isPresentValue(value) {
|
|
435
|
+
if (value === void 0 || value === null) return false;
|
|
436
|
+
return typeof value !== "string" || value.trim().length > 0;
|
|
437
|
+
}
|
|
438
|
+
function comparableProtectedValue(target, value) {
|
|
439
|
+
if (typeof value !== "string") return void 0;
|
|
440
|
+
const trimmed = value.trim();
|
|
441
|
+
if (trimmed.length === 0) return void 0;
|
|
442
|
+
const field = target.split(".")[0];
|
|
443
|
+
return PROTECTED_FIELDS[field].hexCredential ? trimmed.toLowerCase() : trimmed;
|
|
444
|
+
}
|
|
445
|
+
function patchShapeError(message) {
|
|
446
|
+
return new DvmError(
|
|
447
|
+
"config_patch_invalid",
|
|
448
|
+
message,
|
|
449
|
+
"A change declares `set` (fields to write) and `unset` (field names to remove). Anything else cannot be applied, and nothing was written."
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
function freezeDeep(value) {
|
|
453
|
+
if (typeof value !== "object" || value === null || Object.isFrozen(value)) return;
|
|
454
|
+
Object.freeze(value);
|
|
455
|
+
for (const nested of Object.values(value)) freezeDeep(nested);
|
|
456
|
+
}
|
|
457
|
+
function isPlainObject(value) {
|
|
458
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
459
|
+
}
|
|
460
|
+
function toOctal(mode) {
|
|
461
|
+
return "0" + mode.toString(8).padStart(3, "0");
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/lib/identity.ts
|
|
465
|
+
var import_proper_lockfile = __toESM(require_proper_lockfile(), 1);
|
|
466
|
+
var CREDIT_POSTURES = ["suggest", "auto", "off"];
|
|
467
|
+
function isCreditPosture(value) {
|
|
468
|
+
return typeof value === "string" && CREDIT_POSTURES.includes(value);
|
|
469
|
+
}
|
|
470
|
+
var FEEDBACK_POSTURES = ["ask", "auto", "off"];
|
|
471
|
+
function isFeedbackPosture(value) {
|
|
472
|
+
return typeof value === "string" && FEEDBACK_POSTURES.includes(value);
|
|
473
|
+
}
|
|
474
|
+
function migrateCreditConfig(config) {
|
|
475
|
+
const credit = config.credit;
|
|
476
|
+
if (!credit) return config;
|
|
477
|
+
const migrateLayer = (layer) => {
|
|
478
|
+
const railed = migrateFundingRail(layer);
|
|
479
|
+
if (!("disabled" in railed)) return railed;
|
|
480
|
+
const { disabled, ...rest } = railed;
|
|
481
|
+
const migrated = { ...rest };
|
|
482
|
+
if (migrated.posture === void 0 && disabled === true) migrated.posture = "off";
|
|
483
|
+
return migrated;
|
|
484
|
+
};
|
|
485
|
+
const perDvm = credit.perDvm;
|
|
486
|
+
return {
|
|
487
|
+
...config,
|
|
488
|
+
credit: {
|
|
489
|
+
...migrateLayer(credit),
|
|
490
|
+
...perDvm ? {
|
|
491
|
+
perDvm: Object.fromEntries(
|
|
492
|
+
Object.entries(perDvm).map(([endpoint, per]) => [endpoint, migrateLayer(per)])
|
|
493
|
+
)
|
|
494
|
+
} : {}
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function migrateFundingRail(layer) {
|
|
499
|
+
const loose = layer;
|
|
500
|
+
if (loose.fundingRail !== "mpp") return layer;
|
|
501
|
+
return { ...layer, fundingRail: "tempo" };
|
|
502
|
+
}
|
|
503
|
+
function migrateTempoWalletKey(config) {
|
|
504
|
+
const loose = config;
|
|
505
|
+
if (!loose.mpp || loose.tempo) return config;
|
|
506
|
+
const { mpp, ...rest } = loose;
|
|
507
|
+
return { ...rest, tempo: mpp };
|
|
508
|
+
}
|
|
509
|
+
function migrateConfig(config) {
|
|
510
|
+
return migrateCreditConfig(migrateTempoWalletKey(config));
|
|
511
|
+
}
|
|
512
|
+
function loadConfig() {
|
|
513
|
+
const raw = readRawConfig();
|
|
514
|
+
return raw === null ? null : migrateConfig(raw);
|
|
515
|
+
}
|
|
516
|
+
async function updateConfig(update) {
|
|
517
|
+
return withConfigLock(async () => {
|
|
518
|
+
const raw = readRawConfig();
|
|
519
|
+
const snapshot = raw === null ? {} : structuredClone(raw);
|
|
520
|
+
const base = raw === null ? null : migrateConfig(structuredClone(raw));
|
|
521
|
+
const handed = raw === null ? null : deepFreezeConfig(migrateConfig(structuredClone(raw)));
|
|
522
|
+
const next = applyConfigPatch(base ?? {}, await runPatch(update, handed));
|
|
523
|
+
const changed = changedConfigPaths(snapshot, next);
|
|
524
|
+
if (changed.length === 0) {
|
|
525
|
+
return {
|
|
526
|
+
config: base ?? {},
|
|
527
|
+
previous: base,
|
|
528
|
+
changed: [],
|
|
529
|
+
written: false,
|
|
530
|
+
backup: describeConfigBackup()
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
const removals = protectedRemovals(base ?? {}, next);
|
|
534
|
+
const unauthorized = removals.filter((target) => update.authorize?.covers(target) !== true);
|
|
535
|
+
if (unauthorized.length > 0) throw protectedRemovalError(unauthorized, update.reason);
|
|
536
|
+
assertValidConfig(
|
|
537
|
+
next,
|
|
538
|
+
changed.map((path) => path.split(".")[0])
|
|
539
|
+
);
|
|
540
|
+
const superseded = supersededProtectedSecrets(base ?? {}, next);
|
|
541
|
+
const stripped = [
|
|
542
|
+
.../* @__PURE__ */ new Set([...removals, ...superseded, ...update.sanitizeBackup ?? []])
|
|
543
|
+
].filter(isSecretProtectedTarget);
|
|
544
|
+
const sweeping = update.sweepSlots ?? CONFIG_SNAPSHOT_SLOTS;
|
|
545
|
+
const swept = [];
|
|
546
|
+
if (raw !== null) {
|
|
547
|
+
if (stripped.length > 0) {
|
|
548
|
+
writeConfigBackup(withoutProtectedTargets(raw, stripped));
|
|
549
|
+
swept.push("backup");
|
|
550
|
+
} else {
|
|
551
|
+
retainConfigBackup();
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (stripped.length > 0 && sweeping.includes("pinned")) {
|
|
555
|
+
if (sweepPinnedSnapshot(stripped, base ?? {}, update.reason)) swept.push("pinned");
|
|
556
|
+
}
|
|
557
|
+
const backup = describeConfigBackup();
|
|
558
|
+
writeConfigFile(next);
|
|
559
|
+
appendConfigAudit({
|
|
560
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
561
|
+
event: "update",
|
|
562
|
+
reason: update.reason,
|
|
563
|
+
fields: changed,
|
|
564
|
+
...removals.length > 0 ? { removed: removals } : {},
|
|
565
|
+
...superseded.length > 0 ? { superseded } : {},
|
|
566
|
+
...swept.length > 0 ? { sweptSlots: swept } : {},
|
|
567
|
+
backup: backup !== null,
|
|
568
|
+
pid: process.pid
|
|
569
|
+
});
|
|
570
|
+
return { config: next, previous: base, changed, written: true, backup };
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
async function pinConfigSnapshot(reason) {
|
|
574
|
+
return withConfigLock(() => {
|
|
575
|
+
if (describeConfigBackup("pinned") !== null) return null;
|
|
576
|
+
if (!retainConfigBackup("pinned")) return null;
|
|
577
|
+
const pinned = describeConfigBackup("pinned");
|
|
578
|
+
appendConfigAudit({
|
|
579
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
580
|
+
event: "pinned",
|
|
581
|
+
reason,
|
|
582
|
+
fields: [],
|
|
583
|
+
slot: "pinned",
|
|
584
|
+
backup: pinned !== null,
|
|
585
|
+
pid: process.pid
|
|
586
|
+
});
|
|
587
|
+
return pinned;
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
async function ensureConfig(seed = {}, reason = "init") {
|
|
591
|
+
return withConfigLock(() => {
|
|
592
|
+
const existing = readRawConfig();
|
|
593
|
+
if (existing !== null) return { config: migrateConfig(existing), created: false };
|
|
594
|
+
assertValidConfig(seed);
|
|
595
|
+
writeConfigFile(seed);
|
|
596
|
+
appendConfigAudit({
|
|
597
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
598
|
+
event: "create",
|
|
599
|
+
reason,
|
|
600
|
+
fields: changedConfigPaths({}, seed),
|
|
601
|
+
backup: describeConfigBackup() !== null,
|
|
602
|
+
pid: process.pid
|
|
603
|
+
});
|
|
604
|
+
return { config: seed, created: true };
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
function previewConfigRollback(source = "backup") {
|
|
608
|
+
const restored = readConfigBackup(source);
|
|
609
|
+
if (restored === null) return null;
|
|
610
|
+
let currentOnDisk = null;
|
|
611
|
+
try {
|
|
612
|
+
currentOnDisk = readRawConfig();
|
|
613
|
+
} catch {
|
|
614
|
+
}
|
|
615
|
+
return {
|
|
616
|
+
changed: changedConfigPaths(currentOnDisk ?? {}, restored),
|
|
617
|
+
removing: protectedRemovals(
|
|
618
|
+
currentOnDisk === null ? {} : migrateConfig(structuredClone(currentOnDisk)),
|
|
619
|
+
migrateConfig(structuredClone(restored))
|
|
620
|
+
)
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
async function rollbackConfig(reason = "config_rollback", source = "backup", authorize) {
|
|
624
|
+
return withConfigLock(() => {
|
|
625
|
+
const restored = readConfigBackup(source);
|
|
626
|
+
if (restored === null) {
|
|
627
|
+
const stored = describeConfigBackup(source);
|
|
628
|
+
throw stored === null ? new DvmError(
|
|
629
|
+
"config_no_rollback",
|
|
630
|
+
`No ${source === "pinned" ? "pinned snapshot" : "rollback copy"} of the configuration is stored.`,
|
|
631
|
+
`A rollback copy is written at ${configSnapshotPath(source)} before each change, so there is one to restore only after a change has been made on this machine.`
|
|
632
|
+
) : new DvmError(
|
|
633
|
+
"config_rollback_corrupt",
|
|
634
|
+
`The stored copy at ${configSnapshotPath(source)} is not readable as a configuration.`,
|
|
635
|
+
"Nothing was changed. Inspect that file yourself, or remove it and continue with the configuration you have."
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
assertValidConfig(restored);
|
|
639
|
+
let currentOnDisk = null;
|
|
640
|
+
try {
|
|
641
|
+
currentOnDisk = readRawConfig();
|
|
642
|
+
} catch {
|
|
643
|
+
}
|
|
644
|
+
const changed = changedConfigPaths(currentOnDisk ?? {}, restored);
|
|
645
|
+
const removed = protectedRemovals(
|
|
646
|
+
currentOnDisk === null ? {} : migrateConfig(structuredClone(currentOnDisk)),
|
|
647
|
+
migrateConfig(structuredClone(restored))
|
|
648
|
+
);
|
|
649
|
+
const unauthorized = removed.filter((target) => authorize?.covers(target) !== true);
|
|
650
|
+
if (unauthorized.length > 0) throw protectedRemovalError(unauthorized, reason);
|
|
651
|
+
if (!retainConfigBackup()) retireSlot(`${reason}:nothing_to_swap`, "backup");
|
|
652
|
+
if (source === "pinned") retireSlot(`${reason}:pin_restored`, "pinned");
|
|
653
|
+
writeConfigFile(restored);
|
|
654
|
+
appendConfigAudit({
|
|
655
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
656
|
+
event: "rollback",
|
|
657
|
+
reason,
|
|
658
|
+
fields: changed,
|
|
659
|
+
...removed.length > 0 ? { removed } : {},
|
|
660
|
+
source,
|
|
661
|
+
backup: describeConfigBackup() !== null,
|
|
662
|
+
pid: process.pid
|
|
663
|
+
});
|
|
664
|
+
return { config: restored, changed, removed };
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
async function retireConfigBackup(reason, slot = "backup") {
|
|
668
|
+
return withConfigLock(() => retireSlot(reason, slot));
|
|
669
|
+
}
|
|
670
|
+
async function withConfigLock(fn) {
|
|
671
|
+
ensureConfigDir();
|
|
672
|
+
let release;
|
|
673
|
+
try {
|
|
674
|
+
release = await import_proper_lockfile.default.lock(CONFIG_FILE, {
|
|
675
|
+
lockfilePath: `${CONFIG_FILE}.lock`,
|
|
676
|
+
realpath: false,
|
|
677
|
+
stale: 3e4,
|
|
678
|
+
retries: { retries: 10, factor: 1, minTimeout: 100, maxTimeout: 100 }
|
|
679
|
+
});
|
|
680
|
+
} catch {
|
|
681
|
+
throw new DvmError(
|
|
682
|
+
"config_locked",
|
|
683
|
+
"Another dvm process is updating the local configuration.",
|
|
684
|
+
"Wait a moment and retry."
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
try {
|
|
688
|
+
return await fn();
|
|
689
|
+
} finally {
|
|
690
|
+
await release().catch(() => void 0);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
async function runPatch(update, current) {
|
|
694
|
+
try {
|
|
695
|
+
return await update.patch(current);
|
|
696
|
+
} catch (err) {
|
|
697
|
+
if (err instanceof TypeError && /read only|not extensible|frozen|cannot delete property|cannot add property/i.test(
|
|
698
|
+
err.message
|
|
699
|
+
)) {
|
|
700
|
+
throw new DvmError(
|
|
701
|
+
"config_patch_invalid",
|
|
702
|
+
`The '${update.reason}' change tried to modify the configuration it was handed.`,
|
|
703
|
+
"Nothing was written. A change declares what it wants through its `set` and `unset` lists; editing the current config in place bypasses the protected-field gate, so the object is frozen."
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
throw err;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function sweepPinnedSnapshot(targets, departing, reason) {
|
|
710
|
+
if (describeConfigBackup("pinned") === null) return false;
|
|
711
|
+
const pinned = readConfigBackup("pinned");
|
|
712
|
+
if (pinned === null) {
|
|
713
|
+
return retireSlot(`${reason}:unreadable_pin`, "pinned");
|
|
714
|
+
}
|
|
715
|
+
const migrated = migrateConfig(structuredClone(pinned));
|
|
716
|
+
const matching = targets.filter(
|
|
717
|
+
(target) => protectedValuesMatch(
|
|
718
|
+
target,
|
|
719
|
+
protectedTargetValue(departing, target),
|
|
720
|
+
protectedTargetValue(migrated, target)
|
|
721
|
+
)
|
|
722
|
+
);
|
|
723
|
+
if (matching.length === 0) return false;
|
|
724
|
+
const sanitized = withoutProtectedTargets(pinned, matching);
|
|
725
|
+
const changed = changedConfigPaths(pinned, sanitized);
|
|
726
|
+
if (changed.length === 0) return false;
|
|
727
|
+
writeConfigBackup(sanitized, "pinned");
|
|
728
|
+
appendConfigAudit({
|
|
729
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
730
|
+
event: "backup_sanitized",
|
|
731
|
+
reason,
|
|
732
|
+
fields: changed,
|
|
733
|
+
removed: matching,
|
|
734
|
+
slot: "pinned",
|
|
735
|
+
backup: describeConfigBackup() !== null,
|
|
736
|
+
pid: process.pid
|
|
737
|
+
});
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
function retireSlot(reason, slot) {
|
|
741
|
+
if (!removeConfigBackup(slot)) return false;
|
|
742
|
+
appendConfigAudit({
|
|
743
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
744
|
+
event: "backup_retired",
|
|
745
|
+
reason,
|
|
746
|
+
fields: [],
|
|
747
|
+
slot,
|
|
748
|
+
backup: describeConfigBackup() !== null,
|
|
749
|
+
pid: process.pid
|
|
750
|
+
});
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// node_modules/@noble/hashes/utils.js
|
|
755
|
+
function isBytes(a) {
|
|
756
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
757
|
+
}
|
|
758
|
+
function anumber(n, title = "") {
|
|
759
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
760
|
+
const prefix = title && `"${title}" `;
|
|
761
|
+
throw new Error(`${prefix}expected integer >= 0, got ${n}`);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
function abytes(value, length, title = "") {
|
|
765
|
+
const bytes = isBytes(value);
|
|
766
|
+
const len = value?.length;
|
|
767
|
+
const needsLen = length !== void 0;
|
|
768
|
+
if (!bytes || needsLen && len !== length) {
|
|
769
|
+
const prefix = title && `"${title}" `;
|
|
770
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
771
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
772
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
773
|
+
}
|
|
774
|
+
return value;
|
|
775
|
+
}
|
|
776
|
+
function ahash(h) {
|
|
777
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
778
|
+
throw new Error("Hash must wrapped by utils.createHasher");
|
|
779
|
+
anumber(h.outputLen);
|
|
780
|
+
anumber(h.blockLen);
|
|
781
|
+
}
|
|
782
|
+
function aexists(instance, checkFinished = true) {
|
|
783
|
+
if (instance.destroyed)
|
|
784
|
+
throw new Error("Hash instance has been destroyed");
|
|
785
|
+
if (checkFinished && instance.finished)
|
|
786
|
+
throw new Error("Hash#digest() has already been called");
|
|
787
|
+
}
|
|
788
|
+
function aoutput(out, instance) {
|
|
789
|
+
abytes(out, void 0, "digestInto() output");
|
|
790
|
+
const min = instance.outputLen;
|
|
791
|
+
if (out.length < min) {
|
|
792
|
+
throw new Error('"digestInto() output" expected to be of length >=' + min);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function clean(...arrays) {
|
|
796
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
797
|
+
arrays[i].fill(0);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
function createView(arr) {
|
|
801
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
802
|
+
}
|
|
803
|
+
function rotr(word, shift) {
|
|
804
|
+
return word << 32 - shift | word >>> shift;
|
|
805
|
+
}
|
|
806
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
807
|
+
// @ts-ignore
|
|
808
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
809
|
+
))();
|
|
810
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
811
|
+
function bytesToHex(bytes) {
|
|
812
|
+
abytes(bytes);
|
|
813
|
+
if (hasHexBuiltin)
|
|
814
|
+
return bytes.toHex();
|
|
815
|
+
let hex = "";
|
|
816
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
817
|
+
hex += hexes[bytes[i]];
|
|
818
|
+
}
|
|
819
|
+
return hex;
|
|
820
|
+
}
|
|
821
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
822
|
+
function asciiToBase16(ch) {
|
|
823
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
824
|
+
return ch - asciis._0;
|
|
825
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
826
|
+
return ch - (asciis.A - 10);
|
|
827
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
828
|
+
return ch - (asciis.a - 10);
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
function hexToBytes(hex) {
|
|
832
|
+
if (typeof hex !== "string")
|
|
833
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
834
|
+
if (hasHexBuiltin)
|
|
835
|
+
return Uint8Array.fromHex(hex);
|
|
836
|
+
const hl = hex.length;
|
|
837
|
+
const al = hl / 2;
|
|
838
|
+
if (hl % 2)
|
|
839
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
840
|
+
const array = new Uint8Array(al);
|
|
841
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
842
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
843
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
844
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
845
|
+
const char = hex[hi] + hex[hi + 1];
|
|
846
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
847
|
+
}
|
|
848
|
+
array[ai] = n1 * 16 + n2;
|
|
849
|
+
}
|
|
850
|
+
return array;
|
|
851
|
+
}
|
|
852
|
+
function utf8ToBytes(str) {
|
|
853
|
+
if (typeof str !== "string")
|
|
854
|
+
throw new Error("string expected");
|
|
855
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
856
|
+
}
|
|
857
|
+
function kdfInputToBytes(data, errorTitle = "") {
|
|
858
|
+
if (typeof data === "string")
|
|
859
|
+
return utf8ToBytes(data);
|
|
860
|
+
return abytes(data, void 0, errorTitle);
|
|
861
|
+
}
|
|
862
|
+
function concatBytes(...arrays) {
|
|
863
|
+
let sum = 0;
|
|
864
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
865
|
+
const a = arrays[i];
|
|
866
|
+
abytes(a);
|
|
867
|
+
sum += a.length;
|
|
868
|
+
}
|
|
869
|
+
const res = new Uint8Array(sum);
|
|
870
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
871
|
+
const a = arrays[i];
|
|
872
|
+
res.set(a, pad);
|
|
873
|
+
pad += a.length;
|
|
874
|
+
}
|
|
875
|
+
return res;
|
|
876
|
+
}
|
|
877
|
+
function checkOpts(defaults, opts) {
|
|
878
|
+
if (opts !== void 0 && {}.toString.call(opts) !== "[object Object]")
|
|
879
|
+
throw new Error("options must be object or undefined");
|
|
880
|
+
const merged = Object.assign(defaults, opts);
|
|
881
|
+
return merged;
|
|
882
|
+
}
|
|
883
|
+
function createHasher(hashCons, info = {}) {
|
|
884
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
885
|
+
const tmp = hashCons(void 0);
|
|
886
|
+
hashC.outputLen = tmp.outputLen;
|
|
887
|
+
hashC.blockLen = tmp.blockLen;
|
|
888
|
+
hashC.create = (opts) => hashCons(opts);
|
|
889
|
+
Object.assign(hashC, info);
|
|
890
|
+
return Object.freeze(hashC);
|
|
891
|
+
}
|
|
892
|
+
function randomBytes(bytesLength = 32) {
|
|
893
|
+
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
894
|
+
if (typeof cr?.getRandomValues !== "function")
|
|
895
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
896
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
897
|
+
}
|
|
898
|
+
var oidNist = (suffix) => ({
|
|
899
|
+
oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
// node_modules/@noble/hashes/_md.js
|
|
903
|
+
function Chi(a, b, c) {
|
|
904
|
+
return a & b ^ ~a & c;
|
|
905
|
+
}
|
|
906
|
+
function Maj(a, b, c) {
|
|
907
|
+
return a & b ^ a & c ^ b & c;
|
|
908
|
+
}
|
|
909
|
+
var HashMD = class {
|
|
910
|
+
blockLen;
|
|
911
|
+
outputLen;
|
|
912
|
+
padOffset;
|
|
913
|
+
isLE;
|
|
914
|
+
// For partial updates less than block size
|
|
915
|
+
buffer;
|
|
916
|
+
view;
|
|
917
|
+
finished = false;
|
|
918
|
+
length = 0;
|
|
919
|
+
pos = 0;
|
|
920
|
+
destroyed = false;
|
|
921
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
922
|
+
this.blockLen = blockLen;
|
|
923
|
+
this.outputLen = outputLen;
|
|
924
|
+
this.padOffset = padOffset;
|
|
925
|
+
this.isLE = isLE;
|
|
926
|
+
this.buffer = new Uint8Array(blockLen);
|
|
927
|
+
this.view = createView(this.buffer);
|
|
928
|
+
}
|
|
929
|
+
update(data) {
|
|
930
|
+
aexists(this);
|
|
931
|
+
abytes(data);
|
|
932
|
+
const { view, buffer, blockLen } = this;
|
|
933
|
+
const len = data.length;
|
|
934
|
+
for (let pos = 0; pos < len; ) {
|
|
935
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
936
|
+
if (take === blockLen) {
|
|
937
|
+
const dataView = createView(data);
|
|
938
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
939
|
+
this.process(dataView, pos);
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
943
|
+
this.pos += take;
|
|
944
|
+
pos += take;
|
|
945
|
+
if (this.pos === blockLen) {
|
|
946
|
+
this.process(view, 0);
|
|
947
|
+
this.pos = 0;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
this.length += data.length;
|
|
951
|
+
this.roundClean();
|
|
952
|
+
return this;
|
|
953
|
+
}
|
|
954
|
+
digestInto(out) {
|
|
955
|
+
aexists(this);
|
|
956
|
+
aoutput(out, this);
|
|
957
|
+
this.finished = true;
|
|
958
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
959
|
+
let { pos } = this;
|
|
960
|
+
buffer[pos++] = 128;
|
|
961
|
+
clean(this.buffer.subarray(pos));
|
|
962
|
+
if (this.padOffset > blockLen - pos) {
|
|
963
|
+
this.process(view, 0);
|
|
964
|
+
pos = 0;
|
|
965
|
+
}
|
|
966
|
+
for (let i = pos; i < blockLen; i++)
|
|
967
|
+
buffer[i] = 0;
|
|
968
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
969
|
+
this.process(view, 0);
|
|
970
|
+
const oview = createView(out);
|
|
971
|
+
const len = this.outputLen;
|
|
972
|
+
if (len % 4)
|
|
973
|
+
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
974
|
+
const outLen = len / 4;
|
|
975
|
+
const state = this.get();
|
|
976
|
+
if (outLen > state.length)
|
|
977
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
978
|
+
for (let i = 0; i < outLen; i++)
|
|
979
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
980
|
+
}
|
|
981
|
+
digest() {
|
|
982
|
+
const { buffer, outputLen } = this;
|
|
983
|
+
this.digestInto(buffer);
|
|
984
|
+
const res = buffer.slice(0, outputLen);
|
|
985
|
+
this.destroy();
|
|
986
|
+
return res;
|
|
987
|
+
}
|
|
988
|
+
_cloneInto(to) {
|
|
989
|
+
to ||= new this.constructor();
|
|
990
|
+
to.set(...this.get());
|
|
991
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
992
|
+
to.destroyed = destroyed;
|
|
993
|
+
to.finished = finished;
|
|
994
|
+
to.length = length;
|
|
995
|
+
to.pos = pos;
|
|
996
|
+
if (length % blockLen)
|
|
997
|
+
to.buffer.set(buffer);
|
|
998
|
+
return to;
|
|
999
|
+
}
|
|
1000
|
+
clone() {
|
|
1001
|
+
return this._cloneInto();
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1005
|
+
1779033703,
|
|
1006
|
+
3144134277,
|
|
1007
|
+
1013904242,
|
|
1008
|
+
2773480762,
|
|
1009
|
+
1359893119,
|
|
1010
|
+
2600822924,
|
|
1011
|
+
528734635,
|
|
1012
|
+
1541459225
|
|
1013
|
+
]);
|
|
1014
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
1015
|
+
1779033703,
|
|
1016
|
+
4089235720,
|
|
1017
|
+
3144134277,
|
|
1018
|
+
2227873595,
|
|
1019
|
+
1013904242,
|
|
1020
|
+
4271175723,
|
|
1021
|
+
2773480762,
|
|
1022
|
+
1595750129,
|
|
1023
|
+
1359893119,
|
|
1024
|
+
2917565137,
|
|
1025
|
+
2600822924,
|
|
1026
|
+
725511199,
|
|
1027
|
+
528734635,
|
|
1028
|
+
4215389547,
|
|
1029
|
+
1541459225,
|
|
1030
|
+
327033209
|
|
1031
|
+
]);
|
|
1032
|
+
|
|
1033
|
+
// node_modules/@noble/hashes/_u64.js
|
|
1034
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
1035
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
1036
|
+
function fromBig(n, le = false) {
|
|
1037
|
+
if (le)
|
|
1038
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
1039
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1040
|
+
}
|
|
1041
|
+
function split(lst, le = false) {
|
|
1042
|
+
const len = lst.length;
|
|
1043
|
+
let Ah = new Uint32Array(len);
|
|
1044
|
+
let Al = new Uint32Array(len);
|
|
1045
|
+
for (let i = 0; i < len; i++) {
|
|
1046
|
+
const { h, l } = fromBig(lst[i], le);
|
|
1047
|
+
[Ah[i], Al[i]] = [h, l];
|
|
1048
|
+
}
|
|
1049
|
+
return [Ah, Al];
|
|
1050
|
+
}
|
|
1051
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
1052
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1053
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1054
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1055
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1056
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1057
|
+
function add(Ah, Al, Bh, Bl) {
|
|
1058
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1059
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
1060
|
+
}
|
|
1061
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1062
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1063
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1064
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1065
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1066
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1067
|
+
|
|
1068
|
+
// node_modules/@noble/hashes/sha2.js
|
|
1069
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
1070
|
+
1116352408,
|
|
1071
|
+
1899447441,
|
|
1072
|
+
3049323471,
|
|
1073
|
+
3921009573,
|
|
1074
|
+
961987163,
|
|
1075
|
+
1508970993,
|
|
1076
|
+
2453635748,
|
|
1077
|
+
2870763221,
|
|
1078
|
+
3624381080,
|
|
1079
|
+
310598401,
|
|
1080
|
+
607225278,
|
|
1081
|
+
1426881987,
|
|
1082
|
+
1925078388,
|
|
1083
|
+
2162078206,
|
|
1084
|
+
2614888103,
|
|
1085
|
+
3248222580,
|
|
1086
|
+
3835390401,
|
|
1087
|
+
4022224774,
|
|
1088
|
+
264347078,
|
|
1089
|
+
604807628,
|
|
1090
|
+
770255983,
|
|
1091
|
+
1249150122,
|
|
1092
|
+
1555081692,
|
|
1093
|
+
1996064986,
|
|
1094
|
+
2554220882,
|
|
1095
|
+
2821834349,
|
|
1096
|
+
2952996808,
|
|
1097
|
+
3210313671,
|
|
1098
|
+
3336571891,
|
|
1099
|
+
3584528711,
|
|
1100
|
+
113926993,
|
|
1101
|
+
338241895,
|
|
1102
|
+
666307205,
|
|
1103
|
+
773529912,
|
|
1104
|
+
1294757372,
|
|
1105
|
+
1396182291,
|
|
1106
|
+
1695183700,
|
|
1107
|
+
1986661051,
|
|
1108
|
+
2177026350,
|
|
1109
|
+
2456956037,
|
|
1110
|
+
2730485921,
|
|
1111
|
+
2820302411,
|
|
1112
|
+
3259730800,
|
|
1113
|
+
3345764771,
|
|
1114
|
+
3516065817,
|
|
1115
|
+
3600352804,
|
|
1116
|
+
4094571909,
|
|
1117
|
+
275423344,
|
|
1118
|
+
430227734,
|
|
1119
|
+
506948616,
|
|
1120
|
+
659060556,
|
|
1121
|
+
883997877,
|
|
1122
|
+
958139571,
|
|
1123
|
+
1322822218,
|
|
1124
|
+
1537002063,
|
|
1125
|
+
1747873779,
|
|
1126
|
+
1955562222,
|
|
1127
|
+
2024104815,
|
|
1128
|
+
2227730452,
|
|
1129
|
+
2361852424,
|
|
1130
|
+
2428436474,
|
|
1131
|
+
2756734187,
|
|
1132
|
+
3204031479,
|
|
1133
|
+
3329325298
|
|
1134
|
+
]);
|
|
1135
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1136
|
+
var SHA2_32B = class extends HashMD {
|
|
1137
|
+
constructor(outputLen) {
|
|
1138
|
+
super(64, outputLen, 8, false);
|
|
1139
|
+
}
|
|
1140
|
+
get() {
|
|
1141
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
1142
|
+
return [A, B, C, D, E, F, G, H];
|
|
1143
|
+
}
|
|
1144
|
+
// prettier-ignore
|
|
1145
|
+
set(A, B, C, D, E, F, G, H) {
|
|
1146
|
+
this.A = A | 0;
|
|
1147
|
+
this.B = B | 0;
|
|
1148
|
+
this.C = C | 0;
|
|
1149
|
+
this.D = D | 0;
|
|
1150
|
+
this.E = E | 0;
|
|
1151
|
+
this.F = F | 0;
|
|
1152
|
+
this.G = G | 0;
|
|
1153
|
+
this.H = H | 0;
|
|
1154
|
+
}
|
|
1155
|
+
process(view, offset) {
|
|
1156
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
1157
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
1158
|
+
for (let i = 16; i < 64; i++) {
|
|
1159
|
+
const W15 = SHA256_W[i - 15];
|
|
1160
|
+
const W2 = SHA256_W[i - 2];
|
|
1161
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
1162
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
1163
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
1164
|
+
}
|
|
1165
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
1166
|
+
for (let i = 0; i < 64; i++) {
|
|
1167
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
1168
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
1169
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
1170
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
1171
|
+
H = G;
|
|
1172
|
+
G = F;
|
|
1173
|
+
F = E;
|
|
1174
|
+
E = D + T1 | 0;
|
|
1175
|
+
D = C;
|
|
1176
|
+
C = B;
|
|
1177
|
+
B = A;
|
|
1178
|
+
A = T1 + T2 | 0;
|
|
1179
|
+
}
|
|
1180
|
+
A = A + this.A | 0;
|
|
1181
|
+
B = B + this.B | 0;
|
|
1182
|
+
C = C + this.C | 0;
|
|
1183
|
+
D = D + this.D | 0;
|
|
1184
|
+
E = E + this.E | 0;
|
|
1185
|
+
F = F + this.F | 0;
|
|
1186
|
+
G = G + this.G | 0;
|
|
1187
|
+
H = H + this.H | 0;
|
|
1188
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
1189
|
+
}
|
|
1190
|
+
roundClean() {
|
|
1191
|
+
clean(SHA256_W);
|
|
1192
|
+
}
|
|
1193
|
+
destroy() {
|
|
1194
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1195
|
+
clean(this.buffer);
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
var _SHA256 = class extends SHA2_32B {
|
|
1199
|
+
// We cannot use array here since array allows indexing by variable
|
|
1200
|
+
// which means optimizer/compiler cannot use registers.
|
|
1201
|
+
A = SHA256_IV[0] | 0;
|
|
1202
|
+
B = SHA256_IV[1] | 0;
|
|
1203
|
+
C = SHA256_IV[2] | 0;
|
|
1204
|
+
D = SHA256_IV[3] | 0;
|
|
1205
|
+
E = SHA256_IV[4] | 0;
|
|
1206
|
+
F = SHA256_IV[5] | 0;
|
|
1207
|
+
G = SHA256_IV[6] | 0;
|
|
1208
|
+
H = SHA256_IV[7] | 0;
|
|
1209
|
+
constructor() {
|
|
1210
|
+
super(32);
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
1214
|
+
"0x428a2f98d728ae22",
|
|
1215
|
+
"0x7137449123ef65cd",
|
|
1216
|
+
"0xb5c0fbcfec4d3b2f",
|
|
1217
|
+
"0xe9b5dba58189dbbc",
|
|
1218
|
+
"0x3956c25bf348b538",
|
|
1219
|
+
"0x59f111f1b605d019",
|
|
1220
|
+
"0x923f82a4af194f9b",
|
|
1221
|
+
"0xab1c5ed5da6d8118",
|
|
1222
|
+
"0xd807aa98a3030242",
|
|
1223
|
+
"0x12835b0145706fbe",
|
|
1224
|
+
"0x243185be4ee4b28c",
|
|
1225
|
+
"0x550c7dc3d5ffb4e2",
|
|
1226
|
+
"0x72be5d74f27b896f",
|
|
1227
|
+
"0x80deb1fe3b1696b1",
|
|
1228
|
+
"0x9bdc06a725c71235",
|
|
1229
|
+
"0xc19bf174cf692694",
|
|
1230
|
+
"0xe49b69c19ef14ad2",
|
|
1231
|
+
"0xefbe4786384f25e3",
|
|
1232
|
+
"0x0fc19dc68b8cd5b5",
|
|
1233
|
+
"0x240ca1cc77ac9c65",
|
|
1234
|
+
"0x2de92c6f592b0275",
|
|
1235
|
+
"0x4a7484aa6ea6e483",
|
|
1236
|
+
"0x5cb0a9dcbd41fbd4",
|
|
1237
|
+
"0x76f988da831153b5",
|
|
1238
|
+
"0x983e5152ee66dfab",
|
|
1239
|
+
"0xa831c66d2db43210",
|
|
1240
|
+
"0xb00327c898fb213f",
|
|
1241
|
+
"0xbf597fc7beef0ee4",
|
|
1242
|
+
"0xc6e00bf33da88fc2",
|
|
1243
|
+
"0xd5a79147930aa725",
|
|
1244
|
+
"0x06ca6351e003826f",
|
|
1245
|
+
"0x142929670a0e6e70",
|
|
1246
|
+
"0x27b70a8546d22ffc",
|
|
1247
|
+
"0x2e1b21385c26c926",
|
|
1248
|
+
"0x4d2c6dfc5ac42aed",
|
|
1249
|
+
"0x53380d139d95b3df",
|
|
1250
|
+
"0x650a73548baf63de",
|
|
1251
|
+
"0x766a0abb3c77b2a8",
|
|
1252
|
+
"0x81c2c92e47edaee6",
|
|
1253
|
+
"0x92722c851482353b",
|
|
1254
|
+
"0xa2bfe8a14cf10364",
|
|
1255
|
+
"0xa81a664bbc423001",
|
|
1256
|
+
"0xc24b8b70d0f89791",
|
|
1257
|
+
"0xc76c51a30654be30",
|
|
1258
|
+
"0xd192e819d6ef5218",
|
|
1259
|
+
"0xd69906245565a910",
|
|
1260
|
+
"0xf40e35855771202a",
|
|
1261
|
+
"0x106aa07032bbd1b8",
|
|
1262
|
+
"0x19a4c116b8d2d0c8",
|
|
1263
|
+
"0x1e376c085141ab53",
|
|
1264
|
+
"0x2748774cdf8eeb99",
|
|
1265
|
+
"0x34b0bcb5e19b48a8",
|
|
1266
|
+
"0x391c0cb3c5c95a63",
|
|
1267
|
+
"0x4ed8aa4ae3418acb",
|
|
1268
|
+
"0x5b9cca4f7763e373",
|
|
1269
|
+
"0x682e6ff3d6b2b8a3",
|
|
1270
|
+
"0x748f82ee5defb2fc",
|
|
1271
|
+
"0x78a5636f43172f60",
|
|
1272
|
+
"0x84c87814a1f0ab72",
|
|
1273
|
+
"0x8cc702081a6439ec",
|
|
1274
|
+
"0x90befffa23631e28",
|
|
1275
|
+
"0xa4506cebde82bde9",
|
|
1276
|
+
"0xbef9a3f7b2c67915",
|
|
1277
|
+
"0xc67178f2e372532b",
|
|
1278
|
+
"0xca273eceea26619c",
|
|
1279
|
+
"0xd186b8c721c0c207",
|
|
1280
|
+
"0xeada7dd6cde0eb1e",
|
|
1281
|
+
"0xf57d4f7fee6ed178",
|
|
1282
|
+
"0x06f067aa72176fba",
|
|
1283
|
+
"0x0a637dc5a2c898a6",
|
|
1284
|
+
"0x113f9804bef90dae",
|
|
1285
|
+
"0x1b710b35131c471b",
|
|
1286
|
+
"0x28db77f523047d84",
|
|
1287
|
+
"0x32caab7b40c72493",
|
|
1288
|
+
"0x3c9ebe0a15c9bebc",
|
|
1289
|
+
"0x431d67c49c100d4c",
|
|
1290
|
+
"0x4cc5d4becb3e42b6",
|
|
1291
|
+
"0x597f299cfc657e2a",
|
|
1292
|
+
"0x5fcb6fab3ad6faec",
|
|
1293
|
+
"0x6c44198c4a475817"
|
|
1294
|
+
].map((n) => BigInt(n))))();
|
|
1295
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1296
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1297
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1298
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1299
|
+
var SHA2_64B = class extends HashMD {
|
|
1300
|
+
constructor(outputLen) {
|
|
1301
|
+
super(128, outputLen, 16, false);
|
|
1302
|
+
}
|
|
1303
|
+
// prettier-ignore
|
|
1304
|
+
get() {
|
|
1305
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1306
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1307
|
+
}
|
|
1308
|
+
// prettier-ignore
|
|
1309
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1310
|
+
this.Ah = Ah | 0;
|
|
1311
|
+
this.Al = Al | 0;
|
|
1312
|
+
this.Bh = Bh | 0;
|
|
1313
|
+
this.Bl = Bl | 0;
|
|
1314
|
+
this.Ch = Ch | 0;
|
|
1315
|
+
this.Cl = Cl | 0;
|
|
1316
|
+
this.Dh = Dh | 0;
|
|
1317
|
+
this.Dl = Dl | 0;
|
|
1318
|
+
this.Eh = Eh | 0;
|
|
1319
|
+
this.El = El | 0;
|
|
1320
|
+
this.Fh = Fh | 0;
|
|
1321
|
+
this.Fl = Fl | 0;
|
|
1322
|
+
this.Gh = Gh | 0;
|
|
1323
|
+
this.Gl = Gl | 0;
|
|
1324
|
+
this.Hh = Hh | 0;
|
|
1325
|
+
this.Hl = Hl | 0;
|
|
1326
|
+
}
|
|
1327
|
+
process(view, offset) {
|
|
1328
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1329
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
1330
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
1331
|
+
}
|
|
1332
|
+
for (let i = 16; i < 80; i++) {
|
|
1333
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
1334
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
1335
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
1336
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
1337
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
1338
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1339
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1340
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1341
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1342
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1343
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
1344
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
1345
|
+
}
|
|
1346
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1347
|
+
for (let i = 0; i < 80; i++) {
|
|
1348
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
1349
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
1350
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
1351
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
1352
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
1353
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
1354
|
+
const T1l = T1ll | 0;
|
|
1355
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
1356
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
1357
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
1358
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
1359
|
+
Hh = Gh | 0;
|
|
1360
|
+
Hl = Gl | 0;
|
|
1361
|
+
Gh = Fh | 0;
|
|
1362
|
+
Gl = Fl | 0;
|
|
1363
|
+
Fh = Eh | 0;
|
|
1364
|
+
Fl = El | 0;
|
|
1365
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
1366
|
+
Dh = Ch | 0;
|
|
1367
|
+
Dl = Cl | 0;
|
|
1368
|
+
Ch = Bh | 0;
|
|
1369
|
+
Cl = Bl | 0;
|
|
1370
|
+
Bh = Ah | 0;
|
|
1371
|
+
Bl = Al | 0;
|
|
1372
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
1373
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
1374
|
+
Al = All | 0;
|
|
1375
|
+
}
|
|
1376
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
1377
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
1378
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
1379
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
1380
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
1381
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
1382
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
1383
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1384
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1385
|
+
}
|
|
1386
|
+
roundClean() {
|
|
1387
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
1388
|
+
}
|
|
1389
|
+
destroy() {
|
|
1390
|
+
clean(this.buffer);
|
|
1391
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
var _SHA512 = class extends SHA2_64B {
|
|
1395
|
+
Ah = SHA512_IV[0] | 0;
|
|
1396
|
+
Al = SHA512_IV[1] | 0;
|
|
1397
|
+
Bh = SHA512_IV[2] | 0;
|
|
1398
|
+
Bl = SHA512_IV[3] | 0;
|
|
1399
|
+
Ch = SHA512_IV[4] | 0;
|
|
1400
|
+
Cl = SHA512_IV[5] | 0;
|
|
1401
|
+
Dh = SHA512_IV[6] | 0;
|
|
1402
|
+
Dl = SHA512_IV[7] | 0;
|
|
1403
|
+
Eh = SHA512_IV[8] | 0;
|
|
1404
|
+
El = SHA512_IV[9] | 0;
|
|
1405
|
+
Fh = SHA512_IV[10] | 0;
|
|
1406
|
+
Fl = SHA512_IV[11] | 0;
|
|
1407
|
+
Gh = SHA512_IV[12] | 0;
|
|
1408
|
+
Gl = SHA512_IV[13] | 0;
|
|
1409
|
+
Hh = SHA512_IV[14] | 0;
|
|
1410
|
+
Hl = SHA512_IV[15] | 0;
|
|
1411
|
+
constructor() {
|
|
1412
|
+
super(64);
|
|
1413
|
+
}
|
|
1414
|
+
};
|
|
1415
|
+
var sha256 = /* @__PURE__ */ createHasher(
|
|
1416
|
+
() => new _SHA256(),
|
|
1417
|
+
/* @__PURE__ */ oidNist(1)
|
|
1418
|
+
);
|
|
1419
|
+
var sha512 = /* @__PURE__ */ createHasher(
|
|
1420
|
+
() => new _SHA512(),
|
|
1421
|
+
/* @__PURE__ */ oidNist(3)
|
|
1422
|
+
);
|
|
1423
|
+
|
|
1424
|
+
// node_modules/@noble/curves/utils.js
|
|
1425
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
1426
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
1427
|
+
function abool(value, title = "") {
|
|
1428
|
+
if (typeof value !== "boolean") {
|
|
1429
|
+
const prefix = title && `"${title}" `;
|
|
1430
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
1431
|
+
}
|
|
1432
|
+
return value;
|
|
1433
|
+
}
|
|
1434
|
+
function abignumber(n) {
|
|
1435
|
+
if (typeof n === "bigint") {
|
|
1436
|
+
if (!isPosBig(n))
|
|
1437
|
+
throw new Error("positive bigint expected, got " + n);
|
|
1438
|
+
} else
|
|
1439
|
+
anumber(n);
|
|
1440
|
+
return n;
|
|
1441
|
+
}
|
|
1442
|
+
function numberToHexUnpadded(num2) {
|
|
1443
|
+
const hex = abignumber(num2).toString(16);
|
|
1444
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
1445
|
+
}
|
|
1446
|
+
function hexToNumber(hex) {
|
|
1447
|
+
if (typeof hex !== "string")
|
|
1448
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
1449
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
1450
|
+
}
|
|
1451
|
+
function bytesToNumberBE(bytes) {
|
|
1452
|
+
return hexToNumber(bytesToHex(bytes));
|
|
1453
|
+
}
|
|
1454
|
+
function bytesToNumberLE(bytes) {
|
|
1455
|
+
return hexToNumber(bytesToHex(copyBytes(abytes(bytes)).reverse()));
|
|
1456
|
+
}
|
|
1457
|
+
function numberToBytesBE(n, len) {
|
|
1458
|
+
anumber(len);
|
|
1459
|
+
n = abignumber(n);
|
|
1460
|
+
const res = hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
1461
|
+
if (res.length !== len)
|
|
1462
|
+
throw new Error("number too large");
|
|
1463
|
+
return res;
|
|
1464
|
+
}
|
|
1465
|
+
function numberToBytesLE(n, len) {
|
|
1466
|
+
return numberToBytesBE(n, len).reverse();
|
|
1467
|
+
}
|
|
1468
|
+
function copyBytes(bytes) {
|
|
1469
|
+
return Uint8Array.from(bytes);
|
|
1470
|
+
}
|
|
1471
|
+
function asciiToBytes(ascii) {
|
|
1472
|
+
return Uint8Array.from(ascii, (c, i) => {
|
|
1473
|
+
const charCode = c.charCodeAt(0);
|
|
1474
|
+
if (c.length !== 1 || charCode > 127) {
|
|
1475
|
+
throw new Error(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
1476
|
+
}
|
|
1477
|
+
return charCode;
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
1481
|
+
function inRange(n, min, max) {
|
|
1482
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
1483
|
+
}
|
|
1484
|
+
function aInRange(title, n, min, max) {
|
|
1485
|
+
if (!inRange(n, min, max))
|
|
1486
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
1487
|
+
}
|
|
1488
|
+
function bitLen(n) {
|
|
1489
|
+
let len;
|
|
1490
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
1491
|
+
;
|
|
1492
|
+
return len;
|
|
1493
|
+
}
|
|
1494
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
1495
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
1496
|
+
anumber(hashLen, "hashLen");
|
|
1497
|
+
anumber(qByteLen, "qByteLen");
|
|
1498
|
+
if (typeof hmacFn !== "function")
|
|
1499
|
+
throw new Error("hmacFn must be a function");
|
|
1500
|
+
const u8n = (len) => new Uint8Array(len);
|
|
1501
|
+
const NULL = Uint8Array.of();
|
|
1502
|
+
const byte0 = Uint8Array.of(0);
|
|
1503
|
+
const byte1 = Uint8Array.of(1);
|
|
1504
|
+
const _maxDrbgIters = 1e3;
|
|
1505
|
+
let v = u8n(hashLen);
|
|
1506
|
+
let k = u8n(hashLen);
|
|
1507
|
+
let i = 0;
|
|
1508
|
+
const reset = () => {
|
|
1509
|
+
v.fill(1);
|
|
1510
|
+
k.fill(0);
|
|
1511
|
+
i = 0;
|
|
1512
|
+
};
|
|
1513
|
+
const h = (...msgs) => hmacFn(k, concatBytes(v, ...msgs));
|
|
1514
|
+
const reseed = (seed = NULL) => {
|
|
1515
|
+
k = h(byte0, seed);
|
|
1516
|
+
v = h();
|
|
1517
|
+
if (seed.length === 0)
|
|
1518
|
+
return;
|
|
1519
|
+
k = h(byte1, seed);
|
|
1520
|
+
v = h();
|
|
1521
|
+
};
|
|
1522
|
+
const gen = () => {
|
|
1523
|
+
if (i++ >= _maxDrbgIters)
|
|
1524
|
+
throw new Error("drbg: tried max amount of iterations");
|
|
1525
|
+
let len = 0;
|
|
1526
|
+
const out = [];
|
|
1527
|
+
while (len < qByteLen) {
|
|
1528
|
+
v = h();
|
|
1529
|
+
const sl = v.slice();
|
|
1530
|
+
out.push(sl);
|
|
1531
|
+
len += v.length;
|
|
1532
|
+
}
|
|
1533
|
+
return concatBytes(...out);
|
|
1534
|
+
};
|
|
1535
|
+
const genUntil = (seed, pred) => {
|
|
1536
|
+
reset();
|
|
1537
|
+
reseed(seed);
|
|
1538
|
+
let res = void 0;
|
|
1539
|
+
while (!(res = pred(gen())))
|
|
1540
|
+
reseed();
|
|
1541
|
+
reset();
|
|
1542
|
+
return res;
|
|
1543
|
+
};
|
|
1544
|
+
return genUntil;
|
|
1545
|
+
}
|
|
1546
|
+
function validateObject(object, fields = {}, optFields = {}) {
|
|
1547
|
+
if (!object || typeof object !== "object")
|
|
1548
|
+
throw new Error("expected valid options object");
|
|
1549
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
1550
|
+
const val = object[fieldName];
|
|
1551
|
+
if (isOpt && val === void 0)
|
|
1552
|
+
return;
|
|
1553
|
+
const current = typeof val;
|
|
1554
|
+
if (current !== expectedType || val === null)
|
|
1555
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
1556
|
+
}
|
|
1557
|
+
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
1558
|
+
iter(fields, false);
|
|
1559
|
+
iter(optFields, true);
|
|
1560
|
+
}
|
|
1561
|
+
function memoized(fn) {
|
|
1562
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
1563
|
+
return (arg, ...args) => {
|
|
1564
|
+
const val = map.get(arg);
|
|
1565
|
+
if (val !== void 0)
|
|
1566
|
+
return val;
|
|
1567
|
+
const computed = fn(arg, ...args);
|
|
1568
|
+
map.set(arg, computed);
|
|
1569
|
+
return computed;
|
|
1570
|
+
};
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
// node_modules/@noble/curves/abstract/modular.js
|
|
1574
|
+
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
1575
|
+
var _1n2 = /* @__PURE__ */ BigInt(1);
|
|
1576
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
1577
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
1578
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
1579
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
1580
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
1581
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
1582
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
1583
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
1584
|
+
function mod(a, b) {
|
|
1585
|
+
const result = a % b;
|
|
1586
|
+
return result >= _0n2 ? result : b + result;
|
|
1587
|
+
}
|
|
1588
|
+
function pow2(x, power, modulo) {
|
|
1589
|
+
let res = x;
|
|
1590
|
+
while (power-- > _0n2) {
|
|
1591
|
+
res *= res;
|
|
1592
|
+
res %= modulo;
|
|
1593
|
+
}
|
|
1594
|
+
return res;
|
|
1595
|
+
}
|
|
1596
|
+
function invert(number, modulo) {
|
|
1597
|
+
if (number === _0n2)
|
|
1598
|
+
throw new Error("invert: expected non-zero number");
|
|
1599
|
+
if (modulo <= _0n2)
|
|
1600
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
1601
|
+
let a = mod(number, modulo);
|
|
1602
|
+
let b = modulo;
|
|
1603
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
1604
|
+
while (a !== _0n2) {
|
|
1605
|
+
const q = b / a;
|
|
1606
|
+
const r = b % a;
|
|
1607
|
+
const m = x - u * q;
|
|
1608
|
+
const n = y - v * q;
|
|
1609
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
1610
|
+
}
|
|
1611
|
+
const gcd = b;
|
|
1612
|
+
if (gcd !== _1n2)
|
|
1613
|
+
throw new Error("invert: does not exist");
|
|
1614
|
+
return mod(x, modulo);
|
|
1615
|
+
}
|
|
1616
|
+
function assertIsSquare(Fp, root, n) {
|
|
1617
|
+
if (!Fp.eql(Fp.sqr(root), n))
|
|
1618
|
+
throw new Error("Cannot find square root");
|
|
1619
|
+
}
|
|
1620
|
+
function sqrt3mod4(Fp, n) {
|
|
1621
|
+
const p1div4 = (Fp.ORDER + _1n2) / _4n;
|
|
1622
|
+
const root = Fp.pow(n, p1div4);
|
|
1623
|
+
assertIsSquare(Fp, root, n);
|
|
1624
|
+
return root;
|
|
1625
|
+
}
|
|
1626
|
+
function sqrt5mod8(Fp, n) {
|
|
1627
|
+
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
|
1628
|
+
const n2 = Fp.mul(n, _2n);
|
|
1629
|
+
const v = Fp.pow(n2, p5div8);
|
|
1630
|
+
const nv = Fp.mul(n, v);
|
|
1631
|
+
const i = Fp.mul(Fp.mul(nv, _2n), v);
|
|
1632
|
+
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
1633
|
+
assertIsSquare(Fp, root, n);
|
|
1634
|
+
return root;
|
|
1635
|
+
}
|
|
1636
|
+
function sqrt9mod16(P) {
|
|
1637
|
+
const Fp_ = Field(P);
|
|
1638
|
+
const tn = tonelliShanks(P);
|
|
1639
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
1640
|
+
const c2 = tn(Fp_, c1);
|
|
1641
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
1642
|
+
const c4 = (P + _7n) / _16n;
|
|
1643
|
+
return (Fp, n) => {
|
|
1644
|
+
let tv1 = Fp.pow(n, c4);
|
|
1645
|
+
let tv2 = Fp.mul(tv1, c1);
|
|
1646
|
+
const tv3 = Fp.mul(tv1, c2);
|
|
1647
|
+
const tv4 = Fp.mul(tv1, c3);
|
|
1648
|
+
const e1 = Fp.eql(Fp.sqr(tv2), n);
|
|
1649
|
+
const e2 = Fp.eql(Fp.sqr(tv3), n);
|
|
1650
|
+
tv1 = Fp.cmov(tv1, tv2, e1);
|
|
1651
|
+
tv2 = Fp.cmov(tv4, tv3, e2);
|
|
1652
|
+
const e3 = Fp.eql(Fp.sqr(tv2), n);
|
|
1653
|
+
const root = Fp.cmov(tv1, tv2, e3);
|
|
1654
|
+
assertIsSquare(Fp, root, n);
|
|
1655
|
+
return root;
|
|
1656
|
+
};
|
|
1657
|
+
}
|
|
1658
|
+
function tonelliShanks(P) {
|
|
1659
|
+
if (P < _3n)
|
|
1660
|
+
throw new Error("sqrt is not defined for small field");
|
|
1661
|
+
let Q = P - _1n2;
|
|
1662
|
+
let S = 0;
|
|
1663
|
+
while (Q % _2n === _0n2) {
|
|
1664
|
+
Q /= _2n;
|
|
1665
|
+
S++;
|
|
1666
|
+
}
|
|
1667
|
+
let Z = _2n;
|
|
1668
|
+
const _Fp = Field(P);
|
|
1669
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
1670
|
+
if (Z++ > 1e3)
|
|
1671
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
1672
|
+
}
|
|
1673
|
+
if (S === 1)
|
|
1674
|
+
return sqrt3mod4;
|
|
1675
|
+
let cc = _Fp.pow(Z, Q);
|
|
1676
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
1677
|
+
return function tonelliSlow(Fp, n) {
|
|
1678
|
+
if (Fp.is0(n))
|
|
1679
|
+
return n;
|
|
1680
|
+
if (FpLegendre(Fp, n) !== 1)
|
|
1681
|
+
throw new Error("Cannot find square root");
|
|
1682
|
+
let M = S;
|
|
1683
|
+
let c = Fp.mul(Fp.ONE, cc);
|
|
1684
|
+
let t = Fp.pow(n, Q);
|
|
1685
|
+
let R = Fp.pow(n, Q1div2);
|
|
1686
|
+
while (!Fp.eql(t, Fp.ONE)) {
|
|
1687
|
+
if (Fp.is0(t))
|
|
1688
|
+
return Fp.ZERO;
|
|
1689
|
+
let i = 1;
|
|
1690
|
+
let t_tmp = Fp.sqr(t);
|
|
1691
|
+
while (!Fp.eql(t_tmp, Fp.ONE)) {
|
|
1692
|
+
i++;
|
|
1693
|
+
t_tmp = Fp.sqr(t_tmp);
|
|
1694
|
+
if (i === M)
|
|
1695
|
+
throw new Error("Cannot find square root");
|
|
1696
|
+
}
|
|
1697
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
1698
|
+
const b = Fp.pow(c, exponent);
|
|
1699
|
+
M = i;
|
|
1700
|
+
c = Fp.sqr(b);
|
|
1701
|
+
t = Fp.mul(t, c);
|
|
1702
|
+
R = Fp.mul(R, b);
|
|
1703
|
+
}
|
|
1704
|
+
return R;
|
|
1705
|
+
};
|
|
1706
|
+
}
|
|
1707
|
+
function FpSqrt(P) {
|
|
1708
|
+
if (P % _4n === _3n)
|
|
1709
|
+
return sqrt3mod4;
|
|
1710
|
+
if (P % _8n === _5n)
|
|
1711
|
+
return sqrt5mod8;
|
|
1712
|
+
if (P % _16n === _9n)
|
|
1713
|
+
return sqrt9mod16(P);
|
|
1714
|
+
return tonelliShanks(P);
|
|
1715
|
+
}
|
|
1716
|
+
var FIELD_FIELDS = [
|
|
1717
|
+
"create",
|
|
1718
|
+
"isValid",
|
|
1719
|
+
"is0",
|
|
1720
|
+
"neg",
|
|
1721
|
+
"inv",
|
|
1722
|
+
"sqrt",
|
|
1723
|
+
"sqr",
|
|
1724
|
+
"eql",
|
|
1725
|
+
"add",
|
|
1726
|
+
"sub",
|
|
1727
|
+
"mul",
|
|
1728
|
+
"pow",
|
|
1729
|
+
"div",
|
|
1730
|
+
"addN",
|
|
1731
|
+
"subN",
|
|
1732
|
+
"mulN",
|
|
1733
|
+
"sqrN"
|
|
1734
|
+
];
|
|
1735
|
+
function validateField(field) {
|
|
1736
|
+
const initial = {
|
|
1737
|
+
ORDER: "bigint",
|
|
1738
|
+
BYTES: "number",
|
|
1739
|
+
BITS: "number"
|
|
1740
|
+
};
|
|
1741
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
1742
|
+
map[val] = "function";
|
|
1743
|
+
return map;
|
|
1744
|
+
}, initial);
|
|
1745
|
+
validateObject(field, opts);
|
|
1746
|
+
return field;
|
|
1747
|
+
}
|
|
1748
|
+
function FpPow(Fp, num2, power) {
|
|
1749
|
+
if (power < _0n2)
|
|
1750
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
1751
|
+
if (power === _0n2)
|
|
1752
|
+
return Fp.ONE;
|
|
1753
|
+
if (power === _1n2)
|
|
1754
|
+
return num2;
|
|
1755
|
+
let p = Fp.ONE;
|
|
1756
|
+
let d = num2;
|
|
1757
|
+
while (power > _0n2) {
|
|
1758
|
+
if (power & _1n2)
|
|
1759
|
+
p = Fp.mul(p, d);
|
|
1760
|
+
d = Fp.sqr(d);
|
|
1761
|
+
power >>= _1n2;
|
|
1762
|
+
}
|
|
1763
|
+
return p;
|
|
1764
|
+
}
|
|
1765
|
+
function FpInvertBatch(Fp, nums, passZero = false) {
|
|
1766
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
|
|
1767
|
+
const multipliedAcc = nums.reduce((acc, num2, i) => {
|
|
1768
|
+
if (Fp.is0(num2))
|
|
1769
|
+
return acc;
|
|
1770
|
+
inverted[i] = acc;
|
|
1771
|
+
return Fp.mul(acc, num2);
|
|
1772
|
+
}, Fp.ONE);
|
|
1773
|
+
const invertedAcc = Fp.inv(multipliedAcc);
|
|
1774
|
+
nums.reduceRight((acc, num2, i) => {
|
|
1775
|
+
if (Fp.is0(num2))
|
|
1776
|
+
return acc;
|
|
1777
|
+
inverted[i] = Fp.mul(acc, inverted[i]);
|
|
1778
|
+
return Fp.mul(acc, num2);
|
|
1779
|
+
}, invertedAcc);
|
|
1780
|
+
return inverted;
|
|
1781
|
+
}
|
|
1782
|
+
function FpLegendre(Fp, n) {
|
|
1783
|
+
const p1mod2 = (Fp.ORDER - _1n2) / _2n;
|
|
1784
|
+
const powered = Fp.pow(n, p1mod2);
|
|
1785
|
+
const yes = Fp.eql(powered, Fp.ONE);
|
|
1786
|
+
const zero = Fp.eql(powered, Fp.ZERO);
|
|
1787
|
+
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
1788
|
+
if (!yes && !zero && !no)
|
|
1789
|
+
throw new Error("invalid Legendre symbol result");
|
|
1790
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
1791
|
+
}
|
|
1792
|
+
function nLength(n, nBitLength) {
|
|
1793
|
+
if (nBitLength !== void 0)
|
|
1794
|
+
anumber(nBitLength);
|
|
1795
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
1796
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
1797
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
1798
|
+
}
|
|
1799
|
+
var _Field = class {
|
|
1800
|
+
ORDER;
|
|
1801
|
+
BITS;
|
|
1802
|
+
BYTES;
|
|
1803
|
+
isLE;
|
|
1804
|
+
ZERO = _0n2;
|
|
1805
|
+
ONE = _1n2;
|
|
1806
|
+
_lengths;
|
|
1807
|
+
_sqrt;
|
|
1808
|
+
// cached sqrt
|
|
1809
|
+
_mod;
|
|
1810
|
+
constructor(ORDER, opts = {}) {
|
|
1811
|
+
if (ORDER <= _0n2)
|
|
1812
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
1813
|
+
let _nbitLength = void 0;
|
|
1814
|
+
this.isLE = false;
|
|
1815
|
+
if (opts != null && typeof opts === "object") {
|
|
1816
|
+
if (typeof opts.BITS === "number")
|
|
1817
|
+
_nbitLength = opts.BITS;
|
|
1818
|
+
if (typeof opts.sqrt === "function")
|
|
1819
|
+
this.sqrt = opts.sqrt;
|
|
1820
|
+
if (typeof opts.isLE === "boolean")
|
|
1821
|
+
this.isLE = opts.isLE;
|
|
1822
|
+
if (opts.allowedLengths)
|
|
1823
|
+
this._lengths = opts.allowedLengths?.slice();
|
|
1824
|
+
if (typeof opts.modFromBytes === "boolean")
|
|
1825
|
+
this._mod = opts.modFromBytes;
|
|
1826
|
+
}
|
|
1827
|
+
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
1828
|
+
if (nByteLength > 2048)
|
|
1829
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
1830
|
+
this.ORDER = ORDER;
|
|
1831
|
+
this.BITS = nBitLength;
|
|
1832
|
+
this.BYTES = nByteLength;
|
|
1833
|
+
this._sqrt = void 0;
|
|
1834
|
+
Object.preventExtensions(this);
|
|
1835
|
+
}
|
|
1836
|
+
create(num2) {
|
|
1837
|
+
return mod(num2, this.ORDER);
|
|
1838
|
+
}
|
|
1839
|
+
isValid(num2) {
|
|
1840
|
+
if (typeof num2 !== "bigint")
|
|
1841
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num2);
|
|
1842
|
+
return _0n2 <= num2 && num2 < this.ORDER;
|
|
1843
|
+
}
|
|
1844
|
+
is0(num2) {
|
|
1845
|
+
return num2 === _0n2;
|
|
1846
|
+
}
|
|
1847
|
+
// is valid and invertible
|
|
1848
|
+
isValidNot0(num2) {
|
|
1849
|
+
return !this.is0(num2) && this.isValid(num2);
|
|
1850
|
+
}
|
|
1851
|
+
isOdd(num2) {
|
|
1852
|
+
return (num2 & _1n2) === _1n2;
|
|
1853
|
+
}
|
|
1854
|
+
neg(num2) {
|
|
1855
|
+
return mod(-num2, this.ORDER);
|
|
1856
|
+
}
|
|
1857
|
+
eql(lhs, rhs) {
|
|
1858
|
+
return lhs === rhs;
|
|
1859
|
+
}
|
|
1860
|
+
sqr(num2) {
|
|
1861
|
+
return mod(num2 * num2, this.ORDER);
|
|
1862
|
+
}
|
|
1863
|
+
add(lhs, rhs) {
|
|
1864
|
+
return mod(lhs + rhs, this.ORDER);
|
|
1865
|
+
}
|
|
1866
|
+
sub(lhs, rhs) {
|
|
1867
|
+
return mod(lhs - rhs, this.ORDER);
|
|
1868
|
+
}
|
|
1869
|
+
mul(lhs, rhs) {
|
|
1870
|
+
return mod(lhs * rhs, this.ORDER);
|
|
1871
|
+
}
|
|
1872
|
+
pow(num2, power) {
|
|
1873
|
+
return FpPow(this, num2, power);
|
|
1874
|
+
}
|
|
1875
|
+
div(lhs, rhs) {
|
|
1876
|
+
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
1877
|
+
}
|
|
1878
|
+
// Same as above, but doesn't normalize
|
|
1879
|
+
sqrN(num2) {
|
|
1880
|
+
return num2 * num2;
|
|
1881
|
+
}
|
|
1882
|
+
addN(lhs, rhs) {
|
|
1883
|
+
return lhs + rhs;
|
|
1884
|
+
}
|
|
1885
|
+
subN(lhs, rhs) {
|
|
1886
|
+
return lhs - rhs;
|
|
1887
|
+
}
|
|
1888
|
+
mulN(lhs, rhs) {
|
|
1889
|
+
return lhs * rhs;
|
|
1890
|
+
}
|
|
1891
|
+
inv(num2) {
|
|
1892
|
+
return invert(num2, this.ORDER);
|
|
1893
|
+
}
|
|
1894
|
+
sqrt(num2) {
|
|
1895
|
+
if (!this._sqrt)
|
|
1896
|
+
this._sqrt = FpSqrt(this.ORDER);
|
|
1897
|
+
return this._sqrt(this, num2);
|
|
1898
|
+
}
|
|
1899
|
+
toBytes(num2) {
|
|
1900
|
+
return this.isLE ? numberToBytesLE(num2, this.BYTES) : numberToBytesBE(num2, this.BYTES);
|
|
1901
|
+
}
|
|
1902
|
+
fromBytes(bytes, skipValidation = false) {
|
|
1903
|
+
abytes(bytes);
|
|
1904
|
+
const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
1905
|
+
if (allowedLengths) {
|
|
1906
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
1907
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
1908
|
+
}
|
|
1909
|
+
const padded = new Uint8Array(BYTES);
|
|
1910
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
1911
|
+
bytes = padded;
|
|
1912
|
+
}
|
|
1913
|
+
if (bytes.length !== BYTES)
|
|
1914
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
1915
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
1916
|
+
if (modFromBytes)
|
|
1917
|
+
scalar = mod(scalar, ORDER);
|
|
1918
|
+
if (!skipValidation) {
|
|
1919
|
+
if (!this.isValid(scalar))
|
|
1920
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
1921
|
+
}
|
|
1922
|
+
return scalar;
|
|
1923
|
+
}
|
|
1924
|
+
// TODO: we don't need it here, move out to separate fn
|
|
1925
|
+
invertBatch(lst) {
|
|
1926
|
+
return FpInvertBatch(this, lst);
|
|
1927
|
+
}
|
|
1928
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
1929
|
+
// and it's unclear what to return in there.
|
|
1930
|
+
cmov(a, b, condition) {
|
|
1931
|
+
return condition ? b : a;
|
|
1932
|
+
}
|
|
1933
|
+
};
|
|
1934
|
+
function Field(ORDER, opts = {}) {
|
|
1935
|
+
return new _Field(ORDER, opts);
|
|
1936
|
+
}
|
|
1937
|
+
function getFieldBytesLength(fieldOrder) {
|
|
1938
|
+
if (typeof fieldOrder !== "bigint")
|
|
1939
|
+
throw new Error("field order must be bigint");
|
|
1940
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
1941
|
+
return Math.ceil(bitLength / 8);
|
|
1942
|
+
}
|
|
1943
|
+
function getMinHashLength(fieldOrder) {
|
|
1944
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
1945
|
+
return length + Math.ceil(length / 2);
|
|
1946
|
+
}
|
|
1947
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
1948
|
+
abytes(key);
|
|
1949
|
+
const len = key.length;
|
|
1950
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
1951
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
1952
|
+
if (len < 16 || len < minLen || len > 1024)
|
|
1953
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
1954
|
+
const num2 = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
1955
|
+
const reduced = mod(num2, fieldOrder - _1n2) + _1n2;
|
|
1956
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
// node_modules/@noble/curves/abstract/curve.js
|
|
1960
|
+
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
1961
|
+
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
1962
|
+
function negateCt(condition, item) {
|
|
1963
|
+
const neg = item.negate();
|
|
1964
|
+
return condition ? neg : item;
|
|
1965
|
+
}
|
|
1966
|
+
function normalizeZ(c, points) {
|
|
1967
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
1968
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
1969
|
+
}
|
|
1970
|
+
function validateW(W, bits) {
|
|
1971
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
1972
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
1973
|
+
}
|
|
1974
|
+
function calcWOpts(W, scalarBits) {
|
|
1975
|
+
validateW(W, scalarBits);
|
|
1976
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
1977
|
+
const windowSize = 2 ** (W - 1);
|
|
1978
|
+
const maxNumber = 2 ** W;
|
|
1979
|
+
const mask = bitMask(W);
|
|
1980
|
+
const shiftBy = BigInt(W);
|
|
1981
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
1982
|
+
}
|
|
1983
|
+
function calcOffsets(n, window, wOpts) {
|
|
1984
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
1985
|
+
let wbits = Number(n & mask);
|
|
1986
|
+
let nextN = n >> shiftBy;
|
|
1987
|
+
if (wbits > windowSize) {
|
|
1988
|
+
wbits -= maxNumber;
|
|
1989
|
+
nextN += _1n3;
|
|
1990
|
+
}
|
|
1991
|
+
const offsetStart = window * windowSize;
|
|
1992
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
1993
|
+
const isZero = wbits === 0;
|
|
1994
|
+
const isNeg = wbits < 0;
|
|
1995
|
+
const isNegF = window % 2 !== 0;
|
|
1996
|
+
const offsetF = offsetStart;
|
|
1997
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
1998
|
+
}
|
|
1999
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
2000
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
2001
|
+
function getW(P) {
|
|
2002
|
+
return pointWindowSizes.get(P) || 1;
|
|
2003
|
+
}
|
|
2004
|
+
function assert0(n) {
|
|
2005
|
+
if (n !== _0n3)
|
|
2006
|
+
throw new Error("invalid wNAF");
|
|
2007
|
+
}
|
|
2008
|
+
var wNAF = class {
|
|
2009
|
+
BASE;
|
|
2010
|
+
ZERO;
|
|
2011
|
+
Fn;
|
|
2012
|
+
bits;
|
|
2013
|
+
// Parametrized with a given Point class (not individual point)
|
|
2014
|
+
constructor(Point, bits) {
|
|
2015
|
+
this.BASE = Point.BASE;
|
|
2016
|
+
this.ZERO = Point.ZERO;
|
|
2017
|
+
this.Fn = Point.Fn;
|
|
2018
|
+
this.bits = bits;
|
|
2019
|
+
}
|
|
2020
|
+
// non-const time multiplication ladder
|
|
2021
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
2022
|
+
let d = elm;
|
|
2023
|
+
while (n > _0n3) {
|
|
2024
|
+
if (n & _1n3)
|
|
2025
|
+
p = p.add(d);
|
|
2026
|
+
d = d.double();
|
|
2027
|
+
n >>= _1n3;
|
|
2028
|
+
}
|
|
2029
|
+
return p;
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
2033
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
2034
|
+
* Number of precomputed points depends on the curve size:
|
|
2035
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
2036
|
+
* - 𝑊 is the window size
|
|
2037
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
2038
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
2039
|
+
* @param point Point instance
|
|
2040
|
+
* @param W window size
|
|
2041
|
+
* @returns precomputed point tables flattened to a single array
|
|
2042
|
+
*/
|
|
2043
|
+
precomputeWindow(point, W) {
|
|
2044
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
2045
|
+
const points = [];
|
|
2046
|
+
let p = point;
|
|
2047
|
+
let base = p;
|
|
2048
|
+
for (let window = 0; window < windows; window++) {
|
|
2049
|
+
base = p;
|
|
2050
|
+
points.push(base);
|
|
2051
|
+
for (let i = 1; i < windowSize; i++) {
|
|
2052
|
+
base = base.add(p);
|
|
2053
|
+
points.push(base);
|
|
2054
|
+
}
|
|
2055
|
+
p = base.double();
|
|
2056
|
+
}
|
|
2057
|
+
return points;
|
|
2058
|
+
}
|
|
2059
|
+
/**
|
|
2060
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2061
|
+
* More compact implementation:
|
|
2062
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
2063
|
+
* @returns real and fake (for const-time) points
|
|
2064
|
+
*/
|
|
2065
|
+
wNAF(W, precomputes, n) {
|
|
2066
|
+
if (!this.Fn.isValid(n))
|
|
2067
|
+
throw new Error("invalid scalar");
|
|
2068
|
+
let p = this.ZERO;
|
|
2069
|
+
let f = this.BASE;
|
|
2070
|
+
const wo = calcWOpts(W, this.bits);
|
|
2071
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
2072
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
2073
|
+
n = nextN;
|
|
2074
|
+
if (isZero) {
|
|
2075
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
2076
|
+
} else {
|
|
2077
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
assert0(n);
|
|
2081
|
+
return { p, f };
|
|
2082
|
+
}
|
|
2083
|
+
/**
|
|
2084
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
2085
|
+
* @param acc accumulator point to add result of multiplication
|
|
2086
|
+
* @returns point
|
|
2087
|
+
*/
|
|
2088
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
2089
|
+
const wo = calcWOpts(W, this.bits);
|
|
2090
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
2091
|
+
if (n === _0n3)
|
|
2092
|
+
break;
|
|
2093
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
2094
|
+
n = nextN;
|
|
2095
|
+
if (isZero) {
|
|
2096
|
+
continue;
|
|
2097
|
+
} else {
|
|
2098
|
+
const item = precomputes[offset];
|
|
2099
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
assert0(n);
|
|
2103
|
+
return acc;
|
|
2104
|
+
}
|
|
2105
|
+
getPrecomputes(W, point, transform) {
|
|
2106
|
+
let comp = pointPrecomputes.get(point);
|
|
2107
|
+
if (!comp) {
|
|
2108
|
+
comp = this.precomputeWindow(point, W);
|
|
2109
|
+
if (W !== 1) {
|
|
2110
|
+
if (typeof transform === "function")
|
|
2111
|
+
comp = transform(comp);
|
|
2112
|
+
pointPrecomputes.set(point, comp);
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
return comp;
|
|
2116
|
+
}
|
|
2117
|
+
cached(point, scalar, transform) {
|
|
2118
|
+
const W = getW(point);
|
|
2119
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
2120
|
+
}
|
|
2121
|
+
unsafe(point, scalar, transform, prev) {
|
|
2122
|
+
const W = getW(point);
|
|
2123
|
+
if (W === 1)
|
|
2124
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
2125
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
2126
|
+
}
|
|
2127
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
2128
|
+
// using windowed method. This specifies window size and
|
|
2129
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
2130
|
+
createCache(P, W) {
|
|
2131
|
+
validateW(W, this.bits);
|
|
2132
|
+
pointWindowSizes.set(P, W);
|
|
2133
|
+
pointPrecomputes.delete(P);
|
|
2134
|
+
}
|
|
2135
|
+
hasCache(elm) {
|
|
2136
|
+
return getW(elm) !== 1;
|
|
2137
|
+
}
|
|
2138
|
+
};
|
|
2139
|
+
function mulEndoUnsafe(Point, point, k1, k2) {
|
|
2140
|
+
let acc = point;
|
|
2141
|
+
let p1 = Point.ZERO;
|
|
2142
|
+
let p2 = Point.ZERO;
|
|
2143
|
+
while (k1 > _0n3 || k2 > _0n3) {
|
|
2144
|
+
if (k1 & _1n3)
|
|
2145
|
+
p1 = p1.add(acc);
|
|
2146
|
+
if (k2 & _1n3)
|
|
2147
|
+
p2 = p2.add(acc);
|
|
2148
|
+
acc = acc.double();
|
|
2149
|
+
k1 >>= _1n3;
|
|
2150
|
+
k2 >>= _1n3;
|
|
2151
|
+
}
|
|
2152
|
+
return { p1, p2 };
|
|
2153
|
+
}
|
|
2154
|
+
function createField(order, field, isLE) {
|
|
2155
|
+
if (field) {
|
|
2156
|
+
if (field.ORDER !== order)
|
|
2157
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
2158
|
+
validateField(field);
|
|
2159
|
+
return field;
|
|
2160
|
+
} else {
|
|
2161
|
+
return Field(order, { isLE });
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
2165
|
+
if (FpFnLE === void 0)
|
|
2166
|
+
FpFnLE = type === "edwards";
|
|
2167
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
2168
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
2169
|
+
for (const p of ["p", "n", "h"]) {
|
|
2170
|
+
const val = CURVE[p];
|
|
2171
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
2172
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
2173
|
+
}
|
|
2174
|
+
const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
2175
|
+
const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
2176
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
2177
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
2178
|
+
for (const p of params) {
|
|
2179
|
+
if (!Fp.isValid(CURVE[p]))
|
|
2180
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
2181
|
+
}
|
|
2182
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
2183
|
+
return { CURVE, Fp, Fn };
|
|
2184
|
+
}
|
|
2185
|
+
function createKeygen(randomSecretKey, getPublicKey) {
|
|
2186
|
+
return function keygen(seed) {
|
|
2187
|
+
const secretKey = randomSecretKey(seed);
|
|
2188
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
2189
|
+
};
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
// node_modules/@noble/hashes/hmac.js
|
|
2193
|
+
var _HMAC = class {
|
|
2194
|
+
oHash;
|
|
2195
|
+
iHash;
|
|
2196
|
+
blockLen;
|
|
2197
|
+
outputLen;
|
|
2198
|
+
finished = false;
|
|
2199
|
+
destroyed = false;
|
|
2200
|
+
constructor(hash, key) {
|
|
2201
|
+
ahash(hash);
|
|
2202
|
+
abytes(key, void 0, "key");
|
|
2203
|
+
this.iHash = hash.create();
|
|
2204
|
+
if (typeof this.iHash.update !== "function")
|
|
2205
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
2206
|
+
this.blockLen = this.iHash.blockLen;
|
|
2207
|
+
this.outputLen = this.iHash.outputLen;
|
|
2208
|
+
const blockLen = this.blockLen;
|
|
2209
|
+
const pad = new Uint8Array(blockLen);
|
|
2210
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
2211
|
+
for (let i = 0; i < pad.length; i++)
|
|
2212
|
+
pad[i] ^= 54;
|
|
2213
|
+
this.iHash.update(pad);
|
|
2214
|
+
this.oHash = hash.create();
|
|
2215
|
+
for (let i = 0; i < pad.length; i++)
|
|
2216
|
+
pad[i] ^= 54 ^ 92;
|
|
2217
|
+
this.oHash.update(pad);
|
|
2218
|
+
clean(pad);
|
|
2219
|
+
}
|
|
2220
|
+
update(buf) {
|
|
2221
|
+
aexists(this);
|
|
2222
|
+
this.iHash.update(buf);
|
|
2223
|
+
return this;
|
|
2224
|
+
}
|
|
2225
|
+
digestInto(out) {
|
|
2226
|
+
aexists(this);
|
|
2227
|
+
abytes(out, this.outputLen, "output");
|
|
2228
|
+
this.finished = true;
|
|
2229
|
+
this.iHash.digestInto(out);
|
|
2230
|
+
this.oHash.update(out);
|
|
2231
|
+
this.oHash.digestInto(out);
|
|
2232
|
+
this.destroy();
|
|
2233
|
+
}
|
|
2234
|
+
digest() {
|
|
2235
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
2236
|
+
this.digestInto(out);
|
|
2237
|
+
return out;
|
|
2238
|
+
}
|
|
2239
|
+
_cloneInto(to) {
|
|
2240
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
2241
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
2242
|
+
to = to;
|
|
2243
|
+
to.finished = finished;
|
|
2244
|
+
to.destroyed = destroyed;
|
|
2245
|
+
to.blockLen = blockLen;
|
|
2246
|
+
to.outputLen = outputLen;
|
|
2247
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
2248
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
2249
|
+
return to;
|
|
2250
|
+
}
|
|
2251
|
+
clone() {
|
|
2252
|
+
return this._cloneInto();
|
|
2253
|
+
}
|
|
2254
|
+
destroy() {
|
|
2255
|
+
this.destroyed = true;
|
|
2256
|
+
this.oHash.destroy();
|
|
2257
|
+
this.iHash.destroy();
|
|
2258
|
+
}
|
|
2259
|
+
};
|
|
2260
|
+
var hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
|
|
2261
|
+
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
2262
|
+
|
|
2263
|
+
// node_modules/@noble/curves/abstract/weierstrass.js
|
|
2264
|
+
var divNearest = (num2, den) => (num2 + (num2 >= 0 ? den : -den) / _2n2) / den;
|
|
2265
|
+
function _splitEndoScalar(k, basis, n) {
|
|
2266
|
+
const [[a1, b1], [a2, b2]] = basis;
|
|
2267
|
+
const c1 = divNearest(b2 * k, n);
|
|
2268
|
+
const c2 = divNearest(-b1 * k, n);
|
|
2269
|
+
let k1 = k - c1 * a1 - c2 * a2;
|
|
2270
|
+
let k2 = -c1 * b1 - c2 * b2;
|
|
2271
|
+
const k1neg = k1 < _0n4;
|
|
2272
|
+
const k2neg = k2 < _0n4;
|
|
2273
|
+
if (k1neg)
|
|
2274
|
+
k1 = -k1;
|
|
2275
|
+
if (k2neg)
|
|
2276
|
+
k2 = -k2;
|
|
2277
|
+
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n4;
|
|
2278
|
+
if (k1 < _0n4 || k1 >= MAX_NUM || k2 < _0n4 || k2 >= MAX_NUM) {
|
|
2279
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + k);
|
|
2280
|
+
}
|
|
2281
|
+
return { k1neg, k1, k2neg, k2 };
|
|
2282
|
+
}
|
|
2283
|
+
function validateSigFormat(format) {
|
|
2284
|
+
if (!["compact", "recovered", "der"].includes(format))
|
|
2285
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
2286
|
+
return format;
|
|
2287
|
+
}
|
|
2288
|
+
function validateSigOpts(opts, def) {
|
|
2289
|
+
const optsn = {};
|
|
2290
|
+
for (let optName of Object.keys(def)) {
|
|
2291
|
+
optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
2292
|
+
}
|
|
2293
|
+
abool(optsn.lowS, "lowS");
|
|
2294
|
+
abool(optsn.prehash, "prehash");
|
|
2295
|
+
if (optsn.format !== void 0)
|
|
2296
|
+
validateSigFormat(optsn.format);
|
|
2297
|
+
return optsn;
|
|
2298
|
+
}
|
|
2299
|
+
var DERErr = class extends Error {
|
|
2300
|
+
constructor(m = "") {
|
|
2301
|
+
super(m);
|
|
2302
|
+
}
|
|
2303
|
+
};
|
|
2304
|
+
var DER = {
|
|
2305
|
+
// asn.1 DER encoding utils
|
|
2306
|
+
Err: DERErr,
|
|
2307
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
2308
|
+
_tlv: {
|
|
2309
|
+
encode: (tag, data) => {
|
|
2310
|
+
const { Err: E } = DER;
|
|
2311
|
+
if (tag < 0 || tag > 256)
|
|
2312
|
+
throw new E("tlv.encode: wrong tag");
|
|
2313
|
+
if (data.length & 1)
|
|
2314
|
+
throw new E("tlv.encode: unpadded data");
|
|
2315
|
+
const dataLen = data.length / 2;
|
|
2316
|
+
const len = numberToHexUnpadded(dataLen);
|
|
2317
|
+
if (len.length / 2 & 128)
|
|
2318
|
+
throw new E("tlv.encode: long form length too big");
|
|
2319
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
2320
|
+
const t = numberToHexUnpadded(tag);
|
|
2321
|
+
return t + lenLen + len + data;
|
|
2322
|
+
},
|
|
2323
|
+
// v - value, l - left bytes (unparsed)
|
|
2324
|
+
decode(tag, data) {
|
|
2325
|
+
const { Err: E } = DER;
|
|
2326
|
+
let pos = 0;
|
|
2327
|
+
if (tag < 0 || tag > 256)
|
|
2328
|
+
throw new E("tlv.encode: wrong tag");
|
|
2329
|
+
if (data.length < 2 || data[pos++] !== tag)
|
|
2330
|
+
throw new E("tlv.decode: wrong tlv");
|
|
2331
|
+
const first = data[pos++];
|
|
2332
|
+
const isLong = !!(first & 128);
|
|
2333
|
+
let length = 0;
|
|
2334
|
+
if (!isLong)
|
|
2335
|
+
length = first;
|
|
2336
|
+
else {
|
|
2337
|
+
const lenLen = first & 127;
|
|
2338
|
+
if (!lenLen)
|
|
2339
|
+
throw new E("tlv.decode(long): indefinite length not supported");
|
|
2340
|
+
if (lenLen > 4)
|
|
2341
|
+
throw new E("tlv.decode(long): byte length is too big");
|
|
2342
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
2343
|
+
if (lengthBytes.length !== lenLen)
|
|
2344
|
+
throw new E("tlv.decode: length bytes not complete");
|
|
2345
|
+
if (lengthBytes[0] === 0)
|
|
2346
|
+
throw new E("tlv.decode(long): zero leftmost byte");
|
|
2347
|
+
for (const b of lengthBytes)
|
|
2348
|
+
length = length << 8 | b;
|
|
2349
|
+
pos += lenLen;
|
|
2350
|
+
if (length < 128)
|
|
2351
|
+
throw new E("tlv.decode(long): not minimal encoding");
|
|
2352
|
+
}
|
|
2353
|
+
const v = data.subarray(pos, pos + length);
|
|
2354
|
+
if (v.length !== length)
|
|
2355
|
+
throw new E("tlv.decode: wrong value length");
|
|
2356
|
+
return { v, l: data.subarray(pos + length) };
|
|
2357
|
+
}
|
|
2358
|
+
},
|
|
2359
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
2360
|
+
// since we always use positive integers here. It must always be empty:
|
|
2361
|
+
// - add zero byte if exists
|
|
2362
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2363
|
+
_int: {
|
|
2364
|
+
encode(num2) {
|
|
2365
|
+
const { Err: E } = DER;
|
|
2366
|
+
if (num2 < _0n4)
|
|
2367
|
+
throw new E("integer: negative integers are not allowed");
|
|
2368
|
+
let hex = numberToHexUnpadded(num2);
|
|
2369
|
+
if (Number.parseInt(hex[0], 16) & 8)
|
|
2370
|
+
hex = "00" + hex;
|
|
2371
|
+
if (hex.length & 1)
|
|
2372
|
+
throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
2373
|
+
return hex;
|
|
2374
|
+
},
|
|
2375
|
+
decode(data) {
|
|
2376
|
+
const { Err: E } = DER;
|
|
2377
|
+
if (data[0] & 128)
|
|
2378
|
+
throw new E("invalid signature integer: negative");
|
|
2379
|
+
if (data[0] === 0 && !(data[1] & 128))
|
|
2380
|
+
throw new E("invalid signature integer: unnecessary leading zero");
|
|
2381
|
+
return bytesToNumberBE(data);
|
|
2382
|
+
}
|
|
2383
|
+
},
|
|
2384
|
+
toSig(bytes) {
|
|
2385
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
2386
|
+
const data = abytes(bytes, void 0, "signature");
|
|
2387
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
2388
|
+
if (seqLeftBytes.length)
|
|
2389
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2390
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
2391
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
2392
|
+
if (sLeftBytes.length)
|
|
2393
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2394
|
+
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
2395
|
+
},
|
|
2396
|
+
hexFromSig(sig) {
|
|
2397
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
2398
|
+
const rs = tlv.encode(2, int.encode(sig.r));
|
|
2399
|
+
const ss = tlv.encode(2, int.encode(sig.s));
|
|
2400
|
+
const seq = rs + ss;
|
|
2401
|
+
return tlv.encode(48, seq);
|
|
2402
|
+
}
|
|
2403
|
+
};
|
|
2404
|
+
var _0n4 = BigInt(0);
|
|
2405
|
+
var _1n4 = BigInt(1);
|
|
2406
|
+
var _2n2 = BigInt(2);
|
|
2407
|
+
var _3n2 = BigInt(3);
|
|
2408
|
+
var _4n2 = BigInt(4);
|
|
2409
|
+
function weierstrass(params, extraOpts = {}) {
|
|
2410
|
+
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
2411
|
+
const { Fp, Fn } = validated;
|
|
2412
|
+
let CURVE = validated.CURVE;
|
|
2413
|
+
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
2414
|
+
validateObject(extraOpts, {}, {
|
|
2415
|
+
allowInfinityPoint: "boolean",
|
|
2416
|
+
clearCofactor: "function",
|
|
2417
|
+
isTorsionFree: "function",
|
|
2418
|
+
fromBytes: "function",
|
|
2419
|
+
toBytes: "function",
|
|
2420
|
+
endo: "object"
|
|
2421
|
+
});
|
|
2422
|
+
const { endo } = extraOpts;
|
|
2423
|
+
if (endo) {
|
|
2424
|
+
if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
|
|
2425
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
const lengths = getWLengths(Fp, Fn);
|
|
2429
|
+
function assertCompressionIsSupported() {
|
|
2430
|
+
if (!Fp.isOdd)
|
|
2431
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
2432
|
+
}
|
|
2433
|
+
function pointToBytes2(_c, point, isCompressed) {
|
|
2434
|
+
const { x, y } = point.toAffine();
|
|
2435
|
+
const bx = Fp.toBytes(x);
|
|
2436
|
+
abool(isCompressed, "isCompressed");
|
|
2437
|
+
if (isCompressed) {
|
|
2438
|
+
assertCompressionIsSupported();
|
|
2439
|
+
const hasEvenY = !Fp.isOdd(y);
|
|
2440
|
+
return concatBytes(pprefix(hasEvenY), bx);
|
|
2441
|
+
} else {
|
|
2442
|
+
return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
function pointFromBytes(bytes) {
|
|
2446
|
+
abytes(bytes, void 0, "Point");
|
|
2447
|
+
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
2448
|
+
const length = bytes.length;
|
|
2449
|
+
const head = bytes[0];
|
|
2450
|
+
const tail = bytes.subarray(1);
|
|
2451
|
+
if (length === comp && (head === 2 || head === 3)) {
|
|
2452
|
+
const x = Fp.fromBytes(tail);
|
|
2453
|
+
if (!Fp.isValid(x))
|
|
2454
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
2455
|
+
const y2 = weierstrassEquation(x);
|
|
2456
|
+
let y;
|
|
2457
|
+
try {
|
|
2458
|
+
y = Fp.sqrt(y2);
|
|
2459
|
+
} catch (sqrtError) {
|
|
2460
|
+
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
2461
|
+
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
2462
|
+
}
|
|
2463
|
+
assertCompressionIsSupported();
|
|
2464
|
+
const evenY = Fp.isOdd(y);
|
|
2465
|
+
const evenH = (head & 1) === 1;
|
|
2466
|
+
if (evenH !== evenY)
|
|
2467
|
+
y = Fp.neg(y);
|
|
2468
|
+
return { x, y };
|
|
2469
|
+
} else if (length === uncomp && head === 4) {
|
|
2470
|
+
const L = Fp.BYTES;
|
|
2471
|
+
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
2472
|
+
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
2473
|
+
if (!isValidXY(x, y))
|
|
2474
|
+
throw new Error("bad point: is not on curve");
|
|
2475
|
+
return { x, y };
|
|
2476
|
+
} else {
|
|
2477
|
+
throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
const encodePoint = extraOpts.toBytes || pointToBytes2;
|
|
2481
|
+
const decodePoint = extraOpts.fromBytes || pointFromBytes;
|
|
2482
|
+
function weierstrassEquation(x) {
|
|
2483
|
+
const x2 = Fp.sqr(x);
|
|
2484
|
+
const x3 = Fp.mul(x2, x);
|
|
2485
|
+
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
2486
|
+
}
|
|
2487
|
+
function isValidXY(x, y) {
|
|
2488
|
+
const left = Fp.sqr(y);
|
|
2489
|
+
const right = weierstrassEquation(x);
|
|
2490
|
+
return Fp.eql(left, right);
|
|
2491
|
+
}
|
|
2492
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
|
2493
|
+
throw new Error("bad curve params: generator point");
|
|
2494
|
+
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n2), _4n2);
|
|
2495
|
+
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
2496
|
+
if (Fp.is0(Fp.add(_4a3, _27b2)))
|
|
2497
|
+
throw new Error("bad curve params: a or b");
|
|
2498
|
+
function acoord(title, n, banZero = false) {
|
|
2499
|
+
if (!Fp.isValid(n) || banZero && Fp.is0(n))
|
|
2500
|
+
throw new Error(`bad point coordinate ${title}`);
|
|
2501
|
+
return n;
|
|
2502
|
+
}
|
|
2503
|
+
function aprjpoint(other) {
|
|
2504
|
+
if (!(other instanceof Point))
|
|
2505
|
+
throw new Error("Weierstrass Point expected");
|
|
2506
|
+
}
|
|
2507
|
+
function splitEndoScalarN(k) {
|
|
2508
|
+
if (!endo || !endo.basises)
|
|
2509
|
+
throw new Error("no endo");
|
|
2510
|
+
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
2511
|
+
}
|
|
2512
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
2513
|
+
const { X, Y, Z } = p;
|
|
2514
|
+
if (Fp.eql(Z, Fp.ONE))
|
|
2515
|
+
return { x: X, y: Y };
|
|
2516
|
+
const is0 = p.is0();
|
|
2517
|
+
if (iz == null)
|
|
2518
|
+
iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
2519
|
+
const x = Fp.mul(X, iz);
|
|
2520
|
+
const y = Fp.mul(Y, iz);
|
|
2521
|
+
const zz = Fp.mul(Z, iz);
|
|
2522
|
+
if (is0)
|
|
2523
|
+
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
2524
|
+
if (!Fp.eql(zz, Fp.ONE))
|
|
2525
|
+
throw new Error("invZ was invalid");
|
|
2526
|
+
return { x, y };
|
|
2527
|
+
});
|
|
2528
|
+
const assertValidMemo = memoized((p) => {
|
|
2529
|
+
if (p.is0()) {
|
|
2530
|
+
if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
|
|
2531
|
+
return;
|
|
2532
|
+
throw new Error("bad point: ZERO");
|
|
2533
|
+
}
|
|
2534
|
+
const { x, y } = p.toAffine();
|
|
2535
|
+
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
2536
|
+
throw new Error("bad point: x or y not field elements");
|
|
2537
|
+
if (!isValidXY(x, y))
|
|
2538
|
+
throw new Error("bad point: equation left != right");
|
|
2539
|
+
if (!p.isTorsionFree())
|
|
2540
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
2541
|
+
return true;
|
|
2542
|
+
});
|
|
2543
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
2544
|
+
k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
2545
|
+
k1p = negateCt(k1neg, k1p);
|
|
2546
|
+
k2p = negateCt(k2neg, k2p);
|
|
2547
|
+
return k1p.add(k2p);
|
|
2548
|
+
}
|
|
2549
|
+
class Point {
|
|
2550
|
+
// base / generator point
|
|
2551
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
2552
|
+
// zero / infinity / identity point
|
|
2553
|
+
static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
2554
|
+
// 0, 1, 0
|
|
2555
|
+
// math field
|
|
2556
|
+
static Fp = Fp;
|
|
2557
|
+
// scalar field
|
|
2558
|
+
static Fn = Fn;
|
|
2559
|
+
X;
|
|
2560
|
+
Y;
|
|
2561
|
+
Z;
|
|
2562
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2563
|
+
constructor(X, Y, Z) {
|
|
2564
|
+
this.X = acoord("x", X);
|
|
2565
|
+
this.Y = acoord("y", Y, true);
|
|
2566
|
+
this.Z = acoord("z", Z);
|
|
2567
|
+
Object.freeze(this);
|
|
2568
|
+
}
|
|
2569
|
+
static CURVE() {
|
|
2570
|
+
return CURVE;
|
|
2571
|
+
}
|
|
2572
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2573
|
+
static fromAffine(p) {
|
|
2574
|
+
const { x, y } = p || {};
|
|
2575
|
+
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
2576
|
+
throw new Error("invalid affine point");
|
|
2577
|
+
if (p instanceof Point)
|
|
2578
|
+
throw new Error("projective point not allowed");
|
|
2579
|
+
if (Fp.is0(x) && Fp.is0(y))
|
|
2580
|
+
return Point.ZERO;
|
|
2581
|
+
return new Point(x, y, Fp.ONE);
|
|
2582
|
+
}
|
|
2583
|
+
static fromBytes(bytes) {
|
|
2584
|
+
const P = Point.fromAffine(decodePoint(abytes(bytes, void 0, "point")));
|
|
2585
|
+
P.assertValidity();
|
|
2586
|
+
return P;
|
|
2587
|
+
}
|
|
2588
|
+
static fromHex(hex) {
|
|
2589
|
+
return Point.fromBytes(hexToBytes(hex));
|
|
2590
|
+
}
|
|
2591
|
+
get x() {
|
|
2592
|
+
return this.toAffine().x;
|
|
2593
|
+
}
|
|
2594
|
+
get y() {
|
|
2595
|
+
return this.toAffine().y;
|
|
2596
|
+
}
|
|
2597
|
+
/**
|
|
2598
|
+
*
|
|
2599
|
+
* @param windowSize
|
|
2600
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
2601
|
+
* @returns
|
|
2602
|
+
*/
|
|
2603
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
2604
|
+
wnaf.createCache(this, windowSize);
|
|
2605
|
+
if (!isLazy)
|
|
2606
|
+
this.multiply(_3n2);
|
|
2607
|
+
return this;
|
|
2608
|
+
}
|
|
2609
|
+
// TODO: return `this`
|
|
2610
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
2611
|
+
assertValidity() {
|
|
2612
|
+
assertValidMemo(this);
|
|
2613
|
+
}
|
|
2614
|
+
hasEvenY() {
|
|
2615
|
+
const { y } = this.toAffine();
|
|
2616
|
+
if (!Fp.isOdd)
|
|
2617
|
+
throw new Error("Field doesn't support isOdd");
|
|
2618
|
+
return !Fp.isOdd(y);
|
|
2619
|
+
}
|
|
2620
|
+
/** Compare one point to another. */
|
|
2621
|
+
equals(other) {
|
|
2622
|
+
aprjpoint(other);
|
|
2623
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2624
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2625
|
+
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
2626
|
+
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
2627
|
+
return U1 && U2;
|
|
2628
|
+
}
|
|
2629
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
2630
|
+
negate() {
|
|
2631
|
+
return new Point(this.X, Fp.neg(this.Y), this.Z);
|
|
2632
|
+
}
|
|
2633
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
2634
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2635
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2636
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2637
|
+
double() {
|
|
2638
|
+
const { a, b } = CURVE;
|
|
2639
|
+
const b3 = Fp.mul(b, _3n2);
|
|
2640
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2641
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2642
|
+
let t0 = Fp.mul(X1, X1);
|
|
2643
|
+
let t1 = Fp.mul(Y1, Y1);
|
|
2644
|
+
let t2 = Fp.mul(Z1, Z1);
|
|
2645
|
+
let t3 = Fp.mul(X1, Y1);
|
|
2646
|
+
t3 = Fp.add(t3, t3);
|
|
2647
|
+
Z3 = Fp.mul(X1, Z1);
|
|
2648
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2649
|
+
X3 = Fp.mul(a, Z3);
|
|
2650
|
+
Y3 = Fp.mul(b3, t2);
|
|
2651
|
+
Y3 = Fp.add(X3, Y3);
|
|
2652
|
+
X3 = Fp.sub(t1, Y3);
|
|
2653
|
+
Y3 = Fp.add(t1, Y3);
|
|
2654
|
+
Y3 = Fp.mul(X3, Y3);
|
|
2655
|
+
X3 = Fp.mul(t3, X3);
|
|
2656
|
+
Z3 = Fp.mul(b3, Z3);
|
|
2657
|
+
t2 = Fp.mul(a, t2);
|
|
2658
|
+
t3 = Fp.sub(t0, t2);
|
|
2659
|
+
t3 = Fp.mul(a, t3);
|
|
2660
|
+
t3 = Fp.add(t3, Z3);
|
|
2661
|
+
Z3 = Fp.add(t0, t0);
|
|
2662
|
+
t0 = Fp.add(Z3, t0);
|
|
2663
|
+
t0 = Fp.add(t0, t2);
|
|
2664
|
+
t0 = Fp.mul(t0, t3);
|
|
2665
|
+
Y3 = Fp.add(Y3, t0);
|
|
2666
|
+
t2 = Fp.mul(Y1, Z1);
|
|
2667
|
+
t2 = Fp.add(t2, t2);
|
|
2668
|
+
t0 = Fp.mul(t2, t3);
|
|
2669
|
+
X3 = Fp.sub(X3, t0);
|
|
2670
|
+
Z3 = Fp.mul(t2, t1);
|
|
2671
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2672
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2673
|
+
return new Point(X3, Y3, Z3);
|
|
2674
|
+
}
|
|
2675
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
2676
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2677
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
2678
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2679
|
+
add(other) {
|
|
2680
|
+
aprjpoint(other);
|
|
2681
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2682
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2683
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2684
|
+
const a = CURVE.a;
|
|
2685
|
+
const b3 = Fp.mul(CURVE.b, _3n2);
|
|
2686
|
+
let t0 = Fp.mul(X1, X2);
|
|
2687
|
+
let t1 = Fp.mul(Y1, Y2);
|
|
2688
|
+
let t2 = Fp.mul(Z1, Z2);
|
|
2689
|
+
let t3 = Fp.add(X1, Y1);
|
|
2690
|
+
let t4 = Fp.add(X2, Y2);
|
|
2691
|
+
t3 = Fp.mul(t3, t4);
|
|
2692
|
+
t4 = Fp.add(t0, t1);
|
|
2693
|
+
t3 = Fp.sub(t3, t4);
|
|
2694
|
+
t4 = Fp.add(X1, Z1);
|
|
2695
|
+
let t5 = Fp.add(X2, Z2);
|
|
2696
|
+
t4 = Fp.mul(t4, t5);
|
|
2697
|
+
t5 = Fp.add(t0, t2);
|
|
2698
|
+
t4 = Fp.sub(t4, t5);
|
|
2699
|
+
t5 = Fp.add(Y1, Z1);
|
|
2700
|
+
X3 = Fp.add(Y2, Z2);
|
|
2701
|
+
t5 = Fp.mul(t5, X3);
|
|
2702
|
+
X3 = Fp.add(t1, t2);
|
|
2703
|
+
t5 = Fp.sub(t5, X3);
|
|
2704
|
+
Z3 = Fp.mul(a, t4);
|
|
2705
|
+
X3 = Fp.mul(b3, t2);
|
|
2706
|
+
Z3 = Fp.add(X3, Z3);
|
|
2707
|
+
X3 = Fp.sub(t1, Z3);
|
|
2708
|
+
Z3 = Fp.add(t1, Z3);
|
|
2709
|
+
Y3 = Fp.mul(X3, Z3);
|
|
2710
|
+
t1 = Fp.add(t0, t0);
|
|
2711
|
+
t1 = Fp.add(t1, t0);
|
|
2712
|
+
t2 = Fp.mul(a, t2);
|
|
2713
|
+
t4 = Fp.mul(b3, t4);
|
|
2714
|
+
t1 = Fp.add(t1, t2);
|
|
2715
|
+
t2 = Fp.sub(t0, t2);
|
|
2716
|
+
t2 = Fp.mul(a, t2);
|
|
2717
|
+
t4 = Fp.add(t4, t2);
|
|
2718
|
+
t0 = Fp.mul(t1, t4);
|
|
2719
|
+
Y3 = Fp.add(Y3, t0);
|
|
2720
|
+
t0 = Fp.mul(t5, t4);
|
|
2721
|
+
X3 = Fp.mul(t3, X3);
|
|
2722
|
+
X3 = Fp.sub(X3, t0);
|
|
2723
|
+
t0 = Fp.mul(t3, t1);
|
|
2724
|
+
Z3 = Fp.mul(t5, Z3);
|
|
2725
|
+
Z3 = Fp.add(Z3, t0);
|
|
2726
|
+
return new Point(X3, Y3, Z3);
|
|
2727
|
+
}
|
|
2728
|
+
subtract(other) {
|
|
2729
|
+
return this.add(other.negate());
|
|
2730
|
+
}
|
|
2731
|
+
is0() {
|
|
2732
|
+
return this.equals(Point.ZERO);
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
* Constant time multiplication.
|
|
2736
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
2737
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
2738
|
+
* Uses precomputes when available.
|
|
2739
|
+
* Uses endomorphism for Koblitz curves.
|
|
2740
|
+
* @param scalar by which the point would be multiplied
|
|
2741
|
+
* @returns New point
|
|
2742
|
+
*/
|
|
2743
|
+
multiply(scalar) {
|
|
2744
|
+
const { endo: endo2 } = extraOpts;
|
|
2745
|
+
if (!Fn.isValidNot0(scalar))
|
|
2746
|
+
throw new Error("invalid scalar: out of range");
|
|
2747
|
+
let point, fake;
|
|
2748
|
+
const mul = (n) => wnaf.cached(this, n, (p) => normalizeZ(Point, p));
|
|
2749
|
+
if (endo2) {
|
|
2750
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
|
|
2751
|
+
const { p: k1p, f: k1f } = mul(k1);
|
|
2752
|
+
const { p: k2p, f: k2f } = mul(k2);
|
|
2753
|
+
fake = k1f.add(k2f);
|
|
2754
|
+
point = finishEndo(endo2.beta, k1p, k2p, k1neg, k2neg);
|
|
2755
|
+
} else {
|
|
2756
|
+
const { p, f } = mul(scalar);
|
|
2757
|
+
point = p;
|
|
2758
|
+
fake = f;
|
|
2759
|
+
}
|
|
2760
|
+
return normalizeZ(Point, [point, fake])[0];
|
|
2761
|
+
}
|
|
2762
|
+
/**
|
|
2763
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
2764
|
+
* It's faster, but should only be used when you don't care about
|
|
2765
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
2766
|
+
*/
|
|
2767
|
+
multiplyUnsafe(sc) {
|
|
2768
|
+
const { endo: endo2 } = extraOpts;
|
|
2769
|
+
const p = this;
|
|
2770
|
+
if (!Fn.isValid(sc))
|
|
2771
|
+
throw new Error("invalid scalar: out of range");
|
|
2772
|
+
if (sc === _0n4 || p.is0())
|
|
2773
|
+
return Point.ZERO;
|
|
2774
|
+
if (sc === _1n4)
|
|
2775
|
+
return p;
|
|
2776
|
+
if (wnaf.hasCache(this))
|
|
2777
|
+
return this.multiply(sc);
|
|
2778
|
+
if (endo2) {
|
|
2779
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
|
|
2780
|
+
const { p1, p2 } = mulEndoUnsafe(Point, p, k1, k2);
|
|
2781
|
+
return finishEndo(endo2.beta, p1, p2, k1neg, k2neg);
|
|
2782
|
+
} else {
|
|
2783
|
+
return wnaf.unsafe(p, sc);
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
/**
|
|
2787
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
2788
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
2789
|
+
*/
|
|
2790
|
+
toAffine(invertedZ) {
|
|
2791
|
+
return toAffineMemo(this, invertedZ);
|
|
2792
|
+
}
|
|
2793
|
+
/**
|
|
2794
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
2795
|
+
* Always torsion-free for cofactor=1 curves.
|
|
2796
|
+
*/
|
|
2797
|
+
isTorsionFree() {
|
|
2798
|
+
const { isTorsionFree } = extraOpts;
|
|
2799
|
+
if (cofactor === _1n4)
|
|
2800
|
+
return true;
|
|
2801
|
+
if (isTorsionFree)
|
|
2802
|
+
return isTorsionFree(Point, this);
|
|
2803
|
+
return wnaf.unsafe(this, CURVE_ORDER).is0();
|
|
2804
|
+
}
|
|
2805
|
+
clearCofactor() {
|
|
2806
|
+
const { clearCofactor } = extraOpts;
|
|
2807
|
+
if (cofactor === _1n4)
|
|
2808
|
+
return this;
|
|
2809
|
+
if (clearCofactor)
|
|
2810
|
+
return clearCofactor(Point, this);
|
|
2811
|
+
return this.multiplyUnsafe(cofactor);
|
|
2812
|
+
}
|
|
2813
|
+
isSmallOrder() {
|
|
2814
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
2815
|
+
}
|
|
2816
|
+
toBytes(isCompressed = true) {
|
|
2817
|
+
abool(isCompressed, "isCompressed");
|
|
2818
|
+
this.assertValidity();
|
|
2819
|
+
return encodePoint(Point, this, isCompressed);
|
|
2820
|
+
}
|
|
2821
|
+
toHex(isCompressed = true) {
|
|
2822
|
+
return bytesToHex(this.toBytes(isCompressed));
|
|
2823
|
+
}
|
|
2824
|
+
toString() {
|
|
2825
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
const bits = Fn.BITS;
|
|
2829
|
+
const wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
|
|
2830
|
+
Point.BASE.precompute(8);
|
|
2831
|
+
return Point;
|
|
2832
|
+
}
|
|
2833
|
+
function pprefix(hasEvenY) {
|
|
2834
|
+
return Uint8Array.of(hasEvenY ? 2 : 3);
|
|
2835
|
+
}
|
|
2836
|
+
function getWLengths(Fp, Fn) {
|
|
2837
|
+
return {
|
|
2838
|
+
secretKey: Fn.BYTES,
|
|
2839
|
+
publicKey: 1 + Fp.BYTES,
|
|
2840
|
+
publicKeyUncompressed: 1 + 2 * Fp.BYTES,
|
|
2841
|
+
publicKeyHasPrefix: true,
|
|
2842
|
+
signature: 2 * Fn.BYTES
|
|
2843
|
+
};
|
|
2844
|
+
}
|
|
2845
|
+
function ecdh(Point, ecdhOpts = {}) {
|
|
2846
|
+
const { Fn } = Point;
|
|
2847
|
+
const randomBytes_ = ecdhOpts.randomBytes || randomBytes;
|
|
2848
|
+
const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: getMinHashLength(Fn.ORDER) });
|
|
2849
|
+
function isValidSecretKey(secretKey) {
|
|
2850
|
+
try {
|
|
2851
|
+
const num2 = Fn.fromBytes(secretKey);
|
|
2852
|
+
return Fn.isValidNot0(num2);
|
|
2853
|
+
} catch (error) {
|
|
2854
|
+
return false;
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
function isValidPublicKey(publicKey, isCompressed) {
|
|
2858
|
+
const { publicKey: comp, publicKeyUncompressed } = lengths;
|
|
2859
|
+
try {
|
|
2860
|
+
const l = publicKey.length;
|
|
2861
|
+
if (isCompressed === true && l !== comp)
|
|
2862
|
+
return false;
|
|
2863
|
+
if (isCompressed === false && l !== publicKeyUncompressed)
|
|
2864
|
+
return false;
|
|
2865
|
+
return !!Point.fromBytes(publicKey);
|
|
2866
|
+
} catch (error) {
|
|
2867
|
+
return false;
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
function randomSecretKey(seed = randomBytes_(lengths.seed)) {
|
|
2871
|
+
return mapHashToField(abytes(seed, lengths.seed, "seed"), Fn.ORDER);
|
|
2872
|
+
}
|
|
2873
|
+
function getPublicKey(secretKey, isCompressed = true) {
|
|
2874
|
+
return Point.BASE.multiply(Fn.fromBytes(secretKey)).toBytes(isCompressed);
|
|
2875
|
+
}
|
|
2876
|
+
function isProbPub(item) {
|
|
2877
|
+
const { secretKey, publicKey, publicKeyUncompressed } = lengths;
|
|
2878
|
+
if (!isBytes(item))
|
|
2879
|
+
return void 0;
|
|
2880
|
+
if ("_lengths" in Fn && Fn._lengths || secretKey === publicKey)
|
|
2881
|
+
return void 0;
|
|
2882
|
+
const l = abytes(item, void 0, "key").length;
|
|
2883
|
+
return l === publicKey || l === publicKeyUncompressed;
|
|
2884
|
+
}
|
|
2885
|
+
function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
|
|
2886
|
+
if (isProbPub(secretKeyA) === true)
|
|
2887
|
+
throw new Error("first arg must be private key");
|
|
2888
|
+
if (isProbPub(publicKeyB) === false)
|
|
2889
|
+
throw new Error("second arg must be public key");
|
|
2890
|
+
const s = Fn.fromBytes(secretKeyA);
|
|
2891
|
+
const b = Point.fromBytes(publicKeyB);
|
|
2892
|
+
return b.multiply(s).toBytes(isCompressed);
|
|
2893
|
+
}
|
|
2894
|
+
const utils = {
|
|
2895
|
+
isValidSecretKey,
|
|
2896
|
+
isValidPublicKey,
|
|
2897
|
+
randomSecretKey
|
|
2898
|
+
};
|
|
2899
|
+
const keygen = createKeygen(randomSecretKey, getPublicKey);
|
|
2900
|
+
return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
|
|
2901
|
+
}
|
|
2902
|
+
function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
2903
|
+
ahash(hash);
|
|
2904
|
+
validateObject(ecdsaOpts, {}, {
|
|
2905
|
+
hmac: "function",
|
|
2906
|
+
lowS: "boolean",
|
|
2907
|
+
randomBytes: "function",
|
|
2908
|
+
bits2int: "function",
|
|
2909
|
+
bits2int_modN: "function"
|
|
2910
|
+
});
|
|
2911
|
+
ecdsaOpts = Object.assign({}, ecdsaOpts);
|
|
2912
|
+
const randomBytes2 = ecdsaOpts.randomBytes || randomBytes;
|
|
2913
|
+
const hmac2 = ecdsaOpts.hmac || ((key, msg) => hmac(hash, key, msg));
|
|
2914
|
+
const { Fp, Fn } = Point;
|
|
2915
|
+
const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
|
|
2916
|
+
const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
|
|
2917
|
+
const defaultSigOpts = {
|
|
2918
|
+
prehash: true,
|
|
2919
|
+
lowS: typeof ecdsaOpts.lowS === "boolean" ? ecdsaOpts.lowS : true,
|
|
2920
|
+
format: "compact",
|
|
2921
|
+
extraEntropy: false
|
|
2922
|
+
};
|
|
2923
|
+
const hasLargeCofactor = CURVE_ORDER * _2n2 < Fp.ORDER;
|
|
2924
|
+
function isBiggerThanHalfOrder(number) {
|
|
2925
|
+
const HALF = CURVE_ORDER >> _1n4;
|
|
2926
|
+
return number > HALF;
|
|
2927
|
+
}
|
|
2928
|
+
function validateRS(title, num2) {
|
|
2929
|
+
if (!Fn.isValidNot0(num2))
|
|
2930
|
+
throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
|
|
2931
|
+
return num2;
|
|
2932
|
+
}
|
|
2933
|
+
function assertSmallCofactor() {
|
|
2934
|
+
if (hasLargeCofactor)
|
|
2935
|
+
throw new Error('"recovered" sig type is not supported for cofactor >2 curves');
|
|
2936
|
+
}
|
|
2937
|
+
function validateSigLength(bytes, format) {
|
|
2938
|
+
validateSigFormat(format);
|
|
2939
|
+
const size = lengths.signature;
|
|
2940
|
+
const sizer = format === "compact" ? size : format === "recovered" ? size + 1 : void 0;
|
|
2941
|
+
return abytes(bytes, sizer);
|
|
2942
|
+
}
|
|
2943
|
+
class Signature {
|
|
2944
|
+
r;
|
|
2945
|
+
s;
|
|
2946
|
+
recovery;
|
|
2947
|
+
constructor(r, s, recovery) {
|
|
2948
|
+
this.r = validateRS("r", r);
|
|
2949
|
+
this.s = validateRS("s", s);
|
|
2950
|
+
if (recovery != null) {
|
|
2951
|
+
assertSmallCofactor();
|
|
2952
|
+
if (![0, 1, 2, 3].includes(recovery))
|
|
2953
|
+
throw new Error("invalid recovery id");
|
|
2954
|
+
this.recovery = recovery;
|
|
2955
|
+
}
|
|
2956
|
+
Object.freeze(this);
|
|
2957
|
+
}
|
|
2958
|
+
static fromBytes(bytes, format = defaultSigOpts.format) {
|
|
2959
|
+
validateSigLength(bytes, format);
|
|
2960
|
+
let recid;
|
|
2961
|
+
if (format === "der") {
|
|
2962
|
+
const { r: r2, s: s2 } = DER.toSig(abytes(bytes));
|
|
2963
|
+
return new Signature(r2, s2);
|
|
2964
|
+
}
|
|
2965
|
+
if (format === "recovered") {
|
|
2966
|
+
recid = bytes[0];
|
|
2967
|
+
format = "compact";
|
|
2968
|
+
bytes = bytes.subarray(1);
|
|
2969
|
+
}
|
|
2970
|
+
const L = lengths.signature / 2;
|
|
2971
|
+
const r = bytes.subarray(0, L);
|
|
2972
|
+
const s = bytes.subarray(L, L * 2);
|
|
2973
|
+
return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
|
|
2974
|
+
}
|
|
2975
|
+
static fromHex(hex, format) {
|
|
2976
|
+
return this.fromBytes(hexToBytes(hex), format);
|
|
2977
|
+
}
|
|
2978
|
+
assertRecovery() {
|
|
2979
|
+
const { recovery } = this;
|
|
2980
|
+
if (recovery == null)
|
|
2981
|
+
throw new Error("invalid recovery id: must be present");
|
|
2982
|
+
return recovery;
|
|
2983
|
+
}
|
|
2984
|
+
addRecoveryBit(recovery) {
|
|
2985
|
+
return new Signature(this.r, this.s, recovery);
|
|
2986
|
+
}
|
|
2987
|
+
recoverPublicKey(messageHash) {
|
|
2988
|
+
const { r, s } = this;
|
|
2989
|
+
const recovery = this.assertRecovery();
|
|
2990
|
+
const radj = recovery === 2 || recovery === 3 ? r + CURVE_ORDER : r;
|
|
2991
|
+
if (!Fp.isValid(radj))
|
|
2992
|
+
throw new Error("invalid recovery id: sig.r+curve.n != R.x");
|
|
2993
|
+
const x = Fp.toBytes(radj);
|
|
2994
|
+
const R = Point.fromBytes(concatBytes(pprefix((recovery & 1) === 0), x));
|
|
2995
|
+
const ir = Fn.inv(radj);
|
|
2996
|
+
const h = bits2int_modN(abytes(messageHash, void 0, "msgHash"));
|
|
2997
|
+
const u1 = Fn.create(-h * ir);
|
|
2998
|
+
const u2 = Fn.create(s * ir);
|
|
2999
|
+
const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
|
|
3000
|
+
if (Q.is0())
|
|
3001
|
+
throw new Error("invalid recovery: point at infinify");
|
|
3002
|
+
Q.assertValidity();
|
|
3003
|
+
return Q;
|
|
3004
|
+
}
|
|
3005
|
+
// Signatures should be low-s, to prevent malleability.
|
|
3006
|
+
hasHighS() {
|
|
3007
|
+
return isBiggerThanHalfOrder(this.s);
|
|
3008
|
+
}
|
|
3009
|
+
toBytes(format = defaultSigOpts.format) {
|
|
3010
|
+
validateSigFormat(format);
|
|
3011
|
+
if (format === "der")
|
|
3012
|
+
return hexToBytes(DER.hexFromSig(this));
|
|
3013
|
+
const { r, s } = this;
|
|
3014
|
+
const rb = Fn.toBytes(r);
|
|
3015
|
+
const sb = Fn.toBytes(s);
|
|
3016
|
+
if (format === "recovered") {
|
|
3017
|
+
assertSmallCofactor();
|
|
3018
|
+
return concatBytes(Uint8Array.of(this.assertRecovery()), rb, sb);
|
|
3019
|
+
}
|
|
3020
|
+
return concatBytes(rb, sb);
|
|
3021
|
+
}
|
|
3022
|
+
toHex(format) {
|
|
3023
|
+
return bytesToHex(this.toBytes(format));
|
|
3024
|
+
}
|
|
3025
|
+
}
|
|
3026
|
+
const bits2int = ecdsaOpts.bits2int || function bits2int_def(bytes) {
|
|
3027
|
+
if (bytes.length > 8192)
|
|
3028
|
+
throw new Error("input is too large");
|
|
3029
|
+
const num2 = bytesToNumberBE(bytes);
|
|
3030
|
+
const delta = bytes.length * 8 - fnBits;
|
|
3031
|
+
return delta > 0 ? num2 >> BigInt(delta) : num2;
|
|
3032
|
+
};
|
|
3033
|
+
const bits2int_modN = ecdsaOpts.bits2int_modN || function bits2int_modN_def(bytes) {
|
|
3034
|
+
return Fn.create(bits2int(bytes));
|
|
3035
|
+
};
|
|
3036
|
+
const ORDER_MASK = bitMask(fnBits);
|
|
3037
|
+
function int2octets(num2) {
|
|
3038
|
+
aInRange("num < 2^" + fnBits, num2, _0n4, ORDER_MASK);
|
|
3039
|
+
return Fn.toBytes(num2);
|
|
3040
|
+
}
|
|
3041
|
+
function validateMsgAndHash(message, prehash) {
|
|
3042
|
+
abytes(message, void 0, "message");
|
|
3043
|
+
return prehash ? abytes(hash(message), void 0, "prehashed message") : message;
|
|
3044
|
+
}
|
|
3045
|
+
function prepSig(message, secretKey, opts) {
|
|
3046
|
+
const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
|
|
3047
|
+
message = validateMsgAndHash(message, prehash);
|
|
3048
|
+
const h1int = bits2int_modN(message);
|
|
3049
|
+
const d = Fn.fromBytes(secretKey);
|
|
3050
|
+
if (!Fn.isValidNot0(d))
|
|
3051
|
+
throw new Error("invalid private key");
|
|
3052
|
+
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3053
|
+
if (extraEntropy != null && extraEntropy !== false) {
|
|
3054
|
+
const e = extraEntropy === true ? randomBytes2(lengths.secretKey) : extraEntropy;
|
|
3055
|
+
seedArgs.push(abytes(e, void 0, "extraEntropy"));
|
|
3056
|
+
}
|
|
3057
|
+
const seed = concatBytes(...seedArgs);
|
|
3058
|
+
const m = h1int;
|
|
3059
|
+
function k2sig(kBytes) {
|
|
3060
|
+
const k = bits2int(kBytes);
|
|
3061
|
+
if (!Fn.isValidNot0(k))
|
|
3062
|
+
return;
|
|
3063
|
+
const ik = Fn.inv(k);
|
|
3064
|
+
const q = Point.BASE.multiply(k).toAffine();
|
|
3065
|
+
const r = Fn.create(q.x);
|
|
3066
|
+
if (r === _0n4)
|
|
3067
|
+
return;
|
|
3068
|
+
const s = Fn.create(ik * Fn.create(m + r * d));
|
|
3069
|
+
if (s === _0n4)
|
|
3070
|
+
return;
|
|
3071
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
3072
|
+
let normS = s;
|
|
3073
|
+
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3074
|
+
normS = Fn.neg(s);
|
|
3075
|
+
recovery ^= 1;
|
|
3076
|
+
}
|
|
3077
|
+
return new Signature(r, normS, hasLargeCofactor ? void 0 : recovery);
|
|
3078
|
+
}
|
|
3079
|
+
return { seed, k2sig };
|
|
3080
|
+
}
|
|
3081
|
+
function sign(message, secretKey, opts = {}) {
|
|
3082
|
+
const { seed, k2sig } = prepSig(message, secretKey, opts);
|
|
3083
|
+
const drbg = createHmacDrbg(hash.outputLen, Fn.BYTES, hmac2);
|
|
3084
|
+
const sig = drbg(seed, k2sig);
|
|
3085
|
+
return sig.toBytes(opts.format);
|
|
3086
|
+
}
|
|
3087
|
+
function verify(signature, message, publicKey, opts = {}) {
|
|
3088
|
+
const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
|
|
3089
|
+
publicKey = abytes(publicKey, void 0, "publicKey");
|
|
3090
|
+
message = validateMsgAndHash(message, prehash);
|
|
3091
|
+
if (!isBytes(signature)) {
|
|
3092
|
+
const end = signature instanceof Signature ? ", use sig.toBytes()" : "";
|
|
3093
|
+
throw new Error("verify expects Uint8Array signature" + end);
|
|
3094
|
+
}
|
|
3095
|
+
validateSigLength(signature, format);
|
|
3096
|
+
try {
|
|
3097
|
+
const sig = Signature.fromBytes(signature, format);
|
|
3098
|
+
const P = Point.fromBytes(publicKey);
|
|
3099
|
+
if (lowS && sig.hasHighS())
|
|
3100
|
+
return false;
|
|
3101
|
+
const { r, s } = sig;
|
|
3102
|
+
const h = bits2int_modN(message);
|
|
3103
|
+
const is = Fn.inv(s);
|
|
3104
|
+
const u1 = Fn.create(h * is);
|
|
3105
|
+
const u2 = Fn.create(r * is);
|
|
3106
|
+
const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2));
|
|
3107
|
+
if (R.is0())
|
|
3108
|
+
return false;
|
|
3109
|
+
const v = Fn.create(R.x);
|
|
3110
|
+
return v === r;
|
|
3111
|
+
} catch (e) {
|
|
3112
|
+
return false;
|
|
3113
|
+
}
|
|
3114
|
+
}
|
|
3115
|
+
function recoverPublicKey(signature, message, opts = {}) {
|
|
3116
|
+
const { prehash } = validateSigOpts(opts, defaultSigOpts);
|
|
3117
|
+
message = validateMsgAndHash(message, prehash);
|
|
3118
|
+
return Signature.fromBytes(signature, "recovered").recoverPublicKey(message).toBytes();
|
|
3119
|
+
}
|
|
3120
|
+
return Object.freeze({
|
|
3121
|
+
keygen,
|
|
3122
|
+
getPublicKey,
|
|
3123
|
+
getSharedSecret,
|
|
3124
|
+
utils,
|
|
3125
|
+
lengths,
|
|
3126
|
+
Point,
|
|
3127
|
+
sign,
|
|
3128
|
+
verify,
|
|
3129
|
+
recoverPublicKey,
|
|
3130
|
+
Signature,
|
|
3131
|
+
hash
|
|
3132
|
+
});
|
|
3133
|
+
}
|
|
3134
|
+
|
|
3135
|
+
// node_modules/@noble/curves/secp256k1.js
|
|
3136
|
+
var secp256k1_CURVE = {
|
|
3137
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
3138
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
3139
|
+
h: BigInt(1),
|
|
3140
|
+
a: BigInt(0),
|
|
3141
|
+
b: BigInt(7),
|
|
3142
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
3143
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
3144
|
+
};
|
|
3145
|
+
var secp256k1_ENDO = {
|
|
3146
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
3147
|
+
basises: [
|
|
3148
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
3149
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
3150
|
+
]
|
|
3151
|
+
};
|
|
3152
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
3153
|
+
var _2n3 = /* @__PURE__ */ BigInt(2);
|
|
3154
|
+
function sqrtMod(y) {
|
|
3155
|
+
const P = secp256k1_CURVE.p;
|
|
3156
|
+
const _3n3 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
3157
|
+
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
3158
|
+
const b2 = y * y * y % P;
|
|
3159
|
+
const b3 = b2 * b2 * y % P;
|
|
3160
|
+
const b6 = pow2(b3, _3n3, P) * b3 % P;
|
|
3161
|
+
const b9 = pow2(b6, _3n3, P) * b3 % P;
|
|
3162
|
+
const b11 = pow2(b9, _2n3, P) * b2 % P;
|
|
3163
|
+
const b22 = pow2(b11, _11n, P) * b11 % P;
|
|
3164
|
+
const b44 = pow2(b22, _22n, P) * b22 % P;
|
|
3165
|
+
const b88 = pow2(b44, _44n, P) * b44 % P;
|
|
3166
|
+
const b176 = pow2(b88, _88n, P) * b88 % P;
|
|
3167
|
+
const b220 = pow2(b176, _44n, P) * b44 % P;
|
|
3168
|
+
const b223 = pow2(b220, _3n3, P) * b3 % P;
|
|
3169
|
+
const t1 = pow2(b223, _23n, P) * b22 % P;
|
|
3170
|
+
const t2 = pow2(t1, _6n, P) * b2 % P;
|
|
3171
|
+
const root = pow2(t2, _2n3, P);
|
|
3172
|
+
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
|
3173
|
+
throw new Error("Cannot find square root");
|
|
3174
|
+
return root;
|
|
3175
|
+
}
|
|
3176
|
+
var Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
|
|
3177
|
+
var Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
|
|
3178
|
+
Fp: Fpk1,
|
|
3179
|
+
endo: secp256k1_ENDO
|
|
3180
|
+
});
|
|
3181
|
+
var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha256);
|
|
3182
|
+
var TAGGED_HASH_PREFIXES = {};
|
|
3183
|
+
function taggedHash(tag, ...messages) {
|
|
3184
|
+
let tagP = TAGGED_HASH_PREFIXES[tag];
|
|
3185
|
+
if (tagP === void 0) {
|
|
3186
|
+
const tagH = sha256(asciiToBytes(tag));
|
|
3187
|
+
tagP = concatBytes(tagH, tagH);
|
|
3188
|
+
TAGGED_HASH_PREFIXES[tag] = tagP;
|
|
3189
|
+
}
|
|
3190
|
+
return sha256(concatBytes(tagP, ...messages));
|
|
3191
|
+
}
|
|
3192
|
+
var pointToBytes = (point) => point.toBytes(true).slice(1);
|
|
3193
|
+
var hasEven = (y) => y % _2n3 === _0n5;
|
|
3194
|
+
function schnorrGetExtPubKey(priv) {
|
|
3195
|
+
const { Fn, BASE } = Pointk1;
|
|
3196
|
+
const d_ = Fn.fromBytes(priv);
|
|
3197
|
+
const p = BASE.multiply(d_);
|
|
3198
|
+
const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
|
|
3199
|
+
return { scalar, bytes: pointToBytes(p) };
|
|
3200
|
+
}
|
|
3201
|
+
function lift_x(x) {
|
|
3202
|
+
const Fp = Fpk1;
|
|
3203
|
+
if (!Fp.isValidNot0(x))
|
|
3204
|
+
throw new Error("invalid x: Fail if x \u2265 p");
|
|
3205
|
+
const xx = Fp.create(x * x);
|
|
3206
|
+
const c = Fp.create(xx * x + BigInt(7));
|
|
3207
|
+
let y = Fp.sqrt(c);
|
|
3208
|
+
if (!hasEven(y))
|
|
3209
|
+
y = Fp.neg(y);
|
|
3210
|
+
const p = Pointk1.fromAffine({ x, y });
|
|
3211
|
+
p.assertValidity();
|
|
3212
|
+
return p;
|
|
3213
|
+
}
|
|
3214
|
+
var num = bytesToNumberBE;
|
|
3215
|
+
function challenge(...args) {
|
|
3216
|
+
return Pointk1.Fn.create(num(taggedHash("BIP0340/challenge", ...args)));
|
|
3217
|
+
}
|
|
3218
|
+
function schnorrGetPublicKey(secretKey) {
|
|
3219
|
+
return schnorrGetExtPubKey(secretKey).bytes;
|
|
3220
|
+
}
|
|
3221
|
+
function schnorrSign(message, secretKey, auxRand = randomBytes(32)) {
|
|
3222
|
+
const { Fn } = Pointk1;
|
|
3223
|
+
const m = abytes(message, void 0, "message");
|
|
3224
|
+
const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey);
|
|
3225
|
+
const a = abytes(auxRand, 32, "auxRand");
|
|
3226
|
+
const t = Fn.toBytes(d ^ num(taggedHash("BIP0340/aux", a)));
|
|
3227
|
+
const rand = taggedHash("BIP0340/nonce", t, px, m);
|
|
3228
|
+
const { bytes: rx, scalar: k } = schnorrGetExtPubKey(rand);
|
|
3229
|
+
const e = challenge(rx, px, m);
|
|
3230
|
+
const sig = new Uint8Array(64);
|
|
3231
|
+
sig.set(rx, 0);
|
|
3232
|
+
sig.set(Fn.toBytes(Fn.create(k + e * d)), 32);
|
|
3233
|
+
if (!schnorrVerify(sig, m, px))
|
|
3234
|
+
throw new Error("sign: Invalid signature produced");
|
|
3235
|
+
return sig;
|
|
3236
|
+
}
|
|
3237
|
+
function schnorrVerify(signature, message, publicKey) {
|
|
3238
|
+
const { Fp, Fn, BASE } = Pointk1;
|
|
3239
|
+
const sig = abytes(signature, 64, "signature");
|
|
3240
|
+
const m = abytes(message, void 0, "message");
|
|
3241
|
+
const pub = abytes(publicKey, 32, "publicKey");
|
|
3242
|
+
try {
|
|
3243
|
+
const P = lift_x(num(pub));
|
|
3244
|
+
const r = num(sig.subarray(0, 32));
|
|
3245
|
+
if (!Fp.isValidNot0(r))
|
|
3246
|
+
return false;
|
|
3247
|
+
const s = num(sig.subarray(32, 64));
|
|
3248
|
+
if (!Fn.isValidNot0(s))
|
|
3249
|
+
return false;
|
|
3250
|
+
const e = challenge(Fn.toBytes(r), pointToBytes(P), m);
|
|
3251
|
+
const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
|
|
3252
|
+
const { x, y } = R.toAffine();
|
|
3253
|
+
if (R.is0() || !hasEven(y) || x !== r)
|
|
3254
|
+
return false;
|
|
3255
|
+
return true;
|
|
3256
|
+
} catch (error) {
|
|
3257
|
+
return false;
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
var schnorr = /* @__PURE__ */ (() => {
|
|
3261
|
+
const size = 32;
|
|
3262
|
+
const seedLength = 48;
|
|
3263
|
+
const randomSecretKey = (seed = randomBytes(seedLength)) => {
|
|
3264
|
+
return mapHashToField(seed, secp256k1_CURVE.n);
|
|
3265
|
+
};
|
|
3266
|
+
return {
|
|
3267
|
+
keygen: createKeygen(randomSecretKey, schnorrGetPublicKey),
|
|
3268
|
+
getPublicKey: schnorrGetPublicKey,
|
|
3269
|
+
sign: schnorrSign,
|
|
3270
|
+
verify: schnorrVerify,
|
|
3271
|
+
Point: Pointk1,
|
|
3272
|
+
utils: {
|
|
3273
|
+
randomSecretKey,
|
|
3274
|
+
taggedHash,
|
|
3275
|
+
lift_x,
|
|
3276
|
+
pointToBytes
|
|
3277
|
+
},
|
|
3278
|
+
lengths: {
|
|
3279
|
+
secretKey: size,
|
|
3280
|
+
publicKey: size,
|
|
3281
|
+
publicKeyHasPrefix: false,
|
|
3282
|
+
signature: size * 2,
|
|
3283
|
+
seed: seedLength
|
|
3284
|
+
}
|
|
3285
|
+
};
|
|
3286
|
+
})();
|
|
3287
|
+
|
|
3288
|
+
// src/lib/evm-private-key.ts
|
|
3289
|
+
var EVM_PRIVATE_KEY_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
3290
|
+
function isValidEvmPrivateKey(value) {
|
|
3291
|
+
return EVM_PRIVATE_KEY_RE.test(value) && secp256k1.utils.isValidSecretKey(hexToBytes(value.slice(2)));
|
|
3292
|
+
}
|
|
3293
|
+
|
|
3294
|
+
export {
|
|
3295
|
+
authorizeProtectedRemoval,
|
|
3296
|
+
planConfigReset,
|
|
3297
|
+
configSnapshotPath,
|
|
3298
|
+
describeConfigBackup,
|
|
3299
|
+
CREDIT_POSTURES,
|
|
3300
|
+
isCreditPosture,
|
|
3301
|
+
FEEDBACK_POSTURES,
|
|
3302
|
+
isFeedbackPosture,
|
|
3303
|
+
loadConfig,
|
|
3304
|
+
updateConfig,
|
|
3305
|
+
pinConfigSnapshot,
|
|
3306
|
+
ensureConfig,
|
|
3307
|
+
previewConfigRollback,
|
|
3308
|
+
rollbackConfig,
|
|
3309
|
+
retireConfigBackup,
|
|
3310
|
+
anumber,
|
|
3311
|
+
abytes,
|
|
3312
|
+
ahash,
|
|
3313
|
+
clean,
|
|
3314
|
+
createView,
|
|
3315
|
+
bytesToHex,
|
|
3316
|
+
hexToBytes,
|
|
3317
|
+
kdfInputToBytes,
|
|
3318
|
+
concatBytes,
|
|
3319
|
+
checkOpts,
|
|
3320
|
+
randomBytes,
|
|
3321
|
+
sha256,
|
|
3322
|
+
sha512,
|
|
3323
|
+
hmac,
|
|
3324
|
+
secp256k1,
|
|
3325
|
+
schnorr,
|
|
3326
|
+
isValidEvmPrivateKey
|
|
3327
|
+
};
|
|
3328
|
+
/*! Bundled license information:
|
|
3329
|
+
|
|
3330
|
+
@noble/hashes/utils.js:
|
|
3331
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
3332
|
+
|
|
3333
|
+
@noble/curves/utils.js:
|
|
3334
|
+
@noble/curves/abstract/modular.js:
|
|
3335
|
+
@noble/curves/abstract/curve.js:
|
|
3336
|
+
@noble/curves/abstract/weierstrass.js:
|
|
3337
|
+
@noble/curves/secp256k1.js:
|
|
3338
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
3339
|
+
*/
|