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

Remove SquareRootField, and move functionality to Field #422

Merged
merged 31 commits into from
Jul 14, 2022

Conversation

mmagician
Copy link
Member

@mmagician mmagician commented May 25, 2022

Description

Rather than implementing optimised algorithms for each case from the start, let's begin with Tonelli-Shanks, which should work in each instance, then benchmark that and continue to implement optimisations for each case one-by-one. While not fastest from the start, it already unifies the SqrtField and Field traits and makes using of sqrt() method cleaner.

In this draft, I propose moving the following to CubicExtField (prev. only on Fp3):

  • TWO_ADICITY
  • TRACE_MINUS_ONE_DIV_TWO
  • QUADRATIC_NONRESIDUE_TO_T

This puts a small overhead on implementers of CubicExtField which are not Fp3 (e.g. Fq6), requiring these to define the above constants.

TODOs:

  • verify the approach (based on feedback)
  • implement the above constants for test-curves
  • implement the above for all curves
  • (?) generate the constants like done here

Credit to @solomonjoseph for the initial work on this.

closes: #387


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

@@ -446,7 +411,7 @@ where
two_inv.div2();

let two_inv = P::BasePrimeField::from(two_inv);
let two_inv = P::BaseField::from(two_inv);
let two_inv = P::BaseField::from_base_prime_field_elems(&[two_inv]).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Why the reason for this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

The calling method, sqrt, was previously on a SquareRootField trait, and in the implementation for quadratic extension we were restricting P: QuadExtConfig to have:

where P::BaseField: From<P::BasePrimeField>

Now that the method is on Field trait, we don't have these bounds, so I need to explicitly construct a base field element from BasePrimeField elements.

Copy link
Member

Choose a reason for hiding this comment

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

Let's add a BaseField: From<BasePrimeField> bound in the config struct; maybe as a different PR?

@Pratyush
Copy link
Member

Pratyush commented Jun 1, 2022

I had discussed this with Solomon in the past, but a good way to unify the preprocessing for all the various square root algorithms would be to make an enum of the form

pub enum SqrtPreprocessing<F: Field> {
    OneMod16(...),
    NineMod16(...),
    ThreeMod4(...),
    // and more...
}

And then, in Field, we add a constant for this enum:

pub trait Field {
    const SQRT_PREPROCESSING: Option<SqrtPreprocessing<Self>>;
}

The Option is there to allow certain implementations to say unimplemented when Sqrts are not used there (but I'm flexible on this point).

@mmagician mmagician marked this pull request as ready for review July 13, 2022 16:20
@mmagician mmagician requested a review from Pratyush July 13, 2022 16:21
@mmagician
Copy link
Member Author

For now I've added only a SqrtPrecomputation::TonelliShanks variant, which should work for all fields. Then we can start implementing specialised algorithms for other variants such as OneMod16 etc.

Likely to add more variants in the future
@mmagician mmagician requested a review from Pratyush July 14, 2022 20:03
@Pratyush Pratyush changed the title Unify sqrt and field Remove SquareRootField, and move functionality to Field Jul 14, 2022
@mmagician mmagician merged commit 6bf24dd into arkworks-rs:master Jul 14, 2022
@mmagician mmagician deleted the unify-sqrt-and-field branch July 14, 2022 22:04
@Pratyush Pratyush linked an issue Sep 2, 2022 that may be closed by this pull request
@mmagician mmagician mentioned this pull request Oct 17, 2022
6 tasks
mmagician added a commit to ayushm2003/algebra that referenced this pull request Oct 17, 2022
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.

Unify SqrtField and Field traits
3 participants