Skip to content

Commit

Permalink
set ideal module
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangxiecrypto committed Oct 20, 2023
1 parent f532bc2 commit 6561413
Showing 5 changed files with 107 additions and 97 deletions.
93 changes: 0 additions & 93 deletions ot/mpz-ot-core/src/ferret/mod.rs
Original file line number Diff line number Diff line change
@@ -4,96 +4,3 @@ pub mod spcot;

/// Computational security parameter
pub const CSP: usize = 128;

pub mod ideal_cot {
//! Ideal functionality of COT.
use mpz_core::{prg::Prg, Block};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// The message that sender receives from the COT functionality.
pub struct CotMsgForSender {
/// The random blocks that sender receives from the COT functionality.
pub qs: Vec<Block>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// The message that receiver receives from the COT functionality.
pub struct CotMsgForReceiver {
/// The random bits that receiver receives from the COT functionality.
pub rs: Vec<bool>,
/// The chosen blocks that receiver receivers from the COT functionality.
pub ts: Vec<Block>,
}
#[allow(missing_docs)]
pub struct IdealCOT {
pub delta: Block,
pub counter: usize,
pub prg: Prg,
}

impl IdealCOT {
/// Initiate the functionality
pub fn init() -> Self {
let mut prg = Prg::new();
let delta = prg.random_block();
IdealCOT {
delta,
counter: 0,
prg,
}
}

/// Performs the extension with random choice bits.
///
/// # Argument
///
/// * `counter` - The number of COT to extend.
pub fn extend(&mut self, counter: usize) -> (CotMsgForSender, CotMsgForReceiver) {
let mut qs = vec![Block::ZERO; counter];
let mut rs = vec![false; counter];

self.prg.random_blocks(&mut qs);
self.prg.random_bools(&mut rs);

let ts: Vec<Block> = qs
.iter()
.zip(rs.iter())
.map(|(&q, &r)| if r { q ^ self.delta } else { q })
.collect();

self.counter += counter;
(CotMsgForSender { qs }, CotMsgForReceiver { rs, ts })
}

/// Perform the checks.
///
/// # Arguments
///
/// `sender_msg` - The message that the ideal COT sends to the sender.
/// `receiver_msg` - The message that the ideal COT sends to the receiver.
pub fn check(self, sender_msg: CotMsgForSender, receiver_msg: CotMsgForReceiver) -> bool {
let CotMsgForSender { qs } = sender_msg;
let CotMsgForReceiver { rs, ts } = receiver_msg;

qs.into_iter().zip(ts).zip(rs).all(
|((q, t), r)| {
if !r {
q == t
} else {
q == t ^ self.delta
}
},
)
}
}

#[test]
fn ideal_cot_test() {
let num = 100;
let mut ideal_cot = IdealCOT::init();
let (sender, receiver) = ideal_cot.extend(num);

assert!(ideal_cot.check(sender, receiver));
}
}
6 changes: 2 additions & 4 deletions ot/mpz-ot-core/src/ferret/spcot/mod.rs
Original file line number Diff line number Diff line change
@@ -10,10 +10,8 @@ mod tests {
use mpz_core::prg::Prg;

use super::{receiver::Receiver as SpcotReceiver, sender::Sender as SpcotSender};
use crate::ferret::{
ideal_cot::{CotMsgForReceiver, CotMsgForSender, IdealCOT},
CSP,
};
use crate::ferret::CSP;
use crate::ideal::ideal_cot::{CotMsgForReceiver, CotMsgForSender, IdealCOT};

#[test]
fn spcot_test() {
101 changes: 101 additions & 0 deletions ot/mpz-ot-core/src/ideal/ideal_cot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Define ideal functionality of COT with random choise bit.
use mpz_core::{prg::Prg, Block};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// The message that sender receives from the COT functionality.
pub struct CotMsgForSender {
/// The random blocks that sender receives from the COT functionality.
pub qs: Vec<Block>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// The message that receiver receives from the COT functionality.
pub struct CotMsgForReceiver {
/// The random bits that receiver receives from the COT functionality.
pub rs: Vec<bool>,
/// The chosen blocks that receiver receivers from the COT functionality.
pub ts: Vec<Block>,
}
#[allow(missing_docs)]
pub struct IdealCOT {
pub delta: Block,
pub counter: usize,
pub prg: Prg,
}

impl IdealCOT {
/// Initiate the functionality
pub fn init() -> Self {
let mut prg = Prg::new();
let delta = prg.random_block();
IdealCOT {
delta,
counter: 0,
prg,
}
}

/// Initiate with a given delta
pub fn init_with_delta(delta: Block) -> Self {
let prg = Prg::new();
IdealCOT {
delta,
counter: 0,
prg,
}
}

/// Performs the extension with random choice bits.
///
/// # Argument
///
/// * `counter` - The number of COT to extend.
pub fn extend(&mut self, counter: usize) -> (CotMsgForSender, CotMsgForReceiver) {
let mut qs = vec![Block::ZERO; counter];
let mut rs = vec![false; counter];

self.prg.random_blocks(&mut qs);
self.prg.random_bools(&mut rs);

let ts: Vec<Block> = qs
.iter()
.zip(rs.iter())
.map(|(&q, &r)| if r { q ^ self.delta } else { q })
.collect();

self.counter += counter;
(CotMsgForSender { qs }, CotMsgForReceiver { rs, ts })
}

/// Perform the checks.
///
/// # Arguments
///
/// `sender_msg` - The message that the ideal COT sends to the sender.
/// `receiver_msg` - The message that the ideal COT sends to the receiver.
pub fn check(self, sender_msg: CotMsgForSender, receiver_msg: CotMsgForReceiver) -> bool {
let CotMsgForSender { qs } = sender_msg;
let CotMsgForReceiver { rs, ts } = receiver_msg;

qs.into_iter().zip(ts).zip(rs).all(
|((q, t), r)| {
if !r {
q == t
} else {
q == t ^ self.delta
}
},
)
}
}

#[test]
fn ideal_cot_test() {
let num = 100;
let mut ideal_cot = IdealCOT::init();
let (sender, receiver) = ideal_cot.extend(num);

assert!(ideal_cot.check(sender, receiver));
}
3 changes: 3 additions & 0 deletions ot/mpz-ot-core/src/ideal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Define ideal functionalities of OT.
pub mod ideal_cot;
1 change: 1 addition & 0 deletions ot/mpz-ot-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -16,5 +16,6 @@

pub mod chou_orlandi;
pub mod ferret;
pub mod ideal;
pub mod kos;
pub mod msgs;

0 comments on commit 6561413

Please sign in to comment.