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

Decryption -> PlaintextAggregator & CommitteeKey -> PublicKeyAggregator #46

Merged
merged 1 commit into from
Sep 8, 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
29 changes: 19 additions & 10 deletions packages/ciphernode/core/src/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::collections::HashMap;
use actix::{Actor, Addr, Context, Handler, Message};

use crate::{
committee_key::{CommitteeKey},
decryption::Decryption,
plaintext_aggregator::PlaintextAggregator,
eventbus::EventBus,
events::{E3id, EnclaveEvent},
fhe::Fhe,
publickey_aggregator::PublicKeyAggregator,
Subscribe,
};

Expand All @@ -22,8 +22,8 @@ pub struct CommitteeManager {
bus: Addr<EventBus>,
fhe: Addr<Fhe>,

keys: HashMap<E3id, Addr<CommitteeKey>>,
decryptions: HashMap<E3id, Addr<Decryption>>,
keys: HashMap<E3id, Addr<PublicKeyAggregator>>,
decryptions: HashMap<E3id, Addr<PlaintextAggregator>>,
meta: HashMap<E3id, CommitteeMeta>,
}

Expand All @@ -49,9 +49,18 @@ impl CommitteeManager {
addr.clone().recipient(),
));
bus.do_send(Subscribe::new("KeyshareCreated", addr.clone().into()));
bus.do_send(Subscribe::new("CiphertextOutputPublished", addr.clone().into()));
bus.do_send(Subscribe::new("DecryptionshareCreated", addr.clone().into()));
bus.do_send(Subscribe::new("DecryptionOutputPublished", addr.clone().into()));
bus.do_send(Subscribe::new(
"CiphertextOutputPublished",
addr.clone().into(),
));
bus.do_send(Subscribe::new(
"DecryptionshareCreated",
addr.clone().into(),
));
bus.do_send(Subscribe::new(
"PlaintextAggregated",
addr.clone().into(),
));
addr
}
}
Expand All @@ -63,7 +72,7 @@ impl Handler<EnclaveEvent> for CommitteeManager {
match event {
EnclaveEvent::CommitteeRequested { data, .. } => {
// start up a new key
let key = CommitteeKey::new(
let key = PublicKeyAggregator::new(
self.fhe.clone(),
self.bus.clone(),
data.e3_id.clone(),
Expand Down Expand Up @@ -99,7 +108,7 @@ impl Handler<EnclaveEvent> for CommitteeManager {
return;
};
// start up a new key
let key = Decryption::new(
let key = PlaintextAggregator::new(
self.fhe.clone(),
self.bus.clone(),
data.e3_id.clone(),
Expand All @@ -114,7 +123,7 @@ impl Handler<EnclaveEvent> for CommitteeManager {
decryption.do_send(data);
}
}
EnclaveEvent::DecryptedOutputPublished { data, .. } => {
EnclaveEvent::PlaintextAggregated { data, .. } => {
let Some(addr) = self.decryptions.get(&data.e3_id) else {
return;
};
Expand Down
14 changes: 7 additions & 7 deletions packages/ciphernode/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub enum EnclaveEvent {
id: EventId,
data: DecryptionshareCreated,
},
DecryptedOutputPublished {
PlaintextAggregated {
id: EventId,
data: DecryptedOutputPublished
data: PlaintextAggregated
}

// CommitteeSelected,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl From<EnclaveEvent> for EventId {
EnclaveEvent::PublicKeyAggregated { id, .. } => id,
EnclaveEvent::CiphertextOutputPublished { id, .. } => id,
EnclaveEvent::DecryptionshareCreated { id, .. } => id,
EnclaveEvent::DecryptedOutputPublished { id, .. } => id,
EnclaveEvent::PlaintextAggregated { id, .. } => id,
}
}
}
Expand Down Expand Up @@ -154,9 +154,9 @@ impl From<DecryptionshareCreated> for EnclaveEvent {
}
}

impl From<DecryptedOutputPublished> for EnclaveEvent {
fn from(data: DecryptedOutputPublished) -> Self {
EnclaveEvent::DecryptedOutputPublished {
impl From<PlaintextAggregated> for EnclaveEvent {
fn from(data: PlaintextAggregated) -> Self {
EnclaveEvent::PlaintextAggregated {
id: EventId::from(data.clone()),
data: data.clone(),
}
Expand Down Expand Up @@ -211,7 +211,7 @@ pub struct CiphertextOutputPublished {

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct DecryptedOutputPublished {
pub struct PlaintextAggregated {
pub e3_id: E3id,
pub decrypted_output: Vec<u8>
}
Expand Down
12 changes: 6 additions & 6 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

mod ciphernode;
mod committee;
mod committee_key;
mod publickey_aggregator;
mod data;
mod decryption;
mod plaintext_aggregator;
mod enclave_contract;
mod eventbus;
mod events;
Expand All @@ -20,7 +20,7 @@ mod serializers;
pub use actix::prelude::*;
pub use ciphernode::*;
pub use committee::*;
pub use committee_key::*;
pub use publickey_aggregator::*;
pub use data::*;
pub use eventbus::*;
pub use events::*;
Expand All @@ -31,7 +31,7 @@ pub use p2p::*;
pub use actix::prelude::*;
pub use ciphernode::*;
pub use committee::*;
pub use committee_key::*;
pub use publickey_aggregator::*;
pub use data::*;
pub use eventbus::*;
pub use events::*;
Expand Down Expand Up @@ -70,7 +70,7 @@ mod tests {
CiphertextSerializer, DecryptionShareSerializer, PublicKeySerializer,
PublicKeyShareSerializer,
},
DecryptedOutputPublished, CiphertextOutputPublished, DecryptionshareCreated, ResetHistory,
PlaintextAggregated, CiphertextOutputPublished, DecryptionshareCreated, ResetHistory,
};
use actix::prelude::*;
use anyhow::*;
Expand Down Expand Up @@ -287,7 +287,7 @@ mod tests {
decryption_share: ds3.clone(),
e3_id: e3_id.clone(),
}),
EnclaveEvent::from(DecryptedOutputPublished {
EnclaveEvent::from(PlaintextAggregated {
e3_id: e3_id.clone(),
decrypted_output: expected_raw_plaintext.clone()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
ordered_set::OrderedSet, DecryptedOutputPublished, DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, Fhe, GetAggregatePlaintext
ordered_set::OrderedSet, PlaintextAggregated, DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, Fhe, GetAggregatePlaintext
};
use actix::prelude::*;
use anyhow::{anyhow, Result};

#[derive(Debug, Clone)]
pub enum DecryptionState {
pub enum PlaintextAggregatorState {
Collecting {
nodecount: usize,
shares: OrderedSet<Vec<u8>>,
Expand All @@ -25,65 +25,65 @@ struct ComputeAggregate {
pub shares: OrderedSet<Vec<u8>>,
}

pub struct Decryption {
pub struct PlaintextAggregator {
fhe: Addr<Fhe>,
bus: Addr<EventBus>,
e3_id: E3id,
state: DecryptionState,
state: PlaintextAggregatorState,
}

impl Decryption {
impl PlaintextAggregator {
pub fn new(fhe: Addr<Fhe>, bus: Addr<EventBus>, e3_id: E3id, nodecount: usize) -> Self {
Decryption {
PlaintextAggregator {
fhe,
bus,
e3_id,
state: DecryptionState::Collecting {
state: PlaintextAggregatorState::Collecting {
nodecount,
shares: OrderedSet::new(),
},
}
}

pub fn add_share(&mut self, share: Vec<u8>) -> Result<DecryptionState> {
let DecryptionState::Collecting { nodecount, shares } = &mut self.state else {
pub fn add_share(&mut self, share: Vec<u8>) -> Result<PlaintextAggregatorState> {
let PlaintextAggregatorState::Collecting { nodecount, shares } = &mut self.state else {
return Err(anyhow::anyhow!("Can only add share in Collecting state"));
};

shares.insert(share);
if shares.len() == *nodecount {
return Ok(DecryptionState::Computing {
return Ok(PlaintextAggregatorState::Computing {
shares: shares.clone(),
});
}

Ok(self.state.clone())
}

pub fn set_decryption(&mut self, decrypted: Vec<u8>) -> Result<DecryptionState> {
let DecryptionState::Computing { shares } = &mut self.state else {
pub fn set_decryption(&mut self, decrypted: Vec<u8>) -> Result<PlaintextAggregatorState> {
let PlaintextAggregatorState::Computing { shares } = &mut self.state else {
return Ok(self.state.clone());
};

let shares = shares.to_owned();

Ok(DecryptionState::Complete { decrypted, shares })
Ok(PlaintextAggregatorState::Complete { decrypted, shares })
}
}

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

impl Handler<DecryptionshareCreated> for Decryption {
impl Handler<DecryptionshareCreated> for PlaintextAggregator {
type Result = Result<()>;
fn handle(&mut self, event: DecryptionshareCreated, ctx: &mut Self::Context) -> Self::Result {
if event.e3_id != self.e3_id {
return Err(anyhow!(
"Wrong e3_id sent to aggregator. This should not happen."
));
}
let DecryptionState::Collecting { .. } = self.state else {
let PlaintextAggregatorState::Collecting { .. } = self.state else {
return Err(anyhow!(
"Aggregator has been closed for collecting keyshares."
));
Expand All @@ -93,7 +93,7 @@ impl Handler<DecryptionshareCreated> for Decryption {
self.state = self.add_share(event.decryption_share)?;

// Check the state and if it has changed to the computing
if let DecryptionState::Computing { shares } = &self.state {
if let PlaintextAggregatorState::Computing { shares } = &self.state {
ctx.address().do_send(ComputeAggregate {
shares: shares.clone(),
})
Expand All @@ -103,7 +103,7 @@ impl Handler<DecryptionshareCreated> for Decryption {
}
}

impl Handler<ComputeAggregate> for Decryption {
impl Handler<ComputeAggregate> for PlaintextAggregator {
type Result = ResponseActFuture<Self, Result<()>>;
fn handle(&mut self, msg: ComputeAggregate, ctx: &mut Self::Context) -> Self::Result {
Box::pin(
Expand All @@ -118,7 +118,7 @@ impl Handler<ComputeAggregate> for Decryption {
act.state = act.set_decryption(decrypted_output.clone())?;

// Dispatch the PublicKeyAggregated event
let event = EnclaveEvent::from(DecryptedOutputPublished {
let event = EnclaveEvent::from(PlaintextAggregated {
decrypted_output,
e3_id: act.e3_id.clone(),
});
Expand All @@ -131,7 +131,7 @@ impl Handler<ComputeAggregate> for Decryption {
}
}

impl Handler<Die> for Decryption {
impl Handler<Die> for PlaintextAggregator {
type Result = ();

fn handle(&mut self, _msg: Die, ctx: &mut Context<Self>) {
Expand Down
Loading
Loading