From fa3f9b888fc924e77bc05323c5aaf4ba64b98d1c Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:35:38 +0000 Subject: [PATCH] Make cx_math_is_zero run in constant time --- lib_cxng/include/lcx_math.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib_cxng/include/lcx_math.h b/lib_cxng/include/lcx_math.h index 40b9a90d3..d04b68fc6 100644 --- a/lib_cxng/include/lcx_math.h +++ b/lib_cxng/include/lcx_math.h @@ -568,13 +568,11 @@ DEPRECATED static inline void cx_math_next_prime(uint8_t *r, uint32_t len) */ static inline bool cx_math_is_zero(const uint8_t *a, size_t len) { - uint32_t i; - for (i = 0; i < len; i++) { - if (a[i] != 0) { - return 0; - } + uint8_t acc = 0; // accumulate all the bytes in order to run in constant time + for (size_t i = 0; i < len; i++) { + acc |= a[i]; } - return 1; + return acc == 0; } #endif // HAVE_MATH