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.
 
 
 
 
 

303 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. 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 -I$(BUILD_INC) -I$(BUILD_H)
  38. PUB_INCFLAGS = -I$(BUILD_INC)
  39. LANGFLAGS = -std=c99 -fno-strict-aliasing
  40. LANGXXFLAGS = -fno-strict-aliasing
  41. GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
  42. OFLAGS ?= -O2
  43. MACOSX_VERSION_MIN ?= 10.9
  44. ifeq ($(UNAME),Darwin)
  45. GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
  46. endif
  47. TODAY = $(shell date "+%Y-%m-%d")
  48. ifneq (,$(findstring arm,$(MACHINE)))
  49. ifneq (,$(findstring neon,$(ARCH)))
  50. ARCHFLAGS += -mfpu=neon
  51. else
  52. ARCHFLAGS += -mfpu=vfpv3-d16
  53. endif
  54. ARCHFLAGS += -mcpu=cortex-a8 # FIXME
  55. GENFLAGS += -DN_TESTS_BASE=1000 # sooooo sloooooow
  56. else
  57. ARCHFLAGS += -maes -mavx2 -mbmi2 #TODO
  58. endif
  59. ifeq ($(CC),clang)
  60. WARNFLAGS += -Wgcc-compat
  61. endif
  62. ARCHFLAGS += $(XARCHFLAGS)
  63. CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  64. PUB_CFLAGS = $(LANGFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
  65. CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS)
  66. LDFLAGS = $(XLDFLAGS)
  67. ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
  68. SAGE ?= sage
  69. SAGES= $(shell ls test/*.sage)
  70. BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
  71. .PHONY: clean all test bench todo doc lib bat sage sagetest gen_headers
  72. .PRECIOUS: $(BUILD_ASM)/%.s $(BUILD_C)/%.c $(BUILD_IBIN)/%
  73. GEN_HEADERS=\
  74. $(BUILD_INC)/decaf/decaf_255.h \
  75. $(BUILD_INC)/decaf/decaf_448.h \
  76. $(BUILD_INC)/decaf/decaf_255.hxx \
  77. $(BUILD_INC)/decaf/decaf_448.hxx \
  78. $( src/public_include/decaf/* : src/public_include = $(BUILD_INC) )
  79. HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_HEADERS)
  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_H) $(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) --ihpre=$(BUILD_H) --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) -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(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) -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(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 \
  130. $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/utils.o \
  131. $$(COMPONENTS_OF_$(2))
  132. $$(LD) $$(LDFLAGS) -o $$@ $$^
  133. $$(BUILD_C)/decaf_tables_$(1).c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
  134. ./$$< > $$@ || (rm $$@; exit 1)
  135. $$(BUILD_ASM)/decaf_tables_$(1).s: $$(BUILD_C)/decaf_tables_$(1).c $$(HEADERS)
  136. $$(CC) $$(CFLAGS) -S -c -o $$@ $$< \
  137. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  138. -I $(BUILD_H)/curve_$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
  139. $$(BUILD_ASM)/decaf_gen_tables_$(1).s: src/decaf_gen_tables.c $$(HEADERS)
  140. $$(CC) $$(CFLAGS) \
  141. -I src/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  142. -I $(BUILD_H)/curve_$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
  143. -S -c -o $$@ $$<
  144. $$(BUILD_ASM)/decaf_$(1).s: src/decaf.c $$(HEADERS)
  145. $$(CC) $$(CFLAGS) \
  146. -I src/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) \
  147. -I $(BUILD_H)/curve_$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
  148. -S -c -o $$@ $$<
  149. $$(BUILD_ASM)/decaf_crypto_$(1).s: src/decaf_crypto.c $$(HEADERS)
  150. $$(CC) $$(CFLAGS) \
  151. -I src/curve_$(1)/ \
  152. -I $(BUILD_H)/curve_$(1) \
  153. -S -c -o $$@ $$<
  154. LIBCOMPONENTS += $$(BUILD_OBJ)/decaf_$(1).o $$(BUILD_OBJ)/decaf_tables_$(1).o
  155. endef
  156. ################################################################
  157. # call code above to generate curves and fields
  158. $(eval $(call define_field,p25519,arch_x86_64))
  159. $(eval $(call define_curve,ed25519,p25519))
  160. $(eval $(call define_field,p448,arch_x86_64))
  161. $(eval $(call define_curve,ed448goldilocks,p448))
  162. # The shakesum utility is in the public bin directory.
  163. $(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/utils.o
  164. $(LD) $(LDFLAGS) -o $@ $^
  165. # The main decaf library, and its symlinks.
  166. lib: $(BUILD_LIB)/libdecaf.so
  167. $(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
  168. ln -sf `basename $^` $@
  169. $(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
  170. rm -f $@
  171. ifeq ($(UNAME),Darwin)
  172. libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
  173. $(LIBCOMPONENTS)
  174. else
  175. $(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
  176. strip --discard-all $@
  177. endif
  178. $(BUILD_ASM)/%.s: src/%.c $(HEADERS)
  179. $(CC) $(CFLAGS) -S -c -o $@ $<
  180. $(BUILD_ASM)/%.s: test/%.c $(HEADERS)
  181. $(CC) $(PUB_CFLAGS) -S -c -o $@ $<
  182. $(BUILD_ASM)/%.s: test/%.cxx $(HEADERS)
  183. $(CXX) $(CXXFLAGS) -S -c -o $@ $<
  184. # The sage test scripts
  185. sage: $(BUILDPYS)
  186. sagetest: sage lib
  187. $(SAGE) $(BUILD_PY)/test_decaf.sage
  188. $(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
  189. cp -f $(SAGES) $(BUILD_PY)/
  190. $(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
  191. # some sage versions compile to .sage.py
  192. for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
  193. if [ -e $$f.sage.py ]; then \
  194. mv $$f.sage.py $$f.py; \
  195. fi; \
  196. done
  197. # The documentation files
  198. $(BUILD_DOC)/timestamp:
  199. mkdir -p `dirname $@`
  200. touch $@
  201. #
  202. doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
  203. doxygen > /dev/null
  204. # # The eBATS benchmarking script
  205. # bat: $(BATNAME)
  206. #
  207. # $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
  208. # rm -fr $@
  209. # for prim in dh sign; do \
  210. # targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
  211. # (while read arch where; do \
  212. # mkdir -p $$targ/`basename $$arch`; \
  213. # 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`; \
  214. # cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
  215. # perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h; \
  216. # perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h; \
  217. # done \
  218. # ) < test/batarch.map; \
  219. # echo 'Mike Hamburg' > $$targ/designers; \
  220. # echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
  221. # done
  222. # (cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
  223. # Finds todo items in .h and .c files
  224. TODO_TYPES ?= HACK TODO FIXME BUG XXX PERF FUTURE REMOVE MAGIC
  225. TODO_LOCATIONS ?= src test Makefile Doxyfile
  226. todo::
  227. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep --color=auto -w \
  228. `echo $(TODO_TYPES) | tr ' ' '|'`
  229. @echo '============================='
  230. @(for i in $(TODO_TYPES); do \
  231. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i > /dev/null || continue; \
  232. /bin/echo -n $$i' ' | head -c 10; \
  233. (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i| wc -l; \
  234. done)
  235. @echo '============================='
  236. @echo -n 'Total '
  237. @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w \
  238. `echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
  239. bench: $(BUILD_IBIN)/bench
  240. ./$<
  241. test: $(BUILD_IBIN)/test
  242. ./$<
  243. microbench: $(BUILD_IBIN)/bench
  244. ./$< --micro
  245. clean:
  246. rm -fr build $(BATNAME)