Skip to content

Commit

Permalink
fix: optimize tendermint zk light client (#273)
Browse files Browse the repository at this point in the history
Co-authored-by: Uma Roy <uma.roy.us@gmail.com>
  • Loading branch information
i-m-aditya and puma314 committed Mar 8, 2024
1 parent c22dc58 commit c3f906a
Show file tree
Hide file tree
Showing 37 changed files with 4,732 additions and 259 deletions.
Binary file modified examples/chess/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/io/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/json/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/regex/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/rsa/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/ssz-withdrawals/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
933 changes: 933 additions & 0 deletions examples/tendermint-benchmark/program/Cargo.lock

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions examples/tendermint-benchmark/program/Cargo.toml
@@ -0,0 +1,19 @@
[workspace]
[package]
version = "0.1.0"
name = "tendermint-benchmark-program"
edition = "2021"

[dependencies]
sp1-zkvm = { path = "../../../zkvm/entrypoint" }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
tendermint = { version = "0.34.0", default-features = false }
tendermint-light-client-verifier = { version = "0.34.0", default-features = false, features = [
"rust-crypto",
] }

[patch.crates-io]
sha2-v0-9-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-v0.9.8" }
sha2-v0-10-6 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-v0.10.6" }
ed25519-consensus = { git = "https://github.com/sp1-patches/ed25519-consensus", branch = "patch-v2.1.0" }
Binary file not shown.
127 changes: 127 additions & 0 deletions examples/tendermint-benchmark/program/src/main.rs
@@ -0,0 +1,127 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

use core::time::Duration;
use serde::Deserialize;
use tendermint::{node::Id, validator::Info};
use tendermint_light_client_verifier::{
options::Options,
types::{LightBlock, SignedHeader, ValidatorSet},
ProdVerifier, Verdict, Verifier,
};

#[derive(Debug, Deserialize)]
pub struct CommitResponse {
pub result: SignedHeaderWrapper,
}

#[derive(Debug, Deserialize)]
pub struct SignedHeaderWrapper {
pub signed_header: SignedHeader,
}

#[derive(Debug, Deserialize)]
pub struct ValidatorSetResponse {
pub result: BlockValidatorSet,
}

#[derive(Debug, Deserialize)]
pub struct BlockValidatorSet {
pub block_height: String,
pub validators: Vec<Info>,
pub count: String,
pub total: String,
}

fn main() {
let peer_id: [u8; 20] = [
0x72, 0x6b, 0xc8, 0xd2, 0x60, 0x38, 0x7c, 0xf5, 0x6e, 0xcf, 0xad, 0x3a, 0x6b, 0xf6, 0xfe,
0xcd, 0x90, 0x3e, 0x18, 0xa2,
];

println!("cycle-tracker-start: io");
// Generate the Light Block's without testgen
let file_content = include_bytes!("./fixtures/1/signed_header.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");

let commit_response: CommitResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let signed_header = commit_response.result.signed_header;

let file_content = include_bytes!("./fixtures/1/validators.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");
let validators_response: ValidatorSetResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let validators = validators_response.result;
let validators = ValidatorSet::new(validators.validators, None);

let file_content = include_bytes!("./fixtures/1/next_validators.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");
let next_validators_response: ValidatorSetResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let next_validators = next_validators_response.result;
let next_validators = ValidatorSet::new(next_validators.validators, None);

// Create a default light block with a valid chain-id for height `1` with a timestamp 20
// secs before now (to be treated as trusted state)
let light_block_1: LightBlock =
LightBlock::new(signed_header, validators, next_validators, Id::new(peer_id));

// // Generate the Light Block's without testgen
let file_content = include_bytes!("./fixtures/2/signed_header.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");

let commit_response: CommitResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let signed_header = commit_response.result.signed_header;

let file_content = include_bytes!("./fixtures/2/validators.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");
let validators_response: ValidatorSetResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let validators = validators_response.result;
let validators = ValidatorSet::new(validators.validators, None);

let file_content = include_bytes!("./fixtures/2/next_validators.json");
let file_content_str =
core::str::from_utf8(file_content).expect("Failed to convert file content to string");
let next_validators_response: ValidatorSetResponse =
serde_json::from_str(file_content_str).expect("Failed to parse JSON");
let next_validators = next_validators_response.result;
let next_validators = ValidatorSet::new(next_validators.validators, None);
// Create a default light block with a valid chain-id for height `1` with a timestamp 20
// secs before now (to be treated as trusted state)
let light_block_2: LightBlock =
LightBlock::new(signed_header, validators, next_validators, Id::new(peer_id));
println!("cycle-tracker-end: io");

println!("cycle-tracker-start: verify");
let vp = ProdVerifier::default();
let opt = Options {
trust_threshold: Default::default(),
trusting_period: Duration::from_secs(500),
clock_drift: Default::default(),
};

let verify_time = light_block_2.time() + Duration::from_secs(20);

let verdict = vp.verify_update_header(
light_block_2.as_untrusted_state(),
light_block_1.as_trusted_state(),
&opt,
verify_time.unwrap(),
);
println!("cycle-tracker-end: verify");

match verdict {
Verdict::Success => {
println!("success");
}
v => panic!("expected success, got: {:?}", v),
}
}

0 comments on commit c3f906a

Please sign in to comment.