Skip to content

Commit 410c8f3

Browse files
committed
feat: impl LowerHex and UpperHex on UnsignedElement
1 parent 83313da commit 410c8f3

File tree

8 files changed

+60
-21
lines changed

8 files changed

+60
-21
lines changed

crates/math/src/fft/cpu/fft.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ mod tests {
183183
use crate::fft::cpu::roots_of_unity::get_twiddles;
184184
use crate::fft::test_helpers::naive_matrix_dft_test;
185185
use crate::field::{test_fields::u64_test_field::U64TestField, traits::RootsConfig};
186+
use alloc::format;
186187
use proptest::{collection, prelude::*};
187188

188189
use super::*;

crates/math/src/fft/cpu/roots_of_unity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ mod tests {
8383
},
8484
field::{test_fields::u64_test_field::U64TestField, traits::RootsConfig},
8585
};
86+
use alloc::format;
8687
use proptest::prelude::*;
8788

8889
type F = U64TestField;

crates/math/src/fft/polynomial.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ mod tests {
240240
test_fields::u64_test_field::{U64TestField, U64TestFieldExtension},
241241
traits::RootsConfig,
242242
};
243+
use alloc::format;
243244
use proptest::{collection, prelude::*};
244245

245246
use roots_of_unity::{get_powers_of_primitive_root, get_powers_of_primitive_root_coset};

crates/math/src/fft/test_helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn naive_matrix_dft_test<F: IsFFTField>(input: &[FieldElement<F>]) -> Vec<Fi
3535
mod fft_helpers_test {
3636
use super::*;
3737
use crate::{field::test_fields::u64_test_field::U64TestField, polynomial::Polynomial};
38+
use alloc::format;
3839

3940
use proptest::{collection, prelude::*};
4041

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/msm/pippenger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mod tests {
170170
},
171171
unsigned_integer::element::UnsignedInteger,
172172
};
173-
use alloc::vec::Vec;
173+
use alloc::{format, vec::Vec};
174174
use proptest::{collection, prelude::*, prop_assert_eq, prop_compose, proptest};
175175

176176
const _CASES: u32 = 20;

crates/math/src/unsigned_integer/element.rs

Lines changed: 47 additions & 1 deletion
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>;
@@ -108,6 +108,28 @@ impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {
108108
}
109109

110110
impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
111+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112+
write!(f, "0x")?;
113+
114+
let mut limbs_iterator = self.limbs.iter().skip_while(|limb| **limb == 0).peekable();
115+
116+
if limbs_iterator.peek().is_none() {
117+
write!(f, "0")?;
118+
} 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> {
111133
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112134
if f.alternate() {
113135
write!(f, "0x")?;
@@ -131,6 +153,30 @@ impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
131153
}
132154
}
133155

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+
134180
// impl Add for both references and variables
135181

136182
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)