Skip to content

Commit

Permalink
auto-generate code for terminal constraints & quotients
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Nov 21, 2022
1 parent 827c822 commit 899f85a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
42 changes: 42 additions & 0 deletions constraint-evaluation-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
&mut ExtProgramTable::ext_initial_constraints_as_circuits(),
&mut ExtProgramTable::ext_consistency_constraints_as_circuits(),
&mut ExtProgramTable::ext_transition_constraints_as_circuits(),
&mut ExtProgramTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -36,6 +37,7 @@ fn main() {
&mut ExtInstructionTable::ext_initial_constraints_as_circuits(),
&mut ExtInstructionTable::ext_consistency_constraints_as_circuits(),
&mut ExtInstructionTable::ext_transition_constraints_as_circuits(),
&mut ExtInstructionTable::ext_terminal_constraints_as_circuits(),
);
write(tabe_name_snake, source_code);

Expand All @@ -45,6 +47,7 @@ fn main() {
&mut ExtProcessorTable::ext_initial_constraints_as_circuits(),
&mut ExtProcessorTable::ext_consistency_constraints_as_circuits(),
&mut ExtProcessorTable::ext_transition_constraints_as_circuits(),
&mut ExtProcessorTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -54,6 +57,7 @@ fn main() {
&mut ExtOpStackTable::ext_initial_constraints_as_circuits(),
&mut ExtOpStackTable::ext_consistency_constraints_as_circuits(),
&mut ExtOpStackTable::ext_transition_constraints_as_circuits(),
&mut ExtOpStackTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -63,6 +67,7 @@ fn main() {
&mut ExtRamTable::ext_initial_constraints_as_circuits(),
&mut ExtRamTable::ext_consistency_constraints_as_circuits(),
&mut ExtRamTable::ext_transition_constraints_as_circuits(),
&mut ExtRamTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -72,6 +77,7 @@ fn main() {
&mut ExtJumpStackTable::ext_initial_constraints_as_circuits(),
&mut ExtJumpStackTable::ext_consistency_constraints_as_circuits(),
&mut ExtJumpStackTable::ext_transition_constraints_as_circuits(),
&mut ExtJumpStackTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -81,6 +87,7 @@ fn main() {
&mut ExtHashTable::ext_initial_constraints_as_circuits(),
&mut ExtHashTable::ext_consistency_constraints_as_circuits(),
&mut ExtHashTable::ext_transition_constraints_as_circuits(),
&mut ExtHashTable::ext_terminal_constraints_as_circuits(),
);
write(table_name_snake, source_code);

Expand All @@ -101,6 +108,7 @@ fn gen<T: TableChallenges, SII: InputIndicator, DII: InputIndicator>(
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>],
) -> String {
let table_id_name = table_name_snake.to_upper_camel_case();
let challenge_enum_name = format!("{table_id_name}ChallengeId");
Expand All @@ -112,10 +120,13 @@ fn gen<T: TableChallenges, SII: InputIndicator, DII: InputIndicator>(
turn_circuits_into_degree_bounds_string(consistency_constraint_circuits);
let transition_constraints_degrees =
turn_circuits_into_degree_bounds_string(transition_constraint_circuits);
let terminal_constraints_degrees =
turn_circuits_into_degree_bounds_string(terminal_constraint_circuits);

let initial_constraint_strings = turn_circuits_into_string(initial_constraint_circuits);
let consistency_constraint_strings = turn_circuits_into_string(consistency_constraint_circuits);
let transition_constraint_strings = turn_circuits_into_string(transition_constraint_circuits);
let terminal_constraint_strings = turn_circuits_into_string(terminal_constraint_circuits);

// maybe-prefixes to supress clippy's warnings for unused variables
let initial_challenges_used = if initial_constraint_strings.contains("challenges") {
Expand All @@ -128,11 +139,21 @@ fn gen<T: TableChallenges, SII: InputIndicator, DII: InputIndicator>(
} else {
"_"
};
let terminal_challenges_used = if terminal_constraint_strings.contains("challenges") {
""
} else {
"_"
};
let consistency_constraints_exist = if consistency_constraints_degrees.is_empty() {
"_"
} else {
""
};
let terminal_constraints_exist = if terminal_constraints_degrees.is_empty() {
"_"
} else {
""
};

format!(
"
Expand Down Expand Up @@ -182,6 +203,16 @@ impl Evaluable for {table_mod_name} {{
let challenges = &challenges.{table_name_snake}_challenges;
{transition_constraint_strings}
}}
#[inline]
fn evaluate_terminal_constraints(
&self,
{terminal_constraints_exist}row: &[XFieldElement],
challenges: &AllChallenges,
) -> Vec<XFieldElement> {{
let {terminal_challenges_used}challenges = &challenges.{table_name_snake}_challenges;
{terminal_constraint_strings}
}}
}}
impl Quotientable for {table_mod_name} {{
Expand Down Expand Up @@ -215,6 +246,17 @@ impl Quotientable for {table_mod_name} {{
let interpolant_degree = interpolant_degree(padded_height, num_trace_randomizers);
[{transition_constraints_degrees}].to_vec()
}}
fn get_terminal_quotient_degree_bounds(
&self,
padded_height: usize,
num_trace_randomizers: usize,
) -> Vec<Degree> {{
let {terminal_constraints_exist}zerofier_degree = 1 as Degree;
let {terminal_constraints_exist}interpolant_degree =
interpolant_degree(padded_height, num_trace_randomizers);
[{terminal_constraints_degrees}].to_vec()
}}
}}
"
)
Expand Down
13 changes: 11 additions & 2 deletions triton-vm/src/table/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ impl ExtHashTable {
.collect()
}

pub fn ext_terminal_constraints_as_circuits(
) -> Vec<ConstraintCircuit<HashTableChallenges, SingleRowIndicator<FULL_WIDTH>>> {
// no more constraints
vec![]
}

fn ext_initial_constraints(
challenges: &HashTableChallenges,
) -> Vec<MPolynomial<XFieldElement>> {
Expand Down Expand Up @@ -484,9 +490,12 @@ impl ExtHashTable {
}

fn ext_terminal_constraints(
_challenges: &HashTableChallenges,
challenges: &HashTableChallenges,
) -> Vec<MPolynomial<XFieldElement>> {
vec![]
Self::ext_terminal_constraints_as_circuits()
.into_iter()
.map(|circuit| circuit.partial_evaluate(challenges))
.collect_vec()
}
}

Expand Down

0 comments on commit 899f85a

Please sign in to comment.