0

While exploring how to generate maximum period forward and reverse sequences using an LFSR, I ran into some confusion regarding the definition of a GF(2) primitive polynomial.

When I chose a primitive GF(2) polynomial from this table, all polynomials have the x^0 term set to 1 and everything works as expected (see below).

#include <cstdio>
#include <cstdint>

uint8_t rev (uint8_t xi)
{
  uint8_t xo ;

  for (auto n = 8 ; n ; -- n)
    {
      xo = xo << 1 | xi & 1 ;

      xi >>= 1 ;
    }

  return xo ;
}

int main (void)
{
  uint8_t const POLY_F = 0x85 ; // x^8 + x^7 + x^2 + 1 : Normal
  uint8_t const POLY_R = 0x43 ; // x^8 + x^6 + x   + 1 : Reversed

  uint8_t lfsr = 0xFF ;

  for (auto n = 1 ; n <= 5 ; ++ n)                    // 1 -> FF
    {                                                 // 2 -> 7B
      printf ("%u -> %02X\n" , n , lfsr) ;            // 3 -> F6
                                                      // 4 -> 69
      lfsr = - (lfsr >> 7) & POLY_F ^ lfsr << 1 ;     // 5 -> D2
    }

  printf ("\n") ;

  for (auto n = 5 ; n >= 1 ; -- n)                    // 5 -> D2
    {                                                 // 4 -> 69
      lfsr = rev (lfsr) ;                             // 3 -> F6
                                                      // 2 -> 7B
      lfsr = - (lfsr >> 7) & POLY_R ^ lfsr << 1 ;     // 1 -> FF

      lfsr = rev (lfsr) ;

      printf ("%u -> %02X\n" , n ,lfsr) ;
    }
}

When I generate a maximal length GF(2) polynomial using this program, it sometimes outputs a polynomial with the x^0 term set to 0.

I cannot figure out how to reverse the sequence when the x^0 term is 0.

Is a “maximal length” polynomial the same thing as a “primitive polynomial”?

Can a primitive polynomial have the x^0 term set to 0?

Is it possible to reverse a sequence if the x^0 term is 0?

Progman
  • 16,827
  • 6
  • 33
  • 48
  • 2
    As [the Wikipedia page for a primitive polynomial](https://en.wikipedia.org/wiki/Primitive_polynomial_(field_theory)) states, “A primitive polynomial must have a non-zero constant term, for otherwise it will be divisible by *x*.” – Eric Postpischil Jun 22 '23 at 19:01
  • 3
    @SupportUkraine: The Stack Overflow guidelines on subjects are not a dichotomy. Math that is particularly used in or relevant to programming qualifies, particularly when a goal is to explain it to an audience more experienced in programming than in abstract mathematics, as might be the focus in other forums on math. Aspects of Galois Fields in linear-feedback shift registers and cryptography are suitable for Stack Overflow. Programming is not just writing code. – Eric Postpischil Jun 22 '23 at 19:30
  • I realized a bit later after my post that not having the x^0 term makes the polynomial reducible, hence not primitive. What I still don't get is how a reducible polynomial can generate a maximum length LFSR and how to write code to reverse it. – cookiecipher Jun 22 '23 at 20:58
  • @cookiecipher With `- (lfsr >> 7) & POLY_F ^ lfsr << 1` the `-, &, ^` operators might not evaluate as desired. Suggest adding `()`. – chux - Reinstate Monica Jun 22 '23 at 23:05
  • @chux Why do you think the operators might not evaluate as desired? – cookiecipher Jun 23 '23 at 17:41
  • @cookiecipher If `- (lfsr >> 7) & POLY_F ^ lfsr << 1 ;` is meant like `((- (lfsr >> 7)) & POLY_F) ^ (lfsr << 1);` then not using `()` is OK. Yet it is not clear that was your intent. – chux - Reinstate Monica Jun 23 '23 at 18:25

1 Answers1

0

Code has at least these problems:

Uninitialized value

it sometimes outputs a polynomial with the x^0 term set to 0.

The first time xo << 1 | xi & 1 ; is executed, xo is not initialize nor assigned.

Instead:

// uint8_t xo ;
uint8_t xo = 0;

Language?

Post is tagged C yet uses #include <cstdio>. Best to use a C compiler for C code.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I think you misunderstood the x^0 I was referring to. I was referring to the one being generated by the external maximum length polynomial generator. Yes, the 'xo' variable is uninitialized, but will always have a valid final value as it is always left-shifted by 8, so no issue. – cookiecipher Jun 23 '23 at 17:39