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