peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

A Bug With A Really Big Number of BigInteger #188

Closed arjunaskykok closed 5 years ago

arjunaskykok commented 5 years ago
const bigInt = require('big-integer');

const bigIntString = "822752278660603021077484591278675252491367932816789931674304513";
const a = bigInt(bigIntString);
console.log("bigInt(bigIntString) & 1: ", a & 1);

const b = BigInt(a);
const c = BigInt(a.toString());
console.log("bigIntString                           : ", bigIntString);
console.log("bigInt(bigIntString)                   : ", a.value);
console.log("BigInt(bigInt(bigIntString))           : ", b);
console.log("BigInt(bigInt(bigIntString).toString()): ", c);

The result is:

bigInt(bigIntString) & 1:  0
bigIntString                           :  822752278660603021077484591278675252491367932816789931674304513
bigInt(bigIntString)                   :  822752278660603021077484591278675252491367932816789931674304513
BigInt(bigInt(bigIntString))           :  822752278660603021077484591278675252491367932816789931674304512
BigInt(bigInt(bigIntString).toString()):  822752278660603021077484591278675252491367932816789931674304513

I caught this bug when I tried to implement Elliptic Curve Cryptography with this library. It's for learning purpose.

peterolson commented 5 years ago

If I understand correctly, the problem is that you're using the Javascript & operator instead of the library's .and() method.

Doing

a & 1

will coerce a to native Javascript number (8.22752278660603e+62) and then perform the operation. If you want to preserve precision, you should use the .and operator:

> a.and(1).toString()
"1"