@fangorn-network/sdk 2026.7.13 → 2026.7.24
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/README.md +23 -2
- package/lib/cli/cli.js +18 -11
- package/lib/cli/index.d.ts +13 -13
- package/lib/config.d.ts +93 -92
- package/lib/contracts/data-registry/abi.d.ts +207 -35
- package/lib/contracts/data-registry/index.js +4 -4
- package/lib/contracts/index.d.ts +3 -1
- package/lib/contracts/index.js +3 -1
- package/lib/contracts/settlement-registry/abi.d.ts +47 -0
- package/lib/contracts/settlement-registry/abi.js +48 -0
- package/lib/contracts/settlement-registry/index.d.ts +25 -0
- package/lib/contracts/settlement-registry/index.js +40 -0
- package/lib/crypto/aes.d.ts +1 -8
- package/lib/crypto/aes.js +2 -34
- package/lib/crypto/encryption.d.ts +112 -26
- package/lib/crypto/encryption.js +106 -22
- package/lib/engine/index.js +6 -3
- package/lib/fangorn.d.ts +4 -4
- package/lib/fangorn.js +20 -18
- package/lib/harness/index.d.ts +4 -3
- package/lib/harness/index.js +9 -3
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/lib/providers/storage/car.js +3 -2
- package/lib/providers/storage/pinata.js +27 -7
- package/lib/{roles/repo → repo}/index.d.ts +1 -1
- package/lib/{roles/repo → repo}/index.js +1 -1
- package/lib/test/testbed.d.ts +2 -2
- package/lib/test/testbed.js +1 -1
- package/package.json +4 -2
- package/lib/ crypto/constants.d.ts +0 -4
- package/lib/ crypto/constants.js +0 -5
- package/lib/ crypto/merkle.d.ts +0 -10
- package/lib/ crypto/merkle.js +0 -34
- package/lib/ crypto/utils.d.ts +0 -1
- package/lib/ crypto/utils.js +0 -1
- package/lib/builders/a2aCardBuilder.d.ts +0 -28
- package/lib/builders/a2aCardBuilder.js +0 -105
- package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/_polyval.js +0 -218
- package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/aes.js +0 -261
- package/lib/node_modules/.pnpm/@noble_ciphers@1.3.0/node_modules/@noble/ciphers/esm/utils.js +0 -156
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/curve.js +0 -299
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/edwards.js +0 -532
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/modular.js +0 -344
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/abstract/montgomery.js +0 -137
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/ed25519.js +0 -262
- package/lib/node_modules/.pnpm/@noble_curves@1.9.7/node_modules/@noble/curves/esm/utils.js +0 -150
- package/lib/roles/index.d.ts +0 -1
- package/lib/roles/index.js +0 -1
- package/lib/test/car_smoke.d.ts +0 -1
- package/lib/test/car_smoke.js +0 -81
- package/lib/test/cleanup-embeddings-testdata.d.ts +0 -1
- package/lib/test/cleanup-embeddings-testdata.js +0 -75
- package/lib/test/index.d.ts +0 -10
- package/lib/test/index.js +0 -27
- package/lib/test/setup-embeddings-testdata.d.ts +0 -11
- package/lib/test/setup-embeddings-testdata.js +0 -155
- package/lib/utils/index.d.ts +0 -11
- package/lib/utils/index.js +0 -29
- package/lib/utils/manifest.d.ts +0 -23
- package/lib/utils/manifest.js +0 -77
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import { _validateObject, anumber, bitMask, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "../utils.js";
|
|
2
|
-
|
|
3
|
-
//#region node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/abstract/modular.js
|
|
4
|
-
/**
|
|
5
|
-
* Utils for modular division and fields.
|
|
6
|
-
* Field over 11 is a finite (Galois) field is integer number operations `mod 11`.
|
|
7
|
-
* There is no division: it is replaced by modular multiplicative inverse.
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
11
|
-
const _0n = BigInt(0), _1n = BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
|
|
12
|
-
const _4n = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _7n = /* @__PURE__ */ BigInt(7);
|
|
13
|
-
const _8n = /* @__PURE__ */ BigInt(8), _9n = /* @__PURE__ */ BigInt(9), _16n = /* @__PURE__ */ BigInt(16);
|
|
14
|
-
function mod(a, b) {
|
|
15
|
-
const result = a % b;
|
|
16
|
-
return result >= _0n ? result : b + result;
|
|
17
|
-
}
|
|
18
|
-
/** Does `x^(2^power)` mod p. `pow2(30, 4)` == `30^(2^4)` */
|
|
19
|
-
function pow2(x, power, modulo) {
|
|
20
|
-
let res = x;
|
|
21
|
-
while (power-- > _0n) {
|
|
22
|
-
res *= res;
|
|
23
|
-
res %= modulo;
|
|
24
|
-
}
|
|
25
|
-
return res;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Inverses number over modulo.
|
|
29
|
-
* Implemented using [Euclidean GCD](https://brilliant.org/wiki/extended-euclidean-algorithm/).
|
|
30
|
-
*/
|
|
31
|
-
function invert(number, modulo) {
|
|
32
|
-
if (number === _0n) throw new Error("invert: expected non-zero number");
|
|
33
|
-
if (modulo <= _0n) throw new Error("invert: expected positive modulus, got " + modulo);
|
|
34
|
-
let a = mod(number, modulo);
|
|
35
|
-
let b = modulo;
|
|
36
|
-
let x = _0n, y = _1n, u = _1n, v = _0n;
|
|
37
|
-
while (a !== _0n) {
|
|
38
|
-
const q = b / a;
|
|
39
|
-
const r = b % a;
|
|
40
|
-
const m = x - u * q;
|
|
41
|
-
const n = y - v * q;
|
|
42
|
-
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
43
|
-
}
|
|
44
|
-
if (b !== _1n) throw new Error("invert: does not exist");
|
|
45
|
-
return mod(x, modulo);
|
|
46
|
-
}
|
|
47
|
-
function assertIsSquare(Fp, root, n) {
|
|
48
|
-
if (!Fp.eql(Fp.sqr(root), n)) throw new Error("Cannot find square root");
|
|
49
|
-
}
|
|
50
|
-
function sqrt3mod4(Fp, n) {
|
|
51
|
-
const p1div4 = (Fp.ORDER + _1n) / _4n;
|
|
52
|
-
const root = Fp.pow(n, p1div4);
|
|
53
|
-
assertIsSquare(Fp, root, n);
|
|
54
|
-
return root;
|
|
55
|
-
}
|
|
56
|
-
function sqrt5mod8(Fp, n) {
|
|
57
|
-
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
|
58
|
-
const n2 = Fp.mul(n, _2n);
|
|
59
|
-
const v = Fp.pow(n2, p5div8);
|
|
60
|
-
const nv = Fp.mul(n, v);
|
|
61
|
-
const i = Fp.mul(Fp.mul(nv, _2n), v);
|
|
62
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
63
|
-
assertIsSquare(Fp, root, n);
|
|
64
|
-
return root;
|
|
65
|
-
}
|
|
66
|
-
function sqrt9mod16(P) {
|
|
67
|
-
const Fp_ = Field(P);
|
|
68
|
-
const tn = tonelliShanks(P);
|
|
69
|
-
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
70
|
-
const c2 = tn(Fp_, c1);
|
|
71
|
-
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
72
|
-
const c4 = (P + _7n) / _16n;
|
|
73
|
-
return (Fp, n) => {
|
|
74
|
-
let tv1 = Fp.pow(n, c4);
|
|
75
|
-
let tv2 = Fp.mul(tv1, c1);
|
|
76
|
-
const tv3 = Fp.mul(tv1, c2);
|
|
77
|
-
const tv4 = Fp.mul(tv1, c3);
|
|
78
|
-
const e1 = Fp.eql(Fp.sqr(tv2), n);
|
|
79
|
-
const e2 = Fp.eql(Fp.sqr(tv3), n);
|
|
80
|
-
tv1 = Fp.cmov(tv1, tv2, e1);
|
|
81
|
-
tv2 = Fp.cmov(tv4, tv3, e2);
|
|
82
|
-
const e3 = Fp.eql(Fp.sqr(tv2), n);
|
|
83
|
-
const root = Fp.cmov(tv1, tv2, e3);
|
|
84
|
-
assertIsSquare(Fp, root, n);
|
|
85
|
-
return root;
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Tonelli-Shanks square root search algorithm.
|
|
90
|
-
* 1. https://eprint.iacr.org/2012/685.pdf (page 12)
|
|
91
|
-
* 2. Square Roots from 1; 24, 51, 10 to Dan Shanks
|
|
92
|
-
* @param P field order
|
|
93
|
-
* @returns function that takes field Fp (created from P) and number n
|
|
94
|
-
*/
|
|
95
|
-
function tonelliShanks(P) {
|
|
96
|
-
if (P < _3n) throw new Error("sqrt is not defined for small field");
|
|
97
|
-
let Q = P - _1n;
|
|
98
|
-
let S = 0;
|
|
99
|
-
while (Q % _2n === _0n) {
|
|
100
|
-
Q /= _2n;
|
|
101
|
-
S++;
|
|
102
|
-
}
|
|
103
|
-
let Z = _2n;
|
|
104
|
-
const _Fp = Field(P);
|
|
105
|
-
while (FpLegendre(_Fp, Z) === 1) if (Z++ > 1e3) throw new Error("Cannot find square root: probably non-prime P");
|
|
106
|
-
if (S === 1) return sqrt3mod4;
|
|
107
|
-
let cc = _Fp.pow(Z, Q);
|
|
108
|
-
const Q1div2 = (Q + _1n) / _2n;
|
|
109
|
-
return function tonelliSlow(Fp, n) {
|
|
110
|
-
if (Fp.is0(n)) return n;
|
|
111
|
-
if (FpLegendre(Fp, n) !== 1) throw new Error("Cannot find square root");
|
|
112
|
-
let M = S;
|
|
113
|
-
let c = Fp.mul(Fp.ONE, cc);
|
|
114
|
-
let t = Fp.pow(n, Q);
|
|
115
|
-
let R = Fp.pow(n, Q1div2);
|
|
116
|
-
while (!Fp.eql(t, Fp.ONE)) {
|
|
117
|
-
if (Fp.is0(t)) return Fp.ZERO;
|
|
118
|
-
let i = 1;
|
|
119
|
-
let t_tmp = Fp.sqr(t);
|
|
120
|
-
while (!Fp.eql(t_tmp, Fp.ONE)) {
|
|
121
|
-
i++;
|
|
122
|
-
t_tmp = Fp.sqr(t_tmp);
|
|
123
|
-
if (i === M) throw new Error("Cannot find square root");
|
|
124
|
-
}
|
|
125
|
-
const exponent = _1n << BigInt(M - i - 1);
|
|
126
|
-
const b = Fp.pow(c, exponent);
|
|
127
|
-
M = i;
|
|
128
|
-
c = Fp.sqr(b);
|
|
129
|
-
t = Fp.mul(t, c);
|
|
130
|
-
R = Fp.mul(R, b);
|
|
131
|
-
}
|
|
132
|
-
return R;
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Square root for a finite field. Will try optimized versions first:
|
|
137
|
-
*
|
|
138
|
-
* 1. P ≡ 3 (mod 4)
|
|
139
|
-
* 2. P ≡ 5 (mod 8)
|
|
140
|
-
* 3. P ≡ 9 (mod 16)
|
|
141
|
-
* 4. Tonelli-Shanks algorithm
|
|
142
|
-
*
|
|
143
|
-
* Different algorithms can give different roots, it is up to user to decide which one they want.
|
|
144
|
-
* For example there is FpSqrtOdd/FpSqrtEven to choice root based on oddness (used for hash-to-curve).
|
|
145
|
-
*/
|
|
146
|
-
function FpSqrt(P) {
|
|
147
|
-
if (P % _4n === _3n) return sqrt3mod4;
|
|
148
|
-
if (P % _8n === _5n) return sqrt5mod8;
|
|
149
|
-
if (P % _16n === _9n) return sqrt9mod16(P);
|
|
150
|
-
return tonelliShanks(P);
|
|
151
|
-
}
|
|
152
|
-
const isNegativeLE = (num, modulo) => (mod(num, modulo) & _1n) === _1n;
|
|
153
|
-
const FIELD_FIELDS = [
|
|
154
|
-
"create",
|
|
155
|
-
"isValid",
|
|
156
|
-
"is0",
|
|
157
|
-
"neg",
|
|
158
|
-
"inv",
|
|
159
|
-
"sqrt",
|
|
160
|
-
"sqr",
|
|
161
|
-
"eql",
|
|
162
|
-
"add",
|
|
163
|
-
"sub",
|
|
164
|
-
"mul",
|
|
165
|
-
"pow",
|
|
166
|
-
"div",
|
|
167
|
-
"addN",
|
|
168
|
-
"subN",
|
|
169
|
-
"mulN",
|
|
170
|
-
"sqrN"
|
|
171
|
-
];
|
|
172
|
-
function validateField(field) {
|
|
173
|
-
_validateObject(field, FIELD_FIELDS.reduce((map, val) => {
|
|
174
|
-
map[val] = "function";
|
|
175
|
-
return map;
|
|
176
|
-
}, {
|
|
177
|
-
ORDER: "bigint",
|
|
178
|
-
MASK: "bigint",
|
|
179
|
-
BYTES: "number",
|
|
180
|
-
BITS: "number"
|
|
181
|
-
}));
|
|
182
|
-
return field;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Same as `pow` but for Fp: non-constant-time.
|
|
186
|
-
* Unsafe in some contexts: uses ladder, so can expose bigint bits.
|
|
187
|
-
*/
|
|
188
|
-
function FpPow(Fp, num, power) {
|
|
189
|
-
if (power < _0n) throw new Error("invalid exponent, negatives unsupported");
|
|
190
|
-
if (power === _0n) return Fp.ONE;
|
|
191
|
-
if (power === _1n) return num;
|
|
192
|
-
let p = Fp.ONE;
|
|
193
|
-
let d = num;
|
|
194
|
-
while (power > _0n) {
|
|
195
|
-
if (power & _1n) p = Fp.mul(p, d);
|
|
196
|
-
d = Fp.sqr(d);
|
|
197
|
-
power >>= _1n;
|
|
198
|
-
}
|
|
199
|
-
return p;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Efficiently invert an array of Field elements.
|
|
203
|
-
* Exception-free. Will return `undefined` for 0 elements.
|
|
204
|
-
* @param passZero map 0 to 0 (instead of undefined)
|
|
205
|
-
*/
|
|
206
|
-
function FpInvertBatch(Fp, nums, passZero = false) {
|
|
207
|
-
const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
|
|
208
|
-
const multipliedAcc = nums.reduce((acc, num, i) => {
|
|
209
|
-
if (Fp.is0(num)) return acc;
|
|
210
|
-
inverted[i] = acc;
|
|
211
|
-
return Fp.mul(acc, num);
|
|
212
|
-
}, Fp.ONE);
|
|
213
|
-
const invertedAcc = Fp.inv(multipliedAcc);
|
|
214
|
-
nums.reduceRight((acc, num, i) => {
|
|
215
|
-
if (Fp.is0(num)) return acc;
|
|
216
|
-
inverted[i] = Fp.mul(acc, inverted[i]);
|
|
217
|
-
return Fp.mul(acc, num);
|
|
218
|
-
}, invertedAcc);
|
|
219
|
-
return inverted;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Legendre symbol.
|
|
223
|
-
* Legendre constant is used to calculate Legendre symbol (a | p)
|
|
224
|
-
* which denotes the value of a^((p-1)/2) (mod p).
|
|
225
|
-
*
|
|
226
|
-
* * (a | p) ≡ 1 if a is a square (mod p), quadratic residue
|
|
227
|
-
* * (a | p) ≡ -1 if a is not a square (mod p), quadratic non residue
|
|
228
|
-
* * (a | p) ≡ 0 if a ≡ 0 (mod p)
|
|
229
|
-
*/
|
|
230
|
-
function FpLegendre(Fp, n) {
|
|
231
|
-
const p1mod2 = (Fp.ORDER - _1n) / _2n;
|
|
232
|
-
const powered = Fp.pow(n, p1mod2);
|
|
233
|
-
const yes = Fp.eql(powered, Fp.ONE);
|
|
234
|
-
const zero = Fp.eql(powered, Fp.ZERO);
|
|
235
|
-
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
236
|
-
if (!yes && !zero && !no) throw new Error("invalid Legendre symbol result");
|
|
237
|
-
return yes ? 1 : zero ? 0 : -1;
|
|
238
|
-
}
|
|
239
|
-
function nLength(n, nBitLength) {
|
|
240
|
-
if (nBitLength !== void 0) anumber(nBitLength);
|
|
241
|
-
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
242
|
-
return {
|
|
243
|
-
nBitLength: _nBitLength,
|
|
244
|
-
nByteLength: Math.ceil(_nBitLength / 8)
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Creates a finite field. Major performance optimizations:
|
|
249
|
-
* * 1. Denormalized operations like mulN instead of mul.
|
|
250
|
-
* * 2. Identical object shape: never add or remove keys.
|
|
251
|
-
* * 3. `Object.freeze`.
|
|
252
|
-
* Fragile: always run a benchmark on a change.
|
|
253
|
-
* Security note: operations don't check 'isValid' for all elements for performance reasons,
|
|
254
|
-
* it is caller responsibility to check this.
|
|
255
|
-
* This is low-level code, please make sure you know what you're doing.
|
|
256
|
-
*
|
|
257
|
-
* Note about field properties:
|
|
258
|
-
* * CHARACTERISTIC p = prime number, number of elements in main subgroup.
|
|
259
|
-
* * ORDER q = similar to cofactor in curves, may be composite `q = p^m`.
|
|
260
|
-
*
|
|
261
|
-
* @param ORDER field order, probably prime, or could be composite
|
|
262
|
-
* @param bitLen how many bits the field consumes
|
|
263
|
-
* @param isLE (default: false) if encoding / decoding should be in little-endian
|
|
264
|
-
* @param redef optional faster redefinitions of sqrt and other methods
|
|
265
|
-
*/
|
|
266
|
-
function Field(ORDER, bitLenOrOpts, isLE = false, opts = {}) {
|
|
267
|
-
if (ORDER <= _0n) throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
268
|
-
let _nbitLength = void 0;
|
|
269
|
-
let _sqrt = void 0;
|
|
270
|
-
let modFromBytes = false;
|
|
271
|
-
let allowedLengths = void 0;
|
|
272
|
-
if (typeof bitLenOrOpts === "object" && bitLenOrOpts != null) {
|
|
273
|
-
if (opts.sqrt || isLE) throw new Error("cannot specify opts in two arguments");
|
|
274
|
-
const _opts = bitLenOrOpts;
|
|
275
|
-
if (_opts.BITS) _nbitLength = _opts.BITS;
|
|
276
|
-
if (_opts.sqrt) _sqrt = _opts.sqrt;
|
|
277
|
-
if (typeof _opts.isLE === "boolean") isLE = _opts.isLE;
|
|
278
|
-
if (typeof _opts.modFromBytes === "boolean") modFromBytes = _opts.modFromBytes;
|
|
279
|
-
allowedLengths = _opts.allowedLengths;
|
|
280
|
-
} else {
|
|
281
|
-
if (typeof bitLenOrOpts === "number") _nbitLength = bitLenOrOpts;
|
|
282
|
-
if (opts.sqrt) _sqrt = opts.sqrt;
|
|
283
|
-
}
|
|
284
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, _nbitLength);
|
|
285
|
-
if (BYTES > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
286
|
-
let sqrtP;
|
|
287
|
-
const f = Object.freeze({
|
|
288
|
-
ORDER,
|
|
289
|
-
isLE,
|
|
290
|
-
BITS,
|
|
291
|
-
BYTES,
|
|
292
|
-
MASK: bitMask(BITS),
|
|
293
|
-
ZERO: _0n,
|
|
294
|
-
ONE: _1n,
|
|
295
|
-
allowedLengths,
|
|
296
|
-
create: (num) => mod(num, ORDER),
|
|
297
|
-
isValid: (num) => {
|
|
298
|
-
if (typeof num !== "bigint") throw new Error("invalid field element: expected bigint, got " + typeof num);
|
|
299
|
-
return _0n <= num && num < ORDER;
|
|
300
|
-
},
|
|
301
|
-
is0: (num) => num === _0n,
|
|
302
|
-
isValidNot0: (num) => !f.is0(num) && f.isValid(num),
|
|
303
|
-
isOdd: (num) => (num & _1n) === _1n,
|
|
304
|
-
neg: (num) => mod(-num, ORDER),
|
|
305
|
-
eql: (lhs, rhs) => lhs === rhs,
|
|
306
|
-
sqr: (num) => mod(num * num, ORDER),
|
|
307
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
|
308
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
|
309
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
|
310
|
-
pow: (num, power) => FpPow(f, num, power),
|
|
311
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
|
312
|
-
sqrN: (num) => num * num,
|
|
313
|
-
addN: (lhs, rhs) => lhs + rhs,
|
|
314
|
-
subN: (lhs, rhs) => lhs - rhs,
|
|
315
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
|
316
|
-
inv: (num) => invert(num, ORDER),
|
|
317
|
-
sqrt: _sqrt || ((n) => {
|
|
318
|
-
if (!sqrtP) sqrtP = FpSqrt(ORDER);
|
|
319
|
-
return sqrtP(f, n);
|
|
320
|
-
}),
|
|
321
|
-
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
|
322
|
-
fromBytes: (bytes, skipValidation = true) => {
|
|
323
|
-
if (allowedLengths) {
|
|
324
|
-
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
325
|
-
const padded = new Uint8Array(BYTES);
|
|
326
|
-
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
327
|
-
bytes = padded;
|
|
328
|
-
}
|
|
329
|
-
if (bytes.length !== BYTES) throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
330
|
-
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
331
|
-
if (modFromBytes) scalar = mod(scalar, ORDER);
|
|
332
|
-
if (!skipValidation) {
|
|
333
|
-
if (!f.isValid(scalar)) throw new Error("invalid field element: outside of range 0..ORDER");
|
|
334
|
-
}
|
|
335
|
-
return scalar;
|
|
336
|
-
},
|
|
337
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
|
338
|
-
cmov: (a, b, c) => c ? b : a
|
|
339
|
-
});
|
|
340
|
-
return Object.freeze(f);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
//#endregion
|
|
344
|
-
export { Field, FpInvertBatch, isNegativeLE, mod, pow2, validateField };
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { _validateObject, aInRange, abytes, bytesToNumberLE, ensureBytes, numberToBytesLE, randomBytes } from "../utils.js";
|
|
2
|
-
import { mod } from "./modular.js";
|
|
3
|
-
|
|
4
|
-
//#region node_modules/.pnpm/@noble+curves@1.9.7/node_modules/@noble/curves/esm/abstract/montgomery.js
|
|
5
|
-
/**
|
|
6
|
-
* Montgomery curve methods. It's not really whole montgomery curve,
|
|
7
|
-
* just bunch of very specific methods for X25519 / X448 from
|
|
8
|
-
* [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748)
|
|
9
|
-
* @module
|
|
10
|
-
*/
|
|
11
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
12
|
-
const _0n = BigInt(0);
|
|
13
|
-
const _1n = BigInt(1);
|
|
14
|
-
const _2n = BigInt(2);
|
|
15
|
-
function validateOpts(curve) {
|
|
16
|
-
_validateObject(curve, {
|
|
17
|
-
adjustScalarBytes: "function",
|
|
18
|
-
powPminus2: "function"
|
|
19
|
-
});
|
|
20
|
-
return Object.freeze({ ...curve });
|
|
21
|
-
}
|
|
22
|
-
function montgomery(curveDef) {
|
|
23
|
-
const { P, type, adjustScalarBytes, powPminus2, randomBytes: rand } = validateOpts(curveDef);
|
|
24
|
-
const is25519 = type === "x25519";
|
|
25
|
-
if (!is25519 && type !== "x448") throw new Error("invalid type");
|
|
26
|
-
const randomBytes_ = rand || randomBytes;
|
|
27
|
-
const montgomeryBits = is25519 ? 255 : 448;
|
|
28
|
-
const fieldLen = is25519 ? 32 : 56;
|
|
29
|
-
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
30
|
-
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
31
|
-
const minScalar = is25519 ? _2n ** BigInt(254) : _2n ** BigInt(447);
|
|
32
|
-
const maxScalar = minScalar + (is25519 ? BigInt(8) * _2n ** BigInt(251) - _1n : BigInt(4) * _2n ** BigInt(445) - _1n) + _1n;
|
|
33
|
-
const modP = (n) => mod(n, P);
|
|
34
|
-
const GuBytes = encodeU(Gu);
|
|
35
|
-
function encodeU(u) {
|
|
36
|
-
return numberToBytesLE(modP(u), fieldLen);
|
|
37
|
-
}
|
|
38
|
-
function decodeU(u) {
|
|
39
|
-
const _u = ensureBytes("u coordinate", u, fieldLen);
|
|
40
|
-
if (is25519) _u[31] &= 127;
|
|
41
|
-
return modP(bytesToNumberLE(_u));
|
|
42
|
-
}
|
|
43
|
-
function decodeScalar(scalar) {
|
|
44
|
-
return bytesToNumberLE(adjustScalarBytes(ensureBytes("scalar", scalar, fieldLen)));
|
|
45
|
-
}
|
|
46
|
-
function scalarMult(scalar, u) {
|
|
47
|
-
const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
|
|
48
|
-
if (pu === _0n) throw new Error("invalid private or public key received");
|
|
49
|
-
return encodeU(pu);
|
|
50
|
-
}
|
|
51
|
-
function scalarMultBase(scalar) {
|
|
52
|
-
return scalarMult(scalar, GuBytes);
|
|
53
|
-
}
|
|
54
|
-
function cswap(swap, x_2, x_3) {
|
|
55
|
-
const dummy = modP(swap * (x_2 - x_3));
|
|
56
|
-
x_2 = modP(x_2 - dummy);
|
|
57
|
-
x_3 = modP(x_3 + dummy);
|
|
58
|
-
return {
|
|
59
|
-
x_2,
|
|
60
|
-
x_3
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Montgomery x-only multiplication ladder.
|
|
65
|
-
* @param pointU u coordinate (x) on Montgomery Curve 25519
|
|
66
|
-
* @param scalar by which the point would be multiplied
|
|
67
|
-
* @returns new Point on Montgomery curve
|
|
68
|
-
*/
|
|
69
|
-
function montgomeryLadder(u, scalar) {
|
|
70
|
-
aInRange("u", u, _0n, P);
|
|
71
|
-
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
72
|
-
const k = scalar;
|
|
73
|
-
const x_1 = u;
|
|
74
|
-
let x_2 = _1n;
|
|
75
|
-
let z_2 = _0n;
|
|
76
|
-
let x_3 = u;
|
|
77
|
-
let z_3 = _1n;
|
|
78
|
-
let swap = _0n;
|
|
79
|
-
for (let t = BigInt(montgomeryBits - 1); t >= _0n; t--) {
|
|
80
|
-
const k_t = k >> t & _1n;
|
|
81
|
-
swap ^= k_t;
|
|
82
|
-
({x_2, x_3} = cswap(swap, x_2, x_3));
|
|
83
|
-
({x_2: z_2, x_3: z_3} = cswap(swap, z_2, z_3));
|
|
84
|
-
swap = k_t;
|
|
85
|
-
const A = x_2 + z_2;
|
|
86
|
-
const AA = modP(A * A);
|
|
87
|
-
const B = x_2 - z_2;
|
|
88
|
-
const BB = modP(B * B);
|
|
89
|
-
const E = AA - BB;
|
|
90
|
-
const C = x_3 + z_3;
|
|
91
|
-
const DA = modP((x_3 - z_3) * A);
|
|
92
|
-
const CB = modP(C * B);
|
|
93
|
-
const dacb = DA + CB;
|
|
94
|
-
const da_cb = DA - CB;
|
|
95
|
-
x_3 = modP(dacb * dacb);
|
|
96
|
-
z_3 = modP(x_1 * modP(da_cb * da_cb));
|
|
97
|
-
x_2 = modP(AA * BB);
|
|
98
|
-
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
99
|
-
}
|
|
100
|
-
({x_2, x_3} = cswap(swap, x_2, x_3));
|
|
101
|
-
({x_2: z_2, x_3: z_3} = cswap(swap, z_2, z_3));
|
|
102
|
-
const z2 = powPminus2(z_2);
|
|
103
|
-
return modP(x_2 * z2);
|
|
104
|
-
}
|
|
105
|
-
const lengths = {
|
|
106
|
-
secretKey: fieldLen,
|
|
107
|
-
publicKey: fieldLen,
|
|
108
|
-
seed: fieldLen
|
|
109
|
-
};
|
|
110
|
-
const randomSecretKey = (seed = randomBytes_(fieldLen)) => {
|
|
111
|
-
abytes(seed, lengths.seed);
|
|
112
|
-
return seed;
|
|
113
|
-
};
|
|
114
|
-
function keygen(seed) {
|
|
115
|
-
const secretKey = randomSecretKey(seed);
|
|
116
|
-
return {
|
|
117
|
-
secretKey,
|
|
118
|
-
publicKey: scalarMultBase(secretKey)
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
keygen,
|
|
123
|
-
getSharedSecret: (secretKey, publicKey) => scalarMult(secretKey, publicKey),
|
|
124
|
-
getPublicKey: (secretKey) => scalarMultBase(secretKey),
|
|
125
|
-
scalarMult,
|
|
126
|
-
scalarMultBase,
|
|
127
|
-
utils: {
|
|
128
|
-
randomSecretKey,
|
|
129
|
-
randomPrivateKey: randomSecretKey
|
|
130
|
-
},
|
|
131
|
-
GuBytes: GuBytes.slice(),
|
|
132
|
-
lengths
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
//#endregion
|
|
137
|
-
export { montgomery };
|