Skip to content

Commit 5f99b0d

Browse files
committed
Get rid of libomp dependency for ios builds. Add libgmp to distributions.
1 parent 5769ced commit 5f99b0d

File tree

5 files changed

+89
-85
lines changed

5 files changed

+89
-85
lines changed

.github/workflows/build.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ jobs:
1414
with:
1515
submodules: "recursive"
1616

17-
- name: look up for android SDK
18-
run: |
19-
echo $ANDROID_NDK
20-
ls /usr/local/lib/android/sdk/ndk/25.2.9519653
21-
2217
- name: install requirements
2318
uses: awalsh128/cache-apt-pkgs-action@v1
2419
with:
@@ -86,15 +81,17 @@ jobs:
8681

8782
- name: build
8883
run: |
89-
brew install libomp
9084
if [[ ! -d "depends/gmp/package_ios_arm64" ]]; then ./build_gmp.sh ios; fi
85+
if [[ ! -d "depends/gmp/package_iphone_simulator" ]]; then ./build_gmp.sh ios_simulator; fi
9186
mkdir build_prover_ios && cd build_prover_ios
9287
cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios
9388
xcodebuild -destination 'generic/platform=iOS' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj -configuration Release
89+
cp ../depends/gmp/package_ios_arm64/lib/libgmp.a src/Release-iphoneos
9490
cd ../
9591
mkdir build_prover_ios_simulator && cd build_prover_ios_simulator
9692
cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios_simulator -DUSE_ASM=NO
9793
xcodebuild -destination 'generic/platform=iOS Simulator' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj
94+
cp ../depends/gmp/package_ios_simulator/lib/libgmp.a src/Debug-iphonesimulator
9895
9996
- name: upload iOS artifacts
10097
uses: actions/upload-artifact@v3

README.md

+35-31
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,55 @@ You should have installed gcc, cmake, libsodium, and gmp (development)
88

99
In ubuntu:
1010

11-
````
11+
```
1212
sudo apt-get install build-essential cmake libgmp-dev libsodium-dev nasm curl m4
13-
````
13+
```
1414

1515
## Compile prover in standalone mode
1616

1717
### Compile prover for x86_64 host machine
1818

19-
````sh
19+
```sh
2020
git submodule init
2121
git submodule update
2222
./build_gmp.sh host
2323
mkdir build_prover && cd build_prover
2424
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package
2525
make -j4 && make install
26-
````
26+
```
2727

2828
### Compile prover for macOS arm64 host machine
2929

30-
````sh
30+
```sh
3131
git submodule init
3232
git submodule update
3333
./build_gmp.sh host_noasm
3434
mkdir build_prover && cd build_prover
3535
cmake .. -DTARGET_PLATFORM=arm64_host -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package
3636
make -j4 && make install
37-
````
37+
```
3838

3939
### Compile prover for linux arm64 host machine
4040

41-
````sh
41+
```sh
4242
git submodule init
4343
git submodule update
4444
./build_gmp.sh host
4545
mkdir build_prover && cd build_prover
4646
cmake .. -DTARGET_PLATFORM=arm64_host -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package
4747
make -j4 && make install
48-
````
48+
```
4949

5050
### Compile prover for linux arm64 machine
5151

52-
````sh
52+
```sh
5353
git submodule init
5454
git submodule update
5555
./build_gmp.sh host
5656
mkdir build_prover && cd build_prover
5757
cmake .. -DTARGET_PLATFORM=aarch64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_aarch64
5858
make -j4 && make install
59-
````
59+
```
6060

6161
### Compile prover for Android
6262

@@ -66,10 +66,10 @@ Set the value of ANDROID_NDK environment variable to the absolute path of Androi
6666

6767
Examples:
6868

69-
````sh
69+
```sh
7070
export ANDROID_NDK=/home/test/Android/Sdk/ndk/23.1.7779620 # NDK is installed by "SDK Manager" in Android Studio.
7171
export ANDROID_NDK=/home/test/android-ndk-r23b # NDK is installed as a stand-alone package.
72-
````
72+
```
7373

7474
Prerequisites if build on Ubuntu:
7575

@@ -79,82 +79,86 @@ apt-get install curl xz-utils build-essential cmake m4 nasm
7979

8080
Compilation:
8181

82-
````sh
82+
```sh
8383
git submodule init
8484
git submodule update
8585
./build_gmp.sh android
8686
mkdir build_prover_android && cd build_prover_android
8787
cmake .. -DTARGET_PLATFORM=ANDROID -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../package_android
8888
make -j4 && make install
89-
````
89+
```
9090

9191
### Compile prover for iOS
9292

9393
Install Xcode & homebrew.
9494

95-
````sh
96-
brew install libomp
95+
```sh
9796
git submodule init
9897
git submodule update
9998
./build_gmp.sh ios
10099
mkdir build_prover_ios && cd build_prover_ios
101100
cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios
102101
xcodebuild -destination 'generic/platform=iOS' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj -configuration Release
103-
````
102+
```
104103
Open generated Xcode project and compile prover.
105104

106105
## Build for iOS emulator
107106

108107
Install Xcode & homebrew.
109108

110-
````sh
111-
brew install libomp
109+
```sh
112110
git submodule init
113111
git submodule update
114-
./build_gmp.sh ios
112+
./build_gmp.sh ios_simulator
115113
mkdir build_prover_ios_simulator && cd build_prover_ios_simulator
116114
cmake .. -GXcode -DTARGET_PLATFORM=IOS -DCMAKE_INSTALL_PREFIX=../package_ios_simulator -DUSE_ASM=NO
117115
xcodebuild -destination 'generic/platform=iOS Simulator' -scheme rapidsnarkStatic -project rapidsnark.xcodeproj
118-
````
116+
```
117+
118+
Files that you need to copy to your XCode project to link against Rapidsnark:
119+
* build_prover_ios_simulator/src/Debug-iphonesimulator/librapidsnark.a
120+
* build_prover_ios_simulator/src/Debug-iphonesimulator/libfq.a
121+
* build_prover_ios_simulator/src/Debug-iphonesimulator/libfr.a
122+
* depends/gmp/package_iphone_simulator/lib/libgmp.a
119123

120124
## Building proof
121125

122126
You have a full prover compiled in the build directory.
123127

124128
So you can replace snarkjs command:
125129

126-
````sh
130+
```sh
127131
snarkjs groth16 prove <circuit.zkey> <witness.wtns> <proof.json> <public.json>
128-
````
132+
```
129133

130134
by this one
131-
````sh
135+
```sh
132136
./package/bin/prover <circuit.zkey> <witness.wtns> <proof.json> <public.json>
133-
````
137+
```
134138

135139
## Compile prover in server mode
136140

137-
````sh
141+
```sh
138142
npm install
139143
git submodule init
140144
git submodule update
141145
npx task createFieldSources
142146
npx task buildPistache
143147
npx task buildProverServer
144-
````
148+
```
145149

146150
## Launch prover in server mode
147-
````sh
151+
```sh
148152
./build/proverServer <port> <circuit1_zkey> <circuit2_zkey> ... <circuitN_zkey>
149-
````
153+
```
150154

151155
For every `circuit.circom` you have to generate with circom with --c option the `circuit_cpp` and after compilation you have to copy the executable into the `build` folder so the server can generate the witness and then the proof based on this witness.
152156
You have an example of the usage calling the server endpoints to generate the proof with Nodejs in `/tools/request.js`.
153157

154158
To test a request you should pass an `input.json` as a parameter to the request call.
155-
````sh
159+
```sh
156160
node tools/request.js <input.json> <circuit>
157-
````
161+
```
158162

159163
## Benchmark
160164

build_gmp.sh

+50-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/sh
22

3+
set -e
4+
35
NPROC=8
46
fetch_cmd=$( (type wget > /dev/null 2>&1 && echo "wget") || echo "curl -O" )
57

@@ -11,7 +13,7 @@ usage()
1113
echo " android_x86_64: build for Android x86_64"
1214
echo " aarch64: build for Linux aarch64"
1315
echo " ios: build for iOS arm64"
14-
echo " ios_x86_64: build for iOS simulator on x86_64"
16+
echo " ios_simulator: build for iPhone Simulator for arm64/x86_64 (fat binary)"
1517
echo " host: build for this host"
1618
echo " host_noasm: build for this host without asm optimizations (e.g. needed for macOS)"
1719

@@ -233,42 +235,51 @@ build_ios()
233235
cd ..
234236
}
235237

236-
build_ios_x86_64()
238+
build_ios_simulator()
237239
{
238-
PACKAGE_DIR="$GMP_DIR/package_ios_x86_64"
239-
BUILD_DIR=build_ios_x86_64
240-
241-
if [ -d "$PACKAGE_DIR" ]; then
242-
echo "iOS package is built already. See $PACKAGE_DIR"
243-
return 1
244-
fi
245-
246-
export SDK="iphonesimulator"
247-
export TARGET=x86_64-apple-darwin
248-
export MIN_IOS_VERSION=8.0
249-
250-
export ARCH_FLAGS="-arch x86_64"
251-
export OPT_FLAGS="-O3 -g3 -fembed-bitcode"
252-
export HOST_FLAGS="${ARCH_FLAGS} -miphoneos-version-min=${MIN_IOS_VERSION} -isysroot $(xcrun --sdk ${SDK} --show-sdk-path)"
253-
254-
export CC=$(xcrun --find --sdk "${SDK}" clang)
255-
export CXX=$(xcrun --find --sdk "${SDK}" clang++)
256-
export CPP=$(xcrun --find --sdk "${SDK}" cpp)
257-
export CFLAGS="${HOST_FLAGS} ${OPT_FLAGS}"
258-
export CXXFLAGS="${HOST_FLAGS} ${OPT_FLAGS}"
259-
export LDFLAGS="${HOST_FLAGS}"
260-
261-
echo $TARGET
262-
263-
rm -rf "$BUILD_DIR"
264-
mkdir "$BUILD_DIR"
265-
cd "$BUILD_DIR"
266-
267-
../configure --host $TARGET --prefix="$PACKAGE_DIR" --with-pic --disable-fft --disable-assembly &&
268-
make -j${NPROC} &&
269-
make install
270-
271-
cd ..
240+
libs=()
241+
for ARCH in "arm64" "x86_64"; do
242+
case "$ARCH" in
243+
"arm64" )
244+
echo "Building for iPhone Simulator ARM64"
245+
ARCH_FLAGS="-arch arm64 -arch arm64e"
246+
;;
247+
"x86_64" )
248+
echo "Building for iPhone Simulator x86_64"
249+
ARCH_FLAGS="-arch x86_64"
250+
;;
251+
* )
252+
echo "Incorrect iPhone Simulator arch"
253+
exit 1
254+
esac
255+
256+
BUILD_DIR="build_iphone_simulator_${ARCH}"
257+
PACKAGE_DIR="$GMP_DIR/package_iphone_simulator_${ARCH}"
258+
libs+=("${PACKAGE_DIR}/lib/libgmp.a")
259+
260+
if [ -d "$PACKAGE_DIR" ]; then
261+
echo "iPhone Simulator ${ARCH} package is built already. See $PACKAGE_DIR. Skip building this ARCH."
262+
continue
263+
fi
264+
265+
rm -rf "$BUILD_DIR"
266+
mkdir "$BUILD_DIR"
267+
cd "$BUILD_DIR"
268+
269+
../configure --prefix="${PACKAGE_DIR}" \
270+
CC="$(xcrun --sdk iphonesimulator --find clang)" \
271+
CFLAGS="-O3 -isysroot $(xcrun --sdk iphonesimulator --show-sdk-path) ${ARCH_FLAGS} -fvisibility=hidden -mios-simulator-version-min=8.0" \
272+
LDFLAGS="" \
273+
--host ${ARCH}-apple-darwin --disable-assembly --enable-static --disable-shared --with-pic &&
274+
make -j${NPROC} &&
275+
make install
276+
277+
cd ..
278+
done
279+
280+
mkdir -p "${GMP_DIR}/package_iphone_simulator/lib"
281+
lipo ${libs[@]} -create -output "${GMP_DIR}/package_iphone_simulator/lib/libgmp.a"
282+
echo "Wrote universal fat library for iPhone Simulator arm64/x86_64 to ${GMP_DIR}/package_iphone_simulator/lib/libgmp.a"
272283
}
273284

274285
if [ $# -ne 1 ]; then
@@ -292,9 +303,9 @@ case "$TARGET_PLATFORM" in
292303
build_ios
293304
;;
294305

295-
"ios_x86_64" )
296-
echo "Building for ios simulator on x86_64"
297-
build_ios_x86_64
306+
"ios_simulator" )
307+
echo "Building for iPhone Simulator"
308+
build_ios_simulator
298309
;;
299310

300311
"android" )

src/CMakeLists.txt

-8
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,6 @@ include_directories(
105105
../depends/ffiasm/c
106106
../depends/json/single_include)
107107

108-
if(APPLE)
109-
execute_process(COMMAND brew --prefix OUTPUT_VARIABLE BREW_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE)
110-
if(NOT EXISTS "${BREW_PREFIX}/opt/libomp/include")
111-
message(FATAL_ERROR "libomp is not found in ${BREW_PREFIX}/opt/libomp/include, install it with `brew install libomp`")
112-
endif()
113-
include_directories("${BREW_PREFIX}/opt/libomp/include")
114-
endif()
115-
116108
add_library(rapidsnarkStatic STATIC ${LIB_SOURCES})
117109
set_target_properties(rapidsnarkStatic PROPERTIES OUTPUT_NAME rapidsnark)
118110

0 commit comments

Comments
 (0)