math-buffer
Advanced tools
Comparing version 0.1.0 to 0.1.1
71
index.js
@@ -28,8 +28,11 @@ //add two buffers, | ||
//okay, need addShift(a, b, n) | ||
//a + (b<<n) | ||
var add = exports.add = function (a, b, c) { | ||
var l = Math.max(a.length, b.length) | ||
if(a.length == b.length && (a[l-1] + b[l-1])>>1) | ||
l ++ | ||
if(!c) { c = new Buffer(l); c.fill() } | ||
@@ -49,2 +52,3 @@ var carry = 0 | ||
//basically the reverse of add. | ||
var subtract = exports.sub = | ||
@@ -83,4 +87,6 @@ exports.subtract = function (a, b, c) { | ||
while(l--) { | ||
if((a[l] || 0) !== (b[l] || 0)) | ||
return a[l] < b[l] ? -1 : 1 | ||
var x = a[l] || 0 | ||
var y = b[l] || 0 | ||
if(x !== y) | ||
return x < y ? -1 : 1 | ||
} | ||
@@ -119,12 +125,15 @@ return 0 | ||
//multiply, using shift + add | ||
var multiply = | ||
exports.mul = | ||
var multiply = exports.mul = | ||
exports.multiply = function (a, b, m) { | ||
var w = new Buffer(b) | ||
var l = a.length * 8, prev = 0 | ||
var l = a.length + b.length, prev = 0 | ||
var w = new Buffer(l); w.fill() | ||
if(!m) m = new Buffer(a.length) | ||
m.fill() //m must be zeros, or it will affect stuff. | ||
;(Array.isArray(b) ? new Buffer(b) : b).copy(w) | ||
for(var i = 0; i < l; i++) { | ||
if(!m) m = new Buffer(l) | ||
if(a !== m) m.fill() //m must be zeros, or it will affect stuff. | ||
var bits = l<<3 | ||
for(var i = 0; i < bits; i++) { | ||
if(getBit(a, i)) { | ||
@@ -160,4 +169,4 @@ shift(w, i - prev, w) | ||
} | ||
var divide = | ||
exports.divide = function (a, b, q, r) { | ||
var divide = exports.divide = exports.div = function (a, b, q, r) { | ||
if(!q) q = new Buffer(a.length) | ||
@@ -169,3 +178,3 @@ if(!r) r = new Buffer(a.length) | ||
q.fill() | ||
r.copy(a) //remainder is the same | ||
;(Array.isArray(r) ? new Buffer(r) : r).copy(a) //remainder is the same | ||
return {quotient: q, remainder: r} | ||
@@ -179,3 +188,3 @@ } | ||
_b.fill() | ||
a.copy(r) | ||
;(Array.isArray(a) ? new Buffer(a) : a ).copy(r) | ||
@@ -189,2 +198,3 @@ //shift b to line up with a. | ||
// at least in sparse numbers. | ||
var bit = mA - mB | ||
@@ -214,4 +224,4 @@ while(bit >= 0) { | ||
//http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method | ||
var pow = | ||
exports.pow = | ||
var pow = exports.pow = | ||
exports.power = function (base, exp, mod) { | ||
@@ -244,2 +254,3 @@ var result = new Buffer(exp.length) | ||
} | ||
var shifts = 0; | ||
@@ -263,18 +274,20 @@ | ||
//while u is even, divide by two. | ||
while (~u[0] & 1) | ||
while (isEven(u)) { | ||
shift(u, -1, u) | ||
} | ||
//From here on, u is always odd. | ||
do { | ||
//if v is even, 2 cannot be a divisor. | ||
while (~v[0] & 1) { | ||
shift(v, -1, v); | ||
} | ||
// Now u and v are both odd. | ||
// swap so that v is larger, so that we can safely subtract v - u | ||
if (compare(u, v) > 0) { | ||
var t = v; v = u; u = t; | ||
} | ||
subtract(v, u, v) | ||
} while (!isZero(v)); | ||
//if v is even, 2 cannot be a divisor. | ||
while (isEven(v)) shift(v, -1, v) | ||
// Now u and v are both odd. | ||
// swap so that v is larger, so that we can safely subtract v - u | ||
if (compare(u, v) > 0) { | ||
var t = v; v = u; u = t; | ||
} | ||
v = subtract(v, u) | ||
//v is now even, because even = odd1 - odd2 | ||
} while (!isZero(v)) | ||
//multiply by the factors of 2 we removed at line 240 | ||
@@ -281,0 +294,0 @@ return shift(u, shifts, u) |
{ | ||
"name": "math-buffer", | ||
"description": "big integer math operations implemented on top of node buffers", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/dominictarr/math-buffer", | ||
@@ -12,3 +12,4 @@ "repository": { | ||
"devDependencies": { | ||
"tape": "~2.3.2" | ||
"tape": "~2.3.2", | ||
"bignum": "~0.6.2" | ||
}, | ||
@@ -19,3 +20,22 @@ "scripts": { | ||
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"testling": { | ||
"files": "test/*.js", | ||
"browsers": [ | ||
"ie/8..latest", | ||
"firefox/17..latest", | ||
"firefox/nightly", | ||
"chrome/22..latest", | ||
"chrome/canary", | ||
"opera/12..latest", | ||
"opera/next", | ||
"safari/5.1..latest", | ||
"ipad/6.0..latest", | ||
"iphone/6.0..latest", | ||
"android-browser/4.2..latest" | ||
] | ||
}, | ||
"browser": { | ||
"bignum": false | ||
} | ||
} |
@@ -5,2 +5,5 @@ # math-buffer | ||
[![testling badge](https://ci.testling.com/dominictarr/math-buffer.png)](https://ci.testling.com/dominictarr/math-buffer) | ||
## byte order & base | ||
@@ -107,3 +110,3 @@ | ||
### gcd: `x = inverse(a, m)` | ||
### inverse: `x = inverse(a, m)` | ||
@@ -110,0 +113,0 @@ Calculate the [modular multiplicative inverse](http://en.wikipedia.org/wiki/Modular_multiplicative_inverse) |
var big = require('../') | ||
var tape = require('tape') | ||
var u = require('./util') | ||
function B () { | ||
@@ -8,11 +8,5 @@ return new Buffer([].slice.call(arguments)) | ||
function clone (a) { | ||
return JSON.parse(JSON.stringify(a)) | ||
} | ||
var clone = u.clone | ||
var equal = u.equal | ||
function equal(t, a, b, message) { | ||
//clone to turn Buffers into arrays which makes deepEquals work better. | ||
t.deepEqual(clone(a), clone(b), message) | ||
} | ||
function createTest(t, forward, reverse, mutate) { | ||
@@ -116,4 +110,4 @@ return function (a, b, c) { | ||
tape('multiply - carry2', function (t) { | ||
var a = new Buffer([0x00, 0x10, 0x00, 0x00]) | ||
var b = new Buffer([0x00, 0x10, 0x00, 0x00]) | ||
var a = new Buffer([0x00, 0x10]) | ||
var b = new Buffer([0x00, 0x10]) | ||
var c = new Buffer([0x00, 0x00, 0x00, 0x01]) | ||
@@ -126,4 +120,4 @@ var m = new Buffer(4); m.fill() | ||
tape('multiply - carry3', function (t) { | ||
var a = new Buffer([0x10, 0x10, 0, 0]) | ||
var b = new Buffer([0x10, 0x10, 0, 0]) | ||
var a = new Buffer([0x10, 0x10]) | ||
var b = new Buffer([0x10, 0x10]) | ||
var c = new Buffer([0x00, 0x01, 0x02, 0x01]) | ||
@@ -137,4 +131,4 @@ var m = new Buffer(4); m.fill() | ||
tape('multiply - carry3', function (t) { | ||
var a = new Buffer([0x12, 0x34, 0, 0]) | ||
var b = new Buffer([0x56, 0x78, 0, 0]) | ||
var a = new Buffer([0x12, 0x34]) | ||
var b = new Buffer([0x56, 0x78]) | ||
var c = new Buffer([0x0c, 0xee, 0x79, 0x18]) | ||
@@ -141,0 +135,0 @@ var m = new Buffer(4); m.fill() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
347217
25
6318
126
2
2
1