Skip to content

Commit 7278ee0

Browse files
Add_option_to_simulate_builtins (#1956)
1 parent 1f472cf commit 7278ee0

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* feat: adding option to simulate builtins [#1956](https://github.com/lambdaclass/cairo-vm/pull/1956)
6+
57
* feat: adding `all_cairo_stwo` layout to vm [#1957](https://github.com/lambdaclass/cairo-vm/pull/1957)
68

79
* chore: update Rust required version to 1.85.0 [#1990](https://github.com/lambdaclass/cairo-vm/pull/1990)

vm/src/vm/runners/builtin_runner/ec_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct EcOpBuiltinRunner {
2323
}
2424

2525
impl EcOpBuiltinRunner {
26-
pub(crate) fn new(ratio: Option<u32>, included: bool) -> Self {
26+
pub fn new(ratio: Option<u32>, included: bool) -> Self {
2727
EcOpBuiltinRunner {
2828
base: 0,
2929
ratio,

vm/src/vm/runners/builtin_runner/keccak.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct KeccakBuiltinRunner {
3131
}
3232

3333
impl KeccakBuiltinRunner {
34-
pub(crate) fn new(ratio: Option<u32>, included: bool) -> Self {
34+
pub fn new(ratio: Option<u32>, included: bool) -> Self {
3535
KeccakBuiltinRunner {
3636
base: 0,
3737
ratio,

vm/src/vm/runners/builtin_runner/signature.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ lazy_static! {
3535
pub struct SignatureBuiltinRunner {
3636
pub(crate) included: bool,
3737
ratio: Option<u32>,
38-
base: usize,
38+
pub base: usize,
3939
pub(crate) stop_ptr: Option<usize>,
40-
signatures: Rc<RefCell<HashMap<Relocatable, Signature>>>,
40+
pub signatures: Rc<RefCell<HashMap<Relocatable, Signature>>>,
4141
}
4242

4343
impl SignatureBuiltinRunner {
44-
pub(crate) fn new(ratio: Option<u32>, included: bool) -> Self {
44+
pub fn new(ratio: Option<u32>, included: bool) -> Self {
4545
SignatureBuiltinRunner {
4646
base: 0,
4747
included,

vm/src/vm/vm_core.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl DeducedOperands {
8888
pub struct VirtualMachine {
8989
pub(crate) run_context: RunContext,
9090
pub builtin_runners: Vec<BuiltinRunner>,
91+
pub simulated_builtin_runners: Vec<BuiltinRunner>,
9192
pub segments: MemorySegmentManager,
9293
pub(crate) trace: Option<Vec<TraceEntry>>,
9394
pub(crate) current_step: usize,
@@ -119,6 +120,7 @@ impl VirtualMachine {
119120
VirtualMachine {
120121
run_context,
121122
builtin_runners: Vec::new(),
123+
simulated_builtin_runners: Vec::new(),
122124
trace,
123125
current_step: 0,
124126
skip_instruction_execution: false,
@@ -296,12 +298,17 @@ impl VirtualMachine {
296298
&self,
297299
address: Relocatable,
298300
) -> Result<Option<MaybeRelocatable>, VirtualMachineError> {
299-
for builtin in self.builtin_runners.iter() {
300-
if builtin.base() as isize == address.segment_index {
301-
match builtin.deduce_memory_cell(address, &self.segments.memory) {
302-
Ok(maybe_reloc) => return Ok(maybe_reloc),
303-
Err(error) => return Err(VirtualMachineError::RunnerError(error)),
304-
};
301+
let memory = &self.segments.memory;
302+
303+
for runner in self
304+
.builtin_runners
305+
.iter()
306+
.chain(self.simulated_builtin_runners.iter())
307+
{
308+
if runner.base() as isize == address.segment_index {
309+
return runner
310+
.deduce_memory_cell(address, memory)
311+
.map_err(VirtualMachineError::RunnerError);
305312
}
306313
}
307314
Ok(None)
@@ -916,6 +923,16 @@ impl VirtualMachine {
916923
&mut self.builtin_runners
917924
}
918925

926+
/// Returns a mutable iterator over all builtin runners used. That is, both builtin_runners and
927+
/// simulated_builtin_runners.
928+
pub fn get_all_builtin_runners_as_mut_iter(
929+
&mut self,
930+
) -> impl Iterator<Item = &mut BuiltinRunner> {
931+
self.builtin_runners
932+
.iter_mut()
933+
.chain(self.simulated_builtin_runners.iter_mut())
934+
}
935+
919936
///Inserts a value into a memory address given by a Relocatable value
920937
pub fn insert_value<T: Into<MaybeRelocatable>>(
921938
&mut self,
@@ -1004,7 +1021,7 @@ impl VirtualMachine {
10041021
pub fn get_signature_builtin(
10051022
&mut self,
10061023
) -> Result<&mut SignatureBuiltinRunner, VirtualMachineError> {
1007-
for builtin in self.get_builtin_runners_as_mut() {
1024+
for builtin in self.get_all_builtin_runners_as_mut_iter() {
10081025
if let BuiltinRunner::Signature(signature_builtin) = builtin {
10091026
return Ok(signature_builtin);
10101027
};
@@ -1309,6 +1326,7 @@ impl VirtualMachineBuilder {
13091326
VirtualMachine {
13101327
run_context: self.run_context,
13111328
builtin_runners: self.builtin_runners,
1329+
simulated_builtin_runners: Vec::new(),
13121330
trace: self.trace,
13131331
current_step: self.current_step,
13141332
skip_instruction_execution: self.skip_instruction_execution,

vm/src/vm/vm_memory/memory_segments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::memory::MemoryCell;
2323
pub struct MemorySegmentManager {
2424
pub segment_sizes: HashMap<usize, usize>,
2525
pub segment_used_sizes: Option<Vec<usize>>,
26-
pub(crate) memory: Memory,
26+
pub memory: Memory,
2727
// A map from segment index to a list of pairs (offset, page_id) that constitute the
2828
// public memory. Note that the offset is absolute (not based on the page_id).
2929
pub public_memory_offsets: HashMap<usize, Vec<(usize, usize)>>,

0 commit comments

Comments
 (0)