Optimized adler32 hashing algorithm. (#2948)

* Optimized adler32 implementations.

 - Adapted adler32 implementation from Crypto++ public domain library.
 - Added unit tests for adler32 hashing algorithm.

* tabified adler32 implementation to match stdlib.

* Formatting to be consistent. Make unrolling use macro.

---------

Co-authored-by: soerlemans <sebasoerlemans+git@gmail.com>
Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
soerlemans
2026-02-19 17:51:33 +01:00
committed by GitHub
parent 7ae4c5a1ab
commit 152558f5bc
2 changed files with 137 additions and 14 deletions

View File

@@ -0,0 +1,64 @@
module adler32_test @test;
import std::hash::adler32;
import std::io;
fn void test_adler32_empty()
{
const uint EXPECTED = 0x1;
Adler32 adl;
adl.init();
adl.update("");
uint final = adl.final();
test::@check(final == EXPECTED, "Actual Adler32: 0x%x == 0x%x", final, EXPECTED);
}
fn void test_adler32_a()
{
const uint EXPECTED = 0x00620062;
Adler32 adl;
adl.init();
adl.updatec('a');
uint final = adl.final();
test::@check(final == EXPECTED, "Actual Adler32: 0x%x == 0x%x", final, EXPECTED);
}
fn void test_adler32_abc()
{
const uint EXPECTED = 0x024d0127;
Adler32 adl;
adl.init();
adl.update("abc");
uint final = adl.final();
test::@check(final == EXPECTED, "Actual Adler32: 0x%x == 0x%x", final, EXPECTED);
}
fn void test_adler32_longer()
{
const uint EXPECTED = 0x07822df1;
Adler32 adl;
adl.init();
adl.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
uint final = adl.final();
test::@check(final == EXPECTED, "Actual Adler32: 0x%x == 0x%x", final, EXPECTED);
}
fn void test_adler32_alphabet()
{
const uint EXPECTED = 0x90860b20;
Adler32 adl;
adl.init();
adl.update("abcdefghijklmnopqrstuvwxyz");
uint final = adl.final();
test::@check(final == EXPECTED, "Actual Adler32: 0x%x == 0x%x", final, EXPECTED);
}