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

ec_op changes 0.11 release #876

Merged
merged 9 commits into from
Mar 10, 2023
Merged
Changes from 7 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
19 changes: 19 additions & 0 deletions src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;

use crate::math_utils::{ec_add, ec_double, safe_div_usize};
use crate::types::instance_definitions::ec_op_instance_def::{
Expand Down Expand Up @@ -27,6 +29,7 @@ pub struct EcOpBuiltinRunner {
pub(crate) stop_ptr: Option<usize>,
pub(crate) included: bool,
instances_per_component: u32,
cache: RefCell<HashMap<Relocatable, Felt>>,
}

impl EcOpBuiltinRunner {
Expand All @@ -40,6 +43,7 @@ impl EcOpBuiltinRunner {
stop_ptr: None,
included,
instances_per_component: 1,
cache: RefCell::new(HashMap::new()),
}
}
///Returns True if the point (x, y) is on the elliptic curve defined as
Expand Down Expand Up @@ -154,6 +158,13 @@ impl EcOpBuiltinRunner {
return Ok(None);
}
let instance = Relocatable::from((address.segment_index, address.offset - index));
let x_addr = (instance + (&Felt::new(INPUT_CELLS_PER_EC_OP)))
.map_err(|_| RunnerError::Memory(MemoryError::ExpectedInteger(instance)))?;

if let Some(number) = self.cache.borrow().get(&address).cloned() {
return Ok(Some(MaybeRelocatable::Int(number)));
}

//All input cells should be filled, and be integer values
//If an input cell is not filled, return None
let mut input_cells = Vec::<&Felt>::with_capacity(self.n_input_cells as usize);
Expand Down Expand Up @@ -205,6 +216,14 @@ impl EcOpBuiltinRunner {
&prime,
self.ec_op_builtin.scalar_height,
)?;
self.cache
.borrow_mut()
.insert(x_addr, result.0.clone().into());
self.cache.borrow_mut().insert(
(x_addr + 1usize)
.map_err(|_| RunnerError::Memory(MemoryError::ExpectedInteger(x_addr)))?,
result.1.clone().into(),
);
match index - self.n_input_cells as usize {
0 => Ok(Some(MaybeRelocatable::Int(Felt::new(result.0)))),
_ => Ok(Some(MaybeRelocatable::Int(Felt::new(result.1)))),
Expand Down