Skip to content

Commit

Permalink
wip: prepare for private area intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Jun 27, 2024
1 parent 525a900 commit 4ab46d6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
3 changes: 3 additions & 0 deletions iroh-willow/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ async fn open_logical_channels(
.ok_or(MissingChannel(channel))
};

let pai = take_and_spawn_channel(LogicalChannel::Intersection)?;
let rec = take_and_spawn_channel(LogicalChannel::Reconciliation)?;
let stt = take_and_spawn_channel(LogicalChannel::StaticToken)?;
let aoi = take_and_spawn_channel(LogicalChannel::AreaOfInterest)?;
Expand All @@ -182,13 +183,15 @@ async fn open_logical_channels(

Ok((
LogicalChannelSenders {
intersection: pai.0,
reconciliation: rec.0,
static_tokens: stt.0,
aoi: aoi.0,
capability: cap.0,
data: dat.0,
},
LogicalChannelReceivers {
intersection_recv: pai.1.into(),
reconciliation_recv: rec.1.into(),
static_tokens_recv: stt.1.into(),
aoi_recv: aoi.1.into(),
Expand Down
114 changes: 98 additions & 16 deletions iroh-willow/src/proto/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl Channel {
strum::EnumCount,
)]
pub enum LogicalChannel {
/// Logical channel for controlling the binding of new IntersectionHandles.
Intersection,
/// Logical channel for performing 3d range-based set reconciliation.
Reconciliation,
// TODO: use all the channels
Expand Down Expand Up @@ -183,6 +185,7 @@ impl LogicalChannel {
}
pub fn fmt_short(&self) -> &'static str {
match self {
LogicalChannel::Intersection => "Pai",
LogicalChannel::Reconciliation => "Rec",
LogicalChannel::StaticToken => "StT",
LogicalChannel::Capability => "Cap",
Expand All @@ -193,22 +196,24 @@ impl LogicalChannel {

pub fn from_id(id: u8) -> Result<Self, InvalidChannelId> {
match id {
2 => Ok(Self::AreaOfInterest),
3 => Ok(Self::Capability),
4 => Ok(Self::StaticToken),
5 => Ok(Self::Reconciliation),
6 => Ok(Self::Data),
2 => Ok(Self::Intersection),
3 => Ok(Self::AreaOfInterest),
4 => Ok(Self::Capability),
5 => Ok(Self::StaticToken),
6 => Ok(Self::Reconciliation),
7 => Ok(Self::Data),
_ => Err(InvalidChannelId),
}
}

pub fn id(&self) -> u8 {
match self {
LogicalChannel::AreaOfInterest => 2,
LogicalChannel::Capability => 3,
LogicalChannel::StaticToken => 4,
LogicalChannel::Reconciliation => 5,
LogicalChannel::Data => 6,
LogicalChannel::Intersection => 2,
LogicalChannel::AreaOfInterest => 3,
LogicalChannel::Capability => 4,
LogicalChannel::StaticToken => 5,
LogicalChannel::Reconciliation => 6,
LogicalChannel::Data => 7,
}
}
}
Expand Down Expand Up @@ -292,10 +297,14 @@ pub struct CommitmentReveal {
pub enum Message {
#[debug("{:?}", _0)]
CommitmentReveal(CommitmentReveal),
// PaiReplyFragment
// PaiBindFragment
// PaiRequestSubspaceCapability
// PaiReplySubspaceCapability
#[debug("{:?}", _0)]
PaiReplyFragment(PaiReplyFragment),
#[debug("{:?}", _0)]
PaiBindFragment(PaiBindFragment),
#[debug("{:?}", _0)]
PaiRequestSubspaceCapability(PaiRequestSubspaceCapability),
#[debug("{:?}", _0)]
PaiReplySubspaceCapability(PaiReplySubspaceCapability),
#[debug("{:?}", _0)]
SetupBindStaticToken(SetupBindStaticToken),
#[debug("{:?}", _0)]
Expand Down Expand Up @@ -395,6 +404,10 @@ impl Decoder for Message {
impl Message {
pub fn channel(&self) -> Channel {
match self {
Message::PaiBindFragment(_) | Message::PaiReplyFragment(_) => {
Channel::Logical(LogicalChannel::Intersection)
}

Message::SetupBindReadCapability(_) => Channel::Logical(LogicalChannel::Capability),
Message::SetupBindAreaOfInterest(_) => Channel::Logical(LogicalChannel::AreaOfInterest),
Message::SetupBindStaticToken(_) => Channel::Logical(LogicalChannel::StaticToken),
Expand All @@ -412,6 +425,8 @@ impl Message {
| Message::DataSetMetadata(_) => Channel::Logical(LogicalChannel::Data),

Message::CommitmentReveal(_)
| Message::PaiRequestSubspaceCapability(_)
| Message::PaiReplySubspaceCapability(_)
| Message::ControlIssueGuarantee(_)
| Message::ControlAbsolve(_)
| Message::ControlPlead(_)
Expand Down Expand Up @@ -456,7 +471,7 @@ impl Message {
// }
// }
// }
//

#[derive(Debug, derive_more::From, strum::Display)]
pub enum ReconciliationMessage {
SendFingerprint(ReconciliationSendFingerprint),
Expand All @@ -465,6 +480,7 @@ pub enum ReconciliationMessage {
SendPayload(ReconciliationSendPayload),
TerminatePayload(ReconciliationTerminatePayload),
}

impl TryFrom<Message> for ReconciliationMessage {
type Error = ();
fn try_from(message: Message) -> Result<Self, Self::Error> {
Expand All @@ -478,6 +494,7 @@ impl TryFrom<Message> for ReconciliationMessage {
}
}
}

impl From<ReconciliationMessage> for Message {
fn from(message: ReconciliationMessage) -> Self {
match message {
Expand All @@ -496,6 +513,7 @@ pub enum DataMessage {
SendPayload(DataSendPayload),
SetMetadata(DataSetMetadata),
}

impl TryFrom<Message> for DataMessage {
type Error = ();
fn try_from(message: Message) -> Result<Self, Self::Error> {
Expand All @@ -507,6 +525,7 @@ impl TryFrom<Message> for DataMessage {
}
}
}

impl From<DataMessage> for Message {
fn from(message: DataMessage) -> Self {
match message {
Expand All @@ -516,7 +535,33 @@ impl From<DataMessage> for Message {
}
}
}
//

#[derive(Debug, derive_more::From, strum::Display)]
pub enum IntersectionMessage {
BindFragment(PaiBindFragment),
ReplyFragment(PaiReplyFragment),
}

impl TryFrom<Message> for IntersectionMessage {
type Error = ();
fn try_from(message: Message) -> Result<Self, Self::Error> {
match message {
Message::PaiBindFragment(msg) => Ok(msg.into()),
Message::PaiReplyFragment(msg) => Ok(msg.into()),
_ => Err(()),
}
}
}

impl From<IntersectionMessage> for Message {
fn from(message: IntersectionMessage) -> Self {
match message {
IntersectionMessage::BindFragment(msg) => msg.into(),
IntersectionMessage::ReplyFragment(msg) => msg.into(),
}
}
}

// impl Encoder for ReconciliationMessage {
// fn encoded_len(&self) -> usize {
// Message::from(se)
Expand Down Expand Up @@ -827,3 +872,40 @@ pub struct ControlFreeHandle {
mine: bool,
handle_type: HandleType,
}

type PsiGroup = ();
/// Bind data to an IntersectionHandle for performing private area intersection.
#[derive(Debug, Serialize, Deserialize)]
pub struct PaiBindFragment {
/// The result of first applying hash_into_group to some fragment for private area intersection and then performing scalar multiplication with scalar.
group_member: PsiGroup,
/// Set to true if the private set intersection item is a secondary fragment.
is_secondary: bool,
}

/// Finalise private set intersection for a single item.
#[derive(Debug, Serialize, Deserialize)]
pub struct PaiReplyFragment {
/// The IntersectionHandle of the PaiBindFragment message which this finalises.
handle: IntersectionHandle,
/// The result of performing scalar multiplication between the group_member of the message that this is replying to and scalar.
group_member: PsiGroup,
}

/// Ask the receiver to send a SubspaceCapability.
#[derive(Debug, Serialize, Deserialize)]
pub struct PaiRequestSubspaceCapability {
/// The IntersectionHandle bound by the sender for the least-specific secondary fragment for whose NamespaceId to request the SubspaceCapability.
handle: IntersectionHandle,
}

/// Send a previously requested SubspaceCapability.
#[derive(Debug, Serialize, Deserialize)]
pub struct PaiReplySubspaceCapability {
/// The handle of the PaiRequestSubspaceCapability message that this answers (hence, an IntersectionHandle bound by the receiver of this message).
handle: IntersectionHandle,
/// A SubspaceCapability whose granted namespace corresponds to the request this answers.
capability: SubspaceCapability,
/// The SyncSubspaceSignature issued by the receiver of the capability over the sender’s challenge.
signature: SyncSignature,
}
7 changes: 6 additions & 1 deletion iroh-willow/src/session/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::trace;

use crate::{
proto::sync::{
Channel, DataMessage, LogicalChannel, Message, ReconciliationMessage,
Channel, DataMessage, IntersectionMessage, LogicalChannel, Message, ReconciliationMessage,
SetupBindAreaOfInterest, SetupBindReadCapability, SetupBindStaticToken,
},
util::channel::{Receiver, Sender, WriteError},
Expand Down Expand Up @@ -66,6 +66,7 @@ impl<T: TryFrom<Message>> From<Receiver<Message>> for MessageReceiver<T> {

#[derive(Debug)]
pub struct LogicalChannelReceivers {
pub intersection_recv: MessageReceiver<IntersectionMessage>,
pub reconciliation_recv: MessageReceiver<ReconciliationMessage>,
pub static_tokens_recv: MessageReceiver<SetupBindStaticToken>,
pub capability_recv: MessageReceiver<SetupBindReadCapability>,
Expand All @@ -75,6 +76,7 @@ pub struct LogicalChannelReceivers {

impl LogicalChannelReceivers {
pub fn close(&self) {
self.intersection_recv.close();
self.reconciliation_recv.close();
self.static_tokens_recv.close();
self.capability_recv.close();
Expand All @@ -85,6 +87,7 @@ impl LogicalChannelReceivers {

#[derive(Debug, Clone)]
pub struct LogicalChannelSenders {
pub intersection: Sender<Message>,
pub reconciliation: Sender<Message>,
pub static_tokens: Sender<Message>,
pub aoi: Sender<Message>,
Expand All @@ -93,6 +96,7 @@ pub struct LogicalChannelSenders {
}
impl LogicalChannelSenders {
pub fn close(&self) {
self.intersection.close();
self.reconciliation.close();
self.static_tokens.close();
self.aoi.close();
Expand All @@ -102,6 +106,7 @@ impl LogicalChannelSenders {

pub fn get(&self, channel: LogicalChannel) -> &Sender<Message> {
match channel {
LogicalChannel::Intersection => &self.intersection,
LogicalChannel::Reconciliation => &self.reconciliation,
LogicalChannel::StaticToken => &self.static_tokens,
LogicalChannel::Capability => &self.capability,
Expand Down
1 change: 1 addition & 0 deletions iroh-willow/src/session/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Session {
capability_recv,
aoi_recv,
data_recv,
intersection_recv,
},
} = recv;

Expand Down

0 comments on commit 4ab46d6

Please sign in to comment.