Skip to content

Commit

Permalink
Merge pull request #311 from Sacul231/master
Browse files Browse the repository at this point in the history
Adding improvements circom 2.2.1
  • Loading branch information
clararod9 authored Nov 12, 2024
2 parents d199164 + faf69bd commit 63b0f37
Show file tree
Hide file tree
Showing 29 changed files with 1,440 additions and 940 deletions.
15 changes: 15 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# Release notes
## November 12, 2024 circom 2.2.1
#### Improvements:
- Improving the use and heritance of tags inside buses. Now the values are propagated correctly following the same rules as arrays.
- Unassigned inputs: do not executing the circuit twice in C++.
- Allowing tag accesses of subcomponent input/output signals.
- Handling equality checks with dynamic size in witness generation code.
- Improving error messages.
- Improving error recovery in parser.
- Adding flag --constraint_assert_dissabled. When this flag is activated the compiler does not add asserts in the generated code (C++, WASM) for === constraint equalities


#### Fixed bugs:
- Importing function printDebug removed from WASM (circom tests from circomlib working now).


## October 07, 2024 circom 2.2.0
#### New features
- Buses: more information [here](https://github.com/iden3/circom/blob/master/mkdocs/docs/circom-language/buses.md).
Expand Down
8 changes: 7 additions & 1 deletion circom/src/compilation_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct CompilerConfig {
pub c_flag: bool,
pub debug_output: bool,
pub produce_input_log: bool,
pub constraint_assert_dissabled_flag: bool,
pub vcp: VCP,
}

Expand All @@ -30,7 +31,12 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
if config.c_flag || config.wat_flag || config.wasm_flag{
let circuit = compiler_interface::run_compiler(
config.vcp,
Config { debug_output: config.debug_output, produce_input_log: config.produce_input_log, wat_flag: config.wat_flag },
Config {
debug_output: config.debug_output,
produce_input_log: config.produce_input_log,
wat_flag: config.wat_flag,
constraint_assert_dissabled_flag: config.constraint_assert_dissabled_flag,
},
VERSION
)?;

Expand Down
17 changes: 17 additions & 0 deletions circom/src/input_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Input {
pub fast_flag: bool,
pub reduced_simplification_flag: bool,
pub parallel_simplification_flag: bool,
pub constraint_assert_dissabled_flag: bool,
pub flag_old_heuristics: bool,
pub inspect_constraints_flag: bool,
pub no_rounds: usize,
Expand Down Expand Up @@ -101,6 +102,7 @@ impl Input {
fast_flag: o_style == SimplificationStyle::O0,
reduced_simplification_flag: o_style == SimplificationStyle::O1,
parallel_simplification_flag: input_processing::get_parallel_simplification(&matches),
constraint_assert_dissabled_flag: input_processing::get_constraint_assert_dissabled(&matches),
inspect_constraints_flag: input_processing::get_inspect_constraints(&matches),
flag_old_heuristics: input_processing::get_flag_old_heuristics(&matches),
flag_verbose: input_processing::get_flag_verbose(&matches),
Expand Down Expand Up @@ -209,6 +211,9 @@ impl Input {
pub fn parallel_simplification_flag(&self) -> bool {
self.parallel_simplification_flag
}
pub fn constraint_assert_dissabled_flag(&self) -> bool {
self.constraint_assert_dissabled_flag
}
pub fn flag_old_heuristics(&self) -> bool {
self.flag_old_heuristics
}
Expand Down Expand Up @@ -304,6 +309,10 @@ mod input_processing {
matches.is_present("parallel_simplification")
}

pub fn get_constraint_assert_dissabled(matches: &ArgMatches) -> bool {
matches.is_present("constraint_assert_dissabled")
}

pub fn get_ir(matches: &ArgMatches) -> bool {
matches.is_present("print_ir")
}
Expand Down Expand Up @@ -477,6 +486,14 @@ mod input_processing {
.display_order(180)
.help("Runs non-linear simplification in parallel"),
)
.arg(
Arg::with_name("constraint_assert_dissabled")
.long("constraint_assert_dissabled")
.takes_value(false)
.hidden(false)
.display_order(810)
.help("Does not add asserts in the generated code for === constraint equalities"),
)
.arg(
Arg::with_name("main_inputs_log")
.long("inputs")
Expand Down
1 change: 1 addition & 0 deletions circom/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn start() -> Result<(), ()> {
wat_file: user_input.wat_file().to_string(),
wasm_file: user_input.wasm_file().to_string(),
produce_input_log: user_input.main_inputs_flag(),
constraint_assert_dissabled_flag: user_input.constraint_assert_dissabled_flag()
};
compilation_user::compile(compilation_config)?;
Result::Ok(())
Expand Down
14 changes: 9 additions & 5 deletions compiler/src/circuit_design/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn build_template_instances(
c_info: &CircuitInfo,
ti: Vec<TemplateInstance>,
mut field_tracker: FieldTracker,
constraint_assert_dissabled_flag: bool
) -> (FieldTracker, HashMap<String,usize>) {

fn compute_jump(lengths: &Vec<usize>, indexes: &[usize]) -> usize {
Expand Down Expand Up @@ -99,6 +100,7 @@ fn build_template_instances(
template_database: &c_info.template_database,
string_table : string_table,
signals_to_tags: template.signals_to_tags,
constraint_assert_dissabled_flag
};
let mut template_info = TemplateCodeInfo {
name,
Expand Down Expand Up @@ -134,7 +136,8 @@ fn build_function_instances(
c_info: &CircuitInfo,
instances: Vec<VCF>,
mut field_tracker: FieldTracker,
mut string_table : HashMap<String,usize>
mut string_table : HashMap<String,usize>,
constraint_assert_dissabled_flag: bool,
) -> (FieldTracker, HashMap<String, usize>, HashMap<String, usize>) {
let mut function_to_arena_size = HashMap::new();
for instance in instances {
Expand Down Expand Up @@ -162,8 +165,9 @@ fn build_function_instances(
component_to_parallel: HashMap::with_capacity(0),
template_database: &c_info.template_database,
string_table : string_table,
signals_to_tags: BTreeMap::new(),
buses: &c_info.buses
signals_to_tags: HashMap::new(),
buses: &c_info.buses,
constraint_assert_dissabled_flag
};
let mut function_info = FunctionCodeInfo {
name,
Expand Down Expand Up @@ -598,9 +602,9 @@ pub fn build_circuit(vcp: VCP, flag: CompilationFlags, version: &str) -> Circuit
};

let (field_tracker, string_table) =
build_template_instances(&mut circuit, &circuit_info, vcp.templates, field_tracker);
build_template_instances(&mut circuit, &circuit_info, vcp.templates, field_tracker, flag.constraint_assert_dissabled_flag);
let (field_tracker, function_to_arena_size, table_string_to_usize) =
build_function_instances(&mut circuit, &circuit_info, vcp.functions, field_tracker,string_table);
build_function_instances(&mut circuit, &circuit_info, vcp.functions, field_tracker,string_table, flag.constraint_assert_dissabled_flag);

let table_usize_to_string = create_table_usize_to_string(table_string_to_usize);
circuit.wasm_producer.set_string_table(table_usize_to_string.clone());
Expand Down
1 change: 1 addition & 0 deletions compiler/src/circuit_design/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::io::Write;
pub struct CompilationFlags {
pub main_inputs_log: bool,
pub wat_flag:bool,
pub constraint_assert_dissabled_flag: bool
}

pub struct Circuit {
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/circuit_design/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ impl TemplateCodeInfo {
run_body.push(format!("{};", declare_lvar(self.var_stack_depth)));
run_body.push(format!("{};", declare_sub_component_aux()));
run_body.push(format!("{};", declare_index_multiple_eq()));
run_body.push(format!("int cmp_index_ref_load = -1;"));



for t in &self.body {
let (mut instructions_body, _) = t.produce_c(producer, Some(parallel));
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ pub struct Config {
pub debug_output: bool,
pub produce_input_log: bool,
pub wat_flag: bool,
pub constraint_assert_dissabled_flag: bool
}

pub fn run_compiler(vcp: VCP, config: Config, version: &str) -> Result<Circuit, ()> {
let flags = CompilationFlags { main_inputs_log: config.produce_input_log, wat_flag: config.wat_flag };
let flags = CompilationFlags {
main_inputs_log: config.produce_input_log,
wat_flag: config.wat_flag,
constraint_assert_dissabled_flag: config.constraint_assert_dissabled_flag
};
let circuit = Circuit::build(vcp, flags, version);
if config.debug_output {
produce_debug_output(&circuit)?;
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/hir/very_concrete_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub struct TemplateInstance {
pub number_of_outputs: usize,
pub number_of_intermediates: usize,
pub wires: Vec<Wire>,
pub signals_to_tags: BTreeMap<String, TagInfo>,
pub signals_to_tags: HashMap<Vec<String>, BigInt>,
pub components: Vec<Component>,
pub number_of_components: usize,
pub triggers: Vec<Trigger>,
Expand All @@ -216,7 +216,7 @@ pub struct TemplateConfig {
pub clusters: Vec<TriggerCluster>,
pub components: Vec<Component>,
pub arguments: Vec<Argument>,
pub signals_to_tags: BTreeMap<String, TagInfo>,
pub signals_to_tags: HashMap<Vec<String>, BigInt>,
}
impl TemplateInstance {
pub fn new(config: TemplateConfig) -> TemplateInstance {
Expand Down
68 changes: 52 additions & 16 deletions compiler/src/intermediate_representation/compute_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::translating_traits::*;
use code_producers::c_elements::*;
use code_producers::wasm_elements::*;

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq)]
pub enum OperatorType {
Mul,
Div,
Expand All @@ -18,7 +18,7 @@ pub enum OperatorType {
GreaterEq,
Lesser,
Greater,
Eq(usize),
Eq(SizeOption),
NotEq,
BoolOr,
BoolAnd,
Expand All @@ -42,7 +42,8 @@ impl OperatorType {

pub fn is_multiple_eq(&self) -> bool {
match self {
OperatorType::Eq(n) => *n > 1,
OperatorType::Eq(SizeOption::Single(n)) => *n > 1,
OperatorType::Eq(SizeOption::Multiple(_)) => true,
_ => false
}
}
Expand All @@ -52,7 +53,7 @@ impl ToString for OperatorType {
fn to_string(&self) -> String {
use OperatorType::*;
if let Eq(n) = self {
format!("EQ({})", n)
format!("EQ({:?})", n)
} else {
match self {
Mul => "MUL",
Expand Down Expand Up @@ -170,7 +171,7 @@ impl WriteWasm for ComputeBucket {
instructions.push(call("$Fr_toInt"));
}
_ => {
match self.op {
match &self.op {
OperatorType::Add => {
instructions.push(call("$Fr_add")); // Result, Argument, Argument
}
Expand Down Expand Up @@ -211,15 +212,31 @@ impl WriteWasm for ComputeBucket {
instructions.push(call("$Fr_gt"));
}
OperatorType::Eq(n) => {
assert!(n != 0);
if n == 1 {
let mut is_multiple = false;
let (length,values) = match n{
SizeOption::Single(v) => (*v,vec![]),
SizeOption::Multiple(v) => {
is_multiple = true;
(v.len(),v.clone())
}
};
assert!(length != 0);
if !is_multiple && length == 1 {
instructions.push(call("$Fr_eq"));
} else {
instructions.push(set_local(producer.get_aux_2_tag()));
instructions.push(set_local(producer.get_aux_1_tag()));
instructions.push(set_local(producer.get_aux_0_tag()));
instructions.push(set_constant(&n.to_string()));
if is_multiple { //create a nested if-else with all cases
instructions.push(get_local(producer.get_sub_cmp_load_tag()));
instructions.push(load32(None)); // get template id
instructions.push(set_local(producer.get_aux_0_tag()));
let mut instr_if = create_if_selection(&values,producer.get_aux_0_tag());
instructions.append(&mut instr_if);
} else {
instructions.push(set_constant(&length.to_string()));
}
instructions.push(set_local(producer.get_counter_tag()));
instructions.push(set_local(producer.get_aux_2_tag())); // second argument initial position
instructions.push(set_local(producer.get_aux_1_tag())); // first argument initial position
instructions.push(set_local(producer.get_aux_0_tag())); // resut position
instructions.push(add_block());
instructions.push(add_loop());
instructions.push(get_local(producer.get_aux_0_tag()));
Expand Down Expand Up @@ -295,7 +312,7 @@ impl WriteWasm for ComputeBucket {
impl WriteC for ComputeBucket {
fn produce_c(&self, producer: &CProducer, parallel: Option<bool>) -> (Vec<String>, String) {
use c_code_generator::*;
fn get_fr_op(op_type: OperatorType) -> String {
fn get_fr_op(op_type: &OperatorType) -> String {
match op_type {
OperatorType::Add => "Fr_add".to_string(),
OperatorType::Div => "Fr_div".to_string(),
Expand Down Expand Up @@ -346,15 +363,32 @@ impl WriteC for ComputeBucket {

OperatorType::Eq(n) => {
let exp_aux_index = self.op_aux_no.to_string();
let operator = get_fr_op(self.op);
let operator = get_fr_op(&self.op);
let result_ref = format!("&{}", expaux(exp_aux_index.clone()));
let mut arguments = vec![result_ref.clone()];
let operands_copy = operands.clone();
arguments.append(&mut operands);
compute_c.push("{{".to_string());
compute_c.push(format!("{}; // line circom {}", build_call(operator.clone(), arguments),self.line.to_string()));
if *n > 1 {

// We compute the possible sizes, case multiple sizes
let expr_size = match &n{
SizeOption::Single(value) => value.to_string(),
SizeOption::Multiple(values) => {
let cmp_index_ref = "cmp_index_ref_load".to_string();

compute_c.push(format!("std::map<int,int> size_eq {};",
set_list_tuple(values.clone())
));
let sub_component_pos_in_memory = format!("{}[{}]", MY_SUBCOMPONENTS, cmp_index_ref);
let temp_id = template_id_in_component(sub_component_pos_in_memory);
format!("size_eq[{}]", temp_id)
}
};

if expr_size != "1" {
compute_c.push(format!("{} = 1;", index_multiple_eq()));
compute_c.push(format!("while({} < {} && Fr_isTrue({})) {{", index_multiple_eq(), n, result_ref));
compute_c.push(format!("while({} < {} && Fr_isTrue({})) {{", index_multiple_eq(), expr_size, result_ref));
operands = vec![];
arguments = vec![result_ref.clone()];
for operand in &operands_copy {
Expand All @@ -366,6 +400,8 @@ impl WriteC for ComputeBucket {
compute_c.push(format!("}}"));

}
compute_c.push("}}".to_string());

result = result_ref;


Expand All @@ -374,7 +410,7 @@ impl WriteC for ComputeBucket {
_ => {
let exp_aux_index = self.op_aux_no.to_string();
// build assign
let operator = get_fr_op(self.op);
let operator = get_fr_op(&self.op);
let result_ref = format!("&{}", expaux(exp_aux_index.clone()));
let mut arguments = vec![result_ref.clone()];
arguments.append(&mut operands);
Expand Down
Loading

0 comments on commit 63b0f37

Please sign in to comment.