Skip to content

Commit

Permalink
Use itybity (#21)
Browse files Browse the repository at this point in the history
* update mpz-circuits

* update mpz-garble-core

* update mpz-garble

* update mpz-core

* update mpz-ot-core

* update mpz-share-conversion-core

* impl GetBit for Block

* add itybity to workspace

* use FromBits for array
  • Loading branch information
sinui0 authored Jun 19, 2023
1 parent 61c32c9 commit 5a66465
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 166 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ lazy_static = "1"
derive_builder = "0.11"
once_cell = "1"
generic-array = "0.14"
itybity = "0.1"
2 changes: 1 addition & 1 deletion garble/mpz-garble-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ lto = true
[dependencies]
mpz-core.workspace = true
mpz-circuits.workspace = true
tlsn-utils.workspace = true

aes = { workspace = true, features = [] }
cipher.workspace = true
Expand All @@ -27,6 +26,7 @@ serde = { workspace = true, features = ["derive"] }
serde_arrays.workspace = true
thiserror.workspace = true
derive_builder.workspace = true
itybity.workspace = true

[dev-dependencies]
rstest.workspace = true
Expand Down
12 changes: 6 additions & 6 deletions garble/mpz-garble-core/src/encoding/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itybity::{FromBits, ToBits};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use std::ops::BitXor;
use utils::bits::{FromBits, ToBitsIter};

use mpz_circuits::types::{StaticValueType, TypeError, Value, ValueType};
use mpz_core::{hash::DomainSeparatedHash, impl_domain_separated_hash, Block};
Expand All @@ -23,7 +23,7 @@ pub enum ValueError {
}

/// A trait for encoding values.
pub trait Encode: ToBitsIter {
pub trait Encode {
/// The encoded value type.
type Encoded;

Expand Down Expand Up @@ -320,7 +320,7 @@ macro_rules! define_encoded_variant {

/// Returns the active encoding of the plaintext value
pub(crate) fn select(&self, value: $PlaintextTy) -> $EncodedTy<state::Active> {
let mut bits = value.into_lsb0_iter();
let mut bits = value.iter_lsb0();
let delta = self.0.delta();
$EncodedTy::<state::Active>::new(self.0.labels.map(|label| {
if bits.next().expect("bit length should match") {
Expand Down Expand Up @@ -559,7 +559,7 @@ macro_rules! define_decoding_info_variant {
impl $value<state::Active> {
/// Recovers the full encoding of this value using the decoding information and delta.
pub(crate) fn recover(&self, decoding: &$name, delta: Delta) -> $value<state::Full> {
let mut decoding = decoding.0.into_lsb0_iter();
let mut decoding = decoding.0.iter_lsb0();
$value::<state::Full>::new(
delta,
self.0.labels.map(|label| {
Expand All @@ -578,7 +578,7 @@ macro_rules! define_decoding_info_variant {
value: $ty,
delta: Delta,
) -> $value<state::Full> {
let mut value = value.into_lsb0_iter();
let mut value = value.iter_lsb0();
$value::<state::Full>::new(
delta,
self.0.labels.map(|label| {
Expand All @@ -596,7 +596,7 @@ macro_rules! define_decoding_info_variant {
<$ty>::from_lsb0(
self.0
.iter()
.zip(decoding.0.into_lsb0_iter())
.zip(decoding.0.iter_lsb0())
.map(|(label, dec)| label.pointer_bit() ^ dec),
)
.into()
Expand Down
1 change: 1 addition & 0 deletions garble/mpz-garble/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ thiserror.workspace = true
aes = { workspace = true }
rayon = { workspace = true }
derive_builder.workspace = true
itybity.workspace = true

[dev-dependencies]
mpz-ot = { workspace = true, features = ["mock"] }
Expand Down
4 changes: 2 additions & 2 deletions garble/mpz-garble/src/ot.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Traits for transferring encodings via oblivious transfer.
use async_trait::async_trait;
use itybity::IntoBits;
use mpz_circuits::types::Value;
use mpz_core::Block;
use mpz_garble_core::{encoding_state, EncodedValue, Label};
use utils::bits::ToBitsIter;

/// A trait for sending encodings via oblivious transfer.
#[async_trait]
Expand Down Expand Up @@ -61,7 +61,7 @@ where
id,
choice
.iter()
.flat_map(|value| value.clone().into_lsb0_iter())
.flat_map(|value| value.clone().into_iter_lsb0())
.collect(),
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion mpz-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ aes = []
sha2 = ["dep:sha2"]

[dependencies]
tlsn-utils.workspace = true
mpz-circuits-macros = { path = "../mpz-circuits-macros" }

sha2 = { workspace = true, features = ["compress"], optional = true }
Expand All @@ -27,6 +26,7 @@ rand.workspace = true
regex = { workspace = true, optional = true }
once_cell.workspace = true
thiserror.workspace = true
itybity.workspace = true

[dev-dependencies]
aes.workspace = true
2 changes: 1 addition & 1 deletion mpz-circuits/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl BuilderState {
let one = self.get_const_one();

let nodes: Vec<_> = value
.into_lsb0_iter()
.into_iter_lsb0()
.map(|bit| if bit { one } else { zero })
.collect();

Expand Down
4 changes: 2 additions & 2 deletions mpz-circuits/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use utils::bits::ToBits;
use itybity::IntoBits;

use crate::{
components::Gate,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Circuit {
})?;
}

for (node, bit) in input.iter().zip(value.clone().into_lsb0()) {
for (node, bit) in input.iter().zip(value.clone().into_iter_lsb0()) {
feeds[node.id] = Some(bit);
}
}
Expand Down
4 changes: 2 additions & 2 deletions mpz-circuits/src/circuits/big_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::cell::RefCell;

use utils::bits::ToBitsIter;
use itybity::IntoBits;

use crate::{
ops::binary::{switch_nbit, wrapping_add_nbit, wrapping_sub_nbit},
Expand Down Expand Up @@ -47,7 +47,7 @@ pub fn nbyte_add_mod_trace<'a, const N: usize>(
.collect::<Vec<_>>();
let mut modulus_bits = modulus
.into_iter()
.flat_map(|m| m.into_lsb0_iter())
.flat_map(|m| m.into_iter_lsb0())
.map(|bit| Node::new(bit as usize))
.collect::<Vec<_>>();

Expand Down
82 changes: 25 additions & 57 deletions mpz-circuits/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::{
};

use crate::components::{Feed, Node};
use itybity::{FromBits, IntoBits};
use rand::Rng;
use utils::bits::{FromBits, ToBits, ToBitsIter};

/// An error related to binary type conversions.
#[derive(Debug, thiserror::Error)]
Expand All @@ -24,7 +24,7 @@ pub enum TypeError {

/// A type that can be represented in binary form.
#[allow(clippy::len_without_is_empty)]
pub trait ToBinaryRepr: ToBitsIter + Into<Value> {
pub trait ToBinaryRepr: IntoBits + Into<Value> {
/// The binary representation of the type.
type Repr: Clone + Into<BinaryRepr>;

Expand Down Expand Up @@ -573,66 +573,34 @@ impl Value {
}
}

impl ToBitsIter for Value {
type Lsb0Iter = Box<dyn Iterator<Item = bool> + Send + 'static>;
type Msb0Iter = Box<dyn Iterator<Item = bool> + Send + 'static>;
impl IntoBits for Value {
type IterLsb0 = std::vec::IntoIter<bool>;
type IterMsb0 = std::vec::IntoIter<bool>;

fn into_lsb0_iter(self) -> Self::Lsb0Iter {
fn into_iter_lsb0(self) -> Self::IterLsb0 {
match self {
Value::Bit(v) => Box::new(std::iter::once(v)),
Value::U8(v) => Box::new(v.into_lsb0_iter()),
Value::U16(v) => Box::new(v.into_lsb0_iter()),
Value::U32(v) => Box::new(v.into_lsb0_iter()),
Value::U64(v) => Box::new(v.into_lsb0_iter()),
Value::U128(v) => Box::new(v.into_lsb0_iter()),
Value::Array(v) => Box::new(v.into_iter().flat_map(|v| v.into_lsb0_iter())),
}
}

fn into_msb0_iter(self) -> Self::Msb0Iter {
match self {
Value::Bit(v) => Box::new(std::iter::once(v)),
Value::U8(v) => Box::new(v.into_msb0_iter()),
Value::U16(v) => Box::new(v.into_msb0_iter()),
Value::U32(v) => Box::new(v.into_msb0_iter()),
Value::U64(v) => Box::new(v.into_msb0_iter()),
Value::U128(v) => Box::new(v.into_msb0_iter()),
Value::Array(v) => Box::new(v.into_iter().flat_map(|v| v.into_msb0_iter())),
}
Value::Bit(v) => v.into_lsb0_vec(),
Value::U8(v) => v.into_lsb0_vec(),
Value::U16(v) => v.into_lsb0_vec(),
Value::U32(v) => v.into_lsb0_vec(),
Value::U64(v) => v.into_lsb0_vec(),
Value::U128(v) => v.into_lsb0_vec(),
Value::Array(v) => v.into_iter().flat_map(|v| v.into_iter_lsb0()).collect(),
}
.into_iter()
}
}

impl ToBits for Value {
fn into_lsb0(self) -> Vec<bool> {
fn into_iter_msb0(self) -> Self::IterMsb0 {
match self {
Value::Bit(v) => vec![v],
Value::U8(v) => v.into_lsb0(),
Value::U16(v) => v.into_lsb0(),
Value::U32(v) => v.into_lsb0(),
Value::U64(v) => v.into_lsb0(),
Value::U128(v) => v.into_lsb0(),
Value::Array(v) => v.into_iter().flat_map(|v| v.into_lsb0()).collect(),
}
}

fn into_lsb0_boxed(self: Box<Self>) -> Vec<bool> {
self.into_lsb0()
}

fn into_msb0(self) -> Vec<bool> {
match self {
Value::Bit(v) => vec![v],
Value::U8(v) => v.into_msb0(),
Value::U16(v) => v.into_msb0(),
Value::U32(v) => v.into_msb0(),
Value::U64(v) => v.into_msb0(),
Value::U128(v) => v.into_msb0(),
Value::Array(v) => v.into_iter().flat_map(|v| v.into_msb0()).collect(),
}
}

fn into_msb0_boxed(self: Box<Self>) -> Vec<bool> {
self.into_msb0()
Value::Bit(v) => v.into_msb0_vec(),
Value::U8(v) => v.into_msb0_vec(),
Value::U16(v) => v.into_msb0_vec(),
Value::U32(v) => v.into_msb0_vec(),
Value::U64(v) => v.into_msb0_vec(),
Value::U128(v) => v.into_msb0_vec(),
Value::Array(v) => v.into_iter().flat_map(|v| v.into_iter_msb0()).collect(),
}
.into_iter()
}
}

Expand Down
4 changes: 1 addition & 3 deletions mpz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ name = "mpz_core"
default = []

[dependencies]
tlsn-utils.workspace = true

aes = { workspace = true, features = [] }
cipher.workspace = true
blake3.workspace = true
rand.workspace = true
serde = { workspace = true, features = ["derive"] }
thiserror.workspace = true
once_cell.workspace = true

itybity.workspace = true
bcs = "0.1.5"

[dev-dependencies]
Expand Down
20 changes: 11 additions & 9 deletions mpz-core/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use aes::BlockDecrypt;
use cipher::{consts::U16, generic_array::GenericArray, BlockCipher, BlockEncrypt};
use core::ops::{BitAnd, BitXor};
use itybity::{BitLength, GetBit, Lsb0, Msb0};
use rand::{CryptoRng, Rng};
use serde::{Deserialize, Serialize};
use std::convert::{From, TryInto};
use utils::bits::ToBitsIter;

/// A block of 128 bits
#[repr(transparent)]
Expand Down Expand Up @@ -151,17 +151,19 @@ pub trait BlockSerialize {
fn from_blocks(blocks: Self::Serialized) -> Self;
}

impl ToBitsIter for Block {
type Lsb0Iter = <u128 as ToBitsIter>::Lsb0Iter;

type Msb0Iter = <u128 as ToBitsIter>::Msb0Iter;
impl BitLength for Block {
const BITS: usize = 128;
}

fn into_lsb0_iter(self) -> Self::Lsb0Iter {
self.0.into_lsb0_iter()
impl GetBit<Lsb0> for Block {
fn get_bit(&self, index: usize) -> bool {
GetBit::<Lsb0>::get_bit(&self.0, index)
}
}

fn into_msb0_iter(self) -> Self::Msb0Iter {
self.0.into_msb0_iter()
impl GetBit<Msb0> for Block {
fn get_bit(&self, index: usize) -> bool {
GetBit::<Msb0>::get_bit(&self.0, index)
}
}

Expand Down
4 changes: 3 additions & 1 deletion ot/mpz-ot-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ name = "mpz_ot_core"

[dependencies]
mpz-core.workspace = true
tlsn-utils.workspace = true
clmul.workspace = true
matrix-transpose.workspace = true

tlsn-utils.workspace = true

aes.workspace = true
blake3.workspace = true
cipher.workspace = true
Expand All @@ -23,6 +24,7 @@ serde = { workspace = true, features = ["derive"] }
thiserror.workspace = true
derive_builder.workspace = true
merlin.workspace = true
itybity.workspace = true

[dev-dependencies]
rstest.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions ot/mpz-ot-core/benches/ot.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use itybity::IntoBits;
use mpz_core::Block;
use mpz_ot_core::{DhOtReceiver, DhOtSender, Kos15Receiver, Kos15Sender};
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha12Rng;
use utils::bits::IterToBits;

fn base_ot(c: &mut Criterion) {
let mut group = c.benchmark_group("base_ot");
Expand All @@ -13,7 +13,7 @@ fn base_ot(c: &mut Criterion) {
let mut rng = ChaCha12Rng::from_entropy();
let mut choice = vec![0u8; n / 8];
rng.fill_bytes(&mut choice);
let choice = choice.into_msb0();
let choice = choice.into_msb0_vec();
b.iter(|| {
let mut sender = DhOtSender::default();
let sender_setup = sender.setup(&mut rng).unwrap();
Expand All @@ -36,7 +36,7 @@ fn ext_ot(c: &mut Criterion) {
let mut rng = ChaCha12Rng::from_entropy();
let mut choice = vec![0u8; n / 8];
rng.fill_bytes(&mut choice);
let choice = choice.into_msb0();
let choice = choice.into_msb0_vec();
b.iter(|| {
let receiver = Kos15Receiver::default();
let (receiver, base_sender_setup) = receiver.base_setup().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions ot/mpz-ot-core/src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod tests {
use rstest::*;

pub mod fixtures {
use utils::bits::IterToBits;
use itybity::IntoBits;

use super::*;

Expand All @@ -48,7 +48,7 @@ pub mod tests {
pub fn choice() -> Vec<bool> {
let mut choice = vec![0u8; 16];
thread_rng().fill_bytes(&mut choice);
choice.into_msb0()
choice.into_msb0_vec()
}

#[fixture]
Expand Down
Loading

0 comments on commit 5a66465

Please sign in to comment.