Vous êtes sur la page 1sur 4

template <typename Uint> class LFSR { public: LFSR(Uint const& characteristic = Uint(), Uint const& initial = Uint()) { polynomial(characteristic);

value(initial); } void polynomial(Uint const& characteristic) { /* All output values are bounded by a bit-mask the "width" of the p olynomial. So if, for example, our polynomial is 4th degree then our mask i s 1111 (in binary, of course) and our output is thus limited to some value less than or equal to 15 (decimal). */ m_mask = zero; Uint temp = m_polynomial = characteristic; while(temp) { temp >>= one; m_mask <<= one; m_mask |= one; } } Uint degree(void) const { Uint count = zero, temp = m_mask; while(temp) { ++count; temp >>= one; } return count; } inline Uint polynomial(void) const { return m_polynomial; } void value(Uint const& initial) { m_value = initial; /* A zero value is forbidden (as the LFSR would enter a "locked up" state). */ if(!m_value) m_value = one; } inline void seed(Uint const& initial) { value(initial); } inline Uint value(void) const { return m_value; }

inline operator Uint(void) const { return value(); } Uint next(void) { for(Uint shifts = zero, limit = degree(); shifts <= limit; ++shi fts) { Uint feedback = zero, taps = m_value & m_polynomial; while(taps) { feedback ^= taps & one; taps >>= one; } m_value <<= one; m_value |= feedback; } m_value &= m_mask; return m_value; } inline Uint operator()(void) { return next(); } Uint period(void) const { LFSR<Uint> test(m_polynomial); Uint first = test(); Uint count = one; for(;;) { if(test() == first) break; ++count; } return count; } inline Uint maximum(void) const { return m_mask; } inline bool maximal(void) const { return period() == maximum(); } static const Uint zero, one; private: Uint m_polynomial, m_mask, m_value; }; template <typename Uint> const Uint LFSR<Uint>::zero = 0; template <typename Uint> const Uint LFSR<Uint>::one = 1; #include <iostream> #include <cstdlib> #include <sstream> using namespace std; template <typename Uint> string textual(const LFSR<Uint>& lfsr)

{ static const Uint zero = LFSR<Uint>::zero, one = LFSR<Uint>::one; string text; bool started = false; Uint polynomial = lfsr.polynomial(); for(Uint index = zero, limit = lfsr.degree(); index <= limit; ++index) { if(polynomial & (one << index)) { string append, exponent; if(index != zero) { stringstream converter; converter << (index + one); exponent = string("^") + converter.str(); } if(started) append = " + "; else started = true; text = string("x") + exponent + append + text; } } text += " + 1"; return text; } void test(unsigned degree) { if(!degree) return; size_t total = 0; unsigned polynomial = 1 << (degree - 1); LFSR<unsigned> lfsr(polynomial); cout << "degree: " << lfsr.degree() << endl; cout << "maximum period: " << lfsr.maximum() << endl; for(;;) { lfsr.polynomial(polynomial); if(lfsr.maximal()) { cout << textual(lfsr) << endl; ++total; } //cout << "period: " << lfsr.period() << endl; if(polynomial++ == lfsr.maximum()) break; } cout << "number of maximal polynomials found: " << total << endl; cout << "-" << endl; } int main(int argc, char* argv[]) { if(argc == 1) { unsigned degree = 1; for(;;) { test(degree++); cout << "(press enter to continue)" << endl; cin.get();

} } else for(int index = 1; index < argc; ++index) test(atoi(argv[index])); }

Vous aimerez peut-être aussi