Skip to content

Commit 3b96e92

Browse files
authored
feat: support formatter alternate display (#1049)
* feat: support formatter alternate display * feat: impl LowerHex and UpperHex on UnsignedElement
1 parent d9c3c04 commit 3b96e92

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

crates/math/src/field/element.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use crate::unsigned_integer::element::UnsignedInteger;
66
use crate::unsigned_integer::montgomery::MontgomeryAlgorithms;
77
use crate::unsigned_integer::traits::IsUnsignedInteger;
88
#[cfg(feature = "alloc")]
9-
use alloc::{
10-
format,
11-
string::{String, ToString},
12-
};
9+
use alloc::{format, string::String};
1310
use core::fmt;
1411
use core::fmt::Debug;
1512
use core::iter::Sum;
@@ -521,21 +518,11 @@ where
521518
Self: ByteConversion,
522519
F: IsPrimeField,
523520
{
524-
let mod_minus_one = F::modulus_minus_one().to_string();
525-
526-
// We check if `mod_minus_one` is a hex string or a decimal string.
527-
// In case it is a hex we remove the prefix `0x`.
528-
let (digits, radix) = if let Some(hex) = mod_minus_one
529-
.strip_prefix("0x")
530-
.or_else(|| mod_minus_one.strip_prefix("0X"))
531-
{
532-
(hex, 16)
533-
} else {
534-
(mod_minus_one.as_str(), 10)
535-
};
521+
let mod_minus_one = format!("{:x}", F::modulus_minus_one());
536522

537-
let modulus =
538-
BigUint::from_str_radix(digits, radix).expect("invalid modulus representation") + 1u32;
523+
let modulus = BigUint::from_str_radix(&mod_minus_one, 16)
524+
.expect("invalid modulus representation")
525+
+ 1u32;
539526

540527
if value >= &modulus {
541528
Err(ByteConversionError::ValueNotReduced)

crates/math/src/unsigned_integer/element.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::traits::AsBytes;
1919
use crate::traits::ByteConversion;
2020
use crate::unsigned_integer::traits::IsUnsignedInteger;
2121

22-
use core::fmt::{self, Debug, Display};
22+
use core::fmt::{self, Debug, Display, LowerHex, UpperHex};
2323

2424
pub type U384 = UnsignedInteger<6>;
2525
pub type U256 = UnsignedInteger<4>;
@@ -109,12 +109,37 @@ impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {
109109

110110
impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
111111
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112+
write!(f, "0x")?;
113+
112114
let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable();
113115

114116
if limbs_iterator.peek().is_none() {
115-
write!(f, "0x0")?;
117+
write!(f, "0")?;
116118
} else {
119+
if let Some(most_significant_limb) = limbs_iterator.next() {
120+
write!(f, "{most_significant_limb:x}")?;
121+
}
122+
123+
for limb in limbs_iterator {
124+
write!(f, "{limb:016x}")?;
125+
}
126+
}
127+
128+
Ok(())
129+
}
130+
}
131+
132+
impl<const NUM_LIMBS: usize> LowerHex for UnsignedInteger<NUM_LIMBS> {
133+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134+
if f.alternate() {
117135
write!(f, "0x")?;
136+
}
137+
138+
let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable();
139+
140+
if limbs_iterator.peek().is_none() {
141+
write!(f, "0")?;
142+
} else {
118143
if let Some(most_significant_limb) = limbs_iterator.next() {
119144
write!(f, "{most_significant_limb:x}")?;
120145
}
@@ -128,6 +153,30 @@ impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
128153
}
129154
}
130155

156+
impl<const NUM_LIMBS: usize> UpperHex for UnsignedInteger<NUM_LIMBS> {
157+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158+
if f.alternate() {
159+
write!(f, "0X")?;
160+
}
161+
162+
let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable();
163+
164+
if limbs_iterator.peek().is_none() {
165+
write!(f, "0")?;
166+
} else {
167+
if let Some(most_significant_limb) = limbs_iterator.next() {
168+
write!(f, "{most_significant_limb:X}")?;
169+
}
170+
171+
for limb in limbs_iterator {
172+
write!(f, "{limb:016X}")?;
173+
}
174+
}
175+
176+
Ok(())
177+
}
178+
}
179+
131180
// impl Add for both references and variables
132181

133182
impl<const NUM_LIMBS: usize> Add<&UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {

crates/math/src/unsigned_integer/traits.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::{
2-
fmt::Display,
2+
fmt::{Display, LowerHex, UpperHex},
33
ops::{Add, BitAnd, Shr, ShrAssign},
44
};
55

@@ -12,6 +12,8 @@ pub trait IsUnsignedInteger:
1212
+ From<u16>
1313
+ Copy
1414
+ Display
15+
+ LowerHex
16+
+ UpperHex
1517
+ Add<Self, Output = Self>
1618
{
1719
}

0 commit comments

Comments
 (0)