Skip to content

Commit 9edbb8f

Browse files
committed
fix composition relying on PrimeGroup
1 parent 484b365 commit 9edbb8f

File tree

6 files changed

+76
-94
lines changed

6 files changed

+76
-94
lines changed

examples/simple_composition.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ fn create_relation(P1: G, P2: G, Q: G, H: G) -> Protocol<G> {
4848
/// Prove knowledge of one of the witnesses (we know x2 for the DLEQ)
4949
#[allow(non_snake_case)]
5050
fn prove(P1: G, x2: Scalar, H: G) -> ProofResult<Vec<u8>> {
51-
let mut rng = OsRng;
52-
5351
// Compute public values
5452
let P2 = G::generator() * x2;
5553
let Q = H * x2;
@@ -58,7 +56,7 @@ fn prove(P1: G, x2: Scalar, H: G) -> ProofResult<Vec<u8>> {
5856
let witness = ProtocolWitness::Or(1, vec![ProtocolWitness::Simple(vec![x2])]);
5957
let nizk = Nizk::<_, ShakeCodec<G>>::new(b"or_proof_example", protocol);
6058

61-
nizk.prove_batchable(&witness, &mut rng)
59+
nizk.prove_batchable(&witness, &mut OsRng)
6260
}
6361

6462
/// Verify an OR proof given the public values
@@ -72,12 +70,10 @@ fn verify(P1: G, P2: G, Q: G, H: G, proof: &[u8]) -> ProofResult<()> {
7270

7371
#[allow(non_snake_case)]
7472
fn main() {
75-
let mut rng = OsRng;
76-
7773
// Setup: We don't know x1, but we do know x2
78-
let x1 = Scalar::random(&mut rng);
79-
let x2 = Scalar::random(&mut rng);
80-
let H = G::random(&mut rng);
74+
let x1 = Scalar::random(&mut OsRng);
75+
let x2 = Scalar::random(&mut OsRng);
76+
let H = G::random(&mut OsRng);
8177

8278
// Compute public values
8379
let P1 = G::generator() * x1; // We don't actually know x1 in the proof

src/composition.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! ```
2020
2121
use ff::{Field, PrimeField};
22-
use group::{Group, GroupEncoding};
22+
use group::{prime::PrimeGroup, Group};
2323
use sha3::Digest;
2424
use sha3::Sha3_256;
2525

@@ -38,15 +38,15 @@ use crate::{
3838
/// # Type Parameters
3939
/// - `G`: A cryptographic group implementing [`Group`] and [`GroupEncoding`].
4040
#[derive(Clone)]
41-
pub enum Protocol<G: Group + GroupEncoding> {
41+
pub enum Protocol<G: PrimeGroup> {
4242
Simple(SchnorrProof<G>),
4343
And(Vec<Protocol<G>>),
4444
Or(Vec<Protocol<G>>),
4545
}
4646

4747
impl<G> From<SchnorrProof<G>> for Protocol<G>
4848
where
49-
G: Group + GroupEncoding,
49+
G: PrimeGroup,
5050
{
5151
fn from(value: SchnorrProof<G>) -> Self {
5252
Protocol::Simple(value)
@@ -55,7 +55,7 @@ where
5555

5656
impl<G> From<LinearRelation<G>> for Protocol<G>
5757
where
58-
G: Group + GroupEncoding,
58+
G: PrimeGroup,
5959
{
6060
fn from(value: LinearRelation<G>) -> Self {
6161
Self::from(SchnorrProof::from(value))
@@ -64,15 +64,15 @@ where
6464

6565
// Structure representing the Commitment type of Protocol as SigmaProtocol
6666
#[derive(Clone)]
67-
pub enum ProtocolCommitment<G: Group + GroupEncoding> {
67+
pub enum ProtocolCommitment<G: PrimeGroup> {
6868
Simple(<SchnorrProof<G> as SigmaProtocol>::Commitment),
6969
And(Vec<ProtocolCommitment<G>>),
7070
Or(Vec<ProtocolCommitment<G>>),
7171
}
7272

7373
// Structure representing the ProverState type of Protocol as SigmaProtocol
7474
#[derive(Clone)]
75-
pub enum ProtocolProverState<G: Group + GroupEncoding> {
75+
pub enum ProtocolProverState<G: PrimeGroup> {
7676
Simple(<SchnorrProof<G> as SigmaProtocol>::ProverState),
7777
And(Vec<ProtocolProverState<G>>),
7878
Or(
@@ -84,14 +84,14 @@ pub enum ProtocolProverState<G: Group + GroupEncoding> {
8484

8585
// Structure representing the Response type of Protocol as SigmaProtocol
8686
#[derive(Clone)]
87-
pub enum ProtocolResponse<G: Group + GroupEncoding> {
87+
pub enum ProtocolResponse<G: PrimeGroup> {
8888
Simple(<SchnorrProof<G> as SigmaProtocol>::Response),
8989
And(Vec<ProtocolResponse<G>>),
9090
Or(Vec<ProtocolChallenge<G>>, Vec<ProtocolResponse<G>>),
9191
}
9292

9393
// Structure representing the Witness type of Protocol as SigmaProtocol
94-
pub enum ProtocolWitness<G: Group + GroupEncoding> {
94+
pub enum ProtocolWitness<G: PrimeGroup> {
9595
Simple(<SchnorrProof<G> as SigmaProtocol>::Witness),
9696
And(Vec<ProtocolWitness<G>>),
9797
Or(usize, Vec<ProtocolWitness<G>>),
@@ -100,7 +100,7 @@ pub enum ProtocolWitness<G: Group + GroupEncoding> {
100100
// Structure representing the Challenge type of Protocol as SigmaProtocol
101101
type ProtocolChallenge<G> = <SchnorrProof<G> as SigmaProtocol>::Challenge;
102102

103-
impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
103+
impl<G: PrimeGroup> SigmaProtocol for Protocol<G> {
104104
type Commitment = ProtocolCommitment<G>;
105105
type ProverState = ProtocolProverState<G>;
106106
type Response = ProtocolResponse<G>;
@@ -422,7 +422,7 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
422422
}
423423
}
424424

425-
impl<G: Group + GroupEncoding> SigmaProtocolSimulator for Protocol<G> {
425+
impl<G: PrimeGroup> SigmaProtocolSimulator for Protocol<G> {
426426
fn simulate_commitment(
427427
&self,
428428
challenge: &Self::Challenge,

src/schnorr_protocol.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
fn prover_commit(
9494
&self,
9595
witness: &Self::Witness,
96-
mut rng: &mut (impl RngCore + CryptoRng),
96+
rng: &mut (impl RngCore + CryptoRng),
9797
) -> Result<(Self::Commitment, Self::ProverState), Error> {
9898
if witness.len() != self.witness_length() {
9999
return Err(Error::InvalidInstanceWitnessPair);
@@ -105,7 +105,7 @@ where
105105
}
106106

107107
let nonces: Vec<G::Scalar> = (0..self.witness_length())
108-
.map(|_| G::Scalar::random(&mut rng))
108+
.map(|_| G::Scalar::random(&mut *rng))
109109
.collect();
110110
let commitment = self.evaluate(&nonces)?;
111111
let prover_state = (nonces, witness.clone());
@@ -296,9 +296,9 @@ where
296296
///
297297
/// # Returns
298298
/// - A commitment and response forming a valid proof for the given challenge.
299-
fn simulate_response<R: Rng + CryptoRng>(&self, mut rng: &mut R) -> Self::Response {
299+
fn simulate_response<R: Rng + CryptoRng>(&self, rng: &mut R) -> Self::Response {
300300
let response: Vec<G::Scalar> = (0..self.witness_length())
301-
.map(|_| G::Scalar::random(&mut rng))
301+
.map(|_| G::Scalar::random(&mut *rng))
302302
.collect();
303303
response
304304
}

src/tests/composition.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,43 @@ fn composition_proof_correct() {
2323
// Simple( discrete_logarithm ),
2424
// And( pedersen_commitment_dleq, bbs_blind_commitment_computation )
2525
// )
26-
let mut rng = OsRng;
2726
let domain_sep = b"hello world";
2827

2928
// definitions of the underlying protocols
30-
let (relation1, witness1) = dleq(G::random(&mut rng), <G as Group>::Scalar::random(&mut rng));
29+
let (relation1, witness1) = dleq(
30+
G::random(&mut OsRng),
31+
<G as Group>::Scalar::random(&mut OsRng),
32+
);
3133
let (relation2, _) = pedersen_commitment(
32-
G::random(&mut rng),
33-
<G as Group>::Scalar::random(&mut rng),
34-
<G as Group>::Scalar::random(&mut rng),
34+
G::random(&mut OsRng),
35+
<G as Group>::Scalar::random(&mut OsRng),
36+
<G as Group>::Scalar::random(&mut OsRng),
3537
);
36-
let (relation3, witness3) = discrete_logarithm(<G as Group>::Scalar::random(&mut rng));
38+
let (relation3, witness3) = discrete_logarithm(<G as Group>::Scalar::random(&mut OsRng));
3739
let (relation4, witness4) = pedersen_commitment_dleq(
3840
(0..4)
39-
.map(|_| G::random(&mut rng))
41+
.map(|_| G::random(&mut OsRng))
4042
.collect::<Vec<_>>()
4143
.try_into()
4244
.unwrap(),
4345
(0..2)
44-
.map(|_| <G as Group>::Scalar::random(&mut rng))
46+
.map(|_| <G as Group>::Scalar::random(&mut OsRng))
4547
.collect::<Vec<_>>()
4648
.try_into()
4749
.unwrap(),
4850
);
4951
let (relation5, witness5) = bbs_blind_commitment_computation(
5052
(0..4)
51-
.map(|_| G::random(&mut rng))
53+
.map(|_| G::random(&mut OsRng))
5254
.collect::<Vec<_>>()
5355
.try_into()
5456
.unwrap(),
5557
(0..3)
56-
.map(|_| <G as Group>::Scalar::random(&mut rng))
58+
.map(|_| <G as Group>::Scalar::random(&mut OsRng))
5759
.collect::<Vec<_>>()
5860
.try_into()
5961
.unwrap(),
60-
<G as Group>::Scalar::random(&mut rng),
62+
<G as Group>::Scalar::random(&mut OsRng),
6163
);
6264

6365
// second layer protocol definitions
@@ -86,8 +88,8 @@ fn composition_proof_correct() {
8688
let nizk = Nizk::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol);
8789

8890
// Batchable and compact proofs
89-
let proof_batchable_bytes = nizk.prove_batchable(&witness, &mut rng).unwrap();
90-
let proof_compact_bytes = nizk.prove_compact(&witness, &mut rng).unwrap();
91+
let proof_batchable_bytes = nizk.prove_batchable(&witness, &mut OsRng).unwrap();
92+
let proof_compact_bytes = nizk.prove_compact(&witness, &mut OsRng).unwrap();
9193
// Verify proofs
9294
let verified_batchable = nizk.verify_batchable(&proof_batchable_bytes).is_ok();
9395
let verified_compact = nizk.verify_compact(&proof_compact_bytes).is_ok();

0 commit comments

Comments
 (0)