mirror of
https://github.com/arnaucube/circom-compat.git
synced 2026-01-09 15:31:31 +01:00
feat: add ability to choose to allocate inputs as witnesses or instances
This commit is contained in:
@@ -59,6 +59,7 @@ impl<F: PrimeField> CircomBuilder<F> {
|
|||||||
r1cs: self.cfg.r1cs.clone(),
|
r1cs: self.cfg.r1cs.clone(),
|
||||||
witness: None,
|
witness: None,
|
||||||
public_inputs_indexes: vec![],
|
public_inputs_indexes: vec![],
|
||||||
|
allocate_inputs_as_witnesses: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Disable the wire mapping
|
// Disable the wire mapping
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ pub struct CircomCircuit<F: PrimeField> {
|
|||||||
pub r1cs: R1CS<F>,
|
pub r1cs: R1CS<F>,
|
||||||
pub witness: Option<Vec<F>>,
|
pub witness: Option<Vec<F>>,
|
||||||
pub public_inputs_indexes: Vec<Variable>,
|
pub public_inputs_indexes: Vec<Variable>,
|
||||||
|
pub allocate_inputs_as_witnesses: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: PrimeField> CircomCircuit<F> {
|
impl<F: PrimeField> CircomCircuit<F> {
|
||||||
@@ -49,19 +50,40 @@ impl<F: PrimeField> ConstraintSynthesizer<F> for CircomCircuit<F> {
|
|||||||
// allocate non-allocated inputs and update mapping
|
// allocate non-allocated inputs and update mapping
|
||||||
// !! today, we allocate everything as witnesses by default for compatibility with sonobe !!
|
// !! today, we allocate everything as witnesses by default for compatibility with sonobe !!
|
||||||
for circom_public_input_index in 0..n_non_allocated_inputs {
|
for circom_public_input_index in 0..n_non_allocated_inputs {
|
||||||
circom_index_to_cs_index.insert(
|
if circom_public_input_index == 0 {
|
||||||
circom_public_input_index,
|
circom_index_to_cs_index.insert(circom_public_input_index, Variable::One);
|
||||||
Variable::Witness(cs.num_witness_variables()),
|
continue;
|
||||||
);
|
}
|
||||||
cs.new_witness_variable(|| {
|
|
||||||
Ok(match witness {
|
let variable = match self.allocate_inputs_as_witnesses {
|
||||||
None => F::from(1u32),
|
true => {
|
||||||
Some(w) => match wire_mapping {
|
let witness_var = Variable::Witness(cs.num_witness_variables());
|
||||||
Some(m) => w[m[circom_public_input_index]],
|
cs.new_witness_variable(|| {
|
||||||
None => w[circom_public_input_index],
|
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 {
|
for circom_public_input_index in n_non_allocated_inputs..self.r1cs.num_inputs {
|
||||||
|
|||||||
Reference in New Issue
Block a user