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

Improve groth16 encode API #179

Merged
merged 11 commits into from
Aug 7, 2024
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [Linux, macOS]
feature: [default]
device: [cpu]
include:
- os: Linux
feature: default
device: cpu
- os: Linux
feature: cuda
device: nvidia_rtx_a5000
- os: macOS
feature: metal
feature: default
device: apple_m2_pro
env:
FEATURE: ${{ matrix.feature }}
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ risc0-build = { git = "https://github.com/risc0/risc0", branch = "main", default
risc0-zkp = { git = "https://github.com/risc0/risc0", branch = "main", default-features = false }
risc0-zkvm = { git = "https://github.com/risc0/risc0", branch = "main", default-features = false }

alloy = { version = "0.2" }
alloy-primitives = { version = "0.7", features = ["serde", "rlp", "std"] }
alloy-rlp = { version = "0.3.7", default-features = false }
alloy-rlp-derive = { version = "0.3.7", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ repository = { workspace = true }
anyhow = "1.0"

[dependencies]
alloy-sol-types = { workspace = true }
alloy = { workspace = true, features = ["sol-types", "contract"] }
anyhow = { workspace = true }
ethers = { version = "2.0", features = ["rustls", "ws"] }
risc0-zkvm = { workspace = true }

[dev-dependencies]
hex = "0.4"
regex = "1.10"
tokio = { version = "1", features = ["macros", "rt"] }
tokio = { workspace = true, features = ["macros", "rt"] }

[lib]
doctest = false
Expand Down
41 changes: 0 additions & 41 deletions contracts/build.rs

This file was deleted.

33 changes: 16 additions & 17 deletions contracts/src/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use alloy_sol_types::SolValue;
use anyhow::Result;
use risc0_zkvm::{sha::Digestible, Groth16ReceiptVerifierParameters};
use alloy::sol_types::SolValue;
use risc0_zkvm::{Groth16Receipt, ReceiptClaim};

/// ABI encoding of the seal.
pub fn abi_encode(seal: Vec<u8>) -> Result<Vec<u8>> {
Ok(encode(seal)?.abi_encode())
/// ABI encoding of a [Groth16Receipt][risc0_zkvm::Groth16Receipt] seal.
pub fn abi_encode(receipt: &Groth16Receipt<ReceiptClaim>) -> Vec<u8> {
encode(receipt).abi_encode()
}

/// encoding of the seal with selector.
pub fn encode(seal: Vec<u8>) -> Result<Vec<u8>> {
let verifier_parameters_digest = Groth16ReceiptVerifierParameters::default().digest();
let selector = &verifier_parameters_digest.as_bytes()[..4];
/// Encoding of a [Groth16Receipt][risc0_zkvm::Groth16Receipt] by prefixing it with the verifier selector,
/// taken from the first 4 bytes of the hash of the verifier parameters including the Groth16
/// verification key and the control IDs that commit to the RISC Zero circuits.
pub fn encode(receipt: &Groth16Receipt<ReceiptClaim>) -> Vec<u8> {
Copy link
Contributor

Choose a reason for hiding this comment

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

How will we recommend going from https://docs.rs/bonsai-sdk/0.9.0/bonsai_sdk/responses/struct.SnarkReceipt.html, given we still recommend using bonsai-sdk over the zkvm for production use cases?

let selector = &receipt.verifier_parameters.as_bytes()[..4];
// Create a new vector with the capacity to hold both selector and seal
let mut selector_seal = Vec::with_capacity(selector.len() + seal.len());
let mut selector_seal = Vec::with_capacity(selector.len() + receipt.seal.len());
selector_seal.extend_from_slice(selector);
selector_seal.extend_from_slice(&seal);
selector_seal.extend_from_slice(&receipt.seal);

Ok(selector_seal)
selector_seal
}

#[cfg(test)]
mod tests {
use std::fs;

use anyhow::anyhow;
use ethers::utils::hex;
use regex::Regex;

use super::*;
use std::fs;
use risc0_zkvm::Groth16ReceiptVerifierParameters;

const CONTROL_ID_PATH: &str = "./src/groth16/ControlID.sol";
const CONTROL_ROOT: &str = "CONTROL_ROOT";
Expand Down
12 changes: 3 additions & 9 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use ethers::prelude::*;

abigen!(
IRiscZeroVerifier,
"$OUT_DIR/IRiscZeroVerifier.sol/IRiscZeroVerifier.json"
);
abigen!(
RiscZeroGroth16Verifier,
"$OUT_DIR/RiscZeroGroth16Verifier.sol/RiscZeroGroth16Verifier.json"
alloy::sol!(
#![sol(rpc, all_derives)]
"src/IRiscZeroVerifier.sol"
);

pub mod groth16;
2 changes: 1 addition & 1 deletion examples/erc20-counter/apps/src/bin/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn main() -> Result<()> {
)?;

// Encode the groth16 seal with the selector
let seal = encode(receipt.inner.groth16()?.seal.clone())?;
let seal = encode(receipt.inner.groth16()?);

// Encode the function call for `ICounter.increment(journal, seal)`.
let calldata = ICounter::incrementCall {
Expand Down
2 changes: 1 addition & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ homepage = { workspace = true }
repository = { workspace = true }

[dependencies]
alloy = { workspace = true }
anyhow = { workspace = true }
clap = { version = "4.5", features = ["derive", "env"] }
ethers = { version = "2.0" }
hex = { version = "0.4" }
risc0-ethereum-contracts = { workspace = true }
risc0-zkvm = { workspace = true, features = ["client"] }
8 changes: 4 additions & 4 deletions ffi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

use std::io::Write;

use alloy::{primitives::Bytes, sol_types::SolValue};
use anyhow::{Context, Result};
use clap::Parser;
use ethers::abi::Token;
use risc0_ethereum_contracts::groth16::encode;
use risc0_zkvm::{
default_prover, is_dev_mode, sha::Digestible, ExecutorEnv, ProverOpts, VerifierContext,
Expand Down Expand Up @@ -54,8 +54,8 @@ pub fn main() -> Result<()> {
fn prove_ffi(elf_path: String, input: Vec<u8>) -> Result<()> {
let elf = std::fs::read(elf_path).unwrap();
let (journal, seal) = prove(&elf, &input)?;
let calldata = vec![Token::Bytes(journal), Token::Bytes(seal)];
let output = hex::encode(ethers::abi::encode(&calldata));
let calldata = vec![Bytes(journal.into()), Bytes(seal.into())];
let output = hex::encode(calldata.abi_encode());

// Forge test FFI calls expect hex encoded bytes sent to stdout
print!("{output}");
Expand Down Expand Up @@ -85,7 +85,7 @@ fn prove(elf: &[u8], input: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
seal.extend(receipt.claim()?.digest().as_bytes());
seal
}
false => encode(receipt.inner.groth16()?.seal.clone())?,
false => encode(receipt.inner.groth16()?),
};
Ok((journal, seal))
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "1.75"
channel = "1.78"
components = ["clippy", "rustfmt", "rust-src"]
targets = []
profile = "minimal"
Loading