Skip to content

Commit efc8af1

Browse files
committed
feat: solver
1 parent 85e4b08 commit efc8af1

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

constraint_counter/src/main.rs

-8
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ struct Args {
2222
/// Path to the circuit JSON file
2323
#[arg(short, long)]
2424
circuit: String,
25-
26-
/// Length of public IO
27-
#[arg(long)]
28-
public_io_length: usize,
29-
30-
/// Length of private input
31-
#[arg(long)]
32-
private_input_length: usize,
3325
}
3426

3527
pub fn main() {

constraint_counter/src/simple.rs

+46-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ use acvm::{
44
acir::{
55
acir_field::GenericFieldElement,
66
circuit::{brillig::BrilligBytecode, Circuit, Opcode, Program},
7-
native_types::Witness,
7+
native_types::{Witness, WitnessMap},
88
},
9+
blackbox_solver::StubbedBlackBoxSolver,
10+
pwg::ACVM,
911
AcirField,
1012
};
1113
use ark_ff::Field;
12-
use ark_relations::{
13-
lc,
14-
r1cs::{ConstraintSynthesizer, LinearCombination, Variable},
15-
};
14+
use ark_relations::r1cs::{ConstraintSynthesizer, LinearCombination, Variable};
1615
use serde::{Deserialize, Serialize};
1716

1817
use super::*;
@@ -38,6 +37,43 @@ impl NoirProgram {
3837
pub fn unconstrained_functions(&self) -> &Vec<BrilligBytecode<GenericFieldElement<Fr>>> {
3938
&self.bytecode.unconstrained_functions
4039
}
40+
41+
pub fn solve(
42+
&self,
43+
instance_variables: &[Fr],
44+
witness_variables: &[Fr],
45+
) -> WitnessMap<GenericFieldElement<Fr>> {
46+
let mut acvm = ACVM::new(
47+
&StubbedBlackBoxSolver(false),
48+
&self.circuit().opcodes,
49+
WitnessMap::new(),
50+
self.unconstrained_functions(),
51+
&[],
52+
);
53+
54+
self.circuit()
55+
.public_parameters
56+
.0
57+
.iter()
58+
.for_each(|witness| {
59+
let f =
60+
GenericFieldElement::<Fr>::from_repr(instance_variables[witness.as_usize()]);
61+
acvm.overwrite_witness(*witness, f);
62+
});
63+
64+
// write witness values for external_inputs
65+
self.circuit()
66+
.private_parameters
67+
.iter()
68+
.for_each(|witness| {
69+
let idx = witness.as_usize() - instance_variables.len();
70+
71+
let f = GenericFieldElement::<Fr>::from_repr(witness_variables[idx]);
72+
acvm.overwrite_witness(*witness, f);
73+
});
74+
let _status = acvm.solve();
75+
acvm.finalize()
76+
}
4177
}
4278

4379
impl ConstraintSynthesizer<Fr> for NoirProgram {
@@ -53,12 +89,12 @@ impl ConstraintSynthesizer<Fr> for NoirProgram {
5389

5490
// Then, allocate known private witnesses
5591
let private_params = &self.circuit().private_parameters;
56-
for &witness in private_params.iter() {
92+
for &witness in private_params {
5793
let var = cs.new_witness_variable(|| Ok(Fr::ZERO))?;
5894
witness_map.insert(witness, var);
5995
}
6096

61-
for opcode in self.circuit().opcodes.iter() {
97+
for opcode in &self.circuit().opcodes {
6298
if let Opcode::AssertZero(gate) = opcode {
6399
let mut left_terms = LinearCombination::<Fr>::new();
64100
let mut right_terms = LinearCombination::<Fr>::new();
@@ -91,6 +127,9 @@ impl ConstraintSynthesizer<Fr> for NoirProgram {
91127
// The constraint becomes: left_terms * right_terms + output_terms = 0
92128
cs.enforce_constraint(left_terms, right_terms, -output_terms)?;
93129
}
130+
if let Opcode::MemoryInit { .. } | Opcode::MemoryOp { .. } = opcode {
131+
panic!("Memory Opcode was used! This is not currently supported.");
132+
}
94133
}
95134

96135
Ok(())

0 commit comments

Comments
 (0)