PQ-WB-KEM: White-Box ML-KEM-768 1.0
PQ-WB-KEM: Toward a White-Box Construction of ML-KEM-768 with Arithmetic Masking for M2M Communications - Mathematics (MDPI)
Loading...
Searching...
No Matches
build_arm64_benchmark.sh
Go to the documentation of this file.
1#!/bin/bash
2# Regenerate results/arm64_benchmark.json: cross-build liboqs for aarch64 (ML-KEM-768, static,
3# no OpenSSL), compile bench_liboqs.c against it, run under qemu-aarch64, write the full JSON.
4# Every number in the output is measured by bench_liboqs.c; this script only drives the build.
5# Missing tools are a hard error naming the fix, never a silent fallback.
6#
7# Usage (from the repository root, on an x86-64 Linux/WSL host):
8# code/c/build_arm64_benchmark.sh [output.json] [budget_sec] [liboqs_version] [n_runs]
9# Defaults: results/arm64_benchmark.json, 1.0 s per op, liboqs 0.15.0, 20 runs.
10# The binary is run n_runs times and reduced to mean +/- 95% CI by arm64_aggregate.py.
11set -euo pipefail
12
13OUT="${1:-results/arm64_benchmark.json}"
14BUDGET="${2:-1.0}"
15OQS_VER="${3:-0.15.0}"
16NRUNS="${4:-20}"
17
18HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19REPO="$(cd "$HERE/../.." && pwd)"
20# Build tree lives outside the repo (overridable) so a run leaves git status clean; a stable
21# path lets repeat runs skip the liboqs rebuild.
22BUILD="${ARM64_BUILD_DIR:-$HOME/.cache/pqwbkem_arm64_build}"
23SRC="$BUILD/liboqs"
24PREFIX="$BUILD/install-aarch64"
25TC="$BUILD/aarch64-toolchain.cmake"
26HARNESS="$HERE/bench_liboqs.c"
27BIN="$BUILD/bench_liboqs_arm64"
28
29need() { command -v "$1" >/dev/null 2>&1 || { echo "ERROR: '$1' not found. $2" >&2; exit 1; }; }
30need aarch64-linux-gnu-gcc "Install with: sudo apt-get install -y gcc-aarch64-linux-gnu"
31need qemu-aarch64 "Install with: sudo apt-get install -y qemu-user qemu-user-static"
32need cmake "Install with: sudo apt-get install -y cmake"
33need ninja "Install with: sudo apt-get install -y ninja-build"
34need git "Install with: sudo apt-get install -y git"
35
36mkdir -p "$BUILD"
37
38if [ ! -f "$PREFIX/lib/liboqs.a" ]; then
39 [ -d "$SRC" ] || git clone --depth 1 --branch "$OQS_VER" \
40 https://github.com/open-quantum-safe/liboqs.git "$SRC"
41
42 cat > "$TC" <<'EOF'
43set(CMAKE_SYSTEM_NAME Linux)
44set(CMAKE_SYSTEM_PROCESSOR aarch64)
45set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
46set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
47set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
48set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
49set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
50EOF
51
52 cmake -S "$SRC" -B "$SRC/build" -GNinja \
53 -DCMAKE_TOOLCHAIN_FILE="$TC" \
54 -DCMAKE_BUILD_TYPE=Release \
55 -DOQS_USE_OPENSSL=OFF \
56 -DOQS_BUILD_ONLY_LIB=ON \
57 -DOQS_MINIMAL_BUILD="KEM_ml_kem_768" \
58 -DCMAKE_INSTALL_PREFIX="$PREFIX" \
59 -DCMAKE_C_FLAGS="-O3"
60 ninja -C "$SRC/build"
61 ninja -C "$SRC/build" install
62fi
63
64GCC_VER="$(aarch64-linux-gnu-gcc -dumpfullversion)"
65
66aarch64-linux-gnu-gcc -O3 -static -o "$BIN" "$HARNESS" \
67 -I "$PREFIX/include" -L "$PREFIX/lib" \
68 -DBENCH_COMPILER="\"gcc $GCC_VER\"" \
69 -DBENCH_TARGET="\"aarch64-Linux\"" \
70 -DBENCH_PLATFORM="\"ARM64 (QEMU emulation)\"" \
71 -DBENCH_OPENSSL=0 \
72 -loqs -lm -lpthread
73
74mkdir -p "$(dirname "$REPO/$OUT")" 2>/dev/null || true
75case "$OUT" in
76 /*) DEST="$OUT" ;;
77 *) DEST="$REPO/$OUT" ;;
78esac
79
80# A single qemu-aarch64 run drifts run-to-run; report an interval over N runs, the
81# same statistic wallclock_ci.py uses. Run the binary N times, then aggregate.
82RUNDIR="$BUILD/runs"
83rm -rf "$RUNDIR"; mkdir -p "$RUNDIR"
84for i in $(seq 1 "$NRUNS"); do
85 qemu-aarch64 "$BIN" "$BUDGET" > "$RUNDIR/run_$i.json"
86 echo " run $i/$NRUNS done" >&2
87done
88python3 "$REPO/code/python/arm64_aggregate.py" "$RUNDIR" "$DEST" "$NRUNS"
89echo "wrote $DEST" >&2
set euo pipefail OUT