[Pkg-javascript-commits] [node-miller-rabin] 04/37: mr: .getDivisor(), add failing test

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 c499b8f98332245c2f82ed064932f8f7ebca6e43
Author: Fedor Indutny <fedor at indutny.com>
Date:   Thu Nov 6 11:37:55 2014 +0100

    mr: .getDivisor(), add failing test
---
 lib/mr.js        | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/api-test.js | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/lib/mr.js b/lib/mr.js
index 4ff6c6f..88faec7 100644
--- a/lib/mr.js
+++ b/lib/mr.js
@@ -46,3 +46,51 @@ exports.test = function test(n, k) {
 
   return prime;
 };
+
+exports.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);
+
+  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 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 prime;
+};
diff --git a/test/api-test.js b/test/api-test.js
index 476a9ef..d835c2d 100644
--- a/test/api-test.js
+++ b/test/api-test.js
@@ -16,4 +16,40 @@ describe('Miller-Rabin', function() {
     assert(mr.test(p));
     assert(!mr.test(p.subn(1)));
   });
+
+  it('should test big number for primality', function() {
+    var p = new bn('FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
+                   '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
+                   'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
+                   'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
+                   'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' +
+                   'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' +
+                   '83655D23DCA3AD961C62F356208552BB9ED529077096966D' +
+                   '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' +
+                   'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' +
+                   'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' +
+                   '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' +
+                   'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' +
+                   'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' +
+                   'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' +
+                   'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' +
+                   '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' +
+                   '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' +
+                   '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' +
+                   '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' +
+                   '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' +
+                   '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' +
+                   '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' +
+                   'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' +
+                   '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' +
+                   'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' +
+                   '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' +
+                   'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' +
+                   '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' +
+                   'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' +
+                   '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' +
+                   'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' +
+                   '12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF', 16);
+    assert(mr.test(p));
+  });
 });

-- 
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