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

Fix BuiltinRunner::final_stack and remove quick fix #778

Merged
merged 12 commits into from
Jan 27, 2023
44 changes: 23 additions & 21 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
## Cairo-VM Changelog

#### Upcoming Changes
* Add secure_run flag + integrate verify_secure_runner into cairo-run [#771](https://github.com/lambdaclass/cairo-rs/pull/777)
Public Api changes:
* Add command_line argument `secure_run`
* Add argument `secure_run: Option<bool>` to `cairo_run`
* `verify_secure_runner` is now called inside `cairo-run` when `secure_run` is set to true or when it not set and the run is not on `proof_mode`
Bugfixes:
* `EcOpBuiltinRunner::deduce_memory_cell` now checks that both points are on the curve instead of only the first one
* `EcOpBuiltinRunner::deduce_memory_cell` now returns the values of the point coordinates instead of the indices when a `PointNotOnCurve` error is returned

* Refactor `Refactor verify_secure_runner` [#768](https://github.com/lambdaclass/cairo-rs/pull/768)
Public Api changes:
* Remove builtin name from the return value of `BuiltinRunner::get_memory_segment_addresses`
* Simplify the return value of `CairoRunner::get_builtin_segments_info` to `Vec<(usize, usize)>`
* CairoRunner::read_return_values now receives a mutable reference to VirtualMachine
Bugfixes:
* CairoRunner::read_return_values now updates the `stop_ptr` of each builtin after calling `BuiltinRunner::final_stack`
* Fix `BuiltinRunner::final_stack` and remove quick fix [#778](https://github.com/lambdaclass/cairo-rs/pull/778)
* Public Api changes:
* Various changes to public `BuiltinRunner` method's signatures:
* `final_stack(&self, vm: &VirtualMachine, pointer: Relocatable) -> Result<(Relocatable, usize), RunnerError>` to `final_stack(&mut self, segments: &MemorySegmentManager, memory: &Memory, pointer: Relocatable, ) -> Result<Relocatable,RunnerError>`.
fmoletta marked this conversation as resolved.
Show resolved Hide resolved
* `get_used_cells(&self, vm: &VirtualMachine) -> Result<usize, MemoryError>` to `get_used_cells(&self, segments: &MemorySegmentManager) -> Result<usize, MemoryError>`.
* `get_used_instances(&self, vm: &VirtualMachine) -> Result<usize, MemoryError>` to `get_used_instances(&self, segments: &MemorySegmentManager) -> Result<usize, MemoryError>`.
* Bugfixes:
* `BuiltinRunner::final_stack` now updates the builtin's stop_ptr instead of returning it. This replaces the bugfix on PR #768.

* Add secure_run flag + integrate verify_secure_runner into cairo-run [#771](https://github.com/lambdaclass/cairo-rs/pull/777)
* Public Api changes:
* Add command_line argument `secure_run`
* Add argument `secure_run: Option<bool>` to `cairo_run`
* `verify_secure_runner` is now called inside `cairo-run` when `secure_run` is set to true or when it not set and the run is not on `proof_mode`
* Bugfixes:
* `EcOpBuiltinRunner::deduce_memory_cell` now checks that both points are on the curve instead of only the first one
* `EcOpBuiltinRunner::deduce_memory_cell` now returns the values of the point coordinates instead of the indices when a `PointNotOnCurve` error is returned

* Refactor `Refactor verify_secure_runner` [#768](https://github.com/lambdaclass/cairo-rs/pull/768)
Public Api changes:
* Remove builtin name from the return value of `BuiltinRunner::get_memory_segment_addresses`
* Simplify the return value of `CairoRunner::get_builtin_segments_info` to `Vec<(usize, usize)>`
* CairoRunner::read_return_values now receives a mutable reference to VirtualMachine
Bugfixes:
* CairoRunner::read_return_values now updates the `stop_ptr` of each builtin after calling `BuiltinRunner::final_stack`
* Public Api changes:
* Remove builtin name from the return value of `BuiltinRunner::get_memory_segment_addresses`
* Simplify the return value of `CairoRunner::get_builtin_segments_info` to `Vec<(usize, usize)>`
* CairoRunner::read_return_values now receives a mutable reference to VirtualMachine
* Bugfixes:
* CairoRunner::read_return_values now updates the `stop_ptr` of each builtin after calling `BuiltinRunner::final_stack`

* Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint [#686](https://github.com/lambdaclass/cairo-rs/pull/686)
* Public Api changes:
Expand Down
79 changes: 43 additions & 36 deletions src/vm/runners/builtin_runner/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ pub struct BitwiseBuiltinRunner {
pub(crate) n_input_cells: u32,
bitwise_builtin: BitwiseInstanceDef,
pub(crate) stop_ptr: Option<usize>,
pub(crate) _included: bool,
pub(crate) included: bool,
instances_per_component: u32,
}

impl BitwiseBuiltinRunner {
pub(crate) fn new(instance_def: &BitwiseInstanceDef, include: bool) -> Self {
pub(crate) fn new(instance_def: &BitwiseInstanceDef, included: bool) -> Self {
BitwiseBuiltinRunner {
base: 0,
ratio: instance_def.ratio,
cells_per_instance: CELLS_PER_BITWISE,
n_input_cells: INPUT_CELLS_PER_BITWISE,
bitwise_builtin: instance_def.clone(),
stop_ptr: None,
_included: include,
included,
instances_per_component: 1,
}
}
Expand All @@ -49,7 +49,7 @@ impl BitwiseBuiltinRunner {
}

pub fn initial_stack(&self) -> Vec<MaybeRelocatable> {
if self._included {
if self.included {
vec![MaybeRelocatable::from((self.base, 0))]
} else {
vec![]
Expand Down Expand Up @@ -124,9 +124,9 @@ impl BitwiseBuiltinRunner {
(self.base, self.stop_ptr)
}

pub fn get_used_cells(&self, vm: &VirtualMachine) -> Result<usize, MemoryError> {
pub fn get_used_cells(&self, segments: &MemorySegmentManager) -> Result<usize, MemoryError> {
let base = self.base();
vm.segments
segments
.get_segment_used_size(
base.try_into()
.map_err(|_| MemoryError::AddressInTemporarySegment(base))?,
Expand All @@ -144,7 +144,7 @@ impl BitwiseBuiltinRunner {
if vm.current_step < min_step {
Err(MemoryError::InsufficientAllocatedCells)
} else {
let used = self.get_used_cells(vm)?;
let used = self.get_used_cells(&vm.segments)?;
let size = cells_per_instance as usize
* safe_div_usize(vm.current_step, ratio)
.map_err(|_| MemoryError::InsufficientAllocatedCells)?;
Expand Down Expand Up @@ -174,40 +174,43 @@ impl BitwiseBuiltinRunner {
}

pub fn final_stack(
&self,
vm: &VirtualMachine,
&mut self,
segments: &MemorySegmentManager,
memory: &Memory,
pointer: Relocatable,
) -> Result<(Relocatable, usize), RunnerError> {
if self._included {
if let Ok(stop_pointer) =
vm.get_relocatable(&(pointer.sub_usize(1)).map_err(|_| RunnerError::FinalStack)?)
) -> Result<Relocatable, RunnerError> {
if self.included {
if let Ok(stop_pointer) = memory
.get_relocatable(&(pointer.sub_usize(1)).map_err(|_| RunnerError::FinalStack)?)
{
if self.base() != stop_pointer.segment_index {
return Err(RunnerError::InvalidStopPointer("bitwise".to_string()));
}
let stop_ptr = stop_pointer.offset;
let num_instances = self
.get_used_instances(vm)
.get_used_instances(segments)
.map_err(|_| RunnerError::FinalStack)?;
let used_cells = num_instances * self.cells_per_instance as usize;
if stop_ptr != used_cells {
return Err(RunnerError::InvalidStopPointer("bitwise".to_string()));
}
Ok((
pointer.sub_usize(1).map_err(|_| RunnerError::FinalStack)?,
stop_ptr,
))
self.stop_ptr = Some(stop_ptr);
Ok(pointer.sub_usize(1).map_err(|_| RunnerError::FinalStack)?)
} else {
Err(RunnerError::FinalStack)
}
} else {
let stop_ptr = self.base() as usize;
Ok((pointer, stop_ptr))
self.stop_ptr = Some(stop_ptr);
Ok(pointer)
}
}

pub fn get_used_instances(&self, vm: &VirtualMachine) -> Result<usize, MemoryError> {
let used_cells = self.get_used_cells(vm)?;
pub fn get_used_instances(
&self,
segments: &MemorySegmentManager,
) -> Result<usize, MemoryError> {
let used_cells = self.get_used_cells(segments)?;
Ok(div_ceil(used_cells, self.cells_per_instance as usize))
}
}
Expand Down Expand Up @@ -238,12 +241,12 @@ mod tests {

vm.segments.segment_used_sizes = Some(vec![1]);

assert_eq!(builtin.get_used_instances(&vm), Ok(1));
assert_eq!(builtin.get_used_instances(&vm.segments), Ok(1));
}

#[test]
fn final_stack() {
let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);
let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);

let mut vm = vm!();

Expand All @@ -259,14 +262,16 @@ mod tests {
let pointer = Relocatable::from((2, 2));

assert_eq!(
builtin.final_stack(&vm, pointer).unwrap(),
(Relocatable::from((2, 1)), 0)
builtin
.final_stack(&vm.segments, &vm.memory, pointer)
.unwrap(),
Relocatable::from((2, 1))
);
}

#[test]
fn final_stack_error_stop_pointer() {
let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);
let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);

let mut vm = vm!();

Expand All @@ -282,14 +287,14 @@ mod tests {
let pointer = Relocatable::from((2, 2));

assert_eq!(
builtin.final_stack(&vm, pointer),
builtin.final_stack(&vm.segments, &vm.memory, pointer),
Err(RunnerError::InvalidStopPointer("bitwise".to_string()))
);
}

#[test]
fn final_stack_error_when_not_included() {
let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), false);
fn final_stack_error_when_notincluded() {
let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), false);

let mut vm = vm!();

Expand All @@ -305,14 +310,16 @@ mod tests {
let pointer = Relocatable::from((2, 2));

assert_eq!(
builtin.final_stack(&vm, pointer).unwrap(),
(Relocatable::from((2, 2)), 0)
builtin
.final_stack(&vm.segments, &vm.memory, pointer)
.unwrap(),
Relocatable::from((2, 2))
);
}

#[test]
fn final_stack_error_non_relocatable() {
let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);
let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true);

let mut vm = vm!();

Expand All @@ -328,7 +335,7 @@ mod tests {
let pointer = Relocatable::from((2, 2));

assert_eq!(
builtin.final_stack(&vm, pointer),
builtin.final_stack(&vm.segments, &vm.memory, pointer),
Err(RunnerError::FinalStack)
);
}
Expand Down Expand Up @@ -524,7 +531,7 @@ mod tests {
let vm = vm!();

assert_eq!(
builtin.get_used_cells(&vm),
builtin.get_used_cells(&vm.segments),
Err(MemoryError::MissingSegmentUsedSizes)
);
}
Expand All @@ -538,7 +545,7 @@ mod tests {
let mut vm = vm!();

vm.segments.segment_used_sizes = Some(vec![0]);
assert_eq!(builtin.get_used_cells(&vm), Ok(0));
assert_eq!(builtin.get_used_cells(&vm.segments), Ok(0));
}

#[test]
Expand All @@ -550,7 +557,7 @@ mod tests {
let mut vm = vm!();

vm.segments.segment_used_sizes = Some(vec![4]);
assert_eq!(builtin.get_used_cells(&vm), Ok(4));
assert_eq!(builtin.get_used_cells(&vm.segments), Ok(4));
}

#[test]
Expand Down
Loading