dSFMT-jump  0.1
dSFMT-calc-jump.hpp
Go to the documentation of this file.
00001 #pragma once
00002 #ifndef DSFMT_CALC_JUMP_HPP
00003 #define DSFMT_CALC_JUMP_HPP
00004 
00019 #include <iostream>
00020 #include <iomanip>
00021 #include <sstream>
00022 #include <NTL/GF2X.h>
00023 
00024 namespace dsfmt {
00030     static inline void polytostring(std::string& x, NTL::GF2X& polynomial)
00031     {
00032         using namespace NTL;
00033         using namespace std;
00034 
00035         long degree = deg(polynomial);
00036         int buff;
00037         stringstream ss;
00038         for (int i = 0; i <= degree; i+=4) {
00039             buff = 0;
00040             for (int j = 0; j < 4; j++) {
00041                 if (IsOne(coeff(polynomial, i + j))) {
00042                     buff |= 1 << j;
00043                 } else {
00044                     buff &= (0x0f ^ (1 << j));
00045                 }
00046             }
00047             ss << hex << buff;
00048         }
00049         ss << flush;
00050         x = ss.str();
00051     }
00052 
00058     static inline void stringtopoly(NTL::GF2X& poly, std::string& str)
00059     {
00060         using namespace NTL;
00061         using namespace std;
00062 
00063         stringstream ss(str);
00064         char c;
00065         long p = 0;
00066         clear(poly);
00067         while(ss) {
00068             ss >> c;
00069             if (!ss) {
00070                 break;
00071             }
00072             if (c >= 'a') {
00073                 c = c - 'a' + 10;
00074             } else {
00075                 c = c - '0';
00076             }
00077             for (int j = 0; j < 4; j++) {
00078                 if (c & (1 << j)) {
00079                     SetCoeff(poly, p, 1);
00080                 } else {
00081                     SetCoeff(poly, p, 0);
00082                 }
00083                 p++;
00084             }
00085         }
00086     }
00087 
00095     static inline void calc_jump(std::string& jump_str,
00096                                  NTL::ZZ& step,
00097                                  NTL::GF2X& characteristic)
00098     {
00099         using namespace NTL;
00100         using namespace std;
00101         GF2X jump;
00102         PowerXMod(jump, step, characteristic);
00103         polytostring(jump_str, jump);
00104     }
00105 }
00106 #endif