@grapenpm/gpass-sdk 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/README.md +251 -0
- package/dist/index.d.mts +1041 -0
- package/dist/index.d.ts +1041 -0
- package/dist/index.js +3609 -0
- package/dist/index.mjs +3592 -0
- package/package.json +50 -0
- package/src/bn-js.d.ts +18 -0
- package/src/client.ts +349 -0
- package/src/idl.ts +531 -0
- package/src/index.ts +44 -0
- package/src/pda.ts +125 -0
- package/src/types/index.ts +294 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3609 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// node_modules/bn.js/lib/bn.js
|
|
34
|
+
var require_bn = __commonJS({
|
|
35
|
+
"node_modules/bn.js/lib/bn.js"(exports2, module2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
(function(module3, exports3) {
|
|
38
|
+
"use strict";
|
|
39
|
+
function assert(val, msg) {
|
|
40
|
+
if (!val) throw new Error(msg || "Assertion failed");
|
|
41
|
+
}
|
|
42
|
+
function inherits(ctor, superCtor) {
|
|
43
|
+
ctor.super_ = superCtor;
|
|
44
|
+
var TempCtor = function() {
|
|
45
|
+
};
|
|
46
|
+
TempCtor.prototype = superCtor.prototype;
|
|
47
|
+
ctor.prototype = new TempCtor();
|
|
48
|
+
ctor.prototype.constructor = ctor;
|
|
49
|
+
}
|
|
50
|
+
function BN3(number, base, endian) {
|
|
51
|
+
if (BN3.isBN(number)) {
|
|
52
|
+
return number;
|
|
53
|
+
}
|
|
54
|
+
this.negative = 0;
|
|
55
|
+
this.words = null;
|
|
56
|
+
this.length = 0;
|
|
57
|
+
this.red = null;
|
|
58
|
+
if (number !== null) {
|
|
59
|
+
if (base === "le" || base === "be") {
|
|
60
|
+
endian = base;
|
|
61
|
+
base = 10;
|
|
62
|
+
}
|
|
63
|
+
this._init(number || 0, base || 10, endian || "be");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (typeof module3 === "object") {
|
|
67
|
+
module3.exports = BN3;
|
|
68
|
+
} else {
|
|
69
|
+
exports3.BN = BN3;
|
|
70
|
+
}
|
|
71
|
+
BN3.BN = BN3;
|
|
72
|
+
BN3.wordSize = 26;
|
|
73
|
+
var Buffer2;
|
|
74
|
+
try {
|
|
75
|
+
if (typeof window !== "undefined" && typeof window.Buffer !== "undefined") {
|
|
76
|
+
Buffer2 = window.Buffer;
|
|
77
|
+
} else {
|
|
78
|
+
Buffer2 = require("buffer").Buffer;
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
}
|
|
82
|
+
BN3.isBN = function isBN(num) {
|
|
83
|
+
if (num instanceof BN3) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
return num !== null && typeof num === "object" && num.constructor.wordSize === BN3.wordSize && Array.isArray(num.words);
|
|
87
|
+
};
|
|
88
|
+
BN3.max = function max(left, right) {
|
|
89
|
+
if (left.cmp(right) > 0) return left;
|
|
90
|
+
return right;
|
|
91
|
+
};
|
|
92
|
+
BN3.min = function min(left, right) {
|
|
93
|
+
if (left.cmp(right) < 0) return left;
|
|
94
|
+
return right;
|
|
95
|
+
};
|
|
96
|
+
BN3.prototype._init = function init(number, base, endian) {
|
|
97
|
+
if (typeof number === "number") {
|
|
98
|
+
return this._initNumber(number, base, endian);
|
|
99
|
+
}
|
|
100
|
+
if (typeof number === "object") {
|
|
101
|
+
return this._initArray(number, base, endian);
|
|
102
|
+
}
|
|
103
|
+
if (base === "hex") {
|
|
104
|
+
base = 16;
|
|
105
|
+
}
|
|
106
|
+
assert(base === (base | 0) && base >= 2 && base <= 36);
|
|
107
|
+
number = number.toString().replace(/\s+/g, "");
|
|
108
|
+
var start = 0;
|
|
109
|
+
if (number[0] === "-") {
|
|
110
|
+
start++;
|
|
111
|
+
this.negative = 1;
|
|
112
|
+
}
|
|
113
|
+
if (start < number.length) {
|
|
114
|
+
if (base === 16) {
|
|
115
|
+
this._parseHex(number, start, endian);
|
|
116
|
+
} else {
|
|
117
|
+
this._parseBase(number, base, start);
|
|
118
|
+
if (endian === "le") {
|
|
119
|
+
this._initArray(this.toArray(), base, endian);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
BN3.prototype._initNumber = function _initNumber(number, base, endian) {
|
|
125
|
+
if (number < 0) {
|
|
126
|
+
this.negative = 1;
|
|
127
|
+
number = -number;
|
|
128
|
+
}
|
|
129
|
+
if (number < 67108864) {
|
|
130
|
+
this.words = [number & 67108863];
|
|
131
|
+
this.length = 1;
|
|
132
|
+
} else if (number < 4503599627370496) {
|
|
133
|
+
this.words = [
|
|
134
|
+
number & 67108863,
|
|
135
|
+
number / 67108864 & 67108863
|
|
136
|
+
];
|
|
137
|
+
this.length = 2;
|
|
138
|
+
} else {
|
|
139
|
+
assert(number < 9007199254740992);
|
|
140
|
+
this.words = [
|
|
141
|
+
number & 67108863,
|
|
142
|
+
number / 67108864 & 67108863,
|
|
143
|
+
1
|
|
144
|
+
];
|
|
145
|
+
this.length = 3;
|
|
146
|
+
}
|
|
147
|
+
if (endian !== "le") return;
|
|
148
|
+
this._initArray(this.toArray(), base, endian);
|
|
149
|
+
};
|
|
150
|
+
BN3.prototype._initArray = function _initArray(number, base, endian) {
|
|
151
|
+
assert(typeof number.length === "number");
|
|
152
|
+
if (number.length <= 0) {
|
|
153
|
+
this.words = [0];
|
|
154
|
+
this.length = 1;
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
this.length = Math.ceil(number.length / 3);
|
|
158
|
+
this.words = new Array(this.length);
|
|
159
|
+
for (var i = 0; i < this.length; i++) {
|
|
160
|
+
this.words[i] = 0;
|
|
161
|
+
}
|
|
162
|
+
var j, w;
|
|
163
|
+
var off = 0;
|
|
164
|
+
if (endian === "be") {
|
|
165
|
+
for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
|
|
166
|
+
w = number[i] | number[i - 1] << 8 | number[i - 2] << 16;
|
|
167
|
+
this.words[j] |= w << off & 67108863;
|
|
168
|
+
this.words[j + 1] = w >>> 26 - off & 67108863;
|
|
169
|
+
off += 24;
|
|
170
|
+
if (off >= 26) {
|
|
171
|
+
off -= 26;
|
|
172
|
+
j++;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
} else if (endian === "le") {
|
|
176
|
+
for (i = 0, j = 0; i < number.length; i += 3) {
|
|
177
|
+
w = number[i] | number[i + 1] << 8 | number[i + 2] << 16;
|
|
178
|
+
this.words[j] |= w << off & 67108863;
|
|
179
|
+
this.words[j + 1] = w >>> 26 - off & 67108863;
|
|
180
|
+
off += 24;
|
|
181
|
+
if (off >= 26) {
|
|
182
|
+
off -= 26;
|
|
183
|
+
j++;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return this._strip();
|
|
188
|
+
};
|
|
189
|
+
function parseHex4Bits(string, index) {
|
|
190
|
+
var c = string.charCodeAt(index);
|
|
191
|
+
if (c >= 48 && c <= 57) {
|
|
192
|
+
return c - 48;
|
|
193
|
+
} else if (c >= 65 && c <= 70) {
|
|
194
|
+
return c - 55;
|
|
195
|
+
} else if (c >= 97 && c <= 102) {
|
|
196
|
+
return c - 87;
|
|
197
|
+
} else {
|
|
198
|
+
assert(false, "Invalid character in " + string);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function parseHexByte(string, lowerBound, index) {
|
|
202
|
+
var r = parseHex4Bits(string, index);
|
|
203
|
+
if (index - 1 >= lowerBound) {
|
|
204
|
+
r |= parseHex4Bits(string, index - 1) << 4;
|
|
205
|
+
}
|
|
206
|
+
return r;
|
|
207
|
+
}
|
|
208
|
+
BN3.prototype._parseHex = function _parseHex(number, start, endian) {
|
|
209
|
+
this.length = Math.ceil((number.length - start) / 6);
|
|
210
|
+
this.words = new Array(this.length);
|
|
211
|
+
for (var i = 0; i < this.length; i++) {
|
|
212
|
+
this.words[i] = 0;
|
|
213
|
+
}
|
|
214
|
+
var off = 0;
|
|
215
|
+
var j = 0;
|
|
216
|
+
var w;
|
|
217
|
+
if (endian === "be") {
|
|
218
|
+
for (i = number.length - 1; i >= start; i -= 2) {
|
|
219
|
+
w = parseHexByte(number, start, i) << off;
|
|
220
|
+
this.words[j] |= w & 67108863;
|
|
221
|
+
if (off >= 18) {
|
|
222
|
+
off -= 18;
|
|
223
|
+
j += 1;
|
|
224
|
+
this.words[j] |= w >>> 26;
|
|
225
|
+
} else {
|
|
226
|
+
off += 8;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
var parseLength = number.length - start;
|
|
231
|
+
for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
|
|
232
|
+
w = parseHexByte(number, start, i) << off;
|
|
233
|
+
this.words[j] |= w & 67108863;
|
|
234
|
+
if (off >= 18) {
|
|
235
|
+
off -= 18;
|
|
236
|
+
j += 1;
|
|
237
|
+
this.words[j] |= w >>> 26;
|
|
238
|
+
} else {
|
|
239
|
+
off += 8;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
this._strip();
|
|
244
|
+
};
|
|
245
|
+
function parseBase(str, start, end, mul) {
|
|
246
|
+
var r = 0;
|
|
247
|
+
var b = 0;
|
|
248
|
+
var len = Math.min(str.length, end);
|
|
249
|
+
for (var i = start; i < len; i++) {
|
|
250
|
+
var c = str.charCodeAt(i) - 48;
|
|
251
|
+
r *= mul;
|
|
252
|
+
if (c >= 49) {
|
|
253
|
+
b = c - 49 + 10;
|
|
254
|
+
} else if (c >= 17) {
|
|
255
|
+
b = c - 17 + 10;
|
|
256
|
+
} else {
|
|
257
|
+
b = c;
|
|
258
|
+
}
|
|
259
|
+
assert(c >= 0 && b < mul, "Invalid character");
|
|
260
|
+
r += b;
|
|
261
|
+
}
|
|
262
|
+
return r;
|
|
263
|
+
}
|
|
264
|
+
BN3.prototype._parseBase = function _parseBase(number, base, start) {
|
|
265
|
+
this.words = [0];
|
|
266
|
+
this.length = 1;
|
|
267
|
+
for (var limbLen = 0, limbPow = 1; limbPow <= 67108863; limbPow *= base) {
|
|
268
|
+
limbLen++;
|
|
269
|
+
}
|
|
270
|
+
limbLen--;
|
|
271
|
+
limbPow = limbPow / base | 0;
|
|
272
|
+
var total = number.length - start;
|
|
273
|
+
var mod = total % limbLen;
|
|
274
|
+
var end = Math.min(total, total - mod) + start;
|
|
275
|
+
var word = 0;
|
|
276
|
+
for (var i = start; i < end; i += limbLen) {
|
|
277
|
+
word = parseBase(number, i, i + limbLen, base);
|
|
278
|
+
this.imuln(limbPow);
|
|
279
|
+
if (this.words[0] + word < 67108864) {
|
|
280
|
+
this.words[0] += word;
|
|
281
|
+
} else {
|
|
282
|
+
this._iaddn(word);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (mod !== 0) {
|
|
286
|
+
var pow = 1;
|
|
287
|
+
word = parseBase(number, i, number.length, base);
|
|
288
|
+
for (i = 0; i < mod; i++) {
|
|
289
|
+
pow *= base;
|
|
290
|
+
}
|
|
291
|
+
this.imuln(pow);
|
|
292
|
+
if (this.words[0] + word < 67108864) {
|
|
293
|
+
this.words[0] += word;
|
|
294
|
+
} else {
|
|
295
|
+
this._iaddn(word);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
this._strip();
|
|
299
|
+
};
|
|
300
|
+
BN3.prototype.copy = function copy(dest) {
|
|
301
|
+
dest.words = new Array(this.length);
|
|
302
|
+
for (var i = 0; i < this.length; i++) {
|
|
303
|
+
dest.words[i] = this.words[i];
|
|
304
|
+
}
|
|
305
|
+
dest.length = this.length;
|
|
306
|
+
dest.negative = this.negative;
|
|
307
|
+
dest.red = this.red;
|
|
308
|
+
};
|
|
309
|
+
function move(dest, src) {
|
|
310
|
+
dest.words = src.words;
|
|
311
|
+
dest.length = src.length;
|
|
312
|
+
dest.negative = src.negative;
|
|
313
|
+
dest.red = src.red;
|
|
314
|
+
}
|
|
315
|
+
BN3.prototype._move = function _move(dest) {
|
|
316
|
+
move(dest, this);
|
|
317
|
+
};
|
|
318
|
+
BN3.prototype.clone = function clone() {
|
|
319
|
+
var r = new BN3(null);
|
|
320
|
+
this.copy(r);
|
|
321
|
+
return r;
|
|
322
|
+
};
|
|
323
|
+
BN3.prototype._expand = function _expand(size) {
|
|
324
|
+
while (this.length < size) {
|
|
325
|
+
this.words[this.length++] = 0;
|
|
326
|
+
}
|
|
327
|
+
return this;
|
|
328
|
+
};
|
|
329
|
+
BN3.prototype._strip = function strip() {
|
|
330
|
+
while (this.length > 1 && this.words[this.length - 1] === 0) {
|
|
331
|
+
this.length--;
|
|
332
|
+
}
|
|
333
|
+
return this._normSign();
|
|
334
|
+
};
|
|
335
|
+
BN3.prototype._normSign = function _normSign() {
|
|
336
|
+
if (this.length === 1 && this.words[0] === 0) {
|
|
337
|
+
this.negative = 0;
|
|
338
|
+
}
|
|
339
|
+
return this;
|
|
340
|
+
};
|
|
341
|
+
if (typeof Symbol !== "undefined" && typeof Symbol.for === "function") {
|
|
342
|
+
try {
|
|
343
|
+
BN3.prototype[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")] = inspect;
|
|
344
|
+
} catch (e) {
|
|
345
|
+
BN3.prototype.inspect = inspect;
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
BN3.prototype.inspect = inspect;
|
|
349
|
+
}
|
|
350
|
+
function inspect() {
|
|
351
|
+
return (this.red ? "<BN-R: " : "<BN: ") + this.toString(16) + ">";
|
|
352
|
+
}
|
|
353
|
+
var zeros = [
|
|
354
|
+
"",
|
|
355
|
+
"0",
|
|
356
|
+
"00",
|
|
357
|
+
"000",
|
|
358
|
+
"0000",
|
|
359
|
+
"00000",
|
|
360
|
+
"000000",
|
|
361
|
+
"0000000",
|
|
362
|
+
"00000000",
|
|
363
|
+
"000000000",
|
|
364
|
+
"0000000000",
|
|
365
|
+
"00000000000",
|
|
366
|
+
"000000000000",
|
|
367
|
+
"0000000000000",
|
|
368
|
+
"00000000000000",
|
|
369
|
+
"000000000000000",
|
|
370
|
+
"0000000000000000",
|
|
371
|
+
"00000000000000000",
|
|
372
|
+
"000000000000000000",
|
|
373
|
+
"0000000000000000000",
|
|
374
|
+
"00000000000000000000",
|
|
375
|
+
"000000000000000000000",
|
|
376
|
+
"0000000000000000000000",
|
|
377
|
+
"00000000000000000000000",
|
|
378
|
+
"000000000000000000000000",
|
|
379
|
+
"0000000000000000000000000"
|
|
380
|
+
];
|
|
381
|
+
var groupSizes = [
|
|
382
|
+
0,
|
|
383
|
+
0,
|
|
384
|
+
25,
|
|
385
|
+
16,
|
|
386
|
+
12,
|
|
387
|
+
11,
|
|
388
|
+
10,
|
|
389
|
+
9,
|
|
390
|
+
8,
|
|
391
|
+
8,
|
|
392
|
+
7,
|
|
393
|
+
7,
|
|
394
|
+
7,
|
|
395
|
+
7,
|
|
396
|
+
6,
|
|
397
|
+
6,
|
|
398
|
+
6,
|
|
399
|
+
6,
|
|
400
|
+
6,
|
|
401
|
+
6,
|
|
402
|
+
6,
|
|
403
|
+
5,
|
|
404
|
+
5,
|
|
405
|
+
5,
|
|
406
|
+
5,
|
|
407
|
+
5,
|
|
408
|
+
5,
|
|
409
|
+
5,
|
|
410
|
+
5,
|
|
411
|
+
5,
|
|
412
|
+
5,
|
|
413
|
+
5,
|
|
414
|
+
5,
|
|
415
|
+
5,
|
|
416
|
+
5,
|
|
417
|
+
5,
|
|
418
|
+
5
|
|
419
|
+
];
|
|
420
|
+
var groupBases = [
|
|
421
|
+
0,
|
|
422
|
+
0,
|
|
423
|
+
33554432,
|
|
424
|
+
43046721,
|
|
425
|
+
16777216,
|
|
426
|
+
48828125,
|
|
427
|
+
60466176,
|
|
428
|
+
40353607,
|
|
429
|
+
16777216,
|
|
430
|
+
43046721,
|
|
431
|
+
1e7,
|
|
432
|
+
19487171,
|
|
433
|
+
35831808,
|
|
434
|
+
62748517,
|
|
435
|
+
7529536,
|
|
436
|
+
11390625,
|
|
437
|
+
16777216,
|
|
438
|
+
24137569,
|
|
439
|
+
34012224,
|
|
440
|
+
47045881,
|
|
441
|
+
64e6,
|
|
442
|
+
4084101,
|
|
443
|
+
5153632,
|
|
444
|
+
6436343,
|
|
445
|
+
7962624,
|
|
446
|
+
9765625,
|
|
447
|
+
11881376,
|
|
448
|
+
14348907,
|
|
449
|
+
17210368,
|
|
450
|
+
20511149,
|
|
451
|
+
243e5,
|
|
452
|
+
28629151,
|
|
453
|
+
33554432,
|
|
454
|
+
39135393,
|
|
455
|
+
45435424,
|
|
456
|
+
52521875,
|
|
457
|
+
60466176
|
|
458
|
+
];
|
|
459
|
+
BN3.prototype.toString = function toString(base, padding) {
|
|
460
|
+
base = base || 10;
|
|
461
|
+
padding = padding | 0 || 1;
|
|
462
|
+
var out;
|
|
463
|
+
if (base === 16 || base === "hex") {
|
|
464
|
+
out = "";
|
|
465
|
+
var off = 0;
|
|
466
|
+
var carry = 0;
|
|
467
|
+
for (var i = 0; i < this.length; i++) {
|
|
468
|
+
var w = this.words[i];
|
|
469
|
+
var word = ((w << off | carry) & 16777215).toString(16);
|
|
470
|
+
carry = w >>> 24 - off & 16777215;
|
|
471
|
+
off += 2;
|
|
472
|
+
if (off >= 26) {
|
|
473
|
+
off -= 26;
|
|
474
|
+
i--;
|
|
475
|
+
}
|
|
476
|
+
if (carry !== 0 || i !== this.length - 1) {
|
|
477
|
+
out = zeros[6 - word.length] + word + out;
|
|
478
|
+
} else {
|
|
479
|
+
out = word + out;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (carry !== 0) {
|
|
483
|
+
out = carry.toString(16) + out;
|
|
484
|
+
}
|
|
485
|
+
while (out.length % padding !== 0) {
|
|
486
|
+
out = "0" + out;
|
|
487
|
+
}
|
|
488
|
+
if (this.negative !== 0) {
|
|
489
|
+
out = "-" + out;
|
|
490
|
+
}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
if (base === (base | 0) && base >= 2 && base <= 36) {
|
|
494
|
+
var groupSize = groupSizes[base];
|
|
495
|
+
var groupBase = groupBases[base];
|
|
496
|
+
out = "";
|
|
497
|
+
var c = this.clone();
|
|
498
|
+
c.negative = 0;
|
|
499
|
+
while (!c.isZero()) {
|
|
500
|
+
var r = c.modrn(groupBase).toString(base);
|
|
501
|
+
c = c.idivn(groupBase);
|
|
502
|
+
if (!c.isZero()) {
|
|
503
|
+
out = zeros[groupSize - r.length] + r + out;
|
|
504
|
+
} else {
|
|
505
|
+
out = r + out;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (this.isZero()) {
|
|
509
|
+
out = "0" + out;
|
|
510
|
+
}
|
|
511
|
+
while (out.length % padding !== 0) {
|
|
512
|
+
out = "0" + out;
|
|
513
|
+
}
|
|
514
|
+
if (this.negative !== 0) {
|
|
515
|
+
out = "-" + out;
|
|
516
|
+
}
|
|
517
|
+
return out;
|
|
518
|
+
}
|
|
519
|
+
assert(false, "Base should be between 2 and 36");
|
|
520
|
+
};
|
|
521
|
+
BN3.prototype.toNumber = function toNumber() {
|
|
522
|
+
var ret = this.words[0];
|
|
523
|
+
if (this.length === 2) {
|
|
524
|
+
ret += this.words[1] * 67108864;
|
|
525
|
+
} else if (this.length === 3 && this.words[2] === 1) {
|
|
526
|
+
ret += 4503599627370496 + this.words[1] * 67108864;
|
|
527
|
+
} else if (this.length > 2) {
|
|
528
|
+
assert(false, "Number can only safely store up to 53 bits");
|
|
529
|
+
}
|
|
530
|
+
return this.negative !== 0 ? -ret : ret;
|
|
531
|
+
};
|
|
532
|
+
BN3.prototype.toJSON = function toJSON() {
|
|
533
|
+
return this.toString(16, 2);
|
|
534
|
+
};
|
|
535
|
+
if (Buffer2) {
|
|
536
|
+
BN3.prototype.toBuffer = function toBuffer(endian, length) {
|
|
537
|
+
return this.toArrayLike(Buffer2, endian, length);
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
BN3.prototype.toArray = function toArray(endian, length) {
|
|
541
|
+
return this.toArrayLike(Array, endian, length);
|
|
542
|
+
};
|
|
543
|
+
var allocate = function allocate2(ArrayType, size) {
|
|
544
|
+
if (ArrayType.allocUnsafe) {
|
|
545
|
+
return ArrayType.allocUnsafe(size);
|
|
546
|
+
}
|
|
547
|
+
return new ArrayType(size);
|
|
548
|
+
};
|
|
549
|
+
BN3.prototype.toArrayLike = function toArrayLike(ArrayType, endian, length) {
|
|
550
|
+
this._strip();
|
|
551
|
+
var byteLength = this.byteLength();
|
|
552
|
+
var reqLength = length || Math.max(1, byteLength);
|
|
553
|
+
assert(byteLength <= reqLength, "byte array longer than desired length");
|
|
554
|
+
assert(reqLength > 0, "Requested array length <= 0");
|
|
555
|
+
var res = allocate(ArrayType, reqLength);
|
|
556
|
+
var postfix = endian === "le" ? "LE" : "BE";
|
|
557
|
+
this["_toArrayLike" + postfix](res, byteLength);
|
|
558
|
+
return res;
|
|
559
|
+
};
|
|
560
|
+
BN3.prototype._toArrayLikeLE = function _toArrayLikeLE(res, byteLength) {
|
|
561
|
+
var position = 0;
|
|
562
|
+
var carry = 0;
|
|
563
|
+
for (var i = 0, shift = 0; i < this.length; i++) {
|
|
564
|
+
var word = this.words[i] << shift | carry;
|
|
565
|
+
res[position++] = word & 255;
|
|
566
|
+
if (position < res.length) {
|
|
567
|
+
res[position++] = word >> 8 & 255;
|
|
568
|
+
}
|
|
569
|
+
if (position < res.length) {
|
|
570
|
+
res[position++] = word >> 16 & 255;
|
|
571
|
+
}
|
|
572
|
+
if (shift === 6) {
|
|
573
|
+
if (position < res.length) {
|
|
574
|
+
res[position++] = word >> 24 & 255;
|
|
575
|
+
}
|
|
576
|
+
carry = 0;
|
|
577
|
+
shift = 0;
|
|
578
|
+
} else {
|
|
579
|
+
carry = word >>> 24;
|
|
580
|
+
shift += 2;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (position < res.length) {
|
|
584
|
+
res[position++] = carry;
|
|
585
|
+
while (position < res.length) {
|
|
586
|
+
res[position++] = 0;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
BN3.prototype._toArrayLikeBE = function _toArrayLikeBE(res, byteLength) {
|
|
591
|
+
var position = res.length - 1;
|
|
592
|
+
var carry = 0;
|
|
593
|
+
for (var i = 0, shift = 0; i < this.length; i++) {
|
|
594
|
+
var word = this.words[i] << shift | carry;
|
|
595
|
+
res[position--] = word & 255;
|
|
596
|
+
if (position >= 0) {
|
|
597
|
+
res[position--] = word >> 8 & 255;
|
|
598
|
+
}
|
|
599
|
+
if (position >= 0) {
|
|
600
|
+
res[position--] = word >> 16 & 255;
|
|
601
|
+
}
|
|
602
|
+
if (shift === 6) {
|
|
603
|
+
if (position >= 0) {
|
|
604
|
+
res[position--] = word >> 24 & 255;
|
|
605
|
+
}
|
|
606
|
+
carry = 0;
|
|
607
|
+
shift = 0;
|
|
608
|
+
} else {
|
|
609
|
+
carry = word >>> 24;
|
|
610
|
+
shift += 2;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (position >= 0) {
|
|
614
|
+
res[position--] = carry;
|
|
615
|
+
while (position >= 0) {
|
|
616
|
+
res[position--] = 0;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
if (Math.clz32) {
|
|
621
|
+
BN3.prototype._countBits = function _countBits(w) {
|
|
622
|
+
return 32 - Math.clz32(w);
|
|
623
|
+
};
|
|
624
|
+
} else {
|
|
625
|
+
BN3.prototype._countBits = function _countBits(w) {
|
|
626
|
+
var t = w;
|
|
627
|
+
var r = 0;
|
|
628
|
+
if (t >= 4096) {
|
|
629
|
+
r += 13;
|
|
630
|
+
t >>>= 13;
|
|
631
|
+
}
|
|
632
|
+
if (t >= 64) {
|
|
633
|
+
r += 7;
|
|
634
|
+
t >>>= 7;
|
|
635
|
+
}
|
|
636
|
+
if (t >= 8) {
|
|
637
|
+
r += 4;
|
|
638
|
+
t >>>= 4;
|
|
639
|
+
}
|
|
640
|
+
if (t >= 2) {
|
|
641
|
+
r += 2;
|
|
642
|
+
t >>>= 2;
|
|
643
|
+
}
|
|
644
|
+
return r + t;
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
BN3.prototype._zeroBits = function _zeroBits(w) {
|
|
648
|
+
if (w === 0) return 26;
|
|
649
|
+
var t = w;
|
|
650
|
+
var r = 0;
|
|
651
|
+
if ((t & 8191) === 0) {
|
|
652
|
+
r += 13;
|
|
653
|
+
t >>>= 13;
|
|
654
|
+
}
|
|
655
|
+
if ((t & 127) === 0) {
|
|
656
|
+
r += 7;
|
|
657
|
+
t >>>= 7;
|
|
658
|
+
}
|
|
659
|
+
if ((t & 15) === 0) {
|
|
660
|
+
r += 4;
|
|
661
|
+
t >>>= 4;
|
|
662
|
+
}
|
|
663
|
+
if ((t & 3) === 0) {
|
|
664
|
+
r += 2;
|
|
665
|
+
t >>>= 2;
|
|
666
|
+
}
|
|
667
|
+
if ((t & 1) === 0) {
|
|
668
|
+
r++;
|
|
669
|
+
}
|
|
670
|
+
return r;
|
|
671
|
+
};
|
|
672
|
+
BN3.prototype.bitLength = function bitLength() {
|
|
673
|
+
var w = this.words[this.length - 1];
|
|
674
|
+
var hi = this._countBits(w);
|
|
675
|
+
return (this.length - 1) * 26 + hi;
|
|
676
|
+
};
|
|
677
|
+
function toBitArray(num) {
|
|
678
|
+
var w = new Array(num.bitLength());
|
|
679
|
+
for (var bit = 0; bit < w.length; bit++) {
|
|
680
|
+
var off = bit / 26 | 0;
|
|
681
|
+
var wbit = bit % 26;
|
|
682
|
+
w[bit] = num.words[off] >>> wbit & 1;
|
|
683
|
+
}
|
|
684
|
+
return w;
|
|
685
|
+
}
|
|
686
|
+
BN3.prototype.zeroBits = function zeroBits() {
|
|
687
|
+
if (this.isZero()) return 0;
|
|
688
|
+
var r = 0;
|
|
689
|
+
for (var i = 0; i < this.length; i++) {
|
|
690
|
+
var b = this._zeroBits(this.words[i]);
|
|
691
|
+
r += b;
|
|
692
|
+
if (b !== 26) break;
|
|
693
|
+
}
|
|
694
|
+
return r;
|
|
695
|
+
};
|
|
696
|
+
BN3.prototype.byteLength = function byteLength() {
|
|
697
|
+
return Math.ceil(this.bitLength() / 8);
|
|
698
|
+
};
|
|
699
|
+
BN3.prototype.toTwos = function toTwos(width) {
|
|
700
|
+
if (this.negative !== 0) {
|
|
701
|
+
return this.abs().inotn(width).iaddn(1);
|
|
702
|
+
}
|
|
703
|
+
return this.clone();
|
|
704
|
+
};
|
|
705
|
+
BN3.prototype.fromTwos = function fromTwos(width) {
|
|
706
|
+
if (this.testn(width - 1)) {
|
|
707
|
+
return this.notn(width).iaddn(1).ineg();
|
|
708
|
+
}
|
|
709
|
+
return this.clone();
|
|
710
|
+
};
|
|
711
|
+
BN3.prototype.isNeg = function isNeg() {
|
|
712
|
+
return this.negative !== 0;
|
|
713
|
+
};
|
|
714
|
+
BN3.prototype.neg = function neg() {
|
|
715
|
+
return this.clone().ineg();
|
|
716
|
+
};
|
|
717
|
+
BN3.prototype.ineg = function ineg() {
|
|
718
|
+
if (!this.isZero()) {
|
|
719
|
+
this.negative ^= 1;
|
|
720
|
+
}
|
|
721
|
+
return this;
|
|
722
|
+
};
|
|
723
|
+
BN3.prototype.iuor = function iuor(num) {
|
|
724
|
+
while (this.length < num.length) {
|
|
725
|
+
this.words[this.length++] = 0;
|
|
726
|
+
}
|
|
727
|
+
for (var i = 0; i < num.length; i++) {
|
|
728
|
+
this.words[i] = this.words[i] | num.words[i];
|
|
729
|
+
}
|
|
730
|
+
return this._strip();
|
|
731
|
+
};
|
|
732
|
+
BN3.prototype.ior = function ior(num) {
|
|
733
|
+
assert((this.negative | num.negative) === 0);
|
|
734
|
+
return this.iuor(num);
|
|
735
|
+
};
|
|
736
|
+
BN3.prototype.or = function or(num) {
|
|
737
|
+
if (this.length > num.length) return this.clone().ior(num);
|
|
738
|
+
return num.clone().ior(this);
|
|
739
|
+
};
|
|
740
|
+
BN3.prototype.uor = function uor(num) {
|
|
741
|
+
if (this.length > num.length) return this.clone().iuor(num);
|
|
742
|
+
return num.clone().iuor(this);
|
|
743
|
+
};
|
|
744
|
+
BN3.prototype.iuand = function iuand(num) {
|
|
745
|
+
var b;
|
|
746
|
+
if (this.length > num.length) {
|
|
747
|
+
b = num;
|
|
748
|
+
} else {
|
|
749
|
+
b = this;
|
|
750
|
+
}
|
|
751
|
+
for (var i = 0; i < b.length; i++) {
|
|
752
|
+
this.words[i] = this.words[i] & num.words[i];
|
|
753
|
+
}
|
|
754
|
+
this.length = b.length;
|
|
755
|
+
return this._strip();
|
|
756
|
+
};
|
|
757
|
+
BN3.prototype.iand = function iand(num) {
|
|
758
|
+
assert((this.negative | num.negative) === 0);
|
|
759
|
+
return this.iuand(num);
|
|
760
|
+
};
|
|
761
|
+
BN3.prototype.and = function and(num) {
|
|
762
|
+
if (this.length > num.length) return this.clone().iand(num);
|
|
763
|
+
return num.clone().iand(this);
|
|
764
|
+
};
|
|
765
|
+
BN3.prototype.uand = function uand(num) {
|
|
766
|
+
if (this.length > num.length) return this.clone().iuand(num);
|
|
767
|
+
return num.clone().iuand(this);
|
|
768
|
+
};
|
|
769
|
+
BN3.prototype.iuxor = function iuxor(num) {
|
|
770
|
+
var a;
|
|
771
|
+
var b;
|
|
772
|
+
if (this.length > num.length) {
|
|
773
|
+
a = this;
|
|
774
|
+
b = num;
|
|
775
|
+
} else {
|
|
776
|
+
a = num;
|
|
777
|
+
b = this;
|
|
778
|
+
}
|
|
779
|
+
for (var i = 0; i < b.length; i++) {
|
|
780
|
+
this.words[i] = a.words[i] ^ b.words[i];
|
|
781
|
+
}
|
|
782
|
+
if (this !== a) {
|
|
783
|
+
for (; i < a.length; i++) {
|
|
784
|
+
this.words[i] = a.words[i];
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
this.length = a.length;
|
|
788
|
+
return this._strip();
|
|
789
|
+
};
|
|
790
|
+
BN3.prototype.ixor = function ixor(num) {
|
|
791
|
+
assert((this.negative | num.negative) === 0);
|
|
792
|
+
return this.iuxor(num);
|
|
793
|
+
};
|
|
794
|
+
BN3.prototype.xor = function xor(num) {
|
|
795
|
+
if (this.length > num.length) return this.clone().ixor(num);
|
|
796
|
+
return num.clone().ixor(this);
|
|
797
|
+
};
|
|
798
|
+
BN3.prototype.uxor = function uxor(num) {
|
|
799
|
+
if (this.length > num.length) return this.clone().iuxor(num);
|
|
800
|
+
return num.clone().iuxor(this);
|
|
801
|
+
};
|
|
802
|
+
BN3.prototype.inotn = function inotn(width) {
|
|
803
|
+
assert(typeof width === "number" && width >= 0);
|
|
804
|
+
var bytesNeeded = Math.ceil(width / 26) | 0;
|
|
805
|
+
var bitsLeft = width % 26;
|
|
806
|
+
this._expand(bytesNeeded);
|
|
807
|
+
if (bitsLeft > 0) {
|
|
808
|
+
bytesNeeded--;
|
|
809
|
+
}
|
|
810
|
+
for (var i = 0; i < bytesNeeded; i++) {
|
|
811
|
+
this.words[i] = ~this.words[i] & 67108863;
|
|
812
|
+
}
|
|
813
|
+
if (bitsLeft > 0) {
|
|
814
|
+
this.words[i] = ~this.words[i] & 67108863 >> 26 - bitsLeft;
|
|
815
|
+
}
|
|
816
|
+
return this._strip();
|
|
817
|
+
};
|
|
818
|
+
BN3.prototype.notn = function notn(width) {
|
|
819
|
+
return this.clone().inotn(width);
|
|
820
|
+
};
|
|
821
|
+
BN3.prototype.setn = function setn(bit, val) {
|
|
822
|
+
assert(typeof bit === "number" && bit >= 0);
|
|
823
|
+
var off = bit / 26 | 0;
|
|
824
|
+
var wbit = bit % 26;
|
|
825
|
+
this._expand(off + 1);
|
|
826
|
+
if (val) {
|
|
827
|
+
this.words[off] = this.words[off] | 1 << wbit;
|
|
828
|
+
} else {
|
|
829
|
+
this.words[off] = this.words[off] & ~(1 << wbit);
|
|
830
|
+
}
|
|
831
|
+
return this._strip();
|
|
832
|
+
};
|
|
833
|
+
BN3.prototype.iadd = function iadd(num) {
|
|
834
|
+
var r;
|
|
835
|
+
if (this.negative !== 0 && num.negative === 0) {
|
|
836
|
+
this.negative = 0;
|
|
837
|
+
r = this.isub(num);
|
|
838
|
+
this.negative ^= 1;
|
|
839
|
+
return this._normSign();
|
|
840
|
+
} else if (this.negative === 0 && num.negative !== 0) {
|
|
841
|
+
num.negative = 0;
|
|
842
|
+
r = this.isub(num);
|
|
843
|
+
num.negative = 1;
|
|
844
|
+
return r._normSign();
|
|
845
|
+
}
|
|
846
|
+
var a, b;
|
|
847
|
+
if (this.length > num.length) {
|
|
848
|
+
a = this;
|
|
849
|
+
b = num;
|
|
850
|
+
} else {
|
|
851
|
+
a = num;
|
|
852
|
+
b = this;
|
|
853
|
+
}
|
|
854
|
+
var carry = 0;
|
|
855
|
+
for (var i = 0; i < b.length; i++) {
|
|
856
|
+
r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
|
|
857
|
+
this.words[i] = r & 67108863;
|
|
858
|
+
carry = r >>> 26;
|
|
859
|
+
}
|
|
860
|
+
for (; carry !== 0 && i < a.length; i++) {
|
|
861
|
+
r = (a.words[i] | 0) + carry;
|
|
862
|
+
this.words[i] = r & 67108863;
|
|
863
|
+
carry = r >>> 26;
|
|
864
|
+
}
|
|
865
|
+
this.length = a.length;
|
|
866
|
+
if (carry !== 0) {
|
|
867
|
+
this.words[this.length] = carry;
|
|
868
|
+
this.length++;
|
|
869
|
+
} else if (a !== this) {
|
|
870
|
+
for (; i < a.length; i++) {
|
|
871
|
+
this.words[i] = a.words[i];
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return this;
|
|
875
|
+
};
|
|
876
|
+
BN3.prototype.add = function add(num) {
|
|
877
|
+
var res;
|
|
878
|
+
if (num.negative !== 0 && this.negative === 0) {
|
|
879
|
+
num.negative = 0;
|
|
880
|
+
res = this.sub(num);
|
|
881
|
+
num.negative ^= 1;
|
|
882
|
+
return res;
|
|
883
|
+
} else if (num.negative === 0 && this.negative !== 0) {
|
|
884
|
+
this.negative = 0;
|
|
885
|
+
res = num.sub(this);
|
|
886
|
+
this.negative = 1;
|
|
887
|
+
return res;
|
|
888
|
+
}
|
|
889
|
+
if (this.length > num.length) return this.clone().iadd(num);
|
|
890
|
+
return num.clone().iadd(this);
|
|
891
|
+
};
|
|
892
|
+
BN3.prototype.isub = function isub(num) {
|
|
893
|
+
if (num.negative !== 0) {
|
|
894
|
+
num.negative = 0;
|
|
895
|
+
var r = this.iadd(num);
|
|
896
|
+
num.negative = 1;
|
|
897
|
+
return r._normSign();
|
|
898
|
+
} else if (this.negative !== 0) {
|
|
899
|
+
this.negative = 0;
|
|
900
|
+
this.iadd(num);
|
|
901
|
+
this.negative = 1;
|
|
902
|
+
return this._normSign();
|
|
903
|
+
}
|
|
904
|
+
var cmp = this.cmp(num);
|
|
905
|
+
if (cmp === 0) {
|
|
906
|
+
this.negative = 0;
|
|
907
|
+
this.length = 1;
|
|
908
|
+
this.words[0] = 0;
|
|
909
|
+
return this;
|
|
910
|
+
}
|
|
911
|
+
var a, b;
|
|
912
|
+
if (cmp > 0) {
|
|
913
|
+
a = this;
|
|
914
|
+
b = num;
|
|
915
|
+
} else {
|
|
916
|
+
a = num;
|
|
917
|
+
b = this;
|
|
918
|
+
}
|
|
919
|
+
var carry = 0;
|
|
920
|
+
for (var i = 0; i < b.length; i++) {
|
|
921
|
+
r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
|
|
922
|
+
carry = r >> 26;
|
|
923
|
+
this.words[i] = r & 67108863;
|
|
924
|
+
}
|
|
925
|
+
for (; carry !== 0 && i < a.length; i++) {
|
|
926
|
+
r = (a.words[i] | 0) + carry;
|
|
927
|
+
carry = r >> 26;
|
|
928
|
+
this.words[i] = r & 67108863;
|
|
929
|
+
}
|
|
930
|
+
if (carry === 0 && i < a.length && a !== this) {
|
|
931
|
+
for (; i < a.length; i++) {
|
|
932
|
+
this.words[i] = a.words[i];
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
this.length = Math.max(this.length, i);
|
|
936
|
+
if (a !== this) {
|
|
937
|
+
this.negative = 1;
|
|
938
|
+
}
|
|
939
|
+
return this._strip();
|
|
940
|
+
};
|
|
941
|
+
BN3.prototype.sub = function sub(num) {
|
|
942
|
+
return this.clone().isub(num);
|
|
943
|
+
};
|
|
944
|
+
function smallMulTo(self, num, out) {
|
|
945
|
+
out.negative = num.negative ^ self.negative;
|
|
946
|
+
var len = self.length + num.length | 0;
|
|
947
|
+
out.length = len;
|
|
948
|
+
len = len - 1 | 0;
|
|
949
|
+
var a = self.words[0] | 0;
|
|
950
|
+
var b = num.words[0] | 0;
|
|
951
|
+
var r = a * b;
|
|
952
|
+
var lo = r & 67108863;
|
|
953
|
+
var carry = r / 67108864 | 0;
|
|
954
|
+
out.words[0] = lo;
|
|
955
|
+
for (var k = 1; k < len; k++) {
|
|
956
|
+
var ncarry = carry >>> 26;
|
|
957
|
+
var rword = carry & 67108863;
|
|
958
|
+
var maxJ = Math.min(k, num.length - 1);
|
|
959
|
+
for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
|
|
960
|
+
var i = k - j | 0;
|
|
961
|
+
a = self.words[i] | 0;
|
|
962
|
+
b = num.words[j] | 0;
|
|
963
|
+
r = a * b + rword;
|
|
964
|
+
ncarry += r / 67108864 | 0;
|
|
965
|
+
rword = r & 67108863;
|
|
966
|
+
}
|
|
967
|
+
out.words[k] = rword | 0;
|
|
968
|
+
carry = ncarry | 0;
|
|
969
|
+
}
|
|
970
|
+
if (carry !== 0) {
|
|
971
|
+
out.words[k] = carry | 0;
|
|
972
|
+
} else {
|
|
973
|
+
out.length--;
|
|
974
|
+
}
|
|
975
|
+
return out._strip();
|
|
976
|
+
}
|
|
977
|
+
var comb10MulTo = function comb10MulTo2(self, num, out) {
|
|
978
|
+
var a = self.words;
|
|
979
|
+
var b = num.words;
|
|
980
|
+
var o = out.words;
|
|
981
|
+
var c = 0;
|
|
982
|
+
var lo;
|
|
983
|
+
var mid;
|
|
984
|
+
var hi;
|
|
985
|
+
var a0 = a[0] | 0;
|
|
986
|
+
var al0 = a0 & 8191;
|
|
987
|
+
var ah0 = a0 >>> 13;
|
|
988
|
+
var a1 = a[1] | 0;
|
|
989
|
+
var al1 = a1 & 8191;
|
|
990
|
+
var ah1 = a1 >>> 13;
|
|
991
|
+
var a2 = a[2] | 0;
|
|
992
|
+
var al2 = a2 & 8191;
|
|
993
|
+
var ah2 = a2 >>> 13;
|
|
994
|
+
var a3 = a[3] | 0;
|
|
995
|
+
var al3 = a3 & 8191;
|
|
996
|
+
var ah3 = a3 >>> 13;
|
|
997
|
+
var a4 = a[4] | 0;
|
|
998
|
+
var al4 = a4 & 8191;
|
|
999
|
+
var ah4 = a4 >>> 13;
|
|
1000
|
+
var a5 = a[5] | 0;
|
|
1001
|
+
var al5 = a5 & 8191;
|
|
1002
|
+
var ah5 = a5 >>> 13;
|
|
1003
|
+
var a6 = a[6] | 0;
|
|
1004
|
+
var al6 = a6 & 8191;
|
|
1005
|
+
var ah6 = a6 >>> 13;
|
|
1006
|
+
var a7 = a[7] | 0;
|
|
1007
|
+
var al7 = a7 & 8191;
|
|
1008
|
+
var ah7 = a7 >>> 13;
|
|
1009
|
+
var a8 = a[8] | 0;
|
|
1010
|
+
var al8 = a8 & 8191;
|
|
1011
|
+
var ah8 = a8 >>> 13;
|
|
1012
|
+
var a9 = a[9] | 0;
|
|
1013
|
+
var al9 = a9 & 8191;
|
|
1014
|
+
var ah9 = a9 >>> 13;
|
|
1015
|
+
var b0 = b[0] | 0;
|
|
1016
|
+
var bl0 = b0 & 8191;
|
|
1017
|
+
var bh0 = b0 >>> 13;
|
|
1018
|
+
var b1 = b[1] | 0;
|
|
1019
|
+
var bl1 = b1 & 8191;
|
|
1020
|
+
var bh1 = b1 >>> 13;
|
|
1021
|
+
var b2 = b[2] | 0;
|
|
1022
|
+
var bl2 = b2 & 8191;
|
|
1023
|
+
var bh2 = b2 >>> 13;
|
|
1024
|
+
var b3 = b[3] | 0;
|
|
1025
|
+
var bl3 = b3 & 8191;
|
|
1026
|
+
var bh3 = b3 >>> 13;
|
|
1027
|
+
var b4 = b[4] | 0;
|
|
1028
|
+
var bl4 = b4 & 8191;
|
|
1029
|
+
var bh4 = b4 >>> 13;
|
|
1030
|
+
var b5 = b[5] | 0;
|
|
1031
|
+
var bl5 = b5 & 8191;
|
|
1032
|
+
var bh5 = b5 >>> 13;
|
|
1033
|
+
var b6 = b[6] | 0;
|
|
1034
|
+
var bl6 = b6 & 8191;
|
|
1035
|
+
var bh6 = b6 >>> 13;
|
|
1036
|
+
var b7 = b[7] | 0;
|
|
1037
|
+
var bl7 = b7 & 8191;
|
|
1038
|
+
var bh7 = b7 >>> 13;
|
|
1039
|
+
var b8 = b[8] | 0;
|
|
1040
|
+
var bl8 = b8 & 8191;
|
|
1041
|
+
var bh8 = b8 >>> 13;
|
|
1042
|
+
var b9 = b[9] | 0;
|
|
1043
|
+
var bl9 = b9 & 8191;
|
|
1044
|
+
var bh9 = b9 >>> 13;
|
|
1045
|
+
out.negative = self.negative ^ num.negative;
|
|
1046
|
+
out.length = 19;
|
|
1047
|
+
lo = Math.imul(al0, bl0);
|
|
1048
|
+
mid = Math.imul(al0, bh0);
|
|
1049
|
+
mid = mid + Math.imul(ah0, bl0) | 0;
|
|
1050
|
+
hi = Math.imul(ah0, bh0);
|
|
1051
|
+
var w0 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1052
|
+
c = (hi + (mid >>> 13) | 0) + (w0 >>> 26) | 0;
|
|
1053
|
+
w0 &= 67108863;
|
|
1054
|
+
lo = Math.imul(al1, bl0);
|
|
1055
|
+
mid = Math.imul(al1, bh0);
|
|
1056
|
+
mid = mid + Math.imul(ah1, bl0) | 0;
|
|
1057
|
+
hi = Math.imul(ah1, bh0);
|
|
1058
|
+
lo = lo + Math.imul(al0, bl1) | 0;
|
|
1059
|
+
mid = mid + Math.imul(al0, bh1) | 0;
|
|
1060
|
+
mid = mid + Math.imul(ah0, bl1) | 0;
|
|
1061
|
+
hi = hi + Math.imul(ah0, bh1) | 0;
|
|
1062
|
+
var w1 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1063
|
+
c = (hi + (mid >>> 13) | 0) + (w1 >>> 26) | 0;
|
|
1064
|
+
w1 &= 67108863;
|
|
1065
|
+
lo = Math.imul(al2, bl0);
|
|
1066
|
+
mid = Math.imul(al2, bh0);
|
|
1067
|
+
mid = mid + Math.imul(ah2, bl0) | 0;
|
|
1068
|
+
hi = Math.imul(ah2, bh0);
|
|
1069
|
+
lo = lo + Math.imul(al1, bl1) | 0;
|
|
1070
|
+
mid = mid + Math.imul(al1, bh1) | 0;
|
|
1071
|
+
mid = mid + Math.imul(ah1, bl1) | 0;
|
|
1072
|
+
hi = hi + Math.imul(ah1, bh1) | 0;
|
|
1073
|
+
lo = lo + Math.imul(al0, bl2) | 0;
|
|
1074
|
+
mid = mid + Math.imul(al0, bh2) | 0;
|
|
1075
|
+
mid = mid + Math.imul(ah0, bl2) | 0;
|
|
1076
|
+
hi = hi + Math.imul(ah0, bh2) | 0;
|
|
1077
|
+
var w2 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1078
|
+
c = (hi + (mid >>> 13) | 0) + (w2 >>> 26) | 0;
|
|
1079
|
+
w2 &= 67108863;
|
|
1080
|
+
lo = Math.imul(al3, bl0);
|
|
1081
|
+
mid = Math.imul(al3, bh0);
|
|
1082
|
+
mid = mid + Math.imul(ah3, bl0) | 0;
|
|
1083
|
+
hi = Math.imul(ah3, bh0);
|
|
1084
|
+
lo = lo + Math.imul(al2, bl1) | 0;
|
|
1085
|
+
mid = mid + Math.imul(al2, bh1) | 0;
|
|
1086
|
+
mid = mid + Math.imul(ah2, bl1) | 0;
|
|
1087
|
+
hi = hi + Math.imul(ah2, bh1) | 0;
|
|
1088
|
+
lo = lo + Math.imul(al1, bl2) | 0;
|
|
1089
|
+
mid = mid + Math.imul(al1, bh2) | 0;
|
|
1090
|
+
mid = mid + Math.imul(ah1, bl2) | 0;
|
|
1091
|
+
hi = hi + Math.imul(ah1, bh2) | 0;
|
|
1092
|
+
lo = lo + Math.imul(al0, bl3) | 0;
|
|
1093
|
+
mid = mid + Math.imul(al0, bh3) | 0;
|
|
1094
|
+
mid = mid + Math.imul(ah0, bl3) | 0;
|
|
1095
|
+
hi = hi + Math.imul(ah0, bh3) | 0;
|
|
1096
|
+
var w3 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1097
|
+
c = (hi + (mid >>> 13) | 0) + (w3 >>> 26) | 0;
|
|
1098
|
+
w3 &= 67108863;
|
|
1099
|
+
lo = Math.imul(al4, bl0);
|
|
1100
|
+
mid = Math.imul(al4, bh0);
|
|
1101
|
+
mid = mid + Math.imul(ah4, bl0) | 0;
|
|
1102
|
+
hi = Math.imul(ah4, bh0);
|
|
1103
|
+
lo = lo + Math.imul(al3, bl1) | 0;
|
|
1104
|
+
mid = mid + Math.imul(al3, bh1) | 0;
|
|
1105
|
+
mid = mid + Math.imul(ah3, bl1) | 0;
|
|
1106
|
+
hi = hi + Math.imul(ah3, bh1) | 0;
|
|
1107
|
+
lo = lo + Math.imul(al2, bl2) | 0;
|
|
1108
|
+
mid = mid + Math.imul(al2, bh2) | 0;
|
|
1109
|
+
mid = mid + Math.imul(ah2, bl2) | 0;
|
|
1110
|
+
hi = hi + Math.imul(ah2, bh2) | 0;
|
|
1111
|
+
lo = lo + Math.imul(al1, bl3) | 0;
|
|
1112
|
+
mid = mid + Math.imul(al1, bh3) | 0;
|
|
1113
|
+
mid = mid + Math.imul(ah1, bl3) | 0;
|
|
1114
|
+
hi = hi + Math.imul(ah1, bh3) | 0;
|
|
1115
|
+
lo = lo + Math.imul(al0, bl4) | 0;
|
|
1116
|
+
mid = mid + Math.imul(al0, bh4) | 0;
|
|
1117
|
+
mid = mid + Math.imul(ah0, bl4) | 0;
|
|
1118
|
+
hi = hi + Math.imul(ah0, bh4) | 0;
|
|
1119
|
+
var w4 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1120
|
+
c = (hi + (mid >>> 13) | 0) + (w4 >>> 26) | 0;
|
|
1121
|
+
w4 &= 67108863;
|
|
1122
|
+
lo = Math.imul(al5, bl0);
|
|
1123
|
+
mid = Math.imul(al5, bh0);
|
|
1124
|
+
mid = mid + Math.imul(ah5, bl0) | 0;
|
|
1125
|
+
hi = Math.imul(ah5, bh0);
|
|
1126
|
+
lo = lo + Math.imul(al4, bl1) | 0;
|
|
1127
|
+
mid = mid + Math.imul(al4, bh1) | 0;
|
|
1128
|
+
mid = mid + Math.imul(ah4, bl1) | 0;
|
|
1129
|
+
hi = hi + Math.imul(ah4, bh1) | 0;
|
|
1130
|
+
lo = lo + Math.imul(al3, bl2) | 0;
|
|
1131
|
+
mid = mid + Math.imul(al3, bh2) | 0;
|
|
1132
|
+
mid = mid + Math.imul(ah3, bl2) | 0;
|
|
1133
|
+
hi = hi + Math.imul(ah3, bh2) | 0;
|
|
1134
|
+
lo = lo + Math.imul(al2, bl3) | 0;
|
|
1135
|
+
mid = mid + Math.imul(al2, bh3) | 0;
|
|
1136
|
+
mid = mid + Math.imul(ah2, bl3) | 0;
|
|
1137
|
+
hi = hi + Math.imul(ah2, bh3) | 0;
|
|
1138
|
+
lo = lo + Math.imul(al1, bl4) | 0;
|
|
1139
|
+
mid = mid + Math.imul(al1, bh4) | 0;
|
|
1140
|
+
mid = mid + Math.imul(ah1, bl4) | 0;
|
|
1141
|
+
hi = hi + Math.imul(ah1, bh4) | 0;
|
|
1142
|
+
lo = lo + Math.imul(al0, bl5) | 0;
|
|
1143
|
+
mid = mid + Math.imul(al0, bh5) | 0;
|
|
1144
|
+
mid = mid + Math.imul(ah0, bl5) | 0;
|
|
1145
|
+
hi = hi + Math.imul(ah0, bh5) | 0;
|
|
1146
|
+
var w5 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1147
|
+
c = (hi + (mid >>> 13) | 0) + (w5 >>> 26) | 0;
|
|
1148
|
+
w5 &= 67108863;
|
|
1149
|
+
lo = Math.imul(al6, bl0);
|
|
1150
|
+
mid = Math.imul(al6, bh0);
|
|
1151
|
+
mid = mid + Math.imul(ah6, bl0) | 0;
|
|
1152
|
+
hi = Math.imul(ah6, bh0);
|
|
1153
|
+
lo = lo + Math.imul(al5, bl1) | 0;
|
|
1154
|
+
mid = mid + Math.imul(al5, bh1) | 0;
|
|
1155
|
+
mid = mid + Math.imul(ah5, bl1) | 0;
|
|
1156
|
+
hi = hi + Math.imul(ah5, bh1) | 0;
|
|
1157
|
+
lo = lo + Math.imul(al4, bl2) | 0;
|
|
1158
|
+
mid = mid + Math.imul(al4, bh2) | 0;
|
|
1159
|
+
mid = mid + Math.imul(ah4, bl2) | 0;
|
|
1160
|
+
hi = hi + Math.imul(ah4, bh2) | 0;
|
|
1161
|
+
lo = lo + Math.imul(al3, bl3) | 0;
|
|
1162
|
+
mid = mid + Math.imul(al3, bh3) | 0;
|
|
1163
|
+
mid = mid + Math.imul(ah3, bl3) | 0;
|
|
1164
|
+
hi = hi + Math.imul(ah3, bh3) | 0;
|
|
1165
|
+
lo = lo + Math.imul(al2, bl4) | 0;
|
|
1166
|
+
mid = mid + Math.imul(al2, bh4) | 0;
|
|
1167
|
+
mid = mid + Math.imul(ah2, bl4) | 0;
|
|
1168
|
+
hi = hi + Math.imul(ah2, bh4) | 0;
|
|
1169
|
+
lo = lo + Math.imul(al1, bl5) | 0;
|
|
1170
|
+
mid = mid + Math.imul(al1, bh5) | 0;
|
|
1171
|
+
mid = mid + Math.imul(ah1, bl5) | 0;
|
|
1172
|
+
hi = hi + Math.imul(ah1, bh5) | 0;
|
|
1173
|
+
lo = lo + Math.imul(al0, bl6) | 0;
|
|
1174
|
+
mid = mid + Math.imul(al0, bh6) | 0;
|
|
1175
|
+
mid = mid + Math.imul(ah0, bl6) | 0;
|
|
1176
|
+
hi = hi + Math.imul(ah0, bh6) | 0;
|
|
1177
|
+
var w6 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1178
|
+
c = (hi + (mid >>> 13) | 0) + (w6 >>> 26) | 0;
|
|
1179
|
+
w6 &= 67108863;
|
|
1180
|
+
lo = Math.imul(al7, bl0);
|
|
1181
|
+
mid = Math.imul(al7, bh0);
|
|
1182
|
+
mid = mid + Math.imul(ah7, bl0) | 0;
|
|
1183
|
+
hi = Math.imul(ah7, bh0);
|
|
1184
|
+
lo = lo + Math.imul(al6, bl1) | 0;
|
|
1185
|
+
mid = mid + Math.imul(al6, bh1) | 0;
|
|
1186
|
+
mid = mid + Math.imul(ah6, bl1) | 0;
|
|
1187
|
+
hi = hi + Math.imul(ah6, bh1) | 0;
|
|
1188
|
+
lo = lo + Math.imul(al5, bl2) | 0;
|
|
1189
|
+
mid = mid + Math.imul(al5, bh2) | 0;
|
|
1190
|
+
mid = mid + Math.imul(ah5, bl2) | 0;
|
|
1191
|
+
hi = hi + Math.imul(ah5, bh2) | 0;
|
|
1192
|
+
lo = lo + Math.imul(al4, bl3) | 0;
|
|
1193
|
+
mid = mid + Math.imul(al4, bh3) | 0;
|
|
1194
|
+
mid = mid + Math.imul(ah4, bl3) | 0;
|
|
1195
|
+
hi = hi + Math.imul(ah4, bh3) | 0;
|
|
1196
|
+
lo = lo + Math.imul(al3, bl4) | 0;
|
|
1197
|
+
mid = mid + Math.imul(al3, bh4) | 0;
|
|
1198
|
+
mid = mid + Math.imul(ah3, bl4) | 0;
|
|
1199
|
+
hi = hi + Math.imul(ah3, bh4) | 0;
|
|
1200
|
+
lo = lo + Math.imul(al2, bl5) | 0;
|
|
1201
|
+
mid = mid + Math.imul(al2, bh5) | 0;
|
|
1202
|
+
mid = mid + Math.imul(ah2, bl5) | 0;
|
|
1203
|
+
hi = hi + Math.imul(ah2, bh5) | 0;
|
|
1204
|
+
lo = lo + Math.imul(al1, bl6) | 0;
|
|
1205
|
+
mid = mid + Math.imul(al1, bh6) | 0;
|
|
1206
|
+
mid = mid + Math.imul(ah1, bl6) | 0;
|
|
1207
|
+
hi = hi + Math.imul(ah1, bh6) | 0;
|
|
1208
|
+
lo = lo + Math.imul(al0, bl7) | 0;
|
|
1209
|
+
mid = mid + Math.imul(al0, bh7) | 0;
|
|
1210
|
+
mid = mid + Math.imul(ah0, bl7) | 0;
|
|
1211
|
+
hi = hi + Math.imul(ah0, bh7) | 0;
|
|
1212
|
+
var w7 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1213
|
+
c = (hi + (mid >>> 13) | 0) + (w7 >>> 26) | 0;
|
|
1214
|
+
w7 &= 67108863;
|
|
1215
|
+
lo = Math.imul(al8, bl0);
|
|
1216
|
+
mid = Math.imul(al8, bh0);
|
|
1217
|
+
mid = mid + Math.imul(ah8, bl0) | 0;
|
|
1218
|
+
hi = Math.imul(ah8, bh0);
|
|
1219
|
+
lo = lo + Math.imul(al7, bl1) | 0;
|
|
1220
|
+
mid = mid + Math.imul(al7, bh1) | 0;
|
|
1221
|
+
mid = mid + Math.imul(ah7, bl1) | 0;
|
|
1222
|
+
hi = hi + Math.imul(ah7, bh1) | 0;
|
|
1223
|
+
lo = lo + Math.imul(al6, bl2) | 0;
|
|
1224
|
+
mid = mid + Math.imul(al6, bh2) | 0;
|
|
1225
|
+
mid = mid + Math.imul(ah6, bl2) | 0;
|
|
1226
|
+
hi = hi + Math.imul(ah6, bh2) | 0;
|
|
1227
|
+
lo = lo + Math.imul(al5, bl3) | 0;
|
|
1228
|
+
mid = mid + Math.imul(al5, bh3) | 0;
|
|
1229
|
+
mid = mid + Math.imul(ah5, bl3) | 0;
|
|
1230
|
+
hi = hi + Math.imul(ah5, bh3) | 0;
|
|
1231
|
+
lo = lo + Math.imul(al4, bl4) | 0;
|
|
1232
|
+
mid = mid + Math.imul(al4, bh4) | 0;
|
|
1233
|
+
mid = mid + Math.imul(ah4, bl4) | 0;
|
|
1234
|
+
hi = hi + Math.imul(ah4, bh4) | 0;
|
|
1235
|
+
lo = lo + Math.imul(al3, bl5) | 0;
|
|
1236
|
+
mid = mid + Math.imul(al3, bh5) | 0;
|
|
1237
|
+
mid = mid + Math.imul(ah3, bl5) | 0;
|
|
1238
|
+
hi = hi + Math.imul(ah3, bh5) | 0;
|
|
1239
|
+
lo = lo + Math.imul(al2, bl6) | 0;
|
|
1240
|
+
mid = mid + Math.imul(al2, bh6) | 0;
|
|
1241
|
+
mid = mid + Math.imul(ah2, bl6) | 0;
|
|
1242
|
+
hi = hi + Math.imul(ah2, bh6) | 0;
|
|
1243
|
+
lo = lo + Math.imul(al1, bl7) | 0;
|
|
1244
|
+
mid = mid + Math.imul(al1, bh7) | 0;
|
|
1245
|
+
mid = mid + Math.imul(ah1, bl7) | 0;
|
|
1246
|
+
hi = hi + Math.imul(ah1, bh7) | 0;
|
|
1247
|
+
lo = lo + Math.imul(al0, bl8) | 0;
|
|
1248
|
+
mid = mid + Math.imul(al0, bh8) | 0;
|
|
1249
|
+
mid = mid + Math.imul(ah0, bl8) | 0;
|
|
1250
|
+
hi = hi + Math.imul(ah0, bh8) | 0;
|
|
1251
|
+
var w8 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1252
|
+
c = (hi + (mid >>> 13) | 0) + (w8 >>> 26) | 0;
|
|
1253
|
+
w8 &= 67108863;
|
|
1254
|
+
lo = Math.imul(al9, bl0);
|
|
1255
|
+
mid = Math.imul(al9, bh0);
|
|
1256
|
+
mid = mid + Math.imul(ah9, bl0) | 0;
|
|
1257
|
+
hi = Math.imul(ah9, bh0);
|
|
1258
|
+
lo = lo + Math.imul(al8, bl1) | 0;
|
|
1259
|
+
mid = mid + Math.imul(al8, bh1) | 0;
|
|
1260
|
+
mid = mid + Math.imul(ah8, bl1) | 0;
|
|
1261
|
+
hi = hi + Math.imul(ah8, bh1) | 0;
|
|
1262
|
+
lo = lo + Math.imul(al7, bl2) | 0;
|
|
1263
|
+
mid = mid + Math.imul(al7, bh2) | 0;
|
|
1264
|
+
mid = mid + Math.imul(ah7, bl2) | 0;
|
|
1265
|
+
hi = hi + Math.imul(ah7, bh2) | 0;
|
|
1266
|
+
lo = lo + Math.imul(al6, bl3) | 0;
|
|
1267
|
+
mid = mid + Math.imul(al6, bh3) | 0;
|
|
1268
|
+
mid = mid + Math.imul(ah6, bl3) | 0;
|
|
1269
|
+
hi = hi + Math.imul(ah6, bh3) | 0;
|
|
1270
|
+
lo = lo + Math.imul(al5, bl4) | 0;
|
|
1271
|
+
mid = mid + Math.imul(al5, bh4) | 0;
|
|
1272
|
+
mid = mid + Math.imul(ah5, bl4) | 0;
|
|
1273
|
+
hi = hi + Math.imul(ah5, bh4) | 0;
|
|
1274
|
+
lo = lo + Math.imul(al4, bl5) | 0;
|
|
1275
|
+
mid = mid + Math.imul(al4, bh5) | 0;
|
|
1276
|
+
mid = mid + Math.imul(ah4, bl5) | 0;
|
|
1277
|
+
hi = hi + Math.imul(ah4, bh5) | 0;
|
|
1278
|
+
lo = lo + Math.imul(al3, bl6) | 0;
|
|
1279
|
+
mid = mid + Math.imul(al3, bh6) | 0;
|
|
1280
|
+
mid = mid + Math.imul(ah3, bl6) | 0;
|
|
1281
|
+
hi = hi + Math.imul(ah3, bh6) | 0;
|
|
1282
|
+
lo = lo + Math.imul(al2, bl7) | 0;
|
|
1283
|
+
mid = mid + Math.imul(al2, bh7) | 0;
|
|
1284
|
+
mid = mid + Math.imul(ah2, bl7) | 0;
|
|
1285
|
+
hi = hi + Math.imul(ah2, bh7) | 0;
|
|
1286
|
+
lo = lo + Math.imul(al1, bl8) | 0;
|
|
1287
|
+
mid = mid + Math.imul(al1, bh8) | 0;
|
|
1288
|
+
mid = mid + Math.imul(ah1, bl8) | 0;
|
|
1289
|
+
hi = hi + Math.imul(ah1, bh8) | 0;
|
|
1290
|
+
lo = lo + Math.imul(al0, bl9) | 0;
|
|
1291
|
+
mid = mid + Math.imul(al0, bh9) | 0;
|
|
1292
|
+
mid = mid + Math.imul(ah0, bl9) | 0;
|
|
1293
|
+
hi = hi + Math.imul(ah0, bh9) | 0;
|
|
1294
|
+
var w9 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1295
|
+
c = (hi + (mid >>> 13) | 0) + (w9 >>> 26) | 0;
|
|
1296
|
+
w9 &= 67108863;
|
|
1297
|
+
lo = Math.imul(al9, bl1);
|
|
1298
|
+
mid = Math.imul(al9, bh1);
|
|
1299
|
+
mid = mid + Math.imul(ah9, bl1) | 0;
|
|
1300
|
+
hi = Math.imul(ah9, bh1);
|
|
1301
|
+
lo = lo + Math.imul(al8, bl2) | 0;
|
|
1302
|
+
mid = mid + Math.imul(al8, bh2) | 0;
|
|
1303
|
+
mid = mid + Math.imul(ah8, bl2) | 0;
|
|
1304
|
+
hi = hi + Math.imul(ah8, bh2) | 0;
|
|
1305
|
+
lo = lo + Math.imul(al7, bl3) | 0;
|
|
1306
|
+
mid = mid + Math.imul(al7, bh3) | 0;
|
|
1307
|
+
mid = mid + Math.imul(ah7, bl3) | 0;
|
|
1308
|
+
hi = hi + Math.imul(ah7, bh3) | 0;
|
|
1309
|
+
lo = lo + Math.imul(al6, bl4) | 0;
|
|
1310
|
+
mid = mid + Math.imul(al6, bh4) | 0;
|
|
1311
|
+
mid = mid + Math.imul(ah6, bl4) | 0;
|
|
1312
|
+
hi = hi + Math.imul(ah6, bh4) | 0;
|
|
1313
|
+
lo = lo + Math.imul(al5, bl5) | 0;
|
|
1314
|
+
mid = mid + Math.imul(al5, bh5) | 0;
|
|
1315
|
+
mid = mid + Math.imul(ah5, bl5) | 0;
|
|
1316
|
+
hi = hi + Math.imul(ah5, bh5) | 0;
|
|
1317
|
+
lo = lo + Math.imul(al4, bl6) | 0;
|
|
1318
|
+
mid = mid + Math.imul(al4, bh6) | 0;
|
|
1319
|
+
mid = mid + Math.imul(ah4, bl6) | 0;
|
|
1320
|
+
hi = hi + Math.imul(ah4, bh6) | 0;
|
|
1321
|
+
lo = lo + Math.imul(al3, bl7) | 0;
|
|
1322
|
+
mid = mid + Math.imul(al3, bh7) | 0;
|
|
1323
|
+
mid = mid + Math.imul(ah3, bl7) | 0;
|
|
1324
|
+
hi = hi + Math.imul(ah3, bh7) | 0;
|
|
1325
|
+
lo = lo + Math.imul(al2, bl8) | 0;
|
|
1326
|
+
mid = mid + Math.imul(al2, bh8) | 0;
|
|
1327
|
+
mid = mid + Math.imul(ah2, bl8) | 0;
|
|
1328
|
+
hi = hi + Math.imul(ah2, bh8) | 0;
|
|
1329
|
+
lo = lo + Math.imul(al1, bl9) | 0;
|
|
1330
|
+
mid = mid + Math.imul(al1, bh9) | 0;
|
|
1331
|
+
mid = mid + Math.imul(ah1, bl9) | 0;
|
|
1332
|
+
hi = hi + Math.imul(ah1, bh9) | 0;
|
|
1333
|
+
var w10 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1334
|
+
c = (hi + (mid >>> 13) | 0) + (w10 >>> 26) | 0;
|
|
1335
|
+
w10 &= 67108863;
|
|
1336
|
+
lo = Math.imul(al9, bl2);
|
|
1337
|
+
mid = Math.imul(al9, bh2);
|
|
1338
|
+
mid = mid + Math.imul(ah9, bl2) | 0;
|
|
1339
|
+
hi = Math.imul(ah9, bh2);
|
|
1340
|
+
lo = lo + Math.imul(al8, bl3) | 0;
|
|
1341
|
+
mid = mid + Math.imul(al8, bh3) | 0;
|
|
1342
|
+
mid = mid + Math.imul(ah8, bl3) | 0;
|
|
1343
|
+
hi = hi + Math.imul(ah8, bh3) | 0;
|
|
1344
|
+
lo = lo + Math.imul(al7, bl4) | 0;
|
|
1345
|
+
mid = mid + Math.imul(al7, bh4) | 0;
|
|
1346
|
+
mid = mid + Math.imul(ah7, bl4) | 0;
|
|
1347
|
+
hi = hi + Math.imul(ah7, bh4) | 0;
|
|
1348
|
+
lo = lo + Math.imul(al6, bl5) | 0;
|
|
1349
|
+
mid = mid + Math.imul(al6, bh5) | 0;
|
|
1350
|
+
mid = mid + Math.imul(ah6, bl5) | 0;
|
|
1351
|
+
hi = hi + Math.imul(ah6, bh5) | 0;
|
|
1352
|
+
lo = lo + Math.imul(al5, bl6) | 0;
|
|
1353
|
+
mid = mid + Math.imul(al5, bh6) | 0;
|
|
1354
|
+
mid = mid + Math.imul(ah5, bl6) | 0;
|
|
1355
|
+
hi = hi + Math.imul(ah5, bh6) | 0;
|
|
1356
|
+
lo = lo + Math.imul(al4, bl7) | 0;
|
|
1357
|
+
mid = mid + Math.imul(al4, bh7) | 0;
|
|
1358
|
+
mid = mid + Math.imul(ah4, bl7) | 0;
|
|
1359
|
+
hi = hi + Math.imul(ah4, bh7) | 0;
|
|
1360
|
+
lo = lo + Math.imul(al3, bl8) | 0;
|
|
1361
|
+
mid = mid + Math.imul(al3, bh8) | 0;
|
|
1362
|
+
mid = mid + Math.imul(ah3, bl8) | 0;
|
|
1363
|
+
hi = hi + Math.imul(ah3, bh8) | 0;
|
|
1364
|
+
lo = lo + Math.imul(al2, bl9) | 0;
|
|
1365
|
+
mid = mid + Math.imul(al2, bh9) | 0;
|
|
1366
|
+
mid = mid + Math.imul(ah2, bl9) | 0;
|
|
1367
|
+
hi = hi + Math.imul(ah2, bh9) | 0;
|
|
1368
|
+
var w11 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1369
|
+
c = (hi + (mid >>> 13) | 0) + (w11 >>> 26) | 0;
|
|
1370
|
+
w11 &= 67108863;
|
|
1371
|
+
lo = Math.imul(al9, bl3);
|
|
1372
|
+
mid = Math.imul(al9, bh3);
|
|
1373
|
+
mid = mid + Math.imul(ah9, bl3) | 0;
|
|
1374
|
+
hi = Math.imul(ah9, bh3);
|
|
1375
|
+
lo = lo + Math.imul(al8, bl4) | 0;
|
|
1376
|
+
mid = mid + Math.imul(al8, bh4) | 0;
|
|
1377
|
+
mid = mid + Math.imul(ah8, bl4) | 0;
|
|
1378
|
+
hi = hi + Math.imul(ah8, bh4) | 0;
|
|
1379
|
+
lo = lo + Math.imul(al7, bl5) | 0;
|
|
1380
|
+
mid = mid + Math.imul(al7, bh5) | 0;
|
|
1381
|
+
mid = mid + Math.imul(ah7, bl5) | 0;
|
|
1382
|
+
hi = hi + Math.imul(ah7, bh5) | 0;
|
|
1383
|
+
lo = lo + Math.imul(al6, bl6) | 0;
|
|
1384
|
+
mid = mid + Math.imul(al6, bh6) | 0;
|
|
1385
|
+
mid = mid + Math.imul(ah6, bl6) | 0;
|
|
1386
|
+
hi = hi + Math.imul(ah6, bh6) | 0;
|
|
1387
|
+
lo = lo + Math.imul(al5, bl7) | 0;
|
|
1388
|
+
mid = mid + Math.imul(al5, bh7) | 0;
|
|
1389
|
+
mid = mid + Math.imul(ah5, bl7) | 0;
|
|
1390
|
+
hi = hi + Math.imul(ah5, bh7) | 0;
|
|
1391
|
+
lo = lo + Math.imul(al4, bl8) | 0;
|
|
1392
|
+
mid = mid + Math.imul(al4, bh8) | 0;
|
|
1393
|
+
mid = mid + Math.imul(ah4, bl8) | 0;
|
|
1394
|
+
hi = hi + Math.imul(ah4, bh8) | 0;
|
|
1395
|
+
lo = lo + Math.imul(al3, bl9) | 0;
|
|
1396
|
+
mid = mid + Math.imul(al3, bh9) | 0;
|
|
1397
|
+
mid = mid + Math.imul(ah3, bl9) | 0;
|
|
1398
|
+
hi = hi + Math.imul(ah3, bh9) | 0;
|
|
1399
|
+
var w12 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1400
|
+
c = (hi + (mid >>> 13) | 0) + (w12 >>> 26) | 0;
|
|
1401
|
+
w12 &= 67108863;
|
|
1402
|
+
lo = Math.imul(al9, bl4);
|
|
1403
|
+
mid = Math.imul(al9, bh4);
|
|
1404
|
+
mid = mid + Math.imul(ah9, bl4) | 0;
|
|
1405
|
+
hi = Math.imul(ah9, bh4);
|
|
1406
|
+
lo = lo + Math.imul(al8, bl5) | 0;
|
|
1407
|
+
mid = mid + Math.imul(al8, bh5) | 0;
|
|
1408
|
+
mid = mid + Math.imul(ah8, bl5) | 0;
|
|
1409
|
+
hi = hi + Math.imul(ah8, bh5) | 0;
|
|
1410
|
+
lo = lo + Math.imul(al7, bl6) | 0;
|
|
1411
|
+
mid = mid + Math.imul(al7, bh6) | 0;
|
|
1412
|
+
mid = mid + Math.imul(ah7, bl6) | 0;
|
|
1413
|
+
hi = hi + Math.imul(ah7, bh6) | 0;
|
|
1414
|
+
lo = lo + Math.imul(al6, bl7) | 0;
|
|
1415
|
+
mid = mid + Math.imul(al6, bh7) | 0;
|
|
1416
|
+
mid = mid + Math.imul(ah6, bl7) | 0;
|
|
1417
|
+
hi = hi + Math.imul(ah6, bh7) | 0;
|
|
1418
|
+
lo = lo + Math.imul(al5, bl8) | 0;
|
|
1419
|
+
mid = mid + Math.imul(al5, bh8) | 0;
|
|
1420
|
+
mid = mid + Math.imul(ah5, bl8) | 0;
|
|
1421
|
+
hi = hi + Math.imul(ah5, bh8) | 0;
|
|
1422
|
+
lo = lo + Math.imul(al4, bl9) | 0;
|
|
1423
|
+
mid = mid + Math.imul(al4, bh9) | 0;
|
|
1424
|
+
mid = mid + Math.imul(ah4, bl9) | 0;
|
|
1425
|
+
hi = hi + Math.imul(ah4, bh9) | 0;
|
|
1426
|
+
var w13 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1427
|
+
c = (hi + (mid >>> 13) | 0) + (w13 >>> 26) | 0;
|
|
1428
|
+
w13 &= 67108863;
|
|
1429
|
+
lo = Math.imul(al9, bl5);
|
|
1430
|
+
mid = Math.imul(al9, bh5);
|
|
1431
|
+
mid = mid + Math.imul(ah9, bl5) | 0;
|
|
1432
|
+
hi = Math.imul(ah9, bh5);
|
|
1433
|
+
lo = lo + Math.imul(al8, bl6) | 0;
|
|
1434
|
+
mid = mid + Math.imul(al8, bh6) | 0;
|
|
1435
|
+
mid = mid + Math.imul(ah8, bl6) | 0;
|
|
1436
|
+
hi = hi + Math.imul(ah8, bh6) | 0;
|
|
1437
|
+
lo = lo + Math.imul(al7, bl7) | 0;
|
|
1438
|
+
mid = mid + Math.imul(al7, bh7) | 0;
|
|
1439
|
+
mid = mid + Math.imul(ah7, bl7) | 0;
|
|
1440
|
+
hi = hi + Math.imul(ah7, bh7) | 0;
|
|
1441
|
+
lo = lo + Math.imul(al6, bl8) | 0;
|
|
1442
|
+
mid = mid + Math.imul(al6, bh8) | 0;
|
|
1443
|
+
mid = mid + Math.imul(ah6, bl8) | 0;
|
|
1444
|
+
hi = hi + Math.imul(ah6, bh8) | 0;
|
|
1445
|
+
lo = lo + Math.imul(al5, bl9) | 0;
|
|
1446
|
+
mid = mid + Math.imul(al5, bh9) | 0;
|
|
1447
|
+
mid = mid + Math.imul(ah5, bl9) | 0;
|
|
1448
|
+
hi = hi + Math.imul(ah5, bh9) | 0;
|
|
1449
|
+
var w14 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1450
|
+
c = (hi + (mid >>> 13) | 0) + (w14 >>> 26) | 0;
|
|
1451
|
+
w14 &= 67108863;
|
|
1452
|
+
lo = Math.imul(al9, bl6);
|
|
1453
|
+
mid = Math.imul(al9, bh6);
|
|
1454
|
+
mid = mid + Math.imul(ah9, bl6) | 0;
|
|
1455
|
+
hi = Math.imul(ah9, bh6);
|
|
1456
|
+
lo = lo + Math.imul(al8, bl7) | 0;
|
|
1457
|
+
mid = mid + Math.imul(al8, bh7) | 0;
|
|
1458
|
+
mid = mid + Math.imul(ah8, bl7) | 0;
|
|
1459
|
+
hi = hi + Math.imul(ah8, bh7) | 0;
|
|
1460
|
+
lo = lo + Math.imul(al7, bl8) | 0;
|
|
1461
|
+
mid = mid + Math.imul(al7, bh8) | 0;
|
|
1462
|
+
mid = mid + Math.imul(ah7, bl8) | 0;
|
|
1463
|
+
hi = hi + Math.imul(ah7, bh8) | 0;
|
|
1464
|
+
lo = lo + Math.imul(al6, bl9) | 0;
|
|
1465
|
+
mid = mid + Math.imul(al6, bh9) | 0;
|
|
1466
|
+
mid = mid + Math.imul(ah6, bl9) | 0;
|
|
1467
|
+
hi = hi + Math.imul(ah6, bh9) | 0;
|
|
1468
|
+
var w15 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1469
|
+
c = (hi + (mid >>> 13) | 0) + (w15 >>> 26) | 0;
|
|
1470
|
+
w15 &= 67108863;
|
|
1471
|
+
lo = Math.imul(al9, bl7);
|
|
1472
|
+
mid = Math.imul(al9, bh7);
|
|
1473
|
+
mid = mid + Math.imul(ah9, bl7) | 0;
|
|
1474
|
+
hi = Math.imul(ah9, bh7);
|
|
1475
|
+
lo = lo + Math.imul(al8, bl8) | 0;
|
|
1476
|
+
mid = mid + Math.imul(al8, bh8) | 0;
|
|
1477
|
+
mid = mid + Math.imul(ah8, bl8) | 0;
|
|
1478
|
+
hi = hi + Math.imul(ah8, bh8) | 0;
|
|
1479
|
+
lo = lo + Math.imul(al7, bl9) | 0;
|
|
1480
|
+
mid = mid + Math.imul(al7, bh9) | 0;
|
|
1481
|
+
mid = mid + Math.imul(ah7, bl9) | 0;
|
|
1482
|
+
hi = hi + Math.imul(ah7, bh9) | 0;
|
|
1483
|
+
var w16 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1484
|
+
c = (hi + (mid >>> 13) | 0) + (w16 >>> 26) | 0;
|
|
1485
|
+
w16 &= 67108863;
|
|
1486
|
+
lo = Math.imul(al9, bl8);
|
|
1487
|
+
mid = Math.imul(al9, bh8);
|
|
1488
|
+
mid = mid + Math.imul(ah9, bl8) | 0;
|
|
1489
|
+
hi = Math.imul(ah9, bh8);
|
|
1490
|
+
lo = lo + Math.imul(al8, bl9) | 0;
|
|
1491
|
+
mid = mid + Math.imul(al8, bh9) | 0;
|
|
1492
|
+
mid = mid + Math.imul(ah8, bl9) | 0;
|
|
1493
|
+
hi = hi + Math.imul(ah8, bh9) | 0;
|
|
1494
|
+
var w17 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1495
|
+
c = (hi + (mid >>> 13) | 0) + (w17 >>> 26) | 0;
|
|
1496
|
+
w17 &= 67108863;
|
|
1497
|
+
lo = Math.imul(al9, bl9);
|
|
1498
|
+
mid = Math.imul(al9, bh9);
|
|
1499
|
+
mid = mid + Math.imul(ah9, bl9) | 0;
|
|
1500
|
+
hi = Math.imul(ah9, bh9);
|
|
1501
|
+
var w18 = (c + lo | 0) + ((mid & 8191) << 13) | 0;
|
|
1502
|
+
c = (hi + (mid >>> 13) | 0) + (w18 >>> 26) | 0;
|
|
1503
|
+
w18 &= 67108863;
|
|
1504
|
+
o[0] = w0;
|
|
1505
|
+
o[1] = w1;
|
|
1506
|
+
o[2] = w2;
|
|
1507
|
+
o[3] = w3;
|
|
1508
|
+
o[4] = w4;
|
|
1509
|
+
o[5] = w5;
|
|
1510
|
+
o[6] = w6;
|
|
1511
|
+
o[7] = w7;
|
|
1512
|
+
o[8] = w8;
|
|
1513
|
+
o[9] = w9;
|
|
1514
|
+
o[10] = w10;
|
|
1515
|
+
o[11] = w11;
|
|
1516
|
+
o[12] = w12;
|
|
1517
|
+
o[13] = w13;
|
|
1518
|
+
o[14] = w14;
|
|
1519
|
+
o[15] = w15;
|
|
1520
|
+
o[16] = w16;
|
|
1521
|
+
o[17] = w17;
|
|
1522
|
+
o[18] = w18;
|
|
1523
|
+
if (c !== 0) {
|
|
1524
|
+
o[19] = c;
|
|
1525
|
+
out.length++;
|
|
1526
|
+
}
|
|
1527
|
+
return out;
|
|
1528
|
+
};
|
|
1529
|
+
if (!Math.imul) {
|
|
1530
|
+
comb10MulTo = smallMulTo;
|
|
1531
|
+
}
|
|
1532
|
+
function bigMulTo(self, num, out) {
|
|
1533
|
+
out.negative = num.negative ^ self.negative;
|
|
1534
|
+
out.length = self.length + num.length;
|
|
1535
|
+
var carry = 0;
|
|
1536
|
+
var hncarry = 0;
|
|
1537
|
+
for (var k = 0; k < out.length - 1; k++) {
|
|
1538
|
+
var ncarry = hncarry;
|
|
1539
|
+
hncarry = 0;
|
|
1540
|
+
var rword = carry & 67108863;
|
|
1541
|
+
var maxJ = Math.min(k, num.length - 1);
|
|
1542
|
+
for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
|
|
1543
|
+
var i = k - j;
|
|
1544
|
+
var a = self.words[i] | 0;
|
|
1545
|
+
var b = num.words[j] | 0;
|
|
1546
|
+
var r = a * b;
|
|
1547
|
+
var lo = r & 67108863;
|
|
1548
|
+
ncarry = ncarry + (r / 67108864 | 0) | 0;
|
|
1549
|
+
lo = lo + rword | 0;
|
|
1550
|
+
rword = lo & 67108863;
|
|
1551
|
+
ncarry = ncarry + (lo >>> 26) | 0;
|
|
1552
|
+
hncarry += ncarry >>> 26;
|
|
1553
|
+
ncarry &= 67108863;
|
|
1554
|
+
}
|
|
1555
|
+
out.words[k] = rword;
|
|
1556
|
+
carry = ncarry;
|
|
1557
|
+
ncarry = hncarry;
|
|
1558
|
+
}
|
|
1559
|
+
if (carry !== 0) {
|
|
1560
|
+
out.words[k] = carry;
|
|
1561
|
+
} else {
|
|
1562
|
+
out.length--;
|
|
1563
|
+
}
|
|
1564
|
+
return out._strip();
|
|
1565
|
+
}
|
|
1566
|
+
function jumboMulTo(self, num, out) {
|
|
1567
|
+
return bigMulTo(self, num, out);
|
|
1568
|
+
}
|
|
1569
|
+
BN3.prototype.mulTo = function mulTo(num, out) {
|
|
1570
|
+
var res;
|
|
1571
|
+
var len = this.length + num.length;
|
|
1572
|
+
if (this.length === 10 && num.length === 10) {
|
|
1573
|
+
res = comb10MulTo(this, num, out);
|
|
1574
|
+
} else if (len < 63) {
|
|
1575
|
+
res = smallMulTo(this, num, out);
|
|
1576
|
+
} else if (len < 1024) {
|
|
1577
|
+
res = bigMulTo(this, num, out);
|
|
1578
|
+
} else {
|
|
1579
|
+
res = jumboMulTo(this, num, out);
|
|
1580
|
+
}
|
|
1581
|
+
return res;
|
|
1582
|
+
};
|
|
1583
|
+
function FFTM(x, y) {
|
|
1584
|
+
this.x = x;
|
|
1585
|
+
this.y = y;
|
|
1586
|
+
}
|
|
1587
|
+
FFTM.prototype.makeRBT = function makeRBT(N) {
|
|
1588
|
+
var t = new Array(N);
|
|
1589
|
+
var l = BN3.prototype._countBits(N) - 1;
|
|
1590
|
+
for (var i = 0; i < N; i++) {
|
|
1591
|
+
t[i] = this.revBin(i, l, N);
|
|
1592
|
+
}
|
|
1593
|
+
return t;
|
|
1594
|
+
};
|
|
1595
|
+
FFTM.prototype.revBin = function revBin(x, l, N) {
|
|
1596
|
+
if (x === 0 || x === N - 1) return x;
|
|
1597
|
+
var rb = 0;
|
|
1598
|
+
for (var i = 0; i < l; i++) {
|
|
1599
|
+
rb |= (x & 1) << l - i - 1;
|
|
1600
|
+
x >>= 1;
|
|
1601
|
+
}
|
|
1602
|
+
return rb;
|
|
1603
|
+
};
|
|
1604
|
+
FFTM.prototype.permute = function permute(rbt, rws, iws, rtws, itws, N) {
|
|
1605
|
+
for (var i = 0; i < N; i++) {
|
|
1606
|
+
rtws[i] = rws[rbt[i]];
|
|
1607
|
+
itws[i] = iws[rbt[i]];
|
|
1608
|
+
}
|
|
1609
|
+
};
|
|
1610
|
+
FFTM.prototype.transform = function transform(rws, iws, rtws, itws, N, rbt) {
|
|
1611
|
+
this.permute(rbt, rws, iws, rtws, itws, N);
|
|
1612
|
+
for (var s = 1; s < N; s <<= 1) {
|
|
1613
|
+
var l = s << 1;
|
|
1614
|
+
var rtwdf = Math.cos(2 * Math.PI / l);
|
|
1615
|
+
var itwdf = Math.sin(2 * Math.PI / l);
|
|
1616
|
+
for (var p = 0; p < N; p += l) {
|
|
1617
|
+
var rtwdf_ = rtwdf;
|
|
1618
|
+
var itwdf_ = itwdf;
|
|
1619
|
+
for (var j = 0; j < s; j++) {
|
|
1620
|
+
var re = rtws[p + j];
|
|
1621
|
+
var ie = itws[p + j];
|
|
1622
|
+
var ro = rtws[p + j + s];
|
|
1623
|
+
var io = itws[p + j + s];
|
|
1624
|
+
var rx = rtwdf_ * ro - itwdf_ * io;
|
|
1625
|
+
io = rtwdf_ * io + itwdf_ * ro;
|
|
1626
|
+
ro = rx;
|
|
1627
|
+
rtws[p + j] = re + ro;
|
|
1628
|
+
itws[p + j] = ie + io;
|
|
1629
|
+
rtws[p + j + s] = re - ro;
|
|
1630
|
+
itws[p + j + s] = ie - io;
|
|
1631
|
+
if (j !== l) {
|
|
1632
|
+
rx = rtwdf * rtwdf_ - itwdf * itwdf_;
|
|
1633
|
+
itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_;
|
|
1634
|
+
rtwdf_ = rx;
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
};
|
|
1640
|
+
FFTM.prototype.guessLen13b = function guessLen13b(n, m) {
|
|
1641
|
+
var N = Math.max(m, n) | 1;
|
|
1642
|
+
var odd = N & 1;
|
|
1643
|
+
var i = 0;
|
|
1644
|
+
for (N = N / 2 | 0; N; N = N >>> 1) {
|
|
1645
|
+
i++;
|
|
1646
|
+
}
|
|
1647
|
+
return 1 << i + 1 + odd;
|
|
1648
|
+
};
|
|
1649
|
+
FFTM.prototype.conjugate = function conjugate(rws, iws, N) {
|
|
1650
|
+
if (N <= 1) return;
|
|
1651
|
+
for (var i = 0; i < N / 2; i++) {
|
|
1652
|
+
var t = rws[i];
|
|
1653
|
+
rws[i] = rws[N - i - 1];
|
|
1654
|
+
rws[N - i - 1] = t;
|
|
1655
|
+
t = iws[i];
|
|
1656
|
+
iws[i] = -iws[N - i - 1];
|
|
1657
|
+
iws[N - i - 1] = -t;
|
|
1658
|
+
}
|
|
1659
|
+
};
|
|
1660
|
+
FFTM.prototype.normalize13b = function normalize13b(ws, N) {
|
|
1661
|
+
var carry = 0;
|
|
1662
|
+
for (var i = 0; i < N / 2; i++) {
|
|
1663
|
+
var w = Math.round(ws[2 * i + 1] / N) * 8192 + Math.round(ws[2 * i] / N) + carry;
|
|
1664
|
+
ws[i] = w & 67108863;
|
|
1665
|
+
if (w < 67108864) {
|
|
1666
|
+
carry = 0;
|
|
1667
|
+
} else {
|
|
1668
|
+
carry = w / 67108864 | 0;
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
return ws;
|
|
1672
|
+
};
|
|
1673
|
+
FFTM.prototype.convert13b = function convert13b(ws, len, rws, N) {
|
|
1674
|
+
var carry = 0;
|
|
1675
|
+
for (var i = 0; i < len; i++) {
|
|
1676
|
+
carry = carry + (ws[i] | 0);
|
|
1677
|
+
rws[2 * i] = carry & 8191;
|
|
1678
|
+
carry = carry >>> 13;
|
|
1679
|
+
rws[2 * i + 1] = carry & 8191;
|
|
1680
|
+
carry = carry >>> 13;
|
|
1681
|
+
}
|
|
1682
|
+
for (i = 2 * len; i < N; ++i) {
|
|
1683
|
+
rws[i] = 0;
|
|
1684
|
+
}
|
|
1685
|
+
assert(carry === 0);
|
|
1686
|
+
assert((carry & ~8191) === 0);
|
|
1687
|
+
};
|
|
1688
|
+
FFTM.prototype.stub = function stub(N) {
|
|
1689
|
+
var ph = new Array(N);
|
|
1690
|
+
for (var i = 0; i < N; i++) {
|
|
1691
|
+
ph[i] = 0;
|
|
1692
|
+
}
|
|
1693
|
+
return ph;
|
|
1694
|
+
};
|
|
1695
|
+
FFTM.prototype.mulp = function mulp(x, y, out) {
|
|
1696
|
+
var N = 2 * this.guessLen13b(x.length, y.length);
|
|
1697
|
+
var rbt = this.makeRBT(N);
|
|
1698
|
+
var _ = this.stub(N);
|
|
1699
|
+
var rws = new Array(N);
|
|
1700
|
+
var rwst = new Array(N);
|
|
1701
|
+
var iwst = new Array(N);
|
|
1702
|
+
var nrws = new Array(N);
|
|
1703
|
+
var nrwst = new Array(N);
|
|
1704
|
+
var niwst = new Array(N);
|
|
1705
|
+
var rmws = out.words;
|
|
1706
|
+
rmws.length = N;
|
|
1707
|
+
this.convert13b(x.words, x.length, rws, N);
|
|
1708
|
+
this.convert13b(y.words, y.length, nrws, N);
|
|
1709
|
+
this.transform(rws, _, rwst, iwst, N, rbt);
|
|
1710
|
+
this.transform(nrws, _, nrwst, niwst, N, rbt);
|
|
1711
|
+
for (var i = 0; i < N; i++) {
|
|
1712
|
+
var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i];
|
|
1713
|
+
iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i];
|
|
1714
|
+
rwst[i] = rx;
|
|
1715
|
+
}
|
|
1716
|
+
this.conjugate(rwst, iwst, N);
|
|
1717
|
+
this.transform(rwst, iwst, rmws, _, N, rbt);
|
|
1718
|
+
this.conjugate(rmws, _, N);
|
|
1719
|
+
this.normalize13b(rmws, N);
|
|
1720
|
+
out.negative = x.negative ^ y.negative;
|
|
1721
|
+
out.length = x.length + y.length;
|
|
1722
|
+
return out._strip();
|
|
1723
|
+
};
|
|
1724
|
+
BN3.prototype.mul = function mul(num) {
|
|
1725
|
+
var out = new BN3(null);
|
|
1726
|
+
out.words = new Array(this.length + num.length);
|
|
1727
|
+
return this.mulTo(num, out);
|
|
1728
|
+
};
|
|
1729
|
+
BN3.prototype.mulf = function mulf(num) {
|
|
1730
|
+
var out = new BN3(null);
|
|
1731
|
+
out.words = new Array(this.length + num.length);
|
|
1732
|
+
return jumboMulTo(this, num, out);
|
|
1733
|
+
};
|
|
1734
|
+
BN3.prototype.imul = function imul(num) {
|
|
1735
|
+
return this.clone().mulTo(num, this);
|
|
1736
|
+
};
|
|
1737
|
+
BN3.prototype.imuln = function imuln(num) {
|
|
1738
|
+
var isNegNum = num < 0;
|
|
1739
|
+
if (isNegNum) num = -num;
|
|
1740
|
+
assert(typeof num === "number");
|
|
1741
|
+
assert(num < 67108864);
|
|
1742
|
+
var carry = 0;
|
|
1743
|
+
for (var i = 0; i < this.length; i++) {
|
|
1744
|
+
var w = (this.words[i] | 0) * num;
|
|
1745
|
+
var lo = (w & 67108863) + (carry & 67108863);
|
|
1746
|
+
carry >>= 26;
|
|
1747
|
+
carry += w / 67108864 | 0;
|
|
1748
|
+
carry += lo >>> 26;
|
|
1749
|
+
this.words[i] = lo & 67108863;
|
|
1750
|
+
}
|
|
1751
|
+
if (carry !== 0) {
|
|
1752
|
+
this.words[i] = carry;
|
|
1753
|
+
this.length++;
|
|
1754
|
+
}
|
|
1755
|
+
this.length = num === 0 ? 1 : this.length;
|
|
1756
|
+
return isNegNum ? this.ineg() : this;
|
|
1757
|
+
};
|
|
1758
|
+
BN3.prototype.muln = function muln(num) {
|
|
1759
|
+
return this.clone().imuln(num);
|
|
1760
|
+
};
|
|
1761
|
+
BN3.prototype.sqr = function sqr() {
|
|
1762
|
+
return this.mul(this);
|
|
1763
|
+
};
|
|
1764
|
+
BN3.prototype.isqr = function isqr() {
|
|
1765
|
+
return this.imul(this.clone());
|
|
1766
|
+
};
|
|
1767
|
+
BN3.prototype.pow = function pow(num) {
|
|
1768
|
+
var w = toBitArray(num);
|
|
1769
|
+
if (w.length === 0) return new BN3(1);
|
|
1770
|
+
var res = this;
|
|
1771
|
+
for (var i = 0; i < w.length; i++, res = res.sqr()) {
|
|
1772
|
+
if (w[i] !== 0) break;
|
|
1773
|
+
}
|
|
1774
|
+
if (++i < w.length) {
|
|
1775
|
+
for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
|
|
1776
|
+
if (w[i] === 0) continue;
|
|
1777
|
+
res = res.mul(q);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
return res;
|
|
1781
|
+
};
|
|
1782
|
+
BN3.prototype.iushln = function iushln(bits) {
|
|
1783
|
+
assert(typeof bits === "number" && bits >= 0);
|
|
1784
|
+
var r = bits % 26;
|
|
1785
|
+
var s = (bits - r) / 26;
|
|
1786
|
+
var carryMask = 67108863 >>> 26 - r << 26 - r;
|
|
1787
|
+
var i;
|
|
1788
|
+
if (r !== 0) {
|
|
1789
|
+
var carry = 0;
|
|
1790
|
+
for (i = 0; i < this.length; i++) {
|
|
1791
|
+
var newCarry = this.words[i] & carryMask;
|
|
1792
|
+
var c = (this.words[i] | 0) - newCarry << r;
|
|
1793
|
+
this.words[i] = c | carry;
|
|
1794
|
+
carry = newCarry >>> 26 - r;
|
|
1795
|
+
}
|
|
1796
|
+
if (carry) {
|
|
1797
|
+
this.words[i] = carry;
|
|
1798
|
+
this.length++;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
if (s !== 0) {
|
|
1802
|
+
for (i = this.length - 1; i >= 0; i--) {
|
|
1803
|
+
this.words[i + s] = this.words[i];
|
|
1804
|
+
}
|
|
1805
|
+
for (i = 0; i < s; i++) {
|
|
1806
|
+
this.words[i] = 0;
|
|
1807
|
+
}
|
|
1808
|
+
this.length += s;
|
|
1809
|
+
}
|
|
1810
|
+
return this._strip();
|
|
1811
|
+
};
|
|
1812
|
+
BN3.prototype.ishln = function ishln(bits) {
|
|
1813
|
+
assert(this.negative === 0);
|
|
1814
|
+
return this.iushln(bits);
|
|
1815
|
+
};
|
|
1816
|
+
BN3.prototype.iushrn = function iushrn(bits, hint, extended) {
|
|
1817
|
+
assert(typeof bits === "number" && bits >= 0);
|
|
1818
|
+
var h;
|
|
1819
|
+
if (hint) {
|
|
1820
|
+
h = (hint - hint % 26) / 26;
|
|
1821
|
+
} else {
|
|
1822
|
+
h = 0;
|
|
1823
|
+
}
|
|
1824
|
+
var r = bits % 26;
|
|
1825
|
+
var s = Math.min((bits - r) / 26, this.length);
|
|
1826
|
+
var mask = 67108863 ^ 67108863 >>> r << r;
|
|
1827
|
+
var maskedWords = extended;
|
|
1828
|
+
h -= s;
|
|
1829
|
+
h = Math.max(0, h);
|
|
1830
|
+
if (maskedWords) {
|
|
1831
|
+
for (var i = 0; i < s; i++) {
|
|
1832
|
+
maskedWords.words[i] = this.words[i];
|
|
1833
|
+
}
|
|
1834
|
+
maskedWords.length = s;
|
|
1835
|
+
}
|
|
1836
|
+
if (s === 0) {
|
|
1837
|
+
} else if (this.length > s) {
|
|
1838
|
+
this.length -= s;
|
|
1839
|
+
for (i = 0; i < this.length; i++) {
|
|
1840
|
+
this.words[i] = this.words[i + s];
|
|
1841
|
+
}
|
|
1842
|
+
} else {
|
|
1843
|
+
this.words[0] = 0;
|
|
1844
|
+
this.length = 1;
|
|
1845
|
+
}
|
|
1846
|
+
var carry = 0;
|
|
1847
|
+
for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
|
|
1848
|
+
var word = this.words[i] | 0;
|
|
1849
|
+
this.words[i] = carry << 26 - r | word >>> r;
|
|
1850
|
+
carry = word & mask;
|
|
1851
|
+
}
|
|
1852
|
+
if (maskedWords && carry !== 0) {
|
|
1853
|
+
maskedWords.words[maskedWords.length++] = carry;
|
|
1854
|
+
}
|
|
1855
|
+
if (this.length === 0) {
|
|
1856
|
+
this.words[0] = 0;
|
|
1857
|
+
this.length = 1;
|
|
1858
|
+
}
|
|
1859
|
+
return this._strip();
|
|
1860
|
+
};
|
|
1861
|
+
BN3.prototype.ishrn = function ishrn(bits, hint, extended) {
|
|
1862
|
+
assert(this.negative === 0);
|
|
1863
|
+
return this.iushrn(bits, hint, extended);
|
|
1864
|
+
};
|
|
1865
|
+
BN3.prototype.shln = function shln(bits) {
|
|
1866
|
+
return this.clone().ishln(bits);
|
|
1867
|
+
};
|
|
1868
|
+
BN3.prototype.ushln = function ushln(bits) {
|
|
1869
|
+
return this.clone().iushln(bits);
|
|
1870
|
+
};
|
|
1871
|
+
BN3.prototype.shrn = function shrn(bits) {
|
|
1872
|
+
return this.clone().ishrn(bits);
|
|
1873
|
+
};
|
|
1874
|
+
BN3.prototype.ushrn = function ushrn(bits) {
|
|
1875
|
+
return this.clone().iushrn(bits);
|
|
1876
|
+
};
|
|
1877
|
+
BN3.prototype.testn = function testn(bit) {
|
|
1878
|
+
assert(typeof bit === "number" && bit >= 0);
|
|
1879
|
+
var r = bit % 26;
|
|
1880
|
+
var s = (bit - r) / 26;
|
|
1881
|
+
var q = 1 << r;
|
|
1882
|
+
if (this.length <= s) return false;
|
|
1883
|
+
var w = this.words[s];
|
|
1884
|
+
return !!(w & q);
|
|
1885
|
+
};
|
|
1886
|
+
BN3.prototype.imaskn = function imaskn(bits) {
|
|
1887
|
+
assert(typeof bits === "number" && bits >= 0);
|
|
1888
|
+
var r = bits % 26;
|
|
1889
|
+
var s = (bits - r) / 26;
|
|
1890
|
+
assert(this.negative === 0, "imaskn works only with positive numbers");
|
|
1891
|
+
if (this.length <= s) {
|
|
1892
|
+
return this;
|
|
1893
|
+
}
|
|
1894
|
+
if (r !== 0) {
|
|
1895
|
+
s++;
|
|
1896
|
+
}
|
|
1897
|
+
this.length = Math.min(s, this.length);
|
|
1898
|
+
if (r !== 0) {
|
|
1899
|
+
var mask = 67108863 ^ 67108863 >>> r << r;
|
|
1900
|
+
this.words[this.length - 1] &= mask;
|
|
1901
|
+
}
|
|
1902
|
+
return this._strip();
|
|
1903
|
+
};
|
|
1904
|
+
BN3.prototype.maskn = function maskn(bits) {
|
|
1905
|
+
return this.clone().imaskn(bits);
|
|
1906
|
+
};
|
|
1907
|
+
BN3.prototype.iaddn = function iaddn(num) {
|
|
1908
|
+
assert(typeof num === "number");
|
|
1909
|
+
assert(num < 67108864);
|
|
1910
|
+
if (num < 0) return this.isubn(-num);
|
|
1911
|
+
if (this.negative !== 0) {
|
|
1912
|
+
if (this.length === 1 && (this.words[0] | 0) <= num) {
|
|
1913
|
+
this.words[0] = num - (this.words[0] | 0);
|
|
1914
|
+
this.negative = 0;
|
|
1915
|
+
return this;
|
|
1916
|
+
}
|
|
1917
|
+
this.negative = 0;
|
|
1918
|
+
this.isubn(num);
|
|
1919
|
+
this.negative = 1;
|
|
1920
|
+
return this;
|
|
1921
|
+
}
|
|
1922
|
+
return this._iaddn(num);
|
|
1923
|
+
};
|
|
1924
|
+
BN3.prototype._iaddn = function _iaddn(num) {
|
|
1925
|
+
this.words[0] += num;
|
|
1926
|
+
for (var i = 0; i < this.length && this.words[i] >= 67108864; i++) {
|
|
1927
|
+
this.words[i] -= 67108864;
|
|
1928
|
+
if (i === this.length - 1) {
|
|
1929
|
+
this.words[i + 1] = 1;
|
|
1930
|
+
} else {
|
|
1931
|
+
this.words[i + 1]++;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
this.length = Math.max(this.length, i + 1);
|
|
1935
|
+
return this;
|
|
1936
|
+
};
|
|
1937
|
+
BN3.prototype.isubn = function isubn(num) {
|
|
1938
|
+
assert(typeof num === "number");
|
|
1939
|
+
assert(num < 67108864);
|
|
1940
|
+
if (num < 0) return this.iaddn(-num);
|
|
1941
|
+
if (this.negative !== 0) {
|
|
1942
|
+
this.negative = 0;
|
|
1943
|
+
this.iaddn(num);
|
|
1944
|
+
this.negative = 1;
|
|
1945
|
+
return this;
|
|
1946
|
+
}
|
|
1947
|
+
this.words[0] -= num;
|
|
1948
|
+
if (this.length === 1 && this.words[0] < 0) {
|
|
1949
|
+
this.words[0] = -this.words[0];
|
|
1950
|
+
this.negative = 1;
|
|
1951
|
+
} else {
|
|
1952
|
+
for (var i = 0; i < this.length && this.words[i] < 0; i++) {
|
|
1953
|
+
this.words[i] += 67108864;
|
|
1954
|
+
this.words[i + 1] -= 1;
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
return this._strip();
|
|
1958
|
+
};
|
|
1959
|
+
BN3.prototype.addn = function addn(num) {
|
|
1960
|
+
return this.clone().iaddn(num);
|
|
1961
|
+
};
|
|
1962
|
+
BN3.prototype.subn = function subn(num) {
|
|
1963
|
+
return this.clone().isubn(num);
|
|
1964
|
+
};
|
|
1965
|
+
BN3.prototype.iabs = function iabs() {
|
|
1966
|
+
this.negative = 0;
|
|
1967
|
+
return this;
|
|
1968
|
+
};
|
|
1969
|
+
BN3.prototype.abs = function abs() {
|
|
1970
|
+
return this.clone().iabs();
|
|
1971
|
+
};
|
|
1972
|
+
BN3.prototype._ishlnsubmul = function _ishlnsubmul(num, mul, shift) {
|
|
1973
|
+
var len = num.length + shift;
|
|
1974
|
+
var i;
|
|
1975
|
+
this._expand(len);
|
|
1976
|
+
var w;
|
|
1977
|
+
var carry = 0;
|
|
1978
|
+
for (i = 0; i < num.length; i++) {
|
|
1979
|
+
w = (this.words[i + shift] | 0) + carry;
|
|
1980
|
+
var right = (num.words[i] | 0) * mul;
|
|
1981
|
+
w -= right & 67108863;
|
|
1982
|
+
carry = (w >> 26) - (right / 67108864 | 0);
|
|
1983
|
+
this.words[i + shift] = w & 67108863;
|
|
1984
|
+
}
|
|
1985
|
+
for (; i < this.length - shift; i++) {
|
|
1986
|
+
w = (this.words[i + shift] | 0) + carry;
|
|
1987
|
+
carry = w >> 26;
|
|
1988
|
+
this.words[i + shift] = w & 67108863;
|
|
1989
|
+
}
|
|
1990
|
+
if (carry === 0) return this._strip();
|
|
1991
|
+
assert(carry === -1);
|
|
1992
|
+
carry = 0;
|
|
1993
|
+
for (i = 0; i < this.length; i++) {
|
|
1994
|
+
w = -(this.words[i] | 0) + carry;
|
|
1995
|
+
carry = w >> 26;
|
|
1996
|
+
this.words[i] = w & 67108863;
|
|
1997
|
+
}
|
|
1998
|
+
this.negative = 1;
|
|
1999
|
+
return this._strip();
|
|
2000
|
+
};
|
|
2001
|
+
BN3.prototype._wordDiv = function _wordDiv(num, mode) {
|
|
2002
|
+
var shift = this.length - num.length;
|
|
2003
|
+
var a = this.clone();
|
|
2004
|
+
var b = num;
|
|
2005
|
+
var bhi = b.words[b.length - 1] | 0;
|
|
2006
|
+
var bhiBits = this._countBits(bhi);
|
|
2007
|
+
shift = 26 - bhiBits;
|
|
2008
|
+
if (shift !== 0) {
|
|
2009
|
+
b = b.ushln(shift);
|
|
2010
|
+
a.iushln(shift);
|
|
2011
|
+
bhi = b.words[b.length - 1] | 0;
|
|
2012
|
+
}
|
|
2013
|
+
var m = a.length - b.length;
|
|
2014
|
+
var q;
|
|
2015
|
+
if (mode !== "mod") {
|
|
2016
|
+
q = new BN3(null);
|
|
2017
|
+
q.length = m + 1;
|
|
2018
|
+
q.words = new Array(q.length);
|
|
2019
|
+
for (var i = 0; i < q.length; i++) {
|
|
2020
|
+
q.words[i] = 0;
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
var diff = a.clone()._ishlnsubmul(b, 1, m);
|
|
2024
|
+
if (diff.negative === 0) {
|
|
2025
|
+
a = diff;
|
|
2026
|
+
if (q) {
|
|
2027
|
+
q.words[m] = 1;
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
for (var j = m - 1; j >= 0; j--) {
|
|
2031
|
+
var qj = (a.words[b.length + j] | 0) * 67108864 + (a.words[b.length + j - 1] | 0);
|
|
2032
|
+
qj = Math.min(qj / bhi | 0, 67108863);
|
|
2033
|
+
a._ishlnsubmul(b, qj, j);
|
|
2034
|
+
while (a.negative !== 0) {
|
|
2035
|
+
qj--;
|
|
2036
|
+
a.negative = 0;
|
|
2037
|
+
a._ishlnsubmul(b, 1, j);
|
|
2038
|
+
if (!a.isZero()) {
|
|
2039
|
+
a.negative ^= 1;
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
if (q) {
|
|
2043
|
+
q.words[j] = qj;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
if (q) {
|
|
2047
|
+
q._strip();
|
|
2048
|
+
}
|
|
2049
|
+
a._strip();
|
|
2050
|
+
if (mode !== "div" && shift !== 0) {
|
|
2051
|
+
a.iushrn(shift);
|
|
2052
|
+
}
|
|
2053
|
+
return {
|
|
2054
|
+
div: q || null,
|
|
2055
|
+
mod: a
|
|
2056
|
+
};
|
|
2057
|
+
};
|
|
2058
|
+
BN3.prototype.divmod = function divmod(num, mode, positive) {
|
|
2059
|
+
assert(!num.isZero());
|
|
2060
|
+
if (this.isZero()) {
|
|
2061
|
+
return {
|
|
2062
|
+
div: new BN3(0),
|
|
2063
|
+
mod: new BN3(0)
|
|
2064
|
+
};
|
|
2065
|
+
}
|
|
2066
|
+
var div, mod, res;
|
|
2067
|
+
if (this.negative !== 0 && num.negative === 0) {
|
|
2068
|
+
res = this.neg().divmod(num, mode);
|
|
2069
|
+
if (mode !== "mod") {
|
|
2070
|
+
div = res.div.neg();
|
|
2071
|
+
}
|
|
2072
|
+
if (mode !== "div") {
|
|
2073
|
+
mod = res.mod.neg();
|
|
2074
|
+
if (positive && mod.negative !== 0) {
|
|
2075
|
+
mod.iadd(num);
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
return {
|
|
2079
|
+
div,
|
|
2080
|
+
mod
|
|
2081
|
+
};
|
|
2082
|
+
}
|
|
2083
|
+
if (this.negative === 0 && num.negative !== 0) {
|
|
2084
|
+
res = this.divmod(num.neg(), mode);
|
|
2085
|
+
if (mode !== "mod") {
|
|
2086
|
+
div = res.div.neg();
|
|
2087
|
+
}
|
|
2088
|
+
return {
|
|
2089
|
+
div,
|
|
2090
|
+
mod: res.mod
|
|
2091
|
+
};
|
|
2092
|
+
}
|
|
2093
|
+
if ((this.negative & num.negative) !== 0) {
|
|
2094
|
+
res = this.neg().divmod(num.neg(), mode);
|
|
2095
|
+
if (mode !== "div") {
|
|
2096
|
+
mod = res.mod.neg();
|
|
2097
|
+
if (positive && mod.negative !== 0) {
|
|
2098
|
+
mod.isub(num);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
return {
|
|
2102
|
+
div: res.div,
|
|
2103
|
+
mod
|
|
2104
|
+
};
|
|
2105
|
+
}
|
|
2106
|
+
if (num.length > this.length || this.cmp(num) < 0) {
|
|
2107
|
+
return {
|
|
2108
|
+
div: new BN3(0),
|
|
2109
|
+
mod: this
|
|
2110
|
+
};
|
|
2111
|
+
}
|
|
2112
|
+
if (num.length === 1) {
|
|
2113
|
+
if (mode === "div") {
|
|
2114
|
+
return {
|
|
2115
|
+
div: this.divn(num.words[0]),
|
|
2116
|
+
mod: null
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
if (mode === "mod") {
|
|
2120
|
+
return {
|
|
2121
|
+
div: null,
|
|
2122
|
+
mod: new BN3(this.modrn(num.words[0]))
|
|
2123
|
+
};
|
|
2124
|
+
}
|
|
2125
|
+
return {
|
|
2126
|
+
div: this.divn(num.words[0]),
|
|
2127
|
+
mod: new BN3(this.modrn(num.words[0]))
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
return this._wordDiv(num, mode);
|
|
2131
|
+
};
|
|
2132
|
+
BN3.prototype.div = function div(num) {
|
|
2133
|
+
return this.divmod(num, "div", false).div;
|
|
2134
|
+
};
|
|
2135
|
+
BN3.prototype.mod = function mod(num) {
|
|
2136
|
+
return this.divmod(num, "mod", false).mod;
|
|
2137
|
+
};
|
|
2138
|
+
BN3.prototype.umod = function umod(num) {
|
|
2139
|
+
return this.divmod(num, "mod", true).mod;
|
|
2140
|
+
};
|
|
2141
|
+
BN3.prototype.divRound = function divRound(num) {
|
|
2142
|
+
var dm = this.divmod(num);
|
|
2143
|
+
if (dm.mod.isZero()) return dm.div;
|
|
2144
|
+
var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
|
|
2145
|
+
var half = num.ushrn(1);
|
|
2146
|
+
var r2 = num.andln(1);
|
|
2147
|
+
var cmp = mod.cmp(half);
|
|
2148
|
+
if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
|
|
2149
|
+
return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
|
|
2150
|
+
};
|
|
2151
|
+
BN3.prototype.modrn = function modrn(num) {
|
|
2152
|
+
var isNegNum = num < 0;
|
|
2153
|
+
if (isNegNum) num = -num;
|
|
2154
|
+
assert(num <= 67108863);
|
|
2155
|
+
var p = (1 << 26) % num;
|
|
2156
|
+
var acc = 0;
|
|
2157
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
2158
|
+
acc = (p * acc + (this.words[i] | 0)) % num;
|
|
2159
|
+
}
|
|
2160
|
+
return isNegNum ? -acc : acc;
|
|
2161
|
+
};
|
|
2162
|
+
BN3.prototype.modn = function modn(num) {
|
|
2163
|
+
return this.modrn(num);
|
|
2164
|
+
};
|
|
2165
|
+
BN3.prototype.idivn = function idivn(num) {
|
|
2166
|
+
var isNegNum = num < 0;
|
|
2167
|
+
if (isNegNum) num = -num;
|
|
2168
|
+
assert(num <= 67108863);
|
|
2169
|
+
var carry = 0;
|
|
2170
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
2171
|
+
var w = (this.words[i] | 0) + carry * 67108864;
|
|
2172
|
+
this.words[i] = w / num | 0;
|
|
2173
|
+
carry = w % num;
|
|
2174
|
+
}
|
|
2175
|
+
this._strip();
|
|
2176
|
+
return isNegNum ? this.ineg() : this;
|
|
2177
|
+
};
|
|
2178
|
+
BN3.prototype.divn = function divn(num) {
|
|
2179
|
+
return this.clone().idivn(num);
|
|
2180
|
+
};
|
|
2181
|
+
BN3.prototype.egcd = function egcd(p) {
|
|
2182
|
+
assert(p.negative === 0);
|
|
2183
|
+
assert(!p.isZero());
|
|
2184
|
+
var x = this;
|
|
2185
|
+
var y = p.clone();
|
|
2186
|
+
if (x.negative !== 0) {
|
|
2187
|
+
x = x.umod(p);
|
|
2188
|
+
} else {
|
|
2189
|
+
x = x.clone();
|
|
2190
|
+
}
|
|
2191
|
+
var A = new BN3(1);
|
|
2192
|
+
var B = new BN3(0);
|
|
2193
|
+
var C = new BN3(0);
|
|
2194
|
+
var D = new BN3(1);
|
|
2195
|
+
var g = 0;
|
|
2196
|
+
while (x.isEven() && y.isEven()) {
|
|
2197
|
+
x.iushrn(1);
|
|
2198
|
+
y.iushrn(1);
|
|
2199
|
+
++g;
|
|
2200
|
+
}
|
|
2201
|
+
var yp = y.clone();
|
|
2202
|
+
var xp = x.clone();
|
|
2203
|
+
while (!x.isZero()) {
|
|
2204
|
+
for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1) ;
|
|
2205
|
+
if (i > 0) {
|
|
2206
|
+
x.iushrn(i);
|
|
2207
|
+
while (i-- > 0) {
|
|
2208
|
+
if (A.isOdd() || B.isOdd()) {
|
|
2209
|
+
A.iadd(yp);
|
|
2210
|
+
B.isub(xp);
|
|
2211
|
+
}
|
|
2212
|
+
A.iushrn(1);
|
|
2213
|
+
B.iushrn(1);
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1) ;
|
|
2217
|
+
if (j > 0) {
|
|
2218
|
+
y.iushrn(j);
|
|
2219
|
+
while (j-- > 0) {
|
|
2220
|
+
if (C.isOdd() || D.isOdd()) {
|
|
2221
|
+
C.iadd(yp);
|
|
2222
|
+
D.isub(xp);
|
|
2223
|
+
}
|
|
2224
|
+
C.iushrn(1);
|
|
2225
|
+
D.iushrn(1);
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
if (x.cmp(y) >= 0) {
|
|
2229
|
+
x.isub(y);
|
|
2230
|
+
A.isub(C);
|
|
2231
|
+
B.isub(D);
|
|
2232
|
+
} else {
|
|
2233
|
+
y.isub(x);
|
|
2234
|
+
C.isub(A);
|
|
2235
|
+
D.isub(B);
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
return {
|
|
2239
|
+
a: C,
|
|
2240
|
+
b: D,
|
|
2241
|
+
gcd: y.iushln(g)
|
|
2242
|
+
};
|
|
2243
|
+
};
|
|
2244
|
+
BN3.prototype._invmp = function _invmp(p) {
|
|
2245
|
+
assert(p.negative === 0);
|
|
2246
|
+
assert(!p.isZero());
|
|
2247
|
+
var a = this;
|
|
2248
|
+
var b = p.clone();
|
|
2249
|
+
if (a.negative !== 0) {
|
|
2250
|
+
a = a.umod(p);
|
|
2251
|
+
} else {
|
|
2252
|
+
a = a.clone();
|
|
2253
|
+
}
|
|
2254
|
+
var x1 = new BN3(1);
|
|
2255
|
+
var x2 = new BN3(0);
|
|
2256
|
+
var delta = b.clone();
|
|
2257
|
+
while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
|
|
2258
|
+
for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1) ;
|
|
2259
|
+
if (i > 0) {
|
|
2260
|
+
a.iushrn(i);
|
|
2261
|
+
while (i-- > 0) {
|
|
2262
|
+
if (x1.isOdd()) {
|
|
2263
|
+
x1.iadd(delta);
|
|
2264
|
+
}
|
|
2265
|
+
x1.iushrn(1);
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1) ;
|
|
2269
|
+
if (j > 0) {
|
|
2270
|
+
b.iushrn(j);
|
|
2271
|
+
while (j-- > 0) {
|
|
2272
|
+
if (x2.isOdd()) {
|
|
2273
|
+
x2.iadd(delta);
|
|
2274
|
+
}
|
|
2275
|
+
x2.iushrn(1);
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
if (a.cmp(b) >= 0) {
|
|
2279
|
+
a.isub(b);
|
|
2280
|
+
x1.isub(x2);
|
|
2281
|
+
} else {
|
|
2282
|
+
b.isub(a);
|
|
2283
|
+
x2.isub(x1);
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
var res;
|
|
2287
|
+
if (a.cmpn(1) === 0) {
|
|
2288
|
+
res = x1;
|
|
2289
|
+
} else {
|
|
2290
|
+
res = x2;
|
|
2291
|
+
}
|
|
2292
|
+
if (res.cmpn(0) < 0) {
|
|
2293
|
+
res.iadd(p);
|
|
2294
|
+
}
|
|
2295
|
+
return res;
|
|
2296
|
+
};
|
|
2297
|
+
BN3.prototype.gcd = function gcd(num) {
|
|
2298
|
+
if (this.isZero()) return num.abs();
|
|
2299
|
+
if (num.isZero()) return this.abs();
|
|
2300
|
+
var a = this.clone();
|
|
2301
|
+
var b = num.clone();
|
|
2302
|
+
a.negative = 0;
|
|
2303
|
+
b.negative = 0;
|
|
2304
|
+
for (var shift = 0; a.isEven() && b.isEven(); shift++) {
|
|
2305
|
+
a.iushrn(1);
|
|
2306
|
+
b.iushrn(1);
|
|
2307
|
+
}
|
|
2308
|
+
do {
|
|
2309
|
+
while (a.isEven()) {
|
|
2310
|
+
a.iushrn(1);
|
|
2311
|
+
}
|
|
2312
|
+
while (b.isEven()) {
|
|
2313
|
+
b.iushrn(1);
|
|
2314
|
+
}
|
|
2315
|
+
var r = a.cmp(b);
|
|
2316
|
+
if (r < 0) {
|
|
2317
|
+
var t = a;
|
|
2318
|
+
a = b;
|
|
2319
|
+
b = t;
|
|
2320
|
+
} else if (r === 0 || b.cmpn(1) === 0) {
|
|
2321
|
+
break;
|
|
2322
|
+
}
|
|
2323
|
+
a.isub(b);
|
|
2324
|
+
} while (true);
|
|
2325
|
+
return b.iushln(shift);
|
|
2326
|
+
};
|
|
2327
|
+
BN3.prototype.invm = function invm(num) {
|
|
2328
|
+
return this.egcd(num).a.umod(num);
|
|
2329
|
+
};
|
|
2330
|
+
BN3.prototype.isEven = function isEven() {
|
|
2331
|
+
return (this.words[0] & 1) === 0;
|
|
2332
|
+
};
|
|
2333
|
+
BN3.prototype.isOdd = function isOdd() {
|
|
2334
|
+
return (this.words[0] & 1) === 1;
|
|
2335
|
+
};
|
|
2336
|
+
BN3.prototype.andln = function andln(num) {
|
|
2337
|
+
return this.words[0] & num;
|
|
2338
|
+
};
|
|
2339
|
+
BN3.prototype.bincn = function bincn(bit) {
|
|
2340
|
+
assert(typeof bit === "number");
|
|
2341
|
+
var r = bit % 26;
|
|
2342
|
+
var s = (bit - r) / 26;
|
|
2343
|
+
var q = 1 << r;
|
|
2344
|
+
if (this.length <= s) {
|
|
2345
|
+
this._expand(s + 1);
|
|
2346
|
+
this.words[s] |= q;
|
|
2347
|
+
return this;
|
|
2348
|
+
}
|
|
2349
|
+
var carry = q;
|
|
2350
|
+
for (var i = s; carry !== 0 && i < this.length; i++) {
|
|
2351
|
+
var w = this.words[i] | 0;
|
|
2352
|
+
w += carry;
|
|
2353
|
+
carry = w >>> 26;
|
|
2354
|
+
w &= 67108863;
|
|
2355
|
+
this.words[i] = w;
|
|
2356
|
+
}
|
|
2357
|
+
if (carry !== 0) {
|
|
2358
|
+
this.words[i] = carry;
|
|
2359
|
+
this.length++;
|
|
2360
|
+
}
|
|
2361
|
+
return this;
|
|
2362
|
+
};
|
|
2363
|
+
BN3.prototype.isZero = function isZero() {
|
|
2364
|
+
return this.length === 1 && this.words[0] === 0;
|
|
2365
|
+
};
|
|
2366
|
+
BN3.prototype.cmpn = function cmpn(num) {
|
|
2367
|
+
var negative = num < 0;
|
|
2368
|
+
if (this.negative !== 0 && !negative) return -1;
|
|
2369
|
+
if (this.negative === 0 && negative) return 1;
|
|
2370
|
+
this._strip();
|
|
2371
|
+
var res;
|
|
2372
|
+
if (this.length > 1) {
|
|
2373
|
+
res = 1;
|
|
2374
|
+
} else {
|
|
2375
|
+
if (negative) {
|
|
2376
|
+
num = -num;
|
|
2377
|
+
}
|
|
2378
|
+
assert(num <= 67108863, "Number is too big");
|
|
2379
|
+
var w = this.words[0] | 0;
|
|
2380
|
+
res = w === num ? 0 : w < num ? -1 : 1;
|
|
2381
|
+
}
|
|
2382
|
+
if (this.negative !== 0) return -res | 0;
|
|
2383
|
+
return res;
|
|
2384
|
+
};
|
|
2385
|
+
BN3.prototype.cmp = function cmp(num) {
|
|
2386
|
+
if (this.negative !== 0 && num.negative === 0) return -1;
|
|
2387
|
+
if (this.negative === 0 && num.negative !== 0) return 1;
|
|
2388
|
+
var res = this.ucmp(num);
|
|
2389
|
+
if (this.negative !== 0) return -res | 0;
|
|
2390
|
+
return res;
|
|
2391
|
+
};
|
|
2392
|
+
BN3.prototype.ucmp = function ucmp(num) {
|
|
2393
|
+
if (this.length > num.length) return 1;
|
|
2394
|
+
if (this.length < num.length) return -1;
|
|
2395
|
+
var res = 0;
|
|
2396
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
2397
|
+
var a = this.words[i] | 0;
|
|
2398
|
+
var b = num.words[i] | 0;
|
|
2399
|
+
if (a === b) continue;
|
|
2400
|
+
if (a < b) {
|
|
2401
|
+
res = -1;
|
|
2402
|
+
} else if (a > b) {
|
|
2403
|
+
res = 1;
|
|
2404
|
+
}
|
|
2405
|
+
break;
|
|
2406
|
+
}
|
|
2407
|
+
return res;
|
|
2408
|
+
};
|
|
2409
|
+
BN3.prototype.gtn = function gtn(num) {
|
|
2410
|
+
return this.cmpn(num) === 1;
|
|
2411
|
+
};
|
|
2412
|
+
BN3.prototype.gt = function gt(num) {
|
|
2413
|
+
return this.cmp(num) === 1;
|
|
2414
|
+
};
|
|
2415
|
+
BN3.prototype.gten = function gten(num) {
|
|
2416
|
+
return this.cmpn(num) >= 0;
|
|
2417
|
+
};
|
|
2418
|
+
BN3.prototype.gte = function gte(num) {
|
|
2419
|
+
return this.cmp(num) >= 0;
|
|
2420
|
+
};
|
|
2421
|
+
BN3.prototype.ltn = function ltn(num) {
|
|
2422
|
+
return this.cmpn(num) === -1;
|
|
2423
|
+
};
|
|
2424
|
+
BN3.prototype.lt = function lt(num) {
|
|
2425
|
+
return this.cmp(num) === -1;
|
|
2426
|
+
};
|
|
2427
|
+
BN3.prototype.lten = function lten(num) {
|
|
2428
|
+
return this.cmpn(num) <= 0;
|
|
2429
|
+
};
|
|
2430
|
+
BN3.prototype.lte = function lte(num) {
|
|
2431
|
+
return this.cmp(num) <= 0;
|
|
2432
|
+
};
|
|
2433
|
+
BN3.prototype.eqn = function eqn(num) {
|
|
2434
|
+
return this.cmpn(num) === 0;
|
|
2435
|
+
};
|
|
2436
|
+
BN3.prototype.eq = function eq(num) {
|
|
2437
|
+
return this.cmp(num) === 0;
|
|
2438
|
+
};
|
|
2439
|
+
BN3.red = function red(num) {
|
|
2440
|
+
return new Red(num);
|
|
2441
|
+
};
|
|
2442
|
+
BN3.prototype.toRed = function toRed(ctx) {
|
|
2443
|
+
assert(!this.red, "Already a number in reduction context");
|
|
2444
|
+
assert(this.negative === 0, "red works only with positives");
|
|
2445
|
+
return ctx.convertTo(this)._forceRed(ctx);
|
|
2446
|
+
};
|
|
2447
|
+
BN3.prototype.fromRed = function fromRed() {
|
|
2448
|
+
assert(this.red, "fromRed works only with numbers in reduction context");
|
|
2449
|
+
return this.red.convertFrom(this);
|
|
2450
|
+
};
|
|
2451
|
+
BN3.prototype._forceRed = function _forceRed(ctx) {
|
|
2452
|
+
this.red = ctx;
|
|
2453
|
+
return this;
|
|
2454
|
+
};
|
|
2455
|
+
BN3.prototype.forceRed = function forceRed(ctx) {
|
|
2456
|
+
assert(!this.red, "Already a number in reduction context");
|
|
2457
|
+
return this._forceRed(ctx);
|
|
2458
|
+
};
|
|
2459
|
+
BN3.prototype.redAdd = function redAdd(num) {
|
|
2460
|
+
assert(this.red, "redAdd works only with red numbers");
|
|
2461
|
+
return this.red.add(this, num);
|
|
2462
|
+
};
|
|
2463
|
+
BN3.prototype.redIAdd = function redIAdd(num) {
|
|
2464
|
+
assert(this.red, "redIAdd works only with red numbers");
|
|
2465
|
+
return this.red.iadd(this, num);
|
|
2466
|
+
};
|
|
2467
|
+
BN3.prototype.redSub = function redSub(num) {
|
|
2468
|
+
assert(this.red, "redSub works only with red numbers");
|
|
2469
|
+
return this.red.sub(this, num);
|
|
2470
|
+
};
|
|
2471
|
+
BN3.prototype.redISub = function redISub(num) {
|
|
2472
|
+
assert(this.red, "redISub works only with red numbers");
|
|
2473
|
+
return this.red.isub(this, num);
|
|
2474
|
+
};
|
|
2475
|
+
BN3.prototype.redShl = function redShl(num) {
|
|
2476
|
+
assert(this.red, "redShl works only with red numbers");
|
|
2477
|
+
return this.red.shl(this, num);
|
|
2478
|
+
};
|
|
2479
|
+
BN3.prototype.redMul = function redMul(num) {
|
|
2480
|
+
assert(this.red, "redMul works only with red numbers");
|
|
2481
|
+
this.red._verify2(this, num);
|
|
2482
|
+
return this.red.mul(this, num);
|
|
2483
|
+
};
|
|
2484
|
+
BN3.prototype.redIMul = function redIMul(num) {
|
|
2485
|
+
assert(this.red, "redMul works only with red numbers");
|
|
2486
|
+
this.red._verify2(this, num);
|
|
2487
|
+
return this.red.imul(this, num);
|
|
2488
|
+
};
|
|
2489
|
+
BN3.prototype.redSqr = function redSqr() {
|
|
2490
|
+
assert(this.red, "redSqr works only with red numbers");
|
|
2491
|
+
this.red._verify1(this);
|
|
2492
|
+
return this.red.sqr(this);
|
|
2493
|
+
};
|
|
2494
|
+
BN3.prototype.redISqr = function redISqr() {
|
|
2495
|
+
assert(this.red, "redISqr works only with red numbers");
|
|
2496
|
+
this.red._verify1(this);
|
|
2497
|
+
return this.red.isqr(this);
|
|
2498
|
+
};
|
|
2499
|
+
BN3.prototype.redSqrt = function redSqrt() {
|
|
2500
|
+
assert(this.red, "redSqrt works only with red numbers");
|
|
2501
|
+
this.red._verify1(this);
|
|
2502
|
+
return this.red.sqrt(this);
|
|
2503
|
+
};
|
|
2504
|
+
BN3.prototype.redInvm = function redInvm() {
|
|
2505
|
+
assert(this.red, "redInvm works only with red numbers");
|
|
2506
|
+
this.red._verify1(this);
|
|
2507
|
+
return this.red.invm(this);
|
|
2508
|
+
};
|
|
2509
|
+
BN3.prototype.redNeg = function redNeg() {
|
|
2510
|
+
assert(this.red, "redNeg works only with red numbers");
|
|
2511
|
+
this.red._verify1(this);
|
|
2512
|
+
return this.red.neg(this);
|
|
2513
|
+
};
|
|
2514
|
+
BN3.prototype.redPow = function redPow(num) {
|
|
2515
|
+
assert(this.red && !num.red, "redPow(normalNum)");
|
|
2516
|
+
this.red._verify1(this);
|
|
2517
|
+
return this.red.pow(this, num);
|
|
2518
|
+
};
|
|
2519
|
+
var primes = {
|
|
2520
|
+
k256: null,
|
|
2521
|
+
p224: null,
|
|
2522
|
+
p192: null,
|
|
2523
|
+
p25519: null
|
|
2524
|
+
};
|
|
2525
|
+
function MPrime(name, p) {
|
|
2526
|
+
this.name = name;
|
|
2527
|
+
this.p = new BN3(p, 16);
|
|
2528
|
+
this.n = this.p.bitLength();
|
|
2529
|
+
this.k = new BN3(1).iushln(this.n).isub(this.p);
|
|
2530
|
+
this.tmp = this._tmp();
|
|
2531
|
+
}
|
|
2532
|
+
MPrime.prototype._tmp = function _tmp() {
|
|
2533
|
+
var tmp = new BN3(null);
|
|
2534
|
+
tmp.words = new Array(Math.ceil(this.n / 13));
|
|
2535
|
+
return tmp;
|
|
2536
|
+
};
|
|
2537
|
+
MPrime.prototype.ireduce = function ireduce(num) {
|
|
2538
|
+
var r = num;
|
|
2539
|
+
var rlen;
|
|
2540
|
+
do {
|
|
2541
|
+
this.split(r, this.tmp);
|
|
2542
|
+
r = this.imulK(r);
|
|
2543
|
+
r = r.iadd(this.tmp);
|
|
2544
|
+
rlen = r.bitLength();
|
|
2545
|
+
} while (rlen > this.n);
|
|
2546
|
+
var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
|
|
2547
|
+
if (cmp === 0) {
|
|
2548
|
+
r.words[0] = 0;
|
|
2549
|
+
r.length = 1;
|
|
2550
|
+
} else if (cmp > 0) {
|
|
2551
|
+
r.isub(this.p);
|
|
2552
|
+
} else {
|
|
2553
|
+
if (r.strip !== void 0) {
|
|
2554
|
+
r.strip();
|
|
2555
|
+
} else {
|
|
2556
|
+
r._strip();
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
return r;
|
|
2560
|
+
};
|
|
2561
|
+
MPrime.prototype.split = function split(input, out) {
|
|
2562
|
+
input.iushrn(this.n, 0, out);
|
|
2563
|
+
};
|
|
2564
|
+
MPrime.prototype.imulK = function imulK(num) {
|
|
2565
|
+
return num.imul(this.k);
|
|
2566
|
+
};
|
|
2567
|
+
function K256() {
|
|
2568
|
+
MPrime.call(
|
|
2569
|
+
this,
|
|
2570
|
+
"k256",
|
|
2571
|
+
"ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f"
|
|
2572
|
+
);
|
|
2573
|
+
}
|
|
2574
|
+
inherits(K256, MPrime);
|
|
2575
|
+
K256.prototype.split = function split(input, output) {
|
|
2576
|
+
var mask = 4194303;
|
|
2577
|
+
var outLen = Math.min(input.length, 9);
|
|
2578
|
+
for (var i = 0; i < outLen; i++) {
|
|
2579
|
+
output.words[i] = input.words[i];
|
|
2580
|
+
}
|
|
2581
|
+
output.length = outLen;
|
|
2582
|
+
if (input.length <= 9) {
|
|
2583
|
+
input.words[0] = 0;
|
|
2584
|
+
input.length = 1;
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2587
|
+
var prev = input.words[9];
|
|
2588
|
+
output.words[output.length++] = prev & mask;
|
|
2589
|
+
for (i = 10; i < input.length; i++) {
|
|
2590
|
+
var next = input.words[i] | 0;
|
|
2591
|
+
input.words[i - 10] = (next & mask) << 4 | prev >>> 22;
|
|
2592
|
+
prev = next;
|
|
2593
|
+
}
|
|
2594
|
+
prev >>>= 22;
|
|
2595
|
+
input.words[i - 10] = prev;
|
|
2596
|
+
if (prev === 0 && input.length > 10) {
|
|
2597
|
+
input.length -= 10;
|
|
2598
|
+
} else {
|
|
2599
|
+
input.length -= 9;
|
|
2600
|
+
}
|
|
2601
|
+
};
|
|
2602
|
+
K256.prototype.imulK = function imulK(num) {
|
|
2603
|
+
num.words[num.length] = 0;
|
|
2604
|
+
num.words[num.length + 1] = 0;
|
|
2605
|
+
num.length += 2;
|
|
2606
|
+
var lo = 0;
|
|
2607
|
+
for (var i = 0; i < num.length; i++) {
|
|
2608
|
+
var w = num.words[i] | 0;
|
|
2609
|
+
lo += w * 977;
|
|
2610
|
+
num.words[i] = lo & 67108863;
|
|
2611
|
+
lo = w * 64 + (lo / 67108864 | 0);
|
|
2612
|
+
}
|
|
2613
|
+
if (num.words[num.length - 1] === 0) {
|
|
2614
|
+
num.length--;
|
|
2615
|
+
if (num.words[num.length - 1] === 0) {
|
|
2616
|
+
num.length--;
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
return num;
|
|
2620
|
+
};
|
|
2621
|
+
function P224() {
|
|
2622
|
+
MPrime.call(
|
|
2623
|
+
this,
|
|
2624
|
+
"p224",
|
|
2625
|
+
"ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001"
|
|
2626
|
+
);
|
|
2627
|
+
}
|
|
2628
|
+
inherits(P224, MPrime);
|
|
2629
|
+
function P192() {
|
|
2630
|
+
MPrime.call(
|
|
2631
|
+
this,
|
|
2632
|
+
"p192",
|
|
2633
|
+
"ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff"
|
|
2634
|
+
);
|
|
2635
|
+
}
|
|
2636
|
+
inherits(P192, MPrime);
|
|
2637
|
+
function P25519() {
|
|
2638
|
+
MPrime.call(
|
|
2639
|
+
this,
|
|
2640
|
+
"25519",
|
|
2641
|
+
"7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed"
|
|
2642
|
+
);
|
|
2643
|
+
}
|
|
2644
|
+
inherits(P25519, MPrime);
|
|
2645
|
+
P25519.prototype.imulK = function imulK(num) {
|
|
2646
|
+
var carry = 0;
|
|
2647
|
+
for (var i = 0; i < num.length; i++) {
|
|
2648
|
+
var hi = (num.words[i] | 0) * 19 + carry;
|
|
2649
|
+
var lo = hi & 67108863;
|
|
2650
|
+
hi >>>= 26;
|
|
2651
|
+
num.words[i] = lo;
|
|
2652
|
+
carry = hi;
|
|
2653
|
+
}
|
|
2654
|
+
if (carry !== 0) {
|
|
2655
|
+
num.words[num.length++] = carry;
|
|
2656
|
+
}
|
|
2657
|
+
return num;
|
|
2658
|
+
};
|
|
2659
|
+
BN3._prime = function prime(name) {
|
|
2660
|
+
if (primes[name]) return primes[name];
|
|
2661
|
+
var prime2;
|
|
2662
|
+
if (name === "k256") {
|
|
2663
|
+
prime2 = new K256();
|
|
2664
|
+
} else if (name === "p224") {
|
|
2665
|
+
prime2 = new P224();
|
|
2666
|
+
} else if (name === "p192") {
|
|
2667
|
+
prime2 = new P192();
|
|
2668
|
+
} else if (name === "p25519") {
|
|
2669
|
+
prime2 = new P25519();
|
|
2670
|
+
} else {
|
|
2671
|
+
throw new Error("Unknown prime " + name);
|
|
2672
|
+
}
|
|
2673
|
+
primes[name] = prime2;
|
|
2674
|
+
return prime2;
|
|
2675
|
+
};
|
|
2676
|
+
function Red(m) {
|
|
2677
|
+
if (typeof m === "string") {
|
|
2678
|
+
var prime = BN3._prime(m);
|
|
2679
|
+
this.m = prime.p;
|
|
2680
|
+
this.prime = prime;
|
|
2681
|
+
} else {
|
|
2682
|
+
assert(m.gtn(1), "modulus must be greater than 1");
|
|
2683
|
+
this.m = m;
|
|
2684
|
+
this.prime = null;
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
Red.prototype._verify1 = function _verify1(a) {
|
|
2688
|
+
assert(a.negative === 0, "red works only with positives");
|
|
2689
|
+
assert(a.red, "red works only with red numbers");
|
|
2690
|
+
};
|
|
2691
|
+
Red.prototype._verify2 = function _verify2(a, b) {
|
|
2692
|
+
assert((a.negative | b.negative) === 0, "red works only with positives");
|
|
2693
|
+
assert(
|
|
2694
|
+
a.red && a.red === b.red,
|
|
2695
|
+
"red works only with red numbers"
|
|
2696
|
+
);
|
|
2697
|
+
};
|
|
2698
|
+
Red.prototype.imod = function imod(a) {
|
|
2699
|
+
if (this.prime) return this.prime.ireduce(a)._forceRed(this);
|
|
2700
|
+
move(a, a.umod(this.m)._forceRed(this));
|
|
2701
|
+
return a;
|
|
2702
|
+
};
|
|
2703
|
+
Red.prototype.neg = function neg(a) {
|
|
2704
|
+
if (a.isZero()) {
|
|
2705
|
+
return a.clone();
|
|
2706
|
+
}
|
|
2707
|
+
return this.m.sub(a)._forceRed(this);
|
|
2708
|
+
};
|
|
2709
|
+
Red.prototype.add = function add(a, b) {
|
|
2710
|
+
this._verify2(a, b);
|
|
2711
|
+
var res = a.add(b);
|
|
2712
|
+
if (res.cmp(this.m) >= 0) {
|
|
2713
|
+
res.isub(this.m);
|
|
2714
|
+
}
|
|
2715
|
+
return res._forceRed(this);
|
|
2716
|
+
};
|
|
2717
|
+
Red.prototype.iadd = function iadd(a, b) {
|
|
2718
|
+
this._verify2(a, b);
|
|
2719
|
+
var res = a.iadd(b);
|
|
2720
|
+
if (res.cmp(this.m) >= 0) {
|
|
2721
|
+
res.isub(this.m);
|
|
2722
|
+
}
|
|
2723
|
+
return res;
|
|
2724
|
+
};
|
|
2725
|
+
Red.prototype.sub = function sub(a, b) {
|
|
2726
|
+
this._verify2(a, b);
|
|
2727
|
+
var res = a.sub(b);
|
|
2728
|
+
if (res.cmpn(0) < 0) {
|
|
2729
|
+
res.iadd(this.m);
|
|
2730
|
+
}
|
|
2731
|
+
return res._forceRed(this);
|
|
2732
|
+
};
|
|
2733
|
+
Red.prototype.isub = function isub(a, b) {
|
|
2734
|
+
this._verify2(a, b);
|
|
2735
|
+
var res = a.isub(b);
|
|
2736
|
+
if (res.cmpn(0) < 0) {
|
|
2737
|
+
res.iadd(this.m);
|
|
2738
|
+
}
|
|
2739
|
+
return res;
|
|
2740
|
+
};
|
|
2741
|
+
Red.prototype.shl = function shl(a, num) {
|
|
2742
|
+
this._verify1(a);
|
|
2743
|
+
return this.imod(a.ushln(num));
|
|
2744
|
+
};
|
|
2745
|
+
Red.prototype.imul = function imul(a, b) {
|
|
2746
|
+
this._verify2(a, b);
|
|
2747
|
+
return this.imod(a.imul(b));
|
|
2748
|
+
};
|
|
2749
|
+
Red.prototype.mul = function mul(a, b) {
|
|
2750
|
+
this._verify2(a, b);
|
|
2751
|
+
return this.imod(a.mul(b));
|
|
2752
|
+
};
|
|
2753
|
+
Red.prototype.isqr = function isqr(a) {
|
|
2754
|
+
return this.imul(a, a.clone());
|
|
2755
|
+
};
|
|
2756
|
+
Red.prototype.sqr = function sqr(a) {
|
|
2757
|
+
return this.mul(a, a);
|
|
2758
|
+
};
|
|
2759
|
+
Red.prototype.sqrt = function sqrt(a) {
|
|
2760
|
+
if (a.isZero()) return a.clone();
|
|
2761
|
+
var mod3 = this.m.andln(3);
|
|
2762
|
+
assert(mod3 % 2 === 1);
|
|
2763
|
+
if (mod3 === 3) {
|
|
2764
|
+
var pow = this.m.add(new BN3(1)).iushrn(2);
|
|
2765
|
+
return this.pow(a, pow);
|
|
2766
|
+
}
|
|
2767
|
+
var q = this.m.subn(1);
|
|
2768
|
+
var s = 0;
|
|
2769
|
+
while (!q.isZero() && q.andln(1) === 0) {
|
|
2770
|
+
s++;
|
|
2771
|
+
q.iushrn(1);
|
|
2772
|
+
}
|
|
2773
|
+
assert(!q.isZero());
|
|
2774
|
+
var one = new BN3(1).toRed(this);
|
|
2775
|
+
var nOne = one.redNeg();
|
|
2776
|
+
var lpow = this.m.subn(1).iushrn(1);
|
|
2777
|
+
var z = this.m.bitLength();
|
|
2778
|
+
z = new BN3(2 * z * z).toRed(this);
|
|
2779
|
+
while (this.pow(z, lpow).cmp(nOne) !== 0) {
|
|
2780
|
+
z.redIAdd(nOne);
|
|
2781
|
+
}
|
|
2782
|
+
var c = this.pow(z, q);
|
|
2783
|
+
var r = this.pow(a, q.addn(1).iushrn(1));
|
|
2784
|
+
var t = this.pow(a, q);
|
|
2785
|
+
var m = s;
|
|
2786
|
+
while (t.cmp(one) !== 0) {
|
|
2787
|
+
var tmp = t;
|
|
2788
|
+
for (var i = 0; tmp.cmp(one) !== 0; i++) {
|
|
2789
|
+
tmp = tmp.redSqr();
|
|
2790
|
+
}
|
|
2791
|
+
assert(i < m);
|
|
2792
|
+
var b = this.pow(c, new BN3(1).iushln(m - i - 1));
|
|
2793
|
+
r = r.redMul(b);
|
|
2794
|
+
c = b.redSqr();
|
|
2795
|
+
t = t.redMul(c);
|
|
2796
|
+
m = i;
|
|
2797
|
+
}
|
|
2798
|
+
return r;
|
|
2799
|
+
};
|
|
2800
|
+
Red.prototype.invm = function invm(a) {
|
|
2801
|
+
var inv = a._invmp(this.m);
|
|
2802
|
+
if (inv.negative !== 0) {
|
|
2803
|
+
inv.negative = 0;
|
|
2804
|
+
return this.imod(inv).redNeg();
|
|
2805
|
+
} else {
|
|
2806
|
+
return this.imod(inv);
|
|
2807
|
+
}
|
|
2808
|
+
};
|
|
2809
|
+
Red.prototype.pow = function pow(a, num) {
|
|
2810
|
+
if (num.isZero()) return new BN3(1).toRed(this);
|
|
2811
|
+
if (num.cmpn(1) === 0) return a.clone();
|
|
2812
|
+
var windowSize = 4;
|
|
2813
|
+
var wnd = new Array(1 << windowSize);
|
|
2814
|
+
wnd[0] = new BN3(1).toRed(this);
|
|
2815
|
+
wnd[1] = a;
|
|
2816
|
+
for (var i = 2; i < wnd.length; i++) {
|
|
2817
|
+
wnd[i] = this.mul(wnd[i - 1], a);
|
|
2818
|
+
}
|
|
2819
|
+
var res = wnd[0];
|
|
2820
|
+
var current = 0;
|
|
2821
|
+
var currentLen = 0;
|
|
2822
|
+
var start = num.bitLength() % 26;
|
|
2823
|
+
if (start === 0) {
|
|
2824
|
+
start = 26;
|
|
2825
|
+
}
|
|
2826
|
+
for (i = num.length - 1; i >= 0; i--) {
|
|
2827
|
+
var word = num.words[i];
|
|
2828
|
+
for (var j = start - 1; j >= 0; j--) {
|
|
2829
|
+
var bit = word >> j & 1;
|
|
2830
|
+
if (res !== wnd[0]) {
|
|
2831
|
+
res = this.sqr(res);
|
|
2832
|
+
}
|
|
2833
|
+
if (bit === 0 && current === 0) {
|
|
2834
|
+
currentLen = 0;
|
|
2835
|
+
continue;
|
|
2836
|
+
}
|
|
2837
|
+
current <<= 1;
|
|
2838
|
+
current |= bit;
|
|
2839
|
+
currentLen++;
|
|
2840
|
+
if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
|
|
2841
|
+
res = this.mul(res, wnd[current]);
|
|
2842
|
+
currentLen = 0;
|
|
2843
|
+
current = 0;
|
|
2844
|
+
}
|
|
2845
|
+
start = 26;
|
|
2846
|
+
}
|
|
2847
|
+
return res;
|
|
2848
|
+
};
|
|
2849
|
+
Red.prototype.convertTo = function convertTo(num) {
|
|
2850
|
+
var r = num.umod(this.m);
|
|
2851
|
+
return r === num ? r.clone() : r;
|
|
2852
|
+
};
|
|
2853
|
+
Red.prototype.convertFrom = function convertFrom(num) {
|
|
2854
|
+
var res = num.clone();
|
|
2855
|
+
res.red = null;
|
|
2856
|
+
return res;
|
|
2857
|
+
};
|
|
2858
|
+
BN3.mont = function mont(num) {
|
|
2859
|
+
return new Mont(num);
|
|
2860
|
+
};
|
|
2861
|
+
function Mont(m) {
|
|
2862
|
+
Red.call(this, m);
|
|
2863
|
+
this.shift = this.m.bitLength();
|
|
2864
|
+
if (this.shift % 26 !== 0) {
|
|
2865
|
+
this.shift += 26 - this.shift % 26;
|
|
2866
|
+
}
|
|
2867
|
+
this.r = new BN3(1).iushln(this.shift);
|
|
2868
|
+
this.r2 = this.imod(this.r.sqr());
|
|
2869
|
+
this.rinv = this.r._invmp(this.m);
|
|
2870
|
+
this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
|
|
2871
|
+
this.minv = this.minv.umod(this.r);
|
|
2872
|
+
this.minv = this.r.sub(this.minv);
|
|
2873
|
+
}
|
|
2874
|
+
inherits(Mont, Red);
|
|
2875
|
+
Mont.prototype.convertTo = function convertTo(num) {
|
|
2876
|
+
return this.imod(num.ushln(this.shift));
|
|
2877
|
+
};
|
|
2878
|
+
Mont.prototype.convertFrom = function convertFrom(num) {
|
|
2879
|
+
var r = this.imod(num.mul(this.rinv));
|
|
2880
|
+
r.red = null;
|
|
2881
|
+
return r;
|
|
2882
|
+
};
|
|
2883
|
+
Mont.prototype.imul = function imul(a, b) {
|
|
2884
|
+
if (a.isZero() || b.isZero()) {
|
|
2885
|
+
a.words[0] = 0;
|
|
2886
|
+
a.length = 1;
|
|
2887
|
+
return a;
|
|
2888
|
+
}
|
|
2889
|
+
var t = a.imul(b);
|
|
2890
|
+
var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
|
|
2891
|
+
var u = t.isub(c).iushrn(this.shift);
|
|
2892
|
+
var res = u;
|
|
2893
|
+
if (u.cmp(this.m) >= 0) {
|
|
2894
|
+
res = u.isub(this.m);
|
|
2895
|
+
} else if (u.cmpn(0) < 0) {
|
|
2896
|
+
res = u.iadd(this.m);
|
|
2897
|
+
}
|
|
2898
|
+
return res._forceRed(this);
|
|
2899
|
+
};
|
|
2900
|
+
Mont.prototype.mul = function mul(a, b) {
|
|
2901
|
+
if (a.isZero() || b.isZero()) return new BN3(0)._forceRed(this);
|
|
2902
|
+
var t = a.mul(b);
|
|
2903
|
+
var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
|
|
2904
|
+
var u = t.isub(c).iushrn(this.shift);
|
|
2905
|
+
var res = u;
|
|
2906
|
+
if (u.cmp(this.m) >= 0) {
|
|
2907
|
+
res = u.isub(this.m);
|
|
2908
|
+
} else if (u.cmpn(0) < 0) {
|
|
2909
|
+
res = u.iadd(this.m);
|
|
2910
|
+
}
|
|
2911
|
+
return res._forceRed(this);
|
|
2912
|
+
};
|
|
2913
|
+
Mont.prototype.invm = function invm(a) {
|
|
2914
|
+
var res = this.imod(a._invmp(this.m).mul(this.r2));
|
|
2915
|
+
return res._forceRed(this);
|
|
2916
|
+
};
|
|
2917
|
+
})(typeof module2 === "undefined" || module2, exports2);
|
|
2918
|
+
}
|
|
2919
|
+
});
|
|
2920
|
+
|
|
2921
|
+
// src/index.ts
|
|
2922
|
+
var index_exports = {};
|
|
2923
|
+
__export(index_exports, {
|
|
2924
|
+
GPASS_PROGRAM_ID: () => GPASS_PROGRAM_ID,
|
|
2925
|
+
GRAPE_VERIFICATION_PROGRAM_ID: () => GRAPE_VERIFICATION_PROGRAM_ID,
|
|
2926
|
+
GateCriteriaFactory: () => GateCriteriaFactory,
|
|
2927
|
+
GateTypeFactory: () => GateTypeFactory,
|
|
2928
|
+
GpassClient: () => GpassClient,
|
|
2929
|
+
IDL: () => IDL,
|
|
2930
|
+
VINE_REPUTATION_PROGRAM_ID: () => VINE_REPUTATION_PROGRAM_ID,
|
|
2931
|
+
VerificationPlatform: () => VerificationPlatform,
|
|
2932
|
+
findCheckRecordPda: () => findCheckRecordPda,
|
|
2933
|
+
findGatePda: () => findGatePda,
|
|
2934
|
+
findGrapeIdentityPda: () => findGrapeIdentityPda,
|
|
2935
|
+
findGrapeLinkPda: () => findGrapeLinkPda,
|
|
2936
|
+
findGrapeSpacePda: () => findGrapeSpacePda,
|
|
2937
|
+
findVineConfigPda: () => findVineConfigPda,
|
|
2938
|
+
findVineReputationPda: () => findVineReputationPda
|
|
2939
|
+
});
|
|
2940
|
+
module.exports = __toCommonJS(index_exports);
|
|
2941
|
+
|
|
2942
|
+
// src/client.ts
|
|
2943
|
+
var import_anchor = require("@coral-xyz/anchor");
|
|
2944
|
+
var import_web33 = require("@solana/web3.js");
|
|
2945
|
+
|
|
2946
|
+
// src/idl.ts
|
|
2947
|
+
var IDL = {
|
|
2948
|
+
version: "0.1.0",
|
|
2949
|
+
name: "grape_gating_protocol",
|
|
2950
|
+
instructions: [
|
|
2951
|
+
{
|
|
2952
|
+
name: "initializeGate",
|
|
2953
|
+
accounts: [
|
|
2954
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
2955
|
+
{ name: "authority", isMut: false, isSigner: true },
|
|
2956
|
+
{ name: "payer", isMut: true, isSigner: true },
|
|
2957
|
+
{ name: "systemProgram", isMut: false, isSigner: false }
|
|
2958
|
+
],
|
|
2959
|
+
args: [
|
|
2960
|
+
{ name: "gateId", type: "publicKey" },
|
|
2961
|
+
{ name: "criteria", type: { defined: "GateCriteria" } },
|
|
2962
|
+
{ name: "gateType", type: { defined: "GateType" } }
|
|
2963
|
+
]
|
|
2964
|
+
},
|
|
2965
|
+
{
|
|
2966
|
+
name: "setGateAuthority",
|
|
2967
|
+
accounts: [
|
|
2968
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
2969
|
+
{ name: "authority", isMut: false, isSigner: true }
|
|
2970
|
+
],
|
|
2971
|
+
args: [
|
|
2972
|
+
{ name: "gateId", type: "publicKey" },
|
|
2973
|
+
{ name: "newAuthority", type: "publicKey" }
|
|
2974
|
+
]
|
|
2975
|
+
},
|
|
2976
|
+
{
|
|
2977
|
+
name: "updateGateCriteria",
|
|
2978
|
+
accounts: [
|
|
2979
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
2980
|
+
{ name: "authority", isMut: false, isSigner: true }
|
|
2981
|
+
],
|
|
2982
|
+
args: [
|
|
2983
|
+
{ name: "gateId", type: "publicKey" },
|
|
2984
|
+
{ name: "newCriteria", type: { defined: "GateCriteria" } }
|
|
2985
|
+
]
|
|
2986
|
+
},
|
|
2987
|
+
{
|
|
2988
|
+
name: "setGateActive",
|
|
2989
|
+
accounts: [
|
|
2990
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
2991
|
+
{ name: "authority", isMut: false, isSigner: true }
|
|
2992
|
+
],
|
|
2993
|
+
args: [
|
|
2994
|
+
{ name: "gateId", type: "publicKey" },
|
|
2995
|
+
{ name: "isActive", type: "bool" }
|
|
2996
|
+
]
|
|
2997
|
+
},
|
|
2998
|
+
{
|
|
2999
|
+
name: "checkGate",
|
|
3000
|
+
accounts: [
|
|
3001
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
3002
|
+
{ name: "user", isMut: false, isSigner: false },
|
|
3003
|
+
{ name: "reputationAccount", isMut: true, isSigner: false, isOptional: true },
|
|
3004
|
+
{ name: "identityAccount", isMut: false, isSigner: false, isOptional: true },
|
|
3005
|
+
{ name: "linkAccount", isMut: false, isSigner: false, isOptional: true },
|
|
3006
|
+
{ name: "tokenAccount", isMut: false, isSigner: false, isOptional: true },
|
|
3007
|
+
{ name: "checkRecord", isMut: true, isSigner: false, isOptional: true },
|
|
3008
|
+
{ name: "payer", isMut: true, isSigner: true },
|
|
3009
|
+
{ name: "systemProgram", isMut: false, isSigner: false }
|
|
3010
|
+
],
|
|
3011
|
+
args: [{ name: "gateId", type: "publicKey" }]
|
|
3012
|
+
},
|
|
3013
|
+
{
|
|
3014
|
+
name: "createCheckRecord",
|
|
3015
|
+
accounts: [
|
|
3016
|
+
{ name: "gate", isMut: false, isSigner: false },
|
|
3017
|
+
{ name: "user", isMut: false, isSigner: false },
|
|
3018
|
+
{ name: "checkRecord", isMut: true, isSigner: false },
|
|
3019
|
+
{ name: "payer", isMut: true, isSigner: true },
|
|
3020
|
+
{ name: "systemProgram", isMut: false, isSigner: false }
|
|
3021
|
+
],
|
|
3022
|
+
args: [{ name: "gateId", type: "publicKey" }]
|
|
3023
|
+
},
|
|
3024
|
+
{
|
|
3025
|
+
name: "closeGate",
|
|
3026
|
+
accounts: [
|
|
3027
|
+
{ name: "gate", isMut: true, isSigner: false },
|
|
3028
|
+
{ name: "authority", isMut: false, isSigner: true },
|
|
3029
|
+
{ name: "recipient", isMut: true, isSigner: false }
|
|
3030
|
+
],
|
|
3031
|
+
args: [{ name: "gateId", type: "publicKey" }]
|
|
3032
|
+
},
|
|
3033
|
+
{
|
|
3034
|
+
name: "closeCheckRecord",
|
|
3035
|
+
accounts: [
|
|
3036
|
+
{ name: "gate", isMut: false, isSigner: false },
|
|
3037
|
+
{ name: "user", isMut: false, isSigner: false },
|
|
3038
|
+
{ name: "checkRecord", isMut: true, isSigner: false },
|
|
3039
|
+
{ name: "authority", isMut: false, isSigner: true },
|
|
3040
|
+
{ name: "recipient", isMut: true, isSigner: false }
|
|
3041
|
+
],
|
|
3042
|
+
args: [{ name: "gateId", type: "publicKey" }]
|
|
3043
|
+
},
|
|
3044
|
+
{
|
|
3045
|
+
name: "adminCloseAny",
|
|
3046
|
+
accounts: [
|
|
3047
|
+
{ name: "authority", isMut: false, isSigner: true },
|
|
3048
|
+
{ name: "target", isMut: true, isSigner: false },
|
|
3049
|
+
{ name: "recipient", isMut: true, isSigner: false }
|
|
3050
|
+
],
|
|
3051
|
+
args: []
|
|
3052
|
+
}
|
|
3053
|
+
],
|
|
3054
|
+
accounts: [
|
|
3055
|
+
{
|
|
3056
|
+
name: "Gate",
|
|
3057
|
+
type: {
|
|
3058
|
+
kind: "struct",
|
|
3059
|
+
fields: [
|
|
3060
|
+
{ name: "version", type: "u8" },
|
|
3061
|
+
{ name: "gateId", type: "publicKey" },
|
|
3062
|
+
{ name: "authority", type: "publicKey" },
|
|
3063
|
+
{ name: "criteria", type: { defined: "GateCriteria" } },
|
|
3064
|
+
{ name: "gateType", type: { defined: "GateType" } },
|
|
3065
|
+
{ name: "isActive", type: "bool" },
|
|
3066
|
+
{ name: "createdAt", type: "i64" },
|
|
3067
|
+
{ name: "totalChecks", type: "u64" },
|
|
3068
|
+
{ name: "successfulChecks", type: "u64" },
|
|
3069
|
+
{ name: "bump", type: "u8" }
|
|
3070
|
+
]
|
|
3071
|
+
}
|
|
3072
|
+
},
|
|
3073
|
+
{
|
|
3074
|
+
name: "GateCheckRecord",
|
|
3075
|
+
type: {
|
|
3076
|
+
kind: "struct",
|
|
3077
|
+
fields: [
|
|
3078
|
+
{ name: "version", type: "u8" },
|
|
3079
|
+
{ name: "gate", type: "publicKey" },
|
|
3080
|
+
{ name: "user", type: "publicKey" },
|
|
3081
|
+
{ name: "passed", type: "bool" },
|
|
3082
|
+
{ name: "checkedAt", type: "i64" },
|
|
3083
|
+
{ name: "bump", type: "u8" }
|
|
3084
|
+
]
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
],
|
|
3088
|
+
types: [
|
|
3089
|
+
{
|
|
3090
|
+
name: "GateType",
|
|
3091
|
+
type: {
|
|
3092
|
+
kind: "enum",
|
|
3093
|
+
variants: [
|
|
3094
|
+
{ name: "SingleUse" },
|
|
3095
|
+
{ name: "Reusable" },
|
|
3096
|
+
{ name: "TimeLimited", fields: [{ name: "durationSeconds", type: "i64" }] },
|
|
3097
|
+
{ name: "Subscription", fields: [{ name: "intervalSeconds", type: "i64" }] }
|
|
3098
|
+
]
|
|
3099
|
+
}
|
|
3100
|
+
},
|
|
3101
|
+
{
|
|
3102
|
+
name: "GateCriteria",
|
|
3103
|
+
type: {
|
|
3104
|
+
kind: "enum",
|
|
3105
|
+
variants: [
|
|
3106
|
+
{
|
|
3107
|
+
name: "MinReputation",
|
|
3108
|
+
fields: [
|
|
3109
|
+
{ name: "vineConfig", type: "publicKey" },
|
|
3110
|
+
{ name: "minPoints", type: "u64" },
|
|
3111
|
+
{ name: "season", type: "u16" }
|
|
3112
|
+
]
|
|
3113
|
+
},
|
|
3114
|
+
{
|
|
3115
|
+
name: "VerifiedIdentity",
|
|
3116
|
+
fields: [
|
|
3117
|
+
{ name: "grapeSpace", type: "publicKey" },
|
|
3118
|
+
{ name: "platforms", type: "bytes" }
|
|
3119
|
+
]
|
|
3120
|
+
},
|
|
3121
|
+
{
|
|
3122
|
+
name: "VerifiedWithWallet",
|
|
3123
|
+
fields: [
|
|
3124
|
+
{ name: "grapeSpace", type: "publicKey" },
|
|
3125
|
+
{ name: "platforms", type: "bytes" }
|
|
3126
|
+
]
|
|
3127
|
+
},
|
|
3128
|
+
{
|
|
3129
|
+
name: "Combined",
|
|
3130
|
+
fields: [
|
|
3131
|
+
{ name: "vineConfig", type: "publicKey" },
|
|
3132
|
+
{ name: "minPoints", type: "u64" },
|
|
3133
|
+
{ name: "season", type: "u16" },
|
|
3134
|
+
{ name: "grapeSpace", type: "publicKey" },
|
|
3135
|
+
{ name: "platforms", type: "bytes" },
|
|
3136
|
+
{ name: "requireWalletLink", type: "bool" }
|
|
3137
|
+
]
|
|
3138
|
+
},
|
|
3139
|
+
{
|
|
3140
|
+
name: "TimeLockedReputation",
|
|
3141
|
+
fields: [
|
|
3142
|
+
{ name: "vineConfig", type: "publicKey" },
|
|
3143
|
+
{ name: "minPoints", type: "u64" },
|
|
3144
|
+
{ name: "season", type: "u16" },
|
|
3145
|
+
{ name: "minHoldDurationSeconds", type: "u64" }
|
|
3146
|
+
]
|
|
3147
|
+
},
|
|
3148
|
+
{
|
|
3149
|
+
name: "MultiDao",
|
|
3150
|
+
fields: [
|
|
3151
|
+
{ name: "requiredGates", type: { vec: "publicKey" } },
|
|
3152
|
+
{ name: "requireAll", type: "bool" }
|
|
3153
|
+
]
|
|
3154
|
+
},
|
|
3155
|
+
{
|
|
3156
|
+
name: "TokenHolding",
|
|
3157
|
+
fields: [
|
|
3158
|
+
{ name: "mint", type: "publicKey" },
|
|
3159
|
+
{ name: "minAmount", type: "u64" },
|
|
3160
|
+
{ name: "checkAta", type: "bool" }
|
|
3161
|
+
]
|
|
3162
|
+
},
|
|
3163
|
+
{
|
|
3164
|
+
name: "NftCollection",
|
|
3165
|
+
fields: [
|
|
3166
|
+
{ name: "collectionMint", type: "publicKey" },
|
|
3167
|
+
{ name: "minCount", type: "u16" }
|
|
3168
|
+
]
|
|
3169
|
+
},
|
|
3170
|
+
{
|
|
3171
|
+
name: "CustomProgram",
|
|
3172
|
+
fields: [
|
|
3173
|
+
{ name: "programId", type: "publicKey" },
|
|
3174
|
+
{ name: "instructionData", type: "bytes" }
|
|
3175
|
+
]
|
|
3176
|
+
}
|
|
3177
|
+
]
|
|
3178
|
+
}
|
|
3179
|
+
}
|
|
3180
|
+
],
|
|
3181
|
+
errors: [
|
|
3182
|
+
{ code: 6e3, name: "Unauthorized", msg: "Unauthorized" },
|
|
3183
|
+
{ code: 6001, name: "GateInactive", msg: "Gate is inactive" },
|
|
3184
|
+
{ code: 6002, name: "GateCheckFailed", msg: "Gate check failed" },
|
|
3185
|
+
{ code: 6003, name: "ReputationAccountRequired", msg: "Reputation account required" },
|
|
3186
|
+
{ code: 6004, name: "IdentityAccountRequired", msg: "Identity account required" },
|
|
3187
|
+
{ code: 6005, name: "LinkAccountRequired", msg: "Link account required" },
|
|
3188
|
+
{ code: 6006, name: "TokenAccountRequired", msg: "Token account required" },
|
|
3189
|
+
{ code: 6007, name: "InvalidReputationAccount", msg: "Invalid reputation account" },
|
|
3190
|
+
{ code: 6008, name: "InvalidIdentityAccount", msg: "Invalid identity account" },
|
|
3191
|
+
{ code: 6009, name: "InvalidLinkAccount", msg: "Invalid link account" },
|
|
3192
|
+
{ code: 6010, name: "InvalidPda", msg: "Invalid PDA derivation" },
|
|
3193
|
+
{ code: 6011, name: "WrongUser", msg: "Wrong user" },
|
|
3194
|
+
{ code: 6012, name: "SeasonMismatch", msg: "Season mismatch" },
|
|
3195
|
+
{ code: 6013, name: "WrongSpace", msg: "Wrong space" },
|
|
3196
|
+
{ code: 6014, name: "WrongIdentity", msg: "Wrong identity" },
|
|
3197
|
+
{ code: 6015, name: "IdentityNotVerified", msg: "Identity not verified" },
|
|
3198
|
+
{ code: 6016, name: "IdentityExpired", msg: "Identity expired" },
|
|
3199
|
+
{ code: 6017, name: "InsufficientGateChecks", msg: "Insufficient gate checks" },
|
|
3200
|
+
{ code: 6018, name: "InvalidCheckRecord", msg: "Invalid check record" },
|
|
3201
|
+
{ code: 6019, name: "CheckRecordExpired", msg: "Check record expired" },
|
|
3202
|
+
{ code: 6020, name: "WrongMint", msg: "Wrong mint" },
|
|
3203
|
+
{ code: 6021, name: "WrongTokenOwner", msg: "Wrong token owner" },
|
|
3204
|
+
{ code: 6022, name: "InsufficientNfts", msg: "Insufficient NFTs" },
|
|
3205
|
+
{ code: 6023, name: "CustomProgramAccountRequired", msg: "Custom program account required" },
|
|
3206
|
+
{ code: 6024, name: "InvalidCustomProgram", msg: "Invalid custom program" },
|
|
3207
|
+
{ code: 6025, name: "CustomValidationFailed", msg: "Custom validation failed" },
|
|
3208
|
+
{ code: 6026, name: "TargetNotProgramOwned", msg: "Target not program owned" },
|
|
3209
|
+
{ code: 6027, name: "Overflow", msg: "Overflow" }
|
|
3210
|
+
]
|
|
3211
|
+
};
|
|
3212
|
+
|
|
3213
|
+
// src/types/index.ts
|
|
3214
|
+
var import_web3 = require("@solana/web3.js");
|
|
3215
|
+
var import_bn = __toESM(require_bn());
|
|
3216
|
+
var GPASS_PROGRAM_ID = new import_web3.PublicKey(
|
|
3217
|
+
"GPASSzQQF1H8cdj5pUwFkeYEE4VdMQtCrYtUaMXvPz48"
|
|
3218
|
+
);
|
|
3219
|
+
var VINE_REPUTATION_PROGRAM_ID = new import_web3.PublicKey(
|
|
3220
|
+
"V1NE6WCWJPRiVFq5DtaN8p87M9DmmUd2zQuVbvLgQwX"
|
|
3221
|
+
);
|
|
3222
|
+
var GRAPE_VERIFICATION_PROGRAM_ID = new import_web3.PublicKey(
|
|
3223
|
+
"VrFyyRxPoyWxpABpBXU4YUCCF9p8giDSJUv2oXfDr5q"
|
|
3224
|
+
);
|
|
3225
|
+
var VerificationPlatform = /* @__PURE__ */ ((VerificationPlatform2) => {
|
|
3226
|
+
VerificationPlatform2[VerificationPlatform2["Discord"] = 0] = "Discord";
|
|
3227
|
+
VerificationPlatform2[VerificationPlatform2["Telegram"] = 1] = "Telegram";
|
|
3228
|
+
VerificationPlatform2[VerificationPlatform2["Twitter"] = 2] = "Twitter";
|
|
3229
|
+
VerificationPlatform2[VerificationPlatform2["Email"] = 3] = "Email";
|
|
3230
|
+
return VerificationPlatform2;
|
|
3231
|
+
})(VerificationPlatform || {});
|
|
3232
|
+
var GateTypeFactory = {
|
|
3233
|
+
singleUse: () => ({ singleUse: {} }),
|
|
3234
|
+
reusable: () => ({ reusable: {} }),
|
|
3235
|
+
timeLimited: (durationSeconds) => ({
|
|
3236
|
+
timeLimited: { durationSeconds: new import_bn.default(durationSeconds) }
|
|
3237
|
+
}),
|
|
3238
|
+
subscription: (intervalSeconds) => ({
|
|
3239
|
+
subscription: { intervalSeconds: new import_bn.default(intervalSeconds) }
|
|
3240
|
+
})
|
|
3241
|
+
};
|
|
3242
|
+
var GateCriteriaFactory = {
|
|
3243
|
+
minReputation: (params) => ({
|
|
3244
|
+
minReputation: {
|
|
3245
|
+
vineConfig: params.vineConfig,
|
|
3246
|
+
minPoints: new import_bn.default(params.minPoints),
|
|
3247
|
+
season: params.season
|
|
3248
|
+
}
|
|
3249
|
+
}),
|
|
3250
|
+
verifiedIdentity: (params) => ({
|
|
3251
|
+
verifiedIdentity: {
|
|
3252
|
+
grapeSpace: params.grapeSpace,
|
|
3253
|
+
platforms: Buffer.from(params.platforms)
|
|
3254
|
+
}
|
|
3255
|
+
}),
|
|
3256
|
+
verifiedWithWallet: (params) => ({
|
|
3257
|
+
verifiedWithWallet: {
|
|
3258
|
+
grapeSpace: params.grapeSpace,
|
|
3259
|
+
platforms: Buffer.from(params.platforms)
|
|
3260
|
+
}
|
|
3261
|
+
}),
|
|
3262
|
+
combined: (params) => ({
|
|
3263
|
+
combined: {
|
|
3264
|
+
vineConfig: params.vineConfig,
|
|
3265
|
+
minPoints: new import_bn.default(params.minPoints),
|
|
3266
|
+
season: params.season,
|
|
3267
|
+
grapeSpace: params.grapeSpace,
|
|
3268
|
+
platforms: Buffer.from(params.platforms),
|
|
3269
|
+
requireWalletLink: params.requireWalletLink ?? false
|
|
3270
|
+
}
|
|
3271
|
+
}),
|
|
3272
|
+
timeLockedReputation: (params) => ({
|
|
3273
|
+
timeLockedReputation: {
|
|
3274
|
+
vineConfig: params.vineConfig,
|
|
3275
|
+
minPoints: new import_bn.default(params.minPoints),
|
|
3276
|
+
season: params.season,
|
|
3277
|
+
minHoldDurationSeconds: new import_bn.default(params.minHoldDurationSeconds)
|
|
3278
|
+
}
|
|
3279
|
+
}),
|
|
3280
|
+
multiDao: (params) => ({
|
|
3281
|
+
multiDao: {
|
|
3282
|
+
requiredGates: params.requiredGates,
|
|
3283
|
+
requireAll: params.requireAll ?? true
|
|
3284
|
+
}
|
|
3285
|
+
}),
|
|
3286
|
+
tokenHolding: (params) => ({
|
|
3287
|
+
tokenHolding: {
|
|
3288
|
+
mint: params.mint,
|
|
3289
|
+
minAmount: new import_bn.default(params.minAmount),
|
|
3290
|
+
checkAta: params.checkAta ?? true
|
|
3291
|
+
}
|
|
3292
|
+
}),
|
|
3293
|
+
nftCollection: (params) => ({
|
|
3294
|
+
nftCollection: {
|
|
3295
|
+
collectionMint: params.collectionMint,
|
|
3296
|
+
minCount: params.minCount ?? 1
|
|
3297
|
+
}
|
|
3298
|
+
}),
|
|
3299
|
+
customProgram: (params) => ({
|
|
3300
|
+
customProgram: {
|
|
3301
|
+
programId: params.programId,
|
|
3302
|
+
instructionData: params.instructionData ?? Buffer.alloc(0)
|
|
3303
|
+
}
|
|
3304
|
+
})
|
|
3305
|
+
};
|
|
3306
|
+
|
|
3307
|
+
// src/pda.ts
|
|
3308
|
+
var import_web32 = require("@solana/web3.js");
|
|
3309
|
+
async function findGatePda(gateId, programId = GPASS_PROGRAM_ID) {
|
|
3310
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3311
|
+
[Buffer.from("gate"), gateId.toBuffer()],
|
|
3312
|
+
programId
|
|
3313
|
+
);
|
|
3314
|
+
}
|
|
3315
|
+
async function findCheckRecordPda(gate, user, programId = GPASS_PROGRAM_ID) {
|
|
3316
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3317
|
+
[Buffer.from("check"), gate.toBuffer(), user.toBuffer()],
|
|
3318
|
+
programId
|
|
3319
|
+
);
|
|
3320
|
+
}
|
|
3321
|
+
async function findVineConfigPda(daoId, programId = VINE_REPUTATION_PROGRAM_ID) {
|
|
3322
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3323
|
+
[Buffer.from("config"), daoId.toBuffer()],
|
|
3324
|
+
programId
|
|
3325
|
+
);
|
|
3326
|
+
}
|
|
3327
|
+
async function findVineReputationPda(config, user, season, programId = VINE_REPUTATION_PROGRAM_ID) {
|
|
3328
|
+
const seasonBuf = Buffer.alloc(2);
|
|
3329
|
+
seasonBuf.writeUInt16LE(season, 0);
|
|
3330
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3331
|
+
[Buffer.from("reputation"), config.toBuffer(), user.toBuffer(), seasonBuf],
|
|
3332
|
+
programId
|
|
3333
|
+
);
|
|
3334
|
+
}
|
|
3335
|
+
async function findGrapeSpacePda(daoId, programId = GRAPE_VERIFICATION_PROGRAM_ID) {
|
|
3336
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3337
|
+
[Buffer.from("space"), daoId.toBuffer()],
|
|
3338
|
+
programId
|
|
3339
|
+
);
|
|
3340
|
+
}
|
|
3341
|
+
async function findGrapeIdentityPda(space, platformSeed, idHash, programId = GRAPE_VERIFICATION_PROGRAM_ID) {
|
|
3342
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3343
|
+
[
|
|
3344
|
+
Buffer.from("identity"),
|
|
3345
|
+
space.toBuffer(),
|
|
3346
|
+
Buffer.from([platformSeed]),
|
|
3347
|
+
Buffer.from(idHash)
|
|
3348
|
+
],
|
|
3349
|
+
programId
|
|
3350
|
+
);
|
|
3351
|
+
}
|
|
3352
|
+
async function findGrapeLinkPda(identity, walletHash, programId = GRAPE_VERIFICATION_PROGRAM_ID) {
|
|
3353
|
+
return import_web32.PublicKey.findProgramAddress(
|
|
3354
|
+
[Buffer.from("link"), identity.toBuffer(), Buffer.from(walletHash)],
|
|
3355
|
+
programId
|
|
3356
|
+
);
|
|
3357
|
+
}
|
|
3358
|
+
|
|
3359
|
+
// src/client.ts
|
|
3360
|
+
var GpassClient = class {
|
|
3361
|
+
constructor(provider, programId = GPASS_PROGRAM_ID) {
|
|
3362
|
+
this.provider = provider;
|
|
3363
|
+
this.program = new import_anchor.Program(IDL, programId, provider);
|
|
3364
|
+
}
|
|
3365
|
+
// ─────────────────────────────────────────────────────
|
|
3366
|
+
// INITIALIZE GATE
|
|
3367
|
+
// ─────────────────────────────────────────────────────
|
|
3368
|
+
/**
|
|
3369
|
+
* Create a new gate with the given criteria.
|
|
3370
|
+
*
|
|
3371
|
+
* @example
|
|
3372
|
+
* const { tx, gate } = await client.initializeGate({
|
|
3373
|
+
* gateId: Keypair.generate().publicKey,
|
|
3374
|
+
* criteria: GateCriteriaFactory.combined({
|
|
3375
|
+
* vineConfig,
|
|
3376
|
+
* minPoints: 500,
|
|
3377
|
+
* season: 1,
|
|
3378
|
+
* grapeSpace,
|
|
3379
|
+
* platforms: [VerificationPlatform.Discord],
|
|
3380
|
+
* requireWalletLink: true,
|
|
3381
|
+
* }),
|
|
3382
|
+
* gateType: GateTypeFactory.reusable(),
|
|
3383
|
+
* });
|
|
3384
|
+
*/
|
|
3385
|
+
async initializeGate(params) {
|
|
3386
|
+
const authority = params.authority ?? this.provider.wallet.publicKey;
|
|
3387
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3388
|
+
const tx = await this.program.methods.initializeGate(params.gateId, params.criteria, params.gateType).accounts({
|
|
3389
|
+
gate: gatePda,
|
|
3390
|
+
authority,
|
|
3391
|
+
payer: this.provider.wallet.publicKey,
|
|
3392
|
+
systemProgram: import_web33.SystemProgram.programId
|
|
3393
|
+
}).rpc();
|
|
3394
|
+
return { tx, gate: gatePda };
|
|
3395
|
+
}
|
|
3396
|
+
// ─────────────────────────────────────────────────────
|
|
3397
|
+
// CHECK GATE
|
|
3398
|
+
// ─────────────────────────────────────────────────────
|
|
3399
|
+
/**
|
|
3400
|
+
* Check whether a user passes a gate's criteria.
|
|
3401
|
+
* Throws GateCheckFailed (6002) if the user does not pass.
|
|
3402
|
+
*
|
|
3403
|
+
* @example
|
|
3404
|
+
* await client.checkGate({
|
|
3405
|
+
* gateId,
|
|
3406
|
+
* user: walletPublicKey,
|
|
3407
|
+
* reputationAccount: vineRepPda,
|
|
3408
|
+
* identityAccount: grapeIdentityPda,
|
|
3409
|
+
* storeRecord: true,
|
|
3410
|
+
* });
|
|
3411
|
+
*/
|
|
3412
|
+
async checkGate(params) {
|
|
3413
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3414
|
+
const [checkRecordPda] = params.storeRecord ? await findCheckRecordPda(gatePda, params.user) : [null];
|
|
3415
|
+
const tx = await this.program.methods.checkGate(params.gateId).accounts({
|
|
3416
|
+
gate: gatePda,
|
|
3417
|
+
user: params.user,
|
|
3418
|
+
reputationAccount: params.reputationAccount ?? null,
|
|
3419
|
+
identityAccount: params.identityAccount ?? null,
|
|
3420
|
+
linkAccount: params.linkAccount ?? null,
|
|
3421
|
+
tokenAccount: params.tokenAccount ?? null,
|
|
3422
|
+
checkRecord: checkRecordPda ?? null,
|
|
3423
|
+
payer: this.provider.wallet.publicKey,
|
|
3424
|
+
systemProgram: import_web33.SystemProgram.programId
|
|
3425
|
+
}).rpc();
|
|
3426
|
+
return tx;
|
|
3427
|
+
}
|
|
3428
|
+
/**
|
|
3429
|
+
* Simulate a gate check without submitting a transaction.
|
|
3430
|
+
* Returns true if the user would pass, false otherwise.
|
|
3431
|
+
* Does NOT throw — use this for UI gating.
|
|
3432
|
+
*/
|
|
3433
|
+
async simulateCheckGate(params) {
|
|
3434
|
+
try {
|
|
3435
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3436
|
+
const [checkRecordPda] = params.storeRecord ? await findCheckRecordPda(gatePda, params.user) : [null];
|
|
3437
|
+
await this.program.methods.checkGate(params.gateId).accounts({
|
|
3438
|
+
gate: gatePda,
|
|
3439
|
+
user: params.user,
|
|
3440
|
+
reputationAccount: params.reputationAccount ?? null,
|
|
3441
|
+
identityAccount: params.identityAccount ?? null,
|
|
3442
|
+
linkAccount: params.linkAccount ?? null,
|
|
3443
|
+
tokenAccount: params.tokenAccount ?? null,
|
|
3444
|
+
checkRecord: checkRecordPda ?? null,
|
|
3445
|
+
payer: this.provider.wallet.publicKey,
|
|
3446
|
+
systemProgram: import_web33.SystemProgram.programId
|
|
3447
|
+
}).simulate();
|
|
3448
|
+
return true;
|
|
3449
|
+
} catch {
|
|
3450
|
+
return false;
|
|
3451
|
+
}
|
|
3452
|
+
}
|
|
3453
|
+
// ─────────────────────────────────────────────────────
|
|
3454
|
+
// UPDATE GATE
|
|
3455
|
+
// ─────────────────────────────────────────────────────
|
|
3456
|
+
/**
|
|
3457
|
+
* Update the criteria for an existing gate.
|
|
3458
|
+
*/
|
|
3459
|
+
async updateGateCriteria(params) {
|
|
3460
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3461
|
+
return this.program.methods.updateGateCriteria(params.gateId, params.newCriteria).accounts({
|
|
3462
|
+
gate: gatePda,
|
|
3463
|
+
authority: this.provider.wallet.publicKey
|
|
3464
|
+
}).rpc();
|
|
3465
|
+
}
|
|
3466
|
+
/**
|
|
3467
|
+
* Enable or disable a gate.
|
|
3468
|
+
*/
|
|
3469
|
+
async setGateActive(params) {
|
|
3470
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3471
|
+
return this.program.methods.setGateActive(params.gateId, params.isActive).accounts({
|
|
3472
|
+
gate: gatePda,
|
|
3473
|
+
authority: this.provider.wallet.publicKey
|
|
3474
|
+
}).rpc();
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Transfer gate authority to a new public key.
|
|
3478
|
+
*/
|
|
3479
|
+
async setGateAuthority(params) {
|
|
3480
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3481
|
+
return this.program.methods.setGateAuthority(params.gateId, params.newAuthority).accounts({
|
|
3482
|
+
gate: gatePda,
|
|
3483
|
+
authority: this.provider.wallet.publicKey
|
|
3484
|
+
}).rpc();
|
|
3485
|
+
}
|
|
3486
|
+
// ─────────────────────────────────────────────────────
|
|
3487
|
+
// CLOSE
|
|
3488
|
+
// ─────────────────────────────────────────────────────
|
|
3489
|
+
/**
|
|
3490
|
+
* Close a gate and reclaim rent.
|
|
3491
|
+
*/
|
|
3492
|
+
async closeGate(params) {
|
|
3493
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3494
|
+
const recipient = params.recipient ?? this.provider.wallet.publicKey;
|
|
3495
|
+
return this.program.methods.closeGate(params.gateId).accounts({
|
|
3496
|
+
gate: gatePda,
|
|
3497
|
+
authority: this.provider.wallet.publicKey,
|
|
3498
|
+
recipient
|
|
3499
|
+
}).rpc();
|
|
3500
|
+
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Close a check record and reclaim rent.
|
|
3503
|
+
*/
|
|
3504
|
+
async closeCheckRecord(params) {
|
|
3505
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3506
|
+
const [checkRecordPda] = await findCheckRecordPda(gatePda, params.user);
|
|
3507
|
+
const recipient = params.recipient ?? this.provider.wallet.publicKey;
|
|
3508
|
+
return this.program.methods.closeCheckRecord(params.gateId).accounts({
|
|
3509
|
+
gate: gatePda,
|
|
3510
|
+
user: params.user,
|
|
3511
|
+
checkRecord: checkRecordPda,
|
|
3512
|
+
authority: this.provider.wallet.publicKey,
|
|
3513
|
+
recipient
|
|
3514
|
+
}).rpc();
|
|
3515
|
+
}
|
|
3516
|
+
// ─────────────────────────────────────────────────────
|
|
3517
|
+
// FETCH ACCOUNTS
|
|
3518
|
+
// ─────────────────────────────────────────────────────
|
|
3519
|
+
/**
|
|
3520
|
+
* Fetch a Gate account.
|
|
3521
|
+
*/
|
|
3522
|
+
async fetchGate(gateId) {
|
|
3523
|
+
const [gatePda] = await findGatePda(gateId);
|
|
3524
|
+
try {
|
|
3525
|
+
return await this.program.account.Gate.fetch(gatePda);
|
|
3526
|
+
} catch {
|
|
3527
|
+
return null;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
/**
|
|
3531
|
+
* Fetch a GateCheckRecord for a user.
|
|
3532
|
+
*/
|
|
3533
|
+
async fetchCheckRecord(gateId, user) {
|
|
3534
|
+
const [gatePda] = await findGatePda(gateId);
|
|
3535
|
+
const [checkRecordPda] = await findCheckRecordPda(gatePda, user);
|
|
3536
|
+
try {
|
|
3537
|
+
return await this.program.account.GateCheckRecord.fetch(
|
|
3538
|
+
checkRecordPda
|
|
3539
|
+
);
|
|
3540
|
+
} catch {
|
|
3541
|
+
return null;
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
/**
|
|
3545
|
+
* Fetch all gates owned by a specific authority.
|
|
3546
|
+
*/
|
|
3547
|
+
async fetchGatesByAuthority(authority) {
|
|
3548
|
+
const accounts = await this.program.account.Gate.all([
|
|
3549
|
+
{
|
|
3550
|
+
memcmp: {
|
|
3551
|
+
offset: 8 + 1 + 32,
|
|
3552
|
+
// discriminator + version + gateId
|
|
3553
|
+
bytes: authority.toBase58()
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3556
|
+
]);
|
|
3557
|
+
return accounts;
|
|
3558
|
+
}
|
|
3559
|
+
// ─────────────────────────────────────────────────────
|
|
3560
|
+
// INSTRUCTION BUILDERS (for composing with other txns)
|
|
3561
|
+
// ─────────────────────────────────────────────────────
|
|
3562
|
+
/**
|
|
3563
|
+
* Returns the raw instruction for checkGate — useful for
|
|
3564
|
+
* bundling into a larger transaction (e.g., CPI-style on client side).
|
|
3565
|
+
*/
|
|
3566
|
+
async buildCheckGateInstruction(params) {
|
|
3567
|
+
const [gatePda] = await findGatePda(params.gateId);
|
|
3568
|
+
const [checkRecordPda] = params.storeRecord ? await findCheckRecordPda(gatePda, params.user) : [null];
|
|
3569
|
+
return this.program.methods.checkGate(params.gateId).accounts({
|
|
3570
|
+
gate: gatePda,
|
|
3571
|
+
user: params.user,
|
|
3572
|
+
reputationAccount: params.reputationAccount ?? null,
|
|
3573
|
+
identityAccount: params.identityAccount ?? null,
|
|
3574
|
+
linkAccount: params.linkAccount ?? null,
|
|
3575
|
+
tokenAccount: params.tokenAccount ?? null,
|
|
3576
|
+
checkRecord: checkRecordPda ?? null,
|
|
3577
|
+
payer: this.provider.wallet.publicKey,
|
|
3578
|
+
systemProgram: import_web33.SystemProgram.programId
|
|
3579
|
+
}).instruction();
|
|
3580
|
+
}
|
|
3581
|
+
/**
|
|
3582
|
+
* Build a full transaction that checks a gate and then executes
|
|
3583
|
+
* additional instructions (e.g., mint, vote, transfer).
|
|
3584
|
+
*/
|
|
3585
|
+
async buildGatedTransaction(checkParams, ...additionalInstructions) {
|
|
3586
|
+
const checkIx = await this.buildCheckGateInstruction(checkParams);
|
|
3587
|
+
const tx = new import_web33.Transaction();
|
|
3588
|
+
tx.add(checkIx, ...additionalInstructions);
|
|
3589
|
+
return tx;
|
|
3590
|
+
}
|
|
3591
|
+
};
|
|
3592
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
3593
|
+
0 && (module.exports = {
|
|
3594
|
+
GPASS_PROGRAM_ID,
|
|
3595
|
+
GRAPE_VERIFICATION_PROGRAM_ID,
|
|
3596
|
+
GateCriteriaFactory,
|
|
3597
|
+
GateTypeFactory,
|
|
3598
|
+
GpassClient,
|
|
3599
|
+
IDL,
|
|
3600
|
+
VINE_REPUTATION_PROGRAM_ID,
|
|
3601
|
+
VerificationPlatform,
|
|
3602
|
+
findCheckRecordPda,
|
|
3603
|
+
findGatePda,
|
|
3604
|
+
findGrapeIdentityPda,
|
|
3605
|
+
findGrapeLinkPda,
|
|
3606
|
+
findGrapeSpacePda,
|
|
3607
|
+
findVineConfigPda,
|
|
3608
|
+
findVineReputationPda
|
|
3609
|
+
});
|