step 1 done

This commit is contained in:
2026-03-27 14:59:17 +01:00
parent 2442bf62bc
commit b7a4d6619c
2 changed files with 80 additions and 22 deletions

68
main.c
View File

@@ -1,4 +1,5 @@
#include <inttypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -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;
}