Skip to content

Commit cbc09d2

Browse files
committed
Added ProofError structure to handle errors when checking proofs
1 parent 0f127fe commit cbc09d2

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

src/toolbox/sigma/fiat_shamir.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use rand::{RngCore, CryptoRng};
1717
use crate::toolbox::sigma::SigmaProtocol;
1818
use crate::toolbox::sigma::transcript::TranscriptCodec;
19+
use crate::ProofError;
1920
use group::Group;
2021

2122
/// A Fiat-Shamir transformation of a Sigma protocol into a non-interactive proof.
@@ -75,12 +76,12 @@ where
7576
// Prouver's response
7677
let response = self.sigmap.prover_response(prover_state, &challenge);
7778
// Local verification of the proof
78-
assert!(self.sigmap.verifier(&commitment, &challenge, &response) == Ok(()));
79+
assert!(self.sigmap.verifier(&commitment, &challenge, &response).is_ok());
7980
self.sigmap.serialize_batchable(&commitment, &challenge, &response)
8081
}
8182

8283
/// Verify a non-interactive serialized proof and returns a Result: `Ok(())` if the proof verifies successfully, `Err(())` otherwise.
83-
pub fn verify(&mut self, proof: &[u8]) -> Result<(), ()> {
84+
pub fn verify(&mut self, proof: &[u8]) -> Result<(), ProofError> {
8485
self.hash_state = C::new(&self.domain_sep);
8586

8687
let (commitment, response) = self.sigmap.deserialize_batchable(proof).unwrap();

src/toolbox/sigma/proof_composition.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! These constructions preserve zero-knowledge properties and follow standard Sigma protocol composition techniques.
1111
12-
use crate::toolbox::sigma::{SigmaProtocol, SigmaProtocolSimulator};
12+
use crate::{toolbox::sigma::{SigmaProtocol, SigmaProtocolSimulator}, ProofError};
1313
use rand::{Rng, CryptoRng};
1414
use ff::PrimeField;
1515

@@ -78,13 +78,13 @@ where
7878
commitment: &Self::Commitment,
7979
challenge: &Self::Challenge,
8080
response: &Self::Response,
81-
) -> Result<(), ()> {
81+
) -> Result<(), ProofError> {
8282
let verif0 = self.protocol0.verifier(&commitment.0, challenge, &response.0);
8383
let verif1 = self.protocol1.verifier(&commitment.1, challenge, &response.1);
8484

8585
match (verif0, verif1) {
8686
(Ok(()), Ok(())) => Ok(()),
87-
_ => Err(()),
87+
_ => Err(ProofError::VerificationFailure),
8888
}
8989
}
9090
}
@@ -204,15 +204,15 @@ where
204204
commitments: &Self::Commitment,
205205
challenge: &Self::Challenge,
206206
response: &Self::Response,
207-
) -> Result<(), ()> {
207+
) -> Result<(), ProofError> {
208208
let cond0 = self.protocol0.verifier(&commitments.0, &response.0, &response.1);
209209

210210
let challenge1 = *challenge - response.0;
211211
let cond1 = self.protocol1.verifier(&commitments.1, &challenge1, &response.2);
212212

213213
match (cond0, cond1) {
214214
(Ok(()), Ok(())) => Ok(()),
215-
_ => Err(()),
215+
_ => Err(ProofError::VerificationFailure),
216216
}
217217
}
218218
}

src/toolbox/sigma/schnorr_proof.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use rand::{CryptoRng, Rng};
88
use group::{Group, GroupEncoding};
99
use ff::{PrimeField,Field};
10-
use crate::toolbox::sigma::{SigmaProtocol, GroupMorphismPreimage};
10+
use crate::{toolbox::sigma::{GroupMorphismPreimage, SigmaProtocol}, ProofError};
1111

1212
/// A Schnorr protocol proving knowledge some discrete logarithm relation.
1313
///
@@ -70,7 +70,7 @@ where
7070
commitment: &Self::Commitment,
7171
challenge: &Self::Challenge,
7272
response: &Self::Response,
73-
) -> Result<(), ()> {
73+
) -> Result<(), ProofError> {
7474
let lhs = self.morphismp.morphism.evaluate(response);
7575

7676
let mut rhs = Vec::new();
@@ -80,7 +80,7 @@ where
8080

8181
match lhs == rhs {
8282
true => Ok(()),
83-
false => Err(()),
83+
false => Err(ProofError::VerificationFailure),
8484
}
8585
}
8686

src/toolbox/sigma/trait.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module defines the `SigmaProtocol` trait, a generic interface for 3-message Sigma protocols.
44
55
use rand::{Rng, CryptoRng};
6+
use crate::ProofError;
67

78
/// A trait defining the behavior of a generic Sigma protocol.
89
///
@@ -57,7 +58,7 @@ pub trait SigmaProtocol {
5758
commitment: &Self::Commitment,
5859
challenge: &Self::Challenge,
5960
response: &Self::Response,
60-
) -> Result<(), ()>;
61+
) -> Result<(), ProofError>;
6162

6263
/// Serializes a proof transcript (commitment, challenge, response) to bytes for batching.
6364
///

tests/non_interactive_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn fiat_shamir_schnorr_proof_ristretto() {
4545
let proof_bytes = nizk.prove(&witness, &mut rng);
4646

4747
// Verify
48-
let verified = nizk.verify(&proof_bytes) == Ok(());
48+
let verified = nizk.verify(&proof_bytes).is_ok();
4949

5050
assert!(verified, "Fiat-Shamir Schnorr proof verification failed");
5151
}

tests/proof_composition_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rand::{rngs::OsRng, CryptoRng, Rng};
2-
use sigma_rs::toolbox::sigma::{proof_composition::OrEnum, SigmaProtocolSimulator, AndProtocol, OrProtocol, SigmaProtocol};
2+
use sigma_rs::{toolbox::sigma::{proof_composition::OrEnum, AndProtocol, OrProtocol, SigmaProtocol, SigmaProtocolSimulator}, ProofError};
33
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
44

55
pub struct SchnorrZkp {
@@ -39,10 +39,10 @@ impl SigmaProtocol for SchnorrZkp {
3939
commitment: &Self::Commitment,
4040
challenge: &Self::Challenge,
4141
response: &Self::Response,
42-
) -> Result<(), ()> {
42+
) -> Result<(), ProofError> {
4343
match response * self.generator == challenge * self.target + commitment {
4444
true => Ok(()),
45-
false => Err(()),
45+
false => Err(ProofError::VerificationFailure),
4646
}
4747
}
4848
}
@@ -104,7 +104,7 @@ fn andproof_schnorr_correct() {
104104
// Verifier checks
105105
let result = and_proof.verifier(&commitments, &challenge, &responses);
106106

107-
assert!(result == Ok(()));
107+
assert!(result.is_ok());
108108
}
109109

110110

@@ -144,7 +144,7 @@ fn andproof_schnorr_incorrect() {
144144
// Verifier checks
145145
let result = and_proof.verifier(&commitments, &challenge, &responses);
146146

147-
assert!(result == Err(()));
147+
assert!(!result.is_ok());
148148
}
149149

150150

@@ -182,7 +182,7 @@ fn orproof_schnorr_correct() {
182182
// Verifier checks
183183
let result = or_proof.verifier(&commitments, &challenge, &responses);
184184

185-
assert!(result == Ok(()));
185+
assert!(result.is_ok());
186186
}
187187

188188

@@ -220,5 +220,5 @@ fn orproof_schnorr_incorrect() {
220220
// Verifier checks
221221
let result = or_proof.verifier(&commitments, &challenge, &responses);
222222

223-
assert!(result == Err(()));
223+
assert!(!result.is_ok());
224224
}

0 commit comments

Comments
 (0)