From 48bca3114e2bcb2c1bff835d9aa66523a5a84312 Mon Sep 17 00:00:00 2001 From: Yoda Lee Date: Sat, 19 Oct 2024 10:28:18 +0800 Subject: [PATCH 1/3] Add root CMakeLists and add build to gitignore --- .gitignore | 1 + CMakeLists.txt | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index 3dc5cf9a..2a065d77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ tvecs1024 tvecs512 tvecs768 +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..c4bc764b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +# CMake minimum version and project details +cmake_minimum_required(VERSION 3.16) +project(PQCKyber VERSION 1.0 LANGUAGES C CXX ASM) + +# Set C++ standard +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# Option to enable/disable tests +option(ENABLE_TESTS "Enable Unit Tests" ON) + +# Set the output directories for binaries and libraries +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) \ No newline at end of file From 8fa1ef660b5348ab74daf89cee8f4b7691e5b024 Mon Sep 17 00:00:00 2001 From: Yoda Lee Date: Sat, 19 Oct 2024 11:22:32 +0800 Subject: [PATCH 2/3] Add Cmake file to build ref directory --- CMakeLists.txt | 6 ++- ref/CMakeLists.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ref/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index c4bc764b..3787d697 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,4 +11,8 @@ option(ENABLE_TESTS "Enable Unit Tests" ON) # Set the output directories for binaries and libraries set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) \ No newline at end of file +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + +find_package(OpenSSL REQUIRED) + +add_subdirectory(ref) \ No newline at end of file diff --git a/ref/CMakeLists.txt b/ref/CMakeLists.txt new file mode 100644 index 00000000..351bc774 --- /dev/null +++ b/ref/CMakeLists.txt @@ -0,0 +1,100 @@ + + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpedantic") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wredundant-decls") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z noexecstack") +set(NISTFLAGS "${NISTFLAGS}" -Wno-unused-result -O3 -fomit-frame-pointer) + +set(securitylevel 2 3 4) + +# build randombytes +add_library(randombytes SHARED randombytes.c) + +# build nist rng +add_library(rng SHARED nistkat/rng.c) +target_link_libraries(rng OpenSSL::Crypto) + +set(SRCS + kem.c + indcpa.c + polyvec.c + poly.c + ntt.c + cbd.c + reduce.c + verify.c +) + +# build fips202 shared library +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + set(name pqccrystals_fips202_${NBITS}_ref) + + add_library(${name} SHARED fips202.c symmetric-shake.c) + + target_compile_definitions(${name} PUBLIC KYBER_K=${level}) +endforeach() + +# build algorithm shared library +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + set(name pqccrystals_kyber${NBITS}_ref) + + add_library(${name} SHARED ${SRCS}) + target_compile_definitions(${name} PUBLIC KYBER_K=${level}) +endforeach() + +# build test_kyber +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_kyber${NBITS} test/test_kyber.c) + target_link_libraries(test_kyber${NBITS} + pqccrystals_fips202_${NBITS}_ref + pqccrystals_kyber${NBITS}_ref + randombytes) +endforeach() + +# build test_vector +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_vectors${NBITS} test/test_vectors.c) + target_link_libraries(test_vectors${NBITS} + pqccrystals_fips202_${NBITS}_ref + pqccrystals_kyber${NBITS}_ref) +endforeach() + +# build test_speed +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_speed${NBITS} + test/cpucycles.c + test/speed_print.c + test/test_speed.c + ) + target_link_libraries(test_speed${NBITS} + pqccrystals_fips202_${NBITS}_ref + pqccrystals_kyber${NBITS}_ref + randombytes) +endforeach() + +# build nistkat +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_compile_options(${NISTFLAGS}) + add_executable(PQCgenKAT_kem${NBITS} nistkat/PQCgenKAT_kem.c) + target_link_libraries(PQCgenKAT_kem${NBITS} + pqccrystals_fips202_${NBITS}_ref + pqccrystals_kyber${NBITS}_ref + rng) +endforeach() \ No newline at end of file From dbe8eccc84a7643008f9044398191f3945b39566 Mon Sep 17 00:00:00 2001 From: Yoda Lee Date: Sat, 19 Oct 2024 12:56:31 +0800 Subject: [PATCH 3/3] Add Cmake file to build avx2 directory --- CMakeLists.txt | 3 +- avx2/CMakeLists.txt | 101 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 avx2/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 3787d697..976ba9a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,4 +15,5 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) find_package(OpenSSL REQUIRED) -add_subdirectory(ref) \ No newline at end of file +add_subdirectory(ref) +add_subdirectory(avx2) \ No newline at end of file diff --git a/avx2/CMakeLists.txt b/avx2/CMakeLists.txt new file mode 100644 index 00000000..1898c07a --- /dev/null +++ b/avx2/CMakeLists.txt @@ -0,0 +1,101 @@ + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpedantic") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wredundant-decls") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wpointer-arith") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mbmi2") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mpopcnt") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mtune=native") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z noexecstack") + +set(NISTFLAGS "${NISTFLAGS}" + -Wno-unused-result + -mavx2 + -mbmi2 + -mpopcnt + -march=native + -mtune=native + -O3 + -fomit-frame-pointer +) + +set(securitylevel 2 3 4) + + +# build fips202 shared library +set(SRCS fips202.c fips202x4.c keccak4x/KeccakP-1600-times4-SIMD256.c symmetric-shake.c) + +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + set(name pqccrystals_fips202x4_avx2_${NBITS}) + + add_library(${name} SHARED ${SRCS}) + target_compile_definitions(${name} PUBLIC KYBER_K=${level}) +endforeach() + +set(SRCS + kem.c + indcpa.c + polyvec.c + poly.c + shuffle.S + fq.S + ntt.S + invntt.S + basemul.S + consts.c + rejsample.c + cbd.c + verify.c +) + +# build algorithm shared library +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + set(name pqccrystals_kyber${NBITS}_avx2) + + add_library(${name} SHARED ${SRCS}) + target_compile_definitions(${name} PUBLIC KYBER_K=${level}) + target_link_libraries(${name} pqccrystals_fips202x4_avx2_${NBITS}) + target_include_directories(${name} PRIVATE .) +endforeach() + +# build test_kyber +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_kyber${NBITS}_avx2 test/test_kyber.c) + target_link_libraries(test_kyber${NBITS}_avx2 + pqccrystals_kyber${NBITS}_avx2 + randombytes) +endforeach() + +# build test_vector +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_vectors${NBITS}_avx2 test/test_vectors.c) + target_link_libraries(test_vectors${NBITS}_avx2 + pqccrystals_kyber${NBITS}_avx2) +endforeach() + +# build test_speed +foreach(level IN LISTS securitylevel) + MATH(EXPR NBITS "256 * ${level}" OUTPUT_FORMAT DECIMAL) + + add_executable(test_speed${NBITS}_avx2 + test/cpucycles.c + test/speed_print.c + test/test_speed.c + ) + target_link_libraries(test_speed${NBITS}_avx2 + pqccrystals_kyber${NBITS}_avx2 + randombytes) +endforeach() \ No newline at end of file