hash: Make code agnostic of endianness #1093

pull real-or-random wants to merge 3 commits into bitcoin-core:master from real-or-random:202203-endian changing 4 files +55 −59
  1. real-or-random commented at 9:55 am on March 25, 2022: contributor

    Recent compilers compile the two new functions to very efficient code on various platforms. In particular, already GCC >= 5 and clang >= 5 understand do this for the read function, which is the one critical for performance (called 16 times per SHA256 transform).

    Fixes #1080.

  2. real-or-random commented at 9:56 am on March 25, 2022: contributor
    See https://gcc.godbolt.org/z/5ovzd49z1. We can benchmark this but I don’t think it’s necessary to be honest. SHA256 performance anyway is not our primary goal (at least with the current code)
  3. hash: Make code agnostic of endianness
    Recent compilers compile the two new functions to very efficient code
    on various platforms. In particular, already GCC >= 5 and clang >= 5
    understand do this for the read function, which is the one critical
    for performance (called 16 times per SHA256 transform).
    
    Fixes #1080.
    8d89b9e6e5
  4. util: Remove endianness detection 616b43dd3b
  5. real-or-random force-pushed on Mar 25, 2022
  6. tests: Add tests for _read_be32 and _write_be32 37d36927df
  7. sipa commented at 3:31 pm on March 28, 2022: contributor
    utACK 37d36927dff793e61df6d6f6a6c50e8fa2519e33
  8. robot-dreams commented at 6:51 pm on March 28, 2022: contributor

    ACK 37d36927dff793e61df6d6f6a6c50e8fa2519e33

    Glad you caught the int promotion issue when shifting. Sanity checked by searching for strings BE32, ENDIAN, chunk, ->buf to confirm nothing else needed to be changed. Benchmark anecdata on my machine looks reasonable:

    0# 1093
    1hash_sha256                   ,     0.197     ,     0.198     ,     0.202
    2hash_hmac_sha256              ,     0.786     ,     0.788     ,     0.798
    3hash_rfc6979_hmac_sha256      ,     4.29      ,     4.30      ,     4.31
    4
    5# master
    6hash_sha256                   ,     0.212     ,     0.214     ,     0.216
    7hash_hmac_sha256              ,     0.841     ,     0.846     ,     0.854
    8hash_rfc6979_hmac_sha256      ,     4.30      ,     4.50      ,     4.64
    
  9. real-or-random merged this on Mar 28, 2022
  10. real-or-random closed this on Mar 28, 2022

  11. in src/util.h:318 in 37d36927df
    312@@ -338,4 +313,20 @@ static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x) {
    313 #endif
    314 }
    315 
    316+/* Read a uint32_t in big endian */
    317+SECP256K1_INLINE static uint32_t secp256k1_read_be32(const unsigned char* p) {
    318+    return (uint32_t)p[0] << 24 |
    


    JeremyRubin commented at 7:43 pm on March 28, 2022:
    can you add more parens around the cast

    real-or-random commented at 7:56 am on March 29, 2022:
    I can do it in a follow up PR if you insist but the code is correct given C’s operator precedence.

    JeremyRubin commented at 3:41 pm on March 29, 2022:

    like this:

    0(((uint32_t)p[0]) << 24) |
    1(((uint32_t)p[1]) << 16) |
    2(((uint32_t)p[2]) << 8) |
    3((uint32_t)p[3])
    

    is much easier to read IMO


    real-or-random commented at 11:08 pm on March 29, 2022:

    Yeah, I agree, it may be easier to review for some reviewers because they don’t need to to spend time looking up the rules. But given that it has been merged (i.e., review has been done), I don’t think it’s worth spending a PR on this. Feel free to open a PR if you disagree.

    (Sorry, I hadn’t seen your comment earlier. I was spending some time testing this locally before pushing the merge. Your comment arrived after I had created the merge commit locally, but I believe before I pushed it. Then I didn’t check github again before pushing the merge.)

  12. elichai commented at 12:21 pm on March 29, 2022: contributor
    Post merge review ACK 37d36927dff793e61df6d6f6a6c50e8fa2519e33 Good to see less ifdefs as a side effect :)
  13. fanquake referenced this in commit 465d05253a on Mar 30, 2022
  14. real-or-random referenced this in commit 6c0aecf72b on Apr 1, 2022
  15. fanquake referenced this in commit afb7a6fe06 on Apr 6, 2022
  16. gwillen referenced this in commit 35d6112a72 on May 25, 2022
  17. patricklodder referenced this in commit 21badcf9d2 on Jul 25, 2022
  18. patricklodder referenced this in commit 03002a9013 on Jul 28, 2022
  19. janus referenced this in commit 3a0652a777 on Aug 4, 2022
  20. str4d referenced this in commit 522190d5c3 on Apr 21, 2023
  21. vmta referenced this in commit e1120c94a1 on Jun 4, 2023
  22. theStack cross-referenced this on Jun 10, 2023 from issue scalar: refactor: use `secp256k1_{read,write}_be32` helpers by theStack
  23. real-or-random referenced this in commit 67214f5f7d on Jun 12, 2023
  24. vmta referenced this in commit 8f03457eed on Jul 1, 2023

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: 2024-11-22 08:15 UTC

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