Skip to main content

witness_core/
hash.rs

1//! XRPL hashing.
2//!
3//! Almost every identifier in XRPL (the hash of a ledger, a transaction, a
4//! tree node, the signed data of a validation) is SHA-512Half: the first 32
5//! bytes of SHA-512 over the data preceded by a four-byte domain prefix.
6//! The prefix separates the spaces: the same bytes hashed as a transaction
7//! and as a tree node give different hashes.
8
9use sha2::{Digest, Sha512};
10
11/// Hashing domain prefixes.
12///
13/// Each value is three ASCII letters and a zero byte, as in the reference
14/// implementation (`HashPrefix.h` in xrpld). Only the ones needed so far are
15/// here; others get added together with the code that uses them.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[repr(u32)]
18pub enum HashPrefix {
19    /// Transaction ID: `TXN\0`.
20    TransactionId = 0x5458_4E00,
21    /// Leaf of the transaction tree (a transaction with its metadata): `SND\0`.
22    TxNode = 0x534E_4400,
23    /// Leaf of the state tree: `MLN\0`.
24    LeafNode = 0x4D4C_4E00,
25    /// Inner node of either tree: `MIN\0`.
26    InnerNode = 0x4D49_4E00,
27    /// Ledger header: `LWR\0`.
28    LedgerMaster = 0x4C57_5200,
29    /// Signed part of a validation: `VAL\0`.
30    Validation = 0x5641_4C00,
31    /// Signed part of a consensus proposal: `PRP\0`.
32    Proposal = 0x5052_5000,
33    /// Signed part of a validator manifest: `MAN\0`.
34    Manifest = 0x4D41_4E00,
35}
36
37impl HashPrefix {
38    /// The four prefix bytes, in the order they precede the data.
39    pub const fn to_bytes(self) -> [u8; 4] {
40        (self as u32).to_be_bytes()
41    }
42}
43
44/// SHA-512Half: the first 32 bytes of SHA-512 over the data.
45pub fn sha512_half(data: &[u8]) -> [u8; 32] {
46    first_half(Sha512::digest(data).as_slice())
47}
48
49/// SHA-512Half over the data with a domain prefix. This is how XRPL computes hashes.
50pub fn sha512_half_prefixed(prefix: HashPrefix, data: &[u8]) -> [u8; 32] {
51    let mut hasher = Sha512::new();
52    hasher.update(prefix.to_bytes());
53    hasher.update(data);
54    first_half(hasher.finalize().as_slice())
55}
56
57fn first_half(digest: &[u8]) -> [u8; 32] {
58    let mut out = [0u8; 32];
59    out.copy_from_slice(&digest[..32]);
60    out
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    /// SHA-512 of the empty string is a well-known reference value (FIPS 180-4);
68    /// SHA-512Half is its first 32 bytes.
69    #[test]
70    fn sha512_half_of_empty_input_matches_reference() {
71        assert_eq!(
72            hex::encode_upper(sha512_half(b"")),
73            "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE"
74        );
75    }
76
77    /// A prefix is exactly the ASCII bytes visible in its name.
78    #[test]
79    fn prefixes_spell_their_names() {
80        assert_eq!(&HashPrefix::Validation.to_bytes(), b"VAL\0");
81        assert_eq!(&HashPrefix::LedgerMaster.to_bytes(), b"LWR\0");
82        assert_eq!(&HashPrefix::TransactionId.to_bytes(), b"TXN\0");
83        assert_eq!(&HashPrefix::Manifest.to_bytes(), b"MAN\0");
84    }
85
86    /// A prefixed hash equals the hash of the prefix concatenated with the data.
87    #[test]
88    fn prefixed_hash_is_hash_of_concatenation() {
89        let data = b"xrpl-witness";
90        let mut joined = HashPrefix::Validation.to_bytes().to_vec();
91        joined.extend_from_slice(data);
92        assert_eq!(
93            sha512_half_prefixed(HashPrefix::Validation, data),
94            sha512_half(&joined)
95        );
96    }
97}