Skip to content

Commit 2aa0cd2

Browse files
authored
feat(precompile): add a bool to bytes32 helper function (#1170)
1 parent 93557e9 commit 2aa0cd2

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

crates/precompile/src/bn128.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
utilities::right_pad, Address, Error, Precompile, PrecompileResult, PrecompileWithAddress,
2+
utilities::{bool_to_bytes32, right_pad},
3+
Address, Error, Precompile, PrecompileResult, PrecompileWithAddress,
34
};
45
use bn::{AffineG1, AffineG2, Fq, Fq2, Group, Gt, G1, G2};
56
use revm_primitives::Bytes;
@@ -222,10 +223,7 @@ fn run_pair(
222223

223224
mul == Gt::one()
224225
};
225-
226-
let mut out = [0u8; 32];
227-
out[31] = success as u8;
228-
Ok((gas_used, out.into()))
226+
Ok((gas_used, bool_to_bytes32(success)))
229227
}
230228

231229
/*

crates/precompile/src/utilities.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
use revm_primitives::{b256, Bytes, B256};
12
use std::borrow::Cow;
23

34
/// Right-pads the given slice at `offset` with zeroes until `LEN`.
45
///
56
/// Returns the first `LEN` bytes if it does not need padding.
6-
#[inline(always)]
7+
#[inline]
78
pub fn right_pad_with_offset<const LEN: usize>(data: &[u8], offset: usize) -> Cow<'_, [u8; LEN]> {
89
right_pad(data.get(offset..).unwrap_or_default())
910
}
1011

1112
/// Right-pads the given slice at `offset` with zeroes until `len`.
1213
///
1314
/// Returns the first `len` bytes if it does not need padding.
14-
#[inline(always)]
15+
#[inline]
1516
pub fn right_pad_with_offset_vec(data: &[u8], offset: usize, len: usize) -> Cow<'_, [u8]> {
1617
right_pad_vec(data.get(offset..).unwrap_or_default(), len)
1718
}
1819

1920
/// Right-pads the given slice with zeroes until `LEN`.
2021
///
2122
/// Returns the first `LEN` bytes if it does not need padding.
22-
#[inline(always)]
23+
#[inline]
2324
pub fn right_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
2425
if let Some(data) = data.get(..LEN) {
2526
Cow::Borrowed(data.try_into().unwrap())
@@ -33,7 +34,7 @@ pub fn right_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
3334
/// Right-pads the given slice with zeroes until `len`.
3435
///
3536
/// Returns the first `len` bytes if it does not need padding.
36-
#[inline(always)]
37+
#[inline]
3738
pub fn right_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
3839
if let Some(data) = data.get(..len) {
3940
Cow::Borrowed(data)
@@ -47,7 +48,7 @@ pub fn right_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
4748
/// Left-pads the given slice with zeroes until `LEN`.
4849
///
4950
/// Returns the first `LEN` bytes if it does not need padding.
50-
#[inline(always)]
51+
#[inline]
5152
pub fn left_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
5253
if let Some(data) = data.get(..LEN) {
5354
Cow::Borrowed(data.try_into().unwrap())
@@ -61,7 +62,7 @@ pub fn left_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
6162
/// Left-pads the given slice with zeroes until `len`.
6263
///
6364
/// Returns the first `len` bytes if it does not need padding.
64-
#[inline(always)]
65+
#[inline]
6566
pub fn left_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
6667
if let Some(data) = data.get(..len) {
6768
Cow::Borrowed(data)
@@ -72,6 +73,28 @@ pub fn left_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
7273
}
7374
}
7475

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]
80+
pub 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]
88+
pub 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+
7598
#[cfg(test)]
7699
mod tests {
77100
use super::*;
@@ -140,4 +163,14 @@ mod tests {
140163
assert!(matches!(padded, Cow::Borrowed(_)));
141164
assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
142165
}
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+
}
143176
}

0 commit comments

Comments
 (0)