Skip to content

Commit

Permalink
Fix clones in saturating sum module
Browse files Browse the repository at this point in the history
  • Loading branch information
akoshelev committed Sep 28, 2023
1 parent fa7688c commit 16e8768
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/protocol/boolean/saturating_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::Error,
ff::{Field, Gf2},
protocol::{boolean::or::or, context::Context, step::BitOpStep, BasicProtocols, RecordId},
secret_sharing::{BitDecomposed, Linear as LinearSecretSharing},
secret_sharing::{BitDecomposed, Linear as LinearSecretSharing, LinearRefOps},
};

#[derive(Debug)]
Expand Down Expand Up @@ -34,6 +34,7 @@ impl<S: LinearSecretSharing<Gf2>> SaturatingSum<S> {
where
C: Context,
S: LinearSecretSharing<Gf2> + BasicProtocols<C, Gf2>,
for<'a> &'a S: LinearRefOps<'a, S, Gf2>,
{
assert!(self.sum.len() >= value.len());

Expand Down Expand Up @@ -84,6 +85,7 @@ impl<S: LinearSecretSharing<Gf2>> SaturatingSum<S> {
where
C: Context,
S: LinearSecretSharing<Gf2> + BasicProtocols<C, Gf2>,
for<'a> &'a S: LinearRefOps<'a, S, Gf2>,
{
assert!(num_bits as usize <= self.sum.len());

Expand Down Expand Up @@ -136,12 +138,13 @@ async fn one_bit_adder<C, SB>(
where
C: Context,
SB: LinearSecretSharing<Gf2> + BasicProtocols<C, Gf2>,
for<'a> &'a SB: LinearRefOps<'a, SB, Gf2>,
{
// compute sum bit as x XOR y XOR carry_in
let sum_bit = x.clone() + y + carry_in;
let sum_bit = x + y + &*carry_in;

let x_xor_carry_in = x.clone() + carry_in;
let y_xor_carry_in = y.clone() + carry_in;
let x_xor_carry_in = x + &*carry_in;
let y_xor_carry_in = y + &*carry_in;

// There are two cases when the `carry_out` bit is different from the `carry_in` bit
// (1) When the `carry_in` bit is 0 and both `x` and `y` are 1
Expand All @@ -150,7 +153,7 @@ where
*carry_in = x_xor_carry_in
.multiply(&y_xor_carry_in, ctx, record_id)
.await?
+ carry_in;
+ &*carry_in;

Ok(sum_bit)
}
Expand Down Expand Up @@ -183,18 +186,19 @@ async fn one_bit_subtractor<C, SB>(
where
C: Context,
SB: LinearSecretSharing<Gf2> + BasicProtocols<C, Gf2>,
for<'a> &'a SB: LinearRefOps<'a, SB, Gf2>,
{
// compute difference bit as not_y XOR x XOR carry_in
let difference_bit = SB::share_known_value(&ctx, Gf2::ONE) + y + x + carry_in;
let difference_bit = SB::share_known_value(&ctx, Gf2::ONE) + y + x + &*carry_in;
if compute_carry_out {
let x_xor_carry_in = x.clone() + carry_in;
let y_xor_carry_in = y.clone() + carry_in;
let x_xor_carry_in = x + &*carry_in;
let y_xor_carry_in = y + &*carry_in;
let not_y_xor_carry_in = SB::share_known_value(&ctx, Gf2::ONE) + &y_xor_carry_in;

*carry_in = x_xor_carry_in
.multiply(&not_y_xor_carry_in, ctx, record_id)
.await?
+ carry_in;
+ &*carry_in;
}
Ok(difference_bit)
}
Expand Down

0 comments on commit 16e8768

Please sign in to comment.