[Pkg-javascript-commits] [node-miller-rabin] 01/37: lib: initial
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:20:40 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-miller-rabin.
commit 401db8429fe6c01bb403745ed0dec7789b918c8e
Author: Fedor Indutny <fedor at indutny.com>
Date: Wed Nov 5 12:30:22 2014 -0500
lib: initial
---
.gitignore | 2 ++
lib/mr.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 34 ++++++++++++++++++++++++++++++++++
test/api-test.js | 19 +++++++++++++++++++
4 files changed, 103 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1ca9571
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules/
+npm-debug.log
diff --git a/lib/mr.js b/lib/mr.js
new file mode 100644
index 0000000..4ff6c6f
--- /dev/null
+++ b/lib/mr.js
@@ -0,0 +1,48 @@
+var bn = require('bn.js');
+var brorand = require('brorand');
+
+exports.test = function test(n, k) {
+ var len = n.bitLength();
+ var red = bn.mont(n);
+ var rone = new bn(1).toRed(red);
+
+ // Fermat test
+ if (new bn(2).toRed(red).redPow(n.subn(1)).cmp(rone) !== 0)
+ return false;
+
+ if (!k)
+ k = Math.max(1, (len / 48) | 0);
+
+ // Find d and s, (n - 1) = (2 ^ s) * d;
+ var n1 = n.subn(1);
+ var n2 = n1.subn(1);
+ for (var s = 0; !n1.testn(s); s++) {}
+ var d = n.shrn(s);
+
+ var rn1 = n1.toRed(red);
+
+ var prime = true;
+ for (; k > 0; k--) {
+ do
+ var a = new bn(brorand(Math.ceil(len / 8)));
+ while (a.cmpn(2) < 0 || a.cmp(n2) > 0);
+
+ var x = a.toRed(red).redPow(d);
+ if (x.cmp(rone) === 0 || x.cmp(rn1) === 0)
+ continue;
+
+ for (var i = 1; i < s; i++) {
+ x = x.redSqr();
+
+ if (x.cmp(rone) === 0)
+ return false;
+ if (x.cmp(rn1) === 0)
+ break;
+ }
+
+ if (i === s)
+ return false;
+ }
+
+ return prime;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..3e7417a
--- /dev/null
+++ b/package.json
@@ -0,0 +1,34 @@
+{
+ "name": "miller-rabin",
+ "version": "1.0.0",
+ "description": "Miller Rabin algorithm for primality test",
+ "main": "lib/mr.js",
+ "scripts": {
+ "test": "mocha --reporter=spec test/**/*-test.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:indutny/miller-rabin"
+ },
+ "keywords": [
+ "prime",
+ "miller-rabin",
+ "bignumber"
+ ],
+ "author": "Fedor Indutny <fedor at indutny.com>",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/indutny/miller-rabin/issues"
+ },
+ "homepage": "https://github.com/indutny/miller-rabin",
+ "devDependencies": {
+ "bn.js": "^0.15.0",
+ "mocha": "^2.0.1"
+ },
+ "peerDependencies": {
+ "bn.js": "^0.15.0"
+ },
+ "dependencies": {
+ "brorand": "^1.0.1"
+ }
+}
diff --git a/test/api-test.js b/test/api-test.js
new file mode 100644
index 0000000..476a9ef
--- /dev/null
+++ b/test/api-test.js
@@ -0,0 +1,19 @@
+var assert = require('assert');
+var mr = require('../');
+var bn = require('bn.js');
+
+describe('Miller-Rabin', function() {
+ it('should test number for primality', function() {
+ assert(!mr.test(new bn(221)));
+ assert(mr.test(new bn(257)));
+
+ var p = new bn('dba8191813fe8f51eaae1de70213aafede8f323f95f32cff' +
+ '8b64ebada275cfb18a446a0150e5fdaee246244c5f002ce0' +
+ 'aca97584be1745f2dd1eea2849c52aac8c4b5fb78a1c4da7' +
+ '052774338d3310a6e020c46168cb1f94014e9312511cc4fb' +
+ '79d695bb732449f0e015745b86bfa371dc6ca7386e9c7309' +
+ '5549c2e4b8002873', 16);
+ assert(mr.test(p));
+ assert(!mr.test(p.subn(1)));
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-miller-rabin.git
More information about the Pkg-javascript-commits
mailing list