// ************************************************ // ************************************************ // Fast RS on GF(2^16+1) // (c) 2009 Frederic Didier. #include using namespace std; #include "stdio.h" #include "stdlib.h" #include "time.h" typedef unsigned char byte; /************************************************/ /** Random number generator -> 32bits **/ /** Mersenne twister code **/ /************************************************/ /* A C-program for MT19937: Integer version */ /* genrand() generates one pseudorandom unsigned integer (32bit) */ /* which is uniformly distributed among 0 to 2^32-1 for each */ /* call. sgenrand(seed) set initial values to the working area */ /* of 624 words. Before genrand(), sgenrand(seed) must be */ /* called once. (seed is any 32-bit integer except for 0). */ /* Coded by Takuji Nishimura, considering the suggestions by */ /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Library General Public */ /* License as published by the Free Software Foundation; either */ /* version 2 of the License, or (at your option) any later */ /* version. */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ /* See the GNU Library General Public License for more details. */ /* You should have received a copy of the GNU Library General */ /* Public License along with this library; if not, write to the */ /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ /* 02111-1307 USA */ /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */ /* Any feedback is very welcome. For any question, comments, */ /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ /* matumoto@math.keio.ac.jp */ /* Period parameters */ #define MT_N 624 #define MT_M 397 #define MATRIX_A 0x9908b0df /* constant vector a */ #define UPPER_MASK 0x80000000 /* most significant w-r bits */ #define LOWER_MASK 0x7fffffff /* least significant r bits */ /* Tempering parameters */ #define TEMPERING_MASK_B 0x9d2c5680 #define TEMPERING_MASK_C 0xefc60000 #define TEMPERING_SHIFT_U(y) (y >> 11) #define TEMPERING_SHIFT_S(y) (y << 7) #define TEMPERING_SHIFT_T(y) (y << 15) #define TEMPERING_SHIFT_L(y) (y >> 18) static unsigned long mt[MT_N]; /* the table for the state vector */ static int mti=MT_N+1; /* mti==MT_N+1 means mt[MT_N] is not initialized */ /* initializing the table with a NONZERO seed */ void sgenrand(unsigned long seed) { /* setting initial seeds to mt[MT_N] using */ /* the generator Line 25 of Table 1 in */ /* [KNUTH 1981, The Art of Computer Programming */ /* Vol. 2 (2nd Ed.), pp102] */ mt[0]= seed & 0xffffffff; for (mti=1; mti= MT_N) { /* generate MT_N words at one time */ int kk; if (mti == MT_N+1) /* if sgenrand() has not been called, */ sgenrand(4357); /* a default initial seed is used */ for (kk=0;kk> 1) ^ mag01[y & 0x1]; } for (;kk> 1) ^ mag01[y & 0x1]; } y = (mt[MT_N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y & 0x1]; mti = 0; } y = mt[mti++]; y ^= TEMPERING_SHIFT_U(y); y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; y ^= TEMPERING_SHIFT_L(y); return y; } double double_genrand() { return genrand() * (1.0/4294967295.0); } // ******************************************************* // The Art of Computer programming - Knuth // vol 2 - section 3.4.2 page 137 // Algorithm S (Selection sampling technique) void generate_positions(int N, int K, int *pos) { int size=N; int w=K; do { if (double_genrand()*size <= w) { pos[K-w] = N-size; w--; } size--; } while (size); } void generate_message(byte *data, int size) { int *p = (int *)data; size >>=2; int i; for (i=0; i>=2; int res=0; int i; for (i=0; i>16); a += (((int)a)>>31) & GF; return a; } int inline field_mult(unsigned int a, unsigned int b) { if (a==(1<<16)) return -(int)b + (((-(int)b)>>31) & GF); return reduce(a*b); } // this one work if not both a and b are (1<<16) that is -1. int inline field_mult_no(unsigned int a, unsigned int b) { return reduce(a*b); } int inline field_diff(unsigned int a, unsigned int b) { a -= b; return a + ((((int)a)>>31)&GF); } int inline field_sum(unsigned int a, unsigned int b) { a -= GF-b; return a + ((((int)a)>>31)&GF); } void init_field() { GF_log = (int *) malloc(sizeof(int) * GF); GF_exp = (int *) malloc(sizeof(int) * GF); inv = (int *) malloc(sizeof(int) * GF); int p = 1; int i; for (i=0; i+1>(i+1)) i++; return i; } void reverse(int *vect, int n) { int i,j; j=n >> 1; for (i=1; i i) { int temp=vect[i]; vect[i]=vect[j]; vect[j]=temp; } int m = n >> 1; while (m >= 1 && (j & m)) { j ^= m; m >>= 1; } j += m; } } void fft_dit(int *vect, int n) { reverse(vect, n); int i,j; int step=1; int number=n/2; int mult=15; while (number>0) { int *p=vect; int *q=vect+step; for (i=0; i>=1; mult--; } } void ifft_dit(int *vect, int n) { reverse(vect, n); int i,j; int step=1; int number=n/2; int mult=15; while (number>0) { int *p=vect; int *q=vect+step; for (i=0; istep; j--) { int a = *p; int b = field_mult_no(*q, GF_exp[j<>=1; mult--; } } void fft2(int *vect, int n) { int i=n/2; while (i--) { int a = *vect; int b = *(vect+1); *(vect++) = field_sum(a,b); *(vect++) = field_diff(a,b); } } // decimation in frequency void fft_inc(int *vect, int n) { int i,j; int number=1; int step=n/2; int mult = 16 - get_log(n); while (step>0) { int *p=vect; int *q=vect+step; for (i=0; i>=1; number<<=1; mult++; } } // decimation in time void ifft_inc(int *vect, int n) { int i,j; int number=n/2; int step=1; int mult=15; int *root=GF_exp + (1<<16); while (number>0) { int *p=vect; int *q=vect+step; for (i=0; i>=1; mult--; } } void fft_rec(int *vect, int n) { if (n == 1<<11) return fft_inc(vect, n); /* if (n==2) { int a = vect[0]; int b = vect[1]; vect[0] = field_sum(a,b); vect[1] = field_diff(a,b); return; } */ int i; int mult = 16 - get_log(n); n/=2; int *l = vect; int *h = vect + n; for (i=0; i* X, int n) { if (n==1) return; for (int i=0; i a = X[i]; complex b = X[n/2 + i]; X[i] = a + b; X[n/2 + i] = (a - b) * polar(1.0, -2*Pi*i/double(n)); } complex_fft_rec(X, n/2); complex_fft_rec(X + n/2, n/2); } // decimation in time // input is bit reversed. void complex_ifft_rec(complex* X, int n) { if (n==1) return; complex_ifft_rec(X, n/2); complex_ifft_rec(X + n/2, n/2); for (int i=0; i a = X[i]; complex b = X[n/2 + i] * polar(1.0, 2*Pi*i/double(n)); X[i] = a + b; X[n/2 + i] = (a - b); } } // The memory can be allocated only once if many call to this // function are expected. the fft of the log can be precomputed. void compute_prod_fast(int *prod, int *pos, int k, int n) { const int NN = 2*n; complex* R = (complex *)malloc(NN * sizeof(complex)); complex* L = (complex *)malloc(NN * sizeof(complex)); for (int i=0; i0) { L[NN - i] = GF_log[GF - i]; } } // convolution complex_fft_rec(R, NN); complex_fft_rec(L, NN); for (int i=0; i < NN; ++i) { R[i] *= L[i]; } complex_ifft_rec(R, NN); // now we have the GF_log(prod[i]) in Re(R[i]) // we take the result mod 2^16 since we are in the multiplicative // field of GF(2^16+1) for (int x=0; x < n; ++x) { prod[x] = GF_exp[((long long) (real(R[x])/double(NN) + 0.5)) % (1<<16)]; } free(R); free(L); } //********************************************************************* //********************************************************************* int *high; int *low; int *prod; int *enc_fft; int *rev_fft; int *mid_fft; int *prod_enc; void init_code(int n) { low = (int *) malloc(sizeof(int) * n); high = (int *) malloc(sizeof(int) * n); prod = (int *) malloc(sizeof(int) * n); prod_enc = (int *) malloc(sizeof(int) * n); enc_fft = (int *) malloc(sizeof(int) * n); rev_fft = (int *) malloc(sizeof(int) * n); mid_fft = (int *) malloc(sizeof(int) * n); int x; for (x=0; x=n/2 and we don't need the fist half of the output fft(dst, n); for (x=0; x n/2 if (k > n/2) { for (i=0; i