diff --git a/ipa-core/src/ff/boolean.rs b/ipa-core/src/ff/boolean.rs index 3296e3a4e..db166ed4b 100644 --- a/ipa-core/src/ff/boolean.rs +++ b/ipa-core/src/ff/boolean.rs @@ -108,15 +108,23 @@ impl rand::distributions::Distribution for rand::distributions::Standar } } -impl std::ops::Add for Boolean { +impl std::ops::Add<&Boolean> for Boolean { type Output = Self; #[allow(clippy::suspicious_arithmetic_impl)] - fn add(self, rhs: Self) -> Self::Output { + fn add(self, rhs: &Self) -> Self::Output { Self(self.0 ^ rhs.0) } } +impl std::ops::Add for Boolean { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + std::ops::Add::add(self, &rhs) + } +} + impl std::ops::AddAssign for Boolean { #[allow(clippy::assign_op_pattern)] fn add_assign(&mut self, rhs: Self) { @@ -132,12 +140,20 @@ impl std::ops::Neg for Boolean { } } -impl std::ops::Sub for Boolean { +impl std::ops::Sub<&Self> for Boolean { type Output = Self; #[allow(clippy::suspicious_arithmetic_impl)] + fn sub(self, rhs: &Self) -> Self::Output { + std::ops::Add::add(self, rhs) + } +} + +impl std::ops::Sub for Boolean { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { - self + rhs + std::ops::Sub::sub(self, &rhs) } } diff --git a/ipa-core/src/ff/curve_points.rs b/ipa-core/src/ff/curve_points.rs index 32d3077e3..78eccff15 100644 --- a/ipa-core/src/ff/curve_points.rs +++ b/ipa-core/src/ff/curve_points.rs @@ -78,11 +78,19 @@ impl Serializable for RP25519 { ///## Panics /// Panics when decompressing invalid curve point. This can happen when deserialize curve point /// from bit array that does not have a valid representation on the curve +impl std::ops::Add<&Self> for RP25519 { + type Output = Self; + + fn add(self, rhs: &Self) -> Self::Output { + Self((self.0.decompress().unwrap() + rhs.0.decompress().unwrap()).compress()) + } +} + impl std::ops::Add for RP25519 { type Output = Self; fn add(self, rhs: Self) -> Self::Output { - Self((self.0.decompress().unwrap() + rhs.0.decompress().unwrap()).compress()) + std::ops::Add::add(self, &rhs) } } @@ -107,11 +115,19 @@ impl std::ops::Neg for RP25519 { ///## Panics /// Panics when decompressing invalid curve point. This can happen when deserialize curve point /// from bit array that does not have a valid representation on the curve +impl std::ops::Sub<&Self> for RP25519 { + type Output = Self; + + fn sub(self, rhs: &Self) -> Self::Output { + Self((self.0.decompress().unwrap() - rhs.0.decompress().unwrap()).compress()) + } +} + impl std::ops::Sub for RP25519 { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { - Self((self.0.decompress().unwrap() - rhs.0.decompress().unwrap()).compress()) + std::ops::Sub::sub(self, &rhs) } } diff --git a/ipa-core/src/ff/ec_prime_field.rs b/ipa-core/src/ff/ec_prime_field.rs index 64de59f9e..e3dc3d971 100644 --- a/ipa-core/src/ff/ec_prime_field.rs +++ b/ipa-core/src/ff/ec_prime_field.rs @@ -84,11 +84,19 @@ impl rand::distributions::Distribution for rand::distributions::Standar } } +impl std::ops::Add<&Self> for Fp25519 { + type Output = Self; + + fn add(self, rhs: &Self) -> Self::Output { + Self(self.0 + rhs.0) + } +} + impl std::ops::Add for Fp25519 { type Output = Self; fn add(self, rhs: Self) -> Self::Output { - Self(self.0 + rhs.0) + std::ops::Add::add(self, &rhs) } } @@ -107,11 +115,19 @@ impl std::ops::Neg for Fp25519 { } } +impl std::ops::Sub<&Self> for Fp25519 { + type Output = Self; + + fn sub(self, rhs: &Self) -> Self::Output { + Self(self.0 - rhs.0) + } +} + impl std::ops::Sub for Fp25519 { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { - Self(self.0 - rhs.0) + std::ops::Sub::sub(self, &rhs) } } diff --git a/ipa-core/src/ff/galois_field.rs b/ipa-core/src/ff/galois_field.rs index 0f4874dd0..293f282a8 100644 --- a/ipa-core/src/ff/galois_field.rs +++ b/ipa-core/src/ff/galois_field.rs @@ -287,10 +287,17 @@ macro_rules! bit_array_impl { } } + impl std::ops::Sub<&$name> for $name { + type Output = Self; + fn sub(self, rhs: &Self) -> Self::Output { + std::ops::Add::add(self, rhs) + } + } + impl std::ops::Sub for $name { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { - self + rhs + std::ops::Sub::sub(self, &rhs) } } diff --git a/ipa-core/src/ff/prime_field.rs b/ipa-core/src/ff/prime_field.rs index cfe90bd28..10ec87694 100644 --- a/ipa-core/src/ff/prime_field.rs +++ b/ipa-core/src/ff/prime_field.rs @@ -117,10 +117,10 @@ macro_rules! field_impl { const PRIME: Self::PrimeInteger = $prime; } - impl std::ops::Add for $field { + impl std::ops::Add<&$field> for $field { type Output = Self; - fn add(self, rhs: Self) -> Self::Output { + fn add(self, rhs: &Self) -> Self::Output { let c = u64::from; debug_assert!(c(Self::PRIME) < (u64::MAX >> 1)); #[allow(clippy::cast_possible_truncation)] @@ -128,6 +128,14 @@ macro_rules! field_impl { } } + impl std::ops::Add for $field { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + std::ops::Add::add(self, &rhs) + } + } + impl std::ops::AddAssign for $field { #[allow(clippy::assign_op_pattern)] fn add_assign(&mut self, rhs: Self) { @@ -143,10 +151,10 @@ macro_rules! field_impl { } } - impl std::ops::Sub for $field { + impl std::ops::Sub<&$field> for $field { type Output = Self; - fn sub(self, rhs: Self) -> Self::Output { + fn sub(self, rhs: &Self) -> Self::Output { let c = u64::from; debug_assert!(c(Self::PRIME) < (u64::MAX >> 1)); // TODO(mt) - constant time? @@ -158,6 +166,14 @@ macro_rules! field_impl { } } + impl std::ops::Sub for $field { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + std::ops::Sub::sub(self, &rhs) + } + } + impl std::ops::SubAssign for $field { #[allow(clippy::assign_op_pattern)] fn sub_assign(&mut self, rhs: Self) { diff --git a/ipa-core/src/protocol/basics/reshare.rs b/ipa-core/src/protocol/basics/reshare.rs index cb33a8146..28d9169a8 100644 --- a/ipa-core/src/protocol/basics/reshare.rs +++ b/ipa-core/src/protocol/basics/reshare.rs @@ -74,7 +74,7 @@ impl Reshare for Replicated { .await?; // Sleep until `to_helper.right` sends us their part2 value - let part2 = ctx + let part2: F = ctx .recv_channel(to_helper.peer(Direction::Right)) .receive(record_id) .await?; diff --git a/ipa-core/src/protocol/context/mod.rs b/ipa-core/src/protocol/context/mod.rs index ecb569d63..83adb7d5f 100644 --- a/ipa-core/src/protocol/context/mod.rs +++ b/ipa-core/src/protocol/context/mod.rs @@ -690,7 +690,7 @@ mod tests { ctx.role().peer(Direction::Right), ); let record_id = index.into(); - let (l, r) = ctx.prss().generate_fields(record_id); + let (l, r): (F, F) = ctx.prss().generate_fields(record_id); let (seq_l, seq_r) = { let ctx = ctx.narrow(&format!("seq-prss-{record_id}")); diff --git a/ipa-core/src/protocol/ipa_prf/shuffle/sharded.rs b/ipa-core/src/protocol/ipa_prf/shuffle/sharded.rs index 6a37fd6f3..0f2351439 100644 --- a/ipa-core/src/protocol/ipa_prf/shuffle/sharded.rs +++ b/ipa-core/src/protocol/ipa_prf/shuffle/sharded.rs @@ -369,7 +369,7 @@ where send_channel.send(record_id, c1), recv_channel.receive(record_id), ) - .map_ok(move |((), c2)| S::new(b, c1 + c2)) + .map_ok(move |((), c2): ((), S::Share)| S::new(b, c1 + c2)) })) .await?; diff --git a/ipa-core/src/protocol/prss/mod.rs b/ipa-core/src/protocol/prss/mod.rs index 3ec697ba9..46a4be17c 100644 --- a/ipa-core/src/protocol/prss/mod.rs +++ b/ipa-core/src/protocol/prss/mod.rs @@ -655,8 +655,8 @@ pub mod test { let s3 = p3.indexed(&step); let r1: Fp31 = random(&*s1, IDX1); - let r2 = random(&*s2, IDX1); - let r3 = random(&*s3, IDX1); + let r2: Fp31 = random(&*s2, IDX1); + let r3: Fp31 = random(&*s3, IDX1); let v1 = r1 + r2 + r3; // There isn't enough entropy in this field (~5 bits) to be sure that the test will pass. @@ -664,8 +664,8 @@ pub mod test { let mut v2 = Fp31::truncate_from(0_u8); for i in IDX2..(IDX2 + 21) { let r1: Fp31 = random(&*s1, i); - let r2 = random(&*s2, i); - let r3 = random(&*s3, i); + let r2: Fp31 = random(&*s2, i); + let r3: Fp31 = random(&*s3, i); v2 = r1 + r2 + r3; if v1 != v2 { diff --git a/ipa-core/src/secret_sharing/mod.rs b/ipa-core/src/secret_sharing/mod.rs index 3e3085149..384f57387 100644 --- a/ipa-core/src/secret_sharing/mod.rs +++ b/ipa-core/src/secret_sharing/mod.rs @@ -36,12 +36,15 @@ use crate::{ /// Operations supported for weak shared values. pub trait Additive: - AddSub + AddSubAssign + Neg + AddSub + for<'a> AddSub<&'a Rhs, Output> + AddSubAssign + Neg { } impl Additive for T where - T: AddSub + AddSubAssign + Neg + T: AddSub + + for<'a> AddSub<&'a Rhs, Output> + + AddSubAssign + + Neg { }