Skip to content

Commit

Permalink
Start ciphernode binary example first commit (wip) (#20)
Browse files Browse the repository at this point in the history
* Create a ciphernode binary example

* Logging was not accurate
  • Loading branch information
ryardley authored Aug 27, 2024
1 parent 52a28f1 commit 0054246
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/ciphernode/Cargo.lock

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

20 changes: 17 additions & 3 deletions packages/ciphernode/core/src/fhe.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{cmp::Ordering, hash::Hash, mem, sync::Arc};

use crate::ordered_set::OrderedSet;
use actix::{Actor, Context, Handler, Message};
use anyhow::*;
use fhe::{
bfv::{BfvParameters, PublicKey, SecretKey},
bfv::{BfvParameters, BfvParametersBuilder, PublicKey, SecretKey},
mbfv::{AggregateIter, CommonRandomPoly, PublicKeyShare},
};
use fhe_traits::{Deserialize, DeserializeParametrized, Serialize};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use serde::Serializer;

use crate::ordered_set::OrderedSet;

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)]
#[rtype(result = "Result<(WrappedSecretKey, WrappedPublicKeyShare)>")]
pub struct GenerateKeyshare {
Expand Down Expand Up @@ -232,6 +232,20 @@ impl Fhe {
) -> Result<Self> {
Ok(Self { params, crp, rng })
}
pub fn try_default() -> Result<Self> {
let moduli = &vec![0x3FFFFFFF000001];
let degree = 2048usize;
let plaintext_modulus = 1032193u64;
let mut rng = ChaCha20Rng::from_entropy();
let params = BfvParametersBuilder::new()
.set_degree(degree)
.set_plaintext_modulus(plaintext_modulus)
.set_moduli(&moduli)
.build_arc()?;
let crp = CommonRandomPoly::new(&params, &mut rng)?;

Ok(Fhe::new(params, crp, rng)?)
}
}

impl Handler<GenerateKeyshare> for Fhe {
Expand Down
10 changes: 10 additions & 0 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ mod fhe;
mod ordered_set;
mod p2p;

pub use data::*;
pub use ciphernode::*;
pub use committee::*;
pub use committee_key::*;
pub use eventbus::*;
pub use events::*;
pub use fhe::*;
pub use p2p::*;
pub use actix::prelude::*;

// pub struct Core {
// pub name: String,
// }
Expand Down
22 changes: 21 additions & 1 deletion packages/ciphernode/core/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashSet, error::Error};

/// Actor for connecting to an libp2p client via it's mpsc channel interface
/// This Actor should be responsible for
Expand All @@ -7,6 +7,8 @@ use std::collections::HashSet;
/// 3. Broadcasting over the local eventbus
/// 4. Listening to the local eventbus for messages to be published to libp2p
use actix::prelude::*;
use anyhow::anyhow;
use p2p::EnclaveRouter;
use tokio::sync::mpsc::{Receiver, Sender};

use crate::{
Expand Down Expand Up @@ -64,6 +66,24 @@ impl P2p {
// Return the address
p2p
}

pub fn spawn_libp2p(
bus: Addr<EventBus>,
) -> Result<
(
Addr<Self>,
tokio::task::JoinHandle<()>,
),
Box<dyn Error>,
> {
let (mut libp2p, tx, rx) = EnclaveRouter::new()?;
libp2p.connect_swarm("mdns".to_string())?;
libp2p.join_topic("enclave-keygen-01")?;

let p2p_addr = Self::spawn_and_listen(bus, tx, rx);
let handle = tokio::spawn(async move { libp2p.start().await.unwrap() });
Ok((p2p_addr, handle))
}
}

impl Handler<LibP2pEvent> for P2p {
Expand Down
2 changes: 2 additions & 0 deletions packages/ciphernode/enclave_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ repository = "https://github.com/gnosisguild/enclave/packages/ciphernode"
[dependencies]
eth = { path = "../eth" }
p2p = { path = "../p2p" }
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"
20 changes: 20 additions & 0 deletions packages/ciphernode/enclave_node/src/bin/ciphernode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::error::Error;

use enclave_core::Actor;
use enclave_core::Ciphernode;
use enclave_core::Data;
use enclave_core::EventBus;
use enclave_core::Fhe;
use enclave_core::P2p;

#[actix_rt::main]
async fn main() -> Result<(), Box<dyn Error>> {
let fhe = Fhe::try_default()?.start();
let bus = EventBus::new(true).start();
let data = Data::new(true).start(); // TODO: Use a sled backed Data Actor
let _node = Ciphernode::new(bus.clone(), fhe.clone(), data.clone()).start();
let (_, h) = P2p::spawn_libp2p(bus.clone())?;
println!("Ciphernode");
let _ = tokio::join!(h);
Ok(())
}

0 comments on commit 0054246

Please sign in to comment.