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

Add ciphernode core unintegrated #11

Merged
merged 1 commit into from
Aug 22, 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
119 changes: 106 additions & 13 deletions packages/ciphernode/Cargo.lock

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

11 changes: 10 additions & 1 deletion packages/ciphernode/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "core"
name = "enclave-core"
version = "0.1.0"
edition = "2021"
description = ": coordinates the encryption and decryption of enclave computations"
Expand All @@ -13,4 +13,13 @@ libp2p = "0.53.2"
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" }
actix = "0.13.5"
actix-rt = "2.10.0"
anyhow = "1.0.86"
rand = "0.8.5"
rand_chacha = "0.3.1"
secp256k1 = "0.29.0"
tokio = { version = "1.39.3", features = ["full"] }
sha2 = "0.10.8"
bs58 = "0.5.1"

81 changes: 81 additions & 0 deletions packages/ciphernode/core/src/ciphernode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{
data::{Data, Insert},
eventbus::EventBus,
events::{ComputationRequested, EnclaveEvent, KeyshareCreated},
fhe::{Fhe, GenerateKeyshare},
};
use actix::prelude::*;
use anyhow::Result;

pub struct Ciphernode {
fhe: Addr<Fhe>,
data: Addr<Data>,
bus: Addr<EventBus>,
}

impl Actor for Ciphernode {
type Context = Context<Self>;
}

impl Ciphernode {
pub fn new(bus: Addr<EventBus>, fhe: Addr<Fhe>, data: Addr<Data>) -> Self {
Self { bus, fhe, data }
}
}

impl Handler<EnclaveEvent> for Ciphernode {
type Result = ();

fn handle(&mut self, event: EnclaveEvent, ctx: &mut Context<Self>) -> Self::Result {
match event {
EnclaveEvent::ComputationRequested { data, .. } => ctx.address().do_send(data),
_ => (),
}
}
}

impl Handler<ComputationRequested> for Ciphernode {
type Result = ResponseFuture<()>;

fn handle(&mut self, event: ComputationRequested, _: &mut Context<Self>) -> Self::Result {
let fhe = self.fhe.clone();
let data = self.data.clone();
let bus = self.bus.clone();
Box::pin(async {
on_computation_requested(fhe, data, bus, event)
.await
.unwrap()
})
}
}

async fn on_computation_requested(
fhe: Addr<Fhe>,
data: Addr<Data>,
bus: Addr<EventBus>,
event: ComputationRequested,
) -> Result<()> {
let ComputationRequested { e3_id, .. } = event;
// generate keyshare
let (sk, pubkey) = fhe.send(GenerateKeyshare {}).await??;

// TODO: decrypt from FHE actor
// save encrypted key against e3_id/sk
// reencrypt secretkey locally with env var - this is so we don't have to serialize a secret
// best practice would be as you boot up a node you enter in a configured password from
// which we derive a kdf which gets used to generate this key
data.do_send(Insert(format!("{}/sk", e3_id).into(), sk.unsafe_to_vec()));

// save public key against e3_id/pk
data.do_send(Insert(
format!("{}/pk", e3_id).into(),
pubkey.clone().into(),
));

// broadcast the KeyshareCreated message
let event = EnclaveEvent::from(KeyshareCreated { pubkey, e3_id });

bus.do_send(event);

Ok(())
}
65 changes: 65 additions & 0 deletions packages/ciphernode/core/src/committee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::collections::HashMap;

use actix::{Actor, Addr, Context, Handler};

use crate::{
committee_key::{CommitteeKey, Die},
eventbus::EventBus,
events::{E3id, EnclaveEvent},
fhe::Fhe,
};

pub struct Committee {
bus: Addr<EventBus>,
fhe: Addr<Fhe>,
aggregators: HashMap<E3id, Addr<CommitteeKey>>,
}

impl Actor for Committee {
type Context = Context<Self>;
}

impl Committee {
pub fn new(bus: Addr<EventBus>, fhe: Addr<Fhe>) -> Self {
Self {
bus,
fhe,
aggregators: HashMap::new(),
}
}
}

impl Handler<EnclaveEvent> for Committee {
type Result = ();

fn handle(&mut self, event: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result {
match event {
EnclaveEvent::ComputationRequested { data, .. } => {
// start up a new aggregator
let aggregator = CommitteeKey::new(
self.fhe.clone(),
self.bus.clone(),
data.e3_id.clone(),
data.nodecount,
)
.start();

self.aggregators.insert(data.e3_id, aggregator);
}
EnclaveEvent::KeyshareCreated { data, .. } => {
if let Some(aggregator) = self.aggregators.get(&data.e3_id) {
aggregator.do_send(data);
}
},
EnclaveEvent::PublicKeyAggregated { data, .. } => {
let Some(aggregator) = self.aggregators.get(&data.e3_id) else {
return;
};

aggregator.do_send(Die);
self.aggregators.remove(&data.e3_id);
}
// _ => (),
}
}
}
Loading
Loading