@@ -2,13 +2,14 @@ use std::{cell::RefCell, path::Path, rc::Rc};
2
2
3
3
use ark_bn254:: Fr ;
4
4
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
+ } ;
7
8
8
9
use clap:: Parser ;
9
- use noir:: NoirFCircuit ;
10
10
11
- pub mod bridge;
11
+ use noir:: NoirProgram ;
12
+
12
13
pub mod noir;
13
14
14
15
#[ derive( Parser ) ]
@@ -18,14 +19,6 @@ struct Args {
18
19
/// Path to the circuit JSON file
19
20
#[ arg( short, long) ]
20
21
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 ,
29
22
}
30
23
31
24
pub fn main ( ) {
@@ -40,28 +33,63 @@ pub fn main() {
40
33
}
41
34
} ;
42
35
43
- let circuit = NoirFCircuit :: new ( & noir_json) ;
44
-
45
36
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 ;
49
39
let cs = ConstraintSystemRef :: < Fr > :: CS ( Rc :: new ( RefCell :: new ( cs) ) ) ;
40
+ dbg ! ( cs. num_instance_variables( ) ) ;
41
+ dbg ! ( cs. num_witness_variables( ) ) ;
50
42
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
+ }
54
48
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 :: * ;
59
52
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 ( ) ;
65
59
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
+ }
67
95
}
0 commit comments