test: speedup bip324_cipher.py unit test #29390

pull theStack wants to merge 1 commits into bitcoin:master from theStack:202402-speedup_bip324_cipher_python_tests changing 1 files +5 −3
  1. theStack commented at 12:06 AM on February 6, 2024: contributor

    Executing the unit tests for the bip324_cipher.py module currently takes quite long (>60 seconds on my older notebook). Most time here is spent in empty plaintext/ciphertext encryption/decryption loops in test_fschacha20poly1305aead:

    https://github.com/bitcoin/bitcoin/blob/9eeee7caa3f95ee17a645e12d330261f8e3c2dbf/test/functional/test_framework/crypto/bip324_cipher.py#L193-L194 https://github.com/bitcoin/bitcoin/blob/9eeee7caa3f95ee17a645e12d330261f8e3c2dbf/test/functional/test_framework/crypto/bip324_cipher.py#L198-L199

    Their sole purpose is increasing the FSChaCha20Poly1305 packet counter in order to trigger rekeying, i.e. the actual encryption/decryption is not relevant, as the result is thrown away. This commit speeds up the tests by supporting to pass "None" as plaintext/ciphertext, indicating to the routines that no actual encryption/decryption should be done.

    The approach here is a bit hacky, a cleaner alternative would probably be to introduce a special seek/skip_packets method and not touch the encrypt/decrypt routines, but that seemed overkill to me only for speeding up a unit test. Open for suggestions.

    master branch:

    $ python3 -m unittest ./test/functional/test_framework/crypto/bip324_cipher.py
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 64.658s
    

    PR branch:

    $ python3 -m unittest ./test/functional/test_framework/crypto/bip324_cipher.py
    ..
    ---------------------------------------------------------------------- 
    Ran 2 tests in 0.822s
    
  2. DrahtBot commented at 12:06 AM on February 6, 2024: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK epiccurious, marcofleon, cbergqvist, stratospher, achow101
    Concept ACK delta1

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  3. DrahtBot added the label Tests on Feb 6, 2024
  4. theStack renamed this:
    test: speedup bip324_crypto.py unit test
    test: speedup bip324_cipher.py unit test
    on Feb 6, 2024
  5. test: speedup bip324_cipher.py unit test
    Executing the unit tests for the bip324_cipher.py module currently
    takes quite long (>60 seconds on my notebook). Most time here is spent
    in empty plaintext/ciphertext encryption/decryption loops:
    
        ....
        for _ in range(msg_idx):
            enc_aead.encrypt(b"", b"")
        ...
        for _ in range(msg_idx):
            enc_aead.decrypt(b"", bytes(16))
        ...
    
    Their sole purpose is increasing the FSChaCha20Poly1305 packet
    counters in order to trigger rekeying, i.e. the actual
    encryption/decryption is not relevant, as the result is thrown away.
    This commit speeds up the tests by supporting to pass "None" as
    plaintext/ciphertext, indicating to the routines that no actual
    encryption/decryption should be done.
    
    master branch:
    
    $ python3 -m unittest ./test/functional/test_framework/crypto/bip324_cipher.py
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 64.658s
    
    PR branch:
    
    $ python3 -m unittest ./test/functional/test_framework/crypto/bip324_cipher.py
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.822s
    a8c3454ba1
  6. theStack force-pushed on Feb 6, 2024
  7. jo-elimu commented at 4:28 AM on February 6, 2024: none

    Is this pull request associated with one of the issues at https://github.com/bitcoin/bitcoin/issues?

  8. delta1 commented at 7:48 AM on February 6, 2024: none

    Concept ACK a8c3454

    To run the individual test I had to run it from the test/functional directory this way: python3 -m unittest ./crypto/bip324_cipher.py

    Saves about 10 seconds in test_runner on my machine

    Before:

    Running Unit Tests for Test Framework Modules
    .....................
    ----------------------------------------------------------------------
    Ran 21 tests in 15.701s
    

    After:

    Running Unit Tests for Test Framework Modules
    .....................
    ----------------------------------------------------------------------
    Ran 21 tests in 4.984s
    
  9. epiccurious commented at 1:12 PM on February 6, 2024: contributor

    a cleaner alternative would probably be to introduce a special seek/skip_packets method and not touch the encrypt/decrypt routines

    Why did you choose this approach rather than adding a seek/skip_packet method?

  10. delta1 commented at 2:19 PM on February 6, 2024: none

    Why did you choose this approach rather than adding a seek/skip_packet method? @epiccurious he explained in the very next line:

    that seemed overkill to me only for speeding up a unit test. Open for suggestions

  11. epiccurious commented at 2:33 PM on February 6, 2024: contributor

    Tested ACK a8c3454ba137dfac20b3c89bc558374de0524114.

    Before (commit 9eeee7caa3f95ee17a645e12d330261f8e3c2dbf):

    Ran 2 tests in 15.541s
    

    After:

    Ran 2 tests in 0.204s
    
  12. epiccurious approved
  13. jo-elimu approved
  14. marcofleon commented at 3:41 AM on February 23, 2024: contributor

    ACK a8c3454ba137dfac20b3c89bc558374de0524114. The comments at the top of bip324_cipher.py specify that this should only be used for testing, so I think this optimization makes sense in that context.

    Before:

    Ran 2 tests in 8.512s
    

    After:

    Ran 2 tests in 0.106s
    
  15. in test/functional/test_framework/crypto/bip324_cipher.py:196 in a8c3454ba1
     192 | @@ -191,11 +193,11 @@ def test_fschacha20poly1305aead(self):
     193 |              dec_aead = FSChaCha20Poly1305(key)
     194 |  
     195 |              for _ in range(msg_idx):
     196 | -                enc_aead.encrypt(b"", b"")
     197 | +                enc_aead.encrypt(b"", None)
    


    cbergqvist commented at 12:42 PM on February 23, 2024:

    How about adding an assert before this loop verifying ~msg_idx < REKEY_INTERVAL~ msg_idx > REKEY_INTERVAL to help show intent of the test?

  16. cbergqvist approved
  17. cbergqvist commented at 12:45 PM on February 23, 2024: contributor

    ACK a8c3454!

  18. stratospher commented at 3:54 AM on February 27, 2024: contributor

    ACK a8c3454. I think it's worth it because of the significant speedup in the unit test. @theStack, do you get negligible speedup in other v2 tests too? Maybe these negligible benchmarks aren't reliable but I somehow imagined there to be a very negligible slowdown because of an extra check.

    1. unit test went from 10.807s to 0.16s
    2. test/functional/p2p_v2_encrypted.py went from 7.266s to 5.766s
    3. test_runner.py in #29358 went from 1135s to 1125s
  19. fanquake commented at 11:18 AM on February 27, 2024: member

    cc @sipa

  20. achow101 commented at 8:06 PM on February 29, 2024: member

    ACK a8c3454ba137dfac20b3c89bc558374de0524114

  21. achow101 merged this on Feb 29, 2024
  22. achow101 closed this on Feb 29, 2024

  23. kwvg referenced this in commit a950d64b2d on Oct 15, 2024
  24. kwvg referenced this in commit e0fabd200b on Oct 23, 2024
  25. kwvg referenced this in commit e71bd8702a on Oct 23, 2024
  26. kwvg referenced this in commit 6b4ecd4691 on Oct 23, 2024
  27. PastaPastaPasta referenced this in commit bcbecc2a4d on Oct 24, 2024
  28. kwvg referenced this in commit cfde560f64 on Oct 24, 2024
  29. kwvg referenced this in commit 2455862c9f on Oct 24, 2024
  30. PastaPastaPasta referenced this in commit 2e162da06f on Oct 24, 2024
  31. bitcoin locked this on Feb 28, 2025

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-14 21:13 UTC

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