Skip to content

Commit 11ac4e8

Browse files
committed
Add valgrind uninit check to cmovs output
1 parent a39c2b0 commit 11ac4e8

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/field_10x26_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "util.h"
1111
#include "field.h"
1212

13+
#if defined(VALGRIND) && defined(VERIFY)
14+
# include <valgrind/memcheck.h>
15+
#endif
16+
1317
#ifdef VERIFY
1418
static void secp256k1_fe_verify(const secp256k1_fe *a) {
1519
const uint32_t *d = a->n;
@@ -1099,6 +1103,9 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
10991103
uint32_t mask0, mask1;
11001104
mask0 = flag + ~((uint32_t)0);
11011105
mask1 = ~mask0;
1106+
#if defined(VALGRIND) && defined(VERIFY)
1107+
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
1108+
#endif
11021109
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
11031110
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
11041111
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
@@ -1121,6 +1128,9 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
11211128
uint32_t mask0, mask1;
11221129
mask0 = flag + ~((uint32_t)0);
11231130
mask1 = ~mask0;
1131+
#if defined(VALGRIND) && defined(VERIFY)
1132+
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
1133+
#endif
11241134
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
11251135
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
11261136
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);

src/field_5x52_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include "field_5x52_int128_impl.h"
2121
#endif
2222

23+
#if defined(VALGRIND) && defined(VERIFY)
24+
# include <valgrind/memcheck.h>
25+
#endif
26+
2327
/** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
2428
* represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
2529
* each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
@@ -451,6 +455,9 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
451455
uint64_t mask0, mask1;
452456
mask0 = flag + ~((uint64_t)0);
453457
mask1 = ~mask0;
458+
#if defined(VALGRIND) && defined(VERIFY)
459+
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
460+
#endif
454461
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
455462
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
456463
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
@@ -468,6 +475,9 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
468475
uint64_t mask0, mask1;
469476
mask0 = flag + ~((uint64_t)0);
470477
mask1 = ~mask0;
478+
#if defined(VALGRIND) && defined(VERIFY)
479+
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
480+
#endif
471481
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
472482
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
473483
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);

src/scalar_4x64_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
55
**********************************************************************/
66

7+
8+
#if defined(VALGRIND) && defined(VERIFY)
9+
# include <valgrind/memcheck.h>
10+
#endif
11+
712
#ifndef SECP256K1_SCALAR_REPR_IMPL_H
813
#define SECP256K1_SCALAR_REPR_IMPL_H
914

@@ -950,6 +955,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
950955
uint64_t mask0, mask1;
951956
mask0 = flag + ~((uint64_t)0);
952957
mask1 = ~mask0;
958+
#if defined(VALGRIND) && defined(VERIFY)
959+
VALGRIND_CHECK_MEM_IS_DEFINED(r->d, sizeof(r->d));
960+
#endif
953961
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
954962
r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
955963
r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);

src/scalar_8x32_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
55
**********************************************************************/
66

7+
#if defined(VALGRIND) && defined(VERIFY)
8+
# include <valgrind/memcheck.h>
9+
#endif
10+
711
#ifndef SECP256K1_SCALAR_REPR_IMPL_H
812
#define SECP256K1_SCALAR_REPR_IMPL_H
913

@@ -722,6 +726,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
722726
uint32_t mask0, mask1;
723727
mask0 = flag + ~((uint32_t)0);
724728
mask1 = ~mask0;
729+
#if defined(VALGRIND) && defined(VERIFY)
730+
VALGRIND_CHECK_MEM_IS_DEFINED(r->d, sizeof(r->d));
731+
#endif
725732
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
726733
r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
727734
r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);

src/scalar_low_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include <string.h>
1313

14+
#if defined(VALGRIND) && defined(VERIFY)
15+
# include <valgrind/memcheck.h>
16+
#endif
17+
1418
SECP256K1_INLINE static int secp256k1_scalar_is_even(const secp256k1_scalar *a) {
1519
return !(*a & 1);
1620
}
@@ -118,6 +122,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
118122
uint32_t mask0, mask1;
119123
mask0 = flag + ~((uint32_t)0);
120124
mask1 = ~mask0;
125+
#if defined(VALGRIND) && defined(VERIFY)
126+
VALGRIND_CHECK_MEM_IS_DEFINED(r, sizeof(*r));
127+
#endif
121128
*r = (*r & mask0) | (*a & mask1);
122129
}
123130

0 commit comments

Comments
 (0)