-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
405 lines (314 loc) · 12 KB
/
Makefile
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#
# Copyright 2019 Paul Dworzanski et al.
#
# This file is part of c_ewasm_contracts.
#
# c_ewasm_contracts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# c_ewasm_contracts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with c_ewasm_contracts. If not, see <https://www.gnu.org/licenses/>.
#
############################################################
# variables, can be passed as command-line arguments to make
# the c file to compile, without the ".c"
export PROJECT := blake2b_ref
# directory of the c files
export SRC_DIR := src/
# paths to tools
export LLVM_DIR := llvm-project/build/bin/
export WABT_DIR := wabt/build/
export BINARYEN_DIR := binaryen/build/bin/
export SCOUT_DIR :=
#export LLVM_DIR :=
#export WABT_DIR :=
#export BINARYEN_DIR :=
# compiler options
export OPTIMIZATION_CLANG := -O3 #-Oz, -Os, -O0, -O1, -O2, or -O3
export OPTIMIZATION_OPT := -O3 #-Oz, -Os, -O0, -O1, -O2, or -O3
export OPTIMIZATION_LLC := -O3 # -O0, -O1, -O2, or -O3
export OPTIMIZATION_WASM_LD := -O3 # -O0, -O1, or -O2 # this has to do with string merging, don't think it affects wasm
export OPTIMIZATION_BINARYEN := -O3 #-Oz, -Os, -O0, -O1, -O2, or -O3
export DEFINES :=
#######################################################
# The main recipe, does all compilation/transformations
default: project
# This is the main recipe to build, optimize, convert
project:
# compile
$(LLVM_DIR)/clang -cc1 ${OPTIMIZATION_CLANG} ${CFLAGS} -emit-llvm -triple=wasm32-unknown-unknown-wasm ${SRC_DIR}${PROJECT}.c -o ${PROJECT}.ll
$(LLVM_DIR)/opt ${OPTIMIZATION_OPT} ${PROJECT}.ll -o ${PROJECT}.ll
$(LLVM_DIR)/llc ${OPTIMIZATION_LLC} -filetype=obj ${PROJECT}.ll -o ${PROJECT}.o
$(LLVM_DIR)/wasm-ld $(OPTIMIZATION_WASM_LD) ${PROJECT}.o -o ${PROJECT}.wasm --no-entry -allow-undefined-file=src/ewasm.syms -export=_main lib/wasi/libclang_rt.builtins-wasm32.a #--stack-first -z stack-size=10000
# done compiling, optimize with Wasm-specific optimizer
$(BINARYEN_DIR)/wasm-opt ${OPTIMIZATION_BINARYEN} ${PROJECT}.wasm -o ${PROJECT}.wasm -g #-g keeps function names
# hack so that we export "main" instead of "_main"
$(WABT_DIR)/wasm2wat ${PROJECT}.wasm > ${PROJECT}.wat
sed -i -e 's/(export "_main" (func $$_main))/(export "main" (func $$_main))/g' ${PROJECT}.wat
$(WABT_DIR)/wat2wasm ${PROJECT}.wat > ${PROJECT}.wasm
# save files
mv $(PROJECT).wasm wasm/$(PROJECT).wasm
mv $(PROJECT).wat wat/$(PROJECT).wat
# remove intermediate files
rm -f $(PROJECT).ll $(PROJECT).o
########################################################
# Build individual projects, calls the main recipe above
# Add your project here, or copy/paste commands in recipe above
blake2b: blake2b_floodyberry blake2b_mjosref blake2b_openssl blake2b_ref blake2b_ref_small
blake2b_floodyberry: src/blake2b_floodyberry.c
make project PROJECT=blake2b_floodyberry \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
blake2b_mjosref: src/blake2b_mjosref.c
make project PROJECT=blake2b_mjosref \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
# funny enough, these speed optimization flags produced the smallest wasm, there were some ties
blake2b_openssl: src/blake2b_openssl.c
make project PROJECT=blake2b_openssl \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
blake2b_ref: src/blake2b_ref.c
make project PROJECT=blake2b_ref \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
blake2b_ref_small: src/blake2b_ref_small.c
make project PROJECT=blake2b_ref_small \
OPTIMIZATION_CLANG=-Os \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
# these optimization flags produced the smallest wasm, there were some ties
helloworld: src/helloworld.c
make project PROJECT=helloworld \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256: keccak256_ref keccak256_ref_readable_and_compact keccak256_rhash keccak256_libkeccak-tiny keccak256_libkeccak-tiny-unrolled keccak256_openssl
keccak256_ref: src/keccak256_ref.c
make project PROJECT=keccak256_ref \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O0
keccak256_ref_readable_and_compact: src/keccak256_ref_readable_and_compact.c
make project PROJECT=keccak256_ref_readable_and_compact \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256_rhash: src/keccak256_rhash.c
make project PROJECT=keccak256_rhash \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256_openssl: src/keccak256_openssl.c
make project PROJECT=keccak256_openssl \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256_libkeccak-tiny: src/keccak256_libkeccak-tiny.c
make project PROJECT=keccak256_libkeccak-tiny \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256_libkeccak-tiny-unrolled: src/keccak256_libkeccak-tiny-unrolled.c
make project PROJECT=keccak256_libkeccak-tiny-unrolled \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
keccak256_rhash_init_update_final: src/keccak256_rhash_init_update_final.c
make project PROJECT=keccak256_rhash_init_update_final \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
ecrecover: ecrecover_libsecp256k1 ecrecover_trezor
ecrecover_libsecp256k1: src/ecrecover_libsecp256k1_untested.c
make project PROJECT=ecrecover_libsecp256k1_untested \
OPTIMIZATION_CLANG=-O1 \
OPTIMIZATION_OPT=-O1 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
# larger optimizations result in a runtime error
ecrecover_trezor: src/ecrecover_trezor_untested.c
make project PROJECT=ecrecover_trezor_untested \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
# LLC must be -O0, otherwise runtime error at ecdsa_validate_pubkey()
ecrecover_libsecp256k1_broken: src/ecrecover_libsecp256k1_broken.c
make project PROJECT=ecrecover_libsecp256k1_broken \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
sha256: sha256_bcon sha256_nacl sha256_rhash sha256_mbedtls sha256_trezor
sha256_bcon: src/sha256_bcon.c
make project PROJECT=sha256_bcon \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha256_mbedtls: src/sha256_mbedtls.c
make project PROJECT=sha256_mbedtls\
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha256_nacl: src/sha256_nacl.c
make project PROJECT=sha256_nacl \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha256_rhash: src/sha256_rhash.c
make project PROJECT=sha256_rhash \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha256_trezor: src/sha256_trezor.c
make project PROJECT=sha256_trezor \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha1: sha1_bcon sha1_bcon_small sha1_ref sha1_ref_small sha1_rhash sha1_rhash_small
sha1_bcon: src/sha1_bcon.c
make project PROJECT=sha1_bcon \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha1_bcon_small: src/sha1_bcon_small.c
make project PROJECT=sha1_bcon_small \
OPTIMIZATION_CLANG=-Oz \
OPTIMIZATION_OPT=-Oz \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-Oz
sha1_ref: src/sha1_ref.c
make project PROJECT=sha1_ref \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha1_ref_small: src/sha1_ref_small.c
make project PROJECT=sha1_ref_small \
OPTIMIZATION_CLANG=-Oz \
OPTIMIZATION_OPT=-Oz \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-Oz
sha1_rhash: src/sha1_rhash.c
make project PROJECT=sha1_rhash \
OPTIMIZATION_CLANG=-O3 \
OPTIMIZATION_OPT=-O3 \
OPTIMIZATION_LLC=-O3 \
OPTIMIZATION_WASM_LD=-O3 \
OPTIMIZATION_BINARYEN=-O3
sha1_rhash_small: src/sha1_rhash_small.c
make project PROJECT=sha1_rhash_small \
OPTIMIZATION_CLANG=-Oz \
OPTIMIZATION_OPT=-Oz \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-Oz
mul256_: mul256_import_64000 mul256
mul256: src/mul256.c
make project PROJECT=mul256 \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
mul256_import_64000: src/mul256_import_640000.c
make project PROJECT=mul256_import_640000 \
CFLAGS=-DBIGINT=1 \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
ed25519verify: ed25519verify_tweetnacl ed25519verify_floodyberry ed25519verify_ref10
ed25519verify_tweetnacl: src/ed25519verify_tweetnacl_untested.c
make project PROJECT=ed25519verify_tweetnacl_untested \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
ed25519verify_floodyberry: src/ed25519verify_floodyberry_untested.c
make project PROJECT=ed25519verify_floodyberry_untested \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
ed25519verify_ref10: src/ed25519verify_ref10_untested.c
make project PROJECT=ed25519verify_ref10_untested \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
polynomial_evaluation_32bit: src/polynomial_evaluation_32bit.c
make project PROJECT=polynomial_evaluation_32bit \
OPTIMIZATION_CLANG=-O0 \
OPTIMIZATION_OPT=-O0 \
OPTIMIZATION_LLC=-O0 \
OPTIMIZATION_WASM_LD=-O0 \
OPTIMIZATION_BINARYEN=-O0
all: helloworld blake2b keccak256 sha1 sha256 ed25519verify ecrecover polynomial_evaluation_32bit mul256_
#############
# Scout tests
test-helloworld:
cd wasm; ../${SCOUT_DIR}scout.exec ../tests/helloworld.yaml
clean:
rm -f *.ll *.o *.wasm *.wat
.PHONY: default all clean