Skip to content

Commit be5399e

Browse files
committed
Merge bitcoin/bitcoin#29390: test: speedup bip324_cipher.py unit test
a8c3454 test: speedup bip324_cipher.py unit test (Sebastian Falbesoner) Pull request description: 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 ``` ACKs for top commit: delta1: Concept ACK a8c3454 epiccurious: Tested ACK a8c3454. achow101: ACK a8c3454 marcofleon: ACK a8c3454. 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. cbergqvist: ACK a8c3454! stratospher: ACK a8c3454. I think it's worth it because of the significant speedup in the unit test. Tree-SHA512: 737dd805a850be6e035aa3c6d9e2c5b5b5e89ddc564f84a045c37e0238fef6419912de7c902139b64914abdd647c649fe02a694f1a5e1741d7d4459c041caccc
2 parents 9057598 + a8c3454 commit be5399e

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

test/functional/test_framework/crypto/bip324_cipher.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def pad16(x):
2525

2626
def aead_chacha20_poly1305_encrypt(key, nonce, aad, plaintext):
2727
"""Encrypt a plaintext using ChaCha20Poly1305."""
28+
if plaintext is None:
29+
return None
2830
ret = bytearray()
2931
msg_len = len(plaintext)
3032
for i in range((msg_len + 63) // 64):
@@ -42,7 +44,7 @@ def aead_chacha20_poly1305_encrypt(key, nonce, aad, plaintext):
4244

4345
def aead_chacha20_poly1305_decrypt(key, nonce, aad, ciphertext):
4446
"""Decrypt a ChaCha20Poly1305 ciphertext."""
45-
if len(ciphertext) < 16:
47+
if ciphertext is None or len(ciphertext) < 16:
4648
return None
4749
msg_len = len(ciphertext) - 16
4850
poly1305 = Poly1305(chacha20_block(key, nonce, 0)[:32])
@@ -191,11 +193,11 @@ def test_fschacha20poly1305aead(self):
191193
dec_aead = FSChaCha20Poly1305(key)
192194

193195
for _ in range(msg_idx):
194-
enc_aead.encrypt(b"", b"")
196+
enc_aead.encrypt(b"", None)
195197
ciphertext = enc_aead.encrypt(aad, plain)
196198
self.assertEqual(hex_cipher, ciphertext.hex())
197199

198200
for _ in range(msg_idx):
199-
dec_aead.decrypt(b"", bytes(16))
201+
dec_aead.decrypt(b"", None)
200202
plaintext = dec_aead.decrypt(aad, ciphertext)
201203
self.assertEqual(plain, plaintext)

0 commit comments

Comments
 (0)