Skip to content

Commit

Permalink
feat: Add scaffolding for Plonk verification in Solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
storojs72 committed Jun 18, 2024
1 parent f7fe55f commit 10f5673
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 125 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[submodule "aptos/solidity/contracts/lib/forge-std"]
path = aptos/solidity/contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
# https://stackoverflow.com/questions/5126765/how-to-get-rid-of-git-submodules-untracked-status
ignore = dirty
2 changes: 2 additions & 0 deletions aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"light-client",
"aptos-programs",
"proof-server",
"solidity/script",
]

[workspace.dependencies]
Expand Down Expand Up @@ -49,6 +50,7 @@ sphinx-derive = { git = "ssh://[email protected]/lurk-lab/sphinx", branch = "plonk"
sphinx-sdk = { git = "ssh://[email protected]/lurk-lab/sphinx", branch = "plonk", features = ["plonk"] }
sphinx-zkvm = { git = "ssh://[email protected]/lurk-lab/sphinx", branch = "plonk" }
sphinx-helper = { git = "ssh://[email protected]/lurk-lab/sphinx", branch = "plonk" }
sphinx-prover = { git = "ssh://[email protected]/lurk-lab/sphinx", branch = "plonk" }
tokio = "1.37"
tokio-stream = "0.1"

Expand Down
2 changes: 1 addition & 1 deletion aptos/light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ anyhow = { workspace = true }
getset = { workspace = true }
serde = { workspace = true, features = ["derive", "rc"] }
thiserror = { workspace = true }
bcs = { workspace = true }

[dev-dependencies]
aptos-programs = { path = "../aptos-programs", features = ["bench"] }
bcs = { workspace = true }
cfg-if = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
95 changes: 30 additions & 65 deletions aptos/light-client/src/epoch_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ struct EpochChangeOutput {
new_validator_verifier_hash: [u8; 32],
}

#[cfg(feature = "aptos")]
pub fn setup_assets() -> (Vec<u8>, Vec<u8>) {
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper = AptosWrapper::new(20000, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();

let trusted_state = bcs::to_bytes(aptos_wrapper.trusted_state()).unwrap();
let trusted_state_version = *aptos_wrapper.current_version();

aptos_wrapper.generate_traffic().unwrap();

let state_proof = aptos_wrapper
.new_state_proof(trusted_state_version)
.unwrap();

let epoch_change_proof = bcs::to_bytes(state_proof.epoch_changes()).unwrap();

(trusted_state, epoch_change_proof)
}

pub fn generate_stdin(current_trusted_state: &[u8], epoch_change_proof: &[u8]) -> SphinxStdin {
let mut stdin = SphinxStdin::new();
stdin.write(&current_trusted_state);
Expand Down Expand Up @@ -57,6 +80,7 @@ fn prove_epoch_change(

#[cfg(all(test, feature = "aptos"))]
mod test {
use crate::epoch_change::setup_assets;
use crate::error::LightClientError;
use sphinx_sdk::utils::setup_logger;
use sphinx_sdk::{ProverClient, SphinxStdin};
Expand Down Expand Up @@ -85,69 +109,31 @@ mod test {

#[test]
fn test_execute_epoch_change() {
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;
use std::time::Instant;

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper =
AptosWrapper::new(20000, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();

let trusted_state = bcs::to_bytes(aptos_wrapper.trusted_state()).unwrap();
let trusted_state_version = *aptos_wrapper.current_version();

aptos_wrapper.generate_traffic().unwrap();

let state_proof = aptos_wrapper
.new_state_proof(trusted_state_version)
.unwrap();

let epoch_change_proof = &bcs::to_bytes(state_proof.epoch_changes()).unwrap();
let (trusted_state, epoch_change_proof) = setup_assets();

println!("Starting execution of prove_epoch_change...");
let start = Instant::now();
execute_epoch_change(&trusted_state, epoch_change_proof).unwrap();
execute_epoch_change(&trusted_state, &epoch_change_proof).unwrap();
println!("Execution took {:?}", start.elapsed());
}

#[test]
#[ignore = "This test is too slow for CI"]
fn test_prove_epoch_change() {
use super::*;
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;
use aptos_lc_core::crypto::hash::CryptoHash;
use aptos_lc_core::types::trusted_state::TrustedState;
use sphinx_sdk::ProverClient;
use std::time::Instant;

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper =
AptosWrapper::new(20000, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();

let trusted_state = bcs::to_bytes(aptos_wrapper.trusted_state()).unwrap();
let validator_verifier_hash = match TrustedState::from_bytes(&trusted_state).unwrap() {
TrustedState::EpochState { epoch_state, .. } => epoch_state.verifier().hash().to_vec(),
_ => panic!("Expected epoch change for current trusted state"),
};
let trusted_state_version = *aptos_wrapper.current_version();

aptos_wrapper.generate_traffic().unwrap();

let state_proof = aptos_wrapper
.new_state_proof(trusted_state_version)
.unwrap();

let epoch_change_proof = &bcs::to_bytes(state_proof.epoch_changes()).unwrap();
let (trusted_state, epoch_change_proof) = setup_assets();

let client = ProverClient::new();

let start = Instant::now();
println!("Starting generation of prove_epoch_change proof...");
let (proof, output) =
prove_epoch_change(&client, &trusted_state, epoch_change_proof).unwrap();
prove_epoch_change(&client, &trusted_state, &epoch_change_proof).unwrap();
println!("Proving took {:?}", start.elapsed());

assert_eq!(
Expand All @@ -166,38 +152,17 @@ mod test {
#[ignore = "This test is too slow for CI"]
fn test_snark_prove_epoch_change() {
use super::*;
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;
use aptos_lc_core::crypto::hash::CryptoHash;
use aptos_lc_core::types::trusted_state::TrustedState;
use sphinx_sdk::ProverClient;
use std::time::Instant;

setup_logger();

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper = AptosWrapper::new(5, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();

let trusted_state = bcs::to_bytes(aptos_wrapper.trusted_state()).unwrap();
let _validator_verifier_hash = match TrustedState::from_bytes(&trusted_state).unwrap() {
TrustedState::EpochState { epoch_state, .. } => epoch_state.verifier().hash().to_vec(),
_ => panic!("Expected epoch change for current trusted state"),
};
let trusted_state_version = *aptos_wrapper.current_version();

aptos_wrapper.generate_traffic().unwrap();

let state_proof = aptos_wrapper
.new_state_proof(trusted_state_version)
.unwrap();

let epoch_change_proof = &bcs::to_bytes(state_proof.epoch_changes()).unwrap();
let (trusted_state, epoch_change_proof) = setup_assets();

let client = ProverClient::new();
let (pk, vk) = client.setup(aptos_programs::EPOCH_CHANGE_PROGRAM);

let stdin = generate_stdin(trusted_state.as_slice(), epoch_change_proof);
let stdin = generate_stdin(trusted_state.as_slice(), &epoch_change_proof);

let start = Instant::now();
println!("Starting generation of prove_epoch_change proof...");
Expand Down
118 changes: 59 additions & 59 deletions aptos/light-client/src/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,64 @@ impl ValidatorVerifierAssets {
}
}

#[cfg(feature = "aptos")]
pub fn setup_assets() -> (
SparseMerkleProofAssets,
TransactionProofAssets,
ValidatorVerifierAssets,
) {
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;
use aptos_lc_core::types::trusted_state::TrustedState;

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper = AptosWrapper::new(500, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();
aptos_wrapper.generate_traffic().unwrap();

let proof_assets = aptos_wrapper.get_latest_proof_account(400).unwrap();

let sparse_merkle_proof = bcs::to_bytes(proof_assets.state_proof()).unwrap();
let key: [u8; 32] = *proof_assets.key().as_ref();
let element_hash: [u8; 32] = *proof_assets.state_value_hash().unwrap().as_ref();

let transaction = bcs::to_bytes(&proof_assets.transaction()).unwrap();
let transaction_proof = bcs::to_bytes(&proof_assets.transaction_proof()).unwrap();

let latest_li = aptos_wrapper.get_latest_li_bytes().unwrap();

let validator_verifier =
match TrustedState::from_bytes(&bcs::to_bytes(&aptos_wrapper.trusted_state()).unwrap())
.unwrap()
{
TrustedState::EpochState { epoch_state, .. } => epoch_state.verifier().clone(),
_ => panic!("expected epoch state"),
};

let sparse_merkle_proof_assets = SparseMerkleProofAssets {
sparse_merkle_proof,
leaf_key: key,
leaf_hash: element_hash,
};

let transaction_proof_assets = TransactionProofAssets {
transaction,
transaction_index: *proof_assets.transaction_version(),
transaction_proof,
latest_li,
};

let validator_verifier_assets = ValidatorVerifierAssets {
validator_verifier: validator_verifier.to_bytes(),
};

(
sparse_merkle_proof_assets,
transaction_proof_assets,
validator_verifier_assets,
)
}

pub fn generate_stdin(
sparse_merkle_proof_assets: &SparseMerkleProofAssets,
transaction_proof_assets: &TransactionProofAssets,
Expand Down Expand Up @@ -141,70 +199,12 @@ fn prove_inclusion(
mod test {
use crate::error::LightClientError;
use crate::inclusion::{
SparseMerkleProofAssets, TransactionProofAssets, ValidatorVerifierAssets,
setup_assets, SparseMerkleProofAssets, TransactionProofAssets, ValidatorVerifierAssets,
};
use aptos_lc_core::types::validator::ValidatorVerifier;
use sphinx_sdk::utils::setup_logger;
use sphinx_sdk::{ProverClient, SphinxStdin};

fn setup_assets() -> (
SparseMerkleProofAssets,
TransactionProofAssets,
ValidatorVerifierAssets,
) {
use aptos_lc_core::aptos_test_utils::wrapper::AptosWrapper;
use aptos_lc_core::types::trusted_state::TrustedState;

const NBR_VALIDATORS: usize = 130;
const AVERAGE_SIGNERS_NBR: usize = 95;

let mut aptos_wrapper =
AptosWrapper::new(500, NBR_VALIDATORS, AVERAGE_SIGNERS_NBR).unwrap();
aptos_wrapper.generate_traffic().unwrap();

let proof_assets = aptos_wrapper.get_latest_proof_account(400).unwrap();

let sparse_merkle_proof = bcs::to_bytes(proof_assets.state_proof()).unwrap();
let key: [u8; 32] = *proof_assets.key().as_ref();
let element_hash: [u8; 32] = *proof_assets.state_value_hash().unwrap().as_ref();

let transaction = bcs::to_bytes(&proof_assets.transaction()).unwrap();
let transaction_proof = bcs::to_bytes(&proof_assets.transaction_proof()).unwrap();

let latest_li = aptos_wrapper.get_latest_li_bytes().unwrap();

let validator_verifier =
match TrustedState::from_bytes(&bcs::to_bytes(&aptos_wrapper.trusted_state()).unwrap())
.unwrap()
{
TrustedState::EpochState { epoch_state, .. } => epoch_state.verifier().clone(),
_ => panic!("expected epoch state"),
};

let sparse_merkle_proof_assets = SparseMerkleProofAssets {
sparse_merkle_proof,
leaf_key: key,
leaf_hash: element_hash,
};

let transaction_proof_assets = TransactionProofAssets {
transaction,
transaction_index: *proof_assets.transaction_version(),
transaction_proof,
latest_li,
};

let validator_verifier_assets = ValidatorVerifierAssets {
validator_verifier: validator_verifier.to_bytes(),
};

(
sparse_merkle_proof_assets,
transaction_proof_assets,
validator_verifier_assets,
)
}

fn execute_inclusion(
sparse_merkle_proof_assets: &SparseMerkleProofAssets,
transaction_proof_assets: &TransactionProofAssets,
Expand Down
14 changes: 14 additions & 0 deletions aptos/solidity/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
3 changes: 3 additions & 0 deletions aptos/solidity/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## On-chain verification

TODO
6 changes: 6 additions & 0 deletions aptos/solidity/contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

fs_permissions = [{ access = "read-write", path = "./"}]
1 change: 1 addition & 0 deletions aptos/solidity/contracts/lib/forge-std
Submodule forge-std added at 978ac6
18 changes: 18 additions & 0 deletions aptos/solidity/script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
version = "0.1.0"
name = "fixture-generator"
edition = "2021"

[[bin]]
name = "generate-fixture"
path = "src/bin/main.rs"

[dependencies]
sphinx-sdk = { workspace = true }
sphinx-prover = {workspace = true }
aptos-lc = { path = "../../light-client" }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
clap = { version = "4.0", features = ["derive", "env"] }
tracing = "0.1.40"
alloy-sol-types = "0.7.2"
3 changes: 3 additions & 0 deletions aptos/solidity/script/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-04-17"
components = ["llvm-tools", "rustc-dev"]
Loading

0 comments on commit 10f5673

Please sign in to comment.