refactor(koalabear): make Fp an int subclass and tighten field surface#776
Merged
tcoratger merged 3 commits intoMay 25, 2026
Merged
Conversation
Switches Fp from a HAS-A wrapper around `self.value: int` to an IS-A scalar inheriting from `int`, matching the BaseUint primitive pattern. Modular inverse now uses the stdlib `pow(a, -1, P)` form instead of explicit Fermat exponentiation. Errors route through the project SSZ exception hierarchy. Strict typing on arithmetic with reverse-operator guards prevents silent int fallback. A Pydantic core-schema hook lets Fp sit directly inside Pydantic SSZ containers. Call sites in xmss and poseidon1 drop their `.value` reads in favor of `int(fp)`, and the JSON-element helper in types/collections collapses the `IntFieldElement` protocol into a plain `isinstance(item, int)` check that also covers BaseUint. Test coverage of field.py reaches 100% (11 -> 55 tests). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces substring matches with anchored regex assertions so every pytest.raises captures the exact error string the implementation produces, not just a prefix or middle slice. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drops the intermediate `expected` local so every error-message assertion uses the same inline `match=` shape across the file. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fpfrom a HAS-A wrapper (self.value: int) to an IS-A scalar (class Fp(int, SSZType)) matching the BaseUint primitive pattern. Drops the.valueindirection at every call site.inverse()with the stdlibpow(a, -1, P)form (modular inverse via extended Euclidean).ValueError/TypeErrorto the project's SSZ exception hierarchy (SSZTypeError,SSZSerializationError,SSZValueError).int + Fpraises instead of silently returning a plainint.Fp(5) == 5returnsFalse, doesn't raise) so collection membership checks still work;__hash__mixes in the type to keepFp(5)and5distinct in sets.__get_pydantic_core_schema__hook: instances pass through, raw ints are strict-validated against[0, P), JSON round-trips drop the subtype.Migration
xmss/message_hash.py,poseidon1/permutation.py, and theconsensus_testingposeidon fixture drop theirfp.valuereads in favor ofint(fp).types/collections.pycollapses the now-deadIntFieldElementprotocol into a plainisinstance(item, int)check that also coversBaseUint.Test coverage
tests/lean_spec/subspecs/koalabear/test_field.py: 11 -> 55 tests, branch coverage 84% -> 100%.__new__rejection of non-ints, modular reduction at boundary inputs, reverse-operator rejection,__pow__edges (zero base, modular exponent), inverse multiplicative identity, full equality matrix, hash-mixing, Pydantic schema lift/reject/round-trip, and theP - 1boundary in serialize/deserialize.Test plan
uv run pytest— full suite (2956 tests pass)uv run pytest tests/lean_spec/subspecs/koalabear/ --cov=src/lean_spec/subspecs/koalabear/field --cov-report=term-missing— 100% coverage🤖 Generated with Claude Code