1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use ethereum_types::H256;
use ethrex_crypto::Crypto;
use ethrex_rlp::{decode::RLPDecode, encode::RLPEncode, error::RLPDecodeError, structs::Encoder};
use crate::rkyv_utils::H256Wrapper;
/// Struct representing a trie node hash
/// If the encoded node is less than 32 bits, contains the encoded node itself
// TODO: Check if we can omit the Inline variant, as nodes will always be bigger than 32 bits in our use case
// TODO: Check if making this `Copy` can make the code less verbose at a reasonable performance cost
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Hash,
PartialOrd,
Ord,
Eq,
serde::Serialize,
serde::Deserialize,
rkyv::Serialize,
rkyv::Deserialize,
rkyv::Archive,
)]
pub enum NodeHash {
Hashed(#[rkyv(with=H256Wrapper)] H256),
// Inline is always len < 32. We need to store the length of the data, a u8 is enough.
Inline(([u8; 31], u8)),
}
impl AsRef<[u8]> for NodeHash {
fn as_ref(&self) -> &[u8] {
match self {
NodeHash::Inline((slice, len)) => &slice[0..(*len as usize)],
NodeHash::Hashed(x) => x.as_bytes(),
}
}
}
impl NodeHash {
/// Returns the `NodeHash` of an encoded node (encoded using the NodeEncoder)
///
/// `crypto` is a generic parameter, here and in every hashing function of
/// this crate, instead of a `&dyn Crypto`. Passing a concrete provider to a
/// `&dyn Crypto` parameter makes the compiler emit that provider's whole
/// vtable, which keeps every one of its methods linked in. This crate names
/// `NativeCrypto` where no provider is in scope, so a zkVM guest, which
/// hashes through its own provider, would also link `NativeCrypto`'s
/// modexp, pairing and signature code. With a generic parameter those calls
/// are dispatched statically and reference `keccak256` alone. `?Sized`
/// keeps accepting the callers that hold a `&dyn Crypto`.
pub fn from_encoded(encoded: &[u8], crypto: &(impl Crypto + ?Sized)) -> NodeHash {
if encoded.len() >= 32 {
let hash = crypto.keccak256(encoded);
NodeHash::Hashed(H256::from_slice(&hash))
} else {
NodeHash::from_slice(encoded)
}
}
/// Converts a slice of an already hashed data (in case it's not inlineable) to a NodeHash.
/// Panics if the slice is over 32 bytes
/// If you need to hash it in case its len >= 32 see `from_encoded`
pub(crate) fn from_slice(slice: &[u8]) -> NodeHash {
match slice.len() {
0..32 => {
let mut buffer = [0; 31];
buffer[0..slice.len()].copy_from_slice(slice);
NodeHash::Inline((buffer, slice.len() as u8))
}
_ => NodeHash::Hashed(H256::from_slice(slice)),
}
}
/// Returns the finalized hash
/// NOTE: This will hash smaller nodes, only use to get the final root hash, not for intermediate node hashes
pub fn finalize(self, crypto: &(impl Crypto + ?Sized)) -> H256 {
match self {
NodeHash::Inline(_) => H256(crypto.keccak256(self.as_ref())),
NodeHash::Hashed(x) => x,
}
}
/// Returns true if the hash is valid
/// The hash will only be considered invalid if it is empty
/// Aka if it has a default value instead of being a product of hash computation
pub fn is_valid(&self) -> bool {
!matches!(self, NodeHash::Inline(v) if v.1 == 0)
}
/// Encodes this NodeHash with the given encoder.
pub fn encode<'a>(&self, mut encoder: Encoder<'a>) -> Encoder<'a> {
match self {
NodeHash::Inline(_) => {
encoder = encoder.encode_raw(self.as_ref());
}
NodeHash::Hashed(_) => {
encoder = encoder.encode_bytes(self.as_ref());
}
}
encoder
}
pub fn len(&self) -> usize {
match self {
NodeHash::Hashed(h256) => h256.as_bytes().len(),
NodeHash::Inline(value) => value.1 as usize,
}
}
pub fn is_empty(&self) -> bool {
match self {
NodeHash::Hashed(h256) => h256.as_bytes().is_empty(),
NodeHash::Inline(value) => value.1 == 0,
}
}
}
impl From<H256> for NodeHash {
fn from(value: H256) -> Self {
NodeHash::Hashed(value)
}
}
impl From<NodeHash> for Vec<u8> {
fn from(val: NodeHash) -> Self {
val.as_ref().to_vec()
}
}
impl From<&NodeHash> for Vec<u8> {
fn from(val: &NodeHash) -> Self {
val.as_ref().to_vec()
}
}
impl Default for NodeHash {
fn default() -> Self {
NodeHash::Inline(([0; 31], 0))
}
}
// Encoded as Vec<u8>
impl RLPEncode for NodeHash {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
RLPEncode::encode(&Into::<Vec<u8>>::into(self), buf)
}
fn length(&self) -> usize {
match self {
NodeHash::Hashed(_) => 33, // 1 byte prefix + 32 bytes
NodeHash::Inline((_, 0)) => 1, // if empty then it's encoded to RLP_NULL
NodeHash::Inline((_, len)) => *len as usize, // already encoded
}
}
}
impl RLPDecode for NodeHash {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), ethrex_rlp::error::RLPDecodeError> {
let (hash, rest): (Vec<u8>, &[u8]);
(hash, rest) = RLPDecode::decode_unfinished(rlp)?;
if hash.len() > 32 {
return Err(RLPDecodeError::InvalidLength);
}
let hash = NodeHash::from_slice(&hash);
Ok((hash, rest))
}
}