npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

math-buffer

v0.1.1

Published

big integer math operations implemented on top of node buffers

Downloads

10

Readme

math-buffer

use node buffers as big integers.

testling badge

byte order & base

Buffers and interpreted as base 256 numbers, where each place is 256 times larger than the previous one, instead of 10 times larger. value = Math.pow(256, i).

Also note that this means bytes in a buffer are interpreted in little endian order. This means that a number such as 0x12345678 is represented in a buffer as new Buffer([0x78, 0x56, 0x34, 0x12]). This greatly simplifies the implementation because the least significant byte (digit) also has the lowest index.

api

in general, all methods follow this pattern:

result = op(a, b, m?) where m is an optional buffer that the result will be stored into. If m is not provided, then it will be allocated.

bool = isZero(x)

return true if this buffer represents zero.

fromInt: result = fromInt(int_n, length?)

convert int_n into a buffer, that is at least 4 bytes, but if you supply a longer length value, then it will be zero filled to that length.

add: result = add(a, b, m?)

add a to b and store the result in m (if m is not provided a new buffer will be allocated, and returned) in some cases, a may === m, in other cases, it must be a different buffer.

m may be a, b

subtract: result = subtract(a, b, m?)

subtract b from a and store the result in m (if m is not provided a new buffer will be allocated, and returned)

only positive integers are supported (currently) so a must be larger than b.

m may be a, b

multiply: result = multiply (a, b, m?)

multiply a by b.

m must not be a, b

modInt: int_result = modInt(a, int_n)

get the modulus of dividing a big number with a 32 bit int.

compare: order = compare(a, b)

Compare whether a is smaller than (-1), equal (0), or greater than b (1) this the same signature as is expected by Array.sort(comparator)

shift: result = shift(a, bits, m?)

move a big number across by bits. bits can be both positive or negative. a positive number of bits is the same as Math.pow(a, bits) This is an essential part of divide

m may be a

mostSignificantBit: bits = mostSignificantBit (a)

Find the position of the largest valued bit in the number. (since the buffer may have trailing zeros, it's not necessaryily in the last byte)

divide: {quotient, remainder} = divide (a, b, q?, r?)

Divide a by b, updating the q (quotient) and r (remainder) buffers.

a,b,q, and r must all be different buffers.

square: result = square (a, m?)

multiply a by itself.

m must not be a.

power: result = power (e, x, mod?)

Raise e to the power of x, If mod is provided, the result will be e^x % mod.

gcd: result = gcd(u, v, mutate=false)

Calculate the greatest common divisor using the binary gcd algorithm

If mutate is true, the inputs will be mutated, by default, new buffers will be allocated.

inverse: x = inverse(a, m)

Calculate the modular multiplicative inverse This is the inverse of a*x % m = 1.

My implementation is based on sjcl's implementation Unfortunately I was unable to find any reference explaining this particular algorithm, (the benefit this algorithm is that it doesn't require negative numbers, which I havn't implemented)

TODO

  • isProbablyPrime (needed for RSA)

License

MIT