Skip to content

Commit 7e847de

Browse files
committed
fix: avoid panics
1 parent 1070c15 commit 7e847de

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

adapters/sp1/groth16-verifier/src/verifier.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,32 @@ pub struct SP1Groth16Verifier {
3535
}
3636

3737
impl SP1Groth16Verifier {
38-
/// Loads a new `SP1Groth16Verifier` from a compressed Groth16 verifying key and a program ID.
38+
/// Loads a new `SP1Groth16Verifier` from a gnark compressed Groth16 verifying key and a
39+
/// program ID.
3940
///
4041
/// # Parameters
41-
/// - `vk_bytes`: Byte slice containing the compressed Groth16 verifying key. Typically, this is
42-
/// the [`static@crate::GROTH16_VK_BYTES`] constant for the current SP1 version.
42+
/// - `vk_bytes`: Byte slice containing the gnark compressed Groth16 verifying key. Typically,
43+
/// this is the [`static@sp1_verifier::GROTH16_VK_BYTES`] constant for the given SP1 version.
4344
/// - `program_id`: A 32-byte array representing the Fr-element identifier for the SP1 program.
4445
///
4546
/// # Returns
4647
/// - `Ok(SP1Groth16Verifier)`: If the verifying key and program ID are successfully parsed.
4748
/// - `Err(Error)`: If `vk_bytes` is invalid or cannot be parsed into a verifying key.
48-
pub fn load(vk_bytes: &[u8], program_vk_hash: [u8; 32]) -> Result<Self, Error> {
49+
pub fn load(vk_bytes: &[u8], program_vk_hash: [u8; 32]) -> Result<Self, Groth16Error> {
4950
// Compute the SHA-256 hash of `vk_bytes` and take the first `VK_HASH_PREFIX_LENGTH` bytes.
5051
// This prefix is prepended to every raw Groth16 proof by SP1 to signal which verifying key
5152
// was used during proving.
5253
let groth16_vk_hash: [u8; 4] = Sha256::digest(vk_bytes)[..VK_HASH_PREFIX_LENGTH]
5354
.try_into()
54-
.map_err(|_| Groth16Error::GeneralError(Error::InvalidData))
55-
.unwrap();
55+
.map_err(|_| Groth16Error::GeneralError(Error::InvalidData))?;
5656

5757
// Parse the Groth16 verifying key from its byte representation.
5858
// This returns a `Groth16VerifyingKey` that can be used for algebraic verification.
59-
let groth16_vk = load_groth16_verifying_key_from_bytes(vk_bytes).unwrap();
59+
let groth16_vk = load_groth16_verifying_key_from_bytes(vk_bytes)?;
6060

6161
// Parse the program ID (Fr element) from its 32-byte big-endian encoding.
62-
let program_vk_hash = Fr::from_slice(&program_vk_hash).unwrap();
62+
let program_vk_hash =
63+
Fr::from_slice(&program_vk_hash).map_err(|_| Error::FailedToGetFrFromRandomBytes)?;
6364

6465
Ok(SP1Groth16Verifier {
6566
vk: groth16_vk,
@@ -71,9 +72,8 @@ impl SP1Groth16Verifier {
7172
/// Verifies a Groth16 proof against the given public values.
7273
///
7374
/// The proof is expected to be encoded as:
74-
/// ```
7575
/// [ vk_hash_prefix (VK_HASH_PREFIX_LENGTH bytes) || raw_groth16_proof_bytes ]
76-
/// ```
76+
///
7777
/// # Parameters
7878
/// - `proof`: Byte slice containing the prefixed Groth16 proof.
7979
/// - `public_values`: Byte slice representing the public values for the SP1 circuit.
@@ -96,13 +96,14 @@ impl SP1Groth16Verifier {
9696

9797
// Extract the raw Groth16 proof (bytes after the prefix) and parse it.
9898
let raw_proof_bytes = &proof[VK_HASH_PREFIX_LENGTH..];
99-
let proof = load_groth16_proof_from_bytes(raw_proof_bytes).unwrap();
99+
let proof = load_groth16_proof_from_bytes(raw_proof_bytes)?;
100100

101101
// Compute Fr element for hash(public_values) using SHA-256. SP1’s Groth16 circuit expects
102102
// two public inputs: a. `program_id`, b. `hash(public_values)`. Since SP1 allows either
103103
// SHA-256 or Blake3, we try SHA-256 first.
104104
let pp_sha2_hash = hash_public_inputs_with_fn(public_values, sha256_hash);
105-
let fr_sha2 = Fr::from_slice(&pp_sha2_hash).unwrap();
105+
let fr_sha2 =
106+
Fr::from_slice(&pp_sha2_hash).map_err(|_| Error::FailedToGetFrFromRandomBytes)?;
106107

107108
// Attempt algebraic verification with SHA-256 hash as the second input.
108109
if verify_groth16_algebraic(&self.vk, &proof, &[self.program_vk_hash, fr_sha2]).is_ok() {
@@ -111,7 +112,8 @@ impl SP1Groth16Verifier {
111112

112113
// If SHA-256 verification fails, compute the Blake3 hash of `public_values` instead.
113114
let pp_blake3_hash = hash_public_inputs_with_fn(public_values, blake3_hash);
114-
let fr_blake3 = Fr::from_slice(&pp_blake3_hash).unwrap();
115+
let fr_blake3 =
116+
Fr::from_slice(&pp_blake3_hash).map_err(|_| Error::FailedToGetFrFromRandomBytes)?;
115117

116118
// Retry algebraic verification using Blake3 hash as the second input.
117119
verify_groth16_algebraic(&self.vk, &proof, &[self.program_vk_hash, fr_blake3])

0 commit comments

Comments
 (0)