From cb0e7f4364380f09951937d86b1cdf3a9bd7aeab Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 25 Jan 2024 12:27:52 -0800 Subject: [PATCH] Remove SharedValueArray get/set methods --- ipa-core/src/ff/boolean.rs | 3 ++ ipa-core/src/ff/boolean_array.rs | 3 ++ ipa-core/src/ff/curve_points.rs | 3 ++ ipa-core/src/ff/ec_prime_field.rs | 3 ++ ipa-core/src/ff/galois_field.rs | 4 +- ipa-core/src/ff/prime_field.rs | 6 ++- ipa-core/src/secret_sharing/array.rs | 35 +++++------------ ipa-core/src/secret_sharing/mod.rs | 59 ++++++++++++++++------------ 8 files changed, 63 insertions(+), 53 deletions(-) diff --git a/ipa-core/src/ff/boolean.rs b/ipa-core/src/ff/boolean.rs index f577c5ac1..c535914d8 100644 --- a/ipa-core/src/ff/boolean.rs +++ b/ipa-core/src/ff/boolean.rs @@ -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, @@ -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 { diff --git a/ipa-core/src/ff/boolean_array.rs b/ipa-core/src/ff/boolean_array.rs index 16e8b041f..32ba62b8a 100644 --- a/ipa-core/src/ff/boolean_array.rs +++ b/ipa-core/src/ff/boolean_array.rs @@ -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, @@ -317,6 +318,8 @@ macro_rules! boolean_array_impl { type Storage = Store; const BITS: u32 = $bits; const ZERO: Self = Self(::ZERO); + + impl_shared_value_common!(); } impl_serializable_trait!($name, $bits, Store, $deser_type); diff --git a/ipa-core/src/ff/curve_points.rs b/ipa-core/src/ff/curve_points.rs index f0db75b49..c89701ae4 100644 --- a/ipa-core/src/ff/curve_points.rs +++ b/ipa-core/src/ff/curve_points.rs @@ -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}, }; @@ -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 { diff --git a/ipa-core/src/ff/ec_prime_field.rs b/ipa-core/src/ff/ec_prime_field.rs index 4c03a6a6f..c2478c8cf 100644 --- a/ipa-core/src/ff/ec_prime_field.rs +++ b/ipa-core/src/ff/ec_prime_field.rs @@ -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}, }; @@ -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` diff --git a/ipa-core/src/ff/galois_field.rs b/ipa-core/src/ff/galois_field.rs index df2ca6290..1b7d1f526 100644 --- a/ipa-core/src/ff/galois_field.rs +++ b/ipa-core/src/ff/galois_field.rs @@ -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}, }; @@ -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 { diff --git a/ipa-core/src/ff/prime_field.rs b/ipa-core/src/ff/prime_field.rs index 2f9694969..27e22a74e 100644 --- a/ipa-core/src/ff/prime_field.rs +++ b/ipa-core/src/ff/prime_field.rs @@ -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}, }; @@ -22,7 +23,6 @@ pub struct GreaterThanPrimeError(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(::Storage); @@ -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 { diff --git a/ipa-core/src/secret_sharing/array.rs b/ipa-core/src/secret_sharing/array.rs index 7b9e09cff..ce6bc3fb5 100644 --- a/ipa-core/src/secret_sharing/array.rs +++ b/ipa-core/src/secret_sharing/array.rs @@ -47,6 +47,16 @@ impl PartialEq> for [V; N] { } } +impl StdArray { + pub fn first(&self) -> &V { + &self.0[0] + } + + pub fn first_mut(&mut self) -> &mut V { + &mut self.0[0] + } +} + impl SharedValueArray for StdArray where Self: Serializable, @@ -56,18 +66,6 @@ where fn from_fn V>(f: F) -> Self { Self(array::from_fn(f)) } - - 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 FieldArray for StdArray where Self: FromRandom + Serializable @@ -434,17 +432,4 @@ mod test { fn from_short_iter() { StdArray::::from_iter(iter::empty()); } - - proptest! { - #[test] - fn get_set(mut a: StdArray, 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::::from_fn(|i| a.0[i]); - assert_eq!(from_fn, a); - } - } } diff --git a/ipa-core/src/secret_sharing/mod.rs b/ipa-core/src/secret_sharing/mod.rs index 3b4923854..bb9e4891e 100644 --- a/ipa-core/src/secret_sharing/mod.rs +++ b/ipa-core/src/secret_sharing/mod.rs @@ -52,7 +52,6 @@ mod scheme; use std::{ fmt::Debug, - iter::once, ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}, }; @@ -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(self) -> A + fn into_array(self) -> >::Array where - Self: Vectorizable<1, Array = A>, - A: SharedValueArray, - { - once(self).collect::() - } + Self: Vectorizable<1>; - fn from_array(array: &A) -> Self + fn from_array(array: &>::Array) -> Self where - Self: Vectorizable<1, Array = A>, - A: SharedValueArray, - { - array.get(0) - } + Self: Vectorizable<1>; - fn from_array_mut(array: &mut A) -> &mut Self + fn from_array_mut(array: &mut >::Array) -> &mut Self where - Self: Vectorizable<1, Array = A>, - A: SharedValueArray, - { - 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) -> >::Array + where + Self: Vectorizable<1>, + { + std::iter::once(self).collect() + } + + fn from_array(array: &>::Array) -> Self + where + Self: Vectorizable<1>, + { + *array.first() + } + + fn from_array_mut(array: &mut >::Array) -> &mut Self + where + Self: Vectorizable<1>, + { + array.first_mut() + } + }; } // Note that we can either make `trait Vectorizable: SharedValue`, or we can make `trait @@ -226,12 +241,6 @@ pub trait SharedValueArray: const ZERO: Self; fn from_fn 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.