From 68ba8cdcf78ba4d1b6e38b039fb4b69bbacb5328 Mon Sep 17 00:00:00 2001 From: th4s Date: Fri, 19 Jul 2024 11:06:58 +0200 Subject: [PATCH] Simplify finite fields. --- 01-oblivious-transfer/src/lib.rs | 2 +- 02-finite-fields/src/alice.rs | 33 ++++++++++++++++---------------- 02-finite-fields/src/bob.rs | 33 ++++++++++++++++---------------- 02-finite-fields/src/lib.rs | 23 +++++++--------------- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/01-oblivious-transfer/src/lib.rs b/01-oblivious-transfer/src/lib.rs index 3f70e86..8ce1a85 100644 --- a/01-oblivious-transfer/src/lib.rs +++ b/01-oblivious-transfer/src/lib.rs @@ -2,7 +2,7 @@ //! be the OT receiver. //! //! We start again by opening a connection. To be able use the connection with out OT API you need -//! to wrap it in a [`mpz_common::executor::STExecutor`]. +//! to wrap it in a [`mpz_common::executor::STExecutor`], which will be the [`mpz_common::Context`]. //! //! Now either create an [`mpz_ot::OTSender`] or an [`mpz_ot::OTReceiver`] and set it up by calling //! [`mpz_ot::OTSetup::setup`]. You can use [`mpz_ot::chou_orlandi::Sender`] and diff --git a/02-finite-fields/src/alice.rs b/02-finite-fields/src/alice.rs index bfe91dc..0261b25 100644 --- a/02-finite-fields/src/alice.rs +++ b/02-finite-fields/src/alice.rs @@ -1,28 +1,31 @@ -use common::{tcp_mux, FramedUidMux, Role, DEFAULT_LOCAL}; +use common::{tcp_connect, Role, DEFAULT_LOCAL}; use finite_fields::setup_ot_sender; -use mpz_common::{executor::MTExecutor, Allocate, Preprocess}; +use mpz_common::{executor::STExecutor, Allocate, Context, Preprocess}; use mpz_fields::{p256::P256, Field}; use mpz_ole::rot::OLESender; use mpz_share_conversion::{ AdditiveToMultiplicative, MultiplicativeToAdditive, ShareConversionSender, }; -use serio::{stream::IoStreamExt, SinkExt}; +use serio::{ + codec::{Bincode, Codec}, + stream::IoStreamExt, + SinkExt, +}; #[tokio::main] async fn main() { - // Open connection and poll it in the background. - let (future, mut ctrl) = tcp_mux(Role::Alice, DEFAULT_LOCAL).await.unwrap(); - let join_handle = tokio::spawn(future); + // Open a connection. + let tcp = tcp_connect(Role::Alice, DEFAULT_LOCAL).await.unwrap(); + let channel = Bincode::default().new_framed(tcp); // Create an executor and setup OT. - let mut executor = MTExecutor::new(ctrl.clone(), 32); + let mut executor = STExecutor::new(channel); let ot_sender = setup_ot_sender(&mut executor).await.unwrap(); // Setup OLE and share conversion. - let mut context = executor.new_thread().await.unwrap(); let mut ole_sender = OLESender::<_, P256>::new(ot_sender); ole_sender.alloc(2); - ole_sender.preprocess(&mut context).await.unwrap(); + ole_sender.preprocess(&mut executor).await.unwrap(); let mut sender = ShareConversionSender::<_, P256>::new(ole_sender); @@ -31,19 +34,19 @@ async fn main() { // Perform the conversion. let factor = sender - .to_multiplicative(&mut context, vec![number]) + .to_multiplicative(&mut executor, vec![number]) .await .unwrap(); let summand = sender - .to_additive(&mut context, factor) + .to_additive(&mut executor, factor) .await .unwrap() .pop() .unwrap(); - // Open a channel and send/receive starting and final numbers. - let mut channel = ctrl.open_framed(b"1").await.unwrap(); + // Get the channel and send/receive starting and final numbers. + let channel = executor.io_mut(); channel.send(number).await.unwrap(); channel.send(summand).await.unwrap(); @@ -53,8 +56,4 @@ async fn main() { // Check that conversion worked correctly. println!("Original sum: {:?}", (number + number2).to_be_bytes()); println!("Final sum: {:?}", (summand + summand2).to_be_bytes()); - - // Properly close the connection. - ctrl.mux_mut().close(); - join_handle.await.unwrap().unwrap(); } diff --git a/02-finite-fields/src/bob.rs b/02-finite-fields/src/bob.rs index 81c652f..f53d610 100644 --- a/02-finite-fields/src/bob.rs +++ b/02-finite-fields/src/bob.rs @@ -1,28 +1,31 @@ -use common::{tcp_mux, FramedUidMux, Role, DEFAULT_LOCAL}; +use common::{tcp_connect, Role, DEFAULT_LOCAL}; use finite_fields::setup_ot_receiver; -use mpz_common::{executor::MTExecutor, Allocate, Preprocess}; +use mpz_common::{executor::STExecutor, Allocate, Context, Preprocess}; use mpz_fields::{p256::P256, Field}; use mpz_ole::rot::OLEReceiver; use mpz_share_conversion::{ AdditiveToMultiplicative, MultiplicativeToAdditive, ShareConversionReceiver, }; -use serio::{stream::IoStreamExt, SinkExt}; +use serio::{ + codec::{Bincode, Codec}, + stream::IoStreamExt, + SinkExt, +}; #[tokio::main] async fn main() { - // Open connection and poll it in the background. - let (future, mut ctrl) = tcp_mux(Role::Bob, DEFAULT_LOCAL).await.unwrap(); - let join_handle = tokio::spawn(future); + // Open a connection. + let tcp = tcp_connect(Role::Bob, DEFAULT_LOCAL).await.unwrap(); + let channel = Bincode::default().new_framed(tcp); // Create an executor and setup OT. - let mut executor = MTExecutor::new(ctrl.clone(), 32); + let mut executor = STExecutor::new(channel); let ot_receiver = setup_ot_receiver(&mut executor).await.unwrap(); // Setup OLE and share conversion. - let mut context = executor.new_thread().await.unwrap(); let mut ole_receiver = OLEReceiver::<_, P256>::new(ot_receiver); ole_receiver.alloc(2); - ole_receiver.preprocess(&mut context).await.unwrap(); + ole_receiver.preprocess(&mut executor).await.unwrap(); let mut receiver = ShareConversionReceiver::<_, P256>::new(ole_receiver); @@ -31,19 +34,19 @@ async fn main() { // Perform the conversion. let factor = receiver - .to_multiplicative(&mut context, vec![number]) + .to_multiplicative(&mut executor, vec![number]) .await .unwrap(); let summand = receiver - .to_additive(&mut context, factor) + .to_additive(&mut executor, factor) .await .unwrap() .pop() .unwrap(); - // Open a channel and send/receive starting and final numbers. - let mut channel = ctrl.open_framed(b"1").await.unwrap(); + // Get the channel and send/receive starting and final numbers. + let channel = executor.io_mut(); channel.send(number).await.unwrap(); channel.send(summand).await.unwrap(); @@ -53,8 +56,4 @@ async fn main() { // Check that conversion worked correctly. println!("Original sum: {:?}", (number + number2).to_be_bytes()); println!("Final sum: {:?}", (summand + summand2).to_be_bytes()); - - // Properly close the connection. - ctrl.mux_mut().close(); - join_handle.await.unwrap().unwrap(); } diff --git a/02-finite-fields/src/lib.rs b/02-finite-fields/src/lib.rs index 2cda985..75ce386 100644 --- a/02-finite-fields/src/lib.rs +++ b/02-finite-fields/src/lib.rs @@ -11,8 +11,7 @@ //! other. use anyhow::Error as Anyhow; -use common::MuxControl; -use mpz_common::executor::MTExecutor; +use mpz_common::Context; use mpz_ot::{ chou_orlandi::{ Receiver as BaseReceiver, ReceiverConfig as BaseReceiverConfig, Sender as BaseSender, @@ -26,10 +25,8 @@ use mpz_ot::{ /// /// # Arguments /// -/// * `executor` - An executor for creating contexts. -pub async fn setup_ot_sender( - executor: &mut MTExecutor, -) -> Result, Anyhow> { +/// * `context` - A context for IO. +pub async fn setup_ot_sender(context: &mut impl Context) -> Result, Anyhow> { // Create a base OT receiver. let base_receiver_config = BaseReceiverConfig::builder().build()?; let base_receiver = BaseReceiver::new(base_receiver_config); @@ -38,9 +35,7 @@ pub async fn setup_ot_sender( let sender_config = SenderConfig::builder().build()?; let mut sender = Sender::new(sender_config, base_receiver); - let mut context = executor.new_thread().await?; - - sender.setup(&mut context).await?; + sender.setup(context).await?; Ok(sender) } @@ -49,10 +44,8 @@ pub async fn setup_ot_sender( /// /// # Arguments /// -/// * `executor` - An executor for creating contexts. -pub async fn setup_ot_receiver( - executor: &mut MTExecutor, -) -> Result, Anyhow> { +/// * `context` - A context for IO. +pub async fn setup_ot_receiver(context: &mut impl Context) -> Result, Anyhow> { // Create a base OT sender. let base_sender_config = BaseSenderConfig::builder().build()?; let base_sender = BaseSender::new(base_sender_config); @@ -61,9 +54,7 @@ pub async fn setup_ot_receiver( let receiver_config = ReceiverConfig::builder().build()?; let mut receiver = Receiver::new(receiver_config, base_sender); - let mut context = executor.new_thread().await?; - - receiver.setup(&mut context).await?; + receiver.setup(context).await?; Ok(receiver) }