Add header-sensitive size checking via CPubKey::ValidSize, and prefer key/sig size const references #12319

pull Empact wants to merge 5 commits into bitcoin:master from Empact:pubkeysize changing 7 files +63 −46
  1. Empact commented at 11:34 PM on January 31, 2018: member

    Here I:

    • add header-sensitive size checking via CPubKey::ValidSize
    • move toward referring to the consts where possible, rather than magic numbers
    • rename size consts to not repeat the type in variable name

    My first PR! Just getting a sense of the codebase at this point. The last point at least is purely a question of style, happy to back that out.

  2. fanquake added the label Refactoring on Feb 1, 2018
  3. in src/pubkey.h:175 in ed1483b712 outdated
     171 | @@ -164,7 +172,7 @@ class CPubKey
     172 |       */
     173 |      bool IsValid() const
     174 |      {
     175 | -        return size() > 0;
     176 | +        return size() >= COMPRESSED_SIZE && size() <= SIZE;
    


    devrandom commented at 5:35 PM on February 3, 2018:

    I would leave the original code on this line, since size() (and GetLen()) specifically returns 0 to signal an invalid header byte.

    Keep in mind that size() can only return three potential values (zero or one of the two sizes), so any size in between will never be returned.


    Empact commented at 10:23 AM on February 4, 2018:

    Roger! Thanks for pointing that out.

  4. in src/script/interpreter.cpp:63 in ed1483b712 outdated
      60 | @@ -61,17 +61,17 @@ static inline void popstack(std::vector<valtype>& stack)
      61 |  }
      62 |  
      63 |  bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
    


    devrandom commented at 5:37 PM on February 3, 2018:

    Might be good to move these two static methods under CPubKey, but maybe that's a job for another PR.


    Empact commented at 11:49 AM on February 4, 2018:

    Make sense! This commit moves these methods: https://github.com/Empact/bitcoin/commit/f3c5b2d1c6b0348b5751c15cffdd1238209ea463

    I'll plan to submit it once this is merged.

  5. in src/script/standard.cpp:38 in ed1483b712 outdated
      34 | @@ -35,6 +35,10 @@ const char* GetTxnOutputType(txnouttype t)
      35 |      return nullptr;
      36 |  }
      37 |  
      38 | +static constexpr bool ValidPubKeySize(std::vector<unsigned char>::size_type size) {
    


    devrandom commented at 5:38 PM on February 3, 2018:

    Might be good to have this under CPubKey

  6. in src/pubkey.h:54 in ed1483b712 outdated
      55 |       */
      56 |      static_assert(
      57 | -        PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
      58 | -        "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
      59 | +        SIZE >= COMPRESSED_SIZE,
      60 | +        "COMPRESSED_SIZE is larger than SIZE");
    


    devrandom commented at 5:40 PM on February 3, 2018:

    maybe reword to "COMPRESSED_SIZE must be smaller than SIZE"

  7. in src/key.h:41 in ed1483b712 outdated
      40 |       */
      41 |      static_assert(
      42 | -        PRIVATE_KEY_SIZE >= COMPRESSED_PRIVATE_KEY_SIZE,
      43 | -        "COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
      44 | +        SIZE >= COMPRESSED_SIZE,
      45 | +        "COMPRESSED_SIZE is larger than SIZE");
    


    devrandom commented at 5:41 PM on February 3, 2018:

    maybe reword

  8. devrandom commented at 6:07 PM on February 3, 2018: none

    Although this is good cleanup overall, I would revert the change to IsValid. See inline comment.

  9. Empact force-pushed on Feb 4, 2018
  10. Empact force-pushed on Feb 4, 2018
  11. Empact force-pushed on Feb 4, 2018
  12. Empact force-pushed on Feb 4, 2018
  13. Empact renamed this:
    Add size checking to CPubKey::IsValid, and prefer key/sig size const references
    Add header-sensitive size checking to CPubKey::IsValid, and prefer key/sig size const references
    on Feb 4, 2018
  14. Empact force-pushed on Feb 4, 2018
  15. Empact renamed this:
    Add header-sensitive size checking to CPubKey::IsValid, and prefer key/sig size const references
    Add header-sensitive size checking via CPubKey::ValidSize, and prefer key/sig size const references
    on Feb 4, 2018
  16. devrandom commented at 7:57 PM on February 4, 2018: none

    CI failed...

  17. Empact force-pushed on Feb 5, 2018
  18. Empact force-pushed on Feb 5, 2018
  19. Empact force-pushed on Feb 5, 2018
  20. Empact commented at 5:21 AM on February 5, 2018: member

    @devrandom Fixed

  21. Empact force-pushed on Feb 6, 2018
  22. Empact force-pushed on Feb 8, 2018
  23. Extract CPubKey::SIGNATURE_SIZE and COMPACT_SIGNATURE_SIZE
    As CPubKeySig::SIZE and COMPACT_SIZE
    85751349d1
  24. Rename CPubKey::PUBLIC_KEY_SIZE and COMPRESSED_PUBLIC_KEY_SIZE
    As SIZE and COMPRESSED_SIZE
    7d4e713cf7
  25. Rename CKey::PRIVATE_KEY_SIZE and COMPRESSED_PRIVATE_KEY_SIZE
    To SIZE and COMPRESSED_SIZE
    1ff95cc7cf
  26. Assert CPubKey::ValidLength to the pubkey's header-relevent size
    Previously this was an inline test where the specificity was probably judged
    overly specific. As a class method it makes sense to maintain consistency.
    80a2c3aed7
  27. Assert compressed / compact keys and sigs are strictly shorter than regular
    This is currently true and intuitively expected
    
    Reword COMPRESSED_SIZE static assert message to make the desired arrangement
    explicit.
    6d024f82ce
  28. Empact force-pushed on Feb 16, 2018
  29. Empact commented at 8:40 PM on February 16, 2018: member

    Split this up for easier review. Closing in favor of #12461 #12460 and #12459.

  30. Empact closed this on Feb 16, 2018

  31. Empact deleted the branch on Feb 16, 2018
  32. MarcoFalke locked this on Sep 8, 2021
Contributors

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 06:15 UTC

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