Compare commits

...

4 Commits

99
main.c
View File

@@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include "helper.c"
@@ -156,6 +157,74 @@ 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) {
// 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;
uint64_t Mp = q->prime;
uint64_t Mq = p->prime;
// C1: c^(d mod P-1) mod 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);
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);
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;
y.y %= q->prime;
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;
printf("n: ");
print_uint128(n);
printf("\n");
unsigned __int128 fi_n = (p->prime - 1) * (q->prime - 1);
printf("fi_n: ");
print_uint128(fi_n);
printf("\n");
// 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
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;
calc_d.y %= fi_n;
unsigned __int128 d = calc_d.y;
uint64_t length = 0;
uint64_t *nyenye = dec_to_bin(e, &length);
unsigned __int128 c = quick_pow(nyenye, *m, n, length);
free(nyenye);
printf("\nc: ");
print_uint128(c);
return c;
}
int main() {
uint64_t m = 0;
printf("give input for m: \n");
@@ -173,33 +242,15 @@ int main() {
pthread_join(thread_p, NULL);
pthread_join(thread_q, NULL);
printf("\n");
unsigned __int128 n = p.prime * q.prime;
print_uint128(n);
rsa_encrypt(&m, &p, &q);
printf("\nkinai maradek tetel:\n");
unsigned __int128 S = kinai_maradek_tetel(&m, 2263, &p, &q);
printf("S: ");
print_uint128(S);
printf("\n");
unsigned __int128 fi_n = (p.prime - 1) * (q.prime - 1);
print_uint128(fi_n);
printf("\n");
// 2. kulcsgeneralas
uint64_t e = 0;
do {
e = rand64();
} while (e <= 1 && e >= fi_n && prime_test(e, base));
euklidian_result_t calc_d = euklidian_algorigthm_extended(fi_n, e);
__int128 d = calc_d.y;
uint64_t length = 0;
uint64_t *nyenye = dec_to_bin(e, &length);
unsigned __int128 c = quick_pow(nyenye, m, n, length);
free(nyenye);
printf("\nc: ");
print_uint128(c);
return 0;
}