这更像是一个运行时和/或时间复杂性问题,而不是密码学问题,所以请继续阅读:
在大学里,我们必须实现一个蛮力离散对数算法来在一个 diffie hellman 交换中找到秘密,我开始用 C++ 和 NTL 库来实现它,所以我不必担心数据类型和大素数。
我的示例数字如下,带有 25 位素数,我们想要找到离散对数:
p = 20084173; /* the prime */
g = 2; /* the generator */
a = 7709318; /* the logarithm of a */
b = 6676335; /* the logarithm of b */
我在C++ 中使用 NTL实现了以下内容:
int main() {
ZZ p, g, a, b;
// 25 Bit
p = 20084173;
g = 2;
a = 7709318;
b = 6676335;
exhaustiveSearch(p, g, a);
exhaustiveSearch(p, g, b);
return 0;
}
ZZ exhaustiveSearch(ZZ p, ZZ g, ZZ t) {
ZZ i, x;
i = 0;
x = 1;
while ((x = (x % p)) != t) {
i++;
x = x * g;
}
cout << endl << endl << "p = " << p << endl << "g = " << g << endl << "t = " << t << endl << "secret: = " << i << endl;
return i;
}
输出(7.581s):
p = 20084173
g = 2
t = 7709318
secret: = 557254
p = 20084173
g = 2
t = 6676335
secret: = 8949383
-> time: 7.581s
好吧,我认为这真的很长,所以我在没有 NTL 和 C++ 中的普通 long 的情况下对其进行了测试 -> 想象所有 ZZ 都被 long (0.124s) 取代:
p = 20084173
g = 2
t = 7709318
Secret: = 557254
p = 20084173
g = 2
t = 6676335
Secret: = 8949383
-> time: 0.124s
有人可以解释一下为什么 NTL 慢得多吗?请记住,我不是该领域的专家,我只是想弄清楚密码学基础知识以及如何在简单的示例中实现这些基础知识。
谢谢!