zkevm_opcode_defs/definitions/abi/
fat_pointer.rsuse super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FatPointer {
pub offset: u32, pub memory_page: u32, pub start: u32, pub length: u32, }
bitflags::bitflags! {
pub struct FatPointerValidationException: u64 {
const OFFSET_IS_NOT_ZERO_WHEN_EXPECTED = 1u64 << 0;
const DEREF_BEYOND_HEAP_RANGE = 1u64 << 1;
}
}
impl FatPointer {
pub const FAT_POINTER_PACKED_WIDTH: u32 = 128;
pub const fn empty() -> Self {
Self {
length: 0,
start: 0,
memory_page: 0,
offset: 0,
}
}
pub const fn from_u256(value: U256) -> Self {
let raw_value = value.0;
let offset = raw_value[0] as u32;
let memory_page = (raw_value[0] >> 32) as u32;
let start = raw_value[1] as u32;
let length = (raw_value[1] >> 32) as u32;
Self {
offset,
memory_page,
start,
length,
}
}
pub fn validate(self, is_fresh: bool) -> FatPointerValidationException {
let mut exceptions = FatPointerValidationException::empty();
if is_fresh && self.offset != 0 {
exceptions.set(
FatPointerValidationException::OFFSET_IS_NOT_ZERO_WHEN_EXPECTED,
true,
);
}
let (_, of) = self.start.overflowing_add(self.length);
if of {
exceptions.set(FatPointerValidationException::DEREF_BEYOND_HEAP_RANGE, true);
}
exceptions
}
pub const fn validate_in_bounds(&self) -> bool {
let in_bounds = self.offset < self.length;
in_bounds
}
pub const fn validate_as_slice(&self) -> bool {
let is_valid_slice = self.offset <= self.length;
is_valid_slice
}
pub const fn is_trivial(&self) -> bool {
self.length == 0 && self.offset == 0
}
pub const fn to_u256(self) -> U256 {
let mut result = U256::zero();
result.0[0] = (self.offset as u64) | ((self.memory_page as u64) << 32);
result.0[1] = (self.start as u64) | ((self.length as u64) << 32);
result
}
}