Skip to content

Commit c805686

Browse files
Decode choices
1 parent e4f195b commit c805686

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

rust/catalyst-contest/src/choices.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,34 @@ impl Decode<'_, ()> for Choices {
3737
d: &mut Decoder<'_>,
3838
ctx: &mut (),
3939
) -> Result<Self, minicbor::decode::Error> {
40-
// TODO: FIXME:
41-
todo!()
40+
let len = decode_array_len(d, "choices")?;
41+
if len < 2 {
42+
return Err(minicbor::decode::Error::message(format!(
43+
"Unexpected choices array length {len}, expected at least 2"
44+
)));
45+
}
46+
match u8::decode(d, ctx)? {
47+
0 => Ok(Self::Clear(<Vec<i64>>::decode(d, ctx)?)),
48+
1 => {
49+
let len = decode_array_len(d, "elgamal-ristretto255-encrypted-choices")?;
50+
if len < 1 || len > 2 {
51+
return Err(minicbor::decode::Error::message(format!(
52+
"Unexpected elgamal-ristretto255-encrypted-choices array length {len}, expected 1 or 2"
53+
)));
54+
}
55+
let choices = <Vec<ElgamalRistretto255Choice>>::decode(d, ctx)?;
56+
let mut row_proof = None;
57+
if len == 2 {
58+
row_proof = Some(RowProof::decode(d, ctx)?);
59+
}
60+
Ok(Self::ElgamalRistretto255 { choices, row_proof })
61+
},
62+
val => {
63+
Err(minicbor::decode::Error::message(format!(
64+
"Unexpected choices value: {val}"
65+
)))
66+
},
67+
}
4268
}
4369
}
4470

@@ -75,10 +101,10 @@ impl Decode<'_, ()> for ElgamalRistretto255Choice {
75101
d: &mut Decoder<'_>,
76102
ctx: &mut (),
77103
) -> Result<Self, minicbor::decode::Error> {
78-
let len = decode_array_len(d, "encrypted choices")?;
104+
let len = decode_array_len(d, "elgamal ristretto255 choice")?;
79105
if len != 2 {
80106
return Err(minicbor::decode::Error::message(format!(
81-
"Unexpected encrypted choices array length: {len}, expected 2"
107+
"Unexpected elgamal ristretto255 choice array length: {len}, expected 2"
82108
)));
83109
}
84110
let c1 = <[u8; 32]>::decode(d, ctx)?;

rust/catalyst-contest/src/contest_ballot.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::collections::BTreeMap;
44

5+
use cbork_utils::decode_helper::decode_map_len;
56
use minicbor::{Decode, Decoder, Encode, Encoder, encode::Write};
67

78
use crate::{Choices, ColumnProof, EncryptedChoices, MatrixProof};
@@ -24,8 +25,11 @@ impl Decode<'_, ()> for ContentBallot {
2425
d: &mut Decoder<'_>,
2526
ctx: &mut (),
2627
) -> Result<Self, minicbor::decode::Error> {
27-
d.map()?;
28+
let len = decode_map_len(d, "content ballot")?;
2829
// TODO: FIXME:
30+
for _ in 0..len {
31+
// TODO: FIXME:
32+
}
2933
todo!()
3034
}
3135
}
@@ -34,7 +38,7 @@ impl Encode<()> for ContentBallot {
3438
fn encode<W: Write>(
3539
&self,
3640
e: &mut Encoder<W>,
37-
ctx: &mut (),
41+
_ctx: &mut (),
3842
) -> Result<(), minicbor::encode::Error<W::Error>> {
3943
let len = self.choices.len() as u64
4044
+ self.column_proof.is_some() as u64

rust/catalyst-contest/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! [documentation]: https://docs.dev.projectcatalyst.io/libs/main/architecture/08_concepts/signed_doc/docs/contest_ballot/
66
77
// TODO: FIXME:
8-
#![allow(unused_variables)]
8+
//#![allow(unused_variables)]
99

1010
mod choices;
1111
mod column_proof;

0 commit comments

Comments
 (0)