Skip to content

Commit

Permalink
Use file descriptors instead of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed Jun 12, 2024
1 parent cb6e6ec commit 278445a
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 48 deletions.
62 changes: 58 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fixed = "2.0.0-alpha.11"
futures = "0.3.28"
futures-concurrency = "7.6.0"
group = "0.13.0"
itertools = "0.12"
itertools = "0.13"
num-traits = "0.2.16"
overload = "0.1.1"
rand = "0.8.5"
Expand All @@ -30,6 +30,7 @@ tokio-util = { version = "0.7.9", features = ["io", "net", "io-util", "codec", "
tracing = "0.1.40"

[dev-dependencies]
tempfile = "3.10.1"
tokio-test = "0.4.4"
curve25519-dalek = { version = "4.1.1", features = ["group", "serde"] }
sha2 = "0.10.8"
Expand Down
16 changes: 9 additions & 7 deletions src/schemes/spdz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ fn power<F: std::ops::MulAssign + Clone + std::ops::Mul<Output = F>>(base: F, ex
#[cfg(test)]
mod test {

use std::path::Path;
use std::io::Seek;
use ff::Field;
use rand::thread_rng;
use rand::SeedableRng;
Expand Down Expand Up @@ -1410,21 +1410,23 @@ mod test {
fn test_dealer_writing_to_file() {
// preprosessing by dealer
type F = Element32;
let file_names = vec![
Path::new("/tmp/context2.bin"),
Path::new("/tmp/context1.bin"),
let mut files = [
tempfile::tempfile().unwrap(),
tempfile::tempfile().unwrap(),
];
let known_to_each = vec![1, 2];
let number_of_triplets = 2;
preprocessing::write_preproc_to_file(
&file_names,
&mut files,
known_to_each,
number_of_triplets,
F::from_u128(0u128),
)
.unwrap();
let p1_context: SpdzContext<F> = preprocessing::read_preproc_from_file(file_names[0]);
let p2_context: SpdzContext<F> = preprocessing::read_preproc_from_file(file_names[1]);
files[0].rewind().unwrap();
files[1].rewind().unwrap();
let p1_context: SpdzContext<F> = preprocessing::read_preproc_from_file(&mut files[0]);
let p2_context: SpdzContext<F> = preprocessing::read_preproc_from_file(&mut files[1]);
// unpacking
let p1_params = p1_context.params;
let p2_params = p2_context.params;
Expand Down
25 changes: 9 additions & 16 deletions src/schemes/spdz/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::{
error::Error,
fmt,
fs::File,
io::{self, Read, Write},
path::Path,
io::{self, Write},
};
#[derive(Debug, Clone, PartialEq)]
pub enum MissingPreProcErrorType {
Expand Down Expand Up @@ -89,37 +88,31 @@ pub struct SecretValues<F> {
pub mac_key: F,
}
pub fn write_preproc_to_file<F: PrimeField + serde::Serialize + serde::de::DeserializeOwned>(
file_names: &[&Path],
files: &mut [File],
known_to_each: Vec<usize>,
number_of_triplets: usize,
_: F,
) -> Result<(), Box<dyn Error>> {
let number_of_parties = file_names.len();
let number_of_parties = files.len();
assert!(number_of_parties == known_to_each.len());
let rng = rand_chacha::ChaCha20Rng::from_entropy();
// Notice here that the secret values are not written to the file, No party is allowed to know the value.
let (contexts, _): (Vec<SpdzContext<F>>, _) =
dealer_prepross(rng, known_to_each, number_of_triplets, number_of_parties);
let names_and_contexts = file_names.iter().zip(contexts);
for (name, context) in names_and_contexts {
let names_and_contexts = files.iter_mut().zip(contexts);
for (file, context) in names_and_contexts {
let data: Vec<u8> = bincode::serialize(&context)?;
let mut file = File::create(name)?;
file.write_all(&data)?;
file.sync_all()?;
}
Ok(())
}


pub fn read_preproc_from_file<F: PrimeField + serde::Serialize + serde::de::DeserializeOwned>(
file_name: &Path,
file: &mut File,
) -> SpdzContext<F> {
// TODO: return Result instead.
let mut data = Vec::new();
let mut file = File::open(file_name).expect("open file");
file.read_to_end(&mut data).expect("read to end");
let new_context: SpdzContext<F> = bincode::deserialize(&data).expect("deserialize");
new_context
bincode::deserialize_from(file).unwrap()
}


Expand Down Expand Up @@ -259,8 +252,8 @@ impl<F: PrimeField + serde::Serialize + serde::de::DeserializeOwned> SpdzContext
generate_empty_context(number_of_parties, mac_key_share, who_am_i)
}

pub fn from_file(path: &Path) -> Result<Self, io::Error> {
Ok(read_preproc_from_file(path))
pub fn from_file(mut file: File) -> Result<Self, io::Error> {
Ok(read_preproc_from_file(&mut file))
}
}

Expand Down
62 changes: 58 additions & 4 deletions wecare/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions wecare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ tokio = { version = "1.33.0", features = ["full"] }
curve25519-dalek = { version = "4.1.1", features = ["group", "serde"] }
rand = "0.8.5"
fixed = "2.0.0-alpha.11"

[dev-dependencies]
tempfile = "3.10.1"
Loading

0 comments on commit 278445a

Please sign in to comment.