Merge bitcoin-core/secp256k1#1998: Avoid unsequenced accesses to volatile variables
What changed, and why it matters
This change fixes a subtle compiler-warning/undefined-behavior issue in the code that performs secret math inside Bitcoin's cryptographic library. The code uses 'volatile' variables to prevent the compiler from turning certain operations into branches that could leak secrets through timing. However, reading the same volatile variable twice in one expression (for example, '(x ^ v) - v') is technically undefined behavior in modern C and triggers a new Clang warning. The patch copies the volatile value into a regular variable first, then uses that value in the math. It is a defensive, correctness fix rather than a confirmed exploitable bug, but because it touches constant-time code paths it has security relevance.
Merge the patch; it is a low-risk, defensive correctness fix for constant-time code. Downstream integrators should update to include this change, especially when building with newer Clang versions that flag -Wunsequenced as an error or when strict UB sanitizers are enabled. No immediate incident response is required because no practical exploit has been demonstrated.
Security signals we found
Touches constant-time arithmetic paths (modular inversion normalization)
Fixes undefined behavior involving volatile-qualified variables
Compiler warning (-Wunsequenced) treated as a correctness defect
Prevents potential compiler reordering/optimization that could reintroduce branches
No functional algorithm change; purely sequencing/access fix
Evidence from the diff
The patch modifies secp256k1_modinv32_normalize_30() and secp256k1_modinv64_normalize_62() in src/modinv32_impl.h and src/modinv64_impl.h. Previously, volatile-qualified local variables cond_add/cond_negate were read multiple times in expressions such as ‘(r0 ^ cond_negate) - cond_negate’. Under C11, unsequenced side effects on the same scalar object (volatile reads are side effects) produce undefined behavior; Clang 24 emits -Wunsequenced for this. The patch splits each volatile variable into a volatile copy (vcond_add/vcond_negate) and a non-volatile working copy (cond_negate), explicitly copies the volatile value once, and then performs the arithmetic on the non-volatile copy. This preserves the anti-optimization/constant-time intent while avoiding unsequenced volatile accesses.
Changed components
src/modinv32_impl.hsrc/modinv64_impl.hsecp256k1_modinv32_normalize_30secp256k1_modinv64_normalize_62constant-time conditional negate/add logicInspect captured patch +42 / −36
### src/modinv32_impl.h
@@ -64,7 +64,8 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
int32_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4],
r5 = r->v[5], r6 = r->v[6], r7 = r->v[7], r8 = r->v[8];
- volatile int32_t cond_add, cond_negate;
+ int32_t cond_negate;
+ volatile int32_t vcond_add, vcond_negate;
#ifdef VERIFY
/* Verify that all limbs are in range (-2^30,2^30). */
@@ -82,17 +83,19 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
* limbs are in range (-2^30,2^30), this cannot overflow an int32_t. Note that the right
* shifts below are signed sign-extending shifts (see assumptions.h for tests that that is
* indeed the behavior of the right shift operator). */
- cond_add = r8 >> 31;
- r0 += modinfo->modulus.v[0] & cond_add;
- r1 += modinfo->modulus.v[1] & cond_add;
- r2 += modinfo->modulus.v[2] & cond_add;
- r3 += modinfo->modulus.v[3] & cond_add;
- r4 += modinfo->modulus.v[4] & cond_add;
- r5 += modinfo->modulus.v[5] & cond_add;
- r6 += modinfo->modulus.v[6] & cond_add;
- r7 += modinfo->modulus.v[7] & cond_add;
- r8 += modinfo->modulus.v[8] & cond_add;
- cond_negate = sign >> 31;
+ vcond_add = r8 >> 31;
+ r0 += modinfo->modulus.v[0] & vcond_add;
+ r1 += modinfo->modulus.v[1] & vcond_add;
+ r2 += modinfo->modulus.v[2] & vcond_add;
+ r3 += modinfo->modulus.v[3] & vcond_add;
+ r4 += modinfo->modulus.v[4] & vcond_add;
+ r5 += modinfo->modulus.v[5] & vcond_add;
+ r6 += modinfo->modulus.v[6] & vcond_add;
+ r7 += modinfo->modulus.v[7] & vcond_add;
+ r8 += modinfo->modulus.v[8] & vcond_add;
+ vcond_negate = sign >> 31;
+ /* Unsequenced reads to volatile variables are UB, so copy to a non-volatile variable first.*/
+ cond_negate = vcond_negate;
r0 = (r0 ^ cond_negate) - cond_negate;
r1 = (r1 ^ cond_negate) - cond_negate;
r2 = (r2 ^ cond_negate) - cond_negate;
@@ -114,16 +117,16 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
/* In a second step add the modulus again if the result is still negative, bringing r to range
* [0,modulus). */
- cond_add = r8 >> 31;
- r0 += modinfo->modulus.v[0] & cond_add;
- r1 += modinfo->modulus.v[1] & cond_add;
- r2 += modinfo->modulus.v[2] & cond_add;
- r3 += modinfo->modulus.v[3] & cond_add;
- r4 += modinfo->modulus.v[4] & cond_add;
- r5 += modinfo->modulus.v[5] & cond_add;
- r6 += modinfo->modulus.v[6] & cond_add;
- r7 += modinfo->modulus.v[7] & cond_add;
- r8 += modinfo->modulus.v[8] & cond_add;
+ vcond_add = r8 >> 31;
+ r0 += modinfo->modulus.v[0] & vcond_add;
+ r1 += modinfo->modulus.v[1] & vcond_add;
+ r2 += modinfo->modulus.v[2] & vcond_add;
+ r3 += modinfo->modulus.v[3] & vcond_add;
+ r4 += modinfo->modulus.v[4] & vcond_add;
+ r5 += modinfo->modulus.v[5] & vcond_add;
+ r6 += modinfo->modulus.v[6] & vcond_add;
+ r7 += modinfo->modulus.v[7] & vcond_add;
+ r8 += modinfo->modulus.v[8] & vcond_add;
/* And propagate again. */
r1 += r0 >> 30; r0 &= M30;
r2 += r1 >> 30; r1 &= M30;
### src/modinv64_impl.h
@@ -88,7 +88,8 @@ static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *
static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int64_t sign, const secp256k1_modinv64_modinfo *modinfo) {
const int64_t M62 = (int64_t)(UINT64_MAX >> 2);
int64_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4];
- volatile int64_t cond_add, cond_negate;
+ int64_t cond_negate;
+ volatile int64_t vcond_add, vcond_negate;
#ifdef VERIFY
/* Verify that all limbs are in range (-2^62,2^62). */
@@ -106,13 +107,15 @@ static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int6
* limbs are in range (-2^62,2^62), this cannot overflow an int64_t. Note that the right
* shifts below are signed sign-extending shifts (see assumptions.h for tests that that is
* indeed the behavior of the right shift operator). */
- cond_add = r4 >> 63;
- r0 += modinfo->modulus.v[0] & cond_add;
- r1 += modinfo->modulus.v[1] & cond_add;
- r2 += modinfo->modulus.v[2] & cond_add;
- r3 += modinfo->modulus.v[3] & cond_add;
- r4 += modinfo->modulus.v[4] & cond_add;
- cond_negate = sign >> 63;
+ vcond_add = r4 >> 63;
+ r0 += modinfo->modulus.v[0] & vcond_add;
+ r1 += modinfo->modulus.v[1] & vcond_add;
+ r2 += modinfo->modulus.v[2] & vcond_add;
+ r3 += modinfo->modulus.v[3] & vcond_add;
+ r4 += modinfo->modulus.v[4] & vcond_add;
+ vcond_negate = sign >> 63;
+ /* Unsequenced reads to volatile variables are UB, so copy to a non-volatile variable first.*/
+ cond_negate = vcond_negate;
r0 = (r0 ^ cond_negate) - cond_negate;
r1 = (r1 ^ cond_negate) - cond_negate;
r2 = (r2 ^ cond_negate) - cond_negate;
@@ -126,12 +129,12 @@ static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int6
/* In a second step add the modulus again if the result is still negative, bringing
* r to range [0,modulus). */
- cond_add = r4 >> 63;
- r0 += modinfo->modulus.v[0] & cond_add;
- r1 += modinfo->modulus.v[1] & cond_add;
- r2 += modinfo->modulus.v[2] & cond_add;
- r3 += modinfo->modulus.v[3] & cond_add;
- r4 += modinfo->modulus.v[4] & cond_add;
+ vcond_add = r4 >> 63;
+ r0 += modinfo->modulus.v[0] & vcond_add;
+ r1 += modinfo->modulus.v[1] & vcond_add;
+ r2 += modinfo->modulus.v[2] & vcond_add;
+ r3 += modinfo->modulus.v[3] & vcond_add;
+ r4 += modinfo->modulus.v[4] & vcond_add;
/* And propagate again. */
r1 += r0 >> 62; r0 &= M62;
r2 += r1 >> 62; r1 &= M62;Why this scored 36/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.