aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/bls-signatures/build.sh
blob: 2ba7f97ed2ca1c6bcc08d47ae9b8290a4881a50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash -eu
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_NO_OPENSSL -D_LIBCPP_DEBUG=1"
if [[ "$SANITIZER" = "memory" ]]
then
    export CXXFLAGS="$CXXFLAGS -DMSAN"
fi
export LIBFUZZER_LINK="$LIB_FUZZING_ENGINE"
export LINK_FLAGS=""

# Install Boost headers
cd $SRC/
tar jxf boost_1_74_0.tar.bz2
cd boost_1_74_0/
CFLAGS="" CXXFLAGS="" ./bootstrap.sh
CFLAGS="" CXXFLAGS="" ./b2 headers
cp -R boost/ /usr/include/

# Configure Cryptofuzz
cd $SRC/cryptofuzz/
python gen_repository.py
rm extra_options.h
echo -n '"' >>extra_options.h
echo -n "--force-module=blst " >>extra_options.h
echo -n "--operations=" >>extra_options.h
echo -n "BignumCalc," >>extra_options.h
echo -n "BignumCalc_Fp2," >>extra_options.h
echo -n "BignumCalc_Fp12," >>extra_options.h
echo -n "BLS_BatchVerify," >>extra_options.h
echo -n "BLS_FinalExp," >>extra_options.h
echo -n "BLS_GenerateKeyPair," >>extra_options.h
echo -n "BLS_HashToG1," >>extra_options.h
echo -n "BLS_HashToG2," >>extra_options.h
echo -n "BLS_IsG1OnCurve," >>extra_options.h
echo -n "BLS_IsG2OnCurve," >>extra_options.h
echo -n "BLS_Pairing," >>extra_options.h
echo -n "BLS_PrivateToPublic," >>extra_options.h
echo -n "BLS_PrivateToPublic_G2," >>extra_options.h
echo -n "BLS_Sign," >>extra_options.h
echo -n "BLS_Verify," >>extra_options.h
echo -n "BLS_Compress_G1," >>extra_options.h
echo -n "BLS_Compress_G2," >>extra_options.h
echo -n "BLS_Decompress_G1," >>extra_options.h
echo -n "BLS_Decompress_G2," >>extra_options.h
echo -n "BLS_G1_Add," >>extra_options.h
echo -n "BLS_G1_Mul," >>extra_options.h
echo -n "BLS_G1_IsEq," >>extra_options.h
echo -n "BLS_G1_Neg," >>extra_options.h
echo -n "BLS_G2_Add," >>extra_options.h
echo -n "BLS_G2_Mul," >>extra_options.h
echo -n "BLS_G2_IsEq," >>extra_options.h
echo -n "BLS_G2_Neg," >>extra_options.h
echo -n "BLS_Aggregate_G1", >>extra_options.h
echo -n "BLS_Aggregate_G2", >>extra_options.h
echo -n "BignumCalc_Mod_BLS12_381_P," >>extra_options.h
echo -n "BignumCalc_Mod_BLS12_381_R," >>extra_options.h
echo -n "KDF_HKDF," >>extra_options.h
echo -n "Misc " >>extra_options.h
echo -n "--digests=SHA256 " >>extra_options.h
echo -n '"' >>extra_options.h


if [[ $CFLAGS = *-m32* ]]
then
    # Build and install libgmp
    cd $SRC/
    mkdir $SRC/libgmp-install
    tar xf gmp-6.2.1.tar.lz
    cd $SRC/gmp-6.2.1/
    autoreconf -ivf
    if [[ $CFLAGS != *-m32* ]]
    then
        ./configure --prefix="$SRC/libgmp-install/" --enable-cxx
    else
        setarch i386 ./configure --prefix="$SRC/libgmp-install/" --enable-cxx
    fi
    make -j$(nproc)
    make install
    export CXXFLAGS="$CXXFLAGS -I $SRC/libgmp-install/include/"
fi

function build_blst() {
    if [[ "$SANITIZER" == "memory" ]]
    then
        # Patch to disable assembly
        touch new_no_asm.h
        echo "#if LIMB_T_BITS==32" >>new_no_asm.h
        echo "typedef unsigned long long llimb_t;" >>new_no_asm.h
        echo "#else" >>new_no_asm.h
        echo "typedef __uint128_t llimb_t;" >>new_no_asm.h
        echo "#endif" >>new_no_asm.h
        cat src/no_asm.h >>new_no_asm.h
        mv new_no_asm.h src/no_asm.h

        CFLAGS="$CFLAGS -D__BLST_NO_ASM__ -D__BLST_PORTABLE__" ./build.sh
    else
        ./build.sh
    fi

    export BLST_LIBBLST_A_PATH=$(realpath libblst.a)
    export BLST_INCLUDE_PATH=$(realpath bindings/)
    export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_BLST"
}

# Build blst (normal)
cp -R $SRC/blst/ $SRC/blst_normal/
cd $SRC/blst_normal/
build_blst

# Build Chia
if [[ $CFLAGS != *sanitize=memory* && $CFLAGS != *-m32* ]]
then
    # Build and install libsodium
    cd $SRC/
    mkdir $SRC/libsodium-install
    tar zxf libsodium-1.0.18-stable.tar.gz
    cd $SRC/libsodium-stable/
    autoreconf -ivf
    ./configure --prefix="$SRC/libsodium-install/"
    make -j$(nproc)
    make install
    export CXXFLAGS="$CXXFLAGS -I $SRC/libsodium-install/include/"
    export LINK_FLAGS="$LINK_FLAGS $SRC/libsodium-install/lib/libsodium.a"

    cd $SRC/bls-signatures/
    mkdir build/
    cd build/
    if [[ $CFLAGS = *-m32* ]]
    then
        export CHIA_ARCH="X86"
    else
        export CHIA_ARCH="X64"
    fi
    cmake .. -DBUILD_BLS_PYTHON_BINDINGS=0 -DBUILD_BLS_TESTS=0 -DBUILD_BLS_BENCHMARKS=0 -DARCH=$CHIA_ARCH
    make -j$(nproc)
    export CHIA_BLS_LIBBLS_A_PATH=$(realpath src/libbls.a)
    export CHIA_BLS_LIBRELIC_S_A_PATH=$(realpath _deps/relic-build/lib/librelic_s.a)
    export CHIA_BLS_LIBSODIUM_A_PATH=$(realpath _deps/sodium-build/libsodium.a)
    export CHIA_BLS_INCLUDE_PATH=$(realpath ../src/)
    export CHIA_BLS_RELIC_INCLUDE_PATH_1=$(realpath _deps/relic-build/include/)
    export CHIA_BLS_RELIC_INCLUDE_PATH_2=$(realpath _deps/relic-src/include/)
    export LINK_FLAGS="$LINK_FLAGS -lgmp"
    export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_CHIA_BLS"
fi

# Build mcl
if [[ "$SANITIZER" != "memory" ]]
then
    cd $SRC/mcl/
    mkdir build/
    cd build/
    if [[ $CFLAGS != *-m32* ]]
    then
        cmake .. -DMCL_STATIC_LIB=on
        export LINK_FLAGS="$LINK_FLAGS -lgmp"
    else
        cmake .. -DMCL_STATIC_LIB=on \
        -DGMP_INCLUDE_DIR="$SRC/libgmp-install/include/" \
        -DGMP_LIBRARY="$SRC/libgmp-install/lib/libgmp.a" \
        -DGMP_GMPXX_INCLUDE_DIR="$SRC/libgmp-install/include/" \
        -DGMP_GMPXX_LIBRARY="$SRC/libgmp-install/lib/libgmpxx.a" \
        -DMCL_USE_ASM=off
        export LINK_FLAGS="$LINK_FLAGS $SRC/libgmp-install/lib/libgmp.a"
    fi
    make
    export MCL_INCLUDE_PATH=$(realpath ../include/)
    export MCL_LIBMCL_A_PATH=$(realpath lib/libmcl.a)
    export MCL_LIBMCLBN384_A_PATH=$(realpath lib/libmclbn384.a)
    export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_MCL"
fi

# Build Botan
cd $SRC/botan/
if [[ $CFLAGS != *-m32* ]]
then
    ./configure.py --cc-bin=$CXX \
    --cc-abi-flags="$CXXFLAGS" \
    --disable-shared \
    --disable-modules=locking_allocator,x509,tls \
    --build-targets=static \
    --without-documentation
else
    ./configure.py --cpu=x86_32 \
    --cc-bin=$CXX \
    --cc-abi-flags="$CXXFLAGS" \
    --disable-shared \
    --disable-modules=locking_allocator,x509,tls \
    --build-targets=static \
    --without-documentation
fi
make -j$(nproc)

export CXXFLAGS="$CXXFLAGS -DCRYPTOFUZZ_BOTAN -DCRYPTOFUZZ_BOTAN_IS_ORACLE"
export LIBBOTAN_A_PATH="$SRC/botan/libbotan-3.a"
export BOTAN_INCLUDE_PATH="$SRC/botan/build/include"

# Build modules
cd $SRC/cryptofuzz/modules/botan/
make -B

cd $SRC/cryptofuzz/modules/blst/
make -B

if [[ $CFLAGS != *sanitize=memory* && $CFLAGS != *-m32* ]]
then
    cd $SRC/cryptofuzz/modules/chia_bls/
    make -B
fi

if [[ "$SANITIZER" != "memory" ]]
then
    cd $SRC/cryptofuzz/modules/mcl/
    make -B
fi

# Build Cryptofuzz
cd $SRC/cryptofuzz/
make -B -j

cp cryptofuzz $OUT/cryptofuzz-bls-signatures

# Build blst (optimized for size)
cp -R $SRC/blst/ $SRC/blst_optimize_size/
cd $SRC/blst_optimize_size/
export CFLAGS="$CFLAGS -D__OPTIMIZE_SIZE__"
build_blst

cd $SRC/cryptofuzz/modules/blst/
make -B

# Build Cryptofuzz
cd $SRC/cryptofuzz/
rm entry.o; make

cp cryptofuzz $OUT/cryptofuzz-bls-signatures_optimize_size