From 9ea816f8320e0937af8e137d1a45bf9642ec2173 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 3 Nov 2023 09:25:24 +0100 Subject: [PATCH] fix: Make inc_nonce overflow at the correct value --- secret_handshake/util.py | 2 +- tests/test_util.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/secret_handshake/util.py b/secret_handshake/util.py index 429f798..2f2e8e7 100644 --- a/secret_handshake/util.py +++ b/secret_handshake/util.py @@ -35,7 +35,7 @@ def inc_nonce(nonce: bytes) -> bytes: num = bytes_to_long(nonce) + 1 - if num > 2**MAX_NONCE: + if num > 2**MAX_NONCE - 1: num = 0 bnum = long_to_bytes(num) diff --git a/tests/test_util.py b/tests/test_util.py index 7928c14..b43b036 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -33,7 +33,13 @@ from secret_handshake.util import bytes_to_long, inc_nonce, long_to_bytes, split T = TypeVar("T") -@pytest.mark.parametrize("in_,out", ((b"\x00\x00\x00\x00", b"\x00" * 23 + b"\x01"),)) +@pytest.mark.parametrize( + "in_,out", + ( + (b"\x00\x00\x00\x00", b"\x00" * 23 + b"\x01"), + (b"\xff" * 24, b"\x00" * 24), + ), +) def test_inc_nonce(in_: bytes, out: bytes) -> None: """Test the inc_nonce function"""