计算机如何计算或存储 Diffie-Hellman 值?

How does the computer calculate or store Diffie-Hellman values?

据我所知,Diffie-Hellman 密钥通常为 2048 位,但我的计算机几乎无法计算出 10 位数字。 Diffie-Hellman 中有哪些常用数字?

这是我的代码,超级慢:

    $gen = 77;
    $mod = 517165;
    
    $saltA = 1233217;
    $saltB = 5173123;
    
    $calculatedSecretKeyA = gmp_mod(gmp_pow($gen, $saltA), $mod);
    
    $calculatedSecretKeyB = gmp_mod(gmp_pow($gen, $saltB), $mod);

    $calcKeyA = gmp_mod(gmp_pow($calculatedSecretKeyB, $saltA), $mod);
    echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
    
    
    $calcKeyB = gmp_mod(gmp_pow($calculatedSecretKeyA, $saltB), $mod);
    echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;

使用gmp_powm

gmp_powm ( GMP|int|string $num , GMP|int|string $exponent , GMP|int|string $modulus ) : GMP

以下几行。

$calculatedSecretKeyA = gmp_powm($gen, $saltA, $mod);

$calculatedSecretKeyB = gmp_powm($gen, $saltB, $mod);

$calcKeyA = gmp_powm($calculatedSecretKeyB, $saltA, $mod);
    
$calcKeyB = gmp_powm($calculatedSecretKeyA, $saltB, $mod);

它使用modular form of square-and-multiply technique。中间值永远不会超过 mod^2。此外,它具有 O(log n) 复杂性。