Search code examples
chashcryptographycrc

Modify CRC64's hash table to generate 65536 instead of 256 values


Simply modifying the 256 in the loop to 65536 just repeats the same 256 values over and over again. How to generate 65536 different values?

#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL

static uint64_t crc64_table[256] = {0};

static void generate_crc64_table(void)
{
    uint64_t i, j, c, crc;

    for (i = 0; i < 256; i++) {
        crc = 0;
        c = i << 56;

        for (j = 0; j < 8; j++) {
            if ((crc ^ c) & 0x8000000000000000ULL)
                crc = (crc << 1) ^ CRC64_ECMA182_POLY;
            else
                crc <<= 1;
            c <<= 1;
        }

        crc64_table[i] = crc;
    }
}

Solution

  • if you want 65536 values, presumably you want a 16 bit table, so upgrade the bit loop to 16 as well.

    static void generate_crc64_table(void)
    {
        uint64_t i, j, c, crc;
    
        for (i = 0; i < 65536 ; i++) { // 65536 was 256  
            crc = 0;
            c = i << 32; // 32 was 56
    
            for (j = 0; j < 16; j++) { // 16 was 8
                if ((crc ^ c) & 0x8000000000000000ULL)
                    crc = (crc << 1) ^ CRC64_ECMA182_POLY;
                else
                    crc <<= 1;
                c <<= 1;
            }
    
            crc64_table[i] = crc;
        }
    }
    

    no guarantees that this will produce a useful table, but the values should all be different at-least.