Metadata-Version: 2.4
Name: safe-eth-native
Version: 0.2.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
License-File: LICENSE
Summary: Native ecrecover, P-256 verify, keccak256 and EIP-55 checksum helpers for safe-eth-py
Author-email: Uxío <uxio@safe.global>
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/safe-global/safe-eth-py
Project-URL: Source, https://github.com/safe-global/safe-eth-py/tree/main/native
Project-URL: Tracker, https://github.com/safe-global/safe-eth-py/issues

# safe-eth-native

Native (Rust) versions of the hot paths of [safe-eth-py](https://github.com/safe-global/safe-eth-py):

| Function | Replaces | Backed by |
| --- | --- | --- |
| `ecrecover(message_hash, v, r, s) -> bytes \| None` | eth-keys `ecdsa_recover` | [libsecp256k1](https://github.com/bitcoin-core/secp256k1) |
| `p256_verify(message_hash, r, s, public_key_x, public_key_y) -> bool` | `safe_eth.safe.p256.verify` | RustCrypto [`p256`](https://github.com/RustCrypto/elliptic-curves) |
| `keccak256(data) -> bytes` | pysha3 `keccak_256` | [`tiny-keccak`](https://github.com/debris/tiny-keccak) |
| `to_checksum_address(value) -> str` | `fast_to_checksum_address` | |
| `is_checksum_address(value) -> bool` | `fast_is_checksum_address` | |

`ecrecover` and `p256_verify` follow the input rules of the EVM `ecrecover` and `P256VERIFY`
precompiles: `v` must be 27 or 28, `r` and `s` in `[1, n - 1]`, and high `s` values are
accepted. Values out of those ranges return `None` / `False` instead of raising; only a
wrong type, or a hash that is not 32 bytes for `ecrecover`, raises. Both release the GIL
while they run, and so does `keccak256` for inputs of 2048 bytes or more. `keccak256` hashes
the raw bytes of `bytes` or of any C-contiguous buffer, like `hashlib`.

The package is not meant to be used directly. Install it with the `native` extra of
safe-eth-py, which uses it when it is installed and falls back to the Python code when it is
not:

```bash
pip install "safe-eth-py[native]"
```

## Wheels

Platforms: Linux (glibc 2.28+ and musl) x86_64 and aarch64, macOS x86_64 and arm64, and
Windows x86_64. For each one:

- One `abi3` wheel for CPython 3.11 and newer, 3.15 included
- One wheel per free-threaded CPython version, 3.14t and 3.15t, as `abi3` wheels do not load
  on free-threaded builds. The module runs without the GIL: importing it does not enable the
  GIL again

Other platforms and Python versions build from the sdist, which needs a Rust toolchain and a
C compiler.

## Development

From the root of the safe-eth-py repository, `uv sync --group dev --all-extras --frozen` builds the
extension and installs it in the safe-eth-py environment, and `pytest native/tests` runs
its tests. uv rebuilds it when a file in `src/` changes.

The tests compare every function with reference implementations (eth-keys, eth-utils,
pycryptodome, and pysha3 in the safe-eth-py tests) and run the vectors of the Keccak team,
Wycheproof, EIP-55, and the test suites of go-ethereum, eth-keys, eth-utils, coincurve and
libsecp256k1. See
[`tests/vectors`](https://github.com/safe-global/safe-eth-py/blob/main/native/tests/vectors/README.md)
for their sources. CI runs them on every wheel except the musl ones, which are built but not
tested.

## Releasing

Bump `version` in `Cargo.toml`, then push a `native-v<version>` tag on a commit of `main`. Push
only the tag: a GitHub release would also run the safe-eth-py release workflow, for nothing.
The `Native extension` workflow builds the wheels and the sdist and publishes them to PyPI with
trusted publishing. Before the first release, add a trusted publisher (a pending one while the
project does not exist) on PyPI for `safe-global/safe-eth-py` and the `native.yml` workflow.

