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: Forward ports to testnet v1.0.4+ (after #40) #43

Merged
merged 3 commits into from
Jun 15, 2024
Merged
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
730 changes: 730 additions & 0 deletions examples/aggregation/program/Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions examples/aggregation/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[workspace]
[package]
version = "0.1.0"
name = "aggregation-program"
edition = "2021"

[dependencies]
hex = "0.4.3"
sha2 = "0.10.8"
sphinx-zkvm = { path = "../../../zkvm/entrypoint", features = ["verify"] }

[patch.crates-io]
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes.git", package = "sha2", branch = "patch-v0.10.8" }
Binary file not shown.
68 changes: 68 additions & 0 deletions examples/aggregation/program/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! A simple program that aggregates the proofs of multiple programs proven with the zkVM.

#![no_main]
sphinx_zkvm::entrypoint!(main);

use sha2::Digest;
use sha2::Sha256;

pub fn words_to_bytes_le(words: &[u32; 8]) -> [u8; 32] {
let mut bytes = [0u8; 32];
for i in 0..8 {
let word_bytes = words[i].to_le_bytes();
bytes[i * 4..(i + 1) * 4].copy_from_slice(&word_bytes);
}
bytes
}

/// Encode a list of vkeys and committed values into a single byte array. In the future this could
/// be a merkle tree or some other commitment scheme.
///
/// ( vkeys.len() || vkeys || committed_values[0].len as u32 || committed_values[0] || ... )
pub fn commit_proof_pairs(vkeys: &[[u32; 8]], committed_values: &[Vec<u8>]) -> Vec<u8> {
assert_eq!(vkeys.len(), committed_values.len());
let mut res = Vec::with_capacity(
4 + vkeys.len() * 32
+ committed_values.len() * 4
+ committed_values
.iter()
.map(|vals| vals.len())
.sum::<usize>(),
);

// Note we use big endian because abi.encodePacked in solidity does also
res.extend_from_slice(&(vkeys.len() as u32).to_be_bytes());
for vkey in vkeys.iter() {
res.extend_from_slice(&words_to_bytes_le(vkey));
}
for vals in committed_values.iter() {
res.extend_from_slice(&(vals.len() as u32).to_be_bytes());
res.extend_from_slice(vals);
}

res
}

pub fn main() {
// Read the verification keys.
let vkeys = sphinx_zkvm::io::read::<Vec<[u32; 8]>>();

// Read the public values.
let public_values = sphinx_zkvm::io::read::<Vec<Vec<u8>>>();

// Verify the proofs.
assert_eq!(vkeys.len(), public_values.len());
for i in 0..vkeys.len() {
let vkey = &vkeys[i];
let public_values = &public_values[i];
let public_values_digest = Sha256::digest(public_values);
sphinx_zkvm::precompiles::verify::verify_sphinx_proof(vkey, &public_values_digest.into());
}

// TODO: Do something interesting with the proofs here.
//
// For example, commit to the verified proofs in a merkle tree. For now, we'll just commit to
// all the (vkey, input) pairs.
let commitment = commit_proof_pairs(&vkeys, &public_values);
sphinx_zkvm::io::commit_slice(&commitment);
}
Loading
Loading