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

Fix Array Deserialization Panic #837

Merged
merged 8 commits into from
Jun 19, 2024
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 @@ -4,6 +4,7 @@

- [\#772](https://github.com/arkworks-rs/algebra/pull/772) (`ark-ff`) Implementation of `mul` method for `BigInteger`.
- [\#794](https://github.com/arkworks-rs/algebra/pull/794) (`ark-ff`) Fix `wasm` compilation.
- [\#837](https://github.com/arkworks-rs/algebra/pull/837) (`ark-serialize`) Fix array deserialization panic.

### Breaking changes

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ num-traits = { version = "0.2", default-features = false }
num-bigint = { version = "0.4", default-features = false }
num-integer = { version = "0.1", default-features = false }

arrayvec = { version = "0.7", default-features = false }
criterion = "0.5.0"
educe = "0.5.0"
digest = { version = "0.10", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ark-ff-asm.workspace = true
ark-ff-macros.workspace = true
ark-std.workspace = true
ark-serialize.workspace = true
arrayvec = { version = "0.7", default-features = false }
arrayvec.workspace = true
educe.workspace = true
num-traits.workspace = true
paste.workspace = true
Expand Down
1 change: 1 addition & 0 deletions serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ keywords = ["cryptography", "serialization" ]
[dependencies]
ark-serialize-derive = { workspace = true, optional = true }
ark-std.workspace = true
arrayvec.workspace = true
digest.workspace = true
num-bigint.workspace = true
rayon = { workspace = true, optional = true }
Expand Down
16 changes: 11 additions & 5 deletions serialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ark_std::{
string::*,
vec::*,
};
use arrayvec::ArrayVec;
use num_bigint::BigUint;

impl Valid for bool {
Expand Down Expand Up @@ -458,13 +459,18 @@ impl<T: CanonicalDeserialize, const N: usize> CanonicalDeserialize for [T; N] {
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
let result = core::array::from_fn(|_| {
T::deserialize_with_mode(&mut reader, compress, Validate::No).unwrap()
});
let mut array = ArrayVec::<T, N>::new();
for _ in 0..N {
array.push(T::deserialize_with_mode(
&mut reader,
compress,
Validate::No,
)?);
}
if let Validate::Yes = validate {
T::batch_check(result.iter())?
T::batch_check(array.iter())?
}
Ok(result)
Ok(array.into_inner().ok().unwrap())
}
}

Expand Down
7 changes: 7 additions & 0 deletions serialize/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ fn test_array() {
test_serialize([1u8; 33]);
}

#[test]
fn test_array_bad_input() {
// Does not panic on invalid data:
let serialized = vec![0u8; 1];
assert!(<[u8; 2]>::deserialize_compressed(&serialized[..]).is_err());
}

#[test]
fn test_vec() {
test_serialize(vec![1u64, 2, 3, 4, 5]);
Expand Down
Loading