From 60836ac1ae0972c01aa370ca4d9f6f54f0180c5b Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 8 Sep 2024 11:44:12 +1000 Subject: [PATCH] Decryption -> PlaintextAggregator & CommitteeKey -> PublicKeyAggregator --- packages/ciphernode/core/src/committee.rs | 29 +++++++++----- packages/ciphernode/core/src/events.rs | 14 +++---- packages/ciphernode/core/src/lib.rs | 12 +++--- ...{decryption.rs => plaintext_aggregator.rs} | 40 +++++++++---------- ...mmittee_key.rs => publickey_aggregator.rs} | 36 ++++++++--------- 5 files changed, 70 insertions(+), 61 deletions(-) rename packages/ciphernode/core/src/{decryption.rs => plaintext_aggregator.rs} (70%) rename packages/ciphernode/core/src/{committee_key.rs => publickey_aggregator.rs} (82%) diff --git a/packages/ciphernode/core/src/committee.rs b/packages/ciphernode/core/src/committee.rs index 807402ba..db0a3e25 100644 --- a/packages/ciphernode/core/src/committee.rs +++ b/packages/ciphernode/core/src/committee.rs @@ -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, }; @@ -22,8 +22,8 @@ pub struct CommitteeManager { bus: Addr, fhe: Addr, - keys: HashMap>, - decryptions: HashMap>, + keys: HashMap>, + decryptions: HashMap>, meta: HashMap, } @@ -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 } } @@ -63,7 +72,7 @@ impl Handler 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(), @@ -99,7 +108,7 @@ impl Handler 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(), @@ -114,7 +123,7 @@ impl Handler for CommitteeManager { decryption.do_send(data); } } - EnclaveEvent::DecryptedOutputPublished { data, .. } => { + EnclaveEvent::PlaintextAggregated { data, .. } => { let Some(addr) = self.decryptions.get(&data.e3_id) else { return; }; diff --git a/packages/ciphernode/core/src/events.rs b/packages/ciphernode/core/src/events.rs index d1949a37..9b95c310 100644 --- a/packages/ciphernode/core/src/events.rs +++ b/packages/ciphernode/core/src/events.rs @@ -71,9 +71,9 @@ pub enum EnclaveEvent { id: EventId, data: DecryptionshareCreated, }, -DecryptedOutputPublished { +PlaintextAggregated { id: EventId, - data: DecryptedOutputPublished + data: PlaintextAggregated } // CommitteeSelected, @@ -104,7 +104,7 @@ impl From for EventId { EnclaveEvent::PublicKeyAggregated { id, .. } => id, EnclaveEvent::CiphertextOutputPublished { id, .. } => id, EnclaveEvent::DecryptionshareCreated { id, .. } => id, - EnclaveEvent::DecryptedOutputPublished { id, .. } => id, + EnclaveEvent::PlaintextAggregated { id, .. } => id, } } } @@ -154,9 +154,9 @@ impl From for EnclaveEvent { } } -impl From for EnclaveEvent { - fn from(data: DecryptedOutputPublished) -> Self { - EnclaveEvent::DecryptedOutputPublished { +impl From for EnclaveEvent { + fn from(data: PlaintextAggregated) -> Self { + EnclaveEvent::PlaintextAggregated { id: EventId::from(data.clone()), data: data.clone(), } @@ -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 } diff --git a/packages/ciphernode/core/src/lib.rs b/packages/ciphernode/core/src/lib.rs index b24c285d..2ded47ad 100644 --- a/packages/ciphernode/core/src/lib.rs +++ b/packages/ciphernode/core/src/lib.rs @@ -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; @@ -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::*; @@ -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::*; @@ -70,7 +70,7 @@ mod tests { CiphertextSerializer, DecryptionShareSerializer, PublicKeySerializer, PublicKeyShareSerializer, }, - DecryptedOutputPublished, CiphertextOutputPublished, DecryptionshareCreated, ResetHistory, + PlaintextAggregated, CiphertextOutputPublished, DecryptionshareCreated, ResetHistory, }; use actix::prelude::*; use anyhow::*; @@ -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() }) diff --git a/packages/ciphernode/core/src/decryption.rs b/packages/ciphernode/core/src/plaintext_aggregator.rs similarity index 70% rename from packages/ciphernode/core/src/decryption.rs rename to packages/ciphernode/core/src/plaintext_aggregator.rs index ac2fa3a1..fd142550 100644 --- a/packages/ciphernode/core/src/decryption.rs +++ b/packages/ciphernode/core/src/plaintext_aggregator.rs @@ -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>, @@ -25,34 +25,34 @@ struct ComputeAggregate { pub shares: OrderedSet>, } -pub struct Decryption { +pub struct PlaintextAggregator { fhe: Addr, bus: Addr, e3_id: E3id, - state: DecryptionState, + state: PlaintextAggregatorState, } -impl Decryption { +impl PlaintextAggregator { pub fn new(fhe: Addr, bus: Addr, 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) -> Result { - let DecryptionState::Collecting { nodecount, shares } = &mut self.state else { + pub fn add_share(&mut self, share: Vec) -> Result { + 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(), }); } @@ -60,22 +60,22 @@ impl Decryption { Ok(self.state.clone()) } - pub fn set_decryption(&mut self, decrypted: Vec) -> Result { - let DecryptionState::Computing { shares } = &mut self.state else { + pub fn set_decryption(&mut self, decrypted: Vec) -> Result { + 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; } -impl Handler for Decryption { +impl Handler for PlaintextAggregator { type Result = Result<()>; fn handle(&mut self, event: DecryptionshareCreated, ctx: &mut Self::Context) -> Self::Result { if event.e3_id != self.e3_id { @@ -83,7 +83,7 @@ impl Handler for Decryption { "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." )); @@ -93,7 +93,7 @@ impl Handler 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(), }) @@ -103,7 +103,7 @@ impl Handler for Decryption { } } -impl Handler for Decryption { +impl Handler for PlaintextAggregator { type Result = ResponseActFuture>; fn handle(&mut self, msg: ComputeAggregate, ctx: &mut Self::Context) -> Self::Result { Box::pin( @@ -118,7 +118,7 @@ impl Handler 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(), }); @@ -131,7 +131,7 @@ impl Handler for Decryption { } } -impl Handler for Decryption { +impl Handler for PlaintextAggregator { type Result = (); fn handle(&mut self, _msg: Die, ctx: &mut Context) { diff --git a/packages/ciphernode/core/src/committee_key.rs b/packages/ciphernode/core/src/publickey_aggregator.rs similarity index 82% rename from packages/ciphernode/core/src/committee_key.rs rename to packages/ciphernode/core/src/publickey_aggregator.rs index aa31158d..001ed2d8 100644 --- a/packages/ciphernode/core/src/committee_key.rs +++ b/packages/ciphernode/core/src/publickey_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::{anyhow, Result}; #[derive(Debug, Clone)] -pub enum CommitteeKeyState { +pub enum PublicKeyAggregatorState { Collecting { nodecount: usize, keyshares: OrderedSet>, @@ -28,11 +28,11 @@ struct ComputeAggregate { pub keyshares: OrderedSet>, } -pub struct CommitteeKey { +pub struct PublicKeyAggregator { fhe: Addr, bus: Addr, e3_id: E3id, - state: CommitteeKeyState, + state: PublicKeyAggregatorState, } /// Aggregate PublicKey for a committee of nodes. This actor listens for KeyshareCreated events @@ -41,21 +41,21 @@ pub struct CommitteeKey { /// identical events will not be triggered twice. /// It is expected to change this mechanism as we work through adversarial scenarios and write tests /// for them. -impl CommitteeKey { +impl PublicKeyAggregator { pub fn new(fhe: Addr, bus: Addr, e3_id: E3id, nodecount: usize) -> Self { - CommitteeKey { + PublicKeyAggregator { fhe, bus, e3_id, - state: CommitteeKeyState::Collecting { + state: PublicKeyAggregatorState::Collecting { nodecount, keyshares: OrderedSet::new(), }, } } - pub fn add_keyshare(&mut self, keyshare: Vec) -> Result { - let CommitteeKeyState::Collecting { + pub fn add_keyshare(&mut self, keyshare: Vec) -> Result { + let PublicKeyAggregatorState::Collecting { nodecount, keyshares, } = &mut self.state @@ -65,7 +65,7 @@ impl CommitteeKey { keyshares.insert(keyshare); if keyshares.len() == *nodecount { - return Ok(CommitteeKeyState::Computing { + return Ok(PublicKeyAggregatorState::Computing { keyshares: keyshares.clone(), }); } @@ -73,25 +73,25 @@ impl CommitteeKey { Ok(self.state.clone()) } - pub fn set_pubkey(&mut self, pubkey: Vec) -> Result { - let CommitteeKeyState::Computing { keyshares } = &mut self.state else { + pub fn set_pubkey(&mut self, pubkey: Vec) -> Result { + let PublicKeyAggregatorState::Computing { keyshares } = &mut self.state else { return Ok(self.state.clone()); }; let keyshares = keyshares.to_owned(); - Ok(CommitteeKeyState::Complete { + Ok(PublicKeyAggregatorState::Complete { public_key: pubkey, keyshares, }) } } -impl Actor for CommitteeKey { +impl Actor for PublicKeyAggregator { type Context = Context; } -impl Handler for CommitteeKey { +impl Handler for PublicKeyAggregator { type Result = Result<()>; fn handle(&mut self, event: KeyshareCreated, ctx: &mut Self::Context) -> Self::Result { @@ -101,7 +101,7 @@ impl Handler for CommitteeKey { )); } - let CommitteeKeyState::Collecting { .. } = self.state else { + let PublicKeyAggregatorState::Collecting { .. } = self.state else { return Err(anyhow!( "Aggregator has been closed for collecting keyshares." )); @@ -111,7 +111,7 @@ impl Handler for CommitteeKey { self.state = self.add_keyshare(event.pubkey)?; // Check the state and if it has changed to the computing - if let CommitteeKeyState::Computing { keyshares } = &self.state { + if let PublicKeyAggregatorState::Computing { keyshares } = &self.state { ctx.address().do_send(ComputeAggregate { keyshares: keyshares.clone(), }) @@ -121,7 +121,7 @@ impl Handler for CommitteeKey { } } -impl Handler for CommitteeKey { +impl Handler for PublicKeyAggregator { type Result = ResponseActFuture>; fn handle(&mut self, msg: ComputeAggregate, _: &mut Self::Context) -> Self::Result { @@ -160,7 +160,7 @@ impl Handler for CommitteeKey { } } -impl Handler for CommitteeKey { +impl Handler for PublicKeyAggregator { type Result = (); fn handle(&mut self, _msg: Die, ctx: &mut Context) {