-
Notifications
You must be signed in to change notification settings - Fork 189
perf(math): Add #[inline] attributes to hot field arithmetic paths #1128
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,7 @@ impl IsField for Goldilocks64Field { | |
| } | ||
|
|
||
| /// Returns the multiplicative inverse of `a` using optimized addition chain. | ||
| #[inline] | ||
| fn inv(a: &u64) -> Result<u64, FieldError> { | ||
| let canonical = canonicalize(*a); | ||
| if canonical == 0 { | ||
|
|
@@ -141,6 +142,7 @@ impl IsField for Goldilocks64Field { | |
| } | ||
|
|
||
| /// Returns the division of `a` and `b`. | ||
| #[inline] | ||
| fn div(a: &u64, b: &u64) -> Result<u64, FieldError> { | ||
| let b_inv = <Self as IsField>::inv(b)?; | ||
| Ok(<Self as IsField>::mul(a, &b_inv)) | ||
|
|
@@ -415,6 +417,7 @@ impl IsField for Degree2GoldilocksExtensionField { | |
|
|
||
| /// Returns the multiplicative inverse of `a`: | ||
| /// (a0 + a1*w)^-1 = (a0 - a1*w) / (a0^2 - 7*a1^2) | ||
| #[inline] | ||
| fn inv(a: &Self::BaseType) -> Result<Self::BaseType, FieldError> { | ||
| let a0_sq = a[0].square(); | ||
| let a1_sq = a[1].square(); | ||
|
|
@@ -424,19 +427,23 @@ impl IsField for Degree2GoldilocksExtensionField { | |
| Ok([a[0] * norm_inv, -a[1] * norm_inv]) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn div(a: &Self::BaseType, b: &Self::BaseType) -> Result<Self::BaseType, FieldError> { | ||
| let b_inv = Self::inv(b)?; | ||
| Ok(<Self as IsField>::mul(a, &b_inv)) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn eq(a: &Self::BaseType, b: &Self::BaseType) -> bool { | ||
| a[0] == b[0] && a[1] == b[1] | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn zero() -> Self::BaseType { | ||
| [FpE::zero(), FpE::zero()] | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn one() -> Self::BaseType { | ||
| [FpE::one(), FpE::zero()] | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness:
Security:
Performance:
Bugs & Errors:
Code Simplicity:
Recommendation:
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correctness
mul_power_twoimplementation appears to be incorrectly handling the shifting of bits, leading to possible incorrect multiplication results when the bits carried exceed the maximum allowed.pow_2should ensure that squaring does not overflow, as continued squaring can exceed the 31-bit expectation.Security
invshould ensure no secret-dependent branching for side-channel prevention; similar care is needed with functions likemul,sub, andnegfor consistent timing.as_representative,neg, andinvmethods may benefit from checking their handling of inputs like zero and the prime field order.Performance
#[inline(always)]attributes may lead to code bloat; use them judiciously, especially for complex logic.mulconversion fromu32tou64and back to ensure minimal overhead and evaluate if it's performant enough.Bugs & Errors
subfunction uses a subtraction which could underflow ifa < b.Code Simplicity
mul_power_twoimplementation lacks clarity and would benefit from comments or a simplified approach.weak_reduceimply a potential for consolidation or more straightforward flow; repeated usage may cover duplicated logic.Additional Notes
Due to these concerns in correctness, security, performance, and potential bugs, this code is not ready to merge. Further refinement and testing are required to ensure all conditions are safely managed and optimized.