Skip to content

Commit

Permalink
testing architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Dec 24, 2024
1 parent 258a1fb commit ae32187
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 109 deletions.
10 changes: 2 additions & 8 deletions hashers/src/blake/blake256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ impl StatefulHasher for Blake224 {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -217,10 +214,7 @@ impl StatefulHasher for Blake256 {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

crate::stateful_hash_tests!(
Expand Down
59 changes: 18 additions & 41 deletions hashers/src/blake/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,11 @@ impl StatefulHasher for Blake2b {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

#[cfg(test)]
mod blake2b_stests {
use utils::byte_formatting::hex_to_bytes_ltr;

use super::*;

const KEY: &[u8] = &[
Expand Down Expand Up @@ -232,45 +227,27 @@ mod blake2b_stests {
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
];

// crate::incremental_hash_tests!(
// with_key, Blake2b::init(KEY, 64),
// [
// &MSG[..13],
// &MSG[13..148],
// &MSG[148..],
// ],
// "142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461";

// abc, Blake2b::init_hash_512(),
// [
// [0x61],
// [0x62],
// [0x63],
// ],
// "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
// );
crate::incremental_hash_tests!(
with_key, Blake2b::init(KEY, 64),
&[
&MSG[..13],
&MSG[13..148],
&MSG[148..],
],
"142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461";

abc, Blake2b::init_hash_512(),
&[
&[0x61],
&[0x62],
&[0x63],
],
"ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
);

crate::stateful_hash_tests!(
empty, Blake2b::init_hash_512(), &[], "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
abc, Blake2b::init_hash_512(), &[0x61, 0x62, 0x63], "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
with_key, Blake2b::init(KEY, 64), &MSG, "142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461";
);

#[test]
fn test_abc_partial() {
let mut hasher = Blake2b::init_hash_512();
hasher.update(&[0x61]);
hasher.update(&[0x62]);
hasher.update(&[0x63]);
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923").unwrap());
}

#[test]
fn test_with_key_partial() {
let mut hasher = Blake2b::init(KEY, 64);
hasher.update(&MSG[..13]);
hasher.update(&MSG[13..148]);
hasher.update(&MSG[148..]);
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461").unwrap());
}
}
5 changes: 1 addition & 4 deletions hashers/src/blake/blake2b_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,5 @@ impl StatefulHasher for Blake2bLong {
}
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}
5 changes: 1 addition & 4 deletions hashers/src/blake/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ impl StatefulHasher for Blake2s {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

#[cfg(test)]
Expand Down
10 changes: 2 additions & 8 deletions hashers/src/blake/blake512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ impl StatefulHasher for Blake384 {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -245,10 +242,7 @@ impl StatefulHasher for Blake512 {
.collect_vec()
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

crate::stateful_hash_tests!(
Expand Down
5 changes: 1 addition & 4 deletions hashers/src/sha/sha0_stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ impl StatefulHasher for Sha0Stateful {
out
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

impl Sha0Stateful {
Expand Down
5 changes: 1 addition & 4 deletions hashers/src/sha/sha1_stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ impl StatefulHasher for Sha1Stateful {
out
}

fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}
crate::stateful_hash_helpers!();
}

impl Sha1Stateful {
Expand Down
85 changes: 49 additions & 36 deletions hashers/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ pub trait StatefulHasher {

// Simultaneously update and finalize.
fn hash(self, bytes: &[u8]) -> Vec<u8>;

// Hash multiple inputs
fn hash_multiple(self, bytes: &[&[u8]]) -> Vec<u8>;
}

#[macro_export]
macro_rules! stateful_hash_helpers {
() => {
fn hash(mut self, bytes: &[u8]) -> Vec<u8> {
self.update(bytes);
self.finalize()
}

fn hash_multiple(mut self, bytes: &[&[u8]]) -> Vec<u8> {
for b in bytes {
self.update(b)
}
self.finalize()
}
};
}

#[macro_export]
Expand Down Expand Up @@ -88,39 +108,32 @@ macro_rules! stateful_hash_tests {
};
}

// The update doesn't work for some reason
// #[macro_export]
// macro_rules! incremental_hash_tests {
// ($($test_name: ident, $hasher: expr, $input: expr, $output: expr);+ $(;)?) => {
// #[cfg(test)]
// mod stateful_incremental_tests {
// use super::*;
// $(
// #[test]
// fn $test_name() {
// for partial in $input {
// $hasher.update(&partial);
// println!("state {:02x?}", $hasher.state_bytes());
// }
// assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.finalize());
// }
// )+
// }
// };
// // Optional variant with module name for separation
// (($mod_name: ident)?; $($name: ident, $hasher: expr, $input: expr, $output: expr);+ $(;)?) => {
// #[cfg(test)]
// mod $mod_name {
// use super::*;
// $(
// #[test]
// fn $test_name() {
// for partial in input {
// $hasher.update(partial)
// }
// assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.finalize());
// }
// )+
// }
// };
// }
#[macro_export]
macro_rules! incremental_hash_tests {
($($test_name: ident, $hasher: expr, $input: expr, $output: expr);+ $(;)?) => {
#[cfg(test)]
mod stateful_incremental_tests {
use super::*;
$(
#[test]
fn $test_name() {
assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.hash_multiple($input));
}
)+
}
};
// Optional variant with module name for separation
(($mod_name: ident)?; $($name: ident, $hasher: expr, $input: expr, $output: expr);+ $(;)?) => {
#[cfg(test)]
mod $mod_name {
use super::*;
$(
#[test]
fn $test_name() {
$hasher.hash_multiple($input);
assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.hash_multiple($input));
}
)+
}
};
}

0 comments on commit ae32187

Please sign in to comment.