Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Babybear field #549

Merged
merged 14 commits into from
Sep 15, 2023
Merged

Add Babybear field #549

merged 14 commits into from
Sep 15, 2023

Conversation

mdvillagra
Copy link
Contributor

@mdvillagra mdvillagra commented Sep 5, 2023

Babybear field implementation

Description

Add Babybear field with arithmetic operations as requested on issue #539. The field uses MontgomeryBackendPrimeField and implements the same traits as in Stark252PrimeField. The field elements are encoded using type u64 considering that Babybear only requires 31 bits.

Type of change

  • New feature
  • Bug fix
  • Optimization

Checklist

  • Linked to Github Issue
  • Unit tests added
  • This change requires new documentation.
    • Documentation has been added/updated.
  • This change is an Optimization
    • Benchmarks added/run

@mdvillagra mdvillagra requested review from schouhy, ajgara and a team as code owners September 5, 2023 21:53
@codecov-commenter
Copy link

codecov-commenter commented Sep 5, 2023

Codecov Report

Merging #549 (442dabd) into main (29af640) will decrease coverage by 0.04%.
The diff coverage is 88.13%.

@@            Coverage Diff             @@
##             main     #549      +/-   ##
==========================================
- Coverage   95.83%   95.79%   -0.04%     
==========================================
  Files          78       79       +1     
  Lines       12311    12370      +59     
==========================================
+ Hits        11798    11850      +52     
- Misses        513      520       +7     
Files Changed Coverage Δ
math/src/unsigned_integer/element.rs 97.17% <ø> (ø)
math/src/field/fields/fft_friendly/babybear.rs 88.13% <88.13%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@MauroToscano MauroToscano left a comment

Choose a reason for hiding this comment

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

Babybear Field should be using the MontgomeryBackendPrimeField. The code should set the right constants using that backend. See Stark252PrimeFieldas an example

@mdvillagra
Copy link
Contributor Author

Babybear Field should be using the MontgomeryBackendPrimeField. The code should set the right constants using that backend. See Stark252PrimeFieldas an example

What about the field operations? Do I still leave them there? Or just implement the modulus? The file Stark252PrimeField doesn't have any field operations implemented.

@mdvillagra mdvillagra changed the title Added Babybear field with arithmetic operations Add Babybear field Sep 8, 2023
Copy link
Contributor

@schouhy schouhy left a comment

Choose a reason for hiding this comment

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

Nice! Thanks for the PR!
I left a minor request to move and rename stuff.

unsigned_integer::element::UnsignedInteger,
};

pub type U64 = UnsignedInteger<1>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this to unsigned_integer/element.rs (here). So all unsigned integer type aliases are together.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This a nice idea

};

pub type U64 = UnsignedInteger<1>;
pub type U64PrimeField<T> = MontgomeryBackendPrimeField<T, 1>;
Copy link
Contributor

@schouhy schouhy Sep 8, 2023

Choose a reason for hiding this comment

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

There's already a U64PrimeField (here).
@MauroToscano @Oppen I guess there's no need to have both u64primefields if the one with the Montgomery backend is more performant. Maybe we should create an issue to benchmark them and remove the slow one.
For the time being I would change the name of this type alias to U64MontgomeryBackendPrimeField or something along those lines. To avoid confusion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should review this. But let's do it ourselves, and discuss it outside this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The change of name would be nice though

@mdvillagra
Copy link
Contributor Author

mdvillagra commented Sep 8, 2023

guys, I don't know why another PR was created about this issue. I just did a push and that's it.

@schouhy
Copy link
Contributor

schouhy commented Sep 8, 2023

no worries. Looks like they point to different branches in your fork.

@mdvillagra
Copy link
Contributor Author

I did some clean up on my fork, and now it seems to be fine. I didn't noticed but I was working on another branch. Now I pushed into my main branch.

@schouhy
Copy link
Contributor

schouhy commented Sep 8, 2023

Cool. Looks like the lint check is failing. That's prolly solved by running cargo fmt.

Copy link
Contributor

@schouhy schouhy left a comment

Choose a reason for hiding this comment

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

Awesome. Thanks!

Copy link
Collaborator

@MauroToscano MauroToscano left a comment

Choose a reason for hiding this comment

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

Since there's code for the FFT/NTT, there should be a test to check if it's working properly. It can be submitted as a package of IsFFt with tests in another PR, or fixed here. The IsFieldpart seems right.

Either add the tests, or remove the FFT part

@mdvillagra
Copy link
Contributor Author

Since there's code for the FFT/NTT, there should be a test to check if it's working properly. It can be submitted as a package of IsFFt with tests in another PR, or fixed here. The IsFieldpart seems right.

Either add the tests, or remove the FFT part

I will delete the FFT part right now and add it in another PR.

Comment on lines 1 to 5
{
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to merge this?

Copy link
Contributor

Choose a reason for hiding this comment

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

should probably add it to the .gitignore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably is better to just delete it right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I deleted the file

Comment on lines +33 to +43
impl PartialOrd for FieldElement<Babybear31PrimeField> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.representative().partial_cmp(&other.representative())
}
}

impl Ord for FieldElement<Babybear31PrimeField> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.representative().cmp(&other.representative())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't needed, but it's ok, we can document this later and remove if needed. Doesn't hurt either

@diegokingston diegokingston added this pull request to the merge queue Sep 15, 2023
Merged via the queue into lambdaclass:main with commit b394a6a Sep 15, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants