Skip to content

Commit

Permalink
p521: move Debug impl for FieldElement (#966)
Browse files Browse the repository at this point in the history
Puts it with the other trait impls
  • Loading branch information
tarcieri authored Nov 11, 2023
1 parent 10043e2 commit f19ccf0
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions p521/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) use self::loose::LooseFieldElement;
use self::field_impl::*;
use crate::{FieldBytes, NistP521, U576};
use core::{
fmt::{self, Debug},
iter::{Product, Sum},
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
Expand All @@ -54,40 +55,6 @@ const MODULUS: U576 = U576::from_be_hex(MODULUS_HEX);
#[derive(Clone, Copy)]
pub struct FieldElement(fiat_p521_tight_field_element);

impl core::fmt::Debug for FieldElement {
/// Formatting machinery for [`FieldElement`]
///
/// # Why
/// ```ignore
/// let fe1 = FieldElement([9, 0, 0, 0, 0, 0, 0, 0, 0]);
/// let fe2 = FieldElement([
/// 8,
/// 0,
/// 288230376151711744,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 144115188075855871,
/// ]);
/// ```
///
/// For the above example, deriving [`core::fmt::Debug`] will result in returning 2 different strings,
/// which are in reality the same due to p521's unsaturated math, instead print the output as a hex string in
/// big-endian
///
/// This makes debugging easier.
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut bytes = fiat_p521_to_bytes(&self.0);
bytes.reverse();
let formatter = base16ct::HexDisplay(&bytes);
f.debug_tuple("FieldElement")
.field(&format_args!("0x{formatter:X}"))
.finish()
}
}

impl FieldElement {
/// Zero element.
pub const ZERO: Self = Self::from_u64(0);
Expand Down Expand Up @@ -341,6 +308,41 @@ impl Default for FieldElement {
}
}

impl Debug for FieldElement {
/// Formatting machinery for [`FieldElement`]
///
/// # Why
/// ```ignore
/// let fe1 = FieldElement([9, 0, 0, 0, 0, 0, 0, 0, 0]);
/// let fe2 = FieldElement([
/// 8,
/// 0,
/// 288230376151711744,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 288230376151711743,
/// 144115188075855871,
/// ]);
/// ```
///
/// For the above example, deriving [`core::fmt::Debug`] will result in returning 2 different
/// strings, which are in reality the same due to p521's unsaturated math, instead print the
/// output as a hex string in big-endian.
///
/// This makes debugging easier.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut bytes = fiat_p521_to_bytes(&self.0);
bytes.reverse();

let formatter = base16ct::HexDisplay(&bytes);
f.debug_tuple("FieldElement")
.field(&format_args!("0x{formatter:X}"))
.finish()
}
}

impl Eq for FieldElement {}
impl PartialEq for FieldElement {
fn eq(&self, rhs: &Self) -> bool {
Expand Down

0 comments on commit f19ccf0

Please sign in to comment.