@@ -18,20 +18,28 @@ use liminal_ark_relation_macro::snark_relation;
1818mod relation {
1919 use core:: ops:: Add ;
2020
21- use ark_r1cs_std:: { alloc:: AllocVar , eq:: EqGadget , fields:: fp:: FpVar } ;
21+ use ark_r1cs_std:: {
22+ alloc:: {
23+ AllocVar ,
24+ AllocationMode :: { Input , Witness } ,
25+ } ,
26+ eq:: EqGadget ,
27+ fields:: fp:: FpVar ,
28+ } ;
2229 use ark_relations:: ns;
2330
24- use crate :: shielder :: {
25- check_merkle_proof ,
26- circuit_utils :: PathShapeVar ,
27- convert_hash , convert_vec ,
28- note :: check_note ,
29- types :: {
30- BackendLeafIndex , BackendMerklePath , BackendMerkleRoot , BackendNote , BackendNullifier ,
31- BackendTokenAmount , BackendTokenId , BackendTrapdoor , FrontendLeafIndex ,
32- FrontendMerklePath , FrontendMerkleRoot , FrontendNote , FrontendNullifier ,
33- FrontendTokenAmount , FrontendTokenId , FrontendTrapdoor ,
31+ use crate :: {
32+ shielder :: {
33+ check_merkle_proof , convert_hash , convert_vec ,
34+ path_shape_var :: PathShapeVar ,
35+ types :: {
36+ BackendLeafIndex , BackendMerklePath , BackendMerkleRoot , BackendNote ,
37+ BackendNullifier , BackendTokenAmount , BackendTokenId , BackendTrapdoor ,
38+ FrontendLeafIndex , FrontendMerklePath , FrontendMerkleRoot , FrontendNote ,
39+ FrontendNullifier , FrontendTokenAmount , FrontendTokenId , FrontendTrapdoor ,
40+ } ,
3441 } ,
42+ NoteVarBuilder ,
3543 } ;
3644
3745 #[ relation_object_definition]
@@ -82,76 +90,47 @@ mod relation {
8290
8391 #[ circuit_definition]
8492 fn generate_constraints ( ) {
85- let token_id = FpVar :: new_input ( ns ! ( cs, "token id" ) , || self . token_id ( ) ) ?;
8693 //------------------------------
8794 // Check first old note arguments.
8895 //------------------------------
89- let first_old_token_amount = FpVar :: new_witness ( ns ! ( cs, "first old token amount" ) , || {
90- self . first_old_token_amount ( )
91- } ) ?;
92- let first_old_trapdoor =
93- FpVar :: new_witness ( ns ! ( cs, "first old trapdoor" ) , || self . first_old_trapdoor ( ) ) ?;
94- let first_old_nullifier = FpVar :: new_input ( ns ! ( cs, "first old nullifier" ) , || {
95- self . first_old_nullifier ( )
96- } ) ?;
97- let first_old_note =
98- FpVar :: new_witness ( ns ! ( cs, "first old note" ) , || self . first_old_note ( ) ) ?;
99-
100- check_note (
101- & token_id,
102- & first_old_token_amount,
103- & first_old_trapdoor,
104- & first_old_nullifier,
105- & first_old_note,
106- ) ?;
96+ let first_old_note = NoteVarBuilder :: new ( cs. clone ( ) )
97+ . with_token_id ( self . token_id ( ) , Input ) ?
98+ . with_token_amount ( self . first_old_token_amount ( ) , Witness ) ?
99+ . with_trapdoor ( self . first_old_trapdoor ( ) , Witness ) ?
100+ . with_nullifier ( self . first_old_nullifier ( ) , Input ) ?
101+ . with_note ( self . first_old_note ( ) , Witness ) ?
102+ . build ( ) ?;
107103
108104 //------------------------------
109105 // Check second old note arguments.
110106 //------------------------------
111- let second_old_token_amount =
112- FpVar :: new_witness ( ns ! ( cs, "second old token amount" ) , || {
113- self . second_old_token_amount ( )
114- } ) ?;
115- let second_old_trapdoor = FpVar :: new_witness ( ns ! ( cs, "second old trapdoor" ) , || {
116- self . second_old_trapdoor ( )
117- } ) ?;
118- let second_old_nullifier = FpVar :: new_input ( ns ! ( cs, "second old nullifier" ) , || {
119- self . second_old_nullifier ( )
120- } ) ?;
121- let second_old_note =
122- FpVar :: new_witness ( ns ! ( cs, "second old note" ) , || self . second_old_note ( ) ) ?;
123-
124- check_note (
125- & token_id,
126- & second_old_token_amount,
127- & second_old_trapdoor,
128- & second_old_nullifier,
129- & second_old_note,
130- ) ?;
107+ let second_old_note = NoteVarBuilder :: new ( cs. clone ( ) )
108+ . with_token_id_var ( first_old_note. token_id . clone ( ) )
109+ . with_token_amount ( self . second_old_token_amount ( ) , Witness ) ?
110+ . with_trapdoor ( self . second_old_trapdoor ( ) , Witness ) ?
111+ . with_nullifier ( self . second_old_nullifier ( ) , Input ) ?
112+ . with_note ( self . second_old_note ( ) , Witness ) ?
113+ . build ( ) ?;
131114
132115 //------------------------------
133116 // Check new note arguments.
134117 //------------------------------
135- let new_token_amount =
136- FpVar :: new_witness ( ns ! ( cs, "new token amount" ) , || self . new_token_amount ( ) ) ?;
137- let new_trapdoor = FpVar :: new_witness ( ns ! ( cs, "new trapdoor" ) , || self . new_trapdoor ( ) ) ?;
138- let new_nullifier = FpVar :: new_witness ( ns ! ( cs, "new nullifier" ) , || self . new_nullifier ( ) ) ?;
139- let new_note = FpVar :: new_input ( ns ! ( cs, "new note" ) , || self . new_note ( ) ) ?;
140-
141- check_note (
142- & token_id,
143- & new_token_amount,
144- & new_trapdoor,
145- & new_nullifier,
146- & new_note,
147- ) ?;
118+ let new_note = NoteVarBuilder :: new ( cs. clone ( ) )
119+ . with_token_id_var ( first_old_note. token_id . clone ( ) )
120+ . with_token_amount ( self . new_token_amount ( ) , Witness ) ?
121+ . with_trapdoor ( self . new_trapdoor ( ) , Witness ) ?
122+ . with_nullifier ( self . new_nullifier ( ) , Witness ) ?
123+ . with_note ( self . new_note ( ) , Input ) ?
124+ . build ( ) ?;
148125
149126 //----------------------------------
150127 // Check token value soundness.
151128 //----------------------------------
152129 // some range checks for overflows?
153- let token_sum = first_old_token_amount. add ( second_old_token_amount) ;
154- token_sum. enforce_equal ( & new_token_amount) ?;
130+ let token_sum = first_old_note
131+ . token_amount
132+ . add ( second_old_note. token_amount ) ;
133+ token_sum. enforce_equal ( & new_note. token_amount ) ?;
155134
156135 //------------------------
157136 // Check first merkle proof.
@@ -164,7 +143,7 @@ mod relation {
164143 check_merkle_proof (
165144 merkle_root. clone ( ) ,
166145 first_path_shape,
167- first_old_note,
146+ first_old_note. note ,
168147 self . first_merkle_path ( ) . cloned ( ) . unwrap_or_default ( ) ,
169148 * self . max_path_len ( ) ,
170149 cs. clone ( ) ,
@@ -180,7 +159,7 @@ mod relation {
180159 check_merkle_proof (
181160 merkle_root,
182161 second_path_shape,
183- second_old_note,
162+ second_old_note. note ,
184163 self . second_merkle_path ( ) . cloned ( ) . unwrap_or_default ( ) ,
185164 * self . max_path_len ( ) ,
186165 cs,
0 commit comments