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

feat: Add method CairoRunner::initialize_function_runner_cairo1 #1151

Merged
merged 25 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
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 @@ -31,6 +32,7 @@ pub enum BuiltinName {
bitwise,
ec_op,
poseidon,
segment_arena,
}

impl BuiltinName {
Expand All @@ -44,6 +46,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 @@ -387,7 +390,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 @@ -407,6 +409,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 @@ -807,7 +810,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 @@ -865,7 +868,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