From b002da84fd32ab228020b18d58a9c11ee89ed071 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 7 Feb 2024 18:10:54 -0700 Subject: [PATCH] Use `ConstZero` trait from `num_traits` Added in rust-num/num-traits#303 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/modular/const_monty_form.rs | 12 ++++++++++++ src/traits.rs | 12 ++---------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b494c45..a562db9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -424,9 +424,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", "libm", diff --git a/Cargo.toml b/Cargo.toml index 9c3f8ce6..b191dd4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ subtle = { version = "2.5", default-features = false } # optional dependencies der = { version = "0.7", optional = true, default-features = false } hybrid-array = { version = "0.2.0-rc.4", optional = true } -num-traits = { version = "0.2", default-features = false } +num-traits = { version = "0.2.18", default-features = false } rand_core = { version = "0.6.4", optional = true } rlp = { version = "0.5", optional = true, default-features = false } serdect = { version = "0.2", optional = true, default-features = false } diff --git a/src/modular/const_monty_form.rs b/src/modular/const_monty_form.rs index 3e1f52e7..6c3c4c6c 100644 --- a/src/modular/const_monty_form.rs +++ b/src/modular/const_monty_form.rs @@ -185,6 +185,18 @@ impl, const LIMBS: usize> ConstZero for ConstMontyF const ZERO: Self = Self::ZERO; } +impl, const LIMBS: usize> num_traits::Zero + for ConstMontyForm +{ + fn zero() -> Self { + Self::ZERO + } + + fn is_zero(&self) -> bool { + self.ct_eq(&Self::ZERO).into() + } +} + #[cfg(feature = "rand_core")] impl Random for ConstMontyForm where diff --git a/src/traits.rs b/src/traits.rs index 2780226a..1dde9261 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -3,7 +3,7 @@ mod sealed; pub use num_traits::{ - WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub, + ConstZero, WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub, }; pub(crate) use sealed::PrecomputeInverterWithAdjuster; @@ -264,15 +264,7 @@ pub trait Zero: ConstantTimeEq + Sized { } } -/// Trait for associating a constant representing zero. -/// -/// Types which impl this trait automatically receive a blanket impl of [`Zero`]. -pub trait ConstZero: Zero { - /// The value `0`. - const ZERO: Self; -} - -impl Zero for T { +impl Zero for T { #[inline(always)] fn zero() -> T { Self::ZERO