Skip to content

Commit

Permalink
refacto(air/layout): move compute_program_hash to layout/mod.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Dec 20, 2024
1 parent 6fd7101 commit 92d9957
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 39 deletions.
3 changes: 1 addition & 2 deletions crates/air/src/layout/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod global_values;
use crate::{
consts::*,
felt_hex,
layout::stark_curve,
layout::utils::compute_program_hash,
layout::{compute_program_hash, stark_curve},
periodic_columns::{eval_ecdsa_x, eval_ecdsa_y, eval_pedersen_x, eval_pedersen_y},
public_memory::{PublicInput, INITIAL_PC, MAX_ADDRESS, MAX_LOG_N_STEPS, MAX_RANGE_CHECK},
};
Expand Down
3 changes: 1 addition & 2 deletions crates/air/src/layout/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use crate::{
consts::*,
diluted::get_diluted_product,
felt, felt_hex, felt_nonzero, felt_try_nonzero,
layout::utils::compute_program_hash,
layout::{safe_div, stark_curve},
layout::{compute_program_hash, safe_div, stark_curve},
periodic_columns::{
eval_ecdsa_x, eval_ecdsa_y, eval_keccak_round_key0, eval_keccak_round_key1,
eval_keccak_round_key15, eval_keccak_round_key3, eval_keccak_round_key31,
Expand Down
29 changes: 25 additions & 4 deletions crates/air/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::{domains::StarkDomains, public_memory::PublicInput};
use crate::{
consts::{FELT_0, FELT_3},
domains::StarkDomains,
public_memory::PublicInput,
};
use alloc::vec::Vec;
use num_bigint::{BigInt, TryFromBigIntError};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::Felt;
use starknet_crypto::{pedersen_hash, Felt};
use starknet_types_core::felt::FeltIsZeroError;
use swiftness_transcript::transcript::Transcript;

pub mod utils;

#[cfg(feature = "dex")]
pub mod dex;
#[cfg(feature = "dynamic")]
Expand Down Expand Up @@ -112,6 +115,24 @@ pub fn safe_mult(value: Felt, multiplier: Felt) -> Result<Felt, SafeMultError> {
}
}

fn compute_program_hash(
memory: &[Felt],
initial_pc: Felt,
initial_fp: Felt,
) -> Result<Felt, TryFromBigIntError<BigInt>> {
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into()?)
.step_by(2)
.take((initial_fp - FELT_3).to_bigint().try_into()?)
.collect();

let hash = program.iter().fold(FELT_0, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

Ok(program_hash)
}

#[cfg(feature = "std")]
use thiserror::Error;

Expand Down
2 changes: 1 addition & 1 deletion crates/air/src/layout/recursive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
consts::*,
diluted::get_diluted_product,
felt_hex,
layout::utils::compute_program_hash,
layout::compute_program_hash,
periodic_columns::{eval_pedersen_x, eval_pedersen_y},
public_memory::{PublicInput, INITIAL_PC, MAX_ADDRESS, MAX_LOG_N_STEPS, MAX_RANGE_CHECK},
};
Expand Down
2 changes: 1 addition & 1 deletion crates/air/src/layout/recursive_with_poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
consts::*,
diluted::get_diluted_product,
felt_hex,
layout::utils::compute_program_hash,
layout::compute_program_hash,
periodic_columns::{
eval_pedersen_x, eval_pedersen_y, eval_poseidon_poseidon_full_round_key0,
eval_poseidon_poseidon_full_round_key1, eval_poseidon_poseidon_full_round_key2,
Expand Down
3 changes: 1 addition & 2 deletions crates/air/src/layout/small/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod global_values;
use crate::{
consts::*,
felt_hex,
layout::stark_curve,
layout::utils::compute_program_hash,
layout::{compute_program_hash, stark_curve},
periodic_columns::{eval_ecdsa_x, eval_ecdsa_y, eval_pedersen_x, eval_pedersen_y},
public_memory::{PublicInput, INITIAL_PC, MAX_ADDRESS, MAX_LOG_N_STEPS, MAX_RANGE_CHECK},
};
Expand Down
3 changes: 1 addition & 2 deletions crates/air/src/layout/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod global_values;
use crate::{
consts::*,
diluted::get_diluted_product,
layout::stark_curve,
layout::utils::compute_program_hash,
layout::{compute_program_hash, stark_curve},
periodic_columns::{
eval_ecdsa_x, eval_ecdsa_y, eval_pedersen_x, eval_pedersen_y,
eval_poseidon_poseidon_full_round_key0, eval_poseidon_poseidon_full_round_key1,
Expand Down
3 changes: 1 addition & 2 deletions crates/air/src/layout/starknet_with_keccak/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod global_values;
use crate::{
consts::*,
diluted::get_diluted_product,
layout::stark_curve,
layout::utils::compute_program_hash,
layout::{compute_program_hash, stark_curve},
periodic_columns::{
eval_ecdsa_x, eval_ecdsa_y, eval_keccak_round_key0, eval_keccak_round_key1,
eval_keccak_round_key15, eval_keccak_round_key3, eval_keccak_round_key31,
Expand Down
23 changes: 0 additions & 23 deletions crates/air/src/layout/utils.rs

This file was deleted.

0 comments on commit 92d9957

Please sign in to comment.