Skip to content

Commit 7ab36ba

Browse files
committed
wip
1 parent 7c670a0 commit 7ab36ba

File tree

5 files changed

+82
-60
lines changed

5 files changed

+82
-60
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::Read;
55
use anyhow::anyhow;
66

77
use super::{Announcement, Ciphertext, GroupElement, ResponseRandomness, Scalar, UnitVectorProof};
8+
use crate::utils::read_array;
89

910
impl UnitVectorProof {
1011
/// Get an underlying vector length.
@@ -22,36 +23,31 @@ impl UnitVectorProof {
2223
/// - Cannot decode response randomness value.
2324
/// - Cannot decode scalar value.
2425
pub fn from_bytes<R: Read>(reader: &mut R, len: usize) -> anyhow::Result<Self> {
25-
let mut ann_buf = [0u8; Announcement::BYTES_SIZE];
26-
let mut dl_buf = [0u8; Ciphertext::BYTES_SIZE];
27-
let mut rr_buf = [0u8; ResponseRandomness::BYTES_SIZE];
28-
2926
let ann = (0..len)
3027
.map(|i| {
31-
reader.read_exact(&mut ann_buf)?;
32-
Announcement::from_bytes(&ann_buf)
28+
let bytes = read_array(reader)?;
29+
Announcement::from_bytes(&bytes)
3330
.map_err(|e| anyhow!("Cannot decode announcement at {i}, error: {e}."))
3431
})
3532
.collect::<anyhow::Result<_>>()?;
3633
let dl = (0..len)
3734
.map(|i| {
38-
reader.read_exact(&mut dl_buf)?;
39-
Ciphertext::from_bytes(&dl_buf)
35+
let bytes = read_array(reader)?;
36+
Ciphertext::from_bytes(&bytes)
4037
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}."))
4138
})
4239
.collect::<anyhow::Result<_>>()?;
4340
let rr = (0..len)
4441
.map(|i| {
45-
reader.read_exact(&mut rr_buf)?;
46-
ResponseRandomness::from_bytes(&rr_buf)
42+
let bytes = read_array(reader)?;
43+
ResponseRandomness::from_bytes(&bytes)
4744
.map_err(|e| anyhow!("Cannot decode response randomness at {i}, error: {e}."))
4845
})
4946
.collect::<anyhow::Result<_>>()?;
5047

51-
let mut scalar_buf = [0u8; Scalar::BYTES_SIZE];
52-
reader.read_exact(&mut scalar_buf)?;
48+
let bytes = read_array(reader)?;
5349
let scalar =
54-
Scalar::from_bytes(scalar_buf).map_err(|_| anyhow!("Cannot decode scalar field."))?;
50+
Scalar::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode scalar field."))?;
5551
Ok(Self(ann, dl, rr, scalar))
5652
}
5753

rust/catalyst-voting/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
mod crypto;
44
pub mod txs;
5+
mod utils;
56
pub mod vote_protocol;
67

78
pub use crypto::elgamal::{PublicKey, SecretKey};

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

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::Read;
55
use anyhow::{anyhow, bail, ensure};
66

77
use super::{EncryptedVote, PublicKey, Tx, Vote, VoterProof};
8+
use crate::utils::{read_array, read_be_u32, read_be_u64, read_be_u8};
89

910
impl Tx {
1011
/// Convert this `Tx` to its underlying sequence of bytes.
@@ -64,47 +65,38 @@ impl Tx {
6465
/// - Invalid public key.
6566
#[allow(clippy::indexing_slicing)]
6667
pub fn from_bytes<R: Read>(reader: &mut R) -> anyhow::Result<Self> {
67-
let mut u8_buf = [0u8; 1];
68-
let mut u32_buf = [0u8; 4];
69-
let mut u64_buf = [0u8; 8];
70-
let mut u256_buf = [0u8; 32];
71-
7268
// Skip tx size field
73-
reader.read_exact(&mut u32_buf)?;
69+
read_be_u32(reader)?;
7470

75-
reader.read_exact(&mut u8_buf)?;
71+
let padding_tag = read_be_u8(reader)?;
7672
ensure!(
77-
u8_buf[0] == 0,
78-
"Invalid padding tag field value, must be equals to `0`, provided: {0}.",
79-
u8_buf[0]
73+
padding_tag == 0,
74+
"Invalid padding tag field value, must be equals to `0`, provided: {padding_tag}.",
8075
);
8176

82-
reader.read_exact(&mut u8_buf)?;
77+
let fragment_tag = read_be_u8(reader)?;
8378
ensure!(
84-
u8_buf[0] == 11,
85-
"Invalid fragment tag field value, must be equals to `11`, provided: {0}.",
86-
u8_buf[0]
79+
fragment_tag == 11,
80+
"Invalid fragment tag field value, must be equals to `11`, provided: {fragment_tag}.",
8781
);
8882

89-
reader.read_exact(&mut u256_buf)?;
90-
let vote_plan_id = u256_buf;
83+
let vote_plan_id = read_array(reader)?;
9184

92-
reader.read_exact(&mut u8_buf)?;
93-
let proposal_index = u8_buf[0];
85+
let proposal_index = read_be_u8(reader)?;
9486

95-
reader.read_exact(&mut u8_buf)?;
96-
let vote = match u8_buf[0] {
87+
let vote_tag = read_be_u8(reader)?;
88+
let vote = match vote_tag {
9789
1 => {
98-
reader.read_exact(&mut u8_buf)?;
99-
Vote::Public(u8_buf[0])
90+
let vote = read_be_u8(reader)?;
91+
Vote::Public(vote)
10092
},
10193
2 => {
102-
reader.read_exact(&mut u8_buf)?;
103-
let vote = EncryptedVote::from_bytes(reader, u8_buf[0].into())
94+
let size = read_be_u8(reader)?;
95+
let vote = EncryptedVote::from_bytes(reader, size.into())
10496
.map_err(|e| anyhow!("Invalid encrypted vote, error: {e}."))?;
10597

106-
reader.read_exact(&mut u8_buf)?;
107-
let proof = VoterProof::from_bytes(reader, u8_buf[0].into())
98+
let size = read_be_u8(reader)?;
99+
let proof = VoterProof::from_bytes(reader, size.into())
108100
.map_err(|e| anyhow!("Invalid voter proof, error: {e}."))?;
109101

110102
Vote::Private(vote, proof)
@@ -113,34 +105,31 @@ impl Tx {
113105
};
114106

115107
// skip block date (epoch and slot)
116-
reader.read_exact(&mut u64_buf)?;
108+
read_be_u64(reader)?;
117109

118-
reader.read_exact(&mut u8_buf)?;
110+
let inputs_amount = read_be_u8(reader)?;
119111
ensure!(
120-
u8_buf[0] == 1,
121-
"Invalid number of inputs, expected: `1`, provided: {0}",
122-
u8_buf[0]
112+
inputs_amount == 1,
113+
"Invalid number of inputs, expected: `1`, provided: {inputs_amount}",
123114
);
124115

125-
reader.read_exact(&mut u8_buf)?;
116+
let outputs_amount = read_be_u8(reader)?;
126117
ensure!(
127-
u8_buf[0] == 0,
128-
"Invalid number of outputs, expected: `0`, provided: {0}",
129-
u8_buf[0]
118+
outputs_amount == 0,
119+
"Invalid number of outputs, expected: `0`, provided: {outputs_amount}",
130120
);
131121

132-
reader.read_exact(&mut u8_buf)?;
122+
let input_tag = read_be_u8(reader)?;
133123
ensure!(
134-
u8_buf[0] == 0xFF,
135-
"Invalid input tag, expected: `255`, provided: {0}",
136-
u8_buf[0]
124+
input_tag == 0xFF,
125+
"Invalid input tag, expected: `255`, provided: {input_tag}",
137126
);
138127

139128
// skip value
140-
reader.read_exact(&mut u64_buf)?;
129+
read_be_u64(reader)?;
141130

142-
reader.read_exact(&mut u256_buf)?;
143-
let public_key = PublicKey::from_bytes(&u256_buf)
131+
let public_key_bytes = read_array(reader)?;
132+
let public_key = PublicKey::from_bytes(&public_key_bytes)
144133
.map_err(|e| anyhow!("Invalid public key, error: {e}."))?;
145134

146135
Ok(Self {

rust/catalyst-voting/src/utils.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Utility functions.
2+
3+
use std::io::Read;
4+
5+
/// Read a single byte from the reader.
6+
#[inline]
7+
pub(crate) fn read_be_u8<R: Read>(reader: &mut R) -> anyhow::Result<u8> {
8+
let mut buf = [0u8; 1];
9+
reader.read_exact(&mut buf)?;
10+
Ok(u8::from_be_bytes(buf))
11+
}
12+
13+
/// Read a big-endian u32 from the reader.
14+
#[inline]
15+
pub(crate) fn read_be_u32<R: Read>(reader: &mut R) -> anyhow::Result<u32> {
16+
let mut buf = [0u8; 4];
17+
reader.read_exact(&mut buf)?;
18+
Ok(u32::from_be_bytes(buf))
19+
}
20+
21+
/// Read a big-endian u64 from the reader.
22+
#[inline]
23+
pub(crate) fn read_be_u64<R: Read>(reader: &mut R) -> anyhow::Result<u64> {
24+
let mut buf = [0u8; 8];
25+
reader.read_exact(&mut buf)?;
26+
Ok(u64::from_be_bytes(buf))
27+
}
28+
29+
/// Read a N-byte array from the reader.
30+
#[inline]
31+
pub(crate) fn read_array<R: Read, const N: usize>(reader: &mut R) -> anyhow::Result<[u8; N]> {
32+
let mut buf = [0u8; N];
33+
reader.read_exact(&mut buf)?;
34+
Ok(buf)
35+
}

rust/catalyst-voting/src/vote_protocol/voter/decoding.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::io::Read;
55
use anyhow::anyhow;
66

77
use super::{proof::VoterProof, EncryptedVote};
8-
use crate::crypto::{elgamal::Ciphertext, zk_unit_vector::UnitVectorProof};
8+
use crate::{
9+
crypto::{elgamal::Ciphertext, zk_unit_vector::UnitVectorProof},
10+
utils::read_array,
11+
};
912

1013
impl EncryptedVote {
1114
/// Get an underlying vector length.
@@ -18,12 +21,10 @@ impl EncryptedVote {
1821
/// # Errors
1922
/// - Cannot decode ciphertext.
2023
pub fn from_bytes<R: Read>(reader: &mut R, size: usize) -> anyhow::Result<Self> {
21-
let mut ciph_buf = [0u8; Ciphertext::BYTES_SIZE];
22-
2324
let ciphertexts = (0..size)
2425
.map(|i| {
25-
reader.read_exact(&mut ciph_buf)?;
26-
Ciphertext::from_bytes(&ciph_buf)
26+
let bytes = read_array(reader)?;
27+
Ciphertext::from_bytes(&bytes)
2728
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}"))
2829
})
2930
.collect::<anyhow::Result<_>>()?;

0 commit comments

Comments
 (0)