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
Merged

Fix/debug format #36

merged 12 commits into from
Sep 22, 2023

Conversation

RajeshRk18
Copy link
Contributor

Description

Changed debug format for Fp3 and Fp6 such that coeffs of zero and one are handled.
Before the change, Fp6::new([1, 0, 0, 1, 0, 1]) is formatted as 1 + 0 * u + 0 * u^2 + 1 * u^3 + 0 * u^4 + 1 * u^5.

After the change, it is formatted as 1 + u^3 + u^5.

PR Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added or updated tests that comprehensively prove my change is effective or that my feature works

Copy link
Contributor

@Nashtare Nashtare left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, this is indeed a nice idea! I have two suggestions:

  • Fp3 is not exposed by this library, and is only used to speed-up some Fp6 operations internally, hence the choice of not implementing an extended set of features. As such, although a better debug information is welcome, the Fp3::new() method is not needed as only for debug testing purposes, which can be ignored for this particular struct.
  • I believe the approach of string concatenation for both Fp3 and Fp6 can be made simpler and more compact/efficient by reducing the number of write! calls. Have you tried iterating over the coefficients array and folding, with a custom method for writing the underlying Fp element at each step?

src/fp3.rs Outdated
Comment on lines 505 to 506


Copy link
Contributor

Choose a reason for hiding this comment

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

This is unnecessary,

src/fp6.rs Outdated
"{:?} + {:?}*u + {:?}*u^2 + {:?}*u^3 + {:?}*u^4 + {:?}*u^5",
self.c0, self.c1, self.c2, self.c3, self.c4, self.c5
)
let coeffs = [self.c0, self.c1, self.c2, self.c3, self.c4, self.c5];
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: From<Fp6> for [Fp; 6] is implemented.

src/fp6.rs Outdated
Comment on lines 86 to 88
if first_term {
write!(f, "")?; // Handle the case where all coefficients are zero
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not display an empty string for the zero case, it should display 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed! Somehow I missed it

@Nashtare
Copy link
Contributor

Also could you please rebase from latest main? I've had to update the CI to trigger jobs on your PR, sorry about that!

@RajeshRk18
Copy link
Contributor Author

Also could you please rebase from latest main? I've had to update the CI to trigger jobs on your PR, sorry about that!

Is it fine now?

Copy link
Contributor

@Nashtare Nashtare left a comment

Choose a reason for hiding this comment

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

Thanks a bunch!
I've added a few more comments inline, basically to handle the 0 edge-case and simplify the return, otherwise looks good!

Could you just make sure the CI goes green (apart from the code coverage one). It is currently failing for rustfmt and no-std builds (some missing alloc imports). Should be good to merge after this!

// ================================================================================================
#[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.

src/fp3.rs Outdated
Comment on lines 78 to 84
if elem_rep.is_empty() {
write!(f, "0")?; // Handle the case where all coefficients are zero
} else {
write!(f, "{}", elem_rep)?;
}

Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be cleaner to handle the 0 case first, with

if *self == Fp3::zero() {
    return write!(f, "0");
}

so that this segment can be simply replaced by

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

src/fp6.rs Outdated
Comment on lines 92 to 98
if elem_rep.is_empty() {
write!(f, "0")?; // Handle the case where all coefficients are zero
} else {
write!(f, "{}", elem_rep)?;
}

Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment on special case handling than Fp3. And Fp6 does implement is_zero() directly, which can then be converting to a bool.

src/fp3.rs Outdated
Comment on lines 78 to 80
if *self == Fp3::zero() {
return write!(f, "0"); // Handle the case where all coefficients are zero
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah sorry, maybe I wasn't being clear, but I was suggesting to have it as an initial check, at the start of the function (it is not necessary to process the list of coefficients if the element is zero).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I should have done that :)

Copy link
Contributor

@Nashtare Nashtare left a comment

Choose a reason for hiding this comment

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

Thanks again! Looks good!

@Nashtare Nashtare merged commit b3b9f93 into toposware:main Sep 22, 2023
5 of 6 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants