|
1 | 1 | use base64::prelude::*; |
2 | 2 | use log::{debug, warn}; |
3 | 3 |
|
| 4 | +const STATE_HASH_SIZE: usize = 32; |
| 5 | +// TODO(gabrielbosio): check that this length is always the same for every block |
| 6 | +const PROTOCOL_STATE_SIZE: usize = 2056; |
| 7 | + |
4 | 8 | pub fn verify_protocol_state_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool { |
| 9 | + if public_input.len() != (STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) * 2 { |
| 10 | + return false; |
| 11 | + } |
| 12 | + |
5 | 13 | debug!("Checking Mina protocol state proof"); |
6 | 14 | if let Err(err) = check_protocol_state_proof(proof) { |
7 | 15 | warn!("Protocol state proof check failed: {}", err); |
@@ -30,10 +38,21 @@ pub fn check_protocol_state_proof(protocol_state_proof_bytes: &[u8]) -> Result<( |
30 | 38 |
|
31 | 39 | pub fn check_protocol_state_pub(protocol_state_pub: &[u8]) -> Result<(), String> { |
32 | 40 | // TODO(xqft): check hash and binprot deserialization |
33 | | - let protocol_state_base64 = |
34 | | - std::str::from_utf8(&protocol_state_pub[32..]).map_err(|err| err.to_string())?; |
| 41 | + let candidate_protocol_state_base64 = std::str::from_utf8( |
| 42 | + &protocol_state_pub[STATE_HASH_SIZE..(STATE_HASH_SIZE + PROTOCOL_STATE_SIZE)], |
| 43 | + ) |
| 44 | + .map_err(|err| err.to_string())?; |
| 45 | + BASE64_STANDARD |
| 46 | + .decode(candidate_protocol_state_base64) |
| 47 | + .map_err(|err| err.to_string())?; |
| 48 | + |
| 49 | + let tip_protocol_state_base64 = std::str::from_utf8( |
| 50 | + &protocol_state_pub[(STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) + STATE_HASH_SIZE |
| 51 | + ..((STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) * 2)], |
| 52 | + ) |
| 53 | + .map_err(|err| err.to_string())?; |
35 | 54 | BASE64_STANDARD |
36 | | - .decode(protocol_state_base64) |
| 55 | + .decode(tip_protocol_state_base64) |
37 | 56 | .map_err(|err| err.to_string())?; |
38 | 57 |
|
39 | 58 | Ok(()) |
|
0 commit comments