Skip to content

Commit

Permalink
Merge rust-bitcoin#3129: hashes:: Rename const_hash functions
Browse files Browse the repository at this point in the history
e7762e0 hashes:: Rename const_hash functions (Tobin C. Harding)

Pull request description:

  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 rust-bitcoin#3075.

  Close: rust-bitcoin#3075

ACKs for top commit:
  Kixunil:
    ACK e7762e0
  apoelstra:
    ACK e7762e0 successfully ran local tests

Tree-SHA512: 2b765bbbaa596d060a555495582b24175f660bf630de489cf0e0199f1c589f13f46dde5c9735bffece10a1ff116a70472f821df66c62a97fffb424f16e5568f9
  • Loading branch information
apoelstra committed Aug 6, 2024
2 parents 075ab9d + e7762e0 commit 50e3465
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions hashes/src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ impl Hash {
/// Computes hash from `bytes` 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.
Expand Down Expand Up @@ -203,14 +211,14 @@ impl Midstate {
/// Computes non-finalized hash of `sha256(tag) || sha256(tag)` for use in [`sha256t`]. It's
/// provided for use with [`sha256t`].
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 i = 0usize;
while i < buf.len() {
buf[i] = hash.0[i % hash.0.len()];
i += 1;
}
Self::const_hash(&buf, false)
Self::compute_midstate_unoptimized(&buf, false)
}
}

Expand Down Expand Up @@ -336,7 +344,7 @@ impl Midstate {
w
}

const fn const_hash(bytes: &[u8], finalize: bool) -> Self {
const fn compute_midstate_unoptimized(bytes: &[u8], finalize: bool) -> Self {
let mut state = [
0x6a09e667u32,
0xbb67ae85,
Expand Down Expand Up @@ -1018,15 +1026,15 @@ mod tests {
}

#[test]
fn const_hash() {
assert_eq!(Hash::hash(&[]), Hash::const_hash(&[]));
fn hash_unoptimized() {
assert_eq!(Hash::hash(&[]), Hash::hash_unoptimized(&[]));

let mut bytes = Vec::new();
for i in 0..256 {
bytes.push(i as u8);
assert_eq!(
Hash::hash(&bytes),
Hash::const_hash(&bytes),
Hash::hash_unoptimized(&bytes),
"hashes don't match for length {}",
i + 1
);
Expand Down

0 comments on commit 50e3465

Please sign in to comment.