Formatting
This commit is contained in:
parent
66b499bc06
commit
117adf9da1
1 changed files with 14 additions and 26 deletions
40
rsa.c
40
rsa.c
|
@ -7,20 +7,16 @@ u64 gcd(u64 a, u64 b) { return extended_euclid(a, b, NULL, NULL); }
|
||||||
|
|
||||||
u64 extended_euclid(u64 a, u64 b, u64 *x, u64 *y) {
|
u64 extended_euclid(u64 a, u64 b, u64 *x, u64 *y) {
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
if (x)
|
if (x) *x = 1;
|
||||||
*x = 1;
|
if (y) *y = 0;
|
||||||
if (y)
|
|
||||||
*y = 0;
|
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 x1, y1;
|
u64 x1, y1;
|
||||||
u64 gcd = extended_euclid(b, a % b, &x1, &y1);
|
u64 gcd = extended_euclid(b, a % b, &x1, &y1);
|
||||||
|
|
||||||
if (x)
|
if (x) *x = y1;
|
||||||
*x = y1;
|
if (y) *y = x1 - (a / b) * y1;
|
||||||
if (y)
|
|
||||||
*y = x1 - (a / b) * y1;
|
|
||||||
|
|
||||||
return gcd;
|
return gcd;
|
||||||
}
|
}
|
||||||
|
@ -56,8 +52,8 @@ u64 mulmod(u64 a, u64 b, u64 m) {
|
||||||
if (b & 1) {
|
if (b & 1) {
|
||||||
result = (result + a) % m;
|
result = (result + a) % m;
|
||||||
}
|
}
|
||||||
a = (a * 2) % m; // Double a, keep it within the modulus
|
a = (a * 2) % m; // Double a, keep it within the modulus
|
||||||
b >>= 1; // Right shift b (divide by 2)
|
b >>= 1; // Right shift b (divide by 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -86,20 +82,17 @@ u64 gen_prime(u64 min, u64 max) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_prime(u64 n) {
|
bool is_prime(u64 n) {
|
||||||
if (n < 2)
|
if (n < 2) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
for (int i = 2; i < n / 2 + 1; i++) {
|
for (int i = 2; i < n / 2 + 1; i++) {
|
||||||
if (n % i == 0)
|
if (n % i == 0) return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool miller_rabin(u64 n, u64 k) {
|
bool miller_rabin(u64 n, u64 k) {
|
||||||
if (n < 2)
|
if (n < 2) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
u64 d = n - 1;
|
u64 d = n - 1;
|
||||||
u64 s = 0;
|
u64 s = 0;
|
||||||
|
@ -113,17 +106,14 @@ bool miller_rabin(u64 n, u64 k) {
|
||||||
u64 a = prand_range(2, n - 2);
|
u64 a = prand_range(2, n - 2);
|
||||||
u64 x = modexp(a, d, n);
|
u64 x = modexp(a, d, n);
|
||||||
|
|
||||||
if (x == 1 || x == n - 1)
|
if (x == 1 || x == n - 1) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
for (u64 r = 1; r < s; r++) {
|
for (u64 r = 1; r < s; r++) {
|
||||||
x = modexp(x, 2, n);
|
x = modexp(x, 2, n);
|
||||||
if (x == n - 1)
|
if (x == n - 1) break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (x != n - 1)
|
if (x != n - 1) return false; // Not prime
|
||||||
return false; // Not prime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true; // Likely prime
|
return true; // Likely prime
|
||||||
|
@ -134,8 +124,7 @@ u64 mod_inverse(u64 a, u64 m) {
|
||||||
u64 y = 0, x = 1;
|
u64 y = 0, x = 1;
|
||||||
|
|
||||||
// Modular inverse does not exist when m is 1
|
// Modular inverse does not exist when m is 1
|
||||||
if (m == 1)
|
if (m == 1) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
while (a > 1) {
|
while (a > 1) {
|
||||||
// q is quotient
|
// q is quotient
|
||||||
|
@ -153,8 +142,7 @@ u64 mod_inverse(u64 a, u64 m) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make x positive
|
// Make x positive
|
||||||
if (x < 0)
|
if (x < 0) x += m0;
|
||||||
x += m0;
|
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue