Skip to content

Commit

Permalink
feat: remove constraint degree
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Kindi-0 committed Sep 5, 2024
1 parent 536a5be commit d594b4e
Show file tree
Hide file tree
Showing 27 changed files with 92 additions and 308 deletions.
81 changes: 19 additions & 62 deletions air/src/air/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use alloc::vec::Vec;
use core::cmp;

use math::StarkField;

use crate::{air::TransitionConstraintDegree, ProofOptions, TraceInfo};
use crate::{ProofOptions, TraceInfo};

// AIR CONTEXT
// ================================================================================================
Expand All @@ -22,7 +21,7 @@ pub struct AirContext<B: StarkField> {
pub(super) num_main_assertions: usize,
pub(super) num_aux_assertions: usize,
pub(super) lagrange_kernel_aux_column_idx: Option<usize>,
pub(super) ce_blowup_factor: usize,
pub(super) max_degree: usize,
pub(super) trace_domain_generator: B,
pub(super) lde_domain_generator: B,
pub(super) num_transition_exemptions: usize,
Expand All @@ -34,23 +33,17 @@ impl<B: StarkField> AirContext<B> {
/// Returns a new instance of [AirContext] instantiated for computations which require a single
/// execution trace segment.
///
/// The list of transition constraint degrees defines the total number of transition
/// constraints and their expected degrees. Constraint evaluations computed by
/// [Air::evaluate_transition()](crate::Air::evaluate_transition) function are expected to be
/// in the order defined by this list.
///
/// # Panics
/// Panics if
/// * `transition_constraint_degrees` is an empty vector.
/// * `num_main_transition_constraints` is zero.
/// * `num_assertions` is zero.
/// * Blowup factor specified by the provided `options` is too small to accommodate degrees
/// of the specified transition constraints.
/// * `trace_info` describes a multi-segment execution trace.
pub fn new(
trace_info: TraceInfo,
transition_constraint_degrees: Vec<TransitionConstraintDegree>,
max_degree: usize,
num_main_transition_constraints: usize,
num_aux_transition_constraints: usize,
num_assertions: usize,
options: ProofOptions,
) -> Self {
Expand All @@ -60,10 +53,9 @@ impl<B: StarkField> AirContext<B> {
);
Self::new_multi_segment(
trace_info,
transition_constraint_degrees,
Vec::new(),
max_degree,
num_main_transition_constraints,
num_aux_transition_constraints,
0,
num_assertions,
0,
None,
Expand All @@ -74,29 +66,20 @@ impl<B: StarkField> AirContext<B> {
/// Returns a new instance of [AirContext] instantiated for computations which require multiple
/// execution trace segments.
///
/// The lists of transition constraint degrees defines the total number of transition
/// constraints and their expected degrees. Constraint evaluations computed by
/// [Air::evaluate_transition()](crate::Air::evaluate_transition) function are expected to be
/// in the order defined by `main_transition_constraint_degrees` list. Constraint evaluations
/// computed by [Air::evaluate_aux_transition()](crate::Air::evaluate_aux_transition) function
/// are expected to be in the order defined by `aux_transition_constraint_degrees` list.
///
/// # Panics
/// Panics if
/// * `main_transition_constraint_degrees` is an empty vector.
/// * `num_main_assertions` is zero.
/// * `trace_info.is_multi_segment() == true` but:
/// - `aux_transition_constraint_degrees` is an empty vector.
/// - `num_aux_transition_constraints` is zero.
/// - `num_aux_assertions` is zero.
/// * `trace_info.is_multi_segment() == false` but:
/// - `aux_transition_constraint_degrees` is a non-empty vector.
/// - `num_aux_transition_constraints` is non-zero.
/// - `num_aux_assertions` is greater than zero.
/// * Blowup factor specified by the provided `options` is too small to accommodate degrees
/// of the specified transition constraints.
/// * Blowup factor specified by the provided `options` is too small to accommodate max degree
/// of the transition constraints.
pub fn new_multi_segment(
trace_info: TraceInfo,
main_transition_constraint_degrees: Vec<TransitionConstraintDegree>,
aux_transition_constraint_degrees: Vec<TransitionConstraintDegree>,
max_degree: usize,
num_main_transition_constraints: usize,
num_aux_transition_constraints: usize,
num_main_assertions: usize,
Expand All @@ -105,14 +88,14 @@ impl<B: StarkField> AirContext<B> {
options: ProofOptions,
) -> Self {
assert!(
!main_transition_constraint_degrees.is_empty(),
num_main_transition_constraints > 0,
"at least one transition constraint degree must be specified"
);
assert!(num_main_assertions > 0, "at least one assertion must be specified");

if trace_info.is_multi_segment() {
assert!(
!aux_transition_constraint_degrees.is_empty(),
num_aux_transition_constraints > 0,
"at least one transition constraint degree must be specified for the auxiliary trace segment"
);
assert!(
Expand All @@ -121,7 +104,7 @@ impl<B: StarkField> AirContext<B> {
);
} else {
assert!(
aux_transition_constraint_degrees.is_empty(),
num_aux_transition_constraints == 0,
"auxiliary transition constraint degrees specified for a single-segment trace"
);
assert!(
Expand All @@ -139,25 +122,10 @@ impl<B: StarkField> AirContext<B> {
);
}

// determine minimum blowup factor needed to evaluate transition constraints by taking
// the blowup factor of the highest degree constraint
let mut ce_blowup_factor = 0;
for degree in main_transition_constraint_degrees.iter() {
if degree.min_blowup_factor() > ce_blowup_factor {
ce_blowup_factor = degree.min_blowup_factor();
}
}

for degree in aux_transition_constraint_degrees.iter() {
if degree.min_blowup_factor() > ce_blowup_factor {
ce_blowup_factor = degree.min_blowup_factor();
}
}

assert!(
options.blowup_factor() >= ce_blowup_factor,
options.blowup_factor() >= max_degree,
"blowup factor too small; expected at least {}, but was {}",
ce_blowup_factor,
max_degree,
options.blowup_factor()
);

Expand All @@ -170,7 +138,7 @@ impl<B: StarkField> AirContext<B> {
num_main_assertions,
num_aux_assertions,
lagrange_kernel_aux_column_idx,
ce_blowup_factor,
max_degree,
trace_domain_generator: B::get_root_of_unity(trace_length.ilog2()),
lde_domain_generator: B::get_root_of_unity(lde_domain_size.ilog2()),
num_transition_exemptions: 1,
Expand Down Expand Up @@ -205,7 +173,7 @@ impl<B: StarkField> AirContext<B> {
///
/// This is guaranteed to be a power of two, and is equal to `trace_length * ce_blowup_factor`.
pub fn ce_domain_size(&self) -> usize {
self.trace_info.length() * self.ce_blowup_factor
self.trace_info.length() * self.num_constraint_composition_columns()
}

/// Returns the size of the low-degree extension domain.
Expand Down Expand Up @@ -288,18 +256,7 @@ impl<B: StarkField> AirContext<B> {
/// Hence, no matter what the degree of the divisor is for each, the degree of the fraction will
/// be at most `trace_len - 1`.
pub fn num_constraint_composition_columns(&self) -> usize {
let mut highest_constraint_degree = 0_usize;

let trace_length = self.trace_len();
let transition_divisior_degree = trace_length - self.num_transition_exemptions();

//// we use the identity: ceil(a/b) = (a + b - 1)/b
//let num_constraint_col =
//(highest_constraint_degree - transition_divisior_degree + trace_length - 1)
/// trace_length;

let num_constraint_col = 1;
cmp::max(num_constraint_col, 1)
cmp::max(self.max_degree, 1)
}

// DATA MUTATORS
Expand Down
6 changes: 3 additions & 3 deletions air/src/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ pub trait Air: Send + Sync {
///
/// `ce_blowup_factor` is guaranteed to be smaller than or equal to the `lde_blowup_factor`.
fn ce_blowup_factor(&self) -> usize {
self.context().ce_blowup_factor
self.context().max_degree
}

/// Returns size of the constraint evaluation domain.
Expand Down Expand Up @@ -536,7 +536,7 @@ pub trait Air: Send + Sync {
R: RandomCoin<BaseField = Self::BaseField>,
{
let mut t_coefficients = Vec::new();
for _ in 0..10 {
for _ in 0..self.context().num_transition_constraints() {
t_coefficients.push(public_coin.draw()?);
}

Expand Down Expand Up @@ -584,7 +584,7 @@ pub trait Air: Send + Sync {
}

let mut c_coefficients = Vec::new();
for _ in 0..self.context().num_constraint_composition_columns() {
for _ in 0..self.ce_blowup_factor() {
c_coefficients.push(public_coin.draw()?);
}

Expand Down
8 changes: 2 additions & 6 deletions air/src/air/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use alloc::{collections::BTreeMap, vec::Vec};
use crypto::{hashers::Blake3_256, DefaultRandomCoin, RandomCoin};
use math::{fields::f64::BaseElement, get_power_series, polynom, FieldElement, StarkField};

use super::{
Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo,
TransitionConstraintDegree,
};
use super::{Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo};
use crate::FieldExtension;

// PERIODIC COLUMNS
Expand Down Expand Up @@ -268,9 +265,8 @@ pub fn build_context<B: StarkField>(
num_assertions: usize,
) -> AirContext<B> {
let options = ProofOptions::new(32, 8, 0, FieldExtension::None, 4, 31);
let t_degrees = vec![TransitionConstraintDegree::new(2)];
let trace_info = TraceInfo::new(trace_width, trace_length);
AirContext::new(trace_info, t_degrees, 1, 0, num_assertions, options)
AirContext::new(trace_info, 1, 1, num_assertions, options)
}

pub fn build_prng() -> DefaultRandomCoin<Blake3_256<BaseElement>> {
Expand Down
1 change: 0 additions & 1 deletion air/src/air/transition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl<E: FieldElement> TransitionConstraints<E> {
/// Panics if the number of transition constraints in the context does not match the number of
/// provided composition coefficients.
pub fn new(context: &AirContext<E::BaseField>, composition_coefficients: &[E]) -> Self {

// build constraint divisor; the same divisor applies to all transition constraints
let divisor = ConstraintDivisor::from_transition(
context.trace_len(),
Expand Down
7 changes: 2 additions & 5 deletions examples/src/fibonacci/fib2/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use winterfell::{
Air, AirContext, Assertion, EvaluationFrame, TraceInfo, TransitionConstraintDegree,
};
use winterfell::{Air, AirContext, Assertion, EvaluationFrame, TraceInfo};

use super::{BaseElement, FieldElement, ProofOptions, TRACE_WIDTH};
use crate::utils::are_equal;
Expand All @@ -27,10 +25,9 @@ impl Air for FibAir {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
fn new(trace_info: TraceInfo, pub_inputs: Self::BaseField, options: ProofOptions) -> Self {
let degrees = vec![TransitionConstraintDegree::new(1), TransitionConstraintDegree::new(1)];
assert_eq!(TRACE_WIDTH, trace_info.width());
FibAir {
context: AirContext::new(trace_info, degrees, 2, 0, 3, options),
context: AirContext::new(trace_info, 2, 2, 3, options),
result: pub_inputs,
}
}
Expand Down
8 changes: 2 additions & 6 deletions examples/src/fibonacci/fib8/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use winterfell::{
Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo,
TransitionConstraintDegree,
};
use winterfell::{Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo};

use super::{BaseElement, FieldElement, TRACE_WIDTH};
use crate::utils::are_equal;
Expand All @@ -28,10 +25,9 @@ impl Air for Fib8Air {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
fn new(trace_info: TraceInfo, pub_inputs: Self::BaseField, options: ProofOptions) -> Self {
let degrees = vec![TransitionConstraintDegree::new(1), TransitionConstraintDegree::new(1)];
assert_eq!(TRACE_WIDTH, trace_info.width());
Fib8Air {
context: AirContext::new(trace_info, degrees, 2, 0, 3, options),
context: AirContext::new(trace_info, 2, 2, 3, options),
result: pub_inputs,
}
}
Expand Down
7 changes: 2 additions & 5 deletions examples/src/fibonacci/fib_small/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use winterfell::{
Air, AirContext, Assertion, EvaluationFrame, TraceInfo, TransitionConstraintDegree,
};
use winterfell::{Air, AirContext, Assertion, EvaluationFrame, TraceInfo};

use super::{BaseElement, FieldElement, ProofOptions, TRACE_WIDTH};
use crate::utils::are_equal;
Expand All @@ -27,10 +25,9 @@ impl Air for FibSmall {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
fn new(trace_info: TraceInfo, pub_inputs: Self::BaseField, options: ProofOptions) -> Self {
let degrees = vec![TransitionConstraintDegree::new(1), TransitionConstraintDegree::new(1)];
assert_eq!(TRACE_WIDTH, trace_info.width());
FibSmall {
context: AirContext::new(trace_info, degrees, 2, 0, 3, options),
context: AirContext::new(trace_info, 2, 2, 3, options),
result: pub_inputs,
}
}
Expand Down
4 changes: 1 addition & 3 deletions examples/src/fibonacci/mulfib2/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use winterfell::{
math::{fields::f128::BaseElement, FieldElement},
Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo,
TransitionConstraintDegree,
};

use super::TRACE_WIDTH;
Expand All @@ -29,10 +28,9 @@ impl Air for MulFib2Air {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
fn new(trace_info: TraceInfo, pub_inputs: Self::BaseField, options: ProofOptions) -> Self {
let degrees = vec![TransitionConstraintDegree::new(2), TransitionConstraintDegree::new(2)];
assert_eq!(TRACE_WIDTH, trace_info.width());
MulFib2Air {
context: AirContext::new(trace_info, degrees, 2, 0, 3, options),
context: AirContext::new(trace_info, 2, 2, 3, options),
result: pub_inputs,
}
}
Expand Down
13 changes: 1 addition & 12 deletions examples/src/fibonacci/mulfib8/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use winterfell::{
math::{fields::f128::BaseElement, FieldElement},
Air, AirContext, Assertion, EvaluationFrame, ProofOptions, TraceInfo,
TransitionConstraintDegree,
};

use super::TRACE_WIDTH;
Expand All @@ -29,19 +28,9 @@ impl Air for MulFib8Air {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
fn new(trace_info: TraceInfo, pub_inputs: Self::BaseField, options: ProofOptions) -> Self {
let degrees = vec![
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
TransitionConstraintDegree::new(2),
];
assert_eq!(TRACE_WIDTH, trace_info.width());
MulFib8Air {
context: AirContext::new(trace_info, degrees, 8, 0, 3, options),
context: AirContext::new(trace_info, 2, 8, 3, options),
result: pub_inputs,
}
}
Expand Down
Loading

0 comments on commit d594b4e

Please sign in to comment.