Add internal helper for serializing a non-infinity group element to a compressed public key #1773

issue theStack openend this issue on November 15, 2025
  1. theStack commented at 0:46 am on November 15, 2025: contributor

    In more recent modules (ellswift, musig and silentpayments), the following pattern of serializing a non-infinity group element to a 33-bytes compressed public key appears repeatedly:

    0    size_t ser_size = 33;
    1    int ser_ret;
    2    .....
    3    ser_ret = secp256k1_eckey_pubkey_serialize(ge, out33, &ser_size, 1);
    4#ifdef VERIFY
    5    VERIFY_CHECK(ser_ret && ser_size == 33);
    6#else
    7    (void)ser_ret;
    8#endif
    

    One could deduplicate these instances by a new function, as suggested by @w0xlt in the latest silentpayments module PR, see #1765#pullrequestreview-3462461331. This could simplify the code in the modules quite a bit (less LOC, less variables, less pre-processor blocks). Demo branch: https://github.com/theStack/secp256k1/commit/e02365188ac8bb5129dc36c817fb465dfa5f7b76. It seems a reasonable idea to me, but not sure about the right naming of the function and where to put it. Also, one drawback might be that if the VERIFY_CHECK fails, it’s a bit harder to find out the cause if it fails in a helper rather than directly in the module code (not sure if we care about that?). Opening this as an issue first to see if there is conceptual support.

  2. real-or-random commented at 8:10 am on November 17, 2025: contributor

    What about going a further step back and splitting secp256k1_eckey_pubkey_serialize into secp256k1_eckey_pubkey_serialize_compressed and secp256k1_eckey_pubkey_serialize_uncompressed (or alternatively, _serialize33 and serialize65)? That seems even cleaner to me.

    Then we’ll need to change the implementation of the API function secp256k1_ec_pubkey_serialize (which still has compressed flag) and do the branching there. But that’s not a big deal: the current implementation needs to branch on this flag anyway for an ARG_CHECK, and it also needs the literals 33 and 65, i.e., it doesn’t really make great use of the fact that the public function and the internal function both have a compressed flag.

  3. real-or-random added the label tweak/refactor on Nov 17, 2025

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: 2025-11-24 13:15 UTC

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