Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:succinctlabs/sp1 into yuwen/verifier…
Browse files Browse the repository at this point in the history
…-docs
  • Loading branch information
yuwen01 committed Nov 6, 2024
2 parents c47bb62 + c804db2 commit 8c843c8
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 8,831 deletions.
56 changes: 56 additions & 0 deletions .github/ISSUE_TEMPLATE/BUG-FORM.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Bug report
description: File a bug report
labels: ["T-bug", "T-needs-triage"]
body:
- type: markdown
attributes:
value: |
Please ensure that the bug has not already been filed in the issue tracker.
Thanks for taking the time to report this bug in SP1!
- type: dropdown
attributes:
label: Component
description: What component is the bug in?
multiple: true
options:
- sp1-sdk
- sp1-zkvm
- cargo prove CLI/sp1up
- Other (please describe)
validations:
required: true
- type: checkboxes
attributes:
label: Have you ensured that all of these are up to date?
options:
- label: SP1 SDK
- label: cargo prove CLI/sp1up
- type: input
attributes:
label: What version of SP1 SDK are you on?
description: Leave empty if not relevant
placeholder: "Add the version from the Cargo.toml file here"
- type: input
attributes:
label: What version of the cargo prove CLI are you on?
description: Leave empty if not relevant
placeholder: "Run cargo prove --version and paste the output here"
- type: dropdown
attributes:
label: Operating System
description: What operating system are you on?
options:
- Windows
- macOS (Intel)
- macOS (Apple Silicon)
- Linux (Arch)
- Linux (Debian)
- Linux (Ubuntu)
- Linux (Other)
- type: textarea
attributes:
label: Describe the bug
description: Please include relevant Rust snippets/CLI commands as well if relevant.
validations:
required: true
32 changes: 32 additions & 0 deletions .github/ISSUE_TEMPLATE/FEATURE-FORM.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Feature request
description: Suggest a feature
labels: ["T-feature", "T-needs-triage"]
body:
- type: markdown
attributes:
value: |
Please ensure that the feature has not already been requested in the issue tracker.
Thanks for helping us improve SP1!
- type: dropdown
attributes:
label: Component
description: What component is the feature for?
multiple: true
options:
- sp1-sdk
- sp1-zkvm
- cargo prove CLI/sp1up
- Other (please describe)
validations:
required: true
- type: textarea
attributes:
label: Describe the feature you would like
description: Please also describe what the feature is aiming to solve, if relevant.
validations:
required: true
- type: textarea
attributes:
label: Additional context
description: Add any other context to the feature (like screenshots, resources)
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
blank_issues_enabled: true
contact_links:
- name: Support
url: https://t.me/+5q4kfeXaBE1hZjEx
about: This issue tracker is only for bugs and feature requests. Support is available on Telegram!
36 changes: 36 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
Thank you for your Pull Request. Please provide a description above and review
the requirements below.
Bug fixes and new features should include tests.
Typos / punctuation / trivial PRs are generally not accepted.
Contributors guide: https://github.com/succinctlabs/sp1/blob/dev/CONTRIBUTING.md
The contributors guide includes instructions for running rustfmt and building the
documentation.
-->

<!-- ** Please select "Allow edits from maintainers" in the PR Options ** -->

## Motivation

<!--
Explain the context and why you're making that change. What is the problem
you're trying to solve? In some cases there is not a problem and this can be
thought of as being the motivation for your change.
-->

## Solution

<!--
Summarize the solution and provide any necessary context needed to understand
the code change.
-->

## PR Checklist

- [ ] Added Tests
- [ ] Added Documentation
- [ ] Breaking changes
30 changes: 29 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,32 @@ jobs:

- name: Check build
run: cargo check --target riscv32imac-unknown-none-elf --no-default-features -p sp1-verifier


lock-files:
name: "Check lock files"
runs-on: ["runs-on", "runner=8cpu-linux-x64", "hdd=150", "run-id=${{ github.run_id }}"]
env:
CARGO_NET_GIT_FETCH_WITH_CLI: "true"
steps:
- name: "Checkout sources"
uses: "actions/checkout@v4"

- name: Setup CI
uses: ./.github/actions/setup

- name: "Remove lock files"
run: |
find -name Cargo.lock -type f -exec rm {} \;
- name: "Build SP1 without lock files"
run: |
cargo build --all --all-targets
- name: Install SP1 toolchain
run: |
cargo run -p sp1-cli -- prove install-toolchain
- name: "Build examples without lock files"
run: |
cd examples
cargo build --all --all-targets
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,10 @@ p3-bn254-fr = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v4" }

[workspace.metadata.typos]
# TODO: Fix in next version since CommitCommitedValuesDigest is retained since it's present in constraints.json
default.extend-ignore-re = ["Jo-Philipp Wich", "SubEIN", "DivEIN", "CommitCommitedValuesDigest"]
default.extend-ignore-re = [
"Jo-Philipp Wich",
"SubEIN",
"DivEIN",
"CommitCommitedValuesDigest",
]
default.extend-ignore-words-re = ["(?i)groth", "TRE"]
Binary file removed crates/prover/merkle_tree.bin
Binary file not shown.
20 changes: 1 addition & 19 deletions crates/sdk/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,9 @@ impl SP1ProofWithPublicValues {
}
}

/// Returns the *raw* proof as bytes, prepended with the first 4 bytes of the vkey hash.
///
/// This is the format expected by the `sp1-verifier` crate. The extra 4 bytes are used to
/// ensure that the proof will eventually be verified by the correct vkey.
pub fn raw_with_checksum(&self) -> Vec<u8> {
match &self.proof {
SP1Proof::Plonk(plonk) => {
let proof_bytes = hex::decode(&plonk.raw_proof).expect("Invalid Plonk proof");
[plonk.plonk_vkey_hash[..4].to_vec(), proof_bytes].concat()
}
SP1Proof::Groth16(groth16) => {
let proof_bytes = hex::decode(&groth16.raw_proof).expect("Invalid Groth16 proof");
[groth16.groth16_vkey_hash[..4].to_vec(), proof_bytes].concat()
}
_ => unimplemented!(),
}
}

/// For Plonk or Groth16 proofs, returns the proof in a byte encoding the onchain verifier
/// accepts. The bytes consist of the first four bytes of Plonk vkey hash followed by the
/// *encoded* proof.
/// encoded proof, in a form optimized for onchain verification.
pub fn bytes(&self) -> Vec<u8> {
match &self.proof {
SP1Proof::Plonk(plonk_proof) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/verifier/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sp1-verifier"
description = "Verifier for SP1 Groth16 and Plonk proofs."
readme = "../../README.md"
readme = "README.md"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand All @@ -10,7 +10,7 @@ keywords = { workspace = true }
categories = { workspace = true }

[dependencies]
bn = { git = "https://github.com/sp1-patches/bn", tag = "substrate_bn-v0.6.0-patch-v2", package = "substrate-bn" }
bn = { git = "https://github.com/sp1-patches/bn", version = "0.6.0", tag = "substrate_bn-v0.6.0-patch-v2", package = "substrate-bn" }
sha2 = { version = "0.10.8", default-features = false }
thiserror-no-std = "2.0.2"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
Expand All @@ -23,4 +23,4 @@ num-traits = "0.2.19"

[features]
default = ["std"]
std = []
std = ["thiserror-no-std/std"]
36 changes: 2 additions & 34 deletions crates/verifier/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,19 @@ use thiserror_no_std::Error;

#[derive(Error, Debug)]
pub enum Error {
// Cryptographic Errors
#[error("BSB22 Commitment number mismatch")]
Bsb22CommitmentMismatch,
#[error("Challenge already computed")]
ChallengeAlreadyComputed,
#[error("Challenge not found")]
ChallengeNotFound,
#[error("Previous challenge not computed")]
PreviousChallengeNotComputed,
#[error("Pairing check failed")]
PairingCheckFailed,
#[error("Invalid point in subgroup check")]
InvalidPoint,

// Arithmetic Errors
#[error("Beyond the modulus")]
BeyondTheModulus,
#[error("Ell too large")]
EllTooLarge,
#[error("Inverse not found")]
InverseNotFound,
#[error("Opening linear polynomial mismatch")]
OpeningPolyMismatch,

// Input Errors
#[error("DST too large")]
DSTTooLarge,
#[error("Invalid number of digests")]
InvalidNumberOfDigests,
#[error("Invalid witness")]
InvalidWitness,
#[error("Invalid x length")]
InvalidXLength,
#[error("Unexpected flag")]
UnexpectedFlag,
#[error("Invalid data")]
InvalidData,
#[error("Invalid point in subgroup check")]
InvalidPoint,

// Conversion Errors
#[error("Failed to get Fr from random bytes")]
FailedToGetFrFromRandomBytes,
#[error("Failed to get x")]
FailedToGetX,
#[error("Failed to get y")]
FailedToGetY,

// External Library Errors
#[error("BN254 Field Error")]
Expand Down
2 changes: 0 additions & 2 deletions crates/verifier/src/groth16/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub enum Groth16Error {
ProcessVerifyingKeyFailed,
#[error("Prepare inputs failed")]
PrepareInputsFailed,
#[error("Unexpected identity")]
UnexpectedIdentity,
#[error("General error")]
GeneralError(#[from] crate::error::Error),
#[error("Groth16 vkey hash mismatch")]
Expand Down
19 changes: 10 additions & 9 deletions crates/verifier/src/groth16/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod converter;
pub(crate) mod error;
pub mod error;
mod verify;

pub(crate) use converter::{load_groth16_proof_from_bytes, load_groth16_verifying_key_from_bytes};
Expand All @@ -8,7 +8,7 @@ pub(crate) use verify::*;

use error::Groth16Error;

use crate::{bn254_public_values, decode_sp1_vkey_hash};
use crate::{bn254_public_values, decode_sp1_vkey_hash, error::Error};

/// A verifier for Groth16 zero-knowledge proofs.
#[derive(Debug)]
Expand All @@ -30,21 +30,22 @@ impl Groth16Verifier {
/// let sp1_vkey_hash = vk.bytes32();
/// ```
/// * `groth16_vk` - The Groth16 verifying key bytes.
/// Usually this will be the [`crate::GROTH16_VK_BYTES`] constant, which is the Groth16
/// Usually this will be the [`static@crate::GROTH16_VK_BYTES`] constant, which is the Groth16
/// verifying key for the current SP1 version.
///
/// # Returns
///
/// A [`Result`] containing a boolean indicating whether the proof is valid,
/// or a [`Groth16Error`] if verification fails.
/// A success [`Result`] if verification succeeds, or a [`Groth16Error`] if verification fails.
pub fn verify(
proof: &[u8],
sp1_public_inputs: &[u8],
sp1_vkey_hash: &str,
groth16_vk: &[u8],
) -> Result<bool, Groth16Error> {
) -> Result<(), Groth16Error> {
// Hash the vk and get the first 4 bytes.
let groth16_vk_hash: [u8; 4] = Sha256::digest(groth16_vk)[..4].try_into().unwrap();
let groth16_vk_hash: [u8; 4] = Sha256::digest(groth16_vk)[..4]
.try_into()
.map_err(|_| Groth16Error::GeneralError(Error::InvalidData))?;

// Check to make sure that this proof was generated by the groth16 proving key corresponding to
// the given groth16_vk.
Expand All @@ -58,8 +59,8 @@ impl Groth16Verifier {
let sp1_vkey_hash = decode_sp1_vkey_hash(sp1_vkey_hash)?;
let public_inputs = bn254_public_values(&sp1_vkey_hash, sp1_public_inputs);

let proof = load_groth16_proof_from_bytes(&proof[4..]).unwrap();
let groth16_vk = load_groth16_verifying_key_from_bytes(groth16_vk).unwrap();
let proof = load_groth16_proof_from_bytes(&proof[4..])?;
let groth16_vk = load_groth16_verifying_key_from_bytes(groth16_vk)?;

verify_groth16_raw(&groth16_vk, &proof, &public_inputs)
}
Expand Down
11 changes: 8 additions & 3 deletions crates/verifier/src/groth16/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ pub(crate) fn verify_groth16_raw(
vk: &Groth16VerifyingKey,
proof: &Groth16Proof,
public_inputs: &[Fr],
) -> Result<bool, Groth16Error> {
) -> Result<(), Groth16Error> {
let prepared_inputs = prepare_inputs(vk.clone(), public_inputs)?;

Ok(pairing_batch(&[
if pairing_batch(&[
(-Into::<G1>::into(proof.ar), proof.bs.into()),
(prepared_inputs, vk.g2.gamma.into()),
(proof.krs.into(), vk.g2.delta.into()),
(vk.g1.alpha.into(), -Into::<G2>::into(vk.g2.beta)),
]) == Gt::one())
]) == Gt::one()
{
Ok(())
} else {
Err(Groth16Error::ProofVerificationFailed)
}
}
9 changes: 6 additions & 3 deletions crates/verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ lazy_static! {
mod constants;
mod converter;
mod error;
mod groth16;

mod utils;
pub use utils::*;

pub use groth16::error::Groth16Error;
pub use groth16::Groth16Verifier;
pub use utils::*;
mod groth16;

mod plonk;
pub use plonk::error::PlonkError;
pub use plonk::PlonkVerifier;
mod plonk;

#[cfg(test)]
mod tests;
Loading

0 comments on commit 8c843c8

Please sign in to comment.