@fangorn-network/sdk 2026.7.14 → 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/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 +1 -1
- package/lib/fangorn.js +2 -6
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/providers/storage/car.js +1 -1
- package/lib/providers/storage/pinata.js +22 -3
- 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,156 +0,0 @@
|
|
|
1
|
-
//#region node_modules/@noble/ciphers/esm/utils.js
|
|
2
|
-
/**
|
|
3
|
-
* Utilities for hex, bytes, CSPRNG.
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
/*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
|
|
7
|
-
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
8
|
-
function isBytes(a) {
|
|
9
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
10
|
-
}
|
|
11
|
-
/** Asserts something is boolean. */
|
|
12
|
-
function abool(b) {
|
|
13
|
-
if (typeof b !== "boolean") throw new Error(`boolean expected, not ${b}`);
|
|
14
|
-
}
|
|
15
|
-
/** Asserts something is Uint8Array. */
|
|
16
|
-
function abytes(b, ...lengths) {
|
|
17
|
-
if (!isBytes(b)) throw new Error("Uint8Array expected");
|
|
18
|
-
if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
19
|
-
}
|
|
20
|
-
/** Asserts a hash instance has not been destroyed / finished */
|
|
21
|
-
function aexists(instance, checkFinished = true) {
|
|
22
|
-
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
|
23
|
-
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
|
24
|
-
}
|
|
25
|
-
/** Asserts output is properly-sized byte array */
|
|
26
|
-
function aoutput(out, instance) {
|
|
27
|
-
abytes(out);
|
|
28
|
-
const min = instance.outputLen;
|
|
29
|
-
if (out.length < min) throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
30
|
-
}
|
|
31
|
-
/** Cast u8 / u16 / u32 to u8. */
|
|
32
|
-
function u8(arr) {
|
|
33
|
-
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
34
|
-
}
|
|
35
|
-
/** Cast u8 / u16 / u32 to u32. */
|
|
36
|
-
function u32(arr) {
|
|
37
|
-
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
38
|
-
}
|
|
39
|
-
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
40
|
-
function clean(...arrays) {
|
|
41
|
-
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
42
|
-
}
|
|
43
|
-
/** Create DataView of an array for easy byte-level manipulation. */
|
|
44
|
-
function createView(arr) {
|
|
45
|
-
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
46
|
-
}
|
|
47
|
-
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
48
|
-
const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
|
|
49
|
-
/**
|
|
50
|
-
* Converts string to bytes using UTF8 encoding.
|
|
51
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
52
|
-
*/
|
|
53
|
-
function utf8ToBytes(str) {
|
|
54
|
-
if (typeof str !== "string") throw new Error("string expected");
|
|
55
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
59
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
60
|
-
* Keep in mind for future mutable operations.
|
|
61
|
-
*/
|
|
62
|
-
function toBytes(data) {
|
|
63
|
-
if (typeof data === "string") data = utf8ToBytes(data);
|
|
64
|
-
else if (isBytes(data)) data = copyBytes(data);
|
|
65
|
-
else throw new Error("Uint8Array expected, got " + typeof data);
|
|
66
|
-
return data;
|
|
67
|
-
}
|
|
68
|
-
/** Compares 2 uint8array-s in kinda constant time. */
|
|
69
|
-
function equalBytes(a, b) {
|
|
70
|
-
if (a.length !== b.length) return false;
|
|
71
|
-
let diff = 0;
|
|
72
|
-
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
73
|
-
return diff === 0;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Wraps a cipher: validates args, ensures encrypt() can only be called once.
|
|
77
|
-
* @__NO_SIDE_EFFECTS__
|
|
78
|
-
*/
|
|
79
|
-
const wrapCipher = (params, constructor) => {
|
|
80
|
-
function wrappedCipher(key, ...args) {
|
|
81
|
-
abytes(key);
|
|
82
|
-
if (!isLE) throw new Error("Non little-endian hardware is not yet supported");
|
|
83
|
-
if (params.nonceLength !== void 0) {
|
|
84
|
-
const nonce = args[0];
|
|
85
|
-
if (!nonce) throw new Error("nonce / iv required");
|
|
86
|
-
if (params.varSizeNonce) abytes(nonce);
|
|
87
|
-
else abytes(nonce, params.nonceLength);
|
|
88
|
-
}
|
|
89
|
-
const tagl = params.tagLength;
|
|
90
|
-
if (tagl && args[1] !== void 0) abytes(args[1]);
|
|
91
|
-
const cipher = constructor(key, ...args);
|
|
92
|
-
const checkOutput = (fnLength, output) => {
|
|
93
|
-
if (output !== void 0) {
|
|
94
|
-
if (fnLength !== 2) throw new Error("cipher output not supported");
|
|
95
|
-
abytes(output);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
let called = false;
|
|
99
|
-
return {
|
|
100
|
-
encrypt(data, output) {
|
|
101
|
-
if (called) throw new Error("cannot encrypt() twice with same key + nonce");
|
|
102
|
-
called = true;
|
|
103
|
-
abytes(data);
|
|
104
|
-
checkOutput(cipher.encrypt.length, output);
|
|
105
|
-
return cipher.encrypt(data, output);
|
|
106
|
-
},
|
|
107
|
-
decrypt(data, output) {
|
|
108
|
-
abytes(data);
|
|
109
|
-
if (tagl && data.length < tagl) throw new Error("invalid ciphertext length: smaller than tagLength=" + tagl);
|
|
110
|
-
checkOutput(cipher.decrypt.length, output);
|
|
111
|
-
return cipher.decrypt(data, output);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
Object.assign(wrappedCipher, params);
|
|
116
|
-
return wrappedCipher;
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* By default, returns u8a of length.
|
|
120
|
-
* When out is available, it checks it for validity and uses it.
|
|
121
|
-
*/
|
|
122
|
-
function getOutput(expectedLength, out, onlyAligned = true) {
|
|
123
|
-
if (out === void 0) return new Uint8Array(expectedLength);
|
|
124
|
-
if (out.length !== expectedLength) throw new Error("invalid output length, expected " + expectedLength + ", got: " + out.length);
|
|
125
|
-
if (onlyAligned && !isAligned32(out)) throw new Error("invalid output, must be aligned");
|
|
126
|
-
return out;
|
|
127
|
-
}
|
|
128
|
-
/** Polyfill for Safari 14. */
|
|
129
|
-
function setBigUint64(view, byteOffset, value, isLE$1) {
|
|
130
|
-
if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE$1);
|
|
131
|
-
const _32n = BigInt(32);
|
|
132
|
-
const _u32_max = BigInt(4294967295);
|
|
133
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
134
|
-
const wl = Number(value & _u32_max);
|
|
135
|
-
const h = isLE$1 ? 4 : 0;
|
|
136
|
-
const l = isLE$1 ? 0 : 4;
|
|
137
|
-
view.setUint32(byteOffset + h, wh, isLE$1);
|
|
138
|
-
view.setUint32(byteOffset + l, wl, isLE$1);
|
|
139
|
-
}
|
|
140
|
-
function u64Lengths(dataLength, aadLength, isLE$1) {
|
|
141
|
-
abool(isLE$1);
|
|
142
|
-
const num = new Uint8Array(16);
|
|
143
|
-
const view = createView(num);
|
|
144
|
-
setBigUint64(view, 0, BigInt(aadLength), isLE$1);
|
|
145
|
-
setBigUint64(view, 8, BigInt(dataLength), isLE$1);
|
|
146
|
-
return num;
|
|
147
|
-
}
|
|
148
|
-
function isAligned32(bytes) {
|
|
149
|
-
return bytes.byteOffset % 4 === 0;
|
|
150
|
-
}
|
|
151
|
-
function copyBytes(bytes) {
|
|
152
|
-
return Uint8Array.from(bytes);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
//#endregion
|
|
156
|
-
export { abytes, aexists, aoutput, clean, copyBytes, createView, equalBytes, getOutput, isAligned32, setBigUint64, toBytes, u32, u64Lengths, u8, wrapCipher };
|
|
@@ -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 };
|