1- use anyhow:: { Context , anyhow } ;
1+ use anyhow:: Context ;
22use ff:: Field ;
33use group:: Group ;
44use midnight_circuits:: ecc:: curves:: CircuitCurve as CircuitCurveTrait ;
55use midnight_circuits:: instructions:: { AssignmentInstructions , PublicInputInstructions } ;
66use midnight_circuits:: types:: { AssignedNative , AssignedNativePoint } ;
77use midnight_proofs:: circuit:: { Layouter , Value } ;
8- use midnight_proofs:: plonk:: Error ;
98use midnight_zk_stdlib:: { Relation , ZkStdLib , ZkStdLibArch } ;
109
11- use crate :: circuits:: halo2:: errors:: { StmCircuitError , to_synthesis_error } ;
10+ use crate :: circuits:: halo2:: errors:: StmCircuitError ;
1211use crate :: circuits:: halo2:: gadgets:: {
1312 LOTTERY_BIT_BOUND , MerklePathInputs , UniqueSchnorrSignatureInputs ,
1413 assert_lottery_index_in_bounds, assert_lottery_won, assert_strictly_increasing_lottery_index,
@@ -48,13 +47,13 @@ impl StmCertificateCircuit {
4847 ///
4948 /// Enforces `k < m <= 2^LOTTERY_BIT_BOUND - 1`, returning
5049 /// `StmCircuitError::InvalidCircuitParameters` when violated.
51- pub ( crate ) fn validate_parameters ( & self ) -> StmResult < ( ) > {
50+ pub ( crate ) fn validate_parameters ( & self ) -> Result < ( ) , StmCircuitError > {
5251 let max_m = ( 1u32 << LOTTERY_BIT_BOUND ) - 1 ;
5352 if self . k >= self . m || self . m > max_m {
54- return Err ( anyhow ! ( StmCircuitError :: InvalidCircuitParameters {
53+ return Err ( StmCircuitError :: InvalidCircuitParameters {
5554 k : self . k ,
5655 m : self . m ,
57- } ) ) ;
56+ } ) ;
5857 }
5958
6059 Ok ( ( ) )
@@ -64,13 +63,13 @@ impl StmCertificateCircuit {
6463 ///
6564 /// This precondition prevents shape mismatches; failures return
6665 /// `StmCircuitError::WitnessLengthMismatch`.
67- pub ( crate ) fn validate_witness_length ( & self , actual : usize ) -> StmResult < ( ) > {
66+ pub ( crate ) fn validate_witness_length ( & self , actual : usize ) -> Result < ( ) , StmCircuitError > {
6867 let expected_k = self . k as usize ;
6968 if actual != expected_k {
70- return Err ( anyhow ! ( StmCircuitError :: WitnessLengthMismatch {
69+ return Err ( StmCircuitError :: WitnessLengthMismatch {
7170 expected_k : self . k ,
7271 actual : Self :: checked_len_u32 ( actual) ,
73- } ) ) ;
72+ } ) ;
7473 }
7574
7675 Ok ( ( ) )
@@ -80,20 +79,20 @@ impl StmCertificateCircuit {
8079 ///
8180 /// The circuit uses [`LOTTERY_BIT_BOUND`]-bit comparison constraints, so each
8281 /// index must fit in that range and must satisfy `index < m`.
83- pub ( crate ) fn validate_lottery_index ( & self , index : LotteryIndex ) -> StmResult < ( ) > {
82+ pub ( crate ) fn validate_lottery_index (
83+ & self ,
84+ index : LotteryIndex ,
85+ ) -> Result < ( ) , StmCircuitError > {
8486 let max_supported = ( ( 1u64 << LOTTERY_BIT_BOUND ) - 1 ) as LotteryIndex ;
8587 if index > max_supported {
86- return Err ( anyhow ! ( StmCircuitError :: LotteryIndexTooLarge {
88+ return Err ( StmCircuitError :: LotteryIndexTooLarge {
8789 index,
8890 max_supported,
89- } ) ) ;
91+ } ) ;
9092 }
9193
9294 if index >= self . m as LotteryIndex {
93- return Err ( anyhow ! ( StmCircuitError :: LotteryIndexOutOfBounds {
94- index,
95- m: self . m,
96- } ) ) ;
95+ return Err ( StmCircuitError :: LotteryIndexOutOfBounds { index, m : self . m } ) ;
9796 }
9897
9998 Ok ( ( ) )
@@ -103,13 +102,16 @@ impl StmCertificateCircuit {
103102 ///
104103 /// This guards against inconsistent witness paths and returns
105104 /// `StmCircuitError::MerkleSiblingLengthMismatch` on mismatch.
106- pub ( crate ) fn validate_merkle_sibling_length ( & self , actual : usize ) -> StmResult < ( ) > {
105+ pub ( crate ) fn validate_merkle_sibling_length (
106+ & self ,
107+ actual : usize ,
108+ ) -> Result < ( ) , StmCircuitError > {
107109 let expected_depth = self . merkle_tree_depth as usize ;
108110 if actual != expected_depth {
109- return Err ( anyhow ! ( StmCircuitError :: MerkleSiblingLengthMismatch {
111+ return Err ( StmCircuitError :: MerkleSiblingLengthMismatch {
110112 expected_depth : self . merkle_tree_depth ,
111113 actual : Self :: checked_len_u32 ( actual) ,
112- } ) ) ;
114+ } ) ;
113115 }
114116
115117 Ok ( ( ) )
@@ -119,13 +121,16 @@ impl StmCertificateCircuit {
119121 ///
120122 /// Under the current witness shape, this cannot fail independently from sibling-length
121123 /// validation because both lengths derive from `x.siblings`; returns `StmCircuitError::MerklePositionLengthMismatch`.
122- pub ( crate ) fn validate_merkle_position_length ( & self , actual : usize ) -> StmResult < ( ) > {
124+ pub ( crate ) fn validate_merkle_position_length (
125+ & self ,
126+ actual : usize ,
127+ ) -> Result < ( ) , StmCircuitError > {
123128 let expected_depth = self . merkle_tree_depth as usize ;
124129 if actual != expected_depth {
125- return Err ( anyhow ! ( StmCircuitError :: MerklePositionLengthMismatch {
130+ return Err ( StmCircuitError :: MerklePositionLengthMismatch {
126131 expected_depth : self . merkle_tree_depth ,
127132 actual : Self :: checked_len_u32 ( actual) ,
128- } ) ) ;
133+ } ) ;
129134 }
130135
131136 Ok ( ( ) )
@@ -160,11 +165,11 @@ impl StmCertificateCircuit {
160165}
161166
162167impl Relation for StmCertificateCircuit {
163- type Error = Error ;
168+ type Error = StmCircuitError ;
164169 type Instance = CircuitInstance ;
165170 type Witness = CircuitWitness ;
166171
167- fn format_instance ( instance : & Self :: Instance ) -> Result < Vec < CircuitBase > , Error > {
172+ fn format_instance ( instance : & Self :: Instance ) -> Result < Vec < CircuitBase > , StmCircuitError > {
168173 Ok ( vec ! [ instance. 0 . into( ) , instance. 1 . into( ) ] )
169174 }
170175
@@ -174,17 +179,16 @@ impl Relation for StmCertificateCircuit {
174179 layouter : & mut impl Layouter < CircuitBase > ,
175180 instance : Value < Self :: Instance > ,
176181 witness : Value < Self :: Witness > ,
177- ) -> Result < ( ) , Error > {
178- self . validate_parameters ( ) . map_err ( to_synthesis_error ) ?;
182+ ) -> Result < ( ) , StmCircuitError > {
183+ self . validate_parameters ( ) ?;
179184 let witness = witness
180- . map_with_result ( |witness| -> StmResult < _ > {
185+ . map_with_result ( |witness| -> Result < _ , StmCircuitError > {
181186 self . validate_witness_length ( witness. len ( ) ) ?;
182187 witness
183188 . iter ( )
184189 . try_for_each ( |entry| self . validate_lottery_index ( entry. lottery_index ) ) ?;
185190 Ok ( witness)
186- } )
187- . map_err ( to_synthesis_error) ?
191+ } ) ?
188192 . transpose_vec ( self . k as usize ) ;
189193
190194 let merkle_tree_commitment: AssignedNative < CircuitBase > =
@@ -278,6 +282,7 @@ impl Relation for StmCertificateCircuit {
278282 // m can be put as a public instance or a constant
279283 let m = std_lib. assign_fixed ( layouter, CircuitBase :: from ( self . m as u64 ) ) ?;
280284 assert_lottery_index_in_bounds ( std_lib, layouter, & previous_lottery_index, & m)
285+ . map_err ( StmCircuitError :: from)
281286 }
282287
283288 fn used_chips ( & self ) -> ZkStdLibArch {
0 commit comments