Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Results Don't Agree with zlib #3

Closed
xanderdunn opened this issue Apr 22, 2021 · 4 comments
Closed

Results Don't Agree with zlib #3

xanderdunn opened this issue Apr 22, 2021 · 4 comments

Comments

@xanderdunn
Copy link

Although the crc32c results I'm seeing are consistent with the tests in this library, they do not agree with the crc32 results of other libraries, such as zlib:

#include <crc32c/crc32c.h>
#include <zlib.h>

unsigned long crc_zlib = crc32(0L, Z_NULL, 0);
std::string test_string = "hello-world";
uint32_t truth = 2983461467;
const auto test_string_chars = reinterpret_cast<const unsigned char*>(test_string.c_str());
crc_zlib = crc32(crc_zlib, (const unsigned char*)test_string_chars, test_string.length()); // this is zlib

uint32_t crc_c = crc32c::Crc32c(test_string); // 
std::cout << "truth: " << truth << ", zlib: " << crc_zlib << ", crc32c: " << crc_c << std::endl;

produces the output:

truth: 2983461467, zlib: 2983461467, crc32c: 4099351003

Calling the same with test_string = "The quick brown fox jumps over the lazy dog" produces the output:

truth: 576848900, zlib: 1095738169, crc32c: 576848900

You can see the short phrase "The quick brown fox..." produces the same crc as in your tests, but it does not agree with zlib. The zlib result on "hello-world" agrees with all of the sources listed here.

I thought perhaps there was a difference in what was being calculated, such as returning the signed 32-bit crc rather than unsigned, but it is indeed returning a unit32_t, so that wouldn't explain it.

This is a Google Cloud Compute instance with haswell Intel architecture. When I build the project, running ./crc32c_test shows all tests PASSED.

Do you have any guidance on what the discrepancy may be? Thanks!

@htot
Copy link
Owner

htot commented Apr 22, 2021

crc32 <> crc32c, see https://en.wikipedia.org/wiki/Cyclic_redundancy_check

I am not familiar with zlib, what does it calculate exactly?

@xanderdunn
Copy link
Author

xanderdunn commented Apr 22, 2021

Thanks for the comment.

I see in the wikipedia article:

CRC32C computation is implemented in hardware as an operation (CRC32) of SSE4.2 instruction set, first introduced in Intel processors' Nehalem microarchitecture. CRC32C operations are also defined in AArch64.

Are hardware crc32 values (crc32c) expected to be equal to software crc32 values?

zlib is calculating the 32 bit cyclic redundancy check. See the python docs here. Is it different because crc32c is not what it's calculating?

@htot
Copy link
Owner

htot commented Apr 22, 2021

If this one is correct https://stackoverflow.com/questions/50842434/crc32-in-python-vs-crc32b then zlib implements CRC32 as used in ethernet, bzip2, gzip, png and zmodem.

This lib implements CRC32C as implemented in i686/x86_64 instruction and used in iSCSI, btrfs, ext4.

They are different.

Edit: the differences: https://stackoverflow.com/questions/26429360/crc32-vs-crc32c

@xanderdunn
Copy link
Author

This makes sense, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants