diff --git a/rsa/Makefile b/rsa/Makefile index 1d3819d..80cf2bf 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -18,9 +18,15 @@ fast: profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) +profile-clang: + clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) + profile-fast: gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) +profile-fast-clang: + clang -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index cfae50d..6d75a7d 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -68,29 +68,35 @@ 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; + //cursor -= 1; } return 0; } + +void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { + while (bigint_dif(*y, zero)) { + 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); + } + +} + // 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)) { - 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); - } + tool(borrow, &y, zero, a); bigint_destroy(y); bigint_destroy(borrow); bigint_destroy(zero);