Skip to content

Commit e97635b

Browse files
Move ElgamalRistretto255Choice to a module
1 parent 32837b1 commit e97635b

File tree

4 files changed

+82
-69
lines changed

4 files changed

+82
-69
lines changed

rust/catalyst-contest/src/choices.rs

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use cbork_utils::decode_helper::decode_array_len;
44
use minicbor::{Decode, Decoder, Encode, Encoder, encode::Write};
55

6+
use crate::elgamal_ristretto255_choice::ElgamalRistretto255Choice;
7+
68
/// Voters Choices.
79
///
810
/// The CDDL schema:
@@ -32,25 +34,6 @@ pub enum Choices {
3234
},
3335
}
3436

35-
/// An elgamal encrypted ciphertext `(c1, c2)`.
36-
///
37-
/// The CDDL schema:
38-
/// ```cddl
39-
/// elgamal-ristretto255-encrypted-choice = [
40-
/// c1: elgamal-ristretto255-group-element
41-
/// c2: elgamal-ristretto255-group-element
42-
/// ]
43-
///
44-
/// elgamal-ristretto255-group-element = bytes .size 32
45-
/// ```
46-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
47-
pub struct ElgamalRistretto255Choice {
48-
/// An individual Elgamal group element that composes the elgamal cipher text.
49-
pub c1: [u8; 32],
50-
/// An individual Elgamal group element that composes the elgamal cipher text.
51-
pub c2: [u8; 32],
52-
}
53-
5437
/// A universal encrypted row proof.
5538
///
5639
/// The CDDL schema:
@@ -141,35 +124,6 @@ impl Encode<()> for Choices {
141124
}
142125
}
143126

144-
impl Decode<'_, ()> for ElgamalRistretto255Choice {
145-
fn decode(
146-
d: &mut Decoder<'_>,
147-
ctx: &mut (),
148-
) -> Result<Self, minicbor::decode::Error> {
149-
let len = decode_array_len(d, "elgamal ristretto255 choice")?;
150-
if len != 2 {
151-
return Err(minicbor::decode::Error::message(format!(
152-
"Unexpected elgamal ristretto255 choice array length: {len}, expected 2"
153-
)));
154-
}
155-
let c1 = <[u8; 32]>::decode(d, ctx)?;
156-
let c2 = <[u8; 32]>::decode(d, ctx)?;
157-
Ok(ElgamalRistretto255Choice { c1, c2 })
158-
}
159-
}
160-
161-
impl Encode<()> for ElgamalRistretto255Choice {
162-
fn encode<W: Write>(
163-
&self,
164-
e: &mut Encoder<W>,
165-
ctx: &mut (),
166-
) -> Result<(), minicbor::encode::Error<W::Error>> {
167-
e.array(2)?;
168-
self.c1.encode(e, ctx)?;
169-
self.c2.encode(e, ctx)
170-
}
171-
}
172-
173127
impl Decode<'_, ()> for RowProof {
174128
fn decode(
175129
d: &mut Decoder<'_>,
@@ -220,25 +174,6 @@ mod tests {
220174
assert_eq!(original, decoded);
221175
}
222176

223-
#[test]
224-
fn elgamal_ristretto255_choice_roundtrip() {
225-
let bytes = [
226-
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
227-
25, 26, 27, 28, 29, 30, 31, 32,
228-
];
229-
let original = ElgamalRistretto255Choice {
230-
c1: bytes,
231-
c2: bytes,
232-
};
233-
let mut buffer = Vec::new();
234-
original
235-
.encode(&mut Encoder::new(&mut buffer), &mut ())
236-
.unwrap();
237-
let decoded =
238-
ElgamalRistretto255Choice::decode(&mut Decoder::new(&buffer), &mut ()).unwrap();
239-
assert_eq!(original, decoded);
240-
}
241-
242177
#[test]
243178
fn row_proof_roundtrip() {
244179
let original = RowProof {};

rust/catalyst-contest/src/contest_ballot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Encode<()> for ContentBallot {
7676
#[cfg(test)]
7777
mod tests {
7878
use super::*;
79-
use crate::{ElgamalRistretto255Choice, EncryptedBlock, RowProof};
79+
use crate::{EncryptedBlock, RowProof, elgamal_ristretto255_choice::ElgamalRistretto255Choice};
8080

8181
#[test]
8282
fn roundtrip() {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! An elgamal encrypted ciphertext.
2+
3+
use cbork_utils::decode_helper::decode_array_len;
4+
use minicbor::{Decode, Decoder, Encode, Encoder, encode::Write};
5+
6+
/// An elgamal encrypted ciphertext `(c1, c2)`.
7+
///
8+
/// The CDDL schema:
9+
/// ```cddl
10+
/// elgamal-ristretto255-encrypted-choice = [
11+
/// c1: elgamal-ristretto255-group-element
12+
/// c2: elgamal-ristretto255-group-element
13+
/// ]
14+
///
15+
/// elgamal-ristretto255-group-element = bytes .size 32
16+
/// ```
17+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
18+
pub struct ElgamalRistretto255Choice {
19+
/// An individual Elgamal group element that composes the elgamal cipher text.
20+
pub c1: [u8; 32],
21+
/// An individual Elgamal group element that composes the elgamal cipher text.
22+
pub c2: [u8; 32],
23+
}
24+
25+
impl Decode<'_, ()> for ElgamalRistretto255Choice {
26+
fn decode(
27+
d: &mut Decoder<'_>,
28+
ctx: &mut (),
29+
) -> Result<Self, minicbor::decode::Error> {
30+
let len = decode_array_len(d, "elgamal ristretto255 choice")?;
31+
if len != 2 {
32+
return Err(minicbor::decode::Error::message(format!(
33+
"Unexpected elgamal ristretto255 choice array length: {len}, expected 2"
34+
)));
35+
}
36+
let c1 = <[u8; 32]>::decode(d, ctx)?;
37+
let c2 = <[u8; 32]>::decode(d, ctx)?;
38+
Ok(ElgamalRistretto255Choice { c1, c2 })
39+
}
40+
}
41+
42+
impl Encode<()> for ElgamalRistretto255Choice {
43+
fn encode<W: Write>(
44+
&self,
45+
e: &mut Encoder<W>,
46+
ctx: &mut (),
47+
) -> Result<(), minicbor::encode::Error<W::Error>> {
48+
e.array(2)?;
49+
self.c1.encode(e, ctx)?;
50+
self.c2.encode(e, ctx)
51+
}
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn roundtrip() {
60+
let bytes = [
61+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
62+
25, 26, 27, 28, 29, 30, 31, 32,
63+
];
64+
let original = ElgamalRistretto255Choice {
65+
c1: bytes,
66+
c2: bytes,
67+
};
68+
let mut buffer = Vec::new();
69+
original
70+
.encode(&mut Encoder::new(&mut buffer), &mut ())
71+
.unwrap();
72+
let decoded =
73+
ElgamalRistretto255Choice::decode(&mut Decoder::new(&buffer), &mut ()).unwrap();
74+
assert_eq!(original, decoded);
75+
}
76+
}

rust/catalyst-contest/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
mod choices;
1111
mod column_proof;
1212
mod contest_ballot;
13+
mod elgamal_ristretto255_choice;
1314
mod encrypted_choices;
1415
mod matrix_proof;
1516

1617
pub use crate::{
17-
choices::{Choices, ElgamalRistretto255Choice, RowProof},
18+
choices::{Choices, RowProof},
1819
column_proof::ColumnProof,
1920
contest_ballot::ContentBallot,
21+
elgamal_ristretto255_choice::ElgamalRistretto255Choice,
2022
encrypted_choices::{EncryptedBlock, EncryptedChoices},
2123
matrix_proof::MatrixProof,
2224
};

0 commit comments

Comments
 (0)