Skip to content

Commit

Permalink
refactor: return type from bool -> Result<bool, _>
Browse files Browse the repository at this point in the history
  • Loading branch information
zkfriendly committed Jul 15, 2024
1 parent 8a460e5 commit 3d7d2a9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
19 changes: 6 additions & 13 deletions packages/rust-verifier/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use ark_groth16::Proof;
use ark_groth16::VerifyingKey;
use ark_serialize::CanonicalDeserialize;
use ark_serialize::Compress;
use ark_serialize::SerializationError;
use ark_serialize::Validate;

pub fn verify(proof: &[u8], public_inputs: &[u8]) -> bool {
pub fn verify(proof: &[u8], public_inputs: &[u8]) -> Result<bool, SerializationError> {
// no need to check serialization since it's hardcoded and known to be correct
let vk = VerifyingKey::<Bn254>::deserialize_compressed_unchecked(
[
Expand All @@ -38,23 +39,15 @@ pub fn verify(proof: &[u8], public_inputs: &[u8]) -> bool {
113, 226, 198, 186, 49, 80, 39, 152, 13, 241, 151, 254, 186, 114, 68, 148,
]
.as_slice(),
)
.unwrap();
)?;

let public_inputs = <[Fp<MontBackend<FrConfig, 4>, 4>; 3]>::deserialize_with_mode(
&public_inputs[..],
Compress::Yes,
Validate::Yes,
);
)?;

let public_inputs = match public_inputs {
Ok(inputs) => inputs,
Err(_) => return false,
};
let proof = Proof::<Bn<Config>>::deserialize_compressed(&proof[..])?;

if let Ok(proof) = Proof::<Bn<Config>>::deserialize_compressed(&proof[..]) {
Groth16::<Bn254, CircomReduction>::verify(&vk, &public_inputs, &proof).is_ok()
} else {
false
}
Ok(Groth16::<Bn254, CircomReduction>::verify(&vk, &public_inputs, &proof).unwrap())
}
19 changes: 6 additions & 13 deletions packages/rust-verifier/src/verifier_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@ use ark_groth16::Proof;
use ark_groth16::VerifyingKey;
use ark_serialize::CanonicalDeserialize;
use ark_serialize::Compress;
use ark_serialize::SerializationError;
use ark_serialize::Validate;

pub fn verify(proof: &[u8], public_inputs: &[u8]) -> bool {
pub fn verify(proof: &[u8], public_inputs: &[u8]) -> Result<bool, SerializationError> {
// no need to check serialization since it's hardcoded and known to be correct
let vk = VerifyingKey::<Bn254>::deserialize_compressed_unchecked([COMPRESSED_VKEY].as_slice())
.unwrap();
let vk = VerifyingKey::<Bn254>::deserialize_compressed_unchecked([COMPRESSED_VKEY].as_slice())?;

let public_inputs =
<[Fp<MontBackend<FrConfig, 4>, 4>; PUBLIC_INPUTS_COUNT]>::deserialize_with_mode(
&public_inputs[..],
Compress::Yes,
Validate::Yes,
);
)?;

let public_inputs = match public_inputs {
Ok(inputs) => inputs,
Err(_) => return false,
};
let proof = Proof::<Bn<Config>>::deserialize_compressed(&proof[..])?;

if let Ok(proof) = Proof::<Bn<Config>>::deserialize_compressed(&proof[..]) {
Groth16::<Bn254, CircomReduction>::verify(&vk, &public_inputs, &proof).is_ok()
} else {
false
}
Ok(Groth16::<Bn254, CircomReduction>::verify(&vk, &public_inputs, &proof).unwrap())
}
2 changes: 1 addition & 1 deletion packages/rust-verifier/tests/verifier_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod tests {
proof.serialize_compressed(writer).unwrap();
}

let verified = verify(&serialized_proof, &serialized_public_inputs);
let verified = verify(&serialized_proof, &serialized_public_inputs).unwrap();

assert!(verified);
}
Expand Down

0 comments on commit 3d7d2a9

Please sign in to comment.