Skip to content

Commit

Permalink
Merge pull request #59 from tdelabro/factorize-compute-program-hash
Browse files Browse the repository at this point in the history
refactor(air/layout): move compute_program_hash
  • Loading branch information
Okm165 authored Jan 21, 2025
2 parents 7b1b656 + 19f5e9e commit 29c780d
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 283 deletions.
16 changes: 3 additions & 13 deletions crates/air/src/layout/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub mod global_values;
use crate::{
consts::*,
felt_hex,
layout::stark_curve,
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},
};
use alloc::vec;
use alloc::vec::Vec;
use global_values::{EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{pedersen_hash, Felt};
use starknet_crypto::Felt;
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -394,17 +394,7 @@ impl LayoutTrait for Layout {

ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - FELT_2;

let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into()?)
.step_by(2)
.take((program_end_pc - FELT_1).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()));
let program_hash = compute_program_hash(memory, initial_pc, initial_fp)?;

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output =
Expand Down
16 changes: 3 additions & 13 deletions crates/air/src/layout/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
consts::*,
diluted::get_diluted_product,
felt, felt_hex, felt_nonzero, felt_try_nonzero,
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 All @@ -21,7 +21,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{CurveConfig, EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{pedersen_hash, Felt};
use starknet_crypto::Felt;
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -767,17 +767,7 @@ impl LayoutTrait for Layout {

ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - FELT_2;

let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into()?)
.step_by(2)
.take((program_end_pc - FELT_1).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!(program.len()));
let program_hash = compute_program_hash(memory, initial_pc, initial_fp)?;

let output_len: usize = (output_stop - output_start).to_bigint().try_into()?;
let output =
Expand Down
198 changes: 198 additions & 0 deletions crates/air/src/layout/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
use num_bigint::{BigInt, TryFromBigIntError};
use starknet_types_core::felt::FeltIsZeroError;

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

#[cfg(feature = "std")]
#[derive(Error, Debug)]
pub enum CompositionPolyEvalError {
#[error("segment not present {segment}")]
SegmentMissing { segment: usize },

#[error("value out of range")]
ValueOutOfRange,

#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),
}

#[cfg(feature = "std")]
#[derive(Error, Debug)]
pub enum OodsPolyEvalError {
#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),
}

#[cfg(feature = "std")]
#[derive(Error, Debug)]
pub enum PublicInputError {
#[error("max steps exceeded")]
MaxSteps,

#[error("trace length invalid")]
TraceLengthInvalid,

#[error("segment not present {segment}")]
SegmentMissing { segment: usize },

#[error("layout code invalid")]
LayoutCodeInvalid,

#[error("range_check invalid")]
RangeCheckInvalid,

#[error("invalid number of builtin uses")]
UsesInvalid,

#[error("invalid number of builtin copies")]
CopiesInvalid,

#[error("invalid number of segments")]
InvalidSegments,

#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("BigInt conversion Error")]
TryFromBigInt(#[from] TryFromBigIntError<BigInt>),

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),

#[error("dynamic params check failed")]
CheckAsserts(#[from] CheckAssertsError),
}

#[cfg(feature = "std")]
#[derive(Error, Debug)]
pub enum CheckAssertsError {
#[error("value is not power of two")]
NotPowerOfTwo,

#[error("value out of range")]
OutOfRange,

#[error("value not boolean")]
NotBoolean,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),

#[error("field multiplication fail")]
SafeMult(#[from] SafeMultError),

#[error("segment not present {segment}")]
SegmentMissing { segment: usize },
}

#[cfg(feature = "std")]
#[derive(Error, Debug)]
pub enum SafeMultError {
#[error("value multiplication overflowed actual {actual}, expected {expected}")]
Overflow { actual: BigInt, expected: BigInt },
}

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

#[cfg(not(feature = "std"))]
#[derive(Error, Debug)]
pub enum CompositionPolyEvalError {
#[error("segment not present {segment}")]
SegmentMissing { segment: usize },

#[error("value out of range")]
ValueOutOfRange,

#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),
}

#[cfg(not(feature = "std"))]
#[derive(Error, Debug)]
pub enum OodsPolyEvalError {
#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),
}

#[cfg(not(feature = "std"))]
#[derive(Error, Debug)]
pub enum PublicInputError {
#[error("max steps exceeded")]
MaxSteps,

#[error("trace length invalid")]
TraceLengthInvalid,

#[error("segment not present {segment}")]
SegmentMissing { segment: usize },

#[error("layout code invalid")]
LayoutCodeInvalid,

#[error("range_check invalid")]
RangeCheckInvalid,

#[error("invalid number of builtin uses")]
UsesInvalid,

#[error("invalid number of builtin copies")]
CopiesInvalid,

#[error("invalid number of segments")]
InvalidSegments,

#[error("dynamic params missing")]
DynamicParamsMissing,

#[error("BigInt conversion Error")]
TryFromBigInt(#[from] TryFromBigIntError<BigInt>),

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),

#[error("dynamic params check failed")]
CheckAsserts(#[from] CheckAssertsError),
}

#[cfg(not(feature = "std"))]
#[derive(Error, Debug)]
pub enum CheckAssertsError {
#[error("value is not power of two")]
NotPowerOfTwo,

#[error("value out of range")]
OutOfRange,

#[error("value not boolean")]
NotBoolean,

#[error("field element is zero")]
FeltIsZero(#[from] FeltIsZeroError),

#[error("field multiplication fail")]
SafeMult(#[from] SafeMultError),

#[error("segment not present {segment}")]
SegmentMissing { segment: usize },
}

#[cfg(not(feature = "std"))]
#[derive(Error, Debug)]
pub enum SafeMultError {
#[error("value multiplication overflowed actual {actual}, expected {expected}")]
Overflow { actual: BigInt, expected: BigInt },
}
Loading

0 comments on commit 29c780d

Please sign in to comment.