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 {
unsigned char digit[NN];
} XP;
If you follow our suggestions, then pay careful attention to the input
requirements for each function described in XP.h.
Some assume that the inputs handle integers of up to N digits,
while others require up to 2N or 2N+1. The most common mistake is ignoring
these details.
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 (and life for the week).
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, leave in the leading 0s (and leave them
in until you have finished implementing and debugging all of the other
interface functions).
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);
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; }
Your functions should correctly handle inputs with up to 2N+1 digits.
Hint: this requires only one line of code.
This should be fairly similar to addition.
You can stop here for the warmup. But if you feel like you're on a roll,
then keep going with multiplication and division.
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.
Bonus hint: you might find an extremely relevant question in the
Fall 2000 final exam.
This is the hardest of the arithmetic functions.
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
proceeding.
Kevin Wayne