From 22e42ab86c673ec4b2865f98f20bf10139a9dbf9 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:01:54 +0200 Subject: [PATCH 1/8] fix test code being unnecessarily feature gated These cfg attributes were here because these tests used the `vec!` macro in the past. --- hashes/src/hash160.rs | 1 - hashes/src/hmac.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index 4fd7e402e..78039cde5 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -55,7 +55,6 @@ mod tests { use crate::{hash160, HashEngine}; #[derive(Clone)] - #[cfg(feature = "alloc")] struct Test { input: [u8; 65], output: [u8; 20], diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index 7b8c1a401..2f2bbaff4 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -164,7 +164,6 @@ impl<'de, T: GeneralHash + Deserialize<'de>> Deserialize<'de> for Hmac { #[cfg(test)] mod tests { #[test] - #[cfg(feature = "alloc")] fn test() { use crate::{sha256, GeneralHash as _, Hash as _, HashEngine, Hmac, HmacEngine}; From fa3a3afd02f1d1e6901ab02b81bfab863b7835a2 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:34:45 +0200 Subject: [PATCH 2/8] remove unnecessary slicing This was needed in older versions of Rust that are not supported anymore by this crate. --- hashes/src/hash160.rs | 2 +- hashes/src/ripemd160.rs | 2 +- hashes/src/sha1.rs | 2 +- hashes/src/sha256.rs | 2 +- hashes/src/sha256d.rs | 2 +- hashes/src/sha384.rs | 2 +- hashes/src/sha512.rs | 2 +- hashes/src/sha512_256.rs | 2 +- hashes/src/siphash24.rs | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index 78039cde5..3e96c8c1e 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -88,7 +88,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = hash160::Hash::hash(&test.input[..]); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index 2b2e10bb7..70e711d19 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -477,7 +477,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = ripemd160::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); assert_eq!(ripemd160::Hash::from_bytes_ref(&test.output), &hash); assert_eq!(ripemd160::Hash::from_bytes_mut(&mut test.output), &hash); diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index 5cc358ff5..66db27774 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -183,7 +183,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha1::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 1c566ff76..8e78e5136 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -916,7 +916,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index d63545ba7..afd2d10f1 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -76,7 +76,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256d::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/sha384.rs b/hashes/src/sha384.rs index 0b94ecf58..e28448440 100644 --- a/hashes/src/sha384.rs +++ b/hashes/src/sha384.rs @@ -123,7 +123,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha384::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index a62c161ac..b85e2fc51 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -370,7 +370,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha512::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index a21d33489..c1c2767c1 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -123,7 +123,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha512_256::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(&hash[..], &test.output); assert_eq!(&hash.to_string(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index 5ffd8a89f..8999c64b1 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -318,7 +318,7 @@ mod tests { for i in 0..64 { vin[i] = i as u8; - let vec = Hash::from_slice(&vecs[i][..]).unwrap(); + let vec = Hash::from_slice(&vecs[i]).unwrap(); let out = Hash::hash_with_keys(k0, k1, &vin[0..i]); assert_eq!(vec, out, "vec #{}", i); From 28ccf70fa66bec9608472b2c751b3347415dbcfe Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:41:16 +0200 Subject: [PATCH 3/8] remove unnecesarry borrow operator (`&`) `assert_eq!` already borrows the arguments, so this is redundant. --- hashes/src/hash160.rs | 4 ++-- hashes/src/ripemd160.rs | 4 ++-- hashes/src/sha1.rs | 4 ++-- hashes/src/sha256.rs | 4 ++-- hashes/src/sha256d.rs | 4 ++-- hashes/src/sha384.rs | 4 ++-- hashes/src/sha512.rs | 4 ++-- hashes/src/sha512_256.rs | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index 3e96c8c1e..9e85271ba 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -88,8 +88,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = hash160::Hash::hash(&test.input[..]); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = hash160::Hash::engine(); diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index 70e711d19..dffdfaf40 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -477,8 +477,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = ripemd160::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); assert_eq!(ripemd160::Hash::from_bytes_ref(&test.output), &hash); assert_eq!(ripemd160::Hash::from_bytes_mut(&mut test.output), &hash); diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index 66db27774..5c586c07e 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -183,8 +183,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha1::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha1::Hash::engine(); diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 8e78e5136..b3d0f0e59 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -916,8 +916,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha256::Hash::engine(); diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index afd2d10f1..6e26ea643 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -76,8 +76,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256d::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha256d::Hash::engine(); diff --git a/hashes/src/sha384.rs b/hashes/src/sha384.rs index e28448440..6ab941449 100644 --- a/hashes/src/sha384.rs +++ b/hashes/src/sha384.rs @@ -123,8 +123,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha384::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha384::Hash::engine(); diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index b85e2fc51..1642129d4 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -370,8 +370,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha512::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha512::Hash::engine(); diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index c1c2767c1..1f373b11f 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -123,8 +123,8 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha512_256::Hash::hash(test.input.as_bytes()); assert_eq!(hash, test.output_str.parse::().expect("parse hex")); - assert_eq!(&hash[..], &test.output); - assert_eq!(&hash.to_string(), &test.output_str); + assert_eq!(hash[..], test.output); + assert_eq!(hash.to_string(), test.output_str); // Hash through engine, checking that we can input byte by byte let mut engine = sha512_256::Hash::engine(); From 969864e3b0bf0aef2095a06f401370e907cd715e Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:45:07 +0200 Subject: [PATCH 4/8] use fixed size array if possible, otherwise `&'static [u8]` (or `&'static str`) This test is now consistent with other tests. --- hashes/src/hmac.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index 2f2bbaff4..4a90cc043 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -168,10 +168,10 @@ mod tests { use crate::{sha256, GeneralHash as _, Hash as _, HashEngine, Hmac, HmacEngine}; #[derive(Clone)] - struct Test<'a> { - key: &'a [u8], - input: &'a [u8], - output: &'a [u8], + struct Test { + key: &'static [u8], + input: &'static [u8], + output: [u8; 32], } #[rustfmt::skip] @@ -180,9 +180,9 @@ mod tests { // Sadly the RFC2104 test vectors all use MD5 as their underlying hash function, // which of course this library does not support. Test { - key: &[ 0x0b; 20], - input: &[0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65], - output: &[ + key: &[ 0x0b; 20 ], + input: &[ 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 ], + output: [ 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, @@ -197,7 +197,7 @@ mod tests { 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x3f, ], - output: &[ + output: [ 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, @@ -207,7 +207,7 @@ mod tests { Test { key: &[ 0xaa; 20 ], input: &[ 0xdd; 50 ], - output: &[ + output: [ 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, @@ -222,7 +222,7 @@ mod tests { 0x19 ], input: &[ 0xcd; 50 ], - output: &[ + output: [ 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, @@ -240,7 +240,7 @@ mod tests { 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79, 0x20, 0x46, 0x69, 0x72, 0x73, 0x74, ], - output: &[ + output: [ 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, @@ -270,7 +270,7 @@ mod tests { 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c, 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e, ], - output: &[ + output: [ 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, @@ -284,7 +284,7 @@ mod tests { engine.input(test.input); let hash = Hmac::::from_engine(engine); assert_eq!(hash.as_ref(), test.output); - assert_eq!(hash.as_byte_array(), test.output); + assert_eq!(hash.to_byte_array(), test.output); } } From 55749d6f61446dce76bcfe555cecfa7db07bf219 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:49:22 +0200 Subject: [PATCH 5/8] use `hash.to_byte_array` to check equality with `test.output` Tests in 'hashes' used various ways to do this that looked different but did the same. --- hashes/src/hash160.rs | 2 +- hashes/src/ripemd160.rs | 2 +- hashes/src/sha1.rs | 2 +- hashes/src/sha256.rs | 2 +- hashes/src/sha256d.rs | 2 +- hashes/src/sha384.rs | 2 +- hashes/src/sha512.rs | 2 +- hashes/src/sha512_256.rs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index 9e85271ba..b59d0379b 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -98,7 +98,7 @@ mod tests { } let manual_hash = Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index dffdfaf40..3935ea25f 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -489,7 +489,7 @@ mod tests { } let manual_hash = ripemd160::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.as_byte_array(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index 5c586c07e..ea9ce848f 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -193,7 +193,7 @@ mod tests { } let manual_hash = sha1::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.as_byte_array(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index b3d0f0e59..0e23142e7 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -926,7 +926,7 @@ mod tests { } let manual_hash = sha256::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index 6e26ea643..f0be7c0de 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -92,7 +92,7 @@ mod tests { let sha2d_hash = sha2_hash.hash_again(); assert_eq!(hash, sha2d_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/sha384.rs b/hashes/src/sha384.rs index 6ab941449..2531624ed 100644 --- a/hashes/src/sha384.rs +++ b/hashes/src/sha384.rs @@ -133,7 +133,7 @@ mod tests { } let manual_hash = sha384::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } } diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index 1642129d4..689691b89 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -380,7 +380,7 @@ mod tests { } let manual_hash = sha512::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index 1f373b11f..3c303c8c2 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -133,7 +133,7 @@ mod tests { } let manual_hash = sha512_256::Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.to_byte_array(), test.output); } } } From e83830dcfcbd47a9b9651eca539f5c38ef63eb68 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 13:51:13 +0200 Subject: [PATCH 6/8] use slice instead of array to not have to hardcode the length --- bitcoin/src/pow.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index 8809830e0..5e0782fae 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -1930,7 +1930,7 @@ mod tests { #[test] fn work_log2() { // Compare work log2 to historical Bitcoin Core values found in Core logs. - let tests: [(u128, f64); 5] = [ + let tests: &[(u128, f64)] = &[ // (chainwork, core log2) // height (0x200020002, 33.000022), // 1 (0xa97d67041c5e51596ee7, 79.405055), // 308004 @@ -1939,7 +1939,7 @@ mod tests { (0x2ef447e01d1642c40a184ada, 93.553183), // 738965 ]; - for (chainwork, core_log2) in tests { + for &(chainwork, core_log2) in tests { // Core log2 in the logs is rounded to 6 decimal places. let log2 = (Work::from(chainwork).log2() * 1e6).round() / 1e6; assert_eq!(log2, core_log2) From a14cdaf8590677b6b53e275c1ea45c78183fc9a1 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 21:08:23 +0200 Subject: [PATCH 7/8] don't enable std by default when testing - make tests no_std compatible by adding imports to alloc or std - feature gate tests behind the 'alloc' feature if they use anything from 'alloc' (like the `format!` macro) - schemars feature enables alloc --- hashes/Cargo.toml | 1 + hashes/src/hash160.rs | 2 ++ hashes/src/hkdf.rs | 4 ++-- hashes/src/hmac.rs | 2 +- hashes/src/impls.rs | 4 +++- hashes/src/internal_macros.rs | 10 +++++++++- hashes/src/lib.rs | 25 ++++++++++++++----------- hashes/src/ripemd160.rs | 2 ++ hashes/src/sha1.rs | 2 ++ hashes/src/sha256.rs | 21 +++++++++++++++------ hashes/src/sha256d.rs | 6 ++++++ hashes/src/sha256t.rs | 16 +++++++++++++++- hashes/src/sha384.rs | 2 ++ hashes/src/sha512.rs | 2 ++ hashes/src/sha512_256.rs | 2 ++ hashes/src/util.rs | 12 ++++++++++++ 16 files changed, 90 insertions(+), 23 deletions(-) diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml index 404fbb4a3..54bd229a7 100644 --- a/hashes/Cargo.toml +++ b/hashes/Cargo.toml @@ -17,6 +17,7 @@ exclude = ["tests", "contrib"] default = ["std"] std = ["alloc", "hex/std", "bitcoin-io/std"] alloc = ["hex/alloc"] +schemars = ["dep:schemars", "alloc"] # If you want I/O you must enable either "std" or "io". io = ["bitcoin-io"] # Smaller (but slower) implementation of sha256, sha512 and ripemd160 diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index b59d0379b..ba4d4724e 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -51,6 +51,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use super::Hash; use crate::{hash160, HashEngine}; diff --git a/hashes/src/hkdf.rs b/hashes/src/hkdf.rs index 1de1cd616..452c46a58 100644 --- a/hashes/src/hkdf.rs +++ b/hashes/src/hkdf.rs @@ -5,9 +5,9 @@ //! Implementation based on RFC5869, but the interface is scoped //! to BIP324's requirements. -#[cfg(all(feature = "alloc", not(feature = "std")))] +#[cfg(feature = "alloc")] use alloc::vec; -#[cfg(all(feature = "alloc", not(feature = "std")))] +#[cfg(feature = "alloc")] use alloc::vec::Vec; use core::fmt; diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index 4a90cc043..ce501a505 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -23,7 +23,7 @@ pub struct Hmac(T); impl schemars::JsonSchema for Hmac { fn is_referenceable() -> bool { ::is_referenceable() } - fn schema_name() -> std::string::String { ::schema_name() } + fn schema_name() -> alloc::string::String { ::schema_name() } fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { ::json_schema(gen) diff --git a/hashes/src/impls.rs b/hashes/src/impls.rs index 09f46496f..2e69309c8 100644 --- a/hashes/src/impls.rs +++ b/hashes/src/impls.rs @@ -81,8 +81,10 @@ impl_write!( T: crate::GeneralHash ); -#[cfg(test)] +#[cfg(all(test, feature = "alloc"))] // right now every test here depends on `alloc` mod tests { + use alloc::format; + use bitcoin_io::Write; use crate::{ diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index a105a66c3..acae8df6d 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -224,9 +224,17 @@ macro_rules! hash_type_no_default { #[cfg(feature = "schemars")] impl schemars::JsonSchema for Hash { - fn schema_name() -> String { "Hash".to_owned() } + fn schema_name() -> alloc::string::String { + use alloc::borrow::ToOwned; + + "Hash".to_owned() + } fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + use alloc::borrow::ToOwned; + use alloc::boxed::Box; + use alloc::string::String; + let len = $bits / 8; let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); schema.string = Some(Box::new(schemars::schema::StringValidation { diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 897f97b8f..0eadb86e7 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -62,7 +62,7 @@ //! # fn main() {} //! ``` -#![cfg_attr(all(not(test), not(feature = "std")), no_std)] +#![cfg_attr(not(feature = "std"), no_std)] // Experimental features we need. #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![cfg_attr(bench, feature(test))] @@ -75,9 +75,9 @@ #![allow(clippy::manual_range_contains)] // More readable than clippy's format. #![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454 -#[cfg(all(feature = "alloc", not(feature = "std")))] +#[cfg(feature = "alloc")] extern crate alloc; -#[cfg(any(test, feature = "std"))] + extern crate core; #[cfg(feature = "bitcoin-io")] @@ -334,16 +334,19 @@ mod tests { struct TestNewtype2(sha256d::Hash); } - #[rustfmt::skip] - const DUMMY: TestNewtype = TestNewtype::from_byte_array([ - 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, - 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8a, - 0x14, 0x25, 0x36, 0x47, 0x58, 0x69, 0x7a, 0x8b, - 0x15, 0x26, 0x37, 0x48, 0x59, 0x6a, 0x7b, 0x8c, - ]); - #[test] + #[cfg(feature = "alloc")] fn newtype_fmt_roundtrip() { + use alloc::format; + + #[rustfmt::skip] + const DUMMY: TestNewtype = TestNewtype::from_byte_array([ + 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, + 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8a, + 0x14, 0x25, 0x36, 0x47, 0x58, 0x69, 0x7a, 0x8b, + 0x15, 0x26, 0x37, 0x48, 0x59, 0x6a, 0x7b, 0x8c, + ]); + let orig = DUMMY; let hex = format!("{}", orig); let rinsed = hex.parse::().expect("failed to parse hex"); diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index 3935ea25f..13a1248d4 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -413,6 +413,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{ripemd160, HashEngine}; #[derive(Clone)] diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index ea9ce848f..7ca56f0c1 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -132,6 +132,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{sha1, HashEngine}; #[derive(Clone)] diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 0e23142e7..d7377a212 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -864,12 +864,16 @@ impl HashEngine { #[cfg(test)] mod tests { + use core::array; + use super::*; use crate::{sha256, HashEngine}; #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + #[derive(Clone)] struct Test { input: &'static str, @@ -931,7 +935,10 @@ mod tests { } #[test] + #[cfg(feature = "alloc")] fn fmt_roundtrips() { + use alloc::format; + let hash = sha256::Hash::hash(b"some arbitrary bytes"); let hex = format!("{}", hash); let rinsed = hex.parse::().expect("failed to parse hex"); @@ -1023,14 +1030,13 @@ mod tests { #[test] fn hash_unoptimized() { - assert_eq!(Hash::hash(&[]), Hash::hash_unoptimized(&[])); + let bytes: [u8; 256] = array::from_fn(|i| i as u8); - let mut bytes = Vec::new(); - for i in 0..256 { - bytes.push(i as u8); + for i in 0..=256 { + let bytes = &bytes[0..i]; assert_eq!( - Hash::hash(&bytes), - Hash::hash_unoptimized(&bytes), + Hash::hash(bytes), + Hash::hash_unoptimized(bytes), "hashes don't match for length {}", i + 1 ); @@ -1050,7 +1056,10 @@ mod tests { fn const_midstate() { assert_eq!(Midstate::hash_tag(b"TapLeaf"), TAP_LEAF_MIDSTATE,) } #[test] + #[cfg(feature = "alloc")] fn regression_midstate_debug_format() { + use alloc::format; + let want = "Midstate { bytes: 9ce0e4e67c116c3938b3caf2c30f5089d3f3936c47636e607db33eeaddc6f0c9, length: 64 }"; let got = format!("{:?}", TAP_LEAF_MIDSTATE); assert_eq!(got, want); diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index f0be7c0de..72f246887 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -43,11 +43,14 @@ fn from_engine(e: HashEngine) -> Hash { #[cfg(test)] mod tests { + #[allow(unused_imports)] // whether this is used depends on features use crate::sha256d; #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{sha256, HashEngine}; #[derive(Clone)] @@ -97,7 +100,10 @@ mod tests { } #[test] + #[cfg(feature = "alloc")] fn fmt_roundtrips() { + use alloc::format; + let hash = sha256d::Hash::hash(b"some arbitrary bytes"); let hex = format!("{}", hash); let rinsed = hex.parse::().expect("failed to parse hex"); diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 6061e743c..655e58783 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -23,9 +23,17 @@ pub struct Hash([u8; 32], PhantomData); #[cfg(feature = "schemars")] impl schemars::JsonSchema for Hash { - fn schema_name() -> String { "Hash".to_owned() } + fn schema_name() -> alloc::string::String { + use alloc::borrow::ToOwned; + + "Hash".to_owned() + } fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + use alloc::borrow::ToOwned; + use alloc::boxed::Box; + use alloc::string::String; + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); schema.string = Some(Box::new(schemars::schema::StringValidation { max_length: Some(32 * 2), @@ -353,6 +361,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn manually_created_sha256t_hash_type() { + use alloc::string::ToString; + assert_eq!(TestHash::hash(&[0]).to_string(), HASH_ZERO_FORWARD); } @@ -370,6 +380,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn macro_created_sha256t_hash_type_backward() { + use alloc::string::ToString; + let inner = sha256t::Hash::::hash(&[0]); let hash = NewTypeHashBackward::from_byte_array(inner.to_byte_array()); assert_eq!(hash.to_string(), HASH_ZERO_BACKWARD); @@ -391,6 +403,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn macro_created_sha256t_hash_type_prints_forward() { + use alloc::string::ToString; + let inner = sha256t::Hash::::hash(&[0]); let hash = NewTypeHashForward::from_byte_array(inner.to_byte_array()); assert_eq!(hash.to_string(), HASH_ZERO_FORWARD); diff --git a/hashes/src/sha384.rs b/hashes/src/sha384.rs index 2531624ed..c588309cb 100644 --- a/hashes/src/sha384.rs +++ b/hashes/src/sha384.rs @@ -45,6 +45,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{sha384, HashEngine}; #[derive(Clone)] diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index 689691b89..4a4df0617 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -310,6 +310,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{sha512, HashEngine}; #[derive(Clone)] diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index 3c303c8c2..30b36e82b 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -55,6 +55,8 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test() { + use alloc::string::ToString; + use crate::{sha512_256, HashEngine}; #[derive(Clone)] diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 58d08d9f1..17a2dc8f2 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -400,28 +400,40 @@ mod test { } #[test] + #[cfg(feature = "alloc")] fn display() { + use alloc::format; + let want = "0000000000000000000000000000000000000000000000000000000000000000"; let got = format!("{}", TestHash::all_zeros()); assert_eq!(got, want) } #[test] + #[cfg(feature = "alloc")] fn display_alternate() { + use alloc::format; + let want = "0x0000000000000000000000000000000000000000000000000000000000000000"; let got = format!("{:#}", TestHash::all_zeros()); assert_eq!(got, want) } #[test] + #[cfg(feature = "alloc")] fn lower_hex() { + use alloc::format; + let want = "0000000000000000000000000000000000000000000000000000000000000000"; let got = format!("{:x}", TestHash::all_zeros()); assert_eq!(got, want) } #[test] + #[cfg(feature = "alloc")] fn lower_hex_alternate() { + use alloc::format; + let want = "0x0000000000000000000000000000000000000000000000000000000000000000"; let got = format!("{:#x}", TestHash::all_zeros()); assert_eq!(got, want) From dae42bef9de0af8d605fa73ba06f39c576732ccc Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Mon, 26 Aug 2024 21:45:18 +0200 Subject: [PATCH 8/8] do not enable bitcoin-io by default --- hashes/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml index 54bd229a7..4d048ea84 100644 --- a/hashes/Cargo.toml +++ b/hashes/Cargo.toml @@ -15,7 +15,7 @@ exclude = ["tests", "contrib"] [features] default = ["std"] -std = ["alloc", "hex/std", "bitcoin-io/std"] +std = ["alloc", "hex/std", "bitcoin-io?/std"] alloc = ["hex/alloc"] schemars = ["dep:schemars", "alloc"] # If you want I/O you must enable either "std" or "io".