17.1.2: The Invariant Relation Theorem
( \newcommand{\kernel}{\mathrm{null}\,}\)
Exponentiation Algorithms
Consider the following algorithm implemented in Sage to compute ammodn, given an arbitrary integer a, non-negative exponent m, and a modulus n, n≥0. The default sample evaluation computes 25mod7=32mod7=4, but you can edit the final line for other inputs.
1 | def slow_exp(a,m,n): |
2 | b = 1 |
3 | k = m |
4 | while k> 0 : |
5 | b = (b * a) % n # % is integer remainder (mod) operation |
6 | k - = 1 |
7 | return b |
8 | slow_exp( 2 , 5 , 7 ) |
It should be fairly clear that this algorithm will successfully compute am(modn) since it mimics the basic definition of exponentiation. However, this algorithm is highly inefficient. The algorithm that is most commonly used for the task of exponentiation is the following one, also implemented in Sage.
01 | def fast_exp(a,m,n): |
02 | t = a |
03 | b = 1 |
04 | k = m |
05 | while k> 0 : |
06 | if k % 2 = = 1 : b = (b * t) % n |
07 | t = (t^ 2 ) % n |
08 | k = k / / 2 # // is the integer quotient operation |
09 | return b |
10 | fast_exp( 2 , 5 , 7 ) |
The only difficulty with the "fast algorithm" is that it might not be so obvious that it always works. When implemented, it can be verified by example, but an even more rigorous verification can be done using the Invariant Relation Theorem. Before stating the theorem, we define some terminology.
17.1.2.2: Proving the Correctness of the Fast Algorithm
Definition 17.1.2.1: Pre and Post Values
Given a variable x, the pre value of x, denoted ˊx, is the value before an iteration of a loop. The post value, denoted ˊx, is the value after the iteration.
Example 17.1.2.1: Pre and Post Values in the Fast Exponentiation Algorithm
In the fast exponentiation algorithm, the relationships between the pre and post values of the three variables are as follows.
ˊb≡ˊbˊtˊkmod2(modn)
ˊt≡ˊt2(modn)
ˊk=ˊk//2
Definition 17.1.2.2: Invariant Relation
Given an algorithm's inputs and a set of variables that are used in the algorithm, an invariant relation is a set of one or more equations that are true prior to entering a loop and remain true in every iteration of the loop.
Example 17.1.2.2: Invariant Relation for Fast Exponentiation
We claim that the invariant relation in the fast algorithm is btk=am(modn). We will prove that this is indeed true below.
Theorem 17.1.2.1: The Invariant Relation Theorem
Given a loop within an algorithm, if R is a relation with the properties
- R is true before entering the loop
- the truth of R is maintained in any iteration of the loop
- the condition for exiting the loop will always be reached in a finite number of iterations.
then R will be true upon exiting the loop.
- Proof
-
The condition that the loop ends in a finite number of iterations lets us apply mathematical induction with the induction variable being the number of iterations. We leave the details to the reader.
We can verify the correctness of the fast exponentiation algorithm using the Invariant Relation Theorem. First we note that prior to entering the loop, btk=1am=am(modn). Assuming the relation is true at the start of any iteration, that is ˊbˊtˊk=am(modn), then
ˊbˊtˊk≡(ˊbˊtˊkmod2)(ˊt2)ˊk//2(modn)≡ˊbˊt2(ˊk//2)+ˊkmod2(modn)≡ˊbˊtˊk(modn)≡am(modn)
Finally, the value of k will decrease to zero in a finite number of steps because the number of binary digits of k decreases by one with each iteration. At the end of the loop,
b=bt0=btk≡am(modn)
which verifies the correctness of the algorithm.
Exercises
Exercise 17.1.2.1
How are the pre and post values in the slow exponentiation algorithm related? What is the invariant relation between the variables in the slow algorithm?
Exercise 17.1.2.2
Verify the correctness of the following algorithm to compute the greatest common divisor of two integers that are not both zero.
1 | def gcd(a,b): |
2 | r0 = a |
3 | r1 = b |
4 | while r1 ! = 0 : |
5 | t = r0 % r1 |
6 | r0 = r1 |
7 | r1 = t |
8 | return r0 |
9 | gcd( 1001 , 154 ) #test |
- Hint
-
The invariant of this algorithm is gcd(r0,r1)=gcd(a,b).
Exercise 17.1.2.3
Verify the correctness of the Binary Conversion Algorithm, Algorithm 1.4.1, in Chapter 1.