|
1 | | -use std::array::TryFromSliceError; |
2 | | - |
3 | | -use base64::prelude::*; |
4 | 1 | use log::{debug, warn}; |
5 | | - |
6 | | -const STATE_HASH_SIZE: usize = 32; |
| 2 | +use mina_bridge_core::proof::state_proof::{MinaStateProof, MinaStatePubInputs}; |
7 | 3 |
|
8 | 4 | pub fn verify_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool { |
9 | | - debug!("Checking Mina protocol state proof"); |
10 | | - if let Err(err) = check_proof(proof) { |
11 | | - warn!("Protocol state proof check failed: {}", err); |
| 5 | + debug!("Deserializing Mina Proof of State"); |
| 6 | + if let Err(err) = bincode::deserialize::<MinaStateProof>(proof) { |
| 7 | + warn!("Couldn't deserialize Mina Proof of State: {err}"); |
12 | 8 | return false; |
13 | 9 | } |
14 | 10 |
|
15 | | - debug!("Checking Mina protocol state public inputs"); |
16 | | - if let Err(err) = check_pub_inputs(public_input) { |
17 | | - warn!("Protocol state public inputs check failed: {}", err); |
| 11 | + debug!("Deserializing Mina Proof of State public inputs"); |
| 12 | + if let Err(err) = bincode::deserialize::<MinaStatePubInputs>(public_input) { |
| 13 | + warn!("Couldn't deserialize Mina Proof of State public inputs: {err}"); |
18 | 14 | return false; |
19 | 15 | } |
20 | 16 |
|
21 | 17 | true |
22 | 18 | } |
23 | 19 |
|
24 | | -pub fn check_hash(pub_inputs: &[u8], offset: &mut usize) -> Result<(), String> { |
25 | | - pub_inputs |
26 | | - .get(*offset..*offset + STATE_HASH_SIZE) |
27 | | - .ok_or("Failed to slice candidate hash".to_string())?; |
28 | | - |
29 | | - *offset += STATE_HASH_SIZE; |
30 | | - |
31 | | - Ok(()) |
32 | | -} |
33 | | - |
34 | | -pub fn check_state(pub_inputs: &[u8], offset: &mut usize) -> Result<(), String> { |
35 | | - let state_len: usize = pub_inputs |
36 | | - .get(*offset..*offset + 4) |
37 | | - .ok_or("Failed to slice state len".to_string()) |
38 | | - .and_then(|slice| { |
39 | | - slice |
40 | | - .try_into() |
41 | | - .map_err(|err: TryFromSliceError| err.to_string()) |
42 | | - }) |
43 | | - .map(u32::from_be_bytes) |
44 | | - .and_then(|len| usize::try_from(len).map_err(|err| err.to_string()))?; |
45 | | - |
46 | | - pub_inputs |
47 | | - .get(*offset + 4..*offset + 4 + state_len) |
48 | | - .ok_or("Failed to slice state".to_string()) |
49 | | - .and_then(|bytes| std::str::from_utf8(bytes).map_err(|err| err.to_string())) |
50 | | - .and_then(|base64| { |
51 | | - BASE64_STANDARD |
52 | | - .decode(base64) |
53 | | - .map_err(|err| err.to_string()) |
54 | | - })?; |
55 | | - *offset += 4 + state_len; |
56 | | - |
57 | | - Ok(()) |
58 | | -} |
59 | | - |
60 | | -pub fn check_pub_inputs(pub_inputs: &[u8]) -> Result<(), String> { |
61 | | - let mut offset = 0; |
62 | | - |
63 | | - check_hash(pub_inputs, &mut offset)?; // candidate ledger hash |
64 | | - check_hash(pub_inputs, &mut offset)?; // candidate state hash |
65 | | - check_hash(pub_inputs, &mut offset)?; // tip hash |
66 | | - |
67 | | - check_state(pub_inputs, &mut offset)?; // candidate state |
68 | | - check_state(pub_inputs, &mut offset)?; // tip state |
69 | | - |
70 | | - Ok(()) |
71 | | -} |
72 | | - |
73 | | -pub fn check_proof(proof_bytes: &[u8]) -> Result<(), String> { |
74 | | - std::str::from_utf8(proof_bytes) |
75 | | - .map_err(|err| err.to_string()) |
76 | | - .and_then(|base64| { |
77 | | - BASE64_URL_SAFE |
78 | | - .decode(base64) |
79 | | - .map_err(|err| err.to_string()) |
80 | | - })?; |
81 | | - Ok(()) |
82 | | -} |
83 | | - |
84 | 20 | #[cfg(test)] |
85 | 21 | mod test { |
86 | 22 | use super::verify_proof_integrity; |
|
0 commit comments