diff --git a/crates/math/src/field/element.rs b/crates/math/src/field/element.rs index 2c2f5b27c..135908051 100644 --- a/crates/math/src/field/element.rs +++ b/crates/math/src/field/element.rs @@ -6,10 +6,7 @@ use crate::unsigned_integer::element::UnsignedInteger; use crate::unsigned_integer::montgomery::MontgomeryAlgorithms; use crate::unsigned_integer::traits::IsUnsignedInteger; #[cfg(feature = "alloc")] -use alloc::{ - format, - string::{String, ToString}, -}; +use alloc::{format, string::String}; use core::fmt; use core::fmt::Debug; use core::iter::Sum; @@ -521,21 +518,11 @@ where Self: ByteConversion, F: IsPrimeField, { - let mod_minus_one = F::modulus_minus_one().to_string(); - - // We check if `mod_minus_one` is a hex string or a decimal string. - // In case it is a hex we remove the prefix `0x`. - let (digits, radix) = if let Some(hex) = mod_minus_one - .strip_prefix("0x") - .or_else(|| mod_minus_one.strip_prefix("0X")) - { - (hex, 16) - } else { - (mod_minus_one.as_str(), 10) - }; + let mod_minus_one = format!("{:x}", F::modulus_minus_one()); - let modulus = - BigUint::from_str_radix(digits, radix).expect("invalid modulus representation") + 1u32; + let modulus = BigUint::from_str_radix(&mod_minus_one, 16) + .expect("invalid modulus representation") + + 1u32; if value >= &modulus { Err(ByteConversionError::ValueNotReduced) diff --git a/crates/math/src/unsigned_integer/element.rs b/crates/math/src/unsigned_integer/element.rs index 5f25a67da..3bdb81797 100644 --- a/crates/math/src/unsigned_integer/element.rs +++ b/crates/math/src/unsigned_integer/element.rs @@ -19,7 +19,7 @@ use crate::traits::AsBytes; use crate::traits::ByteConversion; use crate::unsigned_integer::traits::IsUnsignedInteger; -use core::fmt::{self, Debug, Display}; +use core::fmt::{self, Debug, Display, LowerHex, UpperHex}; pub type U384 = UnsignedInteger<6>; pub type U256 = UnsignedInteger<4>; @@ -109,12 +109,37 @@ impl From<&str> for UnsignedInteger { impl Display for UnsignedInteger { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "0x")?; + let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable(); if limbs_iterator.peek().is_none() { - write!(f, "0x0")?; + write!(f, "0")?; } else { + if let Some(most_significant_limb) = limbs_iterator.next() { + write!(f, "{most_significant_limb:x}")?; + } + + for limb in limbs_iterator { + write!(f, "{limb:016x}")?; + } + } + + Ok(()) + } +} + +impl LowerHex for UnsignedInteger { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { write!(f, "0x")?; + } + + let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable(); + + if limbs_iterator.peek().is_none() { + write!(f, "0")?; + } else { if let Some(most_significant_limb) = limbs_iterator.next() { write!(f, "{most_significant_limb:x}")?; } @@ -128,6 +153,30 @@ impl Display for UnsignedInteger { } } +impl UpperHex for UnsignedInteger { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if f.alternate() { + write!(f, "0X")?; + } + + let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable(); + + if limbs_iterator.peek().is_none() { + write!(f, "0")?; + } else { + if let Some(most_significant_limb) = limbs_iterator.next() { + write!(f, "{most_significant_limb:X}")?; + } + + for limb in limbs_iterator { + write!(f, "{limb:016X}")?; + } + } + + Ok(()) + } +} + // impl Add for both references and variables impl Add<&UnsignedInteger> for &UnsignedInteger { diff --git a/crates/math/src/unsigned_integer/traits.rs b/crates/math/src/unsigned_integer/traits.rs index ccda44f9f..e1b29d412 100644 --- a/crates/math/src/unsigned_integer/traits.rs +++ b/crates/math/src/unsigned_integer/traits.rs @@ -1,5 +1,5 @@ use core::{ - fmt::Display, + fmt::{Display, LowerHex, UpperHex}, ops::{Add, BitAnd, Shr, ShrAssign}, }; @@ -12,6 +12,8 @@ pub trait IsUnsignedInteger: + From + Copy + Display + + LowerHex + + UpperHex + Add { }