diff --git a/packages/ciphernode/enclave_node/Cargo.toml b/packages/ciphernode/enclave_node/Cargo.toml index f062f405..228541b9 100644 --- a/packages/ciphernode/enclave_node/Cargo.toml +++ b/packages/ciphernode/enclave_node/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://github.com/gnosisguild/enclave/packages/ciphernode" eth = { path = "../eth" } p2p = { path = "../p2p" } bfv = { path = "../bfv" } +sortition = { path = "../sortition" } enclave-core = { path = "../core" } async-std = "1.12.0" fhe = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" } @@ -18,5 +19,6 @@ fhe-traits = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-b fhe-util = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" } tokio = { version = "1.38", features = ["full"] } actix-rt = "2.10.0" +alloy-primitives = { version = "0.6", default-features = false, features = ["rlp", "serde", "std"] } alloy = { version = "0.2.1", features = ["full"] } \ No newline at end of file diff --git a/packages/ciphernode/enclave_node/src/main.rs b/packages/ciphernode/enclave_node/src/main.rs index 9ad0668f..9e5aee83 100644 --- a/packages/ciphernode/enclave_node/src/main.rs +++ b/packages/ciphernode/enclave_node/src/main.rs @@ -2,10 +2,14 @@ use std::error::Error; use p2p::EnclaveRouter; use bfv::EnclaveBFV; +use sortition::DistanceSortition; use tokio::{ self, io::{self, AsyncBufReadExt, BufReader}, }; + +use alloy_primitives::{address}; + const OWO: &str = r#" ___ ___ ___ ___ ___ /\__\ /\ \ /\__\ /\ \ ___ /\__\ @@ -34,6 +38,9 @@ async fn main() -> Result<(), Box> { println!("\n\n\n\n"); println!("Hello, cipher world!"); + let mut committee = DistanceSortition::new(12, vec![address!("d8da6bf26964af9d7eed9e03e53415d37aa96045")], 10); + committee.get_committee(); + let mut new_bfv = EnclaveBFV::new(4096, 4096, vec![0xffffee001, 0xffffc4001, 0x1ffffe0001]); let pk_bytes = new_bfv.serialize_pk(); let param_bytes = new_bfv.serialize_params(); diff --git a/packages/ciphernode/sortition/Cargo.toml b/packages/ciphernode/sortition/Cargo.toml new file mode 100644 index 00000000..b4b794b2 --- /dev/null +++ b/packages/ciphernode/sortition/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "sortition" +version = "0.1.0" +edition = "2021" +description = ": coordinates the encryption and decryption of enclave computations" +repository = "https://github.com/gnosisguild/enclave/packages/ciphernode" +path = "src/lib.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num = "0.4.3" + +# Core +enclave-core = { path = "../core" } + +# Actix +actix = "0.13.5" +async-std = "1.12.0" + +# Ethereum +futures-util = "0.3" +eyre = "0.6" +alloy = { version = "0.2.1", features = ["full"] } +alloy-primitives = { version = "0.6", default-features = false, features = ["rlp", "serde", "std"] } +alloy-sol-types = { version = "0.6" } \ No newline at end of file diff --git a/packages/ciphernode/sortition/src/distance.rs b/packages/ciphernode/sortition/src/distance.rs new file mode 100644 index 00000000..01b1e958 --- /dev/null +++ b/packages/ciphernode/sortition/src/distance.rs @@ -0,0 +1,34 @@ +use alloy_primitives::{address, Address, keccak256}; +use num::{BigInt, Num}; + +pub struct DistanceSortition { + pub random_seed: u64, + pub registered_nodes: Vec
, + pub size: usize, +} + +impl DistanceSortition { + pub fn new(random_seed: u64, registered_nodes: Vec
, size: usize) -> Self { + Self { random_seed, registered_nodes, size } + } + + pub fn get_committee(&mut self) -> Vec<(BigInt, Address)> { + let mut scores = self.registered_nodes.iter() + .map(|address| + { + let concat = address.to_string() + &self.random_seed.to_string(); + let hash = keccak256(concat).to_string(); + let without_prefix = hash.trim_start_matches("0x"); + let z = BigInt::from_str_radix(without_prefix, 16).unwrap(); + let score = z - BigInt::from(self.random_seed); + (score, *address) + }) + .collect::>(); + + println!("{:?}", scores); + + scores.sort_by(|a, b| a.0.cmp(&b.0)); + let result = scores[0..self.size].to_vec(); + result + } +} \ No newline at end of file diff --git a/packages/ciphernode/sortition/src/index.rs b/packages/ciphernode/sortition/src/index.rs new file mode 100644 index 00000000..e69de29b diff --git a/packages/ciphernode/sortition/src/lib.rs b/packages/ciphernode/sortition/src/lib.rs new file mode 100644 index 00000000..6a99e9df --- /dev/null +++ b/packages/ciphernode/sortition/src/lib.rs @@ -0,0 +1,9 @@ +#![crate_name = "sortition"] +#![crate_type = "lib"] +// #![warn(missing_docs, unused_imports)] + +mod distance; +mod index; + +pub use distance::*; +pub use index::*; \ No newline at end of file