From 9279c17714ffea085f9d92bd8ec5f2a557fa5d4c Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Fri, 23 Feb 2024 10:26:24 +0100 Subject: [PATCH] submodule --- .gitmodules | 3 - madara-prover-rpc-server/src/cairo.rs | 164 +++--------------- .../src/services/common.rs | 2 +- .../src/services/prover.rs | 24 ++- .../src/services/starknet_prover.rs | 2 +- .../tests/test_run_bootloader.rs | 2 +- 6 files changed, 42 insertions(+), 155 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 505529a..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "stone-prover/dependencies/stone-prover"] - path = stone-prover/dependencies/stone-prover - url = https://github.com/starkware-libs/stone-prover diff --git a/madara-prover-rpc-server/src/cairo.rs b/madara-prover-rpc-server/src/cairo.rs index b69fc05..3560334 100644 --- a/madara-prover-rpc-server/src/cairo.rs +++ b/madara-prover-rpc-server/src/cairo.rs @@ -1,145 +1,29 @@ -use bincode::error::EncodeError; -use cairo_vm::air_private_input::AirPrivateInput; -use cairo_vm::air_public_input::PublicInputError; -use cairo_vm::cairo_run::{ - cairo_run, write_encoded_memory, write_encoded_trace, CairoRunConfig, EncodeTraceError, -}; -use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; -use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; -use cairo_vm::vm::errors::trace_errors::TraceError; -use cairo_vm::vm::runners::cairo_runner::CairoRunner; -use cairo_vm::vm::vm_core::VirtualMachine; -use thiserror::Error; +use stone_prover_sdk::cairo_vm::ExecutionError; use tonic::Status; -use stone_prover_sdk::models::PublicInput; - -#[derive(Error, Debug)] -pub enum ExecutionError { - #[error(transparent)] - RunFailed(#[from] CairoRunError), - #[error(transparent)] - GeneratePublicInput(#[from] PublicInputError), - #[error(transparent)] - GenerateTrace(#[from] TraceError), - #[error(transparent)] - EncodeMemory(EncodeTraceError), - #[error(transparent)] - EncodeTrace(EncodeTraceError), - #[error(transparent)] - SerializePublicInput(#[from] serde_json::Error), -} - -impl From for Status { - fn from(value: ExecutionError) -> Self { - match value { - ExecutionError::RunFailed(cairo_run_error) => { - Status::internal(format!("Failed to run Cairo program: {}", cairo_run_error)) - } - ExecutionError::GeneratePublicInput(public_input_error) => Status::internal(format!( - "Failed to generate public input: {}", - public_input_error - )), - ExecutionError::GenerateTrace(trace_error) => Status::internal(format!( - "Failed to generate execution trace: {}", - trace_error - )), - ExecutionError::EncodeMemory(encode_error) => Status::internal(format!( - "Failed to encode execution memory: {}", - encode_error - )), - ExecutionError::EncodeTrace(encode_error) => Status::internal(format!( - "Failed to encode execution memory: {}", - encode_error - )), - ExecutionError::SerializePublicInput(serde_error) => { - Status::internal(format!("Failed to serialize public input: {}", serde_error)) - } +pub fn execution_error_to_status(execution_error: ExecutionError) -> Status { + match execution_error { + ExecutionError::RunFailed(cairo_run_error) => { + Status::internal(format!("Failed to run Cairo program: {}", cairo_run_error)) + } + ExecutionError::GeneratePublicInput(public_input_error) => Status::internal(format!( + "Failed to generate public input: {}", + public_input_error + )), + ExecutionError::GenerateTrace(trace_error) => Status::internal(format!( + "Failed to generate execution trace: {}", + trace_error + )), + ExecutionError::EncodeMemory(encode_error) => Status::internal(format!( + "Failed to encode execution memory: {}", + encode_error + )), + ExecutionError::EncodeTrace(encode_error) => Status::internal(format!( + "Failed to encode execution memory: {}", + encode_error + )), + ExecutionError::SerializePublicInput(serde_error) => { + Status::internal(format!("Failed to serialize public input: {}", serde_error)) } } } - -/// An in-memory writer for bincode encoding. -#[derive(Default)] -pub struct MemWriter { - pub buf: Vec, -} - -impl MemWriter { - pub fn new() -> Self { - Self::default() - } -} - -impl bincode::enc::write::Writer for MemWriter { - fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> { - self.buf.extend_from_slice(bytes); - Ok(()) - } -} - -/// Run a Cairo program in proof mode. -/// -/// * `program_content`: Compiled program content. -pub fn run_in_proof_mode( - program_content: &[u8], -) -> Result<(CairoRunner, VirtualMachine), CairoRunError> { - 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, - }; - - let mut hint_processor = BuiltinHintProcessor::new_empty(); - - cairo_run(program_content, &cairo_run_config, &mut hint_processor) -} - -pub struct ExecutionArtifacts { - pub public_input: PublicInput, - pub private_input: AirPrivateInput, - pub memory: Vec, - pub trace: Vec, -} - -/// 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 { - let memory = &cairo_runner.relocated_memory; - let trace = cairo_runner - .relocated_trace - .as_ref() - .ok_or(ExecutionError::GenerateTrace(TraceError::TraceNotEnabled))?; - - let mut memory_writer = MemWriter::new(); - write_encoded_memory(memory, &mut memory_writer).map_err(ExecutionError::EncodeMemory)?; - let memory_raw = memory_writer.buf; - - let mut trace_writer = MemWriter::new(); - 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 public_input = PublicInput::try_from(cairo_vm_public_input)?; - - let private_input = cairo_runner.get_air_private_input(&vm); - - Ok(ExecutionArtifacts { - public_input, - private_input, - memory: memory_raw, - trace: trace_raw, - }) -} diff --git a/madara-prover-rpc-server/src/services/common.rs b/madara-prover-rpc-server/src/services/common.rs index a2ab961..cdc45aa 100644 --- a/madara-prover-rpc-server/src/services/common.rs +++ b/madara-prover-rpc-server/src/services/common.rs @@ -7,8 +7,8 @@ use stone_prover_sdk::prover::run_prover_async; use stone_prover_sdk::verifier::run_verifier_with_annotations_async; use tonic::Status; -use crate::cairo::ExecutionArtifacts; use crate::evm_adapter; +use stone_prover_sdk::cairo_vm::ExecutionArtifacts; pub async fn call_prover( execution_artifacts: &ExecutionArtifacts, diff --git a/madara-prover-rpc-server/src/services/prover.rs b/madara-prover-rpc-server/src/services/prover.rs index d215b60..729f8bf 100644 --- a/madara-prover-rpc-server/src/services/prover.rs +++ b/madara-prover-rpc-server/src/services/prover.rs @@ -1,24 +1,28 @@ use cairo_vm::air_private_input::{AirPrivateInput, AirPrivateInputSerializable}; use tonic::{Request, Response, Status}; -use crate::cairo::{ - extract_execution_artifacts, run_in_proof_mode, ExecutionArtifacts, ExecutionError, -}; +use crate::cairo::execution_error_to_status; use crate::services::common; use crate::services::common::format_prover_error; use crate::services::prover::prover_proto::prover_server::Prover; use crate::services::prover::prover_proto::{ ExecutionRequest, ExecutionResponse, ProverRequest, ProverResponse, }; +use stone_prover_sdk::cairo_vm::{ + extract_execution_artifacts, run_in_proof_mode, ExecutionArtifacts, ExecutionError, +}; use stone_prover_sdk::error::ProverError; -use stone_prover_sdk::models::{Proof, ProverConfig, ProverWorkingDirectory}; +use stone_prover_sdk::models::{Layout, Proof, ProverConfig, ProverWorkingDirectory}; pub mod prover_proto { tonic::include_proto!("prover"); } -fn run_cairo_program_in_proof_mode(program: &[u8]) -> Result { - let (cairo_runner, vm) = run_in_proof_mode(program)?; +fn run_cairo_program_in_proof_mode( + program: &[u8], + layout: Layout, +) -> Result { + let (cairo_runner, vm) = run_in_proof_mode(program, layout)?; extract_execution_artifacts(cairo_runner, vm) } @@ -33,7 +37,7 @@ fn format_execution_result( trace: artifacts.trace, }) .map_err(|_| Status::internal("Failed to serialize public input")), - Err(e) => Err(e.into()), + Err(e) => Err(execution_error_to_status(e)), } } @@ -69,7 +73,8 @@ impl Prover for ProverService { ) -> Result, Status> { let execution_request = request.into_inner(); - let execution_result = run_cairo_program_in_proof_mode(&execution_request.program); + let layout = Layout::StarknetWithKeccak; + let execution_result = run_cairo_program_in_proof_mode(&execution_request.program, layout); let execution_result = format_execution_result(execution_result); execution_result.map(Response::new) @@ -123,8 +128,9 @@ impl Prover for ProverService { } = request.into_inner(); let prover_config = get_prover_config(prover_config_str)?; + let layout = Layout::StarknetWithKeccak; - let execution_artifacts = run_cairo_program_in_proof_mode(&program); + let execution_artifacts = run_cairo_program_in_proof_mode(&program, layout); let execution_artifacts = execution_artifacts .map_err(|e| Status::internal(format!("Failed to run program: {e}")))?; diff --git a/madara-prover-rpc-server/src/services/starknet_prover.rs b/madara-prover-rpc-server/src/services/starknet_prover.rs index fe980e1..21166ea 100644 --- a/madara-prover-rpc-server/src/services/starknet_prover.rs +++ b/madara-prover-rpc-server/src/services/starknet_prover.rs @@ -22,7 +22,6 @@ use tonic::{Request, Response, Status}; use stone_prover_sdk::error::ProverError; use stone_prover_sdk::models::{Proof, ProverConfig, ProverWorkingDirectory}; -use crate::cairo::{extract_execution_artifacts, ExecutionArtifacts, ExecutionError}; use crate::services::common::{ call_prover, format_prover_error, get_prover_parameters, verify_and_annotate_proof, }; @@ -30,6 +29,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}; pub mod starknet_prover_proto { tonic::include_proto!("starknet_prover"); diff --git a/madara-prover-rpc-server/tests/test_run_bootloader.rs b/madara-prover-rpc-server/tests/test_run_bootloader.rs index bb0c7ba..7892a01 100644 --- a/madara-prover-rpc-server/tests/test_run_bootloader.rs +++ b/madara-prover-rpc-server/tests/test_run_bootloader.rs @@ -10,8 +10,8 @@ mod tests { use stone_prover_sdk::json::read_json_from_file; use stone_prover_sdk::models::PublicInput; - use madara_prover_rpc_server::cairo::ExecutionArtifacts; 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};