Browse Source

feat: add ability to choose to allocate inputs as witnesses or instances

pull/1/head
dmpierre 6 months ago
parent
commit
ea476f2567
2 changed files with 36 additions and 13 deletions
  1. +1
    -0
      src/circom/builder.rs
  2. +35
    -13
      src/circom/circuit.rs

+ 1
- 0
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

+ 35
- 13
src/circom/circuit.rs

@ -22,6 +22,7 @@ pub struct CircomCircuit {
pub r1cs: R1CS<F>,
pub witness: Option<Vec<F>>,
pub public_inputs_indexes: Vec<Variable>,
pub allocate_inputs_as_witnesses: bool,
}
impl<F: PrimeField> CircomCircuit<F> {
@ -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 {

Loading…
Cancel
Save