This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
+26
View File
@@ -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.
+29
View File
@@ -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));
}
+113
View File
@@ -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;
};
+97
View File
@@ -0,0 +1,97 @@
{
"_args": [
[
{
"raw": "miller-rabin@^4.0.0",
"scope": null,
"escapedName": "miller-rabin",
"name": "miller-rabin",
"rawSpec": "^4.0.0",
"spec": ">=4.0.0 <5.0.0",
"type": "range"
},
"c:\\xampp\\htdocs\\laravel\\node_modules\\diffie-hellman"
]
],
"_from": "miller-rabin@>=4.0.0 <5.0.0",
"_id": "miller-rabin@4.0.0",
"_inCache": true,
"_location": "/miller-rabin",
"_nodeVersion": "4.2.1",
"_npmUser": {
"name": "indutny",
"email": "fedor@indutny.com"
},
"_npmVersion": "2.14.7",
"_phantomChildren": {},
"_requested": {
"raw": "miller-rabin@^4.0.0",
"scope": null,
"escapedName": "miller-rabin",
"name": "miller-rabin",
"rawSpec": "^4.0.0",
"spec": ">=4.0.0 <5.0.0",
"type": "range"
},
"_requiredBy": [
"/diffie-hellman"
],
"_resolved": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.0.tgz",
"_shasum": "4a62fb1d42933c05583982f4c716f6fb9e6c6d3d",
"_shrinkwrap": null,
"_spec": "miller-rabin@^4.0.0",
"_where": "c:\\xampp\\htdocs\\laravel\\node_modules\\diffie-hellman",
"author": {
"name": "Fedor Indutny",
"email": "fedor@indutny.com"
},
"bin": {
"miller-rabin": "bin/miller-rabin"
},
"bugs": {
"url": "https://github.com/indutny/miller-rabin/issues"
},
"dependencies": {
"bn.js": "^4.0.0",
"brorand": "^1.0.1"
},
"description": "Miller Rabin algorithm for primality test",
"devDependencies": {
"mocha": "^2.0.1"
},
"directories": {},
"dist": {
"shasum": "4a62fb1d42933c05583982f4c716f6fb9e6c6d3d",
"tarball": "https://registry.npmjs.org/miller-rabin/-/miller-rabin-4.0.0.tgz"
},
"gitHead": "94b56df9948a1ae592b331afc9559115f3f98fb2",
"homepage": "https://github.com/indutny/miller-rabin",
"keywords": [
"prime",
"miller-rabin",
"bignumber"
],
"license": "MIT",
"main": "lib/mr.js",
"maintainers": [
{
"name": "indutny",
"email": "fedor@indutny.com"
},
{
"name": "cwmma",
"email": "calvin.metcalf@gmail.com"
}
],
"name": "miller-rabin",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/indutny/miller-rabin.git"
},
"scripts": {
"test": "mocha --reporter=spec test/**/*-test.js"
},
"version": "4.0.0"
}
+18
View File
@@ -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));
});
});