explenatory comments

This commit is contained in:
2026-04-14 09:36:52 +02:00
parent f4068f97b4
commit e02326f35f

13
main.c
View File

@@ -69,6 +69,7 @@ bool prime_test(uint64_t n, int a) {
if (n % 2 == 0) {
return false;
}
//
uint64_t d = n - 1;
uint64_t S = 0;
@@ -83,6 +84,7 @@ bool prime_test(uint64_t n, int a) {
// round 1
// 1: a^d =k 1 mod n
uint64_t length = 0;
// convert exponent to binary to use in quickpow
uint64_t *d_binary = dec_to_bin(d, &length);
unsigned __int128 first_qp_res = quick_pow(d_binary, a, n, length);
@@ -99,6 +101,7 @@ bool prime_test(uint64_t n, int a) {
// printf("true\n");
return true;
} else {
//^2 with mod n, for each step since the next is the previous's squared
first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n);
}
}
@@ -117,9 +120,6 @@ void *prime_thread_worker(void *arg) {
do {
result_ptr->prime = rand32();
// printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr);
// printf("%ju", result_ptr->prime);
// printf("\n");
} while (!prime_test(result_ptr->prime, result_ptr->base));
return NULL;
@@ -158,6 +158,7 @@ euklidian_result_t euklidian_algorigthm_extended(uint64_t a, uint64_t b) {
__int128 x = k % 2 == 0 ? prev_xk : -prev_xk;
__int128 y = k % 2 == 0 ? -prev_yk : prev_yk;
// the reason this can be casted is that two uint64_t-s cant have a lnko which is bigger than a uint64_t
res.lnko = (uint64_t)prev_r;
res.x = x;
res.y = y;
@@ -215,12 +216,13 @@ uint64_t rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *ou
uint64_t fi_n = (uint64_t)(p->prime - 1) * (q->prime - 1);
printf("n: %ju\n", fi_n);
// 2. kulcsgeneralas
// keygen
uint64_t e = 65537;
do {
e = rand32() % fi_n;
e = rand32() % fi_n; // should this go back as a condition inside the while loop?
} while (e <= 1 || !prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg
// calculate the d value, in eae it will be the y value
euklidian_result_t calc_d = euklidian_algorigthm_extended(fi_n, e);
// if either of them is less a negative number shift them into postive range with with hte modulo
@@ -235,6 +237,7 @@ uint64_t rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *ou
*out_d = d;
uint64_t length = 0;
// m^e mod n
uint64_t *nyenye = dec_to_bin(e, &length);
uint64_t c = quick_pow(nyenye, *m, n, length);
free(nyenye);