Skip to content

Commit 8e7e08a

Browse files
committed
Add support for ESP32-C3
1 parent 1396112 commit 8e7e08a

32 files changed

+88
-85
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2022 Adrian Cinal
1+
Copyright (c) 2022-2024 Adrian Cinal
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

esp32/components/cementite/CMakeLists.txt

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
set(FE3C_ROOT_DIR "../../..")
22
set(FE3C_SRC_DIR "${FE3C_ROOT_DIR}/src")
3-
set(FE3C_PORT_DIR "port")
43

5-
# TODO: Fall back to the reference implementation for RISC-V-based ESP32 chips (condition on CONFIG_IDF_TARGET_ARCH_XTENSA)
4+
set(FE3C_DEFINES
5+
"FE3C_32BIT"
6+
"FE3C_LILENDIAN_TARGET"
7+
)
8+
9+
if (CONFIG_IDF_TARGET_ARCH_XTENSA)
10+
set(FE3C_PORT_DIR "port/xtensa")
11+
list(APPEND FE3C_DEFINES "FE3C_PORT_OVERRIDES")
12+
elseif (CONFIG_IDF_TARGET_ARCH_RISCV)
13+
# No target-specific port available for RISC-V at this point
14+
set(FE3C_PORT_DIR ${FE3C_SRC_DIR})
15+
endif()
616

717
set(FE3C_SOURCES
818
"${FE3C_SRC_DIR}/eddsa/eddsa.c"
919
"${FE3C_SRC_DIR}/curves/curves.c"
1020
)
1121

12-
set(FE3C_DEFINES
13-
"FE3C_32BIT"
14-
"FE3C_LILENDIAN_TARGET"
15-
"FE3C_PORT_OVERRIDES"
16-
)
17-
1822
if (CONFIG_FE3C_ENABLE_SANITY_CHECKS)
1923
list(APPEND FE3C_DEFINES "FE3C_ENABLE_SANITY_CHECKS")
2024
endif()
@@ -46,7 +50,6 @@ if (CONFIG_FE3C_SUPPORT_CURVE_ED25519)
4650
list(APPEND FE3C_SOURCES "${FE3C_PORT_DIR}/scalars/scalars_ed25519_32.c")
4751
list(APPEND FE3C_SOURCES "${FE3C_PORT_DIR}/field_elements/field_elements_ed25519_32.c")
4852
list(APPEND FE3C_SOURCES "${FE3C_PORT_DIR}/hash_functions/hash_function_sha512.c")
49-
list(APPEND FE3C_SOURCES "${FE3C_PORT_DIR}/hash_functions/hash_function_sha512_impl_sw.c")
5053

5154
if (CONFIG_FE3C_COMB_METHOD)
5255
list(APPEND FE3C_SOURCES "${FE3C_SRC_DIR}/points/comb/comb_ed25519${ed25519_small_precomp_tag}.c")
@@ -93,7 +96,7 @@ endif()
9396

9497
idf_component_register(SRCS ${FE3C_SOURCES}
9598
INCLUDE_DIRS "${FE3C_ROOT_DIR}/include"
96-
PRIV_INCLUDE_DIRS "."
99+
PRIV_INCLUDE_DIRS "include"
97100
"${FE3C_SRC_DIR}"
98101
# We need a dependency to mbedtls when using the SHA-2 hardware, to use their API
99102
# and this way not risk any race conditions. Due to the two-pass nature of CMake

esp32/components/cementite/Kconfig

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ menu "cementite (fe3c)"
4949

5050
config FE3C_PARALLEL_COMB_METHOD
5151
bool "Use parallel comb method"
52-
depends on FE3C_COMB_METHOD
52+
depends on FE3C_COMB_METHOD && IDF_TARGET_ARCH_XTENSA
5353
default n
5454
help
5555
Use a parallel implementation of the comb method which yields great performance benefits
5656
in systems with small CPU load.
5757

5858
config FE3C_USE_SHA512_HARDWARE
5959
bool "Use SHA512 hardware acceleration when possible"
60-
depends on FE3C_SUPPORT_CURVE_ED25519
60+
depends on FE3C_SUPPORT_CURVE_ED25519 && IDF_TARGET_ARCH_XTENSA
6161
default n
6262
help
6363
Use SHA512 hardware acceleration when possible. This will significantly

esp32/components/cementite/port/hash_functions/hash_function_sha512.c

-21
This file was deleted.

esp32/components/cementite/port/points/comb/comb_parallel_ed25519.h

-9
This file was deleted.

esp32/components/cementite/port/points/comb/comb_parallel_ed448.h

-9
This file was deleted.

esp32/components/cementite/port/hash_functions/hash_function_sha512_impl_sw.c esp32/components/cementite/port/xtensa/hash_functions/hash_function_sha512.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <hash_functions/hash_function_sha512.h>
2-
#include <port/hash_functions/hash_function_sha512_esp32.h>
2+
#include "hash_function_sha512_esp32.h"
33
#include <utils/utils.h>
44
#include <xtensa/config/core-isa.h>
55
#include <xtensa/xtensa-versions.h>
@@ -233,7 +233,7 @@ static inline void sha512_prepare_message_schedule(u64 * schedule, const u8 * in
233233
static inline void store_64(u8 * dst, const u64 * src, size_t wordcount);
234234
static inline void load_64(u64 * dst, const u8 * src, size_t wordcount);
235235

236-
void sha512_impl_sw(u8 * output, const struct iovec * iov, int iovcnt) {
236+
static inline void sha512_impl_sw(u8 * output, const struct iovec * iov, int iovcnt) {
237237

238238
FE3C_SANITY_CHECK(output, NULL);
239239
FE3C_SANITY_CHECK(iov || iovcnt == 0, NULL);
@@ -645,3 +645,22 @@ static inline void load_64(u64 * dst, const u8 * src, size_t wordcount) {
645645
dst[i] = __builtin_bswap64(*(u64 *) &src[8 * i]);
646646
}
647647
}
648+
649+
void hash_sha512(u8 * output, const struct iovec * iov, int iovcnt) {
650+
651+
#if FE3C_USE_SHA512_HARDWARE
652+
sha512_impl impl_select = sha512_try_lock_hw();
653+
if (impl_select == sha512_hw_acceleration) {
654+
655+
/* Try relying on the hardware accelerator. If that fails, we fall back to software. */
656+
int hw_error = sha512_impl_hw(output, iov, iovcnt);
657+
sha512_release_hw();
658+
if (0 == hw_error) {
659+
return;
660+
}
661+
662+
/* Hardware failed us. Do it the boring way. */
663+
}
664+
#endif
665+
sha512_impl_sw(output, iov, iovcnt);
666+
}

esp32/components/cementite/port/hash_functions/hash_function_sha512_esp32.h esp32/components/cementite/port/xtensa/hash_functions/hash_function_sha512_esp32.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11

2-
#ifndef __FE3C_PORT_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H
3-
#define __FE3C_PORT_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H
2+
#ifndef __FE3C_PORT_XTENSA_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H
3+
#define __FE3C_PORT_XTENSA_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H
44

55
#include <hash_functions/hash_functions.h>
66

7-
void sha512_impl_sw(u8 * output, const struct iovec * iov, int iovcnt);
8-
97
#if FE3C_USE_SHA512_HARDWARE
108
/* Note that sha512_impl_hw subverts the convention used in our other APIs where 1 indicates success, and 0 indicates failure.
119
* Here we use the good old POSIX convention, where 0 stands for no errors, and anything else is an error code. */
@@ -20,4 +18,4 @@ sha512_impl sha512_try_lock_hw(void);
2018
void sha512_release_hw(void);
2119
#endif /* FE3C_USE_SHA512_HARDWARE */
2220

23-
#endif /* __FE3C_PORT_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H */
21+
#endif /* __FE3C_PORT_XTENSA_HASH_FUNCTIONS_HASH_FUNCTION_SHA512_ESP32_H */

esp32/components/cementite/port/hash_functions/hash_function_sha512_impl_hw.c esp32/components/cementite/port/xtensa/hash_functions/hash_function_sha512_impl_hw.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <port/hash_functions/hash_function_sha512_esp32.h>
1+
#include "hash_function_sha512_esp32.h"
22
#include <utils/utils.h>
33

44
/* Redefine some constants instead of including hash_functions/hash_function_sha512.h, since the

esp32/components/cementite/port/points/comb/comb_parallel.c esp32/components/cementite/port/xtensa/points/comb/comb_parallel.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#include <port/points/comb/comb_parallel.h>
1+
#include "comb_parallel.h"
22
#include <utils/utils.h>
33
#include <freertos/FreeRTOS.h>
44
#include <freertos/task.h>
55
#include <freertos/semphr.h>
66
#include <freertos/queue.h>
77

88
#if FE3C_SUPPORT_CURVE_ED25519
9-
#include <port/points/comb/comb_parallel_ed25519.h>
9+
#include "comb_parallel_ed25519.h"
1010
#endif /* FE3C_SUPPORT_CURVE_ED25519 */
1111

1212
#if FE3C_SUPPORT_CURVE_ED448
13-
#include <port/points/comb/comb_parallel_ed448.h>
13+
#include "comb_parallel_ed448.h"
1414
#endif /* FE3C_SUPPORT_CURVE_ED448 */
1515

1616
#define COMB_THREAD_STACK_SIZE ( 2 * 1024 )

esp32/components/cementite/port/points/comb/comb_parallel.h esp32/components/cementite/port/xtensa/points/comb/comb_parallel.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

3-
#ifndef __FE3C_PORT_POINTS_COMB_COMB_PARALLEL_H
4-
#define __FE3C_PORT_POINTS_COMB_COMB_PARALLEL_H
3+
#ifndef __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_H
4+
#define __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_H
55

66
#include <fe3c/eddsa.h>
77
#include <points/points.h>
@@ -15,4 +15,4 @@ typedef struct comb_thread_work {
1515
int comb_dispatch_loop_to_thread(comb_thread_work * work);
1616
void comb_join_worker_thread(void);
1717

18-
#endif /* __FE3C_PORT_POINTS_COMB_COMB_PARALLEL_H */
18+
#endif /* __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_H */

esp32/components/cementite/port/points/comb/comb_parallel_ed25519.c esp32/components/cementite/port/xtensa/points/comb/comb_parallel_ed25519.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <port/points/comb/comb_parallel_ed25519.h>
1+
#include "comb_parallel_ed25519.h"
22
#include <points/comb/comb_ed25519.h>
33
#include <field_elements/field_elements_ed25519.h>
44
#include <utils/utils.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#ifndef __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED25519_H
3+
#define __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED25519_H
4+
5+
#include <points/points.h>
6+
7+
void ed25519_comb_loop(point_ed25519 * result, const i8 * scalar_recoding, int odd);
8+
9+
#endif /* __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED25519_H */

esp32/components/cementite/port/points/comb/comb_parallel_ed448.c esp32/components/cementite/port/xtensa/points/comb/comb_parallel_ed448.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <port/points/comb/comb_parallel_ed448.h>
1+
#include "comb_parallel_ed448.h"
22
#include <points/comb/comb_ed448.h>
33
#include <field_elements/field_elements_ed448.h>
44
#include <utils/utils.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#ifndef __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED448_H
3+
#define __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED448_H
4+
5+
#include <points/points.h>
6+
7+
void ed448_comb_loop(point_ed448 * result, const i8 * scalar_recoding, int odd);
8+
9+
#endif /* __FE3C_PORT_XTENSA_POINTS_COMB_COMB_PARALLEL_ED448_H */

esp32/components/cementite/port/points/points_ed25519.c esp32/components/cementite/port/xtensa/points/points_ed25519.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#if !FE3C_COMB_METHOD
2-
#error "Build system inconsistency detected! port/points/points_ed25519.c in use despite FE3C_COMB_METHOD not being set"
2+
#error "Build system inconsistency detected! points_ed25519.c in use despite FE3C_COMB_METHOD not being set"
33
#endif /* !FE3C_COMB_METHOD */
44

55
#include <points/points.h>
66
#include <field_elements/field_elements_ed25519.h>
77
#include <utils/utils.h>
88
#include <points/comb/comb_ed25519.h>
9-
#include <port/points/comb/comb_parallel.h>
10-
#include <port/points/comb/comb_parallel_ed25519.h>
9+
#include "comb/comb_parallel.h"
10+
#include "comb/comb_parallel_ed25519.h"
1111

1212
#define ED25519_STR \
1313
" X = " FE25519_STR "\n" \

esp32/components/cementite/port/points/points_ed448.c esp32/components/cementite/port/xtensa/points/points_ed448.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#if !FE3C_COMB_METHOD
2-
#error "Build system inconsistency detected! port/points/points_ed448.c in use despite FE3C_COMB_METHOD not being set"
2+
#error "Build system inconsistency detected! points_ed448.c in use despite FE3C_COMB_METHOD not being set"
33
#endif /* !FE3C_COMB_METHOD */
44

55
#include <points/points.h>
66
#include <field_elements/field_elements_ed448.h>
77
#include <utils/utils.h>
88
#include <points/comb/comb_ed448.h>
9-
#include <port/points/comb/comb_parallel.h>
10-
#include <port/points/comb/comb_parallel_ed448.h>
9+
#include "comb/comb_parallel.h"
10+
#include "comb/comb_parallel_ed448.h"
1111

1212
#define ED448_STR \
1313
" X = " FE448_STR "\n" \

esp32/components/cementite/test/CMakeLists.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ set(FE3C_UT_DEFINES
33
"FE3C_FUNCTIONAL_TESTS_ESP32"
44
"FE3C_32BIT"
55
"FE3C_LILENDIAN_TARGET"
6-
"FE3C_PORT_OVERRIDES"
76
)
87

8+
set(FE3C_UUT_COMMON_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../../../src")
9+
set(FE3C_UUT_COMMON_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../../../include")
10+
set(FE3C_UUT_PORT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../../../esp32/components/cementite/include")
11+
set(FE3C_UT_COMMON_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../src")
12+
if (CONFIG_IDF_TARGET_ARCH_XTENSA)
13+
set(FE3C_UT_PORT_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../../../esp32/components/cementite/test/port/xtensa")
14+
list(APPEND FE3C_UT_DEFINES "FE3C_PORT_OVERRIDES")
15+
elseif (CONFIG_IDF_TARGET_ARCH_RISCV)
16+
# No target-specific port available for RISC-V at this point
17+
set(FE3C_UT_PORT_SOURCE_DIR ${FE3C_UT_COMMON_SOURCE_DIR})
18+
endif()
19+
920
if (CONFIG_FE3C_SUPPORT_CURVE_ED25519)
1021
list(APPEND FE3C_UT_DEFINES "FE3C_SUPPORT_CURVE_ED25519")
1122
list(APPEND UT_SOURCES

src/field_elements/field_elements.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern "C" {
77
#endif
88

99
#if FE3C_PORT_OVERRIDES
10-
#include <port/field_elements/overrides.h>
10+
#include <field_elements/port_overrides.h>
1111
#endif /* FE3C_PORT_OVERRIDES */
1212

1313
#include <global_defs.h>

tests/benchmarks/esp32/main/bench.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ typedef struct esp32_bench_stopwatch {
2626
void app_main(void) {
2727

2828
/* Spawn a new task to have strict control over the stack size */
29-
BaseType_t ret = xTaskCreatePinnedToCore(
29+
BaseType_t ret = xTaskCreate(
3030
benchmark_task,
3131
"benchmark",
3232
BENCHMARK_TASK_STACK_DEPTH,
3333
NULL,
3434
BENCHMARK_TASK_PRIORITY,
35-
NULL,
36-
1
35+
NULL
3736
);
3837
assert(pdPASS == ret);
3938
}

tests/functional/esp32/CMakeLists.txt

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
# CMakeLists in this exact order for cmake to work correctly
55
cmake_minimum_required(VERSION 3.16)
66

7-
set(FE3C_UUT_COMMON_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../../../src")
8-
set(FE3C_UUT_COMMON_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../../../include")
9-
set(FE3C_UUT_PORT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../../../esp32/components/cementite")
10-
set(FE3C_UT_COMMON_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../src")
11-
set(FE3C_UT_PORT_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../../../esp32/components/cementite/test/port")
12-
137
set(EXTRA_COMPONENT_DIRS "${CMAKE_SOURCE_DIR}/../../../esp32/components")
148
set(TEST_COMPONENTS "cementite")
159

0 commit comments

Comments
 (0)