Implement Karatsuba multiplication for Uint and BoxedUint#649
Merged
tarcieri merged 5 commits intoRustCrypto:masterfrom Aug 16, 2024
Merged
Implement Karatsuba multiplication for Uint and BoxedUint#649tarcieri merged 5 commits intoRustCrypto:masterfrom
tarcieri merged 5 commits intoRustCrypto:masterfrom
Conversation
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Contributor
Author
|
@dignifiedquire I probably could have copied some of the more extensive documentation from your PR, maybe there's something there you'd like to add. |
andrewwhitehead
commented
Aug 14, 2024
tarcieri
reviewed
Aug 16, 2024
| @@ -0,0 +1,414 @@ | |||
| //! Karatsuba multiplication | |||
Member
There was a problem hiding this comment.
Small nit but I'd probably suggest putting this under mul/karatsuba.rs
Contributor
Author
There was a problem hiding this comment.
src/uint/mul or src/mul?
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
tarcieri
reviewed
Aug 16, 2024
src/uint/boxed/mul.rs
Outdated
| let size = self.nlimbs() + rhs.nlimbs(); | ||
| let overlap = self.nlimbs().min(rhs.nlimbs()); | ||
|
|
||
| if self.nlimbs().min(rhs.nlimbs()) >= 32 { |
Member
There was a problem hiding this comment.
Where does 32 come from? Perhaps it could use a constant?
Contributor
Author
There was a problem hiding this comment.
It's from benchmarking on this machine, only applying the reduction where it's likely to be faster.
tarcieri
reviewed
Aug 16, 2024
src/uint/boxed/mul.rs
Outdated
| let mut limbs = vec![Limb::ZERO; self.nlimbs() * 2]; | ||
| let size = self.nlimbs() * 2; | ||
|
|
||
| if self.nlimbs() >= 64 { |
Member
There was a problem hiding this comment.
Likewise here, this could probably also use a constant (or short expression like KARATSUBA_MIN_LIMBS * 2 or thereabouts)
ed4e962 to
ba9763c
Compare
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
ba9763c to
105edc7
Compare
tarcieri
approved these changes
Aug 16, 2024
This was referenced Sep 19, 2024
Closed
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Another PR to review, sorry...
This PR adds Karatsuba multiplication and squaring algorithms for both Uint and BoxedUint. The BoxedUint implementation is fairly flexible, and multiplication is supported for mixed operand sizes. The constants used may need tweaking, especially for 32 bit platforms to ensure the best implementation is chosen. On my 64-bit platform the numbers are currently:
(There is not a linear relationship in the improvements, although the relative timings did become more linear with the integer size)
The Uint implementation is more limited due to the strict typing, and is currently only implemented for
split_mulwith 16, 32, 64, or 128 limb arguments (U1024, U2048, U4096, U8192 on 64-bit) as well assquare_widewith 64 or 128 limb arguments.