Skip to content

Commit 67b1598

Browse files
committed
refactor(trait): simplify HasGroupMorphism trait and implement for Protocol
- refactor: simplify HasGroupMorphism trait interface and implementation - feat: add HasGroupMorphism trait implementation for Protocol structure
1 parent 33793c8 commit 67b1598

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

src/fiat_shamir.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
1515
use crate::codec::Codec;
1616
use crate::errors::Error;
17-
use crate::group_morphism::HasGroupMorphism;
1817
use crate::traits::{CompactProtocol, SigmaProtocol};
1918

20-
use group::{Group, GroupEncoding};
2119
use rand::{CryptoRng, RngCore};
2220

2321
pub trait FiatShamir<C: Codec>: SigmaProtocol {
@@ -26,6 +24,13 @@ pub trait FiatShamir<C: Codec>: SigmaProtocol {
2624
fn get_challenge(&self, codec: &mut C) -> Result<Self::Challenge, Error>;
2725
}
2826

27+
/// Trait for accessing the underlying group morphism in a Sigma protocol.
28+
pub trait HasGroupMorphism {
29+
/// Absorbs the morphism structure into a codec.
30+
/// Only compatible with 64-bit platforms
31+
fn absorb_morphism_structure<C: Codec>(&self, codec: &mut C) -> Result<(), Error>;
32+
}
33+
2934
type Transcript<P> = (
3035
<P as SigmaProtocol>::Commitment,
3136
<P as SigmaProtocol>::Challenge,
@@ -249,13 +254,12 @@ where
249254

250255
impl<P, C> NISigmaProtocol<P, C>
251256
where
252-
P: SigmaProtocol<Challenge = <P::Group as Group>::Scalar> + HasGroupMorphism + FiatShamir<C>,
257+
P: SigmaProtocol + HasGroupMorphism + FiatShamir<C>,
253258
P::Challenge: PartialEq,
254-
P::Group: Group + GroupEncoding,
255259
C: Codec<Challenge = P::Challenge> + Clone,
256260
{
257261
/// Absorbs the morphism structure into the transcript codec.
258262
pub fn absorb_morphism(&self, codec: &mut C) -> Result<(), Error> {
259-
self.sigmap.absorb_morphism_structure(codec)
263+
self.sigmap.absorb_morphism_structure::<C>(codec)
260264
}
261265
}

src/group_morphism.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
//! - [`Morphism`]: a collection of linear combinations acting on group elements
99
//! - [`GroupMorphismPreimage`]: a higher-level structure managing morphisms and their associated images
1010
11-
use std::iter;
12-
13-
use crate::codec::Codec;
1411
use crate::errors::Error;
1512
use group::{Group, GroupEncoding};
13+
use std::iter;
1614

1715
/// Implementations of core ops for the linear combination types.
1816
mod ops;
@@ -460,24 +458,3 @@ where
460458
.collect()
461459
}
462460
}
463-
464-
/// Trait for accessing the underlying group morphism in a Sigma protocol.
465-
pub trait HasGroupMorphism {
466-
type Group: Group + GroupEncoding;
467-
fn group_morphism(&self) -> &GroupMorphismPreimage<Self::Group>;
468-
469-
/// Absorbs the morphism structure into a codec.
470-
/// Only compatible with 64-bit platforms
471-
fn absorb_morphism_structure<C: Codec>(&self, codec: &mut C) -> Result<(), Error> {
472-
let morphism = self.group_morphism();
473-
for lc in &morphism.morphism.constraints {
474-
for term in lc.terms() {
475-
let mut buf = [0u8; 16];
476-
buf[..8].copy_from_slice(&(term.scalar().index() as u64).to_le_bytes());
477-
buf[8..].copy_from_slice(&(term.elem().index() as u64).to_le_bytes());
478-
codec.prover_message(&buf);
479-
}
480-
}
481-
Ok(())
482-
}
483-
}

src/protocol.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::codec::Codec;
55
use crate::traits::CompactProtocol;
66
use crate::{
77
errors::Error,
8-
fiat_shamir::FiatShamir,
8+
fiat_shamir::{FiatShamir, HasGroupMorphism},
99
group_serialization::{deserialize_scalar, serialize_scalar},
1010
schnorr_protocol::SchnorrProtocol,
1111
traits::{SigmaProtocol, SigmaProtocolSimulator},
@@ -622,3 +622,17 @@ where
622622
Ok(codec.verifier_challenge())
623623
}
624624
}
625+
626+
impl<G: Group + GroupEncoding> HasGroupMorphism for Protocol<G> {
627+
fn absorb_morphism_structure<C: Codec>(&self, codec: &mut C) -> Result<(), Error> {
628+
match self {
629+
Protocol::Simple(p) => p.absorb_morphism_structure(codec),
630+
Protocol::And(ps) | Protocol::Or(ps) => {
631+
for p in ps {
632+
p.absorb_morphism_structure(codec)?
633+
}
634+
Ok(())
635+
}
636+
}
637+
}
638+
}

src/schnorr_protocol.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
77
use crate::codec::Codec;
88
use crate::errors::Error;
9-
use crate::fiat_shamir::FiatShamir;
10-
use crate::group_morphism::{GroupMorphismPreimage, HasGroupMorphism};
9+
use crate::fiat_shamir::{FiatShamir, HasGroupMorphism};
10+
use crate::group_morphism::GroupMorphismPreimage;
1111
use crate::{
1212
group_serialization::*,
1313
traits::{CompactProtocol, SigmaProtocol, SigmaProtocolSimulator},
@@ -431,8 +431,15 @@ where
431431
}
432432

433433
impl<G: Group + GroupEncoding> HasGroupMorphism for SchnorrProtocol<G> {
434-
type Group = G;
435-
fn group_morphism(&self) -> &GroupMorphismPreimage<G> {
436-
&self.0
434+
fn absorb_morphism_structure<C: Codec>(&self, codec: &mut C) -> Result<(), Error> {
435+
for lc in &self.0.morphism.constraints {
436+
for term in lc.terms() {
437+
let mut buf = [0u8; 16];
438+
buf[..8].copy_from_slice(&(term.scalar().index() as u64).to_le_bytes());
439+
buf[8..].copy_from_slice(&(term.elem().index() as u64).to_le_bytes());
440+
codec.prover_message(&buf);
441+
}
442+
}
443+
Ok(())
437444
}
438445
}

tests/morphism_preimage.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use bls12_381::{G1Projective as G, Scalar};
22
use group::{Group, ff::Field};
33
use rand::rngs::OsRng;
44

5-
use sigma_rs::fiat_shamir::NISigmaProtocol;
6-
use sigma_rs::group_morphism::HasGroupMorphism;
5+
use sigma_rs::fiat_shamir::{HasGroupMorphism, NISigmaProtocol};
76
use sigma_rs::test_utils::{
87
bbs_blind_commitment_computation, discrete_logarithm, dleq, pedersen_commitment,
98
pedersen_commitment_dleq,

0 commit comments

Comments
 (0)