From f484ed6326282dc834451ae5a51cda4e60f275bf Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 17 Nov 2021 14:22:56 -0700 Subject: [PATCH] elliptic-curve: impl `Neg` for `NonZeroScalar` (#816) --- elliptic-curve/src/scalar/non_zero.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/elliptic-curve/src/scalar/non_zero.rs b/elliptic-curve/src/scalar/non_zero.rs index 8a1749676..818f5d101 100644 --- a/elliptic-curve/src/scalar/non_zero.rs +++ b/elliptic-curve/src/scalar/non_zero.rs @@ -6,7 +6,7 @@ use crate::{ rand_core::{CryptoRng, RngCore}, Curve, Error, FieldBytes, IsHigh, Result, Scalar, ScalarArithmetic, ScalarCore, SecretKey, }; -use core::ops::Deref; +use core::ops::{Deref, Neg}; use ff::{Field, PrimeField}; use generic_array::GenericArray; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -175,6 +175,19 @@ where } } +impl Neg for NonZeroScalar +where + C: Curve + ScalarArithmetic, +{ + type Output = NonZeroScalar; + + fn neg(self) -> NonZeroScalar { + let scalar = -self.scalar; + debug_assert!(!bool::from(scalar.is_zero())); + NonZeroScalar { scalar } + } +} + impl TryFrom<&[u8]> for NonZeroScalar where C: Curve + ScalarArithmetic,