Skip to content

Commit 9cad94c

Browse files
Fix some Clippy warnings and update tests slightly
1 parent 2467add commit 9cad94c

File tree

3 files changed

+108
-35
lines changed

3 files changed

+108
-35
lines changed

rust/catalyst-contest/src/choices.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Decode<'_, ()> for Choices {
4949
0 => Ok(Self::Clear(<Vec<i64>>::decode(d, ctx)?)),
5050
1 => {
5151
let len = decode_array_len(d, "elgamal-ristretto255-encrypted-choices")?;
52-
if len < 1 || len > 2 {
52+
if !(1..=2).contains(&len) {
5353
return Err(minicbor::decode::Error::message(format!(
5454
"Unexpected elgamal-ristretto255-encrypted-choices array length {len}, expected 1 or 2"
5555
)));
@@ -87,7 +87,7 @@ impl Encode<()> for Choices {
8787
Choices::ElgamalRistretto255 { choices, row_proof } => {
8888
e.array(2)?;
8989
1.encode(e, ctx)?;
90-
e.array(choices.len() as u64 + row_proof.is_some() as u64)?;
90+
e.array(choices.len() as u64 + u64::from(row_proof.is_some()))?;
9191
choices.encode(e, ctx)?;
9292
if let Some(row_proof) = row_proof {
9393
row_proof.encode(e, ctx)?;
@@ -101,6 +101,7 @@ impl Encode<()> for Choices {
101101
#[cfg(test)]
102102
mod tests {
103103
use super::*;
104+
use crate::row_proof::{ProofAnnouncement, ProofResponse, ProofScalar, SingleSelectionProof};
104105

105106
#[test]
106107
fn clear_roundtrip() {
@@ -115,9 +116,23 @@ mod tests {
115116

116117
#[test]
117118
fn elgamal_ristretto255_roundtrip() {
119+
let bytes = [
120+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
121+
25, 26, 27, 28, 29, 30, 31, 32,
122+
];
118123
let original = Choices::ElgamalRistretto255 {
119124
choices: vec![],
120-
row_proof: Some(RowProof {}),
125+
row_proof: Some(RowProof {
126+
selections: vec![SingleSelectionProof {
127+
announcement: ProofAnnouncement {},
128+
choice: ElgamalRistretto255Choice {
129+
c1: bytes,
130+
c2: bytes,
131+
},
132+
response: ProofResponse {},
133+
}],
134+
scalar: ProofScalar(bytes),
135+
}),
121136
};
122137
let mut buffer = Vec::new();
123138
original

rust/catalyst-contest/src/contest_ballot.rs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,47 @@ impl Decode<'_, ()> for ContentBallot {
3535
d: &mut Decoder<'_>,
3636
ctx: &mut (),
3737
) -> Result<Self, minicbor::decode::Error> {
38+
use minicbor::data::Type;
39+
3840
let len = decode_map_len(d, "content ballot")?;
39-
// TODO: FIXME:
41+
42+
let mut choices = BTreeMap::new();
43+
let mut column_proof = None;
44+
let mut matrix_proof = None;
45+
let mut voter_choices = None;
4046
for _ in 0..len {
41-
// TODO: FIXME:
47+
match d.datatype()? {
48+
Type::U64 => {
49+
let key = d.u64()?;
50+
let val = Choices::decode(d, ctx)?;
51+
choices.insert(key, val);
52+
},
53+
Type::String => {
54+
match d.str()? {
55+
"column-proof" => column_proof = Some(ColumnProof::decode(d, ctx)?),
56+
"matrix-proof" => matrix_proof = Some(MatrixProof::decode(d, ctx)?),
57+
"voter-choices" => voter_choices = Some(EncryptedChoices::decode(d, ctx)?),
58+
key => {
59+
return Err(minicbor::decode::Error::message(format!(
60+
"Unexpected content ballot key value: {key:?}"
61+
)));
62+
},
63+
}
64+
},
65+
t => {
66+
return Err(minicbor::decode::Error::message(format!(
67+
"Unexpected content ballot key type: {t:?}"
68+
)));
69+
},
70+
}
4271
}
43-
todo!()
72+
73+
Ok(Self {
74+
choices,
75+
column_proof,
76+
matrix_proof,
77+
voter_choices,
78+
})
4479
}
4580
}
4681

@@ -51,12 +86,12 @@ impl Encode<()> for ContentBallot {
5186
_ctx: &mut (),
5287
) -> Result<(), minicbor::encode::Error<W::Error>> {
5388
let len = self.choices.len() as u64
54-
+ self.column_proof.is_some() as u64
55-
+ self.matrix_proof.is_some() as u64
56-
+ self.voter_choices.is_some() as u64;
89+
+ u64::from(self.column_proof.is_some())
90+
+ u64::from(self.matrix_proof.is_some())
91+
+ u64::from(self.voter_choices.is_some());
5792
e.map(len)?;
5893

59-
for (&key, val) in self.choices.iter() {
94+
for (&key, val) in &self.choices {
6095
e.u64(key)?.encode(val)?;
6196
}
6297
if let Some(column_proof) = self.column_proof.as_ref() {
@@ -76,7 +111,11 @@ impl Encode<()> for ContentBallot {
76111
#[cfg(test)]
77112
mod tests {
78113
use super::*;
79-
use crate::{EncryptedBlock, RowProof, elgamal_ristretto255_choice::ElgamalRistretto255Choice};
114+
use crate::{
115+
EncryptedBlock, RowProof,
116+
elgamal_ristretto255_choice::ElgamalRistretto255Choice,
117+
row_proof::{ProofAnnouncement, ProofResponse, ProofScalar, SingleSelectionProof},
118+
};
80119

81120
#[test]
82121
fn roundtrip() {
@@ -86,7 +125,7 @@ mod tests {
86125
];
87126
let original = ContentBallot {
88127
choices: [
89-
(1, Choices::Clear(vec![1, 2, 3, -4, -5].into())),
128+
(1, Choices::Clear(vec![1, 2, 3, -4, -5])),
90129
(2, Choices::ElgamalRistretto255 {
91130
choices: vec![ElgamalRistretto255Choice {
92131
c1: bytes,
@@ -99,7 +138,17 @@ mod tests {
99138
c1: bytes,
100139
c2: bytes,
101140
}],
102-
row_proof: Some(RowProof {}),
141+
row_proof: Some(RowProof {
142+
selections: vec![SingleSelectionProof {
143+
announcement: ProofAnnouncement {},
144+
choice: ElgamalRistretto255Choice {
145+
c1: bytes,
146+
c2: bytes,
147+
},
148+
response: ProofResponse {},
149+
}],
150+
scalar: ProofScalar(bytes),
151+
}),
103152
}),
104153
]
105154
.into(),

rust/catalyst-contest/src/row_proof.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,9 @@ impl Encode<()> for RowProof {
124124

125125
impl Decode<'_, ()> for SingleSelectionProof {
126126
fn decode(
127-
d: &mut Decoder<'_>,
128-
ctx: &mut (),
127+
_d: &mut Decoder<'_>,
128+
_ctx: &mut (),
129129
) -> Result<Self, minicbor::decode::Error> {
130-
let len = decode_array_len(d, "row proof")?;
131-
if len != 3 {
132-
return Err(minicbor::decode::Error::message(format!(
133-
"Unexpected row proof array length {len}, expected 3"
134-
)));
135-
}
136-
137-
let announcement
138-
139-
/*
140-
pub announcement: ProofAnnouncement,
141-
/// An elgamal encrypted ciphertext.
142-
pub choice: ElgamalRistretto255Choice,
143-
/// A ZK proof response values for Ed25519.
144-
pub response: ProofResponse,
145-
*/
146130
// TODO: FIXME:
147131
todo!()
148132
}
@@ -151,8 +135,8 @@ impl Decode<'_, ()> for SingleSelectionProof {
151135
impl Encode<()> for SingleSelectionProof {
152136
fn encode<W: Write>(
153137
&self,
154-
e: &mut Encoder<W>,
155-
ctx: &mut (),
138+
_e: &mut Encoder<W>,
139+
_ctx: &mut (),
156140
) -> Result<(), minicbor::encode::Error<W::Error>> {
157141
// TODO: FIXME:
158142
todo!()
@@ -184,7 +168,21 @@ mod tests {
184168

185169
#[test]
186170
fn row_proof_roundtrip() {
187-
let original = RowProof {};
171+
let bytes = [
172+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
173+
25, 26, 27, 28, 29, 30, 31, 32,
174+
];
175+
let original = RowProof {
176+
selections: vec![SingleSelectionProof {
177+
announcement: ProofAnnouncement {},
178+
choice: ElgamalRistretto255Choice {
179+
c1: bytes,
180+
c2: bytes,
181+
},
182+
response: ProofResponse {},
183+
}],
184+
scalar: ProofScalar(bytes),
185+
};
188186
let mut buffer = Vec::new();
189187
original
190188
.encode(&mut Encoder::new(&mut buffer), &mut ())
@@ -195,7 +193,18 @@ mod tests {
195193

196194
#[test]
197195
fn single_selection_proof_roundtrip() {
198-
let original = SingleSelectionProof {};
196+
let bytes = [
197+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
198+
25, 26, 27, 28, 29, 30, 31, 32,
199+
];
200+
let original = SingleSelectionProof {
201+
announcement: ProofAnnouncement {},
202+
choice: ElgamalRistretto255Choice {
203+
c1: bytes,
204+
c2: bytes,
205+
},
206+
response: ProofResponse {},
207+
};
199208
let mut buffer = Vec::new();
200209
original
201210
.encode(&mut Encoder::new(&mut buffer), &mut ())

0 commit comments

Comments
 (0)