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.
 
 
 
 
 

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