Skip to content

Commit

Permalink
Remove arithmetic implementations without reference for curve25519 keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Sep 19, 2022
1 parent 6cc99e6 commit 02ae722
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 108 deletions.
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 4 additions & 85 deletions src/asymmetric_crypto/curve25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,46 +118,6 @@ impl Display for X25519PrivateKey {
}
}

impl<'a> Mul<&'a Self> for X25519PrivateKey {
type Output = Self;

fn mul(self, rhs: &Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}

impl<'a> Mul<&'a X25519PrivateKey> for &X25519PrivateKey {
type Output = X25519PrivateKey;

fn mul(self, rhs: &X25519PrivateKey) -> Self::Output {
X25519PrivateKey(self.0 * rhs.0)
}
}

impl Add for X25519PrivateKey {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}

impl<'a> Add<&'a Self> for X25519PrivateKey {
type Output = Self;

fn add(self, rhs: &Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}

impl<'a> Add<X25519PrivateKey> for &'a X25519PrivateKey {
type Output = X25519PrivateKey;

fn add(self, rhs: X25519PrivateKey) -> Self::Output {
X25519PrivateKey(self.0 + rhs.0)
}
}

impl<'a> Add<&'a X25519PrivateKey> for &X25519PrivateKey {
type Output = X25519PrivateKey;

Expand All @@ -166,14 +126,6 @@ impl<'a> Add<&'a X25519PrivateKey> for &X25519PrivateKey {
}
}

impl Sub for X25519PrivateKey {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}

impl<'a> Sub<&'a X25519PrivateKey> for &X25519PrivateKey {
type Output = X25519PrivateKey;

Expand All @@ -182,20 +134,11 @@ impl<'a> Sub<&'a X25519PrivateKey> for &X25519PrivateKey {
}
}

impl Mul for X25519PrivateKey {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}

impl Div for X25519PrivateKey {
type Output = Self;
impl<'a> Mul<&'a X25519PrivateKey> for &X25519PrivateKey {
type Output = X25519PrivateKey;

fn div(self, rhs: Self) -> Self::Output {
#[allow(clippy::suspicious_arithmetic_impl)]
Self(self.0 * rhs.0.invert())
fn mul(self, rhs: &X25519PrivateKey) -> Self::Output {
X25519PrivateKey(self.0 * rhs.0)
}
}

Expand Down Expand Up @@ -319,14 +262,6 @@ impl Display for X25519PublicKey {
}
}

impl Sub for X25519PublicKey {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}

impl<'a> Sub<&'a X25519PublicKey> for &X25519PublicKey {
type Output = X25519PublicKey;

Expand All @@ -335,14 +270,6 @@ impl<'a> Sub<&'a X25519PublicKey> for &X25519PublicKey {
}
}

impl Add for X25519PublicKey {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}

impl<'a> Add<&'a X25519PublicKey> for &X25519PublicKey {
type Output = X25519PublicKey;

Expand All @@ -351,14 +278,6 @@ impl<'a> Add<&'a X25519PublicKey> for &X25519PublicKey {
}
}

impl Mul<X25519PrivateKey> for X25519PublicKey {
type Output = Self;

fn mul(self, rhs: X25519PrivateKey) -> Self::Output {
Self(self.0 * rhs.0)
}
}

impl<'a> Mul<&'a X25519PrivateKey> for &X25519PublicKey {
type Output = X25519PublicKey;

Expand Down
9 changes: 7 additions & 2 deletions src/asymmetric_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ pub mod curve25519;
pub trait DhKeyPair<const PK_LENGTH: usize, const SK_LENGTH: usize>:
Debug + PartialEq + Eq + Send + Sync + Sized + Clone + Zeroize + ZeroizeOnDrop
where
Self::PublicKey: From<Self::PrivateKey> + Add + Mul<Self::PrivateKey, Output = Self::PublicKey>,
Self::PrivateKey: Add + Sub + Mul + Div,
Self::PublicKey: From<Self::PrivateKey>,
for<'a, 'b> &'a Self::PublicKey: Add<&'b Self::PublicKey, Output = Self::PublicKey>
+ Mul<&'b Self::PrivateKey, Output = Self::PublicKey>,
for<'a, 'b> &'a Self::PrivateKey: Add<&'b Self::PrivateKey, Output = Self::PrivateKey>
+ Sub<&'b Self::PrivateKey, Output = Self::PrivateKey>
+ Mul<&'b Self::PrivateKey, Output = Self::PrivateKey>
+ Div<&'b Self::PrivateKey, Output = Self::PrivateKey>,
{
/// This is needed to be able to use `{ MyKeyPair::PK_LENGTH }`
/// as associated constant
Expand Down

0 comments on commit 02ae722

Please sign in to comment.