有没有办法获得该系数向量的整数表示?即以某种方式,最高度系数是该整数的 MSB,而 x^0 的系数是 LSB?当使用 BytesFromGF2X 方法时,它会产生一种既不是大端也不是小端的奇怪表示。
例如,如果元素是 x^23+x^20+x+1,那么我想得到整数:2^23+2^20+2+1。
使用这两种方法来回转换为小端整数表示:
从 GF2X 到小端整数
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
知道更高度数的每个系数都为零。