The jsbn library is a fast, portable implementation of large-number math in pure JavaScript, enabling public-key crypto and other applications on desktop and mobile browsers.
I felt compelled to put this on github and publish to npm. I haven't tested every other big integer library out there, but the few that I have tested in comparison to this one have not even come close in performance. I am aware of the
bimodule on npm, however it has been modified and I wanted to publish the original without modifications. This is jsbn and jsbn2 from Tom Wu's original website above, with the module pattern applied to prevent global leaks and to allow for use with node.js on the server side.
var BigInteger = require('jsbn').BigInteger;var bi = new BigInteger('91823918239182398123'); console.log(bi.bitLength()); // 67
bi.toString([radix]) => string
Returns a string representing the BigInteger
bi.
radix- Optional. An integer between 2 and 36 specifying the base to use for representing numeric values.
If the
radixis not specified, the preferred
radixis assumed to be 10.
bi.negate() => BigInteger
Returns a new
BigIntegerequal to the negation of
bi.
bi.abs() => BigInteger
Returns a
BigIntegerequal to the absolute value of
bi.
note: if the
biis a positive value, it will be returned as is, otherwise a new instance of
BigIntegeris returned.
bi.compareTo(other) => number
Compare to
BigIntegers. The return value will be a negative JavaScript number if
biis less than
other, a positive number if
biis greater than
other, and
0if
biand
otherrepresents the same integer.
bi.bitLength() => number
Returns the number of bits used to store
bias a JavaScript number.
bi.mod(m) => BigInteger
Returns a
BigIntegerwith the value of (
bimod
m).
bi.modPow(exponent, m) => BitInteger
Returns a
BigIntegerwith the value of (
bi
exponentmod
m).