@pretense/cli 0.4.0 → 0.6.2
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 +21 -0
- package/README.md +82 -42
- package/dist/chunk-TWMRHZGM.js +44 -0
- package/dist/ed25519-XYI7WRQL.js +551 -0
- package/dist/index.js +19283 -3552
- package/dist/postinstall.js +14 -0
- package/package.json +39 -14
- package/dist/index.d.ts +0 -2
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
init_esm_shims
|
|
4
|
+
} from "./chunk-TWMRHZGM.js";
|
|
5
|
+
|
|
6
|
+
// ../node_modules/.pnpm/@noble+ed25519@3.0.1/node_modules/@noble/ed25519/index.js
|
|
7
|
+
init_esm_shims();
|
|
8
|
+
var ed25519_CURVE = {
|
|
9
|
+
p: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffedn,
|
|
10
|
+
n: 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3edn,
|
|
11
|
+
h: 8n,
|
|
12
|
+
a: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffecn,
|
|
13
|
+
d: 0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3n,
|
|
14
|
+
Gx: 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an,
|
|
15
|
+
Gy: 0x6666666666666666666666666666666666666666666666666666666666666658n
|
|
16
|
+
};
|
|
17
|
+
var { p: P, n: N, Gx, Gy, a: _a, d: _d, h } = ed25519_CURVE;
|
|
18
|
+
var L = 32;
|
|
19
|
+
var captureTrace = (...args) => {
|
|
20
|
+
if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
|
|
21
|
+
Error.captureStackTrace(...args);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var err = (message = "") => {
|
|
25
|
+
const e = new Error(message);
|
|
26
|
+
captureTrace(e, err);
|
|
27
|
+
throw e;
|
|
28
|
+
};
|
|
29
|
+
var isBig = (n) => typeof n === "bigint";
|
|
30
|
+
var isStr = (s) => typeof s === "string";
|
|
31
|
+
var isBytes = (a) => a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
32
|
+
var abytes = (value, length, title = "") => {
|
|
33
|
+
const bytes = isBytes(value);
|
|
34
|
+
const len = value?.length;
|
|
35
|
+
const needsLen = length !== void 0;
|
|
36
|
+
if (!bytes || needsLen && len !== length) {
|
|
37
|
+
const prefix = title && `"${title}" `;
|
|
38
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
39
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
40
|
+
err(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
var u8n = (len) => new Uint8Array(len);
|
|
45
|
+
var u8fr = (buf) => Uint8Array.from(buf);
|
|
46
|
+
var padh = (n, pad) => n.toString(16).padStart(pad, "0");
|
|
47
|
+
var bytesToHex = (b) => Array.from(abytes(b)).map((e) => padh(e, 2)).join("");
|
|
48
|
+
var C = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
49
|
+
var _ch = (ch) => {
|
|
50
|
+
if (ch >= C._0 && ch <= C._9)
|
|
51
|
+
return ch - C._0;
|
|
52
|
+
if (ch >= C.A && ch <= C.F)
|
|
53
|
+
return ch - (C.A - 10);
|
|
54
|
+
if (ch >= C.a && ch <= C.f)
|
|
55
|
+
return ch - (C.a - 10);
|
|
56
|
+
return;
|
|
57
|
+
};
|
|
58
|
+
var hexToBytes = (hex) => {
|
|
59
|
+
const e = "hex invalid";
|
|
60
|
+
if (!isStr(hex))
|
|
61
|
+
return err(e);
|
|
62
|
+
const hl = hex.length;
|
|
63
|
+
const al = hl / 2;
|
|
64
|
+
if (hl % 2)
|
|
65
|
+
return err(e);
|
|
66
|
+
const array = u8n(al);
|
|
67
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
68
|
+
const n1 = _ch(hex.charCodeAt(hi));
|
|
69
|
+
const n2 = _ch(hex.charCodeAt(hi + 1));
|
|
70
|
+
if (n1 === void 0 || n2 === void 0)
|
|
71
|
+
return err(e);
|
|
72
|
+
array[ai] = n1 * 16 + n2;
|
|
73
|
+
}
|
|
74
|
+
return array;
|
|
75
|
+
};
|
|
76
|
+
var cr = () => globalThis?.crypto;
|
|
77
|
+
var subtle = () => cr()?.subtle ?? err("crypto.subtle must be defined, consider polyfill");
|
|
78
|
+
var concatBytes = (...arrs) => {
|
|
79
|
+
const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0));
|
|
80
|
+
let pad = 0;
|
|
81
|
+
arrs.forEach((a) => {
|
|
82
|
+
r.set(a, pad);
|
|
83
|
+
pad += a.length;
|
|
84
|
+
});
|
|
85
|
+
return r;
|
|
86
|
+
};
|
|
87
|
+
var randomBytes = (len = L) => {
|
|
88
|
+
const c = cr();
|
|
89
|
+
return c.getRandomValues(u8n(len));
|
|
90
|
+
};
|
|
91
|
+
var big = BigInt;
|
|
92
|
+
var assertRange = (n, min, max, msg = "bad number: out of range") => isBig(n) && min <= n && n < max ? n : err(msg);
|
|
93
|
+
var M = (a, b = P) => {
|
|
94
|
+
const r = a % b;
|
|
95
|
+
return r >= 0n ? r : b + r;
|
|
96
|
+
};
|
|
97
|
+
var P_MASK = (1n << 255n) - 1n;
|
|
98
|
+
var modP = (num) => {
|
|
99
|
+
if (num < 0n)
|
|
100
|
+
err("negative coordinate");
|
|
101
|
+
let r = (num >> 255n) * 19n + (num & P_MASK);
|
|
102
|
+
r = (r >> 255n) * 19n + (r & P_MASK);
|
|
103
|
+
return r % P;
|
|
104
|
+
};
|
|
105
|
+
var modN = (a) => M(a, N);
|
|
106
|
+
var invert = (num, md) => {
|
|
107
|
+
if (num === 0n || md <= 0n)
|
|
108
|
+
err("no inverse n=" + num + " mod=" + md);
|
|
109
|
+
let a = M(num, md), b = md, x = 0n, y = 1n, u = 1n, v = 0n;
|
|
110
|
+
while (a !== 0n) {
|
|
111
|
+
const q = b / a, r = b % a;
|
|
112
|
+
const m = x - u * q, n = y - v * q;
|
|
113
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
114
|
+
}
|
|
115
|
+
return b === 1n ? M(x, md) : err("no inverse");
|
|
116
|
+
};
|
|
117
|
+
var callHash = (name) => {
|
|
118
|
+
const fn = hashes[name];
|
|
119
|
+
if (typeof fn !== "function")
|
|
120
|
+
err("hashes." + name + " not set");
|
|
121
|
+
return fn;
|
|
122
|
+
};
|
|
123
|
+
var hash = (msg) => callHash("sha512")(msg);
|
|
124
|
+
var apoint = (p) => p instanceof Point ? p : err("Point expected");
|
|
125
|
+
var B256 = 2n ** 256n;
|
|
126
|
+
var Point = class _Point {
|
|
127
|
+
static BASE;
|
|
128
|
+
static ZERO;
|
|
129
|
+
X;
|
|
130
|
+
Y;
|
|
131
|
+
Z;
|
|
132
|
+
T;
|
|
133
|
+
constructor(X, Y, Z, T) {
|
|
134
|
+
const max = B256;
|
|
135
|
+
this.X = assertRange(X, 0n, max);
|
|
136
|
+
this.Y = assertRange(Y, 0n, max);
|
|
137
|
+
this.Z = assertRange(Z, 1n, max);
|
|
138
|
+
this.T = assertRange(T, 0n, max);
|
|
139
|
+
Object.freeze(this);
|
|
140
|
+
}
|
|
141
|
+
static CURVE() {
|
|
142
|
+
return ed25519_CURVE;
|
|
143
|
+
}
|
|
144
|
+
static fromAffine(p) {
|
|
145
|
+
return new _Point(p.x, p.y, 1n, modP(p.x * p.y));
|
|
146
|
+
}
|
|
147
|
+
/** RFC8032 5.1.3: Uint8Array to Point. */
|
|
148
|
+
static fromBytes(hex, zip215 = false) {
|
|
149
|
+
const d = _d;
|
|
150
|
+
const normed = u8fr(abytes(hex, L));
|
|
151
|
+
const lastByte = hex[31];
|
|
152
|
+
normed[31] = lastByte & ~128;
|
|
153
|
+
const y = bytesToNumberLE(normed);
|
|
154
|
+
const max = zip215 ? B256 : P;
|
|
155
|
+
assertRange(y, 0n, max);
|
|
156
|
+
const y2 = modP(y * y);
|
|
157
|
+
const u = M(y2 - 1n);
|
|
158
|
+
const v = modP(d * y2 + 1n);
|
|
159
|
+
let { isValid, value: x } = uvRatio(u, v);
|
|
160
|
+
if (!isValid)
|
|
161
|
+
err("bad point: y not sqrt");
|
|
162
|
+
const isXOdd = (x & 1n) === 1n;
|
|
163
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
164
|
+
if (!zip215 && x === 0n && isLastByteOdd)
|
|
165
|
+
err("bad point: x==0, isLastByteOdd");
|
|
166
|
+
if (isLastByteOdd !== isXOdd)
|
|
167
|
+
x = M(-x);
|
|
168
|
+
return new _Point(x, y, 1n, modP(x * y));
|
|
169
|
+
}
|
|
170
|
+
static fromHex(hex, zip215) {
|
|
171
|
+
return _Point.fromBytes(hexToBytes(hex), zip215);
|
|
172
|
+
}
|
|
173
|
+
get x() {
|
|
174
|
+
return this.toAffine().x;
|
|
175
|
+
}
|
|
176
|
+
get y() {
|
|
177
|
+
return this.toAffine().y;
|
|
178
|
+
}
|
|
179
|
+
/** Checks if the point is valid and on-curve. */
|
|
180
|
+
assertValidity() {
|
|
181
|
+
const a = _a;
|
|
182
|
+
const d = _d;
|
|
183
|
+
const p = this;
|
|
184
|
+
if (p.is0())
|
|
185
|
+
return err("bad point: ZERO");
|
|
186
|
+
const { X, Y, Z, T } = p;
|
|
187
|
+
const X2 = modP(X * X);
|
|
188
|
+
const Y2 = modP(Y * Y);
|
|
189
|
+
const Z2 = modP(Z * Z);
|
|
190
|
+
const Z4 = modP(Z2 * Z2);
|
|
191
|
+
const aX2 = modP(X2 * a);
|
|
192
|
+
const left = modP(Z2 * (aX2 + Y2));
|
|
193
|
+
const right = M(Z4 + modP(d * modP(X2 * Y2)));
|
|
194
|
+
if (left !== right)
|
|
195
|
+
return err("bad point: equation left != right (1)");
|
|
196
|
+
const XY = modP(X * Y);
|
|
197
|
+
const ZT = modP(Z * T);
|
|
198
|
+
if (XY !== ZT)
|
|
199
|
+
return err("bad point: equation left != right (2)");
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
/** Equality check: compare points P&Q. */
|
|
203
|
+
equals(other) {
|
|
204
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
205
|
+
const { X: X2, Y: Y2, Z: Z2 } = apoint(other);
|
|
206
|
+
const X1Z2 = modP(X1 * Z2);
|
|
207
|
+
const X2Z1 = modP(X2 * Z1);
|
|
208
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
209
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
210
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
211
|
+
}
|
|
212
|
+
is0() {
|
|
213
|
+
return this.equals(I);
|
|
214
|
+
}
|
|
215
|
+
/** Flip point over y coordinate. */
|
|
216
|
+
negate() {
|
|
217
|
+
return new _Point(M(-this.X), this.Y, this.Z, M(-this.T));
|
|
218
|
+
}
|
|
219
|
+
/** Point doubling. Complete formula. Cost: `4M + 4S + 1*a + 6add + 1*2`. */
|
|
220
|
+
double() {
|
|
221
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
222
|
+
const a = _a;
|
|
223
|
+
const A = modP(X1 * X1);
|
|
224
|
+
const B = modP(Y1 * Y1);
|
|
225
|
+
const C2 = modP(2n * Z1 * Z1);
|
|
226
|
+
const D = modP(a * A);
|
|
227
|
+
const x1y1 = M(X1 + Y1);
|
|
228
|
+
const E = M(modP(x1y1 * x1y1) - A - B);
|
|
229
|
+
const G2 = M(D + B);
|
|
230
|
+
const F = M(G2 - C2);
|
|
231
|
+
const H = M(D - B);
|
|
232
|
+
const X3 = modP(E * F);
|
|
233
|
+
const Y3 = modP(G2 * H);
|
|
234
|
+
const T3 = modP(E * H);
|
|
235
|
+
const Z3 = modP(F * G2);
|
|
236
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
237
|
+
}
|
|
238
|
+
/** Point addition. Complete formula. Cost: `8M + 1*k + 8add + 1*2`. */
|
|
239
|
+
add(other) {
|
|
240
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
241
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = apoint(other);
|
|
242
|
+
const a = _a;
|
|
243
|
+
const d = _d;
|
|
244
|
+
const A = modP(X1 * X2);
|
|
245
|
+
const B = modP(Y1 * Y2);
|
|
246
|
+
const C2 = modP(modP(T1 * d) * T2);
|
|
247
|
+
const D = modP(Z1 * Z2);
|
|
248
|
+
const E = M(modP(M(X1 + Y1) * M(X2 + Y2)) - A - B);
|
|
249
|
+
const F = M(D - C2);
|
|
250
|
+
const G2 = M(D + C2);
|
|
251
|
+
const H = M(B - modP(a * A));
|
|
252
|
+
const X3 = modP(E * F);
|
|
253
|
+
const Y3 = modP(G2 * H);
|
|
254
|
+
const T3 = modP(E * H);
|
|
255
|
+
const Z3 = modP(F * G2);
|
|
256
|
+
return new _Point(X3, Y3, Z3, T3);
|
|
257
|
+
}
|
|
258
|
+
subtract(other) {
|
|
259
|
+
return this.add(apoint(other).negate());
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
|
|
263
|
+
* Uses {@link wNAF} for base point.
|
|
264
|
+
* Uses fake point to mitigate side-channel leakage.
|
|
265
|
+
* @param n scalar by which point is multiplied
|
|
266
|
+
* @param safe safe mode guards against timing attacks; unsafe mode is faster
|
|
267
|
+
*/
|
|
268
|
+
multiply(n, safe = true) {
|
|
269
|
+
if (!safe && (n === 0n || this.is0()))
|
|
270
|
+
return I;
|
|
271
|
+
assertRange(n, 1n, N);
|
|
272
|
+
if (n === 1n)
|
|
273
|
+
return this;
|
|
274
|
+
if (this.equals(G))
|
|
275
|
+
return wNAF(n).p;
|
|
276
|
+
let p = I;
|
|
277
|
+
let f = G;
|
|
278
|
+
for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
|
|
279
|
+
if (n & 1n)
|
|
280
|
+
p = p.add(d);
|
|
281
|
+
else if (safe)
|
|
282
|
+
f = f.add(d);
|
|
283
|
+
}
|
|
284
|
+
return p;
|
|
285
|
+
}
|
|
286
|
+
multiplyUnsafe(scalar) {
|
|
287
|
+
return this.multiply(scalar, false);
|
|
288
|
+
}
|
|
289
|
+
/** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
|
|
290
|
+
toAffine() {
|
|
291
|
+
const { X, Y, Z } = this;
|
|
292
|
+
if (this.equals(I))
|
|
293
|
+
return { x: 0n, y: 1n };
|
|
294
|
+
const iz = invert(Z, P);
|
|
295
|
+
if (modP(Z * iz) !== 1n)
|
|
296
|
+
err("invalid inverse");
|
|
297
|
+
const x = modP(X * iz);
|
|
298
|
+
const y = modP(Y * iz);
|
|
299
|
+
return { x, y };
|
|
300
|
+
}
|
|
301
|
+
toBytes() {
|
|
302
|
+
const { x, y } = this.toAffine();
|
|
303
|
+
const b = numTo32bLE(y);
|
|
304
|
+
b[31] |= x & 1n ? 128 : 0;
|
|
305
|
+
return b;
|
|
306
|
+
}
|
|
307
|
+
toHex() {
|
|
308
|
+
return bytesToHex(this.toBytes());
|
|
309
|
+
}
|
|
310
|
+
clearCofactor() {
|
|
311
|
+
return this.multiply(big(h), false);
|
|
312
|
+
}
|
|
313
|
+
isSmallOrder() {
|
|
314
|
+
return this.clearCofactor().is0();
|
|
315
|
+
}
|
|
316
|
+
isTorsionFree() {
|
|
317
|
+
let p = this.multiply(N / 2n, false).double();
|
|
318
|
+
if (N % 2n)
|
|
319
|
+
p = p.add(this);
|
|
320
|
+
return p.is0();
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
var G = new Point(Gx, Gy, 1n, M(Gx * Gy));
|
|
324
|
+
var I = new Point(0n, 1n, 1n, 0n);
|
|
325
|
+
Point.BASE = G;
|
|
326
|
+
Point.ZERO = I;
|
|
327
|
+
var numTo32bLE = (num) => hexToBytes(padh(assertRange(num, 0n, B256), 64)).reverse();
|
|
328
|
+
var bytesToNumberLE = (b) => big("0x" + bytesToHex(u8fr(abytes(b)).reverse()));
|
|
329
|
+
var pow2 = (x, power) => {
|
|
330
|
+
let r = x;
|
|
331
|
+
while (power-- > 0n) {
|
|
332
|
+
r = modP(r * r);
|
|
333
|
+
}
|
|
334
|
+
return r;
|
|
335
|
+
};
|
|
336
|
+
var pow_2_252_3 = (x) => {
|
|
337
|
+
const x2 = modP(x * x);
|
|
338
|
+
const b2 = modP(x2 * x);
|
|
339
|
+
const b4 = modP(pow2(b2, 2n) * b2);
|
|
340
|
+
const b5 = modP(pow2(b4, 1n) * x);
|
|
341
|
+
const b10 = modP(pow2(b5, 5n) * b5);
|
|
342
|
+
const b20 = modP(pow2(b10, 10n) * b10);
|
|
343
|
+
const b40 = modP(pow2(b20, 20n) * b20);
|
|
344
|
+
const b80 = modP(pow2(b40, 40n) * b40);
|
|
345
|
+
const b160 = modP(pow2(b80, 80n) * b80);
|
|
346
|
+
const b240 = modP(pow2(b160, 80n) * b80);
|
|
347
|
+
const b250 = modP(pow2(b240, 10n) * b10);
|
|
348
|
+
const pow_p_5_8 = modP(pow2(b250, 2n) * x);
|
|
349
|
+
return { pow_p_5_8, b2 };
|
|
350
|
+
};
|
|
351
|
+
var RM1 = 0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0n;
|
|
352
|
+
var uvRatio = (u, v) => {
|
|
353
|
+
const v3 = modP(v * modP(v * v));
|
|
354
|
+
const v7 = modP(modP(v3 * v3) * v);
|
|
355
|
+
const pow = pow_2_252_3(modP(u * v7)).pow_p_5_8;
|
|
356
|
+
let x = modP(u * modP(v3 * pow));
|
|
357
|
+
const vx2 = modP(v * modP(x * x));
|
|
358
|
+
const root1 = x;
|
|
359
|
+
const root2 = modP(x * RM1);
|
|
360
|
+
const useRoot1 = vx2 === u;
|
|
361
|
+
const useRoot2 = vx2 === M(-u);
|
|
362
|
+
const noRoot = vx2 === M(-u * RM1);
|
|
363
|
+
if (useRoot1)
|
|
364
|
+
x = root1;
|
|
365
|
+
if (useRoot2 || noRoot)
|
|
366
|
+
x = root2;
|
|
367
|
+
if ((M(x) & 1n) === 1n)
|
|
368
|
+
x = M(-x);
|
|
369
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
370
|
+
};
|
|
371
|
+
var modL_LE = (hash2) => modN(bytesToNumberLE(hash2));
|
|
372
|
+
var sha512a = (...m) => hashes.sha512Async(concatBytes(...m));
|
|
373
|
+
var sha512s = (...m) => callHash("sha512")(concatBytes(...m));
|
|
374
|
+
var hash2extK = (hashed) => {
|
|
375
|
+
const head = hashed.slice(0, 32);
|
|
376
|
+
head[0] &= 248;
|
|
377
|
+
head[31] &= 127;
|
|
378
|
+
head[31] |= 64;
|
|
379
|
+
const prefix = hashed.slice(32, 64);
|
|
380
|
+
const scalar = modL_LE(head);
|
|
381
|
+
const point = G.multiply(scalar);
|
|
382
|
+
const pointBytes = point.toBytes();
|
|
383
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
384
|
+
};
|
|
385
|
+
var getExtendedPublicKeyAsync = (secretKey) => sha512a(abytes(secretKey, L)).then(hash2extK);
|
|
386
|
+
var getExtendedPublicKey = (secretKey) => hash2extK(sha512s(abytes(secretKey, L)));
|
|
387
|
+
var getPublicKeyAsync = (secretKey) => getExtendedPublicKeyAsync(secretKey).then((p) => p.pointBytes);
|
|
388
|
+
var getPublicKey = (priv) => getExtendedPublicKey(priv).pointBytes;
|
|
389
|
+
var hashFinishA = (res) => sha512a(res.hashable).then(res.finish);
|
|
390
|
+
var hashFinishS = (res) => res.finish(sha512s(res.hashable));
|
|
391
|
+
var _sign = (e, rBytes, msg) => {
|
|
392
|
+
const { pointBytes: P2, scalar: s } = e;
|
|
393
|
+
const r = modL_LE(rBytes);
|
|
394
|
+
const R = G.multiply(r).toBytes();
|
|
395
|
+
const hashable = concatBytes(R, P2, msg);
|
|
396
|
+
const finish = (hashed) => {
|
|
397
|
+
const S = modN(r + modL_LE(hashed) * s);
|
|
398
|
+
return abytes(concatBytes(R, numTo32bLE(S)), 64);
|
|
399
|
+
};
|
|
400
|
+
return { hashable, finish };
|
|
401
|
+
};
|
|
402
|
+
var signAsync = async (message, secretKey) => {
|
|
403
|
+
const m = abytes(message);
|
|
404
|
+
const e = await getExtendedPublicKeyAsync(secretKey);
|
|
405
|
+
const rBytes = await sha512a(e.prefix, m);
|
|
406
|
+
return hashFinishA(_sign(e, rBytes, m));
|
|
407
|
+
};
|
|
408
|
+
var sign = (message, secretKey) => {
|
|
409
|
+
const m = abytes(message);
|
|
410
|
+
const e = getExtendedPublicKey(secretKey);
|
|
411
|
+
const rBytes = sha512s(e.prefix, m);
|
|
412
|
+
return hashFinishS(_sign(e, rBytes, m));
|
|
413
|
+
};
|
|
414
|
+
var defaultVerifyOpts = { zip215: true };
|
|
415
|
+
var _verify = (sig, msg, publicKey, options = defaultVerifyOpts) => {
|
|
416
|
+
sig = abytes(sig, 64);
|
|
417
|
+
msg = abytes(msg);
|
|
418
|
+
publicKey = abytes(publicKey, L);
|
|
419
|
+
const { zip215 } = options;
|
|
420
|
+
const r = sig.subarray(0, L);
|
|
421
|
+
const s = bytesToNumberLE(sig.subarray(L, L * 2));
|
|
422
|
+
let A, R, SB;
|
|
423
|
+
let hashable = Uint8Array.of();
|
|
424
|
+
let finished = false;
|
|
425
|
+
try {
|
|
426
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
427
|
+
R = Point.fromBytes(r, zip215);
|
|
428
|
+
SB = G.multiply(s, false);
|
|
429
|
+
hashable = concatBytes(R.toBytes(), A.toBytes(), msg);
|
|
430
|
+
finished = true;
|
|
431
|
+
} catch (error) {
|
|
432
|
+
}
|
|
433
|
+
const finish = (hashed) => {
|
|
434
|
+
if (!finished)
|
|
435
|
+
return false;
|
|
436
|
+
if (!zip215 && A.isSmallOrder())
|
|
437
|
+
return false;
|
|
438
|
+
const k = modL_LE(hashed);
|
|
439
|
+
const RkA = R.add(A.multiply(k, false));
|
|
440
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
441
|
+
};
|
|
442
|
+
return { hashable, finish };
|
|
443
|
+
};
|
|
444
|
+
var verifyAsync = async (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishA(_verify(signature, message, publicKey, opts));
|
|
445
|
+
var verify = (signature, message, publicKey, opts = defaultVerifyOpts) => hashFinishS(_verify(signature, message, publicKey, opts));
|
|
446
|
+
var etc = {
|
|
447
|
+
bytesToHex,
|
|
448
|
+
hexToBytes,
|
|
449
|
+
concatBytes,
|
|
450
|
+
mod: M,
|
|
451
|
+
invert,
|
|
452
|
+
randomBytes
|
|
453
|
+
};
|
|
454
|
+
var hashes = {
|
|
455
|
+
sha512Async: async (message) => {
|
|
456
|
+
const s = subtle();
|
|
457
|
+
const m = concatBytes(message);
|
|
458
|
+
return u8n(await s.digest("SHA-512", m.buffer));
|
|
459
|
+
},
|
|
460
|
+
sha512: void 0
|
|
461
|
+
};
|
|
462
|
+
var randomSecretKey = (seed = randomBytes(L)) => seed;
|
|
463
|
+
var keygen = (seed) => {
|
|
464
|
+
const secretKey = randomSecretKey(seed);
|
|
465
|
+
const publicKey = getPublicKey(secretKey);
|
|
466
|
+
return { secretKey, publicKey };
|
|
467
|
+
};
|
|
468
|
+
var keygenAsync = async (seed) => {
|
|
469
|
+
const secretKey = randomSecretKey(seed);
|
|
470
|
+
const publicKey = await getPublicKeyAsync(secretKey);
|
|
471
|
+
return { secretKey, publicKey };
|
|
472
|
+
};
|
|
473
|
+
var utils = {
|
|
474
|
+
getExtendedPublicKeyAsync,
|
|
475
|
+
getExtendedPublicKey,
|
|
476
|
+
randomSecretKey
|
|
477
|
+
};
|
|
478
|
+
var W = 8;
|
|
479
|
+
var scalarBits = 256;
|
|
480
|
+
var pwindows = Math.ceil(scalarBits / W) + 1;
|
|
481
|
+
var pwindowSize = 2 ** (W - 1);
|
|
482
|
+
var precompute = () => {
|
|
483
|
+
const points = [];
|
|
484
|
+
let p = G;
|
|
485
|
+
let b = p;
|
|
486
|
+
for (let w = 0; w < pwindows; w++) {
|
|
487
|
+
b = p;
|
|
488
|
+
points.push(b);
|
|
489
|
+
for (let i = 1; i < pwindowSize; i++) {
|
|
490
|
+
b = b.add(p);
|
|
491
|
+
points.push(b);
|
|
492
|
+
}
|
|
493
|
+
p = b.double();
|
|
494
|
+
}
|
|
495
|
+
return points;
|
|
496
|
+
};
|
|
497
|
+
var Gpows = void 0;
|
|
498
|
+
var ctneg = (cnd, p) => {
|
|
499
|
+
const n = p.negate();
|
|
500
|
+
return cnd ? n : p;
|
|
501
|
+
};
|
|
502
|
+
var wNAF = (n) => {
|
|
503
|
+
const comp = Gpows || (Gpows = precompute());
|
|
504
|
+
let p = I;
|
|
505
|
+
let f = G;
|
|
506
|
+
const pow_2_w = 2 ** W;
|
|
507
|
+
const maxNum = pow_2_w;
|
|
508
|
+
const mask = big(pow_2_w - 1);
|
|
509
|
+
const shiftBy = big(W);
|
|
510
|
+
for (let w = 0; w < pwindows; w++) {
|
|
511
|
+
let wbits = Number(n & mask);
|
|
512
|
+
n >>= shiftBy;
|
|
513
|
+
if (wbits > pwindowSize) {
|
|
514
|
+
wbits -= maxNum;
|
|
515
|
+
n += 1n;
|
|
516
|
+
}
|
|
517
|
+
const off = w * pwindowSize;
|
|
518
|
+
const offF = off;
|
|
519
|
+
const offP = off + Math.abs(wbits) - 1;
|
|
520
|
+
const isEven = w % 2 !== 0;
|
|
521
|
+
const isNeg = wbits < 0;
|
|
522
|
+
if (wbits === 0) {
|
|
523
|
+
f = f.add(ctneg(isEven, comp[offF]));
|
|
524
|
+
} else {
|
|
525
|
+
p = p.add(ctneg(isNeg, comp[offP]));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (n !== 0n)
|
|
529
|
+
err("invalid wnaf");
|
|
530
|
+
return { p, f };
|
|
531
|
+
};
|
|
532
|
+
export {
|
|
533
|
+
Point,
|
|
534
|
+
etc,
|
|
535
|
+
getPublicKey,
|
|
536
|
+
getPublicKeyAsync,
|
|
537
|
+
hash,
|
|
538
|
+
hashes,
|
|
539
|
+
keygen,
|
|
540
|
+
keygenAsync,
|
|
541
|
+
sign,
|
|
542
|
+
signAsync,
|
|
543
|
+
utils,
|
|
544
|
+
verify,
|
|
545
|
+
verifyAsync
|
|
546
|
+
};
|
|
547
|
+
/*! Bundled license information:
|
|
548
|
+
|
|
549
|
+
@noble/ed25519/index.js:
|
|
550
|
+
(*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
|
|
551
|
+
*/
|