Skip to content

Commit

Permalink
Tweak a few things to be more in line with the vectorized version
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed Jan 10, 2024
1 parent 6680b44 commit 578caf1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
12 changes: 11 additions & 1 deletion ipa-core/src/ff/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ impl Block for bool {
#[derive(Clone, Copy, PartialEq, Debug, Eq)]
pub struct Boolean(bool);

impl Boolean {
pub const TRUE: Boolean = Self(true);
pub const FALSE: Boolean = Self(false);

#[must_use]
pub fn as_u128(&self) -> u128 {
bool::from(*self).into()
}
}

impl ExtendableField for Boolean {
type ExtendedField = Gf32Bit;

Expand Down Expand Up @@ -139,7 +149,7 @@ impl Field for Boolean {
const ONE: Boolean = Boolean(true);

fn as_u128(&self) -> u128 {
bool::from(*self).into()
Boolean::as_u128(self)
}

fn truncate_from<T: Into<u128>>(v: T) -> Self {
Expand Down
16 changes: 8 additions & 8 deletions ipa-core/src/protocol/boolean/generate_random_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use futures::stream::{iter as stream_iter, StreamExt};

use crate::{
error::Error,
ff::{ArrayAccessReplicated, PrimeField},
ff::{boolean::Boolean, ArrayAccessReplicated, PrimeField},
helpers::Role,
protocol::{
basics::SecureMul,
Expand Down Expand Up @@ -47,21 +47,21 @@ impl ArrayAccessReplicated for RawRandomBits {
}

impl<'a> BorrowReplicated for RawRandomBitsIndex<'a> {
type Output = bool;
type Output = Boolean;

fn borrow_left(&self) -> &bool {
fn borrow_left(&self) -> &Self::Output {
if ((self.bits.left >> self.index) & 1) == 1 {
&true
&Boolean::TRUE
} else {
&false
&Boolean::FALSE
}
}

fn borrow_right(&self) -> &bool {
fn borrow_right(&self) -> &Self::Output {
if ((self.bits.right >> self.index) & 1) == 1 {
&true
&Boolean::TRUE
} else {
&false
&Boolean::FALSE
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions ipa-core/src/protocol/modulus_conversion/convert_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use pin_project::pin_project;
use crate::{
error::Error,
exact::ExactSizeStream,
ff::{ArrayAccessReplicated, Field, Gf2, PrimeField},
ff::{boolean::Boolean, ArrayAccessReplicated, Field, Gf2, PrimeField},
helpers::Role,
protocol::{
basics::{SecureMul, ZeroPositions},
Expand Down Expand Up @@ -73,14 +73,14 @@ impl<F: PrimeField> BitConversionTriple<Replicated<F>> {
///
/// # Panics
/// If any bits in the bitwise shared input cannot be converted into the given field `F`
/// without truncation or if the bit index is out of range for `B`.
/// without truncation.
#[must_use]
pub fn new<G: Copy + Into<bool>, T: BorrowReplicated<Output = G>>(
helper_role: Role,
src: &T,
) -> Self {
let left = F::try_from(u128::from((*src.borrow_left()).into())).unwrap();
let right = F::try_from(u128::from((*src.borrow_right()).into())).unwrap();
pub fn new<B>(helper_role: Role, src: &B) -> Self
where
B: BorrowReplicated<Output = Boolean>,
{
let left = F::try_from(src.borrow_left().as_u128()).unwrap();
let right = F::try_from(src.borrow_right().as_u128()).unwrap();
Self(match helper_role {
Role::H1 => [
Replicated::new(left, F::ZERO),
Expand Down Expand Up @@ -143,12 +143,11 @@ pub trait ToBitConversionTriples: Sized {
}
}

impl<B, G> ToBitConversionTriples for Replicated<B>
impl<B> ToBitConversionTriples for Replicated<B>
where
Self: ArrayAccessReplicated,
for<'a> <Self as ArrayAccessReplicated>::Ref<'a>: BorrowReplicated<Output = G>,
for<'a> <Self as ArrayAccessReplicated>::Ref<'a>: BorrowReplicated<Output = Boolean>,
B: SharedValue,
G: Copy + Into<bool>,
{
type Residual = ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use generic_array::{ArrayLength, GenericArray};
use typenum::Unsigned;

use crate::{
ff::{ArrayAccess, ArrayAccessReplicated, Expand, Field, Gf2, Serializable},
ff::{boolean::Boolean, ArrayAccess, ArrayAccessReplicated, Expand, Field, Serializable},
secret_sharing::{
replicated::ReplicatedSecretSharing, Linear as LinearSecretSharing, SecretSharing,
SharedValue,
Expand Down Expand Up @@ -71,47 +71,49 @@ pub trait BorrowReplicatedMut: BorrowReplicated {
fn borrow_right_mut(&mut self) -> &mut Self::Output;
}

// This exists as a special case so we can provide an impl of `BorrowReplicated` that
// returns `bool` rather than `Gf2`.
#[derive(Clone, Copy)]
pub struct AdditiveShareGf2(Gf2, Gf2);
pub struct BooleanAdditiveShare(Boolean, Boolean);

impl BorrowReplicated for AdditiveShareGf2 {
type Output = bool;
impl BorrowReplicated for BooleanAdditiveShare {
type Output = Boolean;

fn borrow_left(&self) -> &bool {
fn borrow_left(&self) -> &Self::Output {
if self.0.into() {
&true
&Boolean::TRUE
} else {
&false
&Boolean::FALSE
}
}

fn borrow_right(&self) -> &bool {
fn borrow_right(&self) -> &Self::Output {
if self.1.into() {
&true
&Boolean::TRUE
} else {
&false
&Boolean::FALSE
}
}
}

impl<V: SharedValue> BorrowReplicated for AdditiveShare<V> {
type Output = V;

fn borrow_left(&self) -> &V {
fn borrow_left(&self) -> &Self::Output {
&self.0
}

fn borrow_right(&self) -> &V {
fn borrow_right(&self) -> &Self::Output {
&self.1
}
}

impl<V: SharedValue> BorrowReplicatedMut for AdditiveShare<V> {
fn borrow_left_mut(&mut self) -> &mut V {
fn borrow_left_mut(&mut self) -> &mut Self::Output {
&mut self.0
}

fn borrow_right_mut(&mut self) -> &mut V {
fn borrow_right_mut(&mut self) -> &mut Self::Output {
&mut self.1
}
}
Expand Down Expand Up @@ -351,18 +353,18 @@ where
// This would ideally be an `AdditiveShareRef` type that holds two references,
// but due to Galois fields not being indexable for `Gf2` values currently,
// it is not done that way.
type Ref<'a> = AdditiveShareGf2;
type Ref<'a> = BooleanAdditiveShare;

fn get(&self, index: usize) -> Option<Self::Ref<'_>> {
self.0
.get(index)
.zip(self.1.get(index))
.map(|v| AdditiveShareGf2(bool::from(v.0).into(), bool::from(v.1).into()))
.map(|v| BooleanAdditiveShare(bool::from(v.0).into(), bool::from(v.1).into()))
}

fn set(&mut self, index: usize, e: Self::Ref<'_>) {
self.0.set(index, <bool as From<Gf2>>::from(e.0).into());
self.1.set(index, <bool as From<Gf2>>::from(e.1).into());
self.0.set(index, <bool as From<Boolean>>::from(e.0).into());
self.1.set(index, <bool as From<Boolean>>::from(e.1).into());
}
}

Expand Down

0 comments on commit 578caf1

Please sign in to comment.