Skip to content

Commit

Permalink
Implement submission code for account proof
Browse files Browse the repository at this point in the history
  • Loading branch information
xqft committed Aug 20, 2024
1 parent 605439c commit 3c6ad97
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
58 changes: 51 additions & 7 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::{
};
use log::{error, info};
use mina_p2p_messages::v2::StateHash;
use std::{process, time::SystemTime};
use std::{fmt::format, process, time::SystemTime};

#[derive(Parser)]
#[command(version, about)]
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn main() {

match cli.command {
Command::SubmitState => {
let mina_proof = mina_polling_service::get_mina_proof_of_state(
let proof = mina_polling_service::get_mina_proof_of_state(
&rpc_url,
&proof_generator_addr,
&chain,
Expand All @@ -68,7 +68,7 @@ async fn main() {
if cli.save_proof {
std::fs::write(
"./protocol_state.pub",
mina_proof.pub_input.as_ref().unwrap_or_else(|| {
proof.pub_input.as_ref().unwrap_or_else(|| {
error!("Tried to save public inputs to file but they're missing");
process::exit(1);
}),
Expand All @@ -77,14 +77,14 @@ async fn main() {
error!("{}", err);
process::exit(1);
});
std::fs::write("./protocol_state.proof", &mina_proof.proof).unwrap_or_else(|err| {
std::fs::write("./protocol_state.proof", &proof.proof).unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
}

let verification_data = aligned_polling_service::submit(
&mina_proof,
&proof,
&chain,
&batcher_addr,
&batcher_eth_addr,
Expand All @@ -97,7 +97,7 @@ async fn main() {
process::exit(1);
});

let pub_input = mina_proof.pub_input.unwrap_or_else(|| {
let pub_input = proof.pub_input.unwrap_or_else(|| {
error!("Missing public inputs from Mina proof");
process::exit(1);
});
Expand All @@ -121,7 +121,51 @@ async fn main() {
);
}
Command::SubmitAccount { public_key } => {
todo!()
let proof = mina_polling_service::get_mina_proof_of_account(
&public_key,
&rpc_url,
&proof_generator_addr,
&chain,
&eth_rpc_url,
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

if cli.save_proof {
std::fs::write(
format!("./account_{public_key}.pub"),
proof.pub_input.as_ref().unwrap_or_else(|| {
error!("Tried to save public inputs to file but they're missing");
process::exit(1);
}),
)
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
std::fs::write(format!("./account_{public_key}.proof"), &proof.proof)
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
}

let _verification_data = aligned_polling_service::submit(
&proof,
&chain,
&batcher_addr,
&batcher_eth_addr,
&eth_rpc_url,
wallet.clone(),
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
}
}

Expand Down
47 changes: 44 additions & 3 deletions core/src/mina_polling_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use mina_curves::pasta::Fp;
use mina_p2p_messages::v2::{LedgerHash as MerkleRoot, StateHash};
use mina_tree::{FpExt, MerklePath};

use crate::{smart_contract_utility::get_tip_state_hash, utils::constants::MINA_STATE_HASH_SIZE};
use crate::{smart_contract_utility::get_tip_state_hash, utils::constants::MINA_HASH_SIZE};

type StateHashAsDecimal = String;
type PrecomputedBlockProof = String;
Expand Down Expand Up @@ -106,6 +106,47 @@ pub async fn get_mina_proof_of_state(
})
}

pub async fn get_mina_proof_of_account(
public_key: &str,
rpc_url: &str,
proof_generator_addr: &str,
chain: &Chain,
eth_rpc_url: &str,
) -> Result<VerificationData, String> {
let state_hash = get_tip_state_hash(chain, eth_rpc_url).await?;
let (merkle_root, account_hash, merkle_path) = query_merkle(
rpc_url,
&StateHash::from_fp(state_hash).to_string(),
public_key,
)
.await?;

let proof = merkle_path
.into_iter()
.flat_map(|node| {
match node {
MerklePath::Left(hash) => [vec![0], hash.to_bytes()],
MerklePath::Right(hash) => [vec![1], hash.to_bytes()],
}
.concat()
})
.collect();

let pub_input = Some([merkle_root.to_bytes(), account_hash.to_bytes()].concat());

let proof_generator_addr =
Address::from_str(proof_generator_addr).map_err(|err| err.to_string())?;

Ok(VerificationData {
proving_system: ProvingSystemId::MinaAccount,
proof,
pub_input,
verification_key: None,
vm_program_code: None,
proof_generator_addr,
})
}

pub fn query_state(
rpc_url: &str,
variables: state_query::Variables,
Expand Down Expand Up @@ -218,9 +259,9 @@ fn serialize_state_hash(hash: &StateHashAsDecimal) -> Result<Vec<u8>, String> {
let bytes = Fp::from_str(hash)
.map_err(|_| "Failed to decode hash as a field element".to_string())?
.to_bytes();
if bytes.len() != MINA_STATE_HASH_SIZE {
if bytes.len() != MINA_HASH_SIZE {
return Err(format!(
"Failed to encode hash as bytes: length is not exactly {MINA_STATE_HASH_SIZE}."
"Failed to encode hash as bytes: length is not exactly {MINA_HASH_SIZE}."
));
}
Ok(bytes)
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const HOLESKY_CHAIN_ID: u64 = 17000;
pub const MINA_TIP_PROTOCOL_STATE: &str = "Va9U7YpJjxXGg9IcS2npo+3axwra34v/JNsZW+XS4SUC8DXQX42qQSBaswvRI1uKu+UuVUvMQxEO4trzXicENbvJbooTtatm3+9bq4Z/RGzArLJ5rhTc30sJHoNjGyMZIMJX9MI+K4l1eiTChYphL4+odqeBQ7kGXhI+fVAMVM6ZIFfL2sMs61cDhApcSSi8zR029wdYaVHpph9XZ0ZqwG6Hrl43zlIWHVtuilYPo0fQlp1ItzcbT6c7N6jHva3X/Q8lE7fiEW5jIVHePd3obQSIgeHm857pq8T4H9/pXQdyGznxIVaWPq4kH76XZEfaJWK6gAb32jjhbuQvrPQmGj8SHZ9V7Apwdx2Ux2EcmXDEk+IEayOtrLW8v5kzsjs1Eww1udUeXXx0FFb4ZyBzEkGoKAJzz8bCFmj9e8bFh9DMHQIdVMT8mfe3oP365vIUYuYqfX43NCHQR0u8b5rjy3UtAh1UxPyZ97eg/frm8hRi5ip9fjc0IdBHS7xvmuPLdS1sxnDlJh772cxIxYjNovS7KSfQWcCv0HDJjtaULmZBBgAAAAAA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zdTyG80bmgeL7ljSGQdRDl6Cav6klIt2AC5Lmt1XzP5RmMAFe+grwJMx9Sy9Dh8YVM0lBzjqCEx5zq9r2kAhblYqU//r4PpYnWw5CTfPDHtsqXSoG0RF6ITuM1IIgJV7upWr8zXD38QblgSQzCTRBqRRmB0Da87xFFhlWVYAaqYE3wOWKs0l3pfqDnnUhmG4WMED/odD5FUo90d6VJf7m5ng+OysRzSJtog5ykdhgmVa9U7YpJjxXGg9IcS2npo+3axwra34v/JNsZW+XS4SX+RwUB0WiDnvvPm0OMlpbaiVi9y/86iTLi/0CEPuAjcFqsfjIB6eZmmJLgQh0VsTpNQxJwO6M+ANjEeItPGVJFHnyvUCABjRA0XVmv6t9a3AKtey/RHEtkbzQ9R8h7M3YUjDzpLDoBAf4iAf7kGwf+cAgA/AAEsuWPAQAA";
pub const MINA_TIP_STATE_HASH_FIELD: &str =
"26201757517054449641912404249424749469164718222967816857204695395894215860942";
pub const MINA_STATE_HASH_SIZE: usize = 32;
pub const MINA_HASH_SIZE: usize = 32;

// Bridge related constants
pub const BRIDGE_DEVNET_ETH_ADDR: &str = "0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35";
Expand Down

0 comments on commit 3c6ad97

Please sign in to comment.