|
|
|
@ -1,8 +1,14 @@
|
|
|
|
|
#include "rsa.h"
|
|
|
|
|
|
|
|
|
|
void my_memcpy(void *dst, void *src, size_t n) {
|
|
|
|
|
memcpy(dst, src, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_set_random_bytes(bigint_t n, size_t len) {
|
|
|
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
|
read(fd, n.data, len * sizeof(uint32_t));
|
|
|
|
|
if (read(fd, n.data, len * sizeof(uint32_t)) == -1) {
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -12,38 +18,32 @@ void bigint_set_msb_and_lsb_to_one(bigint_t n, size_t len) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_bitwise_right_shift(bigint_t n) {
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
|
|
|
for (size_t i = 0; i < n.len - 1; i++) {
|
|
|
|
|
n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << size;
|
|
|
|
|
n.data[i] = n.data[i] >> 1 | (n.data[i + 1] & 1) << 31;
|
|
|
|
|
}
|
|
|
|
|
n.data[n.len - 1] >>= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_bitwise_left_shift(bigint_t n) {
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
|
|
|
for (int i = n.len - 1; i > 0; i--) {
|
|
|
|
|
n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << size)) >> size);
|
|
|
|
|
n.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31);
|
|
|
|
|
}
|
|
|
|
|
n.data[0] <<= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bigint_t assignable_bigint_bitwise_left_shift(bigint_t n) {
|
|
|
|
|
bigint_t result = bigint_clone(n);
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
|
|
|
void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) {
|
|
|
|
|
for (int i = result.len - 1; i > 0; i--) {
|
|
|
|
|
result.data[i] = result.data[i] << 1 | ((result.data[i - 1] & (1 << size)) >> size);
|
|
|
|
|
result.data[i] = n.data[i] << 1 | ((n.data[i - 1] & (1 << 31)) >> 31);
|
|
|
|
|
}
|
|
|
|
|
result.data[0] <<= 1;
|
|
|
|
|
return result;
|
|
|
|
|
result.data[0] = n.data[0] << 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Will underflow
|
|
|
|
|
void bigint_decrement(bigint_t n) {
|
|
|
|
|
size_t cursor = 0;
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
|
|
|
while (cursor < size * n.len) {
|
|
|
|
|
n.data[cursor / size] = n.data[cursor / size] ^ (1 << (cursor % size));
|
|
|
|
|
if (((n.data[cursor / size] >> (cursor % size)) & 1) == 0) {
|
|
|
|
|
while (cursor < n.len << 5) {
|
|
|
|
|
n.data[cursor >> 32] = n.data[cursor >> 5] ^ (1 << (cursor % 32));
|
|
|
|
|
if (((n.data[cursor >> 5] >> (cursor % 32)) & 1) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cursor += 1;
|
|
|
|
@ -68,62 +68,56 @@ int64_t bigint_cmp(bigint_t a, bigint_t b) {
|
|
|
|
|
|
|
|
|
|
// TODO refactor/clean assume same length ?
|
|
|
|
|
int bigint_dif(bigint_t a, bigint_t b) {
|
|
|
|
|
int cursor = a.len - 1;
|
|
|
|
|
while (cursor >= 0) {
|
|
|
|
|
int cursor = a.len;
|
|
|
|
|
while (--cursor >= 0) {
|
|
|
|
|
if (a.data[cursor] ^ b.data[cursor]) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
cursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int is_zero(bigint_t n) {
|
|
|
|
|
for (size_t i = 0; i < n.len; i++) {
|
|
|
|
|
if (n.data[i]) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO check opti
|
|
|
|
|
void bigint_substraction(bigint_t a, bigint_t b) {
|
|
|
|
|
bigint_t borrow = bigint_clone(b);
|
|
|
|
|
bigint_t y = bigint_clone(b);
|
|
|
|
|
bigint_t zero = bigint_zero(a.len);
|
|
|
|
|
while (bigint_dif(borrow, zero)) {
|
|
|
|
|
void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) {
|
|
|
|
|
my_memcpy(y.data, b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(y);
|
|
|
|
|
y = assignable_bigint_bitwise_left_shift(borrow);
|
|
|
|
|
move_bigint_bitwise_left_shift(borrow, y);
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(y);
|
|
|
|
|
bigint_destroy(borrow);
|
|
|
|
|
bigint_destroy(zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO check opti
|
|
|
|
|
void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
|
|
|
|
|
void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub) {
|
|
|
|
|
bigint_set_zeros(result);
|
|
|
|
|
memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
|
|
|
bigint_t mod = bigint_clone(b);
|
|
|
|
|
if (a.len > b.len) {
|
|
|
|
|
bigint_destroy(mod);
|
|
|
|
|
mod = bigint_zero(a.len);
|
|
|
|
|
memcpy(mod.data, b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
}
|
|
|
|
|
my_memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
|
|
|
if (bigint_cmp(result, b) < 0) {
|
|
|
|
|
bigint_destroy(mod);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
bigint_bitwise_left_shift(mod);
|
|
|
|
|
while (bigint_cmp(b, mod) < 0) {
|
|
|
|
|
while (bigint_cmp(result, mod) > 0) {
|
|
|
|
|
bigint_bitwise_left_shift(mod);
|
|
|
|
|
}
|
|
|
|
|
while (bigint_cmp(b, mod) < 0) {
|
|
|
|
|
bigint_bitwise_right_shift(mod);
|
|
|
|
|
if (bigint_cmp(result, mod) > 0) {
|
|
|
|
|
bigint_substraction(result, mod);
|
|
|
|
|
bigint_substraction(result, mod, borrow_sub, y_sub);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (bigint_cmp(result, b) > 0) {
|
|
|
|
|
bigint_substraction(result, b);
|
|
|
|
|
while (bigint_cmp(result, b) > -1) {
|
|
|
|
|
bigint_substraction(result, b, borrow_sub, y_sub);
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(mod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bigint_t bigint_new(size_t len) {
|
|
|
|
@ -151,7 +145,7 @@ bigint_t bigint_clone(bigint_t src) {
|
|
|
|
|
|
|
|
|
|
dst.len = src.len;
|
|
|
|
|
dst.data = (uint32_t *)protected_malloc(src.len * sizeof(uint32_t));
|
|
|
|
|
memcpy(dst.data, src.data, src.len * sizeof(uint32_t));
|
|
|
|
|
my_memcpy(dst.data, src.data, src.len * sizeof(uint32_t));
|
|
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
@ -161,16 +155,17 @@ void bigint_destroy(bigint_t n) {
|
|
|
|
|
n.data = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) {
|
|
|
|
|
bigint_set_zeros(result);
|
|
|
|
|
uint32_t carriage = 0;
|
|
|
|
|
void custom_bigint_add(bigint_t a, bigint_t b, int index) {
|
|
|
|
|
uint64_t carriage = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t cursor = 0; cursor < a.len; cursor++) {
|
|
|
|
|
uint64_t tmp = (uint64_t)a.data[cursor] + (uint64_t)b.data[cursor] + carriage;
|
|
|
|
|
memcpy(result.data + cursor, &tmp, sizeof(uint32_t));
|
|
|
|
|
uint64_t tmp = (uint64_t)a.data[cursor] + carriage;
|
|
|
|
|
if ((int)cursor - index >= 0) {
|
|
|
|
|
tmp += (uint64_t)b.data[cursor - index];
|
|
|
|
|
}
|
|
|
|
|
a.data[cursor] = (uint32_t)tmp;
|
|
|
|
|
carriage = tmp >> 32;
|
|
|
|
|
}
|
|
|
|
|
memcpy(a.data, result.data, a.len * sizeof(uint32_t));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bigint_set_zeros(bigint_t n) {
|
|
|
|
@ -179,50 +174,48 @@ void bigint_set_zeros(bigint_t n) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t custom) {
|
|
|
|
|
//bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
|
|
|
|
bigint_t b_tool = bigint_zero(a.len + b.len);
|
|
|
|
|
//memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
|
|
|
int width = a.len * size;
|
|
|
|
|
void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t *b_tool) {
|
|
|
|
|
int width = a.len * 32;
|
|
|
|
|
bigint_set_zeros(result);
|
|
|
|
|
bigint_set_zeros(b_tool[0]);
|
|
|
|
|
my_memcpy(b_tool[0].data, b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < 32; i++) {
|
|
|
|
|
bigint_set_zeros(b_tool[i]);
|
|
|
|
|
my_memcpy(b_tool[i].data, b_tool[i - 1].data, b.len * sizeof(uint32_t));
|
|
|
|
|
bigint_bitwise_left_shift(b_tool[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int cursor = 0; cursor < width; cursor++) {
|
|
|
|
|
if (a.data[cursor >> 5] >> (cursor % 32) & 1) {
|
|
|
|
|
bigint_set_zeros(b_tool);
|
|
|
|
|
memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t));
|
|
|
|
|
int i = cursor - cursor % 32;
|
|
|
|
|
while (i < cursor) {
|
|
|
|
|
bigint_bitwise_left_shift(b_tool);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
custom_bigint_add(result, b_tool, custom);
|
|
|
|
|
int offset = cursor % 32;
|
|
|
|
|
int index = cursor >> 5;
|
|
|
|
|
if (a.data[index] >> offset & 1) {
|
|
|
|
|
custom_bigint_add(result, b_tool[offset], index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(b_tool);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a^e mod n
|
|
|
|
|
// clean memory tricks !!!
|
|
|
|
|
void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t custom3) {
|
|
|
|
|
void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result, bigint_t custom, bigint_t custom2, bigint_t mod, bigint_t borrow_sub, bigint_t y_sub, bigint_t *b_tool) {
|
|
|
|
|
bigint_set_zeros(result);
|
|
|
|
|
bigint_set_zeros(custom);
|
|
|
|
|
bigint_set_zeros(custom2);
|
|
|
|
|
memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
|
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
|
|
|
int cursor = e.len * size - 1;
|
|
|
|
|
while (!(e.data[cursor / 32] & 1 << (cursor % 32))) {
|
|
|
|
|
my_memcpy(result.data, a.data, a.len * sizeof(uint32_t));
|
|
|
|
|
int cursor = (e.len << 5) - 1;
|
|
|
|
|
while (!(e.data[cursor >> 5] & 1 << (cursor % 32))) {
|
|
|
|
|
cursor--;
|
|
|
|
|
}
|
|
|
|
|
cursor--;
|
|
|
|
|
while (cursor >= 0) {
|
|
|
|
|
custom_bigint_mul(result, result, custom, custom3);
|
|
|
|
|
custom_bigint_modulo(custom, n, custom2);
|
|
|
|
|
custom_bigint_mul(result, result, custom, b_tool);
|
|
|
|
|
custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub);
|
|
|
|
|
bigint_set_zeros(result);
|
|
|
|
|
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
|
|
|
if (e.data[cursor / 32] & 1 << (cursor % 32)) {
|
|
|
|
|
custom_bigint_mul(result, a, custom, custom3);
|
|
|
|
|
custom_bigint_modulo(custom, n, custom2);
|
|
|
|
|
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
|
|
|
my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
|
|
|
if (e.data[cursor >> 5] & 1 << (cursor % 32)) {
|
|
|
|
|
custom_bigint_mul(result, a, custom, b_tool);
|
|
|
|
|
custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub);
|
|
|
|
|
my_memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
|
|
|
|
}
|
|
|
|
|
cursor -= 1;
|
|
|
|
|
}
|
|
|
|
@ -245,18 +238,51 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two,
|
|
|
|
|
bigint_destroy(n_minus_two);
|
|
|
|
|
bigint_destroy(n_minus_one);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
bigint_t bigint_random_range(bigint_t low, bigint_t high) {
|
|
|
|
|
|
|
|
|
|
int prime_division(bigint_t *primes, bigint_t n, bigint_t mod, bigint_t custom2, bigint_t borrow_sub, bigint_t y_sub) {
|
|
|
|
|
bigint_set_zeros(mod);
|
|
|
|
|
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);
|
|
|
|
|
if (is_zero(custom2)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
bigint_t bigint_prime(size_t len) {
|
|
|
|
|
//bigint_t n = bigint_zero(len);
|
|
|
|
|
bigint_t n = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
bigint_t d = bigint_clone(n);
|
|
|
|
|
d.data[0] -= 1;
|
|
|
|
|
uint32_t s = 0;
|
|
|
|
@ -265,14 +291,26 @@ bigint_t bigint_prime(size_t len) {
|
|
|
|
|
s += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t x = bigint_zero(my_size);
|
|
|
|
|
bigint_t y = bigint_zero(my_size);
|
|
|
|
|
bigint_t custom = bigint_zero(my_size);
|
|
|
|
|
bigint_t custom2 = bigint_zero(my_size);
|
|
|
|
|
|
|
|
|
|
bigint_t two = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
bigint_t one = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
one.data[0] = 1;
|
|
|
|
|
bigint_t n_minus_two = bigint_clone(n);
|
|
|
|
@ -280,40 +318,25 @@ bigint_t bigint_prime(size_t len) {
|
|
|
|
|
n_minus_two.data[0] -= 1;
|
|
|
|
|
n_minus_one.data[0] -= 1;
|
|
|
|
|
bigint_decrement(n_minus_two);
|
|
|
|
|
//bigint_t a = bigint_zero(len);
|
|
|
|
|
bigint_t a = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2);
|
|
|
|
|
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, custom3);
|
|
|
|
|
for (uint32_t i = 0; i < s; i++) {
|
|
|
|
|
custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3);
|
|
|
|
|
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(custom3);
|
|
|
|
|
bigint_destroy(a);
|
|
|
|
|
return bigint_prime(len);
|
|
|
|
|
}
|
|
|
|
|
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(custom3);
|
|
|
|
|
bigint_destroy(a);
|
|
|
|
|
return bigint_prime(len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bigint_t a = bigint_zero(my_size);
|
|
|
|
|
|
|
|
|
|
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(custom3);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (is_prime) {
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
bigint_destroy(n);
|
|
|
|
|
return bigint_prime(len, primes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|