Py-ed25519-zebra-bindings
Python bindings for the `ed25519-zebra` Rust crate, providing high-performance cryptographic operations for Ed25519 digital signatures. It's currently at version 1.3.0 and has a moderate release cadence, driven by updates to the underlying Rust dependencies and Python version support, ensuring compatibility and performance.
Common errors
-
ModuleNotFoundError: No module named 'ed25519_zebra'
cause The package `py-ed25519-zebra-bindings` was installed, but the Python module it provides is named `ed25519_zebra`. Or the package was not installed at all.fixEnsure correct installation and import: `pip install py-ed25519-zebra-bindings` then `from ed25519_zebra import ...`. -
TypeError: argument 'message': 'str' object cannot be converted to 'bytes'
cause Cryptographic functions within `ed25519_zebra` require messages to be `bytes`, not `str`.fixEncode your string to bytes, e.g., `message.encode('utf-8')` or use a byte literal `b"my message"`. -
ERROR: Could not find a version that satisfies the requirement py-ed25519-zebra-bindings (from versions: none)
cause No pre-built wheels are available for your specific Python version and/or operating system/architecture. This often happens on less common platforms or with very new Python versions before wheels are released.fixCheck PyPI for available wheels (`https://pypi.org/project/py-ed25519-zebra-bindings/#files`). If none, ensure you have a Rust toolchain installed to build from source, or use a supported Python version/platform. Alternatively, you might see `error: can't find Rust compiler` if a source build is attempted without Rust.
Warnings
- gotcha Type Mismatches for Cryptographic Operations. Functions like `sign` and `verify` expect byte strings (`bytes`) for messages and specific key/signature objects (`SecretKey`, `PublicKey`, `Signature`). Passing regular `str` objects or incorrect types will lead to `TypeError`.
- breaking Minimum Python Version. The library requires Python 3.9 or higher. Attempting to install or run on older Python versions will result in installation failures or runtime errors.
- gotcha Source Compilation Requirements. If pre-built wheels are not available for your specific OS/architecture/Python version, `pip` will attempt to build the library from source, which requires a Rust toolchain (Rust compiler, Cargo).
- breaking New Functions in Newer Versions. The `ed_public_from_secret` function was added in v1.1.0. Attempting to use this function in versions prior to 1.1.0 will result in an `AttributeError`.
Install
-
pip install py-ed25519-zebra-bindings
Imports
- generate_keypair
from ed25519_zebra import generate_keypair
- sign
from ed25519_zebra import sign
- verify
from ed25519_zebra import verify
- PublicKey
from ed25519_zebra import PublicKey
- SecretKey
from ed25519_zebra import SecretKey
- ed_public_from_secret
from ed25519_zebra import ed_public_from_secret
Quickstart
from ed25519_zebra import generate_keypair, sign, verify, PublicKey, SecretKey, ed_public_from_secret
# Generate a new keypair
secret_key, public_key = generate_keypair()
print(f"Secret Key: {secret_key.to_bytes().hex()}")
print(f"Public Key: {public_key.to_bytes().hex()}")
# Message to sign (must be bytes)
message = b"This is a test message to be signed."
# Sign the message
signature = sign(secret_key, message)
print(f"Signature: {signature.to_bytes().hex()}")
# Verify the signature
is_valid = verify(public_key, signature, message)
print(f"Signature valid: {is_valid}")
# Example of converting bytes to key objects
public_key_bytes = public_key.to_bytes()
secret_key_bytes = secret_key.to_bytes()
reconstructed_public_key = PublicKey.from_bytes(public_key_bytes)
reconstructed_secret_key = SecretKey.from_bytes(secret_key_bytes)
print(f"Reconstructed Public Key Matches: {reconstructed_public_key == public_key}")
print(f"Reconstructed Secret Key Matches: {reconstructed_secret_key == secret_key}")
# Derived public key from secret (available from v1.1.0+)
derived_public_key = ed_public_from_secret(secret_key)
print(f"Derived Public Key from Secret Matches: {derived_public_key == public_key}")