diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90e3b52..6569790 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,11 +4,16 @@ on: pull_request: branches: - main + # `labeled` lets reviewers opt a PR into integration / GPU coverage by + # adding `run-integration` or `run-gpu` to the PR. Per-job `if:` checks + # the label set, so unrelated jobs still skip. + types: [opened, synchronize, reopened, labeled] push: branches: - main - staging - - develop + tags: + - 'v*' workflow_dispatch: # lets you run it manually from the Actions tab concurrency: @@ -58,12 +63,28 @@ jobs: run: uv run pytest tests/x_vec/test_paillier_encryption.py # ── Integration tests (require XTrace API credentials) ────────── + # Triggers: + # - PRs: only when explicitly labeled `run-integration` (avoids quota + # burn on every PR). + # - Push: main / staging / v* tags (catch regressions before release). + # - workflow_dispatch: manual. + # Fork PRs skip cleanly because GitHub does not pass secrets to forks — + # the test modules' `_REQUIRED` env-var check sees the empty values and + # skips. No global kill-switch variable needed. integration: name: Integration tests (meta search + hamming) runs-on: ubuntu-latest timeout-minutes: 30 needs: qa # only run if qa passes - if: vars.XTRACE_INTEGRATION_TESTS == 'true' # opt-in via repo variable + if: >- + github.event_name == 'workflow_dispatch' + || (github.event_name == 'push' && ( + github.ref == 'refs/heads/main' + || github.ref == 'refs/heads/staging' + || startsWith(github.ref, 'refs/tags/v') + )) + || (github.event_name == 'pull_request' + && contains(github.event.pull_request.labels.*.name, 'run-integration')) steps: - name: Check out repository @@ -82,14 +103,106 @@ jobs: - name: Pytest (test_meta_search) env: - XTRACE_API_KEY: ${{ secrets.XTRACE_API_KEY }} - XTRACE_ADMIN_KEY: ${{ secrets.XTRACE_ADMIN_KEY }} - XTRACE_ORG_ID: ${{ secrets.XTRACE_ORG_ID }} + XTRACE_API_KEY: ${{ secrets.XTRACE_API_KEY }} + XTRACE_ADMIN_KEY: ${{ secrets.XTRACE_ADMIN_KEY }} + XTRACE_ORG_ID: ${{ secrets.XTRACE_ORG_ID }} + XTRACE_API_URL: ${{ secrets.XTRACE_API_URL }} + XTRACE_ADMIN_API_URL: ${{ secrets.XTRACE_ADMIN_API_URL }} run: uv run pytest tests/x_vec/test_meta_search.py -v - name: Pytest (test_hamming_search) env: - XTRACE_API_KEY: ${{ secrets.XTRACE_API_KEY }} - XTRACE_ADMIN_KEY: ${{ secrets.XTRACE_ADMIN_KEY }} - XTRACE_ORG_ID: ${{ secrets.XTRACE_ORG_ID }} + XTRACE_API_KEY: ${{ secrets.XTRACE_API_KEY }} + XTRACE_ADMIN_KEY: ${{ secrets.XTRACE_ADMIN_KEY }} + XTRACE_ORG_ID: ${{ secrets.XTRACE_ORG_ID }} + XTRACE_API_URL: ${{ secrets.XTRACE_API_URL }} + XTRACE_ADMIN_API_URL: ${{ secrets.XTRACE_ADMIN_API_URL }} run: uv run pytest tests/x_vec/test_hamming_search.py -v + + # ── Build GPU extensions on a free runner (Docker + nvcc, no GPU needed) ─ + # The build script only needs Docker — nvcc runs in a pinned nvidia/cuda + # devel image. Splitting the build off the GPU runner keeps the paid GPU + # minutes for actual test execution. Same gating as gpu-tests so we only + # spend time building when the gpu-tests job is going to consume the .so. + build-gpu-extensions: + name: Build GPU extensions (.so via Docker) + runs-on: ubuntu-latest + timeout-minutes: 30 + needs: qa + if: >- + github.event_name == 'workflow_dispatch' + || (github.event_name == 'push' && ( + github.ref == 'refs/heads/main' + || github.ref == 'refs/heads/staging' + || startsWith(github.ref, 'refs/tags/v') + )) + || (github.event_name == 'pull_request' + && contains(github.event.pull_request.labels.*.name, 'run-gpu')) + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Build GPU extensions + run: ./build_gpu_binaries.sh + + - name: Upload .so artifacts + uses: actions/upload-artifact@v4 + with: + name: gpu-extensions + path: | + src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/*.so + src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/*.so + if-no-files-found: error + retention-days: 7 + + # ── GPU tests (run on GitHub-managed partner GPU runner; paid minutes) ─ + # Triggers: + # - PRs: only when explicitly labeled `run-gpu` (paid runner — opt in). + # - Push: main / staging / v* tags. + # - workflow_dispatch: manual. + # Requires `gpu` environment with required reviewers; runner label is + # `gpu-runner`. Compiled .so files are produced by the build-gpu-extensions + # job above and downloaded into place before pytest. + gpu-tests: + name: GPU tests (paillier client) + runs-on: gpu-runner + timeout-minutes: 30 + needs: build-gpu-extensions + if: >- + github.event_name == 'workflow_dispatch' + || (github.event_name == 'push' && ( + github.ref == 'refs/heads/main' + || github.ref == 'refs/heads/staging' + || startsWith(github.ref, 'refs/tags/v') + )) + || (github.event_name == 'pull_request' + && contains(github.event.pull_request.labels.*.name, 'run-gpu')) + environment: gpu + concurrency: + group: gpu-tests + cancel-in-progress: true + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Set up uv + uses: astral-sh/setup-uv@v4 + + - name: Install package + all dev dependencies + run: uv sync --all-groups + + - name: Download GPU extension artifacts + uses: actions/download-artifact@v4 + with: + name: gpu-extensions + path: src/xtrace_sdk/x_vec/crypto/ + + - name: Pytest (test_GPU_paillier_clients) + run: uv run pytest tests/x_vec/test_GPU_paillier_clients.py -v diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c7a7760..b71e4ff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -48,8 +48,75 @@ jobs: - name: Pytest (paillier encryption) run: uv run pytest tests/x_vec/test_paillier_encryption.py - build: - name: Build sdist + wheel + # ── Build matrix wheel: one cp3X wheel per supported Python ─────────── + # Builds the GPU .so inside an Ubuntu 20.04 CUDA container, then `uv build` + # bundles it, and `auditwheel repair` retags as manylinux_2_31_x86_64 so + # PyPI accepts it and pip can pick it up on glibc>=2.31 (Ubuntu 20.04+, + # RHEL 8.4+, Debian 11+). + build-wheel: + name: Build wheel (py${{ matrix.python_version }}) + runs-on: ubuntu-latest + timeout-minutes: 30 + needs: qa + strategy: + fail-fast: false + matrix: + include: + - python_version: "3.11" + python_full_version: "3.11.15" + - python_version: "3.12" + python_full_version: "3.12.13" + - python_version: "3.13" + python_full_version: "3.13.13" + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true + + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python_version }} + + - uses: astral-sh/setup-uv@v4 + with: + enable-cache: true + + - name: Build GPU .so (Ubuntu 20.04 / glibc 2.31 baseline) + env: + PYTHON_VERSION: ${{ matrix.python_version }} + PYTHON_FULL_VERSION: ${{ matrix.python_full_version }} + PYTHON_STANDALONE_TAG: "20260414" + CUDA_IMAGE: nvidia/cuda:12.4.1-devel-ubuntu20.04 + run: ./build_gpu_binaries.sh + + - name: Resolve version from git tag + run: | + if git describe --tags --exact-match HEAD 2>/dev/null; then + VER=$(git describe --tags --exact-match HEAD) + echo "SETUPTOOLS_SCM_PRETEND_VERSION=${VER#v}" >> "$GITHUB_ENV" + fi + + - name: Build wheel + run: uv build --wheel + + - name: Repair wheel (manylinux_2_31_x86_64) + run: | + uv pip install --system auditwheel patchelf + mkdir -p wheelhouse + auditwheel repair dist/*.whl --plat manylinux_2_31_x86_64 -w wheelhouse + rm dist/*.whl + mv wheelhouse/*.whl dist/ + ls -lh dist/ + + - uses: actions/upload-artifact@v4 + with: + name: dist-wheel-py${{ matrix.python_version }} + path: dist/*.whl + + # ── Build sdist once (no Python ABI dependency) ─────────────────────── + build-sdist: + name: Build sdist runs-on: ubuntu-latest needs: qa steps: @@ -73,27 +140,32 @@ jobs: echo "SETUPTOOLS_SCM_PRETEND_VERSION=${VER#v}" >> "$GITHUB_ENV" fi - - name: Build artifacts - run: uv build + - name: Build sdist + run: uv build --sdist - uses: actions/upload-artifact@v4 with: - name: dist-artifacts - path: dist + name: dist-sdist + path: dist/*.tar.gz + # ── Verify each repaired wheel installs and imports on its target ABI ─ verify: - name: Verify wheel is installable + name: Verify wheel (py${{ matrix.python_version }}) runs-on: ubuntu-latest - needs: build + needs: build-wheel + strategy: + fail-fast: false + matrix: + python_version: ["3.11", "3.12", "3.13"] steps: - uses: actions/download-artifact@v4 with: - name: dist-artifacts + name: dist-wheel-py${{ matrix.python_version }} path: dist - uses: actions/setup-python@v5 with: - python-version: "3.12" + python-version: ${{ matrix.python_version }} - uses: astral-sh/setup-uv@v4 @@ -102,18 +174,23 @@ jobs: uv pip install --system dist/*.whl python -c "import xtrace_sdk; print('Imported:', xtrace_sdk.__name__)" + # ── Publish to TestPyPI on staging branch pushes ────────────────────── publish-testpypi: name: Publish to TestPyPI runs-on: ubuntu-latest - needs: verify + needs: [verify, build-sdist] if: github.ref == 'refs/heads/staging' permissions: id-token: write steps: - uses: actions/download-artifact@v4 with: - name: dist-artifacts + pattern: dist-* path: dist + merge-multiple: true + + - name: List artifacts + run: ls -lh dist/ - name: Publish (TestPyPI via OIDC) uses: pypa/gh-action-pypi-publish@release/v1 @@ -123,18 +200,23 @@ jobs: packages-dir: dist skip-existing: true + # ── Publish to PyPI on tag pushes (v*) ──────────────────────────────── publish-pypi: name: Publish to PyPI runs-on: ubuntu-latest - needs: verify + needs: [verify, build-sdist] if: startsWith(github.ref, 'refs/tags/v') permissions: id-token: write steps: - uses: actions/download-artifact@v4 with: - name: dist-artifacts + pattern: dist-* path: dist + merge-multiple: true + + - name: List artifacts + run: ls -lh dist/ - name: Publish (PyPI via OIDC) uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 552cf8f..1e35be6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,31 @@ All notable changes to this project will be documented in this file. +## [Unreleased] + +### Changed + +- **Device selection moved from the `DEVICE` environment variable to a `device=` constructor keyword** on `PaillierClient`, `PaillierLookupClient`, `ExecutionContext.create`, `ExecutionContext.load_from_disk`, and `ExecutionContext.load_from_remote`. The new default is `device="auto"`: clients probe for the GPU extension at construction time and fall back to CPU silently when no GPU is present. If a GPU is detected on the host (`/dev/nvidia0`, `nvidia-smi`, or `CUDA_VISIBLE_DEVICES`) but the extension fails to load, a warning is emitted instead of a silent CPU fallback so misconfigured GPU hosts don't quietly run on CPU. `device="gpu"` raises if the extension is unavailable; `device="cpu"` skips probing entirely. +- The `DEVICE` environment variable is no longer read. Existing callers must migrate to the `device=` kwarg. + +### Added + +- `xtrace_sdk.x_vec.crypto.device.resolve_device` — shared device-resolution helper used by both Paillier clients and intended for future homomorphic clients with optional GPU backends. +- `test_cross_device_interop` — parametrised test covering CPU↔GPU portability for both `PaillierClient` and `PaillierLookupClient`. Verifies that contexts saved on one device load correctly on the other (hash equality), and that ciphertexts produced on either backend round-trip correctly through the other for both encryption and server-side homomorphic add. + +## [0.2.0] - 2026-04-18 + +### Added + +- **CUDA backend for Paillier and Paillier-Lookup homomorphic encryption.** The `.cu` sources are now in-tree under `src/xtrace_sdk/x_vec/crypto/paillier-GPU-{,lookup-}client/`, and the new `build_gpu_binaries.sh` script compiles both pybind11 extensions inside a pinned `nvidia/cuda:12.4.0-devel-ubuntu22.04` Docker container. No local CUDA toolkit is required on the build host — only Docker and `nvcc` (provided by the image). Runtime requires NVIDIA driver ≥ 550. Opt-in via `DEVICE=gpu`; CPU remains the default. +- `PaillierGPUClient` and `PaillierLookupGPUClient` — standalone GPU-backed Hamming clients for `ExecutionContext`. Type dispatch in `ExecutionContext.create` now accepts these as first-class alternatives to their CPU counterparts. +- Documented `DEVICE` environment variable in the configuration reference. +- "GPU acceleration" section in the installation docs covering build script usage, runtime requirements, and per-build knobs (`PYTHON_VERSION`, `SMS`, `KEY_BITS`, `ALPHA_LEN`, `CUDA_IMAGE`). + +### Changed + +- `DEVICE` is now resolved at client instantiation time rather than at module import time. Previously, setting `os.environ["DEVICE"]` after importing a client (common in notebooks) had no effect; the module-level constant was already frozen to `cpu`. The env var is now read fresh inside `PaillierClient.__init__`, `PaillierLookupClient.__init__`, and `PaillierLookupClient.__setstate__`, so CPU and GPU clients can be instantiated in the same process and ordering of imports no longer matters. +- Error messages raised when the GPU extension cannot be loaded now point users at `./build_gpu_binaries.sh` and document runtime driver requirements, replacing the previous stale reference to `src/crypto/` and non-existent documentation. + ## [0.1.1] - 2026-03-31 ### Fixed diff --git a/README.md b/README.md index 6c8ddfb..932c6f0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,41 @@ uv pip install "xtrace-ai-sdk[embedding]" Requires Python 3.11+. +## GPU acceleration (optional) + +The Paillier homomorphic encryption path has an optional CUDA backend that is significantly faster than the CPU implementation for large batches. + +**Prebuilt binaries are not currently shipped on PyPI.** To use the GPU path, compile the pybind11 extensions yourself: + +```bash +# From the repo root. Requires Docker only — no local CUDA toolkit needed. +./build_gpu_binaries.sh +``` + +This produces two `.so` files in place under `src/xtrace_sdk/x_vec/crypto/paillier-GPU-{,lookup-}client/`. + +Backend selection is automatic by default — clients probe for the GPU extension at construction time and fall back to CPU if it's unavailable. You can also force a backend explicitly: + +```python +from xtrace_sdk.x_vec.crypto.paillier_client import PaillierClient +from xtrace_sdk.x_vec.utils.execution_context import ExecutionContext + +# Auto-detect (default): GPU if available, else CPU. +client = PaillierClient(embed_len=512, key_len=1024) + +# Force a backend. device="gpu" raises if the extension is unavailable. +client = PaillierClient(embed_len=512, key_len=1024, device="cpu") +ctx = ExecutionContext.create(passphrase="...", device="gpu") +``` + +**Requirements:** + +- Docker (to run the build; ~10 GB for the `nvidia/cuda` devel image) +- NVIDIA driver ≥ 550 (i.e. CUDA 12.x runtime) on the host that will execute the GPU code +- An NVIDIA GPU with compute capability 7.0–9.0 (V100, T4, A100, RTX 30/40, H100, etc.) + +**Knobs (env vars on the build script):** `PYTHON_VERSION` (default `3.11`), `SMS` (GPU arch list), `KEY_BITS`, `ALPHA_LEN` (lookup variant), `CUDA_IMAGE`. See the header of [`build_gpu_binaries.sh`](./build_gpu_binaries.sh) for defaults. + ## Documentation Full documentation at [docs.xtrace.ai](https://docs.xtrace.ai), or build locally: diff --git a/build_gpu_binaries.sh b/build_gpu_binaries.sh new file mode 100755 index 0000000..dfa01eb --- /dev/null +++ b/build_gpu_binaries.sh @@ -0,0 +1,149 @@ +#!/bin/bash +# +# Build the Paillier GPU and Paillier-Lookup GPU pybind11 extensions inside a +# pinned nvidia/cuda Docker image. Produces the two in-tree .so files imported +# by the normal Python packages under `xtrace_sdk.x_vec.crypto`: +# +# src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/paillier_GPU_client*.so +# src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/paillier_GPU_lookup_client*.so +# +# Overridable via env vars: +# PYTHON_VERSION Python minor version (default: 3.11) +# PYTHON_FULL_VERSION Full python-build-standalone version (default: 3.11.15) +# PYTHON_STANDALONE_TAG python-build-standalone release tag (default: 20260414) +# SMS Space-separated GPU SM list (default: "70 75 80 86 89 90 90a") +# KEY_BITS Paillier key size (default: 1024) +# ALPHA_LEN Lookup variant alpha length (default: 280) +# CUDA_IMAGE nvidia/cuda devel image (default: matched to host Ubuntu, see below) +# CGBN_REPO_URL CGBN header-only lib source (default: NVlabs/CGBN on GitHub) +# PYBIND11_REPO_URL pybind11 header-only lib source (default: pybind/pybind11 on GitHub) +# +# Python is fetched from astral-sh/python-build-standalone (GitHub-hosted +# tarball) instead of distro packages, so the build does not depend on +# Launchpad/PPAs being reachable from the build host. +# +# Only nvcc is needed to compile — no GPU is required, so this runs on a +# standard CI runner (or Apple Silicon via --platform linux/amd64). +# +set -euo pipefail + +PYTHON_VERSION="${PYTHON_VERSION:-3.11}" +PYTHON_FULL_VERSION="${PYTHON_FULL_VERSION:-3.11.15}" +PYTHON_STANDALONE_TAG="${PYTHON_STANDALONE_TAG:-20260414}" +SMS="${SMS:-70 75 80 86 89 90 90a}" +KEY_BITS="${KEY_BITS:-1024}" +ALPHA_LEN="${ALPHA_LEN:-280}" +# Pick a CUDA devel base image whose toolchain (gcc/libstdc++) matches the +# host's, so the resulting .so loads on the host without GLIBCXX_* mismatches. +# Override CUDA_IMAGE explicitly to target a different deployment baseline. +default_cuda_image() { + local host_id="" host_ver="" + if [ -r /etc/os-release ]; then + # shellcheck source=/dev/null + host_id="$(. /etc/os-release && echo "${ID:-}")" + host_ver="$(. /etc/os-release && echo "${VERSION_ID:-}")" + fi + if [ "$host_id" = "ubuntu" ]; then + case "$host_ver" in + 20.04) echo "nvidia/cuda:12.4.1-devel-ubuntu20.04"; return ;; + 22.04) echo "nvidia/cuda:12.4.1-devel-ubuntu22.04"; return ;; + 24.04) echo "nvidia/cuda:12.6.3-devel-ubuntu24.04"; return ;; + esac + fi + # Non-Ubuntu host (Debian, RHEL, macOS, etc.): default to the oldest still + # supported Ubuntu base for broad libstdc++ compatibility. + echo "nvidia/cuda:12.4.1-devel-ubuntu22.04" +} +CUDA_IMAGE="${CUDA_IMAGE:-$(default_cuda_image)}" +CGBN_REPO_URL="${CGBN_REPO_URL:-https://github.com/NVlabs/CGBN.git}" +PYBIND11_REPO_URL="${PYBIND11_REPO_URL:-https://github.com/pybind/pybind11.git}" + +PYTHON_STANDALONE_URL="https://github.com/astral-sh/python-build-standalone/releases/download/${PYTHON_STANDALONE_TAG}/cpython-${PYTHON_FULL_VERSION}+${PYTHON_STANDALONE_TAG}-x86_64-unknown-linux-gnu-install_only.tar.gz" + +REPO_ROOT="$(cd "$(dirname "$0")" && pwd)" +CRYPTO_DIR="$REPO_ROOT/src/xtrace_sdk/x_vec/crypto" + +if [ ! -d "$CRYPTO_DIR/paillier_gpu_ext" ] || \ + [ ! -d "$CRYPTO_DIR/paillier_lookup_gpu_ext" ]; then + echo "Error: expected GPU source dirs under $CRYPTO_DIR" >&2 + exit 1 +fi + +# Fall back through SUDO_UID/GID so `sudo ./build_gpu_binaries.sh` still chowns +# artifacts back to the invoking user, not root. +HOST_UID="${SUDO_UID:-$(id -u)}" +HOST_GID="${SUDO_GID:-$(id -g)}" + +echo "Building Paillier GPU extensions" +echo " Python: $PYTHON_FULL_VERSION (python-build-standalone $PYTHON_STANDALONE_TAG)" +echo " CUDA img: $CUDA_IMAGE" +echo " SMs: $SMS" +echo " KEY_BITS: $KEY_BITS" +echo " ALPHA_LEN: $ALPHA_LEN (lookup variant only)" +echo " Output: $CRYPTO_DIR/paillier{,_lookup}_gpu_ext/*.so" +echo + +docker run --rm \ + --platform linux/amd64 \ + --env DEBIAN_FRONTEND=noninteractive \ + --env PYTHON_VERSION="$PYTHON_VERSION" \ + --env PYTHON_STANDALONE_URL="$PYTHON_STANDALONE_URL" \ + --env SMS="$SMS" \ + --env KEY_BITS="$KEY_BITS" \ + --env ALPHA_LEN="$ALPHA_LEN" \ + --env CGBN_REPO_URL="$CGBN_REPO_URL" \ + --env PYBIND11_REPO_URL="$PYBIND11_REPO_URL" \ + --env HOST_UID="$HOST_UID" \ + --env HOST_GID="$HOST_GID" \ + -v "$REPO_ROOT:/build/repo" \ + -w /build/repo \ + "$CUDA_IMAGE" \ + /bin/bash -c ' + set -euo pipefail + + apt-get update + apt-get install -y --no-install-recommends \ + ca-certificates git build-essential libgmp-dev curl xz-utils + + # Fetch a self-contained CPython build (no PPA / Launchpad needed). + curl -fsSL "$PYTHON_STANDALONE_URL" -o /tmp/python.tar.gz + mkdir -p /opt + tar -xzf /tmp/python.tar.gz -C /opt # creates /opt/python + rm /tmp/python.tar.gz + ln -sf "/opt/python/bin/python${PYTHON_VERSION}" /usr/local/bin/python3 + ln -sf "/opt/python/bin/python${PYTHON_VERSION}" "/usr/local/bin/python${PYTHON_VERSION}" + python3 --version + + # Materialise the include/{pybind11,cgbn} layout the Makefiles expect. + INCDIR=/build/include + mkdir -p "$INCDIR" + git clone --depth=1 "$PYBIND11_REPO_URL" /opt/pybind11 + ln -sfn /opt/pybind11/include/pybind11 "$INCDIR/pybind11" + git clone --depth=1 "$CGBN_REPO_URL" /opt/CGBN + ln -sfn /opt/CGBN/include/cgbn "$INCDIR/cgbn" + + build_one() { + local dir="$1" + shift + echo "=== Building $dir ===" + cd "/build/repo/src/xtrace_sdk/x_vec/crypto/$dir" + make clean || true + make INCDIR="$INCDIR" SMS="$SMS" KEY_BITS="$KEY_BITS" "$@" + ls -lh ./*.so + } + + build_one paillier_gpu_ext + build_one paillier_lookup_gpu_ext ALPHA_LEN="$ALPHA_LEN" + + chown -R "$HOST_UID:$HOST_GID" \ + /build/repo/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext \ + /build/repo/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext + + echo "Done." + ' + +echo +echo "Artifacts:" +ls -lh "$CRYPTO_DIR/paillier_gpu_ext/"*.so \ + "$CRYPTO_DIR/paillier_lookup_gpu_ext/"*.so 2>/dev/null || \ + echo " (no .so produced — check output above)" diff --git a/docs/manual_rst/configuration.rst b/docs/manual_rst/configuration.rst index 59bfc18..c894bbc 100644 --- a/docs/manual_rst/configuration.rst +++ b/docs/manual_rst/configuration.rst @@ -13,15 +13,27 @@ Both ``PaillierClient`` and ``PaillierLookupClient`` run on CPU by default. .. note:: - **GPU backend (internal testing phase).** XTrace maintains a GPU-accelerated implementation of the - homomorphic encryption layer that is approximately **20× faster** than the CPU path for large - embedding collections. It is available as a compiled extension that slots into the same - ``DEVICE=gpu`` switch — no application code changes required. + **GPU backend (optional).** A CUDA-accelerated implementation of the Paillier + and Paillier-Lookup clients is included in the source tree (``~20× faster`` + than the CPU path on large embedding collections). It is **not** shipped + prebuilt on PyPI — compile it yourself with the included Docker-based build + script. See :doc:`install` → *GPU acceleration* for build instructions and + runtime requirements (NVIDIA driver ≥ 550). + +Once the compiled extension is in place, the GPU backend is selected +automatically by default — clients probe for it at construction time and fall +back to CPU if it's unavailable. Pass ``device="cpu"`` or ``device="gpu"`` to +force a backend (CPU and GPU clients can coexist in the same process): - The GPU implementation is not open-sourced at this time as it is under internal testing. Contact us at - `liwen@xtrace.ai `_ if you are interested in access. +.. code-block:: python + + from xtrace_sdk.x_vec.crypto.paillier_client import PaillierClient + + # Auto-detect: GPU if available, else CPU. + client = PaillierClient(embed_len=512, key_len=1024) -Set ``DEVICE=gpu`` to activate the GPU backend once the compiled extension is in place. + # Force a backend. device="gpu" raises if the extension is unavailable. + gpu_client = PaillierClient(embed_len=512, key_len=1024, device="gpu") **Paillier** — standard Paillier encryption: diff --git a/docs/manual_rst/install.rst b/docs/manual_rst/install.rst index 362db11..fc1d2a0 100644 --- a/docs/manual_rst/install.rst +++ b/docs/manual_rst/install.rst @@ -61,8 +61,8 @@ Install from source .. code-block:: bash - git clone https://github.com/XTraceAI/xtrace-vec-sdk.git - cd xtrace-vec-sdk + git clone https://github.com/XTraceAI/xtrace-sdk.git + cd xtrace-sdk pip install -e . To include extras: @@ -70,3 +70,107 @@ To include extras: .. code-block:: bash pip install -e ".[embedding,cli]" + + +GPU acceleration (optional) +--------------------------- + +The Paillier and Paillier-Lookup homomorphic encryption clients ship with an +optional CUDA backend that is roughly **20× faster** than the CPU path on large +embedding collections. It is opt-in — the base install runs fully on CPU and +requires no GPU, no CUDA toolkit, and no Docker. + +Prebuilt binaries are **not** currently published on PyPI. To enable the GPU +path, compile the pybind11 extensions from source using the included Docker-based +build script: + +.. code-block:: bash + + git clone https://github.com/XTraceAI/xtrace-sdk.git + cd xtrace-sdk + ./build_gpu_binaries.sh + +The script spins up an ``nvidia/cuda`` devel container, fetches ``pybind11`` and +``CGBN`` headers, and compiles two ``.so`` files that land in place under +``src/xtrace_sdk/x_vec/crypto/paillier-GPU-{,lookup-}client/``. The SDK's runtime +loader picks them up automatically on import. No NVIDIA GPU is required on the +*build* host — only ``nvcc``, which is provided by the Docker image. + +Backend selection is automatic by default — clients probe for the GPU extension +at construction time and fall back to CPU if it's unavailable. You can also +force a backend explicitly with the ``device`` keyword argument: + +.. code-block:: python + + from xtrace_sdk.x_vec.crypto.paillier_client import PaillierClient + + # Auto-detect (default): GPU if available, else CPU. + client = PaillierClient(embed_len=512, key_len=1024) + + # Force a backend. device="gpu" raises if the extension is unavailable. + cpu_client = PaillierClient(embed_len=512, key_len=1024, device="cpu") + gpu_client = PaillierClient(embed_len=512, key_len=1024, device="gpu") + +The same ``device`` keyword is accepted by +:meth:`~xtrace_sdk.x_vec.utils.execution_context.ExecutionContext.create`, +:meth:`~xtrace_sdk.x_vec.utils.execution_context.ExecutionContext.load_from_disk`, +and :meth:`~xtrace_sdk.x_vec.utils.execution_context.ExecutionContext.load_from_remote`, +so a context saved on a CPU host can be loaded on a GPU host (and vice versa) +without modification — keys are portable, the device is a runtime choice. + +Requirements +^^^^^^^^^^^^ + +.. list-table:: + :header-rows: 1 + :widths: 25 75 + + * - Component + - Version + * - Build host + - Docker (any recent version). ~10 GB free disk for the CUDA devel image. + * - Runtime host + - NVIDIA driver **≥ 550** (CUDA 12.x runtime), with an NVIDIA GPU of + compute capability 7.0–9.0 (V100, T4, A100, RTX 30/40, H100, etc.). + +Build-time configuration +^^^^^^^^^^^^^^^^^^^^^^^^ + +All parameters are environment variables on the build script: + +.. list-table:: + :header-rows: 1 + + * - Variable + - Default + - Description + * - ``PYTHON_VERSION`` + - ``3.11`` + - CPython minor version. Must match the Python you will import the SDK from + (the ``.so`` is ABI-tagged per minor version). + * - ``SMS`` + - ``70 75 80 86 89 90 90a`` + - Space-separated GPU SM targets. Default fat-binary covers V100, T4, + A100, RTX 30/40, and H100 in a single file. + * - ``KEY_BITS`` + - ``1024`` + - Paillier modulus size (bits). Must match the ``key_len`` passed to the + client at runtime. + * - ``ALPHA_LEN`` + - ``280`` + - Paillier-Lookup alpha length. Lookup variant only. + * - ``CUDA_IMAGE`` + - ``nvidia/cuda:12.4.0-devel-ubuntu22.04`` + - Docker image used to compile. Swap for ``nvidia/cuda:11.x-devel-*`` if + you need a CUDA 11 runtime. + * - ``CGBN_REPO_URL`` + - NVlabs/CGBN on GitHub + - Override if you are building behind an air-gapped mirror. + +Example — build a CUDA 11 / Python 3.12 binary: + +.. code-block:: bash + + PYTHON_VERSION=3.12 \ + CUDA_IMAGE=nvidia/cuda:11.8.0-devel-ubuntu22.04 \ + ./build_gpu_binaries.sh diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 0000000..674d1be --- /dev/null +++ b/hatch_build.py @@ -0,0 +1,29 @@ +"""Hatchling build hook: tag wheels as platform-specific when GPU `.so` +artifacts are bundled, so pip won't install a `py3-none-any` wheel onto a +machine whose Python ABI / OS / arch can't actually load the extension. + +If no `.so` is found we leave the wheel pure (so a CPU-only sdist build +still produces a usable cross-platform wheel).""" + +from __future__ import annotations + +import glob +import os + +from hatchling.builders.hooks.plugin.interface import BuildHookInterface + + +class CustomBuildHook(BuildHookInterface): + PLUGIN_NAME = "custom" + + def initialize(self, version: str, build_data: dict) -> None: + gpu_so_globs = [ + "src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/*.so", + "src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/*.so", + ] + has_binary = any( + glob.glob(os.path.join(self.root, pattern)) for pattern in gpu_so_globs + ) + if has_binary: + build_data["pure_python"] = False + build_data["infer_tag"] = True diff --git a/pyproject.toml b/pyproject.toml index a417af1..6707598 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,8 +24,8 @@ classifiers = ["Programming Language :: Python :: 3"] [project.urls] Homepage = "https://xtrace.ai" Documentation = "https://docs.xtrace.ai" -Repository = "https://github.com/XTraceAI/xtrace-vec-sdk" -Issues = "https://github.com/XTraceAI/xtrace-vec-sdk/issues" +Repository = "https://github.com/XTraceAI/xtrace-sdk" +Issues = "https://github.com/XTraceAI/xtrace-sdk/issues" [project.scripts] xtrace = "xtrace_sdk.cli.main:app" @@ -40,11 +40,24 @@ source = "vcs" [tool.hatch.version.raw-options] local_scheme = "no-local-version" +[tool.hatch.build] +# Force-include the GPU extension binaries even though *.so is in .gitignore. +# These are produced by build_gpu_binaries.sh prior to `uv build` in CI. +artifacts = [ + "src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/*.so", + "src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/*.so", +] + [tool.hatch.build.targets.wheel] packages = ["src/xtrace_sdk"] +# Custom hook flips the wheel from pure-Python (py3-none-any) to a +# platform-specific tag when compiled extensions are bundled. +[tool.hatch.build.targets.wheel.hooks.custom] +path = "hatch_build.py" + [tool.hatch.build.targets.sdist] -include = ["src/xtrace_sdk", "README.md", "LICENSE", "pyproject.toml"] +include = ["src/xtrace_sdk", "README.md", "LICENSE", "pyproject.toml", "hatch_build.py"] exclude = ["tests", "docs", "benchmarks"] [project.optional-dependencies] diff --git a/src/xtrace_sdk/x_vec/crypto/device.py b/src/xtrace_sdk/x_vec/crypto/device.py new file mode 100644 index 0000000..8834e5d --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/device.py @@ -0,0 +1,61 @@ +"""Shared device-resolution helper for clients with optional GPU backends. + +Used by ``paillier_client`` and ``paillier_lookup_client``, and intended to be +reused by future homomorphic clients that ship a CUDA extension. +""" + +import logging +import os +import shutil +from collections.abc import Callable +from typing import Any, Literal + +DeviceMode = Literal["auto", "cpu", "gpu"] + +_log = logging.getLogger(__name__) + + +def _gpu_expected() -> bool: + return ( + os.path.exists("/dev/nvidia0") + or shutil.which("nvidia-smi") is not None + or "CUDA_VISIBLE_DEVICES" in os.environ + ) + + +def resolve_device( + requested: DeviceMode, + gpu_loader: Callable[[], type[Any]], +) -> tuple[str, type[Any] | None]: + """Resolve the compute backend for a homomorphic client. + + - ``"cpu"``: never probes; returns CPU. + - ``"gpu"``: loads and probes the extension; raises if it fails. + - ``"auto"``: probes; on failure falls back to CPU. If a GPU is + detected on the host (``/dev/nvidia0``, ``nvidia-smi``, + ``CUDA_VISIBLE_DEVICES``) but the probe fails, logs a warning so + misconfigured GPU hosts don't silently run on CPU. + + :param requested: User-supplied device mode. + :param gpu_loader: Callable returning the GPU client class. Typically + wraps an import of the extension module so that import errors + propagate as load failures. + :return: ``(device, gpu_cls_or_None)``. ``gpu_cls`` is ``None`` for CPU. + """ + if requested == "cpu": + return "cpu", None + + try: + gpu_cls = gpu_loader() + gpu_cls(skip_key_gen=True) + except Exception as exc: + if requested == "gpu": + raise + if _gpu_expected(): + _log.warning( + "GPU detected on host but extension failed to load (%s); falling back to CPU.", + exc, + ) + return "cpu", None + + return "gpu", gpu_cls diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_client.py b/src/xtrace_sdk/x_vec/crypto/paillier_client.py index 758c50d..ed8b7ee 100644 --- a/src/xtrace_sdk/x_vec/crypto/paillier_client.py +++ b/src/xtrace_sdk/x_vec/crypto/paillier_client.py @@ -1,109 +1,29 @@ import json -import os -from typing import Union, cast +from typing import Any import gmpy2 +from xtrace_sdk.x_vec.crypto.device import DeviceMode, resolve_device from xtrace_sdk.x_vec.crypto.encryption.paillier import Paillier from xtrace_sdk.x_vec.crypto.hamming_client_base import HammingClientBase -from xtrace_sdk.x_vec.utils.xtrace_types import PaillierEncryptedNumber, PaillierKeyPair +from xtrace_sdk.x_vec.utils.xtrace_types import EncryptedVector, PaillierEncryptedNumber, PaillierKeyPair -DEVICE = os.getenv("DEVICE", "cpu") - -class PaillierGPU: - """Wrapper for the C++ GPU implementation of the Paillier cryptography system. - - This class is not instantiated directly — use :class:`PaillierClient` which dispatches - to this backend automatically when ``DEVICE=gpu``. - """ - - def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool = False) -> None: - try: - from .paillier_GPU_client import PaillierGPUClient - except Exception as e: - raise ImportError( - "GPU backend unavailable. Expected Paillier GPU .so under src/crypto/ " - "Please refer to the documentation for compilation instructions." - ) from e - self.gpu_client = PaillierGPUClient(embed_len=embed_len, key_len=key_len, skip_key_gen=skip_key_gen) - - def encrypt(self, embds: list[list[int]]) -> list[PaillierEncryptedNumber]: - """Encrypt a batch of binary embedding vectors on GPU. - - :param embds: List of binary vectors with values in {0, 1}. - :type embds: list[list[int]] - :return: List of encrypted vectors. - :rtype: list[PaillierEncryptedNumber] - """ - bytes_batch = self.gpu_client.encrypt_bytes(embds) - return [[int.from_bytes(b, byteorder='little') for b in bytes_list] for bytes_list in bytes_batch] - - def decode_hamming_client(self, cipher: list) -> list[int]: - """Decrypt a batch of encrypted Hamming distances on GPU. - - :param cipher: Batch of encrypted Hamming encodings from the server. - :type cipher: list - :return: List of plain-text Hamming distances. - :rtype: list[int] - """ - chunk_byte_len = (self.gpu_client.key_len * 4) // 8 - bytes_batch = [] - for c in cipher: - record_bytes = [] - for h in c: - if isinstance(h, bytes): - if len(h) != chunk_byte_len: - h = int.from_bytes(h, byteorder='little').to_bytes(chunk_byte_len, byteorder='little') - record_bytes.append(h) - else: - record_bytes.append(int(h).to_bytes(chunk_byte_len, byteorder='little')) - bytes_batch.append(record_bytes) - return self.gpu_client.decode_hamming_client_bytes(bytes_batch) - - def stringify_pk(self) -> str: - """Return a JSON string representation of the public key.""" - return self.gpu_client.stringify_pk() - - def stringify_sk(self) -> str: - """Return a JSON string representation of the secret key.""" - return self.gpu_client.stringify_sk() - - def stringify_config(self) -> str: - """Return a JSON string representation of the encryption configuration.""" - return self.gpu_client.stringify_config() - - def load_stringified_keys(self, pk: str, sk: str) -> None: - """Load public and secret keys from their JSON string representations. - - :param pk: JSON-encoded public key produced by :meth:`stringify_pk`. - :param sk: JSON-encoded secret key produced by :meth:`stringify_sk`. - """ - self.gpu_client.load_stringified_keys(pk, sk) - - def load_config(self, config: dict) -> None: - """Load encryption configuration from a dict produced by :meth:`stringify_config`. - - :param config: Configuration dict (``embed_len``, ``key_len``). - :type config: dict - """ - self.gpu_client.load_config(config) +def _load_gpu_backend() -> type[Any]: + try: + from xtrace_sdk.x_vec.crypto.paillier_gpu_ext import PaillierGPUClient + except ImportError as e: + raise ImportError( + "GPU extension not built. Run `./build_gpu_binaries.sh` " + "(requires Docker, NVIDIA driver >= 550)." + ) from e + return PaillierGPUClient class PaillierCPU: - """CPU implementation of Paillier homomorphic encryption optimised for Hamming distance.""" + """CPU implementation of Paillier homomorphic encryption for Hamming-distance search.""" def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool = False) -> None: - """Generate a Paillier keypair and configure chunking parameters. - - :param embed_len: Dimension of the binary embedding vectors to encrypt, defaults to 512. - :type embed_len: int, optional - :param key_len: RSA modulus size in bits for key generation, defaults to 1024. - :type key_len: int, optional - :param skip_key_gen: If True, skip key generation (keys must be loaded later via - ``load_stringified_keys``), defaults to False. - :type skip_key_gen: bool, optional - """ self.chunk_len = key_len * 2 self.key_len = key_len if embed_len > key_len: @@ -120,125 +40,92 @@ def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool self.embed_len = embed_len def stringify_pk(self) -> str: - """Return a JSON string representation of the public key.""" if self.keys is None: raise RuntimeError("Keys not initialized") - pk = self.keys['pk'] - return json.dumps({'g': str(pk['g']), 'n': str(pk['n']), 'n_squared': str(pk['n_squared'])}) + pk = self.keys["pk"] + return json.dumps({"g": str(pk["g"]), "n": str(pk["n"]), "n_squared": str(pk["n_squared"])}) def stringify_sk(self) -> str: - """Return a JSON string representation of the secret key.""" if self.keys is None: raise RuntimeError("Keys not initialized") - sk = self.keys['sk'] - return json.dumps({'phi': str(sk['phi']), 'inv': str(sk['inv'])}) + sk = self.keys["sk"] + return json.dumps({"phi": str(sk["phi"]), "inv": str(sk["inv"])}) def stringify_config(self) -> str: - """Return a JSON string representation of the encryption configuration.""" return json.dumps({"embed_len": self.embed_len, "key_len": self.key_len}) def load_stringified_keys(self, pk: str, sk: str) -> None: - """Load public and secret keys from their JSON string representations. - - :param pk: JSON-encoded public key produced by :meth:`stringify_pk`. - :param sk: JSON-encoded secret key produced by :meth:`stringify_sk`. - """ pk_data = json.loads(pk) sk_data = json.loads(sk) self.keys = { - 'pk': { - 'g': gmpy2.mpz(pk_data['g']), - 'n': gmpy2.mpz(pk_data['n']), - 'n_squared': gmpy2.mpz(pk_data['n_squared']), + "pk": { + "g": gmpy2.mpz(pk_data["g"]), + "n": gmpy2.mpz(pk_data["n"]), + "n_squared": gmpy2.mpz(pk_data["n_squared"]), }, - 'sk': { - 'phi': gmpy2.mpz(sk_data['phi']), - 'inv': gmpy2.mpz(sk_data['inv']), + "sk": { + "phi": gmpy2.mpz(sk_data["phi"]), + "inv": gmpy2.mpz(sk_data["inv"]), }, } def load_config(self, config: dict) -> None: - """Load encryption configuration from a dict produced by :meth:`stringify_config`. - - :param config: Configuration dict (``embed_len``, ``key_len``). - :type config: dict - """ - self.embed_len = config['embed_len'] - self.key_len = config['key_len'] - - def id2power(self, id_: int) -> tuple[int, int]: - """Return the chunk index and bit power for a given position in the padded embedding. - - :param id_: Index of an entry in the embedding vector. - :type id_: int - :return: Tuple of (chunk_index, bit_power). - :rtype: tuple[int, int] - """ - return id_ // self.chunk_len, self.chunk_len - 1 - id_ % self.chunk_len + self.embed_len = config["embed_len"] + self.key_len = config["key_len"] + self.chunk_len = self.key_len * 2 + if self.embed_len > self.key_len: + self.chunk_num = 2 * self.embed_len // self.chunk_len + int((2 * self.embed_len) % self.chunk_len != 0) + else: + self.chunk_num = 1 def encrypt(self, embd: list[int]) -> PaillierEncryptedNumber: - """Encrypt a single binary embedding vector. - - :param embd: Binary vector of length ``embed_len`` with values in {0, 1}. - :type embd: list[int] - :return: List of Paillier ciphertexts (one per chunk). - :rtype: PaillierEncryptedNumber - """ if len(embd) != self.embed_len: raise ValueError(f"Embedding length {len(embd)} does not match expected {self.embed_len}") if self.keys is None: raise RuntimeError("Keys not initialized") - padded_embd = [] + padded_embd: list[str] = [] for i in range(self.embed_len): if embd[i] not in (0, 1): raise ValueError(f"Embedding vector must be binary, got {embd[i]} at index {i}") - padded_embd += ['0', str(embd[i])] + padded_embd += ["0", str(embd[i])] int_repr = [ - gmpy2.mpz(int("".join(padded_embd[i * self.chunk_len:(i + 1) * self.chunk_len]), 2)) + gmpy2.mpz(int("".join(padded_embd[i * self.chunk_len : (i + 1) * self.chunk_len]), 2)) for i in range(self.chunk_num) ] - return [Paillier.encrypt(i, self.keys['pk']) for i in int_repr] + return [Paillier.encrypt(i, self.keys["pk"]) for i in int_repr] def decode_hamming_client(self, cipher: list[int | bytes]) -> int: - """Decrypt an encoded Hamming distance returned by the XTrace server. - - :param cipher: Encrypted Hamming encoding as returned by the server. - :type cipher: PaillierEncryptedNumber - :return: Plain-text Hamming distance. - :rtype: int - """ if self.keys is None: raise RuntimeError("Keys not initialized") - de_c = [Paillier.decrypt(int.from_bytes(c, byteorder='little') if isinstance(c, bytes) else c, self.keys) for c in cipher] - bin_c_truncated = [f"{c:b}" for c in de_c] - bin_c_str = "" - for c in bin_c_truncated: - if len(c) != self.chunk_len and len(c) != 0: - c = "0" * (self.chunk_len - len(c)) + c - bin_c_str += c - ham = 0 - for i in range(1, len(bin_c_str), 2): - ham += int(bin_c_str[i]) - return ham + decrypted = [ + Paillier.decrypt(int.from_bytes(c, byteorder="little") if isinstance(c, bytes) else c, self.keys) + for c in cipher + ] + bin_chunks = [f"{value:b}" for value in decrypted] + bin_string = "" + for chunk in bin_chunks: + if len(chunk) != self.chunk_len and len(chunk) != 0: + chunk = "0" * (self.chunk_len - len(chunk)) + chunk + bin_string += chunk + return sum(int(bin_string[i]) for i in range(1, len(bin_string), 2)) class PaillierClient(HammingClientBase): - """Paillier homomorphic encryption client optimised for computing encrypted Hamming distances - between binary embedding vectors. Dispatches to CPU or GPU backend via the ``DEVICE`` - environment variable (``cpu`` by default). + """Paillier client that dispatches to CPU or GPU based on the ``device`` kwarg. + + With ``device="auto"`` (the default), the GPU extension is probed at + construction time and used if available, otherwise the CPU implementation + runs. ``"cpu"`` and ``"gpu"`` force a backend; ``"gpu"`` raises if the + extension cannot be loaded. """ - def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool = False) -> None: - """Generate a Paillier keypair and configure the client. - - :param embed_len: Dimension of the binary embedding vectors to encrypt, defaults to 512. - :type embed_len: int, optional - :param key_len: RSA modulus size in bits for key generation, defaults to 1024. - :type key_len: int, optional - :param skip_key_gen: If True, skip key generation (keys must be loaded later via - ``load_stringified_keys``), defaults to False. - :type skip_key_gen: bool, optional - """ + def __init__( + self, + embed_len: int = 512, + key_len: int = 1024, + skip_key_gen: bool = False, + device: DeviceMode = "auto", + ) -> None: self.embed_len = embed_len self.key_len = key_len self.chunk_len = key_len * 2 @@ -247,104 +134,98 @@ def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool else: self.chunk_num = 1 - self.device = DEVICE - self.client: PaillierGPU | PaillierCPU - if self.device == "gpu": - if not self.has_gpu(): - raise RuntimeError("GPU requested via DEVICE=gpu, but GPU backend is unavailable.") - self.client = PaillierGPU(embed_len=embed_len, key_len=key_len, skip_key_gen=skip_key_gen) + self.device, gpu_cls = resolve_device(device, _load_gpu_backend) + if self.device == "gpu" and gpu_cls is not None: + self.client: Any = gpu_cls(embed_len=embed_len, key_len=key_len, skip_key_gen=skip_key_gen) else: self.client = PaillierCPU(embed_len=embed_len, key_len=key_len, skip_key_gen=skip_key_gen) @staticmethod def has_gpu() -> bool: - """Return ``True`` if the GPU backend extension is available and loadable. - - :rtype: bool - """ try: - from xtrace_sdk.x_vec.crypto.paillier_client import PaillierGPU - PaillierGPU(skip_key_gen=True) + _load_gpu_backend()(skip_key_gen=True) return True except Exception: return False - def encrypt_vec_one(self, embd: list[int]) -> PaillierEncryptedNumber: - """Encrypt a single binary embedding vector. + @staticmethod + def _cipher_to_int(value: Any) -> int: + if isinstance(value, bytes): + return int.from_bytes(value, byteorder="little") + if isinstance(value, str): + return int(value, 16) + return int(value) - :param embd: Binary vector of length ``embed_len`` with values in {0, 1}. - :type embd: list[int] - :return: Encrypted vector. - :rtype: PaillierEncryptedNumber - """ + @staticmethod + def _cipher_to_hex(value: int | bytes) -> str: + if isinstance(value, bytes): + return format(int.from_bytes(value, byteorder="little"), "x") + return format(int(value), "x") + + def encrypt_vec_one(self, embd: list[int]) -> EncryptedVector: if isinstance(self.client, PaillierCPU): return self.client.encrypt(embd) - else: - return self.client.encrypt([embd])[0] + row = self.client.encrypt([embd])[0] + return [self._cipher_to_int(cipher) for cipher in row] - def encrypt_vec_batch(self, embds: list[list[int]]) -> list[PaillierEncryptedNumber]: - """Encrypt a batch of binary embedding vectors. - - :param embds: List of binary vectors, each of length ``embed_len`` with values in {0, 1}. - :type embds: list[list[int]] - :return: List of encrypted vectors. - :rtype: list[PaillierEncryptedNumber] - """ + def encrypt_vec_batch(self, embds: list[list[int]]) -> list[EncryptedVector]: if isinstance(self.client, PaillierCPU): return [self.client.encrypt(embd) for embd in embds] - else: - return self.client.encrypt(embds) + return [[self._cipher_to_int(cipher) for cipher in row] for row in self.client.encrypt(embds)] def decode_hamming_client_one(self, cipher: list[int | bytes]) -> int: - """Decrypt a single encrypted Hamming distance returned by the XTrace server. - - :param cipher: Encrypted Hamming encoding. - :type cipher: PaillierEncryptedNumber - :return: Plain-text Hamming distance. - :rtype: int - """ if isinstance(self.client, PaillierCPU): return self.client.decode_hamming_client(cipher) - else: - return self.client.decode_hamming_client([cipher])[0] + return int(self.client.decode_hamming_client([[self._cipher_to_hex(value) for value in cipher]])[0]) def decode_hamming_client_batch(self, ciphers: list[list[int | bytes]]) -> list[int]: - """Decrypt a batch of encrypted Hamming distances returned by the XTrace server. - - :param ciphers: List of encrypted Hamming encodings. - :type ciphers: list[PaillierEncryptedNumber] - :return: List of plain-text Hamming distances. - :rtype: list[int] - """ if isinstance(self.client, PaillierCPU): - return [self.client.decode_hamming_client(c) for c in ciphers] - else: - return self.client.decode_hamming_client(ciphers) + return [self.client.decode_hamming_client(cipher) for cipher in ciphers] + return [ + int(value) + for value in self.client.decode_hamming_client( + [[self._cipher_to_hex(entry) for entry in cipher] for cipher in ciphers] + ) + ] def stringify_pk(self) -> str: - """Return a JSON string representation of the public key.""" return self.client.stringify_pk() def stringify_sk(self) -> str: - """Return a JSON string representation of the secret key.""" return self.client.stringify_sk() def stringify_config(self) -> str: - """Return a JSON string representation of the encryption configuration.""" return self.client.stringify_config() def load_stringified_keys(self, pk: str, sk: str) -> None: - """Load public and secret keys from their JSON string representations. - - :param pk: JSON-encoded public key produced by :meth:`stringify_pk`. - :param sk: JSON-encoded secret key produced by :meth:`stringify_sk`. - """ self.client.load_stringified_keys(pk, sk) def load_config(self, config: dict) -> None: - """Load encryption configuration from a dict produced by :meth:`stringify_config`. + sanitized_config = {k: v for k, v in config.items() if k != "device"} + self.client.load_config(sanitized_config) + self.embed_len = config["embed_len"] + self.key_len = config["key_len"] + self.chunk_len = self.key_len * 2 + if self.embed_len > self.key_len: + self.chunk_num = 2 * self.embed_len // self.chunk_len + int((2 * self.embed_len) % self.chunk_len != 0) + else: + self.chunk_num = 1 - :param config: Configuration dict (``embed_len``, ``key_len``). - :type config: dict - """ - self.client.load_config(config) + def encode_hamming_server(self, ct1: list[int | bytes], ct2: list[int | bytes]) -> EncryptedVector: + if len(ct1) != len(ct2): + raise ValueError("ct1 and ct2 must have the same length") + if isinstance(self.client, PaillierCPU): + if self.client.keys is None: + raise RuntimeError("Keys not initialized") + return [ + int(Paillier.add(self._cipher_to_int(a), self._cipher_to_int(b), self.client.keys["pk"])) + for a, b in zip(ct1, ct2, strict=True) + ] + + pk = json.loads(self.stringify_pk()) + result = type(self.client).encode_hamming_server( + [self._cipher_to_hex(cipher) for cipher in ct1], + [self._cipher_to_hex(cipher) for cipher in ct2], + {"n_squared": format(int(pk["n_squared"]), "x")}, + ) + return [self._cipher_to_int(cipher) for cipher in result] diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/Makefile b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/Makefile new file mode 100644 index 0000000..fda1ef0 --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/Makefile @@ -0,0 +1,61 @@ +# GPU_paillier_client/Makefile +# +# Build a Python extension module paillier_GPU_client.* for different GPU archs and key sizes. +# +# Usage examples: +# make # defaults: KEY_BITS=1024, SMS="70 80 90", TPI=32 +# make KEY_BITS=2048 SMS="80 90" # compile for A100/H100 with 8192-bit n^2 +# make KEY_BITS=1024 TPI=32 SMS="86" # compile for RTX 30-series (sm_86) +# +# Directory layout: +# ./paillier_gpu_ext/paillier_GPU_client.cu +# build_gpu_binaries.sh materialises include/{pybind11,cgbn} and passes INCDIR + +# -------- Parameters you can tweak on the command line -------- +KEY_BITS ?= 1024 +TPI ?= 32 +# List of SM versions to build for (space separated) +SMS ?= 70 80 86 89 90 + +# Compiler/paths +NVCC ?= nvcc +CUDA_PATH?= /usr/local/cuda + +# Python extension suffix (e.g., .cpython-311-x86_64-linux-gnu.so) +PY_INC := $(shell python3 -c 'import sysconfig, sys; print(sysconfig.get_paths()["include"])') +EXT := $(shell python3 -c "import sysconfig;print(sysconfig.get_config_var('EXT_SUFFIX'))") +TARGET := paillier_GPU_client$(EXT) + +SRC := paillier_GPU_client.cu +INCDIR ?= ../_gpu_build_include +CXXSTD := -std=c++17 + +# Construct gencode flags for each SM +GENCODE := $(foreach sm,$(SMS),-gencode arch=compute_$(sm),code=sm_$(sm)) + +# Include paths +INCLUDES := -I$(INCDIR) -I$(INCDIR)/pybind11 -I$(INCDIR)/cgbn -I$(CUDA_PATH)/include -I$(PY_INC) + +# Build flags. -cudart static keeps libcudart embedded so the wheel doesn't +# require the user to have a matching libcudart.so installed at runtime. +NVCCFLAGS := $(CXXSTD) -O3 -Xcompiler -fPIC -shared \ + -cudart static \ + -DKEY_BITS=$(KEY_BITS) -DCGBN_TPI=$(TPI) -DCGBN_USE_GMP \ + -Wno-deprecated-gpu-targets \ + $(GENCODE) $(INCLUDES) + +# Link GMP for the CPU fallback (header demands GMP present) +LDLIBS := -lgmp + +# Note: pybind11 is header-only; normally you don't need to link libpython explicitly. +# If your system requires it, uncomment the following: +# PY_LDFLAGS := $(shell python3-config --ldflags) +# LDFLAGS := $(PY_LDFLAGS) + +all: $(TARGET) + +$(TARGET): $(SRC) + $(NVCC) $(NVCCFLAGS) -o $@ $< $(LDLIBS) + +clean: + rm -f $(TARGET) diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/__init__.py b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/__init__.py new file mode 100644 index 0000000..2598fd4 --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/__init__.py @@ -0,0 +1,3 @@ +from .paillier_GPU_client import PaillierGPUClient + +__all__ = ["PaillierGPUClient"] diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/paillier_GPU_client.cu b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/paillier_GPU_client.cu new file mode 100644 index 0000000..e4e6a6f --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_gpu_ext/paillier_GPU_client.cu @@ -0,0 +1,1166 @@ +// production_GPU_paillier_client/paillier_GPU_client.cu +// +// This file implements a CUDA-accelerated Paillier client whose +// public API mirrors xtrace_sdk.crypto.paillier_client.PaillierClient: +// +// class PaillierGPUClient: +// def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool = False) +// def encrypt(self, embd: list[int]) -> list[int] +// def encode_hamming_client(self, ct1: list[int], ct2: list[int]) -> list[int] +// @staticmethod +// def encode_hamming_server(ct1: list[int], ct2: list[int], pk: dict) -> list[int] +// def decode_hamming_client(self, cipher: list[int]) -> int +// +// Keys are generated once in the constructor (unless skip_key_gen=True) +// using GMP big integers on the CPU, and stored inside the C++ class. +// GPU kernels use NVlabs CGBN for modular arithmetic on ciphertexts. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "pybind11/pybind11.h" +#include "pybind11/stl.h" + +#include +#include "cgbn/cgbn.h" + +namespace py = pybind11; + +// ---------------------- Compile-time configuration ---------------------- +#ifndef KEY_BITS +#define KEY_BITS 1024 +#endif + +#ifndef CGBN_TPI +#define CGBN_TPI 32 +#endif + +// In the Python client, key_len is the prime bit-length. +// n is roughly 2*key_len bits, and we pad plaintext chunks to 2*key_len bits. +#define PAILLIER_N_SQUARED_BITS (KEY_BITS*4) +static constexpr int LIMBS = (PAILLIER_N_SQUARED_BITS + 31) / 32; +static constexpr int RANDOM_CANDIDATES_PER_INSTANCE = 16; + +// ---------------------- Error handling helpers ---------------------- +#define CUDA_CHECK(call) do { \ + cudaError_t _e = (call); \ + if (_e != cudaSuccess) { \ + std::ostringstream _oss; \ + _oss << "CUDA error " << cudaGetErrorName(_e) \ + << " (" << cudaGetErrorString(_e) << ") at " << __FILE__ \ + << ":" << __LINE__; \ + throw std::runtime_error(_oss.str()); \ + } \ +} while(0) + +// ---------------------- Minimal GMP big-int wrapper ---------------------- +namespace bigint { + +class big_int_t { +public: + mpz_t v; + + big_int_t() { mpz_init(v); } + explicit big_int_t(unsigned long long x) { mpz_init(v); mpz_set_ui(v, static_cast(x)); } + explicit big_int_t(const std::string &s, int base = 10) { + mpz_init(v); + if (mpz_set_str(v, s.c_str(), base) != 0) { + throw std::invalid_argument("big_int_t: invalid string"); + } + } + big_int_t(const big_int_t &o) { mpz_init_set(v, o.v); } + big_int_t(big_int_t &&o) noexcept { mpz_init(v); mpz_swap(v, o.v); } + ~big_int_t() { mpz_clear(v); } + + big_int_t &operator=(const big_int_t &o) { + if (this != &o) mpz_set(v, o.v); + return *this; + } + big_int_t &operator=(big_int_t &&o) noexcept { + if (this != &o) mpz_swap(v, o.v); + return *this; + } + + std::string to_string(int base = 10) const { + char *raw = mpz_get_str(nullptr, base, v); + if (!raw) return {}; + std::string s(raw); + void (*freefunc)(void*, size_t) = nullptr; + mp_get_memory_functions(nullptr, nullptr, &freefunc); + if (freefunc) freefunc(raw, std::strlen(raw) + 1); + return s; + } +}; + +inline bigint::big_int_t powm(const bigint::big_int_t &base, + const bigint::big_int_t &exp, + const bigint::big_int_t &mod) { + bigint::big_int_t r; + mpz_powm(r.v, base.v, exp.v, mod.v); + return r; +} + +inline bigint::big_int_t gcd(const bigint::big_int_t &a, const bigint::big_int_t &b) { + bigint::big_int_t r; + mpz_gcd(r.v, a.v, b.v); + return r; +} + +inline void fill_os_random(void *data, std::size_t size) { + std::ifstream rng("/dev/urandom", std::ios::in | std::ios::binary); + if (!rng) { + throw std::runtime_error("secure randomness: failed to open /dev/urandom"); + } + const auto requested = static_cast(size); + rng.read(static_cast(data), requested); + if (rng.gcount() != requested) { + throw std::runtime_error("secure randomness: failed to read enough bytes from /dev/urandom"); + } +} + +inline bigint::big_int_t random_bits(unsigned bits) { + if (bits == 0) throw std::invalid_argument("bits must be > 0"); + const std::size_t byte_count = (static_cast(bits) + 7) / 8; + std::vector bytes(byte_count); + fill_os_random(bytes.data(), bytes.size()); + + const unsigned excess_bits = static_cast(byte_count * 8 - bits); + if (excess_bits > 0) { + bytes[0] &= static_cast(0xFFu >> excess_bits); + } + + bigint::big_int_t out; + mpz_import(out.v, bytes.size(), 1, 1, 0, 0, bytes.data()); + return out; +} + +inline bigint::big_int_t random_prime_bits(unsigned bits) { + if (bits < 2) throw std::invalid_argument("bits must be >= 2"); + bigint::big_int_t x = random_bits(bits); + mpz_setbit(x.v, bits - 1); + if (mpz_even_p(x.v)) mpz_add_ui(x.v, x.v, 1); + bigint::big_int_t p; + mpz_nextprime(p.v, x.v); + while (static_cast(mpz_sizeinbase(p.v, 2)) != bits) { + x = random_bits(bits); + mpz_setbit(x.v, bits - 1); + if (mpz_even_p(x.v)) mpz_add_ui(x.v, x.v, 1); + mpz_nextprime(p.v, x.v); + } + return p; +} + +} // namespace bigint + +// ---------------------- CPU Paillier key structures ---------------------- +struct PaillierPublicKeyCPU { + bigint::big_int_t g; + bigint::big_int_t n; + bigint::big_int_t n_squared; +}; + +struct PaillierSecretKeyCPU { + bigint::big_int_t phi; + bigint::big_int_t inv; +}; + +struct PaillierKeyPairCPU { + PaillierPublicKeyCPU pk; + PaillierSecretKeyCPU sk; +}; + +class PaillierCPU { +public: + static PaillierKeyPairCPU key_gen(unsigned prime_bits) { + using namespace bigint; + + big_int_t p = random_prime_bits(prime_bits); + big_int_t q = random_prime_bits(prime_bits); + + big_int_t n; + mpz_mul(n.v, p.v, q.v); + + big_int_t p_minus_1, q_minus_1; + mpz_sub_ui(p_minus_1.v, p.v, 1); + mpz_sub_ui(q_minus_1.v, q.v, 1); + + big_int_t phi; + mpz_mul(phi.v, p_minus_1.v, q_minus_1.v); + + big_int_t g; + mpz_add_ui(g.v, n.v, 1); + + big_int_t n2; + mpz_mul(n2.v, n.v, n.v); + + big_int_t inv; + if (!mpz_invert(inv.v, phi.v, n.v)) { + throw std::runtime_error("Paillier key_gen: phi^{-1} mod n does not exist"); + } + + PaillierPublicKeyCPU pk{g, n, n2}; + PaillierSecretKeyCPU sk{phi, inv}; + return PaillierKeyPairCPU{pk, sk}; + } +}; + +// ---------------------- Hex <-> limb conversion ---------------------- +static inline uint32_t hex_nibble(char c) { + if (c >= '0' && c <= '9') return (uint32_t)(c - '0'); + if (c >= 'a' && c <= 'f') return (uint32_t)(10 + c - 'a'); + if (c >= 'A' && c <= 'F') return (uint32_t)(10 + c - 'A'); + throw std::runtime_error("Invalid hex digit"); +} + +static std::string normalize_hex(const std::string &s) { + std::string h = s; + if (h.size() >= 2 && h[0] == '0' && (h[1] == 'x' || h[1] == 'X')) h = h.substr(2); + size_t p = 0; + while (p < h.size() && h[p] == '0') ++p; + if (p == h.size()) return "0"; + return h.substr(p); +} + +static std::vector hex_to_le_words(const std::string &hex_in, int words) { + std::string h = normalize_hex(hex_in); + if (h == "0") return std::vector(words, 0u); + if (h.size() & 1) h = "0" + h; + std::vector bytes(h.size() / 2); + for (size_t i = 0, j = 0; i < h.size(); i += 2, ++j) { + uint8_t hi = (uint8_t)hex_nibble(h[i]); + uint8_t lo = (uint8_t)hex_nibble(h[i + 1]); + bytes[j] = (uint8_t)((hi << 4) | lo); + } + std::vector words_le(words, 0u); + const size_t num_bytes = bytes.size(); + for (size_t i = 0; i < num_bytes; ++i) { + size_t limb_index = i / 4; + size_t byte_index = i % 4; + if (limb_index >= (size_t)words) break; + words_le[limb_index] |= ((uint32_t)bytes[num_bytes - 1 - i]) << (8 * byte_index); + } + return words_le; +} + +static std::string le_words_to_hex(const std::vector &words) { + std::vector bytes; + bytes.reserve(words.size() * 4); + for (uint32_t w : words) { + bytes.push_back((uint8_t)(w & 0xFF)); + bytes.push_back((uint8_t)((w >> 8) & 0xFF)); + bytes.push_back((uint8_t)((w >> 16) & 0xFF)); + bytes.push_back((uint8_t)((w >> 24) & 0xFF)); + } + while (!bytes.empty() && bytes.back() == 0) bytes.pop_back(); + if (bytes.empty()) return "0"; + static const char *HEX = "0123456789abcdef"; + std::string hex; + hex.resize(bytes.size() * 2); + size_t idx = 0; + for (int i = (int)bytes.size() - 1; i >= 0; --i) { + uint8_t b = bytes[i]; + hex[idx++] = HEX[(b >> 4) & 0xF]; + hex[idx++] = HEX[b & 0xF]; + } + size_t p = 0; + while (p + 1 < hex.size() && hex[p] == '0') ++p; + return hex.substr(p); +} + +// hex -> binary string (MSB-first), no fixed width; "0" for zero +static inline std::string hex_to_bin_nopad(const std::string &hex) { + auto hn = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); + if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); + return 0; + }; + static const char *LUT[16] = { + "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", + "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; + std::string out; + out.reserve(hex.size() * 4); + for (char c : hex) out.append(LUT[hn(c)]); + size_t p = out.find_first_not_of('0'); + if (p == std::string::npos) return "0"; + return out.substr(p); +} + +// ---------------------- CGBN types ---------------------- +typedef cgbn_context_t context_t; +typedef cgbn_env_t env_t; +typedef cgbn_error_report_t error_report_t; +typedef cgbn_mem_t mem_t; + + + +// ------------------ Arithmetic helpers ------------------ + +// Extract pointer to 32-bit limbs from mem_t (little-endian) +static __device__ __forceinline__ const uint32_t* limbs_of(const mem_t *m) { + return reinterpret_cast(m); +} + +// Find MSB index (bit position) of exponent in mem_t (return -1 if zero) +static __device__ __forceinline__ int msb_index_mem(const mem_t *exp_mem) { + const uint32_t *w = limbs_of(exp_mem); + for (int i=LIMBS-1; i>=0; --i) { + uint32_t v = w[i]; + if (v != 0) { + // position of highest set bit within word + int lz = __clz(v); // 0..32 + int bit = 31 - lz; // 0..31 + return i*32 + bit; + } + } + return -1; +} + +// Square-and-multiply: result = base^exp_mem (mod mod_n2) +// exp_mem is provided as mem_t (n's limbs) +static __device__ void pow_mod_mem(env_t &env, + typename env_t::cgbn_t &result, + const typename env_t::cgbn_t &base, + const mem_t *exp_mem, + const typename env_t::cgbn_t &mod_n2) { + typename env_t::cgbn_t res, b; + typename env_t::cgbn_wide_t wide; + + cgbn_set_ui32(env, res, 1); + cgbn_set(env, b, base); + + const uint32_t *EW = reinterpret_cast(exp_mem); + + // Iterate all LIMBS*32 bits, LSB to MSB + for (int w = 0; w < LIMBS; ++w) { + uint32_t x = EW[w]; + for (int k = 0; k < 32; ++k) { + if (x & 1u) { + cgbn_mul_wide(env, wide, res, b); + cgbn_rem_wide(env, res, wide, mod_n2); + } + // b = b*b mod mod_n2 + cgbn_mul_wide(env, wide, b, b); + cgbn_rem_wide(env, b, wide, mod_n2); + x >>= 1; + } + } + cgbn_set(env, result, res); +} + +// Euclidean GCD (returns gcd(a,b) in 'a'); destructive to inputs +static __device__ void gcd_big(env_t &env, + typename env_t::cgbn_t &a, + typename env_t::cgbn_t &b) { + typename env_t::cgbn_t t; + while (cgbn_compare_ui32(env, b, 0) != 0) { + cgbn_rem(env, t, a, b); + cgbn_set(env, a, b); + cgbn_set(env, b, t); + } +} + +// Sample r in [1, n-1] and (optionally) ensure gcd(r, n) = 1. +// Very unlikely to need retries. +static __device__ void sample_r(env_t &env, + typename env_t::cgbn_t &r, + const typename env_t::cgbn_t &n, + mem_t *random_candidates, + int instance_id, + bool ensure_coprime=true) { + typename env_t::cgbn_t tmp, a, b; + typename env_t::cgbn_t n_minus_1; + cgbn_sub_ui32(env, n_minus_1, n, 1); + + for (int tries=0; tries ensures 1..n-1 + cgbn_load(env, tmp, &random_candidates[instance_id * RANDOM_CANDIDATES_PER_INSTANCE + tries]); + cgbn_rem(env, tmp, tmp, n_minus_1); + cgbn_add_ui32(env, r, tmp, 1); + + if (!ensure_coprime) return; + + // Check gcd(r, n) == 1 + cgbn_set(env, a, n); + cgbn_set(env, b, r); + gcd_big(env, a, b); // gcd in 'a' + if (cgbn_compare_ui32(env, a, 1) == 0) + return; // good + } + + // Fallback: r=1 (extremely unlikely path) + cgbn_set_ui32(env, r, 1); +} + + + +// Build a mask that selects bits at MSB-first positions 1,3,5,... within width=chunk_len +// mapped to little-endian limb storage used by cgbn_mem_t. +static inline mem_t make_odd_mask_host(const int chunk_len) { + mem_t mask{}; + uint32_t *w = reinterpret_cast(&mask); + // zero init + for (int i=0;i> 5; + const int pos = lsb_index & 31; + w[idx] |= (1u << pos); + } + return mask; +} + + + + +// ---------------------- CUDA kernel ---------------------- +__global__ void modmul_pointwise_kernel(error_report_t *report, + mem_t *A, + mem_t *B, + mem_t *MOD, // single modulus at index 0 + mem_t *R, + int count) { + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= count) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + typename env_t::cgbn_t a, b, m, r; + typename env_t::cgbn_wide_t wide; // <<< wide 2*BITS + cgbn_load(env, a, &A[instance]); + cgbn_load(env, b, &B[instance]); + cgbn_load(env, m, &MOD[0]); + + // Use CGBN modular multiply: r = (a*b) mod m + cgbn_mul_wide(env, wide, a, b); // wide = a*b + cgbn_rem_wide(env, r, wide, m); // r = wide % m + + cgbn_store(env, &R[instance], r); +} + + +// Each instance handles one (vector, chunk) +__global__ void encrypt_kernel(error_report_t *report, + const uint8_t *all_bits, // [batch * embed_len], 0/1 + int batch, + int embed_len, + int chunk_len, + int chunk_num, + mem_t *N, // n + mem_t *N2, // n^2 + mem_t *random_candidates, + mem_t *out_ct) { // [batch * chunk_num] + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + int total = batch * chunk_num; + if (instance >= total) return; + + int vec_idx = instance / chunk_num; + int chunk_idx = instance % chunk_num; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load n and n^2 into bigints + typename env_t::cgbn_t n, n2; + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + + // --- Build plaintext m from padded bits within this chunk --- + // Padded scheme: for original bit x[j], padded has at positions: 2*j -> 0, 2*j+1 -> x[j]. + typename env_t::cgbn_t m; + cgbn_set_ui32(env, m, 0); + + const int padded_total = 2 * embed_len; // total padded bits for this vector + const int chunk_base = chunk_idx * chunk_len; // start bit in padded stream + const uint8_t *vec_bits = all_bits + vec_idx * embed_len; + + // Only consume the bits that actually exist in this chunk, do NOT extend to chunk_len + int avail = 0; + if (chunk_base < padded_total) { + int max_take = padded_total - chunk_base; + avail = (max_take > chunk_len) ? chunk_len : max_take; + } + + // Build m by scanning MSB->LSB *for the avail bits only* + for (int t=0; t carries original bit + int j = p >> 1; // source index in the embedding + bit = (uint32_t)(vec_bits[j] & 1); + } + if (bit) { + cgbn_add_ui32(env, m, m, 1); + } + } + + // Make sure 0 <= m < n (same invariant as Python version) + // If m >= n, reduce m modulo n (more permissive than Python assert) + if (cgbn_compare(env, m, n) >= 0) { + cgbn_rem(env, m, m, n); + } + + // --- Compute a = 1 + m*n (mod n^2) using (1+n)^m trick --- + typename env_t::cgbn_t a, prod; + typename env_t::cgbn_wide_t wide; + cgbn_mul_wide(env, wide, m, n); + cgbn_rem_wide(env, prod, wide, n2); + cgbn_add_ui32(env, a, prod, 1); + if (cgbn_compare(env, a, n2) >= 0) { + cgbn_sub(env, a, a, n2); + } + + // --- Sample r and compute b = r^n mod n^2 --- + typename env_t::cgbn_t r, b; + // DEBUG: force r=1 to remove the random factor during debugging + // cgbn_set_ui32(env, r, 1); + sample_r(env, r, n, random_candidates, instance, true); + pow_mod_mem(env, b, r, N, n2); // exponent is n, supplied as mem_t N + + // --- c = (a * b) mod n^2 --- + typename env_t::cgbn_t c; + cgbn_mul_wide(env, wide, a, b); + cgbn_rem_wide(env, c, wide, n2); + + // Store result + cgbn_store(env, &out_ct[instance], c); +} + +// ---------- GPU: decrypt-only kernel (per-chunk) ---------- +__global__ void decrypt_only_kernel(error_report_t *report, + mem_t *CT, // [total] ciphertexts + int total, // batch * chunk_num + mem_t *N, + mem_t *N2, + mem_t *PHI, + mem_t *INV, + mem_t *OUT_M) { // [total] plaintext m + const int thread = blockIdx.x*blockDim.x + threadIdx.x; + const int instance = thread / CGBN_TPI; + if (instance >= total) return; + + typedef cgbn_context_t context_t; + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + typename env_t::cgbn_t n, n2, phi, inv, c, t, x, q, rrem, m; + typename env_t::cgbn_wide_t wide; + + cgbn_load(env, c, &CT[instance]); + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + cgbn_load(env, phi, &PHI[0]); + cgbn_load(env, inv, &INV[0]); + + // t = c^phi mod n^2 (LSB-first exp) + pow_mod_mem(env, t, c, PHI, n2); + + // x = t - 1, then exact div by n: x = q*n + rrem + cgbn_sub_ui32(env, x, t, 1); + cgbn_div(env, q, x, n); + cgbn_rem(env, rrem, x, n); // for debug you can check rrem==0 + + // m = (q * inv) mod n + cgbn_mul_wide(env, wide, q, inv); + cgbn_rem_wide(env, m, wide, n); + + cgbn_store(env, &OUT_M[instance], m); +} + + + +// Each instance handles one (vector, chunk) ciphertext -> produces count of ones at odd bit positions +__global__ void decode_hamming_kernel(error_report_t *report, + mem_t *CT, // [total] ciphertexts + int total, // batch * chunk_num + int chunk_len, // = 2*key_len + mem_t *N, mem_t *N2, + mem_t *PHI, mem_t *INV, + uint32_t *out_odd, // <-- NEW name + uint32_t *out_even, // <-- NEW + uint32_t *out_rem_nz, // <-- NEW + mem_t *out_m_dbg, // <-- OPTIONAL (can pass nullptr) + /* Additional parameters for GPU mask inputs for bitcounting*/ + const mem_t *ODD_MASK, + int words_used) +{ + + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= total) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load inputs + typename env_t::cgbn_t n, n2, phi, inv, c; + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + cgbn_load(env, phi, &PHI[0]); + cgbn_load(env, inv, &INV[0]); + cgbn_load(env, c, &CT[instance]); + + // Decrypt: t = c^phi mod n^2 + typename env_t::cgbn_t t, x, q, rrem, m; +typename env_t::cgbn_wide_t wide; + +pow_mod_mem(env, t, c, PHI, n2); // t = c^phi mod n^2 +// x = t - 1 (t is in [1, n^2-1], so this is >= 0) +cgbn_sub_ui32(env, x, t, 1); + +// Divide exactly: x = q*n + rrem (rrem must be 0) +cgbn_div(env, q, x, n); +cgbn_rem(env, rrem, x, n); +uint32_t rem_nz = (cgbn_compare_ui32(env, rrem, 0) != 0); + +// m = (q * inv) mod n +cgbn_mul_wide(env, wide, q, inv); +cgbn_rem_wide(env, m, wide, n); + +// Store m for host-side inspection if requested +if (out_m_dbg != nullptr) { + cgbn_store(env, &out_m_dbg[instance], m); +} + +// Count odd/even bit positions MSB-first over chunk_len +// ---- PURE GPU COUNTING via mask+popcount ---- + mem_t mmem; + cgbn_store(env, &mmem, m); + + const uint32_t *mw = reinterpret_cast(&mmem); + const uint32_t *ow = reinterpret_cast(&ODD_MASK[0]); + + uint32_t odd_cnt = 0; + #pragma unroll + for (int w=0; w gpu_pointwise_mulmod_hex(const std::vector &ct1_hex, + const std::vector &ct2_hex, + const std::string &n_squared_hex) { + if (ct1_hex.size() != ct2_hex.size()) { + throw std::runtime_error("encode_hamming_*: ct1 and ct2 must have the same length"); + } + const int count = static_cast(ct1_hex.size()); + if (count == 0) return {}; + + std::vector h_A(count), h_B(count); + std::vector mod_words = hex_to_le_words(n_squared_hex, LIMBS); + mem_t h_MOD_one{}; + std::memcpy(&h_MOD_one, mod_words.data(), LIMBS * sizeof(uint32_t)); + + for (int i = 0; i < count; ++i) { + auto a = hex_to_le_words(ct1_hex[i], LIMBS); + auto b = hex_to_le_words(ct2_hex[i], LIMBS); + std::memcpy(&h_A[i], a.data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_B[i], b.data(), LIMBS * sizeof(uint32_t)); + } + + error_report_t *d_report = nullptr; + mem_t *d_A = nullptr, *d_B = nullptr, *d_MOD = nullptr, *d_R = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_A, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_B, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_R, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_MOD, sizeof(mem_t))); + + CUDA_CHECK(cudaMemcpy(d_A, h_A.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_B, h_B.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_MOD, &h_MOD_one, sizeof(mem_t), cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (count + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + modmul_pointwise_kernel<<>>(d_report, d_A, d_B, d_MOD, d_R, count); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_R(count); + CUDA_CHECK(cudaMemcpy(h_R.data(), d_R, count * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_A); + cudaFree(d_B); + cudaFree(d_R); + cudaFree(d_MOD); + cudaFree(d_report); + + std::vector out(count); + for (int i = 0; i < count; ++i) { + std::vector words(LIMBS); + std::memcpy(words.data(), &h_R[i], LIMBS * sizeof(uint32_t)); + out[i] = le_words_to_hex(words); + } + return out; +} + +// ---------------------- PaillierGPUClient class ---------------------- +class PaillierGPUClient { +public: + static int max_key_len() { return KEY_BITS; } + + PaillierGPUClient(int embed_len = 512, int key_len = KEY_BITS, bool skip_key_gen = false) + : embed_len_(embed_len), + key_len_(key_len), + chunk_len_(key_len * 2), + chunk_num_(embed_len > key_len ? ((2 * embed_len) / (key_len * 2) + + (((2 * embed_len) % (key_len * 2)) != 0)) + : 1), + have_keys_(!skip_key_gen) { + if (key_len_ > KEY_BITS) { + throw std::runtime_error( + "PaillierGPUClient: key_len=" + std::to_string(key_len_) + + " exceeds the compile-time KEY_BITS=" + std::to_string(KEY_BITS) + + " of this GPU extension. Rebuild with a larger KEY_BITS " + "(see build_gpu_binaries.sh) or use a smaller key_len."); + } + if (!skip_key_gen) { + keys_ = PaillierCPU::key_gen(static_cast(key_len_)); + pk_n_hex_ = keys_.pk.n.to_string(16); + pk_n2_hex_ = keys_.pk.n_squared.to_string(16); + sk_phi_hex_ = keys_.sk.phi.to_string(16); + sk_inv_hex_ = keys_.sk.inv.to_string(16); + // Light debug: print generated keys (hex) + // std::cerr << "[PaillierGPUClient] Generated keys (hex):\n" + // << " n = " << pk_n_hex_ << "\n" + // << " n_squared= " << pk_n2_hex_ << "\n" + // << " phi = " << sk_phi_hex_ << "\n" + // << " inv = " << sk_inv_hex_ << std::endl; + } + } + + int embed_len() const { return embed_len_; } + int key_len() const { return key_len_; } + int chunk_len() const { return chunk_len_; } + int chunk_num() const { return chunk_num_; } + + // Expose public key components as hex for Python-side checks + std::pair get_pk_hex() const { + if (!have_keys_) { + throw std::runtime_error("get_pk_hex: keys not initialized"); + } + return {pk_n_hex_, pk_n2_hex_}; + } + + // Expose full key components as hex: (n, n_squared, phi, inv) + std::tuple get_keys_hex() const { + if (!have_keys_) { + throw std::runtime_error("get_keys_hex: keys not initialized"); + } + return {pk_n_hex_, pk_n2_hex_, sk_phi_hex_, sk_inv_hex_}; + } + + std::string stringify_pk() const { + if (!have_keys_) { + throw std::runtime_error("stringify_pk: keys not initialized"); + } + py::object json = py::module_::import("json"); + bigint::big_int_t n(pk_n_hex_, 16); + bigint::big_int_t n_squared(pk_n2_hex_, 16); + bigint::big_int_t g; + mpz_add_ui(g.v, n.v, 1); + + py::dict pk_dict; + pk_dict["g"] = g.to_string(10); + pk_dict["n"] = n.to_string(10); + pk_dict["n_squared"] = n_squared.to_string(10); + return py::cast(json.attr("dumps")(pk_dict)); + } + + std::string stringify_sk() const { + if (!have_keys_) { + throw std::runtime_error("stringify_sk: keys not initialized"); + } + py::object json = py::module_::import("json"); + py::dict sk_dict; + sk_dict["phi"] = bigint::big_int_t(sk_phi_hex_, 16).to_string(10); + sk_dict["inv"] = bigint::big_int_t(sk_inv_hex_, 16).to_string(10); + return py::cast(json.attr("dumps")(sk_dict)); + } + + std::string stringify_config() const { + py::object json = py::module_::import("json"); + py::dict cfg; + cfg["embed_len"] = embed_len_; + cfg["key_len"] = key_len_; + return py::cast(json.attr("dumps")(cfg)); + } + + void load_stringified_keys(const std::string &pk_json, const std::string &sk_json) { + py::object json = py::module_::import("json"); + py::dict pk_dict = json.attr("loads")(pk_json).cast(); + py::dict sk_dict = json.attr("loads")(sk_json).cast(); + + auto decimal_field_as_hex = [](const py::dict &d, const char *name) { + return bigint::big_int_t(py::cast(py::str(d[name])), 10).to_string(16); + }; + + pk_n_hex_ = decimal_field_as_hex(pk_dict, "n"); + pk_n2_hex_ = decimal_field_as_hex(pk_dict, "n_squared"); + sk_phi_hex_ = decimal_field_as_hex(sk_dict, "phi"); + sk_inv_hex_ = decimal_field_as_hex(sk_dict, "inv"); + + bigint::big_int_t n2(pk_n2_hex_, 16); + const size_t n2_bits = mpz_sizeinbase(n2.v, 2); + if (n2_bits > static_cast(PAILLIER_N_SQUARED_BITS)) { + throw std::runtime_error( + "load_stringified_keys: n_squared has " + std::to_string(n2_bits) + + " bits, exceeding the compile-time capacity of " + + std::to_string(PAILLIER_N_SQUARED_BITS) + + " bits (KEY_BITS=" + std::to_string(KEY_BITS) + + "). Rebuild the GPU extension with a larger KEY_BITS."); + } + have_keys_ = true; + } + + void load_config(const py::dict &config) { + embed_len_ = py::cast(config["embed_len"]); + const int new_key_len = py::cast(config["key_len"]); + if (new_key_len > KEY_BITS) { + throw std::runtime_error( + "load_config: key_len=" + std::to_string(new_key_len) + + " exceeds the compile-time KEY_BITS=" + std::to_string(KEY_BITS) + + " of this GPU extension."); + } + key_len_ = new_key_len; + chunk_len_ = key_len_ * 2; + if (embed_len_ > key_len_) { + chunk_num_ = 2 * embed_len_ / chunk_len_ + int((2 * embed_len_) % chunk_len_ != 0); + } else { + chunk_num_ = 1; + } + } + + // Batched encrypt: embeddings is [batch][embed_len] of 0/1, returns [batch][chunk_num] hex ciphers + std::vector> + encrypt(const std::vector> &embeddings) const { + if (!have_keys_) { + throw std::runtime_error("encrypt: keys not initialized"); + } + const int batch = static_cast(embeddings.size()); + const int embed_len = embed_len_; + const int chunk_len = chunk_len_; + const int chunk_num = chunk_num_; + + if (batch == 0) return {}; + + // Validate shapes and binary values + for (int i = 0; i < batch; ++i) { + if ((int)embeddings[i].size() != embed_len) { + throw std::runtime_error("encrypt: each embedding must have length == embed_len"); + } + for (int v : embeddings[i]) { + if (v != 0 && v != 1) { + throw std::runtime_error("encrypt: embeddings must be binary 0/1"); + } + } + } + + // Flatten bits into [batch * embed_len] + std::vector h_bits; + h_bits.reserve(static_cast(batch) * static_cast(embed_len)); + for (int i = 0; i < batch; ++i) { + for (int j = 0; j < embed_len; ++j) { + h_bits.push_back(static_cast(embeddings[i][j])); + } + } + + std::vector n_words = hex_to_le_words(pk_n_hex_, LIMBS); + std::vector n2_words = hex_to_le_words(pk_n2_hex_, LIMBS); + mem_t h_N{}, h_N2{}; + std::memcpy(&h_N, n_words.data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, n2_words.data(), LIMBS * sizeof(uint32_t)); + + error_report_t *d_report = nullptr; + uint8_t *d_bits = nullptr; + mem_t *d_N = nullptr, *d_N2 = nullptr, *d_random = nullptr, *d_out = nullptr; + + const int total = batch * chunk_num; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + CUDA_CHECK(cudaMalloc((void **)&d_bits, + static_cast(batch) * static_cast(embed_len) * + sizeof(uint8_t))); + CUDA_CHECK(cudaMemcpy(d_bits, + h_bits.data(), + static_cast(batch) * static_cast(embed_len) * + sizeof(uint8_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_N, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_N, &h_N, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_N2, &h_N2, sizeof(mem_t), cudaMemcpyHostToDevice)); + + std::vector h_random( + static_cast(total) * static_cast(RANDOM_CANDIDATES_PER_INSTANCE)); + bigint::fill_os_random(h_random.data(), h_random.size() * sizeof(mem_t)); + CUDA_CHECK(cudaMalloc((void **)&d_random, h_random.size() * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_random, + h_random.data(), + h_random.size() * sizeof(mem_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_out, total * sizeof(mem_t))); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (total + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + encrypt_kernel<<>>(d_report, d_bits, batch, embed_len, chunk_len, + chunk_num, d_N, d_N2, d_random, d_out); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_out(total); + CUDA_CHECK(cudaMemcpy(h_out.data(), d_out, total * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_bits); + cudaFree(d_N); + cudaFree(d_N2); + cudaFree(d_random); + cudaFree(d_out); + cudaFree(d_report); + + // Reshape back to [batch][chunk_num] hex strings + std::vector> out(batch, std::vector(chunk_num)); + for (int i = 0; i < batch; ++i) { + for (int c = 0; c < chunk_num; ++c) { + int idx = i * chunk_num + c; + std::vector words(LIMBS); + std::memcpy(words.data(), &h_out[idx], LIMBS * sizeof(uint32_t)); + out[i][c] = le_words_to_hex(words); + } + } + return out; + } + + // Client-side homomorphic addition: ct1[i]*ct2[i] mod n^2 + std::vector encode_hamming_client(const std::vector &ct1, + const std::vector &ct2) const { + if (!have_keys_) { + throw std::runtime_error("encode_hamming_client: keys not initialized"); + } + return gpu_pointwise_mulmod_hex(ct1, ct2, pk_n2_hex_); + } + + // Server-side version: takes pk dict with "n_squared" + static std::vector encode_hamming_server(const std::vector &ct1, + const std::vector &ct2, + const py::dict &pk) { + auto n2_obj = pk["n_squared"]; + std::string n2_hex = py::cast(py::str(n2_obj)); + return gpu_pointwise_mulmod_hex(ct1, ct2, n2_hex); + } + + // GPU decrypt chunks to plaintext m (hex), then CPU bit-padding and odd-bit counting + std::vector + decode_hamming_client(const std::vector> &ciphers_hex_batch) const { + if (!have_keys_) { + throw std::runtime_error("decode_hamming_client: keys not initialized"); + } + const int batch = static_cast(ciphers_hex_batch.size()); + const int chunk_num = chunk_num_; + const int chunk_len = chunk_len_; + if (batch == 0) return {}; + + for (int i = 0; i < batch; ++i) { + if ((int)ciphers_hex_batch[i].size() != chunk_num) { + throw std::runtime_error("decode_hamming_client: each inner list must have length == chunk_num"); + } + } + + // 1) GPU decrypt per-chunk to get plaintext m as hex strings + auto m_hex_batch = decrypt_chunks(ciphers_hex_batch); + + // 2) CPU: mirror Python's padding and odd-bit counting + std::vector out(batch, 0); + for (int i = 0; i < batch; ++i) { + long long ham = 0; + std::string bin_concat; + bin_concat.reserve(static_cast(chunk_len) * static_cast(chunk_num)); + for (int c = 0; c < chunk_num; ++c) { + const std::string &mh = m_hex_batch[i][c]; + std::string b = hex_to_bin_nopad(mh); // MSB-first, minimal width ("0" for zero) + if ((int)b.size() != chunk_len && !b.empty()) { + if ((int)b.size() < chunk_len) { + b.insert(0, chunk_len - (int)b.size(), '0'); + } + } else if (b == "0") { + b.assign(static_cast(chunk_len), '0'); + } + bin_concat += b; + } + for (size_t k = 1; k < bin_concat.size(); k += 2) { + if (bin_concat[k] == '1') ++ham; + } + out[i] = ham; + } + return out; + } + +private: + int embed_len_; + int key_len_; + int chunk_len_; + int chunk_num_; + bool have_keys_; + + PaillierKeyPairCPU keys_; + std::string pk_n_hex_; + std::string pk_n2_hex_; + std::string sk_phi_hex_; + std::string sk_inv_hex_; + + // Helper: GPU decrypt all chunks to plaintext m (hex), mirroring original GPU client + std::vector> + decrypt_chunks(const std::vector> &ciphers_hex_batch) const { + const int batch = static_cast(ciphers_hex_batch.size()); + const int chunk_num = this->chunk_num_; + if (batch == 0) return {}; + + for (int i = 0; i < batch; ++i) { + if ((int)ciphers_hex_batch[i].size() != chunk_num) { + throw std::runtime_error("decrypt_chunks: each inner list must have length == chunk_num"); + } + } + + const int total = batch * chunk_num; + + // pack CT + std::vector h_CT(total); + for (int i = 0; i < batch; ++i) { + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + auto w = hex_to_le_words(ciphers_hex_batch[i][c], LIMBS); + std::memcpy(&h_CT[idx], w.data(), LIMBS * sizeof(uint32_t)); + } + } + + // keys + mem_t h_N{}, h_N2{}, h_PHI{}, h_INV{}; + std::memcpy(&h_N, hex_to_le_words(pk_n_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, hex_to_le_words(pk_n2_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_PHI, hex_to_le_words(sk_phi_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_INV, hex_to_le_words(sk_inv_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + + // device + error_report_t *d_report = nullptr; + mem_t *d_CT = nullptr, *d_N = nullptr, *d_N2 = nullptr, *d_PHI = nullptr, *d_INV = nullptr, + *d_M = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + CUDA_CHECK(cudaMalloc((void **)&d_CT, total * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_CT, h_CT.data(), total * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMalloc((void **)&d_N, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_PHI, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_INV, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_N, &h_N, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_N2, &h_N2, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_PHI, &h_PHI, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_INV, &h_INV, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMalloc((void **)&d_M, total * sizeof(mem_t))); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (total + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + decrypt_only_kernel<<>>(d_report, d_CT, total, d_N, d_N2, d_PHI, d_INV, d_M); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_M(total); + CUDA_CHECK(cudaMemcpy(h_M.data(), d_M, total * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_CT); + cudaFree(d_N); + cudaFree(d_N2); + cudaFree(d_PHI); + cudaFree(d_INV); + cudaFree(d_M); + cudaFree(d_report); + + std::vector> out(batch, std::vector(chunk_num)); + for (int i = 0; i < batch; ++i) { + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + std::vector w(LIMBS); + std::memcpy(w.data(), &h_M[idx], LIMBS * sizeof(uint32_t)); + out[i][c] = le_words_to_hex(w); + } + } + return out; + } +}; + +// ---------------------- Pybind module ---------------------- +PYBIND11_MODULE(paillier_GPU_client, m) { + m.attr("KEY_BITS") = py::int_(KEY_BITS); + py::class_(m, "PaillierGPUClient") + .def_static("max_key_len", &PaillierGPUClient::max_key_len) + .def(py::init(), + py::arg("embed_len") = 512, + py::arg("key_len") = KEY_BITS, + py::arg("skip_key_gen") = false) + .def("encrypt", &PaillierGPUClient::encrypt, + py::arg("embeddings")) + .def("encode_hamming_client", &PaillierGPUClient::encode_hamming_client, + py::arg("ct1"), + py::arg("ct2")) + .def_static("encode_hamming_server", &PaillierGPUClient::encode_hamming_server, + py::arg("ct1"), + py::arg("ct2"), + py::arg("pk")) + .def("decode_hamming_client", &PaillierGPUClient::decode_hamming_client, + py::arg("ciphers_hex_batch")) + .def("get_pk_hex", &PaillierGPUClient::get_pk_hex) + .def("get_keys_hex", &PaillierGPUClient::get_keys_hex) + .def("stringify_pk", &PaillierGPUClient::stringify_pk) + .def("stringify_sk", &PaillierGPUClient::stringify_sk) + .def("stringify_config", &PaillierGPUClient::stringify_config) + .def("load_stringified_keys", &PaillierGPUClient::load_stringified_keys) + .def("load_config", &PaillierGPUClient::load_config) + .def_property_readonly("embed_len", &PaillierGPUClient::embed_len) + .def_property_readonly("key_len", &PaillierGPUClient::key_len) + .def_property_readonly("chunk_len", &PaillierGPUClient::chunk_len) + .def_property_readonly("chunk_num", &PaillierGPUClient::chunk_num); +} diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_lookup_client.py b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_client.py index 4286527..73d476a 100644 --- a/src/xtrace_sdk/x_vec/crypto/paillier_lookup_client.py +++ b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_client.py @@ -1,121 +1,23 @@ import json -import os +from typing import Any import gmpy2 -from typing import Optional, Union - +from xtrace_sdk.x_vec.crypto.device import DeviceMode, resolve_device from xtrace_sdk.x_vec.crypto.encryption.paillier_lookup import PaillierLookup from xtrace_sdk.x_vec.crypto.hamming_client_base import HammingClientBase -from xtrace_sdk.x_vec.utils.xtrace_types import PaillierEncryptedNumber, PaillierLookupKeyPair - -DEVICE = os.getenv("DEVICE", "cpu") +from xtrace_sdk.x_vec.utils.xtrace_types import EncryptedVector, PaillierEncryptedNumber, PaillierLookupKeyPair - -class PaillierLookupGPU: - """Wrapper for the C++ GPU implementation of the Paillier-Lookup cryptography system. - - This class is not instantiated directly — use :class:`PaillierLookupClient` which dispatches - to this backend automatically when ``DEVICE=gpu``. - """ - - def __init__(self,embed_len:int=512, key_len:int=1024, alpha_len:int=50, skip_key_gen:bool=False) -> None: - try: - #lazy import here to avoid import error when GPU backend is not available - from .paillier_GPU_lookup_client import PaillierGPULookupClient - except Exception as e: - raise ImportError( - "GPU backend unavailable. Expected Paillier GPU .so under src/crypto/ " - "Please refer to the documentation for compilation instructions." - ) from e - self.gpu_client = PaillierGPULookupClient(embed_len=embed_len, key_len=key_len, alpha_len=alpha_len ,skip_key_gen=skip_key_gen) - - def encrypt(self, embds: list[list[int]]) -> list[PaillierEncryptedNumber]: - """Encrypt a batch of binary embedding vectors on GPU. - - :param embds: List of binary vectors with values in {0, 1}. - :type embds: list[list[int]] - :return: List of encrypted vectors. - :rtype: list[PaillierEncryptedNumber] - """ - # Fast path using raw bytes integration. Eliminates heavy python string operations. - bytes_batch = self.gpu_client.encrypt_bytes(embds) - res = [] - for bytes_list in bytes_batch: - res.append([int.from_bytes(b, byteorder='little') for b in bytes_list]) - return res - - def decode_hamming_client(self, cipher: list[list[int | bytes]]) -> list[int]: - """Decrypt a batch of encrypted Hamming distances on GPU. - - :param cipher: Batch of encrypted Hamming encodings from the server. - :type cipher: list[list[int]] - :return: List of plain-text Hamming distances. - :rtype: list[int] - """ - chunk_byte_len = (self.gpu_client.key_len * 4) // 8 - - bytes_batch = [] - for c in cipher: - # c is a list of chunks (either int or bytes) - record_bytes = [] - for h in c: - if isinstance(h, bytes): - # Ensure bytes are exactly chunk_byte_len. - # Server might send variable length if it's using compact integer representation. - if len(h) != chunk_byte_len: - h = int.from_bytes(h, byteorder='little').to_bytes(chunk_byte_len, byteorder='little') - record_bytes.append(h) - else: - record_bytes.append(int(h).to_bytes(chunk_byte_len, byteorder='little')) - bytes_batch.append(record_bytes) - - return self.gpu_client.decode_hamming_client_bytes(bytes_batch) - - def stringify_pk(self) -> str: - """Return a JSON string representation of the public key.""" - return self.gpu_client.stringify_pk() - - def stringify_sk(self) -> str: - """Return a JSON string representation of the secret key.""" - return self.gpu_client.stringify_sk() - - def stringify_config(self) -> str: - """Return a JSON string representation of the encryption configuration.""" - return self.gpu_client.stringify_config() - - def load_stringified_keys(self, pk: str, sk: str) -> None: - """Load public and secret keys from their JSON string representations. - - :param pk: JSON-encoded public key produced by :meth:`stringify_pk`. - :param sk: JSON-encoded secret key produced by :meth:`stringify_sk`. - """ - self.gpu_client.load_stringified_keys(pk, sk) - - def load_config(self, config: dict, precomputed_tables: dict | None = None, tables_bytes: dict | None = None) -> None: - """Load encryption configuration and optionally restore precomputed tables. - - :param config: Configuration dict (``embed_len``, ``key_len``, etc.). - :type config: dict - :param precomputed_tables: Optional dict of precomputed ``g_table`` / ``noise_table`` - to skip recomputation. - :param tables_bytes: Optional dict of the same tables in raw binary format (faster to load). - """ - if tables_bytes: - self.gpu_client.load_config_bytes(config, tables=tables_bytes) - elif precomputed_tables: - self.gpu_client.load_config(config, tables=precomputed_tables) - else: - self.gpu_client.load_config(config) - - def dump_tables(self) -> dict: - """Dump precomputed tables as a dict of Python-native types for caching.""" - return self.gpu_client.dump_tables() - - def dump_tables_bytes(self) -> dict: - """Dump precomputed tables in compact raw binary format for caching.""" - return self.gpu_client.dump_tables_bytes() +def _load_gpu_backend() -> type[Any]: + try: + from xtrace_sdk.x_vec.crypto.paillier_lookup_gpu_ext import PaillierGPULookupClient + except ImportError as e: + raise ImportError( + "GPU extension not built. Run `./build_gpu_binaries.sh` " + "(requires Docker, NVIDIA driver >= 550)." + ) from e + return PaillierGPULookupClient @@ -244,7 +146,15 @@ def load_config(self, config: dict, precomputed_tables: dict | None = None) -> N self.keys['g_table'] = PaillierLookup.precompute_g_table(g, n, key_len) self.keys['noise_table'] = PaillierLookup.precompute_noise_table(g, n) - self.keys['message_chunks'] = int(config['message_chunks']) + self.alpha_len = config["alpha_len"] + self.embed_len = config["embed_len"] + self.key_len = key_len + self.chunk_len = self.key_len * 2 + if self.embed_len > self.key_len: + self.chunk_num = 2 * self.embed_len // self.chunk_len + int((2 * self.embed_len) % self.chunk_len != 0) + else: + self.chunk_num = 1 + self.keys["message_chunks"] = int(config["message_chunks"]) def id2power(self,id_:int) -> tuple[int, int]: """Helper: return chunk index and corresponding power of 2 for a given position in the padded array. @@ -301,265 +211,173 @@ def decode_hamming_client(self, cipher: list[int | bytes]) -> int: class PaillierLookupClient(HammingClientBase): - """Paillier-Lookup homomorphic encryption client optimised for computing encrypted Hamming - distances between binary embedding vectors. + """Paillier-Lookup client that dispatches to CPU or GPU based on the ``device`` kwarg. - Uses precomputed lookup tables to accelerate encryption, making it significantly faster than - :class:`PaillierClient` for large collections. Dispatches to CPU or GPU backend via the - ``DEVICE`` environment variable (``cpu`` by default). + With ``device="auto"`` (the default), the GPU extension is probed at + construction time and used if available, otherwise the CPU implementation + runs. ``"cpu"`` and ``"gpu"`` force a backend; ``"gpu"`` raises if the + extension cannot be loaded. """ - def __init__(self,embed_len:int=512, key_len:int=1024, alpha_len:int=50, skip_key_gen:bool=False) -> None: - """constructor - - :param key_len: the length (in bit) of the public key and secret key generated for Paillier Cryptosystem, defaults to 1024 - :type key_len: int, optional - :param embed_len: the length of the embedding vector this paillier client supports, defaults to 512 - :type embed_len: int, optional - :param key_path: the path to the keys, defaults to None - :type key_path: str, optional - """ - # Store attributes directly for backward compatibility + def __init__( + self, + embed_len: int = 512, + key_len: int = 1024, + alpha_len: int = 50, + skip_key_gen: bool = False, + device: DeviceMode = "auto", + ) -> None: self.alpha_len = alpha_len self.key_len = key_len self.embed_len = embed_len self.chunk_len = key_len * 2 if embed_len > key_len: - self.chunk_num = 2*embed_len // self.chunk_len + int((2 *embed_len) % self.chunk_len != 0) + self.chunk_num = 2 * embed_len // self.chunk_len + int((2 * embed_len) % self.chunk_len != 0) else: self.chunk_num = 1 - - self.device = DEVICE - self.client: PaillierLookupGPU | PaillierLookupCPU - if self.device == "gpu": - if not self.has_gpu(): - raise RuntimeError("GPU requested via DEVICE=gpu, but GPU backend is unavailable.") - self.client = PaillierLookupGPU(embed_len=embed_len, key_len=key_len, alpha_len=alpha_len, skip_key_gen=skip_key_gen) + + self.device, gpu_cls = resolve_device(device, _load_gpu_backend) + if self.device == "gpu" and gpu_cls is not None: + self.client: Any = gpu_cls( + embed_len=embed_len, + key_len=key_len, + alpha_len=alpha_len, + skip_key_gen=skip_key_gen, + ) else: - self.client = PaillierLookupCPU(embed_len=embed_len, key_len=key_len, alpha_len=alpha_len, skip_key_gen=skip_key_gen) - + self.client = PaillierLookupCPU( + embed_len=embed_len, + key_len=key_len, + alpha_len=alpha_len, + skip_key_gen=skip_key_gen, + ) + @staticmethod def has_gpu() -> bool: - """Return ``True`` if the GPU backend extension is available and loadable. - - :rtype: bool - """ try: - from xtrace_sdk.x_vec.crypto.paillier_lookup_client import PaillierLookupGPU - PaillierLookupGPU(skip_key_gen=True) + _load_gpu_backend()(skip_key_gen=True) return True except Exception: return False - def encrypt_vec_one(self, embd: list[int]) -> PaillierEncryptedNumber: - """Encrypt a single binary embedding vector. + @staticmethod + def _cipher_to_int(value: Any) -> int: + if isinstance(value, bytes): + return int.from_bytes(value, byteorder="little") + return int(value) - :param embd: Binary vector of length ``embed_len`` with values in {0, 1}. - :type embd: list[int] - :return: Encrypted vector. - :rtype: PaillierEncryptedNumber - """ + def encrypt_vec_one(self, embd: list[int]) -> EncryptedVector: if isinstance(self.client, PaillierLookupCPU): return self.client.encrypt(embd) - else: - return self.client.encrypt([embd])[0] - - def encrypt_vec_batch(self, embds: list[list[int]]) -> list[PaillierEncryptedNumber]: - """Encrypt a batch of binary embedding vectors. + return [self._cipher_to_int(cipher) for cipher in self.client.encrypt([embd])[0]] - :param embds: List of binary vectors, each of length ``embed_len`` with values in {0, 1}. - :type embds: list[list[int]] - :return: List of encrypted vectors. - :rtype: list[PaillierEncryptedNumber] - """ + def encrypt_vec_batch(self, embds: list[list[int]]) -> list[EncryptedVector]: if isinstance(self.client, PaillierLookupCPU): return [self.client.encrypt(embd) for embd in embds] - else: - return self.client.encrypt(embds) + return [[self._cipher_to_int(cipher) for cipher in row] for row in self.client.encrypt(embds)] def decode_hamming_client_one(self, cipher: list[int | bytes]) -> int: - """Decrypt a single encrypted Hamming distance returned by the XTrace server. - - :param cipher: Encrypted Hamming encoding. - :type cipher: PaillierEncryptedNumber - :return: Plain-text Hamming distance. - :rtype: int - """ if isinstance(self.client, PaillierLookupCPU): return self.client.decode_hamming_client(cipher) - else: - return self.client.decode_hamming_client([cipher])[0] + normalized = [self._cipher_to_int(value) for value in cipher] + return int(self.client.decode_hamming_client([normalized])[0]) def decode_hamming_client_batch(self, ciphers: list[list[int | bytes]]) -> list[int]: - """Decrypt a batch of encrypted Hamming distances returned by the XTrace server. - - :param ciphers: List of encrypted Hamming encodings. - :type ciphers: list[PaillierEncryptedNumber] - :return: List of plain-text Hamming distances. - :rtype: list[int] - """ if isinstance(self.client, PaillierLookupCPU): return [self.client.decode_hamming_client(cipher) for cipher in ciphers] - else: - return self.client.decode_hamming_client(ciphers) + normalized = [[self._cipher_to_int(value) for value in cipher] for cipher in ciphers] + return [int(value) for value in self.client.decode_hamming_client(normalized)] def stringify_pk(self) -> str: - """Return a JSON string representation of the public key.""" return self.client.stringify_pk() def stringify_sk(self) -> str: - """Return a JSON string representation of the secret key.""" return self.client.stringify_sk() def stringify_config(self) -> str: - """Return a JSON string representation of the encryption configuration.""" return self.client.stringify_config() def load_stringified_keys(self, pk: str, sk: str) -> None: - """Load public and secret keys from their JSON string representations. - - :param pk: JSON-encoded public key produced by :meth:`stringify_pk`. - :param sk: JSON-encoded secret key produced by :meth:`stringify_sk`. - """ self.client.load_stringified_keys(pk, sk) def load_config(self, config: dict, precomputed_tables: dict | None = None) -> None: - """Load encryption configuration and optionally restore precomputed tables. - - :param config: Configuration dict (``embed_len``, ``key_len``, ``alpha_len``, etc.). - :type config: dict - :param precomputed_tables: Optional dict of precomputed ``g_table`` / ``noise_table`` - to skip recomputation on the CPU backend. - """ - if self.device == "cpu": - self.client.load_config(config, precomputed_tables=precomputed_tables) + sanitized_config = {k: v for k, v in config.items() if k != "device"} + if isinstance(self.client, PaillierLookupCPU): + self.client.load_config(sanitized_config, precomputed_tables=precomputed_tables) + elif precomputed_tables is None: + self.client.load_config(sanitized_config) else: - # GPU client might not support explicit table loading yet, fall back to standard - self.client.load_config(config) + self.client.load_config(sanitized_config, precomputed_tables) + self.embed_len = config["embed_len"] + self.key_len = config["key_len"] + self.alpha_len = config.get("alpha_len", self.alpha_len) + self.chunk_len = self.key_len * 2 + if self.embed_len > self.key_len: + self.chunk_num = 2 * self.embed_len // self.chunk_len + int((2 * self.embed_len) % self.chunk_len != 0) + else: + self.chunk_num = 1 + + def encode_hamming_server(self, ct1: list[int | bytes], ct2: list[int | bytes]) -> EncryptedVector: + if len(ct1) != len(ct2): + raise ValueError("ct1 and ct2 must have the same length") + if isinstance(self.client, PaillierLookupCPU): + if self.client.keys is None: + raise RuntimeError("Keys not initialized") + return [ + int(PaillierLookup.add(self._cipher_to_int(a), self._cipher_to_int(b), self.client.keys["pk"])) + for a, b in zip(ct1, ct2, strict=True) + ] + + result = type(self.client).encode_hamming_server( + [self._cipher_to_int(cipher) for cipher in ct1], + [self._cipher_to_int(cipher) for cipher in ct2], + json.loads(self.stringify_pk()), + ) + return [self._cipher_to_int(cipher) for cipher in result] + + def dump_tables(self) -> dict: + dump_fn = getattr(self.client, "dump_tables", None) + return dump_fn() if dump_fn is not None else {} def __getstate__(self) -> dict: - """Support for pickling. - We serialize the configuration, keys, and for CPU, the precomputed tables. - """ state = { - "device": self.device, "embed_len": self.embed_len, "key_len": self.key_len, "alpha_len": self.alpha_len, "pk": self.stringify_pk(), "sk": self.stringify_sk(), - "config": json.loads(self.stringify_config()) + "config": json.loads(self.stringify_config()), } - - # Always try to fetch and standardise tables into raw py::bytes representation. - # This provides a 60-80% reduction in serialization size compared to list of hex strings. - try: - if hasattr(self.client, "dump_tables_bytes"): - raw_bytes = self.client.dump_tables_bytes() - if raw_bytes: - state["tables_bytes"] = raw_bytes - else: - raw_tables = self.client.dump_tables() - if raw_tables: - if self.device == "cpu": - # CPU dump_tables returns: - # g_table: dict[int, list[int]], noise_table: list[int] - # Convert to flat lists of hex strings - flat_g = [] - # Ensure we iterate in order of the chunk indices - for chunk_idx in sorted(raw_tables.get("g_table", {}).keys()): - for val in raw_tables["g_table"][chunk_idx]: - flat_g.append(format(val, "x")) - - flat_noise = [format(val, "x") for val in raw_tables.get("noise_table", [])] - - state["tables"] = { - "g_table": flat_g, - "noise_table": flat_noise - } - elif self.device == "gpu": - # GPU dump_tables already returns flat lists of hex strings - state["tables"] = raw_tables - except Exception: - pass - + tables = self.dump_tables() + if tables: + state["tables"] = tables return state def __setstate__(self, state: dict) -> None: - """Restore from pickle""" - self.device = DEVICE self.embed_len = state["embed_len"] self.key_len = state["key_len"] self.alpha_len = state["alpha_len"] self.chunk_len = self.key_len * 2 - if self.embed_len > self.key_len: - self.chunk_num = 2*self.embed_len // self.chunk_len + int((2 *self.embed_len) % self.chunk_len != 0) + self.chunk_num = 2 * self.embed_len // self.chunk_len + int((2 * self.embed_len) % self.chunk_len != 0) else: self.chunk_num = 1 - - # Re-initialize the inner client - if self.device == "cpu": - self.client = PaillierLookupCPU( - embed_len=self.embed_len, - key_len=self.key_len, - alpha_len=self.alpha_len, - skip_key_gen=True + + self.device, gpu_cls = resolve_device("auto", _load_gpu_backend) + if self.device == "gpu" and gpu_cls is not None: + self.client = gpu_cls( + embed_len=self.embed_len, + key_len=self.key_len, + alpha_len=self.alpha_len, + skip_key_gen=True, ) - elif self.device == "gpu": - self.client = PaillierLookupGPU( - embed_len=self.embed_len, - key_len=self.key_len, - alpha_len=self.alpha_len, - skip_key_gen=True + else: + self.client = PaillierLookupCPU( + embed_len=self.embed_len, + key_len=self.key_len, + alpha_len=self.alpha_len, + skip_key_gen=True, ) - # Load keys self.client.load_stringified_keys(state["pk"], state["sk"]) - - # Load tables if available - if "tables_bytes" in state: - # Fast path: raw binary bytes support - tables_bytes = state["tables_bytes"] - # Check if device supports binary byte loading natively - if hasattr(self.client, "load_config") and "tables_bytes" in self.client.load_config.__code__.co_varnames and isinstance(self.client, PaillierLookupGPU): - self.client.load_config(state["config"], tables_bytes=tables_bytes) - else: - # It indicates a mismatch where the current client can only accept legacy tables but cache has bytes. - # Recompute for fallback. - self.client.load_config(state["config"]) - elif "tables" in state: - tables = state["tables"] - if self.device == "cpu": - # CPU load_config expects raw python int formats: - # g_table: dict[str/int, list[int]], noise_table: list[int] - # We need to un-flatten the hex strings back into this format - from xtrace_sdk.x_vec.crypto.encryption.paillier_lookup import MSG_BITS - msg_table_size = 2**MSG_BITS - - flat_g = tables.get("g_table", []) - g_dict = {} - # Recover dict structure by chunking the flat list - for i in range(0, len(flat_g), msg_table_size): - chunk_idx = i // msg_table_size - g_dict[str(chunk_idx)] = [int(hex_val, 16) for hex_val in flat_g[i : i + msg_table_size]] - - noise_list = [int(hex_val, 16) for hex_val in tables.get("noise_table", [])] - - cpu_tables = { - "g_table": g_dict, - "noise_table": noise_list - } - self.client.load_config(state["config"], precomputed_tables=cpu_tables) - else: - # GPU client natively expects the flat hex lists (legacy format) - self.client.load_config(state["config"], precomputed_tables=tables) - else: - self.client.load_config(state["config"]) - - def dump_tables(self) -> dict: - # Both CPU and GPU clients now support dump_tables - try: - return self.client.dump_tables() - except AttributeError: - return {} \ No newline at end of file + self.load_config(state["config"], precomputed_tables=state.get("tables")) diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/Makefile b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/Makefile new file mode 100644 index 0000000..629d9cf --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/Makefile @@ -0,0 +1,62 @@ +# GPU_paillier_client/Makefile +# +# Build a Python extension module paillier_GPU_lookup_client.* for different GPU archs and key sizes. +# +# Usage examples: +# make # defaults: KEY_BITS=1024, SMS="70 80 90", TPI=32 +# make KEY_BITS=2048 SMS="80 90" # compile for A100/H100 with 8192-bit n^2 +# make KEY_BITS=1024 TPI=32 SMS="86" # compile for RTX 30-series (sm_86) +# +# Directory layout: +# ./paillier_lookup_gpu_ext/paillier_GPU_lookup_client.cu +# build_gpu_binaries.sh materialises include/{pybind11,cgbn} and passes INCDIR + +# -------- Parameters you can tweak on the command line -------- +KEY_BITS ?= 1024 +TPI ?= 32 +ALPHA_LEN ?= 280 +# List of SM versions to build for (space separated) +SMS ?= 70 80 86 89 90 + +# Compiler/paths +NVCC ?= nvcc +CUDA_PATH?= /usr/local/cuda + +# Python extension suffix (e.g., .cpython-311-x86_64-linux-gnu.so) +PY_INC := $(shell python3 -c 'import sysconfig, sys; print(sysconfig.get_paths()["include"])') +EXT := $(shell python3 -c "import sysconfig;print(sysconfig.get_config_var('EXT_SUFFIX'))") +TARGET := paillier_GPU_lookup_client$(EXT) + +SRC := paillier_GPU_lookup_client.cu +INCDIR ?= ../_gpu_build_include +CXXSTD := -std=c++17 + +# Construct gencode flags for each SM +GENCODE := $(foreach sm,$(SMS),-gencode arch=compute_$(sm),code=sm_$(sm)) + +# Include paths +INCLUDES := -I$(INCDIR) -I$(INCDIR)/pybind11 -I$(INCDIR)/cgbn -I$(CUDA_PATH)/include -I$(PY_INC) + +# Build flags. -cudart static keeps libcudart embedded so the wheel doesn't +# require the user to have a matching libcudart.so installed at runtime. +NVCCFLAGS := $(CXXSTD) -O3 -Xcompiler -fPIC -shared \ + -cudart static \ + -DKEY_BITS=$(KEY_BITS) -DALPHA_LEN=$(ALPHA_LEN) -DCGBN_TPI=$(TPI) -DCGBN_USE_GMP \ + -Wno-deprecated-gpu-targets \ + $(GENCODE) $(INCLUDES) + +# Link GMP for the CPU fallback (header demands GMP present) +LDLIBS := -lgmp + +# Note: pybind11 is header-only; normally you don't need to link libpython explicitly. +# If your system requires it, uncomment the following: +# PY_LDFLAGS := $(shell python3-config --ldflags) +# LDFLAGS := $(PY_LDFLAGS) + +all: $(TARGET) + +$(TARGET): $(SRC) + $(NVCC) $(NVCCFLAGS) -o $@ $< $(LDLIBS) + +clean: + rm -f $(TARGET) diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/__init__.py b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/__init__.py new file mode 100644 index 0000000..4da46a3 --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/__init__.py @@ -0,0 +1,3 @@ +from .paillier_GPU_lookup_client import PaillierGPUClient, PaillierGPULookupClient + +__all__ = ["PaillierGPUClient", "PaillierGPULookupClient"] diff --git a/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/paillier_GPU_lookup_client.cu b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/paillier_GPU_lookup_client.cu new file mode 100644 index 0000000..8f1adbb --- /dev/null +++ b/src/xtrace_sdk/x_vec/crypto/paillier_lookup_gpu_ext/paillier_GPU_lookup_client.cu @@ -0,0 +1,2722 @@ +// production_GPU_paillier_client/paillier_GPU_client.cu +// +// This file implements a CUDA-accelerated Paillier client whose +// public API mirrors xtrace_sdk.crypto.paillier_client.PaillierClient: +// +// class PaillierGPUClient: +// def __init__(self, embed_len: int = 512, key_len: int = 1024, skip_key_gen: bool = False) +// def encrypt(self, embd: list[int]) -> list[int] +// def encode_hamming_client(self, ct1: list[int], ct2: list[int]) -> list[int] +// @staticmethod +// def encode_hamming_server(ct1: list[int], ct2: list[int], pk: dict) -> list[int] +// def decode_hamming_client(self, cipher: list[int]) -> int +// +// Keys are generated once in the constructor (unless skip_key_gen=True) +// using GMP big integers on the CPU, and stored inside the C++ class. +// GPU kernels use NVlabs CGBN for modular arithmetic on ciphertexts. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "pybind11/pybind11.h" +#include "pybind11/stl.h" + +#include +#include "cgbn/cgbn.h" + +namespace py = pybind11; + +// ---------------------- Compile-time configuration ---------------------- +#ifndef KEY_BITS +#define KEY_BITS 1024 +#endif + +#ifndef ALPHA_LEN +#define ALPHA_LEN 280 +#endif + +#ifndef CGBN_TPI +#define CGBN_TPI 32 +#endif + +// In the Python client, key_len is the prime bit-length. +// n is roughly 2*key_len bits, and we pad plaintext chunks to 2*key_len bits. +#define PAILLIER_N_SQUARED_BITS (KEY_BITS*4) +static constexpr int LIMBS = (PAILLIER_N_SQUARED_BITS + 31) / 32; + +// Lookup-table parameters (mirror Python paillier_lookup.py defaults) +#define PAILLIER_MSG_BITS 8 +static constexpr int PAILLIER_MSG_TABLE_SIZE = (1 << PAILLIER_MSG_BITS); + +// Precomputed noise table size and how many random entries to multiply +static constexpr int PAILLIER_NOISE_TABLE_SIZE = 1 << 8; // 2**8 +static constexpr int PAILLIER_NOISE_MULTIPLES = 14; + +// ---------------------- Error handling helpers ---------------------- +#define CUDA_CHECK(call) do { \ + cudaError_t _e = (call); \ + if (_e != cudaSuccess) { \ + std::ostringstream _oss; \ + _oss << "CUDA error " << cudaGetErrorName(_e) \ + << " (" << cudaGetErrorString(_e) << ") at " << __FILE__ \ + << ":" << __LINE__; \ + throw std::runtime_error(_oss.str()); \ + } \ +} while(0) + +// ---------------------- Minimal GMP big-int wrapper ---------------------- +namespace bigint { + +class big_int_t { +public: + mpz_t v; + + big_int_t() { mpz_init(v); } + explicit big_int_t(unsigned long long x) { mpz_init(v); mpz_set_ui(v, static_cast(x)); } + explicit big_int_t(const std::string &s, int base = 10) { + mpz_init(v); + if (mpz_set_str(v, s.c_str(), base) != 0) { + throw std::invalid_argument("big_int_t: invalid string"); + } + } + big_int_t(const big_int_t &o) { mpz_init_set(v, o.v); } + big_int_t(big_int_t &&o) noexcept { mpz_init(v); mpz_swap(v, o.v); } + ~big_int_t() { mpz_clear(v); } + + big_int_t &operator=(const big_int_t &o) { + if (this != &o) mpz_set(v, o.v); + return *this; + } + big_int_t &operator=(big_int_t &&o) noexcept { + if (this != &o) mpz_swap(v, o.v); + return *this; + } + + std::string to_string(int base = 10) const { + char *raw = mpz_get_str(nullptr, base, v); + if (!raw) return {}; + std::string s(raw); + void (*freefunc)(void*, size_t) = nullptr; + mp_get_memory_functions(nullptr, nullptr, &freefunc); + if (freefunc) freefunc(raw, std::strlen(raw) + 1); + return s; + } +}; + +inline bigint::big_int_t powm(const bigint::big_int_t &base, + const bigint::big_int_t &exp, + const bigint::big_int_t &mod) { + bigint::big_int_t r; + mpz_powm(r.v, base.v, exp.v, mod.v); + return r; +} + +inline bigint::big_int_t gcd(const bigint::big_int_t &a, const bigint::big_int_t &b) { + bigint::big_int_t r; + mpz_gcd(r.v, a.v, b.v); + return r; +} + +inline void fill_os_random(void *data, std::size_t size) { + std::ifstream rng("/dev/urandom", std::ios::in | std::ios::binary); + if (!rng) { + throw std::runtime_error("secure randomness: failed to open /dev/urandom"); + } + const auto requested = static_cast(size); + rng.read(static_cast(data), requested); + if (rng.gcount() != requested) { + throw std::runtime_error("secure randomness: failed to read enough bytes from /dev/urandom"); + } +} + +inline bigint::big_int_t random_bits(unsigned bits) { + if (bits == 0) throw std::invalid_argument("bits must be > 0"); + const std::size_t byte_count = (static_cast(bits) + 7) / 8; + std::vector bytes(byte_count); + fill_os_random(bytes.data(), bytes.size()); + + const unsigned excess_bits = static_cast(byte_count * 8 - bits); + if (excess_bits > 0) { + bytes[0] &= static_cast(0xFFu >> excess_bits); + } + + bigint::big_int_t out; + mpz_import(out.v, bytes.size(), 1, 1, 0, 0, bytes.data()); + return out; +} + +inline bigint::big_int_t random_below(const bigint::big_int_t &upper) { + if (mpz_sgn(upper.v) <= 0) { + throw std::invalid_argument("random_below: upper bound must be positive"); + } + const unsigned bits = static_cast(mpz_sizeinbase(upper.v, 2)); + bigint::big_int_t value; + do { + value = random_bits(bits); + } while (mpz_cmp(value.v, upper.v) >= 0); + return value; +} + +inline bigint::big_int_t random_prime_bits(unsigned bits) { + if (bits < 2) throw std::invalid_argument("bits must be >= 2"); + bigint::big_int_t x = random_bits(bits); + mpz_setbit(x.v, bits - 1); + if (mpz_even_p(x.v)) mpz_add_ui(x.v, x.v, 1); + bigint::big_int_t p; + mpz_nextprime(p.v, x.v); + while (static_cast(mpz_sizeinbase(p.v, 2)) != bits) { + x = random_bits(bits); + mpz_setbit(x.v, bits - 1); + if (mpz_even_p(x.v)) mpz_add_ui(x.v, x.v, 1); + mpz_nextprime(p.v, x.v); + } + return p; +} + +} // namespace bigint + +// ---------------------- Lookup-style helpers (CPU, GMP) ---------------------- +// Bit length of a big_int_t in base 2. +static inline unsigned bitlen_mpz(const bigint::big_int_t &x) { + if (mpz_sgn(x.v) <= 0) return 0; + return static_cast(mpz_sizeinbase(x.v, 2)); +} + +// L(x) = (x-1)/n, assuming x ≡ 1 (mod n) +static inline void L_div(bigint::big_int_t &out, + const bigint::big_int_t &x, + const bigint::big_int_t &n) { + mpz_sub_ui(out.v, x.v, 1); + mpz_tdiv_q(out.v, out.v, n.v); +} + +// CRT: find r such that r ≡ a1 (mod m1), r ≡ a2 (mod m2). +static void crt_pair(const bigint::big_int_t &a1, const bigint::big_int_t &m1, + const bigint::big_int_t &a2, const bigint::big_int_t &m2, + bigint::big_int_t &r_out, bigint::big_int_t &M_out) { + using namespace bigint; + mpz_mul(M_out.v, m1.v, m2.v); + + big_int_t m1_mod_m2; + mpz_mod(m1_mod_m2.v, m1.v, m2.v); + + big_int_t inv; + if (!mpz_invert(inv.v, m1_mod_m2.v, m2.v)) { + throw std::runtime_error("crt_pair: inverse does not exist"); + } + + big_int_t t, diff; + mpz_sub(diff.v, a2.v, a1.v); + mpz_mod(diff.v, diff.v, m2.v); + mpz_mul(t.v, diff.v, inv.v); + mpz_mod(t.v, t.v, m2.v); + + big_int_t tmp; + mpz_mul(tmp.v, m1.v, t.v); + mpz_add(r_out.v, a1.v, tmp.v); + mpz_mod(r_out.v, r_out.v, M_out.v); +} + +// Multiply by (1+p) to inject an order-p component in Z_{p^2}*. +static bigint::big_int_t lift_to_p2_with_p_component(const bigint::big_int_t &g_mod_p, + const bigint::big_int_t &p) { + using namespace bigint; + big_int_t p2, one_plus_p, r; + mpz_mul(p2.v, p.v, p.v); + mpz_add_ui(one_plus_p.v, p.v, 1); + mpz_mul(r.v, g_mod_p.v, one_plus_p.v); + mpz_mod(r.v, r.v, p2.v); + return r; +} + +// gen_dsa_params_custom: generate (p,q,g) with p_bits for p and q_bits for q, and g of order q mod p. +static void gen_dsa_params_custom(unsigned p_bits, unsigned q_bits, + bigint::big_int_t &p_out, + bigint::big_int_t &q_out, + bigint::big_int_t &g_out) { + using namespace bigint; + if (q_bits >= p_bits) { + throw std::invalid_argument("alpha_len too large: q_bits must be < p_bits"); + } + + // q: exact q_bits prime + big_int_t q = random_prime_bits(q_bits); + + // choose k so that p = k*q + 1 has exactly p_bits bits + big_int_t two_pow_p_1, two_pow_p; + mpz_set_ui(two_pow_p_1.v, 1); + mpz_mul_2exp(two_pow_p_1.v, two_pow_p_1.v, p_bits - 1); + + mpz_set_ui(two_pow_p.v, 1); + mpz_mul_2exp(two_pow_p.v, two_pow_p.v, p_bits); + + big_int_t k_lo, k_hi, tmp; + mpz_tdiv_q(k_lo.v, two_pow_p_1.v, q.v); + + mpz_sub_ui(tmp.v, two_pow_p.v, 1); + mpz_tdiv_q(k_hi.v, tmp.v, q.v); + + if (mpz_odd_p(k_lo.v)) { + mpz_add_ui(k_lo.v, k_lo.v, 1); + } + + big_int_t p, span, t, k; + while (true) { + // span = (k_hi - k_lo) / 2 + 1 + mpz_sub(span.v, k_hi.v, k_lo.v); + mpz_tdiv_q_2exp(span.v, span.v, 1); + mpz_add_ui(span.v, span.v, 1); + + t = random_below(span); // t in [0, span-1] + mpz_mul_2exp(k.v, t.v, 1); // 2*t + mpz_add(k.v, k.v, k_lo.v); // k = k_lo + 2*t + + mpz_mul(p.v, k.v, q.v); + mpz_add_ui(p.v, p.v, 1); + + if (bitlen_mpz(p) != p_bits) { + continue; + } + if (mpz_probab_prime_p(p.v, 40) > 0) { + break; + } + } + + // generator of order q + big_int_t e; + mpz_sub_ui(e.v, p.v, 1); + mpz_tdiv_q(e.v, e.v, q.v); + + big_int_t h, g; + big_int_t p_minus2; + mpz_sub_ui(p_minus2.v, p.v, 2); + + while (true) { + // pick h in [2, p-1] + h = random_below(p_minus2); // 0..p-3 + mpz_add_ui(h.v, h.v, 2); // 2..p-1 + mpz_powm(g.v, h.v, e.v, p.v); + if (mpz_cmp_ui(g.v, 1) != 0) { + break; + } + } + + p_out = p; + q_out = q; + g_out = g; +} + +// ---------------------- CPU Paillier key structures ---------------------- +struct PaillierPublicKeyCPU { + bigint::big_int_t g; + bigint::big_int_t n; + bigint::big_int_t n_squared; +}; + +struct PaillierSecretKeyCPU { + bigint::big_int_t phi; + bigint::big_int_t inv; + // Lookup-optimized decryption exponent and inverse factor + bigint::big_int_t a; + bigint::big_int_t g_a_inv; +}; + +struct PaillierKeyPairCPU { + PaillierPublicKeyCPU pk; + PaillierSecretKeyCPU sk; +}; + +class PaillierCPU { +public: + static PaillierKeyPairCPU key_gen(unsigned prime_bits, unsigned alpha_bits) { + using namespace bigint; + while (true) { + // --- pick q-bit split close to requested alpha_len --- + unsigned q_bits_bound = alpha_bits; + unsigned q_bits_1 = q_bits_bound / 2; + unsigned q_bits_2 = q_bits_bound - q_bits_1; + + big_int_t p1, q1, g1; + big_int_t p2, q2, g2; + gen_dsa_params_custom(prime_bits, q_bits_1, p1, q1, g1); + gen_dsa_params_custom(prime_bits, q_bits_2, p2, q2, g2); + + big_int_t p = p1; + big_int_t q = p2; + + big_int_t n; + mpz_mul(n.v, p.v, q.v); + + big_int_t n2; + mpz_mul(n2.v, n.v, n.v); + + // Lift g1, g2 to p^2, q^2 so their orders gain factors p and q + big_int_t g1_p2 = lift_to_p2_with_p_component(g1, p); + big_int_t g2_q2 = lift_to_p2_with_p_component(g2, q); + + big_int_t p2_mod, q2_mod; + mpz_mul(p2_mod.v, p.v, p.v); + mpz_mul(q2_mod.v, q.v, q.v); + + big_int_t g, M_dummy; + crt_pair(g1_p2, p2_mod, g2_q2, q2_mod, g, M_dummy); + + // alpha = lcm(q1, q2) + big_int_t a; + mpz_lcm(a.v, q1.v, q2.v); + + // (sanity) ensure ord(g) | n*a and n-part is present + big_int_t n_mul_a; + mpz_mul(n_mul_a.v, n.v, a.v); + + big_int_t check1, check2; + mpz_powm(check1.v, g.v, n_mul_a.v, n2.v); + mpz_powm(check2.v, g.v, n.v, n2.v); + + if (mpz_cmp_ui(check1.v, 1) != 0 || mpz_cmp_ui(check2.v, 1) == 0) { + // extremely rare; try again + continue; + } + + big_int_t p_minus_1, q_minus_1; + mpz_sub_ui(p_minus_1.v, p.v, 1); + mpz_sub_ui(q_minus_1.v, q.v, 1); + + big_int_t phi; + mpz_mul(phi.v, p_minus_1.v, q_minus_1.v); + + big_int_t g_n; + mpz_powm(g_n.v, g.v, n.v, n2.v); + + big_int_t g_pow_a; + mpz_powm(g_pow_a.v, g.v, a.v, n2.v); + + big_int_t L_ga; + L_div(L_ga, g_pow_a, n); + + big_int_t g_a_inv; + if (!mpz_invert(g_a_inv.v, L_ga.v, n.v)) { + // extremely unlikely; regenerate keys + continue; + } + + big_int_t inv; + if (!mpz_invert(inv.v, phi.v, n.v)) { + // extremely unlikely; regenerate keys + continue; + } + + PaillierPublicKeyCPU pk{g, n, n2}; + PaillierSecretKeyCPU sk{phi, inv, a, g_a_inv}; + return PaillierKeyPairCPU{pk, sk}; + } + } +}; + +// ---------------------- Hex <-> limb conversion ---------------------- +static inline uint32_t hex_nibble(char c) { + if (c >= '0' && c <= '9') return (uint32_t)(c - '0'); + if (c >= 'a' && c <= 'f') return (uint32_t)(10 + c - 'a'); + if (c >= 'A' && c <= 'F') return (uint32_t)(10 + c - 'A'); + throw std::runtime_error("Invalid hex digit"); +} + +static std::string normalize_hex(const std::string &s) { + std::string h = s; + if (h.size() >= 2 && h[0] == '0' && (h[1] == 'x' || h[1] == 'X')) h = h.substr(2); + size_t p = 0; + while (p < h.size() && h[p] == '0') ++p; + if (p == h.size()) return "0"; + return h.substr(p); +} + +static std::vector hex_to_le_words(const std::string &hex_in, int words) { + std::string h = normalize_hex(hex_in); + if (h == "0") return std::vector(words, 0u); + if (h.size() & 1) h = "0" + h; + std::vector bytes(h.size() / 2); + for (size_t i = 0, j = 0; i < h.size(); i += 2, ++j) { + uint8_t hi = (uint8_t)hex_nibble(h[i]); + uint8_t lo = (uint8_t)hex_nibble(h[i + 1]); + bytes[j] = (uint8_t)((hi << 4) | lo); + } + std::vector words_le(words, 0u); + const size_t num_bytes = bytes.size(); + for (size_t i = 0; i < num_bytes; ++i) { + size_t limb_index = i / 4; + size_t byte_index = i % 4; + if (limb_index >= (size_t)words) break; + words_le[limb_index] |= ((uint32_t)bytes[num_bytes - 1 - i]) << (8 * byte_index); + } + return words_le; +} + +static std::string le_words_to_hex(const std::vector &words) { + std::vector bytes; + bytes.reserve(words.size() * 4); + for (uint32_t w : words) { + bytes.push_back((uint8_t)(w & 0xFF)); + bytes.push_back((uint8_t)((w >> 8) & 0xFF)); + bytes.push_back((uint8_t)((w >> 16) & 0xFF)); + bytes.push_back((uint8_t)((w >> 24) & 0xFF)); + } + while (!bytes.empty() && bytes.back() == 0) bytes.pop_back(); + if (bytes.empty()) return "0"; + static const char *HEX = "0123456789abcdef"; + std::string hex; + hex.resize(bytes.size() * 2); + size_t idx = 0; + for (int i = (int)bytes.size() - 1; i >= 0; --i) { + uint8_t b = bytes[i]; + hex[idx++] = HEX[(b >> 4) & 0xF]; + hex[idx++] = HEX[b & 0xF]; + } + size_t p = 0; + while (p + 1 < hex.size() && hex[p] == '0') ++p; + return hex.substr(p); +} + +// hex -> binary string (MSB-first), no fixed width; "0" for zero +static inline std::string hex_to_bin_nopad(const std::string &hex) { + auto hn = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); + if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); + return 0; + }; + static const char *LUT[16] = { + "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", + "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; + std::string out; + out.reserve(hex.size() * 4); + for (char c : hex) out.append(LUT[hn(c)]); + size_t p = out.find_first_not_of('0'); + if (p == std::string::npos) return "0"; + return out.substr(p); +} + +// ---------------------- CGBN types ---------------------- +typedef cgbn_context_t context_t; +typedef cgbn_env_t env_t; +typedef cgbn_error_report_t error_report_t; +typedef cgbn_mem_t mem_t; + +// Small set of global constants for decryption, shared across all threads +__device__ __constant__ mem_t CONST_N; +__device__ __constant__ mem_t CONST_N2; +__device__ __constant__ mem_t CONST_A; +__device__ __constant__ mem_t CONST_G_A_INV; + +// Convert a GMP big integer to our fixed-size mem_t (little-endian limbs) +static inline mem_t bigint_to_mem(const bigint::big_int_t &x) { + std::array words{}; + size_t count = 0; + mpz_export(words.data(), &count, -1, sizeof(uint32_t), 0, 0, x.v); + if (count > LIMBS) { + throw std::runtime_error("bigint_to_mem: value does not fit in mem_t"); + } + mem_t out{}; + std::memcpy(&out, words.data(), LIMBS * sizeof(uint32_t)); + return out; +} + +// Convert mem_t (little-endian limbs) to a GMP big integer +static inline bigint::big_int_t mem_to_bigint(const mem_t &mem) { + bigint::big_int_t out; + std::array words{}; + std::memcpy(words.data(), &mem, LIMBS * sizeof(uint32_t)); + mpz_import(out.v, LIMBS, -1, sizeof(uint32_t), 0, 0, words.data()); + return out; +} + +// ---------------------- Lookup-table precomputation (CPU) ---------------------- +// Build g_table[i][j] = g^(j * 2^(MSG_BITS * i)) mod n^2, flattened as +// [message_chunks * PAILLIER_MSG_TABLE_SIZE] of mem_t in little-endian limbs. +static std::vector +precompute_g_table(const PaillierKeyPairCPU &keys, int key_len_bits) { + using namespace bigint; + + const big_int_t &g = keys.pk.g; + const big_int_t &n = keys.pk.n; + const big_int_t &n2 = keys.pk.n_squared; + + const int msg_bits = PAILLIER_MSG_BITS; + const int message_chunks = (key_len_bits * 2 + msg_bits - 1) / msg_bits; + const int table_size = PAILLIER_MSG_TABLE_SIZE; + + std::vector table(static_cast(message_chunks) * + static_cast(table_size)); + + // For each chunk i, base = g^(2^(msg_bits * i)) mod n^2, then table[i][j] = base^j. + for (int i = 0; i < message_chunks; ++i) { + big_int_t exp; + mpz_set_ui(exp.v, 1); + mpz_mul_2exp(exp.v, exp.v, msg_bits * i); + + big_int_t base; + mpz_powm(base.v, g.v, exp.v, n2.v); + + big_int_t cur; + mpz_set_ui(cur.v, 1); // base^0 + + const size_t row_offset = static_cast(i) * static_cast(table_size); + for (int j = 0; j < table_size; ++j) { + table[row_offset + static_cast(j)] = bigint_to_mem(cur); + if (j + 1 < table_size) { + mpz_mul(cur.v, cur.v, base.v); + mpz_mod(cur.v, cur.v, n2.v); + } + } + } + + return table; +} + +// Build noise_table[k] = g_n^r mod n^2 for random r in [1, n-1] with gcd(r,n)=1, +// as mem_t entries. This mirrors Paillier_Lookup.precompute_noise_table. +static std::vector +precompute_noise_table(const PaillierKeyPairCPU &keys) { + using namespace bigint; + + const big_int_t &g = keys.pk.g; + const big_int_t &n = keys.pk.n; + const big_int_t &n2 = keys.pk.n_squared; + + big_int_t g_n; + mpz_powm(g_n.v, g.v, n.v, n2.v); + + std::vector table(PAILLIER_NOISE_TABLE_SIZE); + + for (int k = 0; k < PAILLIER_NOISE_TABLE_SIZE; ++k) { + big_int_t r; + // Sample r uniformly in [1, n-1] with gcd(r, n) = 1. + while (true) { + r = random_below(n); // 0 <= r < n + if (mpz_cmp_ui(r.v, 0) == 0) { + continue; + } + big_int_t g_r; + mpz_gcd(g_r.v, r.v, n.v); + if (mpz_cmp_ui(g_r.v, 1) == 0) { + break; + } + } + + big_int_t noise; + mpz_powm(noise.v, g_n.v, r.v, n2.v); + table[static_cast(k)] = bigint_to_mem(noise); + } + + return table; +} + +// ---------------------- Host-side g_table cache ---------------------- +struct GTableCacheEntry { + std::shared_ptr> table; + std::list::iterator lru_it; +}; + +static std::mutex g_table_cache_mutex; +static std::unordered_map g_table_cache; +static std::list g_table_lru; +static constexpr size_t G_TABLE_CACHE_MAX = 4; + +static std::shared_ptr> +get_cached_g_table(const std::string &cache_key, const PaillierKeyPairCPU &keys, int key_len_bits) { + { + std::lock_guard lock(g_table_cache_mutex); + auto it = g_table_cache.find(cache_key); + if (it != g_table_cache.end()) { + g_table_lru.erase(it->second.lru_it); + g_table_lru.push_back(cache_key); + it->second.lru_it = std::prev(g_table_lru.end()); + return it->second.table; + } + } + + auto computed = std::make_shared>(precompute_g_table(keys, key_len_bits)); + + std::lock_guard lock(g_table_cache_mutex); + auto it = g_table_cache.find(cache_key); + if (it != g_table_cache.end()) { + return it->second.table; + } + g_table_lru.push_back(cache_key); + auto lru_it = std::prev(g_table_lru.end()); + g_table_cache.emplace(cache_key, GTableCacheEntry{computed, lru_it}); + if (g_table_cache.size() > G_TABLE_CACHE_MAX) { + const std::string &old_key = g_table_lru.front(); + g_table_cache.erase(old_key); + g_table_lru.pop_front(); + } + return computed; +} + + + +// ------------------ Arithmetic helpers ------------------ + +// Extract pointer to 32-bit limbs from mem_t (little-endian) +static __device__ __forceinline__ const uint32_t* limbs_of(const mem_t *m) { + return reinterpret_cast(m); +} + +// Find MSB index (bit position) of exponent in mem_t (return -1 if zero) +static __device__ __forceinline__ int msb_index_mem(const mem_t *exp_mem) { + const uint32_t *w = limbs_of(exp_mem); + for (int i=LIMBS-1; i>=0; --i) { + uint32_t v = w[i]; + if (v != 0) { + // position of highest set bit within word + int lz = __clz(v); // 0..32 + int bit = 31 - lz; // 0..31 + return i*32 + bit; + } + } + return -1; +} + +// Square-and-multiply: result = base^exp_mem (mod mod_n2) +// exp_mem is provided as mem_t (n's limbs) +static __device__ void pow_mod_mem(env_t &env, + typename env_t::cgbn_t &result, + const typename env_t::cgbn_t &base, + const mem_t *exp_mem, + const typename env_t::cgbn_t &mod_n2) { + typename env_t::cgbn_t res, b; + typename env_t::cgbn_wide_t wide; + + cgbn_set_ui32(env, res, 1); + cgbn_set(env, b, base); + + const uint32_t *EW = reinterpret_cast(exp_mem); + + // Iterate all LIMBS*32 bits, LSB to MSB + for (int w = 0; w < LIMBS; ++w) { + uint32_t x = EW[w]; + for (int k = 0; k < 32; ++k) { + if (x & 1u) { + cgbn_mul_wide(env, wide, res, b); + cgbn_rem_wide(env, res, wide, mod_n2); + } + // b = b*b mod mod_n2 + cgbn_mul_wide(env, wide, b, b); + cgbn_rem_wide(env, b, wide, mod_n2); + x >>= 1; + } + } + cgbn_set(env, result, res); +} + +// Square-and-multiply specialized for a small exponent with dynamic bit length. +// Iterates only over exp_bits bits of exp_mem instead of the full LIMBS*32. +static __device__ void pow_mod_small_exp(env_t &env, + typename env_t::cgbn_t &result, + const typename env_t::cgbn_t &base, + const mem_t *exp_mem, + const typename env_t::cgbn_t &mod_n2, + int exp_bits) { + typename env_t::cgbn_t res, b; + typename env_t::cgbn_wide_t wide; + + cgbn_set_ui32(env, res, 1); + cgbn_set(env, b, base); + + const uint32_t *EW = reinterpret_cast(exp_mem); + + const int total_bits = exp_bits; + const int full_words = total_bits / 32; + const int rem_bits = total_bits % 32; + + // Process full 32-bit words + for (int w = 0; w < full_words; ++w) { + uint32_t x = EW[w]; + for (int k = 0; k < 32; ++k) { + if (x & 1u) { + cgbn_mul_wide(env, wide, res, b); + cgbn_rem_wide(env, res, wide, mod_n2); + } + cgbn_mul_wide(env, wide, b, b); + cgbn_rem_wide(env, b, wide, mod_n2); + x >>= 1; + } + } + + // Process remaining bits in the last word, if any + if (rem_bits > 0) { + uint32_t x = EW[full_words]; + for (int k = 0; k < rem_bits; ++k) { + if (x & 1u) { + cgbn_mul_wide(env, wide, res, b); + cgbn_rem_wide(env, res, wide, mod_n2); + } + cgbn_mul_wide(env, wide, b, b); + cgbn_rem_wide(env, b, wide, mod_n2); + x >>= 1; + } + } + + cgbn_set(env, result, res); +} + +// Euclidean GCD (returns gcd(a,b) in 'a'); destructive to inputs +static __device__ void gcd_big(env_t &env, + typename env_t::cgbn_t &a, + typename env_t::cgbn_t &b) { + typename env_t::cgbn_t t; + while (cgbn_compare_ui32(env, b, 0) != 0) { + cgbn_rem(env, t, a, b); + cgbn_set(env, a, b); + cgbn_set(env, b, t); + } +} + +// Build a mask that selects bits at MSB-first positions 1,3,5,... within width=chunk_len +// mapped to little-endian limb storage used by cgbn_mem_t. +static inline mem_t make_odd_mask_host(const int chunk_len) { + mem_t mask{}; + uint32_t *w = reinterpret_cast(&mask); + // zero init + for (int i=0;i> 5; + const int pos = lsb_index & 31; + w[idx] |= (1u << pos); + } + return mask; +} + + + + +// ---------------------- CUDA kernel ---------------------- +__global__ void modmul_pointwise_kernel(error_report_t *report, + mem_t *A, + mem_t *B, + mem_t *MOD, // single modulus at index 0 + mem_t *R, + int count) { + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= count) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + typename env_t::cgbn_t a, b, m, r; + typename env_t::cgbn_wide_t wide; // <<< wide 2*BITS + cgbn_load(env, a, &A[instance]); + cgbn_load(env, b, &B[instance]); + cgbn_load(env, m, &MOD[0]); + + // Use CGBN modular multiply: r = (a*b) mod m + cgbn_mul_wide(env, wide, a, b); // wide = a*b + cgbn_rem_wide(env, r, wide, m); // r = wide % m + + cgbn_store(env, &R[instance], r); +} + + +// Each instance handles one (vector, chunk) +__global__ void encrypt_kernel(error_report_t *report, + const uint8_t *all_bits, // [batch * embed_len], 0/1 + int batch, + int embed_len, + int chunk_len, + int chunk_num, + mem_t *N, // n + mem_t *N2, // n^2 + mem_t *G_TABLE, // [message_chunks * table_size] + mem_t *NOISE_TABLE, // [noise_table_size] + int message_chunks, + int noise_table_size, + const uint32_t *random_words, + mem_t *out_ct) { // [batch * chunk_num] + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + int total = batch * chunk_num; + if (instance >= total) return; + + int vec_idx = instance / chunk_num; + int chunk_idx = instance % chunk_num; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load n and n^2 into bigints + typename env_t::cgbn_t n, n2; + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + + // --- Build per-chunk base-2^MSG_BITS digits from padded bits --- + // Padded scheme: for original bit x[j], padded has at positions: 2*j -> 0, 2*j+1 -> x[j]. + const int padded_total = 2 * embed_len; // total padded bits for this vector + const int chunk_base = chunk_idx * chunk_len; // start bit in padded stream + const uint8_t *vec_bits = all_bits + vec_idx * embed_len; + + // Only consume the bits that actually exist in this chunk, do NOT extend to chunk_len + int avail = 0; + if (chunk_base < padded_total) { + int max_take = padded_total - chunk_base; + avail = (max_take > chunk_len) ? chunk_len : max_take; + } + + // Split the (at most avail)-bit chunk into MSG_BITS-wide digits, matching + // Python's behavior: for the local substring s = padded_embd[chunk_base:chunk_base+avail], + // interpret m_local = int(s, 2), then m_split[i] = (m_local >> (MSG_BITS*i)) & ((1< carries original bit + int j = p >> 1; // source index in the embedding + bit = (uint32_t)(vec_bits[j] & 1); + } + if (!bit) continue; + + // position within this chunk's substring, MSB-first + int local_e = (local_len - 1) - t; // exponent in 2^local_e + int digit_idx = local_e / msg_bits; + int offset = local_e % msg_bits; + if (digit_idx >= 0 && digit_idx < message_chunks) { + m_split_local[digit_idx] |= (uint8_t)(1u << offset); + } + } + + // --- Compute g^m via lookup table: g_m = prod_i g_table[i][m_split_local[i]] mod n^2 --- + typename env_t::cgbn_t g_m; + typename env_t::cgbn_wide_t wide; + cgbn_set_ui32(env, g_m, 1); + + for (int i = 0; i < message_chunks; ++i) { + uint8_t d = m_split_local[i]; + if (d == 0) continue; // g_table[i][0] == 1 + int idx = i * table_size + (int)d; + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &G_TABLE[idx]); + cgbn_mul_wide(env, wide, g_m, entry); + cgbn_rem_wide(env, g_m, wide, n2); + } + + // --- Sample noise using precomputed NOISE_TABLE entries --- + typename env_t::cgbn_t noise; + cgbn_set_ui32(env, noise, 1); + + for (int k = 0; k < PAILLIER_NOISE_MULTIPLES; ++k) { + uint32_t r32 = random_words[instance * PAILLIER_NOISE_MULTIPLES + k]; + int idx = (int)(r32 % (uint32_t)noise_table_size); + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &NOISE_TABLE[idx]); + cgbn_mul_wide(env, wide, noise, entry); + cgbn_rem_wide(env, noise, wide, n2); + } + + // --- c = g_m * noise mod n^2 --- + typename env_t::cgbn_t c; + cgbn_mul_wide(env, wide, g_m, noise); + cgbn_rem_wide(env, c, wide, n2); + + // Store result + cgbn_store(env, &out_ct[instance], c); +} + +// Each instance handles one plaintext integer (already split into MSG_BITS digits) +__global__ void encrypt_cts_kernel(error_report_t *report, + const uint8_t *all_digits, // [batch * message_chunks] + int batch, + int message_chunks, + mem_t *N, // n + mem_t *N2, // n^2 + mem_t *G_TABLE, // [message_chunks * table_size] + mem_t *NOISE_TABLE, // [noise_table_size] + int noise_table_size, + const uint32_t *random_words, + mem_t *out_ct) { // [batch] + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= batch) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load n and n^2 into bigints + typename env_t::cgbn_t n, n2; + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + + const int table_size = PAILLIER_MSG_TABLE_SIZE; + + // --- Compute g^m via lookup table: g_m = prod_i g_table[i][digit_i] mod n^2 --- + typename env_t::cgbn_t g_m; + typename env_t::cgbn_wide_t wide; + cgbn_set_ui32(env, g_m, 1); + + const uint8_t *digits = all_digits + instance * message_chunks; + for (int i = 0; i < message_chunks; ++i) { + uint8_t d = digits[i]; + if (d == 0) continue; // g_table[i][0] == 1 + int idx = i * table_size + (int)d; + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &G_TABLE[idx]); + cgbn_mul_wide(env, wide, g_m, entry); + cgbn_rem_wide(env, g_m, wide, n2); + } + + // --- Sample noise using precomputed NOISE_TABLE entries --- + typename env_t::cgbn_t noise; + cgbn_set_ui32(env, noise, 1); + + for (int k = 0; k < PAILLIER_NOISE_MULTIPLES; ++k) { + uint32_t r32 = random_words[instance * PAILLIER_NOISE_MULTIPLES + k]; + int idx = (int)(r32 % (uint32_t)noise_table_size); + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &NOISE_TABLE[idx]); + cgbn_mul_wide(env, wide, noise, entry); + cgbn_rem_wide(env, noise, wide, n2); + } + + // --- c = g_m * noise mod n^2 --- + typename env_t::cgbn_t c; + cgbn_mul_wide(env, wide, g_m, noise); + cgbn_rem_wide(env, c, wide, n2); + + cgbn_store(env, &out_ct[instance], c); +} + +// ---------- GPU: decrypt-only kernel (per-chunk) ---------- +__global__ void decrypt_only_kernel(error_report_t *report, + mem_t *CT, // [total] ciphertexts + int total, // batch * chunk_num + mem_t *OUT_M, // [total] plaintext m + int exp_bits) { + const int thread = blockIdx.x*blockDim.x + threadIdx.x; + const int instance = thread / CGBN_TPI; + if (instance >= total) return; + + typedef cgbn_context_t context_t; + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + typename env_t::cgbn_t n, n2, a, g_a_inv, c, t, x, q, rrem, m; + typename env_t::cgbn_wide_t wide; + + cgbn_load(env, c, &CT[instance]); + cgbn_load(env, n, &CONST_N); + cgbn_load(env, n2, &CONST_N2); + cgbn_load(env, a, &CONST_A); + cgbn_load(env, g_a_inv, &CONST_G_A_INV); + + // t = c^a mod n^2 (LSB-first exp), using small exponent loop + pow_mod_small_exp(env, t, c, &CONST_A, n2, exp_bits); + + // x = t - 1, then exact div by n: x = q*n + rrem + cgbn_sub_ui32(env, x, t, 1); + cgbn_div(env, q, x, n); + cgbn_rem(env, rrem, x, n); // for debug you can check rrem==0 + // q = L(t) = (t - 1) / n + + // m = (q * g_a_inv) mod n + cgbn_mul_wide(env, wide, q, g_a_inv); + cgbn_rem_wide(env, m, wide, n); + + cgbn_store(env, &OUT_M[instance], m); +} + +// Fused decrypt-then-reencrypt kernel (one ciphertext per instance) +__global__ void decrypt_then_reencrypt_kernel(error_report_t *report, + mem_t *CT, // [batch] ciphertexts under key_a + int batch, + mem_t *N_a, // key_a n + mem_t *N2_a, // key_a n^2 + mem_t *A_a, // key_a a + mem_t *G_A_INV_a, // key_a g_a_inv + int exp_bits_a, + mem_t *N2_b, // key_b n^2 + mem_t *G_TABLE_b, // [message_chunks * table_size] + mem_t *NOISE_TABLE_b, // [noise_table_size] + int message_chunks, + int noise_table_size, + const uint32_t *random_words, + mem_t *OUT_CT) { // [batch] ciphertexts under key_b + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= batch) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + typename env_t::cgbn_t n_a, n2_a, a_a, g_a_inv_a; + typename env_t::cgbn_t c_in, t, x, q, rrem, m; + typename env_t::cgbn_wide_t wide; + + cgbn_load(env, c_in, &CT[instance]); + cgbn_load(env, n_a, &N_a[0]); + cgbn_load(env, n2_a, &N2_a[0]); + cgbn_load(env, a_a, &A_a[0]); + cgbn_load(env, g_a_inv_a, &G_A_INV_a[0]); + + // Decrypt: t = c^a mod n^2 + pow_mod_small_exp(env, t, c_in, A_a, n2_a, exp_bits_a); + + // x = t - 1, then x = q*n + rrem + cgbn_sub_ui32(env, x, t, 1); + cgbn_div(env, q, x, n_a); + cgbn_rem(env, rrem, x, n_a); + + // m = (q * g_a_inv) mod n + cgbn_mul_wide(env, wide, q, g_a_inv_a); + cgbn_rem_wide(env, m, wide, n_a); + + // Extract plaintext digits (base 2^MSG_BITS) from m + const int table_size = PAILLIER_MSG_TABLE_SIZE; + + // Load n2_b for encryption + typename env_t::cgbn_t n2_b; + cgbn_load(env, n2_b, &N2_b[0]); + + // Compute g^m via lookup table + typename env_t::cgbn_t g_m; + cgbn_set_ui32(env, g_m, 1); + for (int i = 0; i < message_chunks; ++i) { + uint32_t d = env.extract_bits_ui32(m, i * PAILLIER_MSG_BITS, PAILLIER_MSG_BITS); + if (d == 0) continue; + int idx = i * table_size + (int)d; + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &G_TABLE_b[idx]); + cgbn_mul_wide(env, wide, g_m, entry); + cgbn_rem_wide(env, g_m, wide, n2_b); + } + + // Sample noise using precomputed table + typename env_t::cgbn_t noise; + cgbn_set_ui32(env, noise, 1); + for (int k = 0; k < PAILLIER_NOISE_MULTIPLES; ++k) { + uint32_t r32 = random_words[instance * PAILLIER_NOISE_MULTIPLES + k]; + int idx = (int)(r32 % (uint32_t)noise_table_size); + typename env_t::cgbn_t entry; + cgbn_load(env, entry, &NOISE_TABLE_b[idx]); + cgbn_mul_wide(env, wide, noise, entry); + cgbn_rem_wide(env, noise, wide, n2_b); + } + + // c = g_m * noise mod n^2 + typename env_t::cgbn_t c_out; + cgbn_mul_wide(env, wide, g_m, noise); + cgbn_rem_wide(env, c_out, wide, n2_b); + + cgbn_store(env, &OUT_CT[instance], c_out); +} + + + +// Fused decrypt + odd-bit popcount kernel +__global__ void decode_hamming_fused_kernel(error_report_t *report, + mem_t *CT, // [total] ciphertexts + int total, // batch * chunk_num + int chunk_len, // = 2*key_len + const mem_t *ODD_MASK, + int words_used, + int exp_bits, + uint32_t *out_odd) { + const int thread = blockIdx.x * blockDim.x + threadIdx.x; + const int instance = thread / CGBN_TPI; + if (instance >= total) return; + + typedef cgbn_context_t context_t; + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load keys from constant memory + typename env_t::cgbn_t n, n2, a, g_a_inv, c, t, x, q, rrem, m; + typename env_t::cgbn_wide_t wide; + + cgbn_load(env, c, &CT[instance]); + cgbn_load(env, n, &CONST_N); + cgbn_load(env, n2, &CONST_N2); + cgbn_load(env, a, &CONST_A); + cgbn_load(env, g_a_inv, &CONST_G_A_INV); + + // Decrypt: t = c^a mod n^2 using small exponent loop + pow_mod_small_exp(env, t, c, &CONST_A, n2, exp_bits); + + // x = t - 1, then exact div by n: x = q*n + rrem + cgbn_sub_ui32(env, x, t, 1); + cgbn_div(env, q, x, n); + cgbn_rem(env, rrem, x, n); // rrem should be 0; q = L(t) + + // m = (q * g_a_inv) mod n + cgbn_mul_wide(env, wide, q, g_a_inv); + cgbn_rem_wide(env, m, wide, n); + + // Count odd bit positions via mask + popcount + mem_t mmem; + cgbn_store(env, &mmem, m); + + const uint32_t *mw = reinterpret_cast(&mmem); + const uint32_t *ow = reinterpret_cast(&ODD_MASK[0]); + + uint32_t odd_cnt = 0; + #pragma unroll + for (int w = 0; w < words_used; ++w) { + odd_cnt += __popc(mw[w] & ow[w]); + } + + out_odd[instance] = odd_cnt; +} + + +// Each instance handles one (vector, chunk) ciphertext -> produces count of ones at odd bit positions +__global__ void decode_hamming_kernel(error_report_t *report, + mem_t *CT, // [total] ciphertexts + int total, // batch * chunk_num + int chunk_len, // = 2*key_len + mem_t *N, mem_t *N2, + mem_t *A, mem_t *G_A_INV, + uint32_t *out_odd, // <-- NEW name + uint32_t *out_even, // <-- NEW + uint32_t *out_rem_nz, // <-- NEW + mem_t *out_m_dbg, // <-- OPTIONAL (can pass nullptr) + /* Additional parameters for GPU mask inputs for bitcounting*/ + const mem_t *ODD_MASK, + int words_used, + int exp_bits) +{ + + int thread = blockIdx.x * blockDim.x + threadIdx.x; + int instance = thread / CGBN_TPI; + if (instance >= total) return; + + context_t context(cgbn_report_monitor, report, instance); + env_t env(context); + + // Load inputs + typename env_t::cgbn_t n, n2, a, g_a_inv, c; + cgbn_load(env, n, &N[0]); + cgbn_load(env, n2, &N2[0]); + cgbn_load(env, a, &A[0]); + cgbn_load(env, g_a_inv, &G_A_INV[0]); + cgbn_load(env, c, &CT[instance]); + + // Decrypt: t = c^a mod n^2 + typename env_t::cgbn_t t, x, q, rrem, m; + typename env_t::cgbn_wide_t wide; + + pow_mod_small_exp(env, t, c, A, n2, exp_bits); // t = c^a mod n^2 + // pow_mod_small_exp(env, t, c, A, n2, exp_bits); // duplicate call kept commented for reversibility + // x = t - 1 (t is in [1, n^2-1], so this is >= 0) + cgbn_sub_ui32(env, x, t, 1); + +// Divide exactly: x = q*n + rrem (rrem must be 0) +cgbn_div(env, q, x, n); +cgbn_rem(env, rrem, x, n); +uint32_t rem_nz = (cgbn_compare_ui32(env, rrem, 0) != 0); + +// q = L(t) = (t - 1) / n + +// m = (q * g_a_inv) mod n +cgbn_mul_wide(env, wide, q, g_a_inv); +cgbn_rem_wide(env, m, wide, n); + +// Store m for host-side inspection if requested +if (out_m_dbg != nullptr) { + cgbn_store(env, &out_m_dbg[instance], m); +} + +// Count odd/even bit positions MSB-first over chunk_len +// ---- PURE GPU COUNTING via mask+popcount ---- + mem_t mmem; + cgbn_store(env, &mmem, m); + + const uint32_t *mw = reinterpret_cast(&mmem); + const uint32_t *ow = reinterpret_cast(&ODD_MASK[0]); + + uint32_t odd_cnt = 0; + #pragma unroll + for (int w=0; w= total) return; + + mem_t mmem = M[instance]; + const uint32_t *mw = reinterpret_cast(&mmem); + const uint32_t *ow = reinterpret_cast(&ODD_MASK[0]); + + uint32_t odd_cnt = 0; + #pragma unroll + for (int w = 0; w < words_used; ++w) { + odd_cnt += __popc(mw[w] & ow[w]); + } + out_odd[instance] = odd_cnt; +} + +// ---------------------- Python <-> GMP helpers ---------------------- +static bigint::big_int_t big_from_py(py::handle obj) { + std::string s = py::cast(py::str(obj)); + size_t l = s.find_first_not_of(" \t\n\r"); + size_t r = s.find_last_not_of(" \t\n\r"); + if (l == std::string::npos) { + s = "0"; + } else { + s = s.substr(l, r - l + 1); + } + int base = 10; + if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { + base = 16; + s = s.substr(2); + } else if (s.size() > 3 && s[0] == '-' && s[1] == '0' && + (s[2] == 'x' || s[2] == 'X')) { + base = 16; + s = "-" + s.substr(3); + } + return bigint::big_int_t(s, base); +} + +static py::int_ py_int_from_big(const bigint::big_int_t &x) { + std::string dec = x.to_string(10); + PyObject *pobj = PyLong_FromString(const_cast(dec.c_str()), nullptr, 10); + if (!pobj) { + throw py::error_already_set(); + } + return py::reinterpret_steal(pobj); +} + +static py::object py_mpz_from_big(const bigint::big_int_t &x) { + static py::object mpz_ctor = py::module_::import("gmpy2").attr("mpz"); + return mpz_ctor(py::str(x.to_string(10))); +} + +static py::dict extract_dict_field(const py::dict &d, const char *field) { + if (d.contains(field)) { + return py::cast(d[field]); + } + return d; +} + +static PaillierPublicKeyCPU pk_from_py(const py::dict &d_in) { + py::dict pk = extract_dict_field(d_in, "pk"); + if (!pk.contains("g") || !pk.contains("n") || !pk.contains("n_squared")) { + throw std::runtime_error("key pk must contain g, n, n_squared"); + } + PaillierPublicKeyCPU pk_out; + pk_out.g = big_from_py(pk["g"]); + pk_out.n = big_from_py(pk["n"]); + pk_out.n_squared = big_from_py(pk["n_squared"]); + return pk_out; +} + +static PaillierSecretKeyCPU sk_from_py(const py::dict &d_in) { + py::dict sk = extract_dict_field(d_in, "sk"); + if (!sk.contains("a") || !sk.contains("g_a_inv")) { + throw std::runtime_error("key sk must contain a and g_a_inv"); + } + PaillierSecretKeyCPU sk_out; + sk_out.a = big_from_py(sk["a"]); + sk_out.g_a_inv = big_from_py(sk["g_a_inv"]); + if (sk.contains("phi")) sk_out.phi = big_from_py(sk["phi"]); + if (sk.contains("inv")) sk_out.inv = big_from_py(sk["inv"]); + return sk_out; +} + +static int message_chunks_from_key(const py::dict &kb, const bigint::big_int_t &n) { + if (kb.contains("message_chunks")) { + return py::cast(kb["message_chunks"]); + } + if (kb.contains("key_len")) { + int key_len_bits = py::cast(kb["key_len"]); + return (key_len_bits * 2 + PAILLIER_MSG_BITS - 1) / PAILLIER_MSG_BITS; + } + unsigned n_bits = static_cast(mpz_sizeinbase(n.v, 2)); + return (static_cast(n_bits) + PAILLIER_MSG_BITS - 1) / PAILLIER_MSG_BITS; +} + +static std::vector g_table_from_py(const py::handle &obj, int message_chunks) { + const int table_size = PAILLIER_MSG_TABLE_SIZE; + std::vector table(static_cast(message_chunks) * static_cast(table_size)); + if (obj.is_none()) return {}; + + auto load_row = [&](int i, const py::handle &row_obj) { + py::sequence row = py::cast(row_obj); + if (static_cast(py::len(row)) != table_size) { + throw std::runtime_error("g_table row length mismatch"); + } + size_t base = static_cast(i) * static_cast(table_size); + for (int j = 0; j < table_size; ++j) { + table[base + static_cast(j)] = bigint_to_mem(big_from_py(row[j])); + } + }; + + if (py::isinstance(obj)) { + py::dict d = py::reinterpret_borrow(obj); + for (int i = 0; i < message_chunks; ++i) { + py::object row_obj; + py::int_ key_i(i); + if (d.contains(key_i)) { + row_obj = d[key_i]; + } else { + py::str key_s(std::to_string(i)); + if (!d.contains(key_s)) { + throw std::runtime_error("g_table missing row"); + } + row_obj = d[key_s]; + } + load_row(i, row_obj); + } + return table; + } + + if (py::isinstance(obj)) { + py::sequence outer = py::cast(obj); + if (static_cast(py::len(outer)) != message_chunks) { + throw std::runtime_error("g_table outer length mismatch"); + } + for (int i = 0; i < message_chunks; ++i) { + load_row(i, outer[i]); + } + return table; + } + + throw std::runtime_error("g_table must be dict or sequence"); +} + +static std::vector noise_table_from_py(const py::handle &obj) { + if (obj.is_none()) return {}; + py::sequence seq = py::cast(obj); + const int size = static_cast(py::len(seq)); + if (size <= 0) return {}; + std::vector table(static_cast(size)); + for (int i = 0; i < size; ++i) { + table[static_cast(i)] = bigint_to_mem(big_from_py(seq[i])); + } + return table; +} + + +// ---------------------- Host glue helpers ---------------------- +static std::vector gpu_pointwise_mulmod_hex(const std::vector &ct1_hex, + const std::vector &ct2_hex, + const std::string &n_squared_hex) { + if (ct1_hex.size() != ct2_hex.size()) { + throw std::runtime_error("encode_hamming_*: ct1 and ct2 must have the same length"); + } + const int count = static_cast(ct1_hex.size()); + if (count == 0) return {}; + + std::vector h_A(count), h_B(count); + std::vector mod_words = hex_to_le_words(n_squared_hex, LIMBS); + mem_t h_MOD_one{}; + std::memcpy(&h_MOD_one, mod_words.data(), LIMBS * sizeof(uint32_t)); + + for (int i = 0; i < count; ++i) { + auto a = hex_to_le_words(ct1_hex[i], LIMBS); + auto b = hex_to_le_words(ct2_hex[i], LIMBS); + std::memcpy(&h_A[i], a.data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_B[i], b.data(), LIMBS * sizeof(uint32_t)); + } + + error_report_t *d_report = nullptr; + mem_t *d_A = nullptr, *d_B = nullptr, *d_MOD = nullptr, *d_R = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_A, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_B, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_R, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_MOD, sizeof(mem_t))); + + CUDA_CHECK(cudaMemcpy(d_A, h_A.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_B, h_B.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_MOD, &h_MOD_one, sizeof(mem_t), cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (count + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + modmul_pointwise_kernel<<>>(d_report, d_A, d_B, d_MOD, d_R, count); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_R(count); + CUDA_CHECK(cudaMemcpy(h_R.data(), d_R, count * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_A); + cudaFree(d_B); + cudaFree(d_R); + cudaFree(d_MOD); + cudaFree(d_report); + + std::vector out(count); + for (int i = 0; i < count; ++i) { + std::vector words(LIMBS); + std::memcpy(words.data(), &h_R[i], LIMBS * sizeof(uint32_t)); + out[i] = le_words_to_hex(words); + } + return out; +} + +static std::vector +gpu_pointwise_mulmod_bigint(const std::vector &ct1, + const std::vector &ct2, + const bigint::big_int_t &n_squared) { + if (ct1.size() != ct2.size()) { + throw std::runtime_error("encode_hamming_*: ct1 and ct2 must have the same length"); + } + const int count = static_cast(ct1.size()); + if (count == 0) return {}; + + std::vector h_A(count), h_B(count); + mem_t h_MOD_one = bigint_to_mem(n_squared); + + for (int i = 0; i < count; ++i) { + h_A[i] = bigint_to_mem(ct1[i]); + h_B[i] = bigint_to_mem(ct2[i]); + } + + error_report_t *d_report = nullptr; + mem_t *d_A = nullptr, *d_B = nullptr, *d_MOD = nullptr, *d_R = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_A, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_B, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_R, count * sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_MOD, sizeof(mem_t))); + + CUDA_CHECK(cudaMemcpy(d_A, h_A.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_B, h_B.data(), count * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_MOD, &h_MOD_one, sizeof(mem_t), cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (count + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + modmul_pointwise_kernel<<>>(d_report, d_A, d_B, d_MOD, d_R, count); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_R(count); + CUDA_CHECK(cudaMemcpy(h_R.data(), d_R, count * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_A); + cudaFree(d_B); + cudaFree(d_R); + cudaFree(d_MOD); + cudaFree(d_report); + + std::vector out; + out.reserve(static_cast(count)); + for (int i = 0; i < count; ++i) { + out.emplace_back(mem_to_bigint(h_R[i])); + } + return out; +} + +// ---------------------- PaillierGPUClient class ---------------------- +class PaillierGPULookupClient { +public: + static int max_key_len() { return KEY_BITS; } + + PaillierGPULookupClient(int embed_len = 512, + int key_len = KEY_BITS, + int alpha_len = ALPHA_LEN, + bool skip_key_gen = false) + : embed_len_(embed_len), + key_len_(key_len), + chunk_len_(key_len * 2), + chunk_num_(embed_len > key_len ? ((2 * embed_len) / (key_len * 2) + + (((2 * embed_len) % (key_len * 2)) != 0)) + : 1), + alpha_len_(alpha_len), + a_bits_(alpha_len), + have_keys_(!skip_key_gen), + message_chunks_(0), + tables_ready_(!skip_key_gen), + device_tables_ready_(false), + d_g_table_(nullptr), + d_noise_table_(nullptr), + d_g_table_entries_(0), + d_noise_table_entries_(0) { + if (key_len_ > KEY_BITS) { + throw std::runtime_error( + "PaillierGPULookupClient: key_len=" + std::to_string(key_len_) + + " exceeds the compile-time KEY_BITS=" + std::to_string(KEY_BITS) + + " of this GPU extension. Rebuild with a larger KEY_BITS " + "(see build_gpu_binaries.sh) or use a smaller key_len."); + } + if (!skip_key_gen) { + keys_ = PaillierCPU::key_gen(static_cast(key_len_), + static_cast(alpha_len_)); + pk_g_hex_ = keys_.pk.g.to_string(16); + pk_n_hex_ = keys_.pk.n.to_string(16); + pk_n2_hex_ = keys_.pk.n_squared.to_string(16); + sk_phi_hex_ = keys_.sk.phi.to_string(16); + sk_inv_hex_ = keys_.sk.inv.to_string(16); + sk_a_hex_ = keys_.sk.a.to_string(16); + sk_g_a_inv_hex_ = keys_.sk.g_a_inv.to_string(16); + a_bits_ = static_cast(mpz_sizeinbase(keys_.sk.a.v, 2)); + message_chunks_ = (key_len_ * 2 + PAILLIER_MSG_BITS - 1) / PAILLIER_MSG_BITS; + g_table_ = std::make_shared>(precompute_g_table(keys_, key_len_)); + noise_table_ = precompute_noise_table(keys_); + // Light debug: print generated keys (hex) + // std::cerr << "[PaillierGPUClient] Generated keys (hex):\n" + // << " n = " << pk_n_hex_ << "\n" + // << " n_squared= " << pk_n2_hex_ << "\n" + // << " phi = " << sk_phi_hex_ << "\n" + // << " inv = " << sk_inv_hex_ << std::endl; + } + } + + ~PaillierGPULookupClient() { + free_device_tables_(); + } + + int embed_len() const { return embed_len_; } + int key_len() const { return key_len_; } + int chunk_len() const { return chunk_len_; } + int chunk_num() const { return chunk_num_; } + + // Expose public key components as hex for Python-side checks + std::pair get_pk_hex() const { + if (!have_keys_) { + throw std::runtime_error("get_pk_hex: keys not initialized"); + } + return {pk_n_hex_, pk_n2_hex_}; + } + + // Expose full key components as hex: (n, n_squared, phi, inv) + std::tuple get_keys_hex() const { + if (!have_keys_) { + throw std::runtime_error("get_keys_hex: keys not initialized"); + } + return {pk_n_hex_, pk_n2_hex_, sk_phi_hex_, sk_inv_hex_}; + } + + // JSON helpers for interoperability with the Python lookup client + std::string stringify_pk() const { + if (!have_keys_) { + throw std::runtime_error("stringify_pk: keys not initialized"); + } + using namespace bigint; + py::object json = py::module_::import("json"); + + // Compute g_n = g^n mod n^2 + big_int_t g_n; + mpz_powm(g_n.v, keys_.pk.g.v, keys_.pk.n.v, keys_.pk.n_squared.v); + + py::dict pk_dict; + auto to_py_int = [](const big_int_t &x) { + std::string dec = x.to_string(10); + py::str s_dec(dec); + return py::int_(s_dec); + }; + + pk_dict["g"] = to_py_int(keys_.pk.g); + pk_dict["n"] = to_py_int(keys_.pk.n); + pk_dict["n_squared"] = to_py_int(keys_.pk.n_squared); + pk_dict["g_n"] = to_py_int(g_n); + + py::object s = json.attr("dumps")(pk_dict); + return py::cast(s); + } + + std::string stringify_sk() const { + if (!have_keys_) { + throw std::runtime_error("stringify_sk: keys not initialized"); + } + using namespace bigint; + py::object json = py::module_::import("json"); + + py::dict sk_dict; + auto to_py_int = [](const big_int_t &x) { + std::string dec = x.to_string(10); + py::str s_dec(dec); + return py::int_(s_dec); + }; + + sk_dict["phi"] = to_py_int(keys_.sk.phi); + sk_dict["a"] = to_py_int(keys_.sk.a); + sk_dict["g_a_inv"] = to_py_int(keys_.sk.g_a_inv); + + py::object s = json.attr("dumps")(sk_dict); + return py::cast(s); + } + + std::string stringify_config() const { + py::object json = py::module_::import("json"); + py::dict cfg; + cfg["message_chunks"] = py::int_(message_chunks_); + cfg["alpha_len"] = py::int_(alpha_len_); + cfg["embed_len"] = py::int_(embed_len_); + cfg["key_len"] = py::int_(key_len_); + py::object s = json.attr("dumps")(cfg); + return py::cast(s); + } + + void load_stringified_keys(const std::string &pk_json, + const std::string &sk_json) { + using namespace bigint; + py::object json = py::module_::import("json"); + + // Parse pk + py::dict pk_dict = json.attr("loads")(pk_json).cast(); + + auto get_big_int = [](const py::handle &v) -> big_int_t { + py::str s_dec = py::str(v); + std::string s = py::cast(s_dec); + return big_int_t(s, 10); + }; + + big_int_t g = get_big_int(pk_dict["g"]); + big_int_t n = get_big_int(pk_dict["n"]); + big_int_t n2 = get_big_int(pk_dict["n_squared"]); + + // Parse sk + py::dict sk_dict = json.attr("loads")(sk_json).cast(); + big_int_t phi = get_big_int(sk_dict["phi"]); + big_int_t a = get_big_int(sk_dict["a"]); + big_int_t g_a_inv = get_big_int(sk_dict["g_a_inv"]); + + // Recompute inv = phi^{-1} mod n + big_int_t inv; + if (!mpz_invert(inv.v, phi.v, n.v)) { + throw std::runtime_error("load_stringified_keys: phi^{-1} mod n does not exist"); + } + + // Install keys + keys_.pk.g = g; + keys_.pk.n = n; + keys_.pk.n_squared = n2; + keys_.sk.phi = phi; + keys_.sk.inv = inv; + keys_.sk.a = a; + keys_.sk.g_a_inv = g_a_inv; + + const size_t n2_bits = mpz_sizeinbase(keys_.pk.n_squared.v, 2); + if (n2_bits > static_cast(PAILLIER_N_SQUARED_BITS)) { + throw std::runtime_error( + "load_stringified_keys: n_squared has " + std::to_string(n2_bits) + + " bits, exceeding the compile-time capacity of " + + std::to_string(PAILLIER_N_SQUARED_BITS) + + " bits (KEY_BITS=" + std::to_string(KEY_BITS) + + "). Rebuild the GPU extension with a larger KEY_BITS."); + } + + pk_g_hex_ = keys_.pk.g.to_string(16); + pk_n_hex_ = keys_.pk.n.to_string(16); + pk_n2_hex_ = keys_.pk.n_squared.to_string(16); + sk_phi_hex_ = keys_.sk.phi.to_string(16); + sk_inv_hex_ = keys_.sk.inv.to_string(16); + sk_a_hex_ = keys_.sk.a.to_string(16); + sk_g_a_inv_hex_ = keys_.sk.g_a_inv.to_string(16); + a_bits_ = static_cast(mpz_sizeinbase(keys_.sk.a.v, 2)); + alpha_len_ = a_bits_; + + g_table_.reset(); + noise_table_.clear(); + tables_ready_ = false; + free_device_tables_(); + + have_keys_ = true; + } + + void load_config(const py::dict &config, const py::object &tables = py::none()) { + if (!have_keys_) { + throw std::runtime_error("load_config: keys must be loaded first"); + } + + embed_len_ = py::cast(config["embed_len"]); + const int new_key_len = py::cast(config["key_len"]); + if (new_key_len > KEY_BITS) { + throw std::runtime_error( + "load_config: key_len=" + std::to_string(new_key_len) + + " exceeds the compile-time KEY_BITS=" + std::to_string(KEY_BITS) + + " of this GPU extension."); + } + key_len_ = new_key_len; + alpha_len_ = py::cast(config["alpha_len"]); + chunk_len_ = key_len_ * 2; + if (embed_len_ > key_len_) { + chunk_num_ = 2 * embed_len_ / chunk_len_ + + int((2 * embed_len_) % chunk_len_ != 0); + } else { + chunk_num_ = 1; + } + + message_chunks_ = py::cast(config["message_chunks"]); + + free_device_tables_(); + a_bits_ = static_cast(mpz_sizeinbase(keys_.sk.a.v, 2)); + + if (!tables.is_none()) { + if (!py::isinstance(tables)) { + throw std::runtime_error("load_config: tables must be a dict"); + } + py::dict tables_dict = py::reinterpret_borrow(tables); + + std::vector g_table; + std::vector noise_table; + if (tables_dict.contains("g_table")) { + g_table = g_table_from_py(tables_dict["g_table"], message_chunks_); + } + if (tables_dict.contains("noise_table")) { + noise_table = noise_table_from_py(tables_dict["noise_table"]); + } + + const int expected_g_table_size = message_chunks_ * PAILLIER_MSG_TABLE_SIZE; + if (!g_table.empty() && static_cast(g_table.size()) != expected_g_table_size) { + throw std::runtime_error("load_config: g_table size mismatch"); + } + if (!noise_table.empty() && static_cast(noise_table.size()) != PAILLIER_NOISE_TABLE_SIZE) { + throw std::runtime_error("load_config: noise_table size mismatch"); + } + + if (!g_table.empty()) { + g_table_ = std::make_shared>(std::move(g_table)); + } else { + g_table_.reset(); + } + noise_table_ = std::move(noise_table); + tables_ready_ = g_table_ != nullptr && !noise_table_.empty(); + return; + } + + // Lazy init: defer expensive table computation to first use + g_table_.reset(); + noise_table_.clear(); + tables_ready_ = false; + } + + py::dict dump_tables() const { + ensure_tables_ready_(); + + py::dict out; + py::dict g_table_dict; + if (g_table_ != nullptr) { + for (int chunk_idx = 0; chunk_idx < message_chunks_; ++chunk_idx) { + py::list row; + const size_t base = static_cast(chunk_idx) * static_cast(PAILLIER_MSG_TABLE_SIZE); + for (int j = 0; j < PAILLIER_MSG_TABLE_SIZE; ++j) { + row.append(py_mpz_from_big(mem_to_bigint((*g_table_)[base + static_cast(j)]))); + } + g_table_dict[py::int_(chunk_idx)] = row; + } + } + + py::list noise_list; + for (const auto &entry : noise_table_) { + noise_list.append(py_mpz_from_big(mem_to_bigint(entry))); + } + + out["g_table"] = g_table_dict; + out["noise_table"] = noise_list; + return out; + } + + py::dict dump_tables_bytes() const { + return dump_tables(); + } + + void load_config_bytes(const py::dict &config, const py::object &tables = py::none()) { + load_config(config, tables); + } + + // Batched encrypt: embeddings is [batch][embed_len] of 0/1, returns [batch][chunk_num] ciphers + py::list + encrypt(const std::vector> &embeddings) const { + if (!have_keys_) { + throw std::runtime_error("encrypt: keys not initialized"); + } + ensure_tables_ready_(); + ensure_device_tables_(); + const int batch = static_cast(embeddings.size()); + const int embed_len = embed_len_; + const int chunk_len = chunk_len_; + const int chunk_num = chunk_num_; + + if (batch == 0) return py::list(); + + // Validate shapes and binary values + for (int i = 0; i < batch; ++i) { + if ((int)embeddings[i].size() != embed_len) { + throw std::runtime_error("encrypt: each embedding must have length == embed_len"); + } + for (int v : embeddings[i]) { + if (v != 0 && v != 1) { + throw std::runtime_error("encrypt: embeddings must be binary 0/1"); + } + } + } + + // Flatten bits into [batch * embed_len] + std::vector h_bits; + h_bits.reserve(static_cast(batch) * static_cast(embed_len)); + for (int i = 0; i < batch; ++i) { + for (int j = 0; j < embed_len; ++j) { + h_bits.push_back(static_cast(embeddings[i][j])); + } + } + + std::vector n_words = hex_to_le_words(pk_n_hex_, LIMBS); + std::vector n2_words = hex_to_le_words(pk_n2_hex_, LIMBS); + mem_t h_N{}, h_N2{}; + std::memcpy(&h_N, n_words.data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, n2_words.data(), LIMBS * sizeof(uint32_t)); + + error_report_t *d_report = nullptr; + uint8_t *d_bits = nullptr; + mem_t *d_N = nullptr, *d_N2 = nullptr, *d_out = nullptr; + uint32_t *d_random_words = nullptr; + + const int total = batch * chunk_num; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + CUDA_CHECK(cudaMalloc((void **)&d_bits, + static_cast(batch) * static_cast(embed_len) * + sizeof(uint8_t))); + CUDA_CHECK(cudaMemcpy(d_bits, + h_bits.data(), + static_cast(batch) * static_cast(embed_len) * + sizeof(uint8_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_N, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_N, &h_N, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_N2, &h_N2, sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_out, total * sizeof(mem_t))); + + const int msg_bits = PAILLIER_MSG_BITS; + const int table_size = PAILLIER_MSG_TABLE_SIZE; + const int message_chunks = message_chunks_; + const int noise_table_size = PAILLIER_NOISE_TABLE_SIZE; + + if (!g_table_ || (int)g_table_->size() != message_chunks * table_size) { + throw std::runtime_error("encrypt: g_table size mismatch"); + } + if ((int)noise_table_.size() != noise_table_size) { + throw std::runtime_error("encrypt: noise_table size mismatch"); + } + + std::vector h_random_words( + static_cast(total) * static_cast(PAILLIER_NOISE_MULTIPLES)); + bigint::fill_os_random(h_random_words.data(), h_random_words.size() * sizeof(uint32_t)); + CUDA_CHECK(cudaMalloc((void **)&d_random_words, h_random_words.size() * sizeof(uint32_t))); + CUDA_CHECK(cudaMemcpy(d_random_words, + h_random_words.data(), + h_random_words.size() * sizeof(uint32_t), + cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (total + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + encrypt_kernel<<>>(d_report, + d_bits, + batch, + embed_len, + chunk_len, + chunk_num, + d_N, + d_N2, + d_g_table_, + d_noise_table_, + message_chunks, + noise_table_size, + d_random_words, + d_out); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_out(total); + CUDA_CHECK(cudaMemcpy(h_out.data(), d_out, total * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_bits); + cudaFree(d_N); + cudaFree(d_N2); + cudaFree(d_random_words); + cudaFree(d_out); + cudaFree(d_report); + + // Reshape back to [batch][chunk_num] GMP ints + py::list out(batch); + for (int i = 0; i < batch; ++i) { + py::list row(chunk_num); + for (int c = 0; c < chunk_num; ++c) { + int idx = i * chunk_num + c; + row[c] = py_mpz_from_big(mem_to_bigint(h_out[idx])); + } + out[i] = row; + } + return out; + } + + // Batched Paillier encryption of arbitrary plaintext integers m < n, using lookup tables. + // Input: plaintexts as Python ints (converted via string), Output: ciphertexts as Python ints. + py::list + encrypt_cts(const std::vector &plaintexts) const { + if (!have_keys_) { + throw std::runtime_error("encrypt_cts: keys not initialized"); + } + ensure_tables_ready_(); + ensure_device_tables_(); + const int batch = static_cast(plaintexts.size()); + if (batch == 0) return py::list(); + + // Prepare n for range checks + const bigint::big_int_t &n_big = keys_.pk.n; + + const int msg_bits = PAILLIER_MSG_BITS; + const int message_chunks = message_chunks_; + const uint64_t mask = (1u << msg_bits) - 1u; + + // Flatten digits: [batch * message_chunks] + std::vector h_digits(static_cast(batch) * + static_cast(message_chunks)); + + for (int i = 0; i < batch; ++i) { + // Convert Python int to decimal string, then to GMP big_int_t + py::object obj = plaintexts[i]; + py::str s_dec = py::str(obj); + std::string s = py::cast(s_dec); + bigint::big_int_t m_big(s, 10); + + if (mpz_sgn(m_big.v) < 0 || mpz_cmp(m_big.v, n_big.v) >= 0) { + throw std::runtime_error("encrypt_cts: plaintext out of range [0, n)"); + } + + for (int j = 0; j < message_chunks; ++j) { + bigint::big_int_t shifted; + mpz_tdiv_q_2exp(shifted.v, m_big.v, msg_bits * j); + unsigned long d = mpz_get_ui(shifted.v) & mask; + h_digits[static_cast(i) * static_cast(message_chunks) + + static_cast(j)] = static_cast(d); + } + } + + // Prepare n and n^2 for device + std::vector n_words = hex_to_le_words(pk_n_hex_, LIMBS); + std::vector n2_words = hex_to_le_words(pk_n2_hex_, LIMBS); + mem_t h_N{}, h_N2{}; + std::memcpy(&h_N, n_words.data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, n2_words.data(), LIMBS * sizeof(uint32_t)); + + error_report_t *d_report = nullptr; + uint8_t *d_digits = nullptr; + mem_t *d_N = nullptr, *d_N2 = nullptr, *d_out = nullptr; + uint32_t *d_random_words = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_digits, + static_cast(batch) * + static_cast(message_chunks) * sizeof(uint8_t))); + CUDA_CHECK(cudaMemcpy(d_digits, + h_digits.data(), + static_cast(batch) * + static_cast(message_chunks) * sizeof(uint8_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_N, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_N, &h_N, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_N2, &h_N2, sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_out, batch * sizeof(mem_t))); + + const int table_size = PAILLIER_MSG_TABLE_SIZE; + const int noise_table_size = PAILLIER_NOISE_TABLE_SIZE; + + if (!g_table_ || (int)g_table_->size() != message_chunks * table_size) { + throw std::runtime_error("encrypt_cts: g_table size mismatch"); + } + if ((int)noise_table_.size() != noise_table_size) { + throw std::runtime_error("encrypt_cts: noise_table size mismatch"); + } + + std::vector h_random_words( + static_cast(batch) * static_cast(PAILLIER_NOISE_MULTIPLES)); + bigint::fill_os_random(h_random_words.data(), h_random_words.size() * sizeof(uint32_t)); + CUDA_CHECK(cudaMalloc((void **)&d_random_words, h_random_words.size() * sizeof(uint32_t))); + CUDA_CHECK(cudaMemcpy(d_random_words, + h_random_words.data(), + h_random_words.size() * sizeof(uint32_t), + cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (batch + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + encrypt_cts_kernel<<>>(d_report, + d_digits, + batch, + message_chunks, + d_N, + d_N2, + d_g_table_, + d_noise_table_, + noise_table_size, + d_random_words, + d_out); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_out(batch); + CUDA_CHECK(cudaMemcpy(h_out.data(), d_out, batch * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_digits); + cudaFree(d_N); + cudaFree(d_N2); + cudaFree(d_random_words); + cudaFree(d_out); + cudaFree(d_report); + + // Convert to GMP ints + py::list out(batch); + for (int i = 0; i < batch; ++i) { + out[i] = py_mpz_from_big(mem_to_bigint(h_out[i])); + } + return out; + } + + // Decrypt ciphertexts under key_a, then re-encrypt under key_b (fused GPU kernel). + py::list decrypt_then_reencrypt(const py::sequence &ciphers, + const py::dict &key_a, + const py::dict &key_b) const { + const int batch = static_cast(py::len(ciphers)); + if (batch == 0) return py::list(); + + PaillierPublicKeyCPU pk_a = pk_from_py(key_a); + PaillierSecretKeyCPU sk_a = sk_from_py(key_a); + PaillierPublicKeyCPU pk_b = pk_from_py(key_b); + + const int message_chunks = message_chunks_from_key(key_b, pk_b.n); + const int table_size = PAILLIER_MSG_TABLE_SIZE; + + std::vector g_table; + std::vector noise_table; + + if (key_b.contains("g_table")) { + g_table = g_table_from_py(key_b["g_table"], message_chunks); + } + if (key_b.contains("noise_table")) { + noise_table = noise_table_from_py(key_b["noise_table"]); + } + + if (g_table.empty() || noise_table.empty()) { + PaillierKeyPairCPU keys_b{}; + keys_b.pk = pk_b; + if (g_table.empty()) { + int key_len_bits = (message_chunks * PAILLIER_MSG_BITS) / 2; + g_table = precompute_g_table(keys_b, key_len_bits); + } + if (noise_table.empty()) { + noise_table = precompute_noise_table(keys_b); + } + } + + if ((int)g_table.size() != message_chunks * table_size) { + throw std::runtime_error("decrypt_then_reencrypt: g_table size mismatch"); + } + const int noise_table_size = static_cast(noise_table.size()); + if (noise_table_size <= 0) { + throw std::runtime_error("decrypt_then_reencrypt: noise_table is empty"); + } + + // Pack ciphertexts + std::vector h_CT(batch); + for (int i = 0; i < batch; ++i) { + h_CT[i] = bigint_to_mem(big_from_py(ciphers[i])); + } + + mem_t h_Na = bigint_to_mem(pk_a.n); + mem_t h_N2a = bigint_to_mem(pk_a.n_squared); + mem_t h_Aa = bigint_to_mem(sk_a.a); + mem_t h_GaInv = bigint_to_mem(sk_a.g_a_inv); + mem_t h_N2b = bigint_to_mem(pk_b.n_squared); + + error_report_t *d_report = nullptr; + mem_t *d_CT = nullptr, *d_OUT = nullptr; + mem_t *d_Na = nullptr, *d_N2a = nullptr, *d_Aa = nullptr, *d_GaInv = nullptr; + mem_t *d_N2b = nullptr, *d_g_table = nullptr, *d_noise_table = nullptr; + uint32_t *d_random_words = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_CT, batch * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_CT, h_CT.data(), batch * sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_OUT, batch * sizeof(mem_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_Na, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_N2a, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_Aa, sizeof(mem_t))); + CUDA_CHECK(cudaMalloc((void **)&d_GaInv, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_Na, &h_Na, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_N2a, &h_N2a, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_Aa, &h_Aa, sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMemcpy(d_GaInv, &h_GaInv, sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_N2b, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_N2b, &h_N2b, sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_g_table, + static_cast(message_chunks) * + static_cast(table_size) * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_g_table, + g_table.data(), + static_cast(message_chunks) * + static_cast(table_size) * sizeof(mem_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_noise_table, + static_cast(noise_table_size) * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_noise_table, + noise_table.data(), + static_cast(noise_table_size) * sizeof(mem_t), + cudaMemcpyHostToDevice)); + + int exp_bits_a = static_cast(mpz_sizeinbase(sk_a.a.v, 2)); + if (exp_bits_a <= 0) exp_bits_a = key_len_ * 2; + + std::vector h_random_words( + static_cast(batch) * static_cast(PAILLIER_NOISE_MULTIPLES)); + bigint::fill_os_random(h_random_words.data(), h_random_words.size() * sizeof(uint32_t)); + CUDA_CHECK(cudaMalloc((void **)&d_random_words, h_random_words.size() * sizeof(uint32_t))); + CUDA_CHECK(cudaMemcpy(d_random_words, + h_random_words.data(), + h_random_words.size() * sizeof(uint32_t), + cudaMemcpyHostToDevice)); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (batch + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + + decrypt_then_reencrypt_kernel<<>>(d_report, + d_CT, + batch, + d_Na, + d_N2a, + d_Aa, + d_GaInv, + exp_bits_a, + d_N2b, + d_g_table, + d_noise_table, + message_chunks, + noise_table_size, + d_random_words, + d_OUT); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_OUT(batch); + CUDA_CHECK(cudaMemcpy(h_OUT.data(), d_OUT, batch * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_CT); + cudaFree(d_OUT); + cudaFree(d_Na); + cudaFree(d_N2a); + cudaFree(d_Aa); + cudaFree(d_GaInv); + cudaFree(d_N2b); + cudaFree(d_g_table); + cudaFree(d_noise_table); + cudaFree(d_random_words); + cudaFree(d_report); + + py::list out(batch); + for (int i = 0; i < batch; ++i) { + out[i] = py_mpz_from_big(mem_to_bigint(h_OUT[i])); + } + return out; + } + + // Client-side homomorphic addition: ct1[i]*ct2[i] mod n^2 + py::list encode_hamming_client(const py::sequence &ct1, + const py::sequence &ct2) const { + if (!have_keys_) { + throw std::runtime_error("encode_hamming_client: keys not initialized"); + } + if (py::len(ct1) != py::len(ct2)) { + throw std::runtime_error("encode_hamming_client: ct1 and ct2 must have the same length"); + } + std::vector a; + std::vector b; + a.reserve(py::len(ct1)); + b.reserve(py::len(ct2)); + for (auto &&o : ct1) a.emplace_back(big_from_py(o)); + for (auto &&o : ct2) b.emplace_back(big_from_py(o)); + + auto res = gpu_pointwise_mulmod_bigint(a, b, keys_.pk.n_squared); + py::list out(res.size()); + for (size_t i = 0; i < res.size(); ++i) { + out[i] = py_mpz_from_big(res[i]); + } + return out; + } + + // Server-side version: takes pk dict with "n_squared" + static py::list encode_hamming_server(const py::sequence &ct1, + const py::sequence &ct2, + const py::dict &pk) { + if (py::len(ct1) != py::len(ct2)) { + throw std::runtime_error("encode_hamming_server: ct1 and ct2 must have the same length"); + } + if (!pk.contains("n_squared")) { + throw std::runtime_error("encode_hamming_server: pk must contain n_squared"); + } + std::vector a; + std::vector b; + a.reserve(py::len(ct1)); + b.reserve(py::len(ct2)); + for (auto &&o : ct1) a.emplace_back(big_from_py(o)); + for (auto &&o : ct2) b.emplace_back(big_from_py(o)); + + bigint::big_int_t n2_big = big_from_py(pk["n_squared"]); + auto res = gpu_pointwise_mulmod_bigint(a, b, n2_big); + py::list out(res.size()); + for (size_t i = 0; i < res.size(); ++i) { + out[i] = py_mpz_from_big(res[i]); + } + return out; + } + + // GPU decrypt chunks to plaintext m, then CPU bit-padding and odd-bit counting + std::vector + decode_hamming_client(const py::sequence &ciphers_batch) const { + if (!have_keys_) { + throw std::runtime_error("decode_hamming_client: keys not initialized"); + } + const int batch = static_cast(py::len(ciphers_batch)); + const int chunk_num = chunk_num_; + const int chunk_len = chunk_len_; + if (batch == 0) return {}; + + const int total = batch * chunk_num; + + // Pack ciphertexts into mem_t array + std::vector h_CT(total); + for (int i = 0; i < batch; ++i) { + py::sequence row = py::cast(ciphers_batch[i]); + if (static_cast(py::len(row)) != chunk_num) { + throw std::runtime_error("decode_hamming_client: each inner list must have length == chunk_num"); + } + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + h_CT[idx] = bigint_to_mem(big_from_py(row[c])); + } + } + + // Keys -> constant memory (for decrypt kernels) + mem_t h_N{}, h_N2{}, h_A{}, h_G_A_INV{}; + std::memcpy(&h_N, hex_to_le_words(pk_n_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, hex_to_le_words(pk_n2_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_A, hex_to_le_words(sk_a_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_G_A_INV, hex_to_le_words(sk_g_a_inv_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N, &h_N, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N2, &h_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_A, &h_A, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_G_A_INV, &h_G_A_INV, sizeof(mem_t))); + + // Build odd-position mask and compute words used + mem_t h_MASK = make_odd_mask_host(chunk_len); + const int words_used = (chunk_len + 31) / 32; + + // Device buffers + error_report_t *d_report = nullptr; + mem_t *d_CT = nullptr, *d_M = nullptr, *d_MASK = nullptr; + uint32_t *d_out_odd = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_CT, total * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_CT, h_CT.data(), total * sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_M, total * sizeof(mem_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_MASK, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_MASK, &h_MASK, sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_out_odd, total * sizeof(uint32_t))); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (total + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + int exp_bits = (a_bits_ > 0) ? a_bits_ : (key_len_ * 2); + + // First kernel: decrypt-only to plaintext m in d_M + decrypt_only_kernel<<>>(d_report, d_CT, total, d_M, exp_bits); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + // Second kernel: GPU popcount on m with odd-bit mask + odd_popcount_kernel<<>>(d_M, + total, + d_MASK, + words_used, + d_out_odd); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_out_odd(total); + CUDA_CHECK(cudaMemcpy(h_out_odd.data(), d_out_odd, total * sizeof(uint32_t), + cudaMemcpyDeviceToHost)); + + cudaFree(d_CT); + cudaFree(d_M); + cudaFree(d_MASK); + cudaFree(d_out_odd); + cudaFree(d_report); + + // Sum odd counts per embedding (across chunks) + std::vector out(batch, 0); + for (int i = 0; i < batch; ++i) { + long long ham = 0; + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + ham += static_cast(h_out_odd[idx]); + } + out[i] = ham; + } + return out; + } + + // Batched Paillier decryption: ciphers is [batch] ciphertext ints, returns Python ints. + std::vector + decrypt_cts(const py::sequence &ciphers) const { + if (!have_keys_) { + throw std::runtime_error("decrypt_cts: keys not initialized"); + } + const int batch = static_cast(py::len(ciphers)); + if (batch == 0) return {}; + + // Pack CT + std::vector h_CT(batch); + for (int i = 0; i < batch; ++i) { + h_CT[i] = bigint_to_mem(big_from_py(ciphers[i])); + } + + // Keys -> constant memory + mem_t h_N{}, h_N2{}, h_A{}, h_G_A_INV{}; + std::memcpy(&h_N, hex_to_le_words(pk_n_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, hex_to_le_words(pk_n2_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_A, hex_to_le_words(sk_a_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_G_A_INV, hex_to_le_words(sk_g_a_inv_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N, &h_N, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N2, &h_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_A, &h_A, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_G_A_INV, &h_G_A_INV, sizeof(mem_t))); + + // Device + error_report_t *d_report = nullptr; + mem_t *d_CT = nullptr, *d_M = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + + CUDA_CHECK(cudaMalloc((void **)&d_CT, batch * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_CT, h_CT.data(), batch * sizeof(mem_t), cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_M, batch * sizeof(mem_t))); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (batch + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + int exp_bits = (a_bits_ > 0) ? a_bits_ : (key_len_ * 2); + decrypt_only_kernel<<>>(d_report, d_CT, batch, d_M, exp_bits); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_M(batch); + CUDA_CHECK(cudaMemcpy(h_M.data(), d_M, batch * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_CT); + cudaFree(d_M); + cudaFree(d_report); + + // Convert plaintexts to Python ints via decimal strings (avoid base issues) + std::vector out(batch); + for (int i = 0; i < batch; ++i) { + out[i] = py_int_from_big(mem_to_bigint(h_M[i])); + } + return out; + } + +private: + int embed_len_; + int key_len_; + int chunk_len_; + int chunk_num_; + int alpha_len_; + int a_bits_; + bool have_keys_; + + PaillierKeyPairCPU keys_; + std::string pk_g_hex_; + std::string pk_n_hex_; + std::string pk_n2_hex_; + std::string sk_phi_hex_; + std::string sk_inv_hex_; + std::string sk_a_hex_; + std::string sk_g_a_inv_hex_; + + mutable int message_chunks_; + mutable std::shared_ptr> g_table_; // [message_chunks_ * PAILLIER_MSG_TABLE_SIZE] + mutable std::vector noise_table_; // [PAILLIER_NOISE_TABLE_SIZE] + mutable bool tables_ready_; + mutable bool device_tables_ready_; + mutable mem_t *d_g_table_; + mutable mem_t *d_noise_table_; + mutable int d_g_table_entries_; + mutable int d_noise_table_entries_; + + // Helper: GPU decrypt all chunks to plaintext m (hex), mirroring original GPU client + std::vector> + decrypt_chunks(const std::vector> &ciphers_hex_batch) const { + const int batch = static_cast(ciphers_hex_batch.size()); + const int chunk_num = this->chunk_num_; + if (batch == 0) return {}; + + for (int i = 0; i < batch; ++i) { + if ((int)ciphers_hex_batch[i].size() != chunk_num) { + throw std::runtime_error("decrypt_chunks: each inner list must have length == chunk_num"); + } + } + + const int total = batch * chunk_num; + + // pack CT + std::vector h_CT(total); + for (int i = 0; i < batch; ++i) { + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + auto w = hex_to_le_words(ciphers_hex_batch[i][c], LIMBS); + std::memcpy(&h_CT[idx], w.data(), LIMBS * sizeof(uint32_t)); + } + } + + // keys -> constant memory + mem_t h_N{}, h_N2{}, h_A{}, h_G_A_INV{}; + std::memcpy(&h_N, hex_to_le_words(pk_n_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_N2, hex_to_le_words(pk_n2_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_A, hex_to_le_words(sk_a_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + std::memcpy(&h_G_A_INV, hex_to_le_words(sk_g_a_inv_hex_, LIMBS).data(), LIMBS * sizeof(uint32_t)); + + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N, &h_N, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_N2, &h_N2, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_A, &h_A, sizeof(mem_t))); + CUDA_CHECK(cudaMemcpyToSymbol(CONST_G_A_INV, &h_G_A_INV, sizeof(mem_t))); + + // device + error_report_t *d_report = nullptr; + mem_t *d_CT = nullptr, *d_M = nullptr; + + CUDA_CHECK(cudaMalloc((void **)&d_report, sizeof(error_report_t))); + CUDA_CHECK(cudaMemset(d_report, 0, sizeof(error_report_t))); + CUDA_CHECK(cudaMalloc((void **)&d_CT, total * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_CT, h_CT.data(), total * sizeof(mem_t), cudaMemcpyHostToDevice)); + CUDA_CHECK(cudaMalloc((void **)&d_M, total * sizeof(mem_t))); + + const int INSTS_PER_BLK = 8; + const int THREADS = INSTS_PER_BLK * CGBN_TPI; + const int blocks = (total + INSTS_PER_BLK - 1) / INSTS_PER_BLK; + int exp_bits = (a_bits_ > 0) ? a_bits_ : (key_len_ * 2); + decrypt_only_kernel<<>>(d_report, d_CT, total, d_M, exp_bits); + CUDA_CHECK(cudaPeekAtLastError()); + CUDA_CHECK(cudaDeviceSynchronize()); + + std::vector h_M(total); + CUDA_CHECK(cudaMemcpy(h_M.data(), d_M, total * sizeof(mem_t), cudaMemcpyDeviceToHost)); + + cudaFree(d_CT); + cudaFree(d_M); + cudaFree(d_report); + + std::vector> out(batch, std::vector(chunk_num)); + for (int i = 0; i < batch; ++i) { + for (int c = 0; c < chunk_num; ++c) { + const int idx = i * chunk_num + c; + std::vector w(LIMBS); + std::memcpy(w.data(), &h_M[idx], LIMBS * sizeof(uint32_t)); + out[i][c] = le_words_to_hex(w); + } + } + return out; + } + + std::string g_table_cache_key_() const { + return pk_g_hex_ + "|" + pk_n_hex_ + "|" + std::to_string(key_len_); + } + + void ensure_tables_ready_() const { + if (tables_ready_) return; + if (!have_keys_) { + throw std::runtime_error("lookup tables: keys not initialized"); + } + if (message_chunks_ <= 0) { + message_chunks_ = (key_len_ * 2 + PAILLIER_MSG_BITS - 1) / PAILLIER_MSG_BITS; + } + if (!g_table_) { + const std::string cache_key = g_table_cache_key_(); + g_table_ = get_cached_g_table(cache_key, keys_, key_len_); + } + if (noise_table_.empty()) { + noise_table_ = precompute_noise_table(keys_); + } + tables_ready_ = true; + } + + void ensure_device_tables_() const { + if (device_tables_ready_) return; + ensure_tables_ready_(); + if (!g_table_ || g_table_->empty()) { + throw std::runtime_error("lookup tables: g_table not initialized"); + } + if (noise_table_.empty()) { + throw std::runtime_error("lookup tables: noise_table not initialized"); + } + d_g_table_entries_ = static_cast(g_table_->size()); + d_noise_table_entries_ = static_cast(noise_table_.size()); + + CUDA_CHECK(cudaMalloc((void **)&d_g_table_, + static_cast(d_g_table_entries_) * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_g_table_, + g_table_->data(), + static_cast(d_g_table_entries_) * sizeof(mem_t), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc((void **)&d_noise_table_, + static_cast(d_noise_table_entries_) * sizeof(mem_t))); + CUDA_CHECK(cudaMemcpy(d_noise_table_, + noise_table_.data(), + static_cast(d_noise_table_entries_) * sizeof(mem_t), + cudaMemcpyHostToDevice)); + + device_tables_ready_ = true; + } + + void free_device_tables_() const { + if (d_g_table_ != nullptr) { + cudaFree(d_g_table_); + d_g_table_ = nullptr; + } + if (d_noise_table_ != nullptr) { + cudaFree(d_noise_table_); + d_noise_table_ = nullptr; + } + device_tables_ready_ = false; + d_g_table_entries_ = 0; + d_noise_table_entries_ = 0; + } +}; + +// Backwards-compatible alias +using PaillierGPUClient = PaillierGPULookupClient; + +// ---------------------- Pybind module ---------------------- +PYBIND11_MODULE(paillier_GPU_lookup_client, m) { + m.attr("KEY_BITS") = py::int_(KEY_BITS); + py::class_ lookup_cls(m, "PaillierGPULookupClient"); + lookup_cls + .def_static("max_key_len", &PaillierGPULookupClient::max_key_len) + .def(py::init(), + py::arg("embed_len") = 512, + py::arg("key_len") = KEY_BITS, + py::arg("alpha_len") = ALPHA_LEN, + py::arg("skip_key_gen") = false) + .def("encrypt", &PaillierGPULookupClient::encrypt, + py::arg("embeddings")) + .def("encrypt_cts", &PaillierGPULookupClient::encrypt_cts, + py::arg("plaintexts")) + .def("decrypt_then_reencrypt", &PaillierGPULookupClient::decrypt_then_reencrypt, + py::arg("ciphers"), + py::arg("key_a"), + py::arg("key_b")) + .def("encode_hamming_client", &PaillierGPULookupClient::encode_hamming_client, + py::arg("ct1"), + py::arg("ct2")) + .def_static("encode_hamming_server", &PaillierGPULookupClient::encode_hamming_server, + py::arg("ct1"), + py::arg("ct2"), + py::arg("pk")) + .def("decode_hamming_client", &PaillierGPULookupClient::decode_hamming_client, + py::arg("ciphers_batch")) + .def("decrypt_cts", &PaillierGPULookupClient::decrypt_cts, + py::arg("ciphers")) + .def("get_pk_hex", &PaillierGPULookupClient::get_pk_hex) + .def("get_keys_hex", &PaillierGPULookupClient::get_keys_hex) + .def("stringify_pk", &PaillierGPULookupClient::stringify_pk) + .def("stringify_sk", &PaillierGPULookupClient::stringify_sk) + .def("stringify_config", &PaillierGPULookupClient::stringify_config) + .def("load_stringified_keys", &PaillierGPULookupClient::load_stringified_keys) + .def("load_config", &PaillierGPULookupClient::load_config, + py::arg("config"), + py::arg("tables") = py::none()) + .def("dump_tables", &PaillierGPULookupClient::dump_tables) + .def("dump_tables_bytes", &PaillierGPULookupClient::dump_tables_bytes) + .def("load_config_bytes", &PaillierGPULookupClient::load_config_bytes, + py::arg("config"), + py::arg("tables") = py::none()) + .def_property_readonly("embed_len", &PaillierGPULookupClient::embed_len) + .def_property_readonly("key_len", &PaillierGPULookupClient::key_len) + .def_property_readonly("chunk_len", &PaillierGPULookupClient::chunk_len) + .def_property_readonly("chunk_num", &PaillierGPULookupClient::chunk_num); + // Alias for compatibility with existing Python code + m.attr("PaillierGPUClient") = lookup_cls; +} diff --git a/src/xtrace_sdk/x_vec/utils/execution_context.py b/src/xtrace_sdk/x_vec/utils/execution_context.py index 6fd5b2a..ad1d088 100644 --- a/src/xtrace_sdk/x_vec/utils/execution_context.py +++ b/src/xtrace_sdk/x_vec/utils/execution_context.py @@ -6,6 +6,7 @@ _log = logging.getLogger(__name__) +from xtrace_sdk.x_vec.crypto.device import DeviceMode # noqa: E402 from xtrace_sdk.x_vec.crypto.encryption.aes import AESClient # noqa: E402 from xtrace_sdk.x_vec.crypto.key_provider import ( # noqa: E402 KeyProvider, @@ -47,19 +48,31 @@ def _resolve_key_provider( raise ValueError("Either key_provider or passphrase must be supplied.") +def _canonical_homomorphic_type(client_name: str) -> str: + legacy_map = { + "PaillierGPUClient": "PaillierClient", + "PaillierLookupGPUClient": "PaillierLookupClient", + } + return legacy_map.get(client_name, client_name) + + +def _canonical_json_string(value: str) -> str: + return json.dumps(json.loads(value), sort_keys=True, separators=(",", ":")) + + class ExecutionContext: """Bundles a homomorphic encryption client and an AES key under a single key-provider-protected object. An ``ExecutionContext`` is the root secret for a XTrace deployment. It holds: - - A homomorphic client (``PaillierClient`` or ``PaillierLookupClient``) whose secret key is - used to decrypt Hamming distances returned by the XTrace server. + - A homomorphic client (``PaillierClient`` or ``PaillierLookupClient``) whose secret key is used to + decrypt Hamming distances returned by the XTrace server. - An AES key supplied by a :class:`KeyProvider`, used to encrypt chunk content before upload. The secret key is **never transmitted in plaintext** — it is AES-encrypted with the key provider's key before any remote storage. - :param homomorphic_client: An initialised ``PaillierClient`` or ``PaillierLookupClient``. + :param homomorphic_client: An initialised supported homomorphic client. :param key_provider: A :class:`KeyProvider` that supplies the AES encryption key. :param context_id: Optional deterministic ID. If omitted, one is derived from a SHA-256 hash of the public key and configuration. @@ -84,6 +97,7 @@ def create( salt: bytes | None = None, path: str | None = None, key_provider: KeyProvider | None = None, + device: DeviceMode = "auto", ) -> "ExecutionContext": """Create a new execution context and optionally save it to disk. @@ -98,6 +112,8 @@ def create( :param salt: Optional salt bytes for passphrase-based key derivation. :param path: If provided, persist the context to this file path via :meth:`save_to_disk`. :param key_provider: Explicit :class:`KeyProvider` instance (e.g. :class:`AWSKMSKeyProvider`). + :param device: ``"auto"`` (default) probes for a GPU and falls back to CPU; ``"cpu"`` and + ``"gpu"`` force a backend (``"gpu"`` raises if the extension is unavailable). :return: Initialised ``ExecutionContext``. :raises ValueError: If ``homomorphic_client_type`` is not recognised or ``embedding_length >= key_len``. @@ -110,11 +126,11 @@ def create( f"The Paillier-Lookup scheme requires embed_len < key_len to guarantee the padded " f"plaintext fits within the RSA modulus." ) - homomorphic_client: PaillierClient | PaillierLookupClient + homomorphic_client: HomomorphicClient if homomorphic_client_type.lower() == "paillier": - homomorphic_client = PaillierClient(embed_len=embedding_length, key_len=key_len) + homomorphic_client = PaillierClient(embed_len=embedding_length, key_len=key_len, device=device) elif homomorphic_client_type.lower() == "paillier_lookup": - homomorphic_client = PaillierLookupClient(embed_len=embedding_length, key_len=key_len) + homomorphic_client = PaillierLookupClient(embed_len=embedding_length, key_len=key_len, device=device) else: raise ValueError(f"Unsupported homomorphic client type: {homomorphic_client_type}") ctx = cls(homomorphic_client, provider) @@ -167,16 +183,19 @@ def __str__(self) -> str: def hash(self) -> str: """Compute a deterministic SHA-256 fingerprint of this context's cryptographic identity. - The ``device`` field is excluded so that CPU and GPU contexts sharing the same keys compare - as equal. + The ``device`` field and GPU wrapper type are excluded so that CPU and GPU contexts + sharing the same keys compare as equal. :return: Hex-encoded SHA-256 digest. :rtype: str """ _log.debug("Computing execution context hash...") data = self.to_dict_plain() - data['config'] = self.homomorphic.stringify_config() - str_data = json.dumps(data) + data["type"] = _canonical_homomorphic_type(data["type"]) + data["pk"] = _canonical_json_string(data["pk"]) + data["sk"] = _canonical_json_string(data["sk"]) + data["config"] = _canonical_json_string(data["config"]) + str_data = json.dumps(data, sort_keys=True, separators=(",", ":")) hash_obj = hashlib.sha256(str_data.encode('utf-8')) return hash_obj.hexdigest() @@ -190,7 +209,7 @@ def __eq__(self, other: Any) -> bool: def _config_with_device(self) -> str: cfg = json.loads(self.homomorphic.stringify_config()) - return json.dumps(cfg) + return json.dumps(cfg, sort_keys=True) def serialize_exec_context(self) -> str: """Serialise the execution context to a JSON string suitable for storage or transmission. @@ -201,7 +220,7 @@ def serialize_exec_context(self) -> str: :rtype: str :raises ValueError: If the homomorphic client type is not supported. """ - homomorphic_type = type(self.homomorphic).__name__ + homomorphic_type = _canonical_homomorphic_type(type(self.homomorphic).__name__) if homomorphic_type not in SUPPORTED_HOMOMORPHIC_CLIENTS: raise ValueError(f"Unsupported homomorphic client type: {homomorphic_type}") @@ -217,6 +236,7 @@ def _from_serialized_exec_context( passphrase: str | None = None, key_provider: KeyProvider | None = None, context_id: str | None = None, + device: DeviceMode = "auto", ) -> "ExecutionContext": """Reconstruct an ``ExecutionContext`` from a previously serialised dict. @@ -227,6 +247,7 @@ def _from_serialized_exec_context( :param passphrase: Passphrase for passphrase-based contexts. :param key_provider: Explicit :class:`KeyProvider` to use for decryption. :param context_id: Optional context ID to attach; if ``None`` one is recomputed. + :param device: Backend to load the rebuilt client on; ``"auto"`` probes for a GPU. :return: Restored ``ExecutionContext``. :raises ValueError: If the stored homomorphic client type is not supported. """ @@ -244,14 +265,17 @@ def _from_serialized_exec_context( aes_client = AESClient(provider.get_key()) sk = aes_client.decrypt(json_obj["sk"].encode('utf-8')) config = json.loads(json_obj["config"]) - if json_obj["type"] not in SUPPORTED_HOMOMORPHIC_CLIENTS: + homomorphic_type = _canonical_homomorphic_type(json_obj["type"]) + if homomorphic_type not in SUPPORTED_HOMOMORPHIC_CLIENTS: raise ValueError(f"Unsupported homomorphic client type: {json_obj['type']}") - concrete_client: PaillierClient | PaillierLookupClient - if json_obj["type"] == "PaillierLookupClient": - concrete_client = PaillierLookupClient(embed_len=config["embed_len"], key_len=config["key_len"], alpha_len=config["alpha_len"], skip_key_gen=True) - elif json_obj["type"] == "PaillierClient": - concrete_client = PaillierClient(embed_len=config["embed_len"], key_len=config["key_len"], skip_key_gen=True) + concrete_client: HomomorphicClient + if homomorphic_type == "PaillierLookupClient": + concrete_client = PaillierLookupClient(embed_len=config["embed_len"], key_len=config["key_len"], alpha_len=config["alpha_len"], skip_key_gen=True, device=device) + elif homomorphic_type == "PaillierClient": + concrete_client = PaillierClient(embed_len=config["embed_len"], key_len=config["key_len"], skip_key_gen=True, device=device) + else: + raise ValueError(f"Unsupported homomorphic client type: {json_obj['type']}") concrete_client.load_stringified_keys(json_obj['pk'], sk) @@ -261,7 +285,9 @@ def _from_serialized_exec_context( if isinstance(concrete_client, PaillierLookupClient): concrete_client.load_config(config, precomputed_tables=precomputed_tables) else: - concrete_client.load_config(config) + load_config = getattr(concrete_client, "load_config", None) + if load_config is not None: + load_config(config) return cls(concrete_client, provider, context_id=context_id) @@ -294,20 +320,21 @@ def load_from_disk( passphrase: str | None = None, path: str = "", key_provider: KeyProvider | None = None, + device: DeviceMode = "auto", ) -> "ExecutionContext": """Load an ``ExecutionContext`` from a file previously saved with :meth:`save_to_disk`. :param passphrase: Passphrase for passphrase-based contexts. :param path: File path to read from. - :param salt: Optional salt for passphrase-based key derivation. :param key_provider: Explicit :class:`KeyProvider` (e.g. :class:`AWSKMSKeyProvider`). + :param device: Backend to load on; ``"auto"`` probes for a GPU. :return: Restored ``ExecutionContext``. """ with open(path) as f: exec_context = f.read() json_obj = json.loads(exec_context) - return cls._from_serialized_exec_context(json_obj, passphrase=passphrase, key_provider=key_provider) + return cls._from_serialized_exec_context(json_obj, passphrase=passphrase, key_provider=key_provider, device=device) async def save_to_remote(self, integration: "XTraceIntegration") -> str: """Upload the execution context to XTrace remote storage. @@ -328,18 +355,19 @@ async def load_from_remote( context_id: str = "", integration: "XTraceIntegration | None" = None, key_provider: KeyProvider | None = None, + device: DeviceMode = "auto", ) -> "ExecutionContext": """Fetch and decrypt an ``ExecutionContext`` from XTrace remote storage. :param passphrase: Passphrase for passphrase-based contexts. :param context_id: ID returned when the context was originally saved. :param integration: Authenticated :class:`~xtrace_sdk.integrations.xtrace.XTraceIntegration` instance. - :param salt: Optional salt for passphrase-based key derivation. :param key_provider: Explicit :class:`KeyProvider` (e.g. :class:`AWSKMSKeyProvider`). + :param device: Backend to load on; ``"auto"`` probes for a GPU. :return: Restored ``ExecutionContext``. """ if integration is None: raise ValueError("integration is required.") serial_ctx = await integration.get_serialized_exec_context(context_id) - - return cls._from_serialized_exec_context(serial_ctx, key_provider=key_provider, passphrase=passphrase, context_id=str(context_id)) + + return cls._from_serialized_exec_context(serial_ctx, key_provider=key_provider, passphrase=passphrase, context_id=str(context_id), device=device) diff --git a/src/xtrace_sdk/x_vec/utils/settings.py b/src/xtrace_sdk/x_vec/utils/settings.py index 1666848..299dca9 100644 --- a/src/xtrace_sdk/x_vec/utils/settings.py +++ b/src/xtrace_sdk/x_vec/utils/settings.py @@ -1,3 +1,6 @@ -SUPPORTED_HOMOMORPHIC_CLIENTS = ["PaillierClient", "PaillierLookupClient"] +SUPPORTED_HOMOMORPHIC_CLIENTS = [ + "PaillierClient", + "PaillierLookupClient", +] -REQUIRED_METADATA_TAGS = ["tag1", "tag2", "tag3", "tag4", "tag5", "facets"] \ No newline at end of file +REQUIRED_METADATA_TAGS = ["tag1", "tag2", "tag3", "tag4", "tag5", "facets"] diff --git a/tests/x_vec/test_GPU_paillier_clients.py b/tests/x_vec/test_GPU_paillier_clients.py new file mode 100644 index 0000000..294afd7 --- /dev/null +++ b/tests/x_vec/test_GPU_paillier_clients.py @@ -0,0 +1,199 @@ +import json +import pickle + +import pytest + +from xtrace_sdk.x_vec.crypto.paillier_client import PaillierClient +from xtrace_sdk.x_vec.crypto.paillier_lookup_client import PaillierLookupClient +from xtrace_sdk.x_vec.utils.execution_context import ExecutionContext + + +_PASSPHRASE = "test-gpu-exec-ctx-passphrase" +_EMBED_LEN = 8 +_KEY_LEN = 1024 +_VECTORS = [ + [0, 1, 0, 1, 1, 0, 1, 0], + [1, 1, 0, 0, 1, 0, 0, 1], + [1, 0, 1, 0, 1, 1, 0, 0], +] + + +def _hamming(lhs: list[int], rhs: list[int]) -> int: + return sum(int(a != b) for a, b in zip(lhs, rhs, strict=True)) + + +@pytest.mark.parametrize( + ("client_cls", "client_type"), + [ + pytest.param(PaillierClient, "paillier", id="paillier"), + pytest.param(PaillierLookupClient, "paillier_lookup", id="paillier_lookup"), + ], +) +def test_consecutive_gpu_clients_generate_distinct_keypairs( + client_cls: type[PaillierClient] | type[PaillierLookupClient], + client_type: str, +) -> None: + if not client_cls.has_gpu(): + pytest.skip(f"{client_type} GPU backend is unavailable on this machine") + + first = client_cls(embed_len=_EMBED_LEN, key_len=_KEY_LEN, device="gpu") + second = client_cls(embed_len=_EMBED_LEN, key_len=_KEY_LEN, device="gpu") + + assert json.loads(first.stringify_pk()) != json.loads(second.stringify_pk()) + assert json.loads(first.stringify_sk()) != json.loads(second.stringify_sk()) + + +@pytest.mark.parametrize( + ("client_cls", "client_type"), + [ + pytest.param(PaillierClient, "paillier", id="paillier"), + pytest.param(PaillierLookupClient, "paillier_lookup", id="paillier_lookup"), + ], +) +def test_gpu_clients_expose_execution_context_protocol( + client_cls: type[PaillierClient] | type[PaillierLookupClient], + client_type: str, +) -> None: + if not client_cls.has_gpu(): + pytest.skip(f"{client_type} GPU backend is unavailable on this machine") + + ctx = ExecutionContext.create( + passphrase=_PASSPHRASE, + homomorphic_client_type=client_type, + embedding_length=_EMBED_LEN, + key_len=_KEY_LEN, + device="gpu", + ) + client = ctx.homomorphic + + assert ctx.device == "gpu" + assert ctx.embed_len() == _EMBED_LEN + assert ctx.key_len() == _KEY_LEN + + pk = client.stringify_pk() + sk = client.stringify_sk() + config = client.stringify_config() + config_dict = json.loads(config) + + assert json.loads(pk) + assert json.loads(sk) + assert config_dict["embed_len"] == _EMBED_LEN + assert config_dict["key_len"] == _KEY_LEN + + cipher_one = client.encrypt_vec_one(_VECTORS[0]) + batch_ciphers = client.encrypt_vec_batch(_VECTORS) + + assert len(batch_ciphers) == len(_VECTORS) + assert len(cipher_one) == len(batch_ciphers[0]) > 0 + + encoded_one = client.encode_hamming_server(cipher_one, batch_ciphers[1]) + assert client.decode_hamming_client_one(encoded_one) == _hamming(_VECTORS[0], _VECTORS[1]) + + encoded_batch = [ + client.encode_hamming_server(batch_ciphers[0], batch_ciphers[1]), + client.encode_hamming_server(batch_ciphers[1], batch_ciphers[2]), + ] + assert client.decode_hamming_client_batch(encoded_batch) == [ + _hamming(_VECTORS[0], _VECTORS[1]), + _hamming(_VECTORS[1], _VECTORS[2]), + ] + + clone = client_cls( + embed_len=_EMBED_LEN, + key_len=_KEY_LEN, + skip_key_gen=True, + device="gpu", + ) + clone.load_stringified_keys(pk, sk) + if isinstance(clone, PaillierLookupClient): + clone.load_config(config_dict, precomputed_tables=ctx.dump_tables()) + else: + clone.load_config(config_dict) + + clone_cipher = clone.encrypt_vec_one(_VECTORS[0]) + clone_encoded = clone.encode_hamming_server(clone_cipher, clone.encrypt_vec_one(_VECTORS[1])) + + assert json.loads(clone.stringify_pk()) == json.loads(pk) + assert json.loads(clone.stringify_sk()) == json.loads(sk) + assert json.loads(clone.stringify_config()) == config_dict + assert clone.decode_hamming_client_one(clone_encoded) == _hamming(_VECTORS[0], _VECTORS[1]) + + +@pytest.mark.parametrize( + ("client_cls", "client_type"), + [ + pytest.param(PaillierClient, "paillier", id="paillier"), + pytest.param(PaillierLookupClient, "paillier_lookup", id="paillier_lookup"), + ], +) +@pytest.mark.parametrize( + ("source_device", "target_device"), + [ + pytest.param("cpu", "gpu", id="cpu_to_gpu"), + pytest.param("gpu", "cpu", id="gpu_to_cpu"), + ], +) +def test_cross_device_interop( + client_cls: type[PaillierClient] | type[PaillierLookupClient], + client_type: str, + source_device: str, + target_device: str, +) -> None: + """A context created on one device and loaded on the other must: + + 1. Hash equal (keys are portable; device must not affect identity). + 2. Round-trip: ciphertexts from either side decode correctly on the other. + """ + if not client_cls.has_gpu(): + pytest.skip(f"{client_type} GPU backend is unavailable on this machine") + + source_ctx = ExecutionContext.create( + passphrase=_PASSPHRASE, + homomorphic_client_type=client_type, + embedding_length=_EMBED_LEN, + key_len=_KEY_LEN, + device=source_device, + ) + target_ctx = ExecutionContext._from_serialized_exec_context( + json.loads(source_ctx.serialize_exec_context()), + passphrase=_PASSPHRASE, + device=target_device, + ) + + assert source_ctx.device == source_device + assert target_ctx.device == target_device + assert source_ctx.hash() == target_ctx.hash() + + src = source_ctx.homomorphic + tgt = target_ctx.homomorphic + expected = _hamming(_VECTORS[0], _VECTORS[1]) + + # Encrypt + server-encode on source, decode on target. + src_a = src.encrypt_vec_one(_VECTORS[0]) + src_b = src.encrypt_vec_one(_VECTORS[1]) + encoded_on_source = src.encode_hamming_server(src_a, src_b) + assert tgt.decode_hamming_client_one(encoded_on_source) == expected + + # Encrypt on source, server-encode on target, decode on target. + encoded_on_target = tgt.encode_hamming_server(src_a, src_b) + assert tgt.decode_hamming_client_one(encoded_on_target) == expected + + # Reverse direction: encrypt on target, server-encode on source, decode on source. + tgt_a = tgt.encrypt_vec_one(_VECTORS[0]) + tgt_b = tgt.encrypt_vec_one(_VECTORS[1]) + encoded_back = src.encode_hamming_server(tgt_a, tgt_b) + assert src.decode_hamming_client_one(encoded_back) == expected + + +def test_paillier_lookup_gpu_pickle_roundtrip() -> None: + if not PaillierLookupClient.has_gpu(): + pytest.skip("paillier_lookup GPU backend is unavailable on this machine") + + client = PaillierLookupClient(embed_len=_EMBED_LEN, key_len=_KEY_LEN, device="gpu") + restored = pickle.loads(pickle.dumps(client)) + + encoded = restored.encode_hamming_server( + restored.encrypt_vec_one(_VECTORS[0]), + restored.encrypt_vec_one(_VECTORS[1]), + ) + assert restored.decode_hamming_client_one(encoded) == _hamming(_VECTORS[0], _VECTORS[1]) diff --git a/tests/x_vec/test_execution_context.py b/tests/x_vec/test_execution_context.py index af213da..95dfbd8 100644 --- a/tests/x_vec/test_execution_context.py +++ b/tests/x_vec/test_execution_context.py @@ -104,8 +104,8 @@ def test_equality_same_keys(ctx: ExecutionContext) -> None: def test_inequality_different_keys() -> None: - a = ExecutionContext.create(_PASSPHRASE, "paillier", _EMBED_LEN, _KEY_LEN) - b = ExecutionContext.create(_PASSPHRASE, "paillier", _EMBED_LEN, _KEY_LEN) + a = ExecutionContext.create(_PASSPHRASE, "paillier", _EMBED_LEN, _KEY_LEN, device="cpu") + b = ExecutionContext.create(_PASSPHRASE, "paillier", _EMBED_LEN, _KEY_LEN, device="cpu") assert a != b diff --git a/tests/x_vec/test_hamming_search.py b/tests/x_vec/test_hamming_search.py index 7701e3c..3d8721b 100644 --- a/tests/x_vec/test_hamming_search.py +++ b/tests/x_vec/test_hamming_search.py @@ -79,6 +79,8 @@ async def test_env() -> AsyncGenerator[tuple[XTraceIntegration, Retriever, str], org_id=os.environ["XTRACE_ORG_ID"], api_key=os.environ["XTRACE_API_KEY"], admin_key=os.environ["XTRACE_ADMIN_KEY"], + api_url=os.environ.get("XTRACE_API_URL", "https://api.production.xtrace.ai"), + admin_api_url=os.environ.get("XTRACE_ADMIN_API_URL", "https://api.xtrace.ai/api"), ) kb = await integration.create_kb( diff --git a/tests/x_vec/test_meta_search.py b/tests/x_vec/test_meta_search.py index 86cf124..b7a0a6d 100644 --- a/tests/x_vec/test_meta_search.py +++ b/tests/x_vec/test_meta_search.py @@ -82,6 +82,8 @@ async def test_env() -> AsyncGenerator[tuple[XTraceIntegration, str, str], None] org_id=os.environ["XTRACE_ORG_ID"], api_key=os.environ["XTRACE_API_KEY"], admin_key=os.environ["XTRACE_ADMIN_KEY"], + api_url=os.environ.get("XTRACE_API_URL", "https://api.production.xtrace.ai"), + admin_api_url=os.environ.get("XTRACE_ADMIN_API_URL", "https://api.xtrace.ai/api"), ) kb = await integration.create_kb( diff --git a/uv.lock b/uv.lock index 681ca84..a7fcce6 100644 --- a/uv.lock +++ b/uv.lock @@ -2763,7 +2763,6 @@ wheels = [ [[package]] name = "xtrace-ai-sdk" -version = "0.1.0" source = { editable = "." } dependencies = [ { name = "aiohttp" },