You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

297 lines
9.7 KiB

  1. # Copyright (c) 2014 Cryptography Research, Inc.
  2. # Released under the MIT License. See LICENSE.txt for license information.
  3. UNAME := $(shell uname)
  4. MACHINE := $(shell uname -m)
  5. # Subdirectories for objects etc.
  6. # Many of them are mapped to build/obj right now, but could be split later.
  7. # The non-build/obj directories are the public interface.
  8. BUILD_ASM = build/obj
  9. BUILD_OBJ = build/obj
  10. BUILD_C = build/obj
  11. BUILD_PY = build/obj
  12. BUILD_LIB = build/lib
  13. BUILD_INC = build/include
  14. BUILD_BIN = build/bin
  15. BUILD_IBIN = build/obj/bin
  16. BATBASE=ed448goldilocks_decaf_bats_$(TODAY)
  17. BATNAME=build/$(BATBASE)
  18. ifeq ($(UNAME),Darwin)
  19. CC = clang
  20. CXX = clang++
  21. else
  22. CC = gcc
  23. CXX = g++
  24. endif
  25. LD = $(CC)
  26. LDXX = $(CXX)
  27. ASM ?= $(CC)
  28. ifneq (,$(findstring x86_64,$(MACHINE)))
  29. ARCH ?= arch_x86_64
  30. else
  31. # no i386 port yet
  32. ARCH ?= arch_ref32
  33. endif
  34. WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
  35. -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
  36. INCFLAGS = -Isrc/include -Ibuild/include
  37. PUB_INCFLAGS = -Ibuild/include
  38. LANGFLAGS = -std=c99 -fno-strict-aliasing
  39. LANGXXFLAGS = -fno-strict-aliasing
  40. GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
  41. OFLAGS ?= -O2
  42. MACOSX_VERSION_MIN ?= 10.9
  43. ifeq ($(UNAME),Darwin)
  44. GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
  45. endif
  46. TODAY = $(shell date "+%Y-%m-%d")
  47. ifneq (,$(findstring arm,$(MACHINE)))
  48. ifneq (,$(findstring neon,$(ARCH)))
  49. ARCHFLAGS += -mfpu=neon
  50. else
  51. ARCHFLAGS += -mfpu=vfpv3-d16
  52. endif
  53. ARCHFLAGS += -mcpu=cortex-a8 # FIXME
  54. GENFLAGS += -DN_TESTS_BASE=1000 # sooooo sloooooow
  55. else
  56. ARCHFLAGS += -maes -mavx2 -mbmi2 #TODO
  57. endif
  58. ifeq ($(CC),clang)
  59. WARNFLAGS += -Wgcc-compat
  60. endif
  61. ARCHFLAGS += $(XARCHFLAGS)
  62. CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  63. PUB_CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  64. CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS)
  65. LDFLAGS = $(XLDFLAGS)
  66. ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
  67. SAGE ?= sage
  68. SAGES= $(shell ls test/*.sage)
  69. BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
  70. .PHONY: clean all test bench todo doc lib bat sage sagetest gen_headers
  71. .PRECIOUS: $(BUILD_ASM)/%.s $(BUILD_C)/%.c $(BUILD_IBIN)/%
  72. GEN_HEADERS=\
  73. $(BUILD_INC)/decaf/decaf_255.h \
  74. $(BUILD_INC)/decaf/decaf_448.h \
  75. $(BUILD_INC)/decaf/decaf_255.hxx \
  76. $(BUILD_INC)/decaf/decaf_448.hxx \
  77. $( src/public_include/decaf/* : src/public_include = $(BUILD_INC) )
  78. HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_HEADERS)
  79. # components needed by the lib
  80. LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/decaf_crypto_ed25519.o $(BUILD_OBJ)/decaf_crypto_ed448goldilocks.o # and per-field components
  81. BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
  82. all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
  83. scan: clean
  84. scan-build --use-analyzer=`which clang` \
  85. -enable-checker deadcode -enable-checker llvm \
  86. -enable-checker osx -enable-checker security -enable-checker unix \
  87. make all
  88. # Internal test programs, which are not part of the final build/bin directory.
  89. $(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
  90. ifeq ($(UNAME),Darwin)
  91. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  92. else
  93. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  94. endif
  95. $(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
  96. ifeq ($(UNAME),Darwin)
  97. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  98. else
  99. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  100. endif
  101. # Create all the build subdirectories
  102. $(BUILD_OBJ)/timestamp:
  103. mkdir -p $(BUILD_ASM) $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
  104. $(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_INC)/decaf
  105. touch $@
  106. $(BUILD_OBJ)/%.o: $(BUILD_ASM)/%.s
  107. $(ASM) $(ASFLAGS) -c -o $@ $<
  108. gen_headers: $(GEN_HEADERS)
  109. $(GEN_HEADERS): src/gen_headers/*.py src/public_include/decaf/*
  110. python -B src/gen_headers/main.py --hpre=$(BUILD_INC) --cpre=$(BUILD_C)
  111. cp src/public_include/decaf/* $(BUILD_INC)/decaf/
  112. ################################################################
  113. # Per-field code: call with field, arch
  114. ################################################################
  115. define define_field
  116. ARCH_FOR_$(1) = $(2)
  117. COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)_impl.o $$(BUILD_OBJ)/$(1)_arithmetic.o
  118. LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
  119. $$(BUILD_ASM)/$(1)_arithmetic.s: src/$(1)/f_arithmetic.c $$(HEADERS)
  120. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$(2) -S -c -o $$@ $$<
  121. $$(BUILD_ASM)/$(1)_impl.s: src/$(1)/$(2)/f_impl.c $$(HEADERS)
  122. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$(2) -S -c -o $$@ $$<
  123. endef
  124. ################################################################
  125. # Per-field, per-curve code: call with curve, field
  126. ################################################################
  127. define define_curve
  128. $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/decaf_gen_tables_$(1).o $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/utils.o \
  129. $$(COMPONENTS_OF_$(2))
  130. $$(LD) $$(LDFLAGS) -o $$@ $$^
  131. $$(BUILD_C)/decaf_tables_$(1).c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  132. ./$$< > $$@ || (rm $$@; exit 1)
  133. $$(BUILD_ASM)/decaf_tables_$(1).s: $$(BUILD_C)/decaf_tables_$(1).c $$(HEADERS)
  134. $$(CC) $$(CFLAGS) -S -c -o $$@ $$< \
  135. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  136. $$(BUILD_ASM)/decaf_gen_tables_$(1).s: src/decaf_gen_tables.c $$(HEADERS)
  137. $$(CC) $$(CFLAGS) \
  138. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  139. -S -c -o $$@ $$<
  140. $$(BUILD_ASM)/decaf_$(1).s: src/decaf.c $$(HEADERS)
  141. $$(CC) $$(CFLAGS) \
  142. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  143. -S -c -o $$@ $$<
  144. $$(BUILD_ASM)/decaf_crypto_$(1).s: src/decaf_crypto.c $$(HEADERS)
  145. $$(CC) $$(CFLAGS) \
  146. -I src/curve_$(1)/ \
  147. -S -c -o $$@ $$<
  148. LIBCOMPONENTS += $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/decaf_tables_$(1).o
  149. endef
  150. ################################################################
  151. # call code above to generate curves and fields
  152. $(eval $(call define_field,p25519,arch_x86_64))
  153. $(eval $(call define_curve,ed25519,p25519))
  154. $(eval $(call define_field,p448,arch_x86_64))
  155. $(eval $(call define_curve,ed448goldilocks,p448))
  156. # The shakesum utility is in the public bin directory.
  157. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/utils.o
  158. $(LD) $(LDFLAGS) -o $@ $^
  159. # The main decaf library, and its symlinks.
  160. lib: $(BUILD_LIB)/libdecaf.so
  161. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  162. ln -sf `basename $^` $@
  163. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  164. rm -f $@
  165. ifeq ($(UNAME),Darwin)
  166. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  167. $(LIBCOMPONENTS)
  168. else
  169. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  170. strip --discard-all $@
  171. endif
  172. $(BUILD_ASM)/%.s: src/%.c $(HEADERS)
  173. $(CC) $(CFLAGS) -S -c -o $@ $<
  174. $(BUILD_ASM)/%.s: test/%.c $(HEADERS)
  175. $(CC) $(PUB_CFLAGS) -S -c -o $@ $<
  176. $(BUILD_ASM)/%.s: test/%.cxx $(HEADERS)
  177. $(CXX) $(CXXFLAGS) -S -c -o $@ $<
  178. # The sage test scripts
  179. sage: $(BUILDPYS)
  180. sagetest: sage lib
  181. $(SAGE) $(BUILD_PY)/test_decaf.sage
  182. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  183. cp -f $(SAGES) $(BUILD_PY)/
  184. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  185. # some sage versions compile to .sage.py
  186. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  187. if [ -e $$f.sage.py ]; then \
  188. mv $$f.sage.py $$f.py; \
  189. fi; \
  190. done
  191. # The documentation files
  192. $(BUILD_DOC)/timestamp:
  193. mkdir -p `dirname $@`
  194. touch $@
  195. #
  196. # doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS) src/*.c src/$(FIELD)/$(ARCH)/*.c src/$(FIELD)/$(ARCH)/*.h
  197. # doxygen > /dev/null
  198. # # The eBATS benchmarking script
  199. # bat: $(BATNAME)
  200. #
  201. # $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
  202. # rm -fr $@
  203. # for prim in dh sign; do \
  204. # targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
  205. # (while read arch where; do \
  206. # mkdir -p $$targ/`basename $$arch`; \
  207. # 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`; \
  208. # cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
  209. # perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h; \
  210. # perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h; \
  211. # done \
  212. # ) < test/batarch.map; \
  213. # echo 'Mike Hamburg' > $$targ/designers; \
  214. # echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
  215. # done
  216. # (cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
  217. # Finds todo items in .h and .c files
  218. TODO_TYPES ?= HACK TODO FIXME BUG XXX PERF FUTURE REMOVE MAGIC
  219. TODO_LOCATIONS ?= src test Makefile Doxyfile
  220. todo::
  221. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep --color=auto -w \
  222. `echo $(TODO_TYPES) | tr ' ' '|'`
  223. @echo '============================='
  224. @(for i in $(TODO_TYPES); do \
  225. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i > /dev/null || continue; \
  226. /bin/echo -n $$i' ' | head -c 10; \
  227. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i| wc -l; \
  228. done)
  229. @echo '============================='
  230. @echo -n 'Total '
  231. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w \
  232. `echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
  233. bench: $(BUILD_IBIN)/bench
  234. ./$<
  235. test: $(BUILD_IBIN)/test
  236. ./$<
  237. microbench: $(BUILD_IBIN)/bench
  238. ./$< --micro
  239. clean:
  240. rm -fr build $(BATNAME)