Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hollow aggregated proofs #149

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions provers/blevm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion provers/blevm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["blevm", "blevm-mock", "script", "blevm-aggregator", "common"]
members = ["blevm", "blevm-mock", "script", "blevm-aggregator", "common", "blevm-aggregator-mock"]
resolver = "2"

[workspace.dependencies]
Expand Down
21 changes: 21 additions & 0 deletions provers/blevm/blevm-aggregator-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
version = "0.1.0"
name = "blevm-aggregator-mock"
edition = "2021"

[dependencies]
blevm-common = { path = "../common" }
hex-literal = "0.4.1"
alloy-sol-types = { workspace = true }
sp1-zkvm = { workspace = true, features = ["verify"] }
rsp-client-executor = {workspace=true}
celestia-types = {workspace=true}
nmt-rs = "*"
reth-primitives = {workspace=true}
tendermint = {workspace=true}
tendermint-proto = {workspace=true}
bincode = {workspace=true}
hex = "0.4.3"
sha2 = "0.10.8"
serde = { version = "1.0", default-features = false, features = ["derive", "std"] }
serde_cbor = "0.11.2"
66 changes: 66 additions & 0 deletions provers/blevm/blevm-aggregator-mock/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// A buffer of serializable/deserializable objects.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Buffer {
pub data: Vec<u8>,
#[serde(skip)]
pub ptr: usize,
}

impl Buffer {
pub fn new() -> Self {
Self {
data: Vec::new(),
ptr: 0,
}
}

pub fn from(data: &[u8]) -> Self {
Self {
data: data.to_vec(),
ptr: 0,
}
}

#[allow(dead_code)]
/// Set the position ptr to the beginning of the buffer.
pub fn head(&mut self) {
self.ptr = 0;
}

/// Read the serializable object from the buffer.
pub fn read<T: Serialize + DeserializeOwned>(&mut self) -> T {
let result: T =
bincode::deserialize(&self.data[self.ptr..]).expect("failed to deserialize");
let nb_bytes = bincode::serialized_size(&result).expect("failed to get serialized size");
self.ptr += nb_bytes as usize;
result
}

#[allow(dead_code)]
pub fn read_slice(&mut self, slice: &mut [u8]) {
slice.copy_from_slice(&self.data[self.ptr..self.ptr + slice.len()]);
self.ptr += slice.len();
}

#[allow(dead_code)]
/// Write the serializable object from the buffer.
pub fn write<T: Serialize>(&mut self, data: &T) {
let mut tmp = Vec::new();
bincode::serialize_into(&mut tmp, data).expect("serialization failed");
self.data.extend(tmp);
}

#[allow(dead_code)]
/// Write the slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.data.extend_from_slice(slice);
}
}

impl Default for Buffer {
fn default() -> Self {
Self::new()
}
}
49 changes: 49 additions & 0 deletions provers/blevm/blevm-aggregator-mock/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

mod buffer;
use blevm_common::{BlevmAggOutput, BlevmOutput};
use buffer::Buffer;

pub fn main() {
// Read the number of proofs
let n: usize = sp1_zkvm::io::read();

if n < 2 {
panic!("must provide at least 2 proofs");
}

// Read all verification keys first
let mut verification_keys: Vec<[u32; 8]> = Vec::with_capacity(n);
for _ in 0..n {
verification_keys.push(sp1_zkvm::io::read());
}

// Read all public values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, minor update to the inputs, we should have verification keys here as well:

// Read all verification keys first

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries! I'll wait for your PR to merge and then I can replicate it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be addressed! :)

let mut public_values: Vec<Vec<u8>> = Vec::with_capacity(n);
for _ in 0..n {
public_values.push(sp1_zkvm::io::read());
}

// Parse all outputs and collect Celestia header hashes
let mut outputs: Vec<BlevmOutput> = Vec::with_capacity(n);
let mut celestia_header_hashes: Vec<[u8; 32]> = Vec::with_capacity(n);
for values in &public_values {
let mut buffer = Buffer::from(values);
let output: BlevmOutput = buffer.read();
celestia_header_hashes.push(output.celestia_header_hash);
outputs.push(output);
}

// Create aggregate output using first and last blocks
let agg_output = BlevmAggOutput {
newest_header_hash: outputs[n - 1].header_hash,
oldest_header_hash: outputs[0].header_hash,
celestia_header_hashes,
newest_state_root: outputs[n - 1].state_root,
newest_height: outputs[n - 1].height,
};

// Commit the aggregate output
sp1_zkvm::io::commit(&agg_output);
}
4 changes: 0 additions & 4 deletions provers/blevm/blevm-mock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
sp1_zkvm::entrypoint!(main);

use blevm_common::BlevmOutput;
use celestia_types::nmt::Namespace;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was seeing warnings for unused imports

use celestia_types::nmt::NamespaceProof;
use nmt_rs::{simple_merkle::proof::Proof, NamespacedHash, TmSha2Hasher};
use rsp_client_executor::io::ClientExecutorInput;

pub fn main() {
// This is a mock proof so it hard-codes all the output values. Note: these values were sourced
Expand Down
Loading