Skip to content

Commit b05fe10

Browse files
committed
Creating a SigmaProtocolSimulator trait to extend the SigmaProtocol trait
1 parent 5697a94 commit b05fe10

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

src/toolbox/sigma/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod group_morphism;
55
pub mod schnorr_proof;
66
pub mod transcript;
77

8-
pub use r#trait::SigmaProtocol;
8+
pub use r#trait::{SigmaProtocol, SigmaProtocolSimulator};
99
pub use proof_composition::{AndProtocol, OrProtocol};
1010
pub use fiat_shamir::NISigmaProtocol;
1111
pub use schnorr_proof::SchnorrProof;

src/toolbox/sigma/proof_composition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::toolbox::sigma::SigmaProtocol;
1+
use crate::toolbox::sigma::{SigmaProtocol, SigmaProtocolSimulator};
22
use rand::{Rng, CryptoRng};
33
use ff::PrimeField;
44

@@ -110,8 +110,8 @@ where
110110
impl<P, Q, C> SigmaProtocol for OrProtocol<P, Q>
111111
where
112112
C: PrimeField,
113-
P: SigmaProtocol<Challenge = C>,
114-
Q: SigmaProtocol<Challenge = C>,
113+
P: SigmaProtocol<Challenge = C> + SigmaProtocolSimulator,
114+
Q: SigmaProtocol<Challenge = C> + SigmaProtocolSimulator,
115115
P::Response: Clone,
116116
Q::Response: Clone,
117117
{

src/toolbox/sigma/trait.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ pub trait SigmaProtocol {
2727
response: &Self::Response,
2828
) -> Result<(), ()>;
2929

30-
fn simulate_proof(
31-
&self,
32-
_challenge: &Self::Challenge,
33-
_rng: &mut (impl Rng + CryptoRng)
34-
) -> (Self::Commitment, Self::Response) {
35-
panic!("simulatable_proof not implemented for this protocol")
36-
}
37-
38-
fn simulate_transcription(
39-
&self, _rng: &mut (impl Rng + CryptoRng)
40-
) -> (Self::Commitment, Self::Challenge, Self::Response) {
41-
panic!("simulatable_transcription not implemented for this protocol")
42-
}
43-
4430
fn serialize_batchable(
4531
&self,
4632
_commitment: &Self::Commitment,
@@ -55,6 +41,23 @@ pub trait SigmaProtocol {
5541
) -> Option<(Self::Commitment, Self::Response)> {
5642
panic!("deserialize_batchable not implemented for this protocol")
5743
}
44+
}
5845

5946

47+
pub trait SigmaProtocolSimulator
48+
where Self: SigmaProtocol {
49+
50+
fn simulate_proof(
51+
&self,
52+
_challenge: &Self::Challenge,
53+
_rng: &mut (impl Rng + CryptoRng)
54+
) -> (Self::Commitment, Self::Response) {
55+
panic!("simulatable_proof not implemented for this protocol")
56+
}
57+
58+
fn simulate_transcription(
59+
&self, _rng: &mut (impl Rng + CryptoRng)
60+
) -> (Self::Commitment, Self::Challenge, Self::Response) {
61+
panic!("simulatable_transcription not implemented for this protocol")
62+
}
6063
}

tests/proof_composition_test.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rand::{rngs::OsRng, CryptoRng, Rng};
2-
use sigma_rs::toolbox::sigma::{proof_composition::OrEnum, AndProtocol, OrProtocol, SigmaProtocol};
2+
use sigma_rs::toolbox::sigma::{proof_composition::OrEnum, SigmaProtocolSimulator, AndProtocol, OrProtocol, SigmaProtocol};
33
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
44

55
pub struct SchnorrZkp {
@@ -45,26 +45,32 @@ impl SigmaProtocol for SchnorrZkp {
4545
false => Err(()),
4646
}
4747
}
48+
}
4849

50+
#[allow(non_snake_case)]
51+
impl SigmaProtocolSimulator for SchnorrZkp {
4952
fn simulate_proof(
50-
&self,
51-
challenge: &Self::Challenge,
52-
rng: &mut (impl Rng + CryptoRng)
53-
) -> (Self::Commitment, Self::Response) {
54-
let z = Scalar::random(rng);
55-
let R = z * self.generator - challenge * self.target;
56-
(R,z)
57-
}
53+
&self,
54+
challenge: &Self::Challenge,
55+
rng: &mut (impl Rng + CryptoRng)
56+
) -> (Self::Commitment, Self::Response) {
57+
let z = Scalar::random(rng);
58+
let R = z * self.generator - challenge * self.target;
59+
(R,z)
60+
}
5861

59-
fn simulate_transcription(
60-
&self, rng: &mut (impl Rng + CryptoRng)
61-
) -> (Self::Commitment, Self::Challenge, Self::Response) {
62-
let challenge = Scalar::random(rng);
63-
let (commitment, response) = self.simulate_proof(&challenge, rng);
64-
(commitment, challenge, response)
65-
}
62+
fn simulate_transcription(
63+
&self, rng: &mut (impl Rng + CryptoRng)
64+
) -> (Self::Commitment, Self::Challenge, Self::Response) {
65+
let challenge = Scalar::random(rng);
66+
let (commitment, response) = self.simulate_proof(&challenge, rng);
67+
(commitment, challenge, response)
68+
}
6669
}
6770

71+
72+
// Proof calculation and verification in an AND-protocol in the case where:
73+
// both protocols are SchnorrZkp and the proof is correct
6874
#[allow(non_snake_case)]
6975
#[test]
7076
fn andproof_schnorr_correct() {
@@ -101,6 +107,9 @@ fn andproof_schnorr_correct() {
101107
assert!(result == Ok(()));
102108
}
103109

110+
111+
// Proof calculation and verification in an AND-protocol in the case where:
112+
// both protocols are SchnorrZkp and the proof is incorrect
104113
#[allow(non_snake_case)]
105114
#[test]
106115
fn andproof_schnorr_incorrect() {
@@ -138,6 +147,9 @@ fn andproof_schnorr_incorrect() {
138147
assert!(result == Err(()));
139148
}
140149

150+
151+
// Proof calculation and verification in an OR-protocol in the case where:
152+
// both protocols are SchnorrZkp and the proof is correct
141153
#[allow(non_snake_case)]
142154
#[test]
143155
fn orproof_schnorr_correct() {
@@ -173,6 +185,9 @@ fn orproof_schnorr_correct() {
173185
assert!(result == Ok(()));
174186
}
175187

188+
189+
// Proof calculation and verification in an OR-protocol in the case where:
190+
// both protocols are SchnorrZkp and the proof is incorrect
176191
#[allow(non_snake_case)]
177192
#[test]
178193
fn orproof_schnorr_incorrect() {

0 commit comments

Comments
 (0)