|
| 1 | +/* Compiling |
| 2 | +* |
| 3 | +* g++ minipoly_mt19937.c -o minipoly -lntl |
| 4 | +* |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <NTL/GF2X.h> |
| 8 | +#include <NTL/vec_GF2.h> |
| 9 | +#include <cstdlib> |
| 10 | +#include <fstream> |
| 11 | + |
| 12 | +NTL_CLIENT |
| 13 | + |
| 14 | +/* parameters for MT19937 */ |
| 15 | +#define N 624 |
| 16 | +#define M 397 |
| 17 | +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
| 18 | +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 19 | +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 20 | + |
| 21 | +static unsigned long mt[N]; /* the array for the state vector */ |
| 22 | +static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */ |
| 23 | + |
| 24 | +/* Parameter for computing the minimal polynomial */ |
| 25 | +#define MEXP 19937 /* the dimension of the state space */ |
| 26 | + |
| 27 | +GF2X phi; /* phi is the minimal polynomial */ |
| 28 | +GF2X g; /* g(t) is used to store t^J mod phi(t) */ |
| 29 | +ZZ ntl_jump_step; |
| 30 | + |
| 31 | +/* initializes mt[N] with a seed */ |
| 32 | +void init_genrand(unsigned long s) { |
| 33 | + mt[0] = s & 0xffffffffUL; |
| 34 | + for (mti = 1; mti < N; mti++) { |
| 35 | + mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti); |
| 36 | + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 37 | + /* In the previous versions, MSBs of the seed affect */ |
| 38 | + /* only MSBs of the array mt[]. */ |
| 39 | + /* 2002/01/09 modified by Makoto Matsumoto */ |
| 40 | + mt[mti] &= 0xffffffffUL; |
| 41 | + /* for >32 bit machines */ |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/* initialize by an array with array-length */ |
| 46 | +/* init_key is the array for initializing keys */ |
| 47 | +/* key_length is its length */ |
| 48 | +/* slight change for C++, 2004/2/26 */ |
| 49 | +void init_by_array(unsigned long init_key[], int key_length) { |
| 50 | + int i, j, k; |
| 51 | + init_genrand(19650218UL); |
| 52 | + i = 1; |
| 53 | + j = 0; |
| 54 | + k = (N > key_length ? N : key_length); |
| 55 | + for (; k; k--) { |
| 56 | + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL)) + |
| 57 | + init_key[j] + j; /* non linear */ |
| 58 | + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 59 | + i++; |
| 60 | + j++; |
| 61 | + if (i >= N) { |
| 62 | + mt[0] = mt[N - 1]; |
| 63 | + i = 1; |
| 64 | + } |
| 65 | + if (j >= key_length) |
| 66 | + j = 0; |
| 67 | + } |
| 68 | + for (k = N - 1; k; k--) { |
| 69 | + mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL)) - |
| 70 | + i; /* non linear */ |
| 71 | + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 72 | + i++; |
| 73 | + if (i >= N) { |
| 74 | + mt[0] = mt[N - 1]; |
| 75 | + i = 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ |
| 80 | +} |
| 81 | + |
| 82 | +/* generates a random number on [0,0xffffffff]-interval */ |
| 83 | +unsigned long genrand_int32(void) { |
| 84 | + unsigned long y; |
| 85 | + static unsigned long mag01[2] = {0x0UL, MATRIX_A}; |
| 86 | + /* mag01[x] = x * MATRIX_A for x=0,1 */ |
| 87 | + |
| 88 | + if (mti >= N) { /* generate N words at one time */ |
| 89 | + int kk; |
| 90 | + |
| 91 | + if (mti == N + 1) /* if init_genrand() has not been called, */ |
| 92 | + init_genrand(5489UL); /* a default initial seed is used */ |
| 93 | + |
| 94 | + for (kk = 0; kk < N - M; kk++) { |
| 95 | + y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 96 | + mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 97 | + } |
| 98 | + for (; kk < N - 1; kk++) { |
| 99 | + y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 100 | + mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 101 | + } |
| 102 | + y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
| 103 | + mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 104 | + |
| 105 | + mti = 0; |
| 106 | + } |
| 107 | + |
| 108 | + y = mt[mti++]; |
| 109 | + |
| 110 | + /* Tempering */ |
| 111 | + y ^= (y >> 11); |
| 112 | + y ^= (y << 7) & 0x9d2c5680UL; |
| 113 | + y ^= (y << 15) & 0xefc60000UL; |
| 114 | + y ^= (y >> 18); |
| 115 | + |
| 116 | + return y; |
| 117 | +} |
| 118 | + |
| 119 | +/* computes the minimal polynomial of the linear recurrence */ |
| 120 | +void comp_mini_poly(void) { |
| 121 | + int i; |
| 122 | + vec_GF2 v(INIT_SIZE, 2 * MEXP); |
| 123 | + |
| 124 | + for (i = 0; i < 2 * MEXP; i++) |
| 125 | + v[i] = genrand_int32() & 0x01ul; |
| 126 | + |
| 127 | + MinPolySeq(phi, v, MEXP); |
| 128 | +} |
| 129 | + |
| 130 | +/* computes the t^J mod phi(t) */ |
| 131 | +void comp_jump_rem(ZZ jump_step) /*(long jump_step)*/ |
| 132 | +{ |
| 133 | + /* changed by saito 2013.1.25 */ |
| 134 | + // GF2X f; |
| 135 | + // SetCoeff (f, jump_step, 1); |
| 136 | + // g = f % phi; |
| 137 | + PowerXMod(g, jump_step, phi); |
| 138 | + /* changed by saito 2013.1.25 */ |
| 139 | +} |
| 140 | + |
| 141 | +/* computes the t^J mod phi(t) */ |
| 142 | +void comp_jump_rem_ulong(unsigned long jump_step) /*(long jump_step)*/ |
| 143 | +{ |
| 144 | + /* changed by saito 2013.1.25 */ |
| 145 | + // GF2X f; |
| 146 | + // SetCoeff (f, jump_step, 1); |
| 147 | + // g = f % phi; |
| 148 | + PowerXMod(g, jump_step, phi); |
| 149 | + /* changed by saito 2013.1.25 */ |
| 150 | +} |
| 151 | + |
| 152 | +int main(void) { |
| 153 | + int i, a = 0; |
| 154 | + long jump_step = 2147483647; /* the number of steps of jumping ahead */ |
| 155 | + unsigned long init[4] = {0x123, 0x234, 0x345, 0x456}, length = 4; |
| 156 | + ofstream fout; |
| 157 | + |
| 158 | + init_by_array(init, length); |
| 159 | + |
| 160 | + comp_mini_poly(); |
| 161 | + conv(ntl_jump_step, "340282366920938463463374607431768211456"); /* 2 ^ 128 */ |
| 162 | + comp_jump_rem(ntl_jump_step); |
| 163 | + // comp_jump_rem_ulong(jump_step); |
| 164 | + fout.open("clist_mt19937.txt", ios::out); |
| 165 | + if (!fout) |
| 166 | + return -1; |
| 167 | + |
| 168 | + for (i = MEXP - 1; i > -1; i--) |
| 169 | + fout << coeff(g, i); |
| 170 | + |
| 171 | + fout.close(); |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
0 commit comments