Skip to content

Commit

Permalink
Include Add<&Self> in SharedValue
Browse files Browse the repository at this point in the history
This will be required to eliminate `Add` bounds from the shuffle traits.
  • Loading branch information
andyleiserson committed Oct 31, 2024
1 parent 21e954d commit e1adf57
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 22 deletions.
24 changes: 20 additions & 4 deletions ipa-core/src/ff/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,23 @@ impl rand::distributions::Distribution<Boolean> 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) {
Expand All @@ -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)
}
}

Expand Down
20 changes: 18 additions & 2 deletions ipa-core/src/ff/curve_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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())
}

Check warning on line 123 in ipa-core/src/ff/curve_points.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/ff/curve_points.rs#L121-L123

Added lines #L121 - L123 were not covered by tests
}

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)

Check warning on line 130 in ipa-core/src/ff/curve_points.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/ff/curve_points.rs#L130

Added line #L130 was not covered by tests
}
}

Expand Down
20 changes: 18 additions & 2 deletions ipa-core/src/ff/ec_prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ impl rand::distributions::Distribution<Fp25519> 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)
}
}

Expand All @@ -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)
}
}

Expand Down
9 changes: 8 additions & 1 deletion ipa-core/src/ff/galois_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
24 changes: 20 additions & 4 deletions ipa-core/src/ff/prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,25 @@ 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)]
Self(((c(self.0) + c(rhs.0)) % c(Self::PRIME)) as <Self as SharedValue>::Storage)
}
}

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) {
Expand All @@ -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?
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion ipa-core/src/protocol/basics/reshare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<C: Context, F: Field> Reshare<C> for Replicated<F> {
.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?;
Expand Down
2 changes: 1 addition & 1 deletion ipa-core/src/protocol/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
Expand Down
2 changes: 1 addition & 1 deletion ipa-core/src/protocol/ipa_prf/shuffle/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
8 changes: 4 additions & 4 deletions ipa-core/src/protocol/prss/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,17 +655,17 @@ 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.
// So run a few rounds (~21 -> ~100 bits) looking for a mismatch.
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 {
Expand Down
7 changes: 5 additions & 2 deletions ipa-core/src/secret_sharing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ use crate::{

/// Operations supported for weak shared values.
pub trait Additive<Rhs = Self, Output = Self>:
AddSub<Rhs, Output> + AddSubAssign<Rhs> + Neg<Output = Output>
AddSub<Rhs, Output> + for<'a> AddSub<&'a Rhs, Output> + AddSubAssign<Rhs> + Neg<Output = Output>
{
}

impl<T, Rhs, Output> Additive<Rhs, Output> for T where
T: AddSub<Rhs, Output> + AddSubAssign<Rhs> + Neg<Output = Output>
T: AddSub<Rhs, Output>
+ for<'a> AddSub<&'a Rhs, Output>
+ AddSubAssign<Rhs>
+ Neg<Output = Output>
{
}

Expand Down

0 comments on commit e1adf57

Please sign in to comment.