Skip to content

Commit

Permalink
remove type parameter from arithmetic domain
Browse files Browse the repository at this point in the history
We only ever need arithmetic domains over `BFieldElement`s. Hardcoding
it makes the code less general, but also less confusing, especially with
respect to interpolation and evaluation over arithmetic domains.

Fix #127
  • Loading branch information
jan-ferdinand committed Nov 25, 2022
1 parent c486811 commit 381d364
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 73 deletions.
71 changes: 15 additions & 56 deletions triton-vm/src/arithmetic_domain.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
use std::marker::PhantomData;
use std::ops::{Mul, MulAssign};
use std::ops::MulAssign;

use num_traits::One;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::polynomial::Polynomial;
use twenty_first::shared_math::traits::{FiniteField, ModPowU32};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArithmeticDomain<FF> {
pub struct ArithmeticDomain {
pub offset: BFieldElement,
pub generator: BFieldElement,
pub length: usize,
_finite_field: PhantomData<FF>,
}

impl<FF> ArithmeticDomain<FF>
where
FF: FiniteField
+ From<BFieldElement>
+ Mul<BFieldElement, Output = FF>
+ MulAssign<BFieldElement>,
{
impl ArithmeticDomain {
pub fn new(offset: BFieldElement, generator: BFieldElement, length: usize) -> Self {
Self {
offset,
generator,
length,
_finite_field: PhantomData,
}
}

pub fn evaluate<GF>(&self, polynomial: &Polynomial<GF>) -> Vec<GF>
pub fn evaluate<FF>(&self, polynomial: &Polynomial<FF>) -> Vec<FF>
where
GF: FiniteField + From<BFieldElement> + MulAssign<BFieldElement>,
FF: FiniteField + MulAssign<BFieldElement>,
{
polynomial.fast_coset_evaluate(&self.offset, self.generator, self.length)
}

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

pub fn low_degree_extension<GF>(&self, codeword: &[GF], target_domain: &Self) -> Vec<GF>
pub fn low_degree_extension<FF>(&self, codeword: &[FF], target_domain: &Self) -> Vec<FF>
where
GF: FiniteField + From<BFieldElement> + MulAssign<BFieldElement>,
FF: FiniteField + MulAssign<BFieldElement>,
{
target_domain.evaluate(&self.interpolate(codeword))
}

pub fn domain_value(&self, index: u32) -> FF {
let domain_value = self.generator.mod_pow_u32(index) * self.offset;
domain_value.into()
pub fn domain_value(&self, index: u32) -> BFieldElement {
self.generator.mod_pow_u32(index) * self.offset
}

pub fn domain_values(&self) -> Vec<FF> {
let mut accumulator = FF::one();
pub fn domain_values(&self) -> Vec<BFieldElement> {
let mut accumulator = BFieldElement::one();
let mut domain_values = Vec::with_capacity(self.length);

for _ in 0..self.length {
Expand All @@ -73,7 +64,6 @@ mod domain_tests {
use itertools::Itertools;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::traits::PrimitiveRootOfUnity;
use twenty_first::shared_math::x_field_element::XFieldElement;

use super::*;

Expand All @@ -86,10 +76,7 @@ mod domain_tests {
for order in [4, 8, 32] {
let generator = BFieldElement::primitive_root_of_unity(order).unwrap();
let offset = BFieldElement::generator();
let b_domain =
ArithmeticDomain::<BFieldElement>::new(offset, generator, order as usize);
let x_domain =
ArithmeticDomain::<XFieldElement>::new(offset, generator, order as usize);
let b_domain = ArithmeticDomain::new(offset, generator, order as usize);

let expected_b_values: Vec<BFieldElement> =
(0..order).map(|i| offset * generator.mod_pow(i)).collect();
Expand All @@ -106,21 +93,6 @@ mod domain_tests {
"domain_value() generates the given domain BFieldElement value"
);

let expected_x_values: Vec<XFieldElement> =
expected_b_values.iter().map(|bfe| bfe.lift()).collect();
let actual_x_values_1 = x_domain.domain_values();
let actual_x_values_2 = (0..order as u32)
.map(|i| x_domain.domain_value(i))
.collect_vec();
assert_eq!(
expected_x_values, actual_x_values_1,
"domain_values() generates the arithmetic domain's XFieldElement values"
);
assert_eq!(
expected_x_values, actual_x_values_2,
"domain_value() generates the given domain XFieldElement values"
);

let values = b_domain.evaluate(&poly);
assert_ne!(values, x_squared_coefficients);

Expand All @@ -134,19 +106,6 @@ mod domain_tests {
values[i as usize]
);
}

let x_squared_coefficients_lifted: Vec<XFieldElement> = x_squared_coefficients
.clone()
.into_iter()
.map(|x| x.lift())
.collect();
let xpol = Polynomial::new(x_squared_coefficients_lifted.clone());

let x_field_x_values = x_domain.evaluate(&xpol);
assert_ne!(x_field_x_values, x_squared_coefficients_lifted);

let x_interpolant = x_domain.interpolate(&x_field_x_values);
assert_eq!(xpol, x_interpolant);
}
}
}
4 changes: 2 additions & 2 deletions triton-vm/src/cross_table_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait CrossTableArg {
fn terminal_quotient(
&self,
ext_codeword_tables: &ExtTableCollection,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain,
trace_domain_generator: BFieldElement,
) -> Vec<XFieldElement> {
let from_codeword = self.combined_from_codeword(ext_codeword_tables);
Expand Down Expand Up @@ -454,7 +454,7 @@ impl GrandCrossTableArg {
pub fn terminal_quotient_codeword(
&self,
ext_codeword_tables: &ExtTableCollection,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain,
trace_domain_generator: BFieldElement,
) -> Vec<XFieldElement> {
let mut non_linear_sum_codeword = vec![XFieldElement::zero(); quotient_domain.length];
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct Fri<H> {
// nearest power of 2.
pub expansion_factor: usize,
pub colinearity_checks_count: usize,
pub domain: ArithmeticDomain<BFieldElement>,
pub domain: ArithmeticDomain,
_hasher: PhantomData<H>,
}

Expand Down
9 changes: 4 additions & 5 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ impl Stark {
);

prof_start!(maybe_profiler, "LDE 3");
let combination_polynomial = quotient_domain.interpolate(&combination_codeword);
let fri_combination_codeword_without_randomizer =
self.fri.domain.evaluate(&combination_polynomial);
quotient_domain.low_degree_extension(&combination_codeword, &self.fri.domain);
prof_stop!(maybe_profiler, "LDE 3");

let fri_combination_codeword: Vec<_> = fri_combination_codeword_without_randomizer
Expand Down Expand Up @@ -415,7 +414,7 @@ impl Stark {
proof_stream.to_proof()
}

fn quotient_domain(&self) -> ArithmeticDomain<BFieldElement> {
fn quotient_domain(&self) -> ArithmeticDomain {
let offset = self.fri.domain.offset;
let length = roundup_npo2(self.max_degree as u64);
let generator = BFieldElement::primitive_root_of_unity(length).unwrap();
Expand Down Expand Up @@ -452,7 +451,7 @@ impl Stark {
#[allow(clippy::too_many_arguments)]
fn create_combination_codeword(
&self,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain,
base_codewords: Vec<Vec<BFieldElement>>,
extension_codewords: Vec<Vec<XFieldElement>>,
quotient_codewords: Vec<Vec<XFieldElement>>,
Expand Down Expand Up @@ -526,7 +525,7 @@ impl Stark {
#[allow(clippy::too_many_arguments)]
fn debug_check_degrees(
&self,
domain: &ArithmeticDomain<BFieldElement>,
domain: &ArithmeticDomain,
idx: &usize,
degree_bound: &Degree,
shift: &u32,
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/base_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where

fn randomized_low_deg_extension(
&self,
fri_domain: &ArithmeticDomain<BFieldElement>,
fri_domain: &ArithmeticDomain,
num_trace_randomizers: usize,
columns: Range<usize>,
) -> Table<FF> {
Expand Down
10 changes: 5 additions & 5 deletions triton-vm/src/table/extension_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn initial_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
domain: &ArithmeticDomain,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
) -> Vec<Vec<XFieldElement>> {
Expand All @@ -178,7 +178,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn consistency_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
domain: &ArithmeticDomain,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
padded_height: usize,
Expand Down Expand Up @@ -206,7 +206,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn transition_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
domain: &ArithmeticDomain,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
trace_domain_generator: BFieldElement,
Expand Down Expand Up @@ -255,7 +255,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn terminal_quotients(
&self,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
trace_domain_generator: BFieldElement,
Expand Down Expand Up @@ -285,7 +285,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn all_quotients(
&self,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain,
transposed_codewords: Vec<Vec<XFieldElement>>,
challenges: &AllChallenges,
trace_domain_generator: BFieldElement,
Expand Down
6 changes: 3 additions & 3 deletions triton-vm/src/table/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl BaseTableCollection {

pub fn to_fri_domain_tables(
&self,
fri_domain: &ArithmeticDomain<BFieldElement>,
fri_domain: &ArithmeticDomain,
num_trace_randomizers: usize,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Self {
Expand Down Expand Up @@ -512,7 +512,7 @@ impl ExtTableCollection {
/// Heads up: only extension columns are low-degree extended – base columns are already covered.
pub fn to_fri_domain_tables(
&self,
fri_domain: &ArithmeticDomain<BFieldElement>,
fri_domain: &ArithmeticDomain,
num_trace_randomizers: usize,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Self {
Expand Down Expand Up @@ -621,7 +621,7 @@ impl ExtTableCollection {

pub fn get_all_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
domain: &ArithmeticDomain,
challenges: &AllChallenges,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Vec<Vec<XFieldElement>> {
Expand Down

0 comments on commit 381d364

Please sign in to comment.