@arcblock/did-noise 0.0.0-stage → 1.30.25
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 +13 -0
- package/esm/chacha20poly1305.d.mts +18 -0
- package/esm/chacha20poly1305.mjs +152 -0
- package/esm/chain-node-mesh.d.mts +79 -0
- package/esm/chain-node-mesh.mjs +187 -0
- package/esm/http-transport.d.mts +72 -0
- package/esm/http-transport.mjs +216 -0
- package/esm/index.d.mts +6 -0
- package/esm/index.mjs +7 -0
- package/esm/mesh-tunnel.d.mts +165 -0
- package/esm/mesh-tunnel.mjs +350 -0
- package/esm/noise-session.d.mts +157 -0
- package/esm/noise-session.mjs +491 -0
- package/lib/_vendor/@noble/curves/LICENSE +21 -0
- package/lib/_vendor/@noble/curves/abstract/curve.cjs +338 -0
- package/lib/_vendor/@noble/curves/abstract/edwards.cjs +542 -0
- package/lib/_vendor/@noble/curves/abstract/hash-to-curve.cjs +72 -0
- package/lib/_vendor/@noble/curves/abstract/modular.cjs +557 -0
- package/lib/_vendor/@noble/curves/abstract/montgomery.cjs +149 -0
- package/lib/_vendor/@noble/curves/ed25519.cjs +302 -0
- package/lib/_vendor/@noble/curves/utils.cjs +436 -0
- package/lib/_vendor/@noble/hashes/LICENSE +21 -0
- package/lib/_vendor/@noble/hashes/_blake.cjs +300 -0
- package/lib/_vendor/@noble/hashes/_md.cjs +194 -0
- package/lib/_vendor/@noble/hashes/_u64.cjs +60 -0
- package/lib/_vendor/@noble/hashes/blake2.cjs +291 -0
- package/lib/_vendor/@noble/hashes/hkdf.cjs +104 -0
- package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
- package/lib/_vendor/@noble/hashes/sha2.cjs +417 -0
- package/lib/_vendor/@noble/hashes/utils.cjs +422 -0
- package/lib/chacha20poly1305.cjs +153 -0
- package/lib/chacha20poly1305.d.cts +18 -0
- package/lib/chain-node-mesh.cjs +189 -0
- package/lib/chain-node-mesh.d.cts +79 -0
- package/lib/http-transport.cjs +219 -0
- package/lib/http-transport.d.cts +72 -0
- package/lib/index.cjs +31 -0
- package/lib/index.d.cts +6 -0
- package/lib/mesh-tunnel.cjs +354 -0
- package/lib/mesh-tunnel.d.cts +165 -0
- package/lib/noise-session.cjs +503 -0
- package/lib/noise-session.d.cts +157 -0
- package/package.json +80 -4
- package/README.md +0 -3
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
const require_utils = require('../utils.cjs');
|
|
2
|
+
const require_curve = require('./curve.cjs');
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/@noble/curves/abstract/edwards.js
|
|
5
|
+
/**
|
|
6
|
+
* Twisted Edwards curve. The formula is: ax² + y² = 1 + dx²y².
|
|
7
|
+
* For design rationale of types / exports, see weierstrass module documentation.
|
|
8
|
+
* Untwisted Edwards curves exist, but they aren't used in real-world protocols.
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
12
|
+
const _0n = /* @__PURE__ */ BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _8n = /* @__PURE__ */ BigInt(8);
|
|
13
|
+
function isEdValidXY(Fp, CURVE, x, y) {
|
|
14
|
+
const x2 = Fp.sqr(x);
|
|
15
|
+
const y2 = Fp.sqr(y);
|
|
16
|
+
const left = Fp.add(Fp.mul(CURVE.a, x2), y2);
|
|
17
|
+
const right = Fp.add(Fp.ONE, Fp.mul(CURVE.d, Fp.mul(x2, y2)));
|
|
18
|
+
return Fp.eql(left, right);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param params - Curve parameters. See {@link EdwardsOpts}.
|
|
22
|
+
* @param extraOpts - Optional helpers and overrides. See {@link EdwardsExtraOpts}.
|
|
23
|
+
* @returns Edwards point constructor. Generator validation here only checks
|
|
24
|
+
* that `(Gx, Gy)` satisfies the affine Edwards equation.
|
|
25
|
+
* RFC 8032 base-point constraints like `B != (0,1)` and `[L]B = 0`
|
|
26
|
+
* are left to the caller's chosen parameters, since eager subgroup
|
|
27
|
+
* validation here adds about 10-15ms to heavyweight imports like ed448.
|
|
28
|
+
* The returned constructor also eagerly marks `Point.BASE` for W=8
|
|
29
|
+
* precompute caching. Some code paths still assume
|
|
30
|
+
* `Fp.BYTES === Fn.BYTES`, so mismatched byte lengths are not fully audited here.
|
|
31
|
+
* @throws If the curve parameters or Edwards overrides are invalid. {@link Error}
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { edwards } from '@noble/curves/abstract/edwards.js';
|
|
35
|
+
* import { jubjub } from '@noble/curves/misc.js';
|
|
36
|
+
* // Build a point constructor from explicit curve parameters, then use its base point.
|
|
37
|
+
* const Point = edwards(jubjub.Point.CURVE());
|
|
38
|
+
* Point.BASE.toHex();
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function edwards(params, extraOpts = {}) {
|
|
42
|
+
const opts = extraOpts;
|
|
43
|
+
const validated = require_curve.createCurveFields("edwards", params, opts, opts.FpFnLE);
|
|
44
|
+
const { Fp, Fn } = validated;
|
|
45
|
+
let CURVE = validated.CURVE;
|
|
46
|
+
const { h: cofactor } = CURVE;
|
|
47
|
+
require_utils.validateObject(opts, {}, { uvRatio: "function" });
|
|
48
|
+
const MASK = _2n << BigInt(Fn.BYTES * 8) - _1n;
|
|
49
|
+
const modP = (n) => Fp.create(n);
|
|
50
|
+
const uvRatio = opts.uvRatio === void 0 ? (u, v) => {
|
|
51
|
+
try {
|
|
52
|
+
return {
|
|
53
|
+
isValid: true,
|
|
54
|
+
value: Fp.sqrt(Fp.div(u, v))
|
|
55
|
+
};
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return {
|
|
58
|
+
isValid: false,
|
|
59
|
+
value: _0n
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
} : opts.uvRatio;
|
|
63
|
+
if (!isEdValidXY(Fp, CURVE, CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
64
|
+
/**
|
|
65
|
+
* Asserts coordinate is valid: 0 <= n < MASK.
|
|
66
|
+
* Coordinates >= Fp.ORDER are allowed for zip215.
|
|
67
|
+
*/
|
|
68
|
+
function acoord(title, n, banZero = false) {
|
|
69
|
+
const min = banZero ? _1n : _0n;
|
|
70
|
+
require_utils.aInRange("coordinate " + title, n, min, MASK);
|
|
71
|
+
return n;
|
|
72
|
+
}
|
|
73
|
+
function aedpoint(other) {
|
|
74
|
+
if (!(other instanceof Point)) throw new Error("EdwardsPoint expected");
|
|
75
|
+
}
|
|
76
|
+
class Point {
|
|
77
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, _1n, modP(CURVE.Gx * CURVE.Gy));
|
|
78
|
+
static ZERO = new Point(_0n, _1n, _1n, _0n);
|
|
79
|
+
static Fp = Fp;
|
|
80
|
+
static Fn = Fn;
|
|
81
|
+
X;
|
|
82
|
+
Y;
|
|
83
|
+
Z;
|
|
84
|
+
T;
|
|
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
|
+
/**
|
|
96
|
+
* Create one extended Edwards point from affine coordinates.
|
|
97
|
+
* Does NOT validate that the point is on-curve or torsion-free.
|
|
98
|
+
* Use `.assertValidity()` on adversarial inputs.
|
|
99
|
+
*/
|
|
100
|
+
static fromAffine(p) {
|
|
101
|
+
if (p instanceof Point) throw new Error("extended point not allowed");
|
|
102
|
+
const { x, y } = p || {};
|
|
103
|
+
acoord("x", x);
|
|
104
|
+
acoord("y", y);
|
|
105
|
+
return new Point(x, y, _1n, modP(x * y));
|
|
106
|
+
}
|
|
107
|
+
static fromBytes(bytes, zip215 = false) {
|
|
108
|
+
const len = Fp.BYTES;
|
|
109
|
+
const { a, d } = CURVE;
|
|
110
|
+
bytes = require_utils.copyBytes(require_utils.abytes(bytes, len, "point"));
|
|
111
|
+
require_utils.abool(zip215, "zip215");
|
|
112
|
+
const normed = require_utils.copyBytes(bytes);
|
|
113
|
+
const lastByte = bytes[len - 1];
|
|
114
|
+
normed[len - 1] = lastByte & -129;
|
|
115
|
+
const y = require_utils.bytesToNumberLE(normed);
|
|
116
|
+
require_utils.aInRange("point.y", y, _0n, zip215 ? MASK : Fp.ORDER);
|
|
117
|
+
const y2 = modP(y * y);
|
|
118
|
+
let { isValid, value: x } = uvRatio(modP(y2 - _1n), modP(d * y2 - a));
|
|
119
|
+
if (!isValid) throw new Error("bad point: invalid y coordinate");
|
|
120
|
+
const isXOdd = (x & _1n) === _1n;
|
|
121
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
122
|
+
if (!zip215 && x === _0n && isLastByteOdd) throw new Error("bad point: x=0 and x_0=1");
|
|
123
|
+
if (isLastByteOdd !== isXOdd) x = modP(-x);
|
|
124
|
+
return Point.fromAffine({
|
|
125
|
+
x,
|
|
126
|
+
y
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
static fromHex(hex, zip215 = false) {
|
|
130
|
+
return Point.fromBytes(require_utils.hexToBytes(hex), zip215);
|
|
131
|
+
}
|
|
132
|
+
get x() {
|
|
133
|
+
return this.toAffine().x;
|
|
134
|
+
}
|
|
135
|
+
get y() {
|
|
136
|
+
return this.toAffine().y;
|
|
137
|
+
}
|
|
138
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
139
|
+
wnaf.createCache(this, windowSize);
|
|
140
|
+
if (!isLazy) this.multiply(_2n);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
assertValidity() {
|
|
144
|
+
const p = this;
|
|
145
|
+
const { a, d } = CURVE;
|
|
146
|
+
if (p.is0()) throw new Error("bad point: ZERO");
|
|
147
|
+
const { X, Y, Z, T } = p;
|
|
148
|
+
const X2 = modP(X * X);
|
|
149
|
+
const Y2 = modP(Y * Y);
|
|
150
|
+
const Z2 = modP(Z * Z);
|
|
151
|
+
const Z4 = modP(Z2 * Z2);
|
|
152
|
+
if (modP(Z2 * modP(modP(X2 * a) + Y2)) !== modP(Z4 + modP(d * modP(X2 * Y2)))) throw new Error("bad point: equation left != right (1)");
|
|
153
|
+
if (modP(X * Y) !== modP(Z * T)) throw new Error("bad point: equation left != right (2)");
|
|
154
|
+
}
|
|
155
|
+
equals(other) {
|
|
156
|
+
aedpoint(other);
|
|
157
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
158
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
159
|
+
const X1Z2 = modP(X1 * Z2);
|
|
160
|
+
const X2Z1 = modP(X2 * Z1);
|
|
161
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
162
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
163
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
164
|
+
}
|
|
165
|
+
is0() {
|
|
166
|
+
return this.equals(Point.ZERO);
|
|
167
|
+
}
|
|
168
|
+
negate() {
|
|
169
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
170
|
+
}
|
|
171
|
+
double() {
|
|
172
|
+
const { a } = CURVE;
|
|
173
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
174
|
+
const A = modP(X1 * X1);
|
|
175
|
+
const B = modP(Y1 * Y1);
|
|
176
|
+
const C = modP(_2n * modP(Z1 * Z1));
|
|
177
|
+
const D = modP(a * A);
|
|
178
|
+
const x1y1 = X1 + Y1;
|
|
179
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
180
|
+
const G = D + B;
|
|
181
|
+
const F = G - C;
|
|
182
|
+
const H = D - B;
|
|
183
|
+
const X3 = modP(E * F);
|
|
184
|
+
const Y3 = modP(G * H);
|
|
185
|
+
const T3 = modP(E * H);
|
|
186
|
+
return new Point(X3, Y3, modP(F * G), T3);
|
|
187
|
+
}
|
|
188
|
+
add(other) {
|
|
189
|
+
aedpoint(other);
|
|
190
|
+
const { a, d } = CURVE;
|
|
191
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
192
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
193
|
+
const A = modP(X1 * X2);
|
|
194
|
+
const B = modP(Y1 * Y2);
|
|
195
|
+
const C = modP(T1 * d * T2);
|
|
196
|
+
const D = modP(Z1 * Z2);
|
|
197
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
198
|
+
const F = D - C;
|
|
199
|
+
const G = D + C;
|
|
200
|
+
const H = modP(B - a * A);
|
|
201
|
+
const X3 = modP(E * F);
|
|
202
|
+
const Y3 = modP(G * H);
|
|
203
|
+
const T3 = modP(E * H);
|
|
204
|
+
return new Point(X3, Y3, modP(F * G), T3);
|
|
205
|
+
}
|
|
206
|
+
subtract(other) {
|
|
207
|
+
aedpoint(other);
|
|
208
|
+
return this.add(other.negate());
|
|
209
|
+
}
|
|
210
|
+
multiply(scalar) {
|
|
211
|
+
if (!Fn.isValidNot0(scalar)) throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
212
|
+
const { p, f } = wnaf.cached(this, scalar, (p$1) => require_curve.normalizeZ(Point, p$1));
|
|
213
|
+
return require_curve.normalizeZ(Point, [p, f])[0];
|
|
214
|
+
}
|
|
215
|
+
multiplyUnsafe(scalar) {
|
|
216
|
+
if (!Fn.isValid(scalar)) throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
217
|
+
if (scalar === _0n) return Point.ZERO;
|
|
218
|
+
if (this.is0() || scalar === _1n) return this;
|
|
219
|
+
return wnaf.unsafe(this, scalar, (p) => require_curve.normalizeZ(Point, p));
|
|
220
|
+
}
|
|
221
|
+
isSmallOrder() {
|
|
222
|
+
return this.clearCofactor().is0();
|
|
223
|
+
}
|
|
224
|
+
isTorsionFree() {
|
|
225
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
226
|
+
}
|
|
227
|
+
toAffine(invertedZ) {
|
|
228
|
+
const p = this;
|
|
229
|
+
let iz = invertedZ;
|
|
230
|
+
const { X, Y, Z } = p;
|
|
231
|
+
const is0 = p.is0();
|
|
232
|
+
if (iz == null) iz = is0 ? _8n : Fp.inv(Z);
|
|
233
|
+
const x = modP(X * iz);
|
|
234
|
+
const y = modP(Y * iz);
|
|
235
|
+
const zz = Fp.mul(Z, iz);
|
|
236
|
+
if (is0) return {
|
|
237
|
+
x: _0n,
|
|
238
|
+
y: _1n
|
|
239
|
+
};
|
|
240
|
+
if (zz !== _1n) throw new Error("invZ was invalid");
|
|
241
|
+
return {
|
|
242
|
+
x,
|
|
243
|
+
y
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
clearCofactor() {
|
|
247
|
+
if (cofactor === _1n) return this;
|
|
248
|
+
return this.multiplyUnsafe(cofactor);
|
|
249
|
+
}
|
|
250
|
+
toBytes() {
|
|
251
|
+
const { x, y } = this.toAffine();
|
|
252
|
+
const bytes = Fp.toBytes(y);
|
|
253
|
+
bytes[bytes.length - 1] |= x & _1n ? 128 : 0;
|
|
254
|
+
return bytes;
|
|
255
|
+
}
|
|
256
|
+
toHex() {
|
|
257
|
+
return require_utils.bytesToHex(this.toBytes());
|
|
258
|
+
}
|
|
259
|
+
toString() {
|
|
260
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const wnaf = new require_curve.wNAF(Point, Fn.BITS);
|
|
264
|
+
if (Fn.BITS >= 8) Point.BASE.precompute(8);
|
|
265
|
+
Object.freeze(Point.prototype);
|
|
266
|
+
Object.freeze(Point);
|
|
267
|
+
return Point;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Base class for prime-order points like Ristretto255 and Decaf448.
|
|
271
|
+
* These points eliminate cofactor issues by representing equivalence classes
|
|
272
|
+
* of Edwards curve points. Multiple Edwards representatives can describe the
|
|
273
|
+
* same abstract wrapper element, so wrapper validity is not the same thing as
|
|
274
|
+
* the hidden representative being torsion-free.
|
|
275
|
+
* @param ep - Backing Edwards point.
|
|
276
|
+
* @example
|
|
277
|
+
* Base class for prime-order points like Ristretto255 and Decaf448.
|
|
278
|
+
*
|
|
279
|
+
* ```ts
|
|
280
|
+
* import { ristretto255 } from '@noble/curves/ed25519.js';
|
|
281
|
+
* const point = ristretto255.Point.BASE.multiply(2n);
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
var PrimeEdwardsPoint = class {
|
|
285
|
+
static BASE;
|
|
286
|
+
static ZERO;
|
|
287
|
+
static Fp;
|
|
288
|
+
static Fn;
|
|
289
|
+
ep;
|
|
290
|
+
/**
|
|
291
|
+
* Wrap one internal Edwards representative directly.
|
|
292
|
+
* This is not a canonical encoding boundary: alternate Edwards
|
|
293
|
+
* representatives may still describe the same abstract wrapper element.
|
|
294
|
+
*/
|
|
295
|
+
constructor(ep) {
|
|
296
|
+
this.ep = ep;
|
|
297
|
+
}
|
|
298
|
+
static fromBytes(_bytes) {
|
|
299
|
+
require_utils.notImplemented();
|
|
300
|
+
}
|
|
301
|
+
static fromHex(_hex) {
|
|
302
|
+
require_utils.notImplemented();
|
|
303
|
+
}
|
|
304
|
+
get x() {
|
|
305
|
+
return this.toAffine().x;
|
|
306
|
+
}
|
|
307
|
+
get y() {
|
|
308
|
+
return this.toAffine().y;
|
|
309
|
+
}
|
|
310
|
+
clearCofactor() {
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
assertValidity() {
|
|
314
|
+
this.ep.assertValidity();
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Return affine coordinates of the current internal Edwards representative.
|
|
318
|
+
* This is a convenience helper, not a canonical Ristretto/Decaf encoding.
|
|
319
|
+
* Equal abstract elements may expose different `x` / `y`; use
|
|
320
|
+
* `toBytes()` / `fromBytes()` for canonical roundtrips.
|
|
321
|
+
*/
|
|
322
|
+
toAffine(invertedZ) {
|
|
323
|
+
return this.ep.toAffine(invertedZ);
|
|
324
|
+
}
|
|
325
|
+
toHex() {
|
|
326
|
+
return require_utils.bytesToHex(this.toBytes());
|
|
327
|
+
}
|
|
328
|
+
toString() {
|
|
329
|
+
return this.toHex();
|
|
330
|
+
}
|
|
331
|
+
isTorsionFree() {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
isSmallOrder() {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
add(other) {
|
|
338
|
+
this.assertSame(other);
|
|
339
|
+
return this.init(this.ep.add(other.ep));
|
|
340
|
+
}
|
|
341
|
+
subtract(other) {
|
|
342
|
+
this.assertSame(other);
|
|
343
|
+
return this.init(this.ep.subtract(other.ep));
|
|
344
|
+
}
|
|
345
|
+
multiply(scalar) {
|
|
346
|
+
return this.init(this.ep.multiply(scalar));
|
|
347
|
+
}
|
|
348
|
+
multiplyUnsafe(scalar) {
|
|
349
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
350
|
+
}
|
|
351
|
+
double() {
|
|
352
|
+
return this.init(this.ep.double());
|
|
353
|
+
}
|
|
354
|
+
negate() {
|
|
355
|
+
return this.init(this.ep.negate());
|
|
356
|
+
}
|
|
357
|
+
precompute(windowSize, isLazy) {
|
|
358
|
+
this.ep.precompute(windowSize, isLazy);
|
|
359
|
+
return this;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
* Initializes EdDSA signatures over given Edwards curve.
|
|
364
|
+
* @param Point - Edwards point constructor.
|
|
365
|
+
* @param cHash - Hash function.
|
|
366
|
+
* @param eddsaOpts - Optional signature helpers. See {@link EdDSAOpts}.
|
|
367
|
+
* @returns EdDSA helper namespace.
|
|
368
|
+
* @throws If the hash function, options, or derived point operations are invalid. {@link Error}
|
|
369
|
+
* @example
|
|
370
|
+
* Initializes EdDSA signatures over given Edwards curve.
|
|
371
|
+
*
|
|
372
|
+
* ```ts
|
|
373
|
+
* import { eddsa } from '@noble/curves/abstract/edwards.js';
|
|
374
|
+
* import { jubjub } from '@noble/curves/misc.js';
|
|
375
|
+
* import { sha512 } from '@noble/hashes/sha2.js';
|
|
376
|
+
* const sigs = eddsa(jubjub.Point, sha512);
|
|
377
|
+
* const { secretKey, publicKey } = sigs.keygen();
|
|
378
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
379
|
+
* const sig = sigs.sign(msg, secretKey);
|
|
380
|
+
* const isValid = sigs.verify(sig, msg, publicKey);
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
384
|
+
if (typeof cHash !== "function") throw new Error("\"hash\" function param is required");
|
|
385
|
+
const hash = cHash;
|
|
386
|
+
const opts = eddsaOpts;
|
|
387
|
+
require_utils.validateObject(opts, {}, {
|
|
388
|
+
adjustScalarBytes: "function",
|
|
389
|
+
randomBytes: "function",
|
|
390
|
+
domain: "function",
|
|
391
|
+
prehash: "function",
|
|
392
|
+
zip215: "boolean",
|
|
393
|
+
mapToCurve: "function"
|
|
394
|
+
});
|
|
395
|
+
const { prehash } = opts;
|
|
396
|
+
const { BASE, Fp, Fn } = Point;
|
|
397
|
+
const outputLen = hash.outputLen;
|
|
398
|
+
const expectedLen = 2 * Fp.BYTES;
|
|
399
|
+
if (outputLen !== void 0) {
|
|
400
|
+
require_utils.asafenumber(outputLen, "hash.outputLen");
|
|
401
|
+
if (outputLen !== expectedLen) throw new Error(`hash.outputLen must be ${expectedLen}, got ${outputLen}`);
|
|
402
|
+
}
|
|
403
|
+
const randomBytes$1 = opts.randomBytes === void 0 ? require_utils.randomBytes : opts.randomBytes;
|
|
404
|
+
const adjustScalarBytes = opts.adjustScalarBytes === void 0 ? (bytes) => bytes : opts.adjustScalarBytes;
|
|
405
|
+
const domain = opts.domain === void 0 ? (data, ctx, phflag) => {
|
|
406
|
+
require_utils.abool(phflag, "phflag");
|
|
407
|
+
if (ctx.length || phflag) throw new Error("Contexts/pre-hash are not supported");
|
|
408
|
+
return data;
|
|
409
|
+
} : opts.domain;
|
|
410
|
+
function modN_LE(hash$1) {
|
|
411
|
+
return Fn.create(require_utils.bytesToNumberLE(hash$1));
|
|
412
|
+
}
|
|
413
|
+
function getPrivateScalar(key) {
|
|
414
|
+
const len = lengths.secretKey;
|
|
415
|
+
require_utils.abytes(key, lengths.secretKey, "secretKey");
|
|
416
|
+
const hashed = require_utils.abytes(hash(key), 2 * len, "hashedSecretKey");
|
|
417
|
+
const head = adjustScalarBytes(hashed.slice(0, len));
|
|
418
|
+
return {
|
|
419
|
+
head,
|
|
420
|
+
prefix: hashed.slice(len, 2 * len),
|
|
421
|
+
scalar: modN_LE(head)
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
/** Convenience method that creates public key from scalar. RFC8032 5.1.5
|
|
425
|
+
* Also exposes the derived scalar/prefix tuple and point form reused by sign().
|
|
426
|
+
*/
|
|
427
|
+
function getExtendedPublicKey(secretKey) {
|
|
428
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
429
|
+
const point = BASE.multiply(scalar);
|
|
430
|
+
return {
|
|
431
|
+
head,
|
|
432
|
+
prefix,
|
|
433
|
+
scalar,
|
|
434
|
+
point,
|
|
435
|
+
pointBytes: point.toBytes()
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
/** Calculates EdDSA pub key. RFC8032 5.1.5. */
|
|
439
|
+
function getPublicKey(secretKey) {
|
|
440
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
441
|
+
}
|
|
442
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
443
|
+
return modN_LE(hash(domain(require_utils.concatBytes(...msgs), require_utils.abytes(context, void 0, "context"), !!prehash)));
|
|
444
|
+
}
|
|
445
|
+
/** Signs message with secret key. RFC8032 5.1.6 */
|
|
446
|
+
function sign(msg, secretKey, options = {}) {
|
|
447
|
+
msg = require_utils.abytes(msg, void 0, "message");
|
|
448
|
+
if (prehash) msg = prehash(msg);
|
|
449
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
450
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
451
|
+
const R = BASE.multiply(r).toBytes();
|
|
452
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
453
|
+
const s = Fn.create(r + k * scalar);
|
|
454
|
+
if (!Fn.isValid(s)) throw new Error("sign failed: invalid s");
|
|
455
|
+
return require_utils.abytes(require_utils.concatBytes(R, Fn.toBytes(s)), lengths.signature, "result");
|
|
456
|
+
}
|
|
457
|
+
const verifyOpts = { zip215: opts.zip215 };
|
|
458
|
+
/**
|
|
459
|
+
* Verifies EdDSA signature against message and public key. RFC 8032 §§5.1.7 and 5.2.7.
|
|
460
|
+
* A cofactored verification equation is checked.
|
|
461
|
+
*/
|
|
462
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
463
|
+
const { context } = options;
|
|
464
|
+
const zip215 = options.zip215 === void 0 ? !!verifyOpts.zip215 : options.zip215;
|
|
465
|
+
const len = lengths.signature;
|
|
466
|
+
sig = require_utils.abytes(sig, len, "signature");
|
|
467
|
+
msg = require_utils.abytes(msg, void 0, "message");
|
|
468
|
+
publicKey = require_utils.abytes(publicKey, lengths.publicKey, "publicKey");
|
|
469
|
+
if (zip215 !== void 0) require_utils.abool(zip215, "zip215");
|
|
470
|
+
if (prehash) msg = prehash(msg);
|
|
471
|
+
const mid = len / 2;
|
|
472
|
+
const r = sig.subarray(0, mid);
|
|
473
|
+
const s = require_utils.bytesToNumberLE(sig.subarray(mid, len));
|
|
474
|
+
let A, R, SB;
|
|
475
|
+
try {
|
|
476
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
477
|
+
R = Point.fromBytes(r, zip215);
|
|
478
|
+
SB = BASE.multiplyUnsafe(s);
|
|
479
|
+
} catch (error) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
if (!zip215 && A.isSmallOrder()) return false;
|
|
483
|
+
const k = hashDomainToScalar(context, r, publicKey, msg);
|
|
484
|
+
return R.add(A.multiplyUnsafe(k)).subtract(SB).clearCofactor().is0();
|
|
485
|
+
}
|
|
486
|
+
const _size = Fp.BYTES;
|
|
487
|
+
const lengths = {
|
|
488
|
+
secretKey: _size,
|
|
489
|
+
publicKey: _size,
|
|
490
|
+
signature: 2 * _size,
|
|
491
|
+
seed: _size
|
|
492
|
+
};
|
|
493
|
+
function randomSecretKey(seed) {
|
|
494
|
+
seed = seed === void 0 ? randomBytes$1(lengths.seed) : seed;
|
|
495
|
+
return require_utils.abytes(seed, lengths.seed, "seed");
|
|
496
|
+
}
|
|
497
|
+
function isValidSecretKey(key) {
|
|
498
|
+
return require_utils.isBytes(key) && key.length === lengths.secretKey;
|
|
499
|
+
}
|
|
500
|
+
function isValidPublicKey(key, zip215) {
|
|
501
|
+
try {
|
|
502
|
+
return !!Point.fromBytes(key, zip215 === void 0 ? verifyOpts.zip215 : zip215);
|
|
503
|
+
} catch (error) {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const utils = {
|
|
508
|
+
getExtendedPublicKey,
|
|
509
|
+
randomSecretKey,
|
|
510
|
+
isValidSecretKey,
|
|
511
|
+
isValidPublicKey,
|
|
512
|
+
toMontgomery(publicKey) {
|
|
513
|
+
const { y } = Point.fromBytes(publicKey);
|
|
514
|
+
const size = lengths.publicKey;
|
|
515
|
+
const is25519 = size === 32;
|
|
516
|
+
if (!is25519 && size !== 57) throw new Error("only defined for 25519 and 448");
|
|
517
|
+
const u = is25519 ? Fp.div(_1n + y, _1n - y) : Fp.div(y - _1n, y + _1n);
|
|
518
|
+
return Fp.toBytes(u);
|
|
519
|
+
},
|
|
520
|
+
toMontgomerySecret(secretKey) {
|
|
521
|
+
const size = lengths.secretKey;
|
|
522
|
+
require_utils.abytes(secretKey, size);
|
|
523
|
+
return adjustScalarBytes(hash(secretKey.subarray(0, size))).subarray(0, size);
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
Object.freeze(lengths);
|
|
527
|
+
Object.freeze(utils);
|
|
528
|
+
return Object.freeze({
|
|
529
|
+
keygen: require_curve.createKeygen(randomSecretKey, getPublicKey),
|
|
530
|
+
getPublicKey,
|
|
531
|
+
sign,
|
|
532
|
+
verify,
|
|
533
|
+
utils,
|
|
534
|
+
Point,
|
|
535
|
+
lengths
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
//#endregion
|
|
540
|
+
exports.PrimeEdwardsPoint = PrimeEdwardsPoint;
|
|
541
|
+
exports.eddsa = eddsa;
|
|
542
|
+
exports.edwards = edwards;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const require_utils = require('../utils.cjs');
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/@noble/curves/abstract/hash-to-curve.js
|
|
4
|
+
function i2osp(value, length) {
|
|
5
|
+
require_utils.asafenumber(value);
|
|
6
|
+
require_utils.asafenumber(length);
|
|
7
|
+
if (length < 0 || length > 4) throw new Error("invalid I2OSP length: " + length);
|
|
8
|
+
if (value < 0 || value > 2 ** (8 * length) - 1) throw new Error("invalid I2OSP input: " + value);
|
|
9
|
+
const res = Array.from({ length }).fill(0);
|
|
10
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
11
|
+
res[i] = value & 255;
|
|
12
|
+
value >>>= 8;
|
|
13
|
+
}
|
|
14
|
+
return new Uint8Array(res);
|
|
15
|
+
}
|
|
16
|
+
function strxor(a, b) {
|
|
17
|
+
const arr = new Uint8Array(a.length);
|
|
18
|
+
for (let i = 0; i < a.length; i++) arr[i] = a[i] ^ b[i];
|
|
19
|
+
return arr;
|
|
20
|
+
}
|
|
21
|
+
function normDST(DST) {
|
|
22
|
+
if (!require_utils.isBytes(DST) && typeof DST !== "string") throw new Error("DST must be Uint8Array or ascii string");
|
|
23
|
+
const dst = typeof DST === "string" ? require_utils.asciiToBytes(DST) : DST;
|
|
24
|
+
if (dst.length === 0) throw new Error("DST must be non-empty");
|
|
25
|
+
return dst;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Produces a uniformly random byte string using a cryptographic hash
|
|
29
|
+
* function H that outputs b bits.
|
|
30
|
+
* See {@link https://www.rfc-editor.org/rfc/rfc9380#section-5.3.1 | RFC 9380 section 5.3.1}.
|
|
31
|
+
* @param msg - Input message.
|
|
32
|
+
* @param DST - Domain separation tag. This helper normalizes DST, rejects empty DSTs, and
|
|
33
|
+
* oversize-hashes DST when needed.
|
|
34
|
+
* @param lenInBytes - Output length.
|
|
35
|
+
* @param H - Hash function.
|
|
36
|
+
* @returns Uniform byte string.
|
|
37
|
+
* @throws If the message, DST, hash, or output length is invalid. {@link Error}
|
|
38
|
+
* @example
|
|
39
|
+
* Expand one message into uniform bytes with the XMD construction.
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { expand_message_xmd } from '@noble/curves/abstract/hash-to-curve.js';
|
|
43
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
44
|
+
* const uniform = expand_message_xmd(new TextEncoder().encode('hello noble'), 'DST', 32, sha256);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
48
|
+
require_utils.abytes(msg);
|
|
49
|
+
require_utils.asafenumber(lenInBytes);
|
|
50
|
+
DST = normDST(DST);
|
|
51
|
+
if (DST.length > 255) DST = H(require_utils.concatBytes(require_utils.asciiToBytes("H2C-OVERSIZE-DST-"), DST));
|
|
52
|
+
const { outputLen: b_in_bytes, blockLen: r_in_bytes } = H;
|
|
53
|
+
const ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
54
|
+
if (lenInBytes > 65535 || ell > 255) throw new Error("expand_message_xmd: invalid lenInBytes");
|
|
55
|
+
const DST_prime = require_utils.concatBytes(DST, i2osp(DST.length, 1));
|
|
56
|
+
const Z_pad = new Uint8Array(r_in_bytes);
|
|
57
|
+
const l_i_b_str = i2osp(lenInBytes, 2);
|
|
58
|
+
const b = new Array(ell);
|
|
59
|
+
const b_0 = H(require_utils.concatBytes(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
60
|
+
b[0] = H(require_utils.concatBytes(b_0, i2osp(1, 1), DST_prime));
|
|
61
|
+
for (let i = 1; i < ell; i++) b[i] = H(require_utils.concatBytes(...[
|
|
62
|
+
strxor(b_0, b[i - 1]),
|
|
63
|
+
i2osp(i + 1, 1),
|
|
64
|
+
DST_prime
|
|
65
|
+
]));
|
|
66
|
+
return require_utils.concatBytes(...b).slice(0, lenInBytes);
|
|
67
|
+
}
|
|
68
|
+
const _DST_scalar = "HashToScalar-";
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
exports._DST_scalar = _DST_scalar;
|
|
72
|
+
exports.expand_message_xmd = expand_message_xmd;
|