Skip to content

Commit

Permalink
fully removed the old hasher trait
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Jan 5, 2025
1 parent dd6e751 commit 33d85c0
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 480 deletions.
23 changes: 11 additions & 12 deletions hashers/src/balloon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// https://eprint.iacr.org/2016/027.pdf

use std::ops::Range;
use std::{mem, ops::Range};

use num::{BigUint, FromPrimitive};

use crate::{
sha::{Sha2_256, Sha2_512},
Expand Down Expand Up @@ -40,14 +42,6 @@ impl Balloon {
const BLOCKSIZE: usize = 32; // for SHA256
const DELTA: u64 = 3;

fn extract_block(n: usize, blocks: &[u8]) -> &[u8] {
&blocks[n * Self::BLOCKSIZE..][..Self::BLOCKSIZE]
}

fn extract_block_mut(n: usize, blocks: &mut [u8]) -> &mut [u8] {
&mut blocks[n * Self::BLOCKSIZE..][..Self::BLOCKSIZE]
}

fn extract_range(n: usize) -> Range<usize> {
(n * Self::BLOCKSIZE)..(n * Self::BLOCKSIZE + Self::BLOCKSIZE)
}
Expand All @@ -73,6 +67,7 @@ impl StatefulHasher for Balloon {
}

fn finalize(self) -> Vec<u8> {
let m_cost_big_int = BigUint::from_u64(self.m_cost).unwrap();
let mut ctr: u64 = 0;
let mut blocks: Vec<u8> = Vec::with_capacity(self.total_memory());
// Step 1. Expand input into buffer
Expand All @@ -86,7 +81,7 @@ impl StatefulHasher for Balloon {
for i in 1..self.m_cost as usize {
h.update(&ctr.to_le_bytes());
ctr += 1;
h.update(&Self::extract_block(i, &blocks));
h.update(&blocks[Self::extract_range(i)]);
blocks.extend(h.finalize_and_reset());
}

Expand Down Expand Up @@ -116,8 +111,12 @@ impl StatefulHasher for Balloon {
ctr += 1;
h.update(&self.salt);
h.update(&idx_block);
let other = h.finalize_and_reset(); // convert to an integer
let other: usize = todo!();
let other = BigUint::from_bytes_le(&h.finalize_and_reset()) % &m_cost_big_int; // convert to an integer
let other = usize::from_le_bytes(
other.to_bytes_le()[..mem::size_of::<usize>()]
.try_into()
.unwrap(),
);

h.update(&ctr.to_le_bytes());
ctr += 1;
Expand Down
40 changes: 3 additions & 37 deletions hashers/src/belt.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
use crate::traits::ClassicHasher;
use utils::byte_formatting::ByteFormat;

pub struct BeltHash {
pub input_format: ByteFormat,
pub output_format: ByteFormat,
}
pub struct BeltHash {}

impl Default for BeltHash {
fn default() -> Self {
Self {
input_format: ByteFormat::Utf8,
output_format: ByteFormat::Hex,
}
}
}

impl BeltHash {
pub fn input(mut self, input: ByteFormat) -> Self {
self.input_format = input;
self
Self {}
}

pub fn output(mut self, output: ByteFormat) -> Self {
self.output_format = output;
self
}
}

impl ClassicHasher for BeltHash {
fn hash(&self, bytes: &[u8]) -> Vec<u8> {
todo!()
}

crate::hash_bytes_from_string! {}
}

crate::basic_hash_tests!(
test1,
BeltHash::default(),
"INPUT",
"OUTPUT";
);
impl BeltHash {}
176 changes: 88 additions & 88 deletions hashers/src/blake/blake3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{blake_double_round, traits::ClassicHasher};
use crate::blake_double_round;
use std::cmp::min;
use utils::byte_formatting::{make_u32s_le, ByteFormat};

Expand Down Expand Up @@ -378,90 +378,90 @@ impl Default for Blake3 {
}
}

impl ClassicHasher for Blake3 {
fn hash(&self, bytes: &[u8]) -> Vec<u8> {
let mut out = vec![0; self.hash_len as usize];
let mut h = match self.mode {
Blake3Mode::Unkeyed => Blake3Hasher::new(),
Blake3Mode::Keyed => Blake3Hasher::new_keyed(&self.key),
Blake3Mode::KeyDerivation => Blake3Hasher::new_derive_key(&self.key_context),
};
h.update(bytes);
h.finalize(&mut out);
out
}

crate::hash_bytes_from_string! {}
}

#[cfg(test)]
mod blake3_tests {
use itertools::Itertools;

use super::*;

// Test vectors from here
// https://github.com/BLAKE3-team/BLAKE3/blob/master/test_vectors/test_vectors.json

#[test]
fn test_unkeyed() {
let mut hasher = Blake3::default();
hasher.input_format = ByteFormat::Hex;
hasher.output_format = ByteFormat::Hex;
hasher.mode = Blake3Mode::Unkeyed;

let input: Vec<u8> = (0..251).collect_vec();

let hashes: [&str; 5] = [
"af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262e00f03e7b69af26b7faaf09fcd333050338ddfe085b8cc869ca98b206c08243a26f5487789e8f660afe6c99ef9e0c52b92e7393024a80459cf91f476f9ffdbda7001c22e159b402631f277ca96f2defdf1078282314e763699a31c5363165421cce14d",
"2d3adedff11b61f14c886e35afa036736dcd87a74d27b5c1510225d0f592e213c3a6cb8bf623e20cdb535f8d1a5ffb86342d9c0b64aca3bce1d31f60adfa137b358ad4d79f97b47c3d5e79f179df87a3b9776ef8325f8329886ba42f07fb138bb502f4081cbcec3195c5871e6c23e2cc97d3c69a613eba131e5f1351f3f1da786545e5",
"d81293fda863f008c09e92fc382a81f5a0b4a1251cba1634016a0f86a6bd640de3137d477156d1fde56b0cf36f8ef18b44b2d79897bece12227539ac9ae0a5119da47644d934d26e74dc316145dcb8bb69ac3f2e05c242dd6ee06484fcb0e956dc44355b452c5e2bbb5e2b66e99f5dd443d0cbcaaafd4beebaed24ae2f8bb672bcef78",
"10108970eeda3eb932baac1428c7a2163b0e924c9a9e25b35bba72b28f70bd11a182d27a591b05592b15607500e1e8dd56bc6c7fc063715b7a1d737df5bad3339c56778957d870eb9717b57ea3d9fb68d1b55127bba6a906a4a24bbd5acb2d123a37b28f9e9a81bbaae360d58f85e5fc9d75f7c370a0cc09b6522d9c8d822f2f28f485",
"b98cb0ff3623be03326b373de6b9095218513e64f1ee2edd2525c7ad1e5cffd29a3f6b0b978d6608335c09dc94ccf682f9951cdfc501bfe47b9c9189a6fc7b404d120258506341a6d802857322fbd20d3e5dae05b95c88793fa83db1cb08e7d8008d1599b6209d78336e24839724c191b2a52a80448306e0daa84a3fdb566661a37e11",
];

for (length, hash) in [0, 1, 127, 1023, 3072].into_iter().zip(hashes.into_iter()) {
let input_string =
ByteFormat::Hex.byte_iter_to_text(input.iter().cloned().cycle().take(length));
assert_eq!(
hash[0..64],
hasher.hash_bytes_from_string(&input_string).unwrap(),
"failed on input length {}",
length
);
}
}

#[test]
fn test_keyed() {
let mut hasher = Blake3::default();
hasher.input_format = ByteFormat::Hex;
hasher.output_format = ByteFormat::Hex;
hasher.mode = Blake3Mode::Keyed;
hasher.key = "whats the Elvish word for friend"
.as_bytes()
.try_into()
.unwrap();

let input: Vec<u8> = (0..251).collect_vec();

let hashes = [
"92b2b75604ed3c761f9d6f62392c8a9227ad0ea3f09573e783f1498a4ed60d26b18171a2f22a4b94822c701f107153dba24918c4bae4d2945c20ece13387627d3b73cbf97b797d5e59948c7ef788f54372df45e45e4293c7dc18c1d41144a9758be58960856be1eabbe22c2653190de560ca3b2ac4aa692a9210694254c371e851bc8f",
"6d7878dfff2f485635d39013278ae14f1454b8c0a3a2d34bc1ab38228a80c95b6568c0490609413006fbd428eb3fd14e7756d90f73a4725fad147f7bf70fd61c4e0cf7074885e92b0e3f125978b4154986d4fb202a3f331a3fb6cf349a3a70e49990f98fe4289761c8602c4e6ab1138d31d3b62218078b2f3ba9a88e1d08d0dd4cea11",
"c64200ae7dfaf35577ac5a9521c47863fb71514a3bcad18819218b818de85818ee7a317aaccc1458f78d6f65f3427ec97d9c0adb0d6dacd4471374b621b7b5f35cd54663c64dbe0b9e2d95632f84c611313ea5bd90b71ce97b3cf645776f3adc11e27d135cbadb9875c2bf8d3ae6b02f8a0206aba0c35bfe42574011931c9a255ce6dc",
"c951ecdf03288d0fcc96ee3413563d8a6d3589547f2c2fb36d9786470f1b9d6e890316d2e6d8b8c25b0a5b2180f94fb1a158ef508c3cde45e2966bd796a696d3e13efd86259d756387d9becf5c8bf1ce2192b87025152907b6d8cc33d17826d8b7b9bc97e38c3c85108ef09f013e01c229c20a83d9e8efac5b37470da28575fd755a10",
"044a0e7b172a312dc02a4c9a818c036ffa2776368d7f528268d2e6b5df19177022f302d0529e4174cc507c463671217975e81dab02b8fdeb0d7ccc7568dd22574c783a76be215441b32e91b9a904be8ea81f7a0afd14bad8ee7c8efc305ace5d3dd61b996febe8da4f56ca0919359a7533216e2999fc87ff7d8f176fbecb3d6f34278b",
];

for (length, hash) in [0, 1, 127, 1023, 3072].into_iter().zip(hashes.into_iter()) {
let input_string =
ByteFormat::Hex.byte_iter_to_text(input.iter().cloned().cycle().take(length));
assert_eq!(
hash[0..64],
hasher.hash_bytes_from_string(&input_string).unwrap(),
"failed on input length {}",
length
);
}
}
}
// impl ClassicHasher for Blake3 {
// fn hash(&self, bytes: &[u8]) -> Vec<u8> {
// let mut out = vec![0; self.hash_len as usize];
// let mut h = match self.mode {
// Blake3Mode::Unkeyed => Blake3Hasher::new(),
// Blake3Mode::Keyed => Blake3Hasher::new_keyed(&self.key),
// Blake3Mode::KeyDerivation => Blake3Hasher::new_derive_key(&self.key_context),
// };
// h.update(bytes);
// h.finalize(&mut out);
// out
// }

// crate::hash_bytes_from_string! {}
// }

// #[cfg(test)]
// mod blake3_tests {
// use itertools::Itertools;

// use super::*;

// // Test vectors from here
// // https://github.com/BLAKE3-team/BLAKE3/blob/master/test_vectors/test_vectors.json

// #[test]
// fn test_unkeyed() {
// let mut hasher = Blake3::default();
// hasher.input_format = ByteFormat::Hex;
// hasher.output_format = ByteFormat::Hex;
// hasher.mode = Blake3Mode::Unkeyed;

// let input: Vec<u8> = (0..251).collect_vec();

// let hashes: [&str; 5] = [
// "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262e00f03e7b69af26b7faaf09fcd333050338ddfe085b8cc869ca98b206c08243a26f5487789e8f660afe6c99ef9e0c52b92e7393024a80459cf91f476f9ffdbda7001c22e159b402631f277ca96f2defdf1078282314e763699a31c5363165421cce14d",
// "2d3adedff11b61f14c886e35afa036736dcd87a74d27b5c1510225d0f592e213c3a6cb8bf623e20cdb535f8d1a5ffb86342d9c0b64aca3bce1d31f60adfa137b358ad4d79f97b47c3d5e79f179df87a3b9776ef8325f8329886ba42f07fb138bb502f4081cbcec3195c5871e6c23e2cc97d3c69a613eba131e5f1351f3f1da786545e5",
// "d81293fda863f008c09e92fc382a81f5a0b4a1251cba1634016a0f86a6bd640de3137d477156d1fde56b0cf36f8ef18b44b2d79897bece12227539ac9ae0a5119da47644d934d26e74dc316145dcb8bb69ac3f2e05c242dd6ee06484fcb0e956dc44355b452c5e2bbb5e2b66e99f5dd443d0cbcaaafd4beebaed24ae2f8bb672bcef78",
// "10108970eeda3eb932baac1428c7a2163b0e924c9a9e25b35bba72b28f70bd11a182d27a591b05592b15607500e1e8dd56bc6c7fc063715b7a1d737df5bad3339c56778957d870eb9717b57ea3d9fb68d1b55127bba6a906a4a24bbd5acb2d123a37b28f9e9a81bbaae360d58f85e5fc9d75f7c370a0cc09b6522d9c8d822f2f28f485",
// "b98cb0ff3623be03326b373de6b9095218513e64f1ee2edd2525c7ad1e5cffd29a3f6b0b978d6608335c09dc94ccf682f9951cdfc501bfe47b9c9189a6fc7b404d120258506341a6d802857322fbd20d3e5dae05b95c88793fa83db1cb08e7d8008d1599b6209d78336e24839724c191b2a52a80448306e0daa84a3fdb566661a37e11",
// ];

// for (length, hash) in [0, 1, 127, 1023, 3072].into_iter().zip(hashes.into_iter()) {
// let input_string =
// ByteFormat::Hex.byte_iter_to_text(input.iter().cloned().cycle().take(length));
// assert_eq!(
// hash[0..64],
// hasher.hash_bytes_from_string(&input_string).unwrap(),
// "failed on input length {}",
// length
// );
// }
// }

// #[test]
// fn test_keyed() {
// let mut hasher = Blake3::default();
// hasher.input_format = ByteFormat::Hex;
// hasher.output_format = ByteFormat::Hex;
// hasher.mode = Blake3Mode::Keyed;
// hasher.key = "whats the Elvish word for friend"
// .as_bytes()
// .try_into()
// .unwrap();

// let input: Vec<u8> = (0..251).collect_vec();

// let hashes = [
// "92b2b75604ed3c761f9d6f62392c8a9227ad0ea3f09573e783f1498a4ed60d26b18171a2f22a4b94822c701f107153dba24918c4bae4d2945c20ece13387627d3b73cbf97b797d5e59948c7ef788f54372df45e45e4293c7dc18c1d41144a9758be58960856be1eabbe22c2653190de560ca3b2ac4aa692a9210694254c371e851bc8f",
// "6d7878dfff2f485635d39013278ae14f1454b8c0a3a2d34bc1ab38228a80c95b6568c0490609413006fbd428eb3fd14e7756d90f73a4725fad147f7bf70fd61c4e0cf7074885e92b0e3f125978b4154986d4fb202a3f331a3fb6cf349a3a70e49990f98fe4289761c8602c4e6ab1138d31d3b62218078b2f3ba9a88e1d08d0dd4cea11",
// "c64200ae7dfaf35577ac5a9521c47863fb71514a3bcad18819218b818de85818ee7a317aaccc1458f78d6f65f3427ec97d9c0adb0d6dacd4471374b621b7b5f35cd54663c64dbe0b9e2d95632f84c611313ea5bd90b71ce97b3cf645776f3adc11e27d135cbadb9875c2bf8d3ae6b02f8a0206aba0c35bfe42574011931c9a255ce6dc",
// "c951ecdf03288d0fcc96ee3413563d8a6d3589547f2c2fb36d9786470f1b9d6e890316d2e6d8b8c25b0a5b2180f94fb1a158ef508c3cde45e2966bd796a696d3e13efd86259d756387d9becf5c8bf1ce2192b87025152907b6d8cc33d17826d8b7b9bc97e38c3c85108ef09f013e01c229c20a83d9e8efac5b37470da28575fd755a10",
// "044a0e7b172a312dc02a4c9a818c036ffa2776368d7f528268d2e6b5df19177022f302d0529e4174cc507c463671217975e81dab02b8fdeb0d7ccc7568dd22574c783a76be215441b32e91b9a904be8ea81f7a0afd14bad8ee7c8efc305ace5d3dd61b996febe8da4f56ca0919359a7533216e2999fc87ff7d8f176fbecb3d6f34278b",
// ];

// for (length, hash) in [0, 1, 127, 1023, 3072].into_iter().zip(hashes.into_iter()) {
// let input_string =
// ByteFormat::Hex.byte_iter_to_text(input.iter().cloned().cycle().take(length));
// assert_eq!(
// hash[0..64],
// hasher.hash_bytes_from_string(&input_string).unwrap(),
// "failed on input length {}",
// length
// );
// }
// }
// }
50 changes: 14 additions & 36 deletions hashers/src/checksum/bsd.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
use utils::byte_formatting::ByteFormat;

use crate::traits::ClassicHasher;

pub struct BsdChecksum {
pub input_format: ByteFormat,
pub output_format: ByteFormat,
}
pub struct BsdChecksum {}

impl Default for BsdChecksum {
fn default() -> Self {
Self {
input_format: ByteFormat::Hex,
output_format: ByteFormat::Hex,
}
Self {}
}
}

impl BsdChecksum {}

impl ClassicHasher for BsdChecksum {
fn hash(&self, bytes: &[u8]) -> Vec<u8> {
let mut out = 0_u16;
for byte in bytes {
out = out.rotate_right(1);
out = out.wrapping_add(*byte as u16);
}
out.to_le_bytes().to_vec()
}

crate::hash_bytes_from_string! {}
}

#[cfg(test)]
mod bsd_tests {
use super::*;

#[test]
fn test() {
let mut hasher = BsdChecksum::default();
hasher.input_format = ByteFormat::Hex;
hasher.output_format = ByteFormat::Hex;
}
}
// impl ClassicHasher for BsdChecksum {
// fn hash(&self, bytes: &[u8]) -> Vec<u8> {
// let mut out = 0_u16;
// for byte in bytes {
// out = out.rotate_right(1);
// out = out.wrapping_add(*byte as u16);
// }
// out.to_le_bytes().to_vec()
// }

// crate::hash_bytes_from_string! {}
// }
Loading

0 comments on commit 33d85c0

Please sign in to comment.