revm_precompile/
utilities.rs

1//! Utility function that precompiles use, padding and converting between types.
2use primitives::{b256, Bytes, B256};
3use std::borrow::Cow;
4
5/// Right-pads the given slice at `offset` with zeroes until `LEN`.
6///
7/// Returns the first `LEN` bytes if it does not need padding.
8#[inline]
9pub fn right_pad_with_offset<const LEN: usize>(data: &[u8], offset: usize) -> Cow<'_, [u8; LEN]> {
10    right_pad(data.get(offset..).unwrap_or_default())
11}
12
13/// Right-pads the given slice at `offset` with zeroes until `len`.
14///
15/// Returns the first `len` bytes if it does not need padding.
16#[inline]
17pub fn right_pad_with_offset_vec(data: &[u8], offset: usize, len: usize) -> Cow<'_, [u8]> {
18    right_pad_vec(data.get(offset..).unwrap_or_default(), len)
19}
20
21/// Right-pads the given slice with zeroes until `LEN`.
22///
23/// Returns the first `LEN` bytes if it does not need padding.
24#[inline]
25pub fn right_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
26    if let Some(data) = data.get(..LEN) {
27        Cow::Borrowed(data.try_into().unwrap())
28    } else {
29        let mut padded = [0; LEN];
30        padded[..data.len()].copy_from_slice(data);
31        Cow::Owned(padded)
32    }
33}
34
35/// Right-pads the given slice with zeroes until `len`.
36///
37/// Returns the first `len` bytes if it does not need padding.
38#[inline]
39pub fn right_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
40    if let Some(data) = data.get(..len) {
41        Cow::Borrowed(data)
42    } else {
43        let mut padded = vec![0; len];
44        padded[..data.len()].copy_from_slice(data);
45        Cow::Owned(padded)
46    }
47}
48
49/// Left-pads the given slice with zeroes until `LEN`.
50///
51/// Returns the first `LEN` bytes if it does not need padding.
52#[inline]
53pub fn left_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
54    if let Some(data) = data.get(..LEN) {
55        Cow::Borrowed(data.try_into().unwrap())
56    } else {
57        let mut padded = [0; LEN];
58        padded[LEN - data.len()..].copy_from_slice(data);
59        Cow::Owned(padded)
60    }
61}
62
63/// Left-pads the given slice with zeroes until `len`.
64///
65/// Returns the first `len` bytes if it does not need padding.
66#[inline]
67pub fn left_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
68    if let Some(data) = data.get(..len) {
69        Cow::Borrowed(data)
70    } else {
71        let mut padded = vec![0; len];
72        padded[len - data.len()..].copy_from_slice(data);
73        Cow::Owned(padded)
74    }
75}
76
77/// Converts a boolean to a left-padded 32-byte [`Bytes`] value.
78///
79/// This is optimized to not allocate at runtime by using 2 static arrays.
80#[inline]
81pub const fn bool_to_bytes32(value: bool) -> Bytes {
82    Bytes::from_static(&bool_to_b256(value).0)
83}
84
85/// Converts a boolean to a left-padded [`B256`] value.
86///
87/// This is optimized to not allocate at runtime by using 2 static arrays.
88#[inline]
89pub const fn bool_to_b256(value: bool) -> &'static B256 {
90    const TRUE: &B256 =
91        &b256!("0x0000000000000000000000000000000000000000000000000000000000000001");
92    const FALSE: &B256 =
93        &b256!("0x0000000000000000000000000000000000000000000000000000000000000000");
94    if value {
95        TRUE
96    } else {
97        FALSE
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn get_with_right_padding() {
107        let data = [1, 2, 3, 4];
108        let padded = right_pad_with_offset::<8>(&data, 4);
109        assert!(matches!(padded, Cow::Owned(_)));
110        assert_eq!(padded[..], [0, 0, 0, 0, 0, 0, 0, 0]);
111        let padded = right_pad_with_offset_vec(&data, 4, 8);
112        assert!(matches!(padded, Cow::Owned(_)));
113        assert_eq!(padded[..], [0, 0, 0, 0, 0, 0, 0, 0]);
114
115        let data = [1, 2, 3, 4, 5, 6, 7, 8];
116        let padded = right_pad_with_offset::<8>(&data, 0);
117        assert!(matches!(padded, Cow::Borrowed(_)));
118        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
119        let padded = right_pad_with_offset_vec(&data, 0, 8);
120        assert!(matches!(padded, Cow::Borrowed(_)));
121        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
122
123        let data = [1, 2, 3, 4, 5, 6, 7, 8];
124        let padded = right_pad_with_offset::<8>(&data, 4);
125        assert!(matches!(padded, Cow::Owned(_)));
126        assert_eq!(padded[..], [5, 6, 7, 8, 0, 0, 0, 0]);
127        let padded = right_pad_with_offset_vec(&data, 4, 8);
128        assert!(matches!(padded, Cow::Owned(_)));
129        assert_eq!(padded[..], [5, 6, 7, 8, 0, 0, 0, 0]);
130    }
131
132    #[test]
133    fn right_padding() {
134        let data = [1, 2, 3, 4];
135        let padded = right_pad::<8>(&data);
136        assert!(matches!(padded, Cow::Owned(_)));
137        assert_eq!(padded[..], [1, 2, 3, 4, 0, 0, 0, 0]);
138        let padded = right_pad_vec(&data, 8);
139        assert!(matches!(padded, Cow::Owned(_)));
140        assert_eq!(padded[..], [1, 2, 3, 4, 0, 0, 0, 0]);
141
142        let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
143        let padded = right_pad::<8>(&data);
144        assert!(matches!(padded, Cow::Borrowed(_)));
145        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
146        let padded = right_pad_vec(&data, 8);
147        assert!(matches!(padded, Cow::Borrowed(_)));
148        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
149    }
150
151    #[test]
152    fn left_padding() {
153        let data = [1, 2, 3, 4];
154        let padded = left_pad::<8>(&data);
155        assert!(matches!(padded, Cow::Owned(_)));
156        assert_eq!(padded[..], [0, 0, 0, 0, 1, 2, 3, 4]);
157        let padded = left_pad_vec(&data, 8);
158        assert!(matches!(padded, Cow::Owned(_)));
159        assert_eq!(padded[..], [0, 0, 0, 0, 1, 2, 3, 4]);
160
161        let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
162        let padded = left_pad::<8>(&data);
163        assert!(matches!(padded, Cow::Borrowed(_)));
164        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
165        let padded = left_pad_vec(&data, 8);
166        assert!(matches!(padded, Cow::Borrowed(_)));
167        assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
168    }
169
170    #[test]
171    fn bool2bytes() {
172        let f = bool_to_bytes32(false);
173        assert_eq!(f[..], [0; 32]);
174        let t = bool_to_bytes32(true);
175        assert_eq!(t.len(), 32);
176        assert_eq!(t[..31], [0; 31]);
177        assert_eq!(t[31], 1);
178    }
179}