20201002, 20:22  #1 
Mar 2016
356_{10} Posts 
calculation of modulo Mp
A peaceful and pleasant day for you,
I do not understand how the calculation modulo a Mersenne prime is made: https://en.wikipedia.org/wiki/Mersenne_prime: "Arithmetic modulo a Mersenne number is particularly efficient on a binary computer" Perhaps this is an interesting question also for others. Greetings from the modulo operator Bernhard 
20201002, 21:43  #2  
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2
2·4,787 Posts 
Read https://www.mersenne.org/various/math.php first ?
Quote:
Fact 1. When doing mod operations, (a * b) (mod Mp) = a (mod Mp) * b (mod Mp). Consequence: you never need to keep "the real value"; always keep only the value (mod Mp). This means that it will never be more than p bits long. Fact 2. if we multiply one value by another and both are less than p bits long, you will get a result that is < 2p bits long. Fact 3 (assuming mult or square is already done; there was no question about that part). Mod operation becomes this: cut the bits above pth bit. Slide them down, align with lower part. Add. Only if 1 bit carry sticks out above p bits, cut it again and add 1 to lower part. Done! The DWT actually doesn't need this operation literally. But even if it did, this is how cheap computationally it would have been. Essentially  there is no division. Division by 2^{p}1 is luckily this elegant and easy. 

20201003, 07:18  #3 
Romulan Interpreter
"name field"
Jun 2011
Thailand
9,787 Posts 
Probably easier for him, you have a number a which you want to reduce (mod m), so you are looking for a number b such as a=b (mod m). From the definition of the modulus, if a=b (mod m), this means that there is a number k, such as a=k*m+b. Now, if m=2^p1, then you have a=k*(2^p1)+b, or other way written, a=k*(2^p)k+b. When you represent this is binary, on 2p bits, the first mostsignificant p bits (MSB) of the representation contain the k value (because multiplying with 2^p just moves k, p bits to the left), and the last p LSB bits (least significant bits) contain the value bk. If you add the two halves together, you get b. That's all**.
 (**Edit: except, sometimes you may get it all 1, by addition, and then the result is zero, or you may need to repeat the procedure once, but those "trifles" won't be discussed here.) Edit 2, as I have some more time, and there is no reply yet, say you want to reduce 107 (mod 31), now 107 is 3*32+11, i.e. 3*2^5+11, therefore when you represent it in binary on 10 bits (5+5) you have 00011 01011. The MSB contains 3, and the LSB contain 11 (decimal, sorry for the confusion, I will "go advanced" now to change the font for the binary numbers here, and color all of them nicely). This is 3*32+11=3*(31+1)+11=3*31+3+11. The 3*31 part won't matter for the modulus. So, all you have to do, is to add the two halves to get 11+3=14. Which is the right result of 107 (mod 31). Last fiddled with by LaurV on 20201005 at 07:56 
20201003, 08:49  #4 
Sep 2002
Database er0rr
7442_{8} Posts 
2^p  1 == 0 mod Mp
2^p == 1 mod Mp k*2^p == k mod Mp That is k shifted to the left pbits is equivalent to k. If a and b each have no more than p bits then a*b is at most 2*p1 bits, you can just add the number composed of the top most bits (above p1 counting from 0) to the number composed of the bottom p bits, to get the result mod Mp. Last fiddled with by paulunderwood on 20201003 at 09:35 
20201003, 17:03  #5 
Mar 2016
2^{2}×89 Posts 
Most people know the 9rule in the decimalsystem
which based on the fact that 10 = 1 mod 9 For Mersenne numbers it is the same reflection that 2^p = 1 mod 2^p 1 Thanks for all replies, now we know one advantage from the Mersenne numbers. Last fiddled with by bhelmes on 20201003 at 17:04 
20210207, 18:15  #6 
Mar 2016
2^{2}×89 Posts 
Does someone has a function in gmp for calculating mpz_mod_mp ?
Would be nice for me and perhaps also for others. Greetings Bernhard 
20210207, 19:45  #7  
Sep 2002
Database er0rr
111100100010_{2} Posts 
Quote:
Code:
void mpz_mod_mp(mpz_t r, mpz_t a, mp_bitcnt_t p){ mpz_t x,y; mpz_init(x); mpz_init(y); mpz_fdiv_q_2exp(x, a, p); mpz_fdiv_r_2exp(y, a, p); mpz_add(r, x, y); mpz_clear(x); mpz_clear(y); } Last fiddled with by paulunderwood on 20210207 at 20:06 

20210207, 21:13  #8 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2
2×4,787 Posts 
You also need to loop
do { your routine } while (mpz_sizeinbase(r,2)>p); If mpz_fdiv_qr_2exp() exists, could be even neater, but optimizing complier will take care of optimizing the twofunction call, most surely. So, you can do: Code:
void mpz_mod_mp(mpz_t r, mpz_t a, mp_bitcnt_t p) { // assumed that r is initialized and a is expendable do { mpz_fdiv_r_2exp(r, a, p); mpz_fdiv_q_2exp(a, a, p); mpz_add(r, r, a); } while (mpz_sizeinbase(r,2)>p); } Example: a = 2^{p+1}1. After pass #1, we will get exactly r = 2^{p}, pass #2 will turn it into r = 1. Return. 
20210208, 15:27  #9 
Sep 2002
Database er0rr
2·13·149 Posts 
Nice one Serge.
I would also write a function (called mpz_mod_mp_partial) that works at the word boundary instead of being bitaccurate, calling a full reduction at the end of a loop. If p+r == 0 mod 32 and what has to be reduced is k*2^(p+r)+c = k*2^r+c where c < 2^(p+r). But on reflection this extra function does not really help for mod reduction for Mp in particular. See this thread for more details of what I mean. Last fiddled with by paulunderwood on 20210208 at 16:19 
20210209, 01:16  #10 
Mar 2016
2^{2}×89 Posts 
Thanks for the function.
Last fiddled with by bhelmes on 20210209 at 01:47 
Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Factorial modulo a prime  axn  Computer Science & Computational Number Theory  66  20110901 21:55 
modulo operation for polynomials?  smslca  Math  3  20110418 17:18 
Order of 3 modulo a Mersenne prime  T.Rex  Math  7  20090313 10:46 
N! modulo for large N  Cyclamen Persicum  Math  2  20031210 10:52 
The modulo operation, how is it computed?  eepiccolo  Math  7  20030108 03:07 