Large number modulo operation Barrett reduction

Source: Internet
Author: User
Tags modulus

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.