1111//! This struct is generic over:
1212//! - `P`: the underlying Sigma protocol ([`SigmaProtocol`] trait).
1313//! - `C`: the codec ([`Codec`] trait).
14- //! - `G`: the group used for commitments and operations ([`Group`] trait).
1514
1615use crate :: codec:: Codec ;
1716use crate :: errors:: Error ;
1817use crate :: traits:: { CompactProtocol , SigmaProtocol } ;
1918
20- use group:: { Group , GroupEncoding } ;
2119use rand:: { CryptoRng , RngCore } ;
2220
21+ pub trait FiatShamir < C : Codec > : SigmaProtocol {
22+ fn push_commitment ( & self , codec : & mut C , commitment : & Self :: Commitment ) ;
23+
24+ fn get_challenge ( & self , codec : & mut C ) -> Result < Self :: Challenge , Error > ;
25+ }
26+
2327type Transcript < P > = (
2428 <P as SigmaProtocol >:: Commitment ,
2529 <P as SigmaProtocol >:: Challenge ,
@@ -37,12 +41,10 @@ type Transcript<P> = (
3741/// # Type Parameters
3842/// - `P`: the Sigma protocol implementation.
3943/// - `C`: the codec used for Fiat-Shamir.
40- /// - `G`: the group on which the protocol operates.
41- pub struct NISigmaProtocol < P , C , G >
44+ pub struct NISigmaProtocol < P , C >
4245where
43- G : Group + GroupEncoding ,
44- P : SigmaProtocol < Commitment = Vec < G > , Challenge = <G as Group >:: Scalar > ,
45- C : Codec < Challenge = <G as Group >:: Scalar > ,
46+ P : SigmaProtocol < Challenge : PartialEq > + FiatShamir < C > ,
47+ C : Codec < Challenge = P :: Challenge > ,
4648{
4749 /// Current codec state.
4850 pub hash_state : C ,
@@ -51,11 +53,10 @@ where
5153}
5254
5355// TODO: Write a serialization of the morphism to the transcript.
54- impl < P , C , G > NISigmaProtocol < P , C , G >
56+ impl < P , C > NISigmaProtocol < P , C >
5557where
56- G : Group + GroupEncoding ,
57- P : SigmaProtocol < Commitment = Vec < G > , Challenge = <G as Group >:: Scalar > ,
58- C : Codec < Challenge = <G as Group >:: Scalar > + Clone ,
58+ P : SigmaProtocol < Challenge : PartialEq > + FiatShamir < C > ,
59+ C : Codec < Challenge = P :: Challenge > + Clone ,
5960{
6061 /// Constructs a new [`NISigmaProtocol`] instance.
6162 ///
9899 let mut codec = self . hash_state . clone ( ) ;
99100
100101 let ( commitment, prover_state) = self . sigmap . prover_commit ( witness, rng) ?;
101- // Commitment data for challenge generation
102- let mut data = Vec :: new ( ) ;
103- for commit in & commitment {
104- data. extend_from_slice ( commit. to_bytes ( ) . as_ref ( ) ) ;
105- }
106102 // Fiat Shamir challenge
107- let challenge = codec. prover_message ( & data) . verifier_challenge ( ) ;
103+ self . sigmap . push_commitment ( & mut codec, & commitment) ;
104+ let challenge = self . sigmap . get_challenge ( & mut codec) ?;
108105 // Prover's response
109106 let response = self . sigmap . prover_response ( prover_state, & challenge) ?;
110107 // Local verification of the proof
@@ -135,13 +132,9 @@ where
135132 ) -> Result < ( ) , Error > {
136133 let mut codec = self . hash_state . clone ( ) ;
137134
138- // Commitment data for expected challenge generation
139- let mut data = Vec :: new ( ) ;
140- for commit in commitment {
141- data. extend_from_slice ( commit. to_bytes ( ) . as_ref ( ) ) ;
142- }
143135 // Recompute the challenge
144- let expected_challenge = codec. prover_message ( & data) . verifier_challenge ( ) ;
136+ self . sigmap . push_commitment ( & mut codec, commitment) ;
137+ let expected_challenge = self . sigmap . get_challenge ( & mut codec) ?;
145138 // Verification of the proof
146139 match * challenge == expected_challenge {
147140 true => self . sigmap . verifier ( commitment, challenge, response) ,
@@ -189,23 +182,18 @@ where
189182
190183 let mut codec = self . hash_state . clone ( ) ;
191184
192- // Commitment data for expected challenge generation
193- let mut data = Vec :: new ( ) ;
194- for commit in & commitment {
195- data. extend_from_slice ( commit. to_bytes ( ) . as_ref ( ) ) ;
196- }
197185 // Recompute the challenge
198- let challenge = codec. prover_message ( & data) . verifier_challenge ( ) ;
186+ self . sigmap . push_commitment ( & mut codec, & commitment) ;
187+ let challenge = self . sigmap . get_challenge ( & mut codec) ?;
199188 // Verification of the proof
200189 self . sigmap . verifier ( & commitment, & challenge, & response)
201190 }
202191}
203192
204- impl < P , C , G > NISigmaProtocol < P , C , G >
193+ impl < P , C > NISigmaProtocol < P , C >
205194where
206- G : Group + GroupEncoding ,
207- P : SigmaProtocol < Commitment = Vec < G > , Challenge = <G as Group >:: Scalar > + CompactProtocol ,
208- C : Codec < Challenge = <G as Group >:: Scalar > + Clone ,
195+ P : SigmaProtocol < Challenge : PartialEq > + CompactProtocol + FiatShamir < C > ,
196+ C : Codec < Challenge = P :: Challenge > + Clone ,
209197{
210198 /// Generates a compact serialized proof.
211199 ///
0 commit comments