more prime validation tests
This commit is contained in:
parent
15cd113ac9
commit
12a78d9495
|
@ -98,7 +98,7 @@ var bigintCryptoUtils = (function (exports) {
|
|||
const isProbablyPrime = async function (w, iterations = 16) {
|
||||
{
|
||||
return new Promise(resolve => {
|
||||
let worker = _isProbablyPrimeWorker();
|
||||
let worker = new Worker(_isProbablyPrimeWorkerURL());
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
worker.terminate();
|
||||
|
@ -188,8 +188,9 @@ var bigintCryptoUtils = (function (exports) {
|
|||
return new Promise(async (resolve) => {
|
||||
{
|
||||
let workerList = [];
|
||||
let workerURL = _isProbablyPrimeWorkerURL();
|
||||
for (let i = 0; i < self.navigator.hardwareConcurrency; i++) {
|
||||
let newWorker = _isProbablyPrimeWorker();
|
||||
let newWorker = new Worker(workerURL);
|
||||
newWorker.onmessage = async (event) => {
|
||||
if (event.data.isPrime) {
|
||||
// if a prime number has been found, stop all the workers, and return it
|
||||
|
@ -310,7 +311,7 @@ var bigintCryptoUtils = (function (exports) {
|
|||
return bits;
|
||||
}
|
||||
|
||||
function _isProbablyPrimeWorker() {
|
||||
function _isProbablyPrimeWorkerURL() {
|
||||
async function _onmessage(event) { // Let's start once we are called
|
||||
// event.data = {rnd: <bigint>, iterations: <number>}
|
||||
const isPrime = await isProbablyPrime(event.data.rnd, event.data.iterations);
|
||||
|
@ -324,7 +325,7 @@ var bigintCryptoUtils = (function (exports) {
|
|||
|
||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||
|
||||
return new Worker(window.URL.createObjectURL(_blob));
|
||||
return window.URL.createObjectURL(_blob);
|
||||
}
|
||||
|
||||
async function _isProbablyPrime(w, iterations = 16) {
|
||||
|
|
|
@ -1 +1 @@
|
|||
var bigintCryptoUtils=function(a){'use strict';function b(a){let b=BigInt(0);for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function c(b){let c=1;do c++;while((b>>=BigInt(1))>BigInt(1));return c}function d(){let a=`(() => {'use strict';const eGcd = ${g.toString()};const modInv = ${j.toString()};const modPow = ${k.toString()};const toZn = ${o.toString()};const randBytes = ${m.toString()};const randBetween = ${l.toString()};const isProbablyPrime = ${e.toString()};${c.toString()}${b.toString()}onmessage = ${async function(a){const b=await i(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd})}.toString()};})()`;var d=new Blob([a],{type:"text/javascript"});return new Worker(window.URL.createObjectURL(d))}async function e(c,b=16){if(c===BigInt(2))return!0;if((c&BigInt(1))===BigInt(0)||c===BigInt(1))return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===BigInt(0))return!1}let f=BigInt(0),g=c-BigInt(1);for(;g%BigInt(2)===BigInt(0);)g/=BigInt(2),++f;let h=(c-BigInt(1))/BigInt(2)**f;loop:do{let a=await l(c-BigInt(1),2),b=k(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=k(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0}const f=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},g=function(c,d){c=BigInt(c),d=BigInt(d);let e=BigInt(0),f=BigInt(1),g=BigInt(1),h=BigInt(0);for(;c!==BigInt(0);){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}},h=function(c,d){c=f(c),d=f(d);let e=BigInt(0);for(;!((c|d)&BigInt(1));)c>>=BigInt(1),d>>=BigInt(1),e++;for(;!(c&BigInt(1));)c>>=BigInt(1);do{for(;!(d&BigInt(1));)d>>=BigInt(1);if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e},i=async function(a,b=16){return new Promise(c=>{let e=d();e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.postMessage({rnd:a,iterations:b})})},j=function(b,a){let c=g(b,a);return c.b===BigInt(1)?o(c.x,a):null},k=function(c,d,e){if(e=BigInt(e),c=o(c,e),d=BigInt(d),d<BigInt(0))return j(k(c,f(d),e),e);let g=BigInt(1),h=c;for(;0<d;){var i=d%BigInt(2);d/=BigInt(2),i==BigInt(1)&&(g*=h,g%=e),h*=h,h%=e}return g},l=async function(a,d=1){let e,f=c(a),g=f>>3,h=f-8*g;0<h&&(g++,e=2**h-1);let i;do{let a=await m(g);0<h&&(a[0]&=e),i=b(a)}while(i>a||i<d);return i},m=async function(a,b=!1){return new Promise(c=>{let d;d=new Uint8Array(a),self.crypto.getRandomValues(d),b&&(d[0]|=128),c(d)})},o=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b};return a.abs=f,a.eGcd=g,a.gcd=h,a.isProbablyPrime=i,a.lcm=function(c,d){return c=BigInt(c),d=BigInt(d),f(c*d)/h(c,d)},a.modInv=j,a.modPow=k,a.prime=async function(a,c=16){return new Promise(async e=>{{let f=[];for(let g,h=0;h<self.navigator.hardwareConcurrency;h++)g=d(),g.onmessage=async d=>{if(d.data.isPrime){for(let a=0;a<f.length;a++)f[a].terminate();for(;f.length;)f.pop();e(d.data.value)}else{let d=BigInt(0);d=b((await m(a/8,!0))),g.postMessage({rnd:d,iterations:c})}},f.push(g);for(const d of f){let e=BigInt(0);e=b((await m(a/8,!0))),d.postMessage({rnd:e,iterations:c})}}})},a.randBetween=l,a.randBytes=m,a.toZn=o,a}({});
|
||||
var bigintCryptoUtils=function(a){'use strict';function b(a){let b=BigInt(0);for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function c(b){let c=1;do c++;while((b>>=BigInt(1))>BigInt(1));return c}function d(){let a=`(() => {'use strict';const eGcd = ${g.toString()};const modInv = ${j.toString()};const modPow = ${k.toString()};const toZn = ${o.toString()};const randBytes = ${m.toString()};const randBetween = ${l.toString()};const isProbablyPrime = ${e.toString()};${c.toString()}${b.toString()}onmessage = ${async function(a){const b=await i(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd})}.toString()};})()`;var d=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(d)}async function e(c,b=16){if(c===BigInt(2))return!0;if((c&BigInt(1))===BigInt(0)||c===BigInt(1))return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===BigInt(0))return!1}let f=BigInt(0),g=c-BigInt(1);for(;g%BigInt(2)===BigInt(0);)g/=BigInt(2),++f;let h=(c-BigInt(1))/BigInt(2)**f;loop:do{let a=await l(c-BigInt(1),2),b=k(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=k(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0}const f=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},g=function(c,d){c=BigInt(c),d=BigInt(d);let e=BigInt(0),f=BigInt(1),g=BigInt(1),h=BigInt(0);for(;c!==BigInt(0);){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}},h=function(c,d){c=f(c),d=f(d);let e=BigInt(0);for(;!((c|d)&BigInt(1));)c>>=BigInt(1),d>>=BigInt(1),e++;for(;!(c&BigInt(1));)c>>=BigInt(1);do{for(;!(d&BigInt(1));)d>>=BigInt(1);if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e},i=async function(a,b=16){return new Promise(c=>{let e=new Worker(d());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.postMessage({rnd:a,iterations:b})})},j=function(b,a){let c=g(b,a);return c.b===BigInt(1)?o(c.x,a):null},k=function(c,d,e){if(e=BigInt(e),c=o(c,e),d=BigInt(d),d<BigInt(0))return j(k(c,f(d),e),e);let g=BigInt(1),h=c;for(;0<d;){var i=d%BigInt(2);d/=BigInt(2),i==BigInt(1)&&(g*=h,g%=e),h*=h,h%=e}return g},l=async function(a,d=1){let e,f=c(a),g=f>>3,h=f-8*g;0<h&&(g++,e=2**h-1);let i;do{let a=await m(g);0<h&&(a[0]&=e),i=b(a)}while(i>a||i<d);return i},m=async function(a,b=!1){return new Promise(c=>{let d;d=new Uint8Array(a),self.crypto.getRandomValues(d),b&&(d[0]|=128),c(d)})},o=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b};return a.abs=f,a.eGcd=g,a.gcd=h,a.isProbablyPrime=i,a.lcm=function(c,d){return c=BigInt(c),d=BigInt(d),f(c*d)/h(c,d)},a.modInv=j,a.modPow=k,a.prime=async function(a,c=16){return new Promise(async e=>{{let f=[],g=d();for(let d,h=0;h<self.navigator.hardwareConcurrency;h++)d=new Worker(g),d.onmessage=async g=>{if(g.data.isPrime){for(let a=0;a<f.length;a++)f[a].terminate();for(;f.length;)f.pop();e(g.data.value)}else{let e=BigInt(0);e=b((await m(a/8,!0))),d.postMessage({rnd:e,iterations:c})}},f.push(d);for(const d of f){let e=BigInt(0);e=b((await m(a/8,!0))),d.postMessage({rnd:e,iterations:c})}}})},a.randBetween=l,a.randBytes=m,a.toZn=o,a}({});
|
||||
|
|
|
@ -95,7 +95,7 @@ const gcd = function (a, b) {
|
|||
const isProbablyPrime = async function (w, iterations = 16) {
|
||||
{
|
||||
return new Promise(resolve => {
|
||||
let worker = _isProbablyPrimeWorker();
|
||||
let worker = new Worker(_isProbablyPrimeWorkerURL());
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
worker.terminate();
|
||||
|
@ -185,8 +185,9 @@ const prime = async function (bitLength, iterations = 16) {
|
|||
return new Promise(async (resolve) => {
|
||||
{
|
||||
let workerList = [];
|
||||
let workerURL = _isProbablyPrimeWorkerURL();
|
||||
for (let i = 0; i < self.navigator.hardwareConcurrency; i++) {
|
||||
let newWorker = _isProbablyPrimeWorker();
|
||||
let newWorker = new Worker(workerURL);
|
||||
newWorker.onmessage = async (event) => {
|
||||
if (event.data.isPrime) {
|
||||
// if a prime number has been found, stop all the workers, and return it
|
||||
|
@ -307,7 +308,7 @@ function bitLength(a) {
|
|||
return bits;
|
||||
}
|
||||
|
||||
function _isProbablyPrimeWorker() {
|
||||
function _isProbablyPrimeWorkerURL() {
|
||||
async function _onmessage(event) { // Let's start once we are called
|
||||
// event.data = {rnd: <bigint>, iterations: <number>}
|
||||
const isPrime = await isProbablyPrime(event.data.rnd, event.data.iterations);
|
||||
|
@ -321,7 +322,7 @@ function _isProbablyPrimeWorker() {
|
|||
|
||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||
|
||||
return new Worker(window.URL.createObjectURL(_blob));
|
||||
return window.URL.createObjectURL(_blob);
|
||||
}
|
||||
|
||||
async function _isProbablyPrime(w, iterations = 16) {
|
||||
|
|
|
@ -1 +1 @@
|
|||
const abs=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},eGcd=function(c,d){c=BigInt(c),d=BigInt(d);let e=BigInt(0),f=BigInt(1),g=BigInt(1),h=BigInt(0);for(;c!==BigInt(0);){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}},gcd=function(c,d){c=abs(c),d=abs(d);let e=BigInt(0);for(;!((c|d)&BigInt(1));)c>>=BigInt(1),d>>=BigInt(1),e++;for(;!(c&BigInt(1));)c>>=BigInt(1);do{for(;!(d&BigInt(1));)d>>=BigInt(1);if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e},isProbablyPrime=async function(a,b=16){return new Promise(c=>{let d=_isProbablyPrimeWorker();d.onmessage=a=>{d.terminate(),c(a.data.isPrime)},d.postMessage({rnd:a,iterations:b})})},lcm=function(c,d){return c=BigInt(c),d=BigInt(d),abs(c*d)/gcd(c,d)},modInv=function(b,a){let c=eGcd(b,a);return c.b===BigInt(1)?toZn(c.x,a):null},modPow=function(c,d,e){if(e=BigInt(e),c=toZn(c,e),d=BigInt(d),d<BigInt(0))return modInv(modPow(c,abs(d),e),e);let f=BigInt(1),g=c;for(;0<d;){var h=d%BigInt(2);d/=BigInt(2),h==BigInt(1)&&(f*=g,f%=e),g*=g,g%=e}return f},prime=async function(a,b=16){return new Promise(async c=>{{let d=[];for(let e,f=0;f<self.navigator.hardwareConcurrency;f++)e=_isProbablyPrimeWorker(),e.onmessage=async f=>{if(f.data.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(f.data.value)}else{let c=BigInt(0);c=fromBuffer((await randBytes(a/8,!0))),e.postMessage({rnd:c,iterations:b})}},d.push(e);for(const c of d){let d=BigInt(0);d=fromBuffer((await randBytes(a/8,!0))),c.postMessage({rnd:d,iterations:b})}}})},randBetween=async function(a,b=1){let c,d=bitLength(a),e=d>>3,f=d-8*e;0<f&&(e++,c=2**f-1);let g;do{let a=await randBytes(e);0<f&&(a[0]&=c),g=fromBuffer(a)}while(g>a||g<b);return g},randBytes=async function(a,b=!1){return new Promise(c=>{let d;d=new Uint8Array(a),self.crypto.getRandomValues(d),b&&(d[0]|=128),c(d)})},toZn=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b};function fromBuffer(a){let b=BigInt(0);for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function bitLength(b){let c=1;do c++;while((b>>=BigInt(1))>BigInt(1));return c}function _isProbablyPrimeWorker(){let a=`(() => {'use strict';const eGcd = ${eGcd.toString()};const modInv = ${modInv.toString()};const modPow = ${modPow.toString()};const toZn = ${toZn.toString()};const randBytes = ${randBytes.toString()};const randBetween = ${randBetween.toString()};const isProbablyPrime = ${_isProbablyPrime.toString()};${bitLength.toString()}${fromBuffer.toString()}onmessage = ${async function(a){const b=await isProbablyPrime(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd})}.toString()};})()`;var b=new Blob([a],{type:"text/javascript"});return new Worker(window.URL.createObjectURL(b))}async function _isProbablyPrime(c,b=16){if(c===BigInt(2))return!0;if((c&BigInt(1))===BigInt(0)||c===BigInt(1))return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===BigInt(0))return!1}let f=BigInt(0),g=c-BigInt(1);for(;g%BigInt(2)===BigInt(0);)g/=BigInt(2),++f;let h=(c-BigInt(1))/BigInt(2)**f;loop:do{let a=await randBetween(c-BigInt(1),2),b=modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0}export{abs,eGcd,gcd,isProbablyPrime,lcm,modInv,modPow,prime,randBetween,randBytes,toZn};
|
||||
const abs=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},eGcd=function(c,d){c=BigInt(c),d=BigInt(d);let e=BigInt(0),f=BigInt(1),g=BigInt(1),h=BigInt(0);for(;c!==BigInt(0);){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}},gcd=function(c,d){c=abs(c),d=abs(d);let e=BigInt(0);for(;!((c|d)&BigInt(1));)c>>=BigInt(1),d>>=BigInt(1),e++;for(;!(c&BigInt(1));)c>>=BigInt(1);do{for(;!(d&BigInt(1));)d>>=BigInt(1);if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e},isProbablyPrime=async function(a,b=16){return new Promise(c=>{let d=new Worker(_isProbablyPrimeWorkerURL());d.onmessage=a=>{d.terminate(),c(a.data.isPrime)},d.postMessage({rnd:a,iterations:b})})},lcm=function(c,d){return c=BigInt(c),d=BigInt(d),abs(c*d)/gcd(c,d)},modInv=function(b,a){let c=eGcd(b,a);return c.b===BigInt(1)?toZn(c.x,a):null},modPow=function(c,d,e){if(e=BigInt(e),c=toZn(c,e),d=BigInt(d),d<BigInt(0))return modInv(modPow(c,abs(d),e),e);let f=BigInt(1),g=c;for(;0<d;){var h=d%BigInt(2);d/=BigInt(2),h==BigInt(1)&&(f*=g,f%=e),g*=g,g%=e}return f},prime=async function(a,b=16){return new Promise(async c=>{{let d=[],e=_isProbablyPrimeWorkerURL();for(let f,g=0;g<self.navigator.hardwareConcurrency;g++)f=new Worker(e),f.onmessage=async e=>{if(e.data.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.data.value)}else{let c=BigInt(0);c=fromBuffer((await randBytes(a/8,!0))),f.postMessage({rnd:c,iterations:b})}},d.push(f);for(const c of d){let d=BigInt(0);d=fromBuffer((await randBytes(a/8,!0))),c.postMessage({rnd:d,iterations:b})}}})},randBetween=async function(a,b=1){let c,d=bitLength(a),e=d>>3,f=d-8*e;0<f&&(e++,c=2**f-1);let g;do{let a=await randBytes(e);0<f&&(a[0]&=c),g=fromBuffer(a)}while(g>a||g<b);return g},randBytes=async function(a,b=!1){return new Promise(c=>{let d;d=new Uint8Array(a),self.crypto.getRandomValues(d),b&&(d[0]|=128),c(d)})},toZn=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b};function fromBuffer(a){let b=BigInt(0);for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function bitLength(b){let c=1;do c++;while((b>>=BigInt(1))>BigInt(1));return c}function _isProbablyPrimeWorkerURL(){let a=`(() => {'use strict';const eGcd = ${eGcd.toString()};const modInv = ${modInv.toString()};const modPow = ${modPow.toString()};const toZn = ${toZn.toString()};const randBytes = ${randBytes.toString()};const randBetween = ${randBetween.toString()};const isProbablyPrime = ${_isProbablyPrime.toString()};${bitLength.toString()}${fromBuffer.toString()}onmessage = ${async function(a){const b=await isProbablyPrime(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd})}.toString()};})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}async function _isProbablyPrime(c,b=16){if(c===BigInt(2))return!0;if((c&BigInt(1))===BigInt(0)||c===BigInt(1))return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===BigInt(0))return!1}let f=BigInt(0),g=c-BigInt(1);for(;g%BigInt(2)===BigInt(0);)g/=BigInt(2),++f;let h=(c-BigInt(1))/BigInt(2)**f;loop:do{let a=await randBetween(c-BigInt(1),2),b=modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0}export{abs,eGcd,gcd,isProbablyPrime,lcm,modInv,modPow,prime,randBetween,randBytes,toZn};
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<div id="mocha"></div>
|
||||
<script src="../../node_modules/mocha/mocha.js"></script>
|
||||
<script src="../../node_modules/chai/chai.js"></script>
|
||||
<script>mocha.setup('bdd'); mocha.setup({ timeout: 60000 });</script>
|
||||
<script>mocha.setup('bdd'); mocha.setup({ timeout: 90000 });</script>
|
||||
|
||||
{{TESTS}}
|
||||
</body>
|
||||
|
|
|
@ -97,7 +97,7 @@ export const gcd = function (a, b) {
|
|||
export const isProbablyPrime = async function (w, iterations = 16) {
|
||||
if (process.browser) {
|
||||
return new Promise(resolve => {
|
||||
let worker = _isProbablyPrimeWorker();
|
||||
let worker = new Worker(_isProbablyPrimeWorkerURL());
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
worker.terminate();
|
||||
|
@ -189,8 +189,9 @@ export const prime = async function (bitLength, iterations = 16) {
|
|||
return new Promise(async (resolve) => {
|
||||
if (process.browser) {
|
||||
let workerList = [];
|
||||
let workerURL = _isProbablyPrimeWorkerURL();
|
||||
for (let i = 0; i < self.navigator.hardwareConcurrency; i++) {
|
||||
let newWorker = _isProbablyPrimeWorker();
|
||||
let newWorker = new Worker(workerURL);
|
||||
newWorker.onmessage = async (event) => {
|
||||
if (event.data.isPrime) {
|
||||
// if a prime number has been found, stop all the workers, and return it
|
||||
|
@ -327,7 +328,7 @@ function bitLength(a) {
|
|||
return bits;
|
||||
}
|
||||
|
||||
function _isProbablyPrimeWorker() {
|
||||
function _isProbablyPrimeWorkerURL() {
|
||||
async function _onmessage(event) { // Let's start once we are called
|
||||
// event.data = {rnd: <bigint>, iterations: <number>}
|
||||
const isPrime = await isProbablyPrime(event.data.rnd, event.data.iterations);
|
||||
|
@ -341,7 +342,7 @@ function _isProbablyPrimeWorker() {
|
|||
|
||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||
|
||||
return new Worker(window.URL.createObjectURL(_blob));
|
||||
return window.URL.createObjectURL(_blob);
|
||||
}
|
||||
|
||||
async function _isProbablyPrime(w, iterations = 16) {
|
||||
|
|
|
@ -42,9 +42,17 @@ const numbers = [
|
|||
value: BigInt('115922179551495973383410176342643722334557255682879605864838806293659619625004303206250384392546855063844106965156287951749387634112551089284595541103692716528774876311641700929986988023197242224581099872580798960693521778607396791006450968430359009613295725905514216842343121690916290236558767890728449777'),
|
||||
prime: true
|
||||
},
|
||||
{
|
||||
value: BigInt('168694196579467171180863939518634764192343817610869919231900537093664715354591592262546800497540343203057121816378265655992490621138321114570420047522219942818258345349322155251835677199539229050711145144861404607171419723967136221126986330819362088262358855325306938646602003059377699727688477555163239222109') * BigInt('144678545212641449725111562354371812236197961234111744040227045242578772124779004756249085154188369039159690638725821245974978963371615699005072473649705367893567309027634121825164880046600125480885803891136149601797439273507802533807541605261215613891134865916295914192271736572001975016089773532547481638243'),
|
||||
prime: false
|
||||
},
|
||||
{
|
||||
value: BigInt('918145944120889203205646923554374144932845997937845799234617798611046542304088105084854788397071323714642587188481158334265864050544813693415594035822877094557870151480865568334936301231664228940480803192289508235412296324312748621874408067955753620604885023289655277704554716080844406284392300643321715285709865081125252390440327650852470312931679380011885102491340191287595160450544053114365852338670819405357496612993587404998677760882578064637552397840566752638770525765833183986360029736508910848408875329873614164495552615086579144675027852136994842529623698055210822311666048300438808691619782893307972452223713060928388502843564836966586109748062827799521852219158489504529458627699284110902303538160168376473182639384638674469114371472053977558648090155686345760457454061117853710619580819749222459422610617170567016772342291486643520567969321969827786373531753524990712622940069883277763528926899970596407140603912036918433859986491820017690762751824769335720368488097262208835708414085501930989486498185503469986946236128468697606998536541209764920494156326791142098506801288127033229779646920082892258428128572765585196779698362187479280520327053508580551167899837393726371144977951402741307021389967382422805567365901203'),
|
||||
prime: true
|
||||
},
|
||||
{
|
||||
value: BigInt('940719693126280825126763871881743336375040232953039527942717290104060740215493004508206768342926022549956464101136893240409560470269654765366248516968645294076406953865805712688760371102637642013723011744011617678651884521901163090779813242269935310225049805992299292275574585773507915278612311449919050091057023179541184986547995894821648553256021675133997240195429424258757033557367142630663053464438840832073753440939208165158795269598771598124509831433327480118038278887538430675994497384283550890544882369140852048496460551123626856255619494025370171790720106325655890348475483349150258338517508459674722099347335608814922179633411167540545786247819334838979610017735984374883325689517847175539632896026875016305529321705457954181425405794479825617747354596485074451489940385640535898876551301296003465792117006135339109817937663957519031436646579178503423889430062127572272773511424424297800355292430651838502733756881154935252456036638082486459287411002911323257940893413982671660332662880068976408321968046549017547143836993553556640198884769590214676797037397502067035957959952990027503148987727895561468097917730167320715053689862847457761993196945361244822787209076446259359976421264285658106819879849052247546957718175231'),
|
||||
prime: false
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<div id="mocha"></div>
|
||||
<script src="../../node_modules/mocha/mocha.js"></script>
|
||||
<script src="../../node_modules/chai/chai.js"></script>
|
||||
<script>mocha.setup('bdd'); mocha.setup({ timeout: 60000 });</script>
|
||||
<script>mocha.setup('bdd'); mocha.setup({ timeout: 90000 });</script>
|
||||
|
||||
|
||||
<script type="module">
|
||||
|
|
|
@ -40,9 +40,17 @@ const numbers = [
|
|||
value: BigInt('115922179551495973383410176342643722334557255682879605864838806293659619625004303206250384392546855063844106965156287951749387634112551089284595541103692716528774876311641700929986988023197242224581099872580798960693521778607396791006450968430359009613295725905514216842343121690916290236558767890728449777'),
|
||||
prime: true
|
||||
},
|
||||
{
|
||||
value: BigInt('168694196579467171180863939518634764192343817610869919231900537093664715354591592262546800497540343203057121816378265655992490621138321114570420047522219942818258345349322155251835677199539229050711145144861404607171419723967136221126986330819362088262358855325306938646602003059377699727688477555163239222109') * BigInt('144678545212641449725111562354371812236197961234111744040227045242578772124779004756249085154188369039159690638725821245974978963371615699005072473649705367893567309027634121825164880046600125480885803891136149601797439273507802533807541605261215613891134865916295914192271736572001975016089773532547481638243'),
|
||||
prime: false
|
||||
},
|
||||
{
|
||||
value: BigInt('918145944120889203205646923554374144932845997937845799234617798611046542304088105084854788397071323714642587188481158334265864050544813693415594035822877094557870151480865568334936301231664228940480803192289508235412296324312748621874408067955753620604885023289655277704554716080844406284392300643321715285709865081125252390440327650852470312931679380011885102491340191287595160450544053114365852338670819405357496612993587404998677760882578064637552397840566752638770525765833183986360029736508910848408875329873614164495552615086579144675027852136994842529623698055210822311666048300438808691619782893307972452223713060928388502843564836966586109748062827799521852219158489504529458627699284110902303538160168376473182639384638674469114371472053977558648090155686345760457454061117853710619580819749222459422610617170567016772342291486643520567969321969827786373531753524990712622940069883277763528926899970596407140603912036918433859986491820017690762751824769335720368488097262208835708414085501930989486498185503469986946236128468697606998536541209764920494156326791142098506801288127033229779646920082892258428128572765585196779698362187479280520327053508580551167899837393726371144977951402741307021389967382422805567365901203'),
|
||||
prime: true
|
||||
},
|
||||
{
|
||||
value: BigInt('940719693126280825126763871881743336375040232953039527942717290104060740215493004508206768342926022549956464101136893240409560470269654765366248516968645294076406953865805712688760371102637642013723011744011617678651884521901163090779813242269935310225049805992299292275574585773507915278612311449919050091057023179541184986547995894821648553256021675133997240195429424258757033557367142630663053464438840832073753440939208165158795269598771598124509831433327480118038278887538430675994497384283550890544882369140852048496460551123626856255619494025370171790720106325655890348475483349150258338517508459674722099347335608814922179633411167540545786247819334838979610017735984374883325689517847175539632896026875016305529321705457954181425405794479825617747354596485074451489940385640535898876551301296003465792117006135339109817937663957519031436646579178503423889430062127572272773511424424297800355292430651838502733756881154935252456036638082486459287411002911323257940893413982671660332662880068976408321968046549017547143836993553556640198884769590214676797037397502067035957959952990027503148987727895561468097917730167320715053689862847457761993196945361244822787209076446259359976421264285658106819879849052247546957718175231'),
|
||||
prime: false
|
||||
}
|
||||
];
|
||||
|
||||
|
|
Loading…
Reference in New Issue