From c8058644bb1c6e730a5fdb04d497b7bf4a7d9a4f Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 21 Aug 2024 20:07:18 +0800 Subject: [PATCH 1/9] place all artifacts under test/build dir Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- .gitignore | 3 +-- Makefile | 51 +++++++++++++++++++++++++-------------------------- mk/config.mk | 6 +++++- scripts/tests | 11 +++++++---- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index db9ffe367..37072c2f7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,4 @@ .direnv .vscode .idea -test/bin/ -test/obj/ +test/build diff --git a/Makefile b/Makefile index 690086f22..2642bb04e 100644 --- a/Makefile +++ b/Makefile @@ -28,81 +28,81 @@ HEADERNISTKATS = $(HEADERSKECCAK) test/nistrng/aes.h test/nistrng/randombytes.h HEADERSBENCH = $(HEADERSKECCAKRANDOM) test/hal.h mlkem: \ - test/bin/test_kyber512 \ - test/bin/test_kyber768 \ - test/bin/test_kyber1024 + $(BIN_DIR)/test_kyber512 \ + $(BIN_DIR)/test_kyber768 \ + $(BIN_DIR)/test_kyber1024 bench: \ - test/bin/bench_kyber512 \ - test/bin/bench_kyber768 \ - test/bin/bench_kyber1024 + $(BIN_DIR)/bench_kyber512 \ + $(BIN_DIR)/bench_kyber768 \ + $(BIN_DIR)/bench_kyber1024 nistkat: \ - test/bin/gen_NISTKAT512 \ - test/bin/gen_NISTKAT768 \ - test/bin/gen_NISTKAT1024 + $(BIN_DIR)/gen_NISTKAT512 \ + $(BIN_DIR)/gen_NISTKAT768 \ + $(BIN_DIR)/gen_NISTKAT1024 kat: \ - test/bin/gen_KAT512 \ - test/bin/gen_KAT768 \ - test/bin/gen_KAT1024 + $(BIN_DIR)/gen_KAT512 \ + $(BIN_DIR)/gen_KAT768 \ + $(BIN_DIR)/gen_KAT1024 -test/bin/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/bench_kyber512: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) +$(BIN_DIR)/bench_kyber512: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESBENCH) $< -o $@ -test/bin/bench_kyber768: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) +$(BIN_DIR)/bench_kyber768: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESBENCH) $< -o $@ -test/bin/bench_kyber1024: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) +$(BIN_DIR)/bench_kyber1024: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESBENCH) $< -o $@ -test/bin/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) +$(BIN_DIR)/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) +$(BIN_DIR)/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=2 $(SOURCESNISTKATS) $< -o $@ -test/bin/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) +$(BIN_DIR)/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=3 $(SOURCESNISTKATS) $< -o $@ -test/bin/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) +$(BIN_DIR)/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=4 $(SOURCESNISTKATS) $< -o $@ @@ -114,5 +114,4 @@ emulate: clean: -$(RM) -rf *.gcno *.gcda *.lcov *.o *.so - -$(RM) -rf test/bin - -$(RM) -rf test/obj + -$(RM) -rf $(BUILD_DIR) diff --git a/mk/config.mk b/mk/config.mk index d6016524f..b781ed424 100644 --- a/mk/config.mk +++ b/mk/config.mk @@ -67,7 +67,11 @@ endif RETAINED_VARS := -CONFIG := test/obj/.config.mk +BUILD_DIR := test/build +OBJ_DIR := $(BUILD_DIR)/obj +BIN_DIR := $(BUILD_DIR)/bin + +CONFIG := $(BUILD_DIR)/config.mk -include $(CONFIG) diff --git a/scripts/tests b/scripts/tests index 959eba0cf..54b8cbfb8 100755 --- a/scripts/tests +++ b/scripts/tests @@ -25,6 +25,9 @@ def sha256sum(result): return m.hexdigest() +bin_dir = "test/build/bin" + + def base_compile( bin, verbose, @@ -305,7 +308,7 @@ def func(force_qemu, verbose, cross_prefix, cflags, arch_flags): test_schemes( "Functional test", - lambda scheme: scheme.name.replace("MLKEM", "test/bin/test_kyber"), + lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/test_kyber"), lambda result: str(result, encoding="utf-8"), expect, process_test, @@ -326,7 +329,7 @@ def nistkat(force_qemu, verbose, cross_prefix, cflags, arch_flags): test_schemes( "Nistkat test", - lambda scheme: scheme.name.replace("MLKEM", "test/bin/gen_NISTKAT"), + lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/gen_NISTKAT"), sha256sum, lambda scheme: parse_meta(scheme, "nistkat-sha256"), process_test, @@ -347,7 +350,7 @@ def kat(force_qemu, verbose, cross_prefix, cflags, arch_flags): test_schemes( "Kat test", - lambda scheme: scheme.name.replace("MLKEM", "test/bin/gen_KAT"), + lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/gen_KAT"), sha256sum, lambda scheme: parse_meta(scheme, "kat-sha256"), process_test, @@ -425,7 +428,7 @@ def bench( results = test_schemes( "benchmark", - lambda scheme: scheme.name.replace("MLKEM", "test/bin/bench_kyber"), + lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/bench_kyber"), lambda _: True, lambda _: True, process_bench, From 0eaf24f5d247fe035c909b58a939507cf58d898e Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:09:42 +0800 Subject: [PATCH 2/9] refactor the build system Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- Makefile | 112 +++++++------------------------------------ mk/config.mk | 15 ++++-- mk/crypto.mk | 23 +++++++++ mk/hal.mk | 9 ++++ mk/rules.mk | 51 ++++++++++++++++++++ mk/schemes.mk | 19 ++++++++ test/{ => hal}/hal.c | 0 test/{ => hal}/hal.h | 0 8 files changed, 130 insertions(+), 99 deletions(-) create mode 100644 mk/crypto.mk create mode 100644 mk/hal.mk create mode 100644 mk/rules.mk create mode 100644 mk/schemes.mk rename test/{ => hal}/hal.c (100%) rename test/{ => hal}/hal.h (100%) diff --git a/Makefile b/Makefile index 2642bb04e..bcc21ab15 100644 --- a/Makefile +++ b/Makefile @@ -6,106 +6,30 @@ all: mlkem bench kat nistkat include mk/config.mk - -INCLUDE_RANDOM = -I randombytes -INCLUDE_NISTRANDOM = -I test/nistrng - -CFLAGS_RANDOMBYTES = ${CFLAGS} ${INCLUDE_RANDOM} -CFLAGS_NISTRANDOMBYTES = ${CFLAGS} ${INCLUDE_NISTRANDOM} -NISTFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer -RM = /bin/rm - -SOURCES = mlkem/kem.c mlkem/indcpa.c mlkem/polyvec.c mlkem/poly.c mlkem/ntt.c mlkem/cbd.c mlkem/reduce.c mlkem/verify.c -SOURCESKECCAK = $(SOURCES) fips202/keccakf1600.c fips202/fips202.c fips202/fips202x4.c mlkem/symmetric-shake.c -SOURCESKECCAKRANDOM = $(SOURCESKECCAK) randombytes/randombytes.c -SOURCESNISTKATS = $(SOURCESKECCAK) test/nistrng/aes.c test/nistrng/rng.c -SOURCESBENCH = $(SOURCESKECCAKRANDOM) test/hal.c - -HEADERS = mlkem/params.h mlkem/kem.h mlkem/indcpa.h mlkem/polyvec.h mlkem/poly.h mlkem/ntt.h mlkem/cbd.h mlkem/reduce.c mlkem/verify.h mlkem/symmetric.h -HEADERSKECCAK = $(HEADERS) fips202/keccakf1600.h fips202/fips202.h fips202/fips202x4.h -HEADERSKECCAKRANDOM = $(HEADERSKECCAK) randombytes/randombytes.h -HEADERNISTKATS = $(HEADERSKECCAK) test/nistrng/aes.h test/nistrng/randombytes.h -HEADERSBENCH = $(HEADERSKECCAKRANDOM) test/hal.h +include mk/crypto.mk +include mk/schemes.mk +include mk/hal.mk +include mk/rules.mk mlkem: \ - $(BIN_DIR)/test_kyber512 \ - $(BIN_DIR)/test_kyber768 \ - $(BIN_DIR)/test_kyber1024 + $(MLKEM512_DIR)/bin/test_kyber \ + $(MLKEM768_DIR)/bin/test_kyber \ + $(MLKEM1024_DIR)/bin/test_kyber \ -bench: \ - $(BIN_DIR)/bench_kyber512 \ - $(BIN_DIR)/bench_kyber768 \ - $(BIN_DIR)/bench_kyber1024 +bench: + $(MAKE) BENCH=1 $(MLKEM512_DIR)/bin/bench_kyber + $(MAKE) BENCH=1 $(MLKEM768_DIR)/bin/bench_kyber + $(MAKE) BENCH=1 $(MLKEM1024_DIR)/bin/bench_kyber -nistkat: \ - $(BIN_DIR)/gen_NISTKAT512 \ - $(BIN_DIR)/gen_NISTKAT768 \ - $(BIN_DIR)/gen_NISTKAT1024 +nistkat: + $(MAKE) RNG=NISTRNG $(MLKEM512_DIR)/bin/gen_NISTKAT + $(MAKE) RNG=NISTRNG $(MLKEM768_DIR)/bin/gen_NISTKAT + $(MAKE) RNG=NISTRNG $(MLKEM1024_DIR)/bin/gen_NISTKAT kat: \ - $(BIN_DIR)/gen_KAT512 \ - $(BIN_DIR)/gen_KAT768 \ - $(BIN_DIR)/gen_KAT1024 - -$(BIN_DIR)/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/bench_kyber512: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESBENCH) $< -o $@ - -$(BIN_DIR)/bench_kyber768: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESBENCH) $< -o $@ - -$(BIN_DIR)/bench_kyber1024: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESBENCH) $< -o $@ - -$(BIN_DIR)/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ - -$(BIN_DIR)/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=2 $(SOURCESNISTKATS) $< -o $@ - -$(BIN_DIR)/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=3 $(SOURCESNISTKATS) $< -o $@ - -$(BIN_DIR)/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=4 $(SOURCESNISTKATS) $< -o $@ + $(MLKEM512_DIR)/bin/gen_KAT \ + $(MLKEM768_DIR)/bin/gen_KAT \ + $(MLKEM1024_DIR)/bin/gen_KAT # emulate ARM64 binary on x86_64 machine emulate: diff --git a/mk/config.mk b/mk/config.mk index b781ed424..2977001cb 100644 --- a/mk/config.mk +++ b/mk/config.mk @@ -32,10 +32,15 @@ CFLAGS += \ -O3 \ -fomit-frame-pointer \ -pedantic \ + -MMD \ -I mlkem \ -I fips202 \ $(CPPFLAGS) +LDFLAGS += $(ARCH_FLAGS) + +LINKDEPS += $(LIBDEPS) + ################## # Some Variables # ################## @@ -65,11 +70,13 @@ endif # Include retained variables # ############################## -RETAINED_VARS := +RNG ?= +RETAINED_VARS := RNG BENCH CYCLES BUILD_DIR := test/build -OBJ_DIR := $(BUILD_DIR)/obj -BIN_DIR := $(BUILD_DIR)/bin +LIB_DIR := $(BUILD_DIR)/lib + +objs = $(addprefix $(BUILD_DIR)/,$(addsuffix .o,$(1))) CONFIG := $(BUILD_DIR)/config.mk @@ -81,8 +88,6 @@ $(CONFIG): @echo "# These variables are retained and can't be changed without a clean" > $@ @$(foreach var,$(RETAINED_VARS),echo "$(var) := $($(var))" >> $@; echo "LAST_$(var) := $($(var))" >> $@;) -RETAINED_VARS += CYCLES - define VAR_CHECK ifneq ($$(origin LAST_$(1)),undefined) ifneq "$$($(1))" "$$(LAST_$(1))" diff --git a/mk/crypto.mk b/mk/crypto.mk new file mode 100644 index 000000000..5ac5d9d54 --- /dev/null +++ b/mk/crypto.mk @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: Apache-2.0 +LDLIBS += -L$(LIB_DIR) + +LIBDEPS += $(LIB_DIR)/libfips202.a +LDLIBS += -lfips202 +CPPFLAGS += -Ifips202 + +ifeq ($(RNG),NISTRNG) + LIBDEPS += $(LIB_DIR)/libnistrng.a + LDLIBS += -lnistrng + CPPFLAGS += -Itest/nistrng +else + LIBDEPS += $(LIB_DIR)/librng.a + LDLIBS += -lrng + CPPFLAGS += -Irandombytes +endif + +$(LIB_DIR)/librng.a: $(call objs,$(wildcard randombytes/*.c)) + +$(LIB_DIR)/libnistrng.a: CFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer +$(LIB_DIR)/libnistrng.a: $(call objs,$(wildcard test/nistrng/*.c)) + +$(LIB_DIR)/libfips202.a: $(call objs,$(wildcard fips202/*.c)) diff --git a/mk/hal.mk b/mk/hal.mk new file mode 100644 index 000000000..0c3405bf8 --- /dev/null +++ b/mk/hal.mk @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 +ifdef BENCH + +LIBDEPS += $(LIB_DIR)/libhal.a +LDLIBS += -lhal +CPPFLAGS += -Itest/hal +$(LIB_DIR)/libhal.a: $(call objs,$(wildcard test/hal/*.c)) + +endif diff --git a/mk/rules.mk b/mk/rules.mk new file mode 100644 index 000000000..7bbdb1fc2 --- /dev/null +++ b/mk/rules.mk @@ -0,0 +1,51 @@ +# SPDX-License-Identifier: Apache-2.0 +$(BUILD_DIR)/%: $(LINKDEPS) $(CONFIG) + $(Q)echo " LD $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(LD) $(LDFLAGS) -o $@ $(filter %.o,$^) $(LDLIBS) + +$(LIB_DIR)/%.a: $(CONFIG) + $(Q)echo " AR $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)rm -f $@ + $(Q)$(AR) rcs $@ $(filter %.o,$^) + +$(BUILD_DIR)/%.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/%.c.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem512/%.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem512/%.c.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem768/%.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem768/%.c.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem1024/%.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem1024/%.c.o: %.c $(CONFIG) + $(Q)echo " CC $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< diff --git a/mk/schemes.mk b/mk/schemes.mk new file mode 100644 index 000000000..4792dc1b6 --- /dev/null +++ b/mk/schemes.mk @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: Apache-2.0 +SOURCES = $(wildcard mlkem/*.c) +OBJS = $(SOURCES:%.c=$(1)/%.o) + +CPPFLAGS += -Imlkem +TESTS = test_kyber bench_kyber gen_NISTKAT gen_KAT + +MLKEM512_DIR = $(BUILD_DIR)/mlkem512 +MLKEM768_DIR = $(BUILD_DIR)/mlkem768 +MLKEM1024_DIR = $(BUILD_DIR)/mlkem1024 + +$(MLKEM512_DIR)/bin/%: CPPFLAGS += -DKYBER_K=2 +$(TESTS:%=$(MLKEM512_DIR)/bin/%):$(MLKEM512_DIR)/bin/%: $(MLKEM512_DIR)/test/%.o $(call OBJS,$(MLKEM512_DIR)) + +$(MLKEM768_DIR)/bin/%: CPPFLAGS += -DKYBER_K=3 +$(TESTS:%=$(MLKEM768_DIR)/bin/%):$(MLKEM768_DIR)/bin/%: $(MLKEM768_DIR)/test/%.o $(call OBJS,$(MLKEM768_DIR)) + +$(MLKEM1024_DIR)/bin/%: CPPFLAGS += -DKYBER_K=4 +$(TESTS:%=$(MLKEM1024_DIR)/bin/%):$(MLKEM1024_DIR)/bin/%: $(MLKEM1024_DIR)/test/%.o $(call OBJS,$(MLKEM1024_DIR)) diff --git a/test/hal.c b/test/hal/hal.c similarity index 100% rename from test/hal.c rename to test/hal/hal.c diff --git a/test/hal.h b/test/hal/hal.h similarity index 100% rename from test/hal.h rename to test/hal/hal.h From 3e13a7be8e8b2ff34e77e14f9e21de852a485453 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:09:37 +0800 Subject: [PATCH 3/9] refactor build system objs macro Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- mk/config.mk | 3 ++- mk/crypto.mk | 6 +++--- mk/hal.mk | 2 +- mk/rules.mk | 23 +++++++++-------------- mk/schemes.mk | 7 +++---- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/mk/config.mk b/mk/config.mk index 2977001cb..2d5912f05 100644 --- a/mk/config.mk +++ b/mk/config.mk @@ -76,7 +76,8 @@ RETAINED_VARS := RNG BENCH CYCLES BUILD_DIR := test/build LIB_DIR := $(BUILD_DIR)/lib -objs = $(addprefix $(BUILD_DIR)/,$(addsuffix .o,$(1))) +MAKE_OBJS = $(2:%=$(1)/%.o) +OBJS = $(call MAKE_OBJS,$(BUILD_DIR),$(1)) CONFIG := $(BUILD_DIR)/config.mk diff --git a/mk/crypto.mk b/mk/crypto.mk index 5ac5d9d54..476191ab4 100644 --- a/mk/crypto.mk +++ b/mk/crypto.mk @@ -15,9 +15,9 @@ else CPPFLAGS += -Irandombytes endif -$(LIB_DIR)/librng.a: $(call objs,$(wildcard randombytes/*.c)) +$(LIB_DIR)/librng.a: $(call OBJS,$(wildcard randombytes/*.c)) $(LIB_DIR)/libnistrng.a: CFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer -$(LIB_DIR)/libnistrng.a: $(call objs,$(wildcard test/nistrng/*.c)) +$(LIB_DIR)/libnistrng.a: $(call OBJS,$(wildcard test/nistrng/*.c)) -$(LIB_DIR)/libfips202.a: $(call objs,$(wildcard fips202/*.c)) +$(LIB_DIR)/libfips202.a: $(call OBJS,$(wildcard fips202/*.c)) diff --git a/mk/hal.mk b/mk/hal.mk index 0c3405bf8..e2de83a95 100644 --- a/mk/hal.mk +++ b/mk/hal.mk @@ -4,6 +4,6 @@ ifdef BENCH LIBDEPS += $(LIB_DIR)/libhal.a LDLIBS += -lhal CPPFLAGS += -Itest/hal -$(LIB_DIR)/libhal.a: $(call objs,$(wildcard test/hal/*.c)) +$(LIB_DIR)/libhal.a: $(call OBJS,$(wildcard test/hal/*.c)) endif diff --git a/mk/rules.mk b/mk/rules.mk index 7bbdb1fc2..59ec81718 100644 --- a/mk/rules.mk +++ b/mk/rules.mk @@ -10,28 +10,18 @@ $(LIB_DIR)/%.a: $(CONFIG) $(Q)rm -f $@ $(Q)$(AR) rcs $@ $(filter %.o,$^) -$(BUILD_DIR)/%.o: %.c $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(Q)$(CC) -c -o $@ $(CFLAGS) $< - $(BUILD_DIR)/%.c.o: %.c $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< -$(BUILD_DIR)/mlkem512/%.o: %.c $(CONFIG) - $(Q)echo " CC $@" - $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(Q)$(CC) -c -o $@ $(CFLAGS) $< - $(BUILD_DIR)/mlkem512/%.c.o: %.c $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< -$(BUILD_DIR)/mlkem768/%.o: %.c $(CONFIG) - $(Q)echo " CC $@" +$(BUILD_DIR)/mlkem512/%.S.o: %.S $(CONFIG) + $(Q)echo " AS $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< @@ -40,8 +30,8 @@ $(BUILD_DIR)/mlkem768/%.c.o: %.c $(CONFIG) $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< -$(BUILD_DIR)/mlkem1024/%.o: %.c $(CONFIG) - $(Q)echo " CC $@" +$(BUILD_DIR)/mlkem768/%.S.o: %.S $(CONFIG) + $(Q)echo " AS $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< @@ -49,3 +39,8 @@ $(BUILD_DIR)/mlkem1024/%.c.o: %.c $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(Q)$(CC) -c -o $@ $(CFLAGS) $< + +$(BUILD_DIR)/mlkem1024/%.S.o: %.S $(CONFIG) + $(Q)echo " AS $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + $(Q)$(CC) -c -o $@ $(CFLAGS) $< diff --git a/mk/schemes.mk b/mk/schemes.mk index 4792dc1b6..2a44dd42d 100644 --- a/mk/schemes.mk +++ b/mk/schemes.mk @@ -1,6 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 SOURCES = $(wildcard mlkem/*.c) -OBJS = $(SOURCES:%.c=$(1)/%.o) CPPFLAGS += -Imlkem TESTS = test_kyber bench_kyber gen_NISTKAT gen_KAT @@ -10,10 +9,10 @@ MLKEM768_DIR = $(BUILD_DIR)/mlkem768 MLKEM1024_DIR = $(BUILD_DIR)/mlkem1024 $(MLKEM512_DIR)/bin/%: CPPFLAGS += -DKYBER_K=2 -$(TESTS:%=$(MLKEM512_DIR)/bin/%):$(MLKEM512_DIR)/bin/%: $(MLKEM512_DIR)/test/%.o $(call OBJS,$(MLKEM512_DIR)) +$(TESTS:%=$(MLKEM512_DIR)/bin/%):$(MLKEM512_DIR)/bin/%: $(MLKEM512_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM512_DIR),$(SOURCES)) $(MLKEM768_DIR)/bin/%: CPPFLAGS += -DKYBER_K=3 -$(TESTS:%=$(MLKEM768_DIR)/bin/%):$(MLKEM768_DIR)/bin/%: $(MLKEM768_DIR)/test/%.o $(call OBJS,$(MLKEM768_DIR)) +$(TESTS:%=$(MLKEM768_DIR)/bin/%):$(MLKEM768_DIR)/bin/%: $(MLKEM768_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM768_DIR),$(SOURCES)) $(MLKEM1024_DIR)/bin/%: CPPFLAGS += -DKYBER_K=4 -$(TESTS:%=$(MLKEM1024_DIR)/bin/%):$(MLKEM1024_DIR)/bin/%: $(MLKEM1024_DIR)/test/%.o $(call OBJS,$(MLKEM1024_DIR)) +$(TESTS:%=$(MLKEM1024_DIR)/bin/%):$(MLKEM1024_DIR)/bin/%: $(MLKEM1024_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM1024_DIR),$(SOURCES)) From ffe95301c3ce1faa28b38e0ddc05bcd83f832718 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:29:16 +0800 Subject: [PATCH 4/9] make don't print directory Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index bcc21ab15..eba54699a 100644 --- a/Makefile +++ b/Makefile @@ -11,20 +11,22 @@ include mk/schemes.mk include mk/hal.mk include mk/rules.mk +MAKEFLAGS = --no-print-directory + mlkem: \ $(MLKEM512_DIR)/bin/test_kyber \ $(MLKEM768_DIR)/bin/test_kyber \ $(MLKEM1024_DIR)/bin/test_kyber \ bench: - $(MAKE) BENCH=1 $(MLKEM512_DIR)/bin/bench_kyber - $(MAKE) BENCH=1 $(MLKEM768_DIR)/bin/bench_kyber - $(MAKE) BENCH=1 $(MLKEM1024_DIR)/bin/bench_kyber + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM512_DIR)/bin/bench_kyber + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM768_DIR)/bin/bench_kyber + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM1024_DIR)/bin/bench_kyber nistkat: - $(MAKE) RNG=NISTRNG $(MLKEM512_DIR)/bin/gen_NISTKAT - $(MAKE) RNG=NISTRNG $(MLKEM768_DIR)/bin/gen_NISTKAT - $(MAKE) RNG=NISTRNG $(MLKEM1024_DIR)/bin/gen_NISTKAT + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM512_DIR)/bin/gen_NISTKAT + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM768_DIR)/bin/gen_NISTKAT + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM1024_DIR)/bin/gen_NISTKAT kat: \ $(MLKEM512_DIR)/bin/gen_KAT \ From fb6982d46e99e28896bf81ac0eea763eb5152484 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:22:45 +0800 Subject: [PATCH 5/9] integrate the new build system to python script Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- mk/config.mk | 6 +-- scripts/tests | 118 ++++++++++++++++++++++++++------------------------ 2 files changed, 64 insertions(+), 60 deletions(-) diff --git a/mk/config.mk b/mk/config.mk index 2d5912f05..996b4d177 100644 --- a/mk/config.mk +++ b/mk/config.mk @@ -7,7 +7,7 @@ SRCDIR := $(CURDIR) ############## # GCC config # ############## -CROSS_PREFIX ?= +CROSS_PREFIX ?= CC := $(CROSS_PREFIX)gcc CPP := $(CROSS_PREFIX)cpp AR := $(CROSS_PREFIX)ar @@ -37,7 +37,7 @@ CFLAGS += \ -I fips202 \ $(CPPFLAGS) -LDFLAGS += $(ARCH_FLAGS) +LDFLAGS += $(ARCH_FLAGS) LINKDEPS += $(LIBDEPS) @@ -70,7 +70,7 @@ endif # Include retained variables # ############################## -RNG ?= +RNG ?= RETAINED_VARS := RNG BENCH CYCLES BUILD_DIR := test/build diff --git a/scripts/tests b/scripts/tests index 54b8cbfb8..9225391ee 100755 --- a/scripts/tests +++ b/scripts/tests @@ -25,11 +25,52 @@ def sha256sum(result): return m.hexdigest() -bin_dir = "test/build/bin" +class TEST_TYPES(Enum): + MLKEM = 1 + BENCH = 2 + NISTKAT = 3 + KAT = 4 + + def __str__(self): + return self.name.lower() + + def desc(self): + match self: + case TEST_TYPES.MLKEM: + return "Functional Test" + case TEST_TYPES.BENCH: + return "Benchmark" + case TEST_TYPES.NISTKAT: + return "Nistkat Test" + case TEST_TYPES.KAT: + return "Kat Test" + + def bin(self): + match self: + case TEST_TYPES.MLKEM: + return "test_kyber" + case TEST_TYPES.BENCH: + return "bench_kyber" + case TEST_TYPES.NISTKAT: + return "gen_NISTKAT" + case TEST_TYPES.KAT: + return "gen_KAT" + + def bin_path(self, scheme): + return f"test/build/{scheme.name.lower()}/bin/{self.bin()}" + + +class SCHEME(Enum): + MLKEM512 = 1 + MLKEM768 = 2 + MLKEM1024 = 3 + + def __str__(self): + return self.name def base_compile( - bin, + test, verbose, cross_prefix, extra_make_envs={}, @@ -43,11 +84,11 @@ def base_compile( s += f"{k}={v} " return s - logging.debug(f"Compiling {bin}...") + logging.debug(f"Compiling {test}...") args = [ "make", f"CROSS_PREFIX={cross_prefix}", - f"{bin}", + f"{test}", ] + extra_make_args logging.info(dict2str(extra_make_envs) + " ".join(args)) @@ -103,15 +144,6 @@ def base_run( return result.stdout -class SCHEME(Enum): - MLKEM512 = 1 - MLKEM768 = 2 - MLKEM1024 = 3 - - def __str__(self): - return self.name - - def parse_meta(scheme, field): result = subprocess.run( [ @@ -131,8 +163,7 @@ def parse_meta(scheme, field): def test_schemes( - title, - scheme2file, + test, actual_proc, expect_proc, process_result, @@ -147,18 +178,17 @@ def test_schemes( """ :param process_result: process result and return summary """ - logging.info(f"{title}") + logging.info(f"{test.desc()}") summary_file = os.environ.get("GITHUB_STEP_SUMMARY") - summary = f"## {title}\n" + summary = f"## {test.desc()}\n" fail = False results = {} + base_compile(test, verbose, cross_prefix, extra_make_envs, extra_make_args) for scheme in SCHEME: - bin = scheme2file(scheme) - base_compile(bin, verbose, cross_prefix, extra_make_envs, extra_make_args) result = base_run( - bin, + test.bin_path(scheme), force_qemu, verbose, run_as_root, @@ -264,29 +294,6 @@ def add_options(options): return lambda func: reduce(lambda f, o: o(f), reversed(options), func) -@click.command( - short_help="Run the specified binary file", - context_settings={"show_default": True}, -) -@add_options(_shared_options) -@click.option( - "-b", - "--bin", - type=click.Path(), - help="The binary file that you wanted to test.", -) -def run(bin, force_qemu, verbose, cross_prefix, cflags, arch_flags): - config_logger(verbose) - - base_compile(bin, verbose, process_make_envs(cflags, arch_flags)) - result = base_run( - bin, - force_qemu, - verbose, - ) - logging.info(str(result, encoding="utf-8")) - - @click.command( short_help="Run the functional tests for all parameter sets", context_settings={"show_default": True}, @@ -307,15 +314,14 @@ def func(force_qemu, verbose, cross_prefix, cflags, arch_flags): ) test_schemes( - "Functional test", - lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/test_kyber"), + TEST_TYPES.MLKEM, lambda result: str(result, encoding="utf-8"), expect, process_test, force_qemu, verbose, cross_prefix, - process_make_envs(cflags, arch_flags), + extra_make_envs=process_make_envs(cflags, arch_flags), ) @@ -328,15 +334,14 @@ def nistkat(force_qemu, verbose, cross_prefix, cflags, arch_flags): config_logger(verbose) test_schemes( - "Nistkat test", - lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/gen_NISTKAT"), + TEST_TYPES.NISTKAT, sha256sum, lambda scheme: parse_meta(scheme, "nistkat-sha256"), process_test, force_qemu, verbose, cross_prefix, - process_make_envs(cflags, arch_flags), + extra_make_envs=process_make_envs(cflags, arch_flags), ) @@ -349,15 +354,14 @@ def kat(force_qemu, verbose, cross_prefix, cflags, arch_flags): config_logger(verbose) test_schemes( - "Kat test", - lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/gen_KAT"), + TEST_TYPES.KAT, sha256sum, lambda scheme: parse_meta(scheme, "kat-sha256"), process_test, force_qemu, verbose, cross_prefix, - process_make_envs(cflags, arch_flags), + extra_make_envs=process_make_envs(cflags, arch_flags), ) @@ -427,8 +431,7 @@ def bench( exec_wrapper = f"taskpolicy -c {mac_taskpolicy}" results = test_schemes( - "benchmark", - lambda scheme: scheme.name.replace("MLKEM", f"{bin_dir}/bench_kyber"), + TEST_TYPES.BENCH, lambda _: True, lambda _: True, process_bench, @@ -437,8 +440,10 @@ def bench( cross_prefix, run_as_root, exec_wrapper, - process_make_envs(cflags, arch_flags), - [f"CYCLES={cycles}"], + extra_make_envs=process_make_envs(cflags, arch_flags), + extra_make_args=[ + f"CYCLES={cycles}", + ], ) if output: @@ -476,7 +481,6 @@ def cli(): pass -cli.add_command(run) cli.add_command(func) cli.add_command(nistkat) cli.add_command(kat) From 42bb871ed4d7f1a594bea3e82ee60ec4505c6a8c Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 4 Sep 2024 18:00:51 +0800 Subject: [PATCH 6/9] format nix file Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- cbmc/litani.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbmc/litani.nix b/cbmc/litani.nix index 2165e2932..7cacd4048 100644 --- a/cbmc/litani.nix +++ b/cbmc/litani.nix @@ -4,7 +4,7 @@ , fetchFromGitHub }: -stdenvNoCC.mkDerivation rec { +stdenvNoCC.mkDerivation { pname = "litani"; version = "8002c240ef4f424039ed3cc32e076c0234d01768"; src = fetchFromGitHub { From b1d55d868b8f30e2247baaa149ad3e7edbb785b6 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 4 Sep 2024 23:11:08 +0800 Subject: [PATCH 7/9] fix arch flags not correctly set Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- .github/workflows/bench.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 1d684c15b..964a8bedf 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -16,13 +16,13 @@ jobs: target: - system: rpi4 name: Arm Cortex-A72 (Raspberry Pi 4) benchmarks - cmd: tests bench -c PMU --cflags -mcpu=cortex-a72 -v --output output.json + cmd: tests bench -c PMU --arch-flags -mcpu=cortex-a72 -v --output output.json - system: rpi5 name: Arm Cortex-A76 (Raspberry Pi 5) benchmarks - cmd: tests bench -c PERF --cflags -mcpu=cortex-a76 --arch-flags -march=armv8.2-a -v --output output.json + cmd: tests bench -c PERF --arch-flags "-mcpu=cortex-a76 -march=armv8.2-a" -v --output output.json - system: a55 name: Arm Cortex-A55 (Snapdragon 888) benchmarks - cmd: tests bench -c PERF --cflags "-static -mcpu=cortex-a55" --arch-flags -march=armv8.2-a -w exec-on-a55 -v --output output.json + cmd: tests bench -c PERF --arch-flags "-static -mcpu=cortex-a55 -march=armv8.2-a" -w exec-on-a55 -v --output output.json runs-on: self-hosted-${{ matrix.target.system }} defaults: run: From 73b36bf584782fafd32dbdea64895a189b62c482 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:52:31 +0800 Subject: [PATCH 8/9] ci fix -static to be cflags Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- .github/workflows/bench.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 964a8bedf..70b87e3d0 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -22,7 +22,7 @@ jobs: cmd: tests bench -c PERF --arch-flags "-mcpu=cortex-a76 -march=armv8.2-a" -v --output output.json - system: a55 name: Arm Cortex-A55 (Snapdragon 888) benchmarks - cmd: tests bench -c PERF --arch-flags "-static -mcpu=cortex-a55 -march=armv8.2-a" -w exec-on-a55 -v --output output.json + cmd: tests bench -c PERF --cflags -static --arch-flags "-mcpu=cortex-a55 -march=armv8.2-a" -w exec-on-a55 -v --output output.json runs-on: self-hosted-${{ matrix.target.system }} defaults: run: From 96febc3839fc7a73cc650f563ad079fad849358e Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:27:38 +0800 Subject: [PATCH 9/9] fix static compilation for benchmarking on a55 Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- Makefile | 24 ++++++++++++------------ mk/config.mk | 2 -- mk/rules.mk | 2 +- mk/schemes.mk | 6 +++--- scripts/tests | 5 ++++- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index eba54699a..1ec09925f 100644 --- a/Makefile +++ b/Makefile @@ -14,24 +14,24 @@ include mk/rules.mk MAKEFLAGS = --no-print-directory mlkem: \ - $(MLKEM512_DIR)/bin/test_kyber \ - $(MLKEM768_DIR)/bin/test_kyber \ - $(MLKEM1024_DIR)/bin/test_kyber \ + $(MLKEM512_DIR)/bin/test_kyber512 \ + $(MLKEM768_DIR)/bin/test_kyber768 \ + $(MLKEM1024_DIR)/bin/test_kyber1024 \ bench: - $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM512_DIR)/bin/bench_kyber - $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM768_DIR)/bin/bench_kyber - $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM1024_DIR)/bin/bench_kyber + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM512_DIR)/bin/bench_kyber512 + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM768_DIR)/bin/bench_kyber768 + $(MAKE) $(MAKEFLAGS) BENCH=1 $(MLKEM1024_DIR)/bin/bench_kyber1024 nistkat: - $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM512_DIR)/bin/gen_NISTKAT - $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM768_DIR)/bin/gen_NISTKAT - $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM1024_DIR)/bin/gen_NISTKAT + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM512_DIR)/bin/gen_NISTKAT512 + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM768_DIR)/bin/gen_NISTKAT768 + $(MAKE) $(MAKEFLAGS) RNG=NISTRNG $(MLKEM1024_DIR)/bin/gen_NISTKAT1024 kat: \ - $(MLKEM512_DIR)/bin/gen_KAT \ - $(MLKEM768_DIR)/bin/gen_KAT \ - $(MLKEM1024_DIR)/bin/gen_KAT + $(MLKEM512_DIR)/bin/gen_KAT512 \ + $(MLKEM768_DIR)/bin/gen_KAT768 \ + $(MLKEM1024_DIR)/bin/gen_KAT1024 # emulate ARM64 binary on x86_64 machine emulate: diff --git a/mk/config.mk b/mk/config.mk index 996b4d177..4bfc5943c 100644 --- a/mk/config.mk +++ b/mk/config.mk @@ -37,8 +37,6 @@ CFLAGS += \ -I fips202 \ $(CPPFLAGS) -LDFLAGS += $(ARCH_FLAGS) - LINKDEPS += $(LIBDEPS) ################## diff --git a/mk/rules.mk b/mk/rules.mk index 59ec81718..0f009f692 100644 --- a/mk/rules.mk +++ b/mk/rules.mk @@ -2,7 +2,7 @@ $(BUILD_DIR)/%: $(LINKDEPS) $(CONFIG) $(Q)echo " LD $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) - $(LD) $(LDFLAGS) -o $@ $(filter %.o,$^) $(LDLIBS) + $(LD) $(CFLAGS) -o $@ $(filter %.o,$^) $(LDLIBS) $(LIB_DIR)/%.a: $(CONFIG) $(Q)echo " AR $@" diff --git a/mk/schemes.mk b/mk/schemes.mk index 2a44dd42d..24fc1f6c1 100644 --- a/mk/schemes.mk +++ b/mk/schemes.mk @@ -9,10 +9,10 @@ MLKEM768_DIR = $(BUILD_DIR)/mlkem768 MLKEM1024_DIR = $(BUILD_DIR)/mlkem1024 $(MLKEM512_DIR)/bin/%: CPPFLAGS += -DKYBER_K=2 -$(TESTS:%=$(MLKEM512_DIR)/bin/%):$(MLKEM512_DIR)/bin/%: $(MLKEM512_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM512_DIR),$(SOURCES)) +$(TESTS:%=$(MLKEM512_DIR)/bin/%512):$(MLKEM512_DIR)/bin/%512: $(MLKEM512_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM512_DIR),$(SOURCES)) $(MLKEM768_DIR)/bin/%: CPPFLAGS += -DKYBER_K=3 -$(TESTS:%=$(MLKEM768_DIR)/bin/%):$(MLKEM768_DIR)/bin/%: $(MLKEM768_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM768_DIR),$(SOURCES)) +$(TESTS:%=$(MLKEM768_DIR)/bin/%768):$(MLKEM768_DIR)/bin/%768: $(MLKEM768_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM768_DIR),$(SOURCES)) $(MLKEM1024_DIR)/bin/%: CPPFLAGS += -DKYBER_K=4 -$(TESTS:%=$(MLKEM1024_DIR)/bin/%):$(MLKEM1024_DIR)/bin/%: $(MLKEM1024_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM1024_DIR),$(SOURCES)) +$(TESTS:%=$(MLKEM1024_DIR)/bin/%1024):$(MLKEM1024_DIR)/bin/%1024: $(MLKEM1024_DIR)/test/%.c.o $(call MAKE_OBJS,$(MLKEM1024_DIR),$(SOURCES)) diff --git a/scripts/tests b/scripts/tests index 9225391ee..d63131013 100755 --- a/scripts/tests +++ b/scripts/tests @@ -57,7 +57,7 @@ class TEST_TYPES(Enum): return "gen_KAT" def bin_path(self, scheme): - return f"test/build/{scheme.name.lower()}/bin/{self.bin()}" + return f"test/build/{scheme.name.lower()}/bin/{self.bin()}{scheme.suffix()}" class SCHEME(Enum): @@ -68,6 +68,9 @@ class SCHEME(Enum): def __str__(self): return self.name + def suffix(self): + return self.name.removeprefix("MLKEM") + def base_compile( test,