Proof and use of Barrett reduction algorithm.
The author has just finished the course design work, free to write an article.
Exponential operations in large numbers require a number to be modeled, because the maximum possible binary is 2048 bits of the 2048-bit, so the multiplication side must be taken to modulo.
Multiplication uses a fast power, if the base number is X, the number of digits is E, the base is large enough, the complexity depends on the modulus, the modulus is M bit, the complexity is O (m*m*e). In the program, the large number of storage is 2 of the 32-time binary, where the m*m to be divided by (32*32).
Modulo operations, if you call the large number of modules directly, because the division is very slow, so the efficiency is very low. The Barrett reduction algorithm can be used to convert the division into multiplication and bitwise operations, subtraction.
Because the modulus is arbitrary, the Montgomery algorithm is not necessary.
The job only requires the completion of a non-negative large number operation, the subsequent proof that the default large number is non-negative.
The Barrett reduction algorithm is described below
Find x mod m
The number of bits in M is K
Conditions of Use: X-digits are not greater than 2*k. Repeated modulo of the same number.
How to use (non-negative here):
Calculating Constants Mu=b^2k/m
When taking the mold:
Q1 = x/b^ (k-1)
Q2 = Q1 * MU
Q3 = q2/b^ (k+1)
R1 = x% b^ (k+1)
r2 = (Q3 * m)% b^ (k+1)
r = R1-r2
if (R > m) r-= m
Return r
A lot of information mu=b^2k/m written in mu=b^k/m, the author was a pit, very angry, decided to write such an article holiday. In addition, Baidu basically do not see any relevant proof.
Prove:
[] Indicates rounding, B is a large number of binary
Only one division preprocessing is required.
After the division and the modulo are on the binary B several times, so the bit operation can be.
Multiplication, because the final result is to take the last k+1 bit, so in the time, you can directly put the k+1 bit in front of the discard, reduce the number of cycles. The author has also written a multiplication function that limits the number of bits, which improves the efficiency.
Proof part screenshot from Word Experiment report.