Skip to content

Commit

Permalink
proof parser dynamic layout support
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Aug 29, 2024
1 parent cb9c15c commit 579d8bc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 28 deletions.
8 changes: 8 additions & 0 deletions proof_parser/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub enum Builtin {
EcOp,
Keccak,
Poseidon,
RangeCheck96,
AddMod,
MulMod,
}

impl Builtin {
Expand All @@ -29,6 +32,9 @@ impl Builtin {
"ec_op" => Some(Builtin::EcOp),
"keccak" => Some(Builtin::Keccak),
"poseidon" => Some(Builtin::Poseidon),
"range_check96" => Some(Builtin::RangeCheck96),
"add_mod" => Some(Builtin::AddMod),
"mul_mod" => Some(Builtin::MulMod),
_ => None,
}
}
Expand All @@ -44,6 +50,8 @@ impl Builtin {
Builtin::EcOp,
Builtin::Keccak,
Builtin::Poseidon,
Builtin::AddMod,
Builtin::MulMod,
]
}
pub fn sort_segments(
Expand Down
3 changes: 2 additions & 1 deletion proof_parser/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use starknet_types_core::felt::Felt;
use swiftness_air::{
public_memory::PublicInput as PublicInputVerifier,
trace::{
Expand Down Expand Up @@ -118,7 +119,7 @@ impl From<CairoPublicInput> for PublicInputVerifier {
range_check_min: public_input.range_check_min.into(),
range_check_max: public_input.range_check_max.into(),
layout: public_input.layout.into(),
dynamic_params: public_input.dynamic_params.values().map(|x| x.into()).collect(),
dynamic_params: public_input.dynamic_params.values().map(|x| Felt::from(*x)).collect(),
segments: public_input.segments.into_iter().map(|x| x.into()).collect(),
padding_addr: public_input.padding_addr.into(),
padding_value: public_input.padding_value.into(),
Expand Down
7 changes: 2 additions & 5 deletions proof_parser/src/json_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
};
use num_bigint::BigUint;
use serde::Deserialize;
use std::{
collections::{BTreeMap, HashMap},
convert::TryFrom,
};
use std::{collections::HashMap, convert::TryFrom};

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct ProofJSON {
Expand Down Expand Up @@ -55,7 +52,7 @@ pub struct PublicMemoryElement {

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct PublicInput {
dynamic_params: Option<BTreeMap<String, BigUint>>,
dynamic_params: Option<HashMap<String, u32>>,
layout: Layout,
memory_segments: HashMap<String, MemorySegmentAddress>,
n_steps: u32,
Expand Down
40 changes: 21 additions & 19 deletions proof_parser/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use num_bigint::BigUint;
use serde::Deserialize;
use std::{collections::BTreeMap, convert::TryInto, fmt::Display};
use std::{collections::HashMap, fmt::Display};

// For now only the recursive and starknet layouts is supported
#[derive(Debug, Clone, PartialEq, Deserialize)]
Expand All @@ -13,6 +12,7 @@ pub enum Layout {
Small,
Starknet,
StarknetWithKeccak,
Dynamic,
}

impl Layout {
Expand All @@ -25,11 +25,12 @@ impl Layout {
Layout::Small => LayoutConstants::small(),
Layout::Starknet => LayoutConstants::starknet(),
Layout::StarknetWithKeccak => LayoutConstants::starknet_with_keccak(),
Layout::Dynamic => LayoutConstants::dynamic(),
}
}
pub fn get_dynamics_or_consts(
&self,
dynamic_params: &Option<BTreeMap<String, BigUint>>,
dynamic_params: &Option<HashMap<String, u32>>,
) -> Option<LayoutConstants> {
let consts = self.get_consts();

Expand All @@ -39,26 +40,18 @@ impl Layout {
};

Some(LayoutConstants {
cpu_component_step: dynamic_params
cpu_component_step: *dynamic_params
.get("cpu_component_step")
.map(<&BigUint>::try_into)
.map(Result::ok)?
.unwrap_or(consts.cpu_component_step),
constraint_degree: dynamic_params
.unwrap_or(&consts.cpu_component_step),
constraint_degree: *dynamic_params
.get("constraint_degree")
.map(<&BigUint>::try_into)
.map(Result::ok)?
.unwrap_or(consts.constraint_degree),
num_columns_first: dynamic_params
.unwrap_or(&consts.cpu_component_step),
num_columns_first: *dynamic_params
.get("num_columns_first")
.map(<&BigUint>::try_into)
.map(Result::ok)?
.unwrap_or(consts.num_columns_first),
num_columns_second: dynamic_params
.unwrap_or(&consts.cpu_component_step),
num_columns_second: *dynamic_params
.get("num_columns_second")
.map(<&BigUint>::try_into)
.map(Result::ok)?
.unwrap_or(consts.num_columns_second),
.unwrap_or(&consts.cpu_component_step),
})
}
pub fn bytes_encode(&self) -> Vec<u8> {
Expand All @@ -76,6 +69,7 @@ impl Display for Layout {
Layout::Small => write!(f, "small"),
Layout::Starknet => write!(f, "starknet"),
Layout::StarknetWithKeccak => write!(f, "starknet_with_keccak"),
Layout::Dynamic => write!(f, "dynamic"),
}
}
}
Expand Down Expand Up @@ -144,4 +138,12 @@ impl LayoutConstants {
num_columns_second: 1,
}
}
pub fn dynamic() -> Self {
LayoutConstants {
constraint_degree: 2,
cpu_component_step: 1,
num_columns_first: 0,
num_columns_second: 0,
}
}
}
52 changes: 51 additions & 1 deletion proof_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,60 @@ mod tests {
use super::*;

#[test]
fn test_parse() {
fn test_parse_dex() {
let input = include_str!("../../examples/proofs/dex/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_dynamic() {
let input = include_str!("../../examples/proofs/dynamic/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_recursive() {
let input = include_str!("../../examples/proofs/recursive/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_recursive_with_poseidon() {
let input =
include_str!("../../examples/proofs/recursive_with_poseidon/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_small() {
let input = include_str!("../../examples/proofs/small/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_starknet() {
let input = include_str!("../../examples/proofs/starknet/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}

#[test]
fn test_parse_starknet_with_keccak() {
let input =
include_str!("../../examples/proofs/starknet_with_keccak/cairo0_example_proof.json");
let proof_json = serde_json::from_str::<ProofJSON>(input).unwrap();
let stark_proof = StarkProof::try_from(proof_json).unwrap();
let _: StarkProofFromVerifier = stark_proof.into();
}
}
4 changes: 2 additions & 2 deletions proof_parser/src/stark_proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num_bigint::BigUint;
use std::collections::BTreeMap;
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
pub struct StarkProof {
Expand Down Expand Up @@ -140,7 +140,7 @@ pub struct CairoPublicInput {
pub range_check_min: u32,
pub range_check_max: u32,
pub layout: BigUint,
pub dynamic_params: BTreeMap<String, BigUint>,
pub dynamic_params: HashMap<String, u32>,
pub n_segments: usize,
pub segments: Vec<SegmentInfo>,
pub padding_addr: u32,
Expand Down

0 comments on commit 579d8bc

Please sign in to comment.