From d368c925fb4588dd8227217322a250be28b20360 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 15:55:05 +0100 Subject: [PATCH] opti: is_not_zero --- rsa/bigint.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rsa/bigint.c b/rsa/bigint.c index 6d75a7d..1f45b5a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -79,8 +79,17 @@ int bigint_dif(bigint_t a, bigint_t b) { return 0; } -void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { - while (bigint_dif(*y, zero)) { +int is_not_zero(bigint_t n) { + for (size_t i = 0; i < n.len; i++) { + if (n.data[i]) { + return 1; + } + } + return 0; +} + +void tool(bigint_t borrow, bigint_t *y, bigint_t a) { + 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]; @@ -88,18 +97,15 @@ void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) { 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); - tool(borrow, &y, zero, a); + tool(borrow, &y, a); bigint_destroy(y); bigint_destroy(borrow); - bigint_destroy(zero); } // TODO check opti