Skip to content

Commit 7e8607e

Browse files
committed
refactor: remove large footprint Echo512 impl, switch to C++
1 parent c4e7a40 commit 7e8607e

File tree

5 files changed

+15
-107
lines changed

5 files changed

+15
-107
lines changed

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ if test "$enable_debug" = "yes"; then
379379
AX_CHECK_COMPILE_FLAG([-ftrapv], [DEBUG_CXXFLAGS="$DEBUG_CXXFLAGS -ftrapv"], [], [$CXXFLAG_WERROR])
380380
else
381381
dnl If not debugging, enable more aggressive optimizations for sphlib sources
382-
AX_CHECK_COMPILE_FLAG([-O3], [SPHLIB_CFLAGS="$SPHLIB_CFLAGS -O3"], [], [$CXXFLAG_WERROR])
382+
AX_CHECK_COMPILE_FLAG([-O3], [SPHLIB_FLAGS="$SPHLIB_FLAGS -O3"], [], [$CXXFLAG_WERROR])
383383

384384
# We always enable at at least -g1 debug info to support proper stacktraces in crash infos
385385
# Stacktraces will be suboptimal due to optimization, but better than nothing. Also, -fno-omit-frame-pointer
@@ -1877,7 +1877,7 @@ AC_SUBST(PIC_FLAGS)
18771877
AC_SUBST(PIE_FLAGS)
18781878
AC_SUBST(SANITIZER_CXXFLAGS)
18791879
AC_SUBST(SANITIZER_LDFLAGS)
1880-
AC_SUBST(SPHLIB_CFLAGS)
1880+
AC_SUBST(SPHLIB_FLAGS)
18811881
AC_SUBST(SSE42_CXXFLAGS)
18821882
AC_SUBST(SSE41_CXXFLAGS)
18831883
AC_SUBST(CLMUL_CXXFLAGS)

src/Makefile.am

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,9 @@ crypto_libbitcoin_crypto_avx2_la_SOURCES = crypto/sha256_avx2.cpp
728728
# See explanation for -static in crypto_libbitcoin_crypto_base_la's LDFLAGS and
729729
# CXXFLAGS above
730730
crypto_libbitcoin_crypto_sph_la_LDFLAGS = $(AM_LDFLAGS) -static
731-
crypto_libbitcoin_crypto_sph_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) -static
731+
crypto_libbitcoin_crypto_sph_la_CXXFLAGS = $(AM_CXXFLAGS) $(SPHLIB_FLAGS) $(PIE_FLAGS) -static
732732
crypto_libbitcoin_crypto_sph_la_CPPFLAGS = $(AM_CPPFLAGS)
733-
crypto_libbitcoin_crypto_sph_la_CFLAGS = $(SPHLIB_CFLAGS)
733+
crypto_libbitcoin_crypto_sph_la_CFLAGS = $(SPHLIB_FLAGS)
734734
crypto_libbitcoin_crypto_sph_la_CPPFLAGS += \
735735
-DSPH_SMALL_FOOTPRINT_CUBEHASH=1 \
736736
-DSPH_SMALL_FOOTPRINT_JH=1
@@ -739,7 +739,7 @@ crypto_libbitcoin_crypto_sph_la_SOURCES = \
739739
crypto/x11/blake.c \
740740
crypto/x11/bmw.c \
741741
crypto/x11/cubehash.c \
742-
crypto/x11/echo.c \
742+
crypto/x11/echo.cpp \
743743
crypto/x11/groestl.c \
744744
crypto/x11/jh.c \
745745
crypto/x11/keccak.c \

src/crypto/x11/echo.c renamed to src/crypto/x11/echo.cpp

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535

3636
#include "sph_echo.h"
3737

38-
#ifdef __cplusplus
39-
extern "C"{
40-
#endif
41-
42-
#if SPH_SMALL_FOOTPRINT && !defined SPH_SMALL_FOOTPRINT_ECHO
43-
#define SPH_SMALL_FOOTPRINT_ECHO 1
44-
#endif
45-
4638
/*
4739
* We can use a 64-bit implementation only if a 64-bit type is available.
4840
*/
@@ -69,8 +61,6 @@ extern "C"{
6961
} \
7062
} while (0)
7163

72-
#if SPH_SMALL_FOOTPRINT_ECHO
73-
7464
static void
7565
aes_2rounds_all(sph_u64 W[16][2],
7666
sph_u32 *pK0, sph_u32 *pK1, sph_u32 *pK2, sph_u32 *pK3)
@@ -109,46 +99,6 @@ aes_2rounds_all(sph_u64 W[16][2],
10999
aes_2rounds_all(W, &K0, &K1, &K2, &K3); \
110100
} while (0)
111101

112-
#else
113-
114-
#define AES_2ROUNDS(X) do { \
115-
sph_u32 X0 = (sph_u32)(X[0]); \
116-
sph_u32 X1 = (sph_u32)(X[0] >> 32); \
117-
sph_u32 X2 = (sph_u32)(X[1]); \
118-
sph_u32 X3 = (sph_u32)(X[1] >> 32); \
119-
sph_u32 Y0, Y1, Y2, Y3; \
120-
AES_ROUND_LE(X0, X1, X2, X3, K0, K1, K2, K3, Y0, Y1, Y2, Y3); \
121-
AES_ROUND_NOKEY_LE(Y0, Y1, Y2, Y3, X0, X1, X2, X3); \
122-
X[0] = (sph_u64)X0 | ((sph_u64)X1 << 32); \
123-
X[1] = (sph_u64)X2 | ((sph_u64)X3 << 32); \
124-
if ((K0 = T32(K0 + 1)) == 0) { \
125-
if ((K1 = T32(K1 + 1)) == 0) \
126-
if ((K2 = T32(K2 + 1)) == 0) \
127-
K3 = T32(K3 + 1); \
128-
} \
129-
} while (0)
130-
131-
#define BIG_SUB_WORDS do { \
132-
AES_2ROUNDS(W[ 0]); \
133-
AES_2ROUNDS(W[ 1]); \
134-
AES_2ROUNDS(W[ 2]); \
135-
AES_2ROUNDS(W[ 3]); \
136-
AES_2ROUNDS(W[ 4]); \
137-
AES_2ROUNDS(W[ 5]); \
138-
AES_2ROUNDS(W[ 6]); \
139-
AES_2ROUNDS(W[ 7]); \
140-
AES_2ROUNDS(W[ 8]); \
141-
AES_2ROUNDS(W[ 9]); \
142-
AES_2ROUNDS(W[10]); \
143-
AES_2ROUNDS(W[11]); \
144-
AES_2ROUNDS(W[12]); \
145-
AES_2ROUNDS(W[13]); \
146-
AES_2ROUNDS(W[14]); \
147-
AES_2ROUNDS(W[15]); \
148-
} while (0)
149-
150-
#endif
151-
152102
#define SHIFT_ROW1(a, b, c, d) do { \
153103
sph_u64 tmp; \
154104
tmp = W[a][0]; \
@@ -187,8 +137,6 @@ aes_2rounds_all(sph_u64 W[16][2],
187137
SHIFT_ROW3(3, 7, 11, 15); \
188138
} while (0)
189139

190-
#if SPH_SMALL_FOOTPRINT_ECHO
191-
192140
static void
193141
mix_column(sph_u64 W[16][2], int ia, int ib, int ic, int id)
194142
{
@@ -217,35 +165,6 @@ mix_column(sph_u64 W[16][2], int ia, int ib, int ic, int id)
217165

218166
#define MIX_COLUMN(a, b, c, d) mix_column(W, a, b, c, d)
219167

220-
#else
221-
222-
#define MIX_COLUMN1(ia, ib, ic, id, n) do { \
223-
sph_u64 a = W[ia][n]; \
224-
sph_u64 b = W[ib][n]; \
225-
sph_u64 c = W[ic][n]; \
226-
sph_u64 d = W[id][n]; \
227-
sph_u64 ab = a ^ b; \
228-
sph_u64 bc = b ^ c; \
229-
sph_u64 cd = c ^ d; \
230-
sph_u64 abx = ((ab & C64(0x8080808080808080)) >> 7) * 27U \
231-
^ ((ab & C64(0x7F7F7F7F7F7F7F7F)) << 1); \
232-
sph_u64 bcx = ((bc & C64(0x8080808080808080)) >> 7) * 27U \
233-
^ ((bc & C64(0x7F7F7F7F7F7F7F7F)) << 1); \
234-
sph_u64 cdx = ((cd & C64(0x8080808080808080)) >> 7) * 27U \
235-
^ ((cd & C64(0x7F7F7F7F7F7F7F7F)) << 1); \
236-
W[ia][n] = abx ^ bc ^ d; \
237-
W[ib][n] = bcx ^ a ^ cd; \
238-
W[ic][n] = cdx ^ ab ^ d; \
239-
W[id][n] = abx ^ bcx ^ cdx ^ ab ^ c; \
240-
} while (0)
241-
242-
#define MIX_COLUMN(a, b, c, d) do { \
243-
MIX_COLUMN1(a, b, c, d, 0); \
244-
MIX_COLUMN1(a, b, c, d, 1); \
245-
} while (0)
246-
247-
#endif
248-
249168
#define BIG_MIX_COLUMNS do { \
250169
MIX_COLUMN(0, 1, 2, 3); \
251170
MIX_COLUMN(4, 5, 6, 7); \
@@ -282,7 +201,6 @@ mix_column(sph_u64 W[16][2], int ia, int ib, int ic, int id)
282201
FINAL_BIG; \
283202
} while (0)
284203

285-
286204
#define INCR_COUNTER(sc, val) do { \
287205
sc->C0 = T32(sc->C0 + (sph_u32)(val)); \
288206
if (sc->C0 < (sph_u32)(val)) { \
@@ -408,31 +326,28 @@ echo_big_close(sph_echo_big_context *sc, unsigned ub, unsigned n,
408326

409327
/* see sph_echo.h */
410328
void
411-
sph_echo512_init(void *cc)
329+
sph_echo512_init(sph_echo512_context *cc)
412330
{
413331
echo_big_init(cc, 512);
414332
}
415333

416334
/* see sph_echo.h */
417335
void
418-
sph_echo512(void *cc, const void *data, size_t len)
336+
sph_echo512(sph_echo512_context *cc, const void *data, size_t len)
419337
{
420-
echo_big_core(cc, data, len);
338+
echo_big_core(cc, static_cast<const unsigned char*>(data), len);
421339
}
422340

423341
/* see sph_echo.h */
424342
void
425-
sph_echo512_close(void *cc, void *dst)
343+
sph_echo512_close(sph_echo512_context *cc, void *dst)
426344
{
427345
echo_big_close(cc, 0, 0, dst, 16);
428346
}
429347

430348
/* see sph_echo.h */
431349
void
432-
sph_echo512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
350+
sph_echo512_addbits_and_close(sph_echo512_context *cc, unsigned ub, unsigned n, void *dst)
433351
{
434352
echo_big_close(cc, ub, n, dst, 16);
435353
}
436-
#ifdef __cplusplus
437-
}
438-
#endif

src/crypto/x11/sph_echo.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
#ifndef SPH_ECHO_H__
3737
#define SPH_ECHO_H__
3838

39-
#ifdef __cplusplus
40-
extern "C"{
41-
#endif
42-
4339
#include <stddef.h>
4440
#include "sph_types.h"
4541

@@ -80,7 +76,7 @@ typedef sph_echo_big_context sph_echo512_context;
8076
* @param cc the ECHO-512 context (pointer to a
8177
* <code>sph_echo512_context</code>)
8278
*/
83-
void sph_echo512_init(void *cc);
79+
void sph_echo512_init(sph_echo512_context *cc);
8480

8581
/**
8682
* Process some data bytes. It is acceptable that <code>len</code> is zero
@@ -90,7 +86,7 @@ void sph_echo512_init(void *cc);
9086
* @param data the input data
9187
* @param len the input data length (in bytes)
9288
*/
93-
void sph_echo512(void *cc, const void *data, size_t len);
89+
void sph_echo512(sph_echo512_context *cc, const void *data, size_t len);
9490

9591
/**
9692
* Terminate the current ECHO-512 computation and output the result into
@@ -101,7 +97,7 @@ void sph_echo512(void *cc, const void *data, size_t len);
10197
* @param cc the ECHO-512 context
10298
* @param dst the destination buffer
10399
*/
104-
void sph_echo512_close(void *cc, void *dst);
100+
void sph_echo512_close(sph_echo512_context *cc, void *dst);
105101

106102
/**
107103
* Add a few additional bits (0 to 7) to the current computation, then
@@ -117,10 +113,6 @@ void sph_echo512_close(void *cc, void *dst);
117113
* @param dst the destination buffer
118114
*/
119115
void sph_echo512_addbits_and_close(
120-
void *cc, unsigned ub, unsigned n, void *dst);
121-
122-
#ifdef __cplusplus
123-
}
124-
#endif
116+
sph_echo512_context *cc, unsigned ub, unsigned n, void *dst);
125117

126118
#endif

test/lint/lint-whitespace.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
EXCLUDED_DIRS = ["depends/patches/",
2020
"contrib/guix/patches/",
21+
"src/crypto/x11/",
2122
"src/leveldb/",
2223
"src/crc32c/",
2324
"src/secp256k1/",

0 commit comments

Comments
 (0)