Skip to content

Commit 3fb1827

Browse files
authored
feat: decouple constraints from witness solver (#2)
1 parent cb7ec06 commit 3fb1827

File tree

5 files changed

+174
-269
lines changed

5 files changed

+174
-269
lines changed

bin/src/main.nr

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use http::parse;
22

3-
pub fn main(item: pub [Field; 1], data: str<1024>) -> pub Field {
3+
pub fn main(data: str<1024>) {
44
parse(data);
5-
item[0]
65
}
7-

constraint_counter/mock/mock.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"noir_version":"1.0.0-beta.1+03b58fa2dfcc8acc8cf5198b1b23b55676fbdb02","hash":2759475469106495053,"abi":{"parameters":[{"name":"x0","type":{"kind":"field"},"visibility":"public"},{"name":"w0","type":{"kind":"field"},"visibility":"private"},{"name":"w1","type":{"kind":"field"},"visibility":"private"}],"return_type":null,"error_types":{}},"bytecode":"H4sIAAAAAAAA/62QQQoAIAgEVXpQ9YL+/6qKWhTBkw6IexqWZXrIOSYFef3fcww4q71S55q3WzN7oLPfJmIDlIN1nEwBAAA=","debug_symbols":"TYxLCsMwDAXvonUWTVuy8FVKCf7IQWBsI9uFYnL3KCGB7DRv0HRwaNoyU/SpgPp0CMnqSikKdXgcU8k67lSq5gpqfI8DYHRyTc91AE8BQU2v9StgmEKgZb5nZP5pJm0CnuhbtDdb//ky13/mZNE1xr10OMlv","file_map":{"63":{"source":"use http::parse;\n\n// pub fn main(data: str<1024>) {\n// parse(data);\n// }\n\npub fn main(x0: pub Field, w0: Field, w1: Field) {\n assert(x0 * w0 + w1 + 2 == 0);\n}","path":"/Users/autoparallel/Code/noir-web-prover-circuits/bin/src/main.nr"}},"names":["main"],"brillig_names":[]}

constraint_counter/src/bridge.rs

-152
This file was deleted.

constraint_counter/src/main.rs

+58-30
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use std::{cell::RefCell, path::Path, rc::Rc};
22

33
use ark_bn254::Fr;
44
use ark_ff::AdditiveGroup;
5-
use ark_r1cs_std::{alloc::AllocVar, fields::fp::FpVar};
6-
use ark_relations::r1cs::{ConstraintSystem, ConstraintSystemRef, SynthesisMode};
5+
use ark_relations::r1cs::{
6+
ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef, OptimizationGoal, SynthesisMode,
7+
};
78

89
use clap::Parser;
9-
use noir::NoirFCircuit;
1010

11-
pub mod bridge;
11+
use noir::NoirProgram;
12+
1213
pub mod noir;
1314

1415
#[derive(Parser)]
@@ -18,14 +19,6 @@ struct Args {
1819
/// Path to the circuit JSON file
1920
#[arg(short, long)]
2021
circuit: String,
21-
22-
/// Length of public IO
23-
#[arg(long)]
24-
public_io_length: usize,
25-
26-
/// Length of private input
27-
#[arg(long)]
28-
private_input_length: usize,
2922
}
3023

3124
pub fn main() {
@@ -40,28 +33,63 @@ pub fn main() {
4033
}
4134
};
4235

43-
let circuit = NoirFCircuit::new(&noir_json);
44-
4536
let mut cs = ConstraintSystem::<Fr>::new();
46-
cs.mode = SynthesisMode::Prove {
47-
construct_matrices: false,
48-
};
37+
cs.mode = SynthesisMode::Setup;
38+
cs.optimization_goal = OptimizationGoal::Constraints;
4939
let cs = ConstraintSystemRef::<Fr>::CS(Rc::new(RefCell::new(cs)));
40+
dbg!(cs.num_instance_variables());
41+
dbg!(cs.num_witness_variables());
5042

51-
// Use array directly for public inputs
52-
let pub_inputs = vec![Fr::ZERO; args.public_io_length];
53-
let z_i = Vec::<FpVar<Fr>>::new_witness(cs.clone(), || Ok(pub_inputs)).unwrap();
43+
let program = NoirProgram::new(&noir_json);
44+
program.generate_constraints(cs.clone());
45+
cs.finalize();
46+
dbg!(cs.num_constraints());
47+
}
5448

55-
// Use array directly for external inputs
56-
let external_inputs = vec![Fr::ZERO; args.private_input_length];
57-
let external_inputs =
58-
Vec::<FpVar<Fr>>::new_witness(cs.clone(), || Ok(external_inputs)).unwrap();
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
5952

60-
let start = std::time::Instant::now();
61-
circuit
62-
.generate_step_constraints(cs.clone(), &z_i, &external_inputs)
63-
.unwrap();
64-
println!("Duration for witness solving: {:?}", start.elapsed());
53+
#[test]
54+
fn test_mock_noir_circuit() {
55+
// Circuit definition:
56+
// x_0 * w_0 + w_1 + 2 == 0
57+
let json_path = Path::new("./mock").join(format!("mock.json"));
58+
let noir_json = std::fs::read(&json_path).unwrap();
6559

66-
dbg!(cs.num_constraints());
60+
let mut cs = ConstraintSystem::<Fr>::new();
61+
cs.mode = SynthesisMode::Setup;
62+
cs.optimization_goal = OptimizationGoal::Constraints;
63+
let cs = ConstraintSystemRef::<Fr>::CS(Rc::new(RefCell::new(cs)));
64+
65+
let program = NoirProgram::new(&noir_json);
66+
program.generate_constraints(cs.clone());
67+
cs.finalize();
68+
69+
cs.set_mode(SynthesisMode::Prove {
70+
construct_matrices: true,
71+
});
72+
73+
dbg!(cs.to_matrices());
74+
dbg!(cs.num_instance_variables());
75+
dbg!(cs.num_witness_variables());
76+
// NOTE, the 0th instance assignment is the constant term enabler.
77+
// This example is:
78+
// 2 * 3 + (-8) + 2 == 0
79+
cs.borrow_mut().unwrap().instance_assignment = vec![Fr::ONE, Fr::from(2)];
80+
cs.borrow_mut().unwrap().witness_assignment = vec![Fr::from(3), -Fr::from(8)];
81+
assert!(cs.is_satisfied().unwrap());
82+
}
83+
84+
#[test]
85+
fn test_mock_noir_solve() {
86+
// Circuit definition:
87+
// x_0 * w_0 + w_1 + 2 == 0
88+
let json_path = Path::new("./mock").join(format!("mock.json"));
89+
let noir_json = std::fs::read(&json_path).unwrap();
90+
91+
let program = NoirProgram::new(&noir_json);
92+
// NOTE: Don't need to have the instance assignment set to 1 here, so we need a method to handle this if we were sticking with this CS.
93+
program.solve(&[Fr::from(2)], &[Fr::from(3), -Fr::from(8)]);
94+
}
6795
}

0 commit comments

Comments
 (0)