1.2.3 • Published 6 years ago

number-crunch v1.2.3

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Crunch Build Status NPM version Bower version License Codacy grade Codacy coverage

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

<script src="crunch.js"></script>
<script>
var crunch = Crunch();
</script>

or in a web worker

var crunch = new Worker("crunch.js");

or it can be used as a node module

npm install number-crunch
var crunch = require("number-crunch");

Example 1

x = [10, 123, 21, 127];
y = [4, 211, 176, 200];
crunch.add(x, y); //[15, 78, 198, 71]

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

crunch.stringify([1,2,3,4,5,6,7,8,9,0]); // "4759477275222530853120"

crunch.parse("4759477275222530853120");  // [1,2,3,4,5,6,7,8,9,0]

Functions

FunctionInput ParametersOutput
addx, yx + y
subx, yx - y
mulx, yx * y
mulkx, yx * y Karatsuba algorithm
divx, yx / y
sqrxx * x
modx, yx % y
bmrx, y, mux % y
expx, e, nx^e % n
garx, p, q, d, u, dp1, dq1x^d % pq
invx, y1/x % y
cutxRemove leading zeroes of x
zeroxReturn zero array of length x
andx, yx AND y
orx, yx OR y
xorx, yx XOR y
notxNOT x
leftShiftx, sx << s
rightShift1x, sx >>> s
comparex, y-1: x < y, 0: x = y, 1: x > y
decrementxx - 1
incrementxx + 1
factorialnn! n < 268435456
nextPrimexFirst prime after x
testPrimexBoolean x is prime
stringifyxString (base 10 representation)
parsesArbitrary-precision integer
transformx, toRawRadix conversion
configrawIn, rawOutCrunch 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:

{func: "", args: []}

Example 3

var crunch = new Worker("crunch.js");
var message = {func: "add", args: [[10, 123, 21, 127], [4, 211, 176, 200]]};

crunch.onmessage = function(m) {
	console.log(m);
};

crunch.postMessage(message);
1.2.3

6 years ago

1.2.2

7 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

9 years ago

1.0.9

9 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago