clean: miller-rabin sep fn + set e=65537
This commit is contained in:
parent
b4a0432d33
commit
ee899b8c8c
|
@ -15,6 +15,15 @@ $(NAME):
|
|||
fast:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -O3 $(SRC) -o $(NAME)
|
||||
|
||||
fast-info:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -O3 -fopt-info $(SRC) -o $(NAME)
|
||||
|
||||
really-fast:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native $(SRC) -o $(NAME)
|
||||
|
||||
really-fast-info:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -O3 -march=native -fopt-info $(SRC) -o $(NAME)
|
||||
|
||||
profile:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
|
||||
|
||||
|
|
82
rsa/bigint.c
82
rsa/bigint.c
|
@ -32,7 +32,6 @@ void bigint_bitwise_left_shift(bigint_t n) {
|
|||
}
|
||||
|
||||
void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) {
|
||||
// my_memcpy(result.data, n.data, n.len * sizeof(uint32_t));
|
||||
for (int i = result.len - 1; i > 0; i--) {
|
||||
result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31);
|
||||
}
|
||||
|
@ -90,7 +89,6 @@ int is_zero(bigint_t n) {
|
|||
|
||||
// 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_zero(y)) {
|
||||
for (size_t i = 0; i < a.len; i++) {
|
||||
|
@ -242,18 +240,39 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two,
|
|||
}
|
||||
|
||||
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++) {
|
||||
for (int i = 0; i < 200; 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;
|
||||
}
|
||||
|
||||
int miller_rabin(size_t len, bigint_t a, bigint_t two, bigint_t n_minus_two, bigint_t d, bigint_t n, bigint_t x, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool, bigint_t n_minus_one, uint32_t s, bigint_t y, bigint_t one) {
|
||||
for (uint32_t k = 0; k < 20; k++) {
|
||||
bigint_set_zeros(a);
|
||||
while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) {
|
||||
bigint_set_random_bytes(a, len);
|
||||
}
|
||||
custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool);
|
||||
for (uint32_t i = 0; i < s; i++) {
|
||||
custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool);
|
||||
if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) {
|
||||
return 0;
|
||||
}
|
||||
bigint_destroy(x);
|
||||
x = bigint_clone(y);
|
||||
}
|
||||
if (bigint_dif(y, one)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
bigint_t bigint_prime(size_t len, bigint_t *primes) {
|
||||
size_t my_size = len * 2;
|
||||
|
||||
|
@ -276,7 +295,6 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) {
|
|||
bigint_t y = bigint_zero(my_size);
|
||||
bigint_t custom = bigint_zero(my_size);
|
||||
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)) {
|
||||
|
@ -286,11 +304,11 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) {
|
|||
}
|
||||
memcpy(mod.data, n.data, n.len * sizeof(uint32_t));
|
||||
|
||||
bigint_t *b_tool = (bigint_t *)protected_malloc(32 * sizeof(bigint_t));
|
||||
for (int i = 0; i < 32; i++) {
|
||||
b_tool[i] = bigint_zero(my_size * 2);
|
||||
}
|
||||
|
||||
|
||||
bigint_t two = bigint_zero(my_size);
|
||||
bigint_t one = bigint_zero(my_size);
|
||||
two.data[0] = 2;
|
||||
|
@ -301,46 +319,9 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) {
|
|||
n_minus_one.data[0] -= 1;
|
||||
bigint_decrement(n_minus_two);
|
||||
bigint_t a = bigint_zero(my_size);
|
||||
for (uint32_t k = 0; k < 128; k++) {
|
||||
bigint_set_zeros(a);
|
||||
while (bigint_cmp(a, two) < 0 || bigint_cmp(a, n_minus_two) > 0) {
|
||||
bigint_set_random_bytes(a, len);
|
||||
}
|
||||
custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool);
|
||||
for (uint32_t i = 0; i < s; i++) {
|
||||
custom_bigint_pow_mod(x, two, n, y, custom, custom2, mod, borrow_sub, y_sub, b_tool);
|
||||
if (!bigint_dif(y, one) && bigint_dif(x, one) && bigint_dif(x, n_minus_one)) {
|
||||
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
|
||||
bigint_destroy(custom);
|
||||
bigint_destroy(custom2);
|
||||
bigint_destroy(a);
|
||||
bigint_destroy(mod);
|
||||
bigint_destroy(borrow_sub);
|
||||
bigint_destroy(y_sub);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
bigint_destroy(b_tool[i]);
|
||||
}
|
||||
free(b_tool);
|
||||
return bigint_prime(len, primes);
|
||||
}
|
||||
bigint_destroy(x);
|
||||
x = bigint_clone(y);
|
||||
}
|
||||
if (bigint_dif(y, one)) {
|
||||
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
|
||||
bigint_destroy(custom);
|
||||
bigint_destroy(custom2);
|
||||
bigint_destroy(a);
|
||||
bigint_destroy(mod);
|
||||
bigint_destroy(borrow_sub);
|
||||
bigint_destroy(y_sub);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
bigint_destroy(b_tool[i]);
|
||||
}
|
||||
free(b_tool);
|
||||
return bigint_prime(len, primes);
|
||||
}
|
||||
}
|
||||
|
||||
int is_prime = miller_rabin(len, a, two, n_minus_two, d, n, x, custom, custom2, mod, borrow_sub, y_sub, b_tool, n_minus_one, s, y, one);
|
||||
|
||||
bulk_destroy(x, y, custom, d, two, one, n_minus_two, n_minus_one);
|
||||
bigint_destroy(custom2);
|
||||
bigint_destroy(a);
|
||||
|
@ -351,6 +332,11 @@ bigint_t bigint_prime(size_t len, bigint_t *primes) {
|
|||
bigint_destroy(b_tool[i]);
|
||||
}
|
||||
free(b_tool);
|
||||
return n;
|
||||
|
||||
if (is_prime) {
|
||||
return n;
|
||||
}
|
||||
bigint_destroy(n);
|
||||
return bigint_prime(len, primes);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,17 +33,13 @@ rsa_t rsa_generate_keys(size_t block_size) {
|
|||
tok = strtok(NULL, "\n");
|
||||
i += 1;
|
||||
}
|
||||
primes[0].data[0] = 65537;
|
||||
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, primes);
|
||||
printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]);
|
||||
bigint_destroy(p);
|
||||
}
|
||||
|
||||
return rsa;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue