implement BYTE

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-06-04 18:45:06 +02:00
parent 354b1c8d79
commit 2d0a0e2e81
4 changed files with 118 additions and 11 deletions
+18
View File
@@ -154,6 +154,12 @@ sol!(
}
);
sol!(
contract Bitwise {
function opByte(uint i, uint x) public payable returns (uint ret);
}
);
impl Contract {
/// Execute the contract.
///
@@ -508,6 +514,18 @@ impl Contract {
calldata: Value::balance_ofCall::new((address,)).abi_encode(),
}
}
pub fn bitwise_byte(index: U256, value: U256) -> Self {
let code = include_str!("../contracts/Bitwise.sol");
let name = "Bitwise";
Self {
name,
evm_runtime: crate::compile_evm_bin_runtime(name, code),
pvm_runtime: crate::compile_blob(name, code),
calldata: Bitwise::opByteCall::new((index, value)).abi_encode(),
}
}
}
#[cfg(test)]
+24
View File
@@ -1,3 +1,5 @@
use std::str::FromStr;
use alloy_primitives::{keccak256, Address, FixedBytes, B256, I256, U256};
use alloy_sol_types::{sol, SolCall, SolValue};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
@@ -590,3 +592,25 @@ fn balance() {
let received = U256::from_be_slice(&output.data);
assert_eq!(expected, received)
}
#[test]
fn bitwise_byte() {
assert_success(&Contract::bitwise_byte(U256::ZERO, U256::ZERO), true);
assert_success(&Contract::bitwise_byte(U256::ZERO, U256::MAX), true);
assert_success(&Contract::bitwise_byte(U256::MAX, U256::ZERO), true);
assert_success(
&Contract::bitwise_byte(U256::from_str("18446744073709551619").unwrap(), U256::MAX),
true,
);
let de_bruijn_sequence =
hex::decode("4060503824160d0784426150b864361d0f88c4a27148ac5a2f198d46e391d8f4").unwrap();
let value = U256::from_be_bytes::<32>(de_bruijn_sequence.clone().try_into().unwrap());
for (index, byte) in de_bruijn_sequence.iter().enumerate() {
let (_, output) = assert_success(&Contract::bitwise_byte(U256::from(index), value), true);
let expected = U256::from(*byte as i32);
let received = U256::abi_decode(&output.data, true).unwrap();
assert_eq!(expected, received)
}
}