-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base OT & OTe implementation started
- Loading branch information
Showing
9 changed files
with
318 additions
and
50 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
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,77 @@ | ||
use curve25519_dalek::ristretto::RistrettoPoint; | ||
use curve25519_dalek::scalar::Scalar; | ||
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; | ||
use rand::rngs::OsRng; | ||
|
||
use crate::otext::constants::{ALICE, BOB}; | ||
use crate::channel::{MsgChannel, Channel}; | ||
use crate::otext::block::Block; | ||
use crate::otext::utils::kdf; | ||
|
||
use super::block::ZERO_BLOCK; | ||
|
||
// Chou-Orlandi OT | ||
pub struct OTCO{} | ||
|
||
impl OTCO { | ||
pub async fn send(&mut self, channel: &mut MsgChannel<impl Channel>, data0: & Vec<Block>, data1: & Vec<Block>, length: usize) { | ||
let mut rng = OsRng; | ||
let a = Scalar::random(&mut rng); | ||
let aa = RISTRETTO_BASEPOINT_POINT * a; | ||
let aa_inv = aa * -a; | ||
|
||
let mut b = Vec::with_capacity(length); | ||
let mut ba = Vec::with_capacity(length); | ||
|
||
|
||
channel.send_to(ALICE, "asend", &aa).await.unwrap(); //NOT SURE WHICH PARTY IT IS HERE | ||
|
||
for i in 0..length { | ||
b[i] = channel.recv_from(ALICE, "bsend").await.unwrap(); | ||
b[i] = b[i] * a; | ||
ba[i] = b[i] + aa_inv; | ||
} | ||
|
||
let mut res: [Block; 2] = [ZERO_BLOCK; 2]; | ||
for i in 0..length { | ||
res[0] = kdf(&b[i], i) ^ data0[i]; | ||
res[1] = kdf(&ba[i], i) ^ data1[i]; | ||
channel.send_to(ALICE, "res", &res).await.unwrap(); | ||
} | ||
} | ||
|
||
pub async fn recv(&mut self, channel: &mut MsgChannel<impl Channel>, data: &mut Vec<Block>, b: Vec<bool>, length: usize) { | ||
let mut rng = OsRng; | ||
|
||
let mut bb = Vec::with_capacity(length); | ||
for _ in 0..length { | ||
bb.push(Scalar::random(&mut rng)); | ||
} | ||
|
||
let a: RistrettoPoint = channel.recv_from(BOB, "asend").await.unwrap(); | ||
let mut bigb = Vec::with_capacity(length); | ||
let mut a_s = Vec::with_capacity(length); | ||
|
||
for i in 0..length { | ||
bigb[i] = RISTRETTO_BASEPOINT_POINT * bb[i]; | ||
if b[i] { | ||
bigb[i] += a; | ||
} | ||
channel.send_to(BOB, "bsend", &bigb[i]).await.unwrap(); | ||
} | ||
|
||
for i in 0..length { | ||
a_s[i] = a * bb[i]; | ||
} | ||
|
||
for i in 0..length { | ||
let res: [Block; 2] = channel.recv_from(BOB, "res").await.unwrap(); | ||
let kdf_result = kdf(&a_s[i], i); | ||
if b[i] { | ||
data[i] = kdf_result ^ res[1]; | ||
} else { | ||
data[i] = kdf_result ^ res[0]; | ||
} | ||
} | ||
} | ||
} |
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
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,125 @@ | ||
use crate::channel::{Channel, MsgChannel}; | ||
use crate::otext::baseot::OTCO; | ||
use crate::otext::block::{bool_to_block, Block, ZERO_BLOCK}; | ||
use rand::prelude::*; | ||
use rand::rngs::OsRng; | ||
use rand::{RngCore, SeedableRng}; | ||
|
||
use super::block::random_block; | ||
|
||
const BLOCK_SIZE: usize = 1024 * 2; | ||
|
||
pub struct Iknp { | ||
pub ot_delta: Block, | ||
base_ot: OTCO, | ||
setup: bool, | ||
local_out: [Block; BLOCK_SIZE], | ||
s: [bool; 128], | ||
local_r: [bool; 256], | ||
prg: OsRng, | ||
g0: [OsRng; 128], | ||
g1: [OsRng; 128], | ||
malicious: bool, | ||
k0: [Block; 128], | ||
k1: [Block; 128], | ||
} | ||
|
||
impl Iknp { | ||
pub fn new(delta: Block, malicious: bool) -> Self { | ||
Iknp { | ||
ot_delta: delta, | ||
base_ot: OTCO {}, | ||
setup: false, | ||
local_out: [ZERO_BLOCK; BLOCK_SIZE], | ||
s: [false; 128], | ||
local_r: [false; 256], | ||
prg: OsRng, | ||
g0: [OsRng; 128], | ||
g1: [OsRng; 128], | ||
malicious, | ||
k0: [ZERO_BLOCK; 128], | ||
k1: [ZERO_BLOCK; 128], | ||
} | ||
} | ||
|
||
async fn setup_send_delta(&mut self, channel: &mut MsgChannel<impl Channel>, in_s: [bool; 128]) { | ||
self.setup = true; | ||
|
||
self.s.copy_from_slice(&in_s); | ||
|
||
self.base_ot | ||
.recv(channel, &mut self.k0.to_vec(), self.s.to_vec(), 128).await; | ||
|
||
//for i in 0..128 { | ||
//self.g0[i].reseed(&self.k0[i]); | ||
//TODO figure out reseeding here! | ||
//} | ||
self.ot_delta = bool_to_block(&self.s); | ||
} | ||
|
||
async fn setup_send(&mut self, channel: &mut MsgChannel<impl Channel>) { | ||
self.setup = true; | ||
for i in 0..128 { | ||
self.s[i] = self.prg.next_u32() % 2 == 1; | ||
} | ||
|
||
self.base_ot | ||
.recv(channel, &mut self.k0.to_vec(), self.s.to_vec(), 128).await; | ||
|
||
//for i in 0..128 { | ||
//self.g0[i].reseed(&self.k0[i]); | ||
//TODO figure out reseeding here! | ||
//} | ||
self.ot_delta = bool_to_block(&self.s); | ||
} | ||
|
||
async fn setup_recv(&mut self, channel: &mut MsgChannel<impl Channel>) { | ||
self.setup = true; | ||
|
||
for i in 0..128 { | ||
self.k0[i] = random_block(&mut self.prg); | ||
self.k1[i] = random_block(&mut self.prg); | ||
} | ||
self.base_ot.send(channel, &mut self.k0.to_vec(), &self.k1.to_vec(), 128).await; | ||
|
||
/*for i in 0..128 { | ||
self.g0[i].reseed(&self.k0[i]); // Assuming PRG has a reseed method | ||
self.g1[i].reseed(&self.k1[i]); // Assuming PRG has a reseed method | ||
}*/ | ||
} | ||
|
||
pub fn recv_pre(&self, data: &mut Vec<Block>, b: Vec<bool>, length: usize) { | ||
// implement ALSZ OT | ||
} | ||
|
||
pub fn send_pre(&self, out: &mut Vec<Block>, length: usize) { | ||
// implement ALSZ OT | ||
} | ||
|
||
pub fn send_check(&self, out: &mut Vec<Block>, length: usize) -> bool { | ||
//implement KOS check | ||
true | ||
} | ||
|
||
pub fn recv_check(&self, out: &mut Vec<Block>, r: Vec<bool>, length: usize) { | ||
// implement KOS check | ||
} | ||
|
||
pub fn send_cot(&self, data: &mut Vec<Block>, length: usize) { | ||
self.send_pre(data, length); | ||
|
||
if self.malicious { | ||
if !self.send_check(data, length) { | ||
panic!("OT Extension check failed"); | ||
} | ||
} | ||
} | ||
|
||
pub fn recv_cot(&self, data: &mut Vec<Block>, b: Vec<bool>, length: usize) { | ||
self.recv_pre(data, b.clone(), length); | ||
|
||
if self.malicious { | ||
self.recv_check(data, b, length); | ||
} | ||
} | ||
} |
Oops, something went wrong.