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

FR: Loosen bound on merkle tree inputs from &[Vec<u8>] to &[impl AsRef<[u8]>] #1310

Closed
preston-evans98 opened this issue May 1, 2023 · 0 comments · Fixed by #1311
Closed
Labels
enhancement New feature or request

Comments

@preston-evans98
Copy link
Contributor

Description

Change the API of MerkleHash::hash_byte_vectors and simple_hash_from_byte_vectors to accept &[impl AsRef<[u8]>] as an argument instead of only &[Vec<u8>]. This will allow consumers to hash borrowed data, avoiding unnecessary copies.

Definition of "done"

The APIs of both functions have been updated.

pub fn simple_hash_from_byte_vectors<H>(byte_vecs: &[impl AsRef<[u8]>]) -> Hash
where
    H: MerkleHash + Default,
{
    let mut hasher = H::default();
    hasher.hash_byte_vectors(byte_vecs)
}

pub trait MerkleHash { 
    // ...
    fn hash_byte_vectors(&mut self, byte_vecs: &[impl AsRef<[u8]>]) -> Hash {
        let length = byte_vecs.len();
        match length {
            0 => self.empty_hash(),
            1 => self.leaf_hash(byte_vecs[0].as_ref()),
            _ => {
                let split = length.next_power_of_two() / 2;
                let left = self.hash_byte_vectors(&byte_vecs[..split]);
                let right = self.hash_byte_vectors(&byte_vecs[split..]);
                self.inner_hash(left, right)
            },
        }
    }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant