Skip to content

Commit

Permalink
update: dependencies and types
Browse files Browse the repository at this point in the history
  • Loading branch information
whichqua committed Jun 11, 2024
1 parent 5537a04 commit 58873e6
Show file tree
Hide file tree
Showing 8 changed files with 814 additions and 691 deletions.
1,305 changes: 751 additions & 554 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
[workspace]
resolver = "2"

members = ["integration-tests", "madara-prover-rpc-client", "madara-prover-rpc-server", "test-cases", "test-fixtures", "integration-tests/evm-test"]
members = [
"integration-tests",
"madara-prover-rpc-client",
"madara-prover-rpc-server",
"test-cases",
"test-fixtures",
"integration-tests/evm-test",
]

[workspace.dependencies]
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",
] }
prost = "0.12.1"
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
stark_evm_adapter = "0.1.5"
stone-prover-sdk = { git = "https://github.com/Moonsong-Labs/stone-prover-sdk", rev = "ed0499407dc0374c23eca373d88eb3465e15583b" }
stone-prover-sdk = { git = "https://github.com/Moonsong-Labs/stone-prover-sdk", branch = "patch/cairo-vm-git-path" }
cairo-bootloader = { git = "https://github.com/Moonsong-Labs/cairo-bootloader" }
tempfile = "3.8.1"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "process", "rt-multi-thread"] }
tonic = "0.10.2"
tonic-build = "0.10.2"
tokio = { version = "1.34.0", features = [
"macros",
"process",
"rt-multi-thread",
] }
tonic = "0.11.0"
tonic-build = "0.11.0"
rstest = "0.18.2"
1 change: 1 addition & 0 deletions madara-prover-rpc-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ path = "src/lib.rs"

[dependencies]
cairo-vm = { workspace = true }
cairo-bootloader = { workspace = true }
prost = { workspace = true }
stone-prover-sdk = { workspace = true }
thiserror = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions madara-prover-rpc-server/src/services/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use stone_prover_sdk::error::{ProverError, VerifierError};
use stone_prover_sdk::fri::generate_prover_parameters;
use stone_prover_sdk::models::{
Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory,
Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory, Verifier,
};
use stone_prover_sdk::prover::run_prover_async;
use stone_prover_sdk::verifier::run_verifier_with_annotations_async;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn get_prover_parameters(
.map_err(|_| Status::invalid_argument("Could not read prover parameters"));
}

let last_layer_degree_bound = 64;
let last_layer_degree_bound = Verifier::Stone;
Ok(generate_prover_parameters(
nb_steps,
last_layer_degree_bound,
Expand Down
4 changes: 2 additions & 2 deletions madara-prover-rpc-server/src/services/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn run_cairo_program_in_proof_mode(
layout: Layout,
) -> Result<ExecutionArtifacts, ExecutionError> {
let allow_missing_builtins = Some(false);
let (cairo_runner, vm) = run_in_proof_mode(program, layout, allow_missing_builtins)?;
extract_execution_artifacts(cairo_runner, vm)
let cairo_runner = run_in_proof_mode(program, layout, allow_missing_builtins)?;
extract_execution_artifacts(cairo_runner)
}

fn format_execution_result(
Expand Down
144 changes: 29 additions & 115 deletions madara-prover-rpc-server/src/services/starknet_prover.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use std::any::Any;
use std::collections::HashMap;

use cairo_vm::cairo_run::CairoRunConfig;
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;
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 std::any::Any;
use std::collections::HashMap;
use tonic::{Request, Response, Status};

use stone_prover_sdk::error::ProverError;
use stone_prover_sdk::models::{Proof, ProverConfig, ProverWorkingDirectory};
use stone_prover_sdk::models::{Layout, Proof, ProverConfig, ProverWorkingDirectory};

use crate::services::common::{
call_prover, format_prover_error, get_prover_parameters, verify_and_annotate_proof,
Expand All @@ -29,7 +20,7 @@ use crate::services::starknet_prover::starknet_prover_proto::starknet_prover_ser
use crate::services::starknet_prover::starknet_prover_proto::{
StarknetExecutionRequest, StarknetProverResponse,
};
use stone_prover_sdk::cairo_vm::{extract_execution_artifacts, ExecutionArtifacts, ExecutionError};
use stone_prover_sdk::cairo_vm::run_bootloader_in_proof_mode;

pub mod starknet_prover_proto {
tonic::include_proto!("starknet_prover");
Expand All @@ -45,7 +36,7 @@ pub fn cairo_run(
cairo_run_config: &CairoRunConfig,
hint_executor: &mut dyn HintProcessor,
variables: HashMap<String, Box<dyn Any>>,
) -> Result<(CairoRunner, VirtualMachine), CairoRunError> {
) -> Result<CairoRunner, CairoRunError> {
let secure_run = cairo_run_config
.secure_run
.unwrap_or(!cairo_run_config.proof_mode);
Expand All @@ -56,36 +47,29 @@ pub fn cairo_run(
program,
cairo_run_config.layout,
cairo_run_config.proof_mode,
allow_missing_builtins,
)?;
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,
)?;

vm.verify_auto_deductions()?;
cairo_runner.read_return_values(&mut vm)?;
.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)?;
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)
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -94,83 +78,7 @@ enum BootloaderTaskError {
Program(#[from] ProgramError),

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

fn make_bootloader_tasks(
programs: &[Vec<u8>],
pies: &[Vec<u8>],
) -> Result<Vec<TaskSpec>, BootloaderTaskError> {
let program_tasks = programs.iter().map(|program_bytes| {
let program = Program::from_bytes(program_bytes, Some("main"));
program
.map(|program| TaskSpec {
task: Task::Program(program),
})
.map_err(BootloaderTaskError::Program)
});

let cairo_pie_tasks = pies.iter().map(|pie_bytes| {
let pie = CairoPie::from_bytes(pie_bytes);
pie.map(|pie| TaskSpec {
task: Task::Pie(pie),
})
.map_err(BootloaderTaskError::Pie)
});

program_tasks.chain(cairo_pie_tasks).collect()
}

pub fn run_bootloader_in_proof_mode(
bootloader: &Program,
tasks: Vec<TaskSpec>,
) -> Result<ExecutionArtifacts, ExecutionError> {
let proof_mode = true;
let layout = "starknet_with_keccak";

let cairo_run_config = CairoRunConfig {
entrypoint: "main",
trace_enabled: true,
relocate_mem: true,
layout,
proof_mode,
secure_run: None,
disable_trace_padding: false,
allow_missing_builtins: None,
};

let n_tasks = tasks.len();

let bootloader_input = BootloaderInput {
simple_bootloader_input: SimpleBootloaderInput {
fact_topologies_path: None,
single_page: false,
tasks,
},
bootloader_config: BootloaderConfig {
simple_bootloader_program_hash: Felt252::from(0),
supported_cairo_verifier_program_hashes: vec![],
},
packed_outputs: vec![PackedOutput::Plain(vec![]); n_tasks],
};

let mut hint_processor = BuiltinHintProcessor::new_empty();
let variables = HashMap::<String, Box<dyn Any>>::from([
("bootloader_input".to_string(), any_box!(bootloader_input)),
(
"bootloader_program".to_string(),
any_box!(bootloader.clone()),
),
]);

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

extract_execution_artifacts(cairo_runner, vm)
Pie(#[from] std::io::Error),
}

/// Formats the output of the prover subprocess into the server response.
Expand Down Expand Up @@ -204,13 +112,19 @@ impl StarknetProver for StarknetProverService {
.map_err(|e| Status::internal(format!("Failed to load bootloader program: {}", e)))?;
let prover_config = ProverConfig::default();

let bootloader_tasks = make_bootloader_tasks(&programs, &pies).map_err(|e| {
Status::invalid_argument(format!("Could not parse programs/PIEs: {}", e))
})?;

let execution_artifacts =
run_bootloader_in_proof_mode(&bootloader_program, bootloader_tasks)
.map_err(|e| Status::internal(format!("Failed to run bootloader: {e}")))?;
let bootloader_tasks = stone_prover_sdk::cairo_vm::make_bootloader_tasks(&programs, &pies)
.map_err(|e| {
Status::invalid_argument(format!("Could not parse programs/PIEs: {}", e))
})?;

let execution_artifacts = run_bootloader_in_proof_mode(
&bootloader_program,
bootloader_tasks,
Some(Layout::StarknetWithKeccak),
None,
None,
)
.map_err(|e| Status::internal(format!("Failed to run bootloader: {e}")))?;

let prover_parameters =
get_prover_parameters(None, execution_artifacts.public_input.n_steps)?;
Expand Down
16 changes: 7 additions & 9 deletions madara-prover-rpc-server/tests/test_run_bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
mod tests {
use std::path::Path;

use cairo_bootloader::{Task, TaskSpec};
use cairo_vm::air_private_input::{AirPrivateInput, AirPrivateInputSerializable};
use cairo_vm::hint_processor::builtin_hint_processor::bootloader::types::{Task, TaskSpec};
use cairo_vm::types::program::Program;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use rstest::{fixture, rstest};
use stone_prover_sdk::cairo_vm::{run_bootloader_in_proof_mode, ExecutionArtifacts};
use stone_prover_sdk::json::read_json_from_file;
use stone_prover_sdk::models::PublicInput;

use madara_prover_rpc_server::services::starknet_prover::run_bootloader_in_proof_mode;
use stone_prover_sdk::cairo_vm::ExecutionArtifacts;
use test_cases::{get_test_case_file_path, load_test_case_file};
use test_fixtures::{assert_memory_eq, assert_private_input_eq};

Expand Down Expand Up @@ -61,7 +59,7 @@ mod tests {
task: Task::Program(program),
}];

let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks).unwrap();
let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks, None, None, None).unwrap();

assert_eq!(artifacts.public_input, expected_output.public_input);
assert_eq!(artifacts.trace, expected_output.trace);
Expand All @@ -81,12 +79,12 @@ mod tests {
test_case_dir.to_string_lossy()
));

let cairo_pie = CairoPie::from_file(cairo_pie_path.as_path()).unwrap();
let cairo_pie = CairoPie::read_zip_file(cairo_pie_path.as_path()).unwrap();
let tasks = vec![TaskSpec {
task: Task::Pie(cairo_pie),
}];

let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks).unwrap();
let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks, None, None, None).unwrap();

assert_eq!(artifacts.public_input, expected_output.public_input);
assert_eq!(artifacts.trace, expected_output.trace);
Expand All @@ -102,12 +100,12 @@ mod tests {

let os_pie_path = get_test_case_file_path("starknet-os/os.zip");

let os_pie = CairoPie::from_file(os_pie_path.as_path()).unwrap();
let os_pie = CairoPie::read_zip_file(os_pie_path.as_path()).unwrap();
let tasks = vec![TaskSpec {
task: Task::Pie(os_pie),
}];

let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks).unwrap();
let artifacts = run_bootloader_in_proof_mode(&bootloader, tasks, None, None, None).unwrap();

assert_eq!(artifacts.public_input, expected_output.public_input);
assert_eq!(artifacts.trace, expected_output.trace);
Expand Down
5 changes: 2 additions & 3 deletions test-fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io::Read;
use std::path::{Path, PathBuf};

use cairo_vm::air_private_input::{AirPrivateInput, AirPrivateInputSerializable};
use cairo_vm::vm::runners::builtin_runner::OUTPUT_BUILTIN_NAME;
use cairo_vm::Felt252;
use rstest::fixture;
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -190,8 +189,8 @@ pub fn assert_memory_eq(actual: &Vec<u8>, expected: &Vec<u8>) {

pub fn assert_private_input_eq(actual: AirPrivateInput, expected: AirPrivateInput) {
let actual_map = {
let mut map = actual.0;
map.remove(OUTPUT_BUILTIN_NAME);
let map = actual.0;
// map.remove(OUTPUT_BUILTIN_NAME);
map
};

Expand Down

0 comments on commit 58873e6

Please sign in to comment.