1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-05-28 13:46:02 +02:00

UNIVERSAL_HASH_LEN = 16

This commit is contained in:
Jack O'Connor 2023-06-17 14:29:57 -07:00
parent 589f2c3f48
commit 1cb2797abc
4 changed files with 29 additions and 12 deletions

View File

@ -130,6 +130,8 @@ pub const OUT_LEN: usize = 32;
/// The number of bytes in a key, 32.
pub const KEY_LEN: usize = 32;
const UNIVERSAL_HASH_LEN: usize = 16;
const MAX_DEPTH: usize = 54; // 2^54 * CHUNK_LEN = 2^64
use guts::{BLOCK_LEN, CHUNK_LEN};

View File

@ -1,4 +1,4 @@
use crate::{portable, CVWords, IncrementCounter, BLOCK_LEN, CHUNK_LEN};
use crate::{portable, CVWords, IncrementCounter, BLOCK_LEN, CHUNK_LEN, UNIVERSAL_HASH_LEN};
use arrayref::{array_mut_ref, array_ref};
cfg_if::cfg_if! {
@ -335,7 +335,12 @@ impl Platform {
portable::xof_xor(block, block_len, cv, counter, flags, out);
}
pub fn universal_hash(&self, input: &[u8], key: &[u32; 8], counter: u64) -> [u8; 64] {
pub fn universal_hash(
&self,
input: &[u8],
key: &[u32; 8],
counter: u64,
) -> [u8; UNIVERSAL_HASH_LEN] {
portable::universal_hash(input, key, counter)
}

View File

@ -1,6 +1,6 @@
use crate::{
counter_high, counter_low, platform::TransposedVectors, CVBytes, CVWords, IncrementCounter,
BLOCK_LEN, CHUNK_LEN, IV, MSG_SCHEDULE, OUT_LEN,
BLOCK_LEN, CHUNK_LEN, IV, MSG_SCHEDULE, OUT_LEN, UNIVERSAL_HASH_LEN,
};
use arrayref::{array_mut_ref, array_ref};
use core::cmp;
@ -256,9 +256,13 @@ pub fn xof_xor(
}
}
pub fn universal_hash(mut input: &[u8], key: &[u32; 8], mut counter: u64) -> [u8; BLOCK_LEN] {
pub fn universal_hash(
mut input: &[u8],
key: &[u32; 8],
mut counter: u64,
) -> [u8; UNIVERSAL_HASH_LEN] {
let flags = crate::KEYED_HASH | crate::CHUNK_START | crate::CHUNK_END | crate::ROOT;
let mut result = [0u8; BLOCK_LEN];
let mut result = [0u8; UNIVERSAL_HASH_LEN];
while input.len() > BLOCK_LEN {
let block_output = compress_xof(
key,
@ -267,7 +271,7 @@ pub fn universal_hash(mut input: &[u8], key: &[u32; 8], mut counter: u64) -> [u8
counter,
flags,
);
for i in 0..BLOCK_LEN {
for i in 0..UNIVERSAL_HASH_LEN {
result[i] ^= block_output[i];
}
input = &input[BLOCK_LEN..];
@ -276,7 +280,7 @@ pub fn universal_hash(mut input: &[u8], key: &[u32; 8], mut counter: u64) -> [u8
let mut final_block = [0u8; BLOCK_LEN];
final_block[..input.len()].copy_from_slice(input);
let final_output = compress_xof(key, &final_block, input.len() as u8, counter, flags);
for i in 0..BLOCK_LEN {
for i in 0..UNIVERSAL_HASH_LEN {
result[i] ^= final_output[i];
}
result

View File

@ -1,4 +1,6 @@
use crate::{CVBytes, CVWords, IncrementCounter, BLOCK_LEN, CHUNK_LEN, OUT_LEN};
use crate::{
CVBytes, CVWords, IncrementCounter, BLOCK_LEN, CHUNK_LEN, OUT_LEN, UNIVERSAL_HASH_LEN,
};
use arrayref::array_ref;
use arrayvec::ArrayVec;
use core::cmp;
@ -345,7 +347,8 @@ fn test_compare_reference_impl_xof() {
}
}
type UniversalHashFn = unsafe fn(input: &[u8], key: &[u32; 8], counter: u64) -> [u8; BLOCK_LEN];
type UniversalHashFn =
unsafe fn(input: &[u8], key: &[u32; 8], counter: u64) -> [u8; UNIVERSAL_HASH_LEN];
pub fn test_universal_hash_fn(target_fn: UniversalHashFn) {
// 31 (16 + 8 + 4 + 2 + 1) inputs
@ -362,13 +365,16 @@ pub fn test_universal_hash_fn(target_fn: UniversalHashFn) {
}
}
fn reference_impl_universal_hash(input: &[u8], key: &[u8; crate::KEY_LEN]) -> [u8; BLOCK_LEN] {
fn reference_impl_universal_hash(
input: &[u8],
key: &[u8; crate::KEY_LEN],
) -> [u8; UNIVERSAL_HASH_LEN] {
// The reference_impl doesn't support XOF seeking, so we have to materialize an entire extended
// output to seek to a block.
const MAX_BLOCKS: usize = 31;
assert!(input.len() / BLOCK_LEN <= MAX_BLOCKS);
let mut output_buffer: [u8; BLOCK_LEN * MAX_BLOCKS] = [0u8; BLOCK_LEN * MAX_BLOCKS];
let mut result = [0u8; BLOCK_LEN];
let mut result = [0u8; UNIVERSAL_HASH_LEN];
let mut i = 0;
while i == 0 || i < input.len() {
let block_len = cmp::min(input.len() - i, BLOCK_LEN);
@ -377,7 +383,7 @@ fn reference_impl_universal_hash(input: &[u8], key: &[u8; crate::KEY_LEN]) -> [u
reference_hasher.finalize(&mut output_buffer);
for (result_byte, output_byte) in result
.iter_mut()
.zip(output_buffer[i..i + BLOCK_LEN].iter())
.zip(output_buffer[i..i + UNIVERSAL_HASH_LEN].iter())
{
*result_byte ^= *output_byte;
}