Skip to content

Commit ab5a917

Browse files
committed
Merge bitcoin-core/secp256k1#1303: ct: Use more volatile
17fa217 ct: Be cautious and use volatile trick in more "conditional" paths (Tim Ruffing) 5fb336f ct: Use volatile trick in scalar_cond_negate (Tim Ruffing) Pull request description: ACKs for top commit: sipa: ACK 17fa217 jonasnick: ACK 17fa217 Tree-SHA512: 4a0fbee7b1cce4f4647bff697c0e645d93aa8fb49777feef5eb1e1eadce2116bafdcc6175c066ee4fe4bf1340047311e2d7d2c48bb288867a837ecd6c8687121
2 parents 9eb6934 + 17fa217 commit ab5a917

5 files changed

+44
-34
lines changed

src/ecmult_const_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *p
2929
#define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
3030
int m = 0; \
3131
/* Extract the sign-bit for a constant time absolute-value. */ \
32-
int mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \
32+
int volatile mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \
3333
int abs_n = ((n) + mask) ^ mask; \
3434
int idx_n = abs_n >> 1; \
3535
secp256k1_fe neg_y; \

src/modinv32_impl.h

+18-15
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
6464
const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
6565
int32_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4],
6666
r5 = r->v[5], r6 = r->v[6], r7 = r->v[7], r8 = r->v[8];
67-
int32_t cond_add, cond_negate;
67+
volatile int32_t cond_add, cond_negate;
6868

6969
#ifdef VERIFY
7070
/* Verify that all limbs are in range (-2^30,2^30). */
@@ -186,7 +186,8 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t zeta, uint32_t f0, uint32_
186186
* being inside [-2^31,2^31) means that casting to signed works correctly.
187187
*/
188188
uint32_t u = 1, v = 0, q = 0, r = 1;
189-
uint32_t c1, c2, f = f0, g = g0, x, y, z;
189+
volatile uint32_t c1, c2;
190+
uint32_t mask1, mask2, f = f0, g = g0, x, y, z;
190191
int i;
191192

192193
for (i = 0; i < 30; ++i) {
@@ -195,23 +196,25 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t zeta, uint32_t f0, uint32_
195196
VERIFY_CHECK((q * f0 + r * g0) == g << i);
196197
/* Compute conditional masks for (zeta < 0) and for (g & 1). */
197198
c1 = zeta >> 31;
198-
c2 = -(g & 1);
199+
mask1 = c1;
200+
c2 = g & 1;
201+
mask2 = -c2;
199202
/* Compute x,y,z, conditionally negated versions of f,u,v. */
200-
x = (f ^ c1) - c1;
201-
y = (u ^ c1) - c1;
202-
z = (v ^ c1) - c1;
203+
x = (f ^ mask1) - mask1;
204+
y = (u ^ mask1) - mask1;
205+
z = (v ^ mask1) - mask1;
203206
/* Conditionally add x,y,z to g,q,r. */
204-
g += x & c2;
205-
q += y & c2;
206-
r += z & c2;
207-
/* In what follows, c1 is a condition mask for (zeta < 0) and (g & 1). */
208-
c1 &= c2;
207+
g += x & mask2;
208+
q += y & mask2;
209+
r += z & mask2;
210+
/* In what follows, mask1 is a condition mask for (zeta < 0) and (g & 1). */
211+
mask1 &= mask2;
209212
/* Conditionally change zeta into -zeta-2 or zeta-1. */
210-
zeta = (zeta ^ c1) - 1;
213+
zeta = (zeta ^ mask1) - 1;
211214
/* Conditionally add g,q,r to f,u,v. */
212-
f += g & c1;
213-
u += q & c1;
214-
v += r & c1;
215+
f += g & mask1;
216+
u += q & mask1;
217+
v += r & mask1;
215218
/* Shifts */
216219
g >>= 1;
217220
u <<= 1;

src/modinv64_impl.h

+17-14
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *
8888
static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int64_t sign, const secp256k1_modinv64_modinfo *modinfo) {
8989
const int64_t M62 = (int64_t)(UINT64_MAX >> 2);
9090
int64_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4];
91-
int64_t cond_add, cond_negate;
91+
volatile int64_t cond_add, cond_negate;
9292

9393
#ifdef VERIFY
9494
/* Verify that all limbs are in range (-2^62,2^62). */
@@ -175,7 +175,8 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_
175175
* being inside [-2^63,2^63) means that casting to signed works correctly.
176176
*/
177177
uint64_t u = 8, v = 0, q = 0, r = 8;
178-
uint64_t c1, c2, f = f0, g = g0, x, y, z;
178+
volatile uint64_t c1, c2;
179+
uint64_t mask1, mask2, f = f0, g = g0, x, y, z;
179180
int i;
180181

181182
for (i = 3; i < 62; ++i) {
@@ -184,23 +185,25 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_
184185
VERIFY_CHECK((q * f0 + r * g0) == g << i);
185186
/* Compute conditional masks for (zeta < 0) and for (g & 1). */
186187
c1 = zeta >> 63;
187-
c2 = -(g & 1);
188+
mask1 = c1;
189+
c2 = g & 1;
190+
mask2 = -c2;
188191
/* Compute x,y,z, conditionally negated versions of f,u,v. */
189-
x = (f ^ c1) - c1;
190-
y = (u ^ c1) - c1;
191-
z = (v ^ c1) - c1;
192+
x = (f ^ mask1) - mask1;
193+
y = (u ^ mask1) - mask1;
194+
z = (v ^ mask1) - mask1;
192195
/* Conditionally add x,y,z to g,q,r. */
193-
g += x & c2;
194-
q += y & c2;
195-
r += z & c2;
196+
g += x & mask2;
197+
q += y & mask2;
198+
r += z & mask2;
196199
/* In what follows, c1 is a condition mask for (zeta < 0) and (g & 1). */
197-
c1 &= c2;
200+
mask1 &= mask2;
198201
/* Conditionally change zeta into -zeta-2 or zeta-1. */
199-
zeta = (zeta ^ c1) - 1;
202+
zeta = (zeta ^ mask1) - 1;
200203
/* Conditionally add g,q,r to f,u,v. */
201-
f += g & c1;
202-
u += q & c1;
203-
v += r & c1;
204+
f += g & mask1;
205+
u += q & mask1;
206+
v += r & mask1;
204207
/* Shifts */
205208
g >>= 1;
206209
u <<= 1;

src/scalar_4x64_impl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a,
111111

112112
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
113113
secp256k1_uint128 t;
114+
volatile int vflag = flag;
114115
VERIFY_CHECK(bit < 256);
115-
bit += ((uint32_t) flag - 1) & 0x100; /* forcing (bit >> 6) > 3 makes this a noop */
116+
bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 6) > 3 makes this a noop */
116117
secp256k1_u128_from_u64(&t, r->d[0]);
117118
secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F));
118119
r->d[0] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
@@ -189,7 +190,8 @@ static int secp256k1_scalar_is_high(const secp256k1_scalar *a) {
189190
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
190191
/* If we are flag = 0, mask = 00...00 and this is a no-op;
191192
* if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
192-
uint64_t mask = !flag - 1;
193+
volatile int vflag = flag;
194+
uint64_t mask = -vflag;
193195
uint64_t nonzero = (secp256k1_scalar_is_zero(r) != 0) - 1;
194196
secp256k1_uint128 t;
195197
secp256k1_u128_from_u64(&t, r->d[0] ^ mask);

src/scalar_8x32_impl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a,
142142

143143
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
144144
uint64_t t;
145+
volatile int vflag = flag;
145146
VERIFY_CHECK(bit < 256);
146-
bit += ((uint32_t) flag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
147+
bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
147148
t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
148149
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
149150
t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
@@ -242,7 +243,8 @@ static int secp256k1_scalar_is_high(const secp256k1_scalar *a) {
242243
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
243244
/* If we are flag = 0, mask = 00...00 and this is a no-op;
244245
* if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
245-
uint32_t mask = !flag - 1;
246+
volatile int vflag = flag;
247+
uint32_t mask = -vflag;
246248
uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
247249
uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
248250
r->d[0] = t & nonzero; t >>= 32;

0 commit comments

Comments
 (0)