Skip to content

Commit cba39c5

Browse files
committed
const: use function pointer to allow for switching implementation
1 parent d6d3518 commit cba39c5

File tree

12 files changed

+215
-159
lines changed

12 files changed

+215
-159
lines changed

src/Makefile.am

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,12 @@ crypto_libbitcoin_crypto_sph_la_CPPFLAGS += \
735735
-DSPH_SMALL_FOOTPRINT_CUBEHASH=1 \
736736
-DSPH_SMALL_FOOTPRINT_JH=1
737737
crypto_libbitcoin_crypto_sph_la_SOURCES = \
738-
crypto/x11/aes_helper.hpp \
738+
crypto/x11/aes.cpp \
739739
crypto/x11/blake.c \
740740
crypto/x11/bmw.c \
741741
crypto/x11/cubehash.c \
742+
crypto/x11/dispatch.cpp \
743+
crypto/x11/dispatch.h \
742744
crypto/x11/echo.cpp \
743745
crypto/x11/groestl.c \
744746
crypto/x11/jh.c \
@@ -758,7 +760,9 @@ crypto_libbitcoin_crypto_sph_la_SOURCES = \
758760
crypto/x11/sph_shavite.h \
759761
crypto/x11/sph_simd.h \
760762
crypto/x11/sph_skein.h \
761-
crypto/x11/sph_types.h
763+
crypto/x11/sph_types.h \
764+
crypto/x11/util/consts_aes.hpp \
765+
crypto/x11/util/util.hpp
762766

763767
# See explanation for -static in crypto_libbitcoin_crypto_base_la's LDFLAGS and
764768
# CXXFLAGS above

src/bench/bench_bitcoin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <clientversion.h>
88
#include <crypto/sha256.h>
9+
#include <crypto/x11/dispatch.h>
910
#include <fs.h>
1011
#include <util/strencodings.h>
1112
#include <util/system.h>
@@ -61,6 +62,7 @@ int main(int argc, char** argv)
6162
{
6263
ArgsManager argsman;
6364
SetupBenchArgs(argsman);
65+
SapphireAutoDetect();
6466
SHA256AutoDetect();
6567
std::string error;
6668
if (!argsman.ParseParameters(argc, argv, error)) {

src/crypto/x11/aes.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#include <crypto/x11/dispatch.h>
6+
#include <crypto/x11/util/consts_aes.hpp>
7+
8+
#include <cstdint>
9+
10+
namespace sapphire {
11+
namespace soft_aes {
12+
void Round(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
13+
uint32_t k0, uint32_t k1, uint32_t k2, uint32_t k3,
14+
uint32_t& y0, uint32_t& y1, uint32_t& y2, uint32_t& y3)
15+
{
16+
using namespace consts;
17+
y0 = aes_tbox_le[0][(x0) & 0xff] ^ aes_tbox_le[1][((x1) >> 8) & 0xff] ^ aes_tbox_le[2][((x2) >> 16) & 0xff] ^ aes_tbox_le[3][((x3) >> 24) & 0xff] ^ (k0);
18+
y1 = aes_tbox_le[0][(x1) & 0xff] ^ aes_tbox_le[1][((x2) >> 8) & 0xff] ^ aes_tbox_le[2][((x3) >> 16) & 0xff] ^ aes_tbox_le[3][((x0) >> 24) & 0xff] ^ (k1);
19+
y2 = aes_tbox_le[0][(x2) & 0xff] ^ aes_tbox_le[1][((x3) >> 8) & 0xff] ^ aes_tbox_le[2][((x0) >> 16) & 0xff] ^ aes_tbox_le[3][((x1) >> 24) & 0xff] ^ (k2);
20+
y3 = aes_tbox_le[0][(x3) & 0xff] ^ aes_tbox_le[1][((x0) >> 8) & 0xff] ^ aes_tbox_le[2][((x1) >> 16) & 0xff] ^ aes_tbox_le[3][((x2) >> 24) & 0xff] ^ (k3);
21+
}
22+
23+
void RoundKeyless(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
24+
uint32_t& y0, uint32_t& y1, uint32_t& y2, uint32_t& y3)
25+
{
26+
Round(x0, x1, x2, x3, /*k0=*/0, /*k1=*/0, /*k2=*/0, /*k3=*/0, y0, y1, y2, y3);
27+
}
28+
} // namespace soft_aes
29+
} // namespace sapphire
30+
31+
sapphire::dispatch::AESRoundFn aes_round = sapphire::soft_aes::Round;
32+
sapphire::dispatch::AESRoundFnNk aes_round_nk = sapphire::soft_aes::RoundKeyless;

src/crypto/x11/aes_helper.hpp

Lines changed: 0 additions & 146 deletions
This file was deleted.

src/crypto/x11/dispatch.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#include <crypto/x11/dispatch.h>
6+
7+
#include <cstdint>
8+
9+
namespace sapphire {
10+
namespace soft_aes {
11+
void Round(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
12+
uint32_t k0, uint32_t k1, uint32_t k2, uint32_t k3,
13+
uint32_t& y0, uint32_t& y1, uint32_t& y2, uint32_t& y3);
14+
void RoundKeyless(uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3,
15+
uint32_t& y0, uint32_t& y1, uint32_t& y2, uint32_t& y3);
16+
} // namespace soft_aes
17+
} // namespace sapphire
18+
19+
extern sapphire::dispatch::AESRoundFn aes_round;
20+
extern sapphire::dispatch::AESRoundFnNk aes_round_nk;
21+
22+
void SapphireAutoDetect()
23+
{
24+
aes_round = sapphire::soft_aes::Round;
25+
aes_round_nk = sapphire::soft_aes::RoundKeyless;
26+
}

src/crypto/x11/dispatch.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#ifndef BITCOIN_CRYPTO_X11_DISPATCH_H
6+
#define BITCOIN_CRYPTO_X11_DISPATCH_H
7+
8+
#include <cstdint>
9+
10+
namespace sapphire {
11+
namespace dispatch {
12+
typedef void (*AESRoundFn)(uint32_t, uint32_t, uint32_t, uint32_t,
13+
uint32_t, uint32_t, uint32_t, uint32_t,
14+
uint32_t&, uint32_t&, uint32_t&, uint32_t&);
15+
typedef void (*AESRoundFnNk)(uint32_t, uint32_t, uint32_t, uint32_t,
16+
uint32_t&, uint32_t&, uint32_t&, uint32_t&);
17+
} // namespace dispatch
18+
} // namespace sapphire
19+
20+
void SapphireAutoDetect();
21+
22+
#endif // BITCOIN_CRYPTO_X11_DISPATCH_H

src/crypto/x11/echo.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@
3030
* @author Thomas Pornin <thomas.pornin@cryptolog.com>
3131
*/
3232

33-
#include <stddef.h>
34-
#include <string.h>
33+
#include <crypto/x11/dispatch.h>
34+
35+
#include <cstddef>
36+
#include <cstring>
3537

3638
#include "sph_echo.h"
3739

40+
extern sapphire::dispatch::AESRoundFn aes_round;
41+
extern sapphire::dispatch::AESRoundFnNk aes_round_nk;
42+
3843
/*
3944
* We can use a 64-bit implementation only if a 64-bit type is available.
4045
*/
@@ -45,8 +50,6 @@
4550
#define T32 SPH_T32
4651
#define C64 SPH_C64
4752

48-
#include "aes_helper.hpp"
49-
5053
#define DECL_STATE_BIG \
5154
sph_u64 W[16][2];
5255

@@ -79,8 +82,8 @@ aes_2rounds_all(sph_u64 W[16][2],
7982
sph_u32 X2 = (sph_u32)Wh;
8083
sph_u32 X3 = (sph_u32)(Wh >> 32);
8184
sph_u32 Y0, Y1, Y2, Y3; \
82-
AES_ROUND_LE(X0, X1, X2, X3, K0, K1, K2, K3, Y0, Y1, Y2, Y3);
83-
AES_ROUND_NOKEY_LE(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
85+
aes_round(X0, X1, X2, X3, K0, K1, K2, K3, Y0, Y1, Y2, Y3);
86+
aes_round_nk(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
8487
W[n][0] = (sph_u64)X0 | ((sph_u64)X1 << 32);
8588
W[n][1] = (sph_u64)X2 | ((sph_u64)X3 << 32);
8689
if ((K0 = T32(K0 + 1)) == 0) {

src/crypto/x11/shavite.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@
3030
* @author Thomas Pornin <thomas.pornin@cryptolog.com>
3131
*/
3232

33-
#include <stddef.h>
34-
#include <string.h>
33+
#include <crypto/x11/dispatch.h>
34+
35+
#include <cstddef>
36+
#include <cstring>
3537

3638
#include "sph_shavite.h"
3739

40+
extern sapphire::dispatch::AESRoundFnNk aes_round_nk;
41+
3842
#ifdef _MSC_VER
3943
#pragma warning (disable: 4146)
4044
#endif
@@ -53,8 +57,6 @@
5357
* is commented out afterwards.
5458
*/
5559

56-
#include "aes_helper.hpp"
57-
5860
static const sph_u32 IV512[] = {
5961
C32(0x72FCCDD8), C32(0x79CA4727), C32(0x128A077B), C32(0x40D55AEC),
6062
C32(0xD1901A06), C32(0x430AE307), C32(0xB29F5CD1), C32(0xDF07FBFC),
@@ -63,7 +65,7 @@ static const sph_u32 IV512[] = {
6365
};
6466

6567
#define AES_ROUND_NOKEY(x0, x1, x2, x3) do { \
66-
AES_ROUND_NOKEY_LE(x0, x1, x2, x3, x0, x1, x2, x3); \
68+
aes_round_nk(x0, x1, x2, x3, x0, x1, x2, x3); \
6769
} while (0)
6870

6971
/*

0 commit comments

Comments
 (0)