Bring in the macros used for operator overloading in the bls12_381 crate.

This commit is contained in:
Sean Bowe 2019-12-03 18:01:28 -07:00
parent e83f7d2bd1
commit e3766101f4
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
1 changed files with 67 additions and 50 deletions

View File

@ -19,92 +19,75 @@ pub const fn mac(a: u64, b: u64, c: u64, carry: u64) -> (u64, u64) {
(ret as u64, (ret >> 64) as u64)
}
macro_rules! impl_binops_additive {
($lhs:ident, $rhs:ident) => {
impl<'b> Sub<&'b $rhs> for $lhs {
type Output = $lhs;
#[inline]
fn sub(self, rhs: &'b $rhs) -> $lhs {
&self - rhs
}
}
macro_rules! impl_add_binop_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Add<&'b $rhs> for $lhs {
type Output = $lhs;
type Output = $output;
#[inline]
fn add(self, rhs: &'b $rhs) -> $lhs {
fn add(self, rhs: &'b $rhs) -> $output {
&self + rhs
}
}
impl<'a> Sub<$rhs> for &'a $lhs {
type Output = $lhs;
#[inline]
fn sub(self, rhs: $rhs) -> $lhs {
self - &rhs
}
}
impl<'a> Add<$rhs> for &'a $lhs {
type Output = $lhs;
type Output = $output;
#[inline]
fn add(self, rhs: $rhs) -> $lhs {
fn add(self, rhs: $rhs) -> $output {
self + &rhs
}
}
impl Sub<$rhs> for $lhs {
type Output = $lhs;
#[inline]
fn sub(self, rhs: $rhs) -> $lhs {
&self - &rhs
}
}
impl Add<$rhs> for $lhs {
type Output = $lhs;
type Output = $output;
#[inline]
fn add(self, rhs: $rhs) -> $lhs {
fn add(self, rhs: $rhs) -> $output {
&self + &rhs
}
}
};
}
macro_rules! impl_sub_binop_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Sub<&'b $rhs> for $lhs {
type Output = $output;
impl SubAssign<$rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: $rhs) {
*self = &*self - &rhs;
fn sub(self, rhs: &'b $rhs) -> $output {
&self - rhs
}
}
impl AddAssign<$rhs> for $lhs {
impl<'a> Sub<$rhs> for &'a $lhs {
type Output = $output;
#[inline]
fn add_assign(&mut self, rhs: $rhs) {
*self = &*self + &rhs;
fn sub(self, rhs: $rhs) -> $output {
self - &rhs
}
}
impl<'b> SubAssign<&'b $rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: &'b $rhs) {
*self = &*self - rhs;
}
}
impl Sub<$rhs> for $lhs {
type Output = $output;
impl<'b> AddAssign<&'b $rhs> for $lhs {
#[inline]
fn add_assign(&mut self, rhs: &'b $rhs) {
*self = &*self + rhs;
fn sub(self, rhs: $rhs) -> $output {
&self - &rhs
}
}
};
}
macro_rules! impl_binops_additive_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl_add_binop_specify_output!($lhs, $rhs, $output);
impl_sub_binop_specify_output!($lhs, $rhs, $output);
};
}
macro_rules! impl_binops_multiplicative_mixed {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Mul<&'b $rhs> for $lhs {
@ -136,6 +119,40 @@ macro_rules! impl_binops_multiplicative_mixed {
};
}
macro_rules! impl_binops_additive {
($lhs:ident, $rhs:ident) => {
impl_binops_additive_specify_output!($lhs, $rhs, $lhs);
impl SubAssign<$rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: $rhs) {
*self = &*self - &rhs;
}
}
impl AddAssign<$rhs> for $lhs {
#[inline]
fn add_assign(&mut self, rhs: $rhs) {
*self = &*self + &rhs;
}
}
impl<'b> SubAssign<&'b $rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: &'b $rhs) {
*self = &*self - rhs;
}
}
impl<'b> AddAssign<&'b $rhs> for $lhs {
#[inline]
fn add_assign(&mut self, rhs: &'b $rhs) {
*self = &*self + rhs;
}
}
};
}
macro_rules! impl_binops_multiplicative {
($lhs:ident, $rhs:ident) => {
impl_binops_multiplicative_mixed!($lhs, $rhs, $lhs);