@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
65 lines (64 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CURRENT_DH_PRIME = void 0;
exports.gcd = gcd;
exports.decompose = decompose;
const helpers_js_1 = require("../helpers.js");
const CURRENT_DH_PRIME = BigInt('0x' +
'C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F' +
'48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37' +
'20FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F64' +
'2477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4' +
'A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754' +
'FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4' +
'E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F' +
'0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B');
exports.CURRENT_DH_PRIME = CURRENT_DH_PRIME;
function gcd(a, b) {
while (b) {
const c = (0, helpers_js_1.bigIntMod)(a, b);
a = b;
b = c;
}
return a;
}
function decompose(pq) {
if (pq == BigInt(1))
return pq;
if ((0, helpers_js_1.bigIntMod)(pq, BigInt(2)) === BigInt(0))
return BigInt(2);
let y = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
const c = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
const m = (0, helpers_js_1.randBigint)(BigInt(1), pq - BigInt(1));
let g = BigInt(1);
let r = BigInt(1);
let q = BigInt(1);
let x = BigInt(0);
let ys = BigInt(0);
while (g === BigInt(1)) {
x = y;
for (let i = 0; BigInt(i) < r; i++) {
y = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(y, BigInt(2), pq) + c, pq);
}
let k = BigInt(0);
while (k < r && g === BigInt(1)) {
ys = y;
for (let i = 0; BigInt(i) < helpers_js_1.bigMath.min(m, r - k); i++) {
y = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(y, BigInt(2), pq) + c, pq);
q = q * (0, helpers_js_1.bigIntMod)(helpers_js_1.bigMath.abs(BigInt(x - y)), pq);
}
g = gcd(q, pq);
k += m;
}
r *= BigInt(2);
if (g === pq) {
while (true) {
ys = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(ys, BigInt(2), pq) + c, pq);
g = gcd(helpers_js_1.bigMath.abs(BigInt(x - ys)), pq);
if (g > BigInt(1))
break;
}
}
}
return g;
}