Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose some functions, structs and traits for reuse in protostar cheatcodes hint processor #3588

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum ProjectError {

/// Setup to 'db' to compile the file at the given path.
/// Returns the id of the generated crate.
fn setup_single_file_project(
pub fn setup_single_file_project(
db: &mut dyn SemanticGroup,
path: &Path,
) -> Result<CrateId, ProjectError> {
Expand Down
34 changes: 17 additions & 17 deletions crates/cairo-lang-runner/src/casm_run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn get_double_deref_maybe(
}

/// Extracts a parameter assumed to be a buffer, and converts it into a relocatable.
fn extract_relocatable(
pub fn extract_relocatable(
vm: &VirtualMachine,
buffer: &ResOperand,
) -> Result<Relocatable, VirtualMachineError> {
Expand Down Expand Up @@ -383,7 +383,7 @@ impl ResourceTracker for CairoHintProcessor<'_> {
}

/// Wrapper trait for a VM owner.
trait VMWrapper {
pub trait VMWrapper {
fn vm(&mut self) -> &mut VirtualMachine;
}
impl VMWrapper for VirtualMachine {
Expand All @@ -405,7 +405,7 @@ fn segment_with_data<T: Into<MaybeRelocatable>, Data: Iterator<Item = T>>(
}

/// A helper struct to continuously write and read from a buffer in the VM memory.
struct MemBuffer<'a> {
pub struct MemBuffer<'a> {
/// The VM to write to.
/// This is a trait so that we would borrow the actual VM only once.
vm: &'a mut dyn VMWrapper,
Expand All @@ -414,12 +414,12 @@ struct MemBuffer<'a> {
}
impl<'a> MemBuffer<'a> {
/// Creates a new buffer.
fn new(vm: &'a mut dyn VMWrapper, ptr: Relocatable) -> Self {
pub fn new(vm: &'a mut dyn VMWrapper, ptr: Relocatable) -> Self {
Self { vm, ptr }
}

/// Creates a new segment and returns a buffer wrapping it.
fn new_segment(vm: &'a mut dyn VMWrapper) -> Self {
pub fn new_segment(vm: &'a mut dyn VMWrapper) -> Self {
let ptr = vm.vm().add_memory_segment();
Self::new(vm, ptr)
}
Expand All @@ -434,64 +434,64 @@ impl<'a> MemBuffer<'a> {
/// Returns the felt252 value in the current position of the buffer and advances it by one.
/// Fails if the value is not a felt252.
/// Borrows the buffer since a reference is returned.
fn next_felt252(&mut self) -> Result<Cow<'_, Felt252>, MemoryError> {
pub fn next_felt252(&mut self) -> Result<Cow<'_, Felt252>, MemoryError> {
let ptr = self.next();
self.vm.vm().get_integer(ptr)
}

/// Returns the usize value in the current position of the buffer and advances it by one.
/// Fails with `MemoryError` if the value is not a felt252.
/// Panics if the value is not a usize.
fn next_usize(&mut self) -> Result<usize, MemoryError> {
pub fn next_usize(&mut self) -> Result<usize, MemoryError> {
Ok(self.next_felt252()?.to_usize().unwrap())
}

/// Returns the u128 value in the current position of the buffer and advances it by one.
/// Fails with `MemoryError` if the value is not a felt252.
/// Panics if the value is not a u128.
fn next_u128(&mut self) -> Result<u128, MemoryError> {
pub fn next_u128(&mut self) -> Result<u128, MemoryError> {
Ok(self.next_felt252()?.to_u128().unwrap())
}

/// Returns the u64 value in the current position of the buffer and advances it by one.
/// Fails with `MemoryError` if the value is not a felt252.
/// Panics if the value is not a u64.
fn next_u64(&mut self) -> Result<u64, MemoryError> {
pub fn next_u64(&mut self) -> Result<u64, MemoryError> {
Ok(self.next_felt252()?.to_u64().unwrap())
}

/// Returns the u256 value encoded starting from the current position of the buffer and advances
/// it by two.
/// Fails with `MemoryError` if any of the next two values are not felt252s.
/// Panics if any of the next two values are not u128.
fn next_u256(&mut self) -> Result<BigUint, MemoryError> {
pub fn next_u256(&mut self) -> Result<BigUint, MemoryError> {
Ok(self.next_u128()? + BigUint::from(self.next_u128()?).shl(128))
}

/// Returns the address value in the current position of the buffer and advances it by one.
/// Fails if the value is not an address.
fn next_addr(&mut self) -> Result<Relocatable, MemoryError> {
pub fn next_addr(&mut self) -> Result<Relocatable, MemoryError> {
let ptr = self.next();
self.vm.vm().get_relocatable(ptr)
}

/// Returns the array of integer values pointed to by the two next addresses in the buffer and
/// advances it by two. Will fail if the two values are not addresses or if the addresses do
/// not point to an array of integers.
fn next_arr(&mut self) -> Result<Vec<Felt252>, HintError> {
pub fn next_arr(&mut self) -> Result<Vec<Felt252>, HintError> {
let start = self.next_addr()?;
let end = self.next_addr()?;
vm_get_range(self.vm.vm(), start, end)
}

/// Writes a value to the current position of the buffer and advances it by one.
fn write<T: Into<MaybeRelocatable>>(&mut self, value: T) -> Result<(), MemoryError> {
pub fn write<T: Into<MaybeRelocatable>>(&mut self, value: T) -> Result<(), MemoryError> {
let ptr = self.next();
self.vm.vm().insert_value(ptr, value)
}
/// Writes an iterator of values starting from the current position of the buffer and advances
/// it to after the end of the written value.
fn write_data<T: Into<MaybeRelocatable>, Data: Iterator<Item = T>>(
pub fn write_data<T: Into<MaybeRelocatable>, Data: Iterator<Item = T>>(
&mut self,
data: Data,
) -> Result<(), MemoryError> {
Expand All @@ -503,7 +503,7 @@ impl<'a> MemBuffer<'a> {

/// Writes an array into a new segment and writes the start and end pointers to the current
/// position of the buffer. Advances the buffer by two.
fn write_arr<T: Into<MaybeRelocatable>, Data: Iterator<Item = T>>(
pub fn write_arr<T: Into<MaybeRelocatable>, Data: Iterator<Item = T>>(
&mut self,
data: Data,
) -> Result<(), MemoryError> {
Expand Down Expand Up @@ -1831,7 +1831,7 @@ fn read_array_result_as_vec(memory: &[Option<Felt252>], value: &[Felt252]) -> Ve
}

/// Loads a range of values from the VM memory.
fn vm_get_range(
pub fn vm_get_range(
vm: &mut VirtualMachine,
mut calldata_start_ptr: Relocatable,
calldata_end_ptr: Relocatable,
Expand All @@ -1846,7 +1846,7 @@ fn vm_get_range(
}

/// Extracts a parameter assumed to be a buffer.
fn extract_buffer(buffer: &ResOperand) -> (&CellRef, Felt252) {
pub fn extract_buffer(buffer: &ResOperand) -> (&CellRef, Felt252) {
let (cell, base_offset) = match buffer {
ResOperand::Deref(cell) => (cell, 0.into()),
ResOperand::BinOp(BinOpOperand { op: Operation::Add, a, b }) => {
Expand Down