fix: substraction
This commit is contained in:
parent
c8e5e6bf67
commit
2f7d2922c7
|
@ -18,9 +18,15 @@ fast:
|
||||||
profile:
|
profile:
|
||||||
gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
|
gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
|
||||||
|
|
||||||
|
profile-clang:
|
||||||
|
clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
|
||||||
|
|
||||||
profile-fast:
|
profile-fast:
|
||||||
gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME)
|
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:
|
fclean:
|
||||||
rm -rf $(NAME)
|
rm -rf $(NAME)
|
||||||
|
|
||||||
|
|
28
rsa/bigint.c
28
rsa/bigint.c
|
@ -68,29 +68,35 @@ int64_t bigint_cmp(bigint_t a, bigint_t b) {
|
||||||
|
|
||||||
// TODO refactor/clean assume same length ?
|
// TODO refactor/clean assume same length ?
|
||||||
int bigint_dif(bigint_t a, bigint_t b) {
|
int bigint_dif(bigint_t a, bigint_t b) {
|
||||||
int cursor = a.len - 1;
|
int cursor = a.len;
|
||||||
while (cursor >= 0) {
|
while (--cursor >= 0) {
|
||||||
if (a.data[cursor] ^ b.data[cursor]) {
|
if (a.data[cursor] ^ b.data[cursor]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
cursor -= 1;
|
//cursor -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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
|
// TODO check opti
|
||||||
void bigint_substraction(bigint_t a, bigint_t b) {
|
void bigint_substraction(bigint_t a, bigint_t b) {
|
||||||
bigint_t borrow = bigint_clone(b);
|
bigint_t borrow = bigint_clone(b);
|
||||||
bigint_t y = bigint_clone(b);
|
bigint_t y = bigint_clone(b);
|
||||||
bigint_t zero = bigint_zero(a.len);
|
bigint_t zero = bigint_zero(a.len);
|
||||||
while (bigint_dif(borrow, zero)) {
|
tool(borrow, &y, zero, a);
|
||||||
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);
|
|
||||||
}
|
|
||||||
bigint_destroy(y);
|
bigint_destroy(y);
|
||||||
bigint_destroy(borrow);
|
bigint_destroy(borrow);
|
||||||
bigint_destroy(zero);
|
bigint_destroy(zero);
|
||||||
|
|
Loading…
Reference in New Issue