examples: randomize the context in musig, erase the keypair in schnorr #1980

pull fametrano wants to merge 1 commits into bitcoin-core:master from fametrano:examples-randomize-erase-keypair changing 2 files +14 −1
  1. fametrano commented at 11:26 PM on October 23, 2026: contributor

    examples/musig.c is the only example that handles secret keys without randomizing the context, which secp256k1.h highly recommends. Randomize it as schnorr.c does.

    schnorr.c erases the secret key buffer after use, but the keypair holds a copy of the secret key and stayed on the stack. Erase it too.

  2. in examples/musig.c:203 in 86f6a53084
     198 | +    /* Randomizing the context is recommended to protect against side-channel
     199 | +     * leakage. See `secp256k1_context_randomize` in secp256k1.h for more
     200 | +     * information about it. This should never fail. */
     201 | +    return_val = secp256k1_context_randomize(ctx, randomize);
     202 | +    assert(return_val);
     203 | +    (void)return_val;
    


    theStack commented at 3:04 AM on October 25, 2026:

    This seems unneeded?

  3. in examples/schnorr.c:153 in 86f6a53084
     147 | @@ -148,7 +148,10 @@ int main(void) {
     148 |       * swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
     149 |       *
     150 |       * Here we are preventing these writes from being optimized out, as any good compiler
     151 | -     * will remove any writes that aren't used. */
     152 | +     * will remove any writes that aren't used.
     153 | +     *
     154 | +     * The keypair also holds the secret key, so it is erased too. */
    


    theStack commented at 3:07 AM on October 25, 2026:

    nit: seems unnecessary to explain, it's well-known that a keypair also has a secret component

    but you could replace "overwrite the secret key buffer" with "overwrite secret key material" a few lines above (that's how we phrase it in the musig example).

  4. theStack commented at 3:17 AM on October 25, 2026: contributor

    ACK modulo nits

  5. fametrano force-pushed on Oct 25, 2026
  6. theStack approved
  7. theStack commented at 8:43 AM on October 26, 2026: contributor

    ACK a560ec67d1a4d18b1a050ce96dc5527f21979d01

  8. theStack added the label side-channel on Oct 28, 2026
  9. examples: randomize the context in musig, erase the keypair in schnorr
    examples/musig.c is the only example that handles secret keys without
    randomizing the context, which secp256k1.h highly recommends. Randomize
    it as schnorr.c does.
    
    schnorr.c erases the secret key buffer after use, but the keypair holds
    a copy of the secret key and stayed on the stack. Erase it too.
    e3b8fc3b52
  10. fametrano force-pushed on Nov 4, 2026
  11. fametrano commented at 7:24 AM on November 5, 2026: contributor

    Rebased on master to resolve the conflict with #1986. The changes are the same; only a context line in examples/schnorr.c differs (git range-diff 307035b..a560ec67 b97f7f5..e3b8fc3).

  12. fametrano requested review from theStack on Nov 5, 2026
Contributors


theStack


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-12 07:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me