From 7f75894323c8cc5ae1da2ddfd92b01e7815894c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex?= Date: Mon, 1 Sep 2025 16:13:14 +0200 Subject: [PATCH] ci(circuits): Static linking (#90) * Add static linking to witness generator, prover and verifier. * Remove DLL bundling in witness generator for Windows' build. --- .github/resources/prover/linux.Makefile | 94 +++++++++++++++++++ .github/resources/prover/windows.Makefile | 10 +- .../witness-generator/linux.Makefile | 18 ++++ .../witness-generator/macos.Makefile | 10 +- .../witness-generator/windows.Makefile | 2 +- .github/workflows/build-circuits.yml | 32 +++---- 6 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 .github/resources/prover/linux.Makefile create mode 100644 .github/resources/witness-generator/linux.Makefile diff --git a/.github/resources/prover/linux.Makefile b/.github/resources/prover/linux.Makefile new file mode 100644 index 0000000..8eacc90 --- /dev/null +++ b/.github/resources/prover/linux.Makefile @@ -0,0 +1,94 @@ +### + + +# Build targets +EXTRA_CMAKE_FLAGS ?= + +host: + rm -rf build_prover && mkdir build_prover && cd build_prover && \ + cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package $(EXTRA_CMAKE_FLAGS) && \ + make -j$$(nproc) -vvv && make install + +host_noasm: + rm -rf build_prover_noasm && mkdir build_prover_noasm && cd build_prover_noasm && \ + cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_noasm -DUSE_ASM=NO && \ + make -j$(nproc) -vvv && make install + +host_arm64: + rm -rf build_prover_arm64 && mkdir build_prover_arm64 && cd build_prover_arm64 && \ + cmake .. -DTARGET_PLATFORM=aarch64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_arm64 && \ + make -j$(nproc) -vvv && make install + +android: + rm -rf build_prover_android && mkdir build_prover_android && cd build_prover_android && \ + cmake .. -DTARGET_PLATFORM=ANDROID -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android -DBUILD_TESTS=OFF -DUSE_OPENMP=OFF && \ + make -j$(nproc) -vvv && make install + +android_openmp: + rm -rf build_prover_android_openmp && mkdir build_prover_android_openmp && cd build_prover_android_openmp && \ + cmake .. -DTARGET_PLATFORM=ANDROID -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android_openmp -DBUILD_TESTS=OFF -DUSE_OPENMP=ON && \ + make -j$(nproc) -vvv && make install + +android_x86_64: + rm -rf build_prover_android_x86_64 && mkdir build_prover_android_x86_64 && cd build_prover_android_x86_64 && \ + cmake .. -DTARGET_PLATFORM=ANDROID_x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android_x86_64 -DBUILD_TESTS=OFF -DUSE_OPENMP=OFF && \ + make -j$(nproc) -vvv && make install + +android_openmp_x86_64: + rm -rf build_prover_android_openmp_x86_64 && mkdir build_prover_android_openmp_x86_64 && cd build_prover_android_openmp_x86_64 && \ + cmake .. -DTARGET_PLATFORM=ANDROID_x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android_openmp_x86_64 -DBUILD_TESTS=OFF -DUSE_OPENMP=ON && \ + make -j$(nproc) -vvv && make install + +ios: + @if [ ! -d "./depends/gmp/package_ios_arm64" ]; then echo "Looks like gmp lib is not built. Run './build_gmp.sh ios' first." && exit 1; fi + rm -rf build_prover_ios && mkdir build_prover_ios && cd build_prover_ios && \ + cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios && \ + xcodebuild -destination 'generic/platform=iOS' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj -configuration Release && \ + xcodebuild -destination 'generic/platform=iOS' -scheme rapidsnark -project rapidsnark.xcodeproj -configuration Release CODE_SIGNING_ALLOWED=NO && \ + cp ../depends/gmp/package_ios_arm64/lib/libgmp.a src/Release-iphoneos && \ + echo "" && echo "iOS Simulator artifacts built in build_prover_ios/src/Release-iphoneos" && echo "" + +ios_simulator: + @if [ ! -d "./depends/gmp/package_iphone_simulator" ]; then echo "Looks like gmp lib is not built. Run './build_gmp.sh ios_simulator' first." && exit 1; fi + rm -rf build_prover_ios_simulator && mkdir build_prover_ios_simulator && cd build_prover_ios_simulator && \ + cmake .. -GXcode -DTARGET_PLATFORM=IOS_SIMULATOR -DCMAKE_INSTALL_PREFIX=../package_ios_simulator -DUSE_ASM=NO && \ + xcodebuild -destination 'generic/platform=iOS Simulator' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj && \ + xcodebuild -destination 'generic/platform=iOS Simulator' -scheme rapidsnark -project rapidsnark.xcodeproj CODE_SIGNING_ALLOWED=NO ARCHS=arm64 && \ + cp ../depends/gmp/package_iphone_simulator/lib/libgmp.a src/Debug-iphonesimulator && \ + echo "" && echo "iOS Simulator artifacts built in build_prover_ios_simulator/src/Debug-iphonesimulator" && echo "" + +macos_arm64: + @if [ ! -d "./depends/gmp/package_macos_arm64" ]; then echo "Looks like gmp lib is not built. Run './build_gmp.sh macos_arm64' first." && exit 1; fi + rm -rf build_prover_macos_arm64 && mkdir build_prover_macos_arm64 && cd build_prover_macos_arm64 && \ + cmake .. -DTARGET_PLATFORM=macos_arm64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_macos_arm64 && \ + make -j$(nproc) -vvv && make install + +macos_x86_64: + @if [ ! -d "./depends/gmp/package_macos_x86_64" ]; then echo "Looks like gmp lib is not built. Run './build_gmp.sh macos_x86_64' first." && exit 1; fi + rm -rf build_prover_macos_x86_64 && mkdir build_prover_macos_x86_64 && cd build_prover_macos_x86_64 && \ + cmake .. -DTARGET_PLATFORM=macos_x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_macos_x86_64 && \ + make -j$(nproc) -vvv && make install + +clean: + rm -rf build_prover \ + build_prover_macos_arm64 \ + build_prover_macos_x86_64 \ + build_prover_android \ + build_prover_android_x86_64 \ + build_prover_ios \ + build_prover_ios_simulator \ + package \ + package_macos_arm64 \ + package_macos_x86_64 \ + package_android \ + package_android_x86_64 \ + package_ios \ + package_ios_simulator \ + depends/gmp/package \ + depends/gmp/package_macos_arm64 \ + depends/gmp/package_macos_x86_64 \ + depends/gmp/package_android_arm64 \ + depends/gmp/package_android_x86_64 \ + depends/gmp/package_ios_arm64 \ + depends/gmp/package_iphone_simulator + diff --git a/.github/resources/prover/windows.Makefile b/.github/resources/prover/windows.Makefile index 8bc079c..83d60e6 100644 --- a/.github/resources/prover/windows.Makefile +++ b/.github/resources/prover/windows.Makefile @@ -13,11 +13,11 @@ host: host_windows_x86_64: rm -rf build_prover && mkdir build_prover && cd build_prover && \ cmake .. \ - -DUSE_ASM=OFF \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=../package \ - -DCMAKE_EXE_LINKER_FLAGS="-L/lib -lmman" \ - -DCMAKE_CXX_FLAGS="-I/include -include mman_patch.hpp -include cstdint -Duint=unsigned\ int -Du_int32_t=uint32_t -Du_int64_t=uint64_t" && \ + -DUSE_ASM=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=../package \ + -DCMAKE_EXE_LINKER_FLAGS="-static -static-libstdc++ -static-libgcc -L/lib -lmman" \ + -DCMAKE_CXX_FLAGS="-I/include -include mman_patch.hpp -include cstdint -Duint=unsigned\ int -Du_int32_t=uint32_t -Du_int64_t=uint64_t" && \ make -j$(nproc) -vvv && make install host_noasm: diff --git a/.github/resources/witness-generator/linux.Makefile b/.github/resources/witness-generator/linux.Makefile new file mode 100644 index 0000000..3c01dcc --- /dev/null +++ b/.github/resources/witness-generator/linux.Makefile @@ -0,0 +1,18 @@ +CC=g++ +CFLAGS=-std=c++11 -O3 -I. +LDFLAGS = -lgmp -static + +DEPS_HPP = circom.hpp calcwit.hpp fr.hpp pol.cpp +DEPS_O = main.o calcwit.o fr.o pol.o + +all: pol + +%.o: %.cpp $(DEPS_HPP) + $(CC) -Wno-address-of-packed-member -c $< $(CFLAGS) -o $@ + +pol: $(DEPS_O) + $(CC) -o pol $(DEPS_O) $(LDFLAGS) + +clean: + rm -f *.o pol + diff --git a/.github/resources/witness-generator/macos.Makefile b/.github/resources/witness-generator/macos.Makefile index 6bca6a3..926a92d 100644 --- a/.github/resources/witness-generator/macos.Makefile +++ b/.github/resources/witness-generator/macos.Makefile @@ -1,6 +1,7 @@ CC = g++ -CFLAGS = -std=c++11 -O3 -I. -I/opt/homebrew/include -include gmp_patch.hpp -LDFLAGS = -L/opt/homebrew/lib -lgmp +CFLAGS = -std=c++11 -O3 -I. -I/opt/homebrew/include -include gmp_patch.hpp -Wno-address-of-packed-member +LDFLAGS = -Wl,-search_paths_first -Wl,-dead_strip -L/opt/homebrew/lib +LDLIBS = /opt/homebrew/lib/libgmp.a DEPS_HPP = circom.hpp calcwit.hpp fr.hpp pol.cpp DEPS_O = main.o calcwit.o fr.o pol.o @@ -8,10 +9,11 @@ DEPS_O = main.o calcwit.o fr.o pol.o all: pol %.o: %.cpp $(DEPS_HPP) - $(CC) -Wno-address-of-packed-member -c $< $(CFLAGS) -o $@ + $(CC) $(CFLAGS) -c $< -o $@ pol: $(DEPS_O) - $(CC) -o pol $(DEPS_O) $(LDFLAGS) + $(CC) -o $@ $(DEPS_O) $(LDFLAGS) $(LDLIBS) clean: rm -f *.o pol + diff --git a/.github/resources/witness-generator/windows.Makefile b/.github/resources/witness-generator/windows.Makefile index 0aceb95..2f0c7e7 100644 --- a/.github/resources/witness-generator/windows.Makefile +++ b/.github/resources/witness-generator/windows.Makefile @@ -1,6 +1,6 @@ CC = g++ CFLAGS = -std=c++11 -O3 -I. -I/include -Duint="unsigned int" -LDFLAGS = -L/lib -lgmp -lmman +LDFLAGS = -L/lib -lgmp -lmman -static DEPS_HPP = circom.hpp calcwit.hpp fr.hpp pol.cpp DEPS_O = main.o calcwit.o fr.o pol.o diff --git a/.github/workflows/build-circuits.yml b/.github/workflows/build-circuits.yml index 1e3b50b..7bedc16 100644 --- a/.github/workflows/build-circuits.yml +++ b/.github/workflows/build-circuits.yml @@ -84,16 +84,28 @@ jobs: - name: Install Dependencies [Witness Generator] run: sudo apt install nlohmann-json3-dev + - name: Replace Prover Makefile # TODO: Make a fork generate the appropriate Linux Makefile + working-directory: repo + run: cp .github/resources/prover/${{ env.OS }}.Makefile circom_circuits/rapidsnark/Makefile + - name: Compile Prover and Verifier working-directory: repo/circom_circuits/rapidsnark run: | ./build_gmp.sh host - make host + GOMP_A="$(g++ --print-file-name=libgomp.a)" + + PKG_CONFIG_STATIC=1 \ + LDFLAGS= \ + make host EXTRA_CMAKE_FLAGS='-DCMAKE_EXE_LINKER_FLAGS="-static -static-libstdc++ -static-libgcc -no-pie" -DOpenMP_gomp_LIBRARY='"$GOMP_A -DCMAKE_PREFIX_PATH=depends/gmp/package" - name: Generate Witness Generator's C++ Circuit working-directory: repo/circom_circuits/Mantle run: circom --c --r1cs --no_asm --O2 pol.circom + - name: Replace Witness Generator Makefile # TODO: Make a fork generate the appropriate Linux Makefile + working-directory: repo + run: cp .github/resources/witness-generator/${{ env.OS }}.Makefile circom_circuits/Mantle/pol_cpp/Makefile + - name: Compile Witness Generator working-directory: repo/circom_circuits/Mantle/pol_cpp run: make pol @@ -244,7 +256,7 @@ jobs: working-directory: repo run: cp .github/resources/prover/${{ env.OS }}.build_gmp.sh circom_circuits/rapidsnark/build_gmp.sh - - name: Compile Prover + - name: Compile Prover and Verifier shell: msys2 {0} working-directory: repo/circom_circuits/rapidsnark run: | @@ -276,10 +288,6 @@ jobs: BUNDLE_DIR="${BUNDLE_NAME}/${BINARY_NAME}" mkdir -p "$BUNDLE_DIR" - MINGW_BASE_DIR="/${MSYSTEM,,}" - MINGW_DLL_DIR="$MINGW_BASE_DIR/bin" - cp "$MINGW_DLL_DIR"/{libgcc_s_seh-1.dll,libwinpthread-1.dll,libstdc++-6.dll} "$BUNDLE_DIR/" - mv "${RAPIDSNARK_DIR}"/bin/${BINARY_NAME}.exe "$BUNDLE_DIR/" tar -czf "${BUNDLE_NAME}.tar.gz" "${BUNDLE_NAME}" @@ -295,10 +303,6 @@ jobs: BUNDLE_DIR="${BUNDLE_NAME}/${BINARY_NAME}" mkdir -p "$BUNDLE_DIR" - MINGW_BASE_DIR="/${MSYSTEM,,}" - MINGW_DLL_DIR="$MINGW_BASE_DIR/bin" - cp "$MINGW_DLL_DIR"/{libgcc_s_seh-1.dll,libwinpthread-1.dll,libstdc++-6.dll} "$BUNDLE_DIR/" - mv "${RAPIDSNARK_DIR}"/bin/${BINARY_NAME}.exe "$BUNDLE_DIR/" tar -czf "${BUNDLE_NAME}.tar.gz" "${BUNDLE_NAME}" @@ -312,10 +316,6 @@ jobs: run: | BUNDLE_DIR="${BUNDLE_NAME}/witness-generator" mkdir -p "$BUNDLE_DIR" - - MINGW_BASE_DIR="/${MSYSTEM,,}" - MINGW_DLL_DIR="$MINGW_BASE_DIR/bin" - cp "$MINGW_DLL_DIR"/{libgcc_s_seh-1.dll,libwinpthread-1.dll,libstdc++-6.dll,libgmp-10.dll} "$BUNDLE_DIR/" mv "${WITNESS_GENERATOR_DIR}/pol" "$BUNDLE_DIR/pol.exe" mv "${WITNESS_GENERATOR_DIR}/pol.dat" "$BUNDLE_DIR/pol.exe.dat" @@ -380,9 +380,9 @@ jobs: run: brew install nlohmann-json - name: Install Dependencies [Prover] - run: brew install cmake nasm m4 + run: brew install nasm m4 - - name: Compile Prover + - name: Compile Prover and Verifier working-directory: repo/circom_circuits/rapidsnark run: | ./build_gmp.sh macos_arm64