Skip to content

Commit 1b9c1fe

Browse files
committed
Modified the GroupSerialisation trait to remove the dependency on a generic Group G and tested implementations for bls12_381
1 parent 9d74e37 commit 1b9c1fe

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

src/toolbox/sigma/group_serialisation.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ use bls12_381::{G1Affine, G1Projective, Scalar as BlsScalar};
55
use ff::PrimeField;
66
use super::r#trait::GroupSerialisation;
77

8-
pub struct RistrettoSerialisation;
9-
10-
impl GroupSerialisation<RistrettoPoint> for RistrettoSerialisation {
11-
type Scalar = RistrettoScalar;
12-
13-
fn serialize_element(point: &RistrettoPoint) -> Vec<u8> {
8+
impl GroupSerialisation for RistrettoPoint {
9+
fn serialize_element(point: &Self) -> Vec<u8> {
1410
point.compress().to_bytes().to_vec()
1511
}
1612

17-
fn deserialize_element(bytes: &[u8]) -> Option<RistrettoPoint> {
13+
fn deserialize_element(bytes: &[u8]) -> Option<Self> {
1814
let point_size = 32;
1915
if bytes.len() != point_size {
2016
return None;
@@ -38,11 +34,8 @@ impl GroupSerialisation<RistrettoPoint> for RistrettoSerialisation {
3834
}
3935
}
4036

41-
pub struct Bls12381Serialisation;
42-
43-
impl GroupSerialisation<G1Projective> for Bls12381Serialisation {
44-
type Scalar = BlsScalar;
4537

38+
impl GroupSerialisation for G1Projective {
4639
fn serialize_element(point: &G1Projective) -> Vec<u8> {
4740
let affine = G1Affine::from(point);
4841
affine.to_compressed().as_ref().to_vec()
@@ -54,8 +47,14 @@ impl GroupSerialisation<G1Projective> for Bls12381Serialisation {
5447
}
5548
let mut buf = [0u8; 48];
5649
buf.copy_from_slice(bytes);
57-
let affine = G1Affine::from_compressed(&buf).into_option()?;
58-
Some(G1Projective::from(&affine))
50+
let affine_ctoption = G1Affine::from_compressed(&buf);
51+
if affine_ctoption.is_some().into() {
52+
let affine = affine_ctoption.unwrap();
53+
Some(G1Projective::from(&affine))
54+
}
55+
else {
56+
None
57+
}
5958
}
6059

6160
fn serialize_scalar(scalar: &Self::Scalar) -> Vec<u8> {
@@ -64,6 +63,12 @@ impl GroupSerialisation<G1Projective> for Bls12381Serialisation {
6463

6564
fn deserialize_scalar(bytes: &[u8]) -> Option<Self::Scalar> {
6665
let repr = bytes.try_into().ok()?;
67-
BlsScalar::from_repr(repr).into_option()
66+
let result_ctoption = BlsScalar::from_repr(repr);
67+
if result_ctoption.is_some().into() {
68+
Some(result_ctoption.unwrap())
69+
}
70+
else {
71+
None
72+
}
6873
}
6974
}

src/toolbox/sigma/schnorr_proof.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ use super::r#trait::GroupSerialisation;
1414
/// A Schnorr protocol proving knowledge some discrete logarithm relation.
1515
///
1616
/// The specific proof instance is defined by a [`GroupMorphismPreimage`] over a group `G`.
17-
pub struct SchnorrProof<G, S>
17+
pub struct SchnorrProof<G>
1818
where
19-
G: Group + GroupEncoding,
20-
S: GroupSerialisation<G, Scalar = G::Scalar>
19+
G: Group + GroupEncoding + GroupSerialisation
2120
{
2221
/// The public instance and its associated group morphism.
23-
pub morphismp: GroupMorphismPreimage<G>,
24-
_marker: std::marker::PhantomData<S>
22+
pub morphismp: GroupMorphismPreimage<G>
2523
}
2624

2725
/// Internal prover state during the protocol execution: (random nonce, witness)
@@ -32,11 +30,10 @@ pub struct SchnorrState<S> {
3230
pub witness: Vec<S>,
3331
}
3432

35-
impl<G, S> SigmaProtocol for SchnorrProof<G,S>
33+
impl<G> SigmaProtocol for SchnorrProof<G>
3634
where
37-
G: Group + GroupEncoding,
35+
G: Group + GroupEncoding + GroupSerialisation,
3836
G::Scalar: Field + Clone,
39-
S: GroupSerialisation<G, Scalar = G::Scalar>
4037
{
4138
type Commitment = Vec<G>;
4239
type ProverState = (Vec<<G as Group>::Scalar>, Vec<<G as Group>::Scalar>);
@@ -105,12 +102,12 @@ where
105102

106103
// Serialize commitments
107104
for commit in commitment.iter().take(point_nb) {
108-
bytes.extend_from_slice(&S::serialize_element(commit));
105+
bytes.extend_from_slice(&G::serialize_element(commit));
109106
}
110107

111108
// Serialize responses
112109
for response in response.iter().take(scalar_nb) {
113-
bytes.extend_from_slice(&S::serialize_scalar(response));
110+
bytes.extend_from_slice(&G::serialize_scalar(response));
114111
}
115112
bytes
116113
}
@@ -139,7 +136,7 @@ where
139136
let end = start + point_size;
140137

141138
let slice = &data[start..end];
142-
let elem = S::deserialize_element(slice)?;
139+
let elem = G::deserialize_element(slice)?;
143140
commitments.push(elem);
144141
}
145142

@@ -148,7 +145,7 @@ where
148145
let end = start + scalar_size;
149146

150147
let slice = &data[start..end];
151-
let scalar = S::deserialize_scalar(slice)?;
148+
let scalar = G::deserialize_scalar(slice)?;
152149
responses.push(scalar);
153150
}
154151

src/toolbox/sigma/trait.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//!
33
//! This module defines the `SigmaProtocol` trait, a generic interface for 3-message Sigma protocols.
44
5-
use group::Group;
5+
use ff::PrimeField;
6+
use group::{Group, GroupEncoding};
67
use rand::{Rng, CryptoRng};
78
use crate::ProofError;
89

@@ -123,16 +124,13 @@ pub trait SigmaProtocolSimulator: SigmaProtocol {
123124
}
124125
}
125126

126-
pub trait GroupSerialisation<G>
127+
pub trait GroupSerialisation: Group + GroupEncoding
127128
where
128-
G: Group,
129+
Self::Scalar: PrimeField,
129130
{
130-
type Scalar: ff::PrimeField;
131-
132-
fn serialize_element(point: &G) -> Vec<u8>;
133-
fn deserialize_element(bytes: &[u8]) -> Option<G>;
131+
fn serialize_element(point: &Self) -> Vec<u8>;
132+
fn deserialize_element(bytes: &[u8]) -> Option<Self>;
134133

135134
fn serialize_scalar(scalar: &Self::Scalar) -> Vec<u8>;
136135
fn deserialize_scalar(bytes: &[u8]) -> Option<Self::Scalar>;
137-
138136
}

tests/sage_proofs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use bls12_381::G1Projective;
12
use rand::{{Rng, CryptoRng}, rngs::OsRng};
23
use group::{Group, GroupEncoding, ff::Field};
3-
use curve25519_dalek::ristretto::RistrettoPoint;
44

55
use sigma_rs::toolbox::sigma::{
66
GroupMorphismPreimage,
@@ -9,7 +9,7 @@ use sigma_rs::toolbox::sigma::{
99
NISigmaProtocol,
1010
};
1111

12-
type G = RistrettoPoint;
12+
type G = G1Projective;
1313

1414
fn msm_pr<G: Group>(scalars: &[G::Scalar], bases: &[G]) -> G {
1515
let mut acc = G::identity();

0 commit comments

Comments
 (0)