Skip to content

Commit

Permalink
more blake reworks
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Dec 24, 2024
1 parent bbf4b82 commit 258a1fb
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 109 deletions.
37 changes: 9 additions & 28 deletions hashers/src/blake/blake256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,12 @@ impl StatefulHasher for Blake256 {
}
}

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

use super::*;

#[test]
fn test_256_one_byte() {
let mut h = Blake256::init();
h.update(&[0x00]);
assert_eq!(
hex_to_bytes_ltr("0ce8d4ef4dd7cd8d62dfded9d4edb0a774ae6a41929a74da23109e8f11139c87")
.unwrap(),
h.finalize()
);
}

#[test]
fn test_256_72_byte() {
let mut h = Blake256::init();
h.update(&[0x00; 72]);
assert_eq!(
hex_to_bytes_ltr("d419bad32d504fb7d44d460c42c5593fe544fa4c135dec31e21bd9abdcc22d41")
.unwrap(),
h.finalize()
);
}
}
crate::stateful_hash_tests!(
test_256_one_byte, Blake256::init(),
&[0x00],
"0ce8d4ef4dd7cd8d62dfded9d4edb0a774ae6a41929a74da23109e8f11139c87";

test_512_pangram, Blake256::init(),
&[0x00; 72],
"d419bad32d504fb7d44d460c42c5593fe544fa4c135dec31e21bd9abdcc22d41";
);
85 changes: 56 additions & 29 deletions hashers/src/blake/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,62 @@ impl StatefulHasher for Blake2b {
}

#[cfg(test)]
mod blake2b_stateful_tests {

mod blake2b_stests {
use utils::byte_formatting::hex_to_bytes_ltr;

use super::*;

#[test]
fn test_empty() {
let hasher = Blake2b::init_hash_512();
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce").unwrap());
}

#[test]
fn test_abc() {
let mut hasher = Blake2b::init_hash_512();
hasher.update(&[0x61, 0x62, 0x63]);
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923").unwrap());
}
const KEY: &[u8] = &[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0x3e, 0x3f,
];

const MSG: [u8; 255] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2,
0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0,
0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
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::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() {
Expand All @@ -227,23 +265,12 @@ mod blake2b_stateful_tests {
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923").unwrap());
}

#[test]
fn test_with_key() {
let mut hasher = Blake2b::init(
hex_to_bytes_ltr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f").unwrap(),
64);
hasher.update(&hex_to_bytes_ltr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe").unwrap());
assert_eq!(hasher.finalize(), hex_to_bytes_ltr("142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e92484be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461").unwrap());
}

#[test]
fn test_with_key_partial() {
let mut hasher = Blake2b::init(
hex_to_bytes_ltr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f").unwrap(),
64);
hasher.update(&hex_to_bytes_ltr("000102030405060708090a0b0c").unwrap());
hasher.update(&hex_to_bytes_ltr("0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f90919293").unwrap());
hasher.update(&hex_to_bytes_ltr("9495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe").unwrap());
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());
}
}
59 changes: 17 additions & 42 deletions hashers/src/blake/blake512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,45 +251,20 @@ impl StatefulHasher for Blake512 {
}
}

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

use super::*;

#[test]
fn test_512_one_byte() {
let mut h = Blake512::init();
h.update(&[0x00]);
assert_eq!(
hex_to_bytes_ltr("97961587f6d970faba6d2478045de6d1fabd09b61ae50932054d52bc29d31be4ff9102b9f69e2bbdb83be13d4b9c06091e5fa0b48bd081b634058be0ec49beb3").unwrap(),
h.finalize());
}

#[test]
fn test_512_pangram() {
let mut h = Blake512::init();
h.update(b"The quick brown fox jumps over the lazy dog");
assert_eq!(
hex_to_bytes_ltr("1f7e26f63b6ad25a0896fd978fd050a1766391d2fd0471a77afb975e5034b7ad2d9ccf8dfb47abbbe656e1b82fbc634ba42ce186e8dc5e1ce09a885d41f43451").unwrap(),
h.finalize());
}

#[test]
fn test_384_letters() {
let mut h = Blake384::init();
h.update(b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
assert_eq!(
hex_to_bytes_ltr("3b21598562da076378b2b8794e919172502d8a6661a503a6b846457376ce2ba546f4d4a7df2c4d8a875a89b0b4647e10").unwrap(),
h.finalize());
}

#[test]
fn test_384_pangram() {
let mut h = Blake384::init();
h.update(b"The quick brown fox jumps over the lazy dog");
assert_eq!(
hex_to_bytes_ltr("67c9e8ef665d11b5b57a1d99c96adffb3034d8768c0827d1c6e60b54871e8673651767a2c6c43d0ba2a9bb2500227406").unwrap(),
h.finalize());
}
}
crate::stateful_hash_tests!(
test_512_one_byte, Blake512::init(),
&[0x00],
"97961587f6d970faba6d2478045de6d1fabd09b61ae50932054d52bc29d31be4ff9102b9f69e2bbdb83be13d4b9c06091e5fa0b48bd081b634058be0ec49beb3";

test_512_pangram, Blake512::init(),
b"The quick brown fox jumps over the lazy dog",
"1f7e26f63b6ad25a0896fd978fd050a1766391d2fd0471a77afb975e5034b7ad2d9ccf8dfb47abbbe656e1b82fbc634ba42ce186e8dc5e1ce09a885d41f43451";

test_384_letters, Blake384::init(),
b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"3b21598562da076378b2b8794e919172502d8a6661a503a6b846457376ce2ba546f4d4a7df2c4d8a875a89b0b4647e10";

test_384_pangram, Blake384::init(),
b"The quick brown fox jumps over the lazy dog",
"67c9e8ef665d11b5b57a1d99c96adffb3034d8768c0827d1c6e60b54871e8673651767a2c6c43d0ba2a9bb2500227406";
);
66 changes: 66 additions & 0 deletions hashers/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,69 @@ macro_rules! basic_hash_tests {
}
};
}

#[macro_export]
macro_rules! stateful_hash_tests {
($($test_name: ident, $hasher: expr, $input: expr, $output: expr);+ $(;)?) => {
#[cfg(test)]
mod stateful_tests {
use super::*;
$(
#[test]
fn $test_name() {
assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.hash($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 $name() {
assert_eq!(utils::byte_formatting::hex_to_bytes_ltr($output).unwrap(), $hasher.hash($input));
}
)+
}
};
}

// 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());
// }
// )+
// }
// };
// }
2 changes: 2 additions & 0 deletions src/hasher_panel/blake2_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ impl Blake2Frame {
};
if ui.button("🎲").on_hover_text("randomize").clicked() {
let mut rng = thread_rng();
self.key.clear();
self.key.shrink_to(256);
rng.fill_bytes(&mut self.key);
self.key_string = ByteFormat::Hex.byte_slice_to_text(&self.key)
};
Expand Down
29 changes: 19 additions & 10 deletions src/hasher_panel/blake_controls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::ui_elements::UiElements;

use super::HasherFrame;
use crate::ui_elements::UiElements;
use hashers::{
blake::{blake256::Blake256, blake512::Blake512, Blake224, Blake384},
errors::HasherError,
Expand Down Expand Up @@ -41,17 +40,27 @@ impl Default for BlakeFrame {

impl BlakeFrame {
fn salt_control_32(&mut self, ui: &mut egui::Ui) {
ui.u32_hex_edit(&mut self.salt_32[0]);
ui.u32_hex_edit(&mut self.salt_32[1]);
ui.u32_hex_edit(&mut self.salt_32[2]);
ui.u32_hex_edit(&mut self.salt_32[3]);
if ui.button("🎲").on_hover_text("randomize").clicked() {
let mut rng = thread_rng();
for i in 0..4 {
self.salt_32[i] = rng.gen();
}
}
for i in 0..4 {
ui.u32_hex_edit(&mut self.salt_32[i]);
}
}

fn salt_control_64(&mut self, ui: &mut egui::Ui) {
ui.u64_hex_edit(&mut self.salt_64[0]);
ui.u64_hex_edit(&mut self.salt_64[1]);
ui.u64_hex_edit(&mut self.salt_64[2]);
ui.u64_hex_edit(&mut self.salt_64[3]);
if ui.button("🎲").on_hover_text("randomize").clicked() {
let mut rng = thread_rng();
for i in 0..4 {
self.salt_64[i] = rng.gen();
}
}
for i in 0..4 {
ui.u64_hex_edit(&mut self.salt_64[i]);
}
}
}

Expand Down

0 comments on commit 258a1fb

Please sign in to comment.