mirror of
https://github.com/luxfi/crypto.git
synced 2026-10-30 16:59:13 +00:00
Brand stays in import path only; symbols, env vars, and exports are brand-neutral. One-release backwards-compat shim reads deprecated LUX_ names with deprecation log. Env: CRYPTO_BACKEND, CRYPTO_DIR, CRYPTO_BUILD_DIR, CRYPTO_LIB_DIR, CRYPTO_LIB are canonical. Rust: idiomatic Secp256k1Status enum; extern fn names dropped lux_ prefix. TS/Python: cryptoAvailable / crypto_available; FFI symbol strings drop lux_ prefix to match renamed C-ABI in luxcpp/crypto. cabi/main.go: //export names drop lux_ prefix (mlkem768_*, mldsa65_*).
80 lines
1.9 KiB
Python
80 lines
1.9 KiB
Python
"""FFI layer — loads libluxcrypto shared library."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import ctypes.util
|
|
import os
|
|
import sys
|
|
import warnings
|
|
from pathlib import Path
|
|
|
|
_LIB: ctypes.CDLL | None = None
|
|
crypto_available = False
|
|
|
|
|
|
def _env_lib() -> str | None:
|
|
"""Read CRYPTO_LIB; fall back to deprecated LUX_CRYPTO_LIB for one release."""
|
|
v = os.environ.get("CRYPTO_LIB")
|
|
if v:
|
|
return v
|
|
v = os.environ.get("LUX_CRYPTO_LIB")
|
|
if v:
|
|
warnings.warn(
|
|
"LUX_CRYPTO_LIB is deprecated; use CRYPTO_LIB",
|
|
DeprecationWarning,
|
|
stacklevel=2,
|
|
)
|
|
return v
|
|
return None
|
|
|
|
|
|
def _find_lib() -> str | None:
|
|
"""Find libluxcrypto on the system."""
|
|
# 1. Env var override
|
|
env = _env_lib()
|
|
if env and os.path.isfile(env):
|
|
return env
|
|
|
|
# 2. Adjacent to this package (e.g. wheel ships the .dylib/.so)
|
|
here = Path(__file__).parent
|
|
for name in ("libluxcrypto.dylib", "libluxcrypto.so", "libluxcrypto.dll"):
|
|
p = here / name
|
|
if p.exists():
|
|
return str(p)
|
|
|
|
# 3. In the lux/crypto/dist build output
|
|
dist = Path.home() / "work" / "lux" / "crypto" / "dist"
|
|
for name in ("libluxcrypto.dylib", "libluxcrypto.so", "libluxcrypto.dll"):
|
|
p = dist / name
|
|
if p.exists():
|
|
return str(p)
|
|
|
|
# 4. System library path
|
|
found = ctypes.util.find_library("luxcrypto")
|
|
return found
|
|
|
|
|
|
def _load() -> ctypes.CDLL | None:
|
|
path = _find_lib()
|
|
if path is None:
|
|
return None
|
|
try:
|
|
return ctypes.CDLL(path)
|
|
except OSError:
|
|
return None
|
|
|
|
|
|
_LIB = _load()
|
|
crypto_available = _LIB is not None
|
|
|
|
|
|
def get_lib() -> ctypes.CDLL:
|
|
"""Get the loaded library, raising if unavailable."""
|
|
if _LIB is None:
|
|
raise RuntimeError(
|
|
"libluxcrypto not found. Build it with: "
|
|
"cd ~/work/lux/crypto && make lib"
|
|
)
|
|
return _LIB
|