indutny / bn.js

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

Equality seems to be broken when BN created from strings with prefixed 0's #261

Closed pigfrown closed 3 years ago

pigfrown commented 3 years ago
> b1 = new BN("1")
BN { negative: 0, words: [ 1 ], length: 1, red: null }
> b2 = new BN("01")
BN { negative: 0, words: [ 1 ], length: 1, red: null }
> b1 == b2
false
fanatid commented 3 years ago

JavaScript does not support operators overloading and because BN is an Object you will get true only when variables contain the same references. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality

If the operands are both objects, return true only if both operands reference the same object.

If you want to compare 2 BN objects you need to use methods https://github.com/indutny/bn.js#utilities like .eq.