[Pkg-javascript-commits] [node-miller-rabin] 05/37: mr: faster random number generation

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 6cd58b7c1abbcb05abca1b921895ce033d0e7002
Author: Fedor Indutny <fedor at indutny.com>
Date:   Fri Nov 7 12:49:25 2014 +0300

    mr: faster random number generation
---
 lib/mr.js        | 27 ++++++++++++++++++++-------
 test/api-test.js | 36 ------------------------------------
 2 files changed, 20 insertions(+), 43 deletions(-)

diff --git a/lib/mr.js b/lib/mr.js
index 88faec7..67db2b9 100644
--- a/lib/mr.js
+++ b/lib/mr.js
@@ -1,7 +1,22 @@
 var bn = require('bn.js');
 var brorand = require('brorand');
 
-exports.test = function test(n, k) {
+function rand(n) {
+  var len = n.bitLength();
+  var buf = brorand(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);
+}
+
+exports.test = function test(n, k, cb) {
   var len = n.bitLength();
   var red = bn.mont(n);
   var rone = new bn(1).toRed(red);
@@ -23,9 +38,9 @@ exports.test = function test(n, k) {
 
   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 a = rand(n2);
+    if (cb)
+      cb(a);
 
     var x = a.toRed(red).redPow(d);
     if (x.cmp(rone) === 0 || x.cmp(rn1) === 0)
@@ -65,9 +80,7 @@ exports.getDivisor = function getDivisor(n, k) {
 
   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 a = rand(n2);
 
     var g = n.gcd(a);
     if (g.cmpn(1) !== 0)
diff --git a/test/api-test.js b/test/api-test.js
index d835c2d..476a9ef 100644
--- a/test/api-test.js
+++ b/test/api-test.js
@@ -16,40 +16,4 @@ 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