From 5ffb688c30fb85592796f4bea31c157a598e250c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=B3=CE=BB?= Date: Mon, 30 Dec 2024 16:00:50 +1100 Subject: [PATCH] Remove redundant packages (#227) --- packages/ciphernode/Cargo.lock | 13 -- packages/ciphernode/Cargo.toml | 1 - packages/ciphernode/bfv/Cargo.toml | 15 --- packages/ciphernode/bfv/src/lib.rs | 84 ------------- packages/ciphernode/bfv/src/util.rs | 129 -------------------- packages/ciphernode/enclave_core/Cargo.toml | 1 - packages/ciphernode/tests/Cargo.toml | 1 - packages/ciphernode/zkp/Cargo.toml | 14 --- packages/ciphernode/zkp/src/main.rs | 3 - 9 files changed, 261 deletions(-) delete mode 100644 packages/ciphernode/bfv/Cargo.toml delete mode 100644 packages/ciphernode/bfv/src/lib.rs delete mode 100644 packages/ciphernode/bfv/src/util.rs delete mode 100644 packages/ciphernode/zkp/Cargo.toml delete mode 100644 packages/ciphernode/zkp/src/main.rs diff --git a/packages/ciphernode/Cargo.lock b/packages/ciphernode/Cargo.lock index 606d89d1..0798534f 100644 --- a/packages/ciphernode/Cargo.lock +++ b/packages/ciphernode/Cargo.lock @@ -1431,17 +1431,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" -[[package]] -name = "bfv" -version = "0.1.0" -dependencies = [ - "async-std", - "fhe 0.1.0-beta.7", - "fhe-traits", - "fhe-util", - "rand", -] - [[package]] name = "bimap" version = "0.6.3" @@ -2218,7 +2207,6 @@ dependencies = [ "alloy", "alloy-primitives 0.6.4", "anyhow", - "bfv", "bincode", "clap", "config", @@ -6052,7 +6040,6 @@ dependencies = [ "anyhow", "async-std", "base64", - "bfv", "bincode", "clap", "crypto", diff --git a/packages/ciphernode/Cargo.toml b/packages/ciphernode/Cargo.toml index d9569d48..67f7dce7 100644 --- a/packages/ciphernode/Cargo.toml +++ b/packages/ciphernode/Cargo.toml @@ -4,7 +4,6 @@ members = [ "enclave", "enclave_core", "net", - "bfv", "data", "evm", "fhe", diff --git a/packages/ciphernode/bfv/Cargo.toml b/packages/ciphernode/bfv/Cargo.toml deleted file mode 100644 index 2426bd01..00000000 --- a/packages/ciphernode/bfv/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "bfv" -version = "0.1.0" -edition = "2021" -description = ": coordinates the encryption and decryption of enclave computations" -repository = "https://github.com/gnosisguild/enclave/packages/ciphernode" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -async-std = "1.12.0" -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 = "0.8.5" diff --git a/packages/ciphernode/bfv/src/lib.rs b/packages/ciphernode/bfv/src/lib.rs deleted file mode 100644 index b3c76b19..00000000 --- a/packages/ciphernode/bfv/src/lib.rs +++ /dev/null @@ -1,84 +0,0 @@ -#![crate_name = "bfv"] -#![crate_type = "lib"] -#![warn(missing_docs, unused_imports)] - -mod util; - -use fhe::{ - bfv::{BfvParameters, BfvParametersBuilder, Ciphertext, Encoding, Plaintext, SecretKey}, - mbfv::{AggregateIter, CommonRandomPoly, DecryptionShare, PublicKeyShare}, -}; -use fhe_traits::{Deserialize, DeserializeParametrized, FheDecoder, Serialize as FheSerialize}; -use rand::{rngs::OsRng, thread_rng, Rng}; -use std::sync::Arc; -use util::timeit::timeit; - -pub struct EnclaveBFV { - pub pk_share: PublicKeyShare, - sk_share: SecretKey, - pub params: Arc, - pub crp: CommonRandomPoly, -} - -impl EnclaveBFV { - pub fn new(degree: usize, plaintext_modulus: u64, moduli: Vec) -> Self { - // let degree = 4096; - // let plaintext_modulus: u64 = 4096; - // let moduli = vec![0xffffee001, 0xffffc4001, 0x1ffffe0001]; - - // Generate the BFV parameters structure. - let params = timeit!( - "Parameters generation", - BfvParametersBuilder::new() - .set_degree(degree) - .set_plaintext_modulus(plaintext_modulus) - .set_moduli(&moduli) - .build_arc() - .unwrap() - ); - - let crp = CommonRandomPoly::new(¶ms, &mut thread_rng()).unwrap(); - //TODO: save encrypted sk_share to disk? - let sk_share = SecretKey::random(¶ms, &mut OsRng); - let pk_share = PublicKeyShare::new(&sk_share, crp.clone(), &mut thread_rng()).unwrap(); - - Self { - pk_share, - sk_share, - params, - crp, - } - } - - pub fn serialize_pk(&mut self) -> Vec { - self.pk_share.to_bytes() - } - - pub fn deserialize_pk( - &mut self, - bytes: Vec, - par_bytes: Vec, - crp_bytes: Vec, - ) -> PublicKeyShare { - let params = Arc::new(BfvParameters::try_deserialize(&par_bytes).unwrap()); - let crp = CommonRandomPoly::deserialize(&crp_bytes, ¶ms).unwrap(); - PublicKeyShare::deserialize(&bytes, ¶ms, crp.clone()).unwrap() - } - - pub fn serialize_crp(&mut self) -> Vec { - self.crp.to_bytes() - } - - pub fn deserialize_crp(&mut self, bytes: Vec, par_bytes: Vec) -> CommonRandomPoly { - let params = Arc::new(BfvParameters::try_deserialize(&par_bytes).unwrap()); - CommonRandomPoly::deserialize(&bytes, ¶ms).unwrap() - } - - pub fn serialize_params(&mut self) -> Vec { - self.params.to_bytes() - } - - pub fn deserialize_params(&mut self, par_bytes: Vec) -> Arc { - Arc::new(BfvParameters::try_deserialize(&par_bytes).unwrap()) - } -} diff --git a/packages/ciphernode/bfv/src/util.rs b/packages/ciphernode/bfv/src/util.rs deleted file mode 100644 index 2b5c2e48..00000000 --- a/packages/ciphernode/bfv/src/util.rs +++ /dev/null @@ -1,129 +0,0 @@ -//! 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> { - 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>, - par: Arc, - level: usize, -) -> (Vec, (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() {} diff --git a/packages/ciphernode/enclave_core/Cargo.toml b/packages/ciphernode/enclave_core/Cargo.toml index 8e972443..338fba74 100644 --- a/packages/ciphernode/enclave_core/Cargo.toml +++ b/packages/ciphernode/enclave_core/Cargo.toml @@ -12,7 +12,6 @@ aggregator = { path = "../aggregator" } alloy = { workspace = true } alloy-primitives = { workspace = true } anyhow = { workspace = true } -bfv = { path = "../bfv" } bincode = { workspace = true } config = { path = "../config" } clap = { workspace = true } diff --git a/packages/ciphernode/tests/Cargo.toml b/packages/ciphernode/tests/Cargo.toml index ac203690..3e883a35 100644 --- a/packages/ciphernode/tests/Cargo.toml +++ b/packages/ciphernode/tests/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" [dependencies] net = { path = "../net" } -bfv = { path = "../bfv" } sortition = { path = "../sortition" } events = { workspace = true } evm = { path = "../evm" } diff --git a/packages/ciphernode/zkp/Cargo.toml b/packages/ciphernode/zkp/Cargo.toml deleted file mode 100644 index 06f73543..00000000 --- a/packages/ciphernode/zkp/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "zkp" -version = "0.1.0" -edition = "2021" -description = ": coordinates the encryption and decryption of enclave computations" -repository = "https://github.com/gnosisguild/enclave/packages/ciphernode" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -async-std = "1.12.0" -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" } diff --git a/packages/ciphernode/zkp/src/main.rs b/packages/ciphernode/zkp/src/main.rs deleted file mode 100644 index a0d9916c..00000000 --- a/packages/ciphernode/zkp/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, cipher world!"); -}