@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,338 @@
|
|
|
1
|
+
const require_utils = require('../utils.cjs');
|
|
2
|
+
const require_modular = require('./modular.cjs');
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/@noble/curves/abstract/curve.js
|
|
5
|
+
/**
|
|
6
|
+
* Methods for elliptic curve multiplication by scalars.
|
|
7
|
+
* Contains wNAF, pippenger.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
11
|
+
const _0n = /* @__PURE__ */ BigInt(0);
|
|
12
|
+
const _1n = /* @__PURE__ */ BigInt(1);
|
|
13
|
+
/**
|
|
14
|
+
* Computes both candidates first, but the final selection still branches on `condition`, so this
|
|
15
|
+
* is not a strict constant-time CMOV primitive.
|
|
16
|
+
* @param condition - Whether to negate the point.
|
|
17
|
+
* @param item - Point-like value.
|
|
18
|
+
* @returns Original or negated value.
|
|
19
|
+
* @example
|
|
20
|
+
* Keep the point or return its negation based on one boolean branch.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { negateCt } from '@noble/curves/abstract/curve.js';
|
|
24
|
+
* import { p256 } from '@noble/curves/nist.js';
|
|
25
|
+
* const maybeNegated = negateCt(true, p256.Point.BASE);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function negateCt(condition, item) {
|
|
29
|
+
const neg = item.negate();
|
|
30
|
+
return condition ? neg : item;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Takes a bunch of Projective Points but executes only one
|
|
34
|
+
* inversion on all of them. Inversion is very slow operation,
|
|
35
|
+
* so this improves performance massively.
|
|
36
|
+
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
37
|
+
* Input points are left unchanged; the normalized points are returned as fresh instances.
|
|
38
|
+
* @param c - Point constructor.
|
|
39
|
+
* @param points - Projective points.
|
|
40
|
+
* @returns Fresh projective points reconstructed from normalized affine coordinates.
|
|
41
|
+
* @example
|
|
42
|
+
* Batch-normalize projective points with a single shared inversion.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { normalizeZ } from '@noble/curves/abstract/curve.js';
|
|
46
|
+
* import { p256 } from '@noble/curves/nist.js';
|
|
47
|
+
* const points = normalizeZ(p256.Point, [p256.Point.BASE, p256.Point.BASE.double()]);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function normalizeZ(c, points) {
|
|
51
|
+
const invertedZs = require_modular.FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
52
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
53
|
+
}
|
|
54
|
+
function validateW(W, bits) {
|
|
55
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits) throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
56
|
+
}
|
|
57
|
+
function calcWOpts(W, scalarBits) {
|
|
58
|
+
validateW(W, scalarBits);
|
|
59
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
60
|
+
const windowSize = 2 ** (W - 1);
|
|
61
|
+
const maxNumber = 2 ** W;
|
|
62
|
+
return {
|
|
63
|
+
windows,
|
|
64
|
+
windowSize,
|
|
65
|
+
mask: require_utils.bitMask(W),
|
|
66
|
+
maxNumber,
|
|
67
|
+
shiftBy: BigInt(W)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function calcOffsets(n, window, wOpts) {
|
|
71
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
72
|
+
let wbits = Number(n & mask);
|
|
73
|
+
let nextN = n >> shiftBy;
|
|
74
|
+
if (wbits > windowSize) {
|
|
75
|
+
wbits -= maxNumber;
|
|
76
|
+
nextN += _1n;
|
|
77
|
+
}
|
|
78
|
+
const offsetStart = window * windowSize;
|
|
79
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
80
|
+
const isZero = wbits === 0;
|
|
81
|
+
const isNeg = wbits < 0;
|
|
82
|
+
const isNegF = window % 2 !== 0;
|
|
83
|
+
return {
|
|
84
|
+
nextN,
|
|
85
|
+
offset,
|
|
86
|
+
isZero,
|
|
87
|
+
isNeg,
|
|
88
|
+
isNegF,
|
|
89
|
+
offsetF: offsetStart
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
const pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
94
|
+
function getW(P) {
|
|
95
|
+
return pointWindowSizes.get(P) || 1;
|
|
96
|
+
}
|
|
97
|
+
function assert0(n) {
|
|
98
|
+
if (n !== _0n) throw new Error("invalid wNAF");
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Elliptic curve multiplication of Point by scalar. Fragile.
|
|
102
|
+
* Table generation takes **30MB of ram and 10ms on high-end CPU**,
|
|
103
|
+
* but may take much longer on slow devices. Actual generation will happen on
|
|
104
|
+
* first call of `multiply()`. By default, `BASE` point is precomputed.
|
|
105
|
+
*
|
|
106
|
+
* Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
107
|
+
* Creates precomputation tables for fast multiplication:
|
|
108
|
+
* - private scalar is split by fixed size windows of W bits
|
|
109
|
+
* - every window point is collected from window's table & added to accumulator
|
|
110
|
+
* - since windows are different, same point inside tables won't be accessed more than once per calc
|
|
111
|
+
* - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
|
|
112
|
+
* - +1 window is neccessary for wNAF
|
|
113
|
+
* - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
|
|
114
|
+
*
|
|
115
|
+
* TODO: research returning a 2d JS array of windows instead of a single window.
|
|
116
|
+
* This would allow windows to be in different memory locations.
|
|
117
|
+
* @param Point - Point constructor.
|
|
118
|
+
* @param bits - Scalar bit length.
|
|
119
|
+
* @example
|
|
120
|
+
* Elliptic curve multiplication of Point by scalar.
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { wNAF } from '@noble/curves/abstract/curve.js';
|
|
124
|
+
* import { p256 } from '@noble/curves/nist.js';
|
|
125
|
+
* const ladder = new wNAF(p256.Point, p256.Point.Fn.BITS);
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
var wNAF = class {
|
|
129
|
+
BASE;
|
|
130
|
+
ZERO;
|
|
131
|
+
Fn;
|
|
132
|
+
bits;
|
|
133
|
+
constructor(Point, bits) {
|
|
134
|
+
this.BASE = Point.BASE;
|
|
135
|
+
this.ZERO = Point.ZERO;
|
|
136
|
+
this.Fn = Point.Fn;
|
|
137
|
+
this.bits = bits;
|
|
138
|
+
}
|
|
139
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
140
|
+
let d = elm;
|
|
141
|
+
while (n > _0n) {
|
|
142
|
+
if (n & _1n) p = p.add(d);
|
|
143
|
+
d = d.double();
|
|
144
|
+
n >>= _1n;
|
|
145
|
+
}
|
|
146
|
+
return p;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
150
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
151
|
+
* Number of precomputed points depends on the curve size:
|
|
152
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
153
|
+
* - 𝑊 is the window size
|
|
154
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
155
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
156
|
+
* @param point - Point instance
|
|
157
|
+
* @param W - window size
|
|
158
|
+
* @returns precomputed point tables flattened to a single array
|
|
159
|
+
*/
|
|
160
|
+
precomputeWindow(point, W) {
|
|
161
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
162
|
+
const points = [];
|
|
163
|
+
let p = point;
|
|
164
|
+
let base = p;
|
|
165
|
+
for (let window = 0; window < windows; window++) {
|
|
166
|
+
base = p;
|
|
167
|
+
points.push(base);
|
|
168
|
+
for (let i = 1; i < windowSize; i++) {
|
|
169
|
+
base = base.add(p);
|
|
170
|
+
points.push(base);
|
|
171
|
+
}
|
|
172
|
+
p = base.double();
|
|
173
|
+
}
|
|
174
|
+
return points;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
178
|
+
* More compact implementation:
|
|
179
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
180
|
+
* @returns real and fake (for const-time) points
|
|
181
|
+
*/
|
|
182
|
+
wNAF(W, precomputes, n) {
|
|
183
|
+
if (!this.Fn.isValid(n)) throw new Error("invalid scalar");
|
|
184
|
+
let p = this.ZERO;
|
|
185
|
+
let f = this.BASE;
|
|
186
|
+
const wo = calcWOpts(W, this.bits);
|
|
187
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
188
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
189
|
+
n = nextN;
|
|
190
|
+
if (isZero) f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
191
|
+
else p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
192
|
+
}
|
|
193
|
+
assert0(n);
|
|
194
|
+
return {
|
|
195
|
+
p,
|
|
196
|
+
f
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Implements unsafe EC multiplication using precomputed tables
|
|
201
|
+
* and w-ary non-adjacent form.
|
|
202
|
+
* @param acc - accumulator point to add result of multiplication
|
|
203
|
+
* @returns point
|
|
204
|
+
*/
|
|
205
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
206
|
+
const wo = calcWOpts(W, this.bits);
|
|
207
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
208
|
+
if (n === _0n) break;
|
|
209
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
210
|
+
n = nextN;
|
|
211
|
+
if (isZero) continue;
|
|
212
|
+
else {
|
|
213
|
+
const item = precomputes[offset];
|
|
214
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
assert0(n);
|
|
218
|
+
return acc;
|
|
219
|
+
}
|
|
220
|
+
getPrecomputes(W, point, transform) {
|
|
221
|
+
let comp = pointPrecomputes.get(point);
|
|
222
|
+
if (!comp) {
|
|
223
|
+
comp = this.precomputeWindow(point, W);
|
|
224
|
+
if (W !== 1) {
|
|
225
|
+
if (typeof transform === "function") comp = transform(comp);
|
|
226
|
+
pointPrecomputes.set(point, comp);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return comp;
|
|
230
|
+
}
|
|
231
|
+
cached(point, scalar, transform) {
|
|
232
|
+
const W = getW(point);
|
|
233
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
234
|
+
}
|
|
235
|
+
unsafe(point, scalar, transform, prev) {
|
|
236
|
+
const W = getW(point);
|
|
237
|
+
if (W === 1) return this._unsafeLadder(point, scalar, prev);
|
|
238
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
239
|
+
}
|
|
240
|
+
createCache(P, W) {
|
|
241
|
+
validateW(W, this.bits);
|
|
242
|
+
pointWindowSizes.set(P, W);
|
|
243
|
+
pointPrecomputes.delete(P);
|
|
244
|
+
}
|
|
245
|
+
hasCache(elm) {
|
|
246
|
+
return getW(elm) !== 1;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
function createField(order, field, isLE) {
|
|
250
|
+
if (field) {
|
|
251
|
+
if (field.ORDER !== order) throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
252
|
+
require_modular.validateField(field);
|
|
253
|
+
return field;
|
|
254
|
+
} else return require_modular.Field(order, { isLE });
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Validates basic CURVE shape and field membership, then creates fields.
|
|
258
|
+
* This does not prove that the generator is on-curve, that subgroup/order data are consistent, or
|
|
259
|
+
* that the curve equation itself is otherwise sane.
|
|
260
|
+
* @param type - Curve family.
|
|
261
|
+
* @param CURVE - Curve parameters.
|
|
262
|
+
* @param curveOpts - Optional field overrides:
|
|
263
|
+
* - `Fp` (optional): Optional base-field override.
|
|
264
|
+
* - `Fn` (optional): Optional scalar-field override.
|
|
265
|
+
* @param FpFnLE - Whether field encoding is little-endian.
|
|
266
|
+
* @returns Frozen curve parameters and fields.
|
|
267
|
+
* @throws If the curve parameters or field overrides are invalid. {@link Error}
|
|
268
|
+
* @example
|
|
269
|
+
* Build curve fields from raw constants before constructing a curve instance.
|
|
270
|
+
*
|
|
271
|
+
* ```ts
|
|
272
|
+
* const curve = createCurveFields('weierstrass', {
|
|
273
|
+
* p: 17n,
|
|
274
|
+
* n: 19n,
|
|
275
|
+
* h: 1n,
|
|
276
|
+
* a: 2n,
|
|
277
|
+
* b: 2n,
|
|
278
|
+
* Gx: 5n,
|
|
279
|
+
* Gy: 1n,
|
|
280
|
+
* });
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
284
|
+
if (FpFnLE === void 0) FpFnLE = type === "edwards";
|
|
285
|
+
if (!CURVE || typeof CURVE !== "object") throw new Error(`expected valid ${type} CURVE object`);
|
|
286
|
+
for (const p of [
|
|
287
|
+
"p",
|
|
288
|
+
"n",
|
|
289
|
+
"h"
|
|
290
|
+
]) {
|
|
291
|
+
const val = CURVE[p];
|
|
292
|
+
if (!(typeof val === "bigint" && val > _0n)) throw new Error(`CURVE.${p} must be positive bigint`);
|
|
293
|
+
}
|
|
294
|
+
const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
295
|
+
const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
296
|
+
const params = [
|
|
297
|
+
"Gx",
|
|
298
|
+
"Gy",
|
|
299
|
+
"a",
|
|
300
|
+
type === "weierstrass" ? "b" : "d"
|
|
301
|
+
];
|
|
302
|
+
for (const p of params) if (!Fp.isValid(CURVE[p])) throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
303
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
304
|
+
return {
|
|
305
|
+
CURVE,
|
|
306
|
+
Fp,
|
|
307
|
+
Fn
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* @param randomSecretKey - Secret-key generator.
|
|
312
|
+
* @param getPublicKey - Public-key derivation helper.
|
|
313
|
+
* @returns Keypair generator.
|
|
314
|
+
* @example
|
|
315
|
+
* Build a `keygen()` helper from existing secret-key and public-key primitives.
|
|
316
|
+
*
|
|
317
|
+
* ```ts
|
|
318
|
+
* import { createKeygen } from '@noble/curves/abstract/curve.js';
|
|
319
|
+
* import { p256 } from '@noble/curves/nist.js';
|
|
320
|
+
* const keygen = createKeygen(p256.utils.randomSecretKey, p256.getPublicKey);
|
|
321
|
+
* const pair = keygen();
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
function createKeygen(randomSecretKey, getPublicKey) {
|
|
325
|
+
return function keygen(seed) {
|
|
326
|
+
const secretKey = randomSecretKey(seed);
|
|
327
|
+
return {
|
|
328
|
+
secretKey,
|
|
329
|
+
publicKey: getPublicKey(secretKey)
|
|
330
|
+
};
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
//#endregion
|
|
335
|
+
exports.createCurveFields = createCurveFields;
|
|
336
|
+
exports.createKeygen = createKeygen;
|
|
337
|
+
exports.normalizeZ = normalizeZ;
|
|
338
|
+
exports.wNAF = wNAF;
|