paillier-bigint
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -211,10 +211,8 @@ var paillierBigint = (function (exports) { | ||
while (b > 0) { | ||
var leastSignificantBit = b % _TWO; | ||
var leastSignificantBit = b & _ONE; | ||
b = b / _TWO; | ||
if (leastSignificantBit == _ONE) { | ||
result = result * x; | ||
result = result % n; | ||
if (leastSignificantBit === _ONE) { | ||
result = (result * x) % n; | ||
} | ||
x = x * x; | ||
x = x % n; | ||
x = (x * x) % n; | ||
} | ||
@@ -678,20 +676,20 @@ return result; | ||
/* | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
let a = _ZERO, d = w - _ONE; | ||
@@ -698,0 +696,0 @@ while (d % _TWO === _ZERO) { |
@@ -1,1 +0,1 @@ | ||
var paillierBigint=function(a){'use strict';function c(b){return b=BigInt(b),b>=z?b:-b}function d(b){if(b=BigInt(b),b===A)return 1;let c=1;do c++;while((b>>=A)>A);return c}function e(c,d){if(c=BigInt(c),d=BigInt(d),c<=z|d<=z)return NaN;let e=z,f=A,g=A,h=z;for(;c!==z;){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 f(d,e){if(d=c(d),e=c(e),d===z)return e;if(e===z)return d;let f=z;for(;!((d|e)&A);)d>>=A,e>>=A,f++;for(;!(d&A);)d>>=A;do{for(;!(e&A);)e>>=A;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}async function g(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{let e=new Worker(q());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function h(d,e){return d=BigInt(d),e=BigInt(e),d===z&&e===z?z:c(d*e)/f(d,e)}function i(b,a){if(b==z|a<=z)return NaN;let c=e(o(b,a),a);return c.b===A?o(c.x,a):NaN}function j(d,e,f){if(f=BigInt(f),f===z)return NaN;if(d=o(d,f),e=BigInt(e),e<z)return i(j(d,c(e),f),f);let g=A,h=d;for(;0<e;){var k=e%B;e/=B,k==A&&(g*=h,g%=f),h*=h,h%=f}return g}function k(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=m(a,!0),d=p(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=q();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=m(a,!0),f=p(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})}function l(a,b=A){if(a<=b)throw new Error("max must be > min");const c=a-b;let e,f=d(c);do{let a=m(f);e=p(a)}while(e>c);return e+b}function m(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=n(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function n(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function o(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function p(a){let b=z;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function q(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${e.toString()};const modInv = ${i.toString()};const modPow = ${j.toString()};const toZn = ${o.toString()};const randBits = ${m.toString()};const randBytesSync = ${n.toString()};const randBetween = ${l.toString()};const isProbablyPrime = ${s.toString()};${d.toString()}${p.toString()}`;return a+=`onmessage = ${async function(a){const b=await g(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,r(a)}function r(a){a=`(() => {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function s(c,b=16){if(c===B)return!0;if((c&A)===z||c===A)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===z)return!1}let f=z,g=c-A;for(;g%B===z;)g/=B,++f;let h=(c-A)/B**f;loop:do{let a=l(c-A,B),b=j(a,h,c);if(b===A||b===c-A)continue;for(let a=1;a<f;a++){if(b=j(b,B,c),b===c-A)continue loop;if(b===A)break}return!1}while(--b);return!0}function t(b,a){return(b-C)/a}function b(a,b=j(a,2)){const c=l(a),d=l(a);return(c*a+C)*j(d,a,b)%b}const z=BigInt(0),A=BigInt(1),B=BigInt(2),C=BigInt(1),D=class{constructor(a,b){this.n=BigInt(a),this._n2=this.n**BigInt(2),this.g=BigInt(b)}get bitLength(){return d(this.n)}encrypt(a){let b;return b=l(this.n),j(this.g,a,this._n2)*j(b,this.n,this._n2)%this._n2}addition(...a){return a.reduce((a,b)=>a*BigInt(b)%this._n2,C)}multiply(a,b){return"string"==typeof b&&(b=BigInt(b)),j(BigInt(a),b,this._n2)}},E=class{constructor(a,b,c,d,e){this.lambda=BigInt(a),this.mu=BigInt(b),this._p=BigInt(c),this._q=BigInt(d),this.publicKey=e}get bitLength(){return d(this.publicKey.n)}get n(){return this.publicKey.n}decrypt(a){return t(j(BigInt(a),this.lambda,this.publicKey._n2),this.publicKey.n)*this.mu%this.publicKey.n}};return a.PrivateKey=E,a.PublicKey=D,a.generateRandomKeys=async function(a=4096,c=!1){var e=Math.floor;let f,l,m,o,r,s,u,v;do f=await k(e(a/2)+1),l=await k(e(a/2)),m=f*l;while(l===f||d(m)!=a);o=(f-C)*(l-C),r=m**BigInt(2),!0===c?(s=m+C,u=o,v=i(u,m)):(s=b(m,r),u=h(f-C,l-C),v=i(t(j(s,u,r),m),m));const w=new D(m,s),x=new E(u,v,f,l,w);return{publicKey:w,privateKey:x}},a}({}); | ||
var paillierBigint=function(a){'use strict';function c(b){return b=BigInt(b),b>=z?b:-b}function d(b){if(b=BigInt(b),b===A)return 1;let c=1;do c++;while((b>>=A)>A);return c}function e(c,d){if(c=BigInt(c),d=BigInt(d),c<=z|d<=z)return NaN;let e=z,f=A,g=A,h=z;for(;c!==z;){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 f(d,e){if(d=c(d),e=c(e),d===z)return e;if(e===z)return d;let f=z;for(;!((d|e)&A);)d>>=A,e>>=A,f++;for(;!(d&A);)d>>=A;do{for(;!(e&A);)e>>=A;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}async function g(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{let e=new Worker(q());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function h(d,e){return d=BigInt(d),e=BigInt(e),d===z&&e===z?z:c(d*e)/f(d,e)}function i(b,a){if(b==z|a<=z)return NaN;let c=e(o(b,a),a);return c.b===A?o(c.x,a):NaN}function j(d,e,f){if(f=BigInt(f),f===z)return NaN;if(d=o(d,f),e=BigInt(e),e<z)return i(j(d,c(e),f),f);let g=A,h=d;for(;0<e;){var k=e&A;e/=B,k===A&&(g=g*h%f),h=h*h%f}return g}function k(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=m(a,!0),d=p(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=q();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=m(a,!0),f=p(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})}function l(a,b=A){if(a<=b)throw new Error("max must be > min");const c=a-b;let e,f=d(c);do{let a=m(f);e=p(a)}while(e>c);return e+b}function m(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=n(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function n(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function o(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function p(a){let b=z;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function q(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${e.toString()};const modInv = ${i.toString()};const modPow = ${j.toString()};const toZn = ${o.toString()};const randBits = ${m.toString()};const randBytesSync = ${n.toString()};const randBetween = ${l.toString()};const isProbablyPrime = ${s.toString()};${d.toString()}${p.toString()}`;return a+=`onmessage = ${async function(a){const b=await g(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,r(a)}function r(a){a=`(() => {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function s(c,b=16){if(c===B)return!0;if((c&A)===z||c===A)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===z)return!1}let f=z,g=c-A;for(;g%B===z;)g/=B,++f;let h=(c-A)/B**f;loop:do{let a=l(c-A,B),b=j(a,h,c);if(b===A||b===c-A)continue;for(let a=1;a<f;a++){if(b=j(b,B,c),b===c-A)continue loop;if(b===A)break}return!1}while(--b);return!0}function t(b,a){return(b-C)/a}function b(a,b=j(a,2)){const c=l(a),d=l(a);return(c*a+C)*j(d,a,b)%b}const z=BigInt(0),A=BigInt(1),B=BigInt(2),C=BigInt(1),D=class{constructor(a,b){this.n=BigInt(a),this._n2=this.n**BigInt(2),this.g=BigInt(b)}get bitLength(){return d(this.n)}encrypt(a){let b;return b=l(this.n),j(this.g,a,this._n2)*j(b,this.n,this._n2)%this._n2}addition(...a){return a.reduce((a,b)=>a*BigInt(b)%this._n2,C)}multiply(a,b){return"string"==typeof b&&(b=BigInt(b)),j(BigInt(a),b,this._n2)}},E=class{constructor(a,b,c,d,e){this.lambda=BigInt(a),this.mu=BigInt(b),this._p=BigInt(c),this._q=BigInt(d),this.publicKey=e}get bitLength(){return d(this.publicKey.n)}get n(){return this.publicKey.n}decrypt(a){return t(j(BigInt(a),this.lambda,this.publicKey._n2),this.publicKey.n)*this.mu%this.publicKey.n}};return a.PrivateKey=E,a.PublicKey=D,a.generateRandomKeys=async function(a=4096,c=!1){var e=Math.floor;let f,l,m,o,r,s,u,v;do f=await k(e(a/2)+1),l=await k(e(a/2)),m=f*l;while(l===f||d(m)!=a);o=(f-C)*(l-C),r=m**BigInt(2),!0===c?(s=m+C,u=o,v=i(u,m)):(s=b(m,r),u=h(f-C,l-C),v=i(t(j(s,u,r),m),m));const w=new D(m,s),x=new E(u,v,f,l,w);return{publicKey:w,privateKey:x}},a}({}); |
@@ -208,10 +208,8 @@ const _ZERO = BigInt(0); | ||
while (b > 0) { | ||
var leastSignificantBit = b % _TWO; | ||
var leastSignificantBit = b & _ONE; | ||
b = b / _TWO; | ||
if (leastSignificantBit == _ONE) { | ||
result = result * x; | ||
result = result % n; | ||
if (leastSignificantBit === _ONE) { | ||
result = (result * x) % n; | ||
} | ||
x = x * x; | ||
x = x % n; | ||
x = (x * x) % n; | ||
} | ||
@@ -675,20 +673,20 @@ return result; | ||
/* | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
let a = _ZERO, d = w - _ONE; | ||
@@ -695,0 +693,0 @@ while (d % _TWO === _ZERO) { |
@@ -1,1 +0,1 @@ | ||
const _ZERO=BigInt(0),_ONE=BigInt(1),_TWO=BigInt(2);function abs(b){return b=BigInt(b),b>=_ZERO?b:-b}function bitLength(b){if(b=BigInt(b),b===_ONE)return 1;let c=1;do c++;while((b>>=_ONE)>_ONE);return c}function eGcd(c,d){if(c=BigInt(c),d=BigInt(d),c<=_ZERO|d<=_ZERO)return NaN;let e=_ZERO,f=_ONE,g=_ONE,h=_ZERO;for(;c!==_ZERO;){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 gcd(c,d){if(c=abs(c),d=abs(d),c===_ZERO)return d;if(d===_ZERO)return c;let e=_ZERO;for(;!((c|d)&_ONE);)c>>=_ONE,d>>=_ONE,e++;for(;!(c&_ONE);)c>>=_ONE;do{for(;!(d&_ONE);)d>>=_ONE;if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e}async function isProbablyPrime(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{let e=new Worker(_isProbablyPrimeWorkerUrl());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function lcm(c,d){return c=BigInt(c),d=BigInt(d),c===_ZERO&&d===_ZERO?_ZERO:abs(c*d)/gcd(c,d)}function modInv(b,a){if(b==_ZERO|a<=_ZERO)return NaN;let c=eGcd(toZn(b,a),a);return c.b===_ONE?toZn(c.x,a):NaN}function modPow(c,d,e){if(e=BigInt(e),e===_ZERO)return NaN;if(c=toZn(c,e),d=BigInt(d),d<_ZERO)return modInv(modPow(c,abs(d),e),e);let f=_ONE,g=c;for(;0<d;){var h=d%_TWO;d/=_TWO,h==_ONE&&(f*=g,f%=e),g*=g,g%=e}return f}function prime(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=randBits(a,!0),d=fromBuffer(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=_isProbablyPrimeWorkerUrl();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=randBits(a,!0),f=fromBuffer(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})}function randBetween(a,b=_ONE){if(a<=b)throw new Error("max must be > min");const c=a-b;let d,e=bitLength(c);do{let a=randBits(e);d=fromBuffer(a)}while(d>c);return d+b}function randBits(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=randBytesSync(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function randBytesSync(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function toZn(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function fromBuffer(a){let b=_ZERO;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function _isProbablyPrimeWorkerUrl(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${eGcd.toString()};const modInv = ${modInv.toString()};const modPow = ${modPow.toString()};const toZn = ${toZn.toString()};const randBits = ${randBits.toString()};const randBytesSync = ${randBytesSync.toString()};const randBetween = ${randBetween.toString()};const isProbablyPrime = ${_isProbablyPrime.toString()};${bitLength.toString()}${fromBuffer.toString()}`;return a+=`onmessage = ${async function(a){const b=await isProbablyPrime(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,_workerUrl(a)}function _workerUrl(a){a=`(() => {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function _isProbablyPrime(c,b=16){if(c===_TWO)return!0;if((c&_ONE)===_ZERO||c===_ONE)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===_ZERO)return!1}let f=_ZERO,g=c-_ONE;for(;g%_TWO===_ZERO;)g/=_TWO,++f;let h=(c-_ONE)/_TWO**f;loop:do{let a=randBetween(c-_ONE,_TWO),b=modPow(a,h,c);if(b===_ONE||b===c-_ONE)continue;for(let a=1;a<f;a++){if(b=modPow(b,_TWO,c),b===c-_ONE)continue loop;if(b===_ONE)break}return!1}while(--b);return!0}const _ONE$1=BigInt(1),generateRandomKeys=async function(a=4096,b=!1){var c=Math.floor;let d,e,f,h,i,j,k,l;do d=await prime(c(a/2)+1),e=await prime(c(a/2)),f=d*e;while(e===d||bitLength(f)!=a);h=(d-_ONE$1)*(e-_ONE$1),i=f**BigInt(2),!0===b?(j=f+_ONE$1,k=h,l=modInv(k,f)):(j=getGenerator(f,i),k=lcm(d-_ONE$1,e-_ONE$1),l=modInv(L(modPow(j,k,i),f),f));const m=new PublicKey(f,j),o=new PrivateKey(k,l,d,e,m);return{publicKey:m,privateKey:o}},PublicKey=class{constructor(a,b){this.n=BigInt(a),this._n2=this.n**BigInt(2),this.g=BigInt(b)}get bitLength(){return bitLength(this.n)}encrypt(a){let b;return b=randBetween(this.n),modPow(this.g,a,this._n2)*modPow(b,this.n,this._n2)%this._n2}addition(...a){return a.reduce((a,b)=>a*BigInt(b)%this._n2,_ONE$1)}multiply(a,b){return"string"==typeof b&&(b=BigInt(b)),modPow(BigInt(a),b,this._n2)}},PrivateKey=class{constructor(a,b,c,d,e){this.lambda=BigInt(a),this.mu=BigInt(b),this._p=BigInt(c),this._q=BigInt(d),this.publicKey=e}get bitLength(){return bitLength(this.publicKey.n)}get n(){return this.publicKey.n}decrypt(a){return L(modPow(BigInt(a),this.lambda,this.publicKey._n2),this.publicKey.n)*this.mu%this.publicKey.n}};function L(b,a){return(b-_ONE$1)/a}function getGenerator(a,b=modPow(a,2)){const c=randBetween(a),d=randBetween(a);return(c*a+_ONE$1)*modPow(d,a,b)%b}export{PrivateKey,PublicKey,generateRandomKeys}; | ||
const _ZERO=BigInt(0),_ONE=BigInt(1),_TWO=BigInt(2);function abs(b){return b=BigInt(b),b>=_ZERO?b:-b}function bitLength(b){if(b=BigInt(b),b===_ONE)return 1;let c=1;do c++;while((b>>=_ONE)>_ONE);return c}function eGcd(c,d){if(c=BigInt(c),d=BigInt(d),c<=_ZERO|d<=_ZERO)return NaN;let e=_ZERO,f=_ONE,g=_ONE,h=_ZERO;for(;c!==_ZERO;){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 gcd(c,d){if(c=abs(c),d=abs(d),c===_ZERO)return d;if(d===_ZERO)return c;let e=_ZERO;for(;!((c|d)&_ONE);)c>>=_ONE,d>>=_ONE,e++;for(;!(c&_ONE);)c>>=_ONE;do{for(;!(d&_ONE);)d>>=_ONE;if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<<e}async function isProbablyPrime(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{let e=new Worker(_isProbablyPrimeWorkerUrl());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function lcm(c,d){return c=BigInt(c),d=BigInt(d),c===_ZERO&&d===_ZERO?_ZERO:abs(c*d)/gcd(c,d)}function modInv(b,a){if(b==_ZERO|a<=_ZERO)return NaN;let c=eGcd(toZn(b,a),a);return c.b===_ONE?toZn(c.x,a):NaN}function modPow(c,d,e){if(e=BigInt(e),e===_ZERO)return NaN;if(c=toZn(c,e),d=BigInt(d),d<_ZERO)return modInv(modPow(c,abs(d),e),e);let f=_ONE,g=c;for(;0<d;){var h=d&_ONE;d/=_TWO,h===_ONE&&(f=f*g%e),g=g*g%e}return f}function prime(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=randBits(a,!0),d=fromBuffer(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=_isProbablyPrimeWorkerUrl();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=randBits(a,!0),f=fromBuffer(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})}function randBetween(a,b=_ONE){if(a<=b)throw new Error("max must be > min");const c=a-b;let d,e=bitLength(c);do{let a=randBits(e);d=fromBuffer(a)}while(d>c);return d+b}function randBits(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=randBytesSync(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function randBytesSync(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function toZn(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function fromBuffer(a){let b=_ZERO;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function _isProbablyPrimeWorkerUrl(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${eGcd.toString()};const modInv = ${modInv.toString()};const modPow = ${modPow.toString()};const toZn = ${toZn.toString()};const randBits = ${randBits.toString()};const randBytesSync = ${randBytesSync.toString()};const randBetween = ${randBetween.toString()};const isProbablyPrime = ${_isProbablyPrime.toString()};${bitLength.toString()}${fromBuffer.toString()}`;return a+=`onmessage = ${async function(a){const b=await isProbablyPrime(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,_workerUrl(a)}function _workerUrl(a){a=`(() => {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function _isProbablyPrime(c,b=16){if(c===_TWO)return!0;if((c&_ONE)===_ZERO||c===_ONE)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===_ZERO)return!1}let f=_ZERO,g=c-_ONE;for(;g%_TWO===_ZERO;)g/=_TWO,++f;let h=(c-_ONE)/_TWO**f;loop:do{let a=randBetween(c-_ONE,_TWO),b=modPow(a,h,c);if(b===_ONE||b===c-_ONE)continue;for(let a=1;a<f;a++){if(b=modPow(b,_TWO,c),b===c-_ONE)continue loop;if(b===_ONE)break}return!1}while(--b);return!0}const _ONE$1=BigInt(1),generateRandomKeys=async function(a=4096,b=!1){var c=Math.floor;let d,e,f,h,i,j,k,l;do d=await prime(c(a/2)+1),e=await prime(c(a/2)),f=d*e;while(e===d||bitLength(f)!=a);h=(d-_ONE$1)*(e-_ONE$1),i=f**BigInt(2),!0===b?(j=f+_ONE$1,k=h,l=modInv(k,f)):(j=getGenerator(f,i),k=lcm(d-_ONE$1,e-_ONE$1),l=modInv(L(modPow(j,k,i),f),f));const m=new PublicKey(f,j),o=new PrivateKey(k,l,d,e,m);return{publicKey:m,privateKey:o}},PublicKey=class{constructor(a,b){this.n=BigInt(a),this._n2=this.n**BigInt(2),this.g=BigInt(b)}get bitLength(){return bitLength(this.n)}encrypt(a){let b;return b=randBetween(this.n),modPow(this.g,a,this._n2)*modPow(b,this.n,this._n2)%this._n2}addition(...a){return a.reduce((a,b)=>a*BigInt(b)%this._n2,_ONE$1)}multiply(a,b){return"string"==typeof b&&(b=BigInt(b)),modPow(BigInt(a),b,this._n2)}},PrivateKey=class{constructor(a,b,c,d,e){this.lambda=BigInt(a),this.mu=BigInt(b),this._p=BigInt(c),this._q=BigInt(d),this.publicKey=e}get bitLength(){return bitLength(this.publicKey.n)}get n(){return this.publicKey.n}decrypt(a){return L(modPow(BigInt(a),this.lambda,this.publicKey._n2),this.publicKey.n)*this.mu%this.publicKey.n}};function L(b,a){return(b-_ONE$1)/a}function getGenerator(a,b=modPow(a,2)){const c=randBetween(a),d=randBetween(a);return(c*a+_ONE$1)*modPow(d,a,b)%b}export{PrivateKey,PublicKey,generateRandomKeys}; |
{ | ||
"name": "paillier-bigint", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "An implementation of the Paillier cryptosystem using native JS (stage 3) implementation of BigInt", | ||
@@ -47,4 +47,4 @@ "keywords": [ | ||
"dependencies": { | ||
"bigint-crypto-utils": "^2.1.6" | ||
"bigint-crypto-utils": "^2.1.7" | ||
} | ||
} |
@@ -208,10 +208,8 @@ const _ZERO = BigInt(0); | ||
while (b > 0) { | ||
var leastSignificantBit = b % _TWO; | ||
var leastSignificantBit = b & _ONE; | ||
b = b / _TWO; | ||
if (leastSignificantBit == _ONE) { | ||
result = result * x; | ||
result = result % n; | ||
if (leastSignificantBit === _ONE) { | ||
result = (result * x) % n; | ||
} | ||
x = x * x; | ||
x = x % n; | ||
x = (x * x) % n; | ||
} | ||
@@ -700,20 +698,20 @@ return result; | ||
/* | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
1. Let a be the largest integer such that 2**a divides w−1. | ||
2. m = (w−1) / 2**a. | ||
3. wlen = len (w). | ||
4. For i = 1 to iterations do | ||
4.1 Obtain a string b of wlen bits from an RBG. | ||
Comment: Ensure that 1 < b < w−1. | ||
4.2 If ((b ≤ 1) or (b ≥ w−1)), then go to step 4.1. | ||
4.3 z = b**m mod w. | ||
4.4 If ((z = 1) or (z = w − 1)), then go to step 4.7. | ||
4.5 For j = 1 to a − 1 do. | ||
4.5.1 z = z**2 mod w. | ||
4.5.2 If (z = w−1), then go to step 4.7. | ||
4.5.3 If (z = 1), then go to step 4.6. | ||
4.6 Return COMPOSITE. | ||
4.7 Continue. | ||
Comment: Increment i for the do-loop in step 4. | ||
5. Return PROBABLY PRIME. | ||
*/ | ||
let a = _ZERO, d = w - _ONE; | ||
@@ -778,12 +776,6 @@ while (d % _TWO === _ZERO) { | ||
let ciphertexts = []; | ||
before(async function () { | ||
keyPair = await paillierBigint.generateRandomKeys(bitLength); | ||
for (let i = 0; i < tests; i++) { | ||
numbers[i] = bigintCryptoUtilsLatest_browser_mod.randBetween(keyPair.publicKey.n); | ||
ciphertexts[i] = keyPair.publicKey.encrypt(numbers[i]); | ||
} | ||
}); | ||
describe(`generateRandomKeys(${bitLength})`, function () { | ||
it(`it should return a publicKey and a privateKey with public modulus of ${bitLength} bits`, async function () { | ||
keyPair = await paillierBigint.generateRandomKeys(bitLength); | ||
chai.expect(keyPair.publicKey).to.be.an.instanceOf(paillierBigint.PublicKey); | ||
@@ -799,2 +791,4 @@ chai.expect(keyPair.privateKey).to.be.an.instanceOf(paillierBigint.PrivateKey); | ||
for (let i = 0; i < tests; i++) { | ||
numbers[i] = bigintCryptoUtilsLatest_browser_mod.randBetween(keyPair.publicKey.n); | ||
ciphertexts[i] = keyPair.publicKey.encrypt(numbers[i]); | ||
const decrypted = keyPair.privateKey.decrypt(ciphertexts[i]); | ||
@@ -801,0 +795,0 @@ if (numbers[i] !== decrypted) { |
@@ -18,12 +18,6 @@ 'use strict'; | ||
let ciphertexts = []; | ||
before(async function () { | ||
keyPair = await paillierBigint.generateRandomKeys(bitLength); | ||
for (let i = 0; i < tests; i++) { | ||
numbers[i] = bcu.randBetween(keyPair.publicKey.n); | ||
ciphertexts[i] = keyPair.publicKey.encrypt(numbers[i]); | ||
} | ||
}); | ||
describe(`generateRandomKeys(${bitLength})`, function () { | ||
it(`it should return a publicKey and a privateKey with public modulus of ${bitLength} bits`, async function () { | ||
keyPair = await paillierBigint.generateRandomKeys(bitLength); | ||
chai.expect(keyPair.publicKey).to.be.an.instanceOf(paillierBigint.PublicKey); | ||
@@ -39,2 +33,4 @@ chai.expect(keyPair.privateKey).to.be.an.instanceOf(paillierBigint.PrivateKey); | ||
for (let i = 0; i < tests; i++) { | ||
numbers[i] = bcu.randBetween(keyPair.publicKey.n); | ||
ciphertexts[i] = keyPair.publicKey.encrypt(numbers[i]); | ||
const decrypted = keyPair.privateKey.decrypt(ciphertexts[i]); | ||
@@ -41,0 +37,0 @@ if (numbers[i] !== decrypted) { |
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
130979
3071
Updatedbigint-crypto-utils@^2.1.7