tempo_commonware_node/consensus/
digest.rs1use std::ops::Deref;
4
5use alloy_primitives::B256;
6use commonware_codec::{FixedSize, Read, ReadExt as _, Write};
7use commonware_utils::{Array, Span};
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
11#[repr(transparent)]
12pub struct Digest(pub B256);
13
14impl Array for Digest {}
15
16impl AsRef<[u8]> for Digest {
17 fn as_ref(&self) -> &[u8] {
18 self.0.as_ref()
19 }
20}
21
22impl Deref for Digest {
23 type Target = [u8];
24
25 fn deref(&self) -> &Self::Target {
26 self.0.deref()
27 }
28}
29
30impl commonware_math::algebra::Random for Digest {
31 fn random(mut rng: impl rand_core::CryptoRngCore) -> Self {
38 let mut array = B256::ZERO;
39 rng.fill_bytes(&mut *array);
40 Self(array)
41 }
42}
43
44impl commonware_cryptography::Digest for Digest {
45 const EMPTY: Self = Self(B256::ZERO);
46}
47
48impl FixedSize for Digest {
49 const SIZE: usize = 32;
50}
51
52impl std::fmt::Display for Digest {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 self.0.fmt(f)
55 }
56}
57
58impl Read for Digest {
59 type Cfg = ();
60
61 fn read_cfg(
62 buf: &mut impl bytes::Buf,
63 _cfg: &Self::Cfg,
64 ) -> Result<Self, commonware_codec::Error> {
65 let array = <[u8; 32]>::read(buf)?;
66 Ok(Self(B256::new(array)))
67 }
68}
69
70impl Span for Digest {}
71
72impl Write for Digest {
73 fn write(&self, buf: &mut impl bytes::BufMut) {
74 self.0.write(buf)
75 }
76}