-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20fc88c
commit 710593d
Showing
4 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import subprocess | ||
import datetime | ||
import logging | ||
from gmpy2 import is_prime | ||
|
||
#done in dockerfile | ||
def compile_c_program(): | ||
result = subprocess.run(["gcc", "-o", "src/key_gen", "src/key_gen.c", "-lgmp"], capture_output=True, text=True) | ||
if result.returncode == 0: | ||
print("Compilation successful.") | ||
else: | ||
print(f"Compilation failed:\n{result.stderr}") | ||
exit(1) | ||
|
||
def run_c_program(): | ||
command = "./src/key_gen" | ||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
stdout, stderr = process.communicate() | ||
|
||
if stderr: | ||
print(f"Errors encountered:\n{stderr.decode('utf-8')}") | ||
return None, None | ||
|
||
output = stdout.decode('utf-8').strip().split('\n') | ||
output = list(output) | ||
return output[0],output[1] | ||
|
||
def get_prime_from_c(): | ||
while True: | ||
p, q = run_c_program() | ||
p = int(p) | ||
q = int(q) | ||
if is_prime(p) and is_prime(q) and p.bit_length() == 256 and q.bit_length() == 256: | ||
return p,q | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <gmp.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <unistd.h> // Needed for getpid() function | ||
#include <sys/time.h> // Needed for gettimeofday() function | ||
|
||
#define N 256 // Bit size for p and q | ||
|
||
void generate_random_prime(mpz_t prime, gmp_randstate_t state, mp_bitcnt_t bits) { | ||
do { | ||
mpz_urandomb(prime, state, bits); | ||
mpz_setbit(prime, bits - 1); // Ensure most significant bit is set for correct bit size | ||
mpz_nextprime(prime, prime); // Find the next prime number starting from prime | ||
} while (mpz_sizeinbase(prime, 2) != bits); // Ensure exactly N bits | ||
} | ||
|
||
int is_prime(mpz_t n, gmp_randstate_t state) { | ||
return mpz_probab_prime_p(n, 50); // Adjust certainty level as needed | ||
} | ||
|
||
void generate_primes(mpz_t p, mpz_t q, gmp_randstate_t state) { | ||
mpz_t six; | ||
mpz_init(six); | ||
mpz_set_ui(six, 6); | ||
|
||
int found = 0; | ||
|
||
while (!found) { | ||
// Generate random prime p | ||
generate_random_prime(p, state, N); | ||
|
||
// Calculate q = p + 6 | ||
mpz_add(q, p, six); | ||
|
||
// Check if q is prime and both p and q are exactly 256 bits | ||
if (is_prime(q, state) && is_prime(p, state) && mpz_sizeinbase(p, 2) == N && mpz_sizeinbase(q, 2) == N) { | ||
found = 1; | ||
} | ||
} | ||
|
||
mpz_clear(six); | ||
} | ||
|
||
unsigned long long get_seed() { | ||
struct timespec ts; | ||
clock_gettime(CLOCK_REALTIME, &ts); | ||
|
||
// Get current process ID | ||
pid_t pid = getpid(); | ||
|
||
// Mix the time and process ID | ||
unsigned long long seed = ts.tv_sec * 1000000000LL + ts.tv_nsec + pid; | ||
|
||
return seed; | ||
} | ||
|
||
int main() { | ||
// Initialize GMP random state | ||
gmp_randstate_t state; | ||
gmp_randinit_default(state); | ||
|
||
// Seed the random number generator with more randomness | ||
unsigned long long seed = get_seed(); | ||
gmp_randseed_ui(state, seed); | ||
|
||
mpz_t p, q; | ||
mpz_inits(p, q, NULL); | ||
|
||
// Generate random primes | ||
generate_primes(p, q, state); | ||
|
||
// Print the generated primes | ||
gmp_printf("%Zd\n", p); | ||
gmp_printf("%Zd\n", q); | ||
|
||
// Clear resources | ||
mpz_clears(p, q, NULL); | ||
gmp_randclear(state); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters