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

chore: Update Cargo.toml, types and dependencies #10

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ description = "Rust SDK for the Starkware Stone prover and verifier."

[dependencies]
bincode = "2.0.0-rc.3"
cairo-vm = { git = "https://github.com/Moonsong-Labs/cairo-vm", rev = "e0a4653aa5634664a3f792b38715a572e9f89b44", features = ["extensive_hints"] }
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "05352b1c67859a4d8cd128575c1e68ca7e300341", features = ["extensive_hints"] }
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
stark_evm_adapter = "0.1.5"
tempfile = "3.8.1"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "process", "rt-multi-thread"] }
cairo-bootloader = { git = "https://github.com/Moonsong-Labs/cairo-bootloader"}


[dev-dependencies]
Expand Down
60 changes: 26 additions & 34 deletions src/cairo_vm.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::any::Any;
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;

use cairo_bootloader::{
BootloaderConfig, BootloaderInput, PackedOutput, SimpleBootloaderInput, Task, TaskSpec,
};
use cairo_vm::air_private_input::AirPrivateInput;
use cairo_vm::air_public_input::PublicInputError;
use cairo_vm::cairo_run::{
write_encoded_memory, write_encoded_trace, CairoRunConfig, EncodeTraceError,
};
use cairo_vm::hint_processor::builtin_hint_processor::bootloader::types::{
BootloaderConfig, BootloaderInput, PackedOutput, SimpleBootloaderInput, Task, TaskSpec,
};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessor;
use cairo_vm::types::errors::cairo_pie_error::CairoPieError;
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::types::program::Program;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
Expand All @@ -21,7 +21,6 @@ use cairo_vm::vm::errors::vm_exception::VmException;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
use cairo_vm::vm::security::verify_secure_runner;
use cairo_vm::vm::vm_core::VirtualMachine;
use cairo_vm::{any_box, Felt252};
use thiserror::Error;

Expand All @@ -36,7 +35,7 @@ pub fn cairo_run(
cairo_run_config: &CairoRunConfig,
hint_executor: &mut dyn HintProcessor,
variables: HashMap<String, Box<dyn Any>>,
) -> Result<(CairoRunner, VirtualMachine), CairoRunError> {
whichqua marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<CairoRunner, CairoRunError> {
let secure_run = cairo_run_config
.secure_run
.unwrap_or(!cairo_run_config.proof_mode);
Expand All @@ -47,36 +46,30 @@ pub fn cairo_run(
program,
cairo_run_config.layout,
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
for (key, value) in variables {
cairo_runner.exec_scopes.insert_box(&key, value);
}

let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled);
let end = cairo_runner.initialize(&mut vm, allow_missing_builtins)?;
let end = cairo_runner.initialize(allow_missing_builtins)?;
// check step calculation

cairo_runner
.run_until_pc(end, &mut vm, hint_executor)
.map_err(|err| VmException::from_vm_error(&cairo_runner, &vm, err))?;
cairo_runner.end_run(
cairo_run_config.disable_trace_padding,
false,
&mut vm,
hint_executor,
)?;
.run_until_pc(end, hint_executor)
.map_err(|err| VmException::from_vm_error(&cairo_runner, err))?;
cairo_runner.end_run(cairo_run_config.disable_trace_padding, false, hint_executor)?;

vm.verify_auto_deductions()?;
cairo_runner.read_return_values(&mut vm)?;
cairo_runner.read_return_values(allow_missing_builtins)?;
if cairo_run_config.proof_mode {
cairo_runner.finalize_segments(&mut vm)?;
cairo_runner.finalize_segments()?;
}
if secure_run {
verify_secure_runner(&cairo_runner, true, None, &mut vm)?;
verify_secure_runner(&cairo_runner, true, None)?;
}
cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?;
cairo_runner.relocate(cairo_run_config.relocate_mem)?;

Ok((cairo_runner, vm))
Ok(cairo_runner)
}

/// Run a Cairo program in proof mode.
Expand All @@ -86,14 +79,13 @@ pub fn run_in_proof_mode(
program_content: &[u8],
layout: Layout,
allow_missing_builtins: Option<bool>,
) -> Result<(CairoRunner, VirtualMachine), CairoRunError> {
) -> Result<CairoRunner, CairoRunError> {
let proof_mode = true;

let cairo_run_config = CairoRunConfig {
entrypoint: "main",
trace_enabled: true,
relocate_mem: true,
layout: &layout.to_string(),
layout: layout.into(),
proof_mode,
secure_run: None,
disable_trace_padding: false,
Expand All @@ -102,7 +94,9 @@ pub fn run_in_proof_mode(

let mut hint_processor = BuiltinHintProcessor::new_empty();

cairo_vm::cairo_run::cairo_run(program_content, &cairo_run_config, &mut hint_processor)
let runner =
cairo_vm::cairo_run::cairo_run(program_content, &cairo_run_config, &mut hint_processor)?;
Ok(runner)
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -111,7 +105,7 @@ pub enum BootloaderTaskError {
Program(#[from] ProgramError),

#[error("Failed to read PIE: {0}")]
Pie(#[from] CairoPieError),
Pie(#[from] io::Error),
}

pub fn make_bootloader_tasks(
Expand Down Expand Up @@ -183,10 +177,8 @@ impl bincode::enc::write::Writer for MemWriter {
/// Extracts execution artifacts from the runner and VM (after execution).
///
/// * `cairo_runner` Cairo runner object.
/// * `vm`: Cairo VM object.
pub fn extract_execution_artifacts(
cairo_runner: CairoRunner,
vm: VirtualMachine,
) -> Result<ExecutionArtifacts, ExecutionError> {
let memory = &cairo_runner.relocated_memory;
let trace = cairo_runner
Expand All @@ -202,10 +194,10 @@ pub fn extract_execution_artifacts(
write_encoded_trace(trace, &mut trace_writer).map_err(ExecutionError::EncodeTrace)?;
let trace_raw = trace_writer.buf;

let cairo_vm_public_input = cairo_runner.get_air_public_input(&vm)?;
let cairo_vm_public_input = cairo_runner.get_air_public_input()?;
let public_input = PublicInput::try_from(cairo_vm_public_input)?;

let private_input = cairo_runner.get_air_private_input(&vm);
let private_input = cairo_runner.get_air_private_input();

Ok(ExecutionArtifacts {
public_input,
Expand All @@ -229,7 +221,7 @@ pub fn run_bootloader_in_proof_mode(
entrypoint: "main",
trace_enabled: true,
relocate_mem: true,
layout: &layout.to_string(),
layout: layout.into(),
proof_mode,
secure_run: None,
disable_trace_padding: false,
Expand Down Expand Up @@ -260,12 +252,12 @@ pub fn run_bootloader_in_proof_mode(
),
]);

let (cairo_runner, vm) = cairo_run(
let cairo_runner = cairo_run(
bootloader,
&cairo_run_config,
&mut hint_processor,
variables,
)?;

extract_execution_artifacts(cairo_runner, vm)
extract_execution_artifacts(cairo_runner)
}
18 changes: 18 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cairo_vm::air_private_input::AirPrivateInputSerializable;
use cairo_vm::types::layout_name::LayoutName;
use stark_evm_adapter::annotation_parser::SplitProofs;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -89,6 +90,7 @@ pub struct ProverParameters {
pub use_extension_field: bool,
}

// TODO: Should we remove this and just us LayoutName?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably. TBD how easily it can be done.

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub enum Layout {
#[serde(rename = "plain")]
Expand Down Expand Up @@ -119,6 +121,22 @@ impl FromStr for Layout {
}
}

impl From<Layout> for LayoutName {
fn from(val: Layout) -> Self {
match val {
Layout::AllCairo => LayoutName::all_cairo,
Layout::AllSolidity => LayoutName::all_solidity,
Layout::Dex => LayoutName::dex,
Layout::Plain => LayoutName::plain,
Layout::Recursive => LayoutName::recursive,
Layout::RecursiveLargeOutput => LayoutName::recursive_large_output,
Layout::Small => LayoutName::small,
Layout::Starknet => LayoutName::starknet,
Layout::StarknetWithKeccak => LayoutName::starknet_with_keccak,
}
}
}

impl Display for Layout {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let value = serde_json::to_value(self).map_err(|_| std::fmt::Error)?;
Expand Down
Loading