Skip to content

Commit

Permalink
run cairo-vm in proof mode and store AIR inputs (#19)
Browse files Browse the repository at this point in the history
* run cairo-vm in proof mode and sotr

* fmt

* rm useless file
  • Loading branch information
tcoratger authored Sep 5, 2024
1 parent d95dde6 commit 6e60c6f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 45 deletions.
1 change: 1 addition & 0 deletions cairo_programs/compile_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def compile_cairo(file_name):
str(input_path),
"--output",
str(output_path),
"--proof_mode",
"--no_debug_info",
"--cairo_path",
"cairo_programs",
Expand Down
70 changes: 42 additions & 28 deletions cairo_programs/fibonacci.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"attributes": [],
"builtins": [],
"compiler_version": "0.13.2",
"compiler_version": "0.12.0",
"data": [
"0x40780017fff7fff",
"0x0",
"0x1104800180018000",
"0x4",
"0x10780017fff7fff",
"0x0",
"0x480680017fff8000",
"0x1",
"0x480680017fff8000",
Expand Down Expand Up @@ -31,9 +37,17 @@
"debug_info": null,
"hints": {},
"identifiers": {
"__main__.__end__": {
"pc": 4,
"type": "label"
},
"__main__.__start__": {
"pc": 0,
"type": "label"
},
"__main__.fib": {
"decorators": [],
"pc": 11,
"pc": 17,
"type": "function"
},
"__main__.fib.Args": {
Expand Down Expand Up @@ -70,7 +84,7 @@
"value": 0
},
"__main__.fib.fib_body": {
"pc": 16,
"pc": 22,
"type": "label"
},
"__main__.fib.first_element": {
Expand All @@ -79,10 +93,10 @@
"references": [
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-5), felt*)]"
}
],
Expand All @@ -94,10 +108,10 @@
"references": [
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-3), felt*)]"
}
],
Expand All @@ -109,10 +123,10 @@
"references": [
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 1
},
"pc": 14,
"pc": 20,
"value": "[cast(ap + (-1), felt*)]"
}
],
Expand All @@ -124,10 +138,10 @@
"references": [
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-4), felt*)]"
}
],
Expand All @@ -139,18 +153,18 @@
"references": [
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 1
},
"pc": 17,
"pc": 23,
"value": "[cast(ap + (-1), felt*)]"
}
],
"type": "reference"
},
"__main__.main": {
"decorators": [],
"pc": 0,
"pc": 6,
"type": "function"
},
"__main__.main.Args": {
Expand Down Expand Up @@ -179,10 +193,10 @@
"references": [
{
"ap_tracking_data": {
"group": 1,
"group": 3,
"offset": 0
},
"pc": 8,
"pc": 14,
"value": "[cast(ap + (-1), felt*)]"
}
],
Expand All @@ -195,50 +209,50 @@
"references": [
{
"ap_tracking_data": {
"group": 1,
"group": 3,
"offset": 0
},
"pc": 8,
"pc": 14,
"value": "[cast(ap + (-1), felt*)]"
},
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-5), felt*)]"
},
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-4), felt*)]"
},
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 0
},
"pc": 11,
"pc": 17,
"value": "[cast(fp + (-3), felt*)]"
},
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 1
},
"pc": 14,
"pc": 20,
"value": "[cast(ap + (-1), felt*)]"
},
{
"ap_tracking_data": {
"group": 2,
"group": 4,
"offset": 1
},
"pc": 17,
"pc": 23,
"value": "[cast(ap + (-1), felt*)]"
}
]
Expand Down
26 changes: 21 additions & 5 deletions cairo_programs/transaction_hash.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"attributes": [],
"builtins": [],
"compiler_version": "0.13.2",
"data": ["0x208b7fff7fff7ffe"],
"compiler_version": "0.12.0",
"data": [
"0x40780017fff7fff",
"0x0",
"0x1104800180018000",
"0x4",
"0x10780017fff7fff",
"0x0",
"0x208b7fff7fff7ffe"
],
"debug_info": null,
"hints": {
"0": [
"6": [
{
"accessible_scopes": ["__main__", "__main__.main"],
"code": "print_latest_block_transactions",
"flow_tracking_data": {
"ap_tracking": {
"group": 0,
"group": 2,
"offset": 0
},
"reference_ids": {}
Expand All @@ -20,9 +28,17 @@
]
},
"identifiers": {
"__main__.__end__": {
"pc": 4,
"type": "label"
},
"__main__.__start__": {
"pc": 0,
"type": "label"
},
"__main__.main": {
"decorators": [],
"pc": 0,
"pc": 6,
"type": "function"
},
"__main__.main.Args": {
Expand Down
21 changes: 14 additions & 7 deletions crates/exex/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use cairo_vm::{vm::trace::trace_entry::RelocatedTraceEntry, Felt252};
use cairo_vm::{
air_private_input::AirPrivateInput, air_public_input::PublicInput,
vm::trace::trace_entry::RelocatedTraceEntry, Felt252,
};
use reth_primitives::{
revm_primitives::{AccountInfo, Bytecode},
Address, SealedBlockWithSenders, B256, U256,
Expand Down Expand Up @@ -66,10 +69,12 @@ impl Database {
data TEXT
);
CREATE TABLE IF NOT EXISTS trace (
id INTEGER PRIMARY KEY,
number TEXT,
execution TEXT,
memory TEXT
id INTEGER PRIMARY KEY,
number TEXT,
execution TEXT,
memory TEXT,
air_public_input TEXT,
air_private_input TEXT
);
",
)?;
Expand Down Expand Up @@ -170,15 +175,17 @@ impl Database {
number: u64,
trace: Vec<RelocatedTraceEntry>,
memory: Vec<Felt252>,
air_public_input: PublicInput<'_>,
air_private_input: AirPrivateInput,
) -> eyre::Result<()> {
// Acquire a database connection and begin a transaction.
let mut connection = self.connection();
let tx = connection.transaction()?;

// Insert the trace into the `trace` table.
tx.execute(
"INSERT INTO trace (number, execution, memory) VALUES (?, ?, ?)",
(number, serde_json::to_string(&trace)?, serde_json::to_string(&memory)?),
"INSERT INTO trace (number, execution, memory, air_public_input, air_private_input) VALUES (?, ?, ?, ?, ?)",
(number, serde_json::to_string(&trace)?, serde_json::to_string(&memory)?, serde_json::to_string(&air_public_input)?, serde_json::to_string(&air_private_input.0)?),
)?;

// Commit the transaction to persist all changes.
Expand Down
35 changes: 30 additions & 5 deletions crates/exex/src/exex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{db::Database, execution::execute_block, hints::KakarotHintProcessor};
use cairo_vm::{
air_private_input::AirPrivateInput,
air_public_input::PublicInput,
cairo_run::{cairo_run, CairoRunConfig},
types::layout_name::LayoutName,
vm::trace::trace_entry::RelocatedTraceEntry,
Expand Down Expand Up @@ -59,6 +61,7 @@ impl<Node: FullNodeComponents> KakarotRollup<Node> {
layout: LayoutName::all_cairo,
trace_enabled: true,
relocate_mem: true,
proof_mode: true,
..Default::default()
};

Expand All @@ -83,15 +86,35 @@ impl<Node: FullNodeComponents> KakarotRollup<Node> {
// Load the cairo program from the file
let program = std::fs::read(path)?;

// Execute the Cairo program with the specified configuration and hint
// processor.
let res = cairo_run(&program, &config, &mut hint_processor)?;
// Commit the execution traces to the database
let trace = res.relocated_trace.unwrap_or_default();
let memory =
res.relocated_memory.into_iter().map(|x| x.unwrap_or_default()).collect();

// Extract the execution trace
let trace = res.relocated_trace.clone().unwrap_or_default();

// Extract the relocated memory
let memory = res
.relocated_memory
.clone()
.into_iter()
.map(|x| x.unwrap_or_default())
.collect();

// Extract the public and private inputs
//
// We want to store the public input in the database in order to use them to run
// the prover
let public_input = res.get_air_public_input()?;
let private_input = res.get_air_private_input();

// Commit the execution trace to the database
self.commit_cairo_execution_traces(
committed_chain.tip().number,
trace,
memory,
public_input,
private_input,
)?;
}
}
Expand Down Expand Up @@ -157,8 +180,10 @@ impl<Node: FullNodeComponents> KakarotRollup<Node> {
number: u64,
trace: Vec<RelocatedTraceEntry>,
memory: Vec<Felt252>,
air_public_input: PublicInput<'_>,
air_private_input: AirPrivateInput,
) -> eyre::Result<()> {
self.db.insert_execution_trace(number, trace, memory)
self.db.insert_execution_trace(number, trace, memory, air_public_input, air_private_input)
}
}

Expand Down

0 comments on commit 6e60c6f

Please sign in to comment.