Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add impl of Mul for NonZeroScalar * NonZeroScalar #857

Merged
merged 1 commit into from
Jan 3, 2022
Merged
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
32 changes: 31 additions & 1 deletion elliptic-curve/src/scalar/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use core::{
fmt,
ops::{Deref, Neg},
ops::{Deref, Mul, Neg},
str,
};
use crypto_bigint::{ArrayEncoding, Integer};
Expand Down Expand Up @@ -199,6 +199,36 @@ where
}
}

impl<C> Mul<NonZeroScalar<C>> for NonZeroScalar<C>
where
C: Curve + ScalarArithmetic,
{
type Output = Self;

fn mul(self, other: Self) -> Self {
// Assumes that the multiplication is modulo a prime,
// so the product of two non-zero scalars is also non-zero.
let scalar = self.scalar * other.scalar;
debug_assert!(!bool::from(scalar.is_zero()));
NonZeroScalar { scalar }
}
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
}

impl<C> Mul<&NonZeroScalar<C>> for NonZeroScalar<C>
where
C: Curve + ScalarArithmetic,
{
type Output = Self;

fn mul(self, other: &Self) -> Self {
// Assumes that the multiplication is modulo a prime,
// so the product of two non-zero scalars is also non-zero.
let scalar = self.scalar * other.scalar;
debug_assert!(!bool::from(scalar.is_zero()));
NonZeroScalar { scalar }
}
}

/// Note: implementation is the same as `ReduceNonZero`
impl<C, I> Reduce<I> for NonZeroScalar<C>
where
Expand Down