mr.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/miller-rabin/lib/mr.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}var bn = require('bn.js');
  12. var brorand = require('brorand');
  13. function MillerRabin(rand) {
  14. this.rand = rand || new brorand.Rand();
  15. }
  16. module.exports = MillerRabin;
  17. MillerRabin.create = function create(rand) {
  18. return new MillerRabin(rand);
  19. };
  20. MillerRabin.prototype._randbelow = function _randbelow(n) {
  21. var len = n.bitLength();
  22. var min_bytes = Math.ceil(len / 8);
  23. // Generage random bytes until a number less than n is found.
  24. // This ensures that 0..n-1 have an equal probability of being selected.
  25. do
  26. var a = new bn(this.rand.generate(min_bytes));
  27. while (a.cmp(n) >= 0);
  28. return a;
  29. };
  30. MillerRabin.prototype._randrange = function _randrange(start, stop) {
  31. // Generate a random number greater than or equal to start and less than stop.
  32. var size = stop.sub(start);
  33. return start.add(this._randbelow(size));
  34. };
  35. MillerRabin.prototype.test = function test(n, k, cb) {
  36. var len = n.bitLength();
  37. var red = bn.mont(n);
  38. var rone = new bn(1).toRed(red);
  39. if (!k)
  40. k = Math.max(1, (len / 48) | 0);
  41. // Find d and s, (n - 1) = (2 ^ s) * d;
  42. var n1 = n.subn(1);
  43. for (var s = 0; !n1.testn(s); s++) {}
  44. var d = n.shrn(s);
  45. var rn1 = n1.toRed(red);
  46. var prime = true;
  47. for (; k > 0; k--) {
  48. var a = this._randrange(new bn(2), n1);
  49. if (cb)
  50. cb(a);
  51. var x = a.toRed(red).redPow(d);
  52. if (x.cmp(rone) === 0 || x.cmp(rn1) === 0)
  53. continue;
  54. for (var i = 1; i < s; i++) {
  55. x = x.redSqr();
  56. if (x.cmp(rone) === 0)
  57. return false;
  58. if (x.cmp(rn1) === 0)
  59. break;
  60. }
  61. if (i === s)
  62. return false;
  63. }
  64. return prime;
  65. };
  66. MillerRabin.prototype.getDivisor = function getDivisor(n, k) {
  67. var len = n.bitLength();
  68. var red = bn.mont(n);
  69. var rone = new bn(1).toRed(red);
  70. if (!k)
  71. k = Math.max(1, (len / 48) | 0);
  72. // Find d and s, (n - 1) = (2 ^ s) * d;
  73. var n1 = n.subn(1);
  74. for (var s = 0; !n1.testn(s); s++) {}
  75. var d = n.shrn(s);
  76. var rn1 = n1.toRed(red);
  77. for (; k > 0; k--) {
  78. var a = this._randrange(new bn(2), n1);
  79. var g = n.gcd(a);
  80. if (g.cmpn(1) !== 0)
  81. return g;
  82. var x = a.toRed(red).redPow(d);
  83. if (x.cmp(rone) === 0 || x.cmp(rn1) === 0)
  84. continue;
  85. for (var i = 1; i < s; i++) {
  86. x = x.redSqr();
  87. if (x.cmp(rone) === 0)
  88. return x.fromRed().subn(1).gcd(n);
  89. if (x.cmp(rn1) === 0)
  90. break;
  91. }
  92. if (i === s) {
  93. x = x.redSqr();
  94. return x.fromRed().subn(1).gcd(n);
  95. }
  96. }
  97. return false;
  98. };
  99. }
  100. if (nodeEnv) {
  101. __define(__module.exports, __require, __module);
  102. }
  103. else {
  104. __quick_compile_project__.registerModuleFunc(__filename, function () {
  105. __define(__module.exports, __require, __module);
  106. });
  107. }
  108. })();