[Pkg-javascript-commits] [node-miller-rabin] 09/37: lib: extended API
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:20:41 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 d4f159ef76d2ebf175ae4bc3f65ed121611f096a
Author: Fedor Indutny <fedor at indutny.com>
Date: Fri Nov 7 17:54:50 2014 +0300
lib: extended API
---
lib/mr.js | 19 ++++++++++++++-----
test/api-test.js | 2 +-
2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/lib/mr.js b/lib/mr.js
index d3f51d2..117943b 100644
--- a/lib/mr.js
+++ b/lib/mr.js
@@ -1,7 +1,16 @@
var bn = require('bn.js');
var brorand = require('brorand');
-function rand(n) {
+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 = brorand(Math.ceil(len / 8));
@@ -16,7 +25,7 @@ function rand(n) {
return new bn(buf);
}
-exports.test = function test(n, k, cb) {
+MillerRabin.prototype.test = function test(n, k, cb) {
var len = n.bitLength();
var red = bn.mont(n);
var rone = new bn(1).toRed(red);
@@ -34,7 +43,7 @@ exports.test = function test(n, k, cb) {
var prime = true;
for (; k > 0; k--) {
- var a = rand(n2);
+ var a = this._rand(n2);
if (cb)
cb(a);
@@ -58,7 +67,7 @@ exports.test = function test(n, k, cb) {
return prime;
};
-exports.getDivisor = function getDivisor(n, k) {
+MillerRabin.prototype.getDivisor = function getDivisor(n, k) {
var len = n.bitLength();
var red = bn.mont(n);
var rone = new bn(1).toRed(red);
@@ -76,7 +85,7 @@ exports.getDivisor = function getDivisor(n, k) {
var prime = true;
for (; k > 0; k--) {
- var a = rand(n2);
+ var a = this._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 de9dba7..dee094d 100644
--- a/test/api-test.js
+++ b/test/api-test.js
@@ -1,5 +1,5 @@
var assert = require('assert');
-var mr = require('../');
+var mr = require('../').create();
var bn = require('bn.js');
describe('Miller-Rabin', function() {
--
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