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

Implemented add, mult, inv, sub, div, pow and neg for binary fields and wrote tests for each function #864

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

martinsander00
Copy link

Only file to review is bf.rs inside /algebra/ff/src/fields

Description:
Implemented add, mult, inv, sub, div, pow and neg for binary fields and wrote tests for each function.

Description

closes: #XXXX


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (master)
  • Linked to GitHub issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the GitHub PR explorer

@martinsander00 martinsander00 requested review from a team as code owners November 5, 2024 22:29
@martinsander00 martinsander00 requested review from z-tech, Pratyush and mmagician and removed request for a team November 5, 2024 22:29
ff/src/fields/bf.rs Show resolved Hide resolved
ff/src/fields/bf.rs Show resolved Hide resolved
ff/src/fields/bf.rs Show resolved Hide resolved
ff/src/fields/bf.rs Show resolved Hide resolved
}

// Method for addition in GF(2^k)
pub fn add(&self, other: &Self) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here why not implementing directly the Add trait like this is done here for example? I think this is also applicable to sub, mul, neg... so that we can use directly +, -, * operations

algebra/ec/src/pairing.rs

Lines 192 to 200 in 9ce33e6

impl<'a, P: Pairing> Add<&'a Self> for PairingOutput<P> {
type Output = Self;
#[inline]
fn add(mut self, other: &'a Self) -> Self {
self += other;
self
}
}

Copy link
Author

Choose a reason for hiding this comment

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

Will do this! Thanks for the suggestion

}

// Binary field multiplication with reduction
fn binmul(v1: BigUint, v2: BigUint, length: Option<usize>) -> BigUint {
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 we can integrate this method directly inside the BinaryFieldElement impl no? using:

fn binmul(&self, rhs: Self, length: Option<usize>) -> Self {

Copy link
Author

Choose a reason for hiding this comment

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

SO we could, but why would someone use binmul instead of mul? I think mul is more intuitive right?



#[cfg(test)]
mod tests {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add some randomized tests here as this is done here for example? https://github.com/arkworks-rs/algebra/blob/master/ff/src/biginteger/tests.rs

Copy link
Author

Choose a reason for hiding this comment

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

done! Thanks for the suggestion

Comment on lines 4 to 7
#[derive( Clone, PartialEq, Eq)]
pub struct BinaryFieldElement {
value: BigUint,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here I'm not sure this way of doing things is the more efficient approach because no matter the size of the field concerned, you store in your value as BigUint a vector of BigDigit (u64 or 32 I think) but for field extensions before 32, we don't need as much space.

So I wonder if it is not more optimized to do something custom made like this:

https://gitlab.com/IrreducibleOSS/binius/-/blob/main/crates/field/src/binary_field.rs?ref_type=heads#L770-L777

so that we can have a very efficient implementation for small extensions.

Copy link
Author

Choose a reason for hiding this comment

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

This makes a lot of sense, but it would mean implementing a separate Binary Field implementation for each field size. Should I submit these changes as part of my next PR, or should I first push the other changes we discussed (operators and moving binmul) and then implement the different field sizes in subsequent PRs?

martinsander00 and others added 6 commits December 12, 2024 19:55
create a clickable link to the BinaryFieldElement type definition

Co-authored-by: Thomas Coratger <60488569+tcoratger@users.noreply.github.com>
Co-authored-by: Thomas Coratger <60488569+tcoratger@users.noreply.github.com>
Co-authored-by: Thomas Coratger <60488569+tcoratger@users.noreply.github.com>
Co-authored-by: Thomas Coratger <60488569+tcoratger@users.noreply.github.com>
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.

2 participants