@@ -10,7 +10,10 @@ use midnight_curves::Bls12;
1010use midnight_proofs:: { poly:: kzg:: params:: ParamsKZG , utils:: SerdeFormat } ;
1111use sha2:: { Digest , Sha256 } ;
1212
13- use crate :: { StmResult , circuits:: MITHRIL_CIRCUIT_CACHE_FOLDER } ;
13+ use crate :: {
14+ StmResult ,
15+ circuits:: { MITHRIL_CIRCUIT_CACHE_FOLDER , TrustedSetupError } ,
16+ } ;
1417
1518/// TODO: remove allow(dead_code) when the constants are used or remove the constatnts
1619#[ allow( dead_code) ]
@@ -64,10 +67,17 @@ impl TrustedSetupProvider {
6467 }
6568
6669 /// Checks SHA256 hash of the given bytes against the stored expected value.
67- fn verify_bytes_sha256_hash ( & self , srs_bytes : & [ u8 ] ) -> bool {
70+ fn verify_bytes_sha256_hash ( & self , srs_bytes : & [ u8 ] ) -> StmResult < ( ) > {
6871 let recomputed_hash = Self :: compute_hash ( srs_bytes) ;
6972
70- self . srs_expected_hash == recomputed_hash
73+ if self . srs_expected_hash != recomputed_hash {
74+ return Err ( TrustedSetupError :: VerifyHashFail (
75+ self . srs_expected_hash . clone ( ) ,
76+ recomputed_hash,
77+ )
78+ . into ( ) ) ;
79+ }
80+ Ok ( ( ) )
7181 }
7282
7383 /// Fetches the SRS from `self.url_to_download_srs` and returns its bytes.
@@ -114,11 +124,7 @@ impl TrustedSetupProvider {
114124 let srs_bytes = self
115125 . download_srs_file ( )
116126 . with_context ( || "Download of the SRS file should have succeeded!" ) ?;
117- if !self . verify_bytes_sha256_hash ( & srs_bytes) {
118- return Err ( anyhow ! (
119- "Error, the hash of the SRS file does not match the hard-coded value!"
120- ) ) ;
121- }
127+ self . verify_bytes_sha256_hash ( & srs_bytes) ?;
122128 self . store_srs_bytes_to_file ( & srs_bytes)
123129 . with_context ( || "Saving the SRS to disk should have succeeded!" ) ?;
124130 }
@@ -132,11 +138,11 @@ impl TrustedSetupProvider {
132138 self . ensure_srs_file_is_available ( ) ?;
133139
134140 let file = File :: open ( & self . local_srs_path )
135- . with_context ( || format ! ( "Failed to open SRS file at {:?}" , self . local_srs_path) ) ?;
141+ . with_context ( || format ! ( "Failed to open SRS file at {:?}! " , self . local_srs_path) ) ?;
136142 let mut reader = BufReader :: new ( file) ;
137143
138144 ParamsKZG :: read_custom ( & mut reader, SerdeFormat :: RawBytesUnchecked )
139- . with_context ( || "Failed to deserialize SRS from file" )
145+ . with_context ( || "Failed to deserialize SRS from file! " )
140146 }
141147}
142148
@@ -243,15 +249,18 @@ mod tests {
243249 let result = TrustedSetupProvider :: new ( "" , SRS_HASH_K1 , "" , Duration :: from_secs ( 600 ) )
244250 . verify_bytes_sha256_hash ( & tampered_bytes) ;
245251
246- assert ! ( !result) ;
252+ assert ! (
253+ result. is_err( ) ,
254+ "Hash verification should failed due to the tampering of the bytes!"
255+ ) ;
247256 }
248257
249258 #[ test]
250259 fn hash_of_correct_bytes_verifies ( ) {
251260 let result = TrustedSetupProvider :: new ( "" , SRS_HASH_K1 , "" , Duration :: from_secs ( 600 ) )
252261 . verify_bytes_sha256_hash ( SRS_K1 ) ;
253262
254- assert ! ( result) ;
263+ assert ! ( result. is_ok ( ) ) ;
255264 }
256265
257266 #[ test]
0 commit comments