diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/miller-rabin | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/miller-rabin')
-rw-r--r-- | node_modules/miller-rabin/.npmignore | 2 | ||||
-rw-r--r-- | node_modules/miller-rabin/README.md | 26 | ||||
-rwxr-xr-x | node_modules/miller-rabin/bin/miller-rabin | 29 | ||||
-rw-r--r-- | node_modules/miller-rabin/lib/mr.js | 113 | ||||
-rw-r--r-- | node_modules/miller-rabin/package.json | 32 | ||||
-rw-r--r-- | node_modules/miller-rabin/test/api-test.js | 18 |
6 files changed, 220 insertions, 0 deletions
diff --git a/node_modules/miller-rabin/.npmignore b/node_modules/miller-rabin/.npmignore new file mode 100644 index 000000000..1ca957177 --- /dev/null +++ b/node_modules/miller-rabin/.npmignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log diff --git a/node_modules/miller-rabin/README.md b/node_modules/miller-rabin/README.md new file mode 100644 index 000000000..e9d76f67c --- /dev/null +++ b/node_modules/miller-rabin/README.md @@ -0,0 +1,26 @@ +# Miller-Rabin + +#### LICENSE + +This software is licensed under the MIT License. + +Copyright Fedor Indutny, 2014. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/miller-rabin/bin/miller-rabin b/node_modules/miller-rabin/bin/miller-rabin new file mode 100755 index 000000000..2e18dfd53 --- /dev/null +++ b/node_modules/miller-rabin/bin/miller-rabin @@ -0,0 +1,29 @@ +#!/usr/bin/env node +var bn = require('bn.js'); +var fs = require('fs'); +var mr = require('../').create(); + +var num = ''; +if (process.argv[2]) { + num += fs.readFileSync(process.argv[2]); + start(num); +} else { + process.stdin.on('data', function(chunk) { + num += chunk.toString().replace(/[^0-9a-f]/gi, ''); + }); + process.stdin.once('end', function() { + start(num); + }); +} + +function start(text) { + var num = new bn(text, 16); + + var divisor = mr.getDivisor(num); + if (!divisor) + process.exit(1); + if (divisor.cmpn(1) === 0) + process.exit(0); + + console.log(divisor.toString(16)); +} diff --git a/node_modules/miller-rabin/lib/mr.js b/node_modules/miller-rabin/lib/mr.js new file mode 100644 index 000000000..a9e935b1b --- /dev/null +++ b/node_modules/miller-rabin/lib/mr.js @@ -0,0 +1,113 @@ +var bn = require('bn.js'); +var brorand = require('brorand'); + +function MillerRabin(rand) { + this.rand = rand || new brorand.Rand(); +} +module.exports = MillerRabin; + +MillerRabin.create = function create(rand) { + return new MillerRabin(rand); +}; + +MillerRabin.prototype._rand = function _rand(n) { + var len = n.bitLength(); + var buf = this.rand.generate(Math.ceil(len / 8)); + + // Set low bits + buf[0] |= 3; + + // Mask high bits + var mask = len & 0x7; + if (mask !== 0) + buf[buf.length - 1] >>= 7 - mask; + + return new bn(buf); +} + +MillerRabin.prototype.test = function test(n, k, cb) { + var len = n.bitLength(); + var red = bn.mont(n); + var rone = new bn(1).toRed(red); + + 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--) { + var a = this._rand(n2); + if (cb) + cb(a); + + 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; +}; + +MillerRabin.prototype.getDivisor = function getDivisor(n, k) { + var len = n.bitLength(); + var red = bn.mont(n); + var rone = new bn(1).toRed(red); + + 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); + + for (; k > 0; k--) { + var a = this._rand(n2); + + var g = n.gcd(a); + if (g.cmpn(1) !== 0) + return g; + + 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 x.fromRed().subn(1).gcd(n); + if (x.cmp(rn1) === 0) + break; + } + + if (i === s) { + x = x.redSqr(); + return x.fromRed().subn(1).gcd(n); + } + } + + return false; +}; diff --git a/node_modules/miller-rabin/package.json b/node_modules/miller-rabin/package.json new file mode 100644 index 000000000..88a5a3829 --- /dev/null +++ b/node_modules/miller-rabin/package.json @@ -0,0 +1,32 @@ +{ + "name": "miller-rabin", + "version": "4.0.0", + "description": "Miller Rabin algorithm for primality test", + "main": "lib/mr.js", + "bin": "bin/miller-rabin", + "scripts": { + "test": "mocha --reporter=spec test/**/*-test.js" + }, + "repository": { + "type": "git", + "url": "git@github.com:indutny/miller-rabin" + }, + "keywords": [ + "prime", + "miller-rabin", + "bignumber" + ], + "author": "Fedor Indutny <fedor@indutny.com>", + "license": "MIT", + "bugs": { + "url": "https://github.com/indutny/miller-rabin/issues" + }, + "homepage": "https://github.com/indutny/miller-rabin", + "devDependencies": { + "mocha": "^2.0.1" + }, + "dependencies": { + "bn.js": "^4.0.0", + "brorand": "^1.0.1" + } +} diff --git a/node_modules/miller-rabin/test/api-test.js b/node_modules/miller-rabin/test/api-test.js new file mode 100644 index 000000000..dee094d95 --- /dev/null +++ b/node_modules/miller-rabin/test/api-test.js @@ -0,0 +1,18 @@ +var assert = require('assert'); +var mr = require('../').create(); +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)); + }); +}); |