@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.
@@ -1,299 +0,0 @@
1
- import { bitLen, bitMask } from "../utils.js";
2
- import { Field, FpInvertBatch, validateField } from "./modular.js";
3
-
4
- //#region node_modules/@noble/curves/esm/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 = BigInt(0);
12
- const _1n = BigInt(1);
13
- function negateCt(condition, item) {
14
- const neg = item.negate();
15
- return condition ? neg : item;
16
- }
17
- /**
18
- * Takes a bunch of Projective Points but executes only one
19
- * inversion on all of them. Inversion is very slow operation,
20
- * so this improves performance massively.
21
- * Optimization: converts a list of projective points to a list of identical points with Z=1.
22
- */
23
- function normalizeZ(c, points) {
24
- const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
25
- return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
26
- }
27
- function validateW(W, bits) {
28
- if (!Number.isSafeInteger(W) || W <= 0 || W > bits) throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
29
- }
30
- function calcWOpts(W, scalarBits) {
31
- validateW(W, scalarBits);
32
- const windows = Math.ceil(scalarBits / W) + 1;
33
- const windowSize = 2 ** (W - 1);
34
- const maxNumber = 2 ** W;
35
- return {
36
- windows,
37
- windowSize,
38
- mask: bitMask(W),
39
- maxNumber,
40
- shiftBy: BigInt(W)
41
- };
42
- }
43
- function calcOffsets(n, window, wOpts) {
44
- const { windowSize, mask, maxNumber, shiftBy } = wOpts;
45
- let wbits = Number(n & mask);
46
- let nextN = n >> shiftBy;
47
- if (wbits > windowSize) {
48
- wbits -= maxNumber;
49
- nextN += _1n;
50
- }
51
- const offsetStart = window * windowSize;
52
- const offset = offsetStart + Math.abs(wbits) - 1;
53
- const isZero = wbits === 0;
54
- const isNeg = wbits < 0;
55
- const isNegF = window % 2 !== 0;
56
- return {
57
- nextN,
58
- offset,
59
- isZero,
60
- isNeg,
61
- isNegF,
62
- offsetF: offsetStart
63
- };
64
- }
65
- function validateMSMPoints(points, c) {
66
- if (!Array.isArray(points)) throw new Error("array expected");
67
- points.forEach((p, i) => {
68
- if (!(p instanceof c)) throw new Error("invalid point at index " + i);
69
- });
70
- }
71
- function validateMSMScalars(scalars, field) {
72
- if (!Array.isArray(scalars)) throw new Error("array of scalars expected");
73
- scalars.forEach((s, i) => {
74
- if (!field.isValid(s)) throw new Error("invalid scalar at index " + i);
75
- });
76
- }
77
- const pointPrecomputes = /* @__PURE__ */ new WeakMap();
78
- const pointWindowSizes = /* @__PURE__ */ new WeakMap();
79
- function getW(P) {
80
- return pointWindowSizes.get(P) || 1;
81
- }
82
- function assert0(n) {
83
- if (n !== _0n) throw new Error("invalid wNAF");
84
- }
85
- /**
86
- * Elliptic curve multiplication of Point by scalar. Fragile.
87
- * Table generation takes **30MB of ram and 10ms on high-end CPU**,
88
- * but may take much longer on slow devices. Actual generation will happen on
89
- * first call of `multiply()`. By default, `BASE` point is precomputed.
90
- *
91
- * Scalars should always be less than curve order: this should be checked inside of a curve itself.
92
- * Creates precomputation tables for fast multiplication:
93
- * - private scalar is split by fixed size windows of W bits
94
- * - every window point is collected from window's table & added to accumulator
95
- * - since windows are different, same point inside tables won't be accessed more than once per calc
96
- * - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
97
- * - +1 window is neccessary for wNAF
98
- * - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
99
- *
100
- * @todo Research returning 2d JS array of windows, instead of a single window.
101
- * This would allow windows to be in different memory locations
102
- */
103
- var wNAF = class {
104
- constructor(Point, bits) {
105
- this.BASE = Point.BASE;
106
- this.ZERO = Point.ZERO;
107
- this.Fn = Point.Fn;
108
- this.bits = bits;
109
- }
110
- _unsafeLadder(elm, n, p = this.ZERO) {
111
- let d = elm;
112
- while (n > _0n) {
113
- if (n & _1n) p = p.add(d);
114
- d = d.double();
115
- n >>= _1n;
116
- }
117
- return p;
118
- }
119
- /**
120
- * Creates a wNAF precomputation window. Used for caching.
121
- * Default window size is set by `utils.precompute()` and is equal to 8.
122
- * Number of precomputed points depends on the curve size:
123
- * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
124
- * - 𝑊 is the window size
125
- * - 𝑛 is the bitlength of the curve order.
126
- * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
127
- * @param point Point instance
128
- * @param W window size
129
- * @returns precomputed point tables flattened to a single array
130
- */
131
- precomputeWindow(point, W) {
132
- const { windows, windowSize } = calcWOpts(W, this.bits);
133
- const points = [];
134
- let p = point;
135
- let base = p;
136
- for (let window = 0; window < windows; window++) {
137
- base = p;
138
- points.push(base);
139
- for (let i = 1; i < windowSize; i++) {
140
- base = base.add(p);
141
- points.push(base);
142
- }
143
- p = base.double();
144
- }
145
- return points;
146
- }
147
- /**
148
- * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
149
- * More compact implementation:
150
- * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
151
- * @returns real and fake (for const-time) points
152
- */
153
- wNAF(W, precomputes, n) {
154
- if (!this.Fn.isValid(n)) throw new Error("invalid scalar");
155
- let p = this.ZERO;
156
- let f = this.BASE;
157
- const wo = calcWOpts(W, this.bits);
158
- for (let window = 0; window < wo.windows; window++) {
159
- const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
160
- n = nextN;
161
- if (isZero) f = f.add(negateCt(isNegF, precomputes[offsetF]));
162
- else p = p.add(negateCt(isNeg, precomputes[offset]));
163
- }
164
- assert0(n);
165
- return {
166
- p,
167
- f
168
- };
169
- }
170
- /**
171
- * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
172
- * @param acc accumulator point to add result of multiplication
173
- * @returns point
174
- */
175
- wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
176
- const wo = calcWOpts(W, this.bits);
177
- for (let window = 0; window < wo.windows; window++) {
178
- if (n === _0n) break;
179
- const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
180
- n = nextN;
181
- if (isZero) continue;
182
- else {
183
- const item = precomputes[offset];
184
- acc = acc.add(isNeg ? item.negate() : item);
185
- }
186
- }
187
- assert0(n);
188
- return acc;
189
- }
190
- getPrecomputes(W, point, transform) {
191
- let comp = pointPrecomputes.get(point);
192
- if (!comp) {
193
- comp = this.precomputeWindow(point, W);
194
- if (W !== 1) {
195
- if (typeof transform === "function") comp = transform(comp);
196
- pointPrecomputes.set(point, comp);
197
- }
198
- }
199
- return comp;
200
- }
201
- cached(point, scalar, transform) {
202
- const W = getW(point);
203
- return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
204
- }
205
- unsafe(point, scalar, transform, prev) {
206
- const W = getW(point);
207
- if (W === 1) return this._unsafeLadder(point, scalar, prev);
208
- return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
209
- }
210
- createCache(P, W) {
211
- validateW(W, this.bits);
212
- pointWindowSizes.set(P, W);
213
- pointPrecomputes.delete(P);
214
- }
215
- hasCache(elm) {
216
- return getW(elm) !== 1;
217
- }
218
- };
219
- /**
220
- * Pippenger algorithm for multi-scalar multiplication (MSM, Pa + Qb + Rc + ...).
221
- * 30x faster vs naive addition on L=4096, 10x faster than precomputes.
222
- * For N=254bit, L=1, it does: 1024 ADD + 254 DBL. For L=5: 1536 ADD + 254 DBL.
223
- * Algorithmically constant-time (for same L), even when 1 point + scalar, or when scalar = 0.
224
- * @param c Curve Point constructor
225
- * @param fieldN field over CURVE.N - important that it's not over CURVE.P
226
- * @param points array of L curve points
227
- * @param scalars array of L scalars (aka secret keys / bigints)
228
- */
229
- function pippenger(c, fieldN, points, scalars) {
230
- validateMSMPoints(points, c);
231
- validateMSMScalars(scalars, fieldN);
232
- const plength = points.length;
233
- const slength = scalars.length;
234
- if (plength !== slength) throw new Error("arrays of points and scalars must have equal length");
235
- const zero = c.ZERO;
236
- const wbits = bitLen(BigInt(plength));
237
- let windowSize = 1;
238
- if (wbits > 12) windowSize = wbits - 3;
239
- else if (wbits > 4) windowSize = wbits - 2;
240
- else if (wbits > 0) windowSize = 2;
241
- const MASK = bitMask(windowSize);
242
- const buckets = new Array(Number(MASK) + 1).fill(zero);
243
- const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
244
- let sum = zero;
245
- for (let i = lastBits; i >= 0; i -= windowSize) {
246
- buckets.fill(zero);
247
- for (let j = 0; j < slength; j++) {
248
- const scalar = scalars[j];
249
- const wbits$1 = Number(scalar >> BigInt(i) & MASK);
250
- buckets[wbits$1] = buckets[wbits$1].add(points[j]);
251
- }
252
- let resI = zero;
253
- for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
254
- sumI = sumI.add(buckets[j]);
255
- resI = resI.add(sumI);
256
- }
257
- sum = sum.add(resI);
258
- if (i !== 0) for (let j = 0; j < windowSize; j++) sum = sum.double();
259
- }
260
- return sum;
261
- }
262
- function createField(order, field, isLE) {
263
- if (field) {
264
- if (field.ORDER !== order) throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
265
- validateField(field);
266
- return field;
267
- } else return Field(order, { isLE });
268
- }
269
- /** Validates CURVE opts and creates fields */
270
- function _createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
271
- if (FpFnLE === void 0) FpFnLE = type === "edwards";
272
- if (!CURVE || typeof CURVE !== "object") throw new Error(`expected valid ${type} CURVE object`);
273
- for (const p of [
274
- "p",
275
- "n",
276
- "h"
277
- ]) {
278
- const val = CURVE[p];
279
- if (!(typeof val === "bigint" && val > _0n)) throw new Error(`CURVE.${p} must be positive bigint`);
280
- }
281
- const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
282
- const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
283
- const params = [
284
- "Gx",
285
- "Gy",
286
- "a",
287
- type === "weierstrass" ? "b" : "d"
288
- ];
289
- for (const p of params) if (!Fp.isValid(CURVE[p])) throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
290
- CURVE = Object.freeze(Object.assign({}, CURVE));
291
- return {
292
- CURVE,
293
- Fp,
294
- Fn
295
- };
296
- }
297
-
298
- //#endregion
299
- export { _createCurveFields, normalizeZ, pippenger, wNAF };