Skip to content

Commit

Permalink
docs: update hasher documentation (#6392)
Browse files Browse the repository at this point in the history
Description
---
Updates the documentation for `DomainSeparatedBorshHasher`.

Motivation and Context
---
Some hashing is handled by `DomainSeparatedBorshHasher`, which uses
Borsh encoding under the hood instead of manually using length
prepending and byte slices. This PR updates the corresponding
documentation for clarity.

How Has This Been Tested?
---
No testing needed.

What process can a PR reviewer use to test or verify this change?
---
Confirm that the updated documentation is accurate.
  • Loading branch information
AaronFeickert authored Jul 10, 2024
1 parent 0156fc5 commit 50925b4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions hashing/src/borsh_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use borsh::{io, io::Write, BorshSerialize};
use digest::Digest;
use tari_crypto::hashing::DomainSeparation;

/// Domain separated borsh-encoding hasher.
/// A domain-separated hasher that uses Borsh internally to ensure hashing is canonical.
///
/// This assumes that any input type supports `BorshSerialize` canonically; that is, two different values of the same
/// type must serialize distinctly.
pub struct DomainSeparatedBorshHasher<M, D> {
writer: WriteHashWrapper<D>,
_m: PhantomData<M>,
Expand All @@ -50,6 +53,7 @@ impl<D: Digest + Default, M: DomainSeparation> DomainSeparatedBorshHasher<M, D>
self.writer.0.finalize()
}

/// Update the hasher using the Borsh encoding of the input, which is assumed to be canonical.
pub fn update_consensus_encode<T: BorshSerialize>(&mut self, data: &T) {
BorshSerialize::serialize(data, &mut self.writer)
.expect("Incorrect implementation of BorshSerialize encountered. Implementations MUST be infallible.");
Expand All @@ -62,7 +66,9 @@ impl<D: Digest + Default, M: DomainSeparation> DomainSeparatedBorshHasher<M, D>
}

/// This private struct wraps a Digest and implements the Write trait to satisfy the consensus encoding trait.
/// Do not use the DomainSeparatedHasher with this.
///
/// It's important not to use `DomainSeparatedHasher` with this, since that can inconsistently handle length prepending
/// and render hashing inconsistent. It's fine to use it with `DomainSeparatedBorshHasher`.
#[derive(Clone)]
struct WriteHashWrapper<D>(D);

Expand Down

0 comments on commit 50925b4

Please sign in to comment.