Skip to content

Commit 959c9ee

Browse files
committed
crypto: implement ARM AES backend for Echo512's FullStateRound()
1 parent 76bd236 commit 959c9ee

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ crypto_libbitcoin_crypto_arm_aes_la_CPPFLAGS = $(AM_CPPFLAGS)
785785
crypto_libbitcoin_crypto_arm_aes_la_CXXFLAGS += $(ARM_AES_CXXFLAGS)
786786
crypto_libbitcoin_crypto_arm_aes_la_CPPFLAGS += -DENABLE_ARM_AES
787787
crypto_libbitcoin_crypto_arm_aes_la_SOURCES = \
788-
crypto/x11/arm_crypto/aes.cpp
788+
crypto/x11/arm_crypto/aes.cpp \
789+
crypto/x11/arm_crypto/echo.cpp
789790

790791
# See explanation for -static in crypto_libbitcoin_crypto_base_la's LDFLAGS and
791792
# CXXFLAGS above

src/crypto/x11/arm_crypto/echo.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2025 The Dash Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#if defined(ENABLE_ARM_AES)
6+
#include <crypto/x11/util/util.hpp>
7+
8+
#include <cstdint>
9+
10+
#include <arm_neon.h>
11+
12+
namespace sapphire {
13+
namespace arm_crypto_echo {
14+
void FullStateRound(uint64_t W[16][2], uint32_t& k0, uint32_t& k1, uint32_t& k2, uint32_t& k3)
15+
{
16+
uint8x16_t key = util::pack_le(k0, k1, k2, k3);
17+
for (int n = 0; n < 16; n++) {
18+
uint8x16_t block = vreinterpretq_u8_u64(vld1q_u64(&W[n][0]));
19+
block = util::aes_round(block, key);
20+
block = util::aes_round_nk(block);
21+
vst1q_u64(&W[n][0], vreinterpretq_u64_u8(block));
22+
23+
util::unpack_le(key, k0, k1, k2, k3);
24+
if ((k0 = (k0 + 1)) == 0) {
25+
if ((k1 = (k1 + 1)) == 0) {
26+
if ((k2 = (k2 + 1)) == 0) {
27+
k3 = (k3 + 1);
28+
}
29+
}
30+
}
31+
key = util::pack_le(k0, k1, k2, k3);
32+
}
33+
util::unpack_le(key, k0, k1, k2, k3);
34+
}
35+
} // namespace arm_crypto_echo
36+
} // namespace sapphire
37+
38+
#endif // ENABLE_ARM_AES

src/crypto/x11/dispatch.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#include <sys/auxv.h>
2323
#endif // __linux__
2424

25+
#if defined(__FreeBSD__)
26+
#include <machine/elf.h>
27+
#include <sys/auxv.h>
28+
#endif // __FreeBSD__
29+
2530
#if defined(_WIN32)
2631
#include <processthreadsapi.h>
2732
#include <winnt.h>
@@ -41,6 +46,9 @@ void Round(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
4146
void RoundKeyless(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
4247
uint32_t& y0, uint32_t& y1, uint32_t& y2, uint32_t& y3);
4348
} // namespace arm_crypto_aes
49+
namespace arm_crypto_echo {
50+
void FullStateRound(uint64_t W[16][2], uint32_t& k0, uint32_t& k1, uint32_t& k2, uint32_t& k3);
51+
} // namespace arm_crypto_echo
4452
#endif // ENABLE_ARM_AES
4553

4654
#if defined(ENABLE_SSSE3)
@@ -139,13 +147,24 @@ void SapphireAutoDetect()
139147
#endif // __aarch64__
140148
#endif // __linux__
141149

150+
#if defined(__FreeBSD__)
151+
[[maybe_unused]] unsigned long hwcap{0};
152+
#if defined(__arm__)
153+
have_arm_aes = ((::elf_aux_info(AT_HWCAP2, &hwcap, sizeof(hwcap)) == 0) && ((hwcap & HWCAP2_AES) != 0));
154+
#endif // __arm__
155+
#if defined(__aarch64__)
156+
have_arm_aes = ((::elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)) == 0) && ((hwcap & HWCAP_AES) != 0));
157+
#endif // __aarch64__
158+
#endif // __FreeBSD__
159+
142160
#if defined(_WIN32)
143161
have_arm_aes = ::IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
144162
#endif // _WIN32
145163

146164
if (have_arm_aes) {
147165
aes_round = sapphire::arm_crypto_aes::Round;
148166
aes_round_nk = sapphire::arm_crypto_aes::RoundKeyless;
167+
echo_round = sapphire::arm_crypto_echo::FullStateRound;
149168
}
150169
#endif // ENABLE_ARM_AES
151170
#endif // !DISABLE_OPTIMIZED_SHA256

0 commit comments

Comments
 (0)