pax_global_header00006660000000000000000000000064131630206070014510gustar00rootroot0000000000000052 comment=bda17a34d3b33a84a0dbb120ff7f662adea05c01 miller-rabin-4.0.1/000077500000000000000000000000001316302060700140675ustar00rootroot00000000000000miller-rabin-4.0.1/.gitignore000066400000000000000000000000341316302060700160540ustar00rootroot00000000000000node_modules/ npm-debug.log miller-rabin-4.0.1/README.md000066400000000000000000000021571316302060700153530ustar00rootroot00000000000000# 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. miller-rabin-4.0.1/bin/000077500000000000000000000000001316302060700146375ustar00rootroot00000000000000miller-rabin-4.0.1/bin/miller-rabin000077500000000000000000000011271316302060700171430ustar00rootroot00000000000000#!/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)); } miller-rabin-4.0.1/lib/000077500000000000000000000000001316302060700146355ustar00rootroot00000000000000miller-rabin-4.0.1/lib/mr.js000066400000000000000000000046721316302060700156220ustar00rootroot00000000000000var 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._randbelow = function _randbelow(n) { var len = n.bitLength(); var min_bytes = Math.ceil(len / 8); // Generage random bytes until a number less than n is found. // This ensures that 0..n-1 have an equal probability of being selected. do var a = new bn(this.rand.generate(min_bytes)); while (a.cmp(n) >= 0); return a; }; MillerRabin.prototype._randrange = function _randrange(start, stop) { // Generate a random number greater than or equal to start and less than stop. var size = stop.sub(start); return start.add(this._randbelow(size)); }; 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); 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._randrange(new bn(2), n1); 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); 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._randrange(new bn(2), n1); 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; }; miller-rabin-4.0.1/package.json000066400000000000000000000013251316302060700163560ustar00rootroot00000000000000{ "name": "miller-rabin", "version": "4.0.1", "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 ", "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" } } miller-rabin-4.0.1/test/000077500000000000000000000000001316302060700150465ustar00rootroot00000000000000miller-rabin-4.0.1/test/api-test.js000066400000000000000000000012661316302060700171370ustar00rootroot00000000000000var 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)); }); });