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.
 
 
 
 
 

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