Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bool's deserialization non-malleable #112

Merged
merged 4 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
### Bug fixes
- #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`.
- #107 (ark-serialize) Fix handling of `(de)serialize_uncompressed/unchecked` in various impls of `CanonicalSerialize/Deserialize`.
- #112 (ark-serialize) Make `bool`s checked serialization methods non-malleable.
- #119 (ark-poly) Fix bugs in degree calculation if adding/subtracting same degree polynomials
whose leading coefficients cancel.

Expand Down
3 changes: 3 additions & 0 deletions serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ edition = "2018"
ark-serialize-derive = { path = "../serialize-derive", optional = true }
ark-std = { git = "https://github.com/arkworks-rs/utils", default-features = false }

[dev-dependencies]
rand = { version = "0.7", default-features = false }

[features]
default = []
std = [ "ark-std/std" ]
Expand Down
57 changes: 57 additions & 0 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,18 @@ impl CanonicalSerialize for bool {
impl CanonicalDeserialize for bool {
#[inline]
fn deserialize<R: Read>(reader: R) -> Result<Self, SerializationError> {
let val = u8::deserialize(reader)?;
if val == 0 {
return Ok(false);
} else if val == 1 {
return Ok(true);
}

Err(SerializationError::InvalidData)
}

#[inline]
fn deserialize_unchecked<R: Read>(reader: R) -> Result<Self, SerializationError> {
Ok(u8::deserialize(reader)? == 1)
}
}
Expand Down Expand Up @@ -752,6 +764,7 @@ impl<T: CanonicalDeserialize + Ord> CanonicalDeserialize for BTreeSet<T> {
mod test {
use super::*;
use ark_std::vec;
use rand::{RngCore, SeedableRng};

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
struct Dummy;
Expand Down Expand Up @@ -832,6 +845,42 @@ mod test {
assert_eq!(data, de);
}

// Serialize T, randomly mutate the data, and deserialize it.
// Ensure it fails.
// Up to the caller to provide a valid mutation criterion
// to ensure that this test always fails.
// This method requires a concrete instance of the data to be provided,
// to get the serialized size.
fn ensure_non_malleable_encoding<
T: PartialEq + core::fmt::Debug + CanonicalSerialize + CanonicalDeserialize,
>(
data: T,
valid_mutation: fn(&[u8]) -> bool,
) {
// Seed copied from StdRng tests,
// https://rust-random.github.io/rand/src/rand/rngs/std.rs.html#87
let seed = [
1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
];
let mut r = rand::rngs::StdRng::from_seed(seed);
let mut serialized = vec![0; data.serialized_size()];
r.fill_bytes(&mut serialized);
while !valid_mutation(&serialized) {
r.fill_bytes(&mut serialized);
}
let de = T::deserialize(&serialized[..]);
assert!(de.is_err());

let mut serialized = vec![0; data.uncompressed_size()];
r.fill_bytes(&mut serialized);
while !valid_mutation(&serialized) {
r.fill_bytes(&mut serialized);
}
let de = T::deserialize_uncompressed(&serialized[..]);
assert!(de.is_err());
}

#[test]
fn test_vec() {
test_serialize(vec![1u64, 2, 3, 4, 5]);
Expand Down Expand Up @@ -879,6 +928,14 @@ mod test {
fn test_bool() {
test_serialize(true);
test_serialize(false);

let valid_mutation = |data: &[u8]| -> bool {
return data.len() == 1 && data[0] > 1;
};
for _ in 0..10 {
ensure_non_malleable_encoding(true, valid_mutation);
ensure_non_malleable_encoding(false, valid_mutation);
}
}

#[test]
Expand Down