Compare commits

...

22 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
66e261165c extended euklidian algorithm 2026-03-29 16:41:04 +02:00
c6250677dd added prime_test_t type 2026-03-27 15:16:44 +01:00
4910520db3 fixed bug with the random generator 2026-03-27 15:16:31 +01:00
b7a4d6619c step 1 done 2026-03-27 14:59:17 +01:00
2442bf62bc added debug symbols to build 2026-03-27 14:58:48 +01:00
4 changed files with 260 additions and 27 deletions

View File

@@ -1 +1,2 @@
gcc main.c -o main -std=c11 -lm && ./main gcc main.c -g -o main -std=c11 -lm -pthread && ./main

View File

@@ -1,5 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.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) {
@@ -23,3 +26,37 @@ void print_uint128(unsigned __int128 n) {
} }
putchar('\n'); putchar('\n');
} }
uint64_t rand64() {
uint64_t r = 0;
for (int i = 0; i < 4; i++) {
r = (r << 16) | (rand() & 0xFFFF);
}
// Force the bottom bit to 1, this will make all generated primes odd
r = r | 1;
return r;
}
// Stitch two 64-bit random numbers into a 128-bit number
unsigned __int128 generate_stitched_128() {
uint64_t top_half = rand64();
uint64_t bottom_half = rand64();
// Cast the top half to 128-bit, shift it over by 64 bits, and attach the bottom
unsigned __int128 full_number = ((unsigned __int128)top_half << 64) | bottom_half;
return full_number;
}
unsigned __int128 generate_prime_candidate() {
unsigned __int128 candidate = generate_stitched_128();
// Force the bottom bit to 1, this will make all generated primes odd
candidate = candidate | 1;
// 3. Force the top bit to 1 (Ensures it is a full 128-bit number)
candidate = candidate | ((unsigned __int128)1 << 127);
return candidate;
}

BIN
main Executable file

Binary file not shown.

247
main.c
View File

@@ -1,8 +1,12 @@
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#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 "helper.c" #include "helper.c"
@@ -25,7 +29,6 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
powed[0] = a; powed[0] = a;
for (int i = 1; i <= length; i++) { for (int i = 1; i <= length; i++) {
// powed[i] = (powed[i - 1] * powed[i - 1]) % n;
powed[i] = (uint64_t)(((unsigned __int128)powed[i - 1] * powed[i - 1]) % n); powed[i] = (uint64_t)(((unsigned __int128)powed[i - 1] * powed[i - 1]) % n);
// printf("powed: %ju, index: %d; ", powed[i], (i)); // printf("powed: %ju, index: %d; ", powed[i], (i));
} }
@@ -34,7 +37,6 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
uint64_t multiplied = 1; uint64_t multiplied = 1;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (d_binary[i] == 1) { if (d_binary[i] == 1) {
// multiplied = (multiplied * powed[i]) % n;
multiplied = (uint64_t)(((unsigned __int128)multiplied * powed[i]) % n); multiplied = (uint64_t)(((unsigned __int128)multiplied * powed[i]) % n);
} }
} }
@@ -47,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
@@ -57,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;
@@ -69,11 +83,12 @@ 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; 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 = 0; unsigned __int128 first_qp_res = quick_pow(d_binary, a, n, length);
if ((first_qp_res = quick_pow(d_binary, a, n, length)) == 1) { if (first_qp_res == 1) {
free(d_binary); free(d_binary);
return true; return true;
} }
@@ -83,10 +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 { } else {
// first_qp_res = (first_qp_res * first_qp_res) % n; //^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);
} }
} }
@@ -95,33 +110,213 @@ bool prime_test(uint64_t n, int a) {
return false; return false;
} }
int main() { typedef struct {
prime_test(111, 5); int base;
prime_test(29, 2); uint32_t prime;
prime_test(27, 2); } prime_test_t;
prime_test(17, 2);
prime_test(661, 2);
prime_test(18446744073709551557UL, 2);
prime_test(18446744073709551533UL, 3);
uint64_t p = 11; void *prime_thread_worker(void *arg) {
uint64_t q = 29; prime_test_t *result_ptr = (prime_test_t *)arg;
// probably i should generate instead of asking the user to input do {
if (!prime_test(p, 2) || !prime_test(q, 2)) { result_ptr->prime = rand32();
printf("given numbers were not primes"); } while (!prime_test(result_ptr->prime, result_ptr->base));
return 1;
return NULL;
}
typedef struct {
uint64_t lnko;
__int128 x;
__int128 y;
} euklidian_result_t;
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 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};
while (r != 0) {
k++;
prev_q = q;
q = prev_r / r;
next_r = prev_r % r;
prev_r = r;
r = next_r;
xk = prev_xk * prev_q + prev_prev_xk;
prev_prev_xk = prev_xk;
prev_xk = xk;
yk = prev_yk * prev_q + prev_prev_yk;
prev_prev_yk = prev_yk;
prev_yk = yk;
} }
__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;
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 isSignature = 0;
printf("Please input 0 for Rsa encryption or 1 for Rsa signature: ");
scanf("%d", &isSignature);
printf("\n"); printf("\n");
unsigned __int128 n = p * q; uint64_t m = 0;
print_uint128(n); printf("give input for m: ");
scanf("%ju", &m);
printf("\n"); printf("\n");
unsigned __int128 fi_n = (p - 1) * (q - 1); srand(time(NULL));
print_uint128(fi_n);
uint64_t base = 2;
pthread_t thread_p, thread_q;
prime_test_t p = {base, 0};
prime_test_t q = {base, 0};
pthread_create(&thread_p, NULL, prime_thread_worker, &p);
pthread_create(&thread_q, NULL, prime_thread_worker, &q);
pthread_join(thread_p, NULL);
pthread_join(thread_q, NULL);
printf("\n"); printf("\n");
printf("p: %u\n", p.prime);
printf("q: %u\n", q.prime);
uint64_t e = 0;
uint64_t d = 0;
if (!isSignature) {
// 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;
} }