/
bignum.js
284 lines (229 loc) · 7.15 KB
/
bignum.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
'use strict';
const bignum = require('bignum-browserify');
const sslBignum = require('bignum');
const core = require('./core.js');
const hll = require('./hll.js');
const op = {};
exports.op = op;
function intishBool(b) {
return b ? 1 : 0;
}
function makeNum(type, num) {
const instance = type._STable.REPR.allocate(type._STable);
instance.$$setNum(num);
return instance;
}
function makeBI(type, num) {
const instance = type._STable.REPR.allocate(type._STable);
instance.$$setBignum(num);
return instance;
}
function getBI(obj) {
return obj.$$getBignum();
}
op.fromstr_I = function(str, type) {
return makeBI(type, bignum(str));
};
op.tostr_I = function(n) {
return getBI(n).toString();
};
op.base_I = function(n, base) {
const orig = getBI(n);
if (orig.eq(0)) return '0';
if (base == 16 || base == 10) {
return orig.toString(base).toUpperCase().replace(/^(-?)0+/, '$1');
} else if (1 < base && base <= 36) {
let num = orig.abs();
let string = '';
const letters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (num.gt(0)) {
string = letters[num.mod(base).toNumber()] + string;
num = num.div(base);
}
return (orig.lt(0) ? '-' : '') + string;
} else {
throw 'base_I with base: ' + base + ' NYI';
}
};
op.iseq_I = function(a, b) {
return intishBool(getBI(a).eq(getBI(b)));
};
op.isne_I = function(a, b) {
return intishBool(!getBI(a).eq(getBI(b)));
};
op.mul_I = function(a, b, type) {
return makeBI(type, getBI(a).mul(getBI(b)));
};
op.add_I = function(a, b, type) {
return makeBI(type, getBI(a).add(getBI(b)));
};
op.sub_I = function(a, b, type) {
return makeBI(type, getBI(a).sub(getBI(b)));
};
op.sub_I = function(a, b, type) {
return makeBI(type, getBI(a).sub(getBI(b)));
};
op.div_I = function(a, b, type) {
const divident = getBI(a);
const divisor = getBI(b);
// workaround for .div rounding to zero not down
if (divisor.lt(0) != divident.lt(0) && divident.mod(divisor).ne(0)) {
return makeBI(type, divident.div(divisor).sub(1));
}
return makeBI(type, divident.div(divisor));
};
op.pow_I = function(a, b, numType, biType) {
const base = getBI(a);
const exponent = getBI(b);
if (exponent.lt(0)) {
return makeNum(numType, Math.pow(base.toNumber(), exponent.toNumber()));
} else {
if (exponent.gt(4294967296) && !base.eq(1) && !base.eq(0)) {
if (base.eq(-1)) {
// workaround a bug in bignum
return makeBI(biType, bignum(exponent.mod(2).eq(0) ? 1 : -1));
} else {
return makeNum(numType, base.lt(0) && exponent.mod(2).eq(1) ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY);
}
}
return makeBI(biType, base.pow(exponent));
}
};
op.mod_I = function(n, m, type) {
/* TODO - think if this can be optimized. */
/* We are doing this in complicated way because,
bignum returns the module with the sign equal to the dividend not the divisor. */
const a = getBI(n);
const b = getBI(m);
if ((a.lt(0) && b.gt(0)) || (a.gt(0) && b.lt(0))) {
const x = a.div(b).sub(1);
const ret = a.sub(b.mul(x));
return makeBI(type, (ret.eq(b) ? bignum(0) : ret));
}
return makeBI(type, a.mod(b));
};
op.neg_I = function(a, type) {
return makeBI(type, getBI(a).neg());
};
op.isbig_I = function(n) {
/* Check if it needs more bits than a long can offer; note that
* bitLength excludes sign considerations, thus 31 rather than
* 32. */
return getBI(n).bitLength() > 31 ? 1 : 0;
};
op.expmod_I = function(a, b, c, type) {
return makeBI(type, getBI(a).powm(getBI(b), getBI(c)));
};
/* TODO - optimize by using a smaller bignum when so much isn't needed */
const digits = 325;
const digitsBignum = bignum(10).pow(digits);
op.div_In = function(a, b) {
const divisor = getBI(b);
if (divisor.eq(0)) {
return getBI(a).toNumber() / 0;
}
let sign = 1;
let big = getBI(a).mul(digitsBignum).div(divisor).toString();
if (big.substr(0, 1) == '-') {
big = big.substr(1);
sign = -1;
}
if (big.length <= digits) {
return sign * parseFloat('0.' + '0'.repeat(digits - big.length) + big);
} else {
return sign * parseFloat(big.substr(0, big.length - digits) + '.' + big.substr(big.length - digits));
}
};
op.rand_I = function(max, type) {
return makeBI(type, bignum(sslBignum(getBI(max).toString()).rand().toString()));
};
op.isle_I = function(a, b) {
return intishBool(getBI(a).le(getBI(b)));
};
op.islt_I = function(a, b) {
return intishBool(getBI(a).lt(getBI(b)));
};
op.isge_I = function(a, b) {
return intishBool(getBI(a).ge(getBI(b)));
};
op.isgt_I = function(a, b) {
return intishBool(getBI(a).gt(getBI(b)));
};
op.cmp_I = function(a, b) {
const result = getBI(a).cmp(getBI(b));
return result == 0 ? 0 : (result < 0 ? -1 : 1);
};
op.isprime_I = function(n) {
return intishBool(sslBignum(getBI(n).toString()).probPrime(50));
};
op.bitshiftl_I = function(a, b, type) {
return makeBI(type, getBI(a).shiftLeft(b));
};
op.bitshiftr_I = function(a, b, type) {
return makeBI(type, getBI(a).shiftRight(b));
};
op.bitand_I = function(a, b, type) {
return makeBI(type, getBI(a).and(getBI(b)));
};
op.bitor_I = function(a, b, type) {
return makeBI(type, getBI(a).or(getBI(b)));
};
op.bitxor_I = function(a, b, type) {
return makeBI(type, getBI(a).xor(getBI(b)));
};
op.bitneg_I = function(a, type) {
return makeBI(type, getBI(a).neg().sub(1));
};
op.lcm_I = function(n, m, type) {
const a = getBI(n);
const b = getBI(m);
return makeBI(type, (a.abs().div(a.gcd(b)).mul(b.abs())));
};
op.gcd_I = function(a, b, type) {
return makeBI(type, getBI(a).gcd(getBI(b)));
};
op.abs_I = function(n, type) {
return makeBI(type, getBI(n).abs());
};
op.tonum_I = function(n) {
return getBI(n).toNumber();
};
op.fromI_I = function(value, type) {
return makeBI(type, getBI(value));
};
op.fromnum_I = function(num, type) {
// node-bignum bug workaround, when a negative number is too big it gets turned into 0
if (num < 0) {
return makeBI(type, bignum(Math.floor(-num)).neg());
} else {
return makeBI(type, bignum(Math.floor(num)));
}
};
op.bool_I = function(n) {
return intishBool(getBI(n).toNumber());
};
op.radix_I = function(currentHLL, radix, str, zpos, flags, type) {
const extracted = core.radixHelper(radix, str, zpos, flags);
if (extracted == null) {
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1]);
}
if (radix == 10 || radix == 16) {
const pow = bignum(radix).pow(extracted.power);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset]);
} else {
const n = extracted.number;
let base = bignum(1);
let result = bignum(0);
for (let i = n.length - 1; i >= 0; i--) {
let digit = n.charCodeAt(i);
if (digit >= 48 && digit <= 57) digit -= 48; // 0-9
else if (digit >= 97 && digit <= 122) digit = digit - 97 + 10; // a-z
else if (digit >= 65 && digit <= 90) digit = digit - 65 + 10; // A-Z
else break;
result = result.add(base.mul(digit));
base = base.mul(radix);
}
if (n[0] == '-') result = result.neg();
return hll.slurpyArray(currentHLL, [makeBI(type, result), makeBI(type, base), extracted.offset]);
}
};