Skip to content

Commit

Permalink
Add ledger hash to state verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
xqft committed Aug 21, 2024
1 parent 3c6ad97 commit d944c61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
17 changes: 13 additions & 4 deletions contract/src/MinaBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ error TipStateIsWrong(bytes32 pubInputTipStateHash, bytes32 tipStatehash);
contract MinaBridge {
/// @notice The state hash of the last verified state as a Fp.
bytes32 tipStateHash;
/// @notice The ledger hash of the last verified state as a Fp.
bytes32 tipLedgerHash;

/// @notice Reference to the AlignedLayerServiceManager contract.
AlignedLayerServiceManager aligned;
Expand All @@ -19,7 +21,7 @@ contract MinaBridge {
tipStateHash = _tipStateHash;
}

/// @notice Returns the last verified state hash, or the root state hash if none.
/// @notice Returns the last verified state hash.
function getTipStateHash() external view returns (bytes32) {
return tipStateHash;
}
Expand All @@ -33,9 +35,14 @@ contract MinaBridge {
uint256 verificationDataBatchIndex,
bytes memory pubInput
) external {
bytes32 candidateMerkleRoot;
assembly {
candidateMerkleRoot := mload(add(pubInput, 0x20))
}

bytes32 pubInputTipStateHash;
assembly {
pubInputTipStateHash := mload(add(pubInput, 0x40))
pubInputTipStateHash := mload(add(pubInput, 0x60))
}

if (pubInputTipStateHash != tipStateHash) {
Expand All @@ -55,9 +62,11 @@ contract MinaBridge {
);

if (isNewStateVerified) {
// first 32 bytes of pub input is the candidate (now verified) state hash.
// first 32 bytes of pub input is the candidate (now verified) ledger hash.
// second 32 bytes of pub input is the candidate (now verified) state hash.
assembly {
sstore(tipStateHash.slot, mload(add(pubInput, 0x20)))
sstore(tipLedgerHash.slot, mload(add(pubInput, 0x20)))
sstore(tipStateHash.slot, mload(add(pubInput, 0x40)))
}
} else {
revert NewStateIsNotValid();
Expand Down
32 changes: 30 additions & 2 deletions core/src/mina_polling_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::str::FromStr as _;

use aligned_sdk::core::types::{Chain, ProvingSystemId, VerificationData};
use base64::prelude::*;
use ethers::types::Address;
use graphql_client::{
reqwest::{post_graphql, post_graphql_blocking},
Expand All @@ -9,7 +10,10 @@ use graphql_client::{
use kimchi::{o1_utils::FieldHelpers, turshi::helper::CairoFieldHelpers};
use log::{debug, info};
use mina_curves::pasta::Fp;
use mina_p2p_messages::v2::{LedgerHash as MerkleRoot, StateHash};
use mina_p2p_messages::{
binprot::BinProtRead,
v2::{LedgerHash as MerkleRoot, MinaStateProtocolStateValueStableV2, StateHash},
};
use mina_tree::{FpExt, MerklePath};

use crate::{smart_contract_utility::get_tip_state_hash, utils::constants::MINA_HASH_SIZE};
Expand Down Expand Up @@ -81,11 +85,14 @@ pub async fn get_mina_proof_of_state(
let tip_hash = serialize_state_hash(&tip_hash)?;
let tip_state = serialize_state(tip_state);

let candidate_merkle_root = serialize_ledger_hash(&candidate_state)?;

let candidate_hash = serialize_state_hash(&candidate_hash)?;
let candidate_state = serialize_state(candidate_state);
let candidate_proof = serialize_state_proof(&candidate_proof);

let mut pub_input = candidate_hash;
let mut pub_input = candidate_merkle_root;
pub_input.extend(candidate_hash);
pub_input.extend(tip_hash);
pub_input.extend((candidate_state.len() as u32).to_be_bytes());
pub_input.extend(candidate_state);
Expand Down Expand Up @@ -280,3 +287,24 @@ fn encode_state_hash(hash: &StateHashAsDecimal) -> Result<String, String> {
.map_err(|_| "Failed to decode hash as a field element".to_string())
.map(|fp| StateHash::from_fp(fp).to_string())
}

fn serialize_ledger_hash(state: &ProtocolState) -> Result<Vec<u8>, String> {
BASE64_STANDARD
.decode(state)
.map_err(|err| err.to_string())
.and_then(|binprot| {
MinaStateProtocolStateValueStableV2::binprot_read(&mut binprot.as_slice())
.map_err(|err| err.to_string())
})
.and_then(|state| {
state
.body
.blockchain_state
.staged_ledger_hash
.non_snark
.ledger_hash
.to_fp()
.map_err(|err| err.to_string())
})
.map(|ledger_hash| ledger_hash.to_bytes())
}

0 comments on commit d944c61

Please sign in to comment.