From e46b6f553362f491dcf9b1806c2e2cb8b016cde0 Mon Sep 17 00:00:00 2001 From: gabrielbosio Date: Thu, 19 Sep 2024 15:01:30 -0300 Subject: [PATCH] Refactor input size check --- operator/mina/lib/src/lib.rs | 25 ++++++++++++------------- operator/mina_account/lib/src/lib.rs | 12 ++++++------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/operator/mina/lib/src/lib.rs b/operator/mina/lib/src/lib.rs index e2c945113..fc31e89bf 100644 --- a/operator/mina/lib/src/lib.rs +++ b/operator/mina/lib/src/lib.rs @@ -33,31 +33,30 @@ pub extern "C" fn verify_mina_state_ffi( pub_input_buffer: &[u8; MAX_PUB_INPUT_SIZE], pub_input_len: usize, ) -> bool { - if proof_len > MAX_PROOF_SIZE { + let Some(proof_buffer_slice) = proof_buffer.get(..proof_len) else { eprintln!("Proof length argument is greater than max proof size"); return false; - } + }; - if pub_input_len > MAX_PUB_INPUT_SIZE { + let Some(pub_input_buffer_slice) = pub_input_buffer.get(..pub_input_len) else { eprintln!("Public input length argument is greater than max public input size"); return false; - } + }; - let proof: MinaStateProof = match bincode::deserialize(&proof_buffer[..proof_len]) { + let proof: MinaStateProof = match bincode::deserialize(proof_buffer_slice) { Ok(proof) => proof, Err(err) => { eprintln!("Failed to deserialize state proof: {}", err); return false; } }; - let pub_inputs: MinaStatePubInputs = - match bincode::deserialize(&pub_input_buffer[..pub_input_len]) { - Ok(pub_inputs) => pub_inputs, - Err(err) => { - eprintln!("Failed to deserialize state pub inputs: {}", err); - return false; - } - }; + let pub_inputs: MinaStatePubInputs = match bincode::deserialize(pub_input_buffer_slice) { + Ok(pub_inputs) => pub_inputs, + Err(err) => { + eprintln!("Failed to deserialize state pub inputs: {}", err); + return false; + } + }; // Checks the integrity of the public inputs, also checks if the states form a chain. let (candidate_tip_state, bridge_tip_state, candidate_tip_state_hash) = diff --git a/operator/mina_account/lib/src/lib.rs b/operator/mina_account/lib/src/lib.rs index 258467131..1b15c722d 100644 --- a/operator/mina_account/lib/src/lib.rs +++ b/operator/mina_account/lib/src/lib.rs @@ -19,20 +19,20 @@ pub extern "C" fn verify_account_inclusion_ffi( pub_input_buffer: &[u8; MAX_PUB_INPUT_SIZE], pub_input_len: usize, ) -> bool { - if proof_len > MAX_PROOF_SIZE { + let Some(proof_buffer_slice) = proof_buffer.get(..proof_len) else { eprintln!("Proof length argument is greater than max proof size"); return false; - } + }; - if pub_input_len > MAX_PUB_INPUT_SIZE { + let Some(pub_input_buffer_slice) = pub_input_buffer.get(..pub_input_len) else { eprintln!("Public input length argument is greater than max public input size"); return false; - } + }; let MinaAccountProof { merkle_path, account, - } = match bincode::deserialize(&proof_buffer[..proof_len]) { + } = match bincode::deserialize(proof_buffer_slice) { Ok(proof) => proof, Err(err) => { eprintln!("Failed to deserialize account proof: {}", err); @@ -42,7 +42,7 @@ pub extern "C" fn verify_account_inclusion_ffi( let MinaAccountPubInputs { ledger_hash, encoded_account, - } = match bincode::deserialize(&pub_input_buffer[..pub_input_len]) { + } = match bincode::deserialize(pub_input_buffer_slice) { Ok(pub_inputs) => pub_inputs, Err(err) => { eprintln!("Failed to deserialize account pub inputs: {}", err);