From 61275e80389f45d5ee24d6fd1d0679bd654e12d5 Mon Sep 17 00:00:00 2001 From: th4s Date: Thu, 7 Mar 2024 18:15:00 +0100 Subject: [PATCH] Adds an ideal ROT functionality to mpz-ot-core (#102) * Adds an ideal ROT functionality to mpz-ot-core * Update ot/mpz-ot-core/src/ideal/ideal_rot.rs Derive Debug for IdealROT Co-authored-by: sinu.eth <65924192+sinui0@users.noreply.github.com> * Add feedback * Add more feedback --------- Co-authored-by: sinu.eth <65924192+sinui0@users.noreply.github.com> --- crates/mpz-core/src/prg.rs | 2 + crates/mpz-ot-core/src/ideal/ideal_rot.rs | 89 +++++++++++++++++++++++ crates/mpz-ot-core/src/ideal/mod.rs | 1 + 3 files changed, 92 insertions(+) create mode 100644 crates/mpz-ot-core/src/ideal/ideal_rot.rs diff --git a/crates/mpz-core/src/prg.rs b/crates/mpz-core/src/prg.rs index 9d62587b..d49f8d03 100644 --- a/crates/mpz-core/src/prg.rs +++ b/crates/mpz-core/src/prg.rs @@ -71,6 +71,8 @@ impl CryptoRng for PrgCore {} #[derive(Clone)] pub struct Prg(BlockRng); +opaque_debug::implement!(Prg); + impl RngCore for Prg { #[inline(always)] fn next_u32(&mut self) -> u32 { diff --git a/crates/mpz-ot-core/src/ideal/ideal_rot.rs b/crates/mpz-ot-core/src/ideal/ideal_rot.rs new file mode 100644 index 00000000..a5f2dfb8 --- /dev/null +++ b/crates/mpz-ot-core/src/ideal/ideal_rot.rs @@ -0,0 +1,89 @@ +//! Define ideal functionality of ROT with random choice bit. + +use mpz_core::{prg::Prg, Block}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +/// The message that sender receives from the ROT functionality. +pub struct RotMsgForSender { + /// The random blocks that sender receives from the ROT functionality. + pub qs: Vec<[Block; 2]>, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +/// The message that receiver receives from the ROT functionality. +pub struct RotMsgForReceiver { + /// The random bits that receiver receives from the ROT functionality. + pub rs: Vec, + /// The chosen blocks that receiver receives from the ROT functionality. + pub ts: Vec, +} + +/// An ideal functionality for random OT +#[derive(Debug)] +pub struct IdealROT { + counter: usize, + prg: Prg, +} + +impl IdealROT { + /// Initiate the functionality + pub fn new() -> Self { + let prg = Prg::new(); + IdealROT { counter: 0, prg } + } + + /// Performs the extension with random choice bits. + /// + /// # Argument + /// + /// * `counter` - The number of ROT to extend. + pub fn extend(&mut self, counter: usize) -> (RotMsgForSender, RotMsgForReceiver) { + let mut qs1 = vec![Block::ZERO; counter]; + let mut qs2 = vec![Block::ZERO; counter]; + + self.prg.random_blocks(&mut qs1); + self.prg.random_blocks(&mut qs2); + + let qs: Vec<[Block; 2]> = qs1.iter().zip(qs2).map(|(&q1, q2)| [q1, q2]).collect(); + + let mut rs = vec![false; counter]; + + self.prg.random_bools(&mut rs); + + let ts: Vec = qs + .iter() + .zip(rs.iter()) + .map(|(&q, &r)| q[r as usize]) + .collect(); + + self.counter += counter; + (RotMsgForSender { qs }, RotMsgForReceiver { rs, ts }) + } +} + +impl Default for IdealROT { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::{IdealROT, RotMsgForReceiver}; + + #[test] + fn ideal_rot_test() { + let num = 100; + let mut ideal_rot = IdealROT::new(); + let (sender, receiver) = ideal_rot.extend(num); + + let qs = sender.qs; + let RotMsgForReceiver { rs, ts } = receiver; + + qs.iter() + .zip(ts) + .zip(rs) + .for_each(|((q, t), r)| assert_eq!(q[r as usize], t)); + } +} diff --git a/crates/mpz-ot-core/src/ideal/mod.rs b/crates/mpz-ot-core/src/ideal/mod.rs index 1fb56072..ed22897e 100644 --- a/crates/mpz-ot-core/src/ideal/mod.rs +++ b/crates/mpz-ot-core/src/ideal/mod.rs @@ -1,4 +1,5 @@ //! Define ideal functionalities of OTs. pub mod ideal_cot; pub mod ideal_mpcot; +pub mod ideal_rot; pub mod ideal_spcot;