sp1_solana/lib.rs
1//! # Verifier
2//!
3//! This crate contains utilities for verifying SP1 Groth16 proofs on Solana.
4//!
5//! # Example
6//! ```no_run
7//! use sp1_sdk::proof::SP1ProofWithPublicValues;
8//! use sp1_solana::{verify_proof, GROTH16_VK_2_0_0_BYTES};
9//!
10//! // Load the sp1_proof_with_public_values from a file.
11//! let sp1_proof_with_public_values_file = "../proofs/fibonacci_proof.bin";
12//! let sp1_proof_with_public_values =
13//! SP1ProofWithPublicValues::load(&sp1_proof_with_public_values_file).unwrap();
14//!
15//! // Fetch the proof and public inputs from the SP1ProofWithPublicValues.
16//! let proof_bytes = sp1_proof_with_public_values.bytes();
17//! let sp1_public_inputs = sp1_proof_with_public_values.public_values.to_vec();
18//!
19//! // Typically, the vkey hash is computed from `vk.bytes32()` on the SP1 program's vkey.
20//! let vkey_hash = "0x0083e8e370d7f0d1c463337f76c9a60b62ad7cc54c89329107c92c1e62097872";
21//!
22//! verify_proof(&proof_bytes, &sp1_public_inputs, &vkey_hash, &GROTH16_VK_2_0_0_BYTES).unwrap();
23//! ```
24
25use groth16_solana::groth16::Groth16Verifyingkey;
26use sha2::{Digest, Sha256};
27
28#[cfg(test)]
29mod test;
30
31mod utils;
32use utils::*;
33
34/// Groth16 verification keys for different SP1 versions.
35pub const GROTH16_VK_5_0_0_BYTES: &[u8] = include_bytes!("../vk/v5.0.0/groth16_vk.bin");
36pub const GROTH16_VK_4_0_0_RC3_BYTES: &[u8] = include_bytes!("../vk/v4.0.0-rc.3/groth16_vk.bin");
37pub const GROTH16_VK_3_0_0_BYTES: &[u8] = include_bytes!("../vk/v3.0.0/groth16_vk.bin");
38pub const GROTH16_VK_3_0_0_RC4_BYTES: &[u8] = include_bytes!("../vk/v3.0.0rc4/groth16_vk.bin");
39pub const GROTH16_VK_2_0_0_BYTES: &[u8] = include_bytes!("../vk/v2.0.0/groth16_vk.bin");
40
41/// Verifies a proof using raw bytes, without any checks.
42///
43/// The public inputs are the vkey hash and the commited values digest, concatenated.
44/// The proof is a decompressed G1 element, followed by a decompressed G2 element, followed by a
45/// decompressed G1 element.
46pub fn verify_proof_raw(proof: &[u8], public_inputs: &[u8], vk: &[u8]) -> Result<(), Error> {
47 let proof = load_proof_from_bytes(proof)?;
48 let vk = load_groth16_verifying_key_from_bytes(vk)?;
49 let public_inputs = load_public_inputs_from_bytes(public_inputs)?;
50
51 let vk = Groth16Verifyingkey {
52 nr_pubinputs: vk.nr_pubinputs as usize,
53 vk_alpha_g1: vk.vk_alpha_g1,
54 vk_beta_g2: vk.vk_beta_g2,
55 vk_gamme_g2: vk.vk_gamma_g2,
56 vk_delta_g2: vk.vk_delta_g2,
57 vk_ic: vk.vk_ic.as_slice(),
58 };
59
60 let mut verifier = groth16_solana::groth16::Groth16Verifier::new(
61 &proof.pi_a,
62 &proof.pi_b,
63 &proof.pi_c,
64 &public_inputs.inputs,
65 &vk,
66 )
67 .map_err(|_| Error::VerificationError)?;
68
69 verifier.verify().map_err(|_| Error::VerificationError)
70}
71
72/// Verifies a proof generated by [`SP1ProofWithPublicValues`].
73///
74/// The proof is expected to be from this method on `SP1ProofWithPublicValues`:
75/// https://docs.rs/sp1-sdk/latest/sp1_sdk/proof/struct.SP1ProofWithPublicValues.html#method.bytes
76/// The public inputs are directly taken from the `SP1PublicValues`.
77/// https://docs.rs/sp1-sdk/latest/sp1_sdk/struct.SP1PublicValues.html#method.as_slice
78/// The vkey hash is derived from running `vk.bytes32()` on the program's vkey.
79/// https://docs.rs/sp1-sdk/latest/sp1_sdk/trait.HashableKey.html#method.bytes32
80#[inline]
81pub fn verify_proof(
82 proof: &[u8],
83 sp1_public_inputs: &[u8],
84 sp1_vkey_hash: &str,
85 groth16_vk: &[u8],
86) -> Result<(), Error> {
87 // Hash the vk and get the first 4 bytes.
88 let groth16_vk_hash: [u8; 4] = Sha256::digest(groth16_vk)[..4].try_into().unwrap();
89
90 // Check to make sure that this proof was generated by the groth16 proving key corresponding to
91 // the given groth16_vk.
92 //
93 // SP1 prepends the raw Groth16 proof with the first 4 bytes of the groth16 vkey to
94 // facilitate this check.
95 if groth16_vk_hash != proof[..4] {
96 return Err(Error::Groth16VkeyHashMismatch);
97 }
98
99 let sp1_vkey_hash = decode_sp1_vkey_hash(sp1_vkey_hash)?;
100
101 // Verify the proof.
102 verify_proof_raw(
103 &proof[4..],
104 &groth16_public_values(&sp1_vkey_hash, sp1_public_inputs),
105 groth16_vk,
106 )
107}