Makefile 12.6 KB
Newer Older
Michael Hamburg's avatar
Michael Hamburg committed
1 2 3
# Copyright (c) 2014 Cryptography Research, Inc.
# Released under the MIT License.  See LICENSE.txt for license information.

4 5 6 7

UNAME := $(shell uname)
MACHINE := $(shell uname -m)

8 9 10 11
# Subdirectories for objects etc.
# Many of them are mapped to build/obj right now, but could be split later.
# The non-build/obj directories are the public interface.
BUILD_OBJ = build/obj
12 13
BUILD_C   = build/c
BUILD_H   = build/c
14 15 16 17 18 19 20 21
BUILD_PY  = build/obj
BUILD_LIB = build/lib
BUILD_INC = build/include
BUILD_BIN = build/bin
BUILD_IBIN = build/obj/bin
BATBASE=ed448goldilocks_decaf_bats_$(TODAY)
BATNAME=build/$(BATBASE)

22
ifeq ($(UNAME),Darwin)
Michael Hamburg's avatar
Michael Hamburg committed
23
CC = clang
Mike Hamburg's avatar
Mike Hamburg committed
24
CXX = clang++
25 26
else
CC = gcc
Mike Hamburg's avatar
Mike Hamburg committed
27
CXX = g++
28 29
endif
LD = $(CC)
Mike Hamburg's avatar
Mike Hamburg committed
30
LDXX = $(CXX)
31
ASM ?= $(CC)
32

Michael Hamburg's avatar
Michael Hamburg committed
33
WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
34
	 -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
35

Michael Hamburg's avatar
Michael Hamburg committed
36 37
INCFLAGS = -Isrc/include -I$(BUILD_INC) -I$(BUILD_H)
PUB_INCFLAGS = -I$(BUILD_INC)
38
LANGFLAGS = -std=c99 -fno-strict-aliasing
Mike Hamburg's avatar
Mike Hamburg committed
39
LANGXXFLAGS = -fno-strict-aliasing
40
GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
41
OFLAGS ?= -O2
Michael Hamburg's avatar
Michael Hamburg committed
42

43 44 45 46 47
MACOSX_VERSION_MIN ?= 10.9
ifeq ($(UNAME),Darwin)
GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
endif

48 49
TODAY = $(shell date "+%Y-%m-%d")

50 51
#FIXME ARCHFLAGS
ARCHFLAGS ?= -maes -mavx2 -mbmi2 #TODO
52 53 54 55 56

ifeq ($(CC),clang)
WARNFLAGS += -Wgcc-compat
endif

Mike Hamburg's avatar
Mike Hamburg committed
57
ARCHFLAGS += $(XARCHFLAGS)
58
CFLAGS  = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
59 60
PUB_CFLAGS  = $(LANGFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS)
61
LDFLAGS = $(XLDFLAGS)
Mike Hamburg's avatar
Mike Hamburg committed
62
ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
Michael Hamburg's avatar
Michael Hamburg committed
63

64 65 66
SAGE ?= sage
SAGES= $(shell ls test/*.sage)
BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
Michael Hamburg's avatar
Michael Hamburg committed
67

68
.PHONY: clean all test test_ct bench todo doc lib bat sage sagetest gen_headers
69
.PRECIOUS: $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h $(BUILD_IBIN)/%
70

71 72 73 74
HEADER_SRCS= $(shell find src/public_include -name "*.h*")
GEN_HEADERS_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%)
GEN_HEADERS_1= $(GEN_HEADERS_0:%.tmpl.h=%.h)
GEN_HEADERS= $(GEN_HEADERS_1:%.tmpl.hxx=%.hxx)
75
HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_HEADERS)
76 77

# components needed by the lib
Michael Hamburg's avatar
Michael Hamburg committed
78
LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o # and per-field components
Michael Hamburg's avatar
Michael Hamburg committed
79

80
BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
81

82
all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
Michael Hamburg's avatar
Michael Hamburg committed
83 84 85 86 87

scan: clean
	scan-build --use-analyzer=`which clang` \
		 -enable-checker deadcode -enable-checker llvm \
		 -enable-checker osx -enable-checker security -enable-checker unix \
88 89 90 91
		make all

# Internal test programs, which are not part of the final build/bin directory.
$(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
Mike Hamburg's avatar
Mike Hamburg committed
92
ifeq ($(UNAME),Darwin)
93
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
94
else
95
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
96
endif
97

98 99 100 101 102 103 104 105
# Internal test programs, which are not part of the final build/bin directory.
$(BUILD_IBIN)/test_ct: $(BUILD_OBJ)/test_ct.o lib
ifeq ($(UNAME),Darwin)
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
else
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
endif

106
$(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
107
ifeq ($(UNAME),Darwin)
108
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
109
else
110
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
111 112
endif

113 114
# Create all the build subdirectories
$(BUILD_OBJ)/timestamp:
115
	mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
116 117
		$(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \
		$(PER_OBJ_DIRS)
Michael Hamburg's avatar
Michael Hamburg committed
118 119
	touch $@

120
gen_headers: $(GEN_HEADERS)
121 122 123 124 125 126

$(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
	cp -f $< $@
	
$(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/gen_headers/*
	python -B src/gen_headers/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
127
	
128 129
$(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/gen_headers/*
	python -B src/gen_headers/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
130

131 132 133 134
################################################################
# Per-field code: call with field, arch
################################################################
define define_field
135
ARCH_FOR_$(1) ?= $(2)
136 137
COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)/f_impl.o $$(BUILD_OBJ)/$(1)/f_arithmetic.o $$(BUILD_OBJ)/$(1)/f_generic.o
HEADERS_OF_$(1) = $(HEADERS) $$(BUILD_H)/$(1)/f_field.h
138
LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
139
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
140

141 142 143 144 145 146
$$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/gen_headers/* $(HEADERS)
	python -B src/gen_headers/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
	
$$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/gen_headers/* $(HEADERS)
	python -B src/gen_headers/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<

147
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
148 149
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
	-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
150
	-c -o $$@ $$<
151

152
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1))
153 154
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
	-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
155
	-c -o $$@ $$<
156

157
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
158
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
159 160
		-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
		-c -o $$@ $$<
161 162 163 164 165 166
endef

################################################################
# Per-field, per-curve code: call with curve, field
################################################################
define define_curve
167

168
LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
169
	 $$(BUILD_OBJ)/$(1)/crypto.o $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
170
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
171
GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/decaf_$(3).h $(BUILD_INC)/decaf/decaf_$(3).hxx \
172
		$(BUILD_INC)/decaf/crypto_$(3).h $(BUILD_INC)/decaf/crypto_$(3).hxx \
173
		$(BUILD_INC)/decaf/ed$(3).h $(BUILD_INC)/decaf/ed$(3).hxx
174 175
HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
176 177 178 179 180 181

$$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
	
$$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
182 183 184 185
	
$$(BUILD_INC)/decaf/decaf_$(3).%: src/per_curve/decaf.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
	
186
$$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
187 188
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
	
189 190 191 192 193 194
$$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
	
$$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
	
195 196
$$(BUILD_INC)/decaf/crypto_$(3).%: src/per_curve/crypto.tmpl.% src/gen_headers/* $$(HEADERS_OF_$(2))
	python -B src/gen_headers/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
197 198

$$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
199
		$$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
200 201 202
		$$(COMPONENTS_OF_$(2))
	$$(LD) $$(LDFLAGS) -o $$@ $$^

203
$$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
204 205
	./$$< > $$@ || (rm $$@; exit 1)

206 207
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
	$$(CC) $$(CFLAGS) -c -o $$@ $$< \
208
		-I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
209
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
210

211
$$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
212
	$$(CC) $$(CFLAGS) \
213
		-I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
214
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
215
		-c -o $$@ $$<
216 217 218 219 220
endef

################################################################
# call code above to generate curves and fields
$(eval $(call define_field,p25519,arch_x86_64))
221
$(eval $(call define_curve,curve25519,p25519,255))
222
$(eval $(call define_field,p448,arch_x86_64))
223
$(eval $(call define_curve,ed448goldilocks,p448,448))
224

225
# The shakesum utility is in the public bin directory.
Michael Hamburg's avatar
Michael Hamburg committed
226
$(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
227
	$(LD) $(LDFLAGS) -o $@ $^
228 229 230 231 232 233 234 235 236 237

# The main decaf library, and its symlinks.
lib: $(BUILD_LIB)/libdecaf.so

$(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
	ln -sf `basename $^` $@

$(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
	rm -f $@
ifeq ($(UNAME),Darwin)
238
	libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
239 240 241 242 243 244 245 246
		  $(LIBCOMPONENTS)
else
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
	strip --discard-all $@
endif



247 248
$(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
249
	
250 251
$(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
	$(CC) $(PUB_CFLAGS) -c -o $@ $<
Michael Hamburg's avatar
Michael Hamburg committed
252

253 254
$(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS)
	$(CXX) $(CXXFLAGS) -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
255

256
# The sage test scripts
257 258 259
sage: $(BUILDPYS)

sagetest: sage lib
260
	$(SAGE) $(BUILD_PY)/test_decaf.sage
261

262 263 264
$(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
	cp -f $(SAGES) $(BUILD_PY)/
	$(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
265
	# some sage versions compile to .sage.py
266
	for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
267 268 269 270
		 if [ -e $$f.sage.py ]; then \
		 	 mv $$f.sage.py $$f.py; \
		 fi; \
	  done
271

272 273 274
# The documentation files
$(BUILD_DOC)/timestamp:
	mkdir -p `dirname $@`
275
	touch $@
276
#
277 278
doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
	doxygen > /dev/null
279 280 281 282 283 284 285 286 287 288

# # The eBATS benchmarking script
# bat: $(BATNAME)
#
# $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
# 	rm -fr $@
# 	for prim in dh sign; do \
#           targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
# 	  (while read arch where; do \
# 	    mkdir -p $$targ/`basename $$arch`; \
289
# 	    cp include/*.h $(BUILD_C)/decaf_tables.c src/decaf.c src/decaf_crypto.c src/shake.c src/include/*.h src/bat/$$prim.c src/p448/$$where/*.c src/p448/$$where/*.h src/p448/*.c src/p448/*.h $$targ/`basename $$arch`; \
290 291 292 293 294 295 296 297 298
# 	    cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
# 	    perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h;  \
# 	    perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h;  \
# 	    done \
# 	  ) < test/batarch.map; \
# 	  echo 'Mike Hamburg' > $$targ/designers; \
# 	  echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
#         done
# 	(cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
299
	
300
# Finds todo items in .h and .c files
301
TODO_TYPES ?= HACK TODO @todo FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY
302
TODO_LOCATIONS ?= src test Makefile Doxyfile
303
todo::
304
	@(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep --color=auto -w \
Mike Hamburg's avatar
Mike Hamburg committed
305
		`echo $(TODO_TYPES) | tr ' ' '|'`
306
	@echo '============================='
Mike Hamburg's avatar
Mike Hamburg committed
307
	@(for i in $(TODO_TYPES); do \
308
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \
309
	  /bin/echo -n $$i'       ' | head -c 10; \
310
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \
311 312 313
	done)
	@echo '============================='
	@echo -n 'Total     '
314
	@(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w \
Mike Hamburg's avatar
Mike Hamburg committed
315
		`echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
Michael Hamburg's avatar
Michael Hamburg committed
316

317
bench: $(BUILD_IBIN)/bench
Michael Hamburg's avatar
Michael Hamburg committed
318
	./$<
319

320 321
test: $(BUILD_IBIN)/test
	./$<
322 323

test_ct: $(BUILD_IBIN)/test_ct
324
	# NB: you must compile with XCFLAGS=-DNDEBUG or you will get lots of extra warnings.
325
	valgrind ./$<
Mike Hamburg's avatar
Mike Hamburg committed
326
	
327
microbench: $(BUILD_IBIN)/bench
328
	./$< --micro
Michael Hamburg's avatar
Michael Hamburg committed
329 330

clean:
331
	rm -fr build