Skip to content

Commit

Permalink
Remove SharedValueArray get/set methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed Jan 26, 2024
1 parent 6f5feda commit cb0e7f4
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 53 deletions.
3 changes: 3 additions & 0 deletions ipa-core/src/ff/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use typenum::U1;
use super::Gf32Bit;
use crate::{
ff::{Field, Serializable},
impl_shared_value_common,
protocol::prss::FromRandomU128,
secret_sharing::{
replicated::malicious::ExtendableField, Block, FieldVectorizable, SharedValue, StdArray,
Expand Down Expand Up @@ -41,6 +42,8 @@ impl SharedValue for Boolean {
type Storage = bool;
const BITS: u32 = 1;
const ZERO: Self = Self(false);

impl_shared_value_common!();
}

impl Vectorizable<1> for Boolean {
Expand Down
3 changes: 3 additions & 0 deletions ipa-core/src/ff/boolean_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ macro_rules! boolean_array_impl {
use super::*;
use crate::{
ff::{boolean::Boolean, ArrayAccess, Expand, Serializable},
impl_shared_value_common,
secret_sharing::{
replicated::semi_honest::{ASIterator, AdditiveShare},
SharedValue,
Expand Down Expand Up @@ -317,6 +318,8 @@ macro_rules! boolean_array_impl {
type Storage = Store;
const BITS: u32 = $bits;
const ZERO: Self = Self(<Store>::ZERO);

impl_shared_value_common!();
}

impl_serializable_trait!($name, $bits, Store, $deser_type);
Expand Down
3 changes: 3 additions & 0 deletions ipa-core/src/ff/curve_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use typenum::U32;

use crate::{
ff::{ec_prime_field::Fp25519, Serializable},
impl_shared_value_common,
secret_sharing::{Block, SharedValue, StdArray, Vectorizable},
};

Expand All @@ -33,6 +34,8 @@ impl SharedValue for RP25519 {
type Storage = CompressedRistretto;
const BITS: u32 = 256;
const ZERO: Self = Self(CompressedRistretto([0_u8; 32]));

impl_shared_value_common!();
}

impl Vectorizable<1> for RP25519 {
Expand Down
3 changes: 3 additions & 0 deletions ipa-core/src/ff/ec_prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use typenum::U32;

use crate::{
ff::{boolean_array::BA256, Field, Serializable},
impl_shared_value_common,
protocol::prss::FromRandomU128,
secret_sharing::{Block, FieldVectorizable, SharedValue, StdArray, Vectorizable},
};
Expand Down Expand Up @@ -39,6 +40,8 @@ impl SharedValue for Fp25519 {
type Storage = Scalar;
const BITS: u32 = 256;
const ZERO: Self = Self(Scalar::ZERO);

impl_shared_value_common!();
}

///conversion to Scalar struct of `curve25519_dalek`
Expand Down
4 changes: 3 additions & 1 deletion ipa-core/src/ff/galois_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use typenum::{Unsigned, U1, U2, U3, U4, U5};
use super::ArrayAccess;
use crate::{
ff::{boolean_array::NonZeroPadding, Field, Serializable},
impl_serializable_trait,
impl_serializable_trait, impl_shared_value_common,
protocol::prss::FromRandomU128,
secret_sharing::{Block, FieldVectorizable, SharedValue, Vectorizable},
};
Expand Down Expand Up @@ -173,6 +173,8 @@ macro_rules! bit_array_impl {
type Storage = $store;
const BITS: u32 = $bits;
const ZERO: Self = Self(<$store>::ZERO);

impl_shared_value_common!();
}

impl Vectorizable<1> for $name {
Expand Down
6 changes: 4 additions & 2 deletions ipa-core/src/ff/prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use generic_array::GenericArray;

use super::Field;
use crate::{
ff::Serializable,
ff::{FieldType, Serializable},
impl_shared_value_common,
protocol::prss::FromRandomU128,
secret_sharing::{Block, FieldVectorizable, SharedValue, StdArray, Vectorizable},
};
Expand All @@ -22,7 +23,6 @@ pub struct GreaterThanPrimeError<V: Display>(V, u128);
macro_rules! field_impl {
( $field:ident, $store:ty, $bits:expr, $prime:expr ) => {
use super::*;
use crate::ff::FieldType;

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct $field(<Self as SharedValue>::Storage);
Expand All @@ -31,6 +31,8 @@ macro_rules! field_impl {
type Storage = $store;
const BITS: u32 = $bits;
const ZERO: Self = $field(0);

impl_shared_value_common!();
}

impl Vectorizable<1> for $field {
Expand Down
35 changes: 10 additions & 25 deletions ipa-core/src/secret_sharing/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ impl<V: SharedValue, const N: usize> PartialEq<StdArray<V, N>> for [V; N] {
}
}

impl<V: SharedValue, const N: usize> StdArray<V, N> {
pub fn first(&self) -> &V {
&self.0[0]
}

pub fn first_mut(&mut self) -> &mut V {
&mut self.0[0]
}
}

impl<V: SharedValue, const N: usize> SharedValueArray<V> for StdArray<V, N>
where
Self: Serializable,
Expand All @@ -56,18 +66,6 @@ where
fn from_fn<F: FnMut(usize) -> V>(f: F) -> Self {
Self(array::from_fn(f))
}

Check warning on line 68 in ipa-core/src/secret_sharing/array.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/secret_sharing/array.rs#L66-L68

Added lines #L66 - L68 were not covered by tests

fn get(&self, index: usize) -> V {
self.0[index]
}

fn get_mut(&mut self, index: usize) -> &mut V {
&mut self.0[index]
}

fn set(&mut self, index: usize, value: V) {
self.0[index] = value;
}
}

impl<F: Field, const N: usize> FieldArray<F> for StdArray<F, N> where Self: FromRandom + Serializable
Expand Down Expand Up @@ -434,17 +432,4 @@ mod test {
fn from_short_iter() {
StdArray::<Fp32BitPrime, 32>::from_iter(iter::empty());
}

proptest! {
#[test]
fn get_set(mut a: StdArray<Fp32BitPrime, 1>, b: Fp32BitPrime, c: Fp32BitPrime) {
assert_eq!(a.get(0), a.0[0]);
a.set(0, b);
assert_eq!(a.get(0), b);
*a.get_mut(0) = c;
assert_eq!(a.get(0), c);
let from_fn = StdArray::<Fp32BitPrime, 1>::from_fn(|i| a.0[i]);
assert_eq!(from_fn, a);
}
}
}
59 changes: 34 additions & 25 deletions ipa-core/src/secret_sharing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mod scheme;

use std::{
fmt::Debug,
iter::once,
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

Expand Down Expand Up @@ -129,29 +128,45 @@ pub trait SharedValue:

// Note the trait bound of `Vectorizable<1>` here, i.e., these
// helpers only apply to arrays of a single element.
fn into_array<A>(self) -> A
fn into_array(self) -> <Self as Vectorizable<1>>::Array
where
Self: Vectorizable<1, Array = A>,
A: SharedValueArray<Self>,
{
once(self).collect::<A>()
}
Self: Vectorizable<1>;

fn from_array<A>(array: &A) -> Self
fn from_array(array: &<Self as Vectorizable<1>>::Array) -> Self
where
Self: Vectorizable<1, Array = A>,
A: SharedValueArray<Self>,
{
array.get(0)
}
Self: Vectorizable<1>;

fn from_array_mut<A>(array: &mut A) -> &mut Self
fn from_array_mut(array: &mut <Self as Vectorizable<1>>::Array) -> &mut Self
where
Self: Vectorizable<1, Array = A>,
A: SharedValueArray<Self>,
{
array.get_mut(0)
}
Self: Vectorizable<1>;
}

#[macro_export]
macro_rules! impl_shared_value_common {
() => {
// Note the trait bound of `Vectorizable<1>` here, i.e., these
// helpers only apply to arrays of a single element.
fn into_array(self) -> <Self as Vectorizable<1>>::Array
where
Self: Vectorizable<1>,
{
std::iter::once(self).collect()
}

fn from_array(array: &<Self as Vectorizable<1>>::Array) -> Self
where
Self: Vectorizable<1>,
{
*array.first()
}

fn from_array_mut(array: &mut <Self as Vectorizable<1>>::Array) -> &mut Self
where
Self: Vectorizable<1>,
{
array.first_mut()
}
};
}

// Note that we can either make `trait Vectorizable<N>: SharedValue`, or we can make `trait
Expand Down Expand Up @@ -226,12 +241,6 @@ pub trait SharedValueArray<V>:
const ZERO: Self;

fn from_fn<F: FnMut(usize) -> V>(f: F) -> Self;

fn get(&self, index: usize) -> V;

fn get_mut(&mut self, index: usize) -> &mut V;

fn set(&mut self, index: usize, value: V);
}

// Some `SharedValue` types (and thus their arrays) implement `FromRandom`, but `RP25519` does not.
Expand Down

0 comments on commit cb0e7f4

Please sign in to comment.