Hash: start with a state of zero.

This doesn't affect the quality of the hash at all, and an empty
message hashing to zero is kind of nice.
This commit is contained in:
Nathan Vegdahl 2022-08-29 18:47:31 -07:00
parent 03e78299c6
commit c585e38a54

View File

@ -33,13 +33,7 @@ pub struct LedHash256 {
impl LedHash256 { impl LedHash256 {
pub fn new() -> LedHash256 { pub fn new() -> LedHash256 {
LedHash256 { LedHash256 {
state: [ state: [0, 0, 0, 0],
// Initial Chaining Values from Skein-256-256, v1.3
0xfc9da860d048b449,
0x2fca66479fa7d833,
0xb33bc3896656840f,
0x6a54e920fde8da69,
],
buf: [0; BLOCK_SIZE], buf: [0; BLOCK_SIZE],
buf_length: 0, buf_length: 0,
message_length: 0, message_length: 0,
@ -189,7 +183,7 @@ mod test {
} }
let mut s = String::new(); let mut s = String::new();
for byte in &digest { for byte in digest.iter().rev() {
s.push(low_bits_to_char(byte >> 4u8)); s.push(low_bits_to_char(byte >> 4u8));
s.push(low_bits_to_char(byte & 0b00001111)); s.push(low_bits_to_char(byte & 0b00001111));
} }
@ -198,47 +192,47 @@ mod test {
#[test] #[test]
fn hash_empty() { fn hash_empty() {
let correct_digest = "fcdfdd47e35abc0d7ebd5c24aaa81b896c07f2cb0f2dc6395fdda6fc8fb12991"; let correct_digest = "0000000000000000000000000000000000000000000000000000000000000000";
assert_eq!(digest_to_string(hash(&[])), correct_digest); assert_eq!(digest_to_string(hash(&[])), correct_digest);
} }
#[test] #[test]
fn hash_zero() { fn hash_zero() {
let correct_digest = "0f11e90ef9373089f0a337cd1af6c923a2e5d679e92782d3e51da364a34d33e9"; let correct_digest = "8311a9b6f5e6f4d31707eec6bab81a406db32289be10d2dd93b2b3c0cf6ddac3";
assert_eq!(digest_to_string(hash(&[0u8])), correct_digest); assert_eq!(digest_to_string(hash(&[0u8])), correct_digest);
} }
#[test] #[test]
fn hash_one() { fn hash_one() {
let correct_digest = "36bf32dc5bcce36f6e1cc268ab40a5d7c1e2ed8dddec59c51a9e79a8a230802f"; let correct_digest = "c111cf6d91fdbc240c34662af28b3425dfe137f1107a65a40ac0b1bc490e1433";
assert_eq!(digest_to_string(hash(&[1u8])), correct_digest); assert_eq!(digest_to_string(hash(&[1u8])), correct_digest);
} }
#[test] #[test]
fn hash_string_01() { fn hash_string_01() {
let s = "abc"; let s = "abc";
let correct_digest = "e06e17ff841570a558f48991172d522b37f86966f19bc45ee7bde2537b212246"; let correct_digest = "44b8f371ded8bd64a91302da43f0f69117b3795e12a2cd079049d70f675bf19e";
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest); assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
} }
#[test] #[test]
fn hash_string_02() { fn hash_string_02() {
let s = "The quick brown fox jumps over the lazy dog."; let s = "The quick brown fox jumps over the lazy dog.";
let correct_digest = "e82d9acc9ed0e629115585a253baa4ad607225dcba88cbdb4f1f10979e5b1bfc"; let correct_digest = "7f69ce128ed82ed4bc8b8993b52555cf7e960669a8fd141b590b9e3d6e545b53";
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest); assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
} }
#[test] #[test]
fn hash_string_03() { fn hash_string_03() {
let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let correct_digest = "3e85ef4e523b431b4039bc0b67a8ed80e91be1dc7d650ce6c8a498ba97663cb0"; let correct_digest = "1f8676c857099cf4fad280a6a9305f372e0d75341e52aa2b9cb3906324f9a4c7";
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest); assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
} }
#[test] #[test]
fn hash_string_04() { fn hash_string_04() {
let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
let correct_digest = "793360cfe767f993a3ed4d91238a5042c2b6746c9767cdd77f1b3dbd5f632894"; let correct_digest = "61675e48dd173196dc348114ef5dd1f38a04969e6436e5538d6da4ab0e6ab7cc";
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest); assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
} }
@ -251,7 +245,7 @@ mod test {
let test_string4 = "cup"; let test_string4 = "cup";
let test_string5 = let test_string5 =
"idatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; "idatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
let correct_digest = "793360cfe767f993a3ed4d91238a5042c2b6746c9767cdd77f1b3dbd5f632894"; let correct_digest = "61675e48dd173196dc348114ef5dd1f38a04969e6436e5538d6da4ab0e6ab7cc";
let mut hasher = LedHash256::new(); let mut hasher = LedHash256::new();
hasher.update(test_string1.as_bytes()); hasher.update(test_string1.as_bytes());
@ -276,15 +270,15 @@ mod test {
assert_eq!( assert_eq!(
digest_to_string(hash(len_0)), digest_to_string(hash(len_0)),
"fcdfdd47e35abc0d7ebd5c24aaa81b896c07f2cb0f2dc6395fdda6fc8fb12991", "0000000000000000000000000000000000000000000000000000000000000000",
); );
assert_eq!( assert_eq!(
digest_to_string(hash(len_1)), digest_to_string(hash(len_1)),
"0f11e90ef9373089f0a337cd1af6c923a2e5d679e92782d3e51da364a34d33e9", "8311a9b6f5e6f4d31707eec6bab81a406db32289be10d2dd93b2b3c0cf6ddac3",
); );
assert_eq!( assert_eq!(
digest_to_string(hash(len_2)), digest_to_string(hash(len_2)),
"70f4c3fdc580f268bd8a81e2163cafa9109ea193f1a062c12ef72996b396043f", "20dafb63358a6a56d7b185e0c15093787397c551c8c90afc0bb5ee099aacb668",
); );
} }
} }