As equations, encryption takes the form:
setRnd(seed); c = rnd() xor m;
transmit c openly, and seed securely;
Decryption takes the form:
receive seed securely and c openly;
setRnd(seed); m = rnd() xor c;
int exp(int X, int Y) { int x = X, y = Y, z = 1; while (y > 0) { while (y%2 == 0) { x = x*x; y = y/2; } z = z*x; y = y - 1; } return z; }Suppose one knows that the following equation is true, for integer variables x, y, z, X, and Y, and = is the mathematical equals:
z * xy = XYand suppose the following 2 assignment statements are executed, where = is assignment:
z = z * x; y = y - 1;a. Prove that the equation remains true (remains invariant) after the two assignments are executed.
b. At the point in the above algorithm just before the two assignments
statements, it is also true
that y%2 is not zero, that is,
y is odd. Is this needed for the proof of
invariance?
The proof of the invariance after the two statements inside
the inner while loop requires that one know the value of y is even.
However, in this case, even though y is odd, the proof of invariance
does not rely on y being odd.
In light of the above two parts, consider the following C, C++, or Java function?
int exp(int X, int Y) { int x = X, y = Y, z = 1; while (y > 0) { z = z*x; y = y - 1; } return z; }c. Do you think this second function will correctly calculate XY ?
d. Do you think this version of exp
is of any use?
This latter version takes Y many steps, whereas the earlier
version takes at most log2Y many steps (which are
more complicated), a very much
faster algorithm for large Y. This simpler algorithm
is useless for cryptography and other similar applications
because it is far too slow, but the algorithm is useful for small Y and for
educational purposes (as here). Thus this question doesn't have a
simple yes or no answer.