math is good hangs on a big loop or some stupppppid thing

This commit is contained in:
2026-04-09 15:41:09 +02:00
parent 26920636fa
commit ffeec2ba89

94
main.c
View File

@@ -24,8 +24,8 @@ uint64_t *dec_to_bin(uint64_t d, uint64_t *length) {
return binary_form;
}
uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length) {
uint64_t *powed = calloc(100, sizeof(uint64_t));
unsigned __int128 quick_pow(uint64_t *d_binary, unsigned __int128 a, unsigned __int128 n, uint64_t length) {
unsigned __int128 *powed = calloc(100, sizeof(unsigned __int128));
powed[0] = a;
for (int i = 1; i <= length; i++) {
@@ -73,7 +73,7 @@ bool prime_test(uint64_t n, int a) {
// 1: a^d =k 1 mod n
uint64_t length = 0;
uint64_t *d_binary = dec_to_bin(d, &length);
uint64_t first_qp_res = quick_pow(d_binary, a, n, length);
unsigned __int128 first_qp_res = quick_pow(d_binary, a, n, length);
if (first_qp_res == 1) {
free(d_binary);
@@ -87,9 +87,6 @@ bool prime_test(uint64_t n, int a) {
free(d_binary);
// printf("true\n");
return true;
} else if (first_qp_res < n - 2) {
// printf("first_qp_res became smaller then n!!\n");
break;
} else {
first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n);
}
@@ -138,11 +135,11 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
prev_r = r;
r = next_r;
xk = xk * prev_q + prev_prev_xk;
xk = prev_xk * prev_q + prev_prev_xk;
prev_prev_xk = prev_xk;
prev_xk = xk;
yk = yk * prev_q + prev_prev_yk;
yk = prev_yk * prev_q + prev_prev_yk;
prev_prev_yk = prev_yk;
prev_yk = yk;
}
@@ -157,7 +154,7 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
return res;
}
unsigned __int128 kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p, prime_test_t *q) {
unsigned __int128 kinai_maradek_tetel(uint64_t *m, unsigned __int128 d, prime_test_t *p, prime_test_t *q) {
// sum(i: 1,2): Ci * Yi * Mi mod M
// M: P*Q, Mp: M/P, Mq: M/Q
unsigned __int128 M = p->prime * q->prime;
@@ -168,35 +165,38 @@ unsigned __int128 kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p,
uint64_t temp_exponent = d % (p->prime - 1);
uint64_t exponent_bin_length = 0;
uint64_t *exponent_as_binary = dec_to_bin(temp_exponent, &exponent_bin_length);
uint64_t c1 = quick_pow(exponent_as_binary, *m, p->prime, exponent_bin_length);
unsigned __int128 c1 = quick_pow(exponent_as_binary, *m, p->prime, exponent_bin_length);
free(exponent_as_binary);
// C2: c^(d mod Q-1) mod Q
temp_exponent = d % (q->prime - 1);
exponent_as_binary = dec_to_bin(temp_exponent, &exponent_bin_length);
uint64_t c2 = quick_pow(exponent_as_binary, *m, q->prime, exponent_bin_length);
unsigned __int128 c2 = quick_pow(exponent_as_binary, *m, q->prime, exponent_bin_length);
free(exponent_as_binary);
euklidian_result_t y = euklidian_algorigthm_extended(Mp, Mq); // in the struct the x will mean the y1 and y will mean the y2
// if either of them is less a negative number shift them into postive range with with hte modulo
y.x %= p->prime;
y.x += p->prime;
if (y.x < 0)
y.x += p->prime;
y.y %= q->prime;
y.y += q->prime;
if (y.y < 0)
y.y += q->prime;
unsigned __int128 s1 = (c1 * y.x * Mp) % M;
unsigned __int128 s2 = (c2 * y.y * Mq) % M;
return (s1 + s2) % M;
}
unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q) {
unsigned __int128 n = p->prime * q->prime;
unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *out_e, unsigned __int128 *out_d) {
unsigned __int128 n = (unsigned __int128)p->prime * q->prime;
printf("n: ");
print_uint128(n);
printf("\n");
unsigned __int128 fi_n = (p->prime - 1) * (q->prime - 1);
unsigned __int128 fi_n = (unsigned __int128)(p->prime - 1) * (q->prime - 1);
printf("fi_n: ");
print_uint128(fi_n);
printf("\n");
@@ -204,16 +204,23 @@ unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q) {
// 2. kulcsgeneralas
uint64_t e = 0;
do {
e = rand64();
} while (e <= 1 && e >= fi_n && prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg
e = rand64() % fi_n;
} while (e <= 1 && prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg
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
calc_d.x %= fi_n;
if (calc_d.x < 0)
calc_d.x += fi_n;
calc_d.y %= fi_n;
if (calc_d.y < 0)
calc_d.y += fi_n;
unsigned __int128 d = calc_d.y;
*out_e = e;
*out_d = d;
uint64_t length = 0;
uint64_t *nyenye = dec_to_bin(e, &length);
@@ -226,6 +233,11 @@ unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q) {
}
int main() {
int isSignature = 0;
printf("Please input 0 for Rsa encryption or 1 for Rsa signature: ");
scanf("%d", &isSignature);
printf("\n");
uint64_t m = 0;
printf("give input for m: \n");
scanf("%ju", &m);
@@ -244,13 +256,47 @@ int main() {
pthread_join(thread_q, NULL);
printf("\n");
rsa_encrypt(&m, &p, &q);
uint64_t e = 0;
unsigned __int128 d = 0;
printf("\nkinai maradek tetel:\n");
unsigned __int128 S = kinai_maradek_tetel(&m, 2263, &p, &q);
printf("S: ");
print_uint128(S);
printf("\n");
if (!isSignature) {
// rsa encryption
rsa_encrypt(&m, &p, &q, &e, &d);
printf("\nkinai maradek tetel:\n");
unsigned __int128 S = kinai_maradek_tetel(&m, d, &p, &q);
printf("S: ");
print_uint128(S);
printf("\n");
} else if (isSignature == 1) {
// rsa signature
// generate keys
uint64_t dummy = 2;
rsa_encrypt(&dummy, &p, &q, &e, &d);
printf("\n");
unsigned __int128 signature = kinai_maradek_tetel(&m, (uint64_t)d, &p, &q);
printf("Alairas (Signature): ");
print_uint128(signature);
// key verifacation
uint64_t e_length = 0;
uint64_t *e_binary = dec_to_bin(e, &e_length);
unsigned __int128 n = p.prime * q.prime;
unsigned __int128 verified_message = quick_pow(e_binary, (uint64_t)signature, n, e_length);
free(e_binary);
printf("Verified Message: ");
print_uint128(verified_message);
// 4. THE RESULT
if (verified_message == m) {
printf("\nSignature correct\n");
} else {
printf("\nSignature not correct\n");
}
}
return 0;
}