Skip to content

Commit e859a76

Browse files
committed
feat: Refactor ProtocolProverState and ProtocolWitness in Or cases to improve clarity
- Replaced Vec with Box in the Or variants of ProtocolProverState and ProtocolWitness, since these were always single‑element vectors. This change improves readability and overall code comprehension. - Added ProtocolWitness::from helper methods for easier construction. - Updated all related tests to align with the new data structures.
1 parent d6a917b commit e859a76

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ let or_protocol = Protocol::Or(vec![
4848
]);
4949

5050
// If we know the second option, create witness for index 1
51-
let witness = ProtocolWitness::Or(1, vec![
52-
ProtocolWitness::And(vec![
51+
let witness = ProtocolWitness::from((1,
52+
ProtocolWitness::from(vec![
5353
ProtocolWitness::Simple(vec![y]),
5454
ProtocolWitness::Simple(vec![z]),
5555
])
56-
]);
56+
));
5757
```
5858

5959
## Examples

examples/simple_composition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn prove(P1: G, x2: Scalar, H: G) -> ProofResult<Vec<u8>> {
5555
let Q = H * x2;
5656

5757
let protocol = create_relation(P1, P2, Q, H);
58-
let witness = ProtocolWitness::Or(1, vec![ProtocolWitness::Simple(vec![x2])]);
58+
let witness = ProtocolWitness::from((1, ProtocolWitness::Simple(vec![x2])));
5959
let nizk = NISigmaProtocol::<_, ShakeCodec<G>>::new(b"or_proof_example", protocol);
6060

6161
nizk.prove_batchable(&witness, &mut rng)

src/composition.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub enum ProtocolProverState<G: Group + GroupEncoding> {
7777
And(Vec<ProtocolProverState<G>>),
7878
Or(
7979
usize, // real index
80-
Vec<ProtocolProverState<G>>, // real ProverState
80+
Box<ProtocolProverState<G>>, // real ProverState
8181
(Vec<ProtocolChallenge<G>>, Vec<ProtocolResponse<G>>), // simulated transcripts
8282
),
8383
}
@@ -94,7 +94,19 @@ pub enum ProtocolResponse<G: Group + GroupEncoding> {
9494
pub enum ProtocolWitness<G: Group + GroupEncoding> {
9595
Simple(<SchnorrProof<G> as SigmaProtocol>::Witness),
9696
And(Vec<ProtocolWitness<G>>),
97-
Or(usize, Vec<ProtocolWitness<G>>),
97+
Or(usize, Box<ProtocolWitness<G>>),
98+
}
99+
100+
impl<G: Group + GroupEncoding> From<Vec<ProtocolWitness<G>>> for ProtocolWitness<G> {
101+
fn from(value: Vec<ProtocolWitness<G>>) -> Self {
102+
Self::And(value)
103+
}
104+
}
105+
106+
impl<G: Group + GroupEncoding> From<(usize, ProtocolWitness<G>)> for ProtocolWitness<G> {
107+
fn from((i, witness): (usize, ProtocolWitness<G>)) -> Self {
108+
Self::Or(i, Box::new(witness))
109+
}
98110
}
99111

100112
// Structure representing the Challenge type of Protocol as SigmaProtocol
@@ -144,7 +156,7 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
144156
let mut simulated_challenges = Vec::new();
145157
let mut simulated_responses = Vec::new();
146158

147-
let (real_commitment, real_state) = ps[*w_index].prover_commit(&w[0], rng)?;
159+
let (real_commitment, real_state) = ps[*w_index].prover_commit(&w, rng)?;
148160

149161
for i in (0..ps.len()).filter(|i| i != w_index) {
150162
let (commitment, challenge, response) = ps[i].simulate_transcript(rng)?;
@@ -158,7 +170,7 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
158170
ProtocolCommitment::Or(commitments),
159171
ProtocolProverState::Or(
160172
*w_index,
161-
vec![real_state],
173+
Box::new(real_state),
162174
(simulated_challenges, simulated_responses),
163175
),
164176
))
@@ -203,8 +215,7 @@ impl<G: Group + GroupEncoding> SigmaProtocol for Protocol<G> {
203215
for ch in &simulated_challenges {
204216
real_challenge -= ch;
205217
}
206-
let real_response =
207-
ps[w_index].prover_response(real_state[0].clone(), &real_challenge)?;
218+
let real_response = ps[w_index].prover_response(*real_state, &real_challenge)?;
208219

209220
for (i, _) in ps.iter().enumerate() {
210221
if i == w_index {

src/tests/composition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn composition_proof_correct() {
6565
Protocol::Simple(SchnorrProof::from(relation1)),
6666
Protocol::Simple(SchnorrProof::from(relation2)),
6767
]);
68-
let or_witness1 = ProtocolWitness::Or(0, vec![ProtocolWitness::Simple(witness1)]);
68+
let or_witness1 = ProtocolWitness::from((0, ProtocolWitness::Simple(witness1)));
6969

7070
let simple_protocol1 = Protocol::from(relation3);
7171
let simple_witness1 = ProtocolWitness::Simple(witness3);
@@ -74,14 +74,14 @@ fn composition_proof_correct() {
7474
Protocol::Simple(SchnorrProof::from(relation4)),
7575
Protocol::Simple(SchnorrProof::from(relation5)),
7676
]);
77-
let and_witness1 = ProtocolWitness::And(vec![
77+
let and_witness1 = ProtocolWitness::from(vec![
7878
ProtocolWitness::Simple(witness4),
7979
ProtocolWitness::Simple(witness5),
8080
]);
8181

8282
// definition of the final protocol
8383
let protocol = Protocol::And(vec![or_protocol1, simple_protocol1, and_protocol1]);
84-
let witness = ProtocolWitness::And(vec![or_witness1, simple_witness1, and_witness1]);
84+
let witness = ProtocolWitness::from(vec![or_witness1, simple_witness1, and_witness1]);
8585

8686
let nizk =
8787
NISigmaProtocol::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol);

0 commit comments

Comments
 (0)