diff --git a/src/circom/builder.rs b/src/circom/builder.rs index 8acbc2f..222f7c9 100644 --- a/src/circom/builder.rs +++ b/src/circom/builder.rs @@ -59,6 +59,7 @@ impl CircomBuilder { r1cs: self.cfg.r1cs.clone(), witness: None, public_inputs_indexes: vec![], + allocate_inputs_as_witnesses: false, }; // Disable the wire mapping diff --git a/src/circom/circuit.rs b/src/circom/circuit.rs index 295695c..d487d04 100644 --- a/src/circom/circuit.rs +++ b/src/circom/circuit.rs @@ -22,6 +22,7 @@ pub struct CircomCircuit { pub r1cs: R1CS, pub witness: Option>, pub public_inputs_indexes: Vec, + pub allocate_inputs_as_witnesses: bool, } impl CircomCircuit { @@ -49,19 +50,40 @@ impl ConstraintSynthesizer for CircomCircuit { // allocate non-allocated inputs and update mapping // !! today, we allocate everything as witnesses by default for compatibility with sonobe !! for circom_public_input_index in 0..n_non_allocated_inputs { - circom_index_to_cs_index.insert( - circom_public_input_index, - Variable::Witness(cs.num_witness_variables()), - ); - cs.new_witness_variable(|| { - Ok(match witness { - None => F::from(1u32), - Some(w) => match wire_mapping { - Some(m) => w[m[circom_public_input_index]], - None => w[circom_public_input_index], - }, - }) - })?; + if circom_public_input_index == 0 { + circom_index_to_cs_index.insert(circom_public_input_index, Variable::One); + continue; + } + + let variable = match self.allocate_inputs_as_witnesses { + true => { + let witness_var = Variable::Witness(cs.num_witness_variables()); + cs.new_witness_variable(|| { + Ok(match witness { + None => F::from(1u32), + Some(w) => match wire_mapping { + Some(m) => w[m[circom_public_input_index]], + None => w[circom_public_input_index], + }, + }) + })?; + witness_var + } + false => { + let instance_var = Variable::Instance(cs.num_instance_variables()); + cs.new_input_variable(|| { + Ok(match witness { + None => F::from(1u32), + Some(w) => match wire_mapping { + Some(m) => w[m[circom_public_input_index]], + None => w[circom_public_input_index], + }, + }) + })?; + instance_var + } + }; + circom_index_to_cs_index.insert(circom_public_input_index, variable); } for circom_public_input_index in n_non_allocated_inputs..self.r1cs.num_inputs {