Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ng/sortition distance #54

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/ciphernode/enclave_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ 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" }
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" }
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"] }
7 changes: 7 additions & 0 deletions packages/ciphernode/enclave_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
___ ___ ___ ___ ___
/\__\ /\ \ /\__\ /\ \ ___ /\__\
Expand Down Expand Up @@ -34,6 +38,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
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();
Expand Down
26 changes: 26 additions & 0 deletions packages/ciphernode/sortition/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
34 changes: 34 additions & 0 deletions packages/ciphernode/sortition/src/distance.rs
Original file line number Diff line number Diff line change
@@ -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<Address>,
pub size: usize,
}

impl DistanceSortition {
pub fn new(random_seed: u64, registered_nodes: Vec<Address>, 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::<Vec<(BigInt, Address)>>();

println!("{:?}", scores);

scores.sort_by(|a, b| a.0.cmp(&b.0));
let result = scores[0..self.size].to_vec();
result
}
}
Empty file.
9 changes: 9 additions & 0 deletions packages/ciphernode/sortition/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Loading