Add F2 type for testing COT and COPE

This commit is contained in:
th4s
2023-12-18 19:14:17 +01:00
parent c305513d6e
commit 586d54f01a
6 changed files with 118 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -584,6 +584,7 @@ dependencies = [
name = "ole-protocols"
version = "0.1.0"
dependencies = [
"itybity",
"mpz-share-conversion-core",
"p256",
"rand",

View File

@@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
mpz-share-conversion-core = { git = "https://github.com/privacy-scaling-explorations/mpz" }
p256 = { version = "0.13", features = ["arithmetic"] }
itybity = "0.2"
rand = "0.8"

106
src/f2.rs Normal file
View File

@@ -0,0 +1,106 @@
use itybity::{BitLength, FromBitIterator, GetBit, Lsb0, Msb0};
use mpz_share_conversion_core::fields::Field;
use rand::distributions::{Distribution, Standard};
use std::ops::{Add, Mul, Neg};
/// A simple boolean field type
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct F2 {
inner: u8,
}
impl F2 {
/// Create a new `F2` from a `bool`.
///
/// `False` encodes 0 and `true` encodes 1.
pub fn new(value: bool) -> Self {
Self { inner: value as u8 }
}
}
impl Field for F2 {
const BIT_SIZE: u32 = 1;
fn zero() -> Self {
Self::new(false)
}
fn one() -> Self {
Self::new(true)
}
fn two_pow(_rhs: u32) -> Self {
unimplemented!()
}
fn inverse(self) -> Self {
if self.inner == 0 {
panic!("No inverse for 0")
}
Self::one()
}
fn to_le_bytes(&self) -> Vec<u8> {
unimplemented!()
}
fn to_be_bytes(&self) -> Vec<u8> {
unimplemented!()
}
}
impl Distribution<F2> for Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> F2 {
F2::new(rng.gen())
}
}
impl Add for F2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new((self.inner ^ rhs.inner) != 0)
}
}
impl Mul for F2 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.inner & rhs.inner != 0)
}
}
impl Neg for F2 {
type Output = Self;
fn neg(self) -> Self::Output {
self
}
}
impl BitLength for F2 {
const BITS: usize = 1;
}
impl GetBit<Lsb0> for F2 {
fn get_bit(&self, _index: usize) -> bool {
unimplemented!()
}
}
impl GetBit<Msb0> for F2 {
fn get_bit(&self, _index: usize) -> bool {
unimplemented!()
}
}
impl FromBitIterator for F2 {
fn from_lsb0_iter(_iter: impl IntoIterator<Item = bool>) -> Self {
unimplemented!()
}
fn from_msb0_iter(_iter: impl IntoIterator<Item = bool>) -> Self {
unimplemented!()
}
}

7
src/func/cote.rs Normal file
View File

@@ -0,0 +1,7 @@
//! This module implements the COTE functionality (page 5) from <https://eprint.iacr.org/2015/546>
#[derive(Debug)]
pub struct Cote {
kappa: usize,
l: usize,
}

View File

@@ -1,3 +1,4 @@
//! This module implements some functionalities.
pub mod cote;
pub mod ole;

View File

@@ -2,5 +2,6 @@
#![feature(iter_map_windows)]
pub mod e2f;
mod f2;
mod func;
pub mod ghash;