Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: misnamed hash function #324

Open
larry0x opened this issue Jun 14, 2024 · 0 comments · May be fixed by #360
Open

Rust: misnamed hash function #324

larry0x opened this issue Jun 14, 2024 · 0 comments · May be fixed by #360
Labels
rust Issues pertaining to the Rust implementation

Comments

@larry0x
Copy link
Contributor

larry0x commented Jun 14, 2024

In host_functions.rs:

pub trait HostFunctionsProvider {
    /// The SHA-512 hash algorithm with its output truncated to 256 bits.
    fn sha2_512_truncated(message: &[u8]) -> [u8; 32];
}

Then, in ops.rs, this function is used to do the SHA512/256 hash:

pub(crate) fn do_hash<H: HostFunctionsProvider>(hash: HashOp, data: &[u8]) -> Hash {
    match hash {
        HashOp::Sha512256 => Hash::from(H::sha2_512_truncated(data)),
        // ...
    }
}

This is incorrect: "SHA2-512 truncated" and SHA512/256 are two distinct things:

  • "SHA2-512 truncated" means to do the SHA2-512 hash, which produces a 64-byte hash, and manually truncate off the second half;
  • SHA512/256 is a different hash algorithm (also part of the SHA2 family) that natively produces a 32-byte hash.

According to the proto definition, the intention is to use SHA512/256, meaning the function sha2_512_truncated is misnamed. It should be renamed to sha2_512_256. However, the implementation is correct.

Issue caught by @Rhaki

@larry0x larry0x changed the title Rust: incorrect hash used for SHA2-512 truncated Rust: misnamed hash function Jun 14, 2024
@crodriguezvega crodriguezvega added the rust Issues pertaining to the Rust implementation label Aug 27, 2024
@larry0x larry0x linked a pull request Aug 29, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust Issues pertaining to the Rust implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants