Compare commits

...

11 Commits

3 changed files with 118 additions and 49 deletions

View File

@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
uint32_t rand32() { return (rand() << 16) | (rand() & 0xFFFF) | 1; }
void print_uint128(unsigned __int128 n) {
if (n == 0) {
printf("0\n");
@@ -30,12 +32,14 @@ uint64_t rand64() {
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() {
// Make sure to seed rand() in your main function with srand(time(NULL))
uint64_t top_half = rand64();
uint64_t bottom_half = rand64();
@@ -46,10 +50,9 @@ unsigned __int128 generate_stitched_128() {
}
unsigned __int128 generate_prime_candidate() {
// 1. Get the raw random bytes
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;
// 3. Force the top bit to 1 (Ensures it is a full 128-bit number)

BIN
main Executable file

Binary file not shown.

158
main.c
View File

@@ -59,6 +59,18 @@ bool prime_test(uint64_t n, int a) {
// S: see above
// 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 S = 0;
@@ -72,8 +84,9 @@ 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);
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,10 +100,8 @@ 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 {
//^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);
}
}
@@ -101,17 +112,14 @@ bool prime_test(uint64_t n, int a) {
typedef struct {
int base;
uint64_t prime;
uint32_t prime;
} prime_test_t;
void *prime_thread_worker(void *arg) {
prime_test_t *result_ptr = (prime_test_t *)arg;
do {
result_ptr->prime = rand64();
// printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr);
// printf("%ju", result_ptr->prime);
// printf("\n");
result_ptr->prime = rand32();
} while (!prime_test(result_ptr->prime, result_ptr->base));
return NULL;
@@ -123,7 +131,7 @@ typedef struct {
__int128 y;
} 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 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};
@@ -138,11 +146,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;
}
@@ -150,17 +158,18 @@ euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned _
__int128 x = k % 2 == 0 ? prev_xk : -prev_xk;
__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.y = y;
return res;
}
unsigned __int128 kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p, prime_test_t *q) {
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
unsigned __int128 M = p->prime * q->prime;
uint64_t M = (uint64_t)p->prime * q->prime;
uint64_t Mp = q->prime;
uint64_t Mq = p->prime;
@@ -180,44 +189,57 @@ unsigned __int128 kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p,
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;
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;
}
unsigned __int128 s1 = (c1 * y.x * Mp) % M;
unsigned __int128 s2 = (c2 * y.y * Mq) % M;
return (s1 + s2) % M;
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);
}
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");
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);
unsigned __int128 fi_n = (p->prime - 1) * (q->prime - 1);
printf("fi_n: ");
print_uint128(fi_n);
printf("\n");
uint64_t fi_n = (uint64_t)(p->prime - 1) * (q->prime - 1);
printf("n: %ju\n", fi_n);
// 2. kulcsgeneralas
uint64_t e = 0;
// keygen
uint64_t e = 65537;
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 = 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
calc_d.x %= fi_n;
calc_d.y %= fi_n;
uint64_t d;
if (calc_d.y < 0) {
d = fi_n - (uint64_t)(-calc_d.y);
} else {
d = (uint64_t)calc_d.y;
}
unsigned __int128 d = 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);
unsigned __int128 c = quick_pow(nyenye, *m, n, length);
uint64_t c = quick_pow(nyenye, *m, n, length);
free(nyenye);
printf("\nc: ");
@@ -226,9 +248,15 @@ 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");
printf("give input for m: ");
scanf("%ju", &m);
printf("\n");
srand(time(NULL));
@@ -244,13 +272,51 @@ int main() {
pthread_join(thread_q, NULL);
printf("\n");
rsa_encrypt(&m, &p, &q);
printf("p: %u\n", p.prime);
printf("q: %u\n", q.prime);
printf("\nkinai maradek tetel:\n");
unsigned __int128 S = kinai_maradek_tetel(&m, 2263, &p, &q);
printf("S: ");
print_uint128(S);
printf("\n");
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;
}