Skip to content

Commit

Permalink
static + dynamic layout project structuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Sep 1, 2024
1 parent 55a0670 commit 73a93bb
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 22 deletions.
1 change: 1 addition & 0 deletions crates/air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ recursive_with_poseidon = []
small = []
starknet = []
starknet_with_keccak = []
dynamic = []
keccak_160_lsb = ["swiftness_commitment/keccak_160_lsb"]
keccak_248_lsb = ["swiftness_commitment/keccak_248_lsb"]
blake2s_160_lsb = ["swiftness_commitment/blake2s_160_lsb"]
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
@@ -1,9 +1,8 @@
pub mod autogenerated;
pub mod global_values;

use std::borrow::ToOwned;

use crate::{
alloc::borrow::ToOwned,
diluted::get_diluted_product,
layout::stark_curve,
periodic_columns::{
Expand Down
8 changes: 8 additions & 0 deletions crates/air/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub trait StaticLayoutTrait {
const NUM_COLUMNS_SECOND: usize;
}

pub trait DynamicLayoutTrait {}

pub fn safe_div(value: Felt, divisor: Felt) -> Result<Felt, FeltIsZeroError> {
Ok(value.floor_div(&NonZeroFelt::try_from(divisor)?))
}
Expand Down Expand Up @@ -148,6 +150,12 @@ 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,

Expand Down
9 changes: 5 additions & 4 deletions crates/air/src/trace/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::layout::StaticLayoutTrait;
use serde::{Deserialize, Serialize};
use starknet_crypto::Felt;
use swiftness_commitment::vector;
Expand All @@ -13,10 +12,12 @@ pub struct Config {
}

impl Config {
pub fn validate<Layout: StaticLayoutTrait>(
pub fn validate(
&self,
log_eval_domain_size: Felt,
n_verifier_friendly_commitment_layers: Felt,
n_columns_original: Felt,
n_columns_interaction: Felt,
) -> Result<(), Error> {
if self.original.n_columns < Felt::ONE || self.original.n_columns > MAX_N_COLUMNS {
return Err(Error::OutOfBounds { min: Felt::ONE, max: MAX_N_COLUMNS });
Expand All @@ -25,11 +26,11 @@ impl Config {
return Err(Error::OutOfBounds { min: Felt::ONE, max: MAX_N_COLUMNS });
}

if self.original.n_columns != Layout::NUM_COLUMNS_FIRST.into() {
if self.original.n_columns != n_columns_original {
return Err(Error::ColumnsNumInvalid);
}

if self.interaction.n_columns != Layout::NUM_COLUMNS_SECOND.into() {
if self.interaction.n_columns != n_columns_interaction {
return Err(Error::ColumnsNumInvalid);
}

Expand Down
5 changes: 2 additions & 3 deletions crates/stark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ recursive_with_poseidon = [
]
small = ["swiftness_air/small"]
starknet = ["swiftness_air/starknet"]
starknet_with_keccak = [
"swiftness_air/starknet_with_keccak",
]
starknet_with_keccak = ["swiftness_air/starknet_with_keccak"]
dynamic = ["swiftness_air/dynamic"]
keccak_160_lsb = [
"swiftness_pow/keccak",
"swiftness_commitment/keccak_160_lsb"
Expand Down
64 changes: 57 additions & 7 deletions crates/stark/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::alloc::borrow::ToOwned;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use starknet_crypto::Felt;
use swiftness_air::{
layout::{DynamicLayoutTrait, StaticLayoutTrait},
public_memory::PublicInput,
};
use swiftness_commitment::vector;

#[serde_as]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -41,15 +47,22 @@ impl StarkConfig {
self.n_queries * self.log_n_cosets + Felt::from(self.proof_of_work.n_bits)
}

pub fn validate<Layout: StaticLayoutTrait>(&self, security_bits: Felt) -> Result<(), Error> {
pub fn validate_static_layout<Layout: StaticLayoutTrait>(
&self,
security_bits: Felt,
) -> Result<(), Error> {
self.proof_of_work.validate()?;

assert!(security_bits <= self.security_bits());
ensure!(security_bits <= self.security_bits(), Error::InsufficientSecurity);

// Validate traces config.
let log_eval_domain_size = self.log_trace_domain_size + self.log_n_cosets;
self.traces
.validate::<Layout>(log_eval_domain_size, self.n_verifier_friendly_commitment_layers)?;
self.traces.validate(
log_eval_domain_size,
self.n_verifier_friendly_commitment_layers,
Layout::NUM_COLUMNS_FIRST.into(),
Layout::NUM_COLUMNS_SECOND.into(),
)?;

// Validate composition config.
self.composition
Expand All @@ -60,11 +73,40 @@ impl StarkConfig {
self.fri.validate(self.log_n_cosets, self.n_verifier_friendly_commitment_layers)?;
Ok(())
}
}

use swiftness_air::layout::StaticLayoutTrait;
use swiftness_commitment::vector;
pub fn validate_dynamic_layout<Layout: DynamicLayoutTrait>(
&self,
security_bits: Felt,
public_input: &PublicInput,
) -> Result<(), Error> {
self.proof_of_work.validate()?;

ensure!(security_bits <= self.security_bits(), Error::InsufficientSecurity);

let dynamc_params =
public_input.dynamic_params.to_owned().ok_or(Error::DynamicParamsMissing)?;

// Validate traces config.
let log_eval_domain_size = self.log_trace_domain_size + self.log_n_cosets;
self.traces.validate(
log_eval_domain_size,
self.n_verifier_friendly_commitment_layers,
dynamc_params.num_columns_first.into(),
dynamc_params.num_columns_second.into(),
)?;

// Validate composition config.
self.composition
.vector
.validate(log_eval_domain_size, self.n_verifier_friendly_commitment_layers)?;

// Validate Fri config.
self.fri.validate(self.log_n_cosets, self.n_verifier_friendly_commitment_layers)?;
Ok(())
}
}

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

Expand All @@ -79,6 +121,10 @@ pub enum Error {
Pow(#[from] swiftness_pow::config::Error),
#[error("Trace Error")]
Trace(#[from] swiftness_air::trace::config::Error),
#[error("dynamic params missing")]
DynamicParamsMissing,
#[error("insufficient number ofsecurity bits")]
InsufficientSecurity,
}

#[cfg(not(feature = "std"))]
Expand All @@ -95,4 +141,8 @@ pub enum Error {
Pow(#[from] swiftness_pow::config::Error),
#[error("Trace Error")]
Trace(#[from] swiftness_air::trace::config::Error),
#[error("dynamic params missing")]
DynamicParamsMissing,
#[error("insufficient number ofsecurity bits")]
InsufficientSecurity,
}
13 changes: 12 additions & 1 deletion crates/stark/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ impl StarkProof {
&self,
security_bits: Felt,
) -> Result<(Felt, Felt), Error> {
self.config.validate::<Layout>(security_bits)?;
#[cfg(any(
feature = "dex",
feature = "recursive",
feature = "recursive_with_poseidon",
feature = "small",
feature = "starknet",
feature = "starknet_with_keccak"
))]
self.config.validate_static_layout::<Layout>(security_bits)?;

#[cfg(feature = "dynamic")]
self.config.validate_dynamic_layout::<Layout>(security_bits, &self.public_input)?;

// Validate the public input.
let stark_domains =
Expand Down
7 changes: 2 additions & 5 deletions crates/stark/src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use alloc::borrow::ToOwned;
use starknet_crypto::Felt;
use swiftness_air::{
domains::StarkDomains,
layout::{LayoutTrait, StaticLayoutTrait}, public_memory::PublicInput,
};
use swiftness_air::{domains::StarkDomains, layout::LayoutTrait, public_memory::PublicInput};
use swiftness_commitment::table::decommit::table_decommit;
use swiftness_fri::{
fri::{self, fri_verify},
Expand All @@ -17,7 +14,7 @@ use crate::{
};

// STARK verify phase.
pub fn stark_verify<Layout: LayoutTrait + StaticLayoutTrait>(
pub fn stark_verify<Layout: LayoutTrait>(
n_original_columns: usize,
n_interaction_columns: usize,
public_input: &PublicInput,
Expand Down

0 comments on commit 73a93bb

Please sign in to comment.