bigint-secrets
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -0,1 +1,5 @@ | ||
const _ZERO = BigInt(0); | ||
const _ONE = BigInt(1); | ||
const _TWO = BigInt(2); | ||
/** | ||
@@ -8,8 +12,50 @@ * Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 | ||
*/ | ||
const abs = function (a) { | ||
function abs(a) { | ||
a = BigInt(a); | ||
return (a >= BigInt(0)) ? a : -a; | ||
}; | ||
return (a >= _ZERO) ? a : -a; | ||
} | ||
/** | ||
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* @property {bigint} g | ||
* @property {bigint} x | ||
* @property {bigint} y | ||
*/ | ||
/** | ||
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. | ||
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* | ||
* @param {number|bigint} a | ||
* @param {number|bigint} b | ||
* | ||
* @returns {egcdReturn} | ||
*/ | ||
function eGcd(a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
let x = _ZERO; | ||
let y = _ONE; | ||
let u = _ONE; | ||
let v = _ZERO; | ||
while (a !== _ZERO) { | ||
let q = b / a; | ||
let r = b % a; | ||
let m = x - (u * q); | ||
let n = y - (v * q); | ||
b = a; | ||
a = r; | ||
x = u; | ||
y = v; | ||
u = m; | ||
v = n; | ||
} | ||
return { | ||
b: b, | ||
x: x, | ||
y: y | ||
}; | ||
} | ||
/** | ||
* Greatest-common divisor of two integers based on the iterative binary algorithm. | ||
@@ -22,14 +68,14 @@ * | ||
*/ | ||
const gcd = function (a, b) { | ||
function gcd(a, b) { | ||
a = abs(a); | ||
b = abs(b); | ||
let shift = BigInt(0); | ||
while (!((a | b) & BigInt(1))) { | ||
a >>= BigInt(1); | ||
b >>= BigInt(1); | ||
let shift = _ZERO; | ||
while (!((a | b) & _ONE)) { | ||
a >>= _ONE; | ||
b >>= _ONE; | ||
shift++; | ||
} | ||
while (!(a & BigInt(1))) a >>= BigInt(1); | ||
while (!(a & _ONE)) a >>= _ONE; | ||
do { | ||
while (!(b & BigInt(1))) b >>= BigInt(1); | ||
while (!(b & _ONE)) b >>= _ONE; | ||
if (a > b) { | ||
@@ -45,3 +91,3 @@ let x = a; | ||
return a << shift; | ||
}; | ||
} | ||
@@ -55,64 +101,9 @@ /** | ||
*/ | ||
const lcm = function (a, b) { | ||
function lcm(a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
return abs(a * b) / gcd(a, b); | ||
}; | ||
} | ||
/** | ||
* Finds the smallest positive element that is congruent to a in modulo n | ||
* @param {number|bigint} a An integer | ||
* @param {number|bigint} n The modulo | ||
* | ||
* @returns {bigint} The smallest positive representation of a in modulo n | ||
*/ | ||
const toZn = function (a, n) { | ||
n = BigInt(n); | ||
a = BigInt(a) % n; | ||
return (a < 0) ? a + n : a; | ||
}; | ||
/** | ||
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* @property {bigint} g | ||
* @property {bigint} x | ||
* @property {bigint} y | ||
*/ | ||
/** | ||
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. | ||
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* | ||
* @param {number|bigint} a | ||
* @param {number|bigint} b | ||
* | ||
* @returns {egcdReturn} | ||
*/ | ||
const eGcd = function (a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
let x = BigInt(0); | ||
let y = BigInt(1); | ||
let u = BigInt(1); | ||
let v = BigInt(0); | ||
while (a !== BigInt(0)) { | ||
let q = b / a; | ||
let r = b % a; | ||
let m = x - (u * q); | ||
let n = y - (v * q); | ||
b = a; | ||
a = r; | ||
x = u; | ||
y = v; | ||
u = m; | ||
v = n; | ||
} | ||
return { | ||
b: b, | ||
x: x, | ||
y: y | ||
}; | ||
}; | ||
/** | ||
* Modular inverse. | ||
@@ -125,5 +116,5 @@ * | ||
*/ | ||
const modInv = function (a, n) { | ||
function modInv(a, n) { | ||
let egcd = eGcd(a, n); | ||
if (egcd.b !== BigInt(1)) { | ||
if (egcd.b !== _ONE) { | ||
return null; // modular inverse does not exist | ||
@@ -133,3 +124,3 @@ } else { | ||
} | ||
}; | ||
} | ||
@@ -144,3 +135,3 @@ /** | ||
*/ | ||
const modPow = function (a, b, n) { | ||
function modPow(a, b, n) { | ||
// See Knuth, volume 2, section 4.6.3. | ||
@@ -150,11 +141,11 @@ n = BigInt(n); | ||
b = BigInt(b); | ||
if (b < BigInt(0)) { | ||
if (b < _ZERO) { | ||
return modInv(modPow(a, abs(b), n), n); | ||
} | ||
let result = BigInt(1); | ||
let result = _ONE; | ||
let x = a; | ||
while (b > 0) { | ||
var leastSignificantBit = b % BigInt(2); | ||
b = b / BigInt(2); | ||
if (leastSignificantBit == BigInt(1)) { | ||
var leastSignificantBit = b % _TWO; | ||
b = b / _TWO; | ||
if (leastSignificantBit == _ONE) { | ||
result = result * x; | ||
@@ -167,11 +158,26 @@ result = result % n; | ||
return result; | ||
}; | ||
} | ||
var main = { | ||
/** | ||
* Finds the smallest positive element that is congruent to a in modulo n | ||
* @param {number|bigint} a An integer | ||
* @param {number|bigint} n The modulo | ||
* | ||
* @returns {bigint} The smallest positive representation of a in modulo n | ||
*/ | ||
function toZn(a, n) { | ||
n = BigInt(n); | ||
a = BigInt(a) % n; | ||
return (a < 0) ? a + n : a; | ||
} | ||
var bigintModArithLatest_browser_mod = /*#__PURE__*/Object.freeze({ | ||
abs: abs, | ||
eGcd: eGcd, | ||
gcd: gcd, | ||
lcm: lcm, | ||
modInv: modInv, | ||
modPow: modPow | ||
}; | ||
modPow: modPow, | ||
toZn: toZn | ||
}); | ||
@@ -542,3 +548,3 @@ /** | ||
let b = await randBetween(w - BigInt(1), 2); | ||
let z = main.modPow(b, m, w); | ||
let z = bigintModArithLatest_browser_mod.modPow(b, m, w); | ||
if (z === BigInt(1) || z === w - BigInt(1)) | ||
@@ -548,3 +554,3 @@ continue; | ||
for (let j = 1; j < a; j++) { | ||
z = main.modPow(z, BigInt(2), w); | ||
z = bigintModArithLatest_browser_mod.modPow(z, BigInt(2), w); | ||
if (z === w - BigInt(1)) | ||
@@ -632,3 +638,3 @@ continue loop; | ||
var main$1 = { | ||
var main = { | ||
isProbablyPrime: isProbablyPrime, | ||
@@ -639,8 +645,8 @@ prime: prime, | ||
}; | ||
var main_1 = main$1.isProbablyPrime; | ||
var main_2 = main$1.prime; | ||
var main_3 = main$1.randBetween; | ||
var main_4 = main$1.randBytes; | ||
var main_1 = main.isProbablyPrime; | ||
var main_2 = main.prime; | ||
var main_3 = main.randBetween; | ||
var main_4 = main.randBytes; | ||
export default main$1; | ||
export default main; | ||
export { main_1 as isProbablyPrime, main_2 as prime, main_3 as randBetween, main_4 as randBytes }; |
@@ -1,1 +0,1 @@ | ||
const abs=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},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},lcm=function(c,d){return c=BigInt(c),d=BigInt(d),abs(c*d)/gcd(c,d)},toZn=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c: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}},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};var bigintModArithLatest_node={abs:abs,gcd:gcd,lcm:lcm,modInv:modInv,modPow:modPow};const randBytes=async function(a,b=!1){return new Promise(c=>{let d;const e=(a,d)=>{b&&(d[0]|=128),c(d)};d=new Uint8Array(a),getRandomValuesWorker(d,e)})},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},isProbablyPrime=async function(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=bigintModArithLatest_node.modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=bigintModArithLatest_node.modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0},prime=async function(a,b=16){let c=BigInt(0);do c=fromBuffer((await randBytes(a/8,!0)));while(!(await isProbablyPrime(c,b)));return c},getRandomValuesWorker=function(){function a(a,e){c[b]=e,d.postMessage({buf:a,id:b}),b++}let b=0;const c={},d=function(a){const b=new window.Blob(["("+a.toString()+")()"],{type:"text/javascript"}),d=new Worker(window.URL.createObjectURL(b));return d.onmessage=function(a){const{id:b,buf:d}=a.data;c[b]&&(c[b](!1,d),delete c[b])},d}(()=>{onmessage=function(a){const b=self.crypto.getRandomValues(a.data.buf);self.postMessage({buf:b,id:a.data.id})}});return a}();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}var main={isProbablyPrime:isProbablyPrime,prime:prime,randBetween:randBetween,randBytes:randBytes},main_1=main.isProbablyPrime,main_2=main.prime,main_3=main.randBetween,main_4=main.randBytes;export default main;export{main_1 as isProbablyPrime,main_2 as prime,main_3 as randBetween,main_4 as randBytes}; | ||
function unwrapExports(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a["default"]:a}function createCommonjsModule(a,b){return b={exports:{}},a(b,b.exports),b.exports}var bigintModArithLatest_node=createCommonjsModule(function(a,b){function c(b){return b=BigInt(b),b>=i?b:-b}function d(c,d){c=BigInt(c),d=BigInt(d);let e=i,f=j,g=j,h=i;for(;c!==i;){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}}function e(d,e){d=c(d),e=c(e);let f=i;for(;!((d|e)&j);)d>>=j,e>>=j,f++;for(;!(d&j);)d>>=j;do{for(;!(e&j);)e>>=j;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}function f(b,a){let c=d(b,a);return c.b===j?h(c.x,a):null}function g(d,e,l){if(l=BigInt(l),d=h(d,l),e=BigInt(e),e<i)return f(g(d,c(e),l),l);let m=j,o=d;for(;0<e;){var p=e%k;e/=k,p==j&&(m*=o,m%=l),o*=o,o%=l}return m}function h(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b}Object.defineProperty(b,"__esModule",{value:!0});const i=BigInt(0),j=BigInt(1),k=BigInt(2);b.abs=c,b.eGcd=d,b.gcd=e,b.lcm=function(d,f){return d=BigInt(d),f=BigInt(f),c(d*f)/e(d,f)},b.modInv=f,b.modPow=g,b.toZn=h});unwrapExports(bigintModArithLatest_node);var bigintModArithLatest_node_1=bigintModArithLatest_node.abs,bigintModArithLatest_node_2=bigintModArithLatest_node.eGcd,bigintModArithLatest_node_3=bigintModArithLatest_node.gcd,bigintModArithLatest_node_4=bigintModArithLatest_node.lcm,bigintModArithLatest_node_5=bigintModArithLatest_node.modInv,bigintModArithLatest_node_6=bigintModArithLatest_node.modPow,bigintModArithLatest_node_7=bigintModArithLatest_node.toZn;const randBytes=async function(a,b=!1){return new Promise(c=>{let d;const e=(a,d)=>{b&&(d[0]|=128),c(d)};d=new Uint8Array(a),getRandomValuesWorker(d,e)})},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},isProbablyPrime=async function(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=bigintModArithLatest_node.modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=bigintModArithLatest_node.modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0},prime=async function(a,b=16){let c=BigInt(0);do c=fromBuffer((await randBytes(a/8,!0)));while(!(await isProbablyPrime(c,b)));return c},getRandomValuesWorker=function(){function a(a,e){c[b]=e,d.postMessage({buf:a,id:b}),b++}let b=0;const c={},d=function(a){const b=new window.Blob(["("+a.toString()+")()"],{type:"text/javascript"}),d=new Worker(window.URL.createObjectURL(b));return d.onmessage=function(a){const{id:b,buf:d}=a.data;c[b]&&(c[b](!1,d),delete c[b])},d}(()=>{onmessage=function(a){const b=self.crypto.getRandomValues(a.data.buf);self.postMessage({buf:b,id:a.data.id})}});return a}();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}var main={isProbablyPrime:isProbablyPrime,prime:prime,randBetween:randBetween,randBytes:randBytes},main_1=main.isProbablyPrime,main_2=main.prime,main_3=main.randBetween,main_4=main.randBytes;export default main;export{main_1 as isProbablyPrime,main_2 as prime,main_3 as randBetween,main_4 as randBytes}; |
@@ -0,1 +1,5 @@ | ||
const _ZERO = BigInt(0); | ||
const _ONE = BigInt(1); | ||
const _TWO = BigInt(2); | ||
/** | ||
@@ -8,8 +12,50 @@ * Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 | ||
*/ | ||
const abs = function (a) { | ||
function abs(a) { | ||
a = BigInt(a); | ||
return (a >= BigInt(0)) ? a : -a; | ||
}; | ||
return (a >= _ZERO) ? a : -a; | ||
} | ||
/** | ||
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* @property {bigint} g | ||
* @property {bigint} x | ||
* @property {bigint} y | ||
*/ | ||
/** | ||
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. | ||
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* | ||
* @param {number|bigint} a | ||
* @param {number|bigint} b | ||
* | ||
* @returns {egcdReturn} | ||
*/ | ||
function eGcd(a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
let x = _ZERO; | ||
let y = _ONE; | ||
let u = _ONE; | ||
let v = _ZERO; | ||
while (a !== _ZERO) { | ||
let q = b / a; | ||
let r = b % a; | ||
let m = x - (u * q); | ||
let n = y - (v * q); | ||
b = a; | ||
a = r; | ||
x = u; | ||
y = v; | ||
u = m; | ||
v = n; | ||
} | ||
return { | ||
b: b, | ||
x: x, | ||
y: y | ||
}; | ||
} | ||
/** | ||
* Greatest-common divisor of two integers based on the iterative binary algorithm. | ||
@@ -22,14 +68,14 @@ * | ||
*/ | ||
const gcd = function (a, b) { | ||
function gcd(a, b) { | ||
a = abs(a); | ||
b = abs(b); | ||
let shift = BigInt(0); | ||
while (!((a | b) & BigInt(1))) { | ||
a >>= BigInt(1); | ||
b >>= BigInt(1); | ||
let shift = _ZERO; | ||
while (!((a | b) & _ONE)) { | ||
a >>= _ONE; | ||
b >>= _ONE; | ||
shift++; | ||
} | ||
while (!(a & BigInt(1))) a >>= BigInt(1); | ||
while (!(a & _ONE)) a >>= _ONE; | ||
do { | ||
while (!(b & BigInt(1))) b >>= BigInt(1); | ||
while (!(b & _ONE)) b >>= _ONE; | ||
if (a > b) { | ||
@@ -45,3 +91,3 @@ let x = a; | ||
return a << shift; | ||
}; | ||
} | ||
@@ -55,64 +101,9 @@ /** | ||
*/ | ||
const lcm = function (a, b) { | ||
function lcm(a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
return abs(a * b) / gcd(a, b); | ||
}; | ||
} | ||
/** | ||
* Finds the smallest positive element that is congruent to a in modulo n | ||
* @param {number|bigint} a An integer | ||
* @param {number|bigint} n The modulo | ||
* | ||
* @returns {bigint} The smallest positive representation of a in modulo n | ||
*/ | ||
const toZn = function (a, n) { | ||
n = BigInt(n); | ||
a = BigInt(a) % n; | ||
return (a < 0) ? a + n : a; | ||
}; | ||
/** | ||
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* @property {bigint} g | ||
* @property {bigint} x | ||
* @property {bigint} y | ||
*/ | ||
/** | ||
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. | ||
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
* | ||
* @param {number|bigint} a | ||
* @param {number|bigint} b | ||
* | ||
* @returns {egcdReturn} | ||
*/ | ||
const eGcd = function (a, b) { | ||
a = BigInt(a); | ||
b = BigInt(b); | ||
let x = BigInt(0); | ||
let y = BigInt(1); | ||
let u = BigInt(1); | ||
let v = BigInt(0); | ||
while (a !== BigInt(0)) { | ||
let q = b / a; | ||
let r = b % a; | ||
let m = x - (u * q); | ||
let n = y - (v * q); | ||
b = a; | ||
a = r; | ||
x = u; | ||
y = v; | ||
u = m; | ||
v = n; | ||
} | ||
return { | ||
b: b, | ||
x: x, | ||
y: y | ||
}; | ||
}; | ||
/** | ||
* Modular inverse. | ||
@@ -125,5 +116,5 @@ * | ||
*/ | ||
const modInv = function (a, n) { | ||
function modInv(a, n) { | ||
let egcd = eGcd(a, n); | ||
if (egcd.b !== BigInt(1)) { | ||
if (egcd.b !== _ONE) { | ||
return null; // modular inverse does not exist | ||
@@ -133,3 +124,3 @@ } else { | ||
} | ||
}; | ||
} | ||
@@ -144,3 +135,3 @@ /** | ||
*/ | ||
const modPow = function (a, b, n) { | ||
function modPow(a, b, n) { | ||
// See Knuth, volume 2, section 4.6.3. | ||
@@ -150,11 +141,11 @@ n = BigInt(n); | ||
b = BigInt(b); | ||
if (b < BigInt(0)) { | ||
if (b < _ZERO) { | ||
return modInv(modPow(a, abs(b), n), n); | ||
} | ||
let result = BigInt(1); | ||
let result = _ONE; | ||
let x = a; | ||
while (b > 0) { | ||
var leastSignificantBit = b % BigInt(2); | ||
b = b / BigInt(2); | ||
if (leastSignificantBit == BigInt(1)) { | ||
var leastSignificantBit = b % _TWO; | ||
b = b / _TWO; | ||
if (leastSignificantBit == _ONE) { | ||
result = result * x; | ||
@@ -167,11 +158,26 @@ result = result % n; | ||
return result; | ||
}; | ||
} | ||
var main = { | ||
/** | ||
* Finds the smallest positive element that is congruent to a in modulo n | ||
* @param {number|bigint} a An integer | ||
* @param {number|bigint} n The modulo | ||
* | ||
* @returns {bigint} The smallest positive representation of a in modulo n | ||
*/ | ||
function toZn(a, n) { | ||
n = BigInt(n); | ||
a = BigInt(a) % n; | ||
return (a < 0) ? a + n : a; | ||
} | ||
var bigintModArithLatest_browser_mod = /*#__PURE__*/Object.freeze({ | ||
abs: abs, | ||
eGcd: eGcd, | ||
gcd: gcd, | ||
lcm: lcm, | ||
modInv: modInv, | ||
modPow: modPow | ||
}; | ||
modPow: modPow, | ||
toZn: toZn | ||
}); | ||
@@ -542,3 +548,3 @@ /** | ||
let b = await randBetween(w - BigInt(1), 2); | ||
let z = main.modPow(b, m, w); | ||
let z = bigintModArithLatest_browser_mod.modPow(b, m, w); | ||
if (z === BigInt(1) || z === w - BigInt(1)) | ||
@@ -548,3 +554,3 @@ continue; | ||
for (let j = 1; j < a; j++) { | ||
z = main.modPow(z, BigInt(2), w); | ||
z = bigintModArithLatest_browser_mod.modPow(z, BigInt(2), w); | ||
if (z === w - BigInt(1)) | ||
@@ -632,3 +638,3 @@ continue loop; | ||
var main$1 = { | ||
var main = { | ||
isProbablyPrime: isProbablyPrime, | ||
@@ -639,8 +645,8 @@ prime: prime, | ||
}; | ||
var main_1 = main$1.isProbablyPrime; | ||
var main_2 = main$1.prime; | ||
var main_3 = main$1.randBetween; | ||
var main_4 = main$1.randBytes; | ||
var main_1 = main.isProbablyPrime; | ||
var main_2 = main.prime; | ||
var main_3 = main.randBetween; | ||
var main_4 = main.randBytes; | ||
export default main$1; | ||
export default main; | ||
export { main_1 as isProbablyPrime, main_2 as prime, main_3 as randBetween, main_4 as randBytes }; |
@@ -1,1 +0,1 @@ | ||
const abs=function(b){return b=BigInt(b),b>=BigInt(0)?b:-b},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},lcm=function(c,d){return c=BigInt(c),d=BigInt(d),abs(c*d)/gcd(c,d)},toZn=function(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c: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}},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};var bigintModArithLatest_node={abs:abs,gcd:gcd,lcm:lcm,modInv:modInv,modPow:modPow};const randBytes=async function(a,b=!1){return new Promise(c=>{let d;const e=(a,d)=>{b&&(d[0]|=128),c(d)};d=new Uint8Array(a),getRandomValuesWorker(d,e)})},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},isProbablyPrime=async function(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=bigintModArithLatest_node.modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=bigintModArithLatest_node.modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0},prime=async function(a,b=16){let c=BigInt(0);do c=fromBuffer((await randBytes(a/8,!0)));while(!(await isProbablyPrime(c,b)));return c},getRandomValuesWorker=function(){function a(a,e){c[b]=e,d.postMessage({buf:a,id:b}),b++}let b=0;const c={},d=function(a){const b=new window.Blob(["("+a.toString()+")()"],{type:"text/javascript"}),d=new Worker(window.URL.createObjectURL(b));return d.onmessage=function(a){const{id:b,buf:d}=a.data;c[b]&&(c[b](!1,d),delete c[b])},d}(()=>{onmessage=function(a){const b=self.crypto.getRandomValues(a.data.buf);self.postMessage({buf:b,id:a.data.id})}});return a}();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}var main={isProbablyPrime:isProbablyPrime,prime:prime,randBetween:randBetween,randBytes:randBytes},main_1=main.isProbablyPrime,main_2=main.prime,main_3=main.randBetween,main_4=main.randBytes;export default main;export{main_1 as isProbablyPrime,main_2 as prime,main_3 as randBetween,main_4 as randBytes}; | ||
function unwrapExports(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a["default"]:a}function createCommonjsModule(a,b){return b={exports:{}},a(b,b.exports),b.exports}var bigintModArithLatest_node=createCommonjsModule(function(a,b){function c(b){return b=BigInt(b),b>=i?b:-b}function d(c,d){c=BigInt(c),d=BigInt(d);let e=i,f=j,g=j,h=i;for(;c!==i;){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}}function e(d,e){d=c(d),e=c(e);let f=i;for(;!((d|e)&j);)d>>=j,e>>=j,f++;for(;!(d&j);)d>>=j;do{for(;!(e&j);)e>>=j;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}function f(b,a){let c=d(b,a);return c.b===j?h(c.x,a):null}function g(d,e,l){if(l=BigInt(l),d=h(d,l),e=BigInt(e),e<i)return f(g(d,c(e),l),l);let m=j,o=d;for(;0<e;){var p=e%k;e/=k,p==j&&(m*=o,m%=l),o*=o,o%=l}return m}function h(b,c){return c=BigInt(c),b=BigInt(b)%c,0>b?b+c:b}Object.defineProperty(b,"__esModule",{value:!0});const i=BigInt(0),j=BigInt(1),k=BigInt(2);b.abs=c,b.eGcd=d,b.gcd=e,b.lcm=function(d,f){return d=BigInt(d),f=BigInt(f),c(d*f)/e(d,f)},b.modInv=f,b.modPow=g,b.toZn=h});unwrapExports(bigintModArithLatest_node);var bigintModArithLatest_node_1=bigintModArithLatest_node.abs,bigintModArithLatest_node_2=bigintModArithLatest_node.eGcd,bigintModArithLatest_node_3=bigintModArithLatest_node.gcd,bigintModArithLatest_node_4=bigintModArithLatest_node.lcm,bigintModArithLatest_node_5=bigintModArithLatest_node.modInv,bigintModArithLatest_node_6=bigintModArithLatest_node.modPow,bigintModArithLatest_node_7=bigintModArithLatest_node.toZn;const randBytes=async function(a,b=!1){return new Promise(c=>{let d;const e=(a,d)=>{b&&(d[0]|=128),c(d)};d=new Uint8Array(a),getRandomValuesWorker(d,e)})},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},isProbablyPrime=async function(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=bigintModArithLatest_node.modPow(a,h,c);if(b===BigInt(1)||b===c-BigInt(1))continue;for(let a=1;a<f;a++){if(b=bigintModArithLatest_node.modPow(b,BigInt(2),c),b===c-BigInt(1))continue loop;if(b===BigInt(1))break}return!1}while(--b);return!0},prime=async function(a,b=16){let c=BigInt(0);do c=fromBuffer((await randBytes(a/8,!0)));while(!(await isProbablyPrime(c,b)));return c},getRandomValuesWorker=function(){function a(a,e){c[b]=e,d.postMessage({buf:a,id:b}),b++}let b=0;const c={},d=function(a){const b=new window.Blob(["("+a.toString()+")()"],{type:"text/javascript"}),d=new Worker(window.URL.createObjectURL(b));return d.onmessage=function(a){const{id:b,buf:d}=a.data;c[b]&&(c[b](!1,d),delete c[b])},d}(()=>{onmessage=function(a){const b=self.crypto.getRandomValues(a.data.buf);self.postMessage({buf:b,id:a.data.id})}});return a}();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}var main={isProbablyPrime:isProbablyPrime,prime:prime,randBetween:randBetween,randBytes:randBytes},main_1=main.isProbablyPrime,main_2=main.prime,main_3=main.randBetween,main_4=main.randBytes;export default main;export{main_1 as isProbablyPrime,main_2 as prime,main_3 as randBetween,main_4 as randBytes}; |
{ | ||
"name": "bigint-secrets", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Cryptographically secure random numbers and prime generation/testing using native JS (stage 3) implementation of BigInt", | ||
@@ -31,2 +31,3 @@ "keywords": [ | ||
"build:docs": "jsdoc2md --template=README.hbs --files ./src/main.js > README.md", | ||
"build:all": "npm run build && npm run build:browserTests && npm run build:docs", | ||
"prepublishOnly": "npm run build && npm run build:docs" | ||
@@ -37,4 +38,4 @@ }, | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"mocha": "^6.1.3", | ||
"rollup": "^1.10.0", | ||
"mocha": "^6.1.4", | ||
"rollup": "^1.10.1", | ||
"rollup-plugin-babel-minify": "^8.0.0", | ||
@@ -47,4 +48,4 @@ "rollup-plugin-commonjs": "^9.3.4", | ||
"dependencies": { | ||
"bigint-mod-arith": "^1.2.4" | ||
"bigint-mod-arith": "^1.3.0" | ||
} | ||
} |
@@ -1,16 +0,18 @@ | ||
# bigint-secrets | ||
# bigint-secrets | ||
Secure random numbers and probable prime (Miller-Rabin primality test) generation/testing using native JS (stage 3) implementation of BigInt. It can be used with Node.js (>=10.4.0) and [Web Browsers supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility). | ||
Secure random numbers and probable prime (Miller-Rabin primality test) generation/testing using native JS (stage 3) | ||
implementation of BigInt. It can be used by any [Web Browser or webview supporting | ||
BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) | ||
and with Node.js (>=10.4.0). | ||
_The operations supported on BigInts are not constant time. BigInt can be therefore **[unsuitable for use in cryptography](https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html)**_ | ||
_The operations supported on BigInts are not constant time. BigInt can be therefore **[unsuitable for use in | ||
cryptography](https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html).** Many platforms | ||
provide native support for cryptography, such as [Web Cryptography API](https://w3c.github.io/webcrypto/) or [Node.js | ||
Crypto](https://nodejs.org/dist/latest/docs/api/crypto.html)._ | ||
Many platforms provide native support for cryptography, such as [webcrypto](https://w3c.github.io/webcrypto/Overview.html) or [node crypto](https://nodejs.org/dist/latest/docs/api/crypto.html). | ||
## Installation | ||
bigint-secrets is distributed as both an ES6 and a CJS module. | ||
bigint-secrets is distributed for [web browsers and/or webviews supporting | ||
BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) | ||
as an ES6 module; and for Node.js (>=10.4.0), as a CJS module. | ||
The ES6 module is built for any [web browser supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility). The module only uses native javascript implementations and no polyfills had been applied. | ||
The CJS module is built as a standard node module. | ||
bigint-secrets can be imported to your project with `npm`: | ||
@@ -20,4 +22,6 @@ ```bash | ||
``` | ||
NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. | ||
For web browsers, you can also [download the bundle from GitHub](https://raw.githubusercontent.com/juanelas/bigint-secrets/master/dist/bigint-secrets-latest.browser.mod.min.js). | ||
For web browsers, you can also [download the bundle from | ||
GitHub](https://raw.githubusercontent.com/juanelas/bigint-secrets/master/dist/bigint-secrets-latest.browser.mod.min.js). | ||
@@ -35,6 +39,6 @@ ## Usage example | ||
if ( await secrets.isProbablyPrime(prime) ) | ||
return true; | ||
return true; | ||
// Get a cryptographically secure random number between 1 and 2**256 bits. | ||
const rnd = secrets.randBetween(BigInt(2)**256); | ||
const rnd = secrets.randBetween(BigInt(2) ** BigInt(256)); | ||
``` | ||
@@ -44,18 +48,18 @@ | ||
```html | ||
<script type="module"> | ||
import * as bigintSecrets from 'bigint-secrets-latest.browser.mod.min.js'; | ||
<script type="module"> | ||
import * as bigintSecrets from 'bigint-secrets-latest.browser.mod.min.js'; | ||
(async function () { | ||
// Get a cryptographically secure random number between 1 and 2**256 bits. | ||
const rnd = await bigintSecrets.randBetween(BigInt(2) ** 256); | ||
alert(rnd); | ||
(async function () { | ||
// Get a cryptographically secure random number between 1 and 2**256 bits. | ||
const rnd = await bigintSecrets.randBetween(BigInt(2) ** BigInt(256)); | ||
alert(rnd); | ||
// Generation of a probable prime of 2018 bits | ||
const p = await bigintSecrets.prime(2048); | ||
// Generation of a probable prime of 2018 bits | ||
const p = await bigintSecrets.prime(2048); | ||
// Testing if a prime is a probable prime (Miller-Rabin) | ||
const isPrime = await bigintSecrets.isProbablyPrime(p); | ||
alert(p.toString() + '\nIs prime?\n' + isPrime); | ||
})(); | ||
</script> | ||
// Testing if a prime is a probable prime (Miller-Rabin) | ||
const isPrime = await bigintSecrets.isProbablyPrime(p); | ||
alert(p.toString() + '\nIs prime?\n' + isPrime); | ||
})(); | ||
</script> | ||
``` | ||
@@ -135,2 +139,2 @@ | ||
* * * | ||
* * * |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
118301
22
3890
136
0
Updatedbigint-mod-arith@^1.3.0