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

View File

@@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
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;
}