opti: remove many mallocs

This commit is contained in:
gbrochar 2024-02-18 16:38:30 +01:00
parent d368c925fb
commit 81f8fb3c1f
1 changed files with 32 additions and 33 deletions

View File

@ -27,14 +27,13 @@ void bigint_bitwise_left_shift(bigint_t n) {
n.data[0] <<= 1;
}
bigint_t assignable_bigint_bitwise_left_shift(bigint_t n) {
bigint_t result = bigint_clone(n);
void move_bigint_bitwise_left_shift(bigint_t n, bigint_t result) {
memcpy(result.data, n.data, n.len * sizeof(uint32_t));
size_t size = sizeof(uint32_t) * 8 - 1;
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[0] <<= 1;
return result;
}
// Will underflow
@ -88,38 +87,36 @@ int is_not_zero(bigint_t n) {
return 0;
}
void tool(bigint_t borrow, bigint_t *y, bigint_t a) {
while (is_not_zero(*y)) {
// TODO check opti
void bigint_substraction(bigint_t a, bigint_t b, bigint_t borrow, bigint_t y) {
//bigint_t borrow = bigint_clone(b);
//bigint_t y = bigint_clone(b);
memcpy(borrow.data, b.data, b.len * sizeof(uint32_t));
memcpy(y.data, b.data, b.len * sizeof(uint32_t));
while (is_not_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];
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);
}
// TODO check opti
void bigint_substraction(bigint_t a, bigint_t b) {
bigint_t borrow = bigint_clone(b);
bigint_t y = bigint_clone(b);
tool(borrow, &y, a);
bigint_destroy(y);
bigint_destroy(borrow);
}
// 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);
//bigint_t mod = bigint_clone(b);
/*if (a.len > b.len) {
printf("useless\n");
//bigint_destroy(mod);
mod = bigint_zero(a.len);
memcpy(mod.data, b.data, b.len * sizeof(uint32_t));
}
}*/
if (bigint_cmp(result, b) < 0) {
bigint_destroy(mod);
//bigint_destroy(mod);
return ;
}
bigint_bitwise_left_shift(mod);
@ -129,13 +126,13 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
}
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);
bigint_substraction(result, b, borrow_sub, y_sub);
}
bigint_destroy(mod);
//bigint_destroy(mod);
}
bigint_t bigint_new(size_t len) {
@ -222,7 +219,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 mod, bigint_t borrow_sub, bigint_t y_sub) {
bigint_set_zeros(result);
bigint_set_zeros(custom);
bigint_set_zeros(custom2);
@ -235,12 +232,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_modulo(custom, n, custom2);
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);
custom_bigint_modulo(custom, n, custom2);
custom_bigint_modulo(custom, n, custom2, mod, borrow_sub, y_sub);
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
}
cursor -= 1;
@ -270,12 +267,14 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two,
}
*/
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);
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;
@ -305,9 +304,9 @@ bigint_t bigint_prime(size_t len) {
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);
custom_bigint_pow_mod(a, d, n, x, custom, custom2, mod, borrow_sub, y_sub);
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, mod, borrow_sub, y_sub);
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);