Compare commits

...

17 Commits

Author SHA1 Message Date
04c0bfc5fe final touches 2026-04-14 09:43:06 +02:00
bc46aabfc5 deleted dummy var, it does not matter if we generate for dummy or m, it only needs the e and d values from this 2026-04-14 09:37:28 +02:00
e02326f35f explenatory comments 2026-04-14 09:36:52 +02:00
f4068f97b4 generating e 2026-04-14 09:02:35 +02:00
b5f389264a deleted misc 2026-04-14 09:02:25 +02:00
fd3700c960 random generator optimization for primes 2026-04-14 09:01:53 +02:00
de292aba25 comments 2026-04-14 09:01:39 +02:00
bcb399deba primes got generated as 64bit into a 32bit container 2026-04-14 09:01:11 +02:00
5317f37fd7 fixed bug in decryption where incorrect value was passed into the crt 2026-04-14 08:53:30 +02:00
a5cbb82dd9 it works finally 2026-04-12 09:43:43 +02:00
ffeec2ba89 math is good hangs on a big loop or some stupppppid thing 2026-04-09 15:41:09 +02:00
26920636fa reset the user input and deleted test value overwrites 2026-04-09 13:20:06 +02:00
a721456b45 fixed the bugs in crt 2026-04-09 13:18:31 +02:00
3473883507 added negative check for euclidian algo res and implemented crt 2026-04-09 12:57:39 +02:00
aee2d7ffd0 moved rsa encryption to a function 2026-04-09 12:20:52 +02:00
ab5e2ab4cc removed sample data 2026-03-29 17:09:23 +02:00
f72b2156cb working encryption with sample data 2026-03-29 17:09:08 +02:00
3 changed files with 164 additions and 32 deletions

View File

@@ -2,6 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
uint32_t rand32() { return (rand() << 16) | (rand() & 0xFFFF) | 1; }
void print_uint128(unsigned __int128 n) { void print_uint128(unsigned __int128 n) {
if (n == 0) { if (n == 0) {
printf("0\n"); printf("0\n");
@@ -30,12 +32,14 @@ uint64_t rand64() {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
r = (r << 16) | (rand() & 0xFFFF); r = (r << 16) | (rand() & 0xFFFF);
} }
// Force the bottom bit to 1, this will make all generated primes odd
r = r | 1;
return r; return r;
} }
// Stitch two 64-bit random numbers into a 128-bit number // Stitch two 64-bit random numbers into a 128-bit number
unsigned __int128 generate_stitched_128() { unsigned __int128 generate_stitched_128() {
// Make sure to seed rand() in your main function with srand(time(NULL))
uint64_t top_half = rand64(); uint64_t top_half = rand64();
uint64_t bottom_half = rand64(); uint64_t bottom_half = rand64();
@@ -46,10 +50,9 @@ unsigned __int128 generate_stitched_128() {
} }
unsigned __int128 generate_prime_candidate() { unsigned __int128 generate_prime_candidate() {
// 1. Get the raw random bytes
unsigned __int128 candidate = generate_stitched_128(); unsigned __int128 candidate = generate_stitched_128();
// 2. Force the bottom bit to 1 (Ensures it is odd) // Force the bottom bit to 1, this will make all generated primes odd
candidate = candidate | 1; candidate = candidate | 1;
// 3. Force the top bit to 1 (Ensures it is a full 128-bit number) // 3. Force the top bit to 1 (Ensures it is a full 128-bit number)

BIN
main Executable file

Binary file not shown.

187
main.c
View File

@@ -5,6 +5,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <time.h> #include <time.h>
#include "helper.c" #include "helper.c"
@@ -48,7 +49,7 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
} }
bool prime_test(uint64_t n, int a) { bool prime_test(uint64_t n, int a) {
printf("\n\nprime test: %ju\n", n); // printf("\n\nprime test: %ju\n", n);
// Miller Rabin prime test // Miller Rabin prime test
// choose a base: a, which should be a prime so that (n, a) = 1 // choose a base: a, which should be a prime so that (n, a) = 1
// then do 2 rounds of tests provided the first one did not fail // then do 2 rounds of tests provided the first one did not fail
@@ -58,6 +59,18 @@ bool prime_test(uint64_t n, int a) {
// S: see above // S: see above
// r = {0,... S-1} // r = {0,... S-1}
// this should not happen but just in case
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
//
uint64_t d = n - 1; uint64_t d = n - 1;
uint64_t S = 0; uint64_t S = 0;
@@ -71,8 +84,9 @@ bool prime_test(uint64_t n, int a) {
// round 1 // round 1
// 1: a^d =k 1 mod n // 1: a^d =k 1 mod n
uint64_t length = 0; uint64_t length = 0;
// convert exponent to binary to use in quickpow
uint64_t *d_binary = dec_to_bin(d, &length); 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) { if (first_qp_res == 1) {
free(d_binary); free(d_binary);
@@ -84,12 +98,10 @@ bool prime_test(uint64_t n, int a) {
for (int i = 0; i <= r; i++) { for (int i = 0; i <= r; i++) {
if (first_qp_res == n - 1) { if (first_qp_res == n - 1) {
free(d_binary); free(d_binary);
printf("true\n"); // printf("true\n");
return true; return true;
} else if (first_qp_res < n - 2) {
printf("first_qp_res became smaller then n!!\n");
break;
} else { } 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); first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n);
} }
} }
@@ -100,17 +112,14 @@ bool prime_test(uint64_t n, int a) {
typedef struct { typedef struct {
int base; int base;
uint64_t prime; uint32_t prime;
} prime_test_t; } prime_test_t;
void *prime_thread_worker(void *arg) { void *prime_thread_worker(void *arg) {
prime_test_t *result_ptr = (prime_test_t *)arg; prime_test_t *result_ptr = (prime_test_t *)arg;
do { do {
result_ptr->prime = rand64(); 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)); } while (!prime_test(result_ptr->prime, result_ptr->base));
return NULL; return NULL;
@@ -122,7 +131,7 @@ typedef struct {
__int128 y; __int128 y;
} euklidian_result_t; } euklidian_result_t;
euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned __int128 b) { euklidian_result_t euklidian_algorigthm_extended(uint64_t a, uint64_t b) {
__int128 r = a % b, q = a / b, k = 1, xk = 0, yk = 1, next_r; __int128 r = a % b, q = a / b, k = 1, xk = 0, yk = 1, next_r;
__int128 prev_r = b, prev_q, prev_xk = 0, prev_yk = 1, prev_prev_xk = 1, prev_prev_yk = 0; __int128 prev_r = b, prev_q, prev_xk = 0, prev_yk = 1, prev_prev_xk = 1, prev_prev_yk = 0;
euklidian_result_t res = {0, 0, 0}; euklidian_result_t res = {0, 0, 0};
@@ -137,11 +146,11 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
prev_r = r; prev_r = r;
r = next_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_prev_xk = prev_xk;
prev_xk = 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_prev_yk = prev_yk;
prev_yk = yk; prev_yk = yk;
} }
@@ -149,14 +158,106 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
__int128 x = k % 2 == 0 ? prev_xk : -prev_xk; __int128 x = k % 2 == 0 ? prev_xk : -prev_xk;
__int128 y = k % 2 == 0 ? -prev_yk : prev_yk; __int128 y = k % 2 == 0 ? -prev_yk : prev_yk;
res.lnko = prev_r; // 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.x = x;
res.y = y; res.y = y;
return res; return res;
} }
uint64_t 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
uint64_t M = (uint64_t)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
uint64_t y1_pos;
if (y.x < 0) {
y1_pos = p->prime - (uint64_t)(-y.x % p->prime);
} else {
y1_pos = (uint64_t)y.x % p->prime;
}
uint64_t y2_pos;
if (y.y < 0) {
y2_pos = q->prime - (uint64_t)(-y.y % q->prime);
} else {
y2_pos = (uint64_t)y.y % q->prime;
}
// Apply the modulo between multiplications to prevent 192-bit overflows!
uint64_t s1 = (uint64_t)((((unsigned __int128)c1 * y1_pos) % M * Mp) % M);
uint64_t s2 = (uint64_t)((((unsigned __int128)c2 * y2_pos) % M * Mq) % M);
return (uint64_t)(((unsigned __int128)s1 + s2) % M);
}
uint64_t rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q, uint64_t *out_e, uint64_t *out_d) {
uint64_t n = (uint64_t)p->prime * q->prime;
printf("n: %ju\n", n);
uint64_t fi_n = (uint64_t)(p->prime - 1) * (q->prime - 1);
printf("n: %ju\n", fi_n);
// keygen
uint64_t e = 65537;
do {
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
uint64_t d;
if (calc_d.y < 0) {
d = fi_n - (uint64_t)(-calc_d.y);
} else {
d = (uint64_t)calc_d.y;
}
*out_e = e;
*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);
printf("\nc: ");
print_uint128(c);
return c;
}
int main() { 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: ");
scanf("%ju", &m);
printf("\n");
srand(time(NULL)); srand(time(NULL));
uint64_t base = 2; uint64_t base = 2;
@@ -169,25 +270,53 @@ int main() {
pthread_join(thread_p, NULL); pthread_join(thread_p, NULL);
pthread_join(thread_q, NULL); pthread_join(thread_q, NULL);
printf("\n"); printf("\n");
unsigned __int128 n = p.prime * q.prime; printf("p: %u\n", p.prime);
print_uint128(n); printf("q: %u\n", q.prime);
printf("\n");
unsigned __int128 fi_n = (p.prime - 1) * (q.prime - 1);
print_uint128(fi_n);
printf("\n");
// 2. kulcsgeneralas
uint64_t e = 0; uint64_t e = 0;
do { uint64_t d = 0;
e = rand64();
} while (e <= 1 && e >= fi_n && prime_test(e, base));
euklidian_result_t test = euklidian_algorigthm_extended(192, 11); if (!isSignature) {
printf("test lnko: %ju\n", test.lnko); // rsa encryption
uint64_t c = rsa_encrypt(&m, &p, &q, &e, &d);
printf("\nkinai maradek tetel:\n");
unsigned __int128 S = kinai_maradek_tetel(&c, d, &p, &q);
printf("S: ");
print_uint128(S);
printf("\n");
} else if (isSignature == 1) {
// rsa signature
// generate keys
rsa_encrypt(&m, &p, &q, &e, &d);
printf("\n");
// c^d -> creates signature
uint64_t signature = kinai_maradek_tetel(&m, d, &p, &q);
printf("Signature: ");
print_uint128(signature);
// key verifacation
// S^e -> verifies the signature
uint64_t e_length = 0;
uint64_t *e_binary = dec_to_bin(e, &e_length);
uint64_t n = (uint64_t)p.prime * q.prime;
uint64_t verified_message = quick_pow(e_binary, signature, n, e_length);
free(e_binary);
printf("Verified Message: %ju", verified_message);
if (verified_message == m) {
printf("\nSignature correct\n");
} else {
printf("\nSignature not correct\n");
}
} else {
printf("Why?\n");
}
return 0; return 0;
} }