Skip to content

Commit

Permalink
Add valgrind uninit check to cmovs output
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed May 20, 2020
1 parent 7ccbe7c commit b215741
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/field_10x26_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "util.h"
#include "field.h"

#if defined(VALGRIND)
# include <valgrind/memcheck.h>
#endif

#ifdef VERIFY
static void secp256k1_fe_verify(const secp256k1_fe *a) {
const uint32_t *d = a->n;
Expand Down Expand Up @@ -1099,6 +1103,9 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
uint32_t mask0, mask1;
mask0 = flag + ~((uint32_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
#endif
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
Expand All @@ -1121,6 +1128,9 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
uint32_t mask0, mask1;
mask0 = flag + ~((uint32_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
#endif
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
Expand Down
10 changes: 10 additions & 0 deletions src/field_5x52_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "field_5x52_int128_impl.h"
#endif

#if defined(VALGRIND)
# include <valgrind/memcheck.h>
#endif

/** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
* represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
* each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
Expand Down Expand Up @@ -451,6 +455,9 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_
uint64_t mask0, mask1;
mask0 = flag + ~((uint64_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
#endif
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
Expand All @@ -468,6 +475,9 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
uint64_t mask0, mask1;
mask0 = flag + ~((uint64_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->n, sizeof(r->n));
#endif
r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
Expand Down
8 changes: 8 additions & 0 deletions src/scalar_4x64_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
**********************************************************************/


#if defined(VALGRIND)
# include <valgrind/memcheck.h>
#endif

#ifndef SECP256K1_SCALAR_REPR_IMPL_H
#define SECP256K1_SCALAR_REPR_IMPL_H

Expand Down Expand Up @@ -950,6 +955,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
uint64_t mask0, mask1;
mask0 = flag + ~((uint64_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->d, sizeof(r->d));
#endif
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
Expand Down
7 changes: 7 additions & 0 deletions src/scalar_8x32_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
**********************************************************************/

#if defined(VALGRIND)
# include <valgrind/memcheck.h>
#endif

#ifndef SECP256K1_SCALAR_REPR_IMPL_H
#define SECP256K1_SCALAR_REPR_IMPL_H

Expand Down Expand Up @@ -722,6 +726,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
uint32_t mask0, mask1;
mask0 = flag + ~((uint32_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r->d, sizeof(r->d));
#endif
r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
Expand Down
7 changes: 7 additions & 0 deletions src/scalar_low_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include <string.h>

#if defined(VALGRIND)
# include <valgrind/memcheck.h>
#endif

SECP256K1_INLINE static int secp256k1_scalar_is_even(const secp256k1_scalar *a) {
return !(*a & 1);
}
Expand Down Expand Up @@ -118,6 +122,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
uint32_t mask0, mask1;
mask0 = flag + ~((uint32_t)0);
mask1 = ~mask0;
#if defined(VALGRIND) && defined(VERIFY)
VALGRIND_CHECK_MEM_IS_DEFINED(r, sizeof(*r));
#endif
*r = (*r & mask0) | (*a & mask1);
}

Expand Down

0 comments on commit b215741

Please sign in to comment.