在NTL中生成$ GF(2 ^ x)$中的随机元素

时间:2014-11-03 22:07:03

标签: ntl finite-field

我正在尝试研究与有限域算术相关的部分NTL功能,但有些奇怪的事情正在发生。我试图在$ GF(2 ^ 8)$字段中生成2个随机元素,并使用它们进行加法和减法。但似乎我获得的两个“随机”元素在每次执行测试程序时都是相同的。你有任何想法吗?

我的测试代码:

void test3(long n) {
    NTL::GF2X P;
    NTL::BuildIrred(P, n);
    // P is now x^2+x+1, this is irreducable since P(1)=1 and P(0)=1
    NTL::GF2E::init(P);

    NTL::GF2E xx = NTL::random_GF2E();
    NTL::GF2E yy = NTL::random_GF2E();

    std::cout << "xx: " << xx << std::endl; // Prints something like "[0 1]"
    std::cout << "yy: " << yy << std::endl; // Prints something like "[0 1]"

    xx += yy;
    std::cout << "xx: " << xx << std::endl; // Prints something like "[0 1]"
    xx -= yy;
    std::cout << "xx: " << xx << std::endl; // Prints something like "[0 1]"
    xx -= yy;
    std::cout << "xx: " << xx << std::endl; // Prints something like "[0 1]"
}

多次运行时测试程序的输出:

~\Release>test.exe
xx: [0 1 0 0 1 0 0 1]
yy: [0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]
xx: [0 1 0 0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]

~\Release>test.exe
xx: [0 1 0 0 1 0 0 1]
yy: [0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]
xx: [0 1 0 0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]

~\Release>test.exe
xx: [0 1 0 0 1 0 0 1]
yy: [0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]
xx: [0 1 0 0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]

~\Release>test.exe
xx: [0 1 0 0 1 0 0 1]
yy: [0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]
xx: [0 1 0 0 1 0 0 1]
xx: [0 0 0 0 0 0 0 1]

1 个答案:

答案 0 :(得分:1)

NTL中使用随机性的函数正在使用NTL/ZZ (Pseudo-Random Numbers)中可以找到的随机数生成器。

因此,如果您希望扩展字段中包含不同的随机元素,则必须先为随机数生成器设置种子。如果不这样做,种子总是相同的,所以你会得到相同的元素序列。

您可以按如下方式设置种子:

NTL::SetSeed(conv<ZZ>((long) time(0)));

注意:为此,您必须#include <time.h>

这只是一个建议。您也可以使用rand()或任何其他数字作为种子。