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.6 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. HEADERSXX = $(HEADERS) $(shell find . -name "*.hxx")
  80. # components needed by the lib
  81. 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
  82. BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
  83. all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
  84. scan: clean
  85. scan-build --use-analyzer=`which clang` \
  86. -enable-checker deadcode -enable-checker llvm \
  87. -enable-checker osx -enable-checker security -enable-checker unix \
  88. make all
  89. # Internal test programs, which are not part of the final build/bin directory.
  90. $(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
  91. ifeq ($(UNAME),Darwin)
  92. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  93. else
  94. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  95. endif
  96. $(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
  97. ifeq ($(UNAME),Darwin)
  98. $(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
  99. else
  100. $(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
  101. endif
  102. # Create all the build subdirectories
  103. $(BUILD_OBJ)/timestamp:
  104. mkdir -p $(BUILD_ASM) $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
  105. $(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_INC)/decaf
  106. touch $@
  107. $(BUILD_OBJ)/%.o: $(BUILD_ASM)/%.s
  108. $(ASM) $(ASFLAGS) -c -o $@ $<
  109. gen_headers: $(GEN_HEADERS)
  110. $(GEN_HEADERS): src/gen_headers/*.py src/public_include/decaf/*
  111. python -B src/gen_headers/main.py --hpre=$(BUILD_INC) --cpre=$(BUILD_C)
  112. cp src/public_include/decaf/* $(BUILD_INC)/decaf/
  113. ################################################################
  114. # Per-field code: call with field, arch
  115. ################################################################
  116. define define_field
  117. ARCH_FOR_$(1) = $(2)
  118. COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)_impl.o $$(BUILD_OBJ)/$(1)_arithmetic.o
  119. LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
  120. $$(BUILD_ASM)/$(1)_arithmetic.s: src/$(1)/f_arithmetic.c $$(HEADERS)
  121. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$(2) -S -c -o $$@ $$<
  122. $$(BUILD_ASM)/$(1)_impl.s: src/$(1)/$(2)/f_impl.c $$(HEADERS)
  123. $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$(2) -S -c -o $$@ $$<
  124. endef
  125. ################################################################
  126. # Per-field, per-curve code: call with curve, field
  127. ################################################################
  128. define define_curve
  129. $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/decaf_gen_tables_$(1).o $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/utils.o \
  130. $$(COMPONENTS_OF_$(2))
  131. $$(LD) $$(LDFLAGS) -o $$@ $$^
  132. $$(BUILD_C)/decaf_tables_$(1).c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  133. ./$$< > $$@ || (rm $$@; exit 1)
  134. $$(BUILD_ASM)/decaf_tables_$(1).s: $$(BUILD_C)/decaf_tables_$(1).c $$(HEADERS)
  135. $$(CC) $$(CFLAGS) -S -c -o $$@ $$< \
  136. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  137. $$(BUILD_ASM)/decaf_gen_tables_$(1).s: src/decaf_gen_tables.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_$(1).s: src/decaf.c $$(HEADERS)
  142. $$(CC) $$(CFLAGS) \
  143. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  144. -S -c -o $$@ $$<
  145. $$(BUILD_ASM)/decaf_crypto_$(1).s: src/decaf_crypto.c $$(HEADERS)
  146. $$(CC) $$(CFLAGS) \
  147. -I src/curve_$(1)/ \
  148. -S -c -o $$@ $$<
  149. LIBCOMPONENTS += $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/decaf_tables_$(1).o
  150. endef
  151. ################################################################
  152. # call code above to generate curves and fields
  153. $(eval $(call define_field,p25519,arch_x86_64))
  154. $(eval $(call define_curve,ed25519,p25519))
  155. $(eval $(call define_field,p448,arch_x86_64))
  156. $(eval $(call define_curve,ed448goldilocks,p448))
  157. # The shakesum utility is in the public bin directory.
  158. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/utils.o
  159. $(LD) $(LDFLAGS) -o $@ $^
  160. # The main decaf library, and its symlinks.
  161. lib: $(BUILD_LIB)/libdecaf.so
  162. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  163. ln -sf `basename $^` $@
  164. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  165. rm -f $@
  166. ifeq ($(UNAME),Darwin)
  167. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  168. $(LIBCOMPONENTS)
  169. else
  170. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  171. strip --discard-all $@
  172. endif
  173. $(BUILD_ASM)/%.s: src/%.c $(HEADERS)
  174. $(CC) $(CFLAGS) -S -c -o $@ $<
  175. $(BUILD_ASM)/%.s: test/%.c $(HEADERS)
  176. $(CC) $(PUB_CFLAGS) -S -c -o $@ $<
  177. $(BUILD_ASM)/%.s: test/%.cxx $(HEADERSXX)
  178. $(CXX) $(CXXFLAGS) -S -c -o $@ $<
  179. # The sage test scripts
  180. sage: $(BUILDPYS)
  181. sagetest: sage lib
  182. $(SAGE) $(BUILD_PY)/test_decaf.sage
  183. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  184. cp -f $(SAGES) $(BUILD_PY)/
  185. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  186. # some sage versions compile to .sage.py
  187. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  188. if [ -e $$f.sage.py ]; then \
  189. mv $$f.sage.py $$f.py; \
  190. fi; \
  191. done
  192. # The documentation files
  193. $(BUILD_DOC)/timestamp:
  194. mkdir -p `dirname $@`
  195. touch $@
  196. #
  197. # doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS) src/*.c src/$(FIELD)/$(ARCH)/*.c src/$(FIELD)/$(ARCH)/*.h
  198. # doxygen > /dev/null
  199. # # The eBATS benchmarking script
  200. # bat: $(BATNAME)
  201. #
  202. # $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
  203. # rm -fr $@
  204. # for prim in dh sign; do \
  205. # targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
  206. # (while read arch where; do \
  207. # mkdir -p $$targ/`basename $$arch`; \
  208. # 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`; \
  209. # cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
  210. # perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h; \
  211. # perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h; \
  212. # done \
  213. # ) < test/batarch.map; \
  214. # echo 'Mike Hamburg' > $$targ/designers; \
  215. # echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
  216. # done
  217. # (cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
  218. # Finds todo items in .h and .c files
  219. TODO_TYPES ?= HACK TODO FIXME BUG XXX PERF FUTURE REMOVE MAGIC
  220. todo::
  221. @(find * -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 * -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 * -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 * -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)