Skip to content

Commit 99fc735

Browse files
authored
test(poseidon1): improve permutation.py coverage to 100% (leanEthereum#540)
- Add Poseidon1Params validation tests (invalid mds_first_row, invalid round_constants) - Add Poseidon1 engine tests (wrong state length, determinism, structural) - Add NUMBA_DISABLE_JIT=1 to tox.ini base [testenv] for coverage tracking Fixes leanEthereum#515
1 parent 6f1adb8 commit 99fc735

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

tests/lean_spec/subspecs/poseidon1/test_permutation.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,64 @@ def test_permutation_vector(
9999
assert output_state == expected_output, (
100100
f"Permutation output for width {params.width} did not match."
101101
)
102+
103+
104+
class TestPoseidon1ParamsValidation:
105+
"""Tests for Poseidon1Params validation."""
106+
107+
def test_invalid_mds_first_row_length(self) -> None:
108+
"""Raises error when mds_first_row length doesn't match width."""
109+
with pytest.raises(ValueError, match="Length of mds_first_row must equal width"):
110+
Poseidon1Params(
111+
width=3,
112+
rounds_f=8,
113+
rounds_p=20,
114+
mds_first_row=[Fp(1), Fp(2)],
115+
round_constants=[Fp(1)] * 84,
116+
)
117+
118+
def test_invalid_round_constants_count(self) -> None:
119+
"""Raises error when round_constants count is incorrect."""
120+
with pytest.raises(ValueError, match="Incorrect number of round constants"):
121+
Poseidon1Params(
122+
width=3,
123+
rounds_f=8,
124+
rounds_p=20,
125+
mds_first_row=[Fp(1), Fp(2), Fp(3)],
126+
round_constants=[Fp(1)] * 20,
127+
)
128+
129+
130+
class TestPoseidon1Engine:
131+
"""Tests for Poseidon1 engine."""
132+
133+
def test_permute_wrong_state_length_too_short(self) -> None:
134+
"""Raises error when input state is too short."""
135+
engine = Poseidon1(PARAMS_16)
136+
with pytest.raises(ValueError, match="Input state must have length 16"):
137+
engine.permute([Fp(1)] * 10)
138+
139+
def test_permute_wrong_state_length_too_long(self) -> None:
140+
"""Raises error when input state is too long."""
141+
engine = Poseidon1(PARAMS_16)
142+
with pytest.raises(ValueError, match="Input state must have length 16"):
143+
engine.permute([Fp(1)] * 20)
144+
145+
def test_permute_determinism(self) -> None:
146+
"""Same input produces same output."""
147+
engine = Poseidon1(PARAMS_16)
148+
state = [Fp(value=i) for i in range(16)]
149+
150+
output1 = engine.permute(state)
151+
output2 = engine.permute(state)
152+
153+
assert output1 == output2
154+
155+
def test_permute_output_differs_from_input(self) -> None:
156+
"""Permutation changes the state."""
157+
engine = Poseidon1(PARAMS_16)
158+
state = [Fp(value=i) for i in range(16)]
159+
160+
output = engine.permute(state)
161+
162+
assert output != state

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ env_list =
1313
uv_python = >=3.12
1414
package = editable
1515
runner = uv-venv-lock-runner
16+
set_env =
17+
NUMBA_DISABLE_JIT = 1
1618

1719
[testenv:all-checks]
1820
description = Run all quality checks (lint, typecheck, spellcheck, mdformat)

0 commit comments

Comments
 (0)