################################################################################
# BIKE implementation

ccflags-$(CONFIG_LEANCRYPTO_KEM_BIKE) += -I$(src)/../bike/src

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE)				       \
				+= ../bike/src/bike_api.o

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_decode.o		       \
				   ../bike/src/bike_decode_portable.o	       \
				   ../bike/src/bike_gf2x_inv.o		       \
				   ../bike/src/bike_gf2x_ksqr_portable.o       \
				   ../bike/src/bike_gf2x_mul.o		       \
				   ../bike/src/bike_gf2x_mul_base_portable.o   \
				   ../bike/src/bike_gf2x_mul_portable.o	       \
				   ../bike/src/bike_kem.o		       \
				   ../bike/src/bike_sampling.o		       \
				   ../bike/src/bike_sampling_portable.o	       \
				   leancrypto_kernel_bike.o

bike_c_args = -funroll-loops
ifdef CONFIG_X86_64
bike_c_args += -mno-red-zone -DX86_64
endif

CFLAGS_../bike/src/bike_decode.o		= $(bike_c_args)
CFLAGS_../bike/src/bike_decode_portable.o	= $(bike_c_args)
CFLAGS_../bike/src/bike_gf2x_inv.o		= $(bike_c_args)
CFLAGS_../bike/src/bike_gf2x_ksqr_portable.o	= $(bike_c_args)
CFLAGS_../bike/src/bike_gf2x_mul.o		= $(bike_c_args)
CFLAGS_../bike/src/bike_gf2x_mul_base_portable.o= $(bike_c_args)
CFLAGS_../bike/src/bike_gf2x_mul_portable.o	= $(bike_c_args)
CFLAGS_../bike/src/bike_kem.o			= $(bike_c_args)
CFLAGS_../bike/src/bike_sampling.o		= $(bike_c_args)
CFLAGS_../bike/src/bike_sampling_portable.o	= $(bike_c_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_decode.o			       \
				   bike3/bike_decode_portable.o		       \
				   bike3/bike_gf2x_inv.o		       \
				   bike3/bike_gf2x_ksqr_portable.o 	       \
				   bike3/bike_gf2x_mul.o		       \
				   bike3/bike_gf2x_mul_base_portable.o	       \
				   bike3/bike_gf2x_mul_portable.o	       \
				   bike3/bike_kem.o			       \
				   bike3/bike_sampling.o		       \
				   bike3/bike_sampling_portable.o	       \
				   bike3/leancrypto_kernel_bike.o

bike3_c_args = $(bike_c_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_decode.o			= $(bike3_c_args)
CFLAGS_bike3/bike_decode_portable.o		= $(bike3_c_args)
CFLAGS_bike3/bike_gf2x_inv.o			= $(bike3_c_args)
CFLAGS_bike3/bike_gf2x_ksqr_portable.o		= $(bike3_c_args)
CFLAGS_bike3/bike_gf2x_mul.o			= $(bike3_c_args)
CFLAGS_bike3/bike_gf2x_mul_base_portable.o	= $(bike3_c_args)
CFLAGS_bike3/bike_gf2x_mul_portable.o		= $(bike3_c_args)
CFLAGS_bike3/bike_kem.o				= $(bike3_c_args)
CFLAGS_bike3/bike_sampling.o			= $(bike3_c_args)
CFLAGS_bike3/bike_sampling_portable.o		= $(bike3_c_args)
CFLAGS_bike3/leancrypto_kernel_bike.o		= -DLC_BIKE_TYPE_3

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_decode.o			       \
				   bike1/bike_decode_portable.o		       \
				   bike1/bike_gf2x_inv.o		       \
				   bike1/bike_gf2x_ksqr_portable.o 	       \
				   bike1/bike_gf2x_mul.o		       \
				   bike1/bike_gf2x_mul_base_portable.o	       \
				   bike1/bike_gf2x_mul_portable.o	       \
				   bike1/bike_kem.o			       \
				   bike1/bike_sampling.o		       \
				   bike1/bike_sampling_portable.o	       \
				   bike1/leancrypto_kernel_bike.o

bike1_c_args = $(bike_c_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_decode.o			= $(bike1_c_args)
CFLAGS_bike1/bike_decode_portable.o		= $(bike1_c_args)
CFLAGS_bike1/bike_gf2x_inv.o			= $(bike1_c_args)
CFLAGS_bike1/bike_gf2x_ksqr_portable.o		= $(bike1_c_args)
CFLAGS_bike1/bike_gf2x_mul.o			= $(bike1_c_args)
CFLAGS_bike1/bike_gf2x_mul_base_portable.o	= $(bike1_c_args)
CFLAGS_bike1/bike_gf2x_mul_portable.o		= $(bike1_c_args)
CFLAGS_bike1/bike_kem.o				= $(bike1_c_args)
CFLAGS_bike1/bike_sampling.o			= $(bike1_c_args)
CFLAGS_bike1/bike_sampling_portable.o		= $(bike1_c_args)
CFLAGS_bike1/leancrypto_kernel_bike.o		= -DLC_BIKE_TYPE_1

ifdef CONFIG_X86_64

bike_avx2_args += $(bike_c_args) -mavx2 -mbmi2 -mpopcnt			       \
		  -Wno-unused-command-line-argument
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_decode_avx2.o	       \
				   ../bike/src/bike_kem.o		       \
				   ../bike/src/bike_gf2x_mul_avx2.o	       \
				   ../bike/src/bike_gf2x_ksqr_avx2.o	       \
				   ../bike/src/bike_sampling_avx2.o
CFLAGS_../bike/src/bike_decode_avx2.o		= $(bike_avx2_args)
CFLAGS_../bike/src/bike_kem.o			= $(bike_avx2_args)
CFLAGS_../bike/src/bike_gf2x_mul_avx2.o		= $(bike_avx2_args)
CFLAGS_../bike/src/bike_gf2x_ksqr_avx2.o	= $(bike_avx2_args)
CFLAGS_../bike/src/bike_sampling_avx2.o		= $(bike_avx2_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_decode_avx2.o		       \
				   bike3/bike_kem.o			       \
				   bike3/bike_gf2x_mul_avx2.o		       \
				   bike3/bike_gf2x_ksqr_avx2.o		       \
				   bike3/bike_sampling_avx2.o
bike3_avx2_args = $(bike_avx2_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_decode_avx2.o		= $(bike3_avx2_args)
CFLAGS_bike3/bike_kem.o			= $(bike3_avx2_args)
CFLAGS_bike3/bike_gf2x_mul_avx2.o	= $(bike3_avx2_args)
CFLAGS_bike3/bike_gf2x_ksqr_avx2.o	= $(bike3_avx2_args)
CFLAGS_bike3/bike_sampling_avx2.o	= $(bike3_avx2_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_decode_avx2.o		       \
				   bike1/bike_kem.o			       \
				   bike1/bike_gf2x_mul_avx2.o		       \
				   bike1/bike_gf2x_ksqr_avx2.o		       \
				   bike1/bike_sampling_avx2.o
bike1_avx2_args = $(bike_avx2_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_decode_avx2.o		= $(bike1_avx2_args)
CFLAGS_bike1/bike_kem.o			= $(bike1_avx2_args)
CFLAGS_bike1/bike_gf2x_mul_avx2.o	= $(bike1_avx2_args)
CFLAGS_bike1/bike_gf2x_ksqr_avx2.o	= $(bike1_avx2_args)
CFLAGS_bike1/bike_sampling_avx2.o	= $(bike1_avx2_args)

bike_avx512_args += $(bike_c_args) -mavx512bw -mavx512dq -mavx512f -mbmi2      \
		    -mpopcnt -Wno-unused-command-line-argument
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_decode_avx512.o	       \
				   ../bike/src/bike_gf2x_mul_avx512.o	       \
				   ../bike/src/bike_gf2x_ksqr_avx512.o	       \
				   ../bike/src/bike_sampling_avx512.o
CFLAGS_../bike/src/bike_decode_avx512.o		= $(bike_avx512_args)
CFLAGS_../bike/src/bike_gf2x_mul_avx512.o	= $(bike_avx512_args)
CFLAGS_../bike/src/bike_gf2x_ksqr_avx512.o	= $(bike_avx512_args)
CFLAGS_../bike/src/bike_sampling_avx512.o	= $(bike_avx512_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_decode_avx512.o		       \
				   bike3/bike_gf2x_mul_avx512.o		       \
				   bike3/bike_gf2x_ksqr_avx512.o	       \
				   bike3/bike_sampling_avx512.o
bike3_avx512_args = $(bike_avx512_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_decode_avx512.o	= $(bike3_avx512_args)
CFLAGS_bike3/bike_gf2x_mul_avx512.o	= $(bike3_avx512_args)
CFLAGS_bike3/bike_gf2x_ksqr_avx512.o	= $(bike3_avx512_args)
CFLAGS_bike3/bike_sampling_avx512.o	= $(bike3_avx512_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_decode_avx512.o		       \
				   bike1/bike_gf2x_mul_avx512.o		       \
				   bike1/bike_gf2x_ksqr_avx512.o	       \
				   bike1/bike_sampling_avx512.o
bike1_avx512_args = $(bike_avx512_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_decode_avx512.o	= $(bike1_avx512_args)
CFLAGS_bike1/bike_gf2x_mul_avx512.o	= $(bike1_avx512_args)
CFLAGS_bike1/bike_gf2x_ksqr_avx512.o	= $(bike1_avx512_args)
CFLAGS_bike1/bike_sampling_avx512.o	= $(bike1_avx512_args)

bike_pclmul_args += $(bike_c_args) -mpclmul
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_gf2x_mul_base_pclmul.o
CFLAGS_../bike/src/bike_gf2x_mul_base_pclmul.o	= $(bike_pclmul_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_gf2x_mul_base_pclmul.o
bike3_pclmul_args = $(bike_pclmul_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_gf2x_mul_base_pclmul.o	= $(bike3_pclmul_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_gf2x_mul_base_pclmul.o
bike1_pclmul_args = $(bike_pclmul_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_gf2x_mul_base_pclmul.o	= $(bike1_pclmul_args)

bike_vpclmul_args += $(bike_avx512_args) -mvpclmulqdq
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_gf2x_mul_base_vpclmul.o
CFLAGS_../bike/src/bike_gf2x_mul_base_vpclmul.o	= $(bike_vpclmul_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_gf2x_mul_base_vpclmul.o
bike3_vpclmul_args = $(bike_vpclmul_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_gf2x_mul_base_vpclmul.o	= $(bike3_vpclmul_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_gf2x_mul_base_vpclmul.o
bike1_vpclmul_args = $(bike_vpclmul_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_gf2x_mul_base_vpclmul.o	= $(bike1_vpclmul_args)

endif

################################################################################
# Handling of the XOR256 support in bike_kem.c (X86_64 support covered above)
################################################################################

ifdef CONFIG_ARM64
bike_armv8_args += $(bike_c_args) -march=armv8-a+simd			       \
		   -funsafe-math-optimizations
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_kem.o
CFLAGS_../bike/src/bike_kem.o			= $(bike_armv8_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_kem.o
bike3_armv8_args = $(bike_armv8_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_kem.o			= $(bike3_armv8_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_kem.o
bike1_armv8_args = $(bike_armv8_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_kem.o			= $(bike1_armv8_args)
endif

ifdef CONFIG_ARM
bike_armv7_args += $(bike_c_args) -mfpu=neon-vfpv4 -mthumb -march=armv7-a+vfpv4\
		   -mfloat-abi=softfp -funsafe-math-optimizations
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_kem.o
CFLAGS_../bike/src/bike_kem.o			= $(bike_armv7_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_kem.o
bike3_armv7_args = $(bike_armv7_args) -DLC_BIKE_TYPE_3
CFLAGS_bike3/bike_kem.o			= $(bike3_armv7_args)

leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_kem.o
bike1_armv7_args = $(bike_armv7_args) -DLC_BIKE_TYPE_1
CFLAGS_bike1/bike_kem.o			= $(bike1_armv7_args)
endif

ifndef CONFIG_ARM
ifndef CONFIG_ARM64
ifndef CONFIG_X86_64
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_5)				       \
				+= ../bike/src/bike_kem.o
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_3)				       \
				+= bike3/bike_kem.o
CFLAGS_bike3/bike_kem.o			= -DLC_BIKE_TYPE_3
leancrypto-$(CONFIG_LEANCRYPTO_KEM_BIKE_1)				       \
				+= bike1/bike_kem.o
CFLAGS_bike1/bike_kem.o			= -DLC_BIKE_TYPE_1
endif
endif
endif

################################################################################
# Testing Code
################################################################################

ifdef CONFIG_LEANCRYPTO_KEM_BIKE_5
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= bike_5_tester.o
ccflags-y				+= -DCONFIG_LEANCRYPTO_KEM_BIKE_5
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= leancrypto_kernel_bike_5_tester.o
leancrypto_kernel_bike_5_tester-y	+= ../bike/tests/leancrypto_kernel_bike_5_tester.o

bike_5_tester-y			+= ../bike/tests/bike_tester.o		       \
				   ../drng/src/static_rng.o
endif

ifdef CONFIG_LEANCRYPTO_KEM_BIKE_3
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= bike_3_tester.o
ccflags-y				+= -DCONFIG_LEANCRYPTO_KEM_BIKE_3
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= leancrypto_kernel_bike_3_tester.o
leancrypto_kernel_bike_3_tester-y	+= bike3/tests/leancrypto_kernel_bike_3_tester.o

bike_3_tester-y			+= bike3/tests/bike_tester.o		       \
				   ../drng/src/static_rng.o
CFLAGS_bike3/tests/leancrypto_kernel_bike_3_tester.o	= -DLC_BIKE_TYPE_3
CFLAGS_bike3/tests/bike_tester.o		= -DLC_BIKE_TYPE_3
endif


ifdef CONFIG_LEANCRYPTO_KEM_BIKE_1
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= bike_1_tester.o
ccflags-y				+= -DCONFIG_LEANCRYPTO_KEM_BIKE_1
obj-$(CONFIG_LEANCRYPTO_TEST_MODULES)	+= leancrypto_kernel_bike_1_tester.o
leancrypto_kernel_bike_1_tester-y	+= bike1/tests/leancrypto_kernel_bike_1_tester.o

bike_1_tester-y			+= bike1/tests/bike_tester.o		       \
				   ../drng/src/static_rng.o
CFLAGS_bike1/tests/leancrypto_kernel_bike_1_tester.o	= -DLC_BIKE_TYPE_1
CFLAGS_bike1/tests/bike_tester.o		= -DLC_BIKE_TYPE_1
endif
