Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,52 @@ include(cmake/platform.cmake)

set(USE_ASM ON CACHE BOOL "Use asm implementation for Fr and Fq")
set(USE_OPENMP ON CACHE BOOL "Use OpenMP")
set(USE_MARCH_NATIVE OFF CACHE BOOL "Use -march=native for host builds (host-specific; not portable across CPUs)")
set(USE_LTO OFF CACHE BOOL "Enable Link Time Optimization (LTO)")

project(rapidsnark LANGUAGES CXX C ASM)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Host-only optimization flags
if (USE_MARCH_NATIVE AND NOT CMAKE_CROSSCOMPILING)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -mtune=native")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -mtune=native")
message("Using -march=native -mtune=native")
endif()

if (USE_LTO)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
message("LTO enabled")
endif()

# Profile-Guided Optimization (host-only, two-phase). PGO=generate builds an
# instrumented binary that writes .gcda profiles to PGO_DIR during a training
# run; PGO=use rebuilds using those profiles. Flags go on both compile and link.
# Only the C++/C glue benefits (the field arithmetic is hand-written NASM).
set(PGO "" CACHE STRING "Profile-guided optimization mode: generate | use | (empty=off)")
set(PGO_DIR "${CMAKE_BINARY_DIR}/pgo-data" CACHE PATH "Directory for PGO .gcda profile data")

if (PGO STREQUAL "generate")
set(PGO_FLAGS "-fprofile-generate=${PGO_DIR}")
message("PGO: instrumenting (profiles -> ${PGO_DIR})")
elseif (PGO STREQUAL "use")
# -fprofile-correction tolerates the multithreaded prover's racy counters;
# -Wno-missing-profile keeps functions with no profile from erroring.
set(PGO_FLAGS "-fprofile-use=${PGO_DIR} -fprofile-correction -Wno-missing-profile")
message("PGO: using profiles from ${PGO_DIR}")
elseif (NOT PGO STREQUAL "")
message(FATAL_ERROR "PGO must be 'generate', 'use', or empty (got '${PGO}')")
endif()

if (PGO_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PGO_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PGO_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PGO_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PGO_FLAGS}")
endif()

message("BITS_PER_CHUNK=" ${BITS_PER_CHUNK})
message("USE_ASM=" ${USE_ASM})
message("USE_OPENMP=" ${USE_OPENMP})
Expand Down
83 changes: 82 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
###

#Build targets
# Build targets
host:
rm -rf build_prover && mkdir build_prover && cd build_prover && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package && \
Expand All @@ -11,6 +11,44 @@ host_noasm:
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_noasm -DUSE_ASM=NO && \
make -j$(nproc) -vvv && make install

host_march:
rm -rf build_prover_march && mkdir build_prover_march && cd build_prover_march && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_march -DUSE_MARCH_NATIVE=ON && \
make -j$(nproc) -vvv && make install

host_lto:
rm -rf build_prover_lto && mkdir build_prover_lto && cd build_prover_lto && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_lto -DUSE_LTO=ON && \
make -j$(nproc) -vvv && make install

host_march_lto:
rm -rf build_prover_march_lto && mkdir build_prover_march_lto && cd build_prover_march_lto && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_march_lto -DUSE_MARCH_NATIVE=ON -DUSE_LTO=ON && \
make -j$(nproc) -vvv && make install

# Profile-Guided Optimization on top of LTO. Two-phase: build an instrumented
# prover, run it on a TRAINING circuit to collect profiles, then rebuild using
# them. The training circuit should resemble your production workload -- a tiny
# circuit produces a profile that can pessimize large-circuit proving. Override:
# make host_pgo PGO_ZKEY=/path/circuit_final.zkey PGO_WTNS=/path/witness.wtns
# Multiple training runs accumulate (.gcda counters sum), improving coverage.
PGO_ZKEY ?= testdata/circuit_final.zkey
PGO_WTNS ?= testdata/witness.wtns
host_pgo:
rm -rf build_prover_pgo && mkdir build_prover_pgo
@echo "=== PGO phase 1/2: instrumented build (LTO on) ==="
cd build_prover_pgo && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_pgo \
-DUSE_LTO=ON -DPGO=generate && \
make -j$(nproc)
@echo "=== PGO training run on $(PGO_ZKEY) ==="
./build_prover_pgo/src/prover $(PGO_ZKEY) $(PGO_WTNS) /tmp/pgo_train_proof.json /tmp/pgo_train_public.json
@echo "=== PGO phase 2/2: rebuild using collected profiles ==="
cd build_prover_pgo && \
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_pgo \
-DUSE_LTO=ON -DPGO=use && \
make -j$(nproc) && 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 && \
Expand All @@ -21,6 +59,14 @@ 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

# LTO variant. On arm64 only the innermost limb multiply is ASM; the rest of the
# field layer plus curve/FFT/MSM glue is C++, so LTO's cross-TU inlining has more
# to work on than on x86. Portable (unlike -march=native). Slower/heavier link.
android_lto:
rm -rf build_prover_android_lto && mkdir build_prover_android_lto && cd build_prover_android_lto && \
cmake .. -DTARGET_PLATFORM=ANDROID -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android_lto -DBUILD_TESTS=OFF -DUSE_OPENMP=OFF -DUSE_LTO=ON && \
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 && \
Expand All @@ -31,6 +77,13 @@ 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

# LTO variant for x86_64 Android (emulator / x86 devices). Same portable LTO
# path as android_lto; useful for benchmarking LTO on the emulator.
android_x86_64_lto:
rm -rf build_prover_android_x86_64_lto && mkdir build_prover_android_x86_64_lto && cd build_prover_android_x86_64_lto && \
cmake .. -DTARGET_PLATFORM=ANDROID_x86_64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android_x86_64_lto -DBUILD_TESTS=OFF -DUSE_OPENMP=OFF -DUSE_LTO=ON && \
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 && \
Expand All @@ -45,6 +98,18 @@ ios:
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 ""

# LTO variant for iOS. -DUSE_LTO=ON maps to the LLVM_LTO Xcode setting via
# CMAKE_INTERPROCEDURAL_OPTIMIZATION. Same rationale as android_lto (mostly-C++
# field/curve layer on arm64). Portable; slower/heavier link.
ios_lto:
@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_lto && mkdir build_prover_ios_lto && cd build_prover_ios_lto && \
cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios_lto -DUSE_LTO=ON && \
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 LTO artifacts built in build_prover_ios_lto/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 && \
Expand All @@ -68,18 +133,34 @@ macos_x86_64:

clean:
rm -rf build_prover \
build_prover_noasm \
build_prover_lto \
build_prover_march \
build_prover_march_lto \
build_prover_pgo \
build_prover_macos_arm64 \
build_prover_macos_x86_64 \
build_prover_android \
build_prover_android_lto \
build_prover_android_x86_64 \
build_prover_android_x86_64_lto \
build_prover_ios \
build_prover_ios_lto \
build_prover_ios_simulator \
package \
package_noasm \
package_lto \
package_march \
package_march_lto \
package_pgo \
package_macos_arm64 \
package_macos_x86_64 \
package_android \
package_android_lto \
package_android_x86_64 \
package_android_x86_64_lto \
package_ios \
package_ios_lto \
package_ios_simulator \
depends/gmp/package \
depends/gmp/package_macos_arm64 \
Expand Down
2 changes: 1 addition & 1 deletion depends/ffiasm
Submodule ffiasm updated 4 files
+84 −0 c/fft.cpp
+19 −0 c/fft.hpp
+534 −58 c/msm.cpp
+189 −23 c/msm.hpp
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ if(BUILD_TESTS)
target_link_libraries(test_public_size rapidsnarkStaticFrFq pthread)
add_test(NAME test_public_size COMMAND test_public_size circuit_final.zkey 86
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/testdata)

add_executable(test_msm test_msm.cpp)
target_link_libraries(test_msm rapidsnarkStaticFrFq pthread)
add_test(NAME test_msm COMMAND test_msm)
endif()

if(OpenMP_CXX_FOUND)
Expand All @@ -156,6 +160,9 @@ if(OpenMP_CXX_FOUND)
target_link_libraries(prover OpenMP::OpenMP_CXX)
target_link_libraries(verifier OpenMP::OpenMP_CXX)
target_link_libraries(test_public_size OpenMP::OpenMP_CXX)
if(BUILD_TESTS)
target_link_libraries(test_msm OpenMP::OpenMP_CXX)
endif()
endif()

endif()
Expand Down
1 change: 1 addition & 0 deletions src/binfile_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <map>
#include <vector>
#include <memory>
#include <cstdint>
#include "fileloader.hpp"

namespace BinFileUtils {
Expand Down
Loading
Loading