Skip to content

Commit

Permalink
Improve code quality for mpz-ole-core.
Browse files Browse the repository at this point in the history
  • Loading branch information
th4s committed Feb 21, 2024
1 parent bba54c1 commit 3af76ed
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 69 deletions.
3 changes: 0 additions & 3 deletions ole/mpz-ole-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ thiserror.workspace = true
mpz-share-conversion-core.workspace = true
mpz-core.workspace = true
mpz-ot-core.workspace = true

[dev-dependencies]
rand_chacha.workspace = true
4 changes: 2 additions & 2 deletions ole/mpz-ole-core/src/ideal/ole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ impl<F: Field> Default for OLEFunctionality<F> {
#[cfg(test)]
mod tests {
use super::OLEFunctionality;
use mpz_core::{prg::Prg, Block};
use mpz_share_conversion_core::fields::{p256::P256, UniformRand};
use rand::SeedableRng;
use rand_chacha::ChaCha12Rng;

#[test]
fn test_ole_functionality() {
let count = 12;
let mut ole: OLEFunctionality<P256> = OLEFunctionality::default();
let mut rng = ChaCha12Rng::from_seed([0_u8; 32]);
let mut rng = Prg::from_seed(Block::ZERO);

let ak: Vec<P256> = (0..count).map(|_| P256::rand(&mut rng)).collect();
let bk: Vec<P256> = (0..count).map(|_| P256::rand(&mut rng)).collect();
Expand Down
2 changes: 1 addition & 1 deletion ole/mpz-ole-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod role;

#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
/// An error for what can go wrong with OLE
/// An error for what can go wrong with OLE.
pub enum OLECoreError {
#[error("{0}")]
LengthMismatch(String),
Expand Down
36 changes: 15 additions & 21 deletions ole/mpz-ole-core/src/ole/role/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/// An evaluator for OLE with errors
use crate::OLECoreError;
use mpz_share_conversion_core::Field;
use std::marker::PhantomData;

use crate::OLECoreError;

/// A provider for OLE with errors
/// An evaluator for OLE with errors.
pub struct OLEeEvaluator<F>(PhantomData<F>);

impl<F: Field> OLEeEvaluator<F> {
Expand All @@ -13,16 +11,16 @@ impl<F: Field> OLEeEvaluator<F> {
OLEeEvaluator(PhantomData)
}

/// Masks the OLEe input with the ROLEe input
/// Masks the OLEe input with the ROLEe input.
///
/// # Arguments
///
/// * `bk_dash` - The ROLEe input factors
/// * `bk` - The chosen OLEe input
/// * `bk_dash` - The ROLEe input factors.
/// * `bk` - The chosen OLEe input factors.
///
/// # Returns
///
/// * `vk` - The masked chosen input factors, which will be sent to the provider
/// * `vk` - The masked chosen input factors, which will be sent to the provider.
pub fn create_mask(&self, bk_dash: &[F], bk: &[F]) -> Result<Vec<F>, OLECoreError> {
if bk_dash.len() != bk.len() {
return Err(OLECoreError::LengthMismatch(format!(
Expand All @@ -32,26 +30,22 @@ impl<F: Field> OLEeEvaluator<F> {
)));
}

let vk: Vec<F> = bk_dash
.iter()
.zip(bk.iter().copied())
.map(|(&d, b)| b + d)
.collect();
let vk: Vec<F> = bk_dash.iter().zip(bk).map(|(&d, &b)| b + d).collect();

Ok(vk)
}

/// Generates the OLEe output
/// Generates the OLEe output.
///
/// # Arguments
///
/// * `bk` - The OLEe input
/// * `yk_dash` - The ROLEe output
/// * `uk` - The masked chosen input factors from the provider
/// * `bk` - The OLEe input factors.
/// * `yk_dash` - The ROLEe output.
/// * `uk` - The masked chosen input factors from the provider.
///
/// # Returns
///
/// * `yk` - The OLEe output for the evaluator
/// * `yk` - The OLEe output for the evaluator.
pub fn generate_output(
&self,
bk: &[F],
Expand All @@ -69,9 +63,9 @@ impl<F: Field> OLEeEvaluator<F> {

let yk: Vec<F> = yk_dash
.iter()
.zip(bk.iter().copied())
.zip(uk.iter().copied())
.map(|((&y, b), u)| y + b * u)
.zip(bk)
.zip(uk)
.map(|((&y, &b), &u)| y + b * u)
.collect();

Ok(yk)
Expand Down
8 changes: 5 additions & 3 deletions ole/mpz-ole-core/src/ole/role/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Provides implementations of OLEe protocols based on ROLEe.
//! Provides an implementation of OLEe based on ROLEe.
//!
//! This module implements the "OLE from random OLE" protocol in <https://github.com/tlsnotary/docs-mdbook/blob/main/research/ole-flavors.typ>.
mod evaluator;
mod provider;
Expand All @@ -10,14 +12,14 @@ pub use provider::OLEeProvider;
mod tests {
use super::{OLEeEvaluator, OLEeProvider};
use crate::ideal::ROLEFunctionality;
use mpz_core::{prg::Prg, Block};
use mpz_share_conversion_core::fields::{p256::P256, UniformRand};
use rand::SeedableRng;
use rand_chacha::ChaCha12Rng;

#[test]
fn test_ole_role_core() {
let count = 12;
let mut rng = ChaCha12Rng::from_seed([0_u8; 32]);
let mut rng = Prg::from_seed(Block::ZERO);
let mut role: ROLEFunctionality<P256> = ROLEFunctionality::default();

let provider: OLEeProvider<P256> = OLEeProvider::default();
Expand Down
35 changes: 15 additions & 20 deletions ole/mpz-ole-core/src/ole/role/provider.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::OLECoreError;
use mpz_share_conversion_core::Field;
use std::marker::PhantomData;

use crate::OLECoreError;

/// A provider for OLE with errors
/// A provider for OLE with errors.
pub struct OLEeProvider<F>(PhantomData<F>);

impl<F: Field> OLEeProvider<F> {
Expand All @@ -12,16 +11,16 @@ impl<F: Field> OLEeProvider<F> {
OLEeProvider(PhantomData)
}

/// Masks the OLEe input with the ROLEe input
/// Masks the OLEe input with the ROLEe input.
///
/// # Arguments
///
/// * `ak_dash` - The ROLEe input factors
/// * `ak` - The chosen OLEe input
/// * `ak_dash` - The ROLEe input factors.
/// * `ak` - The chosen OLEe input factors.
///
/// # Returns
///
/// * `uk` - The masked chosen input factors, which will be sent to the evaluator
/// * `uk` - The masked chosen input factors, which will be sent to the evaluator.
pub fn create_mask(&self, ak_dash: &[F], ak: &[F]) -> Result<Vec<F>, OLECoreError> {
if ak_dash.len() != ak.len() {
return Err(OLECoreError::LengthMismatch(format!(
Expand All @@ -31,26 +30,22 @@ impl<F: Field> OLEeProvider<F> {
)));
}

let uk: Vec<F> = ak_dash
.iter()
.zip(ak.iter().copied())
.map(|(&d, a)| a + d)
.collect();
let uk: Vec<F> = ak_dash.iter().zip(ak).map(|(&d, &a)| a + d).collect();

Ok(uk)
}

/// Generates the OLEe output
/// Generates the OLEe output.
///
/// # Arguments
///
/// * `ak_dash` - The ROLEe input
/// * `xk_dash` - The ROLEe output
/// * `vk` - The masked chosen input factors from the evaluator
/// * `ak_dash` - The ROLEe input factors.
/// * `xk_dash` - The ROLEe output.
/// * `vk` - The masked chosen input factors from the evaluator.
///
/// # Returns
///
/// * `xk` - The OLEe output for the provider
/// * `xk` - The OLEe output for the provider.
pub fn generate_output(
&self,
ak_dash: &[F],
Expand All @@ -68,9 +63,9 @@ impl<F: Field> OLEeProvider<F> {

let xk: Vec<F> = xk_dash
.iter()
.zip(ak_dash.iter().copied())
.zip(vk.iter().copied())
.map(|((&x, a), v)| -(-x + -a * v))
.zip(ak_dash)
.zip(vk)
.map(|((&x, &a), &v)| -(-x + -a * v))
.collect();

Ok(xk)
Expand Down
13 changes: 8 additions & 5 deletions ole/mpz-ole-core/src/role/ot/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use mpz_share_conversion_core::Field;
use rand::thread_rng;
use std::marker::PhantomData;

#[derive(Debug)]
/// A ROLEeEvaluator.
/// An evaluator for ROLE with errors.
pub struct ROLEeEvaluator<const N: usize, F>(PhantomData<F>);

impl<const N: usize, F: Field> ROLEeEvaluator<N, F> {
Expand All @@ -23,7 +22,11 @@ impl<const N: usize, F: Field> ROLEeEvaluator<N, F> {
/// # Arguments
///
/// * `count` - The batch size, i.e. how many `d`s to sample.
pub fn sample_d_(&self, count: usize) -> Vec<F> {
///
/// # Returns
///
/// * `dk` - The evaluator's input to the random OLEe.
pub fn sample_d(&self, count: usize) -> Vec<F> {
let mut rng = thread_rng();
(0..count).map(|_| F::rand(&mut rng)).collect()
}
Expand All @@ -40,8 +43,8 @@ impl<const N: usize, F: Field> ROLEeEvaluator<N, F> {
///
/// # Returns
///
/// * `bk` - The evaluator's final ROLEe input factor.
/// * `yk` - The evaluator's final ROLEe output summand.
/// * `bk` - The evaluator's final ROLEe input factors.
/// * `yk` - The evaluator's final ROLEe output summands.
pub fn generate_output(
&self,
fi: &[bool],
Expand Down
6 changes: 4 additions & 2 deletions ole/mpz-ole-core/src/role/ot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Provides implementations of ROLEe protocols based on random OT.
//! Provides an implementation of ROLEe based on random OT.
//!
//! This module implements the "Random OLE" protocol in <https://github.com/tlsnotary/docs-mdbook/blob/main/research/ole-flavors.typ>.
mod evaluator;
mod provider;
Expand Down Expand Up @@ -52,7 +54,7 @@ mod tests {
let (ck, ek) = provider.sample_c_and_e(count);
let (ui, t0i) = provider.create_correlation(&ti01, &ck).unwrap();

let dk = evaluator.sample_d_(count);
let dk = evaluator.sample_d(count);

let (ak, xk) = provider.generate_output(&t0i, &ck, &dk, &ek).unwrap();
let (bk, yk) = evaluator.generate_output(&fi, &tfi, &ui, &dk, &ek).unwrap();
Expand Down
22 changes: 11 additions & 11 deletions ole/mpz-ole-core/src/role/ot/provider.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
//! An implementation of a ROLEe provider based on random OT.
use super::Check;
use crate::OLECoreError;
use itybity::IntoBitIterator;
use mpz_share_conversion_core::Field;
use rand::thread_rng;
use std::marker::PhantomData;

use crate::OLECoreError;

use super::Check;

#[derive(Debug)]
/// A ROLEeProvider
/// A provider for ROLE with errors.
pub struct ROLEeProvider<const N: usize, F>(PhantomData<F>);

impl<const N: usize, F: Field> ROLEeProvider<N, F> {
Expand All @@ -27,6 +22,11 @@ impl<const N: usize, F: Field> ROLEeProvider<N, F> {
/// # Arguments
///
/// * `count` - The batch size, i.e. how many `c`s and `e`s to sample.
///
/// # Returns
///
/// * `ck` - The provider's input to the random OLEe.
/// * `ek` - The provider's input to the random OLEe.
pub fn sample_c_and_e(&self, count: usize) -> (Vec<F>, Vec<F>) {
let mut rng = thread_rng();

Expand All @@ -36,7 +36,7 @@ impl<const N: usize, F: Field> ROLEeProvider<N, F> {
(ck, ek)
}

/// Creates the correlation which masks the provider's input `ck` and also returns the 0
/// Creates the correlation which masks the provider's input `ck` and also returns the 0-choice
/// messages of the ROT.
///
/// # Arguments
Expand Down Expand Up @@ -127,9 +127,9 @@ impl<const N: usize, F: Field> ROLEeProvider<N, F> {

let xk: Vec<F> = t0k
.iter()
.zip(ak.iter().copied())
.zip(ak.iter())
.zip(ek)
.map(|((&t, a), &k)| t + -(a * k))
.map(|((&t, &a), &k)| t + -(a * k))
.collect();

Ok((ak, xk))
Expand Down
2 changes: 1 addition & 1 deletion ole/mpz-ole/src/role/rot/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
.try_into_random_provider_msg()
.map_err(|err| OLEError::WrongMessage(err.to_string()))?;

let dk: Vec<F> = self.role_core.sample_d_(count);
let dk: Vec<F> = self.role_core.sample_d(count);

sink.send(ROLEeMessage::RandomEvaluatorMsg(dk.clone()))
.await?;
Expand Down

0 comments on commit 3af76ed

Please sign in to comment.