Skip to content

Commit

Permalink
Fix a bug inside StdArray serialization logic
Browse files Browse the repository at this point in the history
It is not correct to use $BITS/8 to define the slice size and having a test that uses BA3 demonstrates that.

This changes it to use serializable size instead
  • Loading branch information
akoshelev committed Oct 30, 2024
1 parent 46bc58a commit ff7b8b4
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ipa-core/src/secret_sharing/vector/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use generic_array::{ArrayLength, GenericArray};
use typenum::{U16, U256, U32, U64};
use typenum::{Unsigned, U16, U256, U32, U64};

use crate::{
const_assert_eq,
Expand Down Expand Up @@ -353,7 +353,7 @@ macro_rules! impl_serializable {
type DeserializationError = <V as Serializable>::DeserializationError;

fn serialize(&self, buf: &mut GenericArray<u8, Self::Size>) {
let sz: usize = (<V as SharedValue>::BITS / 8).try_into().unwrap();
let sz: usize = <V as Serializable>::Size::USIZE;
for i in 0..$width {
self.0[i].serialize(
GenericArray::try_from_mut_slice(&mut buf[sz * i..sz * (i + 1)]).unwrap(),
Expand All @@ -364,7 +364,7 @@ macro_rules! impl_serializable {
fn deserialize(
buf: &GenericArray<u8, Self::Size>,
) -> Result<Self, Self::DeserializationError> {
let sz: usize = (<V as SharedValue>::BITS / 8).try_into().unwrap();
let sz: usize = <V as Serializable>::Size::USIZE;
let mut res = [V::ZERO; $width];
for i in 0..$width {
res[i] = V::deserialize(GenericArray::from_slice(&buf[sz * i..sz * (i + 1)]))?;
Expand All @@ -390,6 +390,7 @@ mod test {
};

use super::*;
use crate::ff::boolean_array::BA3;

impl<V: SharedValue, const N: usize> Arbitrary for StdArray<V, N>
where
Expand Down Expand Up @@ -483,6 +484,13 @@ mod test {
let copy = StdArray::<Fp32BitPrime, 32>::from_iter(iter);
assert_eq!(copy, a);
}

#[test]
fn serde(a: StdArray<BA3, 32>) {
let mut buf = GenericArray::default();
a.serialize(&mut buf);
assert_eq!(a, StdArray::deserialize(&buf).unwrap());
}
}

#[test]
Expand Down

0 comments on commit ff7b8b4

Please sign in to comment.