Skip to content

Commit

Permalink
Fix a bug in Arbitrary implementation for boolean arrays
Browse files Browse the repository at this point in the history
Going from arbitrary [u8; X] to BAY is only possible when X mod 8 == 0. Previous implementation didn't check that and allowed constructing BA3 with values greater than 8.
  • Loading branch information
akoshelev committed Oct 30, 2024
1 parent ebd4185 commit 46bc58a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ipa-core/src/ff/boolean_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,12 @@ macro_rules! boolean_array_impl {

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
<[u8; $name::STORE_LEN]>::arbitrary_with(args)
.prop_map(|arr| $name(Store::from(arr)))
.prop_map(|arr| {
let mut v = Store::from(arr);
// make sure the value does not overflow
v[$bits..].fill(false);
$name(v)
})
}
}

Expand Down Expand Up @@ -694,6 +699,17 @@ macro_rules! boolean_array_impl {
assert_eq!(a * c, if bool::from(c) { a } else { $name::ZERO });
assert_eq!(a * &c, if bool::from(c) { a } else { $name::ZERO });
}

#[test]
fn serde_prop(a: $name) {
let mut buf = GenericArray::default();
a.serialize(&mut buf);
assert_eq!(
a,
$name::deserialize(&buf).unwrap(),
"Failed to deserialize a valid value: {a:?}"
);
}
}

#[test]
Expand Down

0 comments on commit 46bc58a

Please sign in to comment.