Skip to content

Commit

Permalink
submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Feb 23, 2024
1 parent 45e9db6 commit de14d76
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 155 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

164 changes: 24 additions & 140 deletions madara-prover-rpc-server/src/cairo.rs
Original file line number Diff line number Diff line change
@@ -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<ExecutionError> 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<u8>,
}

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<u8>,
pub trace: Vec<u8>,
}

/// 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
.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,
})
}
2 changes: 1 addition & 1 deletion madara-prover-rpc-server/src/services/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 15 additions & 9 deletions madara-prover-rpc-server/src/services/prover.rs
Original file line number Diff line number Diff line change
@@ -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<ExecutionArtifacts, ExecutionError> {
let (cairo_runner, vm) = run_in_proof_mode(program)?;
fn run_cairo_program_in_proof_mode(
program: &[u8],
layout: Layout,
) -> Result<ExecutionArtifacts, ExecutionError> {
let (cairo_runner, vm) = run_in_proof_mode(program, layout)?;
extract_execution_artifacts(cairo_runner, vm)
}

Expand All @@ -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)),
}
}

Expand Down Expand Up @@ -69,7 +73,8 @@ impl Prover for ProverService {
) -> Result<Response<ExecutionResponse>, 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)
Expand Down Expand Up @@ -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}")))?;

Expand Down
2 changes: 1 addition & 1 deletion madara-prover-rpc-server/src/services/starknet_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ 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,
};
use crate::services::starknet_prover::starknet_prover_proto::starknet_prover_server::StarknetProver;
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");
Expand Down
2 changes: 1 addition & 1 deletion madara-prover-rpc-server/tests/test_run_bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down

0 comments on commit de14d76

Please sign in to comment.