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.
 
 
 
 
 

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