1 #ifndef MTTOOLBOX_ALGORITHM_CALCULATE_PARITY_HPP
2 #define MTTOOLBOX_ALGORITHM_CALCULATE_PARITY_HPP
67 #include <NTL/vec_GF2.h>
68 #include <NTL/mat_GF2.h>
90 template<
typename U,
typename G>
111 int mexp = g.getMexp();
113 cout <<
"searchParity start" << endl;
114 cout <<
"degree of f = " << deg(f) << endl;
116 int maxdegree = g.bitSize();
117 word_width = bit_size<U>();
118 int base_num = maxdegree - mexp;
119 internal_state bases[word_width];
120 internal_state work_base;
124 for (
int i = 0; i < word_width; i++) {
125 bases[i].rg =
new G(g);
126 bases[i].rg->setZero();
127 bases[i].zero =
true;
130 work_base.rg =
new G(g);
131 work_base.rg->setZero();
132 work_base.zero =
true;
135 cout <<
"searchParity before while loop" << endl;
136 cout <<
"word_width = " << word_width << endl;
137 cout <<
"bit_pos = " << bit_pos << endl;
138 cout <<
"maxdegree = " << maxdegree << endl;
139 cout <<
"base_num = " << base_num << endl;
141 while(bit_pos < maxdegree) {
142 calc_basis(work_base, f, &bit_pos);
143 addBase(bases, word_width, work_base);
145 for (
int i = 0; i < word_width; i++) {
146 if (!
isZero(bases[i].next)) {
151 cout <<
"in while loop count = " << count << endl;
153 if (count >= base_num) {
158 cout <<
"searchParity after while loop" << endl;
160 for (
int i = 0; i < word_width; i++) {
164 cout <<
"----" << endl;
165 for (
int i = 0; i < word_width; i++) {
166 if (
isZero(bases[i].next)) {
169 int w = word_width / 4;
170 cout << setw(2) << dec << i <<
" ";
171 cout << hex << setfill(
'0') << setw(w) << bases[i].next << endl;
174 cout <<
"----" << endl;
176 U parity = search_parity_check_vector(bases, base_num);
177 g.setParityValue(parity);
183 struct internal_state {
202 void calc_basis(internal_state& st,
const NTL::GF2X& f,
int *bit_pos) {
204 cout <<
"calc_basis start bit_pos = " << dec << *bit_pos << endl;
206 int maxdegree = st.rg->bitSize();
207 for (;*bit_pos < maxdegree;) {
209 cout <<
"in calc_basis bit_pos = " << dec << *bit_pos << endl;
212 st.rg->setOneBit(*bit_pos);
214 if (st.rg->isZero()) {
215 cout <<
"ERROR rg is ZERO" << endl;
222 if (st.rg->isZero()) {
223 cout <<
"ZERO after annihilate" << endl;
224 cout <<
"deg(f) = " << deg(f) << endl;
226 cout <<
"NON ZERO after annihilate" << endl;
235 cout <<
"calc_basis end bit_pos = " << dec << *bit_pos << endl;
250 void set_state(internal_state& st) {
252 cout <<
"set_state start" << endl;
254 if (st.rg->isZero()) {
258 cout <<
"set_state end zero" << endl;
264 st.next = st.rg->getParityValue();
266 if (st.rg->isZero()) {
271 st.next = st.rg->getParityValue();
274 cout <<
"set_state end parity = "
275 << hex << setfill(
'0') << setw(8) << st.next << dec << endl;
292 void add_state(internal_state& dist,
const internal_state& src) {
293 dist.rg->add(*src.rg);
294 dist.next ^= src.next;
308 void get_next_state(internal_state& st) {
333 void addBase(internal_state bases[],
int size, internal_state& work) {
335 cout <<
"addBase start" << endl;
337 int count = bit_size<U>() * 10;
341 cout <<
"addBase work.next = " << hex << work.next << endl;
344 get_next_state(work);
347 cout <<
"addBase end work.zero" << endl;
354 cout <<
"addBase pivot = " << dec << pivot << endl;
357 cout <<
"pivot > size error pivot = " << dec << pivot
358 <<
" size = " << dec << size << endl;
359 throw "pivot > size error";
361 if (
isZero(bases[pivot].next)) {
362 add_state(bases[pivot], work);
364 cout <<
"addBase end next == 0" << endl;
368 add_state(work, bases[pivot]);
371 cout <<
"addBase end" << endl;
391 U search_parity_check_vector(internal_state base[],
int size) {
393 cout <<
"search_parity_check_vector start" << endl;
398 mx.SetDims(word_width, size);
400 int pos = bit_size<U>() - 1;
403 for (
int i = 0; i < word_width; i++) {
405 for (
int j = 0; j < word_width; j++) {
406 if (
isZero(base[j].next)) {
409 if (!
isZero(mask & base[j].next)) {
421 if (my.NumRows() == 0) {
422 cout <<
"parity vector can't find" << endl;
423 throw "parity vector can't find";
426 cout <<
"dim mx = "<< mx.NumRows() << endl;
427 cout <<
"-----" << endl;
428 for (
int i = 0; i < mx.NumRows(); i++) {
429 for (
int j = 0; j < mx.NumCols(); j++) {
430 cout << mx.get(i, j);
434 cout <<
"dim kernel = "<< my.NumRows() << endl;
435 cout <<
"-----" << endl;
436 for (
int i = 0; i < my.NumRows(); i++) {
437 for (
int j = 0; j < my.NumCols(); j++) {
438 cout << my.get(i, j);
442 cout <<
"search_parity_check_vector end" << endl;
444 return fromGF2Vec<U>(my[0]);
449 #endif // MTTOOLBOX_ALGORITHM_CALCULATE_PARITY_HPP