Skip to content

Commit

Permalink
Rudimentary load and store of pre-computation
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider-bensch committed May 21, 2024
1 parent 6a01ec4 commit fafb085
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
2 changes: 2 additions & 0 deletions atlas-spec/mpc-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ p256.workspace = true
hmac.workspace = true
hacspec-chacha20poly1305.workspace = true
hacspec_lib.workspace = true
serde_json.workspace = true
serde = { workspace = true, features = ["derive"] }
4 changes: 2 additions & 2 deletions atlas-spec/mpc-engine/examples/run_mpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ fn main() {
let c = circuit.clone();
let party_join_handle = thread::spawn(move || {
let mut rng = rand::thread_rng();
let mut bytes = vec![0u8; u16::MAX.try_into().unwrap()];
let mut bytes = vec![0u8; 100 * (u16::MAX as usize)];
rng.fill_bytes(&mut bytes);
let rng = Randomness::new(bytes);
let log_enabled = channel_config.id == 1;
let mut p = mpc_engine::party::Party::new(channel_config, &c, log_enabled, rng);

let _ = p.run();
let _ = p.run(None);
});
party_join_handles.push(party_join_handle);
}
Expand Down
71 changes: 57 additions & 14 deletions atlas-spec/mpc-engine/src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,23 +916,66 @@ impl Party {
}

/// Run the MPC protocol, returning the parties output, if any.
pub fn run(&mut self) -> Result<Option<Vec<bool>>, Error> {
let num_auth_shares = 1;
self.log(&format!(
"Want to generate {num_auth_shares} authenticated share(s)"
));
let pool_depth = num_auth_shares + SEC_MARGIN_SHARE_AUTH;
self.log(&format!(
"Pre-computing {num_auth_shares} (+ {SEC_MARGIN_SHARE_AUTH} for share security) bit authentication(s)..."
));
pub fn run(&mut self, precompute: Option<usize>) -> Result<Option<Vec<bool>>, Error> {
use std::io::Write;
if let Some(target_number) = precompute {
self.log(&format!(
"Pre-computing {target_number} bit authentication(s)..."
));

// We want to compute 1 authenticated share, so need `1 + SEC_MARGIN_SHARE_AUTH` bits in the pool.
self.abit_pool = self.precompute_abits(target_number)?;

let file = std::fs::File::create(format!("{}.triples", self.id))
.map_err(|_| Error::OtherError)?;
let mut writer = std::io::BufWriter::new(file);
serde_json::to_writer(&mut writer, &(self.global_mac_key, &self.abit_pool))
.map_err(|_| Error::OtherError)?;
writer.flush().unwrap();
Ok(None)
} else {
let num_auth_shares = 1;
self.log(&format!(
"Want to generate {num_auth_shares} authenticated share(s)"
));
self.log("Trying to read authenticated bits from file");
let file = std::fs::File::open(format!("{}.triples", self.id));
if let Ok(f) = file {
(self.global_mac_key, self.abit_pool) =
serde_json::from_reader(f).map_err(|_| Error::OtherError)?;
self.log(&format!(
"Read {} authenticated bits from pool",
self.abit_pool.len()
));

// We want to compute 1 authenticated share, so need `1 + SEC_MARGIN_SHARE_AUTH` bits in the pool.
self.abit_pool = self.precompute_abits(pool_depth)?;
let max_id = self
.abit_pool
.iter()
.max_by_key(|abit| abit.bit.id.0)
.map(|abit| abit.bit.id.0)
.unwrap_or(0);
self.bit_counter = max_id;

if num_auth_shares + SEC_MARGIN_SHARE_AUTH > self.abit_pool.len() {
self.log(&format!(
"Insufficient precomputation (by {})",
num_auth_shares + SEC_MARGIN_SHARE_AUTH - self.abit_pool.len()
));
return Ok(None);
}
} else {
self.log("Could not read pre-computed bit authentications from file.");
}

self.log("Starting share authentication");
let _shares = self.random_authenticated_shares(num_auth_shares)?;
self.log("Starting share authentication");
let _shares = self.random_authenticated_shares(num_auth_shares)?;

Ok(None)
//let bucket_size = (STATISTICAL_SECURITY as u32 / self.circuit.num_gates().ilog2()) as usize;
// let bucket_size = 3;
// self.log("Computing AND triples");
//let _and_shares = self.random_and_shares(2, bucket_size)?;
Ok(None)
}
}

/// Synchronise parties.
Expand Down
10 changes: 6 additions & 4 deletions atlas-spec/mpc-engine/src/primitives/auth_share.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
//! This module defines the interface for share authentication.
use serde::{Deserialize, Serialize};

use crate::{primitives::mac::MAC_LENGTH, Error};

use super::mac::{Mac, MacKey};

/// A bit held by a party with a given ID.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bit {
pub(crate) id: BitID,
pub(crate) value: bool,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
/// A bit identifier.
///
/// This is unique per party, not globally, so if referring bits held by another
/// party, their party ID is also required to disambiguate.
pub struct BitID(pub(crate) usize);

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
/// A bit authenticated between two parties.
pub struct AuthBit {
pub(crate) bit: Bit,
Expand Down Expand Up @@ -61,7 +63,7 @@ impl AuthBit {
}

/// The key to authenticate a two-party authenticated bit.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BitKey {
pub(crate) holder_bit_id: BitID,
pub(crate) bit_holder: usize,
Expand Down

0 comments on commit fafb085

Please sign in to comment.