refacto: speed x10
This commit is contained in:
parent
4a3cbd75b5
commit
7d0c774cb7
|
@ -13,7 +13,10 @@ $(NAME):
|
|||
gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME)
|
||||
|
||||
fast:
|
||||
gcc -Wall -Wextra -Werror Wunused-function -o3 $(SRC) -o $(NAME)
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -o3 $(SRC) -o $(NAME)
|
||||
|
||||
profile:
|
||||
gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
|
||||
|
||||
fclean:
|
||||
rm -rf $(NAME)
|
||||
|
|
46
rsa/bigint.c
46
rsa/bigint.c
|
@ -55,12 +55,20 @@ int bigint_cmp(bigint_t a, bigint_t b) {
|
|||
uint32_t size = sizeof(uint32_t) * 8;
|
||||
uint32_t acursor = size * a.len - 1;
|
||||
uint32_t bcursor = size * b.len - 1;
|
||||
while (acursor >= bcursor + size && a.data[acursor / size] == 0) {
|
||||
acursor -= size;
|
||||
}
|
||||
|
||||
while (acursor > bcursor) {
|
||||
if (a.data[acursor / size] & (1 << acursor % size)) {
|
||||
return 1;
|
||||
}
|
||||
acursor -= 1;
|
||||
}
|
||||
while (bcursor >= acursor + size && b.data[bcursor / size] == 0) {
|
||||
bcursor -= size;
|
||||
}
|
||||
|
||||
while (bcursor > acursor) {
|
||||
if (b.data[bcursor / size] & (1 << bcursor % size)) {
|
||||
return -1;
|
||||
|
@ -68,6 +76,9 @@ int bigint_cmp(bigint_t a, bigint_t b) {
|
|||
bcursor -= 1;
|
||||
}
|
||||
int cursor = acursor;
|
||||
while (cursor >= 0 && a.data[cursor / size] == b.data[cursor / size]) {
|
||||
cursor -= size;
|
||||
}
|
||||
while (cursor >= 0) {
|
||||
uint32_t abit = a.data[cursor / size] & (1 << (cursor % size));
|
||||
uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size));
|
||||
|
@ -177,20 +188,20 @@ void bigint_destroy(bigint_t n) {
|
|||
n.data = NULL;
|
||||
}
|
||||
|
||||
void bigint_add(bigint_t a, bigint_t b) {
|
||||
bigint_t result = bigint_zero(a.len);
|
||||
size_t size = sizeof(uint32_t) * 8;
|
||||
size_t width = a.len * size;
|
||||
void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) {
|
||||
//bigint_t result = bigint_zero(a.len);
|
||||
bigint_set_zeros(result);
|
||||
//size_t size = sizeof(uint32_t) * 8;
|
||||
//size_t width = a.len * size;
|
||||
uint32_t carriage = 0;
|
||||
|
||||
for (size_t cursor = 0; cursor < width; cursor++) {
|
||||
uint32_t a_bit = a.data[cursor / size] >> (cursor % size) & 1;
|
||||
uint32_t b_bit = b.data[cursor / size] >> (cursor % size) & 1;
|
||||
result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size);
|
||||
carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage);
|
||||
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));
|
||||
carriage = tmp >> 32;
|
||||
}
|
||||
memcpy(a.data, result.data, a.len * sizeof(uint32_t));
|
||||
bigint_destroy(result);
|
||||
//bigint_destroy(result);
|
||||
}
|
||||
|
||||
void bigint_set_zeros(bigint_t n) {
|
||||
|
@ -199,7 +210,7 @@ void bigint_set_zeros(bigint_t n) {
|
|||
}
|
||||
}
|
||||
|
||||
void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) {
|
||||
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);
|
||||
size_t size = sizeof(uint32_t) * 8;
|
||||
|
@ -212,7 +223,7 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) {
|
|||
for (size_t i = 0; i < cursor; i++) {
|
||||
bigint_bitwise_left_shift(b_tool);
|
||||
}
|
||||
bigint_add(result, b_tool);
|
||||
custom_bigint_add(result, b_tool, custom);
|
||||
}
|
||||
}
|
||||
bigint_destroy(b_tool);
|
||||
|
@ -220,7 +231,7 @@ void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) {
|
|||
|
||||
// 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) {
|
||||
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) {
|
||||
bigint_set_zeros(result);
|
||||
bigint_set_zeros(custom);
|
||||
bigint_set_zeros(custom2);
|
||||
|
@ -232,12 +243,12 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result,
|
|||
}
|
||||
cursor--;
|
||||
while (cursor >= 0) {
|
||||
custom_bigint_mul(result, result, custom);
|
||||
custom_bigint_mul(result, result, custom, custom3);
|
||||
custom_bigint_modulo(custom, n, custom2);
|
||||
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);
|
||||
custom_bigint_mul(result, a, custom, custom3);
|
||||
custom_bigint_modulo(custom, n, custom2);
|
||||
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
|
||||
}
|
||||
|
@ -285,6 +296,7 @@ bigint_t bigint_prime(size_t len) {
|
|||
bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
||||
bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
||||
bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
||||
bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
|
||||
|
||||
bigint_t two = bigint_zero(len);
|
||||
two.data[0] = 2;
|
||||
|
@ -301,9 +313,9 @@ bigint_t bigint_prime(size_t len) {
|
|||
while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) {
|
||||
bigint_set_random_bytes(a);
|
||||
}
|
||||
custom_bigint_pow_mod(a, d, n, x, custom, custom2);
|
||||
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);
|
||||
custom_bigint_pow_mod(x, two, n, y, custom, custom2, custom3);
|
||||
if (bigint_cmp(y, one) == 0 && bigint_cmp(x, one) != 0 && bigint_cmp(x, n_minus_one) != 0) {
|
||||
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
|
||||
bigint_destroy(custom);
|
||||
|
|
Loading…
Reference in New Issue