@qntx/wallet-chains 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin-sign.mjs +1 -1
- package/dist/bitcoin.d.mts +7 -6
- package/dist/bitcoin.mjs +45 -212
- package/dist/bitcoin.mjs.map +1 -1
- package/dist/evm-sign.mjs +1 -1
- package/dist/evm.d.mts +4 -5
- package/dist/evm.mjs +28 -3
- package/dist/evm.mjs.map +1 -1
- package/dist/{message-AfNel4WZ.mjs → message-DtIS0ZWB.mjs} +3 -3
- package/dist/{message-AfNel4WZ.mjs.map → message-DtIS0ZWB.mjs.map} +1 -1
- package/dist/solana-sign.mjs +1 -1
- package/dist/solana.d.mts +48 -4
- package/dist/solana.mjs +96 -5
- package/dist/solana.mjs.map +1 -1
- package/dist/{typed-data-D28VBOIm.mjs → typed-data-C6XANSQF.mjs} +4 -266
- package/dist/typed-data-C6XANSQF.mjs.map +1 -0
- package/package.json +5 -2
- package/dist/curve-BeDNVVGm.mjs +0 -2865
- package/dist/curve-BeDNVVGm.mjs.map +0 -1
- package/dist/ed25519-DUtI5nWr.mjs +0 -586
- package/dist/ed25519-DUtI5nWr.mjs.map +0 -1
- package/dist/secp256k1-D0CRzacO.mjs +0 -1384
- package/dist/secp256k1-D0CRzacO.mjs.map +0 -1
- package/dist/typed-data-D28VBOIm.mjs.map +0 -1
|
@@ -1,586 +0,0 @@
|
|
|
1
|
-
import { D as bytesToNumberLE, F as validateObject, L as sha512, M as isBytes, O as concatBytes, P as randomBytes, T as bytesToHex, a as normalizeZ, b as asafenumber, f as isNegativeLE, g as aInRange, h as pow2, j as hexToBytes, k as copyBytes, l as FpLegendre, m as mod, n as createCurveFields, r as createKeygen, s as validatePointCons, t as ScalarMultiplier, v as abool, y as abytes, z as abytes$1 } from "./curve-BeDNVVGm.mjs";
|
|
2
|
-
//#region ../../node_modules/.bun/@noble+curves@2.4.0/node_modules/@noble/curves/abstract/edwards.js
|
|
3
|
-
/**
|
|
4
|
-
* Twisted Edwards curve. The formula is: ax² + y² = 1 + dx²y².
|
|
5
|
-
* For design rationale of types / exports, see weierstrass module documentation.
|
|
6
|
-
* Untwisted Edwards curves exist, but they aren't used in real-world protocols.
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
10
|
-
const _0n = /* @__PURE__ */ BigInt(0);
|
|
11
|
-
const _1n$1 = /* @__PURE__ */ BigInt(1);
|
|
12
|
-
const _2n$1 = /* @__PURE__ */ BigInt(2);
|
|
13
|
-
const _4n = /* @__PURE__ */ BigInt(4);
|
|
14
|
-
const _8n$1 = /* @__PURE__ */ BigInt(8);
|
|
15
|
-
function isEdValidXY(Fp, CURVE, x, y) {
|
|
16
|
-
const x2 = Fp.sqr(x);
|
|
17
|
-
const y2 = Fp.sqr(y);
|
|
18
|
-
const left = Fp.add(Fp.mul(CURVE.a, x2), y2);
|
|
19
|
-
const right = Fp.add(Fp.ONE, Fp.mul(CURVE.d, Fp.mul(x2, y2)));
|
|
20
|
-
return Fp.eql(left, right);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* @param params - Curve parameters. See {@link EdwardsOpts}.
|
|
24
|
-
* @param extraOpts - Optional helpers and overrides. See {@link EdwardsExtraOpts}.
|
|
25
|
-
* @returns Edwards point constructor. Generator validation here only checks
|
|
26
|
-
* that `(Gx, Gy)` satisfies the affine Edwards equation.
|
|
27
|
-
* RFC 8032 base-point constraints like `B != (0,1)` and `[L]B = 0`
|
|
28
|
-
* are left to the caller's chosen parameters, since eager subgroup
|
|
29
|
-
* validation here adds about 10-15ms to heavyweight imports like ed448.
|
|
30
|
-
* The returned constructor also eagerly marks `Point.BASE` for W=6
|
|
31
|
-
* precompute caching. Some code paths still assume
|
|
32
|
-
* `Fp.BYTES === Fn.BYTES`, so mismatched byte lengths are not fully audited here.
|
|
33
|
-
* @throws If the curve parameters or Edwards overrides are invalid. {@link Error}
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* import { edwards } from '@noble/curves/abstract/edwards.js';
|
|
37
|
-
* import { jubjub } from '@noble/curves/misc.js';
|
|
38
|
-
* // Build a point constructor from explicit curve parameters, then use its base point.
|
|
39
|
-
* const Point = edwards(jubjub.Point.CURVE());
|
|
40
|
-
* Point.BASE.toHex();
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
function edwards(params, extraOpts = {}) {
|
|
44
|
-
validateObject(extraOpts, {}, {}, "extraOpts");
|
|
45
|
-
const opts = extraOpts;
|
|
46
|
-
const validated = createCurveFields("edwards", params, opts, opts.FpFnLE);
|
|
47
|
-
const { Fp, Fn } = validated;
|
|
48
|
-
let CURVE = validated.CURVE;
|
|
49
|
-
const { h: cofactor } = CURVE;
|
|
50
|
-
if (FpLegendre(Fp, CURVE.a) !== 1) throw new Error("edwards: CURVE.a must be a square in Fp for complete addition formulas");
|
|
51
|
-
if (FpLegendre(Fp, CURVE.d) !== -1) throw new Error("edwards: CURVE.d must be a non-square in Fp for complete addition formulas");
|
|
52
|
-
validateObject(opts, {}, {
|
|
53
|
-
uvRatio: "function",
|
|
54
|
-
randomBytes: "function"
|
|
55
|
-
});
|
|
56
|
-
const randomBytes$1 = opts.randomBytes === void 0 ? randomBytes : opts.randomBytes;
|
|
57
|
-
const MASK = _2n$1 << BigInt(Fp.BYTES * 8) - _1n$1;
|
|
58
|
-
function isOdd(n) {
|
|
59
|
-
if (!Fp.isOdd) throw new Error("Field does not have .isOdd()");
|
|
60
|
-
return Fp.isOdd(n);
|
|
61
|
-
}
|
|
62
|
-
const uvRatio = opts.uvRatio === void 0 ? (u, v) => {
|
|
63
|
-
try {
|
|
64
|
-
return {
|
|
65
|
-
isValid: true,
|
|
66
|
-
value: Fp.sqrt(Fp.div(u, v))
|
|
67
|
-
};
|
|
68
|
-
} catch (e) {
|
|
69
|
-
return {
|
|
70
|
-
isValid: false,
|
|
71
|
-
value: _0n
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
} : opts.uvRatio;
|
|
75
|
-
if (!isEdValidXY(Fp, CURVE, CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
76
|
-
const mulA = Fp.eql(CURVE.a, Fp.neg(Fp.ONE)) ? (x) => Fp.neg(x) : Fp.eql(CURVE.a, Fp.ONE) ? (x) => x : (x) => Fp.mul(CURVE.a, x);
|
|
77
|
-
/**
|
|
78
|
-
* Asserts coordinate is valid: 0 <= n < MASK.
|
|
79
|
-
* Coordinates >= Fp.ORDER are allowed for zip215.
|
|
80
|
-
*/
|
|
81
|
-
function acoord(title, n, banZero = false) {
|
|
82
|
-
const min = banZero ? _1n$1 : _0n;
|
|
83
|
-
aInRange("coordinate " + title, n, min, MASK);
|
|
84
|
-
return n;
|
|
85
|
-
}
|
|
86
|
-
function aedpoint(other) {
|
|
87
|
-
if (!(other instanceof Point)) throw new Error("EdwardsPoint expected");
|
|
88
|
-
}
|
|
89
|
-
class Point {
|
|
90
|
-
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE, Fp.mul(CURVE.Gx, CURVE.Gy));
|
|
91
|
-
static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ONE, Fp.ZERO);
|
|
92
|
-
static Fp = Fp;
|
|
93
|
-
static Fn = Fn;
|
|
94
|
-
X;
|
|
95
|
-
Y;
|
|
96
|
-
Z;
|
|
97
|
-
T;
|
|
98
|
-
constructor(X, Y, Z, T) {
|
|
99
|
-
this.X = acoord("x", X);
|
|
100
|
-
this.Y = acoord("y", Y);
|
|
101
|
-
this.Z = acoord("z", Z, true);
|
|
102
|
-
this.T = acoord("t", T);
|
|
103
|
-
Object.freeze(this);
|
|
104
|
-
}
|
|
105
|
-
static CURVE() {
|
|
106
|
-
return CURVE;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Create one extended Edwards point from affine coordinates.
|
|
110
|
-
* Does NOT validate that the point is on-curve or torsion-free.
|
|
111
|
-
* Use `.assertValidity()` on adversarial inputs.
|
|
112
|
-
*/
|
|
113
|
-
static fromAffine(p) {
|
|
114
|
-
if (p instanceof Point) throw new Error("extended point not allowed");
|
|
115
|
-
const { x, y } = p || {};
|
|
116
|
-
acoord("x", x);
|
|
117
|
-
acoord("y", y);
|
|
118
|
-
return new Point(x, y, Fp.ONE, Fp.mul(x, y));
|
|
119
|
-
}
|
|
120
|
-
static fromBytes(bytes, zip215 = false) {
|
|
121
|
-
const len = Fp.BYTES;
|
|
122
|
-
const { a, d } = CURVE;
|
|
123
|
-
bytes = copyBytes(abytes(bytes, len, "point"));
|
|
124
|
-
abool(zip215, "zip215");
|
|
125
|
-
const normed = copyBytes(bytes);
|
|
126
|
-
const lastByte = bytes[len - 1];
|
|
127
|
-
normed[len - 1] = lastByte & -129;
|
|
128
|
-
const y = bytesToNumberLE(normed);
|
|
129
|
-
const max = zip215 ? MASK : Fp.ORDER;
|
|
130
|
-
aInRange("point.y", y, _0n, max);
|
|
131
|
-
const y2 = Fp.sqr(y);
|
|
132
|
-
const u = Fp.sub(y2, Fp.ONE);
|
|
133
|
-
const v = Fp.sub(Fp.mulN(d, y2), a);
|
|
134
|
-
let { isValid, value: x } = uvRatio(u, v);
|
|
135
|
-
if (!isValid) throw new Error("bad point: invalid y coordinate");
|
|
136
|
-
const isXOdd = isOdd(x);
|
|
137
|
-
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
138
|
-
if (!zip215 && Fp.is0(x) && isLastByteOdd) throw new Error("bad point: x=0 and x_0=1");
|
|
139
|
-
if (isLastByteOdd !== isXOdd) x = Fp.neg(x);
|
|
140
|
-
return Point.fromAffine({
|
|
141
|
-
x,
|
|
142
|
-
y
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
static fromHex(hex, zip215 = false) {
|
|
146
|
-
return Point.fromBytes(hexToBytes(hex), zip215);
|
|
147
|
-
}
|
|
148
|
-
get x() {
|
|
149
|
-
return this.toAffine().x;
|
|
150
|
-
}
|
|
151
|
-
get y() {
|
|
152
|
-
return this.toAffine().y;
|
|
153
|
-
}
|
|
154
|
-
precompute(windowSize = 6, isLazy = true) {
|
|
155
|
-
wnaf.setWindowSize(this, windowSize);
|
|
156
|
-
if (!isLazy) this.multiply(_2n$1);
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
assertValidity() {
|
|
160
|
-
const p = this;
|
|
161
|
-
const { a, d } = CURVE;
|
|
162
|
-
if (p.is0()) throw new Error("bad point: ZERO");
|
|
163
|
-
const { X, Y, Z, T } = p;
|
|
164
|
-
const X2 = Fp.sqr(X);
|
|
165
|
-
const Y2 = Fp.sqr(Y);
|
|
166
|
-
const Z2 = Fp.sqr(Z);
|
|
167
|
-
const Z4 = Fp.sqr(Z2);
|
|
168
|
-
const aX2 = Fp.mul(X2, a);
|
|
169
|
-
const left = Fp.mul(Fp.add(aX2, Y2), Z2);
|
|
170
|
-
const right = Fp.add(Z4, Fp.mul(d, Fp.mul(X2, Y2)));
|
|
171
|
-
if (!Fp.eql(left, right)) throw new Error("bad point: equation left != right (1)");
|
|
172
|
-
const XY = Fp.mul(X, Y);
|
|
173
|
-
const ZT = Fp.mul(Z, T);
|
|
174
|
-
if (!Fp.eql(XY, ZT)) throw new Error("bad point: equation left != right (2)");
|
|
175
|
-
}
|
|
176
|
-
equals(other) {
|
|
177
|
-
aedpoint(other);
|
|
178
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
179
|
-
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
180
|
-
const X1Z2 = Fp.mul(X1, Z2);
|
|
181
|
-
const X2Z1 = Fp.mul(X2, Z1);
|
|
182
|
-
const Y1Z2 = Fp.mul(Y1, Z2);
|
|
183
|
-
const Y2Z1 = Fp.mul(Y2, Z1);
|
|
184
|
-
return Fp.eql(X1Z2, X2Z1) && Fp.eql(Y1Z2, Y2Z1);
|
|
185
|
-
}
|
|
186
|
-
is0() {
|
|
187
|
-
return this.equals(Point.ZERO);
|
|
188
|
-
}
|
|
189
|
-
negate() {
|
|
190
|
-
return new Point(Fp.neg(this.X), this.Y, this.Z, Fp.neg(this.T));
|
|
191
|
-
}
|
|
192
|
-
double() {
|
|
193
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
194
|
-
const A = Fp.sqr(X1);
|
|
195
|
-
const B = Fp.sqr(Y1);
|
|
196
|
-
const C = Fp.mul(Fp.sqr(Z1), _2n$1);
|
|
197
|
-
const D = mulA(A);
|
|
198
|
-
const x1y1 = Fp.addN(X1, Y1);
|
|
199
|
-
const E = Fp.sub(Fp.subN(Fp.sqr(x1y1), A), B);
|
|
200
|
-
const G = Fp.addN(D, B);
|
|
201
|
-
const F = Fp.subN(G, C);
|
|
202
|
-
const H = Fp.subN(D, B);
|
|
203
|
-
const X3 = Fp.mul(E, F);
|
|
204
|
-
const Y3 = Fp.mul(G, H);
|
|
205
|
-
const T3 = Fp.mul(E, H);
|
|
206
|
-
const Z3 = Fp.mul(F, G);
|
|
207
|
-
return new Point(X3, Y3, Z3, T3);
|
|
208
|
-
}
|
|
209
|
-
add(other) {
|
|
210
|
-
aedpoint(other);
|
|
211
|
-
const { d } = CURVE;
|
|
212
|
-
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
213
|
-
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
214
|
-
const A = Fp.mul(X1, X2);
|
|
215
|
-
const B = Fp.mul(Y1, Y2);
|
|
216
|
-
const C = Fp.mul(Fp.mulN(T1, d), T2);
|
|
217
|
-
const D = Fp.mul(Z1, Z2);
|
|
218
|
-
const E = Fp.sub(Fp.subN(Fp.mulN(Fp.addN(X1, Y1), Fp.addN(X2, Y2)), A), B);
|
|
219
|
-
const F = Fp.subN(D, C);
|
|
220
|
-
const G = Fp.addN(D, C);
|
|
221
|
-
const H = Fp.sub(B, mulA(A));
|
|
222
|
-
const X3 = Fp.mul(E, F);
|
|
223
|
-
const Y3 = Fp.mul(G, H);
|
|
224
|
-
const T3 = Fp.mul(E, H);
|
|
225
|
-
const Z3 = Fp.mul(F, G);
|
|
226
|
-
return new Point(X3, Y3, Z3, T3);
|
|
227
|
-
}
|
|
228
|
-
subtract(other) {
|
|
229
|
-
aedpoint(other);
|
|
230
|
-
return this.add(other.negate());
|
|
231
|
-
}
|
|
232
|
-
multiply(scalar) {
|
|
233
|
-
if (!Fn.isValidNot0(scalar)) throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
234
|
-
const { p, f } = wnaf.mulSecret(this, scalar, cofactor, normalize);
|
|
235
|
-
return normalize([p, f])[0];
|
|
236
|
-
}
|
|
237
|
-
multiplyUnsafe(scalar) {
|
|
238
|
-
if (!Fn.isValid(scalar)) throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
239
|
-
if (scalar === _0n) return Point.ZERO;
|
|
240
|
-
if (this.is0() || scalar === _1n$1) return this;
|
|
241
|
-
return wnaf.mulUnsafe(this, scalar, normalize);
|
|
242
|
-
}
|
|
243
|
-
isSmallOrder() {
|
|
244
|
-
return this.clearCofactor().is0();
|
|
245
|
-
}
|
|
246
|
-
isTorsionFree() {
|
|
247
|
-
return wnaf.mulUnsafe(this, CURVE.n).is0();
|
|
248
|
-
}
|
|
249
|
-
toAffine(invertedZ) {
|
|
250
|
-
const p = this;
|
|
251
|
-
let iz = invertedZ;
|
|
252
|
-
if (iz != null && typeof iz !== "bigint") throw new TypeError("\"invertedZ\" expected bigint, got type=" + typeof iz);
|
|
253
|
-
const { X, Y, Z } = p;
|
|
254
|
-
const is0 = p.is0();
|
|
255
|
-
if (iz == null) iz = is0 ? Fp.create(_8n$1) : Fp.inv(Z);
|
|
256
|
-
const x = Fp.mul(X, iz);
|
|
257
|
-
const y = Fp.mul(Y, iz);
|
|
258
|
-
const zz = Fp.mul(Z, iz);
|
|
259
|
-
if (is0) return {
|
|
260
|
-
x: Fp.ZERO,
|
|
261
|
-
y: Fp.ONE
|
|
262
|
-
};
|
|
263
|
-
if (!Fp.eql(zz, Fp.ONE)) throw new Error("invZ was invalid");
|
|
264
|
-
return {
|
|
265
|
-
x,
|
|
266
|
-
y
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
clearCofactor() {
|
|
270
|
-
if (cofactor === _1n$1) return this;
|
|
271
|
-
if (cofactor === _2n$1) return this.double();
|
|
272
|
-
if (cofactor === _4n) return this.double().double();
|
|
273
|
-
if (cofactor === _8n$1) return this.double().double().double();
|
|
274
|
-
return this.multiplyUnsafe(cofactor);
|
|
275
|
-
}
|
|
276
|
-
toBytes() {
|
|
277
|
-
const { x, y } = this.toAffine();
|
|
278
|
-
const bytes = Fp.toBytes(y);
|
|
279
|
-
bytes[bytes.length - 1] |= isOdd(x) ? 128 : 0;
|
|
280
|
-
return bytes;
|
|
281
|
-
}
|
|
282
|
-
toHex() {
|
|
283
|
-
return bytesToHex(this.toBytes());
|
|
284
|
-
}
|
|
285
|
-
toString() {
|
|
286
|
-
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
const normalize = (points) => normalizeZ(Point, points);
|
|
290
|
-
const wnaf = new ScalarMultiplier(Point, randomBytes$1);
|
|
291
|
-
if (wnaf.bits >= 6) Point.BASE.precompute(6);
|
|
292
|
-
Object.freeze(Point.prototype);
|
|
293
|
-
Object.freeze(Point);
|
|
294
|
-
return Point;
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Initializes EdDSA signatures over given Edwards curve.
|
|
298
|
-
* @param Point - Edwards point constructor.
|
|
299
|
-
* @param cHash - Hash function.
|
|
300
|
-
* @param eddsaOpts - Optional signature helpers. See {@link EdDSAOpts}.
|
|
301
|
-
* @returns EdDSA helper namespace.
|
|
302
|
-
* @throws If the hash function, options, or derived point operations are invalid. {@link Error}
|
|
303
|
-
* @example
|
|
304
|
-
* Initializes EdDSA signatures over given Edwards curve.
|
|
305
|
-
*
|
|
306
|
-
* ```ts
|
|
307
|
-
* import { eddsa } from '@noble/curves/abstract/edwards.js';
|
|
308
|
-
* import { jubjub } from '@noble/curves/misc.js';
|
|
309
|
-
* import { sha512 } from '@noble/hashes/sha2.js';
|
|
310
|
-
* const sigs = eddsa(jubjub.Point, sha512);
|
|
311
|
-
* const { secretKey, publicKey } = sigs.keygen();
|
|
312
|
-
* const msg = new TextEncoder().encode('hello noble');
|
|
313
|
-
* const sig = sigs.sign(msg, secretKey);
|
|
314
|
-
* const isValid = sigs.verify(sig, msg, publicKey);
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
318
|
-
validatePointCons(Point);
|
|
319
|
-
if (typeof cHash !== "function") throw new Error("\"hash\" function param is required");
|
|
320
|
-
const hash = cHash;
|
|
321
|
-
const opts = eddsaOpts;
|
|
322
|
-
validateObject(opts, {}, {
|
|
323
|
-
adjustScalarBytes: "function",
|
|
324
|
-
randomBytes: "function",
|
|
325
|
-
domain: "function",
|
|
326
|
-
prehash: "function",
|
|
327
|
-
zip215: "boolean",
|
|
328
|
-
mapToCurve: "function",
|
|
329
|
-
toMontgomery: "function",
|
|
330
|
-
toMontgomerySecret: "function"
|
|
331
|
-
});
|
|
332
|
-
const { prehash } = opts;
|
|
333
|
-
const { BASE, Fp, Fn } = Point;
|
|
334
|
-
const outputLen = hash.outputLen;
|
|
335
|
-
const expectedLen = 2 * Fp.BYTES;
|
|
336
|
-
if (outputLen !== void 0) {
|
|
337
|
-
asafenumber(outputLen, "hash.outputLen");
|
|
338
|
-
if (outputLen !== expectedLen) throw new Error(`hash.outputLen must be ${expectedLen}, got ${outputLen}`);
|
|
339
|
-
}
|
|
340
|
-
const randomBytes$2 = opts.randomBytes === void 0 ? randomBytes : opts.randomBytes;
|
|
341
|
-
const toMontgomery = opts.toMontgomery;
|
|
342
|
-
const toMontgomerySecret = opts.toMontgomerySecret;
|
|
343
|
-
const adjustScalarBytes = opts.adjustScalarBytes === void 0 ? (bytes) => bytes : opts.adjustScalarBytes;
|
|
344
|
-
const domain = opts.domain === void 0 ? (data, ctx, phflag) => {
|
|
345
|
-
abool(phflag, "phflag");
|
|
346
|
-
if (ctx.length || phflag) throw new Error("Contexts/pre-hash are not supported");
|
|
347
|
-
return data;
|
|
348
|
-
} : opts.domain;
|
|
349
|
-
function modN_LE(hash) {
|
|
350
|
-
return Fn.create(bytesToNumberLE(hash));
|
|
351
|
-
}
|
|
352
|
-
function getPrivateScalar(key) {
|
|
353
|
-
const len = lengths.secretKey;
|
|
354
|
-
abytes(key, lengths.secretKey, "secretKey");
|
|
355
|
-
const hashed = abytes(hash(key), 2 * len, "hashedSecretKey");
|
|
356
|
-
const head = adjustScalarBytes(hashed.slice(0, len));
|
|
357
|
-
return {
|
|
358
|
-
head,
|
|
359
|
-
prefix: hashed.slice(len, 2 * len),
|
|
360
|
-
scalar: modN_LE(head)
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
/** Convenience method that creates public key from scalar. RFC8032 5.1.5
|
|
364
|
-
* Also exposes the derived scalar/prefix tuple and point form reused by sign().
|
|
365
|
-
*/
|
|
366
|
-
function getExtendedPublicKey(secretKey) {
|
|
367
|
-
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
368
|
-
const point = BASE.multiply(scalar);
|
|
369
|
-
return {
|
|
370
|
-
head,
|
|
371
|
-
prefix,
|
|
372
|
-
scalar,
|
|
373
|
-
point,
|
|
374
|
-
pointBytes: point.toBytes()
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
/** Calculates EdDSA pub key. RFC8032 5.1.5. */
|
|
378
|
-
function getPublicKey(secretKey) {
|
|
379
|
-
return getExtendedPublicKey(secretKey).pointBytes;
|
|
380
|
-
}
|
|
381
|
-
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
382
|
-
const msg = concatBytes(...msgs);
|
|
383
|
-
return modN_LE(hash(domain(msg, abytes(context, void 0, "context"), !!prehash)));
|
|
384
|
-
}
|
|
385
|
-
/** Signs message with secret key. RFC8032 5.1.6 */
|
|
386
|
-
function sign(msg, secretKey, options = {}) {
|
|
387
|
-
validateObject(options, {}, {}, "options");
|
|
388
|
-
msg = copyBytes(abytes(msg, void 0, "message"));
|
|
389
|
-
if (prehash) msg = prehash(msg);
|
|
390
|
-
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
391
|
-
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
392
|
-
const R = BASE.multiply(r).toBytes();
|
|
393
|
-
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
394
|
-
const s = Fn.create(r + k * scalar);
|
|
395
|
-
if (!Fn.isValid(s)) throw new Error("sign failed: invalid s");
|
|
396
|
-
const rs = concatBytes(R, Fn.toBytes(s));
|
|
397
|
-
return abytes(rs, lengths.signature, "result");
|
|
398
|
-
}
|
|
399
|
-
const verifyOpts = { zip215: opts.zip215 };
|
|
400
|
-
/**
|
|
401
|
-
* Verifies EdDSA signature against message and public key. RFC 8032 §§5.1.7 and 5.2.7.
|
|
402
|
-
* A cofactored verification equation is checked.
|
|
403
|
-
*/
|
|
404
|
-
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
405
|
-
validateObject(options);
|
|
406
|
-
const { context } = options;
|
|
407
|
-
const zip215 = options.zip215 === void 0 ? !!verifyOpts.zip215 : options.zip215;
|
|
408
|
-
const len = lengths.signature;
|
|
409
|
-
sig = abytes(sig, len, "signature");
|
|
410
|
-
msg = abytes(msg, void 0, "message");
|
|
411
|
-
publicKey = abytes(publicKey, lengths.publicKey, "publicKey");
|
|
412
|
-
if (zip215 !== void 0) abool(zip215, "zip215");
|
|
413
|
-
if (prehash) msg = prehash(msg);
|
|
414
|
-
const mid = len / 2;
|
|
415
|
-
const r = sig.subarray(0, mid);
|
|
416
|
-
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
417
|
-
let A, R, SB;
|
|
418
|
-
try {
|
|
419
|
-
A = Point.fromBytes(publicKey, zip215);
|
|
420
|
-
R = Point.fromBytes(r, zip215);
|
|
421
|
-
SB = BASE.multiplyUnsafe(s);
|
|
422
|
-
} catch (error) {
|
|
423
|
-
return false;
|
|
424
|
-
}
|
|
425
|
-
if (!zip215 && A.isSmallOrder()) return false;
|
|
426
|
-
const k = hashDomainToScalar(context, r, publicKey, msg);
|
|
427
|
-
return R.add(A.multiplyUnsafe(k)).subtract(SB).clearCofactor().is0();
|
|
428
|
-
}
|
|
429
|
-
const _size = Fp.BYTES;
|
|
430
|
-
const lengths = {
|
|
431
|
-
secretKey: _size,
|
|
432
|
-
publicKey: _size,
|
|
433
|
-
signature: 2 * _size,
|
|
434
|
-
seed: _size
|
|
435
|
-
};
|
|
436
|
-
function randomSecretKey(seed) {
|
|
437
|
-
seed = seed === void 0 ? randomBytes$2(lengths.seed) : seed;
|
|
438
|
-
return abytes(seed, lengths.seed, "seed");
|
|
439
|
-
}
|
|
440
|
-
function isValidSecretKey(key) {
|
|
441
|
-
return isBytes(key) && key.length === lengths.secretKey;
|
|
442
|
-
}
|
|
443
|
-
function isValidPublicKey(key, zip215) {
|
|
444
|
-
try {
|
|
445
|
-
return !!Point.fromBytes(key, zip215 === void 0 ? verifyOpts.zip215 : zip215);
|
|
446
|
-
} catch (error) {
|
|
447
|
-
return false;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
const utils = {
|
|
451
|
-
getExtendedPublicKey,
|
|
452
|
-
randomSecretKey,
|
|
453
|
-
isValidSecretKey,
|
|
454
|
-
isValidPublicKey,
|
|
455
|
-
/** Converts an Edwards public key to a companion Montgomery public key. */
|
|
456
|
-
toMontgomery(publicKey) {
|
|
457
|
-
if (toMontgomery === void 0) throw new Error("Montgomery conversion is not supported for this curve");
|
|
458
|
-
return toMontgomery(Point.fromBytes(publicKey));
|
|
459
|
-
},
|
|
460
|
-
toMontgomerySecret(secretKey) {
|
|
461
|
-
if (toMontgomerySecret === void 0) throw new Error("Montgomery conversion is not supported for this curve");
|
|
462
|
-
return toMontgomerySecret(secretKey);
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
Object.freeze(lengths);
|
|
466
|
-
Object.freeze(utils);
|
|
467
|
-
return Object.freeze({
|
|
468
|
-
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
469
|
-
getPublicKey,
|
|
470
|
-
sign,
|
|
471
|
-
verify,
|
|
472
|
-
utils,
|
|
473
|
-
Point,
|
|
474
|
-
lengths
|
|
475
|
-
});
|
|
476
|
-
}
|
|
477
|
-
//#endregion
|
|
478
|
-
//#region ../../node_modules/.bun/@noble+curves@2.4.0/node_modules/@noble/curves/ed25519.js
|
|
479
|
-
/**
|
|
480
|
-
* ed25519 Twisted Edwards curve with following addons:
|
|
481
|
-
* - X25519 ECDH
|
|
482
|
-
* - Ristretto cofactor elimination
|
|
483
|
-
* - Elligator hash-to-group / point indistinguishability
|
|
484
|
-
* @module
|
|
485
|
-
*/
|
|
486
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
487
|
-
const _1n = /* @__PURE__ */ BigInt(1);
|
|
488
|
-
const _2n = /* @__PURE__ */ BigInt(2);
|
|
489
|
-
const _5n = /* @__PURE__ */ BigInt(5);
|
|
490
|
-
const _8n = /* @__PURE__ */ BigInt(8);
|
|
491
|
-
const ed25519_CURVE_p = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
492
|
-
const ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
493
|
-
p: ed25519_CURVE_p,
|
|
494
|
-
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
495
|
-
h: _8n,
|
|
496
|
-
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
497
|
-
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
498
|
-
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
499
|
-
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
500
|
-
}))();
|
|
501
|
-
function ed25519_pow_2_252_3(x) {
|
|
502
|
-
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
503
|
-
const P = ed25519_CURVE_p;
|
|
504
|
-
const b2 = x * x % P * x % P;
|
|
505
|
-
const b4 = pow2(b2, _2n, P) * b2 % P;
|
|
506
|
-
const b5 = pow2(b4, _1n, P) * x % P;
|
|
507
|
-
const b10 = pow2(b5, _5n, P) * b5 % P;
|
|
508
|
-
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
509
|
-
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
510
|
-
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
511
|
-
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
512
|
-
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
513
|
-
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
514
|
-
return {
|
|
515
|
-
pow_p_5_8: pow2(b250, _2n, P) * x % P,
|
|
516
|
-
b2
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
function adjustScalarBytes(bytes) {
|
|
520
|
-
bytes[0] &= 248;
|
|
521
|
-
bytes[31] &= 127;
|
|
522
|
-
bytes[31] |= 64;
|
|
523
|
-
return bytes;
|
|
524
|
-
}
|
|
525
|
-
const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
526
|
-
function uvRatio(u, v) {
|
|
527
|
-
const P = ed25519_CURVE_p;
|
|
528
|
-
const v3 = mod(v * v * v, P);
|
|
529
|
-
const pow = ed25519_pow_2_252_3(u * mod(v3 * v3 * v, P)).pow_p_5_8;
|
|
530
|
-
let x = mod(u * v3 * pow, P);
|
|
531
|
-
const vx2 = mod(v * x * x, P);
|
|
532
|
-
const root1 = x;
|
|
533
|
-
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
534
|
-
const useRoot1 = vx2 === u;
|
|
535
|
-
const useRoot2 = vx2 === mod(-u, P);
|
|
536
|
-
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
537
|
-
if (useRoot1) x = root1;
|
|
538
|
-
if (useRoot2 || noRoot) x = root2;
|
|
539
|
-
if (isNegativeLE(x, P)) x = mod(-x, P);
|
|
540
|
-
return {
|
|
541
|
-
isValid: useRoot1 || useRoot2,
|
|
542
|
-
value: x
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
const ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
546
|
-
const Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
547
|
-
function toMontgomery(point) {
|
|
548
|
-
const { y } = point;
|
|
549
|
-
return Fp.toBytes(Fp.div(_1n + y, _1n - y));
|
|
550
|
-
}
|
|
551
|
-
function toMontgomerySecret(secretKey) {
|
|
552
|
-
const size = ed25519_Point.Fp.BYTES;
|
|
553
|
-
abytes$1(secretKey, size);
|
|
554
|
-
return adjustScalarBytes(sha512(secretKey.subarray(0, size))).subarray(0, size);
|
|
555
|
-
}
|
|
556
|
-
function ed(opts) {
|
|
557
|
-
return eddsa(ed25519_Point, sha512, Object.assign({
|
|
558
|
-
adjustScalarBytes,
|
|
559
|
-
toMontgomery,
|
|
560
|
-
toMontgomerySecret,
|
|
561
|
-
zip215: true
|
|
562
|
-
}, opts));
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* ed25519 curve with EdDSA signatures.
|
|
566
|
-
* Seeded `keygen(seed)` / `utils.randomSecretKey(seed)` reuse the provided
|
|
567
|
-
* 32-byte seed buffer instead of copying it.
|
|
568
|
-
* @example
|
|
569
|
-
* Generate one Ed25519 keypair, sign a message, and verify it.
|
|
570
|
-
*
|
|
571
|
-
* ```js
|
|
572
|
-
* import { ed25519 } from '@noble/curves/ed25519.js';
|
|
573
|
-
* const { secretKey, publicKey } = ed25519.keygen();
|
|
574
|
-
* // const publicKey = ed25519.getPublicKey(secretKey);
|
|
575
|
-
* const msg = new TextEncoder().encode('hello noble');
|
|
576
|
-
* const sig = ed25519.sign(msg, secretKey);
|
|
577
|
-
* const isValid = ed25519.verify(sig, msg, publicKey); // ZIP215
|
|
578
|
-
* // RFC8032 / FIPS 186-5
|
|
579
|
-
* const isValid2 = ed25519.verify(sig, msg, publicKey, { zip215: false });
|
|
580
|
-
* ```
|
|
581
|
-
*/
|
|
582
|
-
const ed25519 = /* @__PURE__ */ ed({});
|
|
583
|
-
//#endregion
|
|
584
|
-
export { ed25519 as t };
|
|
585
|
-
|
|
586
|
-
//# sourceMappingURL=ed25519-DUtI5nWr.mjs.map
|