Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Fix/debug format #36

Merged
merged 12 commits into from
Sep 22, 2023
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: CI checks

on: push
on:
push:
branches: [main]
pull_request:
branches:
- "**"

jobs:
test:
Expand Down
83 changes: 82 additions & 1 deletion src/fp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

use core::fmt::{self, Formatter};

#[cfg(not(feature = "std"))]
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};

use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

use crate::fp::reduce_u96;
Expand Down Expand Up @@ -42,7 +49,53 @@ pub(crate) struct Fp3 {

impl fmt::Debug for Fp3 {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{:?} + {:?}*u + {:?}*u^2", self.a0, self.a1, self.a2)
if *self == Fp3::zero() {
return write!(f, "0"); // Handle the case where all coefficients are zero
}

let coeffs = [self.a0, self.a1, self.a2];

let format_term = |coef: Fp, degree: usize| -> String {
if coef == Fp::one() && degree > 0 {
let exp = if degree > 1 {
format!("^{}", degree)
} else {
"".to_string()
};
format!("u{}", exp)
} else {
let exp = if degree > 0 {
format!(
"u{}",
if degree > 1 {
format!("^{}", degree)
} else {
"".to_string()
}
)
} else {
"".to_string()
};
format!("{}{}", coef, exp)
}
};

let elem_rep = coeffs
.iter()
.enumerate()
.filter_map(|(i, &coef)| {
if coef == Fp::zero() {
None
} else {
Some(format_term(coef, i))
}
})
.collect::<Vec<String>>()
.join(" + ");

write!(f, "{}", elem_rep)?;

Ok(())
}
}

Expand Down Expand Up @@ -260,6 +313,34 @@ mod tests {
}
}

// DISPLAY
// ================================================================================================
#[test]
fn test_debug() {
assert_eq!(format!("{:?}", Fp3::zero()), "0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but coefficients are crate-accessible, so you could have still tested the various cases you had originally, by directly calling say

Fp3 {
    a0: Fp::one(),
    a1: Fp::new(3),
    a2: Fp::new(5),
}

but again that's not a big deal as this is not part of the API.

assert_eq!(format!("{:?}", Fp3::one()), "1");

let a = Fp3 {
a0: Fp::new(0),
a1: Fp::new(0),
a2: Fp::new(7),
};
assert_eq!(format!("{:?}", a), "7u^2");

let b = Fp3 {
a0: Fp::new(1),
a1: Fp::new(0),
a2: Fp::new(11),
};
assert_eq!(format!("{:?}", b), "1 + 11u^2");

let c = Fp3 {
a0: Fp::new(1),
a1: Fp::new(2),
a2: Fp::new(1),
};
assert_eq!(format!("{:?}", c), "1 + 2u + u^2");
}
// BASIC ALGEBRA
// ================================================================================================

Expand Down
82 changes: 66 additions & 16 deletions src/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ use core::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

#[cfg(not(feature = "std"))]
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};

use group::ff::Field;
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
Expand Down Expand Up @@ -56,11 +63,53 @@ pub struct Fp6 {

impl fmt::Debug for Fp6 {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"{:?} + {:?}*u + {:?}*u^2 + {:?}*u^3 + {:?}*u^4 + {:?}*u^5",
self.c0, self.c1, self.c2, self.c3, self.c4, self.c5
)
if self.is_zero().into() {
return write!(f, "0"); // Handle the case where all coefficients are zero
}

let coeffs: [Fp; 6] = self.into();

let format_term = |coef: Fp, degree: usize| -> String {
if coef == Fp::one() && degree > 0 {
let exp = if degree > 1 {
format!("^{}", degree)
} else {
"".to_string()
};
format!("u{}", exp)
} else {
let exp = if degree > 0 {
format!(
"u{}",
if degree > 1 {
format!("^{}", degree)
} else {
"".to_string()
}
)
} else {
"".to_string()
};
format!("{}{}", coef, exp)
}
};

let elem_rep = coeffs
.iter()
.enumerate()
.filter_map(|(i, &coef)| {
if coef == Fp::zero() {
None
} else {
Some(format_term(coef, i))
}
})
.collect::<Vec<String>>()
.join(" + ");

write!(f, "{}", elem_rep)?;

Ok(())
}
}

Expand Down Expand Up @@ -1258,24 +1307,25 @@ mod tests {

#[test]
fn test_debug() {
assert_eq!(format!("{:?}", Fp6::one()), "1");
assert_eq!(format!("{:?}", Fp6::zero()), "0");
assert_eq!(
format!("{:?}", Fp6::zero()),
"0 + 0*u + 0*u^2 + 0*u^3 + 0*u^4 + 0*u^5"
);
assert_eq!(
format!("{:?}", Fp6::one()),
"1 + 0*u + 0*u^2 + 0*u^3 + 0*u^4 + 0*u^5"
format!("{:?}", Fp6::new([1, 2, 3, 4, 5, 6])),
"1 + 2u + 3u^2 + 4u^3 + 5u^4 + 6u^5"
);
assert_eq!(
format!("{:?}", Fp6::new([1, 2, 3, 4, 5, 6])),
"1 + 2*u + 3*u^2 + 4*u^3 + 5*u^4 + 6*u^5"
format!("{:?}", Fp6::new([0, 1, 2, 0, 5, 23])),
"u + 2u^2 + 5u^4 + 23u^5"
);
assert_eq!(format!("{:?}", Fp6::new([0, 0, 0, 0, 0, 1])), "u^5");

let a = Fp6::one().neg();
assert_eq!(format!("{:?}", a), "18446744069414584320");

assert_eq!(
format!("{:?}", a),
"18446744069414584320 + 0*u + 0*u^2 + 0*u^3 + 0*u^4 + 0*u^5"
);
format!("{:?}", Fp6::new([1, 1, 1, 1, 1, 1])),
"1 + u + u^2 + u^3 + u^4 + u^5"
)
}

#[test]
Expand Down
Loading