mirror of
https://github.com/BLAKE3-team/BLAKE3
synced 2024-05-05 16:06:28 +02:00
test multiple initial counter values for hash_many
I'm adding the i32::MAX test case here because I personally screwed it up while I was working on https://github.com/BLAKE3-team/BLAKE3/issues/271. The correct implementation of the carry bit is the ANDNOT of old high bit (1) and the new high bit (0). Using XOR instead of ANDNOT gives the correct answer in the overflow case, but it also reports an incorrect "extra" overflow when the high bit goes from 0 to 1.
This commit is contained in:
parent
62772b2d04
commit
5dad698d3f
|
@ -207,12 +207,19 @@ type HashManyFn = unsafe extern "C" fn(
|
|||
|
||||
// A shared helper function for platform-specific tests.
|
||||
pub fn test_hash_many_fn(hash_many_fn: HashManyFn) {
|
||||
// Test a few different initial counter values.
|
||||
// - 0: The base case.
|
||||
// - u32::MAX: The low word of the counter overflows for all inputs except the first.
|
||||
// - i32::MAX: *No* overflow. But carry bugs in tricky SIMD code can screw this up, if you XOR
|
||||
// when you're supposed to ANDNOT...
|
||||
let initial_counters = [0, u32::MAX as u64, i32::MAX as u64];
|
||||
for counter in initial_counters {
|
||||
dbg!(counter);
|
||||
|
||||
// 31 (16 + 8 + 4 + 2 + 1) inputs
|
||||
const NUM_INPUTS: usize = 31;
|
||||
let mut input_buf = [0; CHUNK_LEN * NUM_INPUTS];
|
||||
crate::test::paint_test_input(&mut input_buf);
|
||||
// A counter just prior to u32::MAX.
|
||||
let counter = (1u64 << 32) - 1;
|
||||
|
||||
// First hash chunks.
|
||||
let mut chunks = ArrayVec::<&[u8; CHUNK_LEN], NUM_INPUTS>::new();
|
||||
|
@ -302,6 +309,7 @@ pub fn test_hash_many_fn(hash_many_fn: HashManyFn) {
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Testing the portable implementation against itself is circular, but why not.
|
||||
#[test]
|
||||
|
|
13
src/test.rs
13
src/test.rs
|
@ -111,12 +111,20 @@ pub fn test_hash_many_fn(
|
|||
hash_many_chunks_fn: HashManyFn<[u8; CHUNK_LEN]>,
|
||||
hash_many_parents_fn: HashManyFn<[u8; 2 * OUT_LEN]>,
|
||||
) {
|
||||
// Test a few different initial counter values.
|
||||
// - 0: The base case.
|
||||
// - u32::MAX: The low word of the counter overflows for all inputs except the first.
|
||||
// - i32::MAX: *No* overflow. But carry bugs in tricky SIMD code can screw this up, if you XOR
|
||||
// when you're supposed to ANDNOT...
|
||||
let initial_counters = [0, u32::MAX as u64, i32::MAX as u64];
|
||||
for counter in initial_counters {
|
||||
#[cfg(feature = "std")]
|
||||
dbg!(counter);
|
||||
|
||||
// 31 (16 + 8 + 4 + 2 + 1) inputs
|
||||
const NUM_INPUTS: usize = 31;
|
||||
let mut input_buf = [0; CHUNK_LEN * NUM_INPUTS];
|
||||
crate::test::paint_test_input(&mut input_buf);
|
||||
// A counter just prior to u32::MAX.
|
||||
let counter = (1u64 << 32) - 1;
|
||||
|
||||
// First hash chunks.
|
||||
let mut chunks = ArrayVec::<&[u8; CHUNK_LEN], NUM_INPUTS>::new();
|
||||
|
@ -196,6 +204,7 @@ pub fn test_hash_many_fn(
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_key_bytes_equal_key_words() {
|
||||
|
|
Loading…
Reference in New Issue