-
Notifications
You must be signed in to change notification settings - Fork 117
/
index.js
381 lines (322 loc) · 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var bigint = new require('./build/default/bigint');
module.exports = BigInt;
function BigInt (num, base) {
if (!(this instanceof BigInt)) return new BigInt(num, base);
if (typeof num !== 'string') num = num.toString(base || 10);
if (num.match(/e\+/)) { // positive exponent
if (!Number(num).toString().match(/e\+/)) {
this.id = bigint.fromString(
Math.floor(Number(num)).toString(), 10
);
}
else {
var pow = Math.ceil(Math.log(num) / Math.log(2));
var n = (num / Math.pow(2, pow)).toString(2)
.replace(/^0/,'');
var i = n.length - n.indexOf('.');
n = n.replace(/\./,'');
for (; i <= pow; i++) n += '0';
this.id = bigint.fromString(n, 2);
}
}
else if (num.match(/e\-/)) { // negative exponent
this.id = bigint.fromString(
Math.floor(Number(num)).toString(),
base || 10
);
}
else {
this.id = bigint.fromString(num, base || 10);
}
}
BigInt.fromId = function (id) {
var bigi = Object.create(BigInt.prototype);
bigi.id = id;
return bigi;
};
BigInt.prototype.destroy = function () {
if (this.destroyed) throw new Error('BigInt already destroyed');
bigint.destroy(this.id);
this.destroyed = true;
};
BigInt.prototype.inspect = function () {
return '<BigInt ' + this.toString(10) + '>';
};
BigInt.prototype.toString = function (base) {
if (this.destroyed) throw new Error('BigInt already destroyed');
return bigint.toString(this.id, base || 10);
};
BigInt.prototype.toNumber = function () {
return parseInt(this.toString(), 10);
};
[ 'add', 'sub', 'mul', 'div', 'mod' ].forEach(function (op) {
BigInt.prototype[op] = function (num) {
if (this.destroyed) throw new Error('BigInt already destroyed');
if (num instanceof BigInt) {
return BigInt.fromId(bigint['b'+op](this.id, num.id));
}
else if (typeof num === 'number') {
if (num >= 0) {
return BigInt.fromId(bigint['u'+op](this.id, num));
}
else if (op === 'add') {
return BigInt.fromId(bigint.usub(this.id, -num));
}
else if (op === 'sub') {
return BigInt.fromId(bigint.uadd(this.id, -num));
}
else {
var x = new BigInt(num);
var res = BigInt.fromId(bigint['b'+op](this.id, x.id));
x.destroy();
return res;
}
}
else if (typeof num === 'string') {
var x = new BigInt(num);
var res = BigInt.fromId(bigint['b'+op](this.id, x.id));
x.destroy();
return res;
}
else {
throw new TypeError('Unspecified operation for type '
+ (typeof num) + ' for ' + op);
}
};
});
BigInt.prototype.abs = function () {
if (this.destroyed) throw new Error('BigInt already destroyed');
return BigInt.fromId(bigint.babs(this.id));
};
BigInt.prototype.neg = function () {
if (this.destroyed) throw new Error('BigInt already destroyed');
return BigInt.fromId(bigint.bneg(this.id));
};
BigInt.prototype.powm = function (num, mod) {
if (this.destroyed) throw new Error('BigInt already destroyed');
var m, cleanMod = function() {}, res;
if ((typeof mod) === 'number' || (typeof mod) === 'string') {
m = new BigInt(mod);
cleanMod = function(bi) { bi.destroy(); };
} else if (mod instanceof BigInt) {
m = mod;
}
if ((typeof num) === 'number') {
res = BigInt.fromId(bigint.upowm(this.id, num, m.id));
} else if ((typeof num) === 'string') {
var n = new BigInt(num);
res = BigInt.fromId(bigint.bpowm(this.id, n.id, m.id));
n.destroy();
} else if (num instanceof BigInt) {
res = BigInt.fromId(bigint.bpowm(this.id, num.id, m.id));
}
cleanMod(m);
return res;
}
BigInt.prototype.pow = function (num) {
if (this.destroyed) throw new Error('BigInt already destroyed');
if (typeof num === 'number') {
if (num >= 0) {
return BigInt.fromId(bigint.upow(this.id, num));
}
else {
return BigInt.prototype.powm.call(this, num, this);
}
}
else {
var x = parseInt(num.toString(), 10);
return BigInt.prototype.pow.call(this, x);
}
};
BigInt.prototype.cmp = function (num) {
if (num instanceof BigInt) {
return bigint.bcompare(this.id, num.id);
}
else if (typeof num === 'number') {
if (num < 0) {
return bigint.scompare(this.id, num);
}
else {
return bigint.ucompare(this.id, num);
}
}
else {
var x = bigint(num);
var res = bigint.bcompare(this.id, x.id);
x.destroy();
return res;
}
};
BigInt.prototype.gt = function (num) {
return this.cmp(num) > 0;
};
BigInt.prototype.ge = function (num) {
return this.cmp(num) >= 0;
};
BigInt.prototype.eq = function (num) {
return this.cmp(num) === 0;
};
BigInt.prototype.ne = function (num) {
return this.cmp(num) !== 0;
};
BigInt.prototype.lt = function (num) {
return this.cmp(num) < 0;
};
BigInt.prototype.le = function (num) {
return this.cmp(num) <= 0;
};
'and or xor'.split(' ').forEach(function (name) {
BigInt.prototype[name] = function (num) {
if (num instanceof BigInt) {
return BigInt.fromId(bigint['b' + name](this.id, num.id));
}
else {
var x = new BigInt(num);
var res = bigint['b' + name](this.id, x.id);
x.destroy();
return BigInt.fromId(res);
}
};
});
BigInt.prototype.rand = function (to) {
if (this.destroyed) throw new Error('BigInt already destroyed');
if (to === undefined) {
if (this.toString() === '1') {
return new BigInt(0);
}
else {
return BigInt.fromId(bigint.brand0(this.id));
}
}
else {
var x = to instanceof BigInt
? to.sub(this)
: BigInt.sub(to, this)
;
var y = BigInt.fromId(bigint.brand0(x.id));
var res = y.add(this);
x.destroy();
y.destroy();
return res;
}
};
BigInt.prototype.invertm = function (mod) {
if (this.destroyed) throw new Error('BigInt already destroyed');
if (mod instanceof BigInt) {
return BigInt.fromId(bigint.binvertm(this.id, mod.id));
}
else {
var x = bigint(mod);
var res = BigInt.fromId(bigint.binvertm(this.id, x.id));
x.destroy();
return res;
}
};
BigInt.prototype.probPrime = function (reps) {
var n = bigint.probprime(this.id, reps || 10);
return { 2 : true, 1 : 'maybe', 0 : false }[n];
};
BigInt.prototype.nextPrime = function () {
return BigInt.fromId(bigint.nextprime(this.id));
};
BigInt.fromBuffer = function (buf, opts) {
if (!opts) opts = {};
var order = { 1 : 'forward', '-1' : 'backward' }[opts.order]
|| opts.order || 'forward'
;
var endian = { 1 : 'big', '-1' : 'little' }[opts.endian]
|| opts.endian || 'big'
;
var size = opts.size || 1;
if (buf.length % size !== 0) {
throw new RangeError('Buffer length (' + buf.length + ')'
+ ' must be a multiple of size (' + size + ')'
);
}
var hex = [];
for (var i = 0; i < buf.length; i += size) {
var chunk = [];
for (var j = 0; j < size; j++) {
chunk.push(buf[
i + (endian === 'big' ? j : (size - j - 1))
]);
}
hex.push(chunk
.map(function (c) {
return (c < 16 ? '0' : '') + c.toString(16);
})
.join('')
);
}
return new BigInt(
(order === 'forward' ? hex : hex.reverse()).join(''), 16
);
};
BigInt.prototype.toBuffer = function (opts) {
if (typeof opts === 'string') {
if (opts !== 'mpint') return 'Unsupported Buffer representation';
var abs = this.abs();
var buf = abs.toBuffer({ size : 1, endian : 'big' });
abs.destroy();
var len = buf.length === 1 && buf[0] === 0 ? 0 : buf.length;
if (buf[0] & 0x80) len ++;
var ret = new Buffer(4 + len);
if (len > 0) buf.copy(ret, 4 + (buf[0] & 0x80 ? 1 : 0));
if (buf[0] & 0x80) ret[4] = 0;
ret[0] = len & (0xff << 24);
ret[1] = len & (0xff << 16);
ret[2] = len & (0xff << 8);
ret[3] = len & (0xff << 0);
// two's compliment for negative integers:
var isNeg = this.lt(0);
if (isNeg) {
for (var i = 4; i < ret.length; i++) {
ret[i] = 0xff - ret[i];
}
}
ret[4] = (ret[4] & 0x7f) | (isNeg ? 0x80 : 0);
if (isNeg) ret[ret.length - 1] ++;
return ret;
}
if (!opts) opts = {};
var order = { 1 : 'forward', '-1' : 'backward' }[opts.order]
|| opts.order || 'forward'
;
var endian = { 1 : 'big', '-1' : 'little' }[opts.endian]
|| opts.endian || 'big'
;
var size = opts.size || 1;
var hex = this.toString(16);
if (hex.charAt(0) === '-') throw new Error(
'converting negative numbers to Buffers not supported yet'
);
var len = Math.ceil(hex.length / (2 * size)) * size;
var buf = new Buffer(len);
// zero-pad the hex string so the chunks are all `size` long
while (hex.length < 2 * len) hex = '0' + hex;
var hx = hex
.split(new RegExp('(.{' + (2 * size) + '})'))
.filter(function (s) { return s.length > 0 })
;
(order === 'forward' ? hx : hx.reverse())
.forEach(function (chunk, i) {
for (var j = 0; j < size; j++) {
var ix = i * size + (endian === 'big' ? j : size - j - 1);
buf[ix] = parseInt(chunk.slice(j*2,j*2+2), 16);
}
});
return buf;
};
Object.keys(BigInt.prototype).forEach(function (name) {
if (name === 'inspect' || name === 'toString') return;
BigInt[name] = function (num) {
var args = [].slice.call(arguments, 1);
if (num instanceof BigInt) {
return num[name].apply(num, args);
}
else {
var bigi = new BigInt(num);
return bigi[name].apply(bigi, args);
}
};
});