Skip to content

Commit 265563c

Browse files
MariaGhandourMaria Ghandour
andauthored
fix(docs): add hyperlinks to structs in documentation and format code with cargo fmt (#9)
Co-authored-by: Maria Ghandour <[email protected]>
1 parent 96974c5 commit 265563c

14 files changed

+395
-112
lines changed

src/codec/shake_codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Implementation of a Fiat-Shamir codec using SHAKE128
22
//!
3-
//! This module defines `ShakeCodec`, a concrete implementation of the `Codec`
3+
//! This module defines [`ShakeCodec`], a concrete implementation of the [`Codec`]
44
//! trait. It uses the SHAKE128 extendable output function (XOF) from the Keccak family
55
//! to generate Fiat-Shamir challenges for Sigma protocols.
66
//!
77
//! It allows commitments (group elements) to be absorbed into a codec,
88
//! and produces scalar challenges by squeezing bytes from the hash state.
99
//!
1010
//! # Usage
11-
//! - The prover and verifier absorb the same messages into identical `ShakeCodec` instances.
11+
//! - The prover and verifier absorb the same messages into identical [`ShakeCodec`] instances.
1212
//! - The prover and the verifier then squeeze the hash to generate a challenge scalar for the protocol. The verifier can check that the prover used the challenge output by the codec because he owns an identical codec.
1313
1414
use ff::PrimeField;

src/codec/trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Codec Trait
22
//!
3-
//! This module defines the `Codec` trait, a generic interface to manage codecs of a protocol execution.
3+
//! This module defines the [`Codec`] trait, a generic interface to manage codecs of a protocol execution.
44
55
pub trait DuplexSpongeInterface {
66
fn new(iv: &[u8]) -> Self;
@@ -17,7 +17,7 @@ pub trait DuplexSpongeInterface {
1717
/// The output is deterministic for a given set of input. Thus, both Prover and Verifier can generate the codec on their sides and ensure the same inputs have been used in both side of the protocol.
1818
///
1919
/// ## Minimal Implementation
20-
/// Types implementing `Codec` must define:
20+
/// Types implementing [`Codec`] must define:
2121
/// - `new`
2222
/// - `prover_message`
2323
/// - `verifier_challenge`

src/errors.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
//! These errors include:
77
//! - Verification failures (e.g., when a proof does not verify correctly).
88
//! - Mismatched parameters during batch verification.
9-
//! - Not implemented methods
10-
//! - Group element/scalar serialization failed
9+
//! - Unimplemented methods.
10+
//! - Group element or scalar serialization failures.
1111
use thiserror::Error;
1212
/// An error during proving or verification, such as a verification failure.
1313
#[derive(Debug, Error)]
1414
pub enum ProofError {
1515
/// Something is wrong with the proof, causing a verification failure.
1616
#[error("Verification failed.")]
1717
VerificationFailure,
18-
/// Occurs during batch verification if the batch parameters do not have the right size.
18+
/// Indicates a mismatch in parameter sizes during batch verification.
1919
#[error("Mismatched parameter sizes for batch verification.")]
2020
ProofSizeMismatch,
21-
/// Occurs when a feature is not implemented yet.
22-
#[error("The method is not yet implemented for this struct")]
21+
/// Occurs when a feature has not been implemented yet.
22+
#[error("The method is not yet implemented for this struct.")]
2323
NotImplemented(&'static str),
24-
/// Serialization of a group element/scalar failed
25-
#[error("Serialization of a group element/scalar failed")]
24+
/// Serialization of a group element/scalar has failed.
25+
#[error("Serialization of a group element/scalar failed.")]
2626
GroupSerializationFailure,
27-
/// Other error
27+
/// Other error.
2828
#[error("Other")]
2929
Other,
3030
}

src/fiat_shamir.rs

Lines changed: 97 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Fiat-Shamir transformation for Sigma protocols.
22
//!
3-
//! This module defines `NISigmaProtocol`, a generic non-interactive Sigma protocol wrapper,
3+
//! This module defines [`NISigmaProtocol`], a generic non-interactive Sigma protocol wrapper,
44
//! based on applying the Fiat-Shamir heuristic using a codec.
55
//!
66
//! It transforms an interactive Sigma protocol into a non-interactive one,
@@ -9,9 +9,9 @@
99
//!
1010
//! # Usage
1111
//! This struct is generic over:
12-
//! - `P`: the underlying Sigma protocol (`SigmaProtocol` trait).
13-
//! - `C`: the codec (`Codec` trait).
14-
//! - `G`: the group used for commitments and operations (`Group` trait).
12+
//! - `P`: the underlying Sigma protocol ([`SigmaProtocol`] trait).
13+
//! - `C`: the codec ([`Codec`] trait).
14+
//! - `G`: the group used for commitments and operations ([`Group`] trait).
1515
1616
use crate::{codec::Codec, CompactProtocol, ProofError, SigmaProtocol};
1717

@@ -26,7 +26,7 @@ type Transcript<P> = (
2626

2727
/// A Fiat-Shamir transformation of a Sigma protocol into a non-interactive proof.
2828
///
29-
/// `NISigmaProtocol` wraps an interactive Sigma protocol `P`
29+
/// [`NISigmaProtocol`] wraps an interactive Sigma protocol `P`
3030
/// and a hash-based codec `C`, to produce non-interactive proofs.
3131
///
3232
/// It manages the domain separation, codec reset,
@@ -54,7 +54,14 @@ where
5454
P: SigmaProtocol<Commitment = Vec<G>, Challenge = <G as Group>::Scalar>,
5555
C: Codec<Challenge = <G as Group>::Scalar> + Clone,
5656
{
57-
/// Creates a new non-interactive Sigma protocol, identified by a domain separator (usually fixed per protocol instantiation), and an initialized Sigma protocol instance.
57+
/// Constructs a new [`NISigmaProtocol`] instance.
58+
///
59+
/// # Parameters
60+
/// - `iv`: Domain separation tag for the hash function (e.g., protocol name or context).
61+
/// - `instance`: An instance of the interactive Sigma protocol.
62+
///
63+
/// # Returns
64+
/// A new [`NISigmaProtocol`] that can generate and verify non-interactive proofs.
5865
pub fn new(iv: &[u8], instance: P) -> Self {
5966
let hash_state = C::new(iv);
6067
Self {
@@ -63,7 +70,23 @@ where
6370
}
6471
}
6572

66-
/// Produces a non-interactive proof for a witness.
73+
/// Generates a non-interactive proof for a witness.
74+
///
75+
/// Executes the interactive protocol steps (commit, derive challenge via hash, respond),
76+
/// and checks the result locally for consistency.
77+
///
78+
/// # Parameters
79+
/// - `witness`: The secret witness for the Sigma protocol.
80+
/// - `rng`: A cryptographically secure random number generator.
81+
///
82+
/// # Returns
83+
/// A tuple of:
84+
/// - `P::Commitment`: The prover's commitment(s).
85+
/// - `P::Challenge`: The challenge derived via Fiat-Shamir.
86+
/// - `P::Response`: The prover's response.
87+
///
88+
/// # Panics
89+
/// Panics if local verification fails.
6790
pub fn prove(
6891
&mut self,
6992
witness: &P::Witness,
@@ -86,7 +109,21 @@ where
86109
Ok((commitment, challenge, response))
87110
}
88111

89-
/// Verify a non-interactive proof and returns a Result: `Ok(())` if the proof verifies successfully, `Err(())` otherwise.
112+
/// Verifies a non-interactive proof using the Fiat-Shamir transformation.
113+
///
114+
/// # Parameters
115+
/// - `commitment`: The commitment(s) sent by the prover.
116+
/// - `challenge`: The challenge allegedly derived via Fiat-Shamir.
117+
/// - `response`: The prover's response to the challenge.
118+
///
119+
/// # Returns
120+
/// - `Ok(())` if the proof is valid.
121+
/// - `Err(ProofError::VerificationFailure)` if the challenge is invalid or the response fails to verify.
122+
///
123+
/// # Errors
124+
/// - Returns `ProofError::VerificationFailure` if:
125+
/// - The challenge doesn't match the recomputed one from the commitment.
126+
/// - The response fails verification under the Sigma protocol.
90127
pub fn verify(
91128
&mut self,
92129
commitment: &P::Commitment,
@@ -108,7 +145,17 @@ where
108145
false => Err(ProofError::VerificationFailure),
109146
}
110147
}
111-
148+
/// Generates a batchable, serialized non-interactive proof.
149+
///
150+
/// # Parameters
151+
/// - `witness`: The secret witness.
152+
/// - `rng`: A cryptographically secure random number generator.
153+
///
154+
/// # Returns
155+
/// A serialized proof suitable for batch verification.
156+
///
157+
/// # Panics
158+
/// Panics if serialization fails (should not happen under correct implementation).
112159
pub fn prove_batchable(
113160
&mut self,
114161
witness: &P::Witness,
@@ -121,6 +168,19 @@ where
121168
.unwrap())
122169
}
123170

171+
/// Verifies a batchable non-interactive proof.
172+
///
173+
/// # Parameters
174+
/// - `proof`: A serialized batchable proof.
175+
///
176+
/// # Returns
177+
/// - `Ok(())` if the proof is valid.
178+
/// - `Err(ProofError)` if deserialization or verification fails.
179+
///
180+
/// # Errors
181+
/// - Returns `ProofError::VerificationFailure` if:
182+
/// - The challenge doesn't match the recomputed one from the commitment.
183+
/// - The response fails verification under the Sigma protocol.
124184
pub fn verify_batchable(&mut self, proof: &[u8]) -> Result<(), ProofError> {
125185
let (commitment, response) = self.sigmap.deserialize_batchable(proof).unwrap();
126186

@@ -144,6 +204,19 @@ where
144204
P: SigmaProtocol<Commitment = Vec<G>, Challenge = <G as Group>::Scalar> + CompactProtocol,
145205
C: Codec<Challenge = <G as Group>::Scalar> + Clone,
146206
{
207+
/// Generates a compact serialized proof.
208+
///
209+
/// Uses a more space-efficient representation compared to batchable proofs.
210+
///
211+
/// # Parameters
212+
/// - `witness`: The secret witness.
213+
/// - `rng`: A cryptographically secure random number generator.
214+
///
215+
/// # Returns
216+
/// A compact, serialized proof.
217+
///
218+
/// # Panics
219+
/// Panics if serialization fails.
147220
pub fn prove_compact(
148221
&mut self,
149222
witness: &P::Witness,
@@ -156,6 +229,21 @@ where
156229
.unwrap())
157230
}
158231

232+
/// Verifies a compact proof.
233+
///
234+
/// Recomputes the commitment from the challenge and response, then verifies it.
235+
///
236+
/// # Parameters
237+
/// - `proof`: A compact serialized proof.
238+
///
239+
/// # Returns
240+
/// - `Ok(())` if the proof is valid.
241+
/// - `Err(ProofError)` if deserialization or verification fails.
242+
///
243+
/// # Errors
244+
/// - Returns `ProofError::VerificationFailure` if:
245+
/// - Deserialization fails.
246+
/// - The recomputed commitment or response is invalid under the Sigma protocol.
159247
pub fn verify_compact(&mut self, proof: &[u8]) -> Result<(), ProofError> {
160248
let (challenge, response) = self.sigmap.deserialize_compact(proof).unwrap();
161249
// Compute the commitments

0 commit comments

Comments
 (0)