Skip to content

Commit 1d7afdf

Browse files
committed
refactor
1 parent adfccba commit 1d7afdf

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ impl UnitVectorProof {
2121
/// - Cannot decode ciphertext value.
2222
/// - Cannot decode response randomness value.
2323
/// - Cannot decode scalar value.
24-
pub fn from_bytes(mut bytes: &[u8], len: usize) -> anyhow::Result<Self> {
24+
pub fn from_bytes<R: Read>(reader: &mut R, len: usize) -> anyhow::Result<Self> {
2525
let mut ann_buf = [0u8; Announcement::BYTES_SIZE];
2626
let mut dl_buf = [0u8; Ciphertext::BYTES_SIZE];
2727
let mut rr_buf = [0u8; ResponseRandomness::BYTES_SIZE];
2828

2929
let ann = (0..len)
3030
.map(|i| {
31-
bytes.read_exact(&mut ann_buf)?;
31+
reader.read_exact(&mut ann_buf)?;
3232
Announcement::from_bytes(&ann_buf)
3333
.map_err(|e| anyhow!("Cannot decode announcement at {i}, error: {e}."))
3434
})
3535
.collect::<anyhow::Result<_>>()?;
3636
let dl = (0..len)
3737
.map(|i| {
38-
bytes.read_exact(&mut dl_buf)?;
38+
reader.read_exact(&mut dl_buf)?;
3939
Ciphertext::from_bytes(&dl_buf)
4040
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}."))
4141
})
4242
.collect::<anyhow::Result<_>>()?;
4343
let rr = (0..len)
4444
.map(|i| {
45-
bytes.read_exact(&mut rr_buf)?;
45+
reader.read_exact(&mut rr_buf)?;
4646
ResponseRandomness::from_bytes(&rr_buf)
4747
.map_err(|e| anyhow!("Cannot decode response randomness at {i}, error: {e}."))
4848
})
4949
.collect::<anyhow::Result<_>>()?;
5050

5151
let mut scalar_buf = [0u8; Scalar::BYTES_SIZE];
52-
bytes.read_exact(&mut scalar_buf)?;
52+
reader.read_exact(&mut scalar_buf)?;
5353
let scalar =
5454
Scalar::from_bytes(scalar_buf).map_err(|_| anyhow!("Cannot decode scalar field."))?;
5555
Ok(Self(ann, dl, rr, scalar))
5656
}
5757

5858
/// Get a deserialized bytes size
5959
#[must_use]
60-
pub fn bytes_size(&self) -> usize {
60+
fn bytes_size(&self) -> usize {
6161
Scalar::BYTES_SIZE
6262
+ self.0.len() * Announcement::BYTES_SIZE
6363
+ self.0.len() * Ciphertext::BYTES_SIZE
@@ -144,6 +144,8 @@ impl ResponseRandomness {
144144

145145
#[cfg(test)]
146146
mod tests {
147+
use std::io::Cursor;
148+
147149
use test_strategy::proptest;
148150

149151
use super::*;
@@ -154,7 +156,7 @@ mod tests {
154156
) {
155157
let bytes = p1.to_bytes();
156158
assert_eq!(bytes.len(), p1.bytes_size());
157-
let p2 = UnitVectorProof::from_bytes(&bytes, p1.size()).unwrap();
159+
let p2 = UnitVectorProof::from_bytes(&mut Cursor::new(bytes), p1.size()).unwrap();
158160
assert_eq!(p1, p2);
159161
}
160162

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl Tx {
4444
tx_body.push(0xFF);
4545
// Zero value
4646
tx_body.extend_from_slice(&[0u8; 8]);
47+
4748
tx_body.extend_from_slice(&self.public_key.to_bytes());
4849

4950
// Add the size of decoded bytes to the beginning.
@@ -62,91 +63,83 @@ impl Tx {
6263
/// - Invalid vote tag value.
6364
/// - Invalid public key.
6465
#[allow(clippy::indexing_slicing)]
65-
pub fn from_bytes(mut bytes: &[u8]) -> anyhow::Result<Self> {
66+
pub fn from_bytes<R: Read>(reader: &mut R) -> anyhow::Result<Self> {
6667
let mut u8_buf = [0u8; 1];
6768
let mut u32_buf = [0u8; 4];
6869
let mut u64_buf = [0u8; 8];
6970
let mut u256_buf = [0u8; 32];
7071

71-
// Read tx size
72-
bytes.read_exact(&mut u32_buf)?;
73-
let tx_size = u32::from_be_bytes(u32_buf);
74-
ensure!(
75-
tx_size as usize == bytes.len(),
76-
"Invalid tx size, expected: {tx_size}, provided: {0}.",
77-
bytes.len()
78-
);
72+
// Skip tx size field
73+
reader.read_exact(&mut u32_buf)?;
7974

80-
bytes.read_exact(&mut u8_buf)?;
75+
reader.read_exact(&mut u8_buf)?;
8176
ensure!(
8277
u8_buf[0] == 0,
8378
"Invalid padding tag field value, must be equals to `0`, provided: {0}.",
8479
u8_buf[0]
8580
);
8681

87-
bytes.read_exact(&mut u8_buf)?;
82+
reader.read_exact(&mut u8_buf)?;
8883
ensure!(
8984
u8_buf[0] == 11,
9085
"Invalid fragment tag field value, must be equals to `11`, provided: {0}.",
9186
u8_buf[0]
9287
);
9388

94-
bytes.read_exact(&mut u256_buf)?;
89+
reader.read_exact(&mut u256_buf)?;
9590
let vote_plan_id = u256_buf;
9691

97-
bytes.read_exact(&mut u8_buf)?;
92+
reader.read_exact(&mut u8_buf)?;
9893
let proposal_index = u8_buf[0];
9994

100-
bytes.read_exact(&mut u8_buf)?;
95+
reader.read_exact(&mut u8_buf)?;
10196
let vote = match u8_buf[0] {
10297
1 => {
103-
bytes.read_exact(&mut u8_buf)?;
98+
reader.read_exact(&mut u8_buf)?;
10499
Vote::Public(u8_buf[0])
105100
},
106101
2 => {
107-
bytes.read_exact(&mut u8_buf)?;
108-
let vote = EncryptedVote::from_bytes(bytes, u8_buf[0].into())
102+
reader.read_exact(&mut u8_buf)?;
103+
let vote = EncryptedVote::from_bytes(reader, u8_buf[0].into())
109104
.map_err(|e| anyhow!("Invalid encrypted vote, error: {e}."))?;
110-
bytes = &bytes[vote.bytes_size()..];
111105

112-
bytes.read_exact(&mut u8_buf)?;
113-
let proof = VoterProof::from_bytes(bytes, u8_buf[0].into())
106+
reader.read_exact(&mut u8_buf)?;
107+
let proof = VoterProof::from_bytes(reader, u8_buf[0].into())
114108
.map_err(|e| anyhow!("Invalid voter proof, error: {e}."))?;
115-
bytes = &bytes[proof.bytes_size()..];
116109

117110
Vote::Private(vote, proof)
118111
},
119112
tag => bail!("Invalid vote tag value, must be equals to `0` or `1`, provided: {tag}"),
120113
};
121114

122115
// skip block date (epoch and slot)
123-
bytes.read_exact(&mut u64_buf)?;
116+
reader.read_exact(&mut u64_buf)?;
124117

125-
bytes.read_exact(&mut u8_buf)?;
118+
reader.read_exact(&mut u8_buf)?;
126119
ensure!(
127120
u8_buf[0] == 1,
128121
"Invalid number of inputs, expected: `1`, provided: {0}",
129122
u8_buf[0]
130123
);
131124

132-
bytes.read_exact(&mut u8_buf)?;
125+
reader.read_exact(&mut u8_buf)?;
133126
ensure!(
134127
u8_buf[0] == 0,
135128
"Invalid number of outputs, expected: `0`, provided: {0}",
136129
u8_buf[0]
137130
);
138131

139-
bytes.read_exact(&mut u8_buf)?;
132+
reader.read_exact(&mut u8_buf)?;
140133
ensure!(
141134
u8_buf[0] == 0xFF,
142135
"Invalid input tag, expected: `255`, provided: {0}",
143136
u8_buf[0]
144137
);
145138

146139
// skip value
147-
bytes.read_exact(&mut u64_buf)?;
140+
reader.read_exact(&mut u64_buf)?;
148141

149-
bytes.read_exact(&mut u256_buf)?;
142+
reader.read_exact(&mut u256_buf)?;
150143
let public_key = PublicKey::from_bytes(&u256_buf)
151144
.map_err(|e| anyhow!("Invalid public key, error: {e}."))?;
152145

@@ -161,6 +154,8 @@ impl Tx {
161154

162155
#[cfg(test)]
163156
mod tests {
157+
use std::io::Cursor;
158+
164159
use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, Strategy};
165160
use test_strategy::proptest;
166161

@@ -211,7 +206,12 @@ mod tests {
211206
#[allow(clippy::indexing_slicing)]
212207
fn tx_to_bytes_from_bytes_test(t1: Tx) {
213208
let bytes = t1.to_bytes();
214-
let t2 = Tx::from_bytes(&bytes).unwrap();
209+
210+
// verify correctness serializing tx size field
211+
let size = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
212+
assert_eq!(size as usize, bytes.len() - 4);
213+
214+
let t2 = Tx::from_bytes(&mut Cursor::new(bytes)).unwrap();
215215
assert_eq!(t1, t2);
216216
}
217217
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ impl EncryptedVote {
1717
///
1818
/// # Errors
1919
/// - Cannot decode ciphertext.
20-
pub fn from_bytes(mut bytes: &[u8], size: usize) -> anyhow::Result<Self> {
20+
pub fn from_bytes<R: Read>(reader: &mut R, size: usize) -> anyhow::Result<Self> {
2121
let mut ciph_buf = [0u8; Ciphertext::BYTES_SIZE];
2222

2323
let ciphertexts = (0..size)
2424
.map(|i| {
25-
bytes.read_exact(&mut ciph_buf)?;
25+
reader.read_exact(&mut ciph_buf)?;
2626
Ciphertext::from_bytes(&ciph_buf)
2727
.map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}"))
2828
})
@@ -64,14 +64,8 @@ impl VoterProof {
6464
/// - Cannot decode ciphertext value.
6565
/// - Cannot decode response randomness value.
6666
/// - Cannot decode scalar value.
67-
pub fn from_bytes(bytes: &[u8], len: usize) -> anyhow::Result<Self> {
68-
UnitVectorProof::from_bytes(bytes, len).map(Self)
69-
}
70-
71-
/// Get a deserialized bytes size
72-
#[must_use]
73-
pub fn bytes_size(&self) -> usize {
74-
self.0.bytes_size()
67+
pub fn from_bytes<R: Read>(reader: &mut R, len: usize) -> anyhow::Result<Self> {
68+
UnitVectorProof::from_bytes(reader, len).map(Self)
7569
}
7670

7771
/// Encode `EncryptedVote` tos bytes.
@@ -83,6 +77,8 @@ impl VoterProof {
8377

8478
#[cfg(test)]
8579
mod tests {
80+
use std::io::Cursor;
81+
8682
use test_strategy::proptest;
8783

8884
use super::*;
@@ -93,7 +89,7 @@ mod tests {
9389
) {
9490
let bytes = vote1.to_bytes();
9591
assert_eq!(bytes.len(), vote1.bytes_size());
96-
let vote2 = EncryptedVote::from_bytes(&bytes, vote1.size()).unwrap();
92+
let vote2 = EncryptedVote::from_bytes(&mut Cursor::new(bytes), vote1.size()).unwrap();
9793
assert_eq!(vote1, vote2);
9894
}
9995
}

0 commit comments

Comments
 (0)