optimize test_ct_intlog2 test and whirlpool hash (#2938)

* optimize `test_ct_intlog2` while still covering all 128 bit positions

* refactor whirlpool to reduce code bloat

replaced the fully unrolled round loop with a runtime loop, reducing
instruction count by 80k in `process_block` and yielding aprox 30%
performance boost due to improved cache locality.

* use compile-time arrays for `test_ct_intlog2`
This commit is contained in:
Manu Linares
2026-02-15 21:54:46 -03:00
committed by GitHub
parent 5d0c41da6b
commit 4b03a84b00
3 changed files with 107 additions and 30 deletions

View File

@@ -459,20 +459,33 @@ fn void test_log() @test
fn void test_ct_intlog2() @test @if($feature(SLOW_TESTS) && !env::OPENBSD)
{
uint128 actual, expected;
usz[129] actual;
$for var $x = 0; $x <= 128; ++$x :
expected = (uint128)math::floor(math::log2($x));
actual = (uint128)math::@intlog2($x);
assert(expected == actual, "input %d: floor(log2($x)) -> %d is not equal to @intlog2($x) -> %d", $x, expected, actual);
actual[$x] = (usz)math::@intlog2($x);
$endfor
for (usz i = 0; i <= 128; i++)
{
usz expected = (i <= 1) ? 0 : (usz)math::floor(math::log2(i));
assert(actual[i] == expected, "input %d: expected %d, got %d", i, expected, actual[i]);
}
var $logme = (uint128)1;
$for var $x = 0; $x < 8192; ++$x :
$logme *= 13;
expected = (uint128)math::floor(math::log2((uint128)$logme));
actual = (uint128)math::@intlog2((uint128)$logme);
assert(expected == actual, "input %d (idx %d): floor(log2(|$logme|)) -> %d is not equal to @intlog2(|$logme|) -> %d", $logme, $x, expected, actual);
usz[128] powers;
$for var $i = 0; $i < 128; ++$i :
powers[$i] = (usz)math::@intlog2((uint128)1 << $i);
$endfor
for (usz i = 0; i < 128; i++)
{
assert(powers[i] == i, "input 2^%d: expected %d, got %d", i, i, powers[i]);
}
usz[127] complex_vals;
$for var $i = 1; $i < 128; ++$i :
complex_vals[$i - 1] = (usz)math::@intlog2(((uint128)1 << $i) | ((uint128)1 << ($i - 1)));
$endfor
for (usz i = 1; i < 128; i++)
{
assert(complex_vals[i - 1] == (usz)i, "input 2^%d + 2^%d: expected %d, got %d", i, i - 1, i, complex_vals[i - 1]);
}
}
fn void test_pow() @test