indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Wrong `cmpn` result #294

Open devDesu opened 2 years ago

devDesu commented 2 years ago

Version: 5.2.0 Node version: v17.5.0

let x = new BN(9000000000);
assert.equal(x.gten(100000000000), false); // error!

I've checked cmpn function and it seems that it's the source of the problem because x.cmpn(100000000000) returns 1 which means that A (9000000000) > B (100000000000)

This works fine

let x = new BN(9000000000);
assert.equal(x.gte(new BN(100000000000)), false); // ok
fanatid commented 2 years ago

Thanks. Probably we need to move this assertion before if. https://github.com/indutny/bn.js/blob/db57519421f0c47c9f68c05fa6fc12273dcca2c2/lib/bn.js#L2854

Currently, I'd recommend using BigInt instead of bn.js.