From 71697e2854d63e9db1c9a5ab03783ecde3777911 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 17:45:23 +1000 Subject: [PATCH 01/10] Remove redundant imports --- packages/ciphernode/core/src/lib.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/ciphernode/core/src/lib.rs b/packages/ciphernode/core/src/lib.rs index def9c502..25e9ffd3 100644 --- a/packages/ciphernode/core/src/lib.rs +++ b/packages/ciphernode/core/src/lib.rs @@ -28,17 +28,7 @@ pub use fhe::*; pub use logger::*; pub use p2p::*; pub use publickey_aggregator::*; - -pub use actix::prelude::*; -pub use ciphernode::*; pub use ciphernode_selector::*; -pub use ciphernode_supervisor::*; -pub use data::*; -pub use eventbus::*; -pub use events::*; -pub use fhe::*; -pub use p2p::*; -pub use publickey_aggregator::*; // TODO: move these out to a test folder #[cfg(test)] From b458b2a20512b8b0e8c2901b6edb8843efc9fa33 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 19:19:19 +1000 Subject: [PATCH 02/10] Add sequencers that do nothing but spawn children and send them messages --- .../core/src/ciphernode_sequencer.rs | 31 +++++++++++++++++ .../core/src/ciphernode_supervisor.rs | 2 +- packages/ciphernode/core/src/lib.rs | 3 ++ .../core/src/plaintext_aggregator.rs | 10 ++++++ .../core/src/plaintext_sequencer.rs | 33 +++++++++++++++++++ .../core/src/publickey_sequencer.rs | 33 +++++++++++++++++++ packages/ciphernode/core/src/registry.rs | 9 +++++ 7 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 packages/ciphernode/core/src/ciphernode_sequencer.rs create mode 100644 packages/ciphernode/core/src/plaintext_sequencer.rs create mode 100644 packages/ciphernode/core/src/publickey_sequencer.rs create mode 100644 packages/ciphernode/core/src/registry.rs diff --git a/packages/ciphernode/core/src/ciphernode_sequencer.rs b/packages/ciphernode/core/src/ciphernode_sequencer.rs new file mode 100644 index 00000000..602a6c45 --- /dev/null +++ b/packages/ciphernode/core/src/ciphernode_sequencer.rs @@ -0,0 +1,31 @@ +// sequence and persist events for a single E3 request in the correct order +// TODO: spawn and store a ciphernode upon start and forward all events to it in order +// TODO: if the ciphernode fails restart the node by replaying all stored events back to it + +use actix::prelude::*; + +use crate::{Ciphernode, Data, EnclaveEvent, EventBus, Fhe}; + +pub struct CiphernodeSequencer { + fhe: Addr, + data: Addr, + bus: Addr, + ciphernode: Option>, +} + +impl Actor for CiphernodeSequencer { + type Context = Context; +} + +impl Handler for CiphernodeSequencer { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + let bus = self.bus.clone(); + let fhe = self.fhe.clone(); + let data = self.data.clone(); + let sink = self + .ciphernode + .get_or_insert_with(|| Ciphernode::new(bus, fhe, data).start()); + sink.do_send(msg); + } +} diff --git a/packages/ciphernode/core/src/ciphernode_supervisor.rs b/packages/ciphernode/core/src/ciphernode_supervisor.rs index ebe3d935..35e1d099 100644 --- a/packages/ciphernode/core/src/ciphernode_supervisor.rs +++ b/packages/ciphernode/core/src/ciphernode_supervisor.rs @@ -120,7 +120,7 @@ impl Handler for CiphernodeSupervisor { } EnclaveEvent::DecryptionshareCreated { data, .. } => { if let Some(decryption) = self.plaintext_aggregators.get(&data.e3_id) { - decryption.do_send(data); + decryption.do_send(EnclaveEvent::from(data)); } } EnclaveEvent::PlaintextAggregated { data, .. } => { diff --git a/packages/ciphernode/core/src/lib.rs b/packages/ciphernode/core/src/lib.rs index 25e9ffd3..0e15ff3a 100644 --- a/packages/ciphernode/core/src/lib.rs +++ b/packages/ciphernode/core/src/lib.rs @@ -16,6 +16,8 @@ mod p2p; mod plaintext_aggregator; mod publickey_aggregator; mod serializers; +mod ciphernode_sequencer; +mod plaintext_sequencer; // TODO: this is too permissive pub use actix::prelude::*; @@ -28,6 +30,7 @@ pub use fhe::*; pub use logger::*; pub use p2p::*; pub use publickey_aggregator::*; +pub use plaintext_aggregator::*; pub use ciphernode_selector::*; // TODO: move these out to a test folder diff --git a/packages/ciphernode/core/src/plaintext_aggregator.rs b/packages/ciphernode/core/src/plaintext_aggregator.rs index fd142550..c95386d0 100644 --- a/packages/ciphernode/core/src/plaintext_aggregator.rs +++ b/packages/ciphernode/core/src/plaintext_aggregator.rs @@ -75,6 +75,16 @@ impl Actor for PlaintextAggregator { type Context = Context; } +impl Handler for PlaintextAggregator { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + match msg { + EnclaveEvent::DecryptionshareCreated { data, .. } => ctx.notify(data), + _ => () + } + } +} + impl Handler for PlaintextAggregator { type Result = Result<()>; fn handle(&mut self, event: DecryptionshareCreated, ctx: &mut Self::Context) -> Self::Result { diff --git a/packages/ciphernode/core/src/plaintext_sequencer.rs b/packages/ciphernode/core/src/plaintext_sequencer.rs new file mode 100644 index 00000000..bb8f2576 --- /dev/null +++ b/packages/ciphernode/core/src/plaintext_sequencer.rs @@ -0,0 +1,33 @@ +// sequence and persist events for a single E3 request in the correct order +// TODO: spawn and store a ciphernode upon start and forward all events to it in order +// TODO: if the ciphernode fails restart the node by replaying all stored events back to it + +use actix::prelude::*; + +use crate::{Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextAggregator}; + +pub struct CiphernodeSequencer { + fhe: Addr, + e3_id: E3id, + bus: Addr, + nodecount: usize, + child: Option>, +} + +impl Actor for CiphernodeSequencer { + type Context = Context; +} + +impl Handler for CiphernodeSequencer { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + let fhe = self.fhe.clone(); + let bus = self.bus.clone(); + let nodecount = self.nodecount; + let e3_id = self.e3_id.clone(); + let sink = self.child.get_or_insert_with(|| { + PlaintextAggregator::new(fhe, bus, e3_id, nodecount).start() + }); + sink.do_send(msg); + } +} diff --git a/packages/ciphernode/core/src/publickey_sequencer.rs b/packages/ciphernode/core/src/publickey_sequencer.rs new file mode 100644 index 00000000..035a3413 --- /dev/null +++ b/packages/ciphernode/core/src/publickey_sequencer.rs @@ -0,0 +1,33 @@ +// sequence and persist events for a single E3 request in the correct order +// TODO: spawn and store a ciphernode upon start and forward all events to it in order +// TODO: if the ciphernode fails restart the node by replaying all stored events back to it + +use actix::prelude::*; + +use crate::{Data, E3id, EnclaveEvent, EventBus, Fhe, PublicKeyAggregator}; + +pub struct PublicKeySequencer { + fhe: Addr, + e3_id: E3id, + bus: Addr, + nodecount: usize, + child: Option>, +} + +impl Actor for PublicKeySequencer { + type Context = Context; +} + +impl Handler for PublicKeySequencer { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + let fhe = self.fhe.clone(); + let bus = self.bus.clone(); + let nodecount = self.nodecount; + let e3_id = self.e3_id.clone(); + let sink = self.child.get_or_insert_with(|| { + PublicKeyAggregator::new(fhe, bus, e3_id, nodecount).start() + }); + sink.do_send(msg); + } +} diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs new file mode 100644 index 00000000..847410c7 --- /dev/null +++ b/packages/ciphernode/core/src/registry.rs @@ -0,0 +1,9 @@ +use std::collections::HashMap; + +use crate::{ciphernode_sequencer::CiphernodeSequencer, plaintext_sequencer::PlaintextSequencer, publickey_sequencer::PublicKeySequencer, E3id}; + +struct Registry { + pub public_keys: HashMap, + pub plaintexts: HashMap, + pub ciphernodes: HashMap +} From 16bee4fde5c8318bfa2c29271f050dd6745648e7 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 21:10:39 +1000 Subject: [PATCH 03/10] Setting up Registry --- .../core/src/ciphernode_supervisor.rs | 2 +- packages/ciphernode/core/src/events.rs | 14 +++++++++ packages/ciphernode/core/src/lib.rs | 5 +++ .../core/src/plaintext_aggregator.rs | 2 +- .../core/src/plaintext_sequencer.rs | 14 ++++----- .../core/src/publickey_aggregator.rs | 12 ++++++- .../core/src/publickey_sequencer.rs | 18 +++++++++-- packages/ciphernode/core/src/registry.rs | 31 ++++++++++++++++--- 8 files changed, 80 insertions(+), 18 deletions(-) diff --git a/packages/ciphernode/core/src/ciphernode_supervisor.rs b/packages/ciphernode/core/src/ciphernode_supervisor.rs index 35e1d099..7fea6c99 100644 --- a/packages/ciphernode/core/src/ciphernode_supervisor.rs +++ b/packages/ciphernode/core/src/ciphernode_supervisor.rs @@ -89,7 +89,7 @@ impl Handler for CiphernodeSupervisor { } EnclaveEvent::KeyshareCreated { data, .. } => { if let Some(key) = self.publickey_aggregators.get(&data.e3_id) { - key.do_send(data); + key.do_send(EnclaveEvent::from(data)); } } EnclaveEvent::PublicKeyAggregated { data, .. } => { diff --git a/packages/ciphernode/core/src/events.rs b/packages/ciphernode/core/src/events.rs index 95a8dcf2..ff6300fd 100644 --- a/packages/ciphernode/core/src/events.rs +++ b/packages/ciphernode/core/src/events.rs @@ -120,6 +120,20 @@ impl From for EventId { } } +impl From for E3id { + fn from(value: EnclaveEvent) -> Self { + match value { + EnclaveEvent::KeyshareCreated { data, .. } => data.e3_id, + EnclaveEvent::CommitteeRequested { data, .. } => data.e3_id, + EnclaveEvent::PublicKeyAggregated { data, .. } => data.e3_id, + EnclaveEvent::CiphertextOutputPublished { data, .. } => data.e3_id, + EnclaveEvent::DecryptionshareCreated { data, .. } => data.e3_id, + EnclaveEvent::PlaintextAggregated { data, .. } => data.e3_id, + EnclaveEvent::CiphernodeSelected { data, .. } => data.e3_id, + } + } +} + impl From for EnclaveEvent { fn from(data: KeyshareCreated) -> Self { EnclaveEvent::KeyshareCreated { diff --git a/packages/ciphernode/core/src/lib.rs b/packages/ciphernode/core/src/lib.rs index 0e15ff3a..cc2b4bf9 100644 --- a/packages/ciphernode/core/src/lib.rs +++ b/packages/ciphernode/core/src/lib.rs @@ -18,6 +18,8 @@ mod publickey_aggregator; mod serializers; mod ciphernode_sequencer; mod plaintext_sequencer; +mod publickey_sequencer; +mod registry; // TODO: this is too permissive pub use actix::prelude::*; @@ -30,8 +32,11 @@ pub use fhe::*; pub use logger::*; pub use p2p::*; pub use publickey_aggregator::*; +pub use publickey_sequencer::*; +pub use plaintext_sequencer::*; pub use plaintext_aggregator::*; pub use ciphernode_selector::*; +pub use ciphernode_sequencer::*; // TODO: move these out to a test folder #[cfg(test)] diff --git a/packages/ciphernode/core/src/plaintext_aggregator.rs b/packages/ciphernode/core/src/plaintext_aggregator.rs index c95386d0..9b5789c1 100644 --- a/packages/ciphernode/core/src/plaintext_aggregator.rs +++ b/packages/ciphernode/core/src/plaintext_aggregator.rs @@ -104,7 +104,7 @@ impl Handler for PlaintextAggregator { // Check the state and if it has changed to the computing if let PlaintextAggregatorState::Computing { shares } = &self.state { - ctx.address().do_send(ComputeAggregate { + ctx.notify(ComputeAggregate { shares: shares.clone(), }) } diff --git a/packages/ciphernode/core/src/plaintext_sequencer.rs b/packages/ciphernode/core/src/plaintext_sequencer.rs index bb8f2576..af95a51a 100644 --- a/packages/ciphernode/core/src/plaintext_sequencer.rs +++ b/packages/ciphernode/core/src/plaintext_sequencer.rs @@ -4,9 +4,9 @@ use actix::prelude::*; -use crate::{Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextAggregator}; +use crate::{E3id, EnclaveEvent, EventBus, Fhe, PlaintextAggregator}; -pub struct CiphernodeSequencer { +pub struct PlaintextSequencer { fhe: Addr, e3_id: E3id, bus: Addr, @@ -14,20 +14,20 @@ pub struct CiphernodeSequencer { child: Option>, } -impl Actor for CiphernodeSequencer { +impl Actor for PlaintextSequencer { type Context = Context; } -impl Handler for CiphernodeSequencer { +impl Handler for PlaintextSequencer { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { let fhe = self.fhe.clone(); let bus = self.bus.clone(); let nodecount = self.nodecount; let e3_id = self.e3_id.clone(); - let sink = self.child.get_or_insert_with(|| { - PlaintextAggregator::new(fhe, bus, e3_id, nodecount).start() - }); + let sink = self + .child + .get_or_insert_with(|| PlaintextAggregator::new(fhe, bus, e3_id, nodecount).start()); sink.do_send(msg); } } diff --git a/packages/ciphernode/core/src/publickey_aggregator.rs b/packages/ciphernode/core/src/publickey_aggregator.rs index 001ed2d8..c49339a9 100644 --- a/packages/ciphernode/core/src/publickey_aggregator.rs +++ b/packages/ciphernode/core/src/publickey_aggregator.rs @@ -91,6 +91,16 @@ impl Actor for PublicKeyAggregator { type Context = Context; } +impl Handler for PublicKeyAggregator { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + match msg { + EnclaveEvent::KeyshareCreated { data, .. } => ctx.notify(data), + _ => () + } + } +} + impl Handler for PublicKeyAggregator { type Result = Result<()>; @@ -112,7 +122,7 @@ impl Handler for PublicKeyAggregator { // Check the state and if it has changed to the computing if let PublicKeyAggregatorState::Computing { keyshares } = &self.state { - ctx.address().do_send(ComputeAggregate { + ctx.notify(ComputeAggregate { keyshares: keyshares.clone(), }) } diff --git a/packages/ciphernode/core/src/publickey_sequencer.rs b/packages/ciphernode/core/src/publickey_sequencer.rs index 035a3413..68b0b4a4 100644 --- a/packages/ciphernode/core/src/publickey_sequencer.rs +++ b/packages/ciphernode/core/src/publickey_sequencer.rs @@ -14,6 +14,18 @@ pub struct PublicKeySequencer { child: Option>, } +impl PublicKeySequencer { + fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { + Self { + fhe, + e3_id, + bus, + nodecount, + child: None, + } + } +} + impl Actor for PublicKeySequencer { type Context = Context; } @@ -25,9 +37,9 @@ impl Handler for PublicKeySequencer { let bus = self.bus.clone(); let nodecount = self.nodecount; let e3_id = self.e3_id.clone(); - let sink = self.child.get_or_insert_with(|| { - PublicKeyAggregator::new(fhe, bus, e3_id, nodecount).start() - }); + let sink = self + .child + .get_or_insert_with(|| PublicKeyAggregator::new(fhe, bus, e3_id, nodecount).start()); sink.do_send(msg); } } diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index 847410c7..8f93d8d0 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -1,9 +1,30 @@ use std::collections::HashMap; -use crate::{ciphernode_sequencer::CiphernodeSequencer, plaintext_sequencer::PlaintextSequencer, publickey_sequencer::PublicKeySequencer, E3id}; +use actix::prelude::*; -struct Registry { - pub public_keys: HashMap, - pub plaintexts: HashMap, - pub ciphernodes: HashMap +use crate::{CiphernodeSequencer, E3id, EnclaveEvent, Fhe, PlaintextSequencer, PublicKeySequencer}; + +pub struct Registry { + pub public_keys: HashMap>, + pub plaintexts: HashMap>, + pub ciphernodes: HashMap>, + pub fhes: HashMap> +} + +impl Actor for Registry { + type Context = Context; } + +// impl Handler for Registry { +// type Result = (); +// fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { +// let e3_id = E3id::from(msg.clone()); +// match msg { +// EnclaveEvent::CommitteeRequested { data, .. } => { +// self.fhes.entry(e3_id).or_insert_with(|| Fhe::try +// self.public_keys.entry(e3_id).or_insert_with(|| PublicKeySequencer::new()); +// }, +// _ => () +// } +// } +// } From 8055e54eb6de7a06138fbc77d31d5724d48f75a2 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 21:20:11 +1000 Subject: [PATCH 04/10] Fix up Fhe --- packages/ciphernode/core/src/fhe.rs | 30 +++++++++++++++++------- packages/ciphernode/core/src/lib.rs | 2 +- packages/ciphernode/core/src/registry.rs | 10 ++++++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/ciphernode/core/src/fhe.rs b/packages/ciphernode/core/src/fhe.rs index 18a526be..f78cede0 100644 --- a/packages/ciphernode/core/src/fhe.rs +++ b/packages/ciphernode/core/src/fhe.rs @@ -53,18 +53,30 @@ impl Actor for Fhe { } impl Fhe { - pub fn new( - params: Arc, - crp: CommonRandomPoly, - rng: ChaCha20Rng, - ) -> Result { - Ok(Self { params, crp, rng }) + pub fn new(params: Arc, crp: CommonRandomPoly, rng: ChaCha20Rng) -> Self { + Self { params, crp, rng } } - pub fn try_default() -> Result { + + pub fn try_default() -> Result { let moduli = &vec![0x3FFFFFFF000001]; let degree = 2048usize; let plaintext_modulus = 1032193u64; - let mut rng = ChaCha20Rng::from_entropy(); + let rng = ChaCha20Rng::from_entropy(); + + Ok(Fhe::from_raw_params( + moduli, + degree, + plaintext_modulus, + rng, + )?) + } + + pub fn from_raw_params( + moduli: &[u64], + degree: usize, + plaintext_modulus: u64, + mut rng: ChaCha20Rng, + ) -> Result { let params = BfvParametersBuilder::new() .set_degree(degree) .set_plaintext_modulus(plaintext_modulus) @@ -72,7 +84,7 @@ impl Fhe { .build_arc()?; let crp = CommonRandomPoly::new(¶ms, &mut rng)?; - Ok(Fhe::new(params, crp, rng)?) + Ok(Fhe::new(params, crp, rng)) } } diff --git a/packages/ciphernode/core/src/lib.rs b/packages/ciphernode/core/src/lib.rs index cc2b4bf9..9cd3e63a 100644 --- a/packages/ciphernode/core/src/lib.rs +++ b/packages/ciphernode/core/src/lib.rs @@ -127,7 +127,7 @@ mod tests { ) -> Result<(Addr, Arc, CommonRandomPoly)> { let (params, crp) = setup_bfv_params(&moduli, degree, plaintext_modulus, rng1)?; Ok(( - Fhe::new(params.clone(), crp.clone(), rng2)?.start(), + Fhe::new(params.clone(), crp.clone(), rng2).start(), params, crp, )) diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index 8f93d8d0..8886c304 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -14,14 +14,20 @@ pub struct Registry { impl Actor for Registry { type Context = Context; } - +// // impl Handler for Registry { // type Result = (); // fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { // let e3_id = E3id::from(msg.clone()); // match msg { // EnclaveEvent::CommitteeRequested { data, .. } => { -// self.fhes.entry(e3_id).or_insert_with(|| Fhe::try +// +// let moduli = &vec![0x3FFFFFFF000001]; +// let degree = 2048; +// let plaintext_modulus = 1032193; +// +// +// self.fhes.entry(e3_id).or_insert_with(|| Fhe::( // self.public_keys.entry(e3_id).or_insert_with(|| PublicKeySequencer::new()); // }, // _ => () From cd8499464ffa44ee614b3b0e2fef9b033edebb79 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 21:51:22 +1000 Subject: [PATCH 05/10] Setup registry --- .../core/src/ciphernode_sequencer.rs | 15 ++- .../core/src/plaintext_sequencer.rs | 12 ++- .../core/src/publickey_sequencer.rs | 2 +- packages/ciphernode/core/src/registry.rs | 95 ++++++++++++++----- 4 files changed, 94 insertions(+), 30 deletions(-) diff --git a/packages/ciphernode/core/src/ciphernode_sequencer.rs b/packages/ciphernode/core/src/ciphernode_sequencer.rs index 602a6c45..d67702d5 100644 --- a/packages/ciphernode/core/src/ciphernode_sequencer.rs +++ b/packages/ciphernode/core/src/ciphernode_sequencer.rs @@ -10,9 +10,18 @@ pub struct CiphernodeSequencer { fhe: Addr, data: Addr, bus: Addr, - ciphernode: Option>, + child: Option>, +} +impl CiphernodeSequencer { + pub fn new(fhe: Addr, data: Addr, bus: Addr) -> Self { + Self { + fhe, + bus, + data, + child: None, + } + } } - impl Actor for CiphernodeSequencer { type Context = Context; } @@ -24,7 +33,7 @@ impl Handler for CiphernodeSequencer { let fhe = self.fhe.clone(); let data = self.data.clone(); let sink = self - .ciphernode + .child .get_or_insert_with(|| Ciphernode::new(bus, fhe, data).start()); sink.do_send(msg); } diff --git a/packages/ciphernode/core/src/plaintext_sequencer.rs b/packages/ciphernode/core/src/plaintext_sequencer.rs index af95a51a..87de9c5e 100644 --- a/packages/ciphernode/core/src/plaintext_sequencer.rs +++ b/packages/ciphernode/core/src/plaintext_sequencer.rs @@ -13,7 +13,17 @@ pub struct PlaintextSequencer { nodecount: usize, child: Option>, } - +impl PlaintextSequencer { + pub fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { + Self { + fhe, + e3_id, + bus, + nodecount, + child: None, + } + } +} impl Actor for PlaintextSequencer { type Context = Context; } diff --git a/packages/ciphernode/core/src/publickey_sequencer.rs b/packages/ciphernode/core/src/publickey_sequencer.rs index 68b0b4a4..df710d37 100644 --- a/packages/ciphernode/core/src/publickey_sequencer.rs +++ b/packages/ciphernode/core/src/publickey_sequencer.rs @@ -15,7 +15,7 @@ pub struct PublicKeySequencer { } impl PublicKeySequencer { - fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { + pub fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { Self { fhe, e3_id, diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index 8886c304..ba5d14a9 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -1,36 +1,81 @@ use std::collections::HashMap; use actix::prelude::*; +use rand_chacha::ChaCha20Rng; -use crate::{CiphernodeSequencer, E3id, EnclaveEvent, Fhe, PlaintextSequencer, PublicKeySequencer}; +use crate::{ + Ciphernode, CiphernodeSequencer, Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextSequencer, + PublicKeySequencer, +}; pub struct Registry { - pub public_keys: HashMap>, - pub plaintexts: HashMap>, - pub ciphernodes: HashMap>, - pub fhes: HashMap> + bus: Addr, + ciphernodes: HashMap>, + data: Addr, + fhes: HashMap>, + nodecount: usize, + plaintexts: HashMap>, + public_keys: HashMap>, + rng: ChaCha20Rng, } impl Actor for Registry { type Context = Context; } -// -// impl Handler for Registry { -// type Result = (); -// fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { -// let e3_id = E3id::from(msg.clone()); -// match msg { -// EnclaveEvent::CommitteeRequested { data, .. } => { -// -// let moduli = &vec![0x3FFFFFFF000001]; -// let degree = 2048; -// let plaintext_modulus = 1032193; -// -// -// self.fhes.entry(e3_id).or_insert_with(|| Fhe::( -// self.public_keys.entry(e3_id).or_insert_with(|| PublicKeySequencer::new()); -// }, -// _ => () -// } -// } -// } + +impl Handler for Registry { + type Result = (); + fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + let e3_id = E3id::from(msg.clone()); + let mut fhes = self.fhes.clone(); + let bus = self.bus.clone(); + let mut public_keys = self.public_keys.clone(); + let mut plaintexts = self.plaintexts.clone(); + let mut ciphernodes = self.ciphernodes.clone(); + let data = self.data.clone(); + + // Idempotently create references + // TODO: this adds coupling here would be nice to be more abstract + match msg.clone() { + EnclaveEvent::CommitteeRequested { .. } => { + let moduli = &vec![0x3FFFFFFF000001]; + let degree = 2048; + let plaintext_modulus = 1032193; + + let fhe = fhes.entry(e3_id.clone()).or_insert_with(|| { + Fhe::from_raw_params(moduli, degree, plaintext_modulus, self.rng.clone()) + .unwrap() + .start() + }); + + public_keys.entry(e3_id.clone()).or_insert_with(|| { + PublicKeySequencer::new(fhe.clone(), e3_id.clone(), bus.clone(), self.nodecount) + .start() + }); + + ciphernodes.entry(e3_id.clone()).or_insert_with(|| { + CiphernodeSequencer::new(fhe.clone(), data.clone(), bus.clone()).start() + }); + } + EnclaveEvent::CiphertextOutputPublished { .. } => { + let fhe = fhes.get(&e3_id).unwrap(); + plaintexts.entry(e3_id.clone()).or_insert_with(|| { + PlaintextSequencer::new(fhe.clone(), e3_id.clone(), bus, self.nodecount).start() + }); + } + _ => (), + }; + + if let Some(act) = public_keys.get(&e3_id) { + act.do_send(msg.clone()); + } + + if let Some(act) = plaintexts.get(&e3_id) { + act.do_send(msg.clone()); + } + + if let Some(act) = ciphernodes.get(&e3_id) { + act.do_send(msg.clone()); + } + } +} From bb519c3e403762cad27ee6bdc05a2b4c88545e6c Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 21:57:23 +1000 Subject: [PATCH 06/10] Update registry --- packages/ciphernode/core/src/registry.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index ba5d14a9..fc83c937 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -4,7 +4,7 @@ use actix::prelude::*; use rand_chacha::ChaCha20Rng; use crate::{ - Ciphernode, CiphernodeSequencer, Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextSequencer, + CiphernodeSequencer, Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextSequencer, PublicKeySequencer, }; @@ -25,7 +25,7 @@ impl Actor for Registry { impl Handler for Registry { type Result = (); - fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result { let e3_id = E3id::from(msg.clone()); let mut fhes = self.fhes.clone(); let bus = self.bus.clone(); @@ -66,6 +66,7 @@ impl Handler for Registry { _ => (), }; + // Can I iterate over each of these? if let Some(act) = public_keys.get(&e3_id) { act.do_send(msg.clone()); } @@ -76,6 +77,6 @@ impl Handler for Registry { if let Some(act) = ciphernodes.get(&e3_id) { act.do_send(msg.clone()); - } + } } } From c716d3fc359ab99b4c281289e234253d0a61f9b1 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 22:03:26 +1000 Subject: [PATCH 07/10] Modularize --- packages/ciphernode/core/src/registry.rs | 100 ++++++++++++++--------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index fc83c937..eb00017d 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -1,12 +1,10 @@ -use std::collections::HashMap; - -use actix::prelude::*; -use rand_chacha::ChaCha20Rng; - use crate::{ CiphernodeSequencer, Data, E3id, EnclaveEvent, EventBus, Fhe, PlaintextSequencer, PublicKeySequencer, }; +use actix::prelude::*; +use rand_chacha::ChaCha20Rng; +use std::collections::HashMap; pub struct Registry { bus: Addr, @@ -25,57 +23,81 @@ impl Actor for Registry { impl Handler for Registry { type Result = (); + fn handle(&mut self, msg: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result { let e3_id = E3id::from(msg.clone()); - let mut fhes = self.fhes.clone(); - let bus = self.bus.clone(); - let mut public_keys = self.public_keys.clone(); - let mut plaintexts = self.plaintexts.clone(); - let mut ciphernodes = self.ciphernodes.clone(); - let data = self.data.clone(); - // Idempotently create references - // TODO: this adds coupling here would be nice to be more abstract match msg.clone() { EnclaveEvent::CommitteeRequested { .. } => { + self.create_committee_actors(&e3_id); + } + EnclaveEvent::CiphertextOutputPublished { .. } => { + self.create_plaintext_actor(&e3_id); + } + _ => (), + }; + + self.forward_message(&e3_id, msg); + } +} + +impl Registry { + fn create_committee_actors(&mut self, e3_id: &E3id) { + let fhe = self.create_or_get_fhe(e3_id); + self.create_or_get_public_key_sequencer(e3_id, fhe.clone()); + self.create_or_get_ciphernode_sequencer(e3_id, fhe); + } + + fn create_or_get_fhe(&mut self, e3_id: &E3id) -> Addr { + self.fhes + .entry(e3_id.clone()) + .or_insert_with(|| { let moduli = &vec![0x3FFFFFFF000001]; let degree = 2048; let plaintext_modulus = 1032193; + Fhe::from_raw_params(moduli, degree, plaintext_modulus, self.rng.clone()) + .unwrap() + .start() + }) + .clone() + } - let fhe = fhes.entry(e3_id.clone()).or_insert_with(|| { - Fhe::from_raw_params(moduli, degree, plaintext_modulus, self.rng.clone()) - .unwrap() - .start() - }); + fn create_or_get_public_key_sequencer(&mut self, e3_id: &E3id, fhe: Addr) { + self.public_keys.entry(e3_id.clone()).or_insert_with(|| { + PublicKeySequencer::new(fhe, e3_id.clone(), self.bus.clone(), self.nodecount).start() + }); + } - public_keys.entry(e3_id.clone()).or_insert_with(|| { - PublicKeySequencer::new(fhe.clone(), e3_id.clone(), bus.clone(), self.nodecount) - .start() - }); + fn create_or_get_ciphernode_sequencer(&mut self, e3_id: &E3id, fhe: Addr) { + self.ciphernodes.entry(e3_id.clone()).or_insert_with(|| { + CiphernodeSequencer::new(fhe, self.data.clone(), self.bus.clone()).start() + }); + } - ciphernodes.entry(e3_id.clone()).or_insert_with(|| { - CiphernodeSequencer::new(fhe.clone(), data.clone(), bus.clone()).start() - }); - } - EnclaveEvent::CiphertextOutputPublished { .. } => { - let fhe = fhes.get(&e3_id).unwrap(); - plaintexts.entry(e3_id.clone()).or_insert_with(|| { - PlaintextSequencer::new(fhe.clone(), e3_id.clone(), bus, self.nodecount).start() - }); - } - _ => (), - }; + fn create_plaintext_actor(&mut self, e3_id: &E3id) { + if let Some(fhe) = self.fhes.get(e3_id) { + self.plaintexts.entry(e3_id.clone()).or_insert_with(|| { + PlaintextSequencer::new( + fhe.clone(), + e3_id.clone(), + self.bus.clone(), + self.nodecount, + ) + .start() + }); + } + } - // Can I iterate over each of these? - if let Some(act) = public_keys.get(&e3_id) { - act.do_send(msg.clone()); + fn forward_message(&self, e3_id: &E3id, msg: EnclaveEvent) { + if let Some(act) = self.public_keys.get(&e3_id) { + act.clone().recipient().do_send(msg.clone()); } - if let Some(act) = plaintexts.get(&e3_id) { + if let Some(act) = self.plaintexts.get(&e3_id) { act.do_send(msg.clone()); } - if let Some(act) = ciphernodes.get(&e3_id) { + if let Some(act) = self.ciphernodes.get(&e3_id) { act.do_send(msg.clone()); } } From 18d83cbbeaba8b49c12a84056693097d9a8b4550 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 22:42:19 +1000 Subject: [PATCH 08/10] Update registry to be more modular --- packages/ciphernode/core/src/registry.rs | 91 +++++++++++++----------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index eb00017d..940875cb 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -29,10 +29,21 @@ impl Handler for Registry { match msg.clone() { EnclaveEvent::CommitteeRequested { .. } => { - self.create_committee_actors(&e3_id); + let fhe_creator = self.fhe_creator(); + let fhe = store(&e3_id, &mut self.fhes, fhe_creator); + + let public_key_creator = self.public_key_creator(e3_id.clone(), fhe.clone()); + store(&e3_id, &mut self.public_keys, public_key_creator); + + let ciphernode_creator = self.ciphernode_creator(fhe.clone()); + store(&e3_id, &mut self.ciphernodes, ciphernode_creator); } EnclaveEvent::CiphertextOutputPublished { .. } => { - self.create_plaintext_actor(&e3_id); + let Some(fhe) = self.fhes.get(&e3_id) else { + return; + }; + let plaintext_creator = self.plaintext_creator(e3_id.clone(), fhe.clone()); + store(&e3_id, &mut self.plaintexts, plaintext_creator); } _ => (), }; @@ -42,50 +53,42 @@ impl Handler for Registry { } impl Registry { - fn create_committee_actors(&mut self, e3_id: &E3id) { - let fhe = self.create_or_get_fhe(e3_id); - self.create_or_get_public_key_sequencer(e3_id, fhe.clone()); - self.create_or_get_ciphernode_sequencer(e3_id, fhe); - } - - fn create_or_get_fhe(&mut self, e3_id: &E3id) -> Addr { - self.fhes - .entry(e3_id.clone()) - .or_insert_with(|| { - let moduli = &vec![0x3FFFFFFF000001]; - let degree = 2048; - let plaintext_modulus = 1032193; - Fhe::from_raw_params(moduli, degree, plaintext_modulus, self.rng.clone()) - .unwrap() - .start() - }) - .clone() + fn fhe_creator(&self) -> impl FnOnce() -> Addr { + let rng = self.rng.clone(); + move || { + let moduli = &vec![0x3FFFFFFF000001]; + let degree = 2048; + let plaintext_modulus = 1032193; + Fhe::from_raw_params(moduli, degree, plaintext_modulus, rng) + .unwrap() + .start() + } } - fn create_or_get_public_key_sequencer(&mut self, e3_id: &E3id, fhe: Addr) { - self.public_keys.entry(e3_id.clone()).or_insert_with(|| { - PublicKeySequencer::new(fhe, e3_id.clone(), self.bus.clone(), self.nodecount).start() - }); + fn public_key_creator( + &self, + e3_id: E3id, + fhe: Addr, + ) -> impl FnOnce() -> Addr { + let bus = self.bus.clone(); + let nodecount = self.nodecount; + move || PublicKeySequencer::new(fhe, e3_id, bus, nodecount).start() } - fn create_or_get_ciphernode_sequencer(&mut self, e3_id: &E3id, fhe: Addr) { - self.ciphernodes.entry(e3_id.clone()).or_insert_with(|| { - CiphernodeSequencer::new(fhe, self.data.clone(), self.bus.clone()).start() - }); + fn ciphernode_creator(&self, fhe: Addr) -> impl FnOnce() -> Addr { + let data = self.data.clone(); + let bus = self.bus.clone(); + move || CiphernodeSequencer::new(fhe, data, bus).start() } - fn create_plaintext_actor(&mut self, e3_id: &E3id) { - if let Some(fhe) = self.fhes.get(e3_id) { - self.plaintexts.entry(e3_id.clone()).or_insert_with(|| { - PlaintextSequencer::new( - fhe.clone(), - e3_id.clone(), - self.bus.clone(), - self.nodecount, - ) - .start() - }); - } + fn plaintext_creator( + &self, + e3_id: E3id, + fhe: Addr, + ) -> impl FnOnce() -> Addr { + let bus = self.bus.clone(); + let nodecount = self.nodecount; + move || PlaintextSequencer::new(fhe, e3_id, bus, nodecount).start() } fn forward_message(&self, e3_id: &E3id, msg: EnclaveEvent) { @@ -102,3 +105,11 @@ impl Registry { } } } + +fn store(e3_id: &E3id, map: &mut HashMap>, creator: F) -> Addr +where + T: Actor>, + F: FnOnce() -> Addr, +{ + map.entry(e3_id.clone()).or_insert_with(creator).clone() +} From c416dded1a3698810941d92e3fe82bcb49401ccd Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 22:47:13 +1000 Subject: [PATCH 09/10] Rename --- packages/ciphernode/core/src/registry.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index 940875cb..ce9665fa 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -32,18 +32,18 @@ impl Handler for Registry { let fhe_creator = self.fhe_creator(); let fhe = store(&e3_id, &mut self.fhes, fhe_creator); - let public_key_creator = self.public_key_creator(e3_id.clone(), fhe.clone()); - store(&e3_id, &mut self.public_keys, public_key_creator); + let public_key_sequencer_factory = self.public_key_sequencer_factory(e3_id.clone(), fhe.clone()); + store(&e3_id, &mut self.public_keys, public_key_sequencer_factory); - let ciphernode_creator = self.ciphernode_creator(fhe.clone()); - store(&e3_id, &mut self.ciphernodes, ciphernode_creator); + let ciphernode_sequencer_factory = self.ciphernode_sequencer_factory(fhe.clone()); + store(&e3_id, &mut self.ciphernodes, ciphernode_sequencer_factory); } EnclaveEvent::CiphertextOutputPublished { .. } => { let Some(fhe) = self.fhes.get(&e3_id) else { return; }; - let plaintext_creator = self.plaintext_creator(e3_id.clone(), fhe.clone()); - store(&e3_id, &mut self.plaintexts, plaintext_creator); + let plaintext_sequencer_factory = self.plaintext_sequencer_factory(e3_id.clone(), fhe.clone()); + store(&e3_id, &mut self.plaintexts, plaintext_sequencer_factory); } _ => (), }; @@ -65,7 +65,7 @@ impl Registry { } } - fn public_key_creator( + fn public_key_sequencer_factory( &self, e3_id: E3id, fhe: Addr, @@ -75,13 +75,13 @@ impl Registry { move || PublicKeySequencer::new(fhe, e3_id, bus, nodecount).start() } - fn ciphernode_creator(&self, fhe: Addr) -> impl FnOnce() -> Addr { + fn ciphernode_sequencer_factory(&self, fhe: Addr) -> impl FnOnce() -> Addr { let data = self.data.clone(); let bus = self.bus.clone(); move || CiphernodeSequencer::new(fhe, data, bus).start() } - fn plaintext_creator( + fn plaintext_sequencer_factory( &self, e3_id: E3id, fhe: Addr, From 92cacaf71f95c561d187fb9afd73230e999532d6 Mon Sep 17 00:00:00 2001 From: ryardley Date: Wed, 11 Sep 2024 22:56:17 +1000 Subject: [PATCH 10/10] Rename --- packages/ciphernode/core/src/publickey_sequencer.rs | 4 ++-- packages/ciphernode/core/src/registry.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/ciphernode/core/src/publickey_sequencer.rs b/packages/ciphernode/core/src/publickey_sequencer.rs index df710d37..8a2593c4 100644 --- a/packages/ciphernode/core/src/publickey_sequencer.rs +++ b/packages/ciphernode/core/src/publickey_sequencer.rs @@ -4,7 +4,7 @@ use actix::prelude::*; -use crate::{Data, E3id, EnclaveEvent, EventBus, Fhe, PublicKeyAggregator}; +use crate::{E3id, EnclaveEvent, EventBus, Fhe, PublicKeyAggregator}; pub struct PublicKeySequencer { fhe: Addr, @@ -15,7 +15,7 @@ pub struct PublicKeySequencer { } impl PublicKeySequencer { - pub fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { + pub fn new(fhe: Addr, e3_id: E3id, bus: Addr, nodecount: usize) -> Self { Self { fhe, e3_id, diff --git a/packages/ciphernode/core/src/registry.rs b/packages/ciphernode/core/src/registry.rs index ce9665fa..6a13120b 100644 --- a/packages/ciphernode/core/src/registry.rs +++ b/packages/ciphernode/core/src/registry.rs @@ -29,8 +29,8 @@ impl Handler for Registry { match msg.clone() { EnclaveEvent::CommitteeRequested { .. } => { - let fhe_creator = self.fhe_creator(); - let fhe = store(&e3_id, &mut self.fhes, fhe_creator); + let fhe_factory = self.fhe_factory(); + let fhe = store(&e3_id, &mut self.fhes, fhe_factory); let public_key_sequencer_factory = self.public_key_sequencer_factory(e3_id.clone(), fhe.clone()); store(&e3_id, &mut self.public_keys, public_key_sequencer_factory); @@ -53,7 +53,7 @@ impl Handler for Registry { } impl Registry { - fn fhe_creator(&self) -> impl FnOnce() -> Addr { + fn fhe_factory(&self) -> impl FnOnce() -> Addr { let rng = self.rng.clone(); move || { let moduli = &vec![0x3FFFFFFF000001];