Skip to content

Commit 43b506e

Browse files
committed
wip
1 parent 8f652bc commit 43b506e

File tree

9 files changed

+302
-158
lines changed

9 files changed

+302
-158
lines changed

rust/catalyst-voting/src/crypto/elgamal/decoding.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,45 @@
22
33
use anyhow::anyhow;
44

5-
use super::{Ciphertext, GroupElement};
5+
use super::{Ciphertext, GroupElement, PublicKey, Scalar, SecretKey};
6+
7+
impl PublicKey {
8+
/// `PublicKey` bytes size
9+
pub const BYTES_SIZE: usize = GroupElement::BYTES_SIZE;
10+
11+
/// Convert this `PublicKey` to its underlying sequence of bytes.
12+
#[must_use]
13+
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] {
14+
self.0.to_bytes()
15+
}
16+
17+
/// Attempt to construct a `PublicKey` from a byte representation.
18+
///
19+
/// # Errors
20+
/// - Cannot decode group element field.
21+
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
22+
GroupElement::from_bytes(bytes).map(Self)
23+
}
24+
}
25+
26+
impl SecretKey {
27+
/// `SecretKey` bytes size
28+
pub const BYTES_SIZE: usize = Scalar::BYTES_SIZE;
29+
30+
/// Convert this `SecretKey` to its underlying sequence of bytes.
31+
#[must_use]
32+
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] {
33+
self.0.to_bytes()
34+
}
35+
36+
/// Attempt to construct a `SecretKey` from a byte representation.
37+
///
38+
/// # Errors
39+
/// - Cannot decode scalar field.
40+
pub fn from_bytes(bytes: [u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
41+
Scalar::from_bytes(bytes).map(Self)
42+
}
43+
}
644

745
impl Ciphertext {
846
/// `Ciphertext` bytes size
@@ -16,7 +54,7 @@ impl Ciphertext {
1654
res
1755
}
1856

19-
/// Attempt to construct a `Scalar` from a compressed value byte representation.
57+
/// Attempt to construct a `Ciphertext` from a byte representation.
2058
///
2159
/// # Errors
2260
/// - Cannot decode group element field.
@@ -37,6 +75,18 @@ mod tests {
3775

3876
use super::*;
3977

78+
#[proptest]
79+
fn keys_to_bytes_from_bytes_test(s1: SecretKey) {
80+
let bytes = s1.to_bytes();
81+
let s2 = SecretKey::from_bytes(bytes).unwrap();
82+
assert_eq!(s1, s2);
83+
84+
let p1 = s1.public_key();
85+
let bytes = p1.to_bytes();
86+
let p2 = PublicKey::from_bytes(&bytes).unwrap();
87+
assert_eq!(p1, p2);
88+
}
89+
4090
#[proptest]
4191
fn ciphertext_to_bytes_from_bytes_test(c1: Ciphertext) {
4292
let bytes = c1.to_bytes();

rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,40 @@ use anyhow::anyhow;
77
use super::{Announcement, Ciphertext, GroupElement, ResponseRandomness, Scalar, UnitVectorProof};
88

99
impl UnitVectorProof {
10+
/// Get an underlying vector length.
11+
///
12+
/// **Note** each vector field has the same length.
13+
pub fn size(&self) -> usize {
14+
self.0.len()
15+
}
16+
1017
/// Decode `UnitVectorProof` from bytes.
1118
///
1219
/// # Errors
1320
/// - Cannot decode announcement value.
1421
/// - Cannot decode ciphertext value.
1522
/// - Cannot decode response randomness value.
1623
/// - Cannot decode scalar value.
17-
pub fn from_bytes(mut bytes: &[u8], size: usize) -> anyhow::Result<Self> {
24+
pub fn from_bytes(mut bytes: &[u8], len: usize) -> anyhow::Result<Self> {
1825
let mut ann_buf = [0u8; Announcement::BYTES_SIZE];
1926
let mut dl_buf = [0u8; Ciphertext::BYTES_SIZE];
2027
let mut rr_buf = [0u8; ResponseRandomness::BYTES_SIZE];
2128

22-
let ann = (0..size)
29+
let ann = (0..len)
2330
.map(|i| {
2431
bytes.read_exact(&mut ann_buf)?;
2532
Announcement::from_bytes(&ann_buf)
2633
.map_err(|e| anyhow!("Cannot decode announcement at {i}, error: {e}."))
2734
})
2835
.collect::<anyhow::Result<_>>()?;
29-
let dl = (0..size)
36+
let dl = (0..len)
3037
.map(|i| {
3138
bytes.read_exact(&mut dl_buf)?;
3239
Ciphertext::from_bytes(&dl_buf)
3340
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}."))
3441
})
3542
.collect::<anyhow::Result<_>>()?;
36-
let rr = (0..size)
43+
let rr = (0..len)
3744
.map(|i| {
3845
bytes.read_exact(&mut rr_buf)?;
3946
ResponseRandomness::from_bytes(&rr_buf)
@@ -141,9 +148,11 @@ mod tests {
141148
use super::*;
142149

143150
#[proptest]
144-
fn proof_to_bytes_from_bytes_test(p1: UnitVectorProof) {
151+
fn proof_to_bytes_from_bytes_test(
152+
#[strategy(0..20usize)] _size: usize, #[any(#_size)] p1: UnitVectorProof,
153+
) {
145154
let bytes = p1.to_bytes();
146-
let p2 = UnitVectorProof::from_bytes(&bytes, p1.0.len()).unwrap();
155+
let p2 = UnitVectorProof::from_bytes(&bytes, p1.size()).unwrap();
147156
assert_eq!(p1, p2);
148157
}
149158

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
//! A catalyst transaction objects implementation
22
3-
mod utils;
4-
// pub mod v1;
3+
pub mod v1;

rust/catalyst-voting/src/txs/utils.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

rust/catalyst-voting/src/txs/v1.rs

Lines changed: 0 additions & 139 deletions
This file was deleted.

0 commit comments

Comments
 (0)