Skip to content

Commit

Permalink
Deserialization (#29)
Browse files Browse the repository at this point in the history
* new runner version

* apply clippy

* StarkConfig parsed

* PublicInput

* StarkUnsentCommitment

* partial StarkWitness

* FriWitness

* clippy
  • Loading branch information
neotheprogramist authored Jan 2, 2024
1 parent 9554028 commit 91a80bb
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 37 deletions.
3 changes: 0 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[package]
name = "cairo_verifier"
version = "0.1.0"

[lib]
sierra-text = true
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

scarb build
cd runner
cargo run --release -- ../target/dev/cairo_verifier.sierra < resources/parserin.txt
cargo run --release -- ../target/dev/cairo_verifier.sierra.json < resources/parserin.txt
2 changes: 1 addition & 1 deletion runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0.76"
cairo-args-runner = "0.1.1"
cairo-args-runner = "1.0.0"
clap = { version = "4.4.11", features = ["derive"] }
lalrpop-util = { version = "0.20.0", features = ["lexer", "unicode"] }
serde = { version = "1.0.193", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions runner/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Expr {
impl Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expr::Value(v) => write!(f, "{v}"),
Expr::Value(v) => write!(f, "\"{v}\""),
Expr::Array(v) => {
write!(f, "[")?;

Expand All @@ -26,7 +26,7 @@ impl Display for Expr {
write!(f, "]")?;

Ok(())
},
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{stdin, Read};

use cairo_args_runner::{run, WrappedArg};
use cairo_args_runner::{run, Arg, VecFelt252};
use clap::Parser;
use lalrpop_util::lalrpop_mod;

Expand All @@ -23,13 +23,13 @@ fn main() -> anyhow::Result<()> {
let parsed = parser::CairoParserOutputParser::new()
.parse(&input)
.map_err(|e| anyhow::anyhow!("{}", e))?;
let result = format!("{parsed}");
let result = parsed.to_string();

let target = cli.target;
let function = "main";
let args: WrappedArg = serde_json::from_str(&result).unwrap();
let args: VecFelt252 = serde_json::from_str(&result).unwrap();

let result = run(&target, &function, &args)?;
let result = run(&target, function, &[Arg::Array(args.to_vec())])?;

println!("{result:?}");
Ok(())
Expand Down
10 changes: 10 additions & 0 deletions runner/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ CairoParserOutputInner: Exprs = {
<n:Num> => Exprs(vec![Expr::Value(n)]),
"[" <n:Comma<CairoParserOutputInner>> "]" => Exprs(vec![Expr::Array(n.iter().flat_map(|x| x.iter().cloned()).collect())]),
StructName "()" => Exprs(Vec::new()),
"FriWitness" "(" "layers=" "[" <n:Comma<FriWitnessLayers>> "]" ")" => Exprs(vec![Expr::Array(n.iter().flat_map(|x| x.iter().cloned()).collect())]),
StructName "(" <n:Comma<Arg>> ")" => Exprs(n.iter().flat_map(|x| x.iter().cloned()).collect()),
};

Arg: Exprs = {
ArgName "=" <n:CairoParserOutputInner> => n,
};

FriWitnessLayers: Exprs = {
<n:Num> "," "[" <a:Comma<Num>> "]" => Exprs(vec![Expr::Value(n)].into_iter().chain(a.into_iter().map(Expr::Value)).collect()),
StructName "(" ArgName "=" StructName "(" <n:FriWitnessLayersArgs> ")" ")" => n,
};

FriWitnessLayersArgs: Exprs = {
ArgName "=" <n:Num> "," ArgName "=" "[" <a:Comma<Num>> "]" => Exprs(vec![Expr::Value(n)].into_iter().chain(a.into_iter().map(Expr::Value)).collect()),
};

Comma<T>: Vec<T> = {
<mut v:(<T> ",")*> <e:T?> => match e {
None => v,
Expand Down
3 changes: 3 additions & 0 deletions src/input_structs.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod public_input;
mod stark_config;
mod stark_proof;
mod stark_unsent_commitment;
mod stark_witness;
16 changes: 16 additions & 0 deletions src/input_structs/public_input.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Drop, Serde)]
struct PublicInput {
log_n_steps: felt252,
range_check_min: felt252,
range_check_max: felt252,
layout: felt252,
dynamic_params: Array<felt252>,
n_segments: felt252,
segments: Array<felt252>,
padding_addr: felt252,
padding_value: felt252,
main_page_len: felt252,
main_page: Array<felt252>,
n_continuous_pages: felt252,
continuous_page_headers: Array<felt252>,
}
38 changes: 19 additions & 19 deletions src/input_structs/stark_config.cairo
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct StarkConfig {
traces: TracesConfig,
composition: TableCommitmentConfig,
// fri: FriConfig,
// proof_of_work: ProofOfWorkConfig,
// // Log2 of the trace domain size.
// log_trace_domain_size: felt252,
// // Number of queries to the last component, FRI.
// n_queries: felt252,
// // Log2 of the number of cosets composing the evaluation domain, where the coset size is the
// // trace length.
// log_n_cosets: felt252,
// // Number of layers that use a verifier friendly hash in each commitment.
// n_verifier_friendly_commitment_layers: felt252,
fri: FriConfig,
proof_of_work: ProofOfWorkConfig,
// Log2 of the trace domain size.
log_trace_domain_size: felt252,
// Number of queries to the last component, FRI.
n_queries: felt252,
// Log2 of the number of cosets composing the evaluation domain, where the coset size is the
// trace length.
log_n_cosets: felt252,
// Number of layers that use a verifier friendly hash in each commitment.
n_verifier_friendly_commitment_layers: felt252,
}

#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct TracesConfig {
original: TableCommitmentConfig,
interaction: TableCommitmentConfig,
}

#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct TableCommitmentConfig {
columns: felt252,
vector: VectorCommitmentConfig
}

#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct VectorCommitmentConfig {
height: felt252,
verifier_friendly_commitment_layers: felt252,
}

#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct FriConfig {
// Log2 of the size of the input layer to FRI.
log_input_size: felt252,
// Number of layers in the FRI. Inner + last layer.
n_layers: felt252,
// Array of size n_layers - 1, each entry is a configuration of a table commitment for the
// corresponding inner layer.
inner_layers: TableCommitmentConfig,
inner_layers: Array<felt252>,
// Array of size n_layers, each entry represents the FRI step size,
// i.e. the number of FRI-foldings between layer i and i+1.
fri_step_sizes: felt252,
fri_step_sizes: Array<felt252>,
log_last_layer_degree_bound: felt252,
}

#[derive(Copy, Drop)]
#[derive(Drop, Serde)]
struct ProofOfWorkConfig {
// Proof of work difficulty (number of bits required to be 0).
n_bits: felt252,
Expand Down
14 changes: 9 additions & 5 deletions src/input_structs/stark_proof.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use cairo_verifier::input_structs::stark_config::StarkConfig;
use cairo_verifier::input_structs::{
stark_config::StarkConfig, public_input::PublicInput,
stark_unsent_commitment::StarkUnsentCommitment, stark_witness::StarkWitness,
};

#[derive(Copy, Drop)]

#[derive(Drop, Serde)]
struct StarkProof {
config: StarkConfig,
// public_input: PublicInput,
// unsent_commitment: StarkUnsentCommitment,
// witness: StarkWitness,
public_input: PublicInput,
unsent_commitment: StarkUnsentCommitment,
witness: StarkWitness,
}
25 changes: 25 additions & 0 deletions src/input_structs/stark_unsent_commitment.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[derive(Drop, Serde)]
struct StarkUnsentCommitment {
traces: TracesUnsentCommitment,
composition: felt252,
oods_values: Array<felt252>,
fri: FriUnsentCommitment,
proof_of_work: ProofOfWorkUnsentCommitment,
}

#[derive(Drop, Serde)]
struct TracesUnsentCommitment {
original: felt252,
interaction: felt252,
}

#[derive(Drop, Serde)]
struct FriUnsentCommitment {
inner_layers: Array<felt252>,
last_layer_coefficients: Array<felt252>,
}

#[derive(Drop, Serde)]
struct ProofOfWorkUnsentCommitment {
nonce: felt252,
}
42 changes: 42 additions & 0 deletions src/input_structs/stark_witness.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[derive(Drop, Serde)]
struct StarkWitness {
traces_decommitment: TracesDecommitment,
traces_witness: TracesWitness,
interaction: TableCommitmentWitness,
composition_decommitment: TableDecommitment,
composition_witness: TableCommitmentWitness,
fri_witness: FriWitness,
}

#[derive(Drop, Serde)]
struct TracesDecommitment {
original: TableDecommitment,
interaction: TableDecommitment,
}

#[derive(Drop, Serde)]
struct TableDecommitment {
n_values: felt252,
values: Array<felt252>,
}

#[derive(Drop, Serde)]
struct TracesWitness {
original: TableCommitmentWitness,
}

#[derive(Drop, Serde)]
struct TableCommitmentWitness {
vector: VectorCommitmentWitness,
}

#[derive(Drop, Serde)]
struct VectorCommitmentWitness {
n_authentications: felt252,
authentications: Array<felt252>,
}

#[derive(Drop, Serde)]
struct FriWitness {
layers: Array<felt252>,
}
6 changes: 4 additions & 2 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod vector_commitment;

use cairo_verifier::input_structs::stark_proof::StarkProof;

fn main(stark_proof: StarkProof) -> (felt252, felt252) {
(stark_proof.config.traces.original.columns, stark_proof.config.traces.interaction.columns)

fn main(x: Array<felt252>) {
let mut x_span = x.span();
let stark_proof: StarkProof = Serde::deserialize(ref x_span).unwrap();
}

0 comments on commit 91a80bb

Please sign in to comment.