Skip to content

Commit 6e25040

Browse files
committed
perf(protocol): optimize deserialization and enhance test coverage
- perf: return proof size in deserialize_batchable to avoid unnecessary group calculations in Protocol implementations - test: add absorb_morphism step verification in Protocol test suite
1 parent efd6de6 commit 6e25040

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

src/fiat_shamir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ where
194194
/// - The challenge doesn't match the recomputed one from the commitment.
195195
/// - The response fails verification under the Sigma protocol.
196196
pub fn verify_batchable(&self, proof: &[u8]) -> Result<(), Error> {
197-
let (commitment, response) = self.sigmap.deserialize_batchable(proof).unwrap();
197+
let ((commitment, response), _) = self.sigmap.deserialize_batchable(proof).unwrap();
198198

199199
let mut codec = self.hash_state.clone();
200200

src/protocol.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -295,31 +295,32 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
295295
fn deserialize_batchable(
296296
&self,
297297
data: &[u8],
298-
) -> Result<(Self::Commitment, Self::Response), Error> {
298+
) -> Result<((Self::Commitment, Self::Response), usize), Error> {
299299
match self {
300300
Protocol::Simple(p) => {
301-
let (c, r) = p.deserialize_batchable(data)?;
302-
Ok((ProtocolCommitment::Simple(c), ProtocolResponse::Simple(r)))
301+
let ((c, r), size) = p.deserialize_batchable(data)?;
302+
Ok((
303+
(ProtocolCommitment::Simple(c), ProtocolResponse::Simple(r)),
304+
size,
305+
))
303306
}
304307
Protocol::And(ps) => {
305308
let mut cursor = 0;
306309
let mut commitments = Vec::with_capacity(ps.len());
307310
let mut responses = Vec::with_capacity(ps.len());
308311
for p in ps {
309-
let (p_commit, p_resp) = p.deserialize_batchable(&data[cursor..])?;
310-
let serialized = p.serialize_batchable(
311-
&p_commit,
312-
&p.simulate_transcript(&mut rand::thread_rng()).1,
313-
&p_resp,
314-
)?;
315-
cursor += serialized.len();
312+
let ((p_commit, p_resp), size) = p.deserialize_batchable(&data[cursor..])?;
313+
cursor += size;
316314

317315
commitments.push(p_commit);
318316
responses.push(p_resp);
319317
}
320318
Ok((
321-
ProtocolCommitment::And(commitments),
322-
ProtocolResponse::And(responses),
319+
(
320+
ProtocolCommitment::And(commitments),
321+
ProtocolResponse::And(responses),
322+
),
323+
cursor,
323324
))
324325
}
325326
Protocol::Or(ps) => {
@@ -333,14 +334,8 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
333334
.len();
334335

335336
for p in ps.iter() {
336-
let (c, r) = p.deserialize_batchable(&data[cursor..])?;
337-
338-
let serialized_cr = p.serialize_batchable(
339-
&c,
340-
&p.simulate_transcript(&mut rand::thread_rng()).1,
341-
&r,
342-
)?;
343-
cursor += serialized_cr.len();
337+
let ((c, r), size) = p.deserialize_batchable(&data[cursor..])?;
338+
cursor += size;
344339

345340
if data.len() < cursor + ch_bytes_len {
346341
return Err(Error::ProofSizeMismatch);
@@ -355,8 +350,11 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
355350
}
356351

357352
Ok((
358-
ProtocolCommitment::Or(commitments),
359-
ProtocolResponse::Or(challenges, responses),
353+
(
354+
ProtocolCommitment::Or(commitments),
355+
ProtocolResponse::Or(challenges, responses),
356+
),
357+
cursor,
360358
))
361359
}
362360
}

src/schnorr_protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ where
207207
fn deserialize_batchable(
208208
&self,
209209
data: &[u8],
210-
) -> Result<(Self::Commitment, Self::Response), Error> {
210+
) -> Result<((Self::Commitment, Self::Response), usize), Error> {
211211
let commit_nb = self.statements_nb();
212212
let response_nb = self.scalars_nb();
213213

@@ -242,7 +242,7 @@ where
242242
responses.push(scalar);
243243
}
244244

245-
Ok((commitments, responses))
245+
Ok(((commitments, responses), expected_len))
246246
}
247247
}
248248

src/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ pub trait SigmaProtocol {
7070

7171
/// Deserializes a batchable proof from bytes.
7272
///
73-
/// Returns `Some((commitment, response))` if parsing is successful, otherwise `None`.
73+
/// Returns `Ok(((commitment, response), proof_size))` if parsing is successful, otherwise `Err(Error)`.
7474
fn deserialize_batchable(
7575
&self,
7676
_data: &[u8],
77-
) -> Result<(Self::Commitment, Self::Response), Error>;
77+
) -> Result<((Self::Commitment, Self::Response), usize), Error>;
7878
}
7979

8080
/// A feature defining the behavior of a protocol for which it is possible to compact the proofs by omitting the commitments.
@@ -111,7 +111,7 @@ pub trait CompactProtocol: SigmaProtocol {
111111

112112
/// Deserializes a compact proof from bytes.
113113
///
114-
/// Returns `Some((challenge, response))` if parsing is successful, otherwise `None`.
114+
/// Returns `Ok((challenge, response))` if parsing is successful, otherwise `Err(Error)`.
115115
fn deserialize_compact(&self, _data: &[u8])
116116
-> Result<(Self::Challenge, Self::Response), Error>;
117117
}

tests/composition_protocol.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use group::Group;
33
use rand::rngs::OsRng;
44

55
use sigma_rs::codec::ShakeCodec;
6-
use sigma_rs::fiat_shamir::NISigmaProtocol;
6+
use sigma_rs::fiat_shamir::{HasGroupMorphism, NISigmaProtocol};
77
use sigma_rs::protocol::{Protocol, ProtocolWitness};
88
use sigma_rs::schnorr_protocol::SchnorrProtocol;
99
use sigma_rs::test_utils::{
@@ -84,9 +84,13 @@ fn composition_proof_correct() {
8484
let protocol = Protocol::And(vec![or_protocol1, simple_protocol1, and_protocol1]);
8585
let witness = ProtocolWitness::And(vec![or_witness1, simple_witness1, and_witness1]);
8686

87-
let nizk =
87+
let mut nizk =
8888
NISigmaProtocol::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol);
8989

90+
nizk.sigmap
91+
.absorb_morphism_structure(&mut nizk.hash_state)
92+
.unwrap();
93+
9094
// Batchable and compact proofs
9195
let proof_batchable_bytes = nizk.prove_batchable(&witness, &mut rng).unwrap();
9296
let proof_compact_bytes = nizk.prove_compact(&witness, &mut rng).unwrap();

tests/spec/custom_schnorr_protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ where
112112
fn deserialize_batchable(
113113
&self,
114114
data: &[u8],
115-
) -> Result<(Self::Commitment, Self::Response), Error> {
115+
) -> Result<((Self::Commitment, Self::Response), usize), Error> {
116116
let scalar_nb = self.0.morphism.num_scalars;
117117
let point_nb = self.0.morphism.constraints.len();
118118

@@ -147,7 +147,7 @@ where
147147
responses.push(scalar);
148148
}
149149

150-
Ok((commitments, responses))
150+
Ok(((commitments, responses), expected_len))
151151
}
152152
}
153153

0 commit comments

Comments
 (0)