Skip to content

Commit

Permalink
perf: use new twenty-first polynomial functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Apr 15, 2024
1 parent 54dbb99 commit 8799c67
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 195 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ default-features = false
features = ["precommit-hook", "run-cargo-clippy", "run-cargo-fmt"]

[patch.crates-io]
twenty-first = { git = "https://github.com/Neptune-Crypto/twenty-first.git", rev = "db86606f" }
twenty-first = { git = "https://github.com/Neptune-Crypto/twenty-first.git", rev = "1e065d68" }
17 changes: 8 additions & 9 deletions constraint-evaluation-generator/src/codegen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,31 @@ impl Codegen for RustBackend {
const NUM_TERMINAL_CONSTRAINTS: usize = #num_term_constraints;

#[allow(unused_variables)]
fn initial_quotient_degree_bounds(interpolant_degree: Degree) -> Vec<Degree> {
fn initial_quotient_degree_bounds(interpolant_degree: isize) -> Vec<isize> {
let zerofier_degree = 1;
[#init_constraint_degrees].to_vec()
}

#[allow(unused_variables)]
fn consistency_quotient_degree_bounds(
interpolant_degree: Degree,
interpolant_degree: isize,
padded_height: usize,
) -> Vec<Degree> {
let zerofier_degree = padded_height as Degree;
) -> Vec<isize> {
let zerofier_degree = padded_height as isize;
[#cons_constraint_degrees].to_vec()
}

#[allow(unused_variables)]
fn transition_quotient_degree_bounds(
interpolant_degree: Degree,
interpolant_degree: isize,
padded_height: usize,
) -> Vec<Degree> {
let zerofier_degree = padded_height as Degree - 1;
) -> Vec<isize> {
let zerofier_degree = padded_height as isize - 1;
[#tran_constraint_degrees].to_vec()
}

#[allow(unused_variables)]
fn terminal_quotient_degree_bounds(interpolant_degree: Degree) -> Vec<Degree> {
fn terminal_quotient_degree_bounds(interpolant_degree: isize) -> Vec<isize> {
let zerofier_degree = 1;
[#term_constraint_degrees].to_vec()
}
Expand All @@ -97,7 +97,6 @@ impl RustBackend {
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;
Expand Down
15 changes: 8 additions & 7 deletions triton-vm/src/arithmetic_domain.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::ops::MulAssign;

use num_traits::One;
use twenty_first::math::traits::FiniteField;
use twenty_first::math::traits::PrimitiveRootOfUnity;
use twenty_first::prelude::*;
use twenty_first::shared_math::traits::FiniteField;
use twenty_first::shared_math::traits::PrimitiveRootOfUnity;

use crate::error::ArithmeticDomainError;

Expand Down Expand Up @@ -55,9 +55,10 @@ impl ArithmeticDomain {
{
// The limitation arises in `Polynomial::fast_coset_evaluate` in dependency `twenty-first`.
let batch_evaluation_is_possible = self.length >= polynomial.coefficients.len();
match batch_evaluation_is_possible {
true => polynomial.fast_coset_evaluate(self.offset, self.generator, self.length),
false => self.evaluate_in_every_point_individually(polynomial),
if batch_evaluation_is_possible {
polynomial.fast_coset_evaluate(self.offset.into(), self.generator, self.length)
} else {
self.evaluate_in_every_point_individually(polynomial)
}
}

Expand All @@ -73,9 +74,9 @@ impl ArithmeticDomain {

pub fn interpolate<FF>(&self, values: &[FF]) -> Polynomial<FF>
where
FF: FiniteField + MulAssign<BFieldElement>,
FF: FiniteField + MulAssign<BFieldElement> + From<BFieldElement>,
{
Polynomial::fast_coset_interpolate(self.offset, self.generator, values)
Polynomial::fast_coset_interpolate(self.offset.into(), self.generator, values)
}

pub fn low_degree_extension<FF>(&self, codeword: &[FF], target_domain: Self) -> Vec<FF>
Expand Down
10 changes: 5 additions & 5 deletions triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::marker::PhantomData;

use itertools::Itertools;
use rayon::iter::*;
use twenty_first::math::traits::FiniteField;
use twenty_first::prelude::*;
use twenty_first::shared_math::other::log_2_ceil;
use twenty_first::shared_math::traits::FiniteField;

use crate::arithmetic_domain::ArithmeticDomain;
use crate::error::FriProvingError;
Expand Down Expand Up @@ -626,14 +625,15 @@ impl<H: AlgebraicHasher> Fri<H> {

pub fn num_rounds(&self) -> usize {
let first_round_code_dimension = self.first_round_max_degree() + 1;
let max_num_rounds = log_2_ceil(first_round_code_dimension as u128);
let max_num_rounds = first_round_code_dimension.next_power_of_two().ilog2();

// Skip rounds for which Merkle tree verification cost exceeds arithmetic cost,
// because more than half the codeword's locations are queried.
let num_rounds_checking_all_locations = u64::from(self.num_collinearity_checks.ilog2());
let num_rounds_checking_all_locations = self.num_collinearity_checks.ilog2();
let num_rounds_checking_most_locations = num_rounds_checking_all_locations + 1;

max_num_rounds.saturating_sub(num_rounds_checking_most_locations) as usize
let num_rounds = max_num_rounds.saturating_sub(num_rounds_checking_most_locations);
num_rounds.try_into().unwrap()
}

pub fn last_round_max_degree(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! `use triton_vm::twenty_first::prelude::*;`.
pub use twenty_first;
pub use twenty_first::math::traits::FiniteField;
pub use twenty_first::prelude::bfe;
pub use twenty_first::prelude::tip5;
pub use twenty_first::prelude::xfe;
Expand All @@ -13,7 +14,6 @@ pub use twenty_first::prelude::BFieldElement;
pub use twenty_first::prelude::Digest;
pub use twenty_first::prelude::Tip5;
pub use twenty_first::prelude::XFieldElement;
pub use twenty_first::shared_math::traits::FiniteField;

pub use crate::error::InstructionError;
pub use crate::instruction::LabelledInstruction;
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/proof_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ mod tests {
use proptest::collection::vec;
use proptest_arbitrary_interop::arb;
use test_strategy::proptest;
use twenty_first::shared_math::other::random_elements;
use twenty_first::math::other::random_elements;

use crate::proof_item::FriResponse;
use crate::proof_item::ProofItem;
Expand Down
11 changes: 5 additions & 6 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use ndarray::Zip;
use rayon::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use twenty_first::math::traits::FiniteField;
use twenty_first::prelude::*;
use twenty_first::shared_math::mpolynomial::Degree;
use twenty_first::shared_math::traits::FiniteField;

use crate::aet::AlgebraicExecutionTrace;
use crate::arithmetic_domain::ArithmeticDomain;
Expand Down Expand Up @@ -534,7 +533,7 @@ impl Stark {
/// and testing.
pub(crate) fn quotient_domain(
fri_domain: ArithmeticDomain,
max_degree: Degree,
max_degree: isize,
) -> Result<ArithmeticDomain, ProvingError> {
let maybe_blowup_factor = match cfg!(debug_assertions) {
true => 2,
Expand All @@ -548,7 +547,7 @@ impl Stark {
/// Compute the upper bound to use for the maximum degree the quotients given the length of the
/// trace and the number of trace randomizers.
/// The degree of the quotients depends on the constraints, _i.e._, the AIR.
pub fn derive_max_degree(&self, padded_height: usize) -> Degree {
pub fn derive_max_degree(&self, padded_height: usize) -> isize {
let interpolant_degree = interpolant_degree(padded_height, self.num_trace_randomizers);
let max_constraint_degree_with_origin =
max_degree_with_origin(interpolant_degree, padded_height);
Expand All @@ -558,7 +557,7 @@ impl Stark {
let max_degree_supported_by_that_smallest_arithmetic_domain =
min_arithmetic_domain_length_supporting_max_constraint_degree - 1;

max_degree_supported_by_that_smallest_arithmetic_domain as Degree
max_degree_supported_by_that_smallest_arithmetic_domain as isize
}

/// Compute the parameters for FRI. The length of the FRI domain, _i.e._, the number of
Expand Down Expand Up @@ -1086,8 +1085,8 @@ pub(crate) mod tests {
use rand::Rng;
use strum::EnumCount;
use test_strategy::proptest;
use twenty_first::math::other::random_elements;
use twenty_first::prelude::x_field_element::EXTENSION_DEGREE;
use twenty_first::shared_math::other::random_elements;

use crate::error::InstructionError;
use crate::example_programs::*;
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub enum ChallengeId {
///
/// Used by the evaluation argument [`PrepareChunkEvalArg`][prep] and in the Hash Table.
///
/// [rate]: twenty_first::shared_math::tip5::RATE
/// [rate]: twenty_first::math::tip5::RATE
/// [prep]: crate::table::table_column::ProgramExtTableColumn::PrepareChunkRunningEvaluation
ProgramAttestationPrepareChunkIndeterminate,

Expand All @@ -126,7 +126,7 @@ pub enum ChallengeId {
/// Used by the evaluation arguments [`SendChunkEvalArg`][send] and
/// [`ReceiveChunkEvalArg`][recv]. See also: [`ProgramAttestationPrepareChunkIndeterminate`].
///
/// [rate]: twenty_first::shared_math::tip5::RATE
/// [rate]: twenty_first::math::tip5::RATE
/// [send]: crate::table::table_column::ProgramExtTableColumn::SendChunkRunningEvaluation
/// [recv]: crate::table::table_column::HashExtTableColumn::ReceiveChunkRunningEvaluation
ProgramAttestationSendChunkIndeterminate,
Expand Down
11 changes: 5 additions & 6 deletions triton-vm/src/table/constraint_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use num_traits::Zero;
use quote::quote;
use quote::ToTokens;
use twenty_first::prelude::*;
use twenty_first::shared_math::mpolynomial::Degree;

use CircuitExpression::*;

Expand Down Expand Up @@ -428,7 +427,7 @@ impl<II: InputIndicator> ConstraintCircuit<II> {
}

/// Return degree of the multivariate polynomial represented by this circuit
pub fn degree(&self) -> Degree {
pub fn degree(&self) -> isize {
if self.is_zero() {
return -1;
}
Expand Down Expand Up @@ -766,7 +765,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {
/// when a tables' constraints are built using the master table's column indices.
pub fn lower_to_degree(
multicircuit: &mut [Self],
target_degree: Degree,
target_degree: isize,
num_base_cols: usize,
num_ext_cols: usize,
) -> (Vec<Self>, Vec<Self>) {
Expand Down Expand Up @@ -825,7 +824,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {
/// variable. The ID of the chosen node is returned.
fn pick_node_to_substitute(
multicircuit: &[ConstraintCircuitMonad<II>],
target_degree: Degree,
target_degree: isize,
) -> usize {
assert!(!multicircuit.is_empty());

Expand Down Expand Up @@ -899,7 +898,7 @@ impl<II: InputIndicator> ConstraintCircuitMonad<II> {
}

/// Returns the maximum degree of all circuits in the multicircuit.
fn multicircuit_degree(multicircuit: &[ConstraintCircuitMonad<II>]) -> Degree {
fn multicircuit_degree(multicircuit: &[ConstraintCircuitMonad<II>]) -> isize {
multicircuit
.iter()
.map(|circuit| circuit.circuit.borrow().degree())
Expand Down Expand Up @@ -1915,7 +1914,7 @@ mod tests {
/// - the numbers of original and new constraints
fn lower_degree_and_assert_properties<II: InputIndicator>(
multicircuit: &mut [ConstraintCircuitMonad<II>],
target_deg: Degree,
target_deg: isize,
num_base_cols: usize,
num_ext_cols: usize,
) -> (
Expand Down
18 changes: 5 additions & 13 deletions triton-vm/src/table/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
//! Run `cargo run --bin constraint-evaluation-generator`
//! to fill in this file with optimized constraints.
use crate::table::challenges::Challenges;
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;
Expand Down Expand Up @@ -95,26 +94,19 @@ impl Quotientable for MasterExtTable {
const NUM_TRANSITION_CONSTRAINTS: usize = 0;
const NUM_TERMINAL_CONSTRAINTS: usize = 0;


fn initial_quotient_degree_bounds(_: Degree) -> Vec<Degree> {
fn initial_quotient_degree_bounds(_: isize) -> Vec<isize> {
panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}")
}

fn consistency_quotient_degree_bounds(
_: Degree,
_: usize,
) -> Vec<Degree> {
fn consistency_quotient_degree_bounds(_: isize, _: usize) -> Vec<isize> {
panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}")
}

fn transition_quotient_degree_bounds(
_: Degree,
_: usize,
) -> Vec<Degree> {
fn transition_quotient_degree_bounds(_: isize, _: usize) -> Vec<isize> {
panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}")
}

fn terminal_quotient_degree_bounds(_: Degree) -> Vec<Degree> {
fn terminal_quotient_degree_bounds(_: isize) -> Vec<isize> {
panic!("{ERROR_MESSAGE_GENERATE_DEGREE_BOUNDS}")
}
}
27 changes: 13 additions & 14 deletions triton-vm/src/table/extension_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use ndarray::ArrayView1;
use ndarray::ArrayView2;
use ndarray::ArrayViewMut2;
use ndarray::Axis;
use twenty_first::math::traits::FiniteField;
use twenty_first::prelude::*;
use twenty_first::shared_math::mpolynomial::Degree;
use twenty_first::shared_math::traits::FiniteField;

use crate::arithmetic_domain::ArithmeticDomain;
use crate::table::challenges::Challenges;
Expand Down Expand Up @@ -185,19 +184,19 @@ pub trait Quotientable: Evaluable<BFieldElement> {
});
}

fn initial_quotient_degree_bounds(interpolant_degree: Degree) -> Vec<Degree>;
fn initial_quotient_degree_bounds(interpolant_degree: isize) -> Vec<isize>;

fn consistency_quotient_degree_bounds(
interpolant_degree: Degree,
interpolant_degree: isize,
padded_height: usize,
) -> Vec<Degree>;
) -> Vec<isize>;

fn transition_quotient_degree_bounds(
interpolant_degree: Degree,
interpolant_degree: isize,
padded_height: usize,
) -> Vec<Degree>;
) -> Vec<isize>;

fn terminal_quotient_degree_bounds(interpolant_degree: Degree) -> Vec<Degree>;
fn terminal_quotient_degree_bounds(interpolant_degree: isize) -> Vec<isize>;
}

/// The type of constraint. Can be used to determine the degree bounds for the quotient
Expand Down Expand Up @@ -226,9 +225,9 @@ impl Display for ConstraintType {
/// of the FRI domain, which in turn is responsible for the main performance bottleneck.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct DegreeWithOrigin {
pub degree: Degree,
pub interpolant_degree: Degree,
pub zerofier_degree: Degree,
pub degree: isize,
pub interpolant_degree: isize,
pub zerofier_degree: isize,
pub origin_index: usize,
pub origin_table_height: usize,
pub origin_constraint_type: ConstraintType,
Expand All @@ -250,7 +249,7 @@ impl Display for DegreeWithOrigin {

/// 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,
interpolant_degree: isize,
padded_height: usize,
) -> Vec<DegreeWithOrigin> {
let initial_degrees_with_origin =
Expand All @@ -274,7 +273,7 @@ pub(crate) fn all_degrees_with_origin(
.map(|(origin_index, degree)| DegreeWithOrigin {
degree,
interpolant_degree,
zerofier_degree: padded_height as Degree,
zerofier_degree: padded_height as isize,
origin_index,
origin_table_height: padded_height,
origin_constraint_type: ConstraintType::Consistency,
Expand All @@ -288,7 +287,7 @@ pub(crate) fn all_degrees_with_origin(
.map(|(origin_index, degree)| DegreeWithOrigin {
degree,
interpolant_degree,
zerofier_degree: padded_height as Degree - 1,
zerofier_degree: padded_height as isize - 1,
origin_index,
origin_table_height: padded_height,
origin_constraint_type: ConstraintType::Transition,
Expand Down
Loading

0 comments on commit 8799c67

Please sign in to comment.