AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Low 36 Bitcoin

Merge bitcoin-core/secp256k1#1998: Avoid unsequenced accesses to volatile variables

Public commit record

What the developer wrote

Authored by merge-script

98/100 · Strong
Merge bitcoin-core/secp256k1#1998: Avoid unsequenced accesses to volatile variables

e7a34ba3ecee49421404994eeeeb0383f3e2fbbf Avoid unsequenced accesses to volatile variables (Hennadii Stepanov)
709e39c2fb2bcc36d8eda0b04c2d4669d6fd00b2 refactor: Prefix volatile variable names with "v" (Hennadii Stepanov)

Pull request description:

Since C11, if a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined (see https://en.cppreference.com/c/language/eval_order). Although this code targets C89, where the order of volatile reads is merely unspecified, this is exactly what happens with the volatile `cond_negate` in the master branch, because a read of such a variable is a side effect. Clang 24 [warns](https://github.com/llvm/llvm-project/pull/180955) about it with `-Wunsequenced` regardless of the language standard:
```console
$ cmake -B build -DCMAKE_C_COMPILER=clang-24
$ cmake --build build
[5/19] Building C object src/CMakeFiles/exhaustive_tests.dir/tests_exhaustive.c.o
In file included from /home/hebasto/dev/secp256k1/secp256k1/src/tests_exhaustive.c:23:
In file included from /home/hebasto/dev/secp256k1/secp256k1/src/secp256k1.c:27:
In file included from /home/hebasto/dev/secp256k1/secp256k1/src/field_impl.h:14:
In file included from /home/hebasto/dev/secp256k1/secp256k1/src/field_5x52_impl.h:13:
/home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:116:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
116 | r0 = (r0 ^ cond_negate) - cond_negate;
| ~~~~~~~~~~~ ^
/home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:117:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
117 | r1 = (r1 ^ cond_negate) - cond_negate;
| ~~~~~~~~~~~ ^
/home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:118:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
118 | r2 = (r2 ^ cond_negate) - cond_negate;
| ~~~~~~~~~~~ ^
/home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:119:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
119 | r3 = (r3 ^ cond_negate) - cond_negate;
| ~~~~~~~~~~~ ^
/home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:120:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
120 | r4 = (r4 ^ cond_negate) - cond_negate;
| ~~~~~~~~~~~ ^
5 warnings generated.
<snip>
```

Avoid such behavior by separating the access to a volatile variable, which still prevents compilers from generating branching, non-constant-time machine code, from the actual use of its value in expressions where the evaluation order is unspecified.

ACKs for top commit:
real-or-random:
utACK e7a34ba3ecee49421404994eeeeb0383f3e2fbbf
theStack:
re-ACK e7a34ba3ecee49421404994eeeeb0383f3e2fbbf

Tree-SHA512: eb047f0eab822322402024e883c154b7e4985cc6fe018ef347db39afdd3edf64f4885f28cffd0f2e7f975c646f662e82ffa2ac74f28bde90020cece8435abb6f
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly
The short version

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.

Recommended action

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

01

Touches constant-time arithmetic paths (modular inversion normalization)

02

Fixes undefined behavior involving volatile-qualified variables

03

Compiler warning (-Wunsequenced) treated as a correctness defect

04

Prevents potential compiler reordering/optimization that could reintroduce branches

05

No functional algorithm change; purely sequencing/access fix

Risk score

Why this scored 36/100

Our methodology →
Potential impact 8/30
Exploitability 4/25
Stealth signal 6/15
Affected reach 7/15
Confidence 7/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.