Skip to content

Commit

Permalink
add ferret with io
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangxiecrypto committed Jun 28, 2024
1 parent b8ae7ac commit 40aa96a
Show file tree
Hide file tree
Showing 27 changed files with 2,126 additions and 256 deletions.
4 changes: 2 additions & 2 deletions crates/mpz-common/src/ideal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Buffer {
}

/// The ideal functionality from the perspective of Alice.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Alice<F> {
f: Arc<Mutex<F>>,
buffer: Arc<Mutex<Buffer>>,
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<F> Alice<F> {
}

/// The ideal functionality from the perspective of Bob.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Bob<F> {
f: Arc<Mutex<F>>,
buffer: Arc<Mutex<Buffer>>,
Expand Down
52 changes: 27 additions & 25 deletions crates/mpz-core/src/ggm_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,35 @@ impl GgmTree {
assert_eq!(k0.len(), self.depth);
assert_eq!(k1.len(), self.depth);
let mut buf = [Block::ZERO; 8];
self.tkprp.expand_1to2(tree, seed);
k0[0] = tree[0];
k1[0] = tree[1];
if self.depth > 1 {
self.tkprp.expand_1to2(tree, seed);
k0[0] = tree[0];
k1[0] = tree[1];

self.tkprp.expand_2to4(&mut buf, tree);
k0[1] = buf[0] ^ buf[2];
k1[1] = buf[1] ^ buf[3];
tree[0..4].copy_from_slice(&buf[0..4]);

for h in 2..self.depth {
k0[h] = Block::ZERO;
k1[h] = Block::ZERO;

// How many nodes there are in this layer
let sz = 1 << h;
for i in (0..=sz - 4).rev().step_by(4) {
self.tkprp.expand_4to8(&mut buf, &tree[i..]);
k0[h] ^= buf[0];
k0[h] ^= buf[2];
k0[h] ^= buf[4];
k0[h] ^= buf[6];
k1[h] ^= buf[1];
k1[h] ^= buf[3];
k1[h] ^= buf[5];
k1[h] ^= buf[7];
self.tkprp.expand_2to4(&mut buf, tree);
k0[1] = buf[0] ^ buf[2];
k1[1] = buf[1] ^ buf[3];
tree[0..4].copy_from_slice(&buf[0..4]);

tree[2 * i..2 * i + 8].copy_from_slice(&buf);
for h in 2..self.depth {
k0[h] = Block::ZERO;
k1[h] = Block::ZERO;

// How many nodes there are in this layer
let sz = 1 << h;
for i in (0..=sz - 4).rev().step_by(4) {
self.tkprp.expand_4to8(&mut buf, &tree[i..]);
k0[h] ^= buf[0];
k0[h] ^= buf[2];
k0[h] ^= buf[4];
k0[h] ^= buf[6];
k1[h] ^= buf[1];
k1[h] ^= buf[3];
k1[h] ^= buf[5];
k1[h] ^= buf[7];

tree[2 * i..2 * i + 8].copy_from_slice(&buf);
}
}
}
}
Expand Down
19 changes: 4 additions & 15 deletions crates/mpz-ot-core/src/ferret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,26 @@ pub const LPN_PARAMETERS_UNIFORM: LpnParameters = LpnParameters {
};

/// The type of Lpn parameters.
#[derive(Debug)]
#[derive(Debug, Clone, Copy, Default)]
pub enum LpnType {
/// Uniform error distribution.
Uniform,
/// Regular error distribution.
#[default]
Regular,
}

#[cfg(test)]
mod tests {
use super::*;

use msgs::LpnMatrixSeed;
use receiver::Receiver;
use sender::Sender;

use crate::ideal::{cot::IdealCOT, mpcot::IdealMpcot};
use crate::test::assert_cot;
use crate::{MPCOTReceiverOutput, MPCOTSenderOutput, RCOTReceiverOutput, RCOTSenderOutput};
use mpz_core::{lpn::LpnParameters, prg::Prg};
use rand::SeedableRng;

const LPN_PARAMETERS_TEST: LpnParameters = LpnParameters {
n: 9600,
Expand All @@ -66,7 +65,7 @@ mod tests {

#[test]
fn ferret_test() {
let mut prg = Prg::from_seed([1u8; 16].into());
let mut prg = Prg::new();
let delta = prg.random_block();
let mut ideal_cot = IdealCOT::default();
let mut ideal_mpcot = IdealMpcot::default();
Expand Down Expand Up @@ -101,18 +100,8 @@ mod tests {
)
.unwrap();

let LpnMatrixSeed {
seed: lpn_matrix_seed,
} = seed;

let mut sender = sender
.setup(
delta,
LPN_PARAMETERS_TEST,
LpnType::Regular,
lpn_matrix_seed,
&v,
)
.setup(delta, LPN_PARAMETERS_TEST, LpnType::Regular, seed, &v)
.unwrap();

// extend once
Expand Down
5 changes: 2 additions & 3 deletions crates/mpz-ot-core/src/ferret/mpcot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ mod tests {
use crate::ideal::spcot::IdealSpcot;
use crate::{SPCOTReceiverOutput, SPCOTSenderOutput};
use mpz_core::prg::Prg;
use rand::SeedableRng;

#[test]
fn mpcot_general_test() {
let mut prg = Prg::from_seed([1u8; 16].into());
let mut prg = Prg::new();
let delta = prg.random_block();
let mut ideal_spcot = IdealSpcot::new_with_delta(delta);

Expand Down Expand Up @@ -96,7 +95,7 @@ mod tests {

#[test]
fn mpcot_regular_test() {
let mut prg = Prg::from_seed([2u8; 16].into());
let mut prg = Prg::new();
let delta = prg.random_block();
let mut ideal_spcot = IdealSpcot::new_with_delta(delta);

Expand Down
30 changes: 15 additions & 15 deletions crates/mpz-ot-core/src/ferret/mpcot/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl Receiver {
/// # Argument
///
/// * `hash_seed` - Random seed to generate hashes, will be sent to the sender.
pub fn setup(self, hash_seed: Block) -> (Receiver<state::PreExtension>, HashSeed) {
pub fn setup(self, hash_seed: Block) -> (Receiver<state::Extension>, HashSeed) {
let mut prg = Prg::from_seed(hash_seed);
let hashes = std::array::from_fn(|_| AesEncryptor::new(prg.random_block()));
let recv = Receiver {
state: state::PreExtension {
state: state::Extension {
counter: 0,
hashes: Arc::new(hashes),
},
Expand All @@ -48,7 +48,7 @@ impl Receiver {
}
}

impl Receiver<state::PreExtension> {
impl Receiver<state::Extension> {
/// Performs the hash procedure in MPCOT extension.
/// Outputs the length of each bucket plus 1.
///
Expand All @@ -63,7 +63,7 @@ impl Receiver<state::PreExtension> {
self,
alphas: &[u32],
n: u32,
) -> Result<(Receiver<state::Extension>, Vec<(usize, u32)>), ReceiverError> {
) -> Result<(Receiver<state::ExtensionInternal>, Vec<(usize, u32)>), ReceiverError> {
if alphas.len() as u32 > n {
return Err(ReceiverError::InvalidInput(
"length of alphas should not exceed n".to_string(),
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Receiver<state::PreExtension> {
}

let receiver = Receiver {
state: state::Extension {
state: state::ExtensionInternal {
counter: self.state.counter,
m,
n,
Expand All @@ -117,7 +117,7 @@ impl Receiver<state::PreExtension> {
Ok((receiver, p))
}
}
impl Receiver<state::Extension> {
impl Receiver<state::ExtensionInternal> {
/// Performs MPCOT extension.
///
/// See Step 5 in Figure 7.
Expand All @@ -128,7 +128,7 @@ impl Receiver<state::Extension> {
pub fn extend(
self,
rt: &[Vec<Block>],
) -> Result<(Receiver<state::PreExtension>, Vec<Block>), ReceiverError> {
) -> Result<(Receiver<state::Extension>, Vec<Block>), ReceiverError> {
if rt.len() != self.state.m {
return Err(ReceiverError::InvalidInput(
"the length rt should be m".to_string(),
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Receiver<state::Extension> {
}

let receiver = Receiver {
state: state::PreExtension {
state: state::Extension {
counter: self.state.counter + 1,
hashes: self.state.hashes,
},
Expand All @@ -182,8 +182,8 @@ pub mod state {
pub trait Sealed {}

impl Sealed for super::Initialized {}
impl Sealed for super::PreExtension {}
impl Sealed for super::Extension {}
impl Sealed for super::ExtensionInternal {}
}

/// The receiver's state.
Expand All @@ -200,20 +200,20 @@ pub mod state {
/// The receiver's state before extending.
///
/// In this state the receiver performs pre extension in MPCOT (potentially multiple times).
pub struct PreExtension {
pub struct Extension {
/// Current MPCOT counter
pub(super) counter: usize,
/// The hashes to generate Cuckoo hash table.
pub(super) hashes: Arc<[AesEncryptor; CUCKOO_HASH_NUM]>,
}

impl State for PreExtension {}
impl State for Extension {}

opaque_debug::implement!(PreExtension);
opaque_debug::implement!(Extension);
/// The receiver's state of extension.
///
/// In this state the receiver performs MPCOT extension (potentially multiple times).
pub struct Extension {
pub struct ExtensionInternal {
/// Current MPCOT counter
pub(super) counter: usize,
/// Current length of Cuckoo hash table, will possibly be changed in each extension.
Expand All @@ -228,7 +228,7 @@ pub mod state {
pub(super) buckets_length: Vec<usize>,
}

impl State for Extension {}
impl State for ExtensionInternal {}

opaque_debug::implement!(Extension);
opaque_debug::implement!(ExtensionInternal);
}
30 changes: 15 additions & 15 deletions crates/mpz-ot-core/src/ferret/mpcot/receiver_regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ impl Receiver {
}

/// Completes the setup phase of the protocol.
pub fn setup(self) -> Receiver<state::PreExtension> {
pub fn setup(self) -> Receiver<state::Extension> {
Receiver {
state: state::PreExtension { counter: 0 },
state: state::Extension { counter: 0 },
}
}
}
impl Receiver<state::PreExtension> {
impl Receiver<state::Extension> {
/// Performs the prepare procedure in MPCOT extension.
/// Outputs the indices for SPCOT.
///
Expand All @@ -38,7 +38,7 @@ impl Receiver<state::PreExtension> {
self,
alphas: &[u32],
n: u32,
) -> Result<(Receiver<state::Extension>, Vec<(usize, u32)>), ReceiverError> {
) -> Result<(Receiver<state::ExtensionInternal>, Vec<(usize, u32)>), ReceiverError> {
let t = alphas.len() as u32;
if t > n {
return Err(ReceiverError::InvalidInput(
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Receiver<state::PreExtension> {
.collect();

let receiver = Receiver {
state: state::Extension {
state: state::ExtensionInternal {
counter: self.state.counter,
n,
queries_length,
Expand All @@ -103,7 +103,7 @@ impl Receiver<state::PreExtension> {
}
}

impl Receiver<state::Extension> {
impl Receiver<state::ExtensionInternal> {
/// Performs MPCOT extension.
///
/// # Arguments.
Expand All @@ -112,7 +112,7 @@ impl Receiver<state::Extension> {
pub fn extend(
self,
rt: &[Vec<Block>],
) -> Result<(Receiver<state::PreExtension>, Vec<Block>), ReceiverError> {
) -> Result<(Receiver<state::Extension>, Vec<Block>), ReceiverError> {
if rt
.iter()
.zip(self.state.queries_depth.iter())
Expand All @@ -130,7 +130,7 @@ impl Receiver<state::Extension> {
}

let receiver = Receiver {
state: state::PreExtension {
state: state::Extension {
counter: self.state.counter + 1,
},
};
Expand All @@ -145,8 +145,8 @@ pub mod state {
pub trait Sealed {}

impl Sealed for super::Initialized {}
impl Sealed for super::PreExtension {}
impl Sealed for super::Extension {}
impl Sealed for super::ExtensionInternal {}
}

/// The receiver's state.
Expand All @@ -162,19 +162,19 @@ pub mod state {
/// The receiver's state before extending.
///
/// In this state the receiver performs pre extension in MPCOT (potentially multiple times).
pub struct PreExtension {
pub struct Extension {
/// Current MPCOT counter
pub(super) counter: usize,
}

impl State for PreExtension {}
impl State for Extension {}

opaque_debug::implement!(PreExtension);
opaque_debug::implement!(Extension);

/// The receiver's state after the setup phase.
///
/// In this state the receiver performs MPCOT extension (potentially multiple times).
pub struct Extension {
pub struct ExtensionInternal {
/// Current MPCOT counter
#[allow(dead_code)]
pub(super) counter: usize,
Expand All @@ -186,7 +186,7 @@ pub mod state {
pub(super) queries_depth: Vec<usize>,
}

impl State for Extension {}
impl State for ExtensionInternal {}

opaque_debug::implement!(Extension);
opaque_debug::implement!(ExtensionInternal);
}
Loading

0 comments on commit 40aa96a

Please sign in to comment.