/* Returns the greatest common divisor of the two arguments * "a" and "b", using the Euclidean algorithm. */ BigInt RSA::GCD(const BigInt &a, const BigInt &b) { if (b.EqualsZero()) return a; else return RSA::GCD(b, a % b); }
/* Generates a random "number" such as 1 <= "number" < "top". * Returns it by reference in the "number" parameter. */ void PrimeGenerator::makeRandom(BigInt &number, const BigInt &top) { //randomly select the number of digits for the random number unsigned long int newDigitCount = (rand() % top.digitCount) + 1; makeRandom(number, newDigitCount); //make sure the number is < top and not zero while (number >= top || number.EqualsZero()) makeRandom(number, newDigitCount); //make sure the leading digit is not zero while (number.digits[number.digitCount - 1] == 0) number.digitCount--; }
/* Solves the equation * d = ax + by * given a and b, and returns d, x and y by reference. * It uses the Extended Euclidean Algorithm */ void RSA::extendedEuclideanAlgorithm(const BigInt &a, const BigInt &b, BigInt &d, BigInt &x, BigInt &y) { if (b.EqualsZero()) { d = a; x = BigIntOne; y = BigIntZero; return; } RSA::extendedEuclideanAlgorithm(b, a % b, d, x, y); BigInt temp(x); x = y; y = temp - a / b * y; }