moneysurfer 0.1.0
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 +373 -0
- package/README.md +63 -0
- package/dist/cli.js +15450 -0
- package/dist/i.js +1917 -0
- package/dist/l.js +3 -0
- package/dist/msm-worker.js +33 -0
- package/package.json +40 -0
package/dist/i.js
ADDED
|
@@ -0,0 +1,1917 @@
|
|
|
1
|
+
var platform;
|
|
2
|
+
let bn254_Fp12, bn254_frob, res;
|
|
3
|
+
function utils_isBytes(a) {
|
|
4
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && 'Uint8Array' === a.constructor.name && 'BYTES_PER_ELEMENT' in a && 1 === a.BYTES_PER_ELEMENT;
|
|
5
|
+
}
|
|
6
|
+
function anumber(n, title = '') {
|
|
7
|
+
if ('number' != typeof n) {
|
|
8
|
+
let prefix = title && `"${title}" `;
|
|
9
|
+
throw TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
10
|
+
}
|
|
11
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
12
|
+
let prefix = title && `"${title}" `;
|
|
13
|
+
throw RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function utils_abytes(value, length, title = '') {
|
|
17
|
+
let bytes = utils_isBytes(value), len = value?.length, needsLen = void 0 !== length;
|
|
18
|
+
if (!bytes || needsLen && len !== length) {
|
|
19
|
+
let message = (title && `"${title}" `) + 'expected Uint8Array' + (needsLen ? ` of length ${length}` : '') + ', got ' + (bytes ? `length=${len}` : `type=${typeof value}`);
|
|
20
|
+
if (!bytes) throw TypeError(message);
|
|
21
|
+
throw RangeError(message);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function aexists(instance, checkFinished = !0) {
|
|
26
|
+
if (instance.destroyed) throw Error('Hash instance has been destroyed');
|
|
27
|
+
if (checkFinished && instance.finished) throw Error('Hash#digest() has already been called');
|
|
28
|
+
}
|
|
29
|
+
function aoutput(out, instance) {
|
|
30
|
+
utils_abytes(out, void 0, 'digestInto() output');
|
|
31
|
+
let min = instance.outputLen;
|
|
32
|
+
if (out.length < min) throw RangeError('"digestInto() output" expected to be of length >=' + min);
|
|
33
|
+
}
|
|
34
|
+
function clean(...arrays) {
|
|
35
|
+
for(let i = 0; i < arrays.length; i++)arrays[i].fill(0);
|
|
36
|
+
}
|
|
37
|
+
function createView(arr) {
|
|
38
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
39
|
+
}
|
|
40
|
+
function rotr(word, shift) {
|
|
41
|
+
return word << 32 - shift | word >>> shift;
|
|
42
|
+
}
|
|
43
|
+
function utils_bytesToHex(bytes) {
|
|
44
|
+
if (utils_abytes(bytes), hasHexBuiltin) return bytes.toHex();
|
|
45
|
+
let hex = '';
|
|
46
|
+
for(let i = 0; i < bytes.length; i++)hex += hexes[bytes[i]];
|
|
47
|
+
return hex;
|
|
48
|
+
}
|
|
49
|
+
function asciiToBase16(ch) {
|
|
50
|
+
return ch >= asciis._0 && ch <= asciis._9 ? ch - asciis._0 : ch >= asciis.A && ch <= asciis.F ? ch - (asciis.A - 10) : ch >= asciis.a && ch <= asciis.f ? ch - (asciis.a - 10) : void 0;
|
|
51
|
+
}
|
|
52
|
+
function utils_hexToBytes(hex) {
|
|
53
|
+
if ('string' != typeof hex) throw TypeError('hex string expected, got ' + typeof hex);
|
|
54
|
+
if (hasHexBuiltin) try {
|
|
55
|
+
return Uint8Array.fromHex(hex);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error instanceof SyntaxError) throw RangeError(error.message);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
let hl = hex.length, al = hl / 2;
|
|
61
|
+
if (hl % 2) throw RangeError('hex string expected, got unpadded hex of length ' + hl);
|
|
62
|
+
let array = new Uint8Array(al);
|
|
63
|
+
for(let ai = 0, hi = 0; ai < al; ai++, hi += 2){
|
|
64
|
+
let n1 = asciiToBase16(hex.charCodeAt(hi)), n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
65
|
+
if (void 0 === n1 || void 0 === n2) throw RangeError('hex string expected, got non-hex character "' + (hex[hi] + hex[hi + 1]) + '" at index ' + hi);
|
|
66
|
+
array[ai] = 16 * n1 + n2;
|
|
67
|
+
}
|
|
68
|
+
return array;
|
|
69
|
+
}
|
|
70
|
+
function utils_createHasher(hashCons, info = {}) {
|
|
71
|
+
let hashC = (msg, opts)=>hashCons(opts).update(msg).digest(), tmp = hashCons(void 0);
|
|
72
|
+
return hashC.outputLen = tmp.outputLen, hashC.blockLen = tmp.blockLen, hashC.canXOF = tmp.canXOF, hashC.create = (opts)=>hashCons(opts), Object.assign(hashC, info), Object.freeze(hashC);
|
|
73
|
+
}
|
|
74
|
+
function utils_randomBytes(bytesLength = 32) {
|
|
75
|
+
anumber(bytesLength, 'bytesLength');
|
|
76
|
+
let cr = 'object' == typeof globalThis ? globalThis.crypto : null;
|
|
77
|
+
if ('function' != typeof cr?.getRandomValues) throw Error('crypto.getRandomValues must be defined');
|
|
78
|
+
if (bytesLength > 65536) throw RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
79
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
80
|
+
}
|
|
81
|
+
function utils_abool(value, title = '') {
|
|
82
|
+
if ('boolean' != typeof value) throw TypeError((title && `"${title}" `) + 'expected boolean, got type=' + typeof value);
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
function abignumber(n) {
|
|
86
|
+
if ('bigint' == typeof n) {
|
|
87
|
+
if (!isPosBig(n)) throw RangeError('positive bigint expected, got ' + n);
|
|
88
|
+
} else utils_anumber(n);
|
|
89
|
+
return n;
|
|
90
|
+
}
|
|
91
|
+
function asafenumber(value, title = '') {
|
|
92
|
+
if ('number' != typeof value) throw TypeError((title && `"${title}" `) + 'expected number, got type=' + typeof value);
|
|
93
|
+
if (!Number.isSafeInteger(value)) throw RangeError((title && `"${title}" `) + 'expected safe integer, got ' + value);
|
|
94
|
+
}
|
|
95
|
+
function numberToHexUnpadded(num) {
|
|
96
|
+
let hex = abignumber(num).toString(16);
|
|
97
|
+
return 1 & hex.length ? '0' + hex : hex;
|
|
98
|
+
}
|
|
99
|
+
function hexToNumber(hex) {
|
|
100
|
+
if ('string' != typeof hex) throw TypeError('hex string expected, got ' + typeof hex);
|
|
101
|
+
return '' === hex ? _0n : BigInt('0x' + hex);
|
|
102
|
+
}
|
|
103
|
+
function utils_bytesToNumberBE(bytes) {
|
|
104
|
+
return hexToNumber(utils_bytesToHex(bytes));
|
|
105
|
+
}
|
|
106
|
+
function bytesToNumberLE(bytes) {
|
|
107
|
+
return hexToNumber(utils_bytesToHex(utils_copyBytes(utils_abytes(bytes)).reverse()));
|
|
108
|
+
}
|
|
109
|
+
function numberToBytesBE(n, len) {
|
|
110
|
+
if (anumber(len), 0 === len) throw RangeError('zero length');
|
|
111
|
+
let hex = (n = abignumber(n)).toString(16);
|
|
112
|
+
if (hex.length > 2 * len) throw RangeError('number too large');
|
|
113
|
+
return utils_hexToBytes(hex.padStart(2 * len, '0'));
|
|
114
|
+
}
|
|
115
|
+
function numberToBytesLE(n, len) {
|
|
116
|
+
return numberToBytesBE(n, len).reverse();
|
|
117
|
+
}
|
|
118
|
+
function equalBytes(a, b) {
|
|
119
|
+
if (a = curves_utils_abytes(a), b = curves_utils_abytes(b), a.length !== b.length) return !1;
|
|
120
|
+
let diff = 0;
|
|
121
|
+
for(let i = 0; i < a.length; i++)diff |= a[i] ^ b[i];
|
|
122
|
+
return 0 === diff;
|
|
123
|
+
}
|
|
124
|
+
function utils_copyBytes(bytes) {
|
|
125
|
+
return Uint8Array.from(curves_utils_abytes(bytes));
|
|
126
|
+
}
|
|
127
|
+
function asciiToBytes(ascii) {
|
|
128
|
+
if ('string' != typeof ascii) throw TypeError('ascii string expected, got ' + typeof ascii);
|
|
129
|
+
return Uint8Array.from(ascii, (c, i)=>{
|
|
130
|
+
let charCode = c.charCodeAt(0);
|
|
131
|
+
if (1 !== c.length || charCode > 127) throw RangeError(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
132
|
+
return charCode;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function utils_aInRange(title, n, min, max) {
|
|
136
|
+
if (!(isPosBig(n) && isPosBig(min) && isPosBig(max)) || !(min <= n) || !(n < max)) throw RangeError('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);
|
|
137
|
+
}
|
|
138
|
+
function bitLen(n) {
|
|
139
|
+
let len;
|
|
140
|
+
if (n < _0n) throw Error('expected non-negative bigint, got ' + n);
|
|
141
|
+
for(len = 0; n > _0n; n >>= _1n, len += 1);
|
|
142
|
+
return len;
|
|
143
|
+
}
|
|
144
|
+
function bitGet(n, pos) {
|
|
145
|
+
return n >> BigInt(pos) & _1n;
|
|
146
|
+
}
|
|
147
|
+
function utils_validateObject(object, fields = {}, optFields = {}) {
|
|
148
|
+
if ('[object Object]' !== Object.prototype.toString.call(object)) throw TypeError('expected valid options object');
|
|
149
|
+
let iter = (f, isOpt)=>Object.entries(f).forEach(([k, v])=>(function(fieldName, expectedType, isOpt) {
|
|
150
|
+
if (!isOpt && 'function' !== expectedType && !Object.hasOwn(object, fieldName)) throw TypeError(`param "${fieldName}" is invalid: expected own property`);
|
|
151
|
+
let val = object[fieldName];
|
|
152
|
+
if (isOpt && void 0 === val) return;
|
|
153
|
+
let current = typeof val;
|
|
154
|
+
if (current !== expectedType || null === val) throw TypeError(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
155
|
+
})(k, v, isOpt));
|
|
156
|
+
iter(fields, !1), iter(optFields, !0);
|
|
157
|
+
}
|
|
158
|
+
function mod(a, b) {
|
|
159
|
+
if (b <= modular_0n) throw Error('mod: expected positive modulus, got ' + b);
|
|
160
|
+
let result = a % b;
|
|
161
|
+
return result >= modular_0n ? result : b + result;
|
|
162
|
+
}
|
|
163
|
+
function pow2(x, power, modulo) {
|
|
164
|
+
if (power < modular_0n) throw Error('pow2: expected non-negative exponent, got ' + power);
|
|
165
|
+
let res = x;
|
|
166
|
+
for(; power-- > modular_0n;)res *= res, res %= modulo;
|
|
167
|
+
return res;
|
|
168
|
+
}
|
|
169
|
+
function invert(number, modulo) {
|
|
170
|
+
if (number === modular_0n) throw Error('invert: expected non-zero number');
|
|
171
|
+
if (modulo <= modular_0n) throw Error('invert: expected positive modulus, got ' + modulo);
|
|
172
|
+
let a = mod(number, modulo), b = modulo, x = modular_0n, y = modular_1n, u = modular_1n, v = modular_0n;
|
|
173
|
+
for(; a !== modular_0n;){
|
|
174
|
+
let q = b / a, r = b - a * q, m = x - u * q, n = y - v * q;
|
|
175
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
176
|
+
}
|
|
177
|
+
if (b !== modular_1n) throw Error('invert: does not exist');
|
|
178
|
+
return mod(x, modulo);
|
|
179
|
+
}
|
|
180
|
+
function assertIsSquare(Fp, root, n) {
|
|
181
|
+
if (!Fp.eql(Fp.sqr(root), n)) throw Error('Cannot find square root');
|
|
182
|
+
}
|
|
183
|
+
function sqrt3mod4(Fp, n) {
|
|
184
|
+
let p1div4 = (Fp.ORDER + modular_1n) / _4n, root = Fp.pow(n, p1div4);
|
|
185
|
+
return assertIsSquare(Fp, root, n), root;
|
|
186
|
+
}
|
|
187
|
+
function sqrt5mod8(Fp, n) {
|
|
188
|
+
let p5div8 = (Fp.ORDER - _5n) / _8n, n2 = Fp.mul(n, _2n), v = Fp.pow(n2, p5div8), nv = Fp.mul(n, v), i = Fp.mul(Fp.mul(nv, _2n), v), root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
189
|
+
return assertIsSquare(Fp, root, n), root;
|
|
190
|
+
}
|
|
191
|
+
function tonelliShanks(P) {
|
|
192
|
+
if (P < _3n) throw Error('sqrt is not defined for small field');
|
|
193
|
+
let Q = P - modular_1n, S = 0;
|
|
194
|
+
for(; Q % _2n === modular_0n;)Q /= _2n, S++;
|
|
195
|
+
let Z = _2n, _Fp = Field(P);
|
|
196
|
+
for(; 1 === FpLegendre(_Fp, Z);)if (Z++ > 1000) throw Error('Cannot find square root: probably non-prime P');
|
|
197
|
+
if (1 === S) return sqrt3mod4;
|
|
198
|
+
let cc = _Fp.pow(Z, Q), Q1div2 = (Q + modular_1n) / _2n;
|
|
199
|
+
return function(Fp, n) {
|
|
200
|
+
if (Fp.is0(n)) return n;
|
|
201
|
+
if (1 !== FpLegendre(Fp, n)) throw Error('Cannot find square root');
|
|
202
|
+
let M = S, c = Fp.mul(Fp.ONE, cc), t = Fp.pow(n, Q), R = Fp.pow(n, Q1div2);
|
|
203
|
+
for(; !Fp.eql(t, Fp.ONE);){
|
|
204
|
+
if (Fp.is0(t)) return Fp.ZERO;
|
|
205
|
+
let i = 1, t_tmp = Fp.sqr(t);
|
|
206
|
+
for(; !Fp.eql(t_tmp, Fp.ONE);)if (i++, t_tmp = Fp.sqr(t_tmp), i === M) throw Error('Cannot find square root');
|
|
207
|
+
let exponent = modular_1n << BigInt(M - i - 1), b = Fp.pow(c, exponent);
|
|
208
|
+
M = i, c = Fp.sqr(b), t = Fp.mul(t, c), R = Fp.mul(R, b);
|
|
209
|
+
}
|
|
210
|
+
return R;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function modular_validateField(field) {
|
|
214
|
+
if (utils_validateObject(field, FIELD_FIELDS.reduce((map, val)=>(map[val] = 'function', map), {
|
|
215
|
+
ORDER: 'bigint',
|
|
216
|
+
BYTES: 'number',
|
|
217
|
+
BITS: 'number'
|
|
218
|
+
})), asafenumber(field.BYTES, 'BYTES'), asafenumber(field.BITS, 'BITS'), field.BYTES < 1 || field.BITS < 1) throw Error('invalid field: expected BYTES/BITS > 0');
|
|
219
|
+
if (field.ORDER <= modular_1n) throw Error('invalid field: expected ORDER > 1, got ' + field.ORDER);
|
|
220
|
+
return field;
|
|
221
|
+
}
|
|
222
|
+
function FpPow(Fp, num, power) {
|
|
223
|
+
if (power < modular_0n) throw Error('invalid exponent, negatives unsupported');
|
|
224
|
+
if (power === modular_0n) return Fp.ONE;
|
|
225
|
+
if (power === modular_1n) return num;
|
|
226
|
+
let p = Fp.ONE, d = num;
|
|
227
|
+
for(; power > modular_0n;)power & modular_1n && (p = Fp.mul(p, d)), d = Fp.sqr(d), power >>= modular_1n;
|
|
228
|
+
return p;
|
|
229
|
+
}
|
|
230
|
+
function modular_FpInvertBatch(Fp, nums, passZero = !1) {
|
|
231
|
+
let inverted = Array(nums.length).fill(passZero ? Fp.ZERO : void 0), multipliedAcc = nums.reduce((acc, num, i)=>Fp.is0(num) ? acc : (inverted[i] = acc, Fp.mul(acc, num)), Fp.ONE), invertedAcc = Fp.inv(multipliedAcc);
|
|
232
|
+
return nums.reduceRight((acc, num, i)=>Fp.is0(num) ? acc : (inverted[i] = Fp.mul(acc, inverted[i]), Fp.mul(acc, num)), invertedAcc), inverted;
|
|
233
|
+
}
|
|
234
|
+
function FpLegendre(Fp, n) {
|
|
235
|
+
let p1mod2 = (Fp.ORDER - modular_1n) / _2n, powered = Fp.pow(n, p1mod2), yes = Fp.eql(powered, Fp.ONE), zero = Fp.eql(powered, Fp.ZERO), no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
236
|
+
if (!yes && !zero && !no) throw Error('invalid Legendre symbol result');
|
|
237
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
238
|
+
}
|
|
239
|
+
function Field(ORDER, opts = {}) {
|
|
240
|
+
return new _Field(ORDER, opts);
|
|
241
|
+
}
|
|
242
|
+
function getFieldBytesLength(fieldOrder) {
|
|
243
|
+
if ('bigint' != typeof fieldOrder) throw Error('field order must be bigint');
|
|
244
|
+
if (fieldOrder <= modular_1n) throw Error('field order must be greater than 1');
|
|
245
|
+
return Math.ceil(bitLen(fieldOrder - modular_1n) / 8);
|
|
246
|
+
}
|
|
247
|
+
function modular_getMinHashLength(fieldOrder) {
|
|
248
|
+
let length = getFieldBytesLength(fieldOrder);
|
|
249
|
+
return length + Math.ceil(length / 2);
|
|
250
|
+
}
|
|
251
|
+
function calcFrobeniusCoefficients(Fp, nonResidue, modulus, degree, num = 1, divisor) {
|
|
252
|
+
if (asafenumber(num, 'num'), num <= 0) throw Error('calcFrobeniusCoefficients: expected positive row count, got ' + num);
|
|
253
|
+
let _divisor = BigInt(void 0 === divisor ? degree : divisor), towerModulus = modulus ** BigInt(degree), res = [];
|
|
254
|
+
for(let i = 0; i < num; i++){
|
|
255
|
+
let a = BigInt(i + 1), powers = [];
|
|
256
|
+
for(let j = 0, qPower = tower_1n; j < degree; j++){
|
|
257
|
+
let numer = a * qPower - a;
|
|
258
|
+
if (numer % _divisor) throw Error('calcFrobeniusCoefficients: inexact tower exponent');
|
|
259
|
+
let power = numer / _divisor % towerModulus;
|
|
260
|
+
powers.push(Fp.pow(nonResidue, power)), qPower *= modulus;
|
|
261
|
+
}
|
|
262
|
+
res.push(powers);
|
|
263
|
+
}
|
|
264
|
+
return res;
|
|
265
|
+
}
|
|
266
|
+
function negateCt(condition, item) {
|
|
267
|
+
let neg = item.negate();
|
|
268
|
+
return condition ? neg : item;
|
|
269
|
+
}
|
|
270
|
+
function normalizeZ(c, points) {
|
|
271
|
+
let invertedZs = modular_FpInvertBatch(c.Fp, points.map((p)=>p.Z));
|
|
272
|
+
return points.map((p, i)=>c.fromAffine(p.toAffine(invertedZs[i])));
|
|
273
|
+
}
|
|
274
|
+
function validateW(W, bits) {
|
|
275
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits) throw Error('invalid window size, expected [1..' + bits + '], got W=' + W);
|
|
276
|
+
}
|
|
277
|
+
function calcWOpts(W, scalarBits) {
|
|
278
|
+
validateW(W, scalarBits);
|
|
279
|
+
let windows = Math.ceil(scalarBits / W) + 1, windowSize = 2 ** (W - 1), maxNumber = 2 ** W;
|
|
280
|
+
return {
|
|
281
|
+
windows,
|
|
282
|
+
windowSize,
|
|
283
|
+
mask: utils_bitMask(W),
|
|
284
|
+
maxNumber,
|
|
285
|
+
shiftBy: BigInt(W)
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function calcOffsets(n, window, wOpts) {
|
|
289
|
+
let { windowSize, mask, maxNumber, shiftBy } = wOpts, wbits = Number(n & mask), nextN = n >> shiftBy;
|
|
290
|
+
wbits > windowSize && (wbits -= maxNumber, nextN += curve_1n);
|
|
291
|
+
let offsetStart = window * windowSize, offset = offsetStart + Math.abs(wbits) - 1, isZero = 0 === wbits;
|
|
292
|
+
return {
|
|
293
|
+
nextN,
|
|
294
|
+
offset,
|
|
295
|
+
isZero,
|
|
296
|
+
isNeg: wbits < 0,
|
|
297
|
+
isNegF: window % 2 != 0,
|
|
298
|
+
offsetF: offsetStart
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function getW(P) {
|
|
302
|
+
return pointWindowSizes.get(P) || 1;
|
|
303
|
+
}
|
|
304
|
+
function assert0(n) {
|
|
305
|
+
if (n !== curve_0n) throw Error('invalid wNAF');
|
|
306
|
+
}
|
|
307
|
+
function pippenger(c, points, scalars) {
|
|
308
|
+
let fieldN = c.Fn;
|
|
309
|
+
if (!Array.isArray(points)) throw Error('array expected');
|
|
310
|
+
points.forEach((p, i)=>{
|
|
311
|
+
if (!(p instanceof c)) throw Error('invalid point at index ' + i);
|
|
312
|
+
});
|
|
313
|
+
if (!Array.isArray(scalars)) throw Error('array of scalars expected');
|
|
314
|
+
scalars.forEach((s, i)=>{
|
|
315
|
+
if (!fieldN.isValid(s)) throw Error('invalid scalar at index ' + i);
|
|
316
|
+
});
|
|
317
|
+
let plength = points.length, slength = scalars.length;
|
|
318
|
+
if (plength !== slength) throw Error('arrays of points and scalars must have equal length');
|
|
319
|
+
let zero = c.ZERO, wbits = bitLen(BigInt(plength)), windowSize = 1;
|
|
320
|
+
wbits > 12 ? windowSize = wbits - 3 : wbits > 4 ? windowSize = wbits - 2 : wbits > 0 && (windowSize = 2);
|
|
321
|
+
let MASK = utils_bitMask(windowSize), buckets = Array(Number(MASK) + 1).fill(zero), lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize, sum = zero;
|
|
322
|
+
for(let i = lastBits; i >= 0; i -= windowSize){
|
|
323
|
+
buckets.fill(zero);
|
|
324
|
+
for(let j = 0; j < slength; j++){
|
|
325
|
+
let wbits = Number(scalars[j] >> BigInt(i) & MASK);
|
|
326
|
+
buckets[wbits] = buckets[wbits].add(points[j]);
|
|
327
|
+
}
|
|
328
|
+
let resI = zero;
|
|
329
|
+
for(let j = buckets.length - 1, sumI = zero; j > 0; j--)sumI = sumI.add(buckets[j]), resI = resI.add(sumI);
|
|
330
|
+
if (sum = sum.add(resI), 0 !== i) for(let j = 0; j < windowSize; j++)sum = sum.double();
|
|
331
|
+
}
|
|
332
|
+
return sum;
|
|
333
|
+
}
|
|
334
|
+
function createField(order, field, isLE) {
|
|
335
|
+
if (!field) return Field(order, {
|
|
336
|
+
isLE
|
|
337
|
+
});
|
|
338
|
+
if (field.ORDER !== order) throw Error('Field.ORDER must match order: Fp == p, Fn == n');
|
|
339
|
+
return modular_validateField(field), field;
|
|
340
|
+
}
|
|
341
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
342
|
+
if (void 0 === FpFnLE && (FpFnLE = 'edwards' === type), !CURVE || 'object' != typeof CURVE) throw Error(`expected valid ${type} CURVE object`);
|
|
343
|
+
for (let p of [
|
|
344
|
+
'p',
|
|
345
|
+
'n',
|
|
346
|
+
'h'
|
|
347
|
+
]){
|
|
348
|
+
let val = CURVE[p];
|
|
349
|
+
if (!('bigint' == typeof val && val > curve_0n)) throw Error(`CURVE.${p} must be positive bigint`);
|
|
350
|
+
}
|
|
351
|
+
let Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE), Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
352
|
+
for (let p of [
|
|
353
|
+
'Gx',
|
|
354
|
+
'Gy',
|
|
355
|
+
'a',
|
|
356
|
+
'weierstrass' === type ? 'b' : 'd'
|
|
357
|
+
])if (!Fp.isValid(CURVE[p])) throw Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
358
|
+
return {
|
|
359
|
+
CURVE: CURVE = Object.freeze(Object.assign({}, CURVE)),
|
|
360
|
+
Fp,
|
|
361
|
+
Fn
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function curve_createKeygen(randomSecretKey, getPublicKey) {
|
|
365
|
+
return function(seed) {
|
|
366
|
+
let secretKey = randomSecretKey(seed);
|
|
367
|
+
return {
|
|
368
|
+
secretKey,
|
|
369
|
+
publicKey: getPublicKey(secretKey)
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function weierstrass(params, extraOpts = {}) {
|
|
374
|
+
var Fp, Fn;
|
|
375
|
+
function assertCompressionIsSupported() {
|
|
376
|
+
if (!Fp1.isOdd) throw Error('compression is not supported: Field does not have .isOdd()');
|
|
377
|
+
}
|
|
378
|
+
function weierstrassEquation(x) {
|
|
379
|
+
let x2 = Fp1.sqr(x), x3 = Fp1.mul(x2, x);
|
|
380
|
+
return Fp1.add(Fp1.add(x3, Fp1.mul(x, CURVE.a)), CURVE.b);
|
|
381
|
+
}
|
|
382
|
+
function isValidXY(x, y) {
|
|
383
|
+
let left = Fp1.sqr(y), right = weierstrassEquation(x);
|
|
384
|
+
return Fp1.eql(left, right);
|
|
385
|
+
}
|
|
386
|
+
function acoord(title, n, banZero = !1) {
|
|
387
|
+
if (!Fp1.isValid(n) || banZero && Fp1.is0(n)) throw Error(`bad point coordinate ${title}`);
|
|
388
|
+
return n;
|
|
389
|
+
}
|
|
390
|
+
function aprjpoint(other) {
|
|
391
|
+
if (!(other instanceof Point)) throw Error('Weierstrass Point expected');
|
|
392
|
+
}
|
|
393
|
+
function splitEndoScalarN(k) {
|
|
394
|
+
if (!endo || !endo.basises) throw Error('no endo');
|
|
395
|
+
return function(k, basis, n) {
|
|
396
|
+
utils_aInRange('scalar', k, weierstrass_0n, n);
|
|
397
|
+
let [[a1, b1], [a2, b2]] = basis, c1 = divNearest(b2 * k, n), c2 = divNearest(-b1 * k, n), k1 = k - c1 * a1 - c2 * a2, k2 = -c1 * b1 - c2 * b2, k1neg = k1 < weierstrass_0n, k2neg = k2 < weierstrass_0n;
|
|
398
|
+
k1neg && (k1 = -k1), k2neg && (k2 = -k2);
|
|
399
|
+
let MAX_NUM = utils_bitMask(Math.ceil(bitLen(n) / 2)) + weierstrass_1n;
|
|
400
|
+
if (k1 < weierstrass_0n || k1 >= MAX_NUM || k2 < weierstrass_0n || k2 >= MAX_NUM) throw Error('splitScalar (endomorphism): failed for k');
|
|
401
|
+
return {
|
|
402
|
+
k1neg,
|
|
403
|
+
k1,
|
|
404
|
+
k2neg,
|
|
405
|
+
k2
|
|
406
|
+
};
|
|
407
|
+
}(k, endo.basises, Fn1.ORDER);
|
|
408
|
+
}
|
|
409
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
410
|
+
return k2p = new Point(Fp1.mul(k2p.X, endoBeta), k2p.Y, k2p.Z), k1p = negateCt(k1neg, k1p), k2p = negateCt(k2neg, k2p), k1p.add(k2p);
|
|
411
|
+
}
|
|
412
|
+
let validated = createCurveFields('weierstrass', params, extraOpts), Fp1 = validated.Fp, Fn1 = validated.Fn, CURVE = validated.CURVE, { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
413
|
+
utils_validateObject(extraOpts, {}, {
|
|
414
|
+
allowInfinityPoint: 'boolean',
|
|
415
|
+
clearCofactor: 'function',
|
|
416
|
+
isTorsionFree: 'function',
|
|
417
|
+
fromBytes: 'function',
|
|
418
|
+
toBytes: 'function',
|
|
419
|
+
endo: 'object'
|
|
420
|
+
});
|
|
421
|
+
let { endo, allowInfinityPoint } = extraOpts;
|
|
422
|
+
if (endo && (!Fp1.is0(CURVE.a) || 'bigint' != typeof endo.beta || !Array.isArray(endo.basises))) throw Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
423
|
+
let lengths = (Fp = Fp1, {
|
|
424
|
+
secretKey: (Fn = Fn1).BYTES,
|
|
425
|
+
publicKey: 1 + Fp.BYTES,
|
|
426
|
+
publicKeyUncompressed: 1 + 2 * Fp.BYTES,
|
|
427
|
+
publicKeyHasPrefix: !0,
|
|
428
|
+
signature: 2 * Fn.BYTES
|
|
429
|
+
}), encodePoint = void 0 === extraOpts.toBytes ? function(_c, point, isCompressed) {
|
|
430
|
+
var hasEvenY;
|
|
431
|
+
if (allowInfinityPoint && point.is0()) return Uint8Array.of(0);
|
|
432
|
+
let { x, y } = point.toAffine(), bx = Fp1.toBytes(x);
|
|
433
|
+
return (utils_abool(isCompressed, 'isCompressed'), isCompressed) ? (assertCompressionIsSupported(), curves_utils_concatBytes((hasEvenY = !Fp1.isOdd(y), Uint8Array.of(hasEvenY ? 0x02 : 0x03)), bx)) : curves_utils_concatBytes(Uint8Array.of(0x04), bx, Fp1.toBytes(y));
|
|
434
|
+
} : extraOpts.toBytes, decodePoint = void 0 === extraOpts.fromBytes ? function(bytes) {
|
|
435
|
+
curves_utils_abytes(bytes, void 0, 'Point');
|
|
436
|
+
let { publicKey: comp, publicKeyUncompressed: uncomp } = lengths, length = bytes.length, head = bytes[0], tail = bytes.subarray(1);
|
|
437
|
+
if (allowInfinityPoint && 1 === length && 0x00 === head) return {
|
|
438
|
+
x: Fp1.ZERO,
|
|
439
|
+
y: Fp1.ZERO
|
|
440
|
+
};
|
|
441
|
+
if (length === comp && (0x02 === head || 0x03 === head)) {
|
|
442
|
+
let y, x = Fp1.fromBytes(tail);
|
|
443
|
+
if (!Fp1.isValid(x)) throw Error('bad point: is not on curve, wrong x');
|
|
444
|
+
let y2 = weierstrassEquation(x);
|
|
445
|
+
try {
|
|
446
|
+
y = Fp1.sqrt(y2);
|
|
447
|
+
} catch (sqrtError) {
|
|
448
|
+
throw Error('bad point: is not on curve, sqrt error' + (sqrtError instanceof Error ? ': ' + sqrtError.message : ''));
|
|
449
|
+
}
|
|
450
|
+
return assertCompressionIsSupported(), (1 & head) == 1 !== Fp1.isOdd(y) && (y = Fp1.neg(y)), {
|
|
451
|
+
x,
|
|
452
|
+
y
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (length === uncomp && 0x04 === head) {
|
|
456
|
+
let L = Fp1.BYTES, x = Fp1.fromBytes(tail.subarray(0, L)), y = Fp1.fromBytes(tail.subarray(L, 2 * L));
|
|
457
|
+
if (!isValidXY(x, y)) throw Error('bad point: is not on curve');
|
|
458
|
+
return {
|
|
459
|
+
x,
|
|
460
|
+
y
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
throw Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
464
|
+
} : extraOpts.fromBytes;
|
|
465
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy)) throw Error('bad curve params: generator point');
|
|
466
|
+
let _4a3 = Fp1.mul(Fp1.pow(CURVE.a, weierstrass_3n), weierstrass_4n), _27b2 = Fp1.mul(Fp1.sqr(CURVE.b), BigInt(27));
|
|
467
|
+
if (Fp1.is0(Fp1.add(_4a3, _27b2))) throw Error('bad curve params: a or b');
|
|
468
|
+
class Point {
|
|
469
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp1.ONE);
|
|
470
|
+
static ZERO = new Point(Fp1.ZERO, Fp1.ONE, Fp1.ZERO);
|
|
471
|
+
static Fp = Fp1;
|
|
472
|
+
static Fn = Fn1;
|
|
473
|
+
X;
|
|
474
|
+
Y;
|
|
475
|
+
Z;
|
|
476
|
+
constructor(X, Y, Z){
|
|
477
|
+
this.X = acoord('x', X), this.Y = acoord('y', Y, !0), this.Z = acoord('z', Z), Object.freeze(this);
|
|
478
|
+
}
|
|
479
|
+
static CURVE() {
|
|
480
|
+
return CURVE;
|
|
481
|
+
}
|
|
482
|
+
static fromAffine(p) {
|
|
483
|
+
let { x, y } = p || {};
|
|
484
|
+
if (!p || !Fp1.isValid(x) || !Fp1.isValid(y)) throw Error('invalid affine point');
|
|
485
|
+
if (p instanceof Point) throw Error('projective point not allowed');
|
|
486
|
+
return Fp1.is0(x) && Fp1.is0(y) ? Point.ZERO : new Point(x, y, Fp1.ONE);
|
|
487
|
+
}
|
|
488
|
+
static fromBytes(bytes) {
|
|
489
|
+
let P = Point.fromAffine(decodePoint(curves_utils_abytes(bytes, void 0, 'point')));
|
|
490
|
+
return P.assertValidity(), P;
|
|
491
|
+
}
|
|
492
|
+
static fromHex(hex) {
|
|
493
|
+
return Point.fromBytes(curves_utils_hexToBytes(hex));
|
|
494
|
+
}
|
|
495
|
+
get x() {
|
|
496
|
+
return this.toAffine().x;
|
|
497
|
+
}
|
|
498
|
+
get y() {
|
|
499
|
+
return this.toAffine().y;
|
|
500
|
+
}
|
|
501
|
+
precompute(windowSize = 8, isLazy = !0) {
|
|
502
|
+
return wnaf.createCache(this, windowSize), isLazy || this.multiply(weierstrass_3n), this;
|
|
503
|
+
}
|
|
504
|
+
assertValidity() {
|
|
505
|
+
if (this.is0()) {
|
|
506
|
+
if (extraOpts.allowInfinityPoint && Fp1.is0(this.X) && Fp1.eql(this.Y, Fp1.ONE) && Fp1.is0(this.Z)) return;
|
|
507
|
+
throw Error('bad point: ZERO');
|
|
508
|
+
}
|
|
509
|
+
let { x, y } = this.toAffine();
|
|
510
|
+
if (!Fp1.isValid(x) || !Fp1.isValid(y)) throw Error('bad point: x or y not field elements');
|
|
511
|
+
if (!isValidXY(x, y)) throw Error('bad point: equation left != right');
|
|
512
|
+
if (!this.isTorsionFree()) throw Error('bad point: not in prime-order subgroup');
|
|
513
|
+
}
|
|
514
|
+
hasEvenY() {
|
|
515
|
+
let { y } = this.toAffine();
|
|
516
|
+
if (!Fp1.isOdd) throw Error("Field doesn't support isOdd");
|
|
517
|
+
return !Fp1.isOdd(y);
|
|
518
|
+
}
|
|
519
|
+
equals(other) {
|
|
520
|
+
aprjpoint(other);
|
|
521
|
+
let { X: X1, Y: Y1, Z: Z1 } = this, { X: X2, Y: Y2, Z: Z2 } = other, U1 = Fp1.eql(Fp1.mul(X1, Z2), Fp1.mul(X2, Z1)), U2 = Fp1.eql(Fp1.mul(Y1, Z2), Fp1.mul(Y2, Z1));
|
|
522
|
+
return U1 && U2;
|
|
523
|
+
}
|
|
524
|
+
negate() {
|
|
525
|
+
return new Point(this.X, Fp1.neg(this.Y), this.Z);
|
|
526
|
+
}
|
|
527
|
+
double() {
|
|
528
|
+
let { a, b } = CURVE, b3 = Fp1.mul(b, weierstrass_3n), { X: X1, Y: Y1, Z: Z1 } = this, X3 = Fp1.ZERO, Y3 = Fp1.ZERO, Z3 = Fp1.ZERO, t0 = Fp1.mul(X1, X1), t1 = Fp1.mul(Y1, Y1), t2 = Fp1.mul(Z1, Z1), t3 = Fp1.mul(X1, Y1);
|
|
529
|
+
return t3 = Fp1.add(t3, t3), Z3 = Fp1.mul(X1, Z1), Z3 = Fp1.add(Z3, Z3), X3 = Fp1.mul(a, Z3), Y3 = Fp1.mul(b3, t2), Y3 = Fp1.add(X3, Y3), X3 = Fp1.sub(t1, Y3), Y3 = Fp1.add(t1, Y3), Y3 = Fp1.mul(X3, Y3), X3 = Fp1.mul(t3, X3), Z3 = Fp1.mul(b3, Z3), t2 = Fp1.mul(a, t2), t3 = Fp1.sub(t0, t2), t3 = Fp1.mul(a, t3), t3 = Fp1.add(t3, Z3), Z3 = Fp1.add(t0, t0), t0 = Fp1.add(Z3, t0), t0 = Fp1.add(t0, t2), t0 = Fp1.mul(t0, t3), Y3 = Fp1.add(Y3, t0), t2 = Fp1.mul(Y1, Z1), t2 = Fp1.add(t2, t2), t0 = Fp1.mul(t2, t3), X3 = Fp1.sub(X3, t0), Z3 = Fp1.mul(t2, t1), Z3 = Fp1.add(Z3, Z3), new Point(X3, Y3, Z3 = Fp1.add(Z3, Z3));
|
|
530
|
+
}
|
|
531
|
+
add(other) {
|
|
532
|
+
aprjpoint(other);
|
|
533
|
+
let { X: X1, Y: Y1, Z: Z1 } = this, { X: X2, Y: Y2, Z: Z2 } = other, X3 = Fp1.ZERO, Y3 = Fp1.ZERO, Z3 = Fp1.ZERO, a = CURVE.a, b3 = Fp1.mul(CURVE.b, weierstrass_3n), t0 = Fp1.mul(X1, X2), t1 = Fp1.mul(Y1, Y2), t2 = Fp1.mul(Z1, Z2), t3 = Fp1.add(X1, Y1), t4 = Fp1.add(X2, Y2);
|
|
534
|
+
t3 = Fp1.mul(t3, t4), t4 = Fp1.add(t0, t1), t3 = Fp1.sub(t3, t4), t4 = Fp1.add(X1, Z1);
|
|
535
|
+
let t5 = Fp1.add(X2, Z2);
|
|
536
|
+
return t4 = Fp1.mul(t4, t5), t5 = Fp1.add(t0, t2), t4 = Fp1.sub(t4, t5), t5 = Fp1.add(Y1, Z1), X3 = Fp1.add(Y2, Z2), t5 = Fp1.mul(t5, X3), X3 = Fp1.add(t1, t2), t5 = Fp1.sub(t5, X3), Z3 = Fp1.mul(a, t4), X3 = Fp1.mul(b3, t2), Z3 = Fp1.add(X3, Z3), X3 = Fp1.sub(t1, Z3), Z3 = Fp1.add(t1, Z3), Y3 = Fp1.mul(X3, Z3), t1 = Fp1.add(t0, t0), t1 = Fp1.add(t1, t0), t2 = Fp1.mul(a, t2), t4 = Fp1.mul(b3, t4), t1 = Fp1.add(t1, t2), t2 = Fp1.sub(t0, t2), t2 = Fp1.mul(a, t2), t4 = Fp1.add(t4, t2), t0 = Fp1.mul(t1, t4), Y3 = Fp1.add(Y3, t0), t0 = Fp1.mul(t5, t4), X3 = Fp1.mul(t3, X3), X3 = Fp1.sub(X3, t0), t0 = Fp1.mul(t3, t1), Z3 = Fp1.mul(t5, Z3), new Point(X3, Y3, Z3 = Fp1.add(Z3, t0));
|
|
537
|
+
}
|
|
538
|
+
subtract(other) {
|
|
539
|
+
return aprjpoint(other), this.add(other.negate());
|
|
540
|
+
}
|
|
541
|
+
is0() {
|
|
542
|
+
return this.equals(Point.ZERO);
|
|
543
|
+
}
|
|
544
|
+
multiply(scalar) {
|
|
545
|
+
let point, fake, { endo } = extraOpts;
|
|
546
|
+
if (!Fn1.isValidNot0(scalar)) throw RangeError('invalid scalar: out of range');
|
|
547
|
+
let mul = (n)=>wnaf.cached(this, n, (p)=>normalizeZ(Point, p));
|
|
548
|
+
if (endo) {
|
|
549
|
+
let { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar), { p: k1p, f: k1f } = mul(k1), { p: k2p, f: k2f } = mul(k2);
|
|
550
|
+
fake = k1f.add(k2f), point = finishEndo(endo.beta, k1p, k2p, k1neg, k2neg);
|
|
551
|
+
} else {
|
|
552
|
+
let { p, f } = mul(scalar);
|
|
553
|
+
point = p, fake = f;
|
|
554
|
+
}
|
|
555
|
+
return normalizeZ(Point, [
|
|
556
|
+
point,
|
|
557
|
+
fake
|
|
558
|
+
])[0];
|
|
559
|
+
}
|
|
560
|
+
multiplyUnsafe(scalar) {
|
|
561
|
+
let { endo } = extraOpts;
|
|
562
|
+
if (!Fn1.isValid(scalar)) throw RangeError('invalid scalar: out of range');
|
|
563
|
+
if (scalar === weierstrass_0n || this.is0()) return Point.ZERO;
|
|
564
|
+
if (scalar === weierstrass_1n) return this;
|
|
565
|
+
if (wnaf.hasCache(this)) return this.multiply(scalar);
|
|
566
|
+
if (!endo) return wnaf.unsafe(this, scalar);
|
|
567
|
+
{
|
|
568
|
+
let { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar), { p1, p2 } = function(Point, point, k1, k2) {
|
|
569
|
+
let acc = point, p1 = Point.ZERO, p2 = Point.ZERO;
|
|
570
|
+
for(; k1 > curve_0n || k2 > curve_0n;)k1 & curve_1n && (p1 = p1.add(acc)), k2 & curve_1n && (p2 = p2.add(acc)), acc = acc.double(), k1 >>= curve_1n, k2 >>= curve_1n;
|
|
571
|
+
return {
|
|
572
|
+
p1,
|
|
573
|
+
p2
|
|
574
|
+
};
|
|
575
|
+
}(Point, this, k1, k2);
|
|
576
|
+
return finishEndo(endo.beta, p1, p2, k1neg, k2neg);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
toAffine(invertedZ) {
|
|
580
|
+
let iz = invertedZ, { X, Y, Z } = this;
|
|
581
|
+
if (Fp1.eql(Z, Fp1.ONE)) return {
|
|
582
|
+
x: X,
|
|
583
|
+
y: Y
|
|
584
|
+
};
|
|
585
|
+
let is0 = this.is0();
|
|
586
|
+
null == iz && (iz = is0 ? Fp1.ONE : Fp1.inv(Z));
|
|
587
|
+
let x = Fp1.mul(X, iz), y = Fp1.mul(Y, iz), zz = Fp1.mul(Z, iz);
|
|
588
|
+
if (is0) return {
|
|
589
|
+
x: Fp1.ZERO,
|
|
590
|
+
y: Fp1.ZERO
|
|
591
|
+
};
|
|
592
|
+
if (!Fp1.eql(zz, Fp1.ONE)) throw Error('invZ was invalid');
|
|
593
|
+
return {
|
|
594
|
+
x,
|
|
595
|
+
y
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
isTorsionFree() {
|
|
599
|
+
let { isTorsionFree } = extraOpts;
|
|
600
|
+
return cofactor === weierstrass_1n || (isTorsionFree ? isTorsionFree(Point, this) : wnaf.unsafe(this, CURVE_ORDER).is0());
|
|
601
|
+
}
|
|
602
|
+
clearCofactor() {
|
|
603
|
+
let { clearCofactor } = extraOpts;
|
|
604
|
+
return cofactor === weierstrass_1n ? this : clearCofactor ? clearCofactor(Point, this) : this.multiplyUnsafe(cofactor);
|
|
605
|
+
}
|
|
606
|
+
isSmallOrder() {
|
|
607
|
+
return cofactor === weierstrass_1n ? this.is0() : this.clearCofactor().is0();
|
|
608
|
+
}
|
|
609
|
+
toBytes(isCompressed = !0) {
|
|
610
|
+
return utils_abool(isCompressed, 'isCompressed'), this.assertValidity(), encodePoint(Point, this, isCompressed);
|
|
611
|
+
}
|
|
612
|
+
toHex(isCompressed = !0) {
|
|
613
|
+
return curves_utils_bytesToHex(this.toBytes(isCompressed));
|
|
614
|
+
}
|
|
615
|
+
toString() {
|
|
616
|
+
return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
let bits = Fn1.BITS, wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
|
|
620
|
+
return bits >= 8 && Point.BASE.precompute(8), Object.freeze(Point.prototype), Object.freeze(Point), Point;
|
|
621
|
+
}
|
|
622
|
+
import { cpus as external_node_os_cpus } from "node:os";
|
|
623
|
+
import { __webpack_require__ } from "./l.js";
|
|
624
|
+
import * as __rspack_external_node_worker_threads_28ca7740 from "node:worker_threads";
|
|
625
|
+
let hasHexBuiltin = 'function' == typeof Uint8Array.from([]).toHex && 'function' == typeof Uint8Array.fromHex, hexes = Array.from({
|
|
626
|
+
length: 256
|
|
627
|
+
}, (_, i)=>i.toString(16).padStart(2, '0')), asciis = {
|
|
628
|
+
_0: 48,
|
|
629
|
+
_9: 57,
|
|
630
|
+
A: 65,
|
|
631
|
+
F: 70,
|
|
632
|
+
a: 97,
|
|
633
|
+
f: 102
|
|
634
|
+
}, oidNist = (suffix)=>({
|
|
635
|
+
oid: Uint8Array.from([
|
|
636
|
+
0x06,
|
|
637
|
+
0x09,
|
|
638
|
+
0x60,
|
|
639
|
+
0x86,
|
|
640
|
+
0x48,
|
|
641
|
+
0x01,
|
|
642
|
+
0x65,
|
|
643
|
+
0x03,
|
|
644
|
+
0x04,
|
|
645
|
+
0x02,
|
|
646
|
+
suffix
|
|
647
|
+
])
|
|
648
|
+
}), curves_utils_abytes = (value, length, title)=>utils_abytes(value, length, title), utils_anumber = anumber, curves_utils_bytesToHex = utils_bytesToHex, curves_utils_concatBytes = (...arrays)=>(function(...arrays) {
|
|
649
|
+
let sum = 0;
|
|
650
|
+
for(let i = 0; i < arrays.length; i++){
|
|
651
|
+
let a = arrays[i];
|
|
652
|
+
utils_abytes(a), sum += a.length;
|
|
653
|
+
}
|
|
654
|
+
let res = new Uint8Array(sum);
|
|
655
|
+
for(let i = 0, pad = 0; i < arrays.length; i++){
|
|
656
|
+
let a = arrays[i];
|
|
657
|
+
res.set(a, pad), pad += a.length;
|
|
658
|
+
}
|
|
659
|
+
return res;
|
|
660
|
+
})(...arrays), curves_utils_hexToBytes = (hex)=>utils_hexToBytes(hex), curves_utils_isBytes = "l" == __webpack_require__.j ? utils_isBytes : null, curves_utils_randomBytes = (bytesLength)=>utils_randomBytes(bytesLength), _0n = BigInt(0), _1n = BigInt(1), isPosBig = (n)=>'bigint' == typeof n && _0n <= n, utils_bitMask = (n)=>(_1n << BigInt(n)) - _1n, utils_notImplemented = ()=>{
|
|
661
|
+
throw Error('not implemented');
|
|
662
|
+
}, modular_0n = BigInt(0), modular_1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4), _5n = BigInt(5), _7n = BigInt(7), _8n = BigInt(8), _9n = BigInt(9), _16n = BigInt(16), isNegativeLE = (num, modulo)=>(mod(num, modulo) & modular_1n) === modular_1n, FIELD_FIELDS = [
|
|
663
|
+
'create',
|
|
664
|
+
'isValid',
|
|
665
|
+
'is0',
|
|
666
|
+
'neg',
|
|
667
|
+
'inv',
|
|
668
|
+
'sqrt',
|
|
669
|
+
'sqr',
|
|
670
|
+
'eql',
|
|
671
|
+
'add',
|
|
672
|
+
'sub',
|
|
673
|
+
'mul',
|
|
674
|
+
'pow',
|
|
675
|
+
'div',
|
|
676
|
+
'addN',
|
|
677
|
+
'subN',
|
|
678
|
+
'mulN',
|
|
679
|
+
'sqrN'
|
|
680
|
+
], FIELD_SQRT = new WeakMap();
|
|
681
|
+
class _Field {
|
|
682
|
+
ORDER;
|
|
683
|
+
BITS;
|
|
684
|
+
BYTES;
|
|
685
|
+
isLE;
|
|
686
|
+
ZERO = modular_0n;
|
|
687
|
+
ONE = modular_1n;
|
|
688
|
+
_lengths;
|
|
689
|
+
_mod;
|
|
690
|
+
constructor(ORDER, opts = {}){
|
|
691
|
+
let _nbitLength;
|
|
692
|
+
if (ORDER <= modular_1n) throw Error('invalid field: expected ORDER > 1, got ' + ORDER);
|
|
693
|
+
this.isLE = !1, null != opts && 'object' == typeof opts && ('number' == typeof opts.BITS && (_nbitLength = opts.BITS), 'function' == typeof opts.sqrt && Object.defineProperty(this, 'sqrt', {
|
|
694
|
+
value: opts.sqrt,
|
|
695
|
+
enumerable: !0
|
|
696
|
+
}), 'boolean' == typeof opts.isLE && (this.isLE = opts.isLE), opts.allowedLengths && (this._lengths = Object.freeze(opts.allowedLengths.slice())), 'boolean' == typeof opts.modFromBytes && (this._mod = opts.modFromBytes));
|
|
697
|
+
let { nBitLength, nByteLength } = function(n, nBitLength) {
|
|
698
|
+
if (void 0 !== nBitLength && utils_anumber(nBitLength), n <= modular_0n) throw Error('invalid n length: expected positive n, got ' + n);
|
|
699
|
+
if (void 0 !== nBitLength && nBitLength < 1) throw Error('invalid n length: expected positive bit length, got ' + nBitLength);
|
|
700
|
+
let bits = bitLen(n);
|
|
701
|
+
if (void 0 !== nBitLength && nBitLength < bits) throw Error(`invalid n length: expected bit length (${bits}) >= n.length (${nBitLength})`);
|
|
702
|
+
let _nBitLength = void 0 !== nBitLength ? nBitLength : bits, nByteLength = Math.ceil(_nBitLength / 8);
|
|
703
|
+
return {
|
|
704
|
+
nBitLength: _nBitLength,
|
|
705
|
+
nByteLength
|
|
706
|
+
};
|
|
707
|
+
}(ORDER, _nbitLength);
|
|
708
|
+
if (nByteLength > 2048) throw Error('invalid field: expected ORDER of <= 2048 bytes');
|
|
709
|
+
this.ORDER = ORDER, this.BITS = nBitLength, this.BYTES = nByteLength, Object.freeze(this);
|
|
710
|
+
}
|
|
711
|
+
create(num) {
|
|
712
|
+
return mod(num, this.ORDER);
|
|
713
|
+
}
|
|
714
|
+
isValid(num) {
|
|
715
|
+
if ('bigint' != typeof num) throw TypeError('invalid field element: expected bigint, got ' + typeof num);
|
|
716
|
+
return modular_0n <= num && num < this.ORDER;
|
|
717
|
+
}
|
|
718
|
+
is0(num) {
|
|
719
|
+
return num === modular_0n;
|
|
720
|
+
}
|
|
721
|
+
isValidNot0(num) {
|
|
722
|
+
return !this.is0(num) && this.isValid(num);
|
|
723
|
+
}
|
|
724
|
+
isOdd(num) {
|
|
725
|
+
return (num & modular_1n) === modular_1n;
|
|
726
|
+
}
|
|
727
|
+
neg(num) {
|
|
728
|
+
return mod(-num, this.ORDER);
|
|
729
|
+
}
|
|
730
|
+
eql(lhs, rhs) {
|
|
731
|
+
return lhs === rhs;
|
|
732
|
+
}
|
|
733
|
+
sqr(num) {
|
|
734
|
+
return mod(num * num, this.ORDER);
|
|
735
|
+
}
|
|
736
|
+
add(lhs, rhs) {
|
|
737
|
+
return mod(lhs + rhs, this.ORDER);
|
|
738
|
+
}
|
|
739
|
+
sub(lhs, rhs) {
|
|
740
|
+
return mod(lhs - rhs, this.ORDER);
|
|
741
|
+
}
|
|
742
|
+
mul(lhs, rhs) {
|
|
743
|
+
return mod(lhs * rhs, this.ORDER);
|
|
744
|
+
}
|
|
745
|
+
pow(num, power) {
|
|
746
|
+
return FpPow(this, num, power);
|
|
747
|
+
}
|
|
748
|
+
div(lhs, rhs) {
|
|
749
|
+
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
750
|
+
}
|
|
751
|
+
sqrN(num) {
|
|
752
|
+
return num * num;
|
|
753
|
+
}
|
|
754
|
+
addN(lhs, rhs) {
|
|
755
|
+
return lhs + rhs;
|
|
756
|
+
}
|
|
757
|
+
subN(lhs, rhs) {
|
|
758
|
+
return lhs - rhs;
|
|
759
|
+
}
|
|
760
|
+
mulN(lhs, rhs) {
|
|
761
|
+
return lhs * rhs;
|
|
762
|
+
}
|
|
763
|
+
inv(num) {
|
|
764
|
+
return invert(num, this.ORDER);
|
|
765
|
+
}
|
|
766
|
+
sqrt(num) {
|
|
767
|
+
let sqrt = FIELD_SQRT.get(this);
|
|
768
|
+
return sqrt || FIELD_SQRT.set(this, sqrt = function(P) {
|
|
769
|
+
if (P % _4n === _3n) return sqrt3mod4;
|
|
770
|
+
if (P % _8n === _5n) return sqrt5mod8;
|
|
771
|
+
if (P % _16n === _9n) {
|
|
772
|
+
let Fp_, tn, c1, c2, c3, c4;
|
|
773
|
+
return Fp_ = Field(P), c1 = (tn = tonelliShanks(P))(Fp_, Fp_.neg(Fp_.ONE)), c2 = tn(Fp_, c1), c3 = tn(Fp_, Fp_.neg(c1)), c4 = (P + _7n) / _16n, (Fp, n)=>{
|
|
774
|
+
let tv1 = Fp.pow(n, c4), tv2 = Fp.mul(tv1, c1), tv3 = Fp.mul(tv1, c2), tv4 = Fp.mul(tv1, c3), e1 = Fp.eql(Fp.sqr(tv2), n), e2 = Fp.eql(Fp.sqr(tv3), n);
|
|
775
|
+
tv1 = Fp.cmov(tv1, tv2, e1), tv2 = Fp.cmov(tv4, tv3, e2);
|
|
776
|
+
let e3 = Fp.eql(Fp.sqr(tv2), n), root = Fp.cmov(tv1, tv2, e3);
|
|
777
|
+
return assertIsSquare(Fp, root, n), root;
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
return tonelliShanks(P);
|
|
781
|
+
}(this.ORDER)), sqrt(this, num);
|
|
782
|
+
}
|
|
783
|
+
toBytes(num) {
|
|
784
|
+
return this.isLE ? numberToBytesLE(num, this.BYTES) : numberToBytesBE(num, this.BYTES);
|
|
785
|
+
}
|
|
786
|
+
fromBytes(bytes, skipValidation = !1) {
|
|
787
|
+
curves_utils_abytes(bytes);
|
|
788
|
+
let { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
789
|
+
if (allowedLengths) {
|
|
790
|
+
if (bytes.length < 1 || !allowedLengths.includes(bytes.length) || bytes.length > BYTES) throw Error('Field.fromBytes: expected ' + allowedLengths + ' bytes, got ' + bytes.length);
|
|
791
|
+
let padded = new Uint8Array(BYTES);
|
|
792
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length), bytes = padded;
|
|
793
|
+
}
|
|
794
|
+
if (bytes.length !== BYTES) throw Error('Field.fromBytes: expected ' + BYTES + ' bytes, got ' + bytes.length);
|
|
795
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : utils_bytesToNumberBE(bytes);
|
|
796
|
+
if (modFromBytes && (scalar = mod(scalar, ORDER)), !skipValidation && !this.isValid(scalar)) throw Error('invalid field element: outside of range 0..ORDER');
|
|
797
|
+
return scalar;
|
|
798
|
+
}
|
|
799
|
+
invertBatch(lst) {
|
|
800
|
+
return modular_FpInvertBatch(this, lst);
|
|
801
|
+
}
|
|
802
|
+
cmov(a, b, condition) {
|
|
803
|
+
return utils_abool(condition, 'condition'), condition ? b : a;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
Object.freeze(_Field.prototype);
|
|
807
|
+
let bls_0n = BigInt(0), bls_1n = BigInt(1), bls_2n = BigInt(2), bls_3n = BigInt(3), tower_0n = BigInt(0), tower_1n = BigInt(1), tower_2n = BigInt(2), tower_3n = BigInt(3), _6n = BigInt(6), _12n = BigInt(12), isObj = (value)=>!!value && 'object' == typeof value;
|
|
808
|
+
class _Field2 {
|
|
809
|
+
ORDER;
|
|
810
|
+
BITS;
|
|
811
|
+
BYTES;
|
|
812
|
+
isLE;
|
|
813
|
+
ZERO;
|
|
814
|
+
ONE;
|
|
815
|
+
Fp;
|
|
816
|
+
NONRESIDUE;
|
|
817
|
+
mulByB;
|
|
818
|
+
Fp_NONRESIDUE;
|
|
819
|
+
Fp_div2;
|
|
820
|
+
FROBENIUS_COEFFICIENTS;
|
|
821
|
+
constructor(Fp, opts = {}){
|
|
822
|
+
let { NONRESIDUE = BigInt(-1), FP2_NONRESIDUE, Fp2mulByB } = opts, ORDER = Fp.ORDER, FP2_ORDER = ORDER * ORDER;
|
|
823
|
+
this.Fp = Fp, this.ORDER = FP2_ORDER, this.BITS = bitLen(FP2_ORDER), this.BYTES = Math.ceil(bitLen(FP2_ORDER) / 8), this.isLE = Fp.isLE, this.ZERO = this.create({
|
|
824
|
+
c0: Fp.ZERO,
|
|
825
|
+
c1: Fp.ZERO
|
|
826
|
+
}), this.ONE = this.create({
|
|
827
|
+
c0: Fp.ONE,
|
|
828
|
+
c1: Fp.ZERO
|
|
829
|
+
}), this.Fp_NONRESIDUE = Fp.create(NONRESIDUE), this.Fp_div2 = Fp.div(Fp.ONE, tower_2n), this.NONRESIDUE = this.create({
|
|
830
|
+
c0: FP2_NONRESIDUE[0],
|
|
831
|
+
c1: FP2_NONRESIDUE[1]
|
|
832
|
+
}), this.FROBENIUS_COEFFICIENTS = Object.freeze(calcFrobeniusCoefficients(Fp, this.Fp_NONRESIDUE, Fp.ORDER, 2)[0]), this.mulByB = (num)=>{
|
|
833
|
+
let { c0, c1 } = Fp2mulByB(num);
|
|
834
|
+
return Object.freeze({
|
|
835
|
+
c0,
|
|
836
|
+
c1
|
|
837
|
+
});
|
|
838
|
+
}, Object.freeze(this);
|
|
839
|
+
}
|
|
840
|
+
fromBigTuple(tuple) {
|
|
841
|
+
if (!Array.isArray(tuple) || 2 !== tuple.length) throw Error('invalid Fp2.fromBigTuple');
|
|
842
|
+
let [c0, c1] = tuple;
|
|
843
|
+
if ('bigint' != typeof c0 || 'bigint' != typeof c1) throw Error('invalid Fp2.fromBigTuple');
|
|
844
|
+
return this.create({
|
|
845
|
+
c0,
|
|
846
|
+
c1
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
create(num) {
|
|
850
|
+
let { Fp } = this;
|
|
851
|
+
return Object.freeze({
|
|
852
|
+
c0: Fp.create(num.c0),
|
|
853
|
+
c1: Fp.create(num.c1)
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
isValid(num) {
|
|
857
|
+
if (!isObj(num)) throw TypeError('invalid field element: expected object, got ' + typeof num);
|
|
858
|
+
let { c0, c1 } = num, { Fp } = this;
|
|
859
|
+
return Fp.isValid(c0) && Fp.isValid(c1);
|
|
860
|
+
}
|
|
861
|
+
is0(num) {
|
|
862
|
+
if (!isObj(num)) return !1;
|
|
863
|
+
let { c0, c1 } = num, { Fp } = this;
|
|
864
|
+
return Fp.is0(c0) && Fp.is0(c1);
|
|
865
|
+
}
|
|
866
|
+
isValidNot0(num) {
|
|
867
|
+
return !this.is0(num) && this.isValid(num);
|
|
868
|
+
}
|
|
869
|
+
eql({ c0, c1 }, { c0: r0, c1: r1 }) {
|
|
870
|
+
let { Fp } = this;
|
|
871
|
+
return Fp.eql(c0, r0) && Fp.eql(c1, r1);
|
|
872
|
+
}
|
|
873
|
+
neg({ c0, c1 }) {
|
|
874
|
+
let { Fp } = this;
|
|
875
|
+
return Object.freeze({
|
|
876
|
+
c0: Fp.neg(c0),
|
|
877
|
+
c1: Fp.neg(c1)
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
pow(num, power) {
|
|
881
|
+
return FpPow(this, num, power);
|
|
882
|
+
}
|
|
883
|
+
invertBatch(nums) {
|
|
884
|
+
return modular_FpInvertBatch(this, nums);
|
|
885
|
+
}
|
|
886
|
+
add(f1, f2) {
|
|
887
|
+
let { Fp } = this, { c0, c1 } = f1, { c0: r0, c1: r1 } = f2;
|
|
888
|
+
return Object.freeze({
|
|
889
|
+
c0: Fp.add(c0, r0),
|
|
890
|
+
c1: Fp.add(c1, r1)
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
sub({ c0, c1 }, { c0: r0, c1: r1 }) {
|
|
894
|
+
let { Fp } = this;
|
|
895
|
+
return Object.freeze({
|
|
896
|
+
c0: Fp.sub(c0, r0),
|
|
897
|
+
c1: Fp.sub(c1, r1)
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
mul({ c0, c1 }, rhs) {
|
|
901
|
+
let { Fp } = this;
|
|
902
|
+
if ('bigint' == typeof rhs) return Object.freeze({
|
|
903
|
+
c0: Fp.mul(c0, rhs),
|
|
904
|
+
c1: Fp.mul(c1, rhs)
|
|
905
|
+
});
|
|
906
|
+
let { c0: r0, c1: r1 } = rhs, t1 = Fp.mul(c0, r0), t2 = Fp.mul(c1, r1);
|
|
907
|
+
return Object.freeze({
|
|
908
|
+
c0: Fp.sub(t1, t2),
|
|
909
|
+
c1: Fp.sub(Fp.mul(Fp.add(c0, c1), Fp.add(r0, r1)), Fp.add(t1, t2))
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
sqr({ c0, c1 }) {
|
|
913
|
+
let { Fp } = this, a = Fp.add(c0, c1), b = Fp.sub(c0, c1), c = Fp.add(c0, c0);
|
|
914
|
+
return Object.freeze({
|
|
915
|
+
c0: Fp.mul(a, b),
|
|
916
|
+
c1: Fp.mul(c, c1)
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
addN(a, b) {
|
|
920
|
+
return this.add(a, b);
|
|
921
|
+
}
|
|
922
|
+
subN(a, b) {
|
|
923
|
+
return this.sub(a, b);
|
|
924
|
+
}
|
|
925
|
+
mulN(a, b) {
|
|
926
|
+
return this.mul(a, b);
|
|
927
|
+
}
|
|
928
|
+
sqrN(a) {
|
|
929
|
+
return this.sqr(a);
|
|
930
|
+
}
|
|
931
|
+
div(lhs, rhs) {
|
|
932
|
+
let { Fp } = this;
|
|
933
|
+
return this.mul(lhs, 'bigint' == typeof rhs ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
|
|
934
|
+
}
|
|
935
|
+
inv({ c0: a, c1: b }) {
|
|
936
|
+
let { Fp } = this, factor = Fp.inv(Fp.create(a * a + b * b));
|
|
937
|
+
return Object.freeze({
|
|
938
|
+
c0: Fp.mul(factor, Fp.create(a)),
|
|
939
|
+
c1: Fp.mul(factor, Fp.create(-b))
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
sqrt(num) {
|
|
943
|
+
let { Fp } = this, { c0, c1 } = num;
|
|
944
|
+
if (Fp.is0(c1)) if (1 === FpLegendre(Fp, c0)) return this.create({
|
|
945
|
+
c0: Fp.sqrt(c0),
|
|
946
|
+
c1: Fp.ZERO
|
|
947
|
+
});
|
|
948
|
+
else return this.create({
|
|
949
|
+
c0: Fp.ZERO,
|
|
950
|
+
c1: Fp.sqrt(Fp.div(c0, this.Fp_NONRESIDUE))
|
|
951
|
+
});
|
|
952
|
+
let a = Fp.sqrt(Fp.sub(Fp.sqr(c0), Fp.mul(Fp.sqr(c1), this.Fp_NONRESIDUE))), d = Fp.mul(Fp.add(a, c0), this.Fp_div2);
|
|
953
|
+
-1 === FpLegendre(Fp, d) && (d = Fp.sub(d, a));
|
|
954
|
+
let a0 = Fp.sqrt(d), candidateSqrt = this.create({
|
|
955
|
+
c0: a0,
|
|
956
|
+
c1: Fp.div(Fp.mul(c1, this.Fp_div2), a0)
|
|
957
|
+
});
|
|
958
|
+
if (!this.eql(this.sqr(candidateSqrt), num)) throw Error('Cannot find square root');
|
|
959
|
+
let x2 = this.neg(candidateSqrt), { re: re1, im: im1 } = this.reim(candidateSqrt), { re: re2, im: im2 } = this.reim(x2);
|
|
960
|
+
return im1 > im2 || im1 === im2 && re1 > re2 ? candidateSqrt : x2;
|
|
961
|
+
}
|
|
962
|
+
isOdd(x) {
|
|
963
|
+
let { re: x0, im: x1 } = this.reim(x), sign_0 = x0 % tower_2n, zero_0 = x0 === tower_0n, sign_1 = x1 % tower_2n;
|
|
964
|
+
return BigInt(sign_0 || zero_0 && sign_1) == tower_1n;
|
|
965
|
+
}
|
|
966
|
+
fromBytes(b) {
|
|
967
|
+
let { Fp } = this;
|
|
968
|
+
if (curves_utils_abytes(b), b.length !== this.BYTES) throw Error('fromBytes invalid length=' + b.length);
|
|
969
|
+
return this.create({
|
|
970
|
+
c0: Fp.fromBytes(b.subarray(0, Fp.BYTES)),
|
|
971
|
+
c1: Fp.fromBytes(b.subarray(Fp.BYTES))
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
toBytes({ c0, c1 }) {
|
|
975
|
+
return curves_utils_concatBytes(this.Fp.toBytes(c0), this.Fp.toBytes(c1));
|
|
976
|
+
}
|
|
977
|
+
cmov({ c0, c1 }, { c0: r0, c1: r1 }, c) {
|
|
978
|
+
let { Fp } = this;
|
|
979
|
+
return this.create({
|
|
980
|
+
c0: Fp.cmov(c0, r0, c),
|
|
981
|
+
c1: Fp.cmov(c1, r1, c)
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
reim({ c0, c1 }) {
|
|
985
|
+
return {
|
|
986
|
+
re: c0,
|
|
987
|
+
im: c1
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
Fp4Square(a, b) {
|
|
991
|
+
let a2 = this.sqr(a), b2 = this.sqr(b);
|
|
992
|
+
return {
|
|
993
|
+
first: this.add(this.mulByNonresidue(b2), a2),
|
|
994
|
+
second: this.sub(this.sub(this.sqr(this.add(a, b)), a2), b2)
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
mulByNonresidue({ c0, c1 }) {
|
|
998
|
+
return this.mul({
|
|
999
|
+
c0,
|
|
1000
|
+
c1
|
|
1001
|
+
}, this.NONRESIDUE);
|
|
1002
|
+
}
|
|
1003
|
+
frobeniusMap({ c0, c1 }, power) {
|
|
1004
|
+
return Object.freeze({
|
|
1005
|
+
c0,
|
|
1006
|
+
c1: this.Fp.mul(c1, this.FROBENIUS_COEFFICIENTS[power % 2])
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
class _Field6 {
|
|
1011
|
+
ORDER;
|
|
1012
|
+
BITS;
|
|
1013
|
+
BYTES;
|
|
1014
|
+
isLE;
|
|
1015
|
+
ZERO;
|
|
1016
|
+
ONE;
|
|
1017
|
+
Fp2;
|
|
1018
|
+
constructor(Fp2){
|
|
1019
|
+
this.Fp2 = Fp2, this.ORDER = Fp2.Fp.ORDER ** _6n, this.BITS = 3 * Fp2.BITS, this.BYTES = 3 * Fp2.BYTES, this.isLE = Fp2.isLE, this.ZERO = this.create({
|
|
1020
|
+
c0: Fp2.ZERO,
|
|
1021
|
+
c1: Fp2.ZERO,
|
|
1022
|
+
c2: Fp2.ZERO
|
|
1023
|
+
}), this.ONE = this.create({
|
|
1024
|
+
c0: Fp2.ONE,
|
|
1025
|
+
c1: Fp2.ZERO,
|
|
1026
|
+
c2: Fp2.ZERO
|
|
1027
|
+
}), Object.freeze(this);
|
|
1028
|
+
}
|
|
1029
|
+
get FROBENIUS_COEFFICIENTS_1() {
|
|
1030
|
+
let frob = _FROBENIUS_COEFFICIENTS_6.get(this);
|
|
1031
|
+
if (frob) return frob[0];
|
|
1032
|
+
let { Fp2 } = this, { Fp } = Fp2, rows = calcFrobeniusCoefficients(Fp2, Fp2.NONRESIDUE, Fp.ORDER, 6, 2, 3), cache = [
|
|
1033
|
+
Object.freeze(rows[0]),
|
|
1034
|
+
Object.freeze(rows[1])
|
|
1035
|
+
];
|
|
1036
|
+
return _FROBENIUS_COEFFICIENTS_6.set(this, cache), cache[0];
|
|
1037
|
+
}
|
|
1038
|
+
get FROBENIUS_COEFFICIENTS_2() {
|
|
1039
|
+
let frob = _FROBENIUS_COEFFICIENTS_6.get(this);
|
|
1040
|
+
return frob ? frob[1] : (this.FROBENIUS_COEFFICIENTS_1, _FROBENIUS_COEFFICIENTS_6.get(this)[1]);
|
|
1041
|
+
}
|
|
1042
|
+
add({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
|
|
1043
|
+
let { Fp2 } = this;
|
|
1044
|
+
return Object.freeze({
|
|
1045
|
+
c0: Fp2.add(c0, r0),
|
|
1046
|
+
c1: Fp2.add(c1, r1),
|
|
1047
|
+
c2: Fp2.add(c2, r2)
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
sub({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
|
|
1051
|
+
let { Fp2 } = this;
|
|
1052
|
+
return Object.freeze({
|
|
1053
|
+
c0: Fp2.sub(c0, r0),
|
|
1054
|
+
c1: Fp2.sub(c1, r1),
|
|
1055
|
+
c2: Fp2.sub(c2, r2)
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
mul({ c0, c1, c2 }, rhs) {
|
|
1059
|
+
let { Fp2 } = this;
|
|
1060
|
+
if ('bigint' == typeof rhs) return Object.freeze({
|
|
1061
|
+
c0: Fp2.mul(c0, rhs),
|
|
1062
|
+
c1: Fp2.mul(c1, rhs),
|
|
1063
|
+
c2: Fp2.mul(c2, rhs)
|
|
1064
|
+
});
|
|
1065
|
+
let { c0: r0, c1: r1, c2: r2 } = rhs, t0 = Fp2.mul(c0, r0), t1 = Fp2.mul(c1, r1), t2 = Fp2.mul(c2, r2);
|
|
1066
|
+
return Object.freeze({
|
|
1067
|
+
c0: Fp2.add(t0, Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), Fp2.add(r1, r2)), Fp2.add(t1, t2)))),
|
|
1068
|
+
c1: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c1), Fp2.add(r0, r1)), Fp2.add(t0, t1)), Fp2.mulByNonresidue(t2)),
|
|
1069
|
+
c2: Fp2.sub(Fp2.add(t1, Fp2.mul(Fp2.add(c0, c2), Fp2.add(r0, r2))), Fp2.add(t0, t2))
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
sqr({ c0, c1, c2 }) {
|
|
1073
|
+
let { Fp2 } = this, t0 = Fp2.sqr(c0), t1 = Fp2.mul(Fp2.mul(c0, c1), tower_2n), t3 = Fp2.mul(Fp2.mul(c1, c2), tower_2n), t4 = Fp2.sqr(c2);
|
|
1074
|
+
return Object.freeze({
|
|
1075
|
+
c0: Fp2.add(Fp2.mulByNonresidue(t3), t0),
|
|
1076
|
+
c1: Fp2.add(Fp2.mulByNonresidue(t4), t1),
|
|
1077
|
+
c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.sqr(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4)
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
addN(a, b) {
|
|
1081
|
+
return this.add(a, b);
|
|
1082
|
+
}
|
|
1083
|
+
subN(a, b) {
|
|
1084
|
+
return this.sub(a, b);
|
|
1085
|
+
}
|
|
1086
|
+
mulN(a, b) {
|
|
1087
|
+
return this.mul(a, b);
|
|
1088
|
+
}
|
|
1089
|
+
sqrN(a) {
|
|
1090
|
+
return this.sqr(a);
|
|
1091
|
+
}
|
|
1092
|
+
create(num) {
|
|
1093
|
+
let { Fp2 } = this, c0 = Fp2.create(num.c0);
|
|
1094
|
+
return Object.freeze({
|
|
1095
|
+
c0,
|
|
1096
|
+
c1: Fp2.create(num.c1),
|
|
1097
|
+
c2: Fp2.create(num.c2)
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
isValid(num) {
|
|
1101
|
+
if (!isObj(num)) throw TypeError('invalid field element: expected object, got ' + typeof num);
|
|
1102
|
+
let { c0, c1, c2 } = num, { Fp2 } = this;
|
|
1103
|
+
return Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2);
|
|
1104
|
+
}
|
|
1105
|
+
is0(num) {
|
|
1106
|
+
if (!isObj(num)) return !1;
|
|
1107
|
+
let { c0, c1, c2 } = num, { Fp2 } = this;
|
|
1108
|
+
return Fp2.is0(c0) && Fp2.is0(c1) && Fp2.is0(c2);
|
|
1109
|
+
}
|
|
1110
|
+
isValidNot0(num) {
|
|
1111
|
+
return !this.is0(num) && this.isValid(num);
|
|
1112
|
+
}
|
|
1113
|
+
neg({ c0, c1, c2 }) {
|
|
1114
|
+
let { Fp2 } = this;
|
|
1115
|
+
return Object.freeze({
|
|
1116
|
+
c0: Fp2.neg(c0),
|
|
1117
|
+
c1: Fp2.neg(c1),
|
|
1118
|
+
c2: Fp2.neg(c2)
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
eql({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) {
|
|
1122
|
+
let { Fp2 } = this;
|
|
1123
|
+
return Fp2.eql(c0, r0) && Fp2.eql(c1, r1) && Fp2.eql(c2, r2);
|
|
1124
|
+
}
|
|
1125
|
+
sqrt(_) {
|
|
1126
|
+
return utils_notImplemented();
|
|
1127
|
+
}
|
|
1128
|
+
div(lhs, rhs) {
|
|
1129
|
+
let { Fp2 } = this, { Fp } = Fp2;
|
|
1130
|
+
return this.mul(lhs, 'bigint' == typeof rhs ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
|
|
1131
|
+
}
|
|
1132
|
+
pow(num, power) {
|
|
1133
|
+
return FpPow(this, num, power);
|
|
1134
|
+
}
|
|
1135
|
+
invertBatch(nums) {
|
|
1136
|
+
return modular_FpInvertBatch(this, nums);
|
|
1137
|
+
}
|
|
1138
|
+
inv({ c0, c1, c2 }) {
|
|
1139
|
+
let { Fp2 } = this, t0 = Fp2.sub(Fp2.sqr(c0), Fp2.mulByNonresidue(Fp2.mul(c2, c1))), t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.sqr(c2)), Fp2.mul(c0, c1)), t2 = Fp2.sub(Fp2.sqr(c1), Fp2.mul(c0, c2)), t4 = Fp2.inv(Fp2.add(Fp2.mulByNonresidue(Fp2.add(Fp2.mul(c2, t1), Fp2.mul(c1, t2))), Fp2.mul(c0, t0)));
|
|
1140
|
+
return Object.freeze({
|
|
1141
|
+
c0: Fp2.mul(t4, t0),
|
|
1142
|
+
c1: Fp2.mul(t4, t1),
|
|
1143
|
+
c2: Fp2.mul(t4, t2)
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
fromBytes(b) {
|
|
1147
|
+
let { Fp2 } = this;
|
|
1148
|
+
if (curves_utils_abytes(b), b.length !== this.BYTES) throw Error('fromBytes invalid length=' + b.length);
|
|
1149
|
+
let B2 = Fp2.BYTES;
|
|
1150
|
+
return this.create({
|
|
1151
|
+
c0: Fp2.fromBytes(b.subarray(0, B2)),
|
|
1152
|
+
c1: Fp2.fromBytes(b.subarray(B2, 2 * B2)),
|
|
1153
|
+
c2: Fp2.fromBytes(b.subarray(2 * B2))
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
toBytes({ c0, c1, c2 }) {
|
|
1157
|
+
let { Fp2 } = this;
|
|
1158
|
+
return curves_utils_concatBytes(Fp2.toBytes(c0), Fp2.toBytes(c1), Fp2.toBytes(c2));
|
|
1159
|
+
}
|
|
1160
|
+
cmov({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }, c) {
|
|
1161
|
+
let { Fp2 } = this;
|
|
1162
|
+
return this.create({
|
|
1163
|
+
c0: Fp2.cmov(c0, r0, c),
|
|
1164
|
+
c1: Fp2.cmov(c1, r1, c),
|
|
1165
|
+
c2: Fp2.cmov(c2, r2, c)
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
fromBigSix(tuple) {
|
|
1169
|
+
let { Fp2 } = this;
|
|
1170
|
+
if (!Array.isArray(tuple) || 6 !== tuple.length) throw Error('invalid Fp6.fromBigSix');
|
|
1171
|
+
for(let i = 0; i < 6; i++)if ('bigint' != typeof tuple[i]) throw Error('invalid Fp6.fromBigSix');
|
|
1172
|
+
return this.create({
|
|
1173
|
+
c0: Fp2.fromBigTuple(tuple.slice(0, 2)),
|
|
1174
|
+
c1: Fp2.fromBigTuple(tuple.slice(2, 4)),
|
|
1175
|
+
c2: Fp2.fromBigTuple(tuple.slice(4, 6))
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
frobeniusMap({ c0, c1, c2 }, power) {
|
|
1179
|
+
let { Fp2 } = this;
|
|
1180
|
+
return Object.freeze({
|
|
1181
|
+
c0: Fp2.frobeniusMap(c0, power),
|
|
1182
|
+
c1: Fp2.mul(Fp2.frobeniusMap(c1, power), this.FROBENIUS_COEFFICIENTS_1[power % 6]),
|
|
1183
|
+
c2: Fp2.mul(Fp2.frobeniusMap(c2, power), this.FROBENIUS_COEFFICIENTS_2[power % 6])
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
mulByFp2({ c0, c1, c2 }, rhs) {
|
|
1187
|
+
let { Fp2 } = this;
|
|
1188
|
+
return Object.freeze({
|
|
1189
|
+
c0: Fp2.mul(c0, rhs),
|
|
1190
|
+
c1: Fp2.mul(c1, rhs),
|
|
1191
|
+
c2: Fp2.mul(c2, rhs)
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
mulByNonresidue({ c0, c1, c2 }) {
|
|
1195
|
+
let { Fp2 } = this;
|
|
1196
|
+
return Object.freeze({
|
|
1197
|
+
c0: Fp2.mulByNonresidue(c2),
|
|
1198
|
+
c1: c0,
|
|
1199
|
+
c2: c1
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
mul1({ c0, c1, c2 }, b1) {
|
|
1203
|
+
let { Fp2 } = this;
|
|
1204
|
+
return Object.freeze({
|
|
1205
|
+
c0: Fp2.mulByNonresidue(Fp2.mul(c2, b1)),
|
|
1206
|
+
c1: Fp2.mul(c0, b1),
|
|
1207
|
+
c2: Fp2.mul(c1, b1)
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
mul01({ c0, c1, c2 }, b0, b1) {
|
|
1211
|
+
let { Fp2 } = this, t0 = Fp2.mul(c0, b0), t1 = Fp2.mul(c1, b1);
|
|
1212
|
+
return Object.freeze({
|
|
1213
|
+
c0: Fp2.add(Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), b1), t1)), t0),
|
|
1214
|
+
c1: Fp2.sub(Fp2.sub(Fp2.mul(Fp2.add(b0, b1), Fp2.add(c0, c1)), t0), t1),
|
|
1215
|
+
c2: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c2), b0), t0), t1)
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
let _FROBENIUS_COEFFICIENTS_6 = new WeakMap();
|
|
1220
|
+
class _Field12 {
|
|
1221
|
+
ORDER;
|
|
1222
|
+
BITS;
|
|
1223
|
+
BYTES;
|
|
1224
|
+
isLE;
|
|
1225
|
+
ZERO;
|
|
1226
|
+
ONE;
|
|
1227
|
+
Fp6;
|
|
1228
|
+
X_LEN;
|
|
1229
|
+
finalExponentiate;
|
|
1230
|
+
constructor(Fp6, opts){
|
|
1231
|
+
let { X_LEN, Fp12finalExponentiate } = opts, { Fp2 } = Fp6, { Fp } = Fp2;
|
|
1232
|
+
this.Fp6 = Fp6, this.ORDER = Fp.ORDER ** _12n, this.BITS = 2 * Fp6.BITS, this.BYTES = 2 * Fp6.BYTES, this.isLE = Fp6.isLE, this.ZERO = this.create({
|
|
1233
|
+
c0: Fp6.ZERO,
|
|
1234
|
+
c1: Fp6.ZERO
|
|
1235
|
+
}), this.ONE = this.create({
|
|
1236
|
+
c0: Fp6.ONE,
|
|
1237
|
+
c1: Fp6.ZERO
|
|
1238
|
+
}), this.X_LEN = X_LEN, this.finalExponentiate = (num)=>{
|
|
1239
|
+
let copy2 = ({ c0, c1 })=>Object.freeze({
|
|
1240
|
+
c0,
|
|
1241
|
+
c1
|
|
1242
|
+
}), copy6 = ({ c0, c1, c2 })=>Object.freeze({
|
|
1243
|
+
c0: copy2(c0),
|
|
1244
|
+
c1: copy2(c1),
|
|
1245
|
+
c2: copy2(c2)
|
|
1246
|
+
}), res = Fp12finalExponentiate(num);
|
|
1247
|
+
return Object.freeze({
|
|
1248
|
+
c0: copy6(res.c0),
|
|
1249
|
+
c1: copy6(res.c1)
|
|
1250
|
+
});
|
|
1251
|
+
}, Object.freeze(this);
|
|
1252
|
+
}
|
|
1253
|
+
get FROBENIUS_COEFFICIENTS() {
|
|
1254
|
+
let frob = _FROBENIUS_COEFFICIENTS_12.get(this);
|
|
1255
|
+
if (frob) return frob;
|
|
1256
|
+
let { Fp2 } = this.Fp6, { Fp } = Fp2, cache = Object.freeze(calcFrobeniusCoefficients(Fp2, Fp2.NONRESIDUE, Fp.ORDER, 12, 1, 6)[0]);
|
|
1257
|
+
return _FROBENIUS_COEFFICIENTS_12.set(this, cache), cache;
|
|
1258
|
+
}
|
|
1259
|
+
create(num) {
|
|
1260
|
+
let { Fp6 } = this;
|
|
1261
|
+
return Object.freeze({
|
|
1262
|
+
c0: Fp6.create(num.c0),
|
|
1263
|
+
c1: Fp6.create(num.c1)
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
isValid(num) {
|
|
1267
|
+
if (!isObj(num)) throw TypeError('invalid field element: expected object, got ' + typeof num);
|
|
1268
|
+
let { c0, c1 } = num, { Fp6 } = this;
|
|
1269
|
+
return Fp6.isValid(c0) && Fp6.isValid(c1);
|
|
1270
|
+
}
|
|
1271
|
+
is0(num) {
|
|
1272
|
+
if (!isObj(num)) return !1;
|
|
1273
|
+
let { c0, c1 } = num, { Fp6 } = this;
|
|
1274
|
+
return Fp6.is0(c0) && Fp6.is0(c1);
|
|
1275
|
+
}
|
|
1276
|
+
isValidNot0(num) {
|
|
1277
|
+
return !this.is0(num) && this.isValid(num);
|
|
1278
|
+
}
|
|
1279
|
+
neg({ c0, c1 }) {
|
|
1280
|
+
let { Fp6 } = this;
|
|
1281
|
+
return Object.freeze({
|
|
1282
|
+
c0: Fp6.neg(c0),
|
|
1283
|
+
c1: Fp6.neg(c1)
|
|
1284
|
+
});
|
|
1285
|
+
}
|
|
1286
|
+
eql({ c0, c1 }, { c0: r0, c1: r1 }) {
|
|
1287
|
+
let { Fp6 } = this;
|
|
1288
|
+
return Fp6.eql(c0, r0) && Fp6.eql(c1, r1);
|
|
1289
|
+
}
|
|
1290
|
+
sqrt(_) {
|
|
1291
|
+
return utils_notImplemented();
|
|
1292
|
+
}
|
|
1293
|
+
inv({ c0, c1 }) {
|
|
1294
|
+
let { Fp6 } = this, t = Fp6.inv(Fp6.sub(Fp6.sqr(c0), Fp6.mulByNonresidue(Fp6.sqr(c1))));
|
|
1295
|
+
return Object.freeze({
|
|
1296
|
+
c0: Fp6.mul(c0, t),
|
|
1297
|
+
c1: Fp6.neg(Fp6.mul(c1, t))
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
div(lhs, rhs) {
|
|
1301
|
+
let { Fp6 } = this, { Fp2 } = Fp6, { Fp } = Fp2;
|
|
1302
|
+
return this.mul(lhs, 'bigint' == typeof rhs ? Fp.inv(Fp.create(rhs)) : this.inv(rhs));
|
|
1303
|
+
}
|
|
1304
|
+
pow(num, power) {
|
|
1305
|
+
return FpPow(this, num, power);
|
|
1306
|
+
}
|
|
1307
|
+
invertBatch(nums) {
|
|
1308
|
+
return modular_FpInvertBatch(this, nums);
|
|
1309
|
+
}
|
|
1310
|
+
add({ c0, c1 }, { c0: r0, c1: r1 }) {
|
|
1311
|
+
let { Fp6 } = this;
|
|
1312
|
+
return Object.freeze({
|
|
1313
|
+
c0: Fp6.add(c0, r0),
|
|
1314
|
+
c1: Fp6.add(c1, r1)
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
sub({ c0, c1 }, { c0: r0, c1: r1 }) {
|
|
1318
|
+
let { Fp6 } = this;
|
|
1319
|
+
return Object.freeze({
|
|
1320
|
+
c0: Fp6.sub(c0, r0),
|
|
1321
|
+
c1: Fp6.sub(c1, r1)
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
mul({ c0, c1 }, rhs) {
|
|
1325
|
+
let { Fp6 } = this;
|
|
1326
|
+
if ('bigint' == typeof rhs) return Object.freeze({
|
|
1327
|
+
c0: Fp6.mul(c0, rhs),
|
|
1328
|
+
c1: Fp6.mul(c1, rhs)
|
|
1329
|
+
});
|
|
1330
|
+
let { c0: r0, c1: r1 } = rhs, t1 = Fp6.mul(c0, r0), t2 = Fp6.mul(c1, r1);
|
|
1331
|
+
return Object.freeze({
|
|
1332
|
+
c0: Fp6.add(t1, Fp6.mulByNonresidue(t2)),
|
|
1333
|
+
c1: Fp6.sub(Fp6.mul(Fp6.add(c0, c1), Fp6.add(r0, r1)), Fp6.add(t1, t2))
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
sqr({ c0, c1 }) {
|
|
1337
|
+
let { Fp6 } = this, ab = Fp6.mul(c0, c1);
|
|
1338
|
+
return Object.freeze({
|
|
1339
|
+
c0: Fp6.sub(Fp6.sub(Fp6.mul(Fp6.add(Fp6.mulByNonresidue(c1), c0), Fp6.add(c0, c1)), ab), Fp6.mulByNonresidue(ab)),
|
|
1340
|
+
c1: Fp6.add(ab, ab)
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
addN(a, b) {
|
|
1344
|
+
return this.add(a, b);
|
|
1345
|
+
}
|
|
1346
|
+
subN(a, b) {
|
|
1347
|
+
return this.sub(a, b);
|
|
1348
|
+
}
|
|
1349
|
+
mulN(a, b) {
|
|
1350
|
+
return this.mul(a, b);
|
|
1351
|
+
}
|
|
1352
|
+
sqrN(a) {
|
|
1353
|
+
return this.sqr(a);
|
|
1354
|
+
}
|
|
1355
|
+
fromBytes(b) {
|
|
1356
|
+
let { Fp6 } = this;
|
|
1357
|
+
if (curves_utils_abytes(b), b.length !== this.BYTES) throw Error('fromBytes invalid length=' + b.length);
|
|
1358
|
+
return this.create({
|
|
1359
|
+
c0: Fp6.fromBytes(b.subarray(0, Fp6.BYTES)),
|
|
1360
|
+
c1: Fp6.fromBytes(b.subarray(Fp6.BYTES))
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
toBytes({ c0, c1 }) {
|
|
1364
|
+
let { Fp6 } = this;
|
|
1365
|
+
return curves_utils_concatBytes(Fp6.toBytes(c0), Fp6.toBytes(c1));
|
|
1366
|
+
}
|
|
1367
|
+
cmov({ c0, c1 }, { c0: r0, c1: r1 }, c) {
|
|
1368
|
+
let { Fp6 } = this;
|
|
1369
|
+
return this.create({
|
|
1370
|
+
c0: Fp6.cmov(c0, r0, c),
|
|
1371
|
+
c1: Fp6.cmov(c1, r1, c)
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
fromBigTwelve(tuple) {
|
|
1375
|
+
let { Fp6 } = this;
|
|
1376
|
+
if (!Array.isArray(tuple) || 12 !== tuple.length) throw Error('invalid Fp12.fromBigTwelve');
|
|
1377
|
+
for(let i = 0; i < 12; i++)if ('bigint' != typeof tuple[i]) throw Error('invalid Fp12.fromBigTwelve');
|
|
1378
|
+
return this.create({
|
|
1379
|
+
c0: Fp6.fromBigSix(tuple.slice(0, 6)),
|
|
1380
|
+
c1: Fp6.fromBigSix(tuple.slice(6, 12))
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
frobeniusMap(lhs, power) {
|
|
1384
|
+
let { Fp6 } = this, { Fp2 } = Fp6, { c0, c1, c2 } = Fp6.frobeniusMap(lhs.c1, power), coeff = this.FROBENIUS_COEFFICIENTS[power % 12];
|
|
1385
|
+
return Object.freeze({
|
|
1386
|
+
c0: Fp6.frobeniusMap(lhs.c0, power),
|
|
1387
|
+
c1: Object.freeze({
|
|
1388
|
+
c0: Fp2.mul(c0, coeff),
|
|
1389
|
+
c1: Fp2.mul(c1, coeff),
|
|
1390
|
+
c2: Fp2.mul(c2, coeff)
|
|
1391
|
+
})
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
mulByFp2({ c0, c1 }, rhs) {
|
|
1395
|
+
let { Fp6 } = this;
|
|
1396
|
+
return Object.freeze({
|
|
1397
|
+
c0: Fp6.mulByFp2(c0, rhs),
|
|
1398
|
+
c1: Fp6.mulByFp2(c1, rhs)
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
conjugate({ c0, c1 }) {
|
|
1402
|
+
return Object.freeze({
|
|
1403
|
+
c0,
|
|
1404
|
+
c1: this.Fp6.neg(c1)
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
mul014({ c0, c1 }, o0, o1, o4) {
|
|
1408
|
+
let { Fp6 } = this, { Fp2 } = Fp6, t0 = Fp6.mul01(c0, o0, o1), t1 = Fp6.mul1(c1, o4);
|
|
1409
|
+
return Object.freeze({
|
|
1410
|
+
c0: Fp6.add(Fp6.mulByNonresidue(t1), t0),
|
|
1411
|
+
c1: Fp6.sub(Fp6.sub(Fp6.mul01(Fp6.add(c1, c0), o0, Fp2.add(o1, o4)), t0), t1)
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
mul034({ c0, c1 }, o0, o3, o4) {
|
|
1415
|
+
let { Fp6 } = this, { Fp2 } = Fp6, a = Object.freeze({
|
|
1416
|
+
c0: Fp2.mul(c0.c0, o0),
|
|
1417
|
+
c1: Fp2.mul(c0.c1, o0),
|
|
1418
|
+
c2: Fp2.mul(c0.c2, o0)
|
|
1419
|
+
}), b = Fp6.mul01(c1, o3, o4), e = Fp6.mul01(Fp6.add(c0, c1), Fp2.add(o0, o3), o4);
|
|
1420
|
+
return Object.freeze({
|
|
1421
|
+
c0: Fp6.add(Fp6.mulByNonresidue(b), a),
|
|
1422
|
+
c1: Fp6.sub(e, Fp6.add(a, b))
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
_cyclotomicSquare({ c0, c1 }) {
|
|
1426
|
+
let { Fp6 } = this, { Fp2 } = Fp6, { c0: c0c0, c1: c0c1, c2: c0c2 } = c0, { c0: c1c0, c1: c1c1, c2: c1c2 } = c1, { first: t3, second: t4 } = Fp2.Fp4Square(c0c0, c1c1), { first: t5, second: t6 } = Fp2.Fp4Square(c1c0, c0c2), { first: t7, second: t8 } = Fp2.Fp4Square(c0c1, c1c2), t9 = Fp2.mulByNonresidue(t8);
|
|
1427
|
+
return Object.freeze({
|
|
1428
|
+
c0: Object.freeze({
|
|
1429
|
+
c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), tower_2n), t3),
|
|
1430
|
+
c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), tower_2n), t5),
|
|
1431
|
+
c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), tower_2n), t7)
|
|
1432
|
+
}),
|
|
1433
|
+
c1: Object.freeze({
|
|
1434
|
+
c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), tower_2n), t9),
|
|
1435
|
+
c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), tower_2n), t4),
|
|
1436
|
+
c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), tower_2n), t6)
|
|
1437
|
+
})
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
_cyclotomicExp(num, n) {
|
|
1441
|
+
utils_aInRange('cyclotomic exponent', n, tower_0n, tower_1n << BigInt(this.X_LEN));
|
|
1442
|
+
let z = this.ONE;
|
|
1443
|
+
for(let i = this.X_LEN - 1; i >= 0; i--)z = this._cyclotomicSquare(z), bitGet(n, i) && (z = this.mul(z, num));
|
|
1444
|
+
return z;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
let _FROBENIUS_COEFFICIENTS_12 = new WeakMap(), curve_0n = BigInt(0), curve_1n = BigInt(1), pointPrecomputes = new WeakMap(), pointWindowSizes = new WeakMap();
|
|
1448
|
+
class wNAF {
|
|
1449
|
+
BASE;
|
|
1450
|
+
ZERO;
|
|
1451
|
+
Fn;
|
|
1452
|
+
bits;
|
|
1453
|
+
constructor(Point, bits){
|
|
1454
|
+
this.BASE = Point.BASE, this.ZERO = Point.ZERO, this.Fn = Point.Fn, this.bits = bits;
|
|
1455
|
+
}
|
|
1456
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
1457
|
+
let d = elm;
|
|
1458
|
+
for(; n > curve_0n;)n & curve_1n && (p = p.add(d)), d = d.double(), n >>= curve_1n;
|
|
1459
|
+
return p;
|
|
1460
|
+
}
|
|
1461
|
+
precomputeWindow(point, W) {
|
|
1462
|
+
let { windows, windowSize } = calcWOpts(W, this.bits), points = [], p = point, base = p;
|
|
1463
|
+
for(let window = 0; window < windows; window++){
|
|
1464
|
+
base = p, points.push(base);
|
|
1465
|
+
for(let i = 1; i < windowSize; i++)base = base.add(p), points.push(base);
|
|
1466
|
+
p = base.double();
|
|
1467
|
+
}
|
|
1468
|
+
return points;
|
|
1469
|
+
}
|
|
1470
|
+
wNAF(W, precomputes, n) {
|
|
1471
|
+
if (!this.Fn.isValid(n)) throw Error('invalid scalar');
|
|
1472
|
+
let p = this.ZERO, f = this.BASE, wo = calcWOpts(W, this.bits);
|
|
1473
|
+
for(let window = 0; window < wo.windows; window++){
|
|
1474
|
+
let { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
1475
|
+
n = nextN, isZero ? f = f.add(negateCt(isNegF, precomputes[offsetF])) : p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
1476
|
+
}
|
|
1477
|
+
return assert0(n), {
|
|
1478
|
+
p,
|
|
1479
|
+
f
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
1483
|
+
let wo = calcWOpts(W, this.bits);
|
|
1484
|
+
for(let window = 0; window < wo.windows && n !== curve_0n; window++){
|
|
1485
|
+
let { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1486
|
+
if (n = nextN, !isZero) {
|
|
1487
|
+
let item = precomputes[offset];
|
|
1488
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
return assert0(n), acc;
|
|
1492
|
+
}
|
|
1493
|
+
getPrecomputes(W, point, transform) {
|
|
1494
|
+
let comp = pointPrecomputes.get(point);
|
|
1495
|
+
return comp || (comp = this.precomputeWindow(point, W), 1 !== W && ('function' == typeof transform && (comp = transform(comp)), pointPrecomputes.set(point, comp))), comp;
|
|
1496
|
+
}
|
|
1497
|
+
cached(point, scalar, transform) {
|
|
1498
|
+
let W = getW(point);
|
|
1499
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
1500
|
+
}
|
|
1501
|
+
unsafe(point, scalar, transform, prev) {
|
|
1502
|
+
let W = getW(point);
|
|
1503
|
+
return 1 === W ? this._unsafeLadder(point, scalar, prev) : this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
1504
|
+
}
|
|
1505
|
+
createCache(P, W) {
|
|
1506
|
+
validateW(W, this.bits), pointWindowSizes.set(P, W), pointPrecomputes.delete(P);
|
|
1507
|
+
}
|
|
1508
|
+
hasCache(elm) {
|
|
1509
|
+
return 1 !== getW(elm);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
let divNearest = (num, den)=>(num + (num >= 0 ? den : -den) / weierstrass_2n) / den, DER = {
|
|
1513
|
+
Err: class extends Error {
|
|
1514
|
+
constructor(m = ''){
|
|
1515
|
+
super(m);
|
|
1516
|
+
}
|
|
1517
|
+
},
|
|
1518
|
+
_tlv: {
|
|
1519
|
+
encode: (tag, data)=>{
|
|
1520
|
+
let { Err: E } = DER;
|
|
1521
|
+
if (asafenumber(tag, 'tag'), tag < 0 || tag > 255) throw new E('tlv.encode: wrong tag');
|
|
1522
|
+
if ('string' != typeof data) throw TypeError('"data" expected string, got type=' + typeof data);
|
|
1523
|
+
if (1 & data.length) throw new E('tlv.encode: unpadded data');
|
|
1524
|
+
let dataLen = data.length / 2, len = numberToHexUnpadded(dataLen);
|
|
1525
|
+
if (len.length / 2 & 128) throw new E('tlv.encode: long form length too big');
|
|
1526
|
+
let lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : '';
|
|
1527
|
+
return numberToHexUnpadded(tag) + lenLen + len + data;
|
|
1528
|
+
},
|
|
1529
|
+
decode (tag, data) {
|
|
1530
|
+
let { Err: E } = DER;
|
|
1531
|
+
data = curves_utils_abytes(data, void 0, 'DER data');
|
|
1532
|
+
let pos = 0;
|
|
1533
|
+
if (tag < 0 || tag > 255) throw new E('tlv.encode: wrong tag');
|
|
1534
|
+
if (data.length < 2 || data[pos++] !== tag) throw new E('tlv.decode: wrong tlv');
|
|
1535
|
+
let first = data[pos++], length = 0;
|
|
1536
|
+
if (128 & first) {
|
|
1537
|
+
let lenLen = 127 & first;
|
|
1538
|
+
if (!lenLen) throw new E('tlv.decode(long): indefinite length not supported');
|
|
1539
|
+
if (lenLen > 4) throw new E('tlv.decode(long): byte length is too big');
|
|
1540
|
+
let lengthBytes = data.subarray(pos, pos + lenLen);
|
|
1541
|
+
if (lengthBytes.length !== lenLen) throw new E('tlv.decode: length bytes not complete');
|
|
1542
|
+
if (0 === lengthBytes[0]) throw new E('tlv.decode(long): zero leftmost byte');
|
|
1543
|
+
for (let b of lengthBytes)length = length << 8 | b;
|
|
1544
|
+
if (pos += lenLen, length < 128) throw new E('tlv.decode(long): not minimal encoding');
|
|
1545
|
+
} else length = first;
|
|
1546
|
+
let v = data.subarray(pos, pos + length);
|
|
1547
|
+
if (v.length !== length) throw new E('tlv.decode: wrong value length');
|
|
1548
|
+
return {
|
|
1549
|
+
v,
|
|
1550
|
+
l: data.subarray(pos + length)
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
},
|
|
1554
|
+
_int: {
|
|
1555
|
+
encode (num) {
|
|
1556
|
+
let { Err: E } = DER;
|
|
1557
|
+
if (abignumber(num), num < weierstrass_0n) throw new E('integer: negative integers are not allowed');
|
|
1558
|
+
let hex = numberToHexUnpadded(num);
|
|
1559
|
+
if (8 & Number.parseInt(hex[0], 16) && (hex = '00' + hex), 1 & hex.length) throw new E('unexpected DER parsing assertion: unpadded hex');
|
|
1560
|
+
return hex;
|
|
1561
|
+
},
|
|
1562
|
+
decode (data) {
|
|
1563
|
+
let { Err: E } = DER;
|
|
1564
|
+
if (data.length < 1) throw new E('invalid signature integer: empty');
|
|
1565
|
+
if (128 & data[0]) throw new E('invalid signature integer: negative');
|
|
1566
|
+
if (data.length > 1 && 0x00 === data[0] && !(128 & data[1])) throw new E('invalid signature integer: unnecessary leading zero');
|
|
1567
|
+
return utils_bytesToNumberBE(data);
|
|
1568
|
+
}
|
|
1569
|
+
},
|
|
1570
|
+
toSig (bytes) {
|
|
1571
|
+
let { Err: E, _int: int, _tlv: tlv } = DER, data = curves_utils_abytes(bytes, void 0, 'signature'), { v: seqBytes, l: seqLeftBytes } = tlv.decode(0x30, data);
|
|
1572
|
+
if (seqLeftBytes.length) throw new E('invalid signature: left bytes after parsing');
|
|
1573
|
+
let { v: rBytes, l: rLeftBytes } = tlv.decode(0x02, seqBytes), { v: sBytes, l: sLeftBytes } = tlv.decode(0x02, rLeftBytes);
|
|
1574
|
+
if (sLeftBytes.length) throw new E('invalid signature: left bytes after parsing');
|
|
1575
|
+
return {
|
|
1576
|
+
r: int.decode(rBytes),
|
|
1577
|
+
s: int.decode(sBytes)
|
|
1578
|
+
};
|
|
1579
|
+
},
|
|
1580
|
+
hexFromSig (sig) {
|
|
1581
|
+
let { _tlv: tlv, _int: int } = DER, rs = tlv.encode(0x02, int.encode(sig.r)), ss = tlv.encode(0x02, int.encode(sig.s));
|
|
1582
|
+
return tlv.encode(0x30, rs + ss);
|
|
1583
|
+
}
|
|
1584
|
+
};
|
|
1585
|
+
Object.freeze(DER._tlv), Object.freeze(DER._int), Object.freeze(DER);
|
|
1586
|
+
let weierstrass_0n = BigInt(0), weierstrass_1n = BigInt(1), weierstrass_2n = BigInt(2), weierstrass_3n = BigInt(3), weierstrass_4n = BigInt(4), bn254_0n = BigInt(0), bn254_1n = BigInt(1), bn254_2n = BigInt(2), bn254_3n = BigInt(3), bn254_6n = BigInt(6), BN_X = BigInt('4965661367192848881'), BN_X_LEN = bitLen(BN_X), SIX_X_SQUARED = bn254_6n * BN_X ** bn254_2n, bn254_G1_CURVE = {
|
|
1587
|
+
p: BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47'),
|
|
1588
|
+
n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
|
|
1589
|
+
h: bn254_1n,
|
|
1590
|
+
a: bn254_0n,
|
|
1591
|
+
b: bn254_3n,
|
|
1592
|
+
Gx: bn254_1n,
|
|
1593
|
+
Gy: BigInt(2)
|
|
1594
|
+
}, bn254_Fr = Field(bn254_G1_CURVE.n), Fp2B = {
|
|
1595
|
+
c0: BigInt('19485874751759354771024239261021720505790618469301721065564631296452457478373'),
|
|
1596
|
+
c1: BigInt('266929791119991161246907387137283842545076965332900288569378510910307636690')
|
|
1597
|
+
}, tower = (bn254_Fp12 = (res = function(opts) {
|
|
1598
|
+
if (utils_validateObject(opts, {
|
|
1599
|
+
ORDER: 'bigint',
|
|
1600
|
+
X_LEN: 'number',
|
|
1601
|
+
FP2_NONRESIDUE: 'object',
|
|
1602
|
+
Fp2mulByB: 'function',
|
|
1603
|
+
Fp12finalExponentiate: 'function'
|
|
1604
|
+
}, {
|
|
1605
|
+
NONRESIDUE: 'bigint'
|
|
1606
|
+
}), asafenumber(opts.X_LEN, 'X_LEN'), opts.X_LEN < 1) throw Error('invalid X_LEN');
|
|
1607
|
+
let nonresidue = opts.FP2_NONRESIDUE;
|
|
1608
|
+
if (!Array.isArray(nonresidue) || 2 !== nonresidue.length || 'bigint' != typeof nonresidue[0] || 'bigint' != typeof nonresidue[1]) throw Error('invalid FP2_NONRESIDUE');
|
|
1609
|
+
let Fp = Field(opts.ORDER), Fp2 = new _Field2(Fp, opts), Fp6 = new _Field6(Fp2), Fp12 = new _Field12(Fp6, opts);
|
|
1610
|
+
return {
|
|
1611
|
+
Fp,
|
|
1612
|
+
Fp2,
|
|
1613
|
+
Fp6,
|
|
1614
|
+
Fp12
|
|
1615
|
+
};
|
|
1616
|
+
}({
|
|
1617
|
+
ORDER: bn254_G1_CURVE.p,
|
|
1618
|
+
X_LEN: BN_X_LEN,
|
|
1619
|
+
FP2_NONRESIDUE: [
|
|
1620
|
+
BigInt(9),
|
|
1621
|
+
bn254_1n
|
|
1622
|
+
],
|
|
1623
|
+
Fp2mulByB: (num)=>bn254_Fp2.mul(num, Fp2B),
|
|
1624
|
+
Fp12finalExponentiate: (num)=>{
|
|
1625
|
+
let powMinusX = (num)=>bn254_Fp12.conjugate(bn254_Fp12._cyclotomicExp(num, BN_X)), r0 = bn254_Fp12.mul(bn254_Fp12.conjugate(num), bn254_Fp12.inv(num)), r = bn254_Fp12.mul(bn254_Fp12.frobeniusMap(r0, 2), r0), y1 = bn254_Fp12._cyclotomicSquare(powMinusX(r)), y2 = bn254_Fp12.mul(bn254_Fp12._cyclotomicSquare(y1), y1), y4 = powMinusX(y2), y6 = powMinusX(bn254_Fp12._cyclotomicSquare(y4)), y8 = bn254_Fp12.mul(bn254_Fp12.mul(bn254_Fp12.conjugate(y6), y4), bn254_Fp12.conjugate(y2)), y9 = bn254_Fp12.mul(y8, y1);
|
|
1626
|
+
return bn254_Fp12.mul(bn254_Fp12.frobeniusMap(bn254_Fp12.mul(bn254_Fp12.conjugate(r), y9), 3), bn254_Fp12.mul(bn254_Fp12.frobeniusMap(y8, 2), bn254_Fp12.mul(bn254_Fp12.frobeniusMap(y9, 1), bn254_Fp12.mul(bn254_Fp12.mul(y8, y4), r))));
|
|
1627
|
+
}
|
|
1628
|
+
})).Fp12, res), bn254_Fp = tower.Fp, bn254_Fp2 = tower.Fp2, getFrob = ()=>bn254_frob || (bn254_frob = function(Fp, Fp2, base) {
|
|
1629
|
+
function psi(x, y) {
|
|
1630
|
+
return [
|
|
1631
|
+
Fp2.mul(Fp2.frobeniusMap(x, 1), PSI_X),
|
|
1632
|
+
Fp2.mul(Fp2.frobeniusMap(y, 1), PSI_Y)
|
|
1633
|
+
];
|
|
1634
|
+
}
|
|
1635
|
+
function psi2(x, y) {
|
|
1636
|
+
return [
|
|
1637
|
+
Fp2.mul(x, PSI2_X),
|
|
1638
|
+
Fp2.neg(y)
|
|
1639
|
+
];
|
|
1640
|
+
}
|
|
1641
|
+
let PSI_X = Fp2.pow(base, (Fp.ORDER - tower_1n) / tower_3n), PSI_Y = Fp2.pow(base, (Fp.ORDER - tower_1n) / tower_2n), PSI2_X = Fp2.pow(base, (Fp.ORDER ** tower_2n - tower_1n) / tower_3n), PSI2_Y = Fp2.pow(base, (Fp.ORDER ** tower_2n - tower_1n) / tower_2n);
|
|
1642
|
+
if (!Fp2.eql(PSI2_Y, Fp2.neg(Fp2.ONE))) throw Error('psiFrobenius: PSI2_Y!==-1');
|
|
1643
|
+
let mapAffine = (fn)=>(c, P)=>{
|
|
1644
|
+
let affine = P.toAffine(), p = fn(affine.x, affine.y);
|
|
1645
|
+
return c.fromAffine({
|
|
1646
|
+
x: p[0],
|
|
1647
|
+
y: p[1]
|
|
1648
|
+
});
|
|
1649
|
+
}, G2psi = mapAffine(psi), G2psi2 = mapAffine(psi2);
|
|
1650
|
+
return {
|
|
1651
|
+
psi,
|
|
1652
|
+
psi2,
|
|
1653
|
+
G2psi,
|
|
1654
|
+
G2psi2,
|
|
1655
|
+
PSI_X,
|
|
1656
|
+
PSI_Y,
|
|
1657
|
+
PSI2_X,
|
|
1658
|
+
PSI2_Y
|
|
1659
|
+
};
|
|
1660
|
+
}(bn254_Fp, bn254_Fp2, bn254_Fp2.NONRESIDUE)), bn254_psi = (x, y)=>{
|
|
1661
|
+
let fn = getFrob().psi;
|
|
1662
|
+
return bn254_psi = fn, fn(x, y);
|
|
1663
|
+
}, bn254_G2psi = (c, P)=>{
|
|
1664
|
+
let fn = getFrob().G2psi;
|
|
1665
|
+
return bn254_G2psi = fn, fn(c, P);
|
|
1666
|
+
}, bn254_G2_CURVE = {
|
|
1667
|
+
p: bn254_Fp2.ORDER,
|
|
1668
|
+
n: bn254_G1_CURVE.n,
|
|
1669
|
+
h: BigInt('0x30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d'),
|
|
1670
|
+
a: bn254_Fp2.ZERO,
|
|
1671
|
+
b: Fp2B,
|
|
1672
|
+
Gx: bn254_Fp2.fromBigTuple([
|
|
1673
|
+
BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
|
|
1674
|
+
BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634')
|
|
1675
|
+
]),
|
|
1676
|
+
Gy: bn254_Fp2.fromBigTuple([
|
|
1677
|
+
BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
|
|
1678
|
+
BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531')
|
|
1679
|
+
])
|
|
1680
|
+
}, bn254 = function(fields, G1_Point, G2_Point, params) {
|
|
1681
|
+
let { Fp, Fr, Fp2, Fp6, Fp12 } = fields, G1 = {
|
|
1682
|
+
Point: G1_Point
|
|
1683
|
+
}, G2 = {
|
|
1684
|
+
Point: G2_Point
|
|
1685
|
+
}, { millerLoopBatch, pairing, pairingBatch, calcPairingPrecomputes, randomSecretKey, lengths } = function(fields, params) {
|
|
1686
|
+
let lineFunction;
|
|
1687
|
+
function pointAdd(ell, Rx, Ry, Rz, Qx, Qy) {
|
|
1688
|
+
let t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)), t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)), c0 = Fp2.sub(Fp2.mul(t0, Qx), Fp2.mul(t1, Qy)), c1 = Fp2.neg(t0);
|
|
1689
|
+
ell.push([
|
|
1690
|
+
c0,
|
|
1691
|
+
c1,
|
|
1692
|
+
t1
|
|
1693
|
+
]);
|
|
1694
|
+
let t2 = Fp2.sqr(t1), t3 = Fp2.mul(t2, t1), t4 = Fp2.mul(t2, Rx), t5 = Fp2.add(Fp2.sub(t3, Fp2.mul(t4, bls_2n)), Fp2.mul(Fp2.sqr(t0), Rz));
|
|
1695
|
+
return Rx = Fp2.mul(t1, t5), {
|
|
1696
|
+
Rx,
|
|
1697
|
+
Ry: Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4, t5), t0), Fp2.mul(t3, Ry)),
|
|
1698
|
+
Rz: Rz = Fp2.mul(Rz, t3)
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
function millerLoopBatch(pairs, withFinalExponent = !1) {
|
|
1702
|
+
let f12 = Fp12.ONE;
|
|
1703
|
+
if (pairs.length) {
|
|
1704
|
+
let ellLen = pairs[0][0].length;
|
|
1705
|
+
for(let i = 0; i < ellLen; i++)for (let [ell, Px, Py] of (f12 = Fp12.sqr(f12), pairs))for (let [c0, c1, c2] of ell[i])f12 = lineFunction(c0, c1, c2, f12, Px, Py);
|
|
1706
|
+
}
|
|
1707
|
+
return xNegative && (f12 = Fp12.conjugate(f12)), withFinalExponent ? Fp12.finalExponentiate(f12) : f12;
|
|
1708
|
+
}
|
|
1709
|
+
function pairingBatch(pairs, withFinalExponent = !0) {
|
|
1710
|
+
let res = [];
|
|
1711
|
+
for (let { g1, g2 } of pairs){
|
|
1712
|
+
if (g1.is0() || g2.is0()) throw Error('pairing is not available for ZERO point');
|
|
1713
|
+
g1.assertValidity(), g2.assertValidity();
|
|
1714
|
+
let Qa = g1.toAffine();
|
|
1715
|
+
res.push([
|
|
1716
|
+
calcPairingPrecomputes(g2),
|
|
1717
|
+
Qa.x,
|
|
1718
|
+
Qa.y
|
|
1719
|
+
]);
|
|
1720
|
+
}
|
|
1721
|
+
return millerLoopBatch(res, withFinalExponent);
|
|
1722
|
+
}
|
|
1723
|
+
let { Fr, Fp2, Fp12 } = fields, { twistType, ateLoopSize, xNegative, postPrecompute } = params;
|
|
1724
|
+
if ('multiplicative' === twistType) lineFunction = (c0, c1, c2, f, Px, Py)=>Fp12.mul014(f, c0, Fp2.mul(c1, Px), Fp2.mul(c2, Py));
|
|
1725
|
+
else if ('divisive' === twistType) lineFunction = (c0, c1, c2, f, Px, Py)=>Fp12.mul034(f, Fp2.mul(c2, Py), Fp2.mul(c1, Px), c0);
|
|
1726
|
+
else throw Error('bls: unknown twist type');
|
|
1727
|
+
let Fp2div2 = Fp2.div(Fp2.ONE, Fp2.mul(Fp2.ONE, bls_2n)), ATE_NAF = function(a) {
|
|
1728
|
+
let res = [];
|
|
1729
|
+
for(; a > bls_1n; a >>= bls_1n)(a & bls_1n) === bls_0n ? res.unshift(0) : (a & bls_3n) === bls_3n ? (res.unshift(-1), a += bls_1n) : res.unshift(1);
|
|
1730
|
+
return res;
|
|
1731
|
+
}(ateLoopSize), calcPairingPrecomputes = (point)=>{
|
|
1732
|
+
let { x, y } = point.toAffine(), negQy = Fp2.neg(y), Rx = x, Ry = y, Rz = Fp2.ONE, ell = [];
|
|
1733
|
+
for (let bit of ATE_NAF){
|
|
1734
|
+
let cur = [];
|
|
1735
|
+
({ Rx, Ry, Rz } = function(ell, Rx, Ry, Rz) {
|
|
1736
|
+
let t0 = Fp2.sqr(Ry), t1 = Fp2.sqr(Rz), t2 = Fp2.mulByB(Fp2.mul(t1, bls_3n)), t3 = Fp2.mul(t2, bls_3n), t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0), c0 = Fp2.sub(t2, t0), c1 = Fp2.mul(Fp2.sqr(Rx), bls_3n), c2 = Fp2.neg(t4);
|
|
1737
|
+
return ell.push([
|
|
1738
|
+
c0,
|
|
1739
|
+
c1,
|
|
1740
|
+
c2
|
|
1741
|
+
]), Rx = Fp2.mul(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), Fp2div2), {
|
|
1742
|
+
Rx,
|
|
1743
|
+
Ry: Ry = Fp2.sub(Fp2.sqr(Fp2.mul(Fp2.add(t0, t3), Fp2div2)), Fp2.mul(Fp2.sqr(t2), bls_3n)),
|
|
1744
|
+
Rz: Rz = Fp2.mul(t0, t4)
|
|
1745
|
+
};
|
|
1746
|
+
}(cur, Rx, Ry, Rz)), bit && ({ Rx, Ry, Rz } = pointAdd(cur, Rx, Ry, Rz, x, -1 === bit ? negQy : y)), ell.push(cur);
|
|
1747
|
+
}
|
|
1748
|
+
if (postPrecompute) {
|
|
1749
|
+
let last = ell[ell.length - 1];
|
|
1750
|
+
postPrecompute(Rx, Ry, Rz, x, y, pointAdd.bind(null, last));
|
|
1751
|
+
}
|
|
1752
|
+
return ell;
|
|
1753
|
+
}, lengths = {
|
|
1754
|
+
seed: modular_getMinHashLength(Fr.ORDER)
|
|
1755
|
+
}, rand = void 0 === params.randomBytes ? curves_utils_randomBytes : params.randomBytes;
|
|
1756
|
+
return Object.freeze(lengths), {
|
|
1757
|
+
lengths,
|
|
1758
|
+
Fr,
|
|
1759
|
+
Fp12,
|
|
1760
|
+
millerLoopBatch,
|
|
1761
|
+
pairing: function(Q, P, withFinalExponent = !0) {
|
|
1762
|
+
return pairingBatch([
|
|
1763
|
+
{
|
|
1764
|
+
g1: Q,
|
|
1765
|
+
g2: P
|
|
1766
|
+
}
|
|
1767
|
+
], withFinalExponent);
|
|
1768
|
+
},
|
|
1769
|
+
pairingBatch,
|
|
1770
|
+
calcPairingPrecomputes,
|
|
1771
|
+
randomSecretKey: (seed)=>(curves_utils_abytes(seed = void 0 === seed ? rand(lengths.seed) : seed, lengths.seed, 'seed'), function(key, fieldOrder, isLE = !1) {
|
|
1772
|
+
curves_utils_abytes(key);
|
|
1773
|
+
let len = key.length, fieldLen = getFieldBytesLength(fieldOrder), minLen = Math.max(modular_getMinHashLength(fieldOrder), 16);
|
|
1774
|
+
if (len < minLen || len > 1024) throw Error('expected ' + minLen + '-1024 bytes of input, got ' + len);
|
|
1775
|
+
let reduced = mod(isLE ? bytesToNumberLE(key) : utils_bytesToNumberBE(key), fieldOrder - modular_1n) + modular_1n;
|
|
1776
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
1777
|
+
}(seed, Fr.ORDER))
|
|
1778
|
+
};
|
|
1779
|
+
}(fields, params);
|
|
1780
|
+
return G1.Point.BASE.precompute(4), Object.freeze(G1), Object.freeze(G2), Object.freeze({
|
|
1781
|
+
lengths: Object.freeze(lengths),
|
|
1782
|
+
millerLoopBatch,
|
|
1783
|
+
pairing,
|
|
1784
|
+
pairingBatch,
|
|
1785
|
+
G1,
|
|
1786
|
+
G2,
|
|
1787
|
+
fields: Object.freeze({
|
|
1788
|
+
Fr,
|
|
1789
|
+
Fp,
|
|
1790
|
+
Fp2,
|
|
1791
|
+
Fp6,
|
|
1792
|
+
Fp12
|
|
1793
|
+
}),
|
|
1794
|
+
params: Object.freeze({
|
|
1795
|
+
ateLoopSize: params.ateLoopSize,
|
|
1796
|
+
twistType: params.twistType
|
|
1797
|
+
}),
|
|
1798
|
+
utils: Object.freeze({
|
|
1799
|
+
randomSecretKey,
|
|
1800
|
+
calcPairingPrecomputes
|
|
1801
|
+
})
|
|
1802
|
+
});
|
|
1803
|
+
}({
|
|
1804
|
+
Fp: bn254_Fp,
|
|
1805
|
+
Fp2: bn254_Fp2,
|
|
1806
|
+
Fp6: tower.Fp6,
|
|
1807
|
+
Fp12: bn254_Fp12,
|
|
1808
|
+
Fr: bn254_Fr
|
|
1809
|
+
}, weierstrass(bn254_G1_CURVE, {
|
|
1810
|
+
Fp: bn254_Fp,
|
|
1811
|
+
Fn: bn254_Fr,
|
|
1812
|
+
allowInfinityPoint: !0
|
|
1813
|
+
}), weierstrass(bn254_G2_CURVE, {
|
|
1814
|
+
Fp: bn254_Fp2,
|
|
1815
|
+
Fn: bn254_Fr,
|
|
1816
|
+
allowInfinityPoint: !0,
|
|
1817
|
+
isTorsionFree: (c, P)=>P.multiplyUnsafe(SIX_X_SQUARED).equals(bn254_G2psi(c, P))
|
|
1818
|
+
}), {
|
|
1819
|
+
ateLoopSize: BN_X * bn254_6n + bn254_2n,
|
|
1820
|
+
r: bn254_Fr.ORDER,
|
|
1821
|
+
xNegative: !1,
|
|
1822
|
+
twistType: 'divisive',
|
|
1823
|
+
postPrecompute: (Rx, Ry, Rz, Qx, Qy, pointAdd)=>{
|
|
1824
|
+
let q = bn254_psi(Qx, Qy);
|
|
1825
|
+
({ Rx, Ry, Rz } = pointAdd(Rx, Ry, Rz, q[0], q[1]));
|
|
1826
|
+
let q2 = bn254_psi(q[0], q[1]);
|
|
1827
|
+
pointAdd(Rx, Ry, Rz, q2[0], bn254_Fp2.neg(q2[1]));
|
|
1828
|
+
}
|
|
1829
|
+
}), checkChunks = (numChunks, name = 'numChunks')=>{
|
|
1830
|
+
if ('number' != typeof numChunks) throw TypeError(`expected ${name} number, got ${typeof numChunks}`);
|
|
1831
|
+
if (!Number.isSafeInteger(numChunks) || numChunks <= 0) throw RangeError(`${name} must be > 0`);
|
|
1832
|
+
return numChunks;
|
|
1833
|
+
}, getConcurrencyFromPlatform = (platform)=>{
|
|
1834
|
+
let cpus = platform.cpus();
|
|
1835
|
+
return void 0 === cpus ? 1 : cpus;
|
|
1836
|
+
}, wrkr = Object.freeze({
|
|
1837
|
+
getConcurrency: ()=>getConcurrencyFromPlatform(platform),
|
|
1838
|
+
initWorker: (platform = {
|
|
1839
|
+
cpus: ()=>external_node_os_cpus().length,
|
|
1840
|
+
initWorker (handlers) {
|
|
1841
|
+
__rspack_external_node_worker_threads_28ca7740.parentPort?.on('message', (msg)=>{
|
|
1842
|
+
let { id, fn, payload } = msg, pp = __rspack_external_node_worker_threads_28ca7740.parentPort;
|
|
1843
|
+
if (!pp) throw Error('expected parentPort in worker');
|
|
1844
|
+
try {
|
|
1845
|
+
let res = handlers[fn](payload);
|
|
1846
|
+
pp.postMessage({
|
|
1847
|
+
id,
|
|
1848
|
+
res
|
|
1849
|
+
});
|
|
1850
|
+
} catch (e) {
|
|
1851
|
+
pp.postMessage({
|
|
1852
|
+
id,
|
|
1853
|
+
err: '' + (e instanceof Error ? e.message : e)
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
});
|
|
1857
|
+
},
|
|
1858
|
+
createWorker (getWorker, onMessage, onError) {
|
|
1859
|
+
let worker;
|
|
1860
|
+
if ("u" > typeof Worker) throw Error('Worker defined on node');
|
|
1861
|
+
globalThis.Worker = class {
|
|
1862
|
+
constructor(fileUrl){
|
|
1863
|
+
worker = new __rspack_external_node_worker_threads_28ca7740.Worker(fileUrl);
|
|
1864
|
+
}
|
|
1865
|
+
};
|
|
1866
|
+
try {
|
|
1867
|
+
getWorker();
|
|
1868
|
+
} finally{
|
|
1869
|
+
delete globalThis.Worker;
|
|
1870
|
+
}
|
|
1871
|
+
return worker.on('message', onMessage), worker.on('error', (err)=>onError(err.message)), {
|
|
1872
|
+
send: (msg)=>worker.postMessage(msg),
|
|
1873
|
+
terminate: ()=>worker.terminate()
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
}).initWorker,
|
|
1877
|
+
initBatch: (getWorker, reducers, threads)=>(function(platform, getWorker, reducers, threads) {
|
|
1878
|
+
null == threads && (threads = getConcurrencyFromPlatform(platform));
|
|
1879
|
+
let id = 0, workers = [], WAIT = {}, errHandler = (err)=>{
|
|
1880
|
+
for(let i in WAIT)WAIT[i].reject(err);
|
|
1881
|
+
}, msgHandler = (msg)=>{
|
|
1882
|
+
let { id, res, err } = msg, handler = WAIT[id];
|
|
1883
|
+
if (delete WAIT[id], !handler) throw Error('unknown id');
|
|
1884
|
+
void 0 !== err ? handler.reject(Error(err)) : handler.resolve(res);
|
|
1885
|
+
};
|
|
1886
|
+
for(let i = 0; i < threads; i++)workers.push(platform.createWorker(getWorker, msgHandler, errHandler));
|
|
1887
|
+
let methods = {};
|
|
1888
|
+
for(let fn in reducers)methods[fn] = async (input, _threads)=>{
|
|
1889
|
+
let requested = checkChunks(void 0 !== _threads ? _threads : threads, 'threads'), promises = (function(list, numChunks) {
|
|
1890
|
+
numChunks = checkChunks(numChunks);
|
|
1891
|
+
let chunkSize = Math.ceil(list.length / numChunks), res = [];
|
|
1892
|
+
for(let i = 0; i < list.length; i += chunkSize)res.push(list.slice(i, i + chunkSize));
|
|
1893
|
+
return res;
|
|
1894
|
+
})(input, requested > workers.length ? workers.length : requested).map((chunk, i)=>{
|
|
1895
|
+
let currId = id++, thread = workers[i], p = new Promise((resolve, reject)=>{
|
|
1896
|
+
void 0 !== WAIT[currId] && reject(Error('worker: id re-use')), WAIT[currId] = {
|
|
1897
|
+
resolve,
|
|
1898
|
+
reject
|
|
1899
|
+
};
|
|
1900
|
+
});
|
|
1901
|
+
return thread.send({
|
|
1902
|
+
id: currId,
|
|
1903
|
+
fn,
|
|
1904
|
+
payload: chunk
|
|
1905
|
+
}), p;
|
|
1906
|
+
}), results = await Promise.all(promises), reducer = reducers[fn];
|
|
1907
|
+
return reducer ? reducer(results) : results.flat(1);
|
|
1908
|
+
};
|
|
1909
|
+
return {
|
|
1910
|
+
methods,
|
|
1911
|
+
terminate: ()=>{
|
|
1912
|
+
for (let w of (errHandler(Error('worker stopped')), workers))w.terminate();
|
|
1913
|
+
}
|
|
1914
|
+
};
|
|
1915
|
+
})(platform, getWorker, reducers, threads)
|
|
1916
|
+
});
|
|
1917
|
+
export { FpPow, aexists, aoutput, asafenumber, asciiToBytes, bitGet, bn254, bytesToNumberLE, clean, createCurveFields, createView, curve_createKeygen, curves_utils_abytes, curves_utils_bytesToHex, curves_utils_concatBytes, curves_utils_hexToBytes, curves_utils_isBytes, curves_utils_randomBytes, equalBytes, isNegativeLE, mod, modular_FpInvertBatch, modular_validateField, normalizeZ, numberToBytesLE, oidNist, pippenger, pow2, rotr, utils_aInRange, utils_abool, utils_abytes, utils_bytesToNumberBE, utils_copyBytes, utils_createHasher, utils_hexToBytes, utils_notImplemented, utils_randomBytes, utils_validateObject, wNAF, wrkr };
|