Add secp256k1_fe_add_int function #1217

pull sipa wants to merge 1 commits into bitcoin-core:master from sipa:202303_add_int changing 5 files +43 −3
  1. sipa commented at 9:45 PM on March 1, 2023: contributor

    Suggested here: #1118 (review)

    This adds a new field function secp256k1_fe_add_int which is equivalent to secp256k1_fe_set_int + secp256k1_fe_add, which is a pattern that occurs a few times, and uses a needlessly convoluted computation for an absolutely trivial effect (incrementing one limb variable). Existing PRs like #1118 and #1129 add more ways this function would be useful.

  2. in src/group_impl.h:379 in 42518bf1f8 outdated
     375 | @@ -376,7 +376,7 @@ static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, cons
     376 |              secp256k1_gej_double_var(r, a, rzr);
     377 |          } else {
     378 |              if (rzr != NULL) {
     379 | -                secp256k1_fe_set_int(rzr, 0);
     380 | +                secp256k1_fe_clear(rzr);
    


    real-or-random commented at 10:02 AM on March 2, 2023:

    This reminds me of https://github.com/bitcoin-core/secp256k1/pull/636/commits/b33a8e49e89f5a1ea02a9b9b9b208cb4f3d59e44 Okay, the PR is almost 4 years old now... :/ But I should continue the work at some point.

    Currently, clear is mostly used to kill secrets (though that doesn't work due to dead-store elimination), and this should be separated from setting to 0 in the future for the purposes of the aforementioned PR. Now, the advantage of clear over set_int(0) is that the former sets the magnitude to 0 instead of 1. In the mentioned PR, I change set_int to do the same for input 0 but that's a bad idea because we want statically implied magnitude in the long term. The proper solution to all of this is a set_zero function that guarantees to set the value and magnitude to 0 (and won't just clear out memory).

    What does this mean for this PR? To avoid getting side tracked again, I'd say don't introduce that new function here, but maybe keep that line and the instances below just how it is. That avoids introducing more code locations where clear is not used for clearing memory. The current behavior of setting magnitude of 1 is apparently good enough with the current code.


    sipa commented at 10:10 PM on March 2, 2023:

    Done, I've refrained from touching any set_int(..., 0) -> clear(...).

  3. in src/field_5x52_impl.h:432 in 42518bf1f8 outdated
     424 | @@ -425,6 +425,20 @@ SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
     425 |  #endif
     426 |  }
     427 |  
     428 | +SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
     429 | +#ifdef VERIFY
     430 | +    secp256k1_fe_verify(r);
     431 | +    VERIFY_CHECK(a >= 0);
     432 | +    VERIFY_CHECK(a <= 0xFFFF);
    


    real-or-random commented at 10:05 AM on March 2, 2023:

    Is there a reason to keep it in these bounds? Sorry if it's obvious, but I don't see it.


    sipa commented at 10:11 PM on March 2, 2023:

    Apparently (for unclear reasons...) set_int is restricted to range 0...0x7FFF. The 0xFFFF is a typo here; I wanted to copy the bound, so that the tests can verify correspondence between add_int and set_int + add.


    real-or-random commented at 10:46 PM on March 2, 2023:

    0x7FFF is the minimum portable MAX_INT, see #943 (review)

  4. in src/field.h:88 in 42518bf1f8 outdated
      84 | @@ -85,6 +85,9 @@ static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
      85 |   *  as an argument. The magnitude of the output is one higher. */
      86 |  static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
      87 |  
      88 | +/** Adds a small integer (up to 0x7FFF) to r. The resulting magnitude increases by one. */
    


    real-or-random commented at 10:07 AM on March 2, 2023:
    /** Adds a small integer (up to 0xFFFF) to r. The resulting magnitude increases by one. */
    

    At least that's what you VERIFY_CHECK.


    sipa commented at 10:11 PM on March 2, 2023:

    Changed to 0x7FFF everywhere instead.

  5. real-or-random commented at 10:10 AM on March 2, 2023: contributor

    Nice! Concept ACK

  6. Add secp256k1_fe_add_int function b081f7e4cb
  7. sipa force-pushed on Mar 2, 2023
  8. real-or-random approved
  9. real-or-random commented at 10:21 PM on March 2, 2023: contributor

    utACK b081f7e4cbfd27edc36e823dcd93537a46f7d2a6

  10. jonasnick commented at 2:06 PM on March 7, 2023: contributor

    ACK b081f7e4cbfd27edc36e823dcd93537a46f7d2a6

  11. jonasnick merged this on Mar 7, 2023
  12. jonasnick closed this on Mar 7, 2023

  13. dhruv referenced this in commit a5df79db12 on Mar 7, 2023
  14. dhruv referenced this in commit 77b510d84c on Mar 7, 2023
  15. sipa referenced this in commit 763079a3f1 on Mar 8, 2023
  16. in src/group_impl.h:230 in b081f7e4cb
     226 | @@ -227,7 +227,7 @@ static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int o
     227 |      secp256k1_fe_sqr(&x2, x);
     228 |      secp256k1_fe_mul(&x3, x, &x2);
     229 |      r->infinity = 0;
     230 | -    secp256k1_fe_add(&x3, &secp256k1_fe_const_b);
    


    roconnor-blockstream commented at 4:01 PM on March 14, 2023:

    secp256k1_fe_const_b is now unused.


    real-or-random commented at 5:40 PM on April 20, 2023:

    @roconnor-blockstream Good catch. Do you want to open a PR to remove it?


    roconnor-blockstream commented at 5:53 PM on April 20, 2023:

    I suppose I can do that.

  17. div72 referenced this in commit 945b094575 on Mar 14, 2023
  18. roconnor-blockstream referenced this in commit d6be470ee5 on Apr 20, 2023
  19. real-or-random referenced this in commit f6bef03c0a on Apr 21, 2023
  20. vmta referenced this in commit e1120c94a1 on Jun 4, 2023
  21. vmta referenced this in commit 8f03457eed on Jul 1, 2023
  22. delta1 referenced this in commit 3f32c20932 on Aug 8, 2023
  23. delta1 referenced this in commit 31ac0c1081 on Aug 31, 2023
  24. janus referenced this in commit 4816e2a921 on Sep 3, 2023
  25. backpacker69 referenced this in commit 26f6d832fb on Mar 5, 2024
  26. str4d referenced this in commit b350ac56ac on Jun 4, 2025
  27. Fabcien referenced this in commit c16204f26c on Apr 14, 2026
  28. Fabcien referenced this in commit 24d6211de6 on Apr 14, 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-05-19 06:52 UTC