Skip to content

Commit

Permalink
incorporate Alan's feedback
Browse files Browse the repository at this point in the history
- consistently name parameters `quotient_domain`
- use `prof_start` & `prof_stop` macros consistently

Co-authored-by: Alan <alan@neptune.cash>
  • Loading branch information
jan-ferdinand and aszepieniec committed Nov 17, 2022
1 parent e656ecd commit c85a68e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 64 deletions.
14 changes: 7 additions & 7 deletions triton-vm/src/cross_table_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ pub trait CrossTableArg {
fn terminal_quotient(
&self,
ext_codeword_tables: &ExtTableCollection,
domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain<BFieldElement>,
trace_domain_generator: BFieldElement,
) -> Vec<XFieldElement> {
let from_codeword = self.combined_from_codeword(ext_codeword_tables);
let to_codeword = self.combined_to_codeword(ext_codeword_tables);

let trace_domain_generator_inverse = trace_domain_generator.inverse();
let zerofier = domain
let zerofier = quotient_domain
.domain_values()
.into_iter()
.map(|x| x - trace_domain_generator_inverse)
Expand Down Expand Up @@ -454,10 +454,10 @@ impl GrandCrossTableArg {
pub fn terminal_quotient_codeword(
&self,
ext_codeword_tables: &ExtTableCollection,
domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain<BFieldElement>,
trace_domain_generator: BFieldElement,
) -> Vec<XFieldElement> {
let mut non_linear_sum_codeword = vec![XFieldElement::zero(); domain.length];
let mut non_linear_sum_codeword = vec![XFieldElement::zero(); quotient_domain.length];

// cross-table arguments
for (arg, weight) in self.into_iter() {
Expand All @@ -473,7 +473,7 @@ impl GrandCrossTableArg {
}

// standard input
let input_terminal_codeword = vec![self.input_terminal; domain.length];
let input_terminal_codeword = vec![self.input_terminal; quotient_domain.length];
let (to_table, to_column) = self.input_to_processor;
let to_codeword = &ext_codeword_tables.data(to_table)[to_column];
let weight = self.input_to_processor_weight;
Expand All @@ -488,7 +488,7 @@ impl GrandCrossTableArg {
// standard output
let (from_table, from_column) = self.processor_to_output;
let from_codeword = &ext_codeword_tables.data(from_table)[from_column];
let output_terminal_codeword = vec![self.output_terminal; domain.length];
let output_terminal_codeword = vec![self.output_terminal; quotient_domain.length];
let weight = self.processor_to_output_weight;
let non_linear_summand =
weighted_difference_codeword(from_codeword, &output_terminal_codeword, weight);
Expand All @@ -499,7 +499,7 @@ impl GrandCrossTableArg {
);

let trace_domain_generator_inverse = trace_domain_generator.inverse();
let zerofier = domain
let zerofier = quotient_domain
.domain_values()
.into_iter()
.map(|x| x - trace_domain_generator_inverse)
Expand Down
20 changes: 8 additions & 12 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,14 @@ impl Stark {
prof_stop!(maybe_profiler, "Merkle tree 3");

// Get indices of slices that go across codewords to prove nonlinear combination
if let Some(profiler) = maybe_profiler.as_mut() {
profiler.start("Fiat-Shamir 3");
}
prof_start!(maybe_profiler, "Fiat-Shamir 3");
let indices_seed = proof_stream.prover_fiat_shamir();
let cross_codeword_slice_indices = StarkHasher::sample_indices(
self.parameters.security_level,
&indices_seed,
self.fri.domain.length,
);
if let Some(profiler) = maybe_profiler.as_mut() {
profiler.stop("Fiat-Shamir 3");
}
prof_stop!(maybe_profiler, "Fiat-Shamir 3");

prof_start!(maybe_profiler, "FRI");
match self.fri.prove(&fri_combination_codeword, &mut proof_stream) {
Expand Down Expand Up @@ -472,8 +468,7 @@ impl Stark {
let mut weights_iterator = weights.into_iter();
let mut combination_codeword: Vec<XFieldElement> = vec![0.into(); quotient_domain.length];

// TODO don't keep the entire domain's values in memory, create them lazily when needed
let domain_values = quotient_domain.domain_values();
let quotient_domain_values = quotient_domain.domain_values();

for (codewords, bounds, identifier) in [
(base_codewords_lifted, base_degree_bounds, "base"),
Expand All @@ -488,7 +483,8 @@ impl Stark {
codewords.into_iter().zip_eq(bounds.iter()).enumerate()
{
let shift = (self.max_degree as Degree - degree_bound) as u32;
let codeword_shifted = Self::shift_codeword(&domain_values, &codeword, shift);
let codeword_shifted =
Self::shift_codeword(&quotient_domain_values, &codeword, shift);

combination_codeword = Self::non_linearly_add_to_codeword(
&combination_codeword,
Expand Down Expand Up @@ -574,11 +570,11 @@ impl Stark {
}

fn shift_codeword(
fri_x_values: &Vec<BFieldElement>,
codeword: &Vec<XFieldElement>,
quotient_domain_values: &[BFieldElement],
codeword: &[XFieldElement],
shift: u32,
) -> Vec<XFieldElement> {
fri_x_values
quotient_domain_values
.par_iter()
.zip_eq(codeword.par_iter())
.map(|(x, &codeword_element)| (codeword_element * x.mod_pow_u32(shift)))
Expand Down
68 changes: 29 additions & 39 deletions triton-vm/src/table/extension_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use twenty_first::shared_math::traits::{FiniteField, Inverse, ModPowU32};
use twenty_first::shared_math::x_field_element::XFieldElement;

use triton_profiler::triton_profiler::TritonProfiler;
use triton_profiler::{prof_start, prof_stop};

use crate::arithmetic_domain::ArithmeticDomain;
use crate::table::extension_table;
Expand Down Expand Up @@ -307,16 +308,16 @@ pub trait Quotientable: ExtensionTable + Evaluable {

fn terminal_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain<BFieldElement>,
transposed_codewords: &[Vec<XFieldElement>],
challenges: &AllChallenges,
trace_domain_generator: BFieldElement,
) -> Vec<Vec<XFieldElement>> {
debug_assert_eq!(domain.length, transposed_codewords.len());
debug_assert_eq!(quotient_domain.length, transposed_codewords.len());

// The zerofier for the terminal quotient has a root in the last
// value in the cyclical group generated from the trace domain's generator.
let zerofier_codeword = domain
let zerofier_codeword = quotient_domain
.domain_values()
.into_iter()
.map(|x| x - trace_domain_generator.inverse())
Expand All @@ -333,63 +334,52 @@ pub trait Quotientable: ExtensionTable + Evaluable {
})
.collect();
let quotient_codewords = transpose(&transposed_quotient_codewords);
self.debug_domain_bound_check(domain, &quotient_codewords, "terminal");
self.debug_domain_bound_check(quotient_domain, &quotient_codewords, "terminal");

quotient_codewords
}

fn all_quotients(
&self,
domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain<BFieldElement>,
transposed_codewords: Vec<Vec<XFieldElement>>,
challenges: &AllChallenges,
trace_domain_generator: BFieldElement,
padded_height: usize,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Vec<Vec<XFieldElement>> {
if let Some(p) = maybe_profiler.as_mut() {
p.start("initial quotients")
}
let initial_quotients = self.initial_quotients(domain, &transposed_codewords, challenges);
if let Some(p) = maybe_profiler.as_mut() {
p.stop("initial quotients")
}

if let Some(p) = maybe_profiler.as_mut() {
p.start("consistency quotients")
}
let consistency_quotients =
self.consistency_quotients(domain, &transposed_codewords, challenges, padded_height);
if let Some(p) = maybe_profiler.as_mut() {
p.stop("consistency quotients")
}
prof_start!(maybe_profiler, "initial quotients");
let initial_quotients =
self.initial_quotients(quotient_domain, &transposed_codewords, challenges);
prof_stop!(maybe_profiler, "initial quotients");

prof_start!(maybe_profiler, "consistency quotients");
let consistency_quotients = self.consistency_quotients(
quotient_domain,
&transposed_codewords,
challenges,
padded_height,
);
prof_stop!(maybe_profiler, "consistency quotients");

if let Some(p) = maybe_profiler.as_mut() {
p.start("transition quotients")
}
prof_start!(maybe_profiler, "transition quotients");
let transition_quotients = self.transition_quotients(
domain,
quotient_domain,
&transposed_codewords,
challenges,
trace_domain_generator,
padded_height,
);
if let Some(p) = maybe_profiler.as_mut() {
p.stop("transition quotients")
}
prof_stop!(maybe_profiler, "transition quotients");

if let Some(p) = maybe_profiler.as_mut() {
p.start("terminal quotients")
}
prof_start!(maybe_profiler, "terminal quotients");
let terminal_quotients = self.terminal_quotients(
domain,
quotient_domain,
&transposed_codewords,
challenges,
trace_domain_generator,
);
if let Some(p) = maybe_profiler.as_mut() {
p.stop("terminal quotients")
}
prof_stop!(maybe_profiler, "terminal quotients");

vec![
initial_quotients,
Expand All @@ -410,24 +400,24 @@ pub trait Quotientable: ExtensionTable + Evaluable {
/// probably the result of un-clean division.
fn debug_domain_bound_check(
&self,
domain: &ArithmeticDomain<BFieldElement>,
quotient_domain: &ArithmeticDomain<BFieldElement>,
quotient_codewords: &[Vec<XFieldElement>],
quotient_type: &str,
) {
if std::env::var("DEBUG").is_err() {
return;
}
for (idx, qc) in quotient_codewords.iter().enumerate() {
let interpolated = domain.interpolate(qc);
let interpolated = quotient_domain.interpolate(qc);
assert!(
interpolated.degree() < domain.length as isize - 1,
interpolated.degree() < quotient_domain.length as isize - 1,
"Degree of {} quotient index {idx} (total {} quotients) in {} must not be maximal. \
Got degree {}, and domain length was {}.",
quotient_type,
quotient_codewords.len(),
self.name(),
interpolated.degree(),
domain.length,
quotient_domain.length,
);
}
}
Expand Down
8 changes: 2 additions & 6 deletions triton-vm/src/table/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ impl ExtTableCollection {

self.into_iter()
.map(|ext_codeword_table| {
if let Some(profiler) = maybe_profiler.as_mut() {
profiler.start(&ext_codeword_table.name());
}
prof_start!(maybe_profiler, &ext_codeword_table.name());
// TODO: Consider if we can use `transposed_ext_codewords` from caller, Stark::prove().
// This would require more complicated indexing, but it would save a lot of allocation.
let transposed_codewords = transpose(ext_codeword_table.data());
Expand All @@ -475,9 +473,7 @@ impl ExtTableCollection {
padded_height,
maybe_profiler,
);
if let Some(profiler) = maybe_profiler.as_mut() {
profiler.stop(&ext_codeword_table.name());
}
prof_stop!(maybe_profiler, &ext_codeword_table.name());
res
})
.concat()
Expand Down

0 comments on commit c85a68e

Please sign in to comment.