Skip to content

Commit

Permalink
Merge pull request #57 from tdelabro/verify-return-all-output
Browse files Browse the repository at this point in the history
feat: verifier returns all outputs rather than their hash
  • Loading branch information
Okm165 authored Jan 9, 2025
2 parents ccd16d3 + f151e66 commit f7c539f
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 52 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.1.3"

[dependencies]
clap = { version = "4.4.4", features = ["derive"] }
starknet-core = "0.12.0"

swiftness_air = { path = "../crates/air", default-features = false, features = [
"std",
Expand Down Expand Up @@ -68,4 +69,4 @@ blake2s_160_lsb = [
blake2s_248_lsb = [
"swiftness_air/blake2s_248_lsb",
"swiftness_stark/blake2s_248_lsb",
]
]
15 changes: 13 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ struct CairoVMVerifier {
/// Path to proof JSON file
#[clap(short, long)]
proof: PathBuf,
/// Whether to print the pedersen hash of the output instead of the full output.
#[clap(short = 'o', long, num_args=0..=1, default_missing_value = "true")]
print_output_hash: Option<bool>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = CairoVMVerifier::parse();
let stark_proof = parse(std::fs::read_to_string(cli.proof)?)?.transform_to();
let security_bits = stark_proof.config.security_bits();
let result = stark_proof.verify::<Layout>(security_bits)?;
println!("{:?}", result);
let (program_hash, program_output) = stark_proof.verify::<Layout>(security_bits)?;

println!("program hash: {:#x}", program_hash);
if let Some(true) = cli.print_output_hash {
let hash = starknet_core::crypto::compute_hash_on_elements(&program_output);
println!("program output hash: {:#x}", hash);
} else {
println!("program output: {:x?}", program_output);
}

Ok(())
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -405,10 +407,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,9 @@ impl LayoutTrait for Layout {
Ok(autogenerated::check_asserts(&dynamic_params, stark_domains)?)
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -778,10 +780,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &felt!(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &felt!(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
5 changes: 4 additions & 1 deletion crates/air/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{domains::StarkDomains, public_memory::PublicInput};
use alloc::vec::Vec;
use num_bigint::{BigInt, TryFromBigIntError};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::Felt;
Expand Down Expand Up @@ -84,7 +85,9 @@ pub trait LayoutTrait {
witness: crate::trace::Witness,
) -> Result<(), crate::trace::decommit::Error>;

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError>;
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError>;
}

pub trait StaticLayoutTrait {
Expand Down
11 changes: 6 additions & 5 deletions crates/air/src/layout/recursive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -405,10 +407,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/recursive_with_poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -462,10 +464,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/small/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -405,10 +407,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -532,10 +534,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
11 changes: 6 additions & 5 deletions crates/air/src/layout/starknet_with_keccak/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ impl LayoutTrait for Layout {
Ok(())
}

fn verify_public_input(public_input: &PublicInput) -> Result<(Felt, Felt), PublicInputError> {
fn verify_public_input(
public_input: &PublicInput,
) -> Result<(Felt, Vec<Felt>), PublicInputError> {
let public_segments = &public_input.segments;

let initial_pc = public_segments
Expand Down Expand Up @@ -581,10 +583,9 @@ impl LayoutTrait for Layout {
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
let output = &memory[memory.len() - output_len * 2..];
let hash = output.iter().skip(1).step_by(2).fold(FELT_0, |acc, e| pedersen_hash(&acc, e));
let output_hash = pedersen_hash(&hash, &Felt::from(output_len));
let output =
memory[memory.len() - output_len * 2..].iter().skip(1).step_by(2).cloned().collect();

Ok((program_hash, output_hash))
Ok((program_hash, output))
}
}
Loading

0 comments on commit f7c539f

Please sign in to comment.