@fangorn-network/sdk 2026.7.14 → 2026.7.27
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/lib/cli/cli.js +28 -33
- package/lib/cli/index.d.ts +13 -13
- package/lib/config.d.ts +76 -76
- package/lib/crypto/aes.js +1 -1
- package/lib/crypto/encryption.d.ts +2 -2
- package/lib/crypto/encryption.js +3 -3
- package/lib/engine/index.js +1 -1
- package/lib/fangorn.d.ts +8 -2
- package/lib/fangorn.js +29 -13
- package/lib/index.d.ts +5 -2
- package/lib/index.js +4 -2
- package/lib/providers/storage/car.js +1 -1
- package/lib/providers/storage/index.d.ts +2 -1
- package/lib/providers/storage/index.js +2 -1
- package/lib/providers/storage/pinata.js +23 -22
- package/lib/providers/storage/signed-url.d.ts +52 -0
- package/lib/providers/storage/signed-url.js +119 -0
- package/lib/providers/storage/utils.d.ts +19 -1
- package/lib/providers/storage/utils.js +55 -1
- package/lib/types/index.d.ts +5 -0
- package/package.json +3 -1
- package/lib/node_modules/@noble/ciphers/esm/_polyval.js +0 -218
- package/lib/node_modules/@noble/ciphers/esm/aes.js +0 -261
- package/lib/node_modules/@noble/ciphers/esm/utils.js +0 -156
- package/lib/node_modules/@noble/curves/esm/abstract/curve.js +0 -299
- package/lib/node_modules/@noble/curves/esm/abstract/edwards.js +0 -532
- package/lib/node_modules/@noble/curves/esm/abstract/modular.js +0 -344
- package/lib/node_modules/@noble/curves/esm/abstract/montgomery.js +0 -137
- package/lib/node_modules/@noble/curves/esm/ed25519.js +0 -262
- package/lib/node_modules/@noble/curves/esm/utils.js +0 -150
|
@@ -1,532 +0,0 @@
|
|
|
1
|
-
import { _abool2, _abytes2, _validateObject, aInRange, bytesToHex, bytesToNumberLE, concatBytes, copyBytes, ensureBytes, isBytes, memoized, notImplemented, randomBytes } from "../utils.js";
|
|
2
|
-
import { Field } from "./modular.js";
|
|
3
|
-
import { _createCurveFields, normalizeZ, pippenger, wNAF } from "./curve.js";
|
|
4
|
-
|
|
5
|
-
//#region node_modules/@noble/curves/esm/abstract/edwards.js
|
|
6
|
-
/**
|
|
7
|
-
* Twisted Edwards curve. The formula is: ax² + y² = 1 + dx²y².
|
|
8
|
-
* For design rationale of types / exports, see weierstrass module documentation.
|
|
9
|
-
* Untwisted Edwards curves exist, but they aren't used in real-world protocols.
|
|
10
|
-
* @module
|
|
11
|
-
*/
|
|
12
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
13
|
-
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _8n = BigInt(8);
|
|
14
|
-
function isEdValidXY(Fp, CURVE, x, y) {
|
|
15
|
-
const x2 = Fp.sqr(x);
|
|
16
|
-
const y2 = Fp.sqr(y);
|
|
17
|
-
const left = Fp.add(Fp.mul(CURVE.a, x2), y2);
|
|
18
|
-
const right = Fp.add(Fp.ONE, Fp.mul(CURVE.d, Fp.mul(x2, y2)));
|
|
19
|
-
return Fp.eql(left, right);
|
|
20
|
-
}
|
|
21
|
-
function edwards(params, extraOpts = {}) {
|
|
22
|
-
const validated = _createCurveFields("edwards", params, extraOpts, extraOpts.FpFnLE);
|
|
23
|
-
const { Fp, Fn } = validated;
|
|
24
|
-
let CURVE = validated.CURVE;
|
|
25
|
-
const { h: cofactor } = CURVE;
|
|
26
|
-
_validateObject(extraOpts, {}, { uvRatio: "function" });
|
|
27
|
-
const MASK = _2n << BigInt(Fn.BYTES * 8) - _1n;
|
|
28
|
-
const modP = (n) => Fp.create(n);
|
|
29
|
-
const uvRatio = extraOpts.uvRatio || ((u, v) => {
|
|
30
|
-
try {
|
|
31
|
-
return {
|
|
32
|
-
isValid: true,
|
|
33
|
-
value: Fp.sqrt(Fp.div(u, v))
|
|
34
|
-
};
|
|
35
|
-
} catch (e) {
|
|
36
|
-
return {
|
|
37
|
-
isValid: false,
|
|
38
|
-
value: _0n
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
if (!isEdValidXY(Fp, CURVE, CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
43
|
-
/**
|
|
44
|
-
* Asserts coordinate is valid: 0 <= n < MASK.
|
|
45
|
-
* Coordinates >= Fp.ORDER are allowed for zip215.
|
|
46
|
-
*/
|
|
47
|
-
function acoord(title, n, banZero = false) {
|
|
48
|
-
const min = banZero ? _1n : _0n;
|
|
49
|
-
aInRange("coordinate " + title, n, min, MASK);
|
|
50
|
-
return n;
|
|
51
|
-
}
|
|
52
|
-
function aextpoint(other) {
|
|
53
|
-
if (!(other instanceof Point)) throw new Error("ExtendedPoint expected");
|
|
54
|
-
}
|
|
55
|
-
const toAffineMemo = memoized((p, iz) => {
|
|
56
|
-
const { X, Y, Z } = p;
|
|
57
|
-
const is0 = p.is0();
|
|
58
|
-
if (iz == null) iz = is0 ? _8n : Fp.inv(Z);
|
|
59
|
-
const x = modP(X * iz);
|
|
60
|
-
const y = modP(Y * iz);
|
|
61
|
-
const zz = Fp.mul(Z, iz);
|
|
62
|
-
if (is0) return {
|
|
63
|
-
x: _0n,
|
|
64
|
-
y: _1n
|
|
65
|
-
};
|
|
66
|
-
if (zz !== _1n) throw new Error("invZ was invalid");
|
|
67
|
-
return {
|
|
68
|
-
x,
|
|
69
|
-
y
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
const assertValidMemo = memoized((p) => {
|
|
73
|
-
const { a, d } = CURVE;
|
|
74
|
-
if (p.is0()) throw new Error("bad point: ZERO");
|
|
75
|
-
const { X, Y, Z, T } = p;
|
|
76
|
-
const X2 = modP(X * X);
|
|
77
|
-
const Y2 = modP(Y * Y);
|
|
78
|
-
const Z2 = modP(Z * Z);
|
|
79
|
-
const Z4 = modP(Z2 * Z2);
|
|
80
|
-
if (modP(Z2 * modP(modP(X2 * a) + Y2)) !== modP(Z4 + modP(d * modP(X2 * Y2)))) throw new Error("bad point: equation left != right (1)");
|
|
81
|
-
if (modP(X * Y) !== modP(Z * T)) throw new Error("bad point: equation left != right (2)");
|
|
82
|
-
return true;
|
|
83
|
-
});
|
|
84
|
-
class Point {
|
|
85
|
-
constructor(X, Y, Z, T) {
|
|
86
|
-
this.X = acoord("x", X);
|
|
87
|
-
this.Y = acoord("y", Y);
|
|
88
|
-
this.Z = acoord("z", Z, true);
|
|
89
|
-
this.T = acoord("t", T);
|
|
90
|
-
Object.freeze(this);
|
|
91
|
-
}
|
|
92
|
-
static CURVE() {
|
|
93
|
-
return CURVE;
|
|
94
|
-
}
|
|
95
|
-
static fromAffine(p) {
|
|
96
|
-
if (p instanceof Point) throw new Error("extended point not allowed");
|
|
97
|
-
const { x, y } = p || {};
|
|
98
|
-
acoord("x", x);
|
|
99
|
-
acoord("y", y);
|
|
100
|
-
return new Point(x, y, _1n, modP(x * y));
|
|
101
|
-
}
|
|
102
|
-
static fromBytes(bytes, zip215 = false) {
|
|
103
|
-
const len = Fp.BYTES;
|
|
104
|
-
const { a, d } = CURVE;
|
|
105
|
-
bytes = copyBytes(_abytes2(bytes, len, "point"));
|
|
106
|
-
_abool2(zip215, "zip215");
|
|
107
|
-
const normed = copyBytes(bytes);
|
|
108
|
-
const lastByte = bytes[len - 1];
|
|
109
|
-
normed[len - 1] = lastByte & -129;
|
|
110
|
-
const y = bytesToNumberLE(normed);
|
|
111
|
-
aInRange("point.y", y, _0n, zip215 ? MASK : Fp.ORDER);
|
|
112
|
-
const y2 = modP(y * y);
|
|
113
|
-
let { isValid, value: x } = uvRatio(modP(y2 - _1n), modP(d * y2 - a));
|
|
114
|
-
if (!isValid) throw new Error("bad point: invalid y coordinate");
|
|
115
|
-
const isXOdd = (x & _1n) === _1n;
|
|
116
|
-
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
117
|
-
if (!zip215 && x === _0n && isLastByteOdd) throw new Error("bad point: x=0 and x_0=1");
|
|
118
|
-
if (isLastByteOdd !== isXOdd) x = modP(-x);
|
|
119
|
-
return Point.fromAffine({
|
|
120
|
-
x,
|
|
121
|
-
y
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
static fromHex(bytes, zip215 = false) {
|
|
125
|
-
return Point.fromBytes(ensureBytes("point", bytes), zip215);
|
|
126
|
-
}
|
|
127
|
-
get x() {
|
|
128
|
-
return this.toAffine().x;
|
|
129
|
-
}
|
|
130
|
-
get y() {
|
|
131
|
-
return this.toAffine().y;
|
|
132
|
-
}
|
|
133
|
-
precompute(windowSize = 8, isLazy = true) {
|
|
134
|
-
wnaf.createCache(this, windowSize);
|
|
135
|
-
if (!isLazy) this.multiply(_2n);
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
assertValidity() {
|
|
139
|
-
assertValidMemo(this);
|
|
140
|
-
}
|
|
141
|
-
equals(other) {
|
|
142
|
-
aextpoint(other);
|
|
143
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
144
|
-
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
145
|
-
const X1Z2 = modP(X1 * Z2);
|
|
146
|
-
const X2Z1 = modP(X2 * Z1);
|
|
147
|
-
const Y1Z2 = modP(Y1 * Z2);
|
|
148
|
-
const Y2Z1 = modP(Y2 * Z1);
|
|
149
|
-
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
150
|
-
}
|
|
151
|
-
is0() {
|
|
152
|
-
return this.equals(Point.ZERO);
|
|
153
|
-
}
|
|
154
|
-
negate() {
|
|
155
|
-
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
156
|
-
}
|
|
157
|
-
double() {
|
|
158
|
-
const { a } = CURVE;
|
|
159
|
-
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
160
|
-
const A = modP(X1 * X1);
|
|
161
|
-
const B = modP(Y1 * Y1);
|
|
162
|
-
const C = modP(_2n * modP(Z1 * Z1));
|
|
163
|
-
const D = modP(a * A);
|
|
164
|
-
const x1y1 = X1 + Y1;
|
|
165
|
-
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
166
|
-
const G = D + B;
|
|
167
|
-
const F = G - C;
|
|
168
|
-
const H = D - B;
|
|
169
|
-
const X3 = modP(E * F);
|
|
170
|
-
const Y3 = modP(G * H);
|
|
171
|
-
const T3 = modP(E * H);
|
|
172
|
-
return new Point(X3, Y3, modP(F * G), T3);
|
|
173
|
-
}
|
|
174
|
-
add(other) {
|
|
175
|
-
aextpoint(other);
|
|
176
|
-
const { a, d } = CURVE;
|
|
177
|
-
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
178
|
-
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
179
|
-
const A = modP(X1 * X2);
|
|
180
|
-
const B = modP(Y1 * Y2);
|
|
181
|
-
const C = modP(T1 * d * T2);
|
|
182
|
-
const D = modP(Z1 * Z2);
|
|
183
|
-
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
184
|
-
const F = D - C;
|
|
185
|
-
const G = D + C;
|
|
186
|
-
const H = modP(B - a * A);
|
|
187
|
-
const X3 = modP(E * F);
|
|
188
|
-
const Y3 = modP(G * H);
|
|
189
|
-
const T3 = modP(E * H);
|
|
190
|
-
return new Point(X3, Y3, modP(F * G), T3);
|
|
191
|
-
}
|
|
192
|
-
subtract(other) {
|
|
193
|
-
return this.add(other.negate());
|
|
194
|
-
}
|
|
195
|
-
multiply(scalar) {
|
|
196
|
-
if (!Fn.isValidNot0(scalar)) throw new Error("invalid scalar: expected 1 <= sc < curve.n");
|
|
197
|
-
const { p, f } = wnaf.cached(this, scalar, (p$1) => normalizeZ(Point, p$1));
|
|
198
|
-
return normalizeZ(Point, [p, f])[0];
|
|
199
|
-
}
|
|
200
|
-
multiplyUnsafe(scalar, acc = Point.ZERO) {
|
|
201
|
-
if (!Fn.isValid(scalar)) throw new Error("invalid scalar: expected 0 <= sc < curve.n");
|
|
202
|
-
if (scalar === _0n) return Point.ZERO;
|
|
203
|
-
if (this.is0() || scalar === _1n) return this;
|
|
204
|
-
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p), acc);
|
|
205
|
-
}
|
|
206
|
-
isSmallOrder() {
|
|
207
|
-
return this.multiplyUnsafe(cofactor).is0();
|
|
208
|
-
}
|
|
209
|
-
isTorsionFree() {
|
|
210
|
-
return wnaf.unsafe(this, CURVE.n).is0();
|
|
211
|
-
}
|
|
212
|
-
toAffine(invertedZ) {
|
|
213
|
-
return toAffineMemo(this, invertedZ);
|
|
214
|
-
}
|
|
215
|
-
clearCofactor() {
|
|
216
|
-
if (cofactor === _1n) return this;
|
|
217
|
-
return this.multiplyUnsafe(cofactor);
|
|
218
|
-
}
|
|
219
|
-
toBytes() {
|
|
220
|
-
const { x, y } = this.toAffine();
|
|
221
|
-
const bytes = Fp.toBytes(y);
|
|
222
|
-
bytes[bytes.length - 1] |= x & _1n ? 128 : 0;
|
|
223
|
-
return bytes;
|
|
224
|
-
}
|
|
225
|
-
toHex() {
|
|
226
|
-
return bytesToHex(this.toBytes());
|
|
227
|
-
}
|
|
228
|
-
toString() {
|
|
229
|
-
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
230
|
-
}
|
|
231
|
-
get ex() {
|
|
232
|
-
return this.X;
|
|
233
|
-
}
|
|
234
|
-
get ey() {
|
|
235
|
-
return this.Y;
|
|
236
|
-
}
|
|
237
|
-
get ez() {
|
|
238
|
-
return this.Z;
|
|
239
|
-
}
|
|
240
|
-
get et() {
|
|
241
|
-
return this.T;
|
|
242
|
-
}
|
|
243
|
-
static normalizeZ(points) {
|
|
244
|
-
return normalizeZ(Point, points);
|
|
245
|
-
}
|
|
246
|
-
static msm(points, scalars) {
|
|
247
|
-
return pippenger(Point, Fn, points, scalars);
|
|
248
|
-
}
|
|
249
|
-
_setWindowSize(windowSize) {
|
|
250
|
-
this.precompute(windowSize);
|
|
251
|
-
}
|
|
252
|
-
toRawBytes() {
|
|
253
|
-
return this.toBytes();
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, _1n, modP(CURVE.Gx * CURVE.Gy));
|
|
257
|
-
Point.ZERO = new Point(_0n, _1n, _1n, _0n);
|
|
258
|
-
Point.Fp = Fp;
|
|
259
|
-
Point.Fn = Fn;
|
|
260
|
-
const wnaf = new wNAF(Point, Fn.BITS);
|
|
261
|
-
Point.BASE.precompute(8);
|
|
262
|
-
return Point;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Base class for prime-order points like Ristretto255 and Decaf448.
|
|
266
|
-
* These points eliminate cofactor issues by representing equivalence classes
|
|
267
|
-
* of Edwards curve points.
|
|
268
|
-
*/
|
|
269
|
-
var PrimeEdwardsPoint = class {
|
|
270
|
-
constructor(ep) {
|
|
271
|
-
this.ep = ep;
|
|
272
|
-
}
|
|
273
|
-
static fromBytes(_bytes) {
|
|
274
|
-
notImplemented();
|
|
275
|
-
}
|
|
276
|
-
static fromHex(_hex) {
|
|
277
|
-
notImplemented();
|
|
278
|
-
}
|
|
279
|
-
get x() {
|
|
280
|
-
return this.toAffine().x;
|
|
281
|
-
}
|
|
282
|
-
get y() {
|
|
283
|
-
return this.toAffine().y;
|
|
284
|
-
}
|
|
285
|
-
clearCofactor() {
|
|
286
|
-
return this;
|
|
287
|
-
}
|
|
288
|
-
assertValidity() {
|
|
289
|
-
this.ep.assertValidity();
|
|
290
|
-
}
|
|
291
|
-
toAffine(invertedZ) {
|
|
292
|
-
return this.ep.toAffine(invertedZ);
|
|
293
|
-
}
|
|
294
|
-
toHex() {
|
|
295
|
-
return bytesToHex(this.toBytes());
|
|
296
|
-
}
|
|
297
|
-
toString() {
|
|
298
|
-
return this.toHex();
|
|
299
|
-
}
|
|
300
|
-
isTorsionFree() {
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
isSmallOrder() {
|
|
304
|
-
return false;
|
|
305
|
-
}
|
|
306
|
-
add(other) {
|
|
307
|
-
this.assertSame(other);
|
|
308
|
-
return this.init(this.ep.add(other.ep));
|
|
309
|
-
}
|
|
310
|
-
subtract(other) {
|
|
311
|
-
this.assertSame(other);
|
|
312
|
-
return this.init(this.ep.subtract(other.ep));
|
|
313
|
-
}
|
|
314
|
-
multiply(scalar) {
|
|
315
|
-
return this.init(this.ep.multiply(scalar));
|
|
316
|
-
}
|
|
317
|
-
multiplyUnsafe(scalar) {
|
|
318
|
-
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
319
|
-
}
|
|
320
|
-
double() {
|
|
321
|
-
return this.init(this.ep.double());
|
|
322
|
-
}
|
|
323
|
-
negate() {
|
|
324
|
-
return this.init(this.ep.negate());
|
|
325
|
-
}
|
|
326
|
-
precompute(windowSize, isLazy) {
|
|
327
|
-
return this.init(this.ep.precompute(windowSize, isLazy));
|
|
328
|
-
}
|
|
329
|
-
/** @deprecated use `toBytes` */
|
|
330
|
-
toRawBytes() {
|
|
331
|
-
return this.toBytes();
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
/**
|
|
335
|
-
* Initializes EdDSA signatures over given Edwards curve.
|
|
336
|
-
*/
|
|
337
|
-
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
338
|
-
if (typeof cHash !== "function") throw new Error("\"hash\" function param is required");
|
|
339
|
-
_validateObject(eddsaOpts, {}, {
|
|
340
|
-
adjustScalarBytes: "function",
|
|
341
|
-
randomBytes: "function",
|
|
342
|
-
domain: "function",
|
|
343
|
-
prehash: "function",
|
|
344
|
-
mapToCurve: "function"
|
|
345
|
-
});
|
|
346
|
-
const { prehash } = eddsaOpts;
|
|
347
|
-
const { BASE, Fp, Fn } = Point;
|
|
348
|
-
const randomBytes$1 = eddsaOpts.randomBytes || randomBytes;
|
|
349
|
-
const adjustScalarBytes = eddsaOpts.adjustScalarBytes || ((bytes) => bytes);
|
|
350
|
-
const domain = eddsaOpts.domain || ((data, ctx, phflag) => {
|
|
351
|
-
_abool2(phflag, "phflag");
|
|
352
|
-
if (ctx.length || phflag) throw new Error("Contexts/pre-hash are not supported");
|
|
353
|
-
return data;
|
|
354
|
-
});
|
|
355
|
-
function modN_LE(hash) {
|
|
356
|
-
return Fn.create(bytesToNumberLE(hash));
|
|
357
|
-
}
|
|
358
|
-
function getPrivateScalar(key) {
|
|
359
|
-
const len = lengths.secretKey;
|
|
360
|
-
key = ensureBytes("private key", key, len);
|
|
361
|
-
const hashed = ensureBytes("hashed private key", cHash(key), 2 * len);
|
|
362
|
-
const head = adjustScalarBytes(hashed.slice(0, len));
|
|
363
|
-
return {
|
|
364
|
-
head,
|
|
365
|
-
prefix: hashed.slice(len, 2 * len),
|
|
366
|
-
scalar: modN_LE(head)
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
/** Convenience method that creates public key from scalar. RFC8032 5.1.5 */
|
|
370
|
-
function getExtendedPublicKey(secretKey) {
|
|
371
|
-
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
372
|
-
const point = BASE.multiply(scalar);
|
|
373
|
-
return {
|
|
374
|
-
head,
|
|
375
|
-
prefix,
|
|
376
|
-
scalar,
|
|
377
|
-
point,
|
|
378
|
-
pointBytes: point.toBytes()
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
/** Calculates EdDSA pub key. RFC8032 5.1.5. */
|
|
382
|
-
function getPublicKey(secretKey) {
|
|
383
|
-
return getExtendedPublicKey(secretKey).pointBytes;
|
|
384
|
-
}
|
|
385
|
-
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
386
|
-
return modN_LE(cHash(domain(concatBytes(...msgs), ensureBytes("context", context), !!prehash)));
|
|
387
|
-
}
|
|
388
|
-
/** Signs message with privateKey. RFC8032 5.1.6 */
|
|
389
|
-
function sign(msg, secretKey, options = {}) {
|
|
390
|
-
msg = ensureBytes("message", msg);
|
|
391
|
-
if (prehash) msg = prehash(msg);
|
|
392
|
-
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
393
|
-
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
394
|
-
const R = BASE.multiply(r).toBytes();
|
|
395
|
-
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
396
|
-
const s = Fn.create(r + k * scalar);
|
|
397
|
-
if (!Fn.isValid(s)) throw new Error("sign failed: invalid s");
|
|
398
|
-
return _abytes2(concatBytes(R, Fn.toBytes(s)), lengths.signature, "result");
|
|
399
|
-
}
|
|
400
|
-
const verifyOpts = { zip215: true };
|
|
401
|
-
/**
|
|
402
|
-
* Verifies EdDSA signature against message and public key. RFC8032 5.1.7.
|
|
403
|
-
* An extended group equation is checked.
|
|
404
|
-
*/
|
|
405
|
-
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
406
|
-
const { context, zip215 } = options;
|
|
407
|
-
const len = lengths.signature;
|
|
408
|
-
sig = ensureBytes("signature", sig, len);
|
|
409
|
-
msg = ensureBytes("message", msg);
|
|
410
|
-
publicKey = ensureBytes("publicKey", publicKey, lengths.publicKey);
|
|
411
|
-
if (zip215 !== void 0) _abool2(zip215, "zip215");
|
|
412
|
-
if (prehash) msg = prehash(msg);
|
|
413
|
-
const mid = len / 2;
|
|
414
|
-
const r = sig.subarray(0, mid);
|
|
415
|
-
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
416
|
-
let A, R, SB;
|
|
417
|
-
try {
|
|
418
|
-
A = Point.fromBytes(publicKey, zip215);
|
|
419
|
-
R = Point.fromBytes(r, zip215);
|
|
420
|
-
SB = BASE.multiplyUnsafe(s);
|
|
421
|
-
} catch (error) {
|
|
422
|
-
return false;
|
|
423
|
-
}
|
|
424
|
-
if (!zip215 && A.isSmallOrder()) return false;
|
|
425
|
-
const k = hashDomainToScalar(context, R.toBytes(), A.toBytes(), msg);
|
|
426
|
-
return R.add(A.multiplyUnsafe(k)).subtract(SB).clearCofactor().is0();
|
|
427
|
-
}
|
|
428
|
-
const _size = Fp.BYTES;
|
|
429
|
-
const lengths = {
|
|
430
|
-
secretKey: _size,
|
|
431
|
-
publicKey: _size,
|
|
432
|
-
signature: 2 * _size,
|
|
433
|
-
seed: _size
|
|
434
|
-
};
|
|
435
|
-
function randomSecretKey(seed = randomBytes$1(lengths.seed)) {
|
|
436
|
-
return _abytes2(seed, lengths.seed, "seed");
|
|
437
|
-
}
|
|
438
|
-
function keygen(seed) {
|
|
439
|
-
const secretKey = utils.randomSecretKey(seed);
|
|
440
|
-
return {
|
|
441
|
-
secretKey,
|
|
442
|
-
publicKey: getPublicKey(secretKey)
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
|
-
function isValidSecretKey(key) {
|
|
446
|
-
return isBytes(key) && key.length === Fn.BYTES;
|
|
447
|
-
}
|
|
448
|
-
function isValidPublicKey(key, zip215) {
|
|
449
|
-
try {
|
|
450
|
-
return !!Point.fromBytes(key, zip215);
|
|
451
|
-
} catch (error) {
|
|
452
|
-
return false;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
const utils = {
|
|
456
|
-
getExtendedPublicKey,
|
|
457
|
-
randomSecretKey,
|
|
458
|
-
isValidSecretKey,
|
|
459
|
-
isValidPublicKey,
|
|
460
|
-
toMontgomery(publicKey) {
|
|
461
|
-
const { y } = Point.fromBytes(publicKey);
|
|
462
|
-
const size = lengths.publicKey;
|
|
463
|
-
const is25519 = size === 32;
|
|
464
|
-
if (!is25519 && size !== 57) throw new Error("only defined for 25519 and 448");
|
|
465
|
-
const u = is25519 ? Fp.div(_1n + y, _1n - y) : Fp.div(y - _1n, y + _1n);
|
|
466
|
-
return Fp.toBytes(u);
|
|
467
|
-
},
|
|
468
|
-
toMontgomerySecret(secretKey) {
|
|
469
|
-
const size = lengths.secretKey;
|
|
470
|
-
_abytes2(secretKey, size);
|
|
471
|
-
return adjustScalarBytes(cHash(secretKey.subarray(0, size))).subarray(0, size);
|
|
472
|
-
},
|
|
473
|
-
randomPrivateKey: randomSecretKey,
|
|
474
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
|
475
|
-
return point.precompute(windowSize, false);
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
return Object.freeze({
|
|
479
|
-
keygen,
|
|
480
|
-
getPublicKey,
|
|
481
|
-
sign,
|
|
482
|
-
verify,
|
|
483
|
-
utils,
|
|
484
|
-
Point,
|
|
485
|
-
lengths
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
function _eddsa_legacy_opts_to_new(c) {
|
|
489
|
-
const CURVE = {
|
|
490
|
-
a: c.a,
|
|
491
|
-
d: c.d,
|
|
492
|
-
p: c.Fp.ORDER,
|
|
493
|
-
n: c.n,
|
|
494
|
-
h: c.h,
|
|
495
|
-
Gx: c.Gx,
|
|
496
|
-
Gy: c.Gy
|
|
497
|
-
};
|
|
498
|
-
const curveOpts = {
|
|
499
|
-
Fp: c.Fp,
|
|
500
|
-
Fn: Field(CURVE.n, c.nBitLength, true),
|
|
501
|
-
uvRatio: c.uvRatio
|
|
502
|
-
};
|
|
503
|
-
const eddsaOpts = {
|
|
504
|
-
randomBytes: c.randomBytes,
|
|
505
|
-
adjustScalarBytes: c.adjustScalarBytes,
|
|
506
|
-
domain: c.domain,
|
|
507
|
-
prehash: c.prehash,
|
|
508
|
-
mapToCurve: c.mapToCurve
|
|
509
|
-
};
|
|
510
|
-
return {
|
|
511
|
-
CURVE,
|
|
512
|
-
curveOpts,
|
|
513
|
-
hash: c.hash,
|
|
514
|
-
eddsaOpts
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
function _eddsa_new_output_to_legacy(c, eddsa$1) {
|
|
518
|
-
const Point = eddsa$1.Point;
|
|
519
|
-
return Object.assign({}, eddsa$1, {
|
|
520
|
-
ExtendedPoint: Point,
|
|
521
|
-
CURVE: c,
|
|
522
|
-
nBitLength: Point.Fn.BITS,
|
|
523
|
-
nByteLength: Point.Fn.BYTES
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
function twistedEdwards(c) {
|
|
527
|
-
const { CURVE, curveOpts, hash, eddsaOpts } = _eddsa_legacy_opts_to_new(c);
|
|
528
|
-
return _eddsa_new_output_to_legacy(c, eddsa(edwards(CURVE, curveOpts), hash, eddsaOpts));
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
//#endregion
|
|
532
|
-
export { PrimeEdwardsPoint, twistedEdwards };
|