Skip to content
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
52 changes: 26 additions & 26 deletions willow/src/shell/ahe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,24 @@ pub struct PartialDecryptionMetadata {

macro_rules! impl_proto_traits_single_poly {
($type:ty, $proto:ty) => {
impl ToProto for $type {
impl<Context: AsRef<ShellAhe>> ToProto<Context> for $type {
type Proto = $proto;
type Context = ShellAhe;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.public_ahe_parameters);
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.as_ref().public_ahe_parameters);
let poly_proto = rns_polynomial_to_proto(&self.0, &moduli)?;
Ok(proto!($proto { poly: poly_proto }))
}
}

impl FromProto for $type {
impl<Context: AsRef<ShellAhe>> FromProto<Context> for $type {
type Proto = $proto;
type Context = ShellAhe;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.public_ahe_parameters);
let moduli = ahe::get_moduli(&ctx.as_ref().public_ahe_parameters);
let poly = rns_polynomial_from_proto(proto.as_view().poly(), &moduli)?;
Ok(Self(poly))
}
Expand All @@ -424,12 +422,11 @@ impl_proto_traits_single_poly!(PublicKey, ShellAhePublicKey);

macro_rules! impl_proto_traits_vec_poly {
($type:ty, $proto:ty) => {
impl ToProto for $type {
impl<Context: AsRef<ShellAhe>> ToProto<Context> for $type {
type Proto = $proto;
type Context = ShellAhe;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.public_ahe_parameters);
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.as_ref().public_ahe_parameters);
let mut result = proto!($proto {});
for poly in &self.0 {
result.poly_mut().push(rns_polynomial_to_proto(&poly, &moduli)?);
Expand All @@ -438,15 +435,14 @@ macro_rules! impl_proto_traits_vec_poly {
}
}

impl FromProto for $type {
impl<Context: AsRef<ShellAhe>> FromProto<Context> for $type {
type Proto = $proto;
type Context = ShellAhe;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, status::StatusError> {
let moduli = ahe::get_moduli(&ctx.public_ahe_parameters);
let moduli = ahe::get_moduli(&ctx.as_ref().public_ahe_parameters);
let polys: Result<Vec<_>, _> = proto
.as_view()
.poly()
Expand All @@ -463,34 +459,38 @@ impl_proto_traits_vec_poly!(PartialDecryption, ShellAhePartialDecryption);
impl_proto_traits_vec_poly!(PartialDecCiphertext, ShellAhePartialDecCiphertext);
impl_proto_traits_vec_poly!(RecoverCiphertext, ShellAheRecoverCiphertext);

impl ToProto for Ciphertext {
impl<Context: AsRef<ShellAhe>> ToProto<Context> for Ciphertext {
type Proto = ShellAheCiphertext;
type Context = ShellAhe;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, status::StatusError> {
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, status::StatusError> {
Ok(proto!(ShellAheCiphertext {
component_b: self.component_b.to_proto(ctx)?,
component_a: self.component_a.to_proto(ctx)?,
component_b: self.component_b.to_proto(&ctx)?,
component_a: self.component_a.to_proto(&ctx)?,
}))
}
}

impl FromProto for Ciphertext {
impl<Context: AsRef<ShellAhe>> FromProto<Context> for Ciphertext {
type Proto = ShellAheCiphertext;
type Context = ShellAhe;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, status::StatusError> {
let proto_view = proto.as_view();
Ok(Ciphertext {
component_b: RecoverCiphertext::from_proto(proto_view.component_b(), ctx)?,
component_a: PartialDecCiphertext::from_proto(proto_view.component_a(), ctx)?,
component_b: RecoverCiphertext::from_proto(proto_view.component_b(), &ctx)?,
component_a: PartialDecCiphertext::from_proto(proto_view.component_a(), &ctx)?,
})
}
}

impl AsRef<ShellAhe> for ShellAhe {
fn as_ref(&self) -> &ShellAhe {
self
}
}

impl AheBase for ShellAhe {
type SecretKeyShare = SecretKeyShare;
type PublicKeyShare = PublicKeyShare;
Expand Down
34 changes: 18 additions & 16 deletions willow/src/shell/kahe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,34 @@ pub struct SecretKey(pub RnsPolynomial);
#[derive(Clone)]
pub struct Ciphertext(pub RnsPolynomialVec);

impl ToProto for SecretKey {
impl<Context: AsRef<ShellKahe>> ToProto<Context> for SecretKey {
type Proto = ShellKaheSecretKey;
type Context = ShellKahe;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.public_kahe_parameters);
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.as_ref().public_kahe_parameters);
let poly_proto = rns_polynomial_to_proto(&self.0, &moduli)?;
Ok(proto!(ShellKaheSecretKey { poly: poly_proto }))
}
}

impl FromProto for SecretKey {
impl<Context: AsRef<ShellKahe>> FromProto<Context> for SecretKey {
type Proto = ShellKaheSecretKey;
type Context = ShellKahe;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.public_kahe_parameters);
let moduli = kahe::get_moduli(&ctx.as_ref().public_kahe_parameters);
let poly = rns_polynomial_from_proto(proto.as_view().poly(), &moduli)?;
Ok(Self(poly))
}
}

impl ToProto for Ciphertext {
impl<Context: AsRef<ShellKahe>> ToProto<Context> for Ciphertext {
type Proto = ShellKaheCiphertext;
type Context = ShellKahe;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.public_kahe_parameters);
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.as_ref().public_kahe_parameters);
let mut result = proto!(ShellKaheCiphertext {});
for poly in self.0.iter() {
result.poly_mut().push(rns_polynomial_to_proto(&poly, &moduli)?);
Expand All @@ -149,21 +146,26 @@ impl ToProto for Ciphertext {
}
}

impl FromProto for Ciphertext {
impl<Context: AsRef<ShellKahe>> FromProto<Context> for Ciphertext {
type Proto = ShellKaheCiphertext;
type Context = ShellKahe;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, status::StatusError> {
let moduli = kahe::get_moduli(&ctx.public_kahe_parameters);
let moduli = kahe::get_moduli(&ctx.as_ref().public_kahe_parameters);
let polys: Result<Vec<_>, _> =
proto.as_view().poly().iter().map(|p| rns_polynomial_from_proto(p, &moduli)).collect();
Ok(Ciphertext(rust_vec_to_rns_polynomial_vec(polys?)))
}
}

impl AsRef<ShellKahe> for ShellKahe {
fn as_ref(&self) -> &ShellKahe {
self
}
}

impl KaheBase for ShellKahe {
type SecretKey = SecretKey;

Expand Down
12 changes: 5 additions & 7 deletions willow/src/traits/proto_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
use status::StatusError;

/// Trait for converting a struct to a Protobuf message.
pub trait ToProto {
pub trait ToProto<Context = ()> {
type Proto;
type Context;

fn to_proto(&self, ctx: &Self::Context) -> Result<Self::Proto, StatusError>;
fn to_proto(&self, ctx: Context) -> Result<Self::Proto, StatusError>;
}

/// Trait for converting a Protobuf message view to a struct.
pub trait FromProto: Sized {
type Proto: protobuf::AsView;
type Context;
pub trait FromProto<Context = ()>: Sized {
type Proto;

fn from_proto(
proto: impl protobuf::AsView<Proxied = Self::Proto>,
ctx: &Self::Context,
ctx: Context,
) -> Result<Self, StatusError>;
}