Skip to content

Commit fe9d203

Browse files
authored
Merge pull request #823 from LedgerHQ/is_zero
Make cx_math_is_zero run in constant time
2 parents 98eaa57 + fa3f9b8 commit fe9d203

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib_cxng/include/lcx_math.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,11 @@ DEPRECATED static inline void cx_math_next_prime(uint8_t *r, uint32_t len)
568568
*/
569569
static inline bool cx_math_is_zero(const uint8_t *a, size_t len)
570570
{
571-
uint32_t i;
572-
for (i = 0; i < len; i++) {
573-
if (a[i] != 0) {
574-
return 0;
575-
}
571+
uint8_t acc = 0; // accumulate all the bytes in order to run in constant time
572+
for (size_t i = 0; i < len; i++) {
573+
acc |= a[i];
576574
}
577-
return 1;
575+
return acc == 0;
578576
}
579577

580578
#endif // HAVE_MATH

0 commit comments

Comments
 (0)