feat: check first primes before miller rabin

This commit is contained in:
gbrochar 2024-02-19 13:01:51 +01:00
parent 0bc6bf62a4
commit b4a0432d33
3 changed files with 53 additions and 16 deletions

View File

@ -79,20 +79,20 @@ int bigint_dif(bigint_t a, bigint_t b) {
return 0;
}
int is_not_zero(bigint_t n) {
int is_zero(bigint_t n) {
for (size_t i = 0; i < n.len; i++) {
if (n.data[i]) {
return 1;
return 0;
}
}
return 0;
return 1;
}
// TODO check opti
void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) {
//my_memcpy(borrow.data, b.data, b.len * sizeof(uint32_t));
my_memcpy(y.data, b.data, b.len * sizeof(uint32_t));
while (is_not_zero(y)) {
while (!is_zero(y)) {
for (size_t i = 0; i < a.len; i++) {
borrow.data[i] = ~a.data[i] & y.data[i];
a.data[i] = a.data[i] ^ y.data[i];
@ -117,7 +117,7 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod,
bigint_substraction(result, mod, borrow_sub, y_sub);
}
}
while (bigint_cmp(result, b) > 0) {
while (bigint_cmp(result, b) > -1) {
bigint_substraction(result, b, borrow_sub, y_sub);
}
}
@ -241,14 +241,26 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two,
bigint_destroy(n_minus_one);
}
bigint_t bigint_prime(size_t len) {
int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) {
//printf("testing %u\n", n.data[0]);
bigint_set_zeros(mod);
for (int i = 0; i < 3245; i++) {
mod.data[0] = primes[i].data[0];
custom_bigint_modulo(n, primes[i], custom2, mod, borrow_sub, y_sub);
//printf("i %d prime %d mod %d\n", i, primes[i].data[0], custom2.data[0]);
if (is_zero(custom2)) {
return 1;
}
}
return 0;
}
bigint_t bigint_prime(size_t len, bigint_t *primes) {
size_t my_size = len * 2;
bigint_t n = bigint_zero(my_size);
bigint_set_random_bytes(n, len);
bigint_set_msb_and_lsb_to_one(n, len);
bigint_t mod = bigint_clone(n);
bigint_t borrow_sub = bigint_clone(n);
bigint_t y_sub = bigint_clone(n);
@ -266,6 +278,14 @@ bigint_t bigint_prime(size_t len) {
bigint_t custom2 = bigint_zero(my_size);
bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t));
if (prime_division(primes, n, mod, custom2, borrow_sub, y_sub)) {
bulk_destroy(x, y, n, d, custom, custom2, mod, borrow_sub);
bigint_destroy(y_sub);
return bigint_prime(len, primes);
}
memcpy(mod.data, n.data, n.len * sizeof(uint32_t));
for (int i = 0; i < 32; i++) {
b_tool[i] = bigint_zero(my_size * 2);
}
@ -301,7 +321,7 @@ bigint_t bigint_prime(size_t len) {
bigint_destroy(b_tool[i]);
}
free(b_tool);
return bigint_prime(len);
return bigint_prime(len, primes);
}
bigint_destroy(x);
x = bigint_clone(y);
@ -318,7 +338,7 @@ bigint_t bigint_prime(size_t len) {
bigint_destroy(b_tool[i]);
}
free(b_tool);
return bigint_prime(len);
return bigint_prime(len, primes);
}
}
bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one);

View File

@ -1,15 +1,15 @@
#include "rsa.h"
rsa_t rsa_init(size_t len) {
rsa_t rsa_init(size_t len, bigint_t *primes) {
rsa_t rsa;
printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2);
//printf("Generating p...\n");
rsa.p = bigint_prime(len / 2);
rsa.p = bigint_prime(len / 2, primes);
printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]);
//printf("p = %u\n", rsa.p.data[0]);
//printf("Generating q...\n");
rsa.q = bigint_prime(len / 2);
rsa.q = bigint_prime(len / 2, primes);
printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]);
//printf("q = %u\n", rsa.q.data[0]);
@ -19,11 +19,28 @@ rsa_t rsa_init(size_t len) {
rsa_t rsa_generate_keys(size_t block_size) {
size_t len = block_size / sizeof(uint32_t) / 8;
rsa_t rsa = rsa_init(len);
bigint_t *primes = (bigint_t *)protected_malloc(3245 * sizeof(bigint_t));
for (int i = 0; i < 3245; i++) {
primes[i] = bigint_zero(len);
}
int fd = open("primes.0000", O_RDONLY);
char *buf = (char *)malloc(21290 * sizeof(char));
int ret = read(fd, buf, 21290);
char *tok = strtok(buf, "\n");
int i = 0;
while (tok) {
primes[i].data[0] = (uint32_t)atoi(tok);
tok = strtok(NULL, "\n");
i += 1;
}
printf("ret %d\n", ret);
rsa_t rsa = rsa_init(len, primes);
bigint_destroy(rsa.p);
bigint_destroy(rsa.q);
for (int i = 0; i < 18; i++) {
bigint_t p = bigint_prime(len / 2);
bigint_t p = bigint_prime(len / 2, primes);
printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]);
bigint_destroy(p);
}

View File

@ -9,7 +9,7 @@
#include <unistd.h>
#include <string.h>
#define RSA_BLOCK_SIZE 256
#define RSA_BLOCK_SIZE 256
typedef struct bigint_s {
uint32_t *data;
@ -33,7 +33,7 @@ void bigint_bitwise_left_shift(bigint_t n);
void bigint_bitwise_right_shift(bigint_t n);
void bigint_decrement(bigint_t n);
int64_t bigint_cmp(bigint_t a, bigint_t b);
bigint_t bigint_prime(size_t len);
bigint_t bigint_prime(size_t len, bigint_t *primes);
void bigint_print(bigint_t n);
bigint_t bigint_new(size_t len);
bigint_t bigint_zero(size_t len);