revm_precompile/
utilities.rs

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