MTToolBox  0.2.10
AlgorithmRecursionAndTempering.hpp
Go to the documentation of this file.
1 #ifndef MTTOOLBOX_ALGORITHM_RECURSION_AND_TEMPERING_HPP
2 #define MTTOOLBOX_ALGORITHM_RECURSION_AND_TEMPERING_HPP
3 
33 #include <iostream>
34 #include <ostream>
35 #include <iomanip>
36 #include <vector>
37 #include <cstdlib>
38 #include <cerrno>
39 #include <unistd.h>
40 #include <time.h>
45 #include <MTToolBox/util.hpp>
46 
47 namespace MTToolBox {
80  template<typename U, typename V = U>
82  public:
104  const AlgorithmPrimitivity& primitivity = MersennePrimitivity) {
105  baseGenerator = &bg;
106  isPrime = &primitivity;
107  }
139  bool verbose = false,
140  std::ostream& os = std::cout,
141  bool no_lsb = false) {
142  using namespace NTL;
143  using namespace std;
144 
145  out = &os;
146  int veq[bit_size<U>()];
147  AlgorithmRecursionSearch<U, V> search(lg, *baseGenerator, *isPrime);
148  int mexp = lg.bitSize();
149  bool found = false;
150  for (int i = 0;; i++) {
151  if (search.start(1000 * mexp)) {
152  found = true;
153  break;
154  }
155  if (verbose) {
156  *out << "not found in " << (i + 1) * 10000 << endl;
157  }
158  }
159  if (!found) {
160  return false;
161  }
162  if (verbose) {
163  time_t t = time(NULL);
164  *out << "irreducible parameter is found at " << ctime(&t);
165  }
166  if (verbose) {
167  *out << "count = " << search.getCount() << endl;
168  *out << lg.getParamString() << endl;
169  }
170  poly = search.getMinPoly();
171  weight = NTL::weight(poly);
172  if (verbose) {
173  AlgorithmEquidistribution<U, V> sb(lg, bit_size<U>());
174  int delta = sb.get_all_equidist(veq);
175  print_kv(veq, mexp, bit_size<U>());
176  *out << "delta = " << dec << delta << endl;
177  }
178  if (! no_lsb) {
179  st2(lg, verbose);
180  if (verbose) {
181  if (st2.isLSBTempering()) {
182  lg.setReverseOutput();
183  }
184  AlgorithmEquidistribution<U, V> sc(lg, bit_size<U>());
185  delta = sc.get_all_equidist(veq);
186  lg.resetReverseOutput();
187  time_t t = time(NULL);
188  *out << "lsb tempering parameters are found at "
189  << ctime(&t) << endl;
190  print_kv(veq, mexp, bit_size<U>());
191  *out << "lsb delta = " << dec << delta << endl;
192  }
193  }
194  st1(lg, verbose);
195  AlgorithmEquidistribution<U, V> sc(lg, bit_size<U>());
196  delta = sc.get_all_equidist(veq);
197  if (verbose) {
198  time_t t = time(NULL);
199  *out << "tempering parameters are found at " << ctime(&t)
200  << endl;
201  *out << lg.getParamString() << endl;
202  print_kv(veq, mexp, bit_size<U>());
203  *out << "delta = " << dec << delta << endl;
204  }
205  return true;
206  }
207 
239  bool verbose = false,
240  std::ostream& os = std::cout) {
241  return search(lg, st, st, verbose, os, true);
242  }
243 
256  int getWeight() {
257  return weight;
258  }
259 
272  int getDelta() {
273  return delta;
274  }
275 
287  const NTL::GF2X& getCharacteristicPolynomial() {
288  return poly;
289  }
290  private:
291  int weight;
292  int delta;
293  NTL::GF2X poly;
294  std::ostream * out;
295  AbstractGenerator<V> * baseGenerator;
297  void print_kv(int veq[], int mexp, int size) {
298  using namespace std;
299  for (int i = 0; i < size; i++) {
300  *out << dec << i + 1 << ":" << veq[i]
301  << "(" << mexp / (i + 1) - veq[i] << ")"
302  << endl;
303  }
304  }
305  };
306 
307 }
308 #endif //MTTOOLBOX_ALGORITHM_RECURSION_AND_TEMPERING_HPP
Abstruct class for searching tempering parameters.
virtual void resetReverseOutput()=0
Reset bit order of output.
bool search(TemperingCalculatable< U, V > &lg, AlgorithmTempering< U, V > &st, bool verbose=false, std::ostream &os=std::cout)
Simple wrapper for users who want to search tempering parameter only from MSB.
Definition: AlgorithmRecursionAndTempering.hpp:237
bool search(TemperingCalculatable< U, V > &lg, AlgorithmTempering< U, V > &st1, AlgorithmTempering< U, V > &st2, bool verbose=false, std::ostream &os=std::cout, bool no_lsb=false)
Search parameters for state transition function and parameters for tempering.
Definition: AlgorithmRecursionAndTempering.hpp:136
Algorithm which check if given polynomial is primitive.
Definition: AlgorithmPrimitivity.hpp:42
const AlgorithmPrimitivity MersennePrimitivity
An algorithm which checks if given polynomial is a primitive polynomial of given degree for pseudo ra...
virtual void setReverseOutput()=0
Changes bit order of output.
virtual int bitSize() const =0
Return bit size of internal state, i.e dimension of GF(2)-vector space.
Definition: AlgorithmRecursionAndTempering.hpp:81
Search parameters of state transition function.
Users can search tempering parameters by making GF(2)-linear pseudo random generator class which inhe...
Definition: TemperingCalculatable.hpp:54
Search parameters of state transition function of pseudo random number generator so that the characte...
Definition: AlgorithmRecursionSearch.hpp:74
const NTL::GF2X & getCharacteristicPolynomial()
Returns characteristic polynomial of state transition function.
Definition: AlgorithmRecursionAndTempering.hpp:287
Calculate dimension of equi-distribution of output of pseudo random number generators.
Definition: AlgorithmEquidistribution.hpp:238
Utility functions.
int get_all_equidist(int veq[])
Calculate dimension of equi-distribution with v-bit accuracy.
Definition: AlgorithmEquidistribution.hpp:470
int getDelta()
returns sum of d(v)s, which are difference between k(v) and theoretical upper bound.
Definition: AlgorithmRecursionAndTempering.hpp:272
Calculate dimension of equi-distribution of output of pseudo random number generators.
Abstract class for searching tempering parameters.
virtual const std::string getParamString()=0
Returns string expression of parameters.
int getWeight()
Returns Hamming weight of characteristic polynomial of state transition function. ...
Definition: AlgorithmRecursionAndTempering.hpp:256
AlgorithmRecursionAndTempering(AbstractGenerator< V > &bg, const AlgorithmPrimitivity &primitivity=MersennePrimitivity)
Constructor.
Definition: AlgorithmRecursionAndTempering.hpp:103
Algorithm that search tempering parameters to improve dimension of equi-distribution of output of pse...
Definition: AlgorithmTempering.hpp:62
virtual bool isLSBTempering() const
Shows if searching tempering parameters is from LSBs.
Definition: AlgorithmTempering.hpp:107
bool isPrime(const NTL::GF2X &poly)
Check if polynomial is primitive.
name space for MTToolBox