COS 126 RSA Cryptosystem Assignment Hints
Extended precision arithmetic data type
|
First, make sure you understand the data structure XP that
will be used to store extended precision integers.
#define N 40
#define NN (2*N + 1)
typedef struct {
char digit[NN];
} XP;
Write a function
XP XPinitDecimal(char s[]);
so that you can initialize an extended precision integer
with the following syntax.
XP a;
a = XPinitDecimal("123456789");
Basically you just want to copy the string s[] into the
digit[] field of a variable of type XP. But there
are a number of design issues that will significantly
affect the rest of your code.
Write a function
void XPshowDecimal(XP a);
that prints out the decimal representation of a by looping
through the NN digits of a.digit[] and printing them out in the
appropriate order. Test out your function with the following code:
XP a;
a = XPinitDecimal("123456789");
XPshowDecimal(a);
It should print out:
123456789
For debugging purposes, it would be a good idea to leave in
the leading 0's. If N = 10, and NN = 21, print out
000000000000123456789
Write a function
XP XPadd(XP a, XP b);
that computes a + b and return the sum. Add two integers using
pencil and paper, and observe that at each step you compute a new
output digit and a carry digit. You can test your function with
the following code:
XP a, b;
a = XPinitDecimal("123456789");
b = XPinitDecimal("54545454");
c = XPadd(a, b);
XPshowDecimal(c);
If you feel like you're on a roll now, you may wish to write code
for subtraction and multiplication, as they're quite similar in
style.
Devise an algorithm for determining whether one integer is greater
(less, equal) to another.
Instead of writing 3 separate functions (that have almost the exact
same code), consider writing a helper function
int compare(XP a, XP b);
that returns 1 if a > b; -1 if a < b; and 0 if they are equal.
If you do this, you can quickly crank out code for
XPless() as follows:
int XPless(XP a, XP b) { return compare(a, b) == - 1; }
Hint: this requires only one line of code.
You do not need to handle negative integers. The subtract function
need only subtract correctly when the first integer is greater than or
equal to the second. It would be a good debugging idea to check
this condition (using your comparison functions) at the beginning
of the function.
Your multiply function need only work if the two inputs are N digits
or less. This will ensure that the result will not overflow.
It would be a good debugging idea to check this condition at the
beginning of the function.
This is the hardest of the arithmetic functions.
Don't be surprized if you code runs slow. After you perform
the complexity analysis, you'll understand why.
This is a valuable lesson in analysis of algorithms.
Professional implementations of RSA need to use more sophisticated
algorithms (with significantly lower asymptotic running times) for
multiplication and division so that they can encrypt/decrypt in
real-time, even for keys with thousands of digits.
Given a small piece of text, you're job is to convert it into
a decimal integer.
Now you want to "undo" the transformation above.
If you use the direct method above, then if the function
XPshowASCII()
is called with the integer 104101108108111033, it should
print "hello!".
Now, use your extended precision data type to write a client
program to encrypt and decrypt messages using RSA. Be sure that
you have debugged and tested all of the interface functions before
proceding.
Kevin Wayne