From a34121d3c6e13bb6499c1c7b4cfd7e75465e53bf Mon Sep 17 00:00:00 2001 From: SimplifiedPrivacy Date: Tue, 22 Sep 2026 17:43:21 -0400 Subject: [PATCH] Miller loop loaded on appimage via an .so C module. --- core/services/crypto/TicketCustomer.py | 16 +++----- core/services/helpers/load_packages.py | 57 ++++++++++++++++++++++++++ core/utils/run_commands.py | 1 - pyproject.toml | 2 + 4 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 core/services/helpers/load_packages.py diff --git a/core/services/crypto/TicketCustomer.py b/core/services/crypto/TicketCustomer.py index e08d2e3..39cd14f 100644 --- a/core/services/crypto/TicketCustomer.py +++ b/core/services/crypto/TicketCustomer.py @@ -19,16 +19,6 @@ from py_ecc.optimized_bls12_381.optimized_curve import FQ, FQ2 from core.errors.logger import logger import traceback -# for the (optional) validity tests: -try: - from core.services.crypto.cython.bls12_381_pairing import pairing - logger.info("Imported Cython pairings") -except ImportError: - # Fallback to py_ecc if not compiled - from py_ecc.optimized_bls12_381 import pairing - logger.error("FAILED to import cython pairings, used python py_ecc") - - class TicketCustomer: """ This class is the basis for all the crypto operations, @@ -37,6 +27,10 @@ class TicketCustomer: """ def __init__(self): + # lazy load of millers from so file: + from core.services.helpers.load_packages import load_millers_pairing + self.pairing = load_millers_pairing() + self.blinded_data = None self.signature = None @@ -223,7 +217,7 @@ class TicketCustomer: # All of that was to prep the values for this pairing equation, # Which now uses Cython try: - if pairing(blinded_signature, G1) == pairing( + if self.pairing(blinded_signature, G1) == self.pairing( blinded_commitment, projective_public_key ): return {"valid": True, "message": "It worked."} diff --git a/core/services/helpers/load_packages.py b/core/services/helpers/load_packages.py new file mode 100644 index 0000000..6d74bbf --- /dev/null +++ b/core/services/helpers/load_packages.py @@ -0,0 +1,57 @@ + +from core.errors.logger import logger + +import sys +import importlib.util +import os + +def is_this_an_appimage(): + if "APPIMAGE" in os.environ: + return True + else: + return False + +def load_millers_pairing(): + + if is_this_an_appimage(): + # 1. Get the path to the python binary currently running + # This will be something like /tmp/.mount_myapp-EpCCLd/usr/bin/python3 + exe_path = sys.executable + + # 2. Find the AppImage mount root. + # We climb up from /usr/bin/python3 until we hit the root of the mount. + # In most AppImages, the root is 2 or 3 levels above the binary. + # A safer way is to look for the 'src' folder relative to the binary. + app_root = os.path.dirname(os.path.dirname(os.path.dirname(exe_path))) + + # 3. Now build the absolute path using the root from step 2 + the relative path: + relative_path = "src/sp-hydra-veil-core/core/services/crypto/cython/bls12_381_pairing.cpython-312-x86_64-linux-gnu.so" + full_path = os.path.join(app_root, relative_path) + + print(f"AppImage's Correct absolute path: {full_path}") + + try: + spec = importlib.util.spec_from_file_location("bls12_381_pairing", full_path) + my_module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(my_module) + print("Successfully loaded the so!") + return pairing + + except Exception as e: + logger.error(f"AppImage Failed to import cython pairings, used python py_ecc. {str(e)}") + # Fallback to py_ecc if not compiled + from py_ecc.optimized_bls12_381 import pairing + return pairing + + # NOT an appimage: + else: + try: + from core.services.crypto.cython.bls12_381_pairing import pairing + logger.info("Imported Cython pairings") + return pairing + + except ImportError as e: + # Fallback to py_ecc if not compiled + from py_ecc.optimized_bls12_381 import pairing + logger.error(f"Non-Appimage Failed to import cython pairings, used python py_ecc. {str(e)}") + return pairing \ No newline at end of file diff --git a/core/utils/run_commands.py b/core/utils/run_commands.py index e534966..a2ec91a 100644 --- a/core/utils/run_commands.py +++ b/core/utils/run_commands.py @@ -146,7 +146,6 @@ def run_generic_command( try: returncode, stdout = _run_command_via_terminal(command, timeout) - print("got output") # output_data = stdout diff --git a/pyproject.toml b/pyproject.toml index 92d006b..84f9d19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ dependencies = [ "charset-normalizer==3.4.7", "click==8.3.3", "cytoolz==1.1.0", + "Cython==3.3.0", "eth-hash==0.8.0", "eth-typing==6.0.0", "eth-utils==6.0.0", @@ -41,6 +42,7 @@ dependencies = [ "pydeps==3.0.6", "pytokens==0.4.1", "PyYAML==6.0.3", + "setuptools==84.0.0", "stdlib-list==0.12.0", "SQLAlchemy==2.0.51", "toolz==1.1.0",