macro_rules! impl_delegated {
($function_name:ident, self, $return_type:ty) => {
pub fn $function_name(&self) -> $return_type {
self.value.$function_name()
}
};
($function_name:ident, mut, self, $return_type:ty) => {
pub fn $function_name(&mut self) -> $return_type {
self.value.$function_name()
}
};
($function_name:ident, $base_type:ty, $return_type:ty) => {
pub fn $function_name($($i:$j)*) -> $return_type {
$base_type::$function_name($($i)*)
}
};
}
macro_rules! impl_delegated_wrapper {
($base_type:ty, $function_name:ident) => {
pub fn $function_name(network: SharedNetwork<N>, beaver_source: BeaverSource<S>) -> Self {
Self {
value: <$base_type>::$function_name(),
visibility: Visibility::Public,
network,
beaver_source,
}
}
};
($base_type:ty, $function_name:ident, $private_fn_name:ident, $with_visibility_function:ident) => {
pub fn $function_name(network: SharedNetwork<N>, beaver_source: BeaverSource<S>) -> Self {
Self::$with_visibility_function(Visibility::Public, network, beaver_source)
}
pub fn $private_fn_name(network: SharedNetwork<N>, beaver_source: BeaverSource<S>) -> Self {
Self::$with_visibility_function(Visibility::Private, network, beaver_source)
}
fn $with_visibility_function(
visibility: Visibility,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self {
network,
visibility,
beaver_source,
value: <$base_type>::$function_name(),
}
}
};
($base_type:ty, $function_name:ident, $with_visibility_function:ident, $param_name:ident, $param_type:ty) => {
pub fn $function_name(
$param_name: $param_type,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self::$with_visibility_function($param_name, Visibility::Public, network, beaver_source)
}
pub fn $with_visibility_function(
$param_name: $param_type,
visibility: Visibility,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self {
visibility,
network,
beaver_source,
value: <$base_type>::$function_name($param_name),
}
}
};
}
macro_rules! impl_authenticated {
($base_type:ty, $function_name:ident) => {
pub fn $function_name(
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self {
value: <$base_type>::$function_name(network, beaver_source),
visibility: Visibility::Public,
mac_share: None,
key_share,
}
}
};
($base_type:ty, $public_fn:ident, $private_fn:ident, $with_visibility_function:ident) => {
pub fn $public_fn(
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self::$with_visibility_function(Visibility::Public, key_share, network, beaver_source)
}
pub fn $private_fn(
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self::$with_visibility_function(Visibility::Private, key_share, network, beaver_source)
}
fn $with_visibility_function(
visibility: Visibility,
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
let value =
<$base_type>::$with_visibility_function(Visibility::Public, network, beaver_source);
Self {
value,
key_share,
mac_share: None,
visibility: Visibility::Public,
}
}
};
($base_type:ty, $public_fn:ident, $private_fn:ident, $with_visibility_function:ident, $param_type:ty) => {
pub fn $public_fn(
x: $param_type,
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self::$with_visibility_function(
x,
Visibility::Public,
key_share,
network,
beaver_source,
)
}
pub fn $private_fn(
x: $param_type,
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self::$with_visibility_function(
x,
Visibility::Private,
key_share,
network,
beaver_source,
)
}
pub fn $with_visibility_function(
x: $param_type,
visibility: Visibility,
key_share: MpcScalar<N, S>,
network: SharedNetwork<N>,
beaver_source: BeaverSource<S>,
) -> Self {
Self {
value: <$base_type>::$with_visibility_function(
x,
visibility,
network,
beaver_source,
),
visibility,
mac_share: None,
key_share,
}
}
};
}
macro_rules! impl_arithmetic_assign {
($lhs:ty, $trait:ident, $fn_name:ident, $op:tt, $rhs:ty) => {
impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<$rhs> for $lhs {
fn $fn_name(&mut self, rhs: $rhs) {
*self = &*self $op &rhs;
}
}
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $rhs> for $lhs {
fn $fn_name(&mut self, rhs: &'a $rhs) {
*self = &*self $op rhs
}
}
}
}
macro_rules! impl_operator_variants {
($lhs:ty, $trait:ident, $fn_name:ident, $op:tt, $rhs:ty) => {
macros::impl_operator_variants!($lhs, $trait, $fn_name, $op, $rhs, Output=$lhs);
};
($lhs:ty, $trait:ident, $fn_name:ident, $op:tt, $rhs:ty, Output=$out_type:ty) => {
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<$rhs> for &'a $lhs {
type Output = $out_type;
fn $fn_name(self, rhs: $rhs) -> Self::Output {
self $op &rhs
}
}
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $rhs> for $lhs {
type Output = $out_type;
fn $fn_name(self, rhs: &'a $rhs) -> Self::Output {
&self $op rhs
}
}
impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<$rhs> for $lhs {
type Output = $out_type;
fn $fn_name(self, rhs: $rhs) -> Self::Output {
&self $op &rhs
}
}
}
}
macro_rules! impl_wrapper_type {
($wrapper_type:ty, $wrapped_type:ty, $from_fn:expr, $trait:ident, $fn_name:ident, $op:tt, authenticated=false) => {
macros::impl_wrapper_type!(
$wrapper_type,
$wrapped_type,
$from_fn,
$trait,
$fn_name,
$op,
Output=$wrapper_type,
authenticated=false
);
};
($wrapper_type:ty, $wrapped_type:ty, $from_fn:expr, $trait:ident, $fn_name:ident, $op:tt, authenticated=true) => {
macros::impl_wrapper_type!(
$wrapper_type,
$wrapped_type,
$from_fn,
$trait,
$fn_name,
$op,
Output=$wrapper_type,
authenticated=true
);
};
($wrapper_type:ty, $wrapped_type:ty, $from_fn:expr, $trait:ident, $fn_name:ident, $op:tt, Output=$output_type:ty, authenticated=false) => {
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $wrapped_type>
for &'a $wrapper_type
{
type Output = $output_type;
fn $fn_name(self, rhs: &'a $wrapped_type) -> Self::Output {
self $op $from_fn(rhs.clone(), self.network.clone(), self.beaver_source.clone())
}
}
macros::impl_operator_variants!($wrapper_type, $trait, $fn_name, $op, $wrapped_type, Output=$output_type);
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $wrapper_type> for &'a $wrapped_type {
type Output = $output_type;
fn $fn_name(self, rhs: &'a $wrapper_type) -> Self::Output {
$from_fn(self.clone(), rhs.network.clone(), rhs.beaver_source.clone()) $op rhs
}
}
macros::impl_operator_variants!($wrapped_type, $trait, $fn_name, $op, $wrapper_type, Output=$output_type);
};
($wrapper_type:ty, $wrapped_type:ty, $from_fn:expr, $trait:ident, $fn_name:ident, $op:tt, Output=$output_type:ty, authenticated=true) => {
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $wrapped_type>
for &'a $wrapper_type
{
type Output = $output_type;
fn $fn_name(self, rhs: &'a $wrapped_type) -> Self::Output {
self $op $from_fn(rhs.clone(), self.key_share(), self.network(), self.beaver_source())
}
}
macros::impl_operator_variants!($wrapper_type, $trait, $fn_name, $op, $wrapped_type, Output=$output_type);
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> $trait<&'a $wrapper_type> for &'a $wrapped_type {
type Output = $output_type;
fn $fn_name(self, rhs: &'a $wrapper_type) -> Self::Output {
$from_fn(self.clone(), rhs.key_share(), rhs.network(), rhs.beaver_source()) $op rhs
}
}
macros::impl_operator_variants!($wrapped_type, $trait, $fn_name, $op, $wrapper_type, Output=$output_type);
};
}
pub(crate) use impl_arithmetic_assign;
pub(crate) use impl_authenticated;
pub(crate) use impl_delegated;
pub(crate) use impl_delegated_wrapper;
pub(crate) use impl_operator_variants;
pub(crate) use impl_wrapper_type;