generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ciphernode core unintegrated (#11)
- Loading branch information
Showing
12 changed files
with
1,313 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
// _ => (), | ||
} | ||
} | ||
} |
Oops, something went wrong.