Skip to content

Commit

Permalink
Add test for MPZ Ferret implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kisakishy committed Jul 22, 2024
1 parent 05a62bd commit 34b8362
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ serde = { version = "1.0.195", features = ["derive"] }
tokio = { version = "1.35.1", features = ["full"] }
trait-variant = "0.1.1"
async-trait = "0.1.77"
futures = "0.3"
rstest = "0.12"

mpz-common = { git = "https://github.com/privacy-scaling-explorations/mpz.git", branch = "ferret-sinu", features = ["test-utils"] }
mpz-ot = { git = "https://github.com/privacy-scaling-explorations/mpz.git", branch = "ferret-sinu", features = ["ideal"] }
mpz-ot-core = { git = "https://github.com/privacy-scaling-explorations/mpz.git", branch = "ferret-sinu" }
mpz-core = { git = "https://github.com/privacy-scaling-explorations/mpz.git", branch = "ferret-sinu" }
103 changes: 102 additions & 1 deletion src/faand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use blake3::Hasher;
use rand::{random, Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
use serde::{Deserialize, Serialize};
use mpz_core::Block;

use crate::{
channel::{self, Channel, MsgChannel},
Expand Down Expand Up @@ -774,7 +775,7 @@ pub(crate) async fn combine_two_leaky_ands(
}

#[cfg(test)]
mod tests {
mod pre_tests {
use rand::random;

use crate::{
Expand Down Expand Up @@ -1189,3 +1190,103 @@ mod tests {
Ok(())
}
}

/// Transform Block to u128
pub fn block_to_u128(inp: Block) -> u128 {
u128::from_le_bytes(inp.to_bytes())
}

/// Transform u128 to Block
pub fn u128_to_block(inp: u128) -> Block {
Block::new(inp.to_le_bytes())
}

#[cfg(test)]
mod tests {
use crate::faand::block_to_u128;

use futures::TryFutureExt as _;
use mpz_core::lpn::LpnParameters;
use mpz_ot_core::{ferret::LpnType, test::assert_cot, RCOTReceiverOutput, RCOTSenderOutput};
use rstest::*;

use mpz_ot::{Correlation, OTError, RandomCOTReceiver, RandomCOTSender};
use mpz_ot::ideal::cot::ideal_rcot;
use mpz_ot::ferret::FerretConfig;
use mpz_ot::ferret::{Sender, Receiver};

use mpz_common::executor::test_st_executor;

// l = n - k = 8380
const LPN_PARAMETERS_TEST: LpnParameters = LpnParameters {
n: 9600,
k: 1220,
t: 600,
};

#[rstest]
#[case::uniform(LpnType::Uniform)]
#[case::regular(LpnType::Regular)]
#[tokio::test]
async fn test_ferret(#[case] lpn_type: LpnType) {
let (mut ctx_sender, mut ctx_receiver) = test_st_executor(8);

let (rcot_sender, rcot_receiver) = ideal_rcot();

let config = FerretConfig::new(LPN_PARAMETERS_TEST, lpn_type);

let mut sender = Sender::new(config.clone(), rcot_sender);
let mut receiver = Receiver::new(config, rcot_receiver);

tokio::try_join!(
sender.setup(&mut ctx_sender).map_err(OTError::from),
receiver.setup(&mut ctx_receiver).map_err(OTError::from)
)
.unwrap();

// extend once.
let count = LPN_PARAMETERS_TEST.k;
tokio::try_join!(
sender.extend(&mut ctx_sender, count).map_err(OTError::from),
receiver
.extend(&mut ctx_receiver, count)
.map_err(OTError::from)
)
.unwrap();

// extend twice
let count = 10;
tokio::try_join!(
sender.extend(&mut ctx_sender, count).map_err(OTError::from),
receiver
.extend(&mut ctx_receiver, count)
.map_err(OTError::from)
)
.unwrap();

let (
RCOTSenderOutput {
id: sender_id,
msgs: u,
},
RCOTReceiverOutput {
id: receiver_id,
choices: b,
msgs: w,
},
) = tokio::try_join!(
sender.send_random_correlated(&mut ctx_sender, count),
receiver.receive_random_correlated(&mut ctx_receiver, count)
)
.unwrap();

assert_eq!(sender_id, receiver_id);
assert_cot(sender.delta(), &b, &u, &w);

for i in 0..count {
println!("b {:?}", b[i]);
println!("u {:?}", block_to_u128(u[i]));
println!("w {:?}", block_to_u128(w[i]));
}
}
}

0 comments on commit 34b8362

Please sign in to comment.