Skip to content

Commit

Permalink
only ever interpolate and evaluate over Domains of BFieldElements
Browse files Browse the repository at this point in the history
Only accept generators of type `BFieldElement`, not any Finite Field.
Remove duplicate FRI domain in struct `Stark`.
  • Loading branch information
jan-ferdinand committed Nov 16, 2022
1 parent a366ea9 commit 4204444
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 64 deletions.
12 changes: 6 additions & 6 deletions triton-vm/src/cross_table_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub trait CrossTableArg {
fn terminal_quotient(
&self,
ext_codeword_tables: &ExtTableCollection,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
) -> Vec<XFieldElement> {
let from_codeword = self.combined_from_codeword(ext_codeword_tables);
let to_codeword = self.combined_to_codeword(ext_codeword_tables);
Expand All @@ -55,7 +55,7 @@ pub trait CrossTableArg {
.into_iter()
.map(|x| x - omicron.inverse())
.collect();
let zerofier_inverse = XFieldElement::batch_inversion(zerofier);
let zerofier_inverse = BFieldElement::batch_inversion(zerofier);

zerofier_inverse
.into_iter()
Expand Down Expand Up @@ -453,8 +453,8 @@ impl GrandCrossTableArg {
pub fn terminal_quotient_codeword(
&self,
ext_codeword_tables: &ExtTableCollection,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
) -> Vec<XFieldElement> {
let mut non_linear_sum_codeword = vec![XFieldElement::zero(); fri_domain.length];

Expand Down Expand Up @@ -502,7 +502,7 @@ impl GrandCrossTableArg {
.into_iter()
.map(|x| x - omicron.inverse())
.collect();
let zerofier_inverse = XFieldElement::batch_inversion(zerofier);
let zerofier_inverse = BFieldElement::batch_inversion(zerofier);

zerofier_inverse
.into_iter()
Expand Down
12 changes: 9 additions & 3 deletions triton-vm/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ where
}
}

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

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

pub fn domain_value(&self, index: u32) -> FF {
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 @@ -50,7 +50,7 @@ pub struct Fri<H> {
// nearest power of 2.
pub expansion_factor: usize,
pub colinearity_checks_count: usize,
pub domain: Domain<XFieldElement>,
pub domain: Domain<BFieldElement>,
_hasher: PhantomData<H>,
}

Expand Down
27 changes: 13 additions & 14 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use triton_profiler::{prof_itr0, prof_start, prof_stop};
use crate::cross_table_arguments::{
CrossTableArg, EvalArg, GrandCrossTableArg, NUM_CROSS_TABLE_ARGS, NUM_PUBLIC_EVAL_ARGS,
};
use crate::domain::Domain;
use crate::fri::{Fri, FriValidationError};
use crate::proof::{Claim, Proof};
use crate::proof_item::ProofItem;
Expand Down Expand Up @@ -108,7 +107,6 @@ pub struct Stark {
parameters: StarkParameters,
claim: Claim,
max_degree: Degree,
fri_domain: Domain<BFieldElement>,
fri: Fri<StarkHasher>,
}

Expand All @@ -122,7 +120,6 @@ impl Stark {
let omega =
BFieldElement::primitive_root_of_unity(fri_domain_length.try_into().unwrap()).unwrap();
let coset_offset = BFieldElement::generator();
let fri_domain: Domain<BFieldElement> = Domain::new(coset_offset, omega, fri_domain_length);
let fri = Fri::new(
coset_offset,
omega,
Expand All @@ -134,7 +131,6 @@ impl Stark {
parameters,
claim,
max_degree,
fri_domain,
fri,
}
}
Expand All @@ -153,7 +149,7 @@ impl Stark {

prof_start!(maybe_profiler, "LDE 1");
let base_fri_domain_tables = base_trace_tables
.to_fri_domain_tables(&self.fri_domain, self.parameters.num_trace_randomizers);
.to_fri_domain_tables(&self.fri.domain, self.parameters.num_trace_randomizers);
let base_fri_domain_codewords = base_fri_domain_tables.get_all_base_columns();
let randomizer_and_base_fri_domain_codewords =
vec![b_rand_codewords, base_fri_domain_codewords.clone()].concat();
Expand Down Expand Up @@ -557,7 +553,7 @@ impl Stark {
}

fn shift_codeword(
fri_x_values: &Vec<XFieldElement>,
fri_x_values: &Vec<BFieldElement>,
codeword: &Vec<XFieldElement>,
shift: u32,
) -> Vec<XFieldElement> {
Expand Down Expand Up @@ -606,7 +602,7 @@ impl Stark {

fn get_randomizer_codewords(&self) -> (Vec<XFieldElement>, Vec<Vec<BFieldElement>>) {
let randomizer_coefficients = random_elements(self.max_degree as usize + 1);
let randomizer_polynomial = Polynomial::new(randomizer_coefficients);
let randomizer_polynomial = Polynomial::<XFieldElement>::new(randomizer_coefficients);

let x_randomizer_codeword = self.fri.domain.evaluate(&randomizer_polynomial);
let mut b_randomizer_codewords = vec![vec![], vec![], vec![]];
Expand Down Expand Up @@ -849,7 +845,7 @@ impl Stark {
let base_offset = self.parameters.num_randomizer_polynomials;
let ext_offset = base_offset + num_base_polynomials;
let final_offset = ext_offset + num_extension_polynomials;
let omicron: XFieldElement = derive_omicron(padded_height as u64);
let omicron = derive_omicron(padded_height as u64);
let omicron_inverse = omicron.inverse();
for (combination_check_index, revealed_combination_leaf) in combination_check_indices
.into_iter()
Expand Down Expand Up @@ -951,7 +947,8 @@ impl Stark {
.zip_eq(initial_quotient_degree_bounds.iter())
{
let shift = self.max_degree - degree_bound;
let quotient = evaluated_bc / (current_fri_domain_value - BFieldElement::one());
let quotient =
evaluated_bc / (current_fri_domain_value - BFieldElement::one()).lift();
let quotient_shifted =
quotient * current_fri_domain_value.mod_pow_u32(shift as u32);
summands.push(quotient);
Expand All @@ -968,7 +965,8 @@ impl Stark {
let shift = self.max_degree - degree_bound;
let quotient = evaluated_cc
/ (current_fri_domain_value.mod_pow_u32(padded_height as u32)
- BFieldElement::one());
- BFieldElement::one())
.lift();
let quotient_shifted =
quotient * current_fri_domain_value.mod_pow_u32(shift as u32);
summands.push(quotient);
Expand All @@ -992,7 +990,7 @@ impl Stark {
let denominator = current_fri_domain_value
.mod_pow_u32(padded_height as u32)
- BFieldElement::one();
evaluated_tc * numerator / denominator
evaluated_tc * numerator / denominator.lift()
};
let quotient_shifted =
quotient * current_fri_domain_value.mod_pow_u32(shift as u32);
Expand All @@ -1008,7 +1006,8 @@ impl Stark {
.zip_eq(terminal_quotient_degree_bounds.iter())
{
let shift = self.max_degree - degree_bound;
let quotient = evaluated_termc / (current_fri_domain_value - omicron_inverse);
let quotient =
evaluated_termc / (current_fri_domain_value - omicron_inverse).lift();
let quotient_shifted =
quotient * current_fri_domain_value.mod_pow_u32(shift as u32);
summands.push(quotient);
Expand All @@ -1028,8 +1027,8 @@ impl Stark {
let shift = self.max_degree - grand_cross_table_arg_degree_bound;
let grand_cross_table_arg_evaluated =
grand_cross_table_arg.evaluate_non_linear_sum_of_differences(&cross_slice_by_table);
let grand_cross_table_arg_quotient =
grand_cross_table_arg_evaluated / (current_fri_domain_value - omicron_inverse);
let grand_cross_table_arg_quotient = grand_cross_table_arg_evaluated
/ (current_fri_domain_value - omicron_inverse).lift();
let grand_cross_table_arg_quotient_shifted =
grand_cross_table_arg_quotient * current_fri_domain_value.mod_pow_u32(shift as u32);
summands.push(grand_cross_table_arg_quotient);
Expand Down
15 changes: 6 additions & 9 deletions triton-vm/src/table/base_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::super::domain::Domain;
use itertools::Itertools;
use num_traits::Zero;
use num_traits::{One, Zero};
use rand_distr::{Distribution, Standard};
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use std::ops::{Mul, MulAssign, Range};
Expand Down Expand Up @@ -270,8 +269,8 @@ where

fn low_degree_extension(
&self,
fri_domain: &Domain<FF>,
omicron: FF,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
columns: Range<usize>,
) -> Vec<Vec<FF>> {
Expand All @@ -287,7 +286,7 @@ where
/// if it is called with a subset, it *will* fail.
fn interpolate_columns(
&self,
omicron: FF,
omicron: BFieldElement,
num_trace_randomizers: usize,
columns: Range<usize>,
) -> Vec<Polynomial<FF>> {
Expand All @@ -303,10 +302,8 @@ where
self.name()
);

// FIXME: Use Domain::new(…).domain_values() (needs some refactoring)
let trace_domain = (0..padded_height)
.map(|i| omicron.mod_pow_u32(i as u32))
.collect_vec();
let trace_domain =
Domain::new(BFieldElement::one(), omicron, padded_height).domain_values();

let randomizer_domain = disjoint_domain(num_trace_randomizers, &trace_domain);
let interpolation_domain = vec![trace_domain, randomizer_domain].concat();
Expand Down
24 changes: 12 additions & 12 deletions triton-vm/src/table/extension_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn initial_quotients(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
) -> Vec<Vec<XFieldElement>> {
Expand All @@ -205,7 +205,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {
.into_iter()
.map(|x| x - BFieldElement::one())
.collect();
let zerofier_inverse = XFieldElement::batch_inversion(zerofier_codeword);
let zerofier_inverse = BFieldElement::batch_inversion(zerofier_codeword);

let transposed_quotient_codewords: Vec<_> = zerofier_inverse
.par_iter()
Expand All @@ -224,7 +224,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn consistency_quotients(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
padded_height: usize,
Expand All @@ -236,7 +236,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {
.iter()
.map(|x| x.mod_pow_u32(padded_height as u32) - BFieldElement::one())
.collect();
let zerofier_inverse = XFieldElement::batch_inversion(zerofier_codeword);
let zerofier_inverse = BFieldElement::batch_inversion(zerofier_codeword);

let transposed_quotient_codewords: Vec<_> = zerofier_inverse
.par_iter()
Expand All @@ -255,10 +255,10 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn transition_quotients(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
omicron: XFieldElement,
omicron: BFieldElement,
padded_height: usize,
) -> Vec<Vec<XFieldElement>> {
debug_assert_eq!(fri_domain.length, transposed_codewords.len());
Expand Down Expand Up @@ -305,10 +305,10 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn terminal_quotients(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
omicron: XFieldElement,
omicron: BFieldElement,
) -> Vec<Vec<XFieldElement>> {
debug_assert_eq!(fri_domain.length, transposed_codewords.len());

Expand All @@ -319,7 +319,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {
.into_iter()
.map(|x| x - omicron.inverse())
.collect_vec();
let zerofier_inverse = XFieldElement::batch_inversion(zerofier_codeword);
let zerofier_inverse = BFieldElement::batch_inversion(zerofier_codeword);

let transposed_quotient_codewords: Vec<_> = zerofier_inverse
.par_iter()
Expand All @@ -338,10 +338,10 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn all_quotients(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
transposed_codewords: Vec<Vec<XFieldElement>>,
challenges: &AllChallenges,
omicron: XFieldElement,
omicron: BFieldElement,
padded_height: usize,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Vec<Vec<XFieldElement>> {
Expand Down Expand Up @@ -409,7 +409,7 @@ pub trait Quotientable: ExtensionTable + Evaluable {
/// probably the result of un-clean division.
fn debug_fri_domain_bound_check(
&self,
fri_domain: &Domain<XFieldElement>,
fri_domain: &Domain<BFieldElement>,
quotient_codewords: &[Vec<XFieldElement>],
quotient_type: &str,
) {
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ impl HashTable {
impl ExtHashTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/instruction_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ impl InstructionTable {
impl ExtInstructionTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/jump_stack_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ impl JumpStackTable {
impl ExtJumpStackTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/op_stack_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ impl OpStackTable {
impl ExtOpStackTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/processor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ impl ProcessorTable {
impl ExtProcessorTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/program_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ impl ProgramTable {
impl ExtProgramTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/ram_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl RamTable {
impl ExtRamTable {
pub fn to_fri_domain_table(
&self,
fri_domain: &Domain<XFieldElement>,
omicron: XFieldElement,
fri_domain: &Domain<BFieldElement>,
omicron: BFieldElement,
num_trace_randomizers: usize,
) -> Self {
let ext_columns = self.base_width()..self.full_width();
Expand Down
Loading

0 comments on commit 4204444

Please sign in to comment.