Merge bitcoin-core/secp256k1#1980: examples: randomize the context in musig, erase the keypair in schnorr
What changed, and why it matters
This change improves two example programs in the secp256k1 cryptographic library. It adds a recommended side-channel protection step to the multi-signature example (musig.c) and ensures the single-signature example (schnorr.c) also wipes a second copy of the secret key from memory. These are hardening fixes in demonstration code, not the library itself, so they do not directly fix an exploitable vulnerability in production use.
Treat as a minor hardening improvement in example code. Users should ensure their own applications using libsecp256k1 follow the documented recommendations to randomize contexts and erase secret material. No urgent action is required for the library itself.
Security signals we found
Context randomization added to protect against side-channel leakage
Secret key material explicitly erased from keypair object after use
Changes are confined to example code, not the core library
No CVE, advisory, or vendor security disclosure referenced in commit
Evidence from the diff
The commit modifies examples/musig.c to call secp256k1_context_randomize() before handling secret keys, matching the existing practice in examples/schnorr.c. It also modifies examples/schnorr.c to call secure_erase() on the secp256k1_keypair object after use, in addition to the existing erase of the seckey buffer, because the keypair structure retains a copy of the secret key. Both changes are defensive: one reduces side-channel leakage risk, the other reduces the window in which secret key material could be exposed via memory disclosure bugs or swap. They affect example/demonstration code only, not the secp256k1 library implementation or its public API.
Changed components
examples/musig.cexamples/schnorr.cInspect captured patch +14 / −1
### examples/musig.c
@@ -186,9 +186,21 @@ int main(void) {
secp256k1_musig_keyagg_cache cache;
unsigned char msg[32] = "this_could_be_the_hash_of_a_msg";
unsigned char sig[64];
+ unsigned char randomize[32];
+ int return_val;
/* Create a secp256k1 context */
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
+ if (!fill_random(randomize, sizeof(randomize))) {
+ printf("Failed to generate randomness\n");
+ return EXIT_FAILURE;
+ }
+ /* Randomizing the context is recommended to protect against side-channel
+ * leakage. See `secp256k1_context_randomize` in secp256k1.h for more
+ * information about it. This should never fail. */
+ return_val = secp256k1_context_randomize(ctx, randomize);
+ assert(return_val);
+
printf("Creating key pairs......");
fflush(stdout);
for (i = 0; i < N_SIGNERS; i++) {
### examples/schnorr.c
@@ -145,10 +145,11 @@ int main(void) {
/* It's best practice to try to clear secrets from memory after using them.
* This is done because some bugs can allow an attacker to leak memory, for
* example through "out of bounds" array access (see Heartbleed), or the OS
- * swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
+ * swapping them to disk. Hence, we overwrite secret key material with zeros.
*
* Here we are preventing these writes from being optimized out, as any good compiler
* will remove any writes that aren't used. */
secure_erase(seckey, sizeof(seckey));
+ secure_erase(&keypair, sizeof(keypair));
return is_signature_valid ? EXIT_SUCCESS : EXIT_FAILURE;
}Why this scored 21/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.