|
| 1 | +use base64::prelude::*; |
| 2 | +use log::{debug, warn}; |
| 3 | + |
| 4 | +pub fn verify_protocol_state_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool { |
| 5 | + debug!("Reading Mina protocol state proof base64"); |
| 6 | + let protocol_state_proof_base64 = |
| 7 | + if let Ok(protocol_state_proof_base64) = std::str::from_utf8(&proof) { |
| 8 | + protocol_state_proof_base64 |
| 9 | + } else { |
| 10 | + return false; |
| 11 | + }; |
| 12 | + debug!("Reading Mina protocol state hash base58"); |
| 13 | + let protocol_state_hash_base58 = |
| 14 | + if let Ok(protocol_state_hash_base58) = std::str::from_utf8(&public_input) { |
| 15 | + protocol_state_hash_base58 |
| 16 | + } else { |
| 17 | + return false; |
| 18 | + }; |
| 19 | + |
| 20 | + debug!("Decoding Mina protocol state proof base64"); |
| 21 | + if BASE64_URL_SAFE |
| 22 | + .decode(protocol_state_proof_base64.trim_end()) |
| 23 | + .is_err() |
| 24 | + { |
| 25 | + warn!("Failed to decode Mina protocol state proof base64"); |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + debug!("Decoding Mina protocol state hash base58"); |
| 30 | + if bs58::decode(protocol_state_hash_base58.trim_end()) |
| 31 | + .into_vec() |
| 32 | + .is_err() |
| 33 | + { |
| 34 | + warn!("Failed to decode Mina protocol state hash base58"); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + true |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod test { |
| 43 | + use super::verify_protocol_state_proof_integrity; |
| 44 | + |
| 45 | + const PROTOCOL_STATE_PROOF_BYTES: &[u8] = |
| 46 | + include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state_proof.proof"); |
| 47 | + const PROTOCOL_STATE_HASH_BYTES: &[u8] = |
| 48 | + include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state_hash.pub"); |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn verify_protocol_state_proof_integrity_does_not_fail() { |
| 52 | + assert!(verify_protocol_state_proof_integrity( |
| 53 | + PROTOCOL_STATE_PROOF_BYTES, |
| 54 | + PROTOCOL_STATE_HASH_BYTES, |
| 55 | + )); |
| 56 | + } |
| 57 | +} |
0 commit comments