2015-10-19 80 views
1

有沒有辦法獲得該係數向量的整數表示?即以最高度係數是該整數的MSB並且x^0的係數是LSB?當使用BytesFromGF2X方法時,它會產生一個奇怪的表示,它既不是大端,也不是小端。例如,如果元素是x^23 + x^20 + x + 1,那麼我想要得到整數:2^23 + 2^20 + 2 + 1。NTL - 如何獲得GF(2^n)中元素的整數表示

回答

1

使用這兩種方法來來回轉換成小端整數表示:

從GF2X爲小端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你ca使用:

從小端ENDIAN INTEGER到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); 
     } 
    } 
} 
1

如何:

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

看看我的編輯。我認爲我的代碼比你所建議的更有效率。有一種方法可以獲得GF2X的程度(請參閱我的代碼)。 – Bush

+1

@布什尼斯。我沒有看到'NTL:deg'。是的,也許你的解決方案更快。我只發佈了一個可行的解決方案。我會在我的解決方案中使用NTL:deg。 – AbcAeffchen