## number-crunch 1.2.3 • Public • Published

# Crunch      Crunch is an arbitrary-precision integer arithmetic library for JavaScript.

It was designed to execute arithmetic operations as quickly as possible, in particular those upon which asymmetric encryption cryptosystems such as RSA are built.

## Usage

Crunch can be loaded as a classic browser script

or in a web worker

or it can be used as a node module

Example 1

The library accepts and returns 8-bit integer arrays which represent artbitrary-precision (big) integers, but internally it uses 28-bit arrays and performs the conversions automatically.

Crunch also converts the between big integer byte-array representation and base-10 strings (a string is used as the Number type could not represent large numbers) using the .stringify() and .parse() functions.

Example 2

## Functions

Function Input Parameters Output
add x, y x + y
sub x, y x - y
mul x, y x * y
mulk x, y x * y Karatsuba algorithm
div x, y x / y
sqr x x * x
mod x, y x % y
bmr x, y, [mu] x % y
exp x, e, n x^e % n
gar x, p, q, d, u, [dp1], [dq1] x^d % pq
inv x, y 1/x % y
cut x Remove leading zeroes of x
zero x Return zero array of length x
and x, y x AND y
or x, y x OR y
xor x, y x XOR y
not x NOT x
leftShift x, s x << s
rightShift1 x, s x >>> s
compare x, y -1: x < y, 0: x = y, 1: x > y
decrement x x - 1
increment x x + 1
factorial n n! [n < 268435456]
nextPrime x First prime after x
testPrime x Boolean x is prime
stringify x String (base 10 representation)
parse s Arbitrary-precision integer
config rawIn, rawOut Crunch object, changed rawIn/rawOut

1: Be aware, negative numbers retain their sign but Crunch uses a form of signed-magnitude representation rather than two's complement representation for negative numbers. Right shifting will not produce the same result as when it is done to complement representation. So: -4 >> 5 will be -0, not -1.

Miller-Rabin primality testing `mrb`, simple mod `mds` and greatest common divisor `gcd` are also implemented as internal methods not exposed via the Crunch object.

## Web Workers

Crunch can be loaded in a Web Worker. Instruction messages are sent to the worker in the following format:

Example 3

## Keywords

### Repository

github.com/vukicevic/crunch

### Homepage

github.com/vukicevic/crunch

2

1.2.3

MIT

37.6 kB

5