Uh oh!
There was an error while loading. Please reload this page.
NTT multiplication and Newton-Raphson division - #407
Merged
Conversation
tompngforce-pushed
the
ntt_mult_div
branch
9 times, most recently
from
August 27, 2025 16:57
fabc2d2 to
236c1ffComparetompngforce-pushed
the
ntt_mult_div
branch
3 times, most recently
from
September 3, 2025 13:46
64e3d7c to
6b2506bComparetompng
marked this pull request as ready for review
September 3, 2025 14:24
tompngforce-pushed
the
ntt_mult_div
branch
2 times, most recently
from
September 9, 2025 12:16
46fd632 to
b2e5dacComparetompngforce-pushed
the
ntt_mult_div
branch
2 times, most recently
from
September 13, 2025 13:33
1a0a3a0 to
0f02ddcCompare
This was referenced Sep 18, 2025
tompngforce-pushed
the
ntt_mult_div
branch
2 times, most recently
from
October 7, 2025 13:08
7d52b9d to
2513f01Comparetompngforce-pushed
the
ntt_mult_div
branch
2 times, most recently
from
October 28, 2025 16:20
d94dcf1 to
93b2248Compare
This was referenced Dec 1, 2025
Performs ntt with three primes (29<<27|1, 26<<27|1, 24<<27|1)
Improve performance of huge divisions
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
mensfeld
commented
Jun 5, 2026
Respect |
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 freeto 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.
Multiplication gets maximum 800,000 times faster.
Raises
Multiply size too large (ArgumentError)if size is larger than the limitation:x * yrequires[x.n_significant_digits, y.n_significant_digits].min <= 603979776Basic policy of this pull request
NTT(Numeric Theory Translation) multiplication
Calculates multiplication/convolution using NTT with three primes.
Consider calculating convolution of two arrays. Each array is of size
Nwitharray[i] in 0..999999999.Maximum value of
convolution[i]is999999999**2 * N. This value is larger than 64bit and smaller than 96bit, so we need three 32-bit primes:29<<27|1,26<<27|1,24<<27|1.These are three largest 32-bit primes that satisfies
P > 999999999, andP-1need to be a multiple of large powers of two.Constraints from this primes, maximum
Nis1<<27.Combination of primes/sizes
Multiplication of various size bigdecimal
Considering
xx_xx_xx * yyCalculate by
convolution(ntt(xx_xx_xx), ntt(00_00_yy))is possible, but repeatingconvolution(ntt(xx), ntt(yy))is faster.If n_significant_digits is both larger than
1<<<26 == 603979776, multiplication fails with Error(too large).Newton-Raphson division
X / Ycan be calculated byX * Yinvand
Yinvcan be calculated only by add/sub/mult using Newton's method.Division of various size bigdecimal
Considering
1111_1111_1111_1111.div(7777_7777_7777)Required precision is 4. Calculating inverse of 7777_7777_7777 in 4 digit is enough.
1111_1111_1111_1111 * 1.285e-12 == 1428Considering
1111_1111_1111_1111.div(7777)We can calculate this by repeating
xxxx_xxxx.divmod(7777)3 times.Calculating inverse of 7777 in 4 digit is enough.
Generic case: Split X into several blocks
xxx_xxxxx_xxxxx_xxxxx / yyyyCan be calculated by repeating
xxxx_xxxxx.divmod(yyyy)3 times.xxx_xxxx_xxxx / yyyyyCan be calculated by repeating
xxxxx_xxxx.divmod(yyyyy)2 times.xxxxx_xxxxxxx / yyyyyCan be calculated by repeating
xxxxxx_xxxxxxx.divmod(yyyyy)1 time.Tests
All bigdecimal test passes with
NTT_MULTIPLICATION_THRESHOLD==1andNEWTON_RAPHSON_DIVISION_THRESHOLD==1(All mult/div uses NTT and Newton-Raphson)These large multiplication test passes. (Too slow to add to CI)