ecdsa: Clarify derivation of the r check in `_sig_verify` #1949

pull real-or-random wants to merge 1 commits into bitcoin-core:master from real-or-random:202609-ecdsa-comments changing 1 files +21 −15
  1. real-or-random commented at 7:25 AM on September 29, 2026: contributor

    Introduce the xr + n < p condition in the step where it is needed, namely right before switching from integer equality to equality modulo p, and explain why it is required there. Write congruences as a == b (mod m), because a mod p == b reads as if mod p were an operator applied to the left-hand side only.

    Also reword the comment at the p - n check: the check is needed for correctness, not merely an optimization.

    Made these improvements when trying to understand the background of #1948.

  2. real-or-random added the label tweak/refactor on Sep 29, 2026
  3. real-or-random added the label meta/development on Sep 29, 2026
  4. in src/ecdsa_impl.h:254 in af52718c1d
     255 | -     *    [Multiplying both sides of the equations by pr.z^2 mod p]
     256 | -     *    <=> (xr * pr.z^2 mod p == pr.x) || (xr + n < p && (xr + n) * pr.z^2 mod p == pr.x)
     257 | +     *    [Now both sides of each equality are in [0, p), so we can compare them modulo p
     258 | +     *     instead. In Jacobian coordinates, X(pr) is pr.x / pr.z^2]
     259 | +     *    <=> (xr == pr.x / pr.z^2 (mod p)) || (xr + n < p && xr + n == pr.x / pr.z^2 (mod p))
     260 | +     *    [Multiplying both sides of the equalities by pr.z^2]
    


    ViniciusCestarii commented at 2:25 PM on September 29, 2026:

    nit: since the previous step introduced (mod p), these are congruences now

         *    [Multiplying both sides of the congruences by pr.z^2]
    

    real-or-random commented at 6:59 AM on September 30, 2026:

    done

  5. ViniciusCestarii commented at 2:55 PM on September 29, 2026: contributor

    As someone not so familiar with this code, I found this clearer. It's nice that it state the check xr + n >= p is required for correctness and not just an optimization.

  6. theStack commented at 12:16 AM on September 30, 2026: contributor

    ACK af52718c1d871b5e2448d50863beae940c92d0aa (modulo #1949 (review) if it's taken)

    As someone not so familiar with this code, I found this clearer. It's nice that it state the check xr + n >= p is required for correctness and not just an optimization.

    +1, agree that this is clearer, and made reviewing #1948 much easier for me. I vaguely remember that I looked at this code a while ago and also had the impression the second return condition is an optimization.

    nit: if we want to be fancy, we could use directly the tribar (≡) character in the file for the modular congruences (maybe a bit too fancy though :p)

  7. ecdsa: Clarify derivation of the r check in `_sig_verify`
    Introduce the `xr + n < p` condition in the step where it is needed,
    namely right before switching from integer equality to congruence
    modulo p, and explain why it is required there. Write congruences as
    `a === b (mod m)`, because `a mod p == b` reads as if `mod p` were an
    operator applied to the left-hand side only.
    
    Also reword the comment at the `p - n` check: the check is needed for
    correctness, not merely an optimization.
    93987567a1
  8. real-or-random force-pushed on Sep 30, 2026
  9. real-or-random commented at 7:06 AM on September 30, 2026: contributor

    Updated. I made a few more small improvements.

    nit: if we want to be fancy, we could use directly the tribar (≡) character in the file for the modular congruences (maybe a bit too fancy though :p)

    I've settled on === now to avoid the non-ASCII char. I think writing the relations as congruences is a bit uncommon – it feels more like high school than real algebra – but it makes sense here. Usually we write just = (or == in code) because the two sides are literally equal if we consider them elements of the corresponding finite field $\mathbb{Z}_n$ or $\mathbb{Z}_p$. But here we deal with different fields/moduli (damn ECDSA!), and then working with ordinary integers in $\mathbb{Z}$ and writing congreunces explicitly seems clearer.

    I vaguely remember that I looked at this code a while ago and also had the impression the second return condition is an optimization.

    Indeed, this confused me to (though then it would be a misoptimization because it optimizes for the rare case of a wrong signature).

  10. theStack approved
  11. theStack commented at 12:43 PM on September 30, 2026: contributor

    re-ACK 93987567a1bf2bb5d3ea1492c286cd2b5a76f544

  12. theStack merged this on Sep 30, 2026
  13. theStack closed this on Sep 30, 2026


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-10-03 04:15 UTC

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