-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: execute Cairo programs on the RPC server (#8)
Users can now run any Cairo program using the RPC client + server. The new `Execute` RPC endpoint sends a program to the server and returns execution traces. The server now relies on the `cairo-vm` crate to run programs in proof mode and then extract artifacts (public input, memory and trace).
- Loading branch information
Olivier Desenfans
authored
Nov 22, 2023
1 parent
b933115
commit bcd361d
Showing
14 changed files
with
2,190 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use bincode::error::EncodeError; | ||
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 crate::prover::ExecutionResponse; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ExecutionError { | ||
#[error("failed to generate public input")] | ||
GeneratePublicInput(#[from] PublicInputError), | ||
#[error("failed to generate program execution trace")] | ||
GenerateTrace(#[from] TraceError), | ||
#[error("failed to encode the VM memory in binary format")] | ||
EncodeMemory(EncodeTraceError), | ||
#[error("failed to encode the execution trace in binary format")] | ||
EncodeTrace(EncodeTraceError), | ||
#[error("failed to serialize the public input")] | ||
SerializePublicInput(#[from] serde_json::Error), | ||
} | ||
|
||
/// An in-memory writer for bincode encoding. | ||
pub struct MemWriter { | ||
pub buf: Vec<u8>, | ||
} | ||
|
||
impl MemWriter { | ||
pub fn new() -> Self { | ||
Self { buf: vec![] } | ||
} | ||
} | ||
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 = "plain"; | ||
|
||
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) | ||
} | ||
|
||
// TODO: split in two (extract data + format to ExecutionResponse) | ||
/// Extracts execution artifacts from the runner and VM (after execution). | ||
/// | ||
/// * `cairo_runner` Cairo runner object. | ||
/// * `vm`: Cairo VM object. | ||
pub fn extract_run_artifacts( | ||
cairo_runner: CairoRunner, | ||
vm: VirtualMachine, | ||
) -> Result<ExecutionResponse, ExecutionError> { | ||
let cairo_vm_public_input = cairo_runner.get_air_public_input(&vm)?; | ||
let memory = cairo_runner.relocated_memory.clone(); | ||
let trace = vm.get_relocated_trace()?; | ||
|
||
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 public_input_str = serde_json::to_string(&cairo_vm_public_input)?; | ||
|
||
Ok(ExecutionResponse { | ||
public_input: public_input_str, | ||
memory: memory_raw, | ||
trace: trace_raw, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
stone-prover/tests/fixtures/fibonacci-no-hint/fibonacci.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
func main() { | ||
// Call fib(1, 1, 10). | ||
let result: felt = fib(1, 1, 10); | ||
|
||
// Make sure the 10th Fibonacci number is 144. | ||
assert result = 144; | ||
ret; | ||
} | ||
|
||
func fib(first_element, second_element, n) -> (res: felt) { | ||
jmp fib_body if n != 0; | ||
tempvar result = second_element; | ||
return (second_element,); | ||
|
||
fib_body: | ||
tempvar y = first_element + second_element; | ||
return fib(second_element, y, n - 1); | ||
} |
Oops, something went wrong.