Skip to content

Commit

Permalink
feat: Add method CairoRunner::initialize_function_runner_cairo1 (la…
Browse files Browse the repository at this point in the history
…mbdaclass#1151)

* Move builtins field outside of shared_program_data

* Add method CairoRunner::run_from_entrypoint_with_builtins

* Add changelog entry

* Revert change

* Add `CairoRunner::initialize_function_runner_cairo1`

* Use initialize_function_runner_cairo1

* Update changelog

* Add feature flag

* Clippy

* Add missing import

* Fix std path

* Add path for Vec in program macro

* Add segment_arena to BuiltinName enum

* Add change to changelog

* Fix test func

* Add String & Vec imports for alloc feature

* Remove irrelevant information from doc comment

* Add path for String

* Fix import

* Fix test

* Fix test

* Fix test

* Fix typo in changelog

* Fix duplicate entry title in changelog
  • Loading branch information
fmoletta authored and kariy committed Jun 23, 2023
1 parent ad62a43 commit 9ece98b
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 100 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

#### Upcoming Changes

* feat: Add method `CairoRunner::initialize_function_runner_cairo_1` [#1151](https://github.com/lambdaclass/cairo-rs/pull/1151)

* Add method `pub fn initialize_function_runner_cairo_1(
&mut self,
vm: &mut VirtualMachine,
program_builtins: &[BuiltinName],
) -> Result<(), RunnerError>` to `CairoRunner`

* BREAKING: Move field `builtins` from `SharedProgramData` to `Program`
* BREAKING: Remove argument `add_segment_arena_builtin` from `CairoRunner::initialize_function_runner`, it is now always false
* BREAKING: Add `segment_arena` enum variant to `BuiltinName`

* Fix implementation of `InitSquashData` and `ShouldSkipSquashLoop`

* Add more hints to `Cairo1HintProcessor` [#1143](https://github.com/lambdaclass/cairo-rs/pull/1098)
* Add more hints to `Cairo1HintProcessor` [#1143](https://github.com/lambdaclass/cairo-rs/pull/1143)

* `Cairo1HintProcessor` can now run the following hints:
* Felt252DictEntryInit
Expand Down
9 changes: 6 additions & 3 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::stdlib::{collections::HashMap, fmt, prelude::*, sync::Arc};

use crate::vm::runners::builtin_runner::SEGMENT_ARENA_BUILTIN_NAME;
use crate::{
serde::deserialize_utils,
types::{
Expand Down Expand Up @@ -32,6 +33,7 @@ pub enum BuiltinName {
bitwise,
ec_op,
poseidon,
segment_arena,
}

impl BuiltinName {
Expand All @@ -45,6 +47,7 @@ impl BuiltinName {
BuiltinName::bitwise => BITWISE_BUILTIN_NAME,
BuiltinName::ec_op => EC_OP_BUILTIN_NAME,
BuiltinName::poseidon => POSEIDON_BUILTIN_NAME,
BuiltinName::segment_arena => SEGMENT_ARENA_BUILTIN_NAME,
}
}
}
Expand Down Expand Up @@ -407,7 +410,6 @@ pub fn parse_program_json(
}

let shared_program_data = SharedProgramData {
builtins: program_json.builtins,
data: program_json.data,
hints: program_json.hints,
main: entrypoint_pc,
Expand All @@ -427,6 +429,7 @@ pub fn parse_program_json(
shared_program_data: Arc::new(shared_program_data),
constants,
reference_manager: program_json.reference_manager,
builtins: program_json.builtins,
})
}

Expand Down Expand Up @@ -845,7 +848,7 @@ mod tests {
}],
);

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, Some(0));
assert_eq!(program.shared_program_data.hints, hints);
Expand Down Expand Up @@ -903,7 +906,7 @@ mod tests {
}],
);

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.hints, hints);
Expand Down
63 changes: 48 additions & 15 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "cairo-1-hints")]
use crate::{
hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor,
serde::deserialize_program::BuiltinName,
types::relocatable::MaybeRelocatable,
vm::{
runners::cairo_runner::{CairoArg, CairoRunner},
Expand All @@ -23,6 +24,9 @@ use crate::{
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::{string::String, vec::Vec};

mod bitwise_test;

#[cfg(feature = "cairo-1-hints")]
Expand Down Expand Up @@ -117,28 +121,24 @@ pub(self) fn run_cairo_1_entrypoint(
.unwrap();
let mut vm = VirtualMachine::new(false);

runner.initialize_function_runner(&mut vm, true).unwrap();

// Get builtin bases
// Extract builtins from CasmContractClass entrypoint data from the entrypoint which's offset is being ran
let builtins: Vec<String> = contract_class
.entry_points_by_type
.external
.iter()
.find(|e| e.offset == entrypoint_offset)
.unwrap()
.builtins
.iter()
.map(|n| format!("{}_builtin", n))
.collect();
let program_builtins = get_casm_contract_builtins(&contract_class, entrypoint_offset);
runner
.initialize_function_runner_cairo_1(&mut vm, &program_builtins)
.unwrap();

// Implicit Args
let syscall_segment = MaybeRelocatable::from(vm.add_memory_segment());

let builtins: Vec<&'static str> = runner
.get_program_builtins()
.iter()
.map(|b| b.name())
.collect();

let builtin_segment: Vec<MaybeRelocatable> = vm
.get_builtin_runners()
.iter()
.filter(|b| builtins.contains(&(b.name().to_string())))
.filter(|b| builtins.contains(&b.name()))
.flat_map(|b| b.initial_stack())
.collect();

Expand Down Expand Up @@ -205,3 +205,36 @@ pub(self) fn run_cairo_1_entrypoint(
.collect();
assert_eq!(expected_retdata, &retdata);
}

#[cfg(feature = "cairo-1-hints")]
fn get_casm_contract_builtins(
contract_class: &CasmContractClass,
entrypoint_offset: usize,
) -> Vec<BuiltinName> {
contract_class
.entry_points_by_type
.external
.iter()
.find(|e| e.offset == entrypoint_offset)
.unwrap()
.builtins
.iter()
.map(|n| format!("{}_builtin", n))
.map(|s| match &*s {
crate::vm::runners::builtin_runner::OUTPUT_BUILTIN_NAME => BuiltinName::output,
crate::vm::runners::builtin_runner::RANGE_CHECK_BUILTIN_NAME => {
BuiltinName::range_check
}
crate::vm::runners::builtin_runner::HASH_BUILTIN_NAME => BuiltinName::pedersen,
crate::vm::runners::builtin_runner::SIGNATURE_BUILTIN_NAME => BuiltinName::ecdsa,
crate::vm::runners::builtin_runner::KECCAK_BUILTIN_NAME => BuiltinName::keccak,
crate::vm::runners::builtin_runner::BITWISE_BUILTIN_NAME => BuiltinName::bitwise,
crate::vm::runners::builtin_runner::EC_OP_BUILTIN_NAME => BuiltinName::ec_op,
crate::vm::runners::builtin_runner::POSEIDON_BUILTIN_NAME => BuiltinName::poseidon,
crate::vm::runners::builtin_runner::SEGMENT_ARENA_BUILTIN_NAME => {
BuiltinName::segment_arena
}
_ => panic!("Invalid builtin {}", s),
})
.collect()
}
17 changes: 9 additions & 8 deletions src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use std::path::Path;
// Fields in `Program` (other than `SharedProgramData` itself) are used by the main logic.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(crate) struct SharedProgramData {
pub(crate) builtins: Vec<BuiltinName>,
pub(crate) data: Vec<MaybeRelocatable>,
pub(crate) hints: HashMap<usize, Vec<HintParams>>,
pub(crate) main: Option<usize>,
Expand All @@ -55,6 +54,7 @@ pub(crate) struct SharedProgramData {
pub struct Program {
pub(crate) shared_program_data: Arc<SharedProgramData>,
pub(crate) constants: HashMap<String, Felt252>,
pub(crate) builtins: Vec<BuiltinName>,
pub(crate) reference_manager: ReferenceManager,
}

Expand All @@ -81,7 +81,6 @@ impl Program {
}
}
let shared_program_data = SharedProgramData {
builtins,
data,
hints,
main,
Expand All @@ -95,6 +94,7 @@ impl Program {
shared_program_data: Arc::new(shared_program_data),
constants,
reference_manager,
builtins,
})
}

Expand All @@ -114,7 +114,7 @@ impl Program {
}

pub fn iter_builtins(&self) -> impl Iterator<Item = &BuiltinName> {
self.shared_program_data.builtins.iter()
self.builtins.iter()
}

pub fn iter_data(&self) -> impl Iterator<Item = &MaybeRelocatable> {
Expand Down Expand Up @@ -145,6 +145,7 @@ impl Default for Program {
reference_manager: ReferenceManager {
references: Vec::new(),
},
builtins: Vec::new(),
}
}
}
Expand Down Expand Up @@ -235,7 +236,7 @@ mod tests {
)
.unwrap();

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.identifiers, HashMap::new());
Expand Down Expand Up @@ -297,7 +298,7 @@ mod tests {
)
.unwrap();

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.identifiers, identifiers);
Expand Down Expand Up @@ -685,7 +686,7 @@ mod tests {
},
);

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, Some(0));
assert_eq!(program.shared_program_data.identifiers, identifiers);
Expand Down Expand Up @@ -784,7 +785,7 @@ mod tests {
},
);

assert_eq!(program.shared_program_data.builtins, builtins);
assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.identifiers, identifiers);
Expand Down Expand Up @@ -837,7 +838,6 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn default_program() {
let shared_program_data = SharedProgramData {
builtins: Vec::new(),
data: Vec::new(),
hints: HashMap::new(),
main: None,
Expand All @@ -853,6 +853,7 @@ mod tests {
reference_manager: ReferenceManager {
references: Vec::new(),
},
builtins: Vec::new(),
};

assert_eq!(program, Program::default());
Expand Down
Loading

0 comments on commit 9ece98b

Please sign in to comment.