MTGP  1.1.1
mtgp-calc-jump.hpp
Go to the documentation of this file.
1 #ifndef MTGP_CALC_JUMP_HPP
2 #define MTGP_CALC_JUMP_HPP
3 
24 #include <iostream>
25 #include <iomanip>
26 #include <sstream>
27 #include <NTL/GF2X.h>
28 
34 static inline void polytostring(std::string& x, NTL::GF2X& polynomial)
35 {
36  using namespace NTL;
37  using namespace std;
38 
39  long degree = deg(polynomial);
40  int buff = 0;
41  int index = 0;
42  stringstream ss;
43  for (int i = 0; i <= degree; i++) {
44  if (index < i / 4) {
45  ss << hex << buff;
46  buff = 0;
47  index = i / 4;
48  }
49  if (IsOne(coeff(polynomial, i))) {
50  buff |= 1 << (i % 4);
51  }
52  }
53  if (buff != 0) {
54  ss << hex << buff;
55  }
56  ss << flush;
57  x = ss.str();
58 }
59 
67 static inline void polytoarray(uint32_t array[],
68  int size,
69  NTL::GF2X& polynomial)
70 {
71  using namespace NTL;
72  using namespace std;
73 
74  long degree = deg(polynomial);
75  if (size == 0) {
76  return;
77  } else if (size < degree / 32 + 1) {
78  array[0] = 0;
79  return;
80  }
81  for (int i = 0; i < size; i++) {
82  array[i] = 0;
83  }
84  for (int i = 0; i <= degree; i++) {
85  int index = i / 32;
86  int pos = i % 32;
87  if (IsOne(coeff(polynomial, i))) {
88  array[index] |= 1 << pos;
89  }
90  }
91 }
92 
98 static inline void stringtopoly(NTL::GF2X& poly, std::string& str)
99 {
100  using namespace NTL;
101  using namespace std;
102 
103  stringstream ss(str);
104  char c;
105  long p = 0;
106  clear(poly);
107  while(ss) {
108  ss >> c;
109  if (!ss) {
110  break;
111  }
112  if (c >= 'a') {
113  c = c - 'a' + 10;
114  } else {
115  c = c - '0';
116  }
117  for (int j = 0; j < 4; j++) {
118  if (c & (1 << j)) {
119  SetCoeff(poly, p, 1);
120  } else {
121  SetCoeff(poly, p, 0);
122  }
123  p++;
124  }
125  }
126 }
127 
135 static inline void calc_jump(uint32_t array[],
136  int size,
137  NTL::ZZ& step,
138  NTL::GF2X& characteristic)
139 {
140  using namespace NTL;
141  using namespace std;
142  GF2X jump;
143  PowerXMod(jump, step, characteristic);
144  polytoarray(array, size, jump);
145 }
146 
153 static inline void calc_jump(std::string& jump_str,
154  NTL::ZZ& step,
155  NTL::GF2X& characteristic)
156 {
157  using namespace NTL;
158  using namespace std;
159  GF2X jump;
160  PowerXMod(jump, step, characteristic);
161  polytostring(jump_str, jump);
162 }
163 
164 #endif