From fc92620d48475e48c44ef65a8077e2b58cb86386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=A6=E9=85=A5?= Date: Wed, 8 Jan 2025 18:00:24 +0800 Subject: [PATCH] add aarch64 support. --- CMakeLists.txt | 94 +++++++++++++++++++++++++++++++++++++++++++++ Makefile | 61 ----------------------------- README.md | 30 ++++++++++++--- kats_test.c | 2 +- picnic_impl.c | 2 +- sha3/CMakeLists.txt | 7 ++++ sha3/Makefile | 20 ---------- 7 files changed, 128 insertions(+), 88 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 Makefile create mode 100644 sha3/CMakeLists.txt delete mode 100644 sha3/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8a56fb6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,94 @@ +cmake_minimum_required(VERSION 3.21) +project(Picnic C) + +set(CMAKE_C_STANDARD 99) + +# WARNING_FLAGS=-Wall -Wextra -Wpedantic -Wshadow +# we need to remove -Werror since there are two unused functions in KeccakP-1600-reference.c that causes warnings. +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic -Wshadow") +# CFLAGS= -O2 -march=native $(WARNING_FLAGS) -std=gnu99 -D__LINUX__ -D__X64__ -I./sha3 +# NISTKATFLAGS = -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-result +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-result") +# add other configs for aarch64 +IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "(aarch64)|(arm64)") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+simd+crypto+crc -D _ARM64_") +ELSE () + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -D _AMD64_") +ENDIF () + +# build type +IF (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +ENDIF () +message(STATUS "Build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}") +IF (${CMAKE_BUILD_TYPE} MATCHES Debug) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") +ELSEIF (${CMAKE_BUILD_TYPE} MATCHES Release) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2") +ENDIF () + +add_subdirectory(sha3) + +add_library( + picnic + SHARED + hash.c + lowmc_constants.c + picnic.c + picnic_impl.c + picnic3_impl.c + picnic_types.c + tree.c +) + +target_include_directories( + picnic + PUBLIC + sha3 +) + +target_link_libraries( + picnic + PUBLIC + shake +) + +# tool +add_executable(create_test_vectors create_test_vectors.c) +target_link_libraries( + create_test_vectors + PUBLIC + picnic +) + +add_executable(example example.c) +target_link_libraries( + example + PUBLIC + picnic +) + +# test cases +add_executable(kats_test kats_test.c) +target_link_libraries( + kats_test + PUBLIC + picnic +) +add_executable(tree_test tree_test.c) +target_link_libraries( + tree_test + PUBLIC + picnic +) +add_executable(unit_test unit_test.c) +target_link_libraries( + unit_test + PUBLIC + picnic +) + +enable_testing() +add_test(tree_test tree_test) +add_test(unit_test unit_test) +add_test(kats_test kats_test) \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index 5f5d2d0..0000000 --- a/Makefile +++ /dev/null @@ -1,61 +0,0 @@ - -CC=gcc -WARNING_FLAGS=-Wall -Wextra -Wpedantic -Werror -Wshadow -CFLAGS= -O2 -march=native $(WARNING_FLAGS) -std=gnu99 -D__LINUX__ -D__X64__ -I./sha3 -CFLAGS_DEBUG= -g -march=native $(WARNING_FLAGS) -std=gnu99 -D__LINUX__ -D__X64__ -I./sha3 -NISTKATFLAGS = -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-result -SHA3LIB=libshake.a -SHA3_PATH=sha3 -LDFLAGS= $(SHA3_PATH)/$(SHA3LIB) - -SOURCES= picnic_impl.c picnic3_impl.c picnic.c lowmc_constants.c -PICNIC_OBJECTS= picnic_impl.o picnic3_impl.o picnic.o lowmc_constants.o hash.o picnic_types.o tree.o -PICNIC_LIB= libpicnic.a -EXECUTABLE_EXAMPLE=example -EXECUTABLE_TESTVECTORS=create_test_vectors -EXECUTABLE_UNITTEST=unit_test -EXECUTABLE_KATSTEST=kats_test -EXECUTABLE_TREETEST=tree_test - -all: $(SHA3LIB) $(SOURCES) $(PICNIC_LIB) $(EXECUTABLE_EXAMPLE) $(EXECUTABLE_TESTVECTORS) $(EXECUTABLE_UNITTEST) $(EXECUTABLE_KATSTEST) $(EXECUTABLE_TREETEST) - -$(SHA3LIB): - $(MAKE) -C $(SHA3_PATH) - -# debug build -debug: CFLAGS = $(CFLAGS_DEBUG) -debug: all - -$(EXECUTABLE_EXAMPLE): $(EXECUTABLE_EXAMPLE).c $(PICNIC_LIB) - $(CC) $(@).c $(CFLAGS) $(PICNIC_LIB) -o $@ $(LDFLAGS) - -$(EXECUTABLE_UNITTEST): $(EXECUTABLE_UNITTEST).c $(PICNIC_LIB) - $(CC) $(@).c $(CFLAGS) $(PICNIC_LIB) -o $@ $(LDFLAGS) - -$(EXECUTABLE_TREETEST): $(EXECUTABLE_TREETEST).c $(PICNIC_LIB) - $(CC) $(@).c $(CFLAGS) $(PICNIC_LIB) -o $@ $(LDFLAGS) - -$(EXECUTABLE_TESTVECTORS): $(EXECUTABLE_TESTVECTORS).c $(PICNIC_LIB) - $(CC) $(@).c $(CFLAGS) $(PICNIC_LIB) -o $@ $(LDFLAGS) - -$(EXECUTABLE_KATSTEST): $(EXECUTABLE_KATSTEST).c $(PICNIC_LIB) - $(CC) $(@).c $(CFLAGS) $(PICNIC_LIB) -o $@ $(LDFLAGS) - -.c.o: - $(CC) -c $(CFLAGS) $< -o $@ - -$(PICNIC_LIB): $(PICNIC_OBJECTS) - ar rcs $@ $^ - -clean: - rm *.o 2>/dev/null || true - rm *.exe 2>/dev/null || true - rm $(EXECUTABLE_TESTVECTORS) 2>/dev/null || true - rm $(EXECUTABLE_EXAMPLE) 2>/dev/null || true - rm $(EXECUTABLE_UNITTEST) 2>/dev/null || true - rm $(EXECUTABLE_TREETEST) 2>/dev/null || true - rm $(EXECUTABLE_KATSTEST) 2>/dev/null || true - rm $(EXECUTABLE_TESTVECTORS) 2>/dev/null || true - rm $(PICNIC_LIB) 2>/dev/null || true - $(MAKE) -C $(SHA3_PATH) clean - diff --git a/README.md b/README.md index 852f88f..25cc8ec 100644 --- a/README.md +++ b/README.md @@ -10,21 +10,41 @@ The library is provided under the MIT License. The authors are Steven Goldfeder The library builds a static library. The public API surface is defined in [picnic.h](https://github.com/Microsoft/Picnic/blob/master/picnic.h). +## `aarch64` Support + +The library is modified for supporting `aarch64` (including MacBook). The modifications contain: + +- From makefile to CMake: Replace `makefile` to `CMakeList.txt`. +- `kat_test.c`, line 18: change `#define KATDIR "kats"` to `#define KATDIR "../kats"`. +- `picnic_impl.c`, line 806: change `#if defined(__LINUX__)` to `#if defined(__LINUX__) || defined(__linux__) || defined(__MACH__)`. + ## Linux Build Instructions -Tested on Ubuntu Linux, and the Windows Subsystem for Linux on Windows 10 (build 1709). +Tested on MacBook (Apple M1 Pro), Ubuntu Linux, and the Windows Subsystem for Linux on Windows 10 (build 1709). + +### Compile + +```shell +mkdir build # you must create a sub-dictionary, otherwise `kats_test` would fail (because `KATDIR` is defined as `../kats`). +cd build +cmake .. +make +make test +``` + +### Example -1. `make` -This will build the project. `make debug` will build with symbols. +```shell +./example +``` -2. `./example` Runs an example program that exercises the keygen, sign, verify and serialization APIs. See [example.c](https://github.com/Microsoft/Picnic/blob/master/example.c). ## Windows Build Instructions -Tested on Windows 10 with Visual Studio 2017. +Tested on Windows 10 with Visual Studio 2022 (community version). Open the solution in `VisualStudio\picnic.sln`, and build the projects. diff --git a/kats_test.c b/kats_test.c index 20505d9..b6c7f4e 100644 --- a/kats_test.c +++ b/kats_test.c @@ -15,7 +15,7 @@ #include #ifndef KATDIR -#define KATDIR "kats" +#define KATDIR "../kats" #endif #define PICNIC_CONCAT2(a, b) a##_##b #define PICNIC_CONCAT(a, b) PICNIC_CONCAT2(a, b) diff --git a/picnic_impl.c b/picnic_impl.c index e67baad..27c9552 100644 --- a/picnic_impl.c +++ b/picnic_impl.c @@ -803,7 +803,7 @@ void mpc_LowMC(randomTape_t* tapes, view_t views[3], int random_bytes_default(uint8_t* buf, size_t len) { -#if defined(__LINUX__) +#if defined(__LINUX__) || defined(__linux__) || defined(__MACH__) FILE* urandom = fopen("/dev/urandom", "r"); if (urandom == NULL) { return -1; diff --git a/sha3/CMakeLists.txt b/sha3/CMakeLists.txt new file mode 100644 index 0000000..57f903e --- /dev/null +++ b/sha3/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library( + shake + SHARED + KeccakHash.c + KeccakP-1600-reference.c + KeccakSpongeWidth1600.c +) \ No newline at end of file diff --git a/sha3/Makefile b/sha3/Makefile deleted file mode 100644 index 700be87..0000000 --- a/sha3/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -CC=gcc -WARNING_FLAGS=-Wall -Wextra -Wpedantic -Werror -Wno-unused-function -CFLAGS= -O2 -march=native $(WARNING_FLAGS) -std=gnu99 - -SOURCES=$(wildcard *.c) -OBJECTS=$(patsubst %.c,%.o,$(wildcard *.c)) -SHA3LIB=libshake.a - - -all: $(SOURCES) $(SHA3LIB) - -$(SHA3LIB): $(OBJECTS) - ar rcs $@ $^ - -#.c.o: -# $(CC) -c $(CFLAGS) $< -o $@ $(LDFLAGS) - -clean: - rm *.o 2>/dev/null || true - rm libkeccak.a 2>/dev/null || true