Skip to content

Commit 27a4325

Browse files
committed
docs: Improve documentation for the various hash_* methods
- Clarify differences. - Indicate preferred option. - Link to alternatives. Addresses #239.
1 parent 47853c0 commit 27a4325

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

twenty-first/src/math/tip5.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,15 @@ impl Tip5 {
561561
trace
562562
}
563563

564-
/// Hash 10 elements, or two digests. There is no padding because
565-
/// the input length is fixed.
564+
/// Hash 10 [`BFieldElement`]s.
565+
///
566+
/// There is no input-padding because the input length is fixed.
567+
///
568+
/// When you want to hash together two [`Digest`]s, use [`Self::hash_pair`]
569+
/// instead. In some rare cases you do want to hash a fixed-length string
570+
/// of individual [`BFieldElement`]s, which is why this function is exposed.
571+
///
572+
/// See also: [`Self::hash_pair`], [`Self::hash`], [`Self::hash_varlen`].
566573
pub fn hash_10(input: &[BFieldElement; 10]) -> [BFieldElement; Digest::LEN] {
567574
let mut sponge = Self::new(Domain::FixedLength);
568575

@@ -575,6 +582,12 @@ impl Tip5 {
575582
sponge.state[..Digest::LEN].try_into().unwrap()
576583
}
577584

585+
/// Hash two [`Digest`]s together.
586+
///
587+
/// This function is syntax sugar for calling [`Self::hash_10`] on the
588+
/// concatenation of the digests' values.
589+
///
590+
/// See also: [`Self::hash_10`], [`Self::hash`], [`Self::hash_varlen`].
578591
pub fn hash_pair(left: Digest, right: Digest) -> Digest {
579592
let mut sponge = Self::new(Domain::FixedLength);
580593
sponge.state[..Digest::LEN].copy_from_slice(&left.values());
@@ -586,16 +599,35 @@ impl Tip5 {
586599
Digest::new(digest_values)
587600
}
588601

602+
/// Hash an object based on its [`BFieldCodec`]-encoding.
603+
///
589604
/// Thin wrapper around [`hash_varlen`](Self::hash_varlen).
605+
///
606+
/// See also: [`Self::hash_10`], [`Self::hash_pair`], [`Self::hash_varlen`].
590607
pub fn hash<T: BFieldCodec>(value: &T) -> Digest {
591608
Self::hash_varlen(&value.encode())
592609
}
593610

594611
/// Hash a variable-length sequence of [`BFieldElement`].
595612
///
596-
/// - Apply the correct padding
597-
/// - [Sponge::pad_and_absorb_all()]
598-
/// - [Sponge::squeeze()] once.
613+
/// This function pads the input as its length is variable.
614+
///
615+
/// Note that [`Self::hash_varlen`] and [`Self::hash_10`] are different
616+
/// functions, even when the input to the former, after padding, agrees with
617+
/// the input to the latter. The difference comes from the initial value of
618+
/// the capacity-part of the state, which in the case of variable-length
619+
/// hashing is all-ones but in the case of fixed-length hashing is
620+
/// all-zeroes.
621+
///
622+
/// Prefer [`Self::hash`] whenever an object is being hashed whose type
623+
/// implements [`BFieldCodec`]. However, such an object is not always
624+
/// available, which is why this function is exposed.
625+
///
626+
/// See also: [`Self::hash_10`], [`Self::hash_pair`], [`Self::hash`].
627+
//
628+
// - Apply the correct padding
629+
// - [Sponge::pad_and_absorb_all()]
630+
// - [Sponge::squeeze()] once.
599631
pub fn hash_varlen(input: &[BFieldElement]) -> Digest {
600632
let mut sponge = Self::init();
601633
sponge.pad_and_absorb_all(input);

0 commit comments

Comments
 (0)