diff --git a/rsa.c b/rsa.c index 06774fc..1320f85 100644 --- a/rsa.c +++ b/rsa.c @@ -3,13 +3,12 @@ #include #include -int gcd(int a, int b) { +uint64_t gcd(uint64_t a, uint64_t b) { while (b != 0) { - int temp = b; + uint64_t temp = b; b = a % b; a = temp; } - return a; } @@ -71,7 +70,7 @@ uint64_t modexp(uint64_t a, uint64_t b, uint64_t m) { uint64_t gen_prime(uint64_t min, uint64_t max) { uint64_t cand = 0; - while (!miller_rabin(cand, 10)) cand = prand_range(min, max); + while (!miller_rabin(cand, 5)) cand = prand_range(min, max); return cand; } @@ -88,26 +87,26 @@ bool is_prime(int n) { return true; } -bool miller_rabin(int n, int k) { +bool miller_rabin(uint64_t n, uint64_t k) { if (n < 2) return false; - int d = n - 1; - int s = 0; + uint64_t d = n - 1; + uint64_t s = 0; while (d % 2 == 0) { d /= 2; s++; } - for (int i = 0; i < k; i++) { - int a = prand_range(2, n - 2); - int x = modexp(a, d, n); + for (uint64_t i = 0; i < k; i++) { + uint64_t a = prand_range(2, n - 2); + uint64_t x = modexp(a, d, n); if (x == 1 || x == n - 1) continue; - for (int r = 1; r < s; r++) { + for (uint64_t r = 1; r < s; r++) { x = modexp(x, 2, n); if (x == n - 1) break; @@ -120,11 +119,31 @@ bool miller_rabin(int n, int k) { return true; // Likely prime } -int mod_inverse(int e, int phi) { - for (int d = 0; d < phi; d++) { - if ((d * e) % phi == 1) - return d; +int mod_inverse(int a, int m) { + int m0 = m; + int y = 0, x = 1; + + if (m == 1) + return 0; + + while (a > 1) { + // q is quotient + int q = a / m; + int t = m; + + // m is remainder now + m = a % m; + a = t; + t = y; + + // Update x and y + y = x - q * y; + x = t; } - return 0; + // Make x positive + if (x < 0) + x += m0; + + return x; } diff --git a/rsa.h b/rsa.h index 690d0e3..e56004d 100644 --- a/rsa.h +++ b/rsa.h @@ -10,7 +10,7 @@ * @param b Second number * @return The greatest common divider */ -int gcd(int a, int b); +uint64_t gcd(uint64_t a, uint64_t b); /** * @brief Computes Euler's Totient function φ(n), which counts the number of @@ -31,13 +31,13 @@ int totient(int n); uint64_t modexp(uint64_t a, uint64_t b, uint64_t m); /** - * @brief Computes the modular inverse of e modulo phi. + * @brief Computes the modular inverse of a modulo m. * - * @param e The integer whose modular inverse is to be found. - * @param phi The modulus. - * @return The modular inverse of e modulo phi, or -1 if no inverse exists. + * @param a The integer whose modular inverse is to be found. + * @param m The modulus. + * @return The modular inverse of a modulo m, or -1 if no inverse exists. */ -int mod_inverse(int e, int phi); +int mod_inverse(int a, int m); /** * @brief Generates a random prime number within the given range. @@ -64,7 +64,7 @@ bool is_prime(int n); * @param k The number of rounds of testing to perform. * @return true if n is probably prime, false if n is composite. */ -bool miller_rabin(int n, int k); +bool miller_rabin(uint64_t n, uint64_t k); /** * @brief Computes the greatest common divisor (GCD) of two integers a and b