Skip to content

Commit 5fb336f

Browse files
ct: Use volatile trick in scalar_cond_negate
1 parent 24c768a commit 5fb336f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/scalar_4x64_impl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ static int secp256k1_scalar_is_high(const secp256k1_scalar *a) {
189189
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
190190
/* If we are flag = 0, mask = 00...00 and this is a no-op;
191191
* if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
192-
uint64_t mask = !flag - 1;
192+
volatile int vflag = flag;
193+
uint64_t mask = -vflag;
193194
uint64_t nonzero = (secp256k1_scalar_is_zero(r) != 0) - 1;
194195
secp256k1_uint128 t;
195196
secp256k1_u128_from_u64(&t, r->d[0] ^ mask);

src/scalar_8x32_impl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ static int secp256k1_scalar_is_high(const secp256k1_scalar *a) {
242242
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
243243
/* If we are flag = 0, mask = 00...00 and this is a no-op;
244244
* if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
245-
uint32_t mask = !flag - 1;
245+
volatile int vflag = flag;
246+
uint32_t mask = -vflag;
246247
uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
247248
uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
248249
r->d[0] = t & nonzero; t >>= 32;

0 commit comments

Comments
 (0)