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

Update QuadExtField::sqrt for better performance #352

Merged
merged 1 commit into from
Nov 25, 2021

Conversation

vlopes11
Copy link
Member

@vlopes11 vlopes11 commented Nov 24, 2021

Description

Currently, the QuadExtField uses an expensive runtime inverse of two
calculation defined as P::BaseField::one().double().inverse().

With the proposed change, we compute the BigInt (p+1)/2 that is ~15%
cheaper than the previous method.

Alternatively, we could require a compile-time constant provided by the
user that represents 1/2. However, having a constant requirement to
satisfy a single use-case isn't ideal.

closes: #210


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.

  • 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

  • Wrote unit tests (tests are already covering this section: bls12_381::tests::test_fq2)

@vlopes11 vlopes11 force-pushed the vlopes11/quad-ext-sqrt-improve-2-inv branch 2 times, most recently from 31b2a96 to 5f16308 Compare November 24, 2021 03:55
Copy link
Member

@mmagician mmagician left a comment

Choose a reason for hiding this comment

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

Cool! I believe you can skip <P as QuadExtsParameters> since it's already dictated by the trait bounds.

Copy link
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

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

Untested ACK.

@vlopes11
Copy link
Member Author

Better rebase on top of #354 before merging so the performance difference will be clear and documented

Currently, the QuadExtField uses an expensive runtime inverse of two
calculation defined as `P::BaseField::one().double().inverse()`.

With the proposed change, we compute the BigInt `(p+1)/2` that is ~15%
cheaper than the previous method, by avoiding `inverse`.

Alternatively, we could require a compile-time constant provided by the
user that represents `1/2`. However, having a constant requirement to
satisfy a single use-case isn't ideal.

`PrimeField::modulus` was introduced to facilitate the available
constants usage.

Closes #210
@vlopes11 vlopes11 force-pushed the vlopes11/quad-ext-sqrt-improve-2-inv branch from d465245 to e55606d Compare November 25, 2021 21:30
@vlopes11 vlopes11 merged commit 2f2f231 into master Nov 25, 2021
@vlopes11 vlopes11 deleted the vlopes11/quad-ext-sqrt-improve-2-inv branch November 25, 2021 21:52
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.

Precompute 2.inverse() in Fq2::sqrt
4 participants