Skip to content

Commit adfccba

Browse files
committed
fix
1 parent f40f1a8 commit adfccba

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ impl UnitVectorProof {
5858
/// Get a deserialized bytes size
5959
#[must_use]
6060
pub fn bytes_size(&self) -> usize {
61-
self.0.len() * Announcement::BYTES_SIZE
61+
Scalar::BYTES_SIZE
62+
+ self.0.len() * Announcement::BYTES_SIZE
6263
+ self.0.len() * Ciphertext::BYTES_SIZE
6364
+ self.0.len() * ResponseRandomness::BYTES_SIZE
6465
}
@@ -152,6 +153,7 @@ mod tests {
152153
#[strategy(0..5usize)] _size: usize, #[any(#_size)] p1: UnitVectorProof,
153154
) {
154155
let bytes = p1.to_bytes();
156+
assert_eq!(bytes.len(), p1.bytes_size());
155157
let p2 = UnitVectorProof::from_bytes(&bytes, p1.size()).unwrap();
156158
assert_eq!(p1, p2);
157159
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ impl Tx {
7070

7171
// Read tx size
7272
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+
);
7379

7480
bytes.read_exact(&mut u8_buf)?;
7581
ensure!(
@@ -106,7 +112,7 @@ impl Tx {
106112
bytes.read_exact(&mut u8_buf)?;
107113
let proof = VoterProof::from_bytes(bytes, u8_buf[0].into())
108114
.map_err(|e| anyhow!("Invalid voter proof, error: {e}."))?;
109-
bytes = &bytes[vote.bytes_size()..];
115+
bytes = &bytes[proof.bytes_size()..];
110116

111117
Vote::Private(vote, proof)
112118
},
@@ -155,7 +161,7 @@ impl Tx {
155161

156162
#[cfg(test)]
157163
mod tests {
158-
use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, ProptestConfig, Strategy};
164+
use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, Strategy};
159165
use test_strategy::proptest;
160166

161167
use super::*;
@@ -201,13 +207,10 @@ mod tests {
201207
}
202208
}
203209

204-
#[proptest(ProptestConfig::with_cases(1))]
210+
#[proptest]
205211
#[allow(clippy::indexing_slicing)]
206212
fn tx_to_bytes_from_bytes_test(t1: Tx) {
207213
let bytes = t1.to_bytes();
208-
// verify correctness serializing tx size field
209-
let size = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
210-
assert_eq!(size as usize, bytes.len() - 4);
211214
let t2 = Tx::from_bytes(&bytes).unwrap();
212215
assert_eq!(t1, t2);
213216
}

0 commit comments

Comments
 (0)