remove unused func and comments

This commit is contained in:
gbrochar 2024-02-16 16:48:25 +01:00
parent 6806db1c6f
commit 52f65086c2
3 changed files with 40 additions and 159 deletions

View File

@ -10,10 +10,10 @@ SRC = \
all: $(NAME)
$(NAME):
gcc -Wall -Wextra -Werror $(SRC) -o $(NAME)
gcc -Wall -Wextra -Werror -Wunused-function $(SRC) -o $(NAME)
fast:
gcc -Wall -Wextra -Werror -o3 $(SRC) -o $(NAME)
gcc -Wall -Wextra -Werror Wunused-function -o3 $(SRC) -o $(NAME)
fclean:
rm -rf $(NAME)

View File

@ -135,35 +135,6 @@ void bigint_substraction(bigint_t a, bigint_t bb) {
bigint_destroy(zero);
}
// TODO check opti
bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b) {
bigint_t result = bigint_clone(a);
bigint_t mod = bigint_clone(b);
if (a.len > b.len) {
mod = bigint_zero(a.len);
memcpy(mod.data, b.data, b.len * sizeof(uint32_t));
}
if (bigint_cmp(result, b) == -1) {
bigint_destroy(mod);
return result;
}
bigint_bitwise_left_shift(mod);
while (bigint_cmp(b, mod) == -1) {
while (bigint_cmp(result, mod) == 1) {
bigint_bitwise_left_shift(mod);
}
bigint_bitwise_right_shift(mod);
if (bigint_cmp(result, mod) == 1) {
bigint_substraction(result, mod);
}
}
while (bigint_cmp(result, b) == 1) {
bigint_substraction(result, b);
}
bigint_destroy(mod);
return result;
}
// TODO check opti
void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
bigint_set_zeros(result);
@ -193,6 +164,41 @@ void custom_bigint_modulo(bigint_t a, bigint_t b, bigint_t result) {
bigint_destroy(mod);
}
bigint_t bigint_new(size_t len) {
bigint_t bigint;
bigint.len = len;
bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t));
return bigint;
}
bigint_t bigint_zero(size_t len) {
bigint_t bigint;
bigint = bigint_new(len);
for (size_t i = 0; i < len; i++) {
bigint.data[i] = 0;
}
return bigint;
}
bigint_t bigint_clone(bigint_t src) {
bigint_t dst;
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));
return dst;
}
void bigint_destroy(bigint_t n) {
free(n.data);
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;
@ -205,6 +211,7 @@ void bigint_add(bigint_t a, bigint_t b) {
result.data[cursor / size] |= (a_bit ^ b_bit ^ carriage) << (cursor % size);
carriage = (a_bit & b_bit) | ((a_bit ^ b_bit) & carriage);
}
// memcpy(a.data, result.data, a.len * sizeof(uint32_t));
bigint_destroy(a);
a = bigint_clone(result);
bigint_destroy(result);
@ -216,28 +223,9 @@ void bigint_set_zeros(bigint_t n) {
}
}
bigint_t assignable_bigint_mul(bigint_t a, bigint_t b) {
bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
size_t size = sizeof(uint32_t) * 8;
size_t width = a.len * size;
for (size_t cursor = 0; cursor < width; cursor++) {
if (a.data[cursor / 32] >> (cursor % 32) & 1) {
bigint_set_zeros(b_tool);
memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t));
for (size_t i = 0; i < cursor; i++) {
bigint_bitwise_left_shift(b_tool);
}
bigint_add(result, b_tool);
}
}
bigint_destroy(b_tool);
return result;
}
void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result) {
bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
//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;
size_t width = a.len * size;
bigint_set_zeros(result);
@ -280,35 +268,6 @@ void custom_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n, bigint_t result,
cursor -= 1;
}
}
// a^e mod n
// clean memory tricks !!!
bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n) {
bigint_t result = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
memcpy(result.data, a.data, a.len * sizeof(uint32_t));
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);
size_t size = sizeof(uint32_t) * 8;
int cursor = e.len * size - 1;
while (!(e.data[cursor / 32] & 1 << (cursor % 32))) {
cursor--;
}
cursor--;
while (cursor >= 0) {
custom_bigint_mul(result, result, custom);
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_modulo(custom, n, custom2);
memcpy(result.data, custom2.data, custom2.len * sizeof(uint32_t));
}
cursor -= 1;
}
bigint_destroy(custom);
bigint_destroy(custom2);
return result;
}
void bigint_print(bigint_t n) {
for (int i = n.len - 1; i >= 0; i--) {
@ -316,15 +275,6 @@ void bigint_print(bigint_t n) {
}
}
bigint_t bigint_clone(bigint_t src) {
bigint_t dst;
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));
return dst;
}
void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t one, bigint_t n_minus_two, bigint_t n_minus_one) {
bigint_destroy(x);
@ -346,8 +296,6 @@ bigint_t bigint_prime(size_t len) {
bigint_set_random_bytes(n);
bigint_set_msb_and_lsb_to_one(n);
// printf("msb and lsb set to one N IS: \n");
// bigint_print(n);
bigint_t d = bigint_clone(n);
d.data[0] -= 1;
@ -356,8 +304,6 @@ bigint_t bigint_prime(size_t len) {
bigint_bitwise_right_shift(d);
s += 1;
}
// printf("D IS:\n");
// bigint_print(d);
bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4);
@ -374,73 +320,29 @@ bigint_t bigint_prime(size_t len) {
n_minus_one.data[0] -= 1;
bigint_decrement(n_minus_two);
bigint_t a = bigint_zero(len);
// printf("s is %d\n", s);
for (uint32_t k = 0; k < 128; k++) {
// printf("this is good %d\n", k);
//bigint_t a = bigint_zero(len);
bigint_set_zeros(a);
while (bigint_cmp(a, two) == -1 || bigint_cmp(a, n_minus_two) == 1) {
bigint_set_random_bytes(a);
}
//printf("A IS: \n");
//bigint_print(a);
//printf("a %d\n", k);
//bigint_t x = custom_bigint_pow_mod(a, d, n, x, custom, custom2);
custom_bigint_pow_mod(a, d, n, x, custom, custom2);
//printf("b %d\n", k);
for (uint32_t i = 0; i < s; i++) {
//bigint_destroy(y);
custom_bigint_pow_mod(x, two, n, y, custom, custom2);
//y = assignable_bigint_pow_mod(x, two, n);
//printf("X IS: \n");
//bigint_print(x);
//printf("Y IS: \n");
//bigint_print(y);
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(a);
// printf("failed first test\n");
return bigint_prime(len);
}
bigint_destroy(x);
x = bigint_clone(y);
}
if (bigint_cmp(y, one) != 0) {
// printf("y is equal to %d and %d", y.data[1], y.data[0]);
// printf("failed second test\n");
bulk_destroy(x, y, n, d, two, one, n_minus_two, n_minus_one);
bigint_destroy(a);
return bigint_prime(len);
}
}
bigint_destroy(a);
// for (int i = 0; i < 10000; i++) {
// printf("CHU A MON PRIME!!!\n");
// }
return n;
}
bigint_t bigint_new(size_t len) {
bigint_t bigint;
bigint.len = len;
bigint.data = (uint32_t *)protected_malloc(len * sizeof(uint32_t));
return bigint;
}
bigint_t bigint_zero(size_t len) {
bigint_t bigint;
bigint = bigint_new(len);
for (size_t i = 0; i < len; i++) {
bigint.data[i] = 0;
}
return bigint;
}
void bigint_destroy(bigint_t n) {
free(n.data);
n.data = NULL;
}

View File

@ -19,26 +19,5 @@ 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 a = bigint_zero(4);
bigint_t b = bigint_zero(4);
a.data[0] = 1234567890;
b.data[0] = 234567;
bigint_t result = assignable_bigint_modulo(a, b);
printf("result is %ud\n", result.data[0]);
bigint_destroy(a);
a = bigint_clone(result);
b.data[0] = 5764;
printf("length\na: %lu e: %lu n: %lu\n", result.len, a.len, b.len);
bigint_t result2 = assignable_bigint_pow_mod(result, a, b);
bigint_destroy(a);
bigint_destroy(b);
bigint_destroy(result);
printf("bigpowmod is %u \n", result2.data[0]);
bigint_destroy(result2);
return rsa;
}