From 4ad6567c480b5ebc0cd7b397023762dd13c43c72 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Tue, 3 Sep 2024 14:36:20 +0200 Subject: [PATCH] change of proof parser & refactor --- Cargo.toml | 4 +- examples/starknet/Cargo.toml | 4 +- examples/starknet/src/main.rs | 6 +- runner/Cargo.toml | 2 +- runner/src/lib.rs | 2 + runner/src/main.rs | 8 +- runner/src/transform.rs | 400 ++++++++++++++++++++++++++++++++++ runner/src/vec252.rs | 3 +- 8 files changed, 416 insertions(+), 13 deletions(-) create mode 100644 runner/src/transform.rs diff --git a/Cargo.toml b/Cargo.toml index 44505284b..442f2ab69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,12 @@ cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo/", rev = "447 cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo/", rev = "4471a55923663eb8150ea6cd636d5c1038b137d1"} cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo/", rev = "4471a55923663eb8150ea6cd636d5c1038b137d1"} cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo/", rev = "4471a55923663eb8150ea6cd636d5c1038b137d1"} -cairo-proof-parser = { git = "https://github.com/Okm165/cairo-proof-parser", rev = "97a04bbee07330311b38d6f4cecfed3acb237626"} -cairo-vm = "0.9.2" +cairo-vm = "1.0.1" clap = { version = "4.5.2", features = ["derive"] } itertools = "0.13.0" num-bigint = "0.4.4" runner = { path = "runner" } serde = { version = "1", features = ["derive"] } serde_json = "1" +swiftness_proof_parser = { git = "https://github.com/iosis-tech/swiftness", rev = "4647b60cb6e0b6a637a2022a16f0a6e93a117cb3"} thiserror = "1.0" \ No newline at end of file diff --git a/examples/starknet/Cargo.toml b/examples/starknet/Cargo.toml index 7790022f1..dd0a6f7d8 100644 --- a/examples/starknet/Cargo.toml +++ b/examples/starknet/Cargo.toml @@ -7,9 +7,9 @@ edition = "2021" [dependencies] anyhow.workspace = true -cairo-proof-parser.workspace = true clap.workspace = true itertools.workspace = true runner.workspace = true serde_json.workspace = true -serde.workspace = true \ No newline at end of file +serde.workspace = true +swiftness_proof_parser.workspace = true \ No newline at end of file diff --git a/examples/starknet/src/main.rs b/examples/starknet/src/main.rs index 21febedc8..7dbfbb592 100644 --- a/examples/starknet/src/main.rs +++ b/examples/starknet/src/main.rs @@ -1,8 +1,8 @@ -use cairo_proof_parser::parse; use clap::Parser; use itertools::chain; -use runner::{CairoVersion, VecFelt252}; +use runner::{transform::ParsedStarkProof, CairoVersion, VecFelt252}; use std::io::{stdin, Read}; +use swiftness_proof_parser::parse; #[derive(Parser)] #[command(author, version, about)] @@ -17,7 +17,7 @@ fn main() -> anyhow::Result<()> { let mut input = String::new(); stdin().read_to_string(&mut input)?; - let parsed = parse(input)?; + let parsed: ParsedStarkProof = parse(input)?.into(); let config: VecFelt252 = serde_json::from_str(&parsed.config.to_string()).unwrap(); let public_input: VecFelt252 = serde_json::from_str(&parsed.public_input.to_string()).unwrap(); diff --git a/runner/Cargo.toml b/runner/Cargo.toml index 7abb8a725..083db7100 100644 --- a/runner/Cargo.toml +++ b/runner/Cargo.toml @@ -12,11 +12,11 @@ cairo-lang-casm.workspace = true cairo-lang-runner.workspace = true cairo-lang-sierra.workspace = true cairo-lang-utils.workspace = true -cairo-proof-parser.workspace = true cairo-vm.workspace = true clap.workspace = true itertools.workspace = true num-bigint.workspace = true serde_json.workspace = true serde.workspace = true +swiftness_proof_parser.workspace = true thiserror.workspace = true \ No newline at end of file diff --git a/runner/src/lib.rs b/runner/src/lib.rs index 9c786dc84..7abca839a 100644 --- a/runner/src/lib.rs +++ b/runner/src/lib.rs @@ -1,4 +1,6 @@ +pub mod transform; pub mod vec252; + use cairo_felt::Felt252; use clap::ValueEnum; pub use vec252::VecFelt252; diff --git a/runner/src/main.rs b/runner/src/main.rs index 138e0ae75..68af9cfef 100644 --- a/runner/src/main.rs +++ b/runner/src/main.rs @@ -1,10 +1,10 @@ +mod transform; mod vec252; -use crate::vec252::VecFelt252; +use crate::vec252::VecFelt252; use cairo_lang_runner::{Arg, ProfilingInfoCollectionConfig, RunResultValue, SierraCasmRunner}; use cairo_lang_sierra::program::VersionedProgram; use cairo_lang_utils::ordered_hash_map::OrderedHashMap; -use cairo_proof_parser::parse; use clap::Parser; use itertools::{chain, Itertools}; use runner::CairoVersion; @@ -12,6 +12,8 @@ use std::{ fs, io::{stdin, Read}, }; +use swiftness_proof_parser::parse; +use transform::ParsedStarkProof; #[derive(Parser)] #[command(author, version, about)] @@ -28,7 +30,7 @@ fn main() -> anyhow::Result<()> { let cli = Cli::parse(); let mut input = String::new(); stdin().read_to_string(&mut input)?; - let parsed = parse(input)?; + let parsed: ParsedStarkProof = parse(input)?.into(); let function = "main"; diff --git a/runner/src/transform.rs b/runner/src/transform.rs new file mode 100644 index 000000000..51cc46e8c --- /dev/null +++ b/runner/src/transform.rs @@ -0,0 +1,400 @@ +use num_bigint::BigUint; +use swiftness_proof_parser::*; + +pub trait IntoAst { + fn into_ast(self) -> Vec; +} + +impl IntoAst for u32 { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{self}"))] + } +} + +impl IntoAst for usize { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{self}"))] + } +} + +impl IntoAst for BigUint { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{self}"))] + } +} + +impl IntoAst for &BigUint { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{self}"))] + } +} + +impl IntoAst for StarkProof { + fn into_ast(self) -> Vec { + [ + self.config.into_ast(), + self.public_input.into_ast(), + self.unsent_commitment.into_ast(), + self.witness.into_ast(), + ] + .concat() + } +} + +impl IntoAst for StarkConfig { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.traces.into_ast()); + exprs.append(&mut self.composition.into_ast()); + exprs.append(&mut self.fri.into_ast()); + exprs.append(&mut self.proof_of_work.into_ast()); + exprs.append(&mut self.log_trace_domain_size.into_ast()); + exprs.append(&mut self.n_queries.into_ast()); + exprs.append(&mut self.log_n_cosets.into_ast()); + exprs.append(&mut self.n_verifier_friendly_commitment_layers.into_ast()); + exprs + } +} + +impl IntoAst for TracesConfig { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.original.into_ast()); + exprs.append(&mut self.interaction.into_ast()); + exprs + } +} + +impl IntoAst for TableCommitmentConfig { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.n_columns.into_ast()); + exprs.append(&mut self.vector.into_ast()); + exprs + } +} + +impl IntoAst for VectorCommitmentConfig { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.height.into_ast()); + exprs.append(&mut self.n_verifier_friendly_commitment_layers.into_ast()); + exprs + } +} + +impl IntoAst for FriConfig { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.log_input_size.into_ast()); + exprs.append(&mut self.n_layers.into_ast()); + exprs.append(&mut self.inner_layers.into_ast()); + exprs.append(&mut self.fri_step_sizes.into_ast()); + exprs.append(&mut self.log_last_layer_degree_bound.into_ast()); + exprs + } +} + +impl IntoAst for StarkUnsentCommitment { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.traces.into_ast()); + exprs.append(&mut self.composition.into_ast()); + exprs.append(&mut self.oods_values.into_ast()); + exprs.append(&mut self.fri.into_ast()); + exprs.append(&mut self.proof_of_work.into_ast()); + exprs + } +} + +impl IntoAst for TracesUnsentCommitment { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.original.into_ast()); + exprs.append(&mut self.interaction.into_ast()); + exprs + } +} + +impl IntoAst for FriUnsentCommitment { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.inner_layers.into_ast()); + exprs.append(&mut self.last_layer_coefficients.into_ast()); + exprs + } +} + +impl IntoAst for ProofOfWorkUnsentCommitment { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{}", self.nonce))] + } +} + +impl IntoAst for ProofOfWorkConfig { + fn into_ast(self) -> Vec { + vec![Expr::Value(format!("{}", self.n_bits))] + } +} + +impl IntoAst for StarkWitness { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.traces_decommitment.into_ast()); + exprs.append(&mut self.traces_witness.into_ast()); + exprs.append(&mut self.composition_decommitment.into_ast()); + exprs.append(&mut self.composition_witness.into_ast()); + exprs.append(&mut self.fri_witness.into_ast()); + exprs + } +} + +impl IntoAst for TracesDecommitment { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.original.into_ast()); + exprs.append(&mut self.interaction.into_ast()); + exprs + } +} + +impl IntoAst for TableDecommitment { + fn into_ast(self) -> Vec { + let mut exprs = vec![Expr::Value(format!("{}", self.n_values))]; + exprs.append(&mut self.values.into_ast()); + exprs + } +} + +impl IntoAst for TracesWitness { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.original.into_ast()); + exprs.append(&mut self.interaction.into_ast()); + exprs + } +} + +impl IntoAst for TableCommitmentWitness { + fn into_ast(self) -> Vec { + self.vector.into_ast() + } +} + +impl IntoAst for TableCommitmentWitnessFlat { + fn into_ast(self) -> Vec { + self.vector.into_ast() + } +} + +impl IntoAst for VectorCommitmentWitness { + fn into_ast(self) -> Vec { + let mut exprs = vec![Expr::Value(format!("{}", self.n_authentications))]; + exprs.append(&mut self.authentications.into_ast()); + exprs + } +} + +impl IntoAst for VectorCommitmentWitnessFlat { + fn into_ast(self) -> Vec { + let mut exprs = vec![Expr::Value(format!("{}", self.n_authentications))]; + exprs.append( + &mut self + .authentications + .iter() + .flat_map(|x| x.into_ast()) + .collect::>(), + ); + exprs + } +} + +impl IntoAst for FriWitness { + fn into_ast(self) -> Vec { + self.layers.into_ast() + } +} + +impl IntoAst for FriLayerWitness { + fn into_ast(self) -> Vec { + let mut exprs = vec![Expr::Value(format!("{}", self.n_leaves))]; + exprs.append( + &mut self + .leaves + .iter() + .flat_map(|x| x.into_ast()) + .collect::>(), + ); + exprs.append(&mut self.table_witness.into_ast()); + exprs + } +} + +impl IntoAst for PublicInput { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.log_n_steps.into_ast()); + exprs.append(&mut self.range_check_min.into_ast()); + exprs.append(&mut self.range_check_max.into_ast()); + exprs.append(&mut self.layout.into_ast()); + exprs.push(Expr::Array( + self.dynamic_params + .values() + .map(|v| Expr::Value(format!("{v}"))) + .collect(), + )); + exprs.append(&mut self.n_segments.into_ast()); + exprs.append(&mut self.segments.into_ast()); + exprs.append(&mut self.padding_addr.into_ast()); + exprs.append(&mut self.padding_value.into_ast()); + exprs.append(&mut self.main_page_len.into_ast()); + exprs.append(&mut self.main_page.into_ast()); + exprs.append(&mut self.n_continuous_pages.into_ast()); + exprs.append(&mut self.continuous_page_headers.into_ast()); + exprs + } +} + +impl IntoAst for SegmentInfo { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.begin_addr.into_ast()); + exprs.append(&mut self.stop_ptr.into_ast()); + exprs + } +} + +impl IntoAst for PubilcMemoryCell { + fn into_ast(self) -> Vec { + let mut exprs = vec![]; + exprs.append(&mut self.address.into_ast()); + exprs.append(&mut self.value.into_ast()); + exprs + } +} + +impl IntoAst for Vec +where + T: IntoAst, +{ + fn into_ast(self) -> Vec { + vec![Expr::Array( + self.into_iter().flat_map(|x| x.into_ast()).collect(), + )] + } +} + +impl From for Exprs { + fn from(v: StarkConfig) -> Self { + Exprs(v.into_ast()) + } +} + +impl From for Exprs { + fn from(v: PublicInput) -> Self { + Exprs(v.into_ast()) + } +} + +impl From for Exprs { + fn from(v: StarkUnsentCommitment) -> Self { + Exprs(v.into_ast()) + } +} + +impl From for Exprs { + fn from(v: StarkWitness) -> Self { + Exprs(v.into_ast()) + } +} + +impl From for Exprs { + fn from(proof: StarkProof) -> Self { + Exprs(proof.into_ast()) + } +} + +use std::{ + fmt::Display, + ops::{Deref, DerefMut}, +}; + +#[derive(Debug, Clone)] +pub enum Expr { + Value(String), + Array(Vec), +} + +impl Display for Expr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Expr::Value(v) => write!(f, "\"{v}\""), + Expr::Array(v) => { + write!(f, "[")?; + + for (i, expr) in v.iter().enumerate() { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{expr}")?; + } + + write!(f, "]")?; + + Ok(()) + } + } + } +} + +#[derive(Debug, Clone)] +pub struct Exprs(pub Vec); + +impl Display for Exprs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[")?; + + for (i, expr) in self.iter().enumerate() { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{expr}")?; + } + + write!(f, "]")?; + + Ok(()) + } +} + +impl Deref for Exprs { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Exprs { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +pub struct ParsedStarkProof { + pub config: Exprs, + pub public_input: Exprs, + pub unsent_commitment: Exprs, + pub witness: Exprs, +} + +impl From for ParsedStarkProof { + fn from(value: StarkProof) -> Self { + Self { + config: Exprs::from(value.config), + public_input: Exprs::from(value.public_input), + unsent_commitment: Exprs::from(value.unsent_commitment), + witness: Exprs::from(value.witness), + } + } +} diff --git a/runner/src/vec252.rs b/runner/src/vec252.rs index 0321a251d..b6e69540c 100644 --- a/runner/src/vec252.rs +++ b/runner/src/vec252.rs @@ -1,8 +1,7 @@ -use std::{ops::Deref, str::FromStr}; - use cairo_felt::Felt252; use serde::{de::Visitor, Deserialize}; use serde_json::Value; +use std::{ops::Deref, str::FromStr}; use thiserror::Error; #[derive(Error, Debug)]