working prime test with 128bit nums

This commit is contained in:
2026-03-20 12:10:16 +01:00
parent 1eaae9ca4a
commit e0006f049c

75
main.c
View File

@@ -1,12 +1,11 @@
#include <inttypes.h> #include <inttypes.h>
#include <math.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>
int *dec_to_bin(int d, int *length) { uint64_t *dec_to_bin(uint64_t d, uint64_t *length) {
int *binary_form = calloc(20, sizeof(int)); uint64_t *binary_form = calloc(100, sizeof(uint64_t));
int index = 0; int index = 0;
while (d != 0) { while (d != 0) {
binary_form[index] = d % 2; binary_form[index] = d % 2;
@@ -17,41 +16,41 @@ int *dec_to_bin(int d, int *length) {
*length = index; *length = index;
for (int i = index; i >= 0; i--) { for (int i = index; i >= 0; i--) {
printf("%d", binary_form[i]); printf("%ju", binary_form[i]);
} }
printf(" index: %d\n", index); printf(" index: %d\n", index);
return binary_form; return binary_form;
} }
uint64_t quick_pow(int *d_binary, int a, int n, int length) { uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length) {
uint64_t *powed = calloc(20, sizeof(uint64_t)); uint64_t *powed = calloc(100, sizeof(uint64_t));
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] = (powed[i - 1] * powed[i - 1]) % n;
printf("powed: %ju, index: %d; ", powed[i], (i)); powed[i] = (uint64_t)(((unsigned __int128)powed[i - 1] * powed[i - 1]) % n);
// printf("powed: %ju, index: %d; ", powed[i], (i));
} }
// check where in the binary are ones // check where in the binary are ones
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 *= powed[i]; // multiplied = (multiplied * powed[i]) % n;
multiplied = (uint64_t)(((unsigned __int128)multiplied * powed[i]) % n);
} }
} }
printf("\nbm quick math: %ju; %d ", multiplied, n); // printf("\nbm quick math: %ju; %ju ", multiplied, n);
multiplied = multiplied % n;
printf("quick math: %ju", multiplied);
free(powed); free(powed);
return multiplied; return multiplied;
} }
bool prime_test(uint64_t n) { bool prime_test(uint64_t n, int a) {
printf("\n\nprime test: %jd\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
@@ -61,38 +60,56 @@ bool prime_test(uint64_t n) {
// S: see above // S: see above
// r = {0,... S-1} // r = {0,... S-1}
int a = 2; uint64_t d = n - 1;
int d = n - 1; uint64_t S = 0;
int S = 0;
int r = S - 1; // this stores the number of elements from 0 to S-1
while (d % 2 == 0) { while (d % 2 == 0) {
d = floor((double)d / 2.0); d = d / 2;
S++; S++;
printf("%d ", d); printf("%ju ", d);
} }
printf("S: %d", S); printf("S: %ju", S);
printf("\n"); printf("\n");
uint64_t r = S - 1; // this stores the number of elements from 0 to S-1
// round 1 // round 1
// 1: a^d =k 1 mod n // 1: a^d =k 1 mod n
int length; uint64_t length;
int *d_binary = dec_to_bin(d, &length); uint64_t *d_binary = dec_to_bin(d, &length);
uint64_t first_qp_res = 0;
if (quick_pow(d_binary, a, n, length) == 1) { if ((first_qp_res = quick_pow(d_binary, a, n, length)) == 1) {
free(d_binary); free(d_binary);
printf("true\n");
return true; return true;
} }
printf("\n\n"); // round 2
// 2: a^(d * 2^r) =k n-1 mod n
for (int i = 0; i <= r; i++) {
if (first_qp_res == n - 1) {
free(d_binary);
printf("true\n");
return true;
} 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);
}
}
printf("\nfalse\n\n");
free(d_binary); free(d_binary);
return false; return false;
} }
int main() { int main() {
prime_test(111); prime_test(111, 5);
prime_test(29); prime_test(29, 2);
prime_test(27); prime_test(27, 2);
prime_test(17, 2);
prime_test(661, 2);
prime_test(18446744073709551557UL, 2);
prime_test(18446744073709551533UL, 3);
return 0; return 0;
} }