Skip to content

Commit 43fb3c3

Browse files
committed
Add state size in pub input deserialization
1 parent a6a464b commit 43fb3c3

2 files changed

Lines changed: 46 additions & 37 deletions

File tree

  • batcher/aligned-batcher/src/mina
  • operator/mina/lib/src

batcher/aligned-batcher/src/mina/mod.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use base64::prelude::*;
22
use log::{debug, warn};
33

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 = 2060;
4+
const PROTOCOL_STATE_HASH_SIZE: usize = 32;
75

86
pub fn verify_protocol_state_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool {
97
debug!("Checking Mina protocol state proof");
@@ -33,25 +31,31 @@ pub fn check_protocol_state_proof(protocol_state_proof_bytes: &[u8]) -> Result<(
3331
}
3432

3533
pub fn check_protocol_state_pub(protocol_state_pub: &[u8]) -> Result<(), String> {
34+
let candidate_offset = parse_protocol_state_with_hash(&protocol_state_pub)?;
35+
36+
let _ = parse_protocol_state_with_hash(&protocol_state_pub[candidate_offset..])?;
37+
38+
Ok(())
39+
}
40+
41+
fn parse_protocol_state_with_hash(protocol_state_pub: &[u8]) -> Result<usize, String> {
3642
// TODO(xqft): check hash and binprot deserialization
37-
let candidate_protocol_state_base64 = std::str::from_utf8(
38-
&protocol_state_pub[STATE_HASH_SIZE..(STATE_HASH_SIZE + PROTOCOL_STATE_SIZE)],
39-
)
40-
.map_err(|err| err.to_string())?;
41-
BASE64_STANDARD
42-
.decode(candidate_protocol_state_base64)
43-
.map_err(|err| err.to_string())?;
43+
let mut protocol_state_size_bytes = [0u8; 4];
44+
protocol_state_size_bytes.copy_from_slice(
45+
&protocol_state_pub[PROTOCOL_STATE_HASH_SIZE..(PROTOCOL_STATE_HASH_SIZE + 4)],
46+
);
47+
let protocol_state_size = u32::from_be_bytes(protocol_state_size_bytes) as usize;
4448

45-
let tip_protocol_state_base64 = std::str::from_utf8(
46-
&protocol_state_pub[(STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) + STATE_HASH_SIZE
47-
..((STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) * 2)],
49+
let protocol_state_base64 = std::str::from_utf8(
50+
&protocol_state_pub
51+
[(PROTOCOL_STATE_HASH_SIZE + 4)..(PROTOCOL_STATE_HASH_SIZE + 4 + protocol_state_size)],
4852
)
4953
.map_err(|err| err.to_string())?;
5054
BASE64_STANDARD
51-
.decode(tip_protocol_state_base64)
55+
.decode(protocol_state_base64)
5256
.map_err(|err| err.to_string())?;
5357

54-
Ok(())
58+
Ok(PROTOCOL_STATE_HASH_SIZE + 4 + protocol_state_size)
5559
}
5660

5761
#[cfg(test)]

operator/mina/lib/src/lib.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ lazy_static! {
2525
const MAX_PROOF_SIZE: usize = 16 * 1024;
2626
const MAX_PUB_INPUT_SIZE: usize = 6 * 1024;
2727
const PROTOCOL_STATE_HASH_SIZE: usize = 32;
28-
// TODO(gabrielbosio): check that this length is always the same for every block
29-
const PROTOCOL_STATE_SIZE: usize = 2060;
3028

3129
#[no_mangle]
3230
pub extern "C" fn verify_protocol_state_proof_ffi(
@@ -110,14 +108,13 @@ pub fn parse_protocol_state_pub(
110108
),
111109
String,
112110
> {
113-
let (tip_protocol_state_hash, tip_protocol_state) = parse_protocol_state_with_hash(
114-
&protocol_state_pub[..(PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE)],
115-
)?;
111+
let (tip_protocol_state_hash, tip_protocol_state, candidate_offset) =
112+
parse_protocol_state_with_hash(&protocol_state_pub)?;
116113

117-
let (candidate_protocol_state_hash, candidate_protocol_state) = parse_protocol_state_with_hash(
118-
&protocol_state_pub[(PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE)
119-
..((PROTOCOL_STATE_HASH_SIZE + PROTOCOL_STATE_SIZE) * 2)],
120-
)?;
114+
let (candidate_protocol_state_hash, candidate_protocol_state, protocol_state_pub_len) =
115+
parse_protocol_state_with_hash(&protocol_state_pub[candidate_offset..])?;
116+
117+
debug_assert_eq!(protocol_state_pub_len, protocol_state_pub.len());
121118

122119
Ok((
123120
tip_protocol_state_hash,
@@ -129,25 +126,33 @@ pub fn parse_protocol_state_pub(
129126

130127
fn parse_protocol_state_with_hash(
131128
protocol_state_pub: &[u8],
132-
) -> Result<
133-
(
134-
ark_ff::Fp256<mina_curves::pasta::fields::FpParameters>,
135-
MinaStateProtocolStateValueStableV2,
136-
),
137-
String,
138-
> {
139-
let protocol_state_hash =
140-
Fp::from_bytes(&protocol_state_pub[..32]).map_err(|err| err.to_string())?;
141-
let protocol_state_base64 =
142-
std::str::from_utf8(&protocol_state_pub[32..]).map_err(|err| err.to_string())?;
129+
) -> Result<(Fp, MinaStateProtocolStateValueStableV2, usize), String> {
130+
let protocol_state_hash = Fp::from_bytes(&protocol_state_pub[..PROTOCOL_STATE_HASH_SIZE])
131+
.map_err(|err| err.to_string())?;
132+
133+
let mut protocol_state_size_bytes = [0u8; 4];
134+
protocol_state_size_bytes.copy_from_slice(
135+
&protocol_state_pub[PROTOCOL_STATE_HASH_SIZE..(PROTOCOL_STATE_HASH_SIZE + 4)],
136+
);
137+
let protocol_state_size = u32::from_be_bytes(protocol_state_size_bytes) as usize;
138+
139+
let protocol_state_base64 = std::str::from_utf8(
140+
&protocol_state_pub
141+
[(PROTOCOL_STATE_HASH_SIZE + 4)..(PROTOCOL_STATE_HASH_SIZE + 4 + protocol_state_size)],
142+
)
143+
.map_err(|err| err.to_string())?;
143144
let protocol_state_binprot = BASE64_STANDARD
144145
.decode(protocol_state_base64)
145146
.map_err(|err| err.to_string())?;
146147
let protocol_state =
147148
MinaStateProtocolStateValueStableV2::binprot_read(&mut protocol_state_binprot.as_slice())
148149
.map_err(|err| err.to_string())?;
149150

150-
Ok((protocol_state_hash, protocol_state))
151+
Ok((
152+
protocol_state_hash,
153+
protocol_state,
154+
PROTOCOL_STATE_HASH_SIZE + 4 + protocol_state_size,
155+
))
151156
}
152157

153158
#[cfg(test)]
@@ -159,7 +164,7 @@ mod test {
159164
const PROTOCOL_STATE_PUB_BYTES: &[u8] =
160165
include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state.pub");
161166
const BAD_PROTOCOL_STATE_PUB_BYTES: &[u8] =
162-
include_bytes!("../../../../batcher/aligned/test_files/mina/bad_protocol_state.pub");
167+
include_bytes!("../../../../batcher/aligned/test_files/mina/protocol_state_bad_hash.pub");
163168
// BAD_PROTOCOL_STATE_PUB_BYTES has an invalid hash.
164169

165170
#[test]

0 commit comments

Comments
 (0)