util: add constant-time is_zero_array function
This commit is contained in:
16
src/util.h
16
src/util.h
@@ -239,6 +239,22 @@ static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Return 1 if all elements of array s are 0 and otherwise return 0.
|
||||
* Constant-time. */
|
||||
static SECP256K1_INLINE int secp256k1_is_zero_array(const unsigned char *s, size_t len) {
|
||||
unsigned char acc = 0;
|
||||
int ret;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
acc |= s[i];
|
||||
}
|
||||
ret = (acc == 0);
|
||||
/* acc may contain secret values. Try to explicitly clear it. */
|
||||
acc = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized and non-negative.*/
|
||||
static SECP256K1_INLINE void secp256k1_int_cmov(int *r, const int *a, int flag) {
|
||||
unsigned int mask0, mask1, r_masked, a_masked;
|
||||
|
||||
Reference in New Issue
Block a user