2021年4月8日星期四

Does this program calculate CRC correctly?

I'm trying to make a function that takes a single byte and returns CRC value for any generated polynomial given.

#include <stdio.h>  #include <stdint.h>    #define MSG_SIZE    7                             // number of message bits  #define POLY_SIZE   9                             // number of gen. poly. bits  #define SHIFT_INPUT POLY_SIZE-1                   // zeros to be added to input  #define SHIFT_POLY  MSG_SIZE-1                    // zeros to be added to polynomial  #define POLY        0x11D                          // generated polynomial  #define POLY_MSK    (POLY << SHIFT_POLY)          // add zeros to polynomial to be aligned with input  #define ALIGN_MSK   (1 << (MSG_SIZE+POLY_SIZE-2)) // for MSB_poly and MSB_input alignment check    uint64_t PartCRC(uint64_t input);    uint64_t PartCRC(uint64_t input){        input = ( input << SHIFT_INPUT );        for(int n = 0; n < MSG_SIZE; n++){          // if current (MSB) bit of input is "1"; if MSB_poly and MSB_input are aligned          if( input & ALIGN_MSK ){              input ^= POLY_MSK;      // input XOR poly          }          // if crc is calculated, don't shift          if( n == (MSG_SIZE-1) ) break;          input = ( input << 1 );       // shift input left each step      }      return (input >> SHIFT_POLY);   // return crc - trim zeros  }    void main(void) {        printf( "CRC for input: %llX\n", PartCRC(0x41) );        return;  }  

Besides the message given to PartCRC() function from the main(), I have to define only generated polynomial as hex number plus its size and size of message (number of bits from LSB to MSB). What this function does is that it shifts input left every iteration, where it check, whether polynomial's MSB and input's MSB are aligned; if so, perform bitwise XOR between currently shifted input and polynomial (which is POLY_MSK - POLY is shifted for it to be aligned with currently shifted input). At the end, the calculated remainder is shifted right (because it was padded with zeros during CRC calculation).

I understand how this process works out and I'm pretty confident it should operate properly for every CRC standardized generated polynomial, however it does its job only for few non-standardized polynomials. For example, it works for example of the 2.Introduction paragraph (and for few other polynomials).

However, for every other (standardized) polynomial, it doesn't work; (non)validity of code checked at this site. Here, I want to point out that I understand there are few different ways to calculate CRC regarding generated polynomial - normal, reversed, reciprocal, reversed reciprocal. For simplicity I stayed with normal representation, and I also considered that fact when asking for the already calculated CRC at earlier mentioned site (with that I mean "initial value" and "final XOR value" both being 0x00, although I'm not sure, what those two mean).

https://stackoverflow.com/questions/67012318/does-this-program-calculate-crc-correctly April 09, 2021 at 05:48AM

没有评论:

发表评论