Skip to content

Commit

Permalink
simplify handling of challenges
Browse files Browse the repository at this point in the history
Fix #90.
  • Loading branch information
jan-ferdinand committed Feb 9, 2023
1 parent 022245b commit b02779c
Show file tree
Hide file tree
Showing 14 changed files with 964 additions and 2,610 deletions.
57 changes: 23 additions & 34 deletions constraint-evaluation-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use itertools::Itertools;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::x_field_element::XFieldElement;

use triton_vm::table::challenges::TableChallenges;
use triton_vm::table::constraint_circuit::CircuitExpression;
use triton_vm::table::constraint_circuit::ConstraintCircuit;
use triton_vm::table::constraint_circuit::InputIndicator;
Expand Down Expand Up @@ -124,15 +123,14 @@ fn write(table_name_snake: &str, rust_source_code: String) {
std::fs::write(output_filename, rust_source_code).expect("Write Rust source code");
}

fn gen<T: TableChallenges, SII: InputIndicator, DII: InputIndicator>(
fn gen<SII: InputIndicator, DII: InputIndicator>(
table_name_snake: &str,
table_id_name: &str,
initial_constraint_circuits: &mut [ConstraintCircuit<T, SII>],
consistency_constraint_circuits: &mut [ConstraintCircuit<T, SII>],
transition_constraint_circuits: &mut [ConstraintCircuit<T, DII>],
terminal_constraint_circuits: &mut [ConstraintCircuit<T, SII>],
initial_constraint_circuits: &mut [ConstraintCircuit<SII>],
consistency_constraint_circuits: &mut [ConstraintCircuit<SII>],
transition_constraint_circuits: &mut [ConstraintCircuit<DII>],
terminal_constraint_circuits: &mut [ConstraintCircuit<SII>],
) -> String {
let challenge_enum_name = format!("{table_id_name}ChallengeId");
let table_mod_name = format!("Ext{table_id_name}");

let num_initial_constraints = initial_constraint_circuits.len();
Expand Down Expand Up @@ -161,12 +159,11 @@ use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::mpolynomial::Degree;
use twenty_first::shared_math::x_field_element::XFieldElement;
use crate::table::challenges::AllChallenges;
use crate::table::challenges::TableChallenges;
use crate::table::challenges::Challenges;
use crate::table::challenges::ChallengeId::*;
use crate::table::extension_table::Evaluable;
use crate::table::extension_table::Quotientable;
use crate::table::{table_name_snake}::{table_mod_name};
use crate::table::{table_name_snake}::{challenge_enum_name}::*;
// This file has been auto-generated. Any modifications _will_ be lost.
// To re-generate, execute:
Expand All @@ -177,9 +174,8 @@ impl Evaluable for {table_mod_name} {{
fn evaluate_initial_constraints(
base_row: ArrayView1<BFieldElement>,
ext_row: ArrayView1<XFieldElement>,
challenges: &AllChallenges,
challenges: &Challenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{initial_constraint_strings}
}}
Expand All @@ -188,9 +184,8 @@ impl Evaluable for {table_mod_name} {{
fn evaluate_consistency_constraints(
base_row: ArrayView1<BFieldElement>,
ext_row: ArrayView1<XFieldElement>,
challenges: &AllChallenges,
challenges: &Challenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{consistency_constraint_strings}
}}
Expand All @@ -201,9 +196,8 @@ impl Evaluable for {table_mod_name} {{
current_ext_row: ArrayView1<XFieldElement>,
next_base_row: ArrayView1<BFieldElement>,
next_ext_row: ArrayView1<XFieldElement>,
challenges: &AllChallenges,
challenges: &Challenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{transition_constraint_strings}
}}
Expand All @@ -212,9 +206,8 @@ impl Evaluable for {table_mod_name} {{
fn evaluate_terminal_constraints(
base_row: ArrayView1<BFieldElement>,
ext_row: ArrayView1<XFieldElement>,
challenges: &AllChallenges,
challenges: &Challenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{terminal_constraint_strings}
}}
}}
Expand Down Expand Up @@ -274,8 +267,8 @@ impl Quotientable for {table_mod_name} {{
)
}

fn turn_circuits_into_degree_bounds_string<T: TableChallenges, II: InputIndicator>(
constraint_circuits: &[ConstraintCircuit<T, II>],
fn turn_circuits_into_degree_bounds_string<II: InputIndicator>(
constraint_circuits: &[ConstraintCircuit<II>],
) -> String {
constraint_circuits
.iter()
Expand All @@ -284,8 +277,8 @@ fn turn_circuits_into_degree_bounds_string<T: TableChallenges, II: InputIndicato
.join(",\n")
}

fn turn_circuits_into_string<T: TableChallenges, II: InputIndicator>(
constraint_circuits: &mut [ConstraintCircuit<T, II>],
fn turn_circuits_into_string<II: InputIndicator>(
constraint_circuits: &mut [ConstraintCircuit<II>],
) -> String {
// Delete redundant nodes
ConstraintCircuit::constant_folding(&mut constraint_circuits.iter_mut().collect_vec());
Expand Down Expand Up @@ -356,9 +349,9 @@ fn turn_circuits_into_string<T: TableChallenges, II: InputIndicator>(
/// Produce the code to evaluate code for all nodes that share a value number of
/// times visited. A value for all nodes with a higher count than the provided are assumed
/// to be in scope.
fn declare_nodes_with_visit_count<T: TableChallenges, II: InputIndicator>(
fn declare_nodes_with_visit_count<II: InputIndicator>(
requested_visited_count: usize,
circuits: &[ConstraintCircuit<T, II>],
circuits: &[ConstraintCircuit<II>],
) -> String {
let mut in_scope: HashSet<usize> = HashSet::new();
let mut output = String::default();
Expand All @@ -375,9 +368,9 @@ fn declare_nodes_with_visit_count<T: TableChallenges, II: InputIndicator>(
output
}

fn declare_single_node_with_visit_count<T: TableChallenges, II: InputIndicator>(
fn declare_single_node_with_visit_count<II: InputIndicator>(
requested_visited_count: usize,
circuit: &ConstraintCircuit<T, II>,
circuit: &ConstraintCircuit<II>,
in_scope: &mut HashSet<usize>,
output: &mut String,
) {
Expand Down Expand Up @@ -433,9 +426,7 @@ fn declare_single_node_with_visit_count<T: TableChallenges, II: InputIndicator>(

/// Return a variable name for the node. Returns `point[n]` if node is just
/// a value from the codewords. Otherwise returns the ID of the circuit.
fn get_binding_name<T: TableChallenges, II: InputIndicator>(
circuit: &ConstraintCircuit<T, II>,
) -> String {
fn get_binding_name<II: InputIndicator>(circuit: &ConstraintCircuit<II>) -> String {
match &circuit.expression {
CircuitExpression::XConstant(xfe) => print_xfe(xfe),
CircuitExpression::BConstant(bfe) => print_bfe(bfe),
Expand All @@ -450,9 +441,7 @@ fn get_binding_name<T: TableChallenges, II: InputIndicator>(
/// Recursively check whether a node is composed of only BFieldElements, i.e., only uses
/// (1) inputs from base rows, (2) constants from the B-field, and (3) binary operations on
/// BFieldElements.
fn is_bfield_element<T: TableChallenges, II: InputIndicator>(
circuit: &ConstraintCircuit<T, II>,
) -> bool {
fn is_bfield_element<II: InputIndicator>(circuit: &ConstraintCircuit<II>) -> bool {
match &circuit.expression {
CircuitExpression::XConstant(_) => false,
CircuitExpression::BConstant(_) => true,
Expand All @@ -466,9 +455,9 @@ fn is_bfield_element<T: TableChallenges, II: InputIndicator>(

/// Return (1) the code for evaluating a single node and (2) a list of symbols that this evaluation
/// depends on.
fn evaluate_single_node<T: TableChallenges, II: InputIndicator>(
fn evaluate_single_node<II: InputIndicator>(
requested_visited_count: usize,
circuit: &ConstraintCircuit<T, II>,
circuit: &ConstraintCircuit<II>,
in_scope: &HashSet<usize>,
) -> (String, Vec<String>) {
let mut output = String::default();
Expand Down
42 changes: 21 additions & 21 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::proof::Claim;
use crate::proof::Proof;
use crate::proof_item::ProofItem;
use crate::proof_stream::ProofStream;
use crate::table::challenges::AllChallenges;
use crate::table::challenges::Challenges;
use crate::table::master_table::*;
use crate::vm::AlgebraicExecutionTrace;

Expand Down Expand Up @@ -180,13 +180,10 @@ impl Stark {
proof_stream.enqueue(&ProofItem::MerkleRoot(base_merkle_tree_root));
let extension_weights = Self::sample_weights(
proof_stream.prover_fiat_shamir(),
AllChallenges::TOTAL_CHALLENGES,
);
let extension_challenges = AllChallenges::create_challenges(
extension_weights,
&self.claim.input,
&self.claim.output,
Challenges::num_challenges_to_sample(),
);
let extension_challenges =
Challenges::new(extension_weights, &self.claim.input, &self.claim.output);
prof_stop!(maybe_profiler, "Fiat-Shamir");

prof_start!(maybe_profiler, "extend");
Expand Down Expand Up @@ -537,9 +534,11 @@ impl Stark {
let base_merkle_tree_root = proof_stream.dequeue()?.as_merkle_root()?;

let extension_challenge_seed = proof_stream.verifier_fiat_shamir();
let extension_challenge_weights =
Self::sample_weights(extension_challenge_seed, AllChallenges::TOTAL_CHALLENGES);
let challenges = AllChallenges::create_challenges(
let extension_challenge_weights = Self::sample_weights(
extension_challenge_seed,
Challenges::num_challenges_to_sample(),
);
let challenges = Challenges::new(
extension_challenge_weights,
&self.claim.input,
&self.claim.output,
Expand Down Expand Up @@ -872,11 +871,14 @@ pub(crate) mod triton_stark_tests {
use num_traits::Zero;
use rand::prelude::ThreadRng;
use rand_core::RngCore;

use triton_opcodes::instruction::AnInstruction;
use triton_opcodes::program::Program;

use crate::shared_tests::*;
use crate::table::challenges::ChallengeId::StandardInputIndeterminate;
use crate::table::challenges::ChallengeId::StandardInputTerminal;
use crate::table::challenges::ChallengeId::StandardOutputIndeterminate;
use crate::table::challenges::ChallengeId::StandardOutputTerminal;
use crate::table::cross_table_argument::CrossTableArg;
use crate::table::cross_table_argument::EvalArg;
use crate::table::cross_table_argument::GrandCrossTableArg;
Expand Down Expand Up @@ -964,12 +966,12 @@ pub(crate) mod triton_stark_tests {
MasterBaseTable,
MasterBaseTable,
MasterExtTable,
AllChallenges,
Challenges,
) {
let (stark, unpadded_master_base_table, master_base_table) =
parse_simulate_pad(code, stdin, secret_in);

let dummy_challenges = AllChallenges::placeholder(&stark.claim.input, &stark.claim.output);
let dummy_challenges = Challenges::placeholder(&stark.claim.input, &stark.claim.output);
let master_ext_table = master_base_table.extend(
&dummy_challenges,
stark.parameters.num_randomizer_polynomials,
Expand Down Expand Up @@ -1106,17 +1108,15 @@ pub(crate) mod triton_stark_tests {
let ine = EvalArg::compute_terminal(
&stark.claim.input,
EvalArg::default_initial(),
all_challenges.input_challenges.processor_eval_indeterminate,
all_challenges.get_challenge(StandardInputIndeterminate),
);
assert_eq!(ptie, ine, "The input evaluation arguments do not match.");

let ptoe = processor_table_last_row[OutputTableEvalArg.ext_table_index()];
let oute = EvalArg::compute_terminal(
&stark.claim.output,
EvalArg::default_initial(),
all_challenges
.output_challenges
.processor_eval_indeterminate,
all_challenges.get_challenge(StandardOutputIndeterminate),
);
assert_eq!(ptoe, oute, "The output evaluation arguments do not match.");
}
Expand All @@ -1137,12 +1137,12 @@ pub(crate) mod triton_stark_tests {
let processor_table = master_ext_table.table(ProcessorTable);
let processor_table_last_row = processor_table.slice(s![-1, ..]);
assert_eq!(
all_challenges.cross_table_challenges.input_terminal,
all_challenges.get_challenge(StandardInputTerminal),
processor_table_last_row[InputTableEvalArg.ext_table_index()],
"The input terminal must match for TASM snippet #{code_idx}."
);
assert_eq!(
all_challenges.cross_table_challenges.output_terminal,
all_challenges.get_challenge(StandardOutputTerminal),
processor_table_last_row[OutputTableEvalArg.ext_table_index()],
"The output terminal must match for TASM snippet #{code_idx}."
);
Expand Down Expand Up @@ -1170,7 +1170,7 @@ pub(crate) mod triton_stark_tests {

#[test]
fn constraint_polynomials_use_right_variable_count_test() {
let challenges = AllChallenges::placeholder(&[], &[]);
let challenges = Challenges::placeholder(&[], &[]);
let base_row = Array1::zeros(NUM_BASE_COLUMNS);
let ext_row = Array1::zeros(NUM_EXT_COLUMNS);

Expand Down Expand Up @@ -1294,7 +1294,7 @@ pub(crate) mod triton_stark_tests {
fn number_of_quotient_degree_bounds_match_number_of_constraints_test() {
let base_row = Array1::zeros(NUM_BASE_COLUMNS);
let ext_row = Array1::zeros(NUM_EXT_COLUMNS);
let challenges = AllChallenges::placeholder(&[], &[]);
let challenges = Challenges::placeholder(&[], &[]);
let padded_height = 2;
let num_trace_randomizers = 2;
let interpolant_degree = interpolant_degree(padded_height, num_trace_randomizers);
Expand Down
Loading

0 comments on commit b02779c

Please sign in to comment.