Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Jul 4, 2024
1 parent fc0302c commit 7816b28
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,7 @@ mod test {
use tari_utilities::hex::{from_hex, to_hex};

use crate::hashing::{
AsFixedBytes,
DomainSeparatedHasher,
DomainSeparation,
Mac,
MacDomain,
AsFixedBytes, DomainSeparatedHasher, DomainSeparation, Flag, Mac, MacDomain
};

mod util {
Expand Down Expand Up @@ -674,7 +670,7 @@ mod test {
util::hash_from_digest(
MyDemoHasher::new(),
&[0, 0, 0],
"d4cbf5b6b97485a991973db8a6ce4d3fc660db5dff5f55f2b0cb363fca34b0a2",
"7f018785a03c826fe26be7a1d4c90cf99db0d8e313c3dd6d53eeb6233827e8e3",
);
}
{
Expand All @@ -683,13 +679,12 @@ mod test {
util::hash_from_digest(
MyDemoHasher2::new(),
&[0, 0, 0],
"d4cbf5b6b97485a991973db8a6ce4d3fc660db5dff5f55f2b0cb363fca34b0a2",
"7f018785a03c826fe26be7a1d4c90cf99db0d8e313c3dd6d53eeb6233827e8e3",
);
}
}

#[test]
// Regression test
fn mac_domain_metadata() {
assert_eq!(MacDomain::version(), 1);
assert_eq!(MacDomain::domain(), "com.tari.mac");
Expand Down Expand Up @@ -743,7 +738,7 @@ mod test {
assert_eq!(hash.as_ref(), hash2.as_ref());
assert_eq!(
to_hex(hash.as_ref()),
"a8326620e305430a0b632a0a5e33c6c1124d7513b4bd84736faaa3a0b9ba557f"
"f2288bde23da07c021468a1b63487514cc4cbb68d15c34bc6621964b31547df6"
);

let hash_1 =
Expand Down Expand Up @@ -856,28 +851,21 @@ mod test {
"com.discworld"
}
}
let hash = DomainSeparatedHasher::<Blake2b<U32>, MyDemoHasher>::new_with_label("turtles").finalize();
let label = "turtles";
let hash = DomainSeparatedHasher::<Blake2b<U32>, MyDemoHasher>::new_with_label(label).finalize();
let expected = Blake2b::<U32>::default()
.chain((domain.len() as u64).to_le_bytes())
.chain(domain)
.chain([Flag::DomainSeparator as u8])
.chain((MyDemoHasher::domain().len() as u64).to_le_bytes())
.chain(MyDemoHasher::domain())
.chain([Flag::Version as u8])
.chain([MyDemoHasher::version()])
.chain([Flag::Label as u8])
.chain((label.as_bytes().len() as u64).to_le_bytes())
.chain(label.as_bytes())
.finalize();
assert_eq!(hash.as_ref(), expected.as_slice());
}

#[test]
fn update_domain_separation_tag() {
hash_domain!(TestDomain, "com.test");
let s_tag = TestDomain::domain_separation_tag("mytest");
let expected_hash = Blake2b::<U32>::default()
.chain(s_tag.len().to_le_bytes())
.chain(s_tag)
.finalize();

let mut digest = Blake2b::<U32>::default();
TestDomain::add_domain_separation_tag(&mut digest, "mytest");
assert_eq!(digest.finalize(), expected_hash);
}

#[test]
fn application_hasher() {
struct MyDemoHasher;
Expand Down Expand Up @@ -905,17 +893,9 @@ mod test {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `LengthExtensionAttackResistant` is not implemented for
// `Sha256`
let mac = Mac::<Blake2b<U32>>::generate(key, "test message", "test");
assert_eq!(MacDomain::domain_separation_tag("test"), "com.tari.mac.v1.test");
assert_eq!(
to_hex(mac.as_ref()),
"9bcfbe2bad73b14ac42f673ddca34e82ce03cbbac69d34526004f5d108dff061"
)
}

#[test]
fn check_bytes_to_decimal_ascii_bytes() {
assert_eq!(byte_to_decimal_ascii_bytes(0), (2, [0u8, 0, 48]));
assert_eq!(byte_to_decimal_ascii_bytes(42), (1, [0u8, 52, 50]));
assert_eq!(byte_to_decimal_ascii_bytes(255), (0, [50u8, 53, 53]));
}
}

0 comments on commit 7816b28

Please sign in to comment.