RustCrypto / hashes

Collection of cryptographic hash functions written in pure Rust
1.75k stars 238 forks source link

Replace assertions with debug assertions #578

Closed StackOverflowExcept1on closed 2 months ago

StackOverflowExcept1on commented 2 months ago

I'm using blake2 in no_std environment and it's quite strange that panic can occur in the release build. This increases the size of the output binary file.

blake2/src/macros.rs:                assert!(key_size <= $bytes::to_usize());
blake2/src/macros.rs:                assert!(output_size <= $bytes::to_usize());
blake2/src/macros.rs:                assert!(salt.len() <= length);
blake2/src/macros.rs:                assert!(persona.len() <= length);
sha3/src/macros.rs:                assert!((0x01..=0x7F).contains(&domain_separation));

You can also consider compile time assertions:

const _: () = assert!(42 <= 1024); //ok
const _: () = assert!(42 > 1024); //compile error
newpavlov commented 2 months ago

All those asserts check user-provided values. Their validity is important for algorithm correctness, so using debug asserts would be incorrect here.