FastRandomContext improvements and switch to ChaCha20 #9792

pull sipa wants to merge 5 commits into bitcoin:master from sipa:chacha changing 20 files +482 −44
  1. sipa commented at 3:08 am on February 18, 2017: member

    This switches FastRandomContext to use a ChaCha20-based random number generator. It also makes the class richer by adding support for getting single bits of entropy.

    Benchmarks (also added) show that rand32 became around 5.25x slower on my machine (from 1.5ns to 8ns), but the new randbool is 15% faster than the old one (1.3ns).

  2. gmaxwell commented at 3:14 am on February 18, 2017: contributor

    src/addrman.cpp: nKBucket = (nKBucket + insecure_rand.rand32()) % ADDRMAN_TRIED_BUCKET_COUNT; src/addrman.cpp: nKBucketPos = (nKBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE; src/addrman.cpp: nUBucket = (nUBucket + insecure_rand.rand32()) % ADDRMAN_NEW_BUCKET_COUNT; src/addrman.cpp: nUBucketPos = (nUBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE; src/bench/checkqueue.cpp: p.resize(insecure_rand.rand32() % (PREVECTOR_SIZE*2)); src/net.h: vAddrToSend[insecure_rand.rand32() % vAddrToSend.size()] = _addr;

    Usage wants a randrange a lot more than a rand32

  3. fanquake added the label Refactoring on Feb 18, 2017
  4. in src/test/crypto_tests.cpp: in ca6b170e15 outdated
    439@@ -439,4 +440,29 @@ BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {
    440                   "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644");
    441 }
    442 
    443+BOOST_AUTO_TEST_CASE(chacha20_testvector)
    


    jonasschnelli commented at 7:31 pm on February 19, 2017:
    Maybe add all test vectors (only 5) from the IEFT draft specs: https://github.com/jonasschnelli/chacha20poly1305/blob/master/tests.c#L35
  5. jonasschnelli commented at 7:32 pm on February 19, 2017: contributor
    Concept ACK
  6. sipa force-pushed on Feb 19, 2017
  7. sipa force-pushed on Feb 20, 2017
  8. sipa commented at 6:48 am on February 20, 2017: member
    Added randrange and the test vectors @jonasschnelli suggested.
  9. in src/test/prevector_tests.cpp: in 561fe7a3f0 outdated
    182@@ -183,11 +183,8 @@ class prevector_tester {
    183     }
    184 
    185     ~prevector_tester() {
    186-        BOOST_CHECK_MESSAGE(passed, "insecure_rand_Rz: "
    


    TheBlueMatt commented at 6:22 pm on February 22, 2017:
    Looks like this disabled a bunch of tests?

    sipa commented at 9:57 am on February 25, 2017:
    Oops, nice catch. Fixed.
  10. in configure.ac: in b50ff222c9 outdated
    532@@ -533,6 +533,9 @@ AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
    533                  #include <byteswap.h>
    534                  #endif])
    535 
    536+AC_MSG_CHECKING(for __builtin_clzl)
    


    TheBlueMatt commented at 6:35 pm on February 22, 2017:
    Hmm…is it definitely the case that all the compilers we support have this? Can we not have some fallback for those that do not?

    sipa commented at 9:57 am on February 25, 2017:
    I don’t know of any compilers that don’t. I’d be happy to write a fallback if there is one that isn’t.

    laanwj commented at 10:13 am on February 25, 2017:
    I think MSVC doesn’t.
  11. in src/random.h: in 4635e35667 outdated
    34@@ -35,6 +35,7 @@ void GetStrongRandBytes(unsigned char* buf, int num);
    35  */
    36 class FastRandomContext {
    37 private:
    38+    bool requires_seed;
    


    TheBlueMatt commented at 6:37 pm on February 22, 2017:
    …except you never check requires_seed to do the actual seeding?

    sipa commented at 9:57 am on February 25, 2017:
    Fixed, and added a test to catch that.
  12. in src/random.h: in b50ff222c9 outdated
    80@@ -81,6 +81,16 @@ class FastRandomContext {
    81         }
    82     }
    83 
    84+    unsigned long randrange(unsigned long range)
    85+    {
    86+        --range;
    87+        int bits = 8 * sizeof(long) - __builtin_clzl(range);
    


    gmaxwell commented at 6:12 pm on February 23, 2017:
    CLZ is undefined for 0, a range of 1 is dumb but might be mechanically generated in some case. At a minimum there should be a comment that range must be greater than 1.

    sipa commented at 8:42 pm on February 25, 2017:
    Fixed.
  13. sipa force-pushed on Feb 25, 2017
  14. sipa force-pushed on Feb 25, 2017
  15. sipa commented at 8:41 pm on February 25, 2017: member
    Added a wrapper for __builtin_clzl, added unit tests, and fixed a few edge cases.
  16. sipa force-pushed on Feb 26, 2017
  17. sipa force-pushed on Feb 27, 2017
  18. sipa force-pushed on Feb 27, 2017
  19. in src/random.h: in 3ea7f9e657 outdated
    89@@ -89,6 +90,16 @@ class FastRandomContext {
    90         }
    91     }
    92 
    93+    uint64_t randrange(uint64_t range)
    


    gmaxwell commented at 9:44 pm on February 28, 2017:
    This needs a comment that points out that range returned will be [0..range) and that range must not be zero.

    sipa commented at 6:31 pm on March 29, 2017:
    Fixed.
  20. Introduce FastRandomContext::randbool() c21cbe61c6
  21. FastRandom benchmark 663fbae777
  22. Add ChaCha20 e04326fe66
  23. Switch FastRandomContext to ChaCha20 16329224e7
  24. Add a FastRandomContext::randrange and use it 4fd2d2fc97
  25. sipa force-pushed on Mar 29, 2017
  26. sipa commented at 6:31 pm on March 29, 2017: member
    Rebased.
  27. laanwj added this to the "Blockers" column in a project

  28. TheBlueMatt commented at 10:22 pm on April 14, 2017: member
    Looks good to me. I didnt re-verify the chacha code is correct, and dont know that the makefile changes are sane.
  29. gmaxwell approved
  30. gmaxwell commented at 10:11 am on April 15, 2017: contributor
    utACK
  31. laanwj commented at 12:28 pm on April 24, 2017: member
    utACK 4fd2d2f
  32. laanwj merged this on Apr 24, 2017
  33. laanwj closed this on Apr 24, 2017

  34. laanwj referenced this in commit 342b9bc390 on Apr 24, 2017
  35. laanwj removed this from the "Blockers" column in a project

  36. Warrows referenced this in commit 2b23796095 on Feb 23, 2018
  37. PastaPastaPasta referenced this in commit a4e6e8d388 on May 31, 2019
  38. PastaPastaPasta referenced this in commit d9fe4413b2 on Jun 10, 2019
  39. PastaPastaPasta referenced this in commit 9bf7640ff8 on Jun 10, 2019
  40. PastaPastaPasta referenced this in commit 58594da9dc on Jun 11, 2019
  41. PastaPastaPasta referenced this in commit 40c36af45d on Jun 11, 2019
  42. PastaPastaPasta referenced this in commit e658fe99ef on Jun 12, 2019
  43. PastaPastaPasta referenced this in commit 8a0997d7d2 on Jun 14, 2019
  44. PastaPastaPasta referenced this in commit 198558a362 on Jun 14, 2019
  45. PastaPastaPasta referenced this in commit dc6e9ceb58 on Jun 15, 2019
  46. PastaPastaPasta referenced this in commit 33b9818e7e on Jun 19, 2019
  47. PastaPastaPasta referenced this in commit 8603e60594 on Jun 19, 2019
  48. barrystyle referenced this in commit e7a44fb4fa on Jan 22, 2020
  49. zkbot referenced this in commit aa225ebb0b on Jan 24, 2020
  50. zkbot referenced this in commit 74ff73abab on Jan 24, 2020
  51. furszy referenced this in commit 4ed15cc69d on Jun 8, 2020
  52. furszy referenced this in commit 07b88da888 on Jan 25, 2021
  53. zkbot referenced this in commit 65122845c5 on Feb 18, 2021
  54. MarcoFalke locked this on Sep 8, 2021

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: 2024-11-17 15:12 UTC

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