From abd15be830581ec645579e69105b2ac4f2472e69 Mon Sep 17 00:00:00 2001 From: Jan Ferdinand Sauer Date: Mon, 11 Mar 2024 12:56:42 +0100 Subject: [PATCH] refactor: public `NUM_*_CONSTRAINTS` constants --- triton-vm/src/stark.rs | 4 +- triton-vm/src/table/constraints.rs | 103 ++++++++- triton-vm/src/table/extension_table.rs | 226 +++++++++----------- triton-vm/src/table/master_table.rs | 8 +- triton-vm/src/table/tasm_air_constraints.rs | 2 +- 5 files changed, 206 insertions(+), 137 deletions(-) diff --git a/triton-vm/src/stark.rs b/triton-vm/src/stark.rs index 8f9b0dad..ba8abf37 100644 --- a/triton-vm/src/stark.rs +++ b/triton-vm/src/stark.rs @@ -1108,6 +1108,7 @@ pub(crate) mod tests { use crate::table::cross_table_argument::CrossTableArg; use crate::table::cross_table_argument::EvalArg; use crate::table::cross_table_argument::GrandCrossTableArg; + use crate::table::extension_table; use crate::table::extension_table::Evaluable; use crate::table::extension_table::Quotientable; use crate::table::hash_table; @@ -1116,7 +1117,6 @@ pub(crate) mod tests { use crate::table::jump_stack_table::ExtJumpStackTable; use crate::table::lookup_table; use crate::table::lookup_table::ExtLookupTable; - use crate::table::master_table::all_degrees_with_origin; use crate::table::master_table::MasterExtTable; use crate::table::master_table::TableId::LookupTable; use crate::table::master_table::TableId::ProcessorTable; @@ -1377,7 +1377,7 @@ pub(crate) mod tests { let padded_height = 2; let num_trace_randomizers = 2; let interpolant_degree = interpolant_degree(padded_height, num_trace_randomizers); - for deg in all_degrees_with_origin(interpolant_degree, padded_height) { + for deg in extension_table::all_degrees_with_origin(interpolant_degree, padded_height) { println!("{deg}"); } } diff --git a/triton-vm/src/table/constraints.rs b/triton-vm/src/table/constraints.rs index 01e165ee..23c6fa4c 100644 --- a/triton-vm/src/table/constraints.rs +++ b/triton-vm/src/table/constraints.rs @@ -2,20 +2,119 @@ //! Run `cargo run --bin constraint-evaluation-generator` //! to fill in this file with optimized constraints. +use ndarray::ArrayView1; use twenty_first::prelude::BFieldElement; use twenty_first::prelude::XFieldElement; +use twenty_first::shared_math::mpolynomial::Degree; +use crate::table::challenges::Challenges; use crate::table::extension_table::Evaluable; use crate::table::extension_table::Quotientable; use crate::table::master_table::MasterExtTable; -impl Evaluable for MasterExtTable {} +pub(crate) const ERROR_MESSAGE_GENERATE_CONSTRAINTS: &str = + "Constraints must be in place. Run: `cargo run --bin constraint-evaluation-generator`"; +const ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS: &str = + "Degree bounds must be in place. Run: `cargo run --bin constraint-evaluation-generator`"; -impl Evaluable for MasterExtTable {} +impl Evaluable for MasterExtTable { + fn evaluate_initial_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_consistency_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_transition_constraints( + _: ArrayView1, + _: ArrayView1, + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_terminal_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } +} + +impl Evaluable for MasterExtTable { + fn evaluate_initial_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_consistency_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_transition_constraints( + _: ArrayView1, + _: ArrayView1, + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } + + fn evaluate_terminal_constraints( + _: ArrayView1, + _: ArrayView1, + _: &Challenges, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") + } +} impl Quotientable for MasterExtTable { const NUM_INITIAL_CONSTRAINTS: usize = 0; const NUM_CONSISTENCY_CONSTRAINTS: usize = 0; const NUM_TRANSITION_CONSTRAINTS: usize = 0; const NUM_TERMINAL_CONSTRAINTS: usize = 0; + + + fn initial_quotient_degree_bounds(_: Degree) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") + } + + fn consistency_quotient_degree_bounds( + _: Degree, + _: usize, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") + } + + fn transition_quotient_degree_bounds( + _: Degree, + _: usize, + ) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") + } + + fn terminal_quotient_degree_bounds(_: Degree) -> Vec { + panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") + } } diff --git a/triton-vm/src/table/extension_table.rs b/triton-vm/src/table/extension_table.rs index 6da34d08..d8cb25ab 100644 --- a/triton-vm/src/table/extension_table.rs +++ b/triton-vm/src/table/extension_table.rs @@ -16,57 +16,45 @@ use twenty_first::shared_math::traits::FiniteField; use crate::arithmetic_domain::ArithmeticDomain; use crate::table::challenges::Challenges; - -pub(crate) const ERROR_MESSAGE_GENERATE_CONSTRAINTS: &str = - "Constraints must be in place. Run: `cargo run --bin constraint-evaluation-generator`"; -const ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS: &str = - "Degree bounds must be in place. Run: `cargo run --bin constraint-evaluation-generator`"; +use crate::table::master_table::MasterExtTable; pub trait Evaluable { /// The code for this method must be generated by running /// `cargo run --bin constraint-evaluation-generator` fn evaluate_initial_constraints( - _base_row: ArrayView1, - _ext_row: ArrayView1, - _challenges: &Challenges, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") - } + base_row: ArrayView1, + ext_row: ArrayView1, + challenges: &Challenges, + ) -> Vec; /// The code for this method must be generated by running /// `cargo run --bin constraint-evaluation-generator` fn evaluate_consistency_constraints( - _base_row: ArrayView1, - _ext_row: ArrayView1, - _challenges: &Challenges, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") - } + base_row: ArrayView1, + ext_row: ArrayView1, + challenges: &Challenges, + ) -> Vec; /// The code for this method must be generated by running /// `cargo run --bin constraint-evaluation-generator` fn evaluate_transition_constraints( - _current_base_row: ArrayView1, - _current_ext_row: ArrayView1, - _next_base_row: ArrayView1, - _next_ext_row: ArrayView1, - _challenges: &Challenges, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") - } + current_base_row: ArrayView1, + current_ext_row: ArrayView1, + next_base_row: ArrayView1, + next_ext_row: ArrayView1, + challenges: &Challenges, + ) -> Vec; /// The code for this method must be generated by running /// `cargo run --bin constraint-evaluation-generator` fn evaluate_terminal_constraints( - _base_row: ArrayView1, - _ext_row: ArrayView1, - _challenges: &Challenges, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_CONSTRAINTS}") - } + base_row: ArrayView1, + ext_row: ArrayView1, + challenges: &Challenges, + ) -> Vec; } -pub(crate) trait Quotientable: Evaluable { +pub trait Quotientable: Evaluable { const NUM_INITIAL_CONSTRAINTS: usize; const NUM_CONSISTENCY_CONSTRAINTS: usize; const NUM_TRANSITION_CONSTRAINTS: usize; @@ -78,80 +66,6 @@ pub(crate) trait Quotientable: Evaluable { + Self::NUM_TRANSITION_CONSTRAINTS + Self::NUM_TERMINAL_CONSTRAINTS; - /// Compute the degrees of the quotients from all AIR constraints that apply to the table. - fn all_degrees_with_origin( - table_name: &str, - interpolant_degree: Degree, - padded_height: usize, - ) -> Vec { - let initial_degrees_with_origin = Self::initial_quotient_degree_bounds(interpolant_degree) - .into_iter() - .enumerate() - .map(|(origin_index, degree)| DegreeWithOrigin { - degree, - interpolant_degree, - zerofier_degree: 1, - origin_table_name: table_name.to_owned(), - origin_index, - origin_table_height: padded_height, - origin_constraint_type: ConstraintType::Initial, - }) - .collect_vec(); - - let consistency_degrees_with_origin = - Self::consistency_quotient_degree_bounds(interpolant_degree, padded_height) - .into_iter() - .enumerate() - .map(|(origin_index, degree)| DegreeWithOrigin { - degree, - interpolant_degree, - zerofier_degree: padded_height as Degree, - origin_table_name: table_name.to_owned(), - origin_index, - origin_table_height: padded_height, - origin_constraint_type: ConstraintType::Consistency, - }) - .collect(); - - let transition_degrees_with_origin = - Self::transition_quotient_degree_bounds(interpolant_degree, padded_height) - .into_iter() - .enumerate() - .map(|(origin_index, degree)| DegreeWithOrigin { - degree, - interpolant_degree, - zerofier_degree: padded_height as Degree - 1, - origin_table_name: table_name.to_owned(), - origin_index, - origin_table_height: padded_height, - origin_constraint_type: ConstraintType::Transition, - }) - .collect(); - - let terminal_degrees_with_origin = - Self::terminal_quotient_degree_bounds(interpolant_degree) - .into_iter() - .enumerate() - .map(|(origin_index, degree)| DegreeWithOrigin { - degree, - interpolant_degree, - zerofier_degree: 1, - origin_table_name: table_name.to_owned(), - origin_index, - origin_table_height: padded_height, - origin_constraint_type: ConstraintType::Terminal, - }) - .collect(); - - [ - initial_degrees_with_origin, - consistency_degrees_with_origin, - transition_degrees_with_origin, - terminal_degrees_with_origin, - ] - .concat() - } - fn fill_initial_quotients( master_base_table: ArrayView2, master_ext_table: ArrayView2, @@ -271,27 +185,19 @@ pub(crate) trait Quotientable: Evaluable { }); } - fn initial_quotient_degree_bounds(_interpolant_degree: Degree) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") - } + fn initial_quotient_degree_bounds(interpolant_degree: Degree) -> Vec; fn consistency_quotient_degree_bounds( - _interpolant_degree: Degree, - _padded_height: usize, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") - } + interpolant_degree: Degree, + padded_height: usize, + ) -> Vec; fn transition_quotient_degree_bounds( - _interpolant_degree: Degree, - _padded_height: usize, - ) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") - } + interpolant_degree: Degree, + padded_height: usize, + ) -> Vec; - fn terminal_quotient_degree_bounds(_interpolant_degree: Degree) -> Vec { - panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}") - } + fn terminal_quotient_degree_bounds(interpolant_degree: Degree) -> Vec; } /// The type of constraint. Can be used to determine the degree bounds for the quotient @@ -323,7 +229,6 @@ pub(crate) struct DegreeWithOrigin { pub degree: Degree, pub interpolant_degree: Degree, pub zerofier_degree: Degree, - pub origin_table_name: String, pub origin_index: usize, pub origin_table_height: usize, pub origin_constraint_type: ConstraintType, @@ -334,10 +239,81 @@ impl Display for DegreeWithOrigin { assert!(self.degree > 0); let zerofier_corrected_degree = self.degree + self.zerofier_degree; let degree = zerofier_corrected_degree / self.interpolant_degree; + let idx = self.origin_index; + let constraint_type = self.origin_constraint_type; write!( f, - "Degree of poly for table {} (index {:02}) of type “{}” is {}.", - self.origin_table_name, self.origin_index, self.origin_constraint_type, degree, + "Degree of polynomial {idx:02} of type “{constraint_type}” is {degree}." ) } } + +/// Compute the degrees of the quotients from all AIR constraints that apply to the table. +pub(crate) fn all_degrees_with_origin( + interpolant_degree: Degree, + padded_height: usize, +) -> Vec { + let initial_degrees_with_origin = + MasterExtTable::initial_quotient_degree_bounds(interpolant_degree) + .into_iter() + .enumerate() + .map(|(origin_index, degree)| DegreeWithOrigin { + degree, + interpolant_degree, + zerofier_degree: 1, + origin_index, + origin_table_height: padded_height, + origin_constraint_type: ConstraintType::Initial, + }) + .collect_vec(); + + let consistency_degrees_with_origin = + MasterExtTable::consistency_quotient_degree_bounds(interpolant_degree, padded_height) + .into_iter() + .enumerate() + .map(|(origin_index, degree)| DegreeWithOrigin { + degree, + interpolant_degree, + zerofier_degree: padded_height as Degree, + origin_index, + origin_table_height: padded_height, + origin_constraint_type: ConstraintType::Consistency, + }) + .collect(); + + let transition_degrees_with_origin = + MasterExtTable::transition_quotient_degree_bounds(interpolant_degree, padded_height) + .into_iter() + .enumerate() + .map(|(origin_index, degree)| DegreeWithOrigin { + degree, + interpolant_degree, + zerofier_degree: padded_height as Degree - 1, + origin_index, + origin_table_height: padded_height, + origin_constraint_type: ConstraintType::Transition, + }) + .collect(); + + let terminal_degrees_with_origin = + MasterExtTable::terminal_quotient_degree_bounds(interpolant_degree) + .into_iter() + .enumerate() + .map(|(origin_index, degree)| DegreeWithOrigin { + degree, + interpolant_degree, + zerofier_degree: 1, + origin_index, + origin_table_height: padded_height, + origin_constraint_type: ConstraintType::Terminal, + }) + .collect(); + + [ + initial_degrees_with_origin, + consistency_degrees_with_origin, + transition_degrees_with_origin, + terminal_degrees_with_origin, + ] + .concat() +} diff --git a/triton-vm/src/table/master_table.rs b/triton-vm/src/table/master_table.rs index d778e890..e6f4b78c 100644 --- a/triton-vm/src/table/master_table.rs +++ b/triton-vm/src/table/master_table.rs @@ -29,6 +29,7 @@ use crate::stark::NUM_RANDOMIZER_POLYNOMIALS; use crate::table::cascade_table::CascadeTable; use crate::table::challenges::Challenges; use crate::table::degree_lowering_table::DegreeLoweringTable; +use crate::table::extension_table::all_degrees_with_origin; use crate::table::extension_table::DegreeWithOrigin; use crate::table::extension_table::Quotientable; use crate::table::hash_table::HashTable; @@ -957,13 +958,6 @@ impl MasterExtTable { } } -pub(crate) fn all_degrees_with_origin( - interpolant_degree: Degree, - padded_height: usize, -) -> Vec { - MasterExtTable::all_degrees_with_origin("master table", interpolant_degree, padded_height) -} - pub(crate) fn max_degree_with_origin( interpolant_degree: Degree, padded_height: usize, diff --git a/triton-vm/src/table/tasm_air_constraints.rs b/triton-vm/src/table/tasm_air_constraints.rs index c5a44075..96d653cf 100644 --- a/triton-vm/src/table/tasm_air_constraints.rs +++ b/triton-vm/src/table/tasm_air_constraints.rs @@ -3,7 +3,7 @@ //! to fill in this file with optimized constraints. use crate::instruction::LabelledInstruction; -use crate::table::extension_table::ERROR_MESSAGE_GENERATE_CONSTRAINTS; +use crate::table::constraints::ERROR_MESSAGE_GENERATE_CONSTRAINTS; use crate::table::TasmConstraintEvaluationMemoryLayout; pub fn air_constraint_evaluation_tasm(