Skip to content

Commit

Permalink
Merge pull request #22 from lambdaclass/relative_finalization
Browse files Browse the repository at this point in the history
Relative finalization
  • Loading branch information
xqft authored Sep 3, 2024
2 parents ce5f842 + 0afb987 commit a85d7f9
Show file tree
Hide file tree
Showing 14 changed files with 7,617 additions and 1,788 deletions.
2,637 changes: 2,151 additions & 486 deletions batcher/Cargo.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions batcher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[workspace]
members = [
"aligned-batcher",
"aligned-sdk",
"aligned"
]
members = ["aligned-batcher", "aligned-sdk", "aligned"]
resolver = "2"

[patch.crates-io]
ark-ff = { git = "https://github.com/openmina/algebra", branch = "openmina" }
ark-ec = { git = "https://github.com/openmina/algebra", branch = "openmina" }
ark-poly = { git = "https://github.com/openmina/algebra", branch = "openmina" }
ark-serialize = { git = "https://github.com/openmina/algebra", branch = "openmina" }
1 change: 1 addition & 0 deletions batcher/aligned-batcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
tokio-tungstenite = "0.21.0"
mina_bridge_core = { git = "https://github.com/lambdaclass/mina_bridge.git", branch = "relative_finalization" }
futures-util = "0.3.30"
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "macros"] }
log = "0.4.21"
Expand Down
78 changes: 7 additions & 71 deletions batcher/aligned-batcher/src/mina/mod.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,22 @@
use std::array::TryFromSliceError;

use base64::prelude::*;
use log::{debug, warn};

const STATE_HASH_SIZE: usize = 32;
use mina_bridge_core::proof::state_proof::{MinaStateProof, MinaStatePubInputs};

pub fn verify_proof_integrity(proof: &[u8], public_input: &[u8]) -> bool {
debug!("Checking Mina protocol state proof");
if let Err(err) = check_proof(proof) {
warn!("Protocol state proof check failed: {}", err);
debug!("Deserializing Mina Proof of State");
if let Err(err) = bincode::deserialize::<MinaStateProof>(proof) {
warn!("Couldn't deserialize Mina Proof of State: {err}");
return false;
}

debug!("Checking Mina protocol state public inputs");
if let Err(err) = check_pub_inputs(public_input) {
warn!("Protocol state public inputs check failed: {}", err);
debug!("Deserializing Mina Proof of State public inputs");
if let Err(err) = bincode::deserialize::<MinaStatePubInputs>(public_input) {
warn!("Couldn't deserialize Mina Proof of State public inputs: {err}");
return false;
}

true
}

pub fn check_hash(pub_inputs: &[u8], offset: &mut usize) -> Result<(), String> {
pub_inputs
.get(*offset..*offset + STATE_HASH_SIZE)
.ok_or("Failed to slice candidate hash".to_string())?;

*offset += STATE_HASH_SIZE;

Ok(())
}

pub fn check_state(pub_inputs: &[u8], offset: &mut usize) -> Result<(), String> {
let state_len: usize = pub_inputs
.get(*offset..*offset + 4)
.ok_or("Failed to slice state len".to_string())
.and_then(|slice| {
slice
.try_into()
.map_err(|err: TryFromSliceError| err.to_string())
})
.map(u32::from_be_bytes)
.and_then(|len| usize::try_from(len).map_err(|err| err.to_string()))?;

pub_inputs
.get(*offset + 4..*offset + 4 + state_len)
.ok_or("Failed to slice state".to_string())
.and_then(|bytes| std::str::from_utf8(bytes).map_err(|err| err.to_string()))
.and_then(|base64| {
BASE64_STANDARD
.decode(base64)
.map_err(|err| err.to_string())
})?;
*offset += 4 + state_len;

Ok(())
}

pub fn check_pub_inputs(pub_inputs: &[u8]) -> Result<(), String> {
let mut offset = 0;

check_hash(pub_inputs, &mut offset)?; // candidate ledger hash
check_hash(pub_inputs, &mut offset)?; // candidate state hash
check_hash(pub_inputs, &mut offset)?; // tip hash

check_state(pub_inputs, &mut offset)?; // candidate state
check_state(pub_inputs, &mut offset)?; // tip state

Ok(())
}

pub fn check_proof(proof_bytes: &[u8]) -> Result<(), String> {
std::str::from_utf8(proof_bytes)
.map_err(|err| err.to_string())
.and_then(|base64| {
BASE64_URL_SAFE
.decode(base64)
.map_err(|err| err.to_string())
})?;
Ok(())
}

#[cfg(test)]
mod test {
use super::verify_proof_integrity;
Expand Down
Binary file modified batcher/aligned/test_files/mina/protocol_state.proof
Binary file not shown.
Binary file modified batcher/aligned/test_files/mina/protocol_state.pub
Binary file not shown.
Loading

0 comments on commit a85d7f9

Please sign in to comment.