test.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <gcrypt.h>
#include <gmp.h>
#include <sys/random.h>
gmp_randstate_t state;
void *wrapper_gcry_alloc(size_t size);
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size);
void wrapper_gcry_free(void *ptr, size_t cur_size);
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
mpz_t key;
mpz_init(key);
clock_t start_random = clock();
size_t bytes = (n + 7) / 8; // bytes needed for n bits
unsigned char *buffer_key = (unsigned char *)malloc(bytes);
if (buffer_key == NULL) {
printf("Memory allocation failed.");
return -1;
}
getrandom(buffer_key, bytes, 0); // No flags needed
mpz_import(key, bytes, 1, 1, 0, 0, buffer_key);
free(buffer_key);
mpz_clear(key);
clock_t end_random = clock();
double time_random = ((double)(end_random - start_random)) / CLOCKS_PER_SEC;
printf("Time taken for random method: %f seconds\n", time_random);
return 0;
}
void *wrapper_gcry_alloc(size_t size) {
return malloc(size); // Changed to malloc
}
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size) {
return realloc(ptr, new_size); // Changed to realloc
}
void wrapper_gcry_free(void *ptr, size_t cur_size) {
free(ptr); // Changed to free
}
g++ -m64 -march=native -mtune=native -O3 test.cpp -o test $(libgcrypt-config --cflags --libs) -lgcrypt -lgmp
./test
Enter the value of n: 40
Time taken for random method: 0.000012 seconds
This is a very demanding one.
This code will loop through all numbers from 1 to 2^n - 1 and increment by 1 (optionally print each number)
test.cpp
#include <sys/random.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <gcrypt.h>
#include <gmp.h>
gmp_randstate_t state;
void *wrapper_gcry_alloc(size_t size);
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size);
void wrapper_gcry_free(void *ptr, size_t cur_size);
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
mpz_t key;
mpz_init(key);
clock_t start_random = clock();
size_t bytes = (n + 7) / 8;
unsigned char *buffer_key = (unsigned char *)malloc(bytes);
if (buffer_key == NULL) {
printf("Memory allocation failed.");
return -1;
}
getrandom(buffer_key, bytes, 0);
mpz_import(key, bytes, 1, 1, 0, 0, buffer_key);
free(buffer_key);
// Loop through all numbers from 1 to 2^n - 1 and add 1 to each number
mpz_t num;
mpz_init(num);
mpz_set_ui(num, 1);
mpz_t max_num;
mpz_init(max_num);
mpz_ui_pow_ui(max_num, 2, n);
mpz_sub_ui(max_num, max_num, 1);
while (mpz_cmp(num, max_num) <= 0) {
// Print the current number (num)
// gmp_printf("%Zd\n", num);
// Increment num by 1
mpz_add_ui(num, num, 1);
}
mpz_clear(num);
mpz_clear(max_num);
mpz_clear(key);
clock_t end_random = clock();
double time_random = ((double)(end_random - start_random)) / CLOCKS_PER_SEC;
printf("Time taken for random method: %f seconds\n", time_random);
return 0;
}
void *wrapper_gcry_alloc(size_t size) {
return malloc(size);
}
void *wrapper_gcry_realloc(void *ptr, size_t old_size, size_t new_size) {
return realloc(ptr, new_size);
}
void wrapper_gcry_free(void *ptr, size_t cur_size) {
free(ptr);
}
# ./test
Enter the value of n: 30
Time taken for random method: 6.051514 seconds