aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2022-11-22 23:09:05 -0800
committerJack O'Connor <[email protected]>2022-11-22 23:31:29 -0800
commit5dad698d3f89594012f355245a783607b6ffcc0c (patch)
tree8ff5b0cbf63b4eb42bccc803d66a848067f27be6 /src
parent62772b2d0464c56eb1137e645d23ef4f8ee6d01f (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/test.rs141
1 files changed, 75 insertions, 66 deletions
diff --git a/src/test.rs b/src/test.rs
index c2bea95..4b6272c 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -111,89 +111,98 @@ pub fn test_hash_many_fn(
hash_many_chunks_fn: HashManyFn<[u8; CHUNK_LEN]>,
hash_many_parents_fn: HashManyFn<[u8; 2 * OUT_LEN]>,
) {
- // 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();
- for i in 0..NUM_INPUTS {
- chunks.push(array_ref!(input_buf, i * CHUNK_LEN, CHUNK_LEN));
- }
- let mut portable_chunks_out = [0; NUM_INPUTS * OUT_LEN];
- crate::portable::hash_many(
- &chunks,
- &TEST_KEY_WORDS,
- counter,
- IncrementCounter::Yes,
- crate::KEYED_HASH,
- crate::CHUNK_START,
- crate::CHUNK_END,
- &mut portable_chunks_out,
- );
+ // 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);
- let mut test_chunks_out = [0; NUM_INPUTS * OUT_LEN];
- unsafe {
- hash_many_chunks_fn(
- &chunks[..],
+ // 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);
+
+ // First hash chunks.
+ let mut chunks = ArrayVec::<&[u8; CHUNK_LEN], NUM_INPUTS>::new();
+ for i in 0..NUM_INPUTS {
+ chunks.push(array_ref!(input_buf, i * CHUNK_LEN, CHUNK_LEN));
+ }
+ let mut portable_chunks_out = [0; NUM_INPUTS * OUT_LEN];
+ crate::portable::hash_many(
+ &chunks,
&TEST_KEY_WORDS,
counter,
IncrementCounter::Yes,
crate::KEYED_HASH,
crate::CHUNK_START,
crate::CHUNK_END,
- &mut test_chunks_out,
- );
- }
- for n in 0..NUM_INPUTS {
- #[cfg(feature = "std")]
- dbg!(n);
- assert_eq!(
- &portable_chunks_out[n * OUT_LEN..][..OUT_LEN],
- &test_chunks_out[n * OUT_LEN..][..OUT_LEN]
+ &mut portable_chunks_out,
);
- }
- // Then hash parents.
- let mut parents = ArrayVec::<&[u8; 2 * OUT_LEN], NUM_INPUTS>::new();
- for i in 0..NUM_INPUTS {
- parents.push(array_ref!(input_buf, i * 2 * OUT_LEN, 2 * OUT_LEN));
- }
- let mut portable_parents_out = [0; NUM_INPUTS * OUT_LEN];
- crate::portable::hash_many(
- &parents,
- &TEST_KEY_WORDS,
- counter,
- IncrementCounter::No,
- crate::KEYED_HASH | crate::PARENT,
- 0,
- 0,
- &mut portable_parents_out,
- );
+ let mut test_chunks_out = [0; NUM_INPUTS * OUT_LEN];
+ unsafe {
+ hash_many_chunks_fn(
+ &chunks[..],
+ &TEST_KEY_WORDS,
+ counter,
+ IncrementCounter::Yes,
+ crate::KEYED_HASH,
+ crate::CHUNK_START,
+ crate::CHUNK_END,
+ &mut test_chunks_out,
+ );
+ }
+ for n in 0..NUM_INPUTS {
+ #[cfg(feature = "std")]
+ dbg!(n);
+ assert_eq!(
+ &portable_chunks_out[n * OUT_LEN..][..OUT_LEN],
+ &test_chunks_out[n * OUT_LEN..][..OUT_LEN]
+ );
+ }
- let mut test_parents_out = [0; NUM_INPUTS * OUT_LEN];
- unsafe {
- hash_many_parents_fn(
- &parents[..],
+ // Then hash parents.
+ let mut parents = ArrayVec::<&[u8; 2 * OUT_LEN], NUM_INPUTS>::new();
+ for i in 0..NUM_INPUTS {
+ parents.push(array_ref!(input_buf, i * 2 * OUT_LEN, 2 * OUT_LEN));
+ }
+ let mut portable_parents_out = [0; NUM_INPUTS * OUT_LEN];
+ crate::portable::hash_many(
+ &parents,
&TEST_KEY_WORDS,
counter,
IncrementCounter::No,
crate::KEYED_HASH | crate::PARENT,
0,
0,
- &mut test_parents_out,
- );
- }
- for n in 0..NUM_INPUTS {
- #[cfg(feature = "std")]
- dbg!(n);
- assert_eq!(
- &portable_parents_out[n * OUT_LEN..][..OUT_LEN],
- &test_parents_out[n * OUT_LEN..][..OUT_LEN]
+ &mut portable_parents_out,
);
+
+ let mut test_parents_out = [0; NUM_INPUTS * OUT_LEN];
+ unsafe {
+ hash_many_parents_fn(
+ &parents[..],
+ &TEST_KEY_WORDS,
+ counter,
+ IncrementCounter::No,
+ crate::KEYED_HASH | crate::PARENT,
+ 0,
+ 0,
+ &mut test_parents_out,
+ );
+ }
+ for n in 0..NUM_INPUTS {
+ #[cfg(feature = "std")]
+ dbg!(n);
+ assert_eq!(
+ &portable_parents_out[n * OUT_LEN..][..OUT_LEN],
+ &test_parents_out[n * OUT_LEN..][..OUT_LEN]
+ );
+ }
}
}