-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> * Add feedback * Add more feedback --------- Co-authored-by: sinu.eth <[email protected]>
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<bool>, | ||
/// The chosen blocks that receiver receives from the ROT functionality. | ||
pub ts: Vec<Block>, | ||
} | ||
|
||
/// 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<Block> = 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |