Modulo repetition square algorithm

Source: Internet
Author: User

In the RSA algorithm , the process of" evaluate the n m of X "is often used, generally, the O (log (N) modulo repeats the square algorithm to improve efficiency.

in fact, this is the content in information security mathematical basics, which was specially written in Code is used for the test in the blog space -......

Likewise, the recursive algorithms of O (log (N) are easy to understand:

 
/* C */INT square (int x) {return x * X;}/* Can I use a macro here? */INT fast_mod (INT X, int N, int m) {If (n = 0) return 1; else if (N % 2 = 0) return square (fast_mod (x, n/2, m) % m; else return x * fast_mod (x, n-1, m) % m;} # pythonfast_mod = Lambda X, n, m: 1 if n = 0 else fast_mod (X, n/2, m) ** 2% M if n % 2 = 0 else x * fast_mod (x, n-1, m) % m; Scheme (define (even? N) (= (remainder N 2) 0) (define (mod-fast x n m) (define (square X) (* X )) (cond (= N 0) 1) (even? N) (remainder (square (mod-fast X (/N 2) M) (else (remainder (* X (mod-fast X (-N 1) m) (mod-fast 79 24 33 );

However, an iterative version is required. I remember that I could write N as binary (for example, n = 13,110 1) and then push it one by one.

I tried to push it from the high position to the low position, which is quite simple. Remember that B [I] is the value of the I bit, calculate a [0] from high to low using a [I] = A [I + 1] ^ 2 * x ^ B [I], and the result is displayed. But the problem is that, in order to calculate from high to low, another recursion does not seem to meet the requirements.

In turn, we had to push from low to high. This process is actually quite simple, for example:

When n = 13, that is, the binary 1101 = 1*2 ^ 3 + 1*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0, the final result is

Ans = x ^ n % m
= X ^ (1*2 ^ 3 + 1*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0) % m
= [X ^ (1*2 ^ 3)] * [x ^ (1*2 ^ 2)] * [(x ^ (0*2 ^ 1)] * [x ^ (1*2 ^ 0)] % m

In other words, from low to high, x ^ (bit [I] * 2 ^ I) % m is multiplied to the result in the I-th bit. Here, we can change it slightly: When bit [I] = 1, we can multiply x ^ (2 ^ I) % m. Therefore, an auxiliary variable B can be used to save x ^ (2 ^ I) % m. During each iteration, B =
B ^ 2% M.

So the implementation is easy:

/* C */INT fast_mod_iter (int x, int N, int m) {int A = 1, B = X; // when I = 0, B = x ^ (2 ^ 0) = x while (n) {If (N % 2 = 1) A = a * B % m; B = B * B % m; N/= 2;} return a ;}# pythondef fast_mod (x, n, m): a = 1 B = x while true: if n = 0: return a if n % 2 = 1: A = a * B % m B = B * B % m n/= 2; scheme (define (even? N) (= (remainder N 2) 0) (define (mod-fast-iter x n m) (define (iter a B N) (cond (= N 0) a) (even? N) (iter a (remainder (* B) M) (/N 2) (else (ITER (remainder (* a B) M) (* B) (/(-N 1) 2) (ITER 1 x n) (mod-fast-iter 79 24 33); 

transferred from Daniel http://www.felix021.com/blog/read.php? 2112

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.