Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed May 20, 2024
1 parent 16c7711 commit 39ca87f
Show file tree
Hide file tree
Showing 18 changed files with 5,307 additions and 18 deletions.
5,129 changes: 5,129 additions & 0 deletions packages/ciphernode/Cargo.lock

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/ciphernode/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> Result<(), ()> {

Ok(())
}
18 changes: 0 additions & 18 deletions packages/rust/src/main.rs

This file was deleted.

File renamed without changes.
41 changes: 41 additions & 0 deletions packages/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "rfv"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
console = "0.15.7"
fhe = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" }
fhe-traits = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" }
fhe-util = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" }
rand_chacha = "0.3.1"
rand = "0.8.5"
ethers = "2.0"
getrandom = { version = "0.2.11", features = ["js"] }
# Ethers' async features rely upon the Tokio async runtime.
tokio = { version = "1.37.0", features = ["full"] }
bincode = "1.0"
hyper = { version = "1", features = ["full"] }
http-body-util = "0.1"
hyper-util = { version = "0.1", features = ["full"] }
hyper-tls = "0.6.0"
iron = "0.6.0"
router = "0.6.0"
walkdir = "2.5.0"
dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
wasm-bindgen = "0.2"
chrono = "0.4.38"
sled = "0.34"
once_cell = "1.19.0"
async-std = { version = "1", features = ["attributes", "tokio1"] }
bytes = "1.6.0"
iron-cors = "0.8.0"
headers = "0.4.0"
jwt = "0.16.0"
hmac = "0.12.1"
sha2 = "0.10.8"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
129 changes: 129 additions & 0 deletions packages/server/src/bin/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//! Utility functions
use fhe::bfv;
use fhe_traits::FheEncoder;
use fhe_util::transcode_from_bytes;
use std::{cmp::min, fmt, sync::Arc, time::Duration};

/// Macros to time code and display a human-readable duration.
pub mod timeit {
#[allow(unused_macros)]
macro_rules! timeit_n {
($name:expr, $loops:expr, $code:expr) => {{
use util::DisplayDuration;
let start = std::time::Instant::now();
let r = $code;
for _ in 1..$loops {
let _ = $code;
}
println!(
"⏱ {}: {}",
$name,
DisplayDuration(start.elapsed() / $loops)
);
r
}};
}

#[allow(unused_macros)]
macro_rules! timeit {
($name:expr, $code:expr) => {{
use util::DisplayDuration;
let start = std::time::Instant::now();
let r = $code;
println!("⏱ {}: {}", $name, DisplayDuration(start.elapsed()));
r
}};
}

#[allow(unused_imports)]
pub(crate) use timeit;
#[allow(unused_imports)]
pub(crate) use timeit_n;
}

/// Utility struct for displaying human-readable duration of the form "10.5 ms",
/// "350 μs", or "27 ns".
pub struct DisplayDuration(pub Duration);

impl fmt::Display for DisplayDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let duration_ns = self.0.as_nanos();
if duration_ns < 1_000_u128 {
write!(f, "{duration_ns} ns")
} else if duration_ns < 1_000_000_u128 {
write!(f, "{} μs", (duration_ns + 500) / 1_000)
} else {
let duration_ms_times_10 = (duration_ns + 50_000) / (100_000);
write!(f, "{} ms", (duration_ms_times_10 as f64) / 10.0)
}
}
}

// Utility functions for Private Information Retrieval.

/// Generate a database of elements of the form [i || 0...0] where i is the 4B
/// little endian encoding of the index. When the element size is less than 4B,
/// the encoding is truncated.
#[allow(dead_code)]
pub fn generate_database(database_size: usize, elements_size: usize) -> Vec<Vec<u8>> {
assert!(database_size > 0 && elements_size > 0);
let mut database = vec![vec![0u8; elements_size]; database_size];
for (i, element) in database.iter_mut().enumerate() {
element[..min(4, elements_size)]
.copy_from_slice(&(i as u32).to_le_bytes()[..min(4, elements_size)]);
}
database
}

#[allow(dead_code)]
pub fn number_elements_per_plaintext(
degree: usize,
plaintext_nbits: usize,
elements_size: usize,
) -> usize {
(plaintext_nbits * degree) / (elements_size * 8)
}

#[allow(dead_code)]
pub fn encode_database(
database: &Vec<Vec<u8>>,
par: Arc<bfv::BfvParameters>,
level: usize,
) -> (Vec<bfv::Plaintext>, (usize, usize)) {
assert!(!database.is_empty());

let elements_size = database[0].len();
let plaintext_nbits = par.plaintext().ilog2() as usize;
let number_elements_per_plaintext =
number_elements_per_plaintext(par.degree(), plaintext_nbits, elements_size);
let number_rows =
(database.len() + number_elements_per_plaintext - 1) / number_elements_per_plaintext;
println!("number_rows = {number_rows}");
println!("number_elements_per_plaintext = {number_elements_per_plaintext}");
let dimension_1 = (number_rows as f64).sqrt().ceil() as usize;
let dimension_2 = (number_rows + dimension_1 - 1) / dimension_1;
println!("dimensions = {dimension_1} {dimension_2}");
println!("dimension = {}", dimension_1 * dimension_2);
let mut preprocessed_database =
vec![
bfv::Plaintext::zero(bfv::Encoding::poly_at_level(level), &par).unwrap();
dimension_1 * dimension_2
];
(0..number_rows).for_each(|i| {
let mut serialized_plaintext = vec![0u8; number_elements_per_plaintext * elements_size];
for j in 0..number_elements_per_plaintext {
if let Some(pt) = database.get(j + i * number_elements_per_plaintext) {
serialized_plaintext[j * elements_size..(j + 1) * elements_size].copy_from_slice(pt)
}
}
let pt_values = transcode_from_bytes(&serialized_plaintext, plaintext_nbits);
preprocessed_database[i] =
bfv::Plaintext::try_encode(&pt_values, bfv::Encoding::poly_at_level(level), &par)
.unwrap();
});
(preprocessed_database, (dimension_1, dimension_2))
}

#[allow(dead_code)]
fn main() {}
4 changes: 4 additions & 0 deletions packages/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> Result<(), ()> {

Ok(())
}

0 comments on commit 39ca87f

Please sign in to comment.