Extended euclidean algorithm using recursion
This commit is contained in:
parent
2c7904d2b3
commit
62439387e5
2 changed files with 32 additions and 0 deletions
17
rsa.c
17
rsa.c
|
@ -13,6 +13,23 @@ int gcd(int a, int b) {
|
|||
return a;
|
||||
}
|
||||
|
||||
int extended_euclid(int a, int b, int *x, int *y) {
|
||||
if (b == 0) {
|
||||
*x = 1;
|
||||
*y = 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
int x1, y1;
|
||||
int gcd = extended_euclid(b, a % b, &x1, &y1);
|
||||
|
||||
// Update x and y using results from recursive call
|
||||
*x = y1;
|
||||
*y = x1 - (a / b) * y1;
|
||||
|
||||
return gcd;
|
||||
}
|
||||
|
||||
int totient(int n) {
|
||||
int result = n;
|
||||
|
||||
|
|
15
rsa.h
15
rsa.h
|
@ -65,3 +65,18 @@ bool is_prime(int n);
|
|||
* @return true if n is probably prime, false if n is composite.
|
||||
*/
|
||||
bool miller_rabin(int n, int k);
|
||||
|
||||
/**
|
||||
* @brief Computes the greatest common divisor (GCD) of two integers a and b
|
||||
* using the Extended Euclidean Algorithm. Also finds coefficients x and y such
|
||||
* that ax + by = gcd(a, b).
|
||||
*
|
||||
* @param a The first integer.
|
||||
* @param b The second integer.
|
||||
* @param x Pointer to an integer to store the coefficient x in the equation ax
|
||||
* + by = gcd(a, b).
|
||||
* @param y Pointer to an integer to store the coefficient y in the equation ax
|
||||
* + by = gcd(a, b).
|
||||
* @return The greatest common divisor (gcd) of a and b.
|
||||
*/
|
||||
int extended_euclid(int a, int b, int *x, int *y);
|
||||
|
|
Loading…
Add table
Reference in a new issue