Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/halmos/bitvec.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,9 @@ def addmod(
assert size == modulus.size

if self.is_concrete and other.is_concrete and modulus.is_concrete:
# mod by zero is zero
if modulus.value == 0:
return HalmosBitVec(0, size=size)
return HalmosBitVec((self.value + other.value) % modulus.value, size=size)

# to avoid add overflow; and to be a multiple of 8-bit
Expand Down Expand Up @@ -762,6 +765,9 @@ def mulmod(
assert size == modulus.size

if self.is_concrete and other.is_concrete and modulus.is_concrete:
# mod by zero is zero
if modulus.value == 0:
return HalmosBitVec(0, size=size)
return HalmosBitVec((self.value * other.value) % modulus.value, size=size)

# to avoid mul overflow
Expand Down
14 changes: 14 additions & 0 deletions tests/test_bitvec.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,17 @@ def test_bool_hashable():
assert y not in dict
assert TRUE not in dict
assert FALSE in dict


@pytest.mark.parametrize(
"a, b, n, expected", [(2, 3, 0, 0), (2, 3, 4, 1), (7, 5, 1, 0)]
)
def test_addmod_concrete(a, b, n, expected):
assert int(BV(a).addmod(BV(b), BV(n))) == expected


@pytest.mark.parametrize(
"a, b, n, expected", [(2, 3, 0, 0), (2, 3, 4, 2), (7, 5, 1, 0)]
)
def test_mulmod_concrete(a, b, n, expected):
assert int(BV(a).mulmod(BV(b), BV(n))) == expected