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

Use Shuffleable traits for all shuffle routines #1402

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions ipa-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ pub enum Error {
ShardInfraError(#[from] crate::helpers::Error<ShardIndex>),
#[error("Value truncation error: {0}")]
FieldValueTruncation(String),
#[error("The field element size is too small: {0}")]
FieldConversion(String),
#[error("Invalid query parameter: {0}")]
InvalidQueryParameter(BoxError),
#[error("invalid report: {0}")]
Expand Down
28 changes: 5 additions & 23 deletions ipa-core/src/ff/boolean_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ use generic_array::GenericArray;
use typenum::{U12, U14, U18, U2, U32, U8};

use crate::{
error::{Error, LengthError},
error::LengthError,
ff::{boolean::Boolean, ArrayAccess, Expand, Field, Gf32Bit, Serializable, U128Conversions},
protocol::prss::{FromRandom, FromRandomU128},
secret_sharing::{
replicated::{semi_honest::AdditiveShare, ReplicatedSecretSharing},
Block, SharedValue, StdArray, Vectorizable,
},
secret_sharing::{Block, SharedValue, StdArray, Vectorizable},
};

/// The implementation below cannot be constrained without breaking Rust's
Expand All @@ -39,7 +36,7 @@ pub trait BooleanArray:
+ ArrayAccess<Output = Boolean>
+ Expand<Input = Boolean>
+ FromIterator<Boolean>
+ TryInto<Vec<Gf32Bit>, Error = crate::error::Error>
+ TryInto<Vec<Gf32Bit>, Error = LengthError>
{
}

Expand All @@ -48,25 +45,10 @@ impl<A> BooleanArray for A where
+ ArrayAccess<Output = Boolean>
+ Expand<Input = Boolean>
+ FromIterator<Boolean>
+ TryInto<Vec<Gf32Bit>, Error = crate::error::Error>
+ TryInto<Vec<Gf32Bit>, Error = LengthError>
{
}

impl<A> AdditiveShare<A>
where
A: BooleanArray,
AdditiveShare<A>: Sized,
{
pub(crate) fn to_gf32bit(&self) -> Result<impl Iterator<Item = AdditiveShare<Gf32Bit>>, Error> {
let left_shares: Vec<Gf32Bit> = self.left().try_into()?;
let right_shares: Vec<Gf32Bit> = self.right().try_into()?;
Ok(left_shares
.into_iter()
.zip(right_shares)
.map(|(left, right)| AdditiveShare::new(left, right)))
}
}

/// Iterator returned by `.iter()` on Boolean arrays
pub struct BAIterator<'a> {
iterator: std::iter::Take<Iter<'a, u8, Lsb0>>,
Expand Down Expand Up @@ -525,7 +507,7 @@ macro_rules! boolean_array_impl {
/// ##Errors
/// Outputs an error when conversion from raw slice to Galois field element fails.
impl TryFrom<$name> for Vec<Gf32Bit> {
type Error = crate::error::Error;
type Error = LengthError;

fn try_from(value: $name) -> Result<Self, Self::Error> {
// len() returns bits, so divide by 8
Expand Down
24 changes: 13 additions & 11 deletions ipa-core/src/ff/galois_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use generic_array::GenericArray;
use typenum::{Unsigned, U1, U2, U3, U4, U5};

use crate::{
error::LengthError,
ff::{boolean_array::NonZeroPadding, Field, Serializable, U128Conversions},
impl_serializable_trait, impl_shared_value_common,
protocol::prss::FromRandomU128,
Expand Down Expand Up @@ -200,20 +201,18 @@ macro_rules! bit_array_impl {
/// ## Panics
/// Panics when `u32` to `usize` conversion fails
impl TryFrom<&[u8]> for $name {

type Error = crate::error::Error;
type Error = LengthError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len()<=usize::try_from(Self::BITS/8).unwrap() {
let mut bitarray = [0u8;{($bits+7)/8}];
bitarray[0..value.len()].copy_from_slice(value);
Ok($name(BitArray::<[u8;{($bits+7)/8}],Lsb0>::new(bitarray)))
} else {
Err(crate::error::Error::FieldConversion(format!(
"Element bit size {} is too small to hold {} bytes.",
Self::BITS,
value.len()
)))
Err(LengthError {
expected: usize::try_from(Self::BITS).unwrap(),
actual: value.len(),
})
}
}
}
Expand Down Expand Up @@ -620,10 +619,13 @@ macro_rules! bit_array_impl {
let mut rng = thread_rng();
let vec = (0..{(<$name>::BITS+7)/8+1}).map(|_| rng.gen::<u8>()).collect::<Vec<_>>();
let element = <$name>::try_from(vec.as_slice());
assert!(matches!(
element,
Err(crate::error::Error::FieldConversion(_))
));
assert_eq!(
element.unwrap_err(),
LengthError {
expected: <$name>::BITS as usize,
actual: ((<$name>::BITS + 7) / 8 + 1) as usize,
},
);
}
}

Expand Down
Loading