diff --git a/helper.c b/helper.c index cef6815..83bdafc 100644 --- a/helper.c +++ b/helper.c @@ -1,5 +1,6 @@ #include #include +#include void print_uint128(unsigned __int128 n) { if (n == 0) { @@ -23,3 +24,36 @@ void print_uint128(unsigned __int128 n) { } putchar('\n'); } + +uint64_t rand64() { + uint64_t r = 0; + for (int i = 0; i < 4; i++) { + r = (r << 16) | (rand() & 0xFFFF); + } + 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(); + + // 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() { + // 1. Get the raw random bytes + unsigned __int128 candidate = generate_stitched_128(); + + // 2. Force the bottom bit to 1 (Ensures it is 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; +} diff --git a/main.c b/main.c index 431772d..0533db1 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -25,21 +26,19 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length) powed[0] = a; 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); - // printf("powed: %ju, index: %d; ", powed[i], (i)); + printf("powed: %ju, index: %d; ", powed[i], (i)); } // check where in the binary are ones uint64_t multiplied = 1; for (int i = 0; i < length; i++) { if (d_binary[i] == 1) { - // multiplied = (multiplied * powed[i]) % n; multiplied = (uint64_t)(((unsigned __int128)multiplied * powed[i]) % n); } } - // printf("\nbm quick math: %ju; %ju ", multiplied, n); + printf("\nbm quick math: %ju; %ju ", multiplied, n); free(powed); @@ -69,11 +68,11 @@ bool prime_test(uint64_t n, int a) { // round 1 // 1: a^d =k 1 mod n - uint64_t length; + uint64_t length = 0; uint64_t *d_binary = dec_to_bin(d, &length); - uint64_t first_qp_res = 0; + uint64_t 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); return true; } @@ -85,8 +84,10 @@ 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 = (first_qp_res * first_qp_res) % n; first_qp_res = (uint64_t)(((unsigned __int128)first_qp_res * first_qp_res) % n); } } @@ -95,23 +96,44 @@ bool prime_test(uint64_t n, int a) { return false; } +void *prime_thread_worker(void *arg) { + uint64_t *result_ptr = (uint64_t *)arg; + + do { + *result_ptr = rand64(); + printf("\nGenerating a new prime number (%p). Candidate: ", result_ptr); + printf("%ju", *result_ptr); + printf("\n"); + } while (!prime_test(*result_ptr, 2)); + + return NULL; +} + int main() { - prime_test(111, 5); - prime_test(29, 2); - prime_test(27, 2); - prime_test(17, 2); - prime_test(661, 2); - prime_test(18446744073709551557UL, 2); - prime_test(18446744073709551533UL, 3); + // prime_test(111, 5); + // prime_test(29, 2); + // prime_test(27, 2); + // prime_test(17, 2); + // prime_test(661, 2); + // prime_test(18446744073709551557UL, 2); + // prime_test(18446744073709551533UL, 3); - uint64_t p = 11; - uint64_t q = 29; + uint64_t p = 0; + uint64_t q = 0; + uint64_t base = 2; + pthread_t thread_p, thread_q; - // probably i should generate instead of asking the user to input - if (!prime_test(p, 2) || !prime_test(q, 2)) { - printf("given numbers were not primes"); - return 1; - } + // for (int i = 0; i < 10; i++) { + // p = rand64(); + // printf("%ju", p); + // prime_test(p, base); + // } + + 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"); @@ -123,5 +145,7 @@ int main() { print_uint128(fi_n); printf("\n"); + // 2. kulcsgeneralas + return 0; }