|
|
|
@ -144,6 +144,12 @@ function bytesFromBigInt (bigInt, len) {
@@ -144,6 +144,12 @@ function bytesFromBigInt (bigInt, len) {
|
|
|
|
|
return bytes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function bytesFromLeemonBigInt (bigInt, len) { |
|
|
|
|
var str = bigInt2str(bigInt, 16); |
|
|
|
|
return bytesFromHex(str); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function bytesToArrayBuffer (b) { |
|
|
|
|
return (new Uint8Array(b)).buffer; |
|
|
|
|
} |
|
|
|
@ -272,12 +278,19 @@ function nextRandomInt (maxValue) {
@@ -272,12 +278,19 @@ function nextRandomInt (maxValue) {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
function pqPrimeFactorization (pqBytes) { |
|
|
|
|
var what = new BigInteger(pqBytes), |
|
|
|
|
var what = new BigInteger(pqBytes), |
|
|
|
|
result = false; |
|
|
|
|
|
|
|
|
|
console.log('PQ start', pqBytes, what.bitLength()); |
|
|
|
|
|
|
|
|
|
if (what.bitLength() <= 64) { |
|
|
|
|
try { |
|
|
|
|
result = pqPrimeLeemon(str2bigInt(what.toString(16), 16, Math.ceil(64 / bpe) + 1)) |
|
|
|
|
} catch (e) { |
|
|
|
|
console.error(e); |
|
|
|
|
console.error('Pq leemon Exception', e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (result === false && what.bitLength() <= 64) { |
|
|
|
|
// console.time('PQ long');
|
|
|
|
|
try { |
|
|
|
|
result = pqPrimeLong(goog.math.Long.fromString(what.toString(16), 16)); |
|
|
|
@ -374,7 +387,6 @@ function gcdLong(a, b) {
@@ -374,7 +387,6 @@ function gcdLong(a, b) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function pqPrimeLong(what) { |
|
|
|
|
// console.log('start long');
|
|
|
|
|
var it = 0, |
|
|
|
|
g; |
|
|
|
|
for (var i = 0; i < 3; i++) { |
|
|
|
@ -385,9 +397,6 @@ function pqPrimeLong(what) {
@@ -385,9 +397,6 @@ function pqPrimeLong(what) {
|
|
|
|
|
|
|
|
|
|
for (var j = 1; j < lim; j++) { |
|
|
|
|
++it; |
|
|
|
|
// if (!(it % 100)) {
|
|
|
|
|
// console.log(dT(), 'it', it, i, j, x.toString());
|
|
|
|
|
// }
|
|
|
|
|
var a = x, |
|
|
|
|
b = x, |
|
|
|
|
c = q; |
|
|
|
@ -433,3 +442,78 @@ function pqPrimeLong(what) {
@@ -433,3 +442,78 @@ function pqPrimeLong(what) {
|
|
|
|
|
|
|
|
|
|
return [bytesFromHex(P.toString(16)), bytesFromHex(Q.toString(16))]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function pqPrimeLeemon (what) { |
|
|
|
|
var minBits = 64, |
|
|
|
|
minLen = Math.ceil(minBits / bpe) + 1, |
|
|
|
|
it = 0, i, q, j, lim, g, P, Q, |
|
|
|
|
a = new Array(minLen), |
|
|
|
|
b = new Array(minLen), |
|
|
|
|
c = new Array(minLen), |
|
|
|
|
g = new Array(minLen), |
|
|
|
|
z = new Array(minLen), |
|
|
|
|
x = new Array(minLen), |
|
|
|
|
y = new Array(minLen); |
|
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) { |
|
|
|
|
q = (nextRandomInt(128) & 15) + 17; |
|
|
|
|
copyInt_(x, nextRandomInt(1000000000) + 1); |
|
|
|
|
copy_(y, x); |
|
|
|
|
lim = 1 << (i + 18); |
|
|
|
|
|
|
|
|
|
for (j = 1; j < lim; j++) { |
|
|
|
|
++it; |
|
|
|
|
copy_(a, x); |
|
|
|
|
copy_(b, x); |
|
|
|
|
copyInt_(c, q); |
|
|
|
|
|
|
|
|
|
while (!isZero(b)) { |
|
|
|
|
if (b[0] & 1) { |
|
|
|
|
add_(c, a); |
|
|
|
|
if (greater(c, what)) { |
|
|
|
|
sub_(c, what); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
add_(a, a); |
|
|
|
|
if (greater(a, what)) { |
|
|
|
|
sub_(a, what); |
|
|
|
|
} |
|
|
|
|
rightShift_(b, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
copy_(x, c); |
|
|
|
|
if (greater(x,y)) { |
|
|
|
|
copy_(z, x); |
|
|
|
|
sub_(z, y); |
|
|
|
|
} else { |
|
|
|
|
copy_(z, y); |
|
|
|
|
sub_(z, x); |
|
|
|
|
} |
|
|
|
|
eGCD_(z, what, g, a, b); |
|
|
|
|
if (!equalsInt(g, 1)) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if ((j & (j - 1)) == 0) { |
|
|
|
|
copy_(y, x); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (greater(g, one)) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
divide_(what, g, x, y); |
|
|
|
|
|
|
|
|
|
if (greater(g, x)) { |
|
|
|
|
P = x; |
|
|
|
|
Q = g; |
|
|
|
|
} else { |
|
|
|
|
P = g; |
|
|
|
|
Q = x; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// console.log(dT(), 'done', bigInt2str(what, 10), bigInt2str(P, 10), bigInt2str(Q, 10));
|
|
|
|
|
|
|
|
|
|
return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q)]; |
|
|
|
|
} |