Consider Galois field GF(2^m) .Why does the following quadratic equation tour the entire field?

45 Views Asked by At
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
int cmp(const void *a, const void *b)
{
   unsigned long A=*(unsigned long *) a;
   unsigned long B=*(unsigned long *) b;
   if( A < B) return (-1);
   else if (A > B) return 1;
   else return (0);
};
int main(int argc,char **argv)
{
   {unsigned int seed= (argc==1)?1:atoi(argv[1]);srand(seed);}
   int n = 24;/* will theoretically work for n<= 64,#number bits in unsigned long but takes too long*/
   assert( n < sizeof(unsigned long)*8);
   unsigned long N= (1<<n);  // N  a power of 2  */
   unsigned long M= N-1; // mod N,  and it  with  M
   fprintf(stdout,"N=%lu M=%lu,  bits in N =%lu\n",N,M,sizeof(unsigned long)*8);
   unsigned  long r0= rand() & M;
   unsigned  long q= (rand() & M) | 1; // or with 1 to make sure its odd
   fprintf(stdout,"r0=%lu q=%lu\n",r0,q);
   unsigned long r,a;
   unsigned long *P=calloc(N,sizeof(unsigned long ));
   assert( P != 0); // allocation success required
   long i;
   for (a=0,r=r0, i=0;i<N;i++ , a+=q, r=(r + a) & M)   P[i]=r;
   qsort(P,N,sizeof(unsigned long),cmp);
   for (i=0;i<N;i++) assert( P[i] == i);
   free(P);
}
1

There are 1 best solutions below

1
On

The main code is there : $q,r_0$ are random, $a_{i+1} = a_i + q= i q, r_{i+1} = (r_i+a)\ \ \land \ \ 2^N-1$ (where $\land$ is bitwise and).

Thus $$r_{i+1} = r_i+a r_i= r_i+iq = r_i+q \frac{i(i+1)}{2}=r_0+q\frac{i(i+1)(i+2)}{6} \bmod 2^N$$

At the end the program checks if $\{r_i\}$ contains all the integers $\bmod 2^N$.