Skip to content

Fix __pow__ recursion for s390x compatibility#160

Open
ottok wants to merge 1 commit intoethereum:mainfrom
ottok:fix-s390x
Open

Fix __pow__ recursion for s390x compatibility#160
ottok wants to merge 1 commit intoethereum:mainfrom
ottok:fix-s390x

Conversation

@ottok
Copy link
Copy Markdown

@ottok ottok commented Mar 28, 2026

What was wrong?

In Debian, where the tests of this Python package across multiple architectures, it was discovered that the pairing tests fail on s390x.

Supercedes PR #159.

How was it fixed?

Fix the recursion issue by converting FQP.pow from a recursive to an iterative square-and-multiply implementation. The new implementation is mathematically equivalent, handles negative exponents (which the old version didn't), and won't hit maximum recursion depth on large exponents. With this change tests are fully passing on architecture s390x in Debian.

Additionally, also convert the FQ classes to use the same iterative square-and-multiply algorithm. Thehey were not seen failing on s390, but probably because tests only run with smaller exponents on those functions.

Proof that latest version also has all tests passing on s390x: https://autopkgtest.ubuntu.com/results/autopkgtest-resolute-otto-ppa/resolute/s390x/p/python-py-ecc/20260328_153048_a9f0e@/log.gz

Todo:

  • Clean up commit history
  • N/A Add or update documentation related to these changes
  • N/A Add entry to the release notes

Cute Animal Picture

🐱

@ottok
Copy link
Copy Markdown
Author

ottok commented Mar 28, 2026

CI failed on these:

run mypy with all dev dependencies present...............................Failed
- hook id: mypy-local
- exit code: 1

py_ecc/fields/optimized_field_elements.py:162: error: Returning Any from function declared to return "T_FQ"  [no-any-return]
py_ecc/fields/optimized_field_elements.py:162: error: "T_FQ" has no attribute "inv"  [attr-defined]
py_ecc/fields/field_elements.py:150: error: Returning Any from function declared to return "T_FQ"  [no-any-return]
py_ecc/fields/field_elements.py:150: error: "T_FQ" has no attribute "inv"  [attr-defined]
Found 4 errors in 2 files (checked 32 source files)

I will fix them tomorrow.

In Debian, where the tests of this Python package across multiple
architectures, it was discovered that the pairing tests fail on s390x.

Fix the recursion issue by converting FQP.__pow__ from a recursive to an
iterative square-and-multiply implementation. The new implementation is
mathematically equivalent, handles negative exponents (which the old
version didn't), and won't hit maximum recursion depth on large
exponents. With this change tests are fully passing on architecture
s390x in Debian.

Additionally, also convert the FQ classes to use the same iterative
square-and-multiply algorithm. Thehey were not seen failing on s390, but
probably because tests only run with smaller exponents on those
functions.

Also add the `inv` method to both `FQ` classes so that mypy can properly
type check the negative exponent handling in `__pow__` by calling was
calling `self.inv()`.

This is needed to avoid `mypy` failing on:

    py_ecc/fields/optimized_field_elements.py:162: error: Returning Any from function declared to return "T_FQ"  [no-any-return]
    py_ecc/fields/optimized_field_elements.py:162: error: "T_FQ" has no attribute "inv"  [attr-defined]
    py_ecc/fields/field_elements.py:150: error: Returning Any from function declared to return "T_FQ"  [no-any-return]
    py_ecc/fields/field_elements.py:150: error: "T_FQ" has no attribute "inv"  [attr-defined]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant