MTToolBox  0.2.10
TestLinearity.hpp
Go to the documentation of this file.
1 #ifndef MTTOOLBOX_TEST_LINEARITY_HPP
2 #define MTTOOLBOX_TEST_LINEARITY_HPP
3 
36 #include <iostream>
37 #include <iomanip>
40 #include <MTToolBox/util.hpp>
41 
42 namespace MTToolBox {
80  template<typename U, typename V = U>
81  class TestLinearity {
82  public:
100  EquidistributionCalculatable<U, V> *g1 = generator.clone();
101  EquidistributionCalculatable<U, V> *g2 = generator.clone();
102  g1->seed(convert<U>(1234U));
103  g2->seed(convert<U>(4321U));
104  bool result = test1(*g1) && test2(*g1, *g2);
105  delete g1;
106  delete g2;
107  return result;
108  }
109 
110  private:
111  bool test1(EquidistributionCalculatable<U, V>& g1) {
112  using namespace std;
114  bool result = true;
115  g2->add(g1);
116  for (int i = 0; i < 100; i++) {
117  if (!isZero(g2->generate())) {
118  result = false;
119  break;
120  }
121  }
122  delete g2;
123 #if defined(DEBUG)
124  if (result) {
125  cout << "test1 passed" << endl;
126  } else {
127  cout << "test1 failed" << endl;
128  }
129 #endif
130  return result;
131  }
132 
133  bool test2(EquidistributionCalculatable<U, V>& g1,
134  EquidistributionCalculatable<U, V>& g2) {
135  using namespace std;
136  EquidistributionCalculatable<U, V> *g3 = g2.clone();
137  g3->add(g1);
138  bool result = true;
139  for (int i = 0; i < 100; i++) {
140  U res1 = g1.generate();
141  U res2 = g2.generate();
142  U res3 = g3->generate();
143  if (!((res1 ^ res2) == res3)) {
144  result = false;
145 #if defined(DEBUG)
146  cout << "i,res1,res2,res3 = " << dec << i << ","
147  << hex << res1 << ","
148  << res2 <<"," << res3 << ","
149  << (res1 ^ res2) << endl;
150 #endif
151  break;
152  }
153  }
154  delete g3;
155 #if defined(DEBUG)
156  if (result) {
157  cout << "test2 passed" << endl;
158  } else {
159  cout << "test2 failed" << endl;
160  }
161 #endif
162  return result;
163  }
164 
165  };
166 }
167 #endif // MTTOOLBOX_TEST_LINEARITY_HPP
bool operator()(const EquidistributionCalculatable< U, V > &generator)
Checks if generator is GF(2)-linear.
Definition: TestLinearity.hpp:99
virtual void add(EquidistributionCalculatable< U, V > &that)=0
Add internal state of GF(2)-linear pseudo random number generators.
Abstract class of GF(2)-linear pseudo random number generators.
This class is an Abstract class for calculating dimension of equi-distribution for GF(2)-linear pseud...
Definition: EquidistributionCalculatable.hpp:50
virtual EquidistributionCalculatable< U, V > * clone() const =0
Return copy of myself.
Utility functions.
This class is an Abstract class for calculating dimension of equi-distribution for GF(2)-linear pseud...
virtual void seed(U value)=0
initialize internal state
virtual U generate(int outBitLen)=0
output outBitLen from MSB.
Checks if a pseudo random number generator is GF(2)-linear.
Definition: TestLinearity.hpp:81
bool isZero(U x)
check if varible is zero or not
Definition: util.hpp:691
name space for MTToolBox