Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions crates/math/src/field/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 51 additions & 2 deletions crates/math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -109,12 +109,37 @@ impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {

impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
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<const NUM_LIMBS: usize> LowerHex for UnsignedInteger<NUM_LIMBS> {
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}")?;
}
Expand All @@ -128,6 +153,30 @@ impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
}
}

impl<const NUM_LIMBS: usize> UpperHex for UnsignedInteger<NUM_LIMBS> {
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<const NUM_LIMBS: usize> Add<&UnsignedInteger<NUM_LIMBS>> for &UnsignedInteger<NUM_LIMBS> {
Expand Down
4 changes: 3 additions & 1 deletion crates/math/src/unsigned_integer/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::{
fmt::Display,
fmt::{Display, LowerHex, UpperHex},
ops::{Add, BitAnd, Shr, ShrAssign},
};

Expand All @@ -12,6 +12,8 @@ pub trait IsUnsignedInteger:
+ From<u16>
+ Copy
+ Display
+ LowerHex
+ UpperHex
+ Add<Self, Output = Self>
{
}
Expand Down
Loading