hashes:: Rename const_hash functions
There are a number of issues with the two `const_hash` functions in the `sha256` module: - The two `const_hash` functions in the `sha256` module differ slightly, one finalizes the hash and one is for computing the midstate. - They are inefficient and provided for usage for const context only. Fix both issues by renaming the functions as discussed in #3075. Close: #3075
This commit is contained in:
parent
5cca2f271d
commit
e7762e0612
|
@ -146,7 +146,15 @@ impl Hash {
|
||||||
/// Computes hash from `bytes` in `const` context.
|
/// Computes hash from `bytes` in `const` context.
|
||||||
///
|
///
|
||||||
/// Warning: this function is inefficient. It should be only used in `const` context.
|
/// Warning: this function is inefficient. It should be only used in `const` context.
|
||||||
pub const fn const_hash(bytes: &[u8]) -> Self { Hash(Midstate::const_hash(bytes, true).bytes) }
|
#[deprecated(since = "0.0.0-NEXT-RELEASE", note = "use Self::hash_unoptimized")]
|
||||||
|
pub const fn const_hash(bytes: &[u8]) -> Self { Hash::hash_unoptimized(bytes) }
|
||||||
|
|
||||||
|
/// Computes hash from `bytes` in `const` context.
|
||||||
|
///
|
||||||
|
/// Warning: this function is inefficient. It should be only used in `const` context.
|
||||||
|
pub const fn hash_unoptimized(bytes: &[u8]) -> Self {
|
||||||
|
Hash(Midstate::compute_midstate_unoptimized(bytes, true).bytes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unfinalized output of the SHA256 hash function.
|
/// Unfinalized output of the SHA256 hash function.
|
||||||
|
@ -203,14 +211,14 @@ impl Midstate {
|
||||||
/// Computes non-finalized hash of `sha256(tag) || sha256(tag)` for use in [`sha256t`]. It's
|
/// Computes non-finalized hash of `sha256(tag) || sha256(tag)` for use in [`sha256t`]. It's
|
||||||
/// provided for use with [`sha256t`].
|
/// provided for use with [`sha256t`].
|
||||||
pub const fn hash_tag(tag: &[u8]) -> Self {
|
pub const fn hash_tag(tag: &[u8]) -> Self {
|
||||||
let hash = Hash::const_hash(tag);
|
let hash = Hash::hash_unoptimized(tag);
|
||||||
let mut buf = [0u8; 64];
|
let mut buf = [0u8; 64];
|
||||||
let mut i = 0usize;
|
let mut i = 0usize;
|
||||||
while i < buf.len() {
|
while i < buf.len() {
|
||||||
buf[i] = hash.0[i % hash.0.len()];
|
buf[i] = hash.0[i % hash.0.len()];
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
Self::const_hash(&buf, false)
|
Self::compute_midstate_unoptimized(&buf, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,7 +344,7 @@ impl Midstate {
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn const_hash(bytes: &[u8], finalize: bool) -> Self {
|
const fn compute_midstate_unoptimized(bytes: &[u8], finalize: bool) -> Self {
|
||||||
let mut state = [
|
let mut state = [
|
||||||
0x6a09e667u32,
|
0x6a09e667u32,
|
||||||
0xbb67ae85,
|
0xbb67ae85,
|
||||||
|
@ -1018,15 +1026,15 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn const_hash() {
|
fn hash_unoptimized() {
|
||||||
assert_eq!(Hash::hash(&[]), Hash::const_hash(&[]));
|
assert_eq!(Hash::hash(&[]), Hash::hash_unoptimized(&[]));
|
||||||
|
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
for i in 0..256 {
|
for i in 0..256 {
|
||||||
bytes.push(i as u8);
|
bytes.push(i as u8);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Hash::hash(&bytes),
|
Hash::hash(&bytes),
|
||||||
Hash::const_hash(&bytes),
|
Hash::hash_unoptimized(&bytes),
|
||||||
"hashes don't match for length {}",
|
"hashes don't match for length {}",
|
||||||
i + 1
|
i + 1
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue