Exercise 2.4.21* (Modular product)

Let X be a large positive integer. Suppose that m X 2 , and that 0 a < m , 0 b < m . Explain why the number c determined by the following algorithm satisfies 0 c < m , and c ab ( mod m ) . Verify that in executing the algorithm, all numbers encountered lie in the interval [ 0 , X ) .

1.
Set k = b , c = 0 , g = [ X m ] .
2.
As long as a > 0 , perform the following operations:
(a)
Set r = a g [ a g ] .
(b)
Choose s so that s kr ( mod m ) and 0 s < m .
(c)
Replace c by c + s .
(d)
If c m , replace c by c m .
(e)
Replace k by gk m [ gk m ] .
(f)
Replace a by ( a r ) g .

Answers

Notations:

  • n = x ( x ) if n and n x < n + 1 .
  • r = a % b if a = bq + r , 0 r < b for some q ( r is the remainder of the Euclidean division of a by b ). Then q = a b .
  • a b : Replace a by b (in the algorithm).

Proof. We reformulate the algorithm:

1.
Set k b , c 0 , g X m .
2.
While a > 0 ,
(a)
r = a % g .
(b)
s = ( kr ) % m .
(c)
c c + s .
(d)
If c m , c c m .
(e)
k ( gk ) % m .
(f)
a a g .

With python:

     def modular_product(a, b, m, X):
         k, c, g = b, 0, X // m
         while a>0:
             r = a % g
             s = (k * r) % m
             c = c + s
             if c >= m: c = c - m
             k = (g * k) % m
             a = a // g
         return c

Now we explain the algorithm. Note that g = X m 2 does not change during the execution of the algorithm. We interpret g as the base of the system of numeration, so that

a = i = 0 M 1 r i g i = r 0 + r 1 g + r 2 g 2 + + r M 1 g M 1 ,

where M is the numbers of digits in base g , and 0 r i < g .

Then

ab = ( j = 0 M 1 r j g j ) b = i = 0 M 1 r j ( g j b ) i = 0 M 1 r j k j ( mod m ) ,

where k i g i b ( mod m ) , 0 k i < m (that is k i = ( g i b ) % m ).

Define s i = ( k i r i ) % m , and a i = a g i = j = i M 1 r j g j for i = 0 , 1 , , M 1 .

Now we verify that r i , a i , k i are the actual values the parameters r , a , k at the i -th passing in the loop (the first passing has index 0 ), if we test these values after the step (d).

The rightmost digits of a is r 0 = a % g , the remainder of a by g . To obtain the following digits, we replace a by a g , a g 2 , . This gives the well known algorithm to decompose a positive integer in the base g :

while  a > 0 , r a % g a a g

This corresponds to the steps (a) and (f) in the loop, so r i and a i are the actual values of r and a in the loop. Moreover, step (e) gives k i = ( g i b ) % m .

At each passing of the loop, c i c i 1 + s i ( mod m ) , so that c i j = 0 i r j k j ( mod m ) , and when a M = 0 , c = c M j = 0 M 1 r j k j ab ( mod m ) .

We prove by induction that a i , k i , c i are in the interval [ 0 , m [ .

0 a 0 = a < m , 0 = k 0 = b < m , c 0 = 0 , so the property is true for i = 0 .

Assume that 0 a i 1 < m , 0 k i 1 < m , 0 c i 1 < m . Then s i = ( k i r i ) % m < m , so 0 c i 1 + s i < 2 m . Therefore, after the instruction (d), 0 c i < m . Moreover 0 k i = ( g i b ) % m < m , and a i = a i 1 g < a I 1 < m . The induction is done.

Finally, 0 r i < g , and 0 k i < m , therefore 0 k i r i < gm X , since g = X m satisfies g X m . Similarly, 0 g k i < gm X , so all numbers encountered lie in the interval [ 0 , X [ .

Since at the end 0 c < m , and c ab ( mod m ) , the result c is the waited result. □

To give an example, take m = 1000 , X = 10000 so that the base is g = 10 .

If we compute ab modulo 1000 for a = 237 , b = 682 , with an instruction ’print’ after the step (d), we obtain the following intermediate results

r a k s c 7 237 682 774 774 3 23 820 460 234 2 2 200 400 634

The result is c = 634 , which is right, since ab = 161634 634 ( mod 1000 ) .

User profile picture
2024-08-26 09:52
Comments