如何得到GF(2^n)中元素的整数表示

NTL - how to get the integer representation of an element in GF(2^n)

本文关键字:元素 表示 整数 何得 GF      更新时间:2023-10-16

是否有方法获得该系数向量的整数表示?也就是说,最高次系数是该整数的MSB而x^0的系数是LSB?当使用BytesFromGF2X方法时,它会导致一个奇怪的表示,既不是大端序也不是小端序。

例如,如果元素是x^23+x^20+x+1,那么我想得到的是整数:2^23+2^20+2+1。

使用这两个方法来来回转换为小端序整数表示:

FROM GF2X TO LITTLE ENDIAN INTEGER

void MyBytesFromGF2X(unsigned char* buffer, NTL::GF2X& p, int numbytes) {
    int degree = NTL::deg(p);
    memset(buffer,0,numbytes);
    for(int i=0; i<=degree; i++) {
        if(NTL::IsOne(NTL::coeff(p,i))) {
            buffer[i/8] |= 1 << i%8;
        }
    }
}

buffer的末尾,p以正常的小端方式表示数字。

如果你想得到整数,那么假设p的最大度数是32,然后你做以下操作:

使用

unsigned char buffer[4];
int *integer = buffer;
NTL::GF2X p;
NTL::SetCoeff(p,1,1); // set the coefficient of x^1 to 1
NTL::SetCoeff(p,30,1); // set the coefficient of x^30 to 1
MyBytesFromGF2X(buffer,p,4);
printf("%d",integer);
//result will be LE integer representation of 2^30+2^1

为了转换回GF2X,您可以使用以下命令:

从小端整数到GF2X

void MyBytesToGF2X(const unsigned char* buffer, NTL::GF2X& p, int numbytes) {
    for(int i=0; i<numbytes; i++) {
        for(int j=0; j<8; j++) {
            int bit = (buffer[i]>>j)&1;
            NTL::SetCoeff(p, i*8+j, bit);
        }
    }
}

这个怎么样:

GF2X P;
SetCoeff(P, 0, 1);
SetCoeff(P, 20, 1);
SetCoeff(P, 23, 1);
ZZ number;
clear(number);
long degree = NTL::deg(P);
for(long i = 0; i <= degree; ++i)
    number += conv<ZZ>(P[i]) * power2_ZZ(i);

注意:P看起来像一个大小为24的数组,如果你打印它。但事实并非如此。它总是被打印为一个系数列表,这样最高的一个是1。但是P是知道每个高度数的系数都是0。