Skip to content

Commit

Permalink
WIP: produce initial constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Nov 16, 2022
1 parent 1cacc95 commit 825f36e
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions constraint-evaluation-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,57 @@ fn main() {
println!("Generate those constraint evaluators!");

// Program table
let mut circuits = ExtProgramTable::ext_transition_constraints_as_circuits();
let source_code = gen("program_table", "ProgramTable", &mut circuits);
let source_code = gen(
"program_table",
"ProgramTable",
&mut ExtProgramTable::ext_initial_constraints_as_circuits(),
&mut ExtProgramTable::ext_transition_constraints_as_circuits(),
);
write("program_table", source_code);

// Instruction table
let mut circuits = ExtInstructionTable::ext_transition_constraints_as_circuits();
let source_code = gen("instruction_table", "InstructionTable", &mut circuits);
let source_code = gen(
"instruction_table",
"InstructionTable",
&mut ExtInstructionTable::ext_initial_constraints_as_circuits(),
&mut ExtInstructionTable::ext_transition_constraints_as_circuits(),
);
write("instruction_table", source_code);

// Processor table
let mut circuits = ExtProcessorTable::ext_transition_constraints_as_circuits();
let source_code = gen("processor_table", "ProcessorTable", &mut circuits);
let source_code = gen(
"processor_table",
"ProcessorTable",
&mut ExtProcessorTable::ext_initial_constraints_as_circuits(),
&mut ExtProcessorTable::ext_transition_constraints_as_circuits(),
);
write("processor_table", source_code);

// Opstack table
let mut constraint_circuits = ExtOpStackTable::ext_transition_constraints_as_circuits();
let source_code = gen("op_stack_table", "OpStackTable", &mut constraint_circuits);
let source_code = gen(
"op_stack_table",
"OpStackTable",
&mut ExtOpStackTable::ext_initial_constraints_as_circuits(),
&mut ExtOpStackTable::ext_transition_constraints_as_circuits(),
);
write("op_stack_table", source_code);

// RAM table
let mut circuits = ExtRamTable::ext_transition_constraints_as_circuits();
let source_code = gen("ram_table", "RamTable", &mut circuits);
let source_code = gen(
"ram_table",
"RamTable",
&mut ExtRamTable::ext_initial_constraints_as_circuits(),
&mut ExtRamTable::ext_transition_constraints_as_circuits(),
);
write("ram_table", source_code);

// JumpStack table
let mut circuits = ExtJumpStackTable::ext_transition_constraints_as_circuits();
let source_code = gen("jump_stack_table", "JumpStackTable", &mut circuits);
let source_code = gen(
"jump_stack_table",
"JumpStackTable",
&mut ExtJumpStackTable::ext_initial_constraints_as_circuits(),
&mut ExtJumpStackTable::ext_transition_constraints_as_circuits(),
);
write("jump_stack_table", source_code);
}

Expand All @@ -55,15 +79,17 @@ 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, II: InputIndicator>(
fn gen<T: TableChallenges, SII: InputIndicator, DII: InputIndicator>(
table_name_snake: &str,
table_id_name: &str,
constraint_circuits: &mut [ConstraintCircuit<T, II>],
initial_constraint_circuits: &mut [ConstraintCircuit<T, SII>],
transition_constraint_circuits: &mut [ConstraintCircuit<T, DII>],
) -> String {
let challenge_enum_name = format!("{table_id_name}ChallengeId");
let table_mod_name = format!("Ext{table_id_name}");

let circuits_as_string = turn_circuits_into_string(constraint_circuits);
let initial_constraint_strings = turn_circuits_into_string(initial_constraint_circuits);
let transition_constraint_strings = turn_circuits_into_string(transition_constraint_circuits);

format!(
"
Expand All @@ -77,6 +103,16 @@ use crate::table::{table_name_snake}::{table_mod_name};
use crate::table::{table_name_snake}::{challenge_enum_name}::*;
impl Evaluable for {table_mod_name} {{
#[inline]
fn evaluate_initial_constraints(
&self,
row: &[XFieldElement],
challenges: &AllChallenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{initial_constraint_strings}
}}
#[inline]
fn evaluate_transition_constraints(
&self,
Expand All @@ -85,7 +121,7 @@ impl Evaluable for {table_mod_name} {{
challenges: &AllChallenges,
) -> Vec<XFieldElement> {{
let challenges = &challenges.{table_name_snake}_challenges;
{circuits_as_string}
{transition_constraint_strings}
}}
}}
"
Expand Down

0 comments on commit 825f36e

Please sign in to comment.