Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport: Add ChaCha20 encryption option (XOR)/Unroll the ChaCha20 inner loop for performance #3523

Open
wants to merge 60 commits into
base: 1.15.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
de911c4
qt: remove BIP70
patricklodder Mar 2, 2024
8fadf22
build: remove protobuf requirement
patricklodder Mar 2, 2024
8f96a38
[squashme] remove -enable-bip70 flag from init.cpp
patricklodder Apr 1, 2024
c43d021
Fix -Wthread-safety-analysis warnings. Change the sync.h primitives t…
TheBlueMatt Aug 16, 2017
506cc94
Switched sync.{cpp,h} to std threading primitives.
tjps Nov 18, 2017
6e862f8
sync: remove boost foreach
xanimo Apr 16, 2024
995eda1
threads: add a thread_local autoconf check
theuni Nov 27, 2017
29dd4a0
Add unit test for DEBUG_LOCKORDER code
ryanofsky Nov 8, 2017
aa606a3
MOVEONLY Move AnnotatedMixin declaration
ryanofsky Dec 7, 2017
79afc95
Make LOCK, LOCK2, TRY_LOCK work with CWaitableCriticalSection
ryanofsky Nov 8, 2017
7a52292
Use LOCK macros for non-recursive locks
ryanofsky Nov 8, 2017
fc90d70
scripted-diff: Small locking rename
ryanofsky Nov 3, 2017
e62a345
Introduce FastRandomContext::randbool()
sipa Feb 18, 2017
180f7b0
FastRandom benchmark
sipa Feb 16, 2017
b6ef7e9
Add ChaCha20
sipa Feb 15, 2017
2e80b49
util: Specific GetOSRandom for Linux/FreeBSD/OpenBSD
xanimo Apr 16, 2024
29de1fc
sanity: Move OS random to sanity check function
laanwj Feb 22, 2017
ff6eab4
random: Add fallback if getrandom syscall not available
laanwj Feb 22, 2017
10d2504
Switch FastRandomContext to ChaCha20
sipa Feb 16, 2017
eb3c8da
Add a FastRandomContext::randrange and use it
sipa Feb 25, 2017
c88237a
Add FastRandomContext::rand256() and ::randbytes()
sipa Mar 27, 2024
92b3327
Merge test_random.h into test_bitcoin.h
sipa Mar 26, 2024
d378c5c
Add various insecure_rand wrappers for tests
sipa May 23, 2017
c1ef69e
scripted-diff: use insecure_rand256/randrange more
sipa Mar 27, 2024
bd75b67
Replace more rand() % NUM by randranges
sipa Jun 7, 2017
4ef34f5
Replace rand() & ((1 << N) - 1) with randbits(N)
sipa Jun 7, 2017
7900e3d
Use randbits instead of ad-hoc emulation in prevector tests
sipa May 23, 2017
e037f9c
scripted-diff: Use randbits/bool instead of randrange where possible
sipa May 23, 2017
1316bf0
scripted-diff: Use new naming style for insecure_rand* functions
sipa Jun 7, 2017
cbbcc6f
Check if sys/random.h is required for getentropy on OSX.
jameshilliard Jul 27, 2017
2f497fc
random: getentropy on macOS does not need unistd.h
fanquake May 19, 2023
16f0ffe
Use hardware timestamps in RNG seeding
sipa May 3, 2017
631e3a9
Test that GetPerformanceCounter() increments
sipa May 5, 2017
d45bace
Use sanity check timestamps as entropy
sipa May 5, 2017
3e75d69
Use rdrand as entropy source on supported platforms
sipa May 9, 2017
543962d
Initialize randomness in benchmarks
achow101 Jun 23, 2017
6973e5e
random: fix crash on some 64bit platforms
theuni Jun 16, 2017
681d662
Use cpuid intrinsics instead of asm code
sipa Jul 13, 2017
8512ba5
Clarify entropy source
sipa Jul 14, 2017
2d34354
Fix resource leak
Jul 15, 2017
5d6cdff
Minimal code changes to allow msvc compilation.
sipsorcery Nov 9, 2017
a74d965
Make FastRandomContext support standard C++11 RNG interface
sipa Mar 21, 2018
a5b036b
Don't log RandAddSeedPerfmon details
sipa Dec 19, 2018
7a97961
Automatically initialize RNG on first use.
sipa Dec 18, 2018
549026c
Rename some hardware RNG related functions
sipa Jan 16, 2019
1d8f3cb
Add thread safety annotations to RNG state
sipa Dec 18, 2018
a9455f9
Abstract out seeding/extracting entropy into RNGState::MixExtract
sipa Mar 28, 2024
60c0e07
Integrate util/system's CInit into RNGState
sipa Mar 28, 2024
bd9b288
Switch all RNG code to the built-in PRNG.
sipa Apr 3, 2024
cf9c48f
Remove hwrand_initialized.
sipa Dec 18, 2018
1ac7940
Sprinkle some sweet noexcepts over the RNG code
sipa Dec 17, 2018
fac9b1a
DRY: Implement GetRand using FastRandomContext::randrange
sipa Jan 4, 2019
756303c
Encapsulate RNGState better
sipa Jan 11, 2019
0c6be8f
Use secure allocator for RNG state
sipa Jan 11, 2019
7f6b2e5
Document RNG design in random.h
sipa Jan 13, 2019
284b373
Add hash strengthening to the RNG
sipa Dec 17, 2018
ebc728c
Document strenghtening
sipa Jan 21, 2019
aa11064
Add ChaCha20 encryption option (XOR)
jonasschnelli Mar 1, 2019
2112bbb
Add ChaCha20 bench
jonasschnelli Mar 5, 2019
4ebd97b
Unroll the ChaCha20 inner loop for performance
sipa Apr 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 59 additions & 7 deletions configure.ac
Expand Up @@ -220,8 +220,6 @@ AC_ARG_WITH([armv82-crypto],
[armv82_crypto=$withval],
[armv82_crypto=no])

AC_ARG_WITH([protoc-bindir],[AS_HELP_STRING([--with-protoc-bindir=BIN_DIR],[specify protoc bin path])], [protoc_bin_path=$withval], [])

AC_ARG_ENABLE(man,
[AS_HELP_STRING([--disable-man],
[do not install man pages (default is to install)])],,
Expand Down Expand Up @@ -603,6 +601,8 @@ AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
#include <byteswap.h>
#endif])

AC_CHECK_DECLS([__builtin_clz, __builtin_clzl, __builtin_clzll])

dnl Check for MSG_NOSIGNAL
AC_MSG_CHECKING(for MSG_NOSIGNAL)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]],
Expand Down Expand Up @@ -636,6 +636,63 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
]
)

TEMP_LDFLAGS="$LDFLAGS"
LDFLAGS="$TEMP_LDFLAGS $PTHREAD_CFLAGS"
AC_MSG_CHECKING([for thread_local support])
AC_LINK_IFELSE([AC_LANG_SOURCE([
#include <thread>
static thread_local int foo = 0;
static void run_thread() { foo++;}
int main(){
for(int i = 0; i < 10; i++) { std::thread(run_thread).detach();}
return foo;
}
])],
[
AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.])
AC_MSG_RESULT(yes)
],
[
AC_MSG_RESULT(no)
]
)
LDFLAGS="$TEMP_LDFLAGS"

# Check for different ways of gathering OS randomness
AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>]],
[[ syscall(SYS_getrandom, nullptr, 32, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYS_GETRANDOM, 1,[Define this symbol if the Linux getrandom system call is available]) ],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING(for getentropy)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
[[ getentropy(nullptr, 32) ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GETENTROPY, 1,[Define this symbol if the BSD getentropy system call is available]) ],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING([for getentropy via sys/random.h])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <sys/random.h>]],
[[ getentropy(nullptr, 32) ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GETENTROPY_RAND, 1,[Define this symbol if the BSD getentropy system call is available with sys/random.h]) ],
[ AC_MSG_RESULT(no)]
)

AC_MSG_CHECKING(for sysctl KERN_ARND)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]],
[[ static const int name[2] = {CTL_KERN, KERN_ARND};
sysctl(name, 2, nullptr, nullptr, nullptr, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYSCTL_ARND, 1,[Define this symbol if the BSD sysctl(KERN_ARND) is available]) ],
[ AC_MSG_RESULT(no)]
)

# Check for reduced exports
if test x$use_reduce_exports = xyes; then
AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],[RE_CXXFLAGS="-fvisibility=hidden"],
[AC_MSG_ERROR([Cannot set default symbol visibility. Use --disable-reduce-exports.])])
Expand Down Expand Up @@ -806,7 +863,6 @@ if test x$use_pkgconfig = xyes; then
[
PKG_CHECK_MODULES([SSL], [libssl],, [AC_MSG_ERROR(openssl not found.)])
PKG_CHECK_MODULES([CRYPTO], [libcrypto],,[AC_MSG_ERROR(libcrypto not found.)])
BITCOIN_QT_CHECK([PKG_CHECK_MODULES([PROTOBUF], [protobuf], [have_protobuf=yes], [BITCOIN_QT_FAIL(libprotobuf not found)])])
if test x$use_qr != xno; then
BITCOIN_QT_CHECK([PKG_CHECK_MODULES([QR], [libqrencode], [have_qrencode=yes], [have_qrencode=no])])
fi
Expand Down Expand Up @@ -866,7 +922,6 @@ else
esac
fi

BITCOIN_QT_CHECK(AC_CHECK_LIB([protobuf] ,[main],[PROTOBUF_LIBS=-lprotobuf], BITCOIN_QT_FAIL(libprotobuf not found)))
if test x$use_qr != xno; then
BITCOIN_QT_CHECK([AC_CHECK_LIB([qrencode], [main],[QR_LIBS=-lqrencode], [have_qrencode=no])])
BITCOIN_QT_CHECK([AC_CHECK_HEADER([qrencode.h],, have_qrencode=no)])
Expand Down Expand Up @@ -971,8 +1026,6 @@ AM_CONDITIONAL([EMBEDDED_UNIVALUE],[test x$need_bundled_univalue = xyes])
AC_SUBST(UNIVALUE_CFLAGS)
AC_SUBST(UNIVALUE_LIBS)

BITCOIN_QT_PATH_PROGS([PROTOC], [protoc],$protoc_bin_path)

AC_MSG_CHECKING([whether to build dogecoind])
AM_CONDITIONAL([BUILD_BITCOIND], [test x$build_bitcoind = xyes])
AC_MSG_RESULT($build_bitcoind)
Expand Down Expand Up @@ -1172,7 +1225,6 @@ AC_SUBST(SSL_LIBS)
AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(ZMQ_LIBS)
AC_SUBST(PROTOBUF_LIBS)
AC_SUBST(QR_LIBS)
AC_SUBST(HAVE_FDATASYNC)
AC_SUBST(HAVE_FULLFSYNC)
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -256,6 +256,8 @@ crypto_libdogecoin_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
crypto_libdogecoin_crypto_a_SOURCES = \
crypto/aes.cpp \
crypto/aes.h \
crypto/chacha20.h \
crypto/chacha20.cpp \
crypto/common.h \
crypto/hmac_sha256.cpp \
crypto/hmac_sha256.h \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.bench.include
Expand Up @@ -17,6 +17,7 @@ bench_bench_dogecoin_SOURCES = \
bench/checkqueue.cpp \
bench/Examples.cpp \
bench/rollingbloom.cpp \
bench/chacha20.cpp \
bench/crypto_hash.cpp \
bench/ccoins_caching.cpp \
bench/mempool_eviction.cpp \
Expand Down
25 changes: 7 additions & 18 deletions src/Makefile.qt.include
Expand Up @@ -162,10 +162,6 @@ QT_QRC = qt/bitcoin.qrc
QT_QRC_LOCALE_CPP = qt/qrc_bitcoin_locale.cpp
QT_QRC_LOCALE = qt/bitcoin_locale.qrc

PROTOBUF_CC = qt/paymentrequest.pb.cc
PROTOBUF_H = qt/paymentrequest.pb.h
PROTOBUF_PROTO = qt/paymentrequest.proto

BITCOIN_QT_H = \
qt/addressbookpage.h \
qt/addresstablemodel.h \
Expand Down Expand Up @@ -193,7 +189,6 @@ BITCOIN_QT_H = \
qt/optionsdialog.h \
qt/optionsmodel.h \
qt/overviewpage.h \
qt/paymentrequestplus.h \
qt/paymentserver.h \
qt/peertablemodel.h \
qt/platformstyle.h \
Expand Down Expand Up @@ -321,7 +316,6 @@ BITCOIN_QT_WALLET_CPP = \
qt/importkeysdialog.cpp \
qt/openuridialog.cpp \
qt/overviewpage.cpp \
qt/paymentrequestplus.cpp \
qt/paymentserver.cpp \
qt/receivecoinsdialog.cpp \
qt/receiverequestdialog.cpp \
Expand Down Expand Up @@ -371,15 +365,15 @@ BITCOIN_QT_INCLUDES = -I$(builddir)/qt -I$(srcdir)/qt -I$(srcdir)/qt/forms \
-I$(builddir)/qt/forms -DQT_NO_KEYWORDS

qt_libdogecoinqt_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(QT_DBUS_INCLUDES) $(PROTOBUF_CFLAGS) $(QR_CFLAGS)
$(QT_INCLUDES) $(QT_DBUS_INCLUDES) $(QR_CFLAGS)
qt_libdogecoinqt_a_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)
qt_libdogecoinqt_a_OBJCXXFLAGS = $(AM_OBJCXXFLAGS) $(QT_PIE_FLAGS)

qt_libdogecoinqt_a_SOURCES = $(BITCOIN_QT_CPP) $(BITCOIN_QT_H) $(QT_FORMS_UI) \
$(QT_QRC) $(QT_QRC_LOCALE) $(QT_TS) $(PROTOBUF_PROTO) $(RES_ICONS) $(RES_IMAGES) $(RES_MOVIES) $(RES_FONTS)
$(QT_QRC) $(QT_QRC_LOCALE) $(QT_TS) $(RES_ICONS) $(RES_IMAGES) $(RES_MOVIES) $(RES_FONTS)

nodist_qt_libdogecoinqt_a_SOURCES = $(QT_MOC_CPP) $(QT_MOC) $(PROTOBUF_CC) \
$(PROTOBUF_H) $(QT_QRC_CPP) $(QT_QRC_LOCALE_CPP)
nodist_qt_libdogecoinqt_a_SOURCES = $(QT_MOC_CPP) $(QT_MOC) \
$(QT_QRC_CPP) $(QT_QRC_LOCALE_CPP)

# forms/foo.h -> forms/ui_foo.h
QT_FORMS_H=$(join $(dir $(QT_FORMS_UI)),$(addprefix ui_, $(notdir $(QT_FORMS_UI:.ui=.h))))
Expand All @@ -389,14 +383,9 @@ QT_FORMS_H=$(join $(dir $(QT_FORMS_UI)),$(addprefix ui_, $(notdir $(QT_FORMS_UI:
$(QT_MOC): $(QT_FORMS_H)
$(qt_libdogecoinqt_a_OBJECTS) $(qt_dogecoin_qt_OBJECTS) : | $(QT_MOC)

#Generating these with a half-written protobuf header leads to wacky results.
#This makes sure it's done.
$(QT_MOC): $(PROTOBUF_H)
$(QT_MOC_CPP): $(PROTOBUF_H)

# bitcoin-qt binary #
qt_dogecoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(PROTOBUF_CFLAGS) $(QR_CFLAGS)
$(QT_INCLUDES) $(QR_CFLAGS)
qt_dogecoin_qt_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)

qt_dogecoin_qt_SOURCES = qt/bitcoin.cpp
Expand All @@ -414,7 +403,7 @@ if ENABLE_ZMQ
qt_dogecoin_qt_LDADD += $(LIBDOGECOIN_ZMQ) $(ZMQ_LIBS)
endif
qt_dogecoin_qt_LDADD += $(LIBDOGECOIN_CLI) $(LIBDOGECOIN_COMMON) $(LIBDOGECOIN_UTIL) $(LIBDOGECOIN_CONSENSUS) $(LIBDOGECOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
qt_dogecoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
qt_dogecoin_qt_LIBTOOLFLAGS = --tag CXX
Expand Down Expand Up @@ -445,7 +434,7 @@ $(QT_QRC_LOCALE_CPP): $(QT_QRC_LOCALE) $(QT_QM)
$(SED) -e '/^\*\*.*Created:/d' -e '/^\*\*.*by:/d' > $@
@rm $(@D)/temp_$(<F)

$(QT_QRC_CPP): $(QT_QRC) $(QT_FORMS_H) $(RES_ICONS) $(RES_IMAGES) $(RES_MOVIES) $(RES_FONTS) $(PROTOBUF_H)
$(QT_QRC_CPP): $(QT_QRC) $(QT_FORMS_H) $(RES_ICONS) $(RES_IMAGES) $(RES_MOVIES) $(RES_FONTS)
@test -f $(RCC)
$(AM_V_GEN) QT_SELECT=$(QT_SELECT) $(RCC) -name bitcoin $< | \
$(SED) -e '/^\*\*.*Created:/d' -e '/^\*\*.*by:/d' > $@
Expand Down
19 changes: 3 additions & 16 deletions src/Makefile.qttest.include
Expand Up @@ -6,34 +6,21 @@ bin_PROGRAMS += qt/test/test_dogecoin-qt
TESTS += qt/test/test_dogecoin-qt

TEST_QT_MOC_CPP = \
qt/test/moc_compattests.cpp \
qt/test/moc_rpcnestedtests.cpp \
qt/test/moc_uritests.cpp

if ENABLE_WALLET
TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
endif

TEST_QT_H = \
qt/test/compattests.h \
qt/test/rpcnestedtests.h \
qt/test/uritests.h \
qt/test/paymentrequestdata.h \
qt/test/paymentservertests.h
qt/test/uritests.h

qt_test_test_dogecoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
$(QT_INCLUDES) $(QT_TEST_INCLUDES)

qt_test_test_dogecoin_qt_SOURCES = \
qt/test/compattests.cpp \
qt/test/rpcnestedtests.cpp \
qt/test/test_main.cpp \
qt/test/uritests.cpp \
$(TEST_QT_H)
if ENABLE_WALLET
qt_test_test_dogecoin_qt_SOURCES += \
qt/test/paymentservertests.cpp
endif

nodist_qt_test_test_dogecoin_qt_SOURCES = $(TEST_QT_MOC_CPP)

Expand All @@ -46,7 +33,7 @@ qt_test_test_dogecoin_qt_LDADD += $(LIBDOGECOIN_ZMQ) $(ZMQ_LIBS)
endif
qt_test_test_dogecoin_qt_LDADD += $(LIBDOGECOIN_CLI) $(LIBDOGECOIN_COMMON) $(LIBDOGECOIN_UTIL) $(LIBDOGECOIN_CONSENSUS) $(LIBDOGECOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) \
$(LIBMEMENV) $(BOOST_LIBS) $(QT_DBUS_LIBS) $(QT_TEST_LIBS) $(QT_LIBS) \
$(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(QR_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
qt_test_test_dogecoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
qt_test_test_dogecoin_qt_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.test.include
Expand Up @@ -114,6 +114,7 @@ BITCOIN_TESTS =\
test/pow_tests.cpp \
test/prevector_tests.cpp \
test/raii_event_tests.cpp \
test/random_tests.cpp \
test/reverselock_tests.cpp \
test/rpc_tests.cpp \
test/sanity_tests.cpp \
Expand All @@ -129,9 +130,9 @@ BITCOIN_TESTS =\
test/streams_tests.cpp \
test/test_bitcoin.cpp \
test/test_bitcoin.h \
test/test_random.h \
test/testutil.cpp \
test/testutil.h \
test/sync_tests.cpp \
test/timedata_tests.cpp \
test/transaction_tests.cpp \
test/txvalidationcache_tests.cpp \
Expand Down
8 changes: 4 additions & 4 deletions src/addrman.cpp
Expand Up @@ -351,8 +351,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvTried[nKBucket][nKBucketPos] == -1) {
nKBucket = (nKBucket + insecure_rand.rand32()) % ADDRMAN_TRIED_BUCKET_COUNT;
nKBucketPos = (nKBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
nKBucket = (nKBucket + insecure_rand.randbits(ADDRMAN_TRIED_BUCKET_COUNT_LOG2)) % ADDRMAN_TRIED_BUCKET_COUNT;
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvTried[nKBucket][nKBucketPos];
assert(mapInfo.count(nId) == 1);
Expand All @@ -368,8 +368,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
while (vvNew[nUBucket][nUBucketPos] == -1) {
nUBucket = (nUBucket + insecure_rand.rand32()) % ADDRMAN_NEW_BUCKET_COUNT;
nUBucketPos = (nUBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
nUBucket = (nUBucket + insecure_rand.randbits(ADDRMAN_NEW_BUCKET_COUNT_LOG2)) % ADDRMAN_NEW_BUCKET_COUNT;
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
}
int nId = vvNew[nUBucket][nUBucketPos];
assert(mapInfo.count(nId) == 1);
Expand Down
11 changes: 8 additions & 3 deletions src/addrman.h
Expand Up @@ -136,13 +136,13 @@ class CAddrInfo : public CAddress
*/

//! total number of buckets for tried addresses
#define ADDRMAN_TRIED_BUCKET_COUNT 256
#define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8

//! total number of buckets for new addresses
#define ADDRMAN_NEW_BUCKET_COUNT 1024
#define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10

//! maximum allowed number of entries in buckets for new and tried addresses
#define ADDRMAN_BUCKET_SIZE 64
#define ADDRMAN_BUCKET_SIZE_LOG2 6

//! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
#define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
Expand Down Expand Up @@ -171,6 +171,11 @@ class CAddrInfo : public CAddress
//! the maximum number of nodes to return in a getaddr call
#define ADDRMAN_GETADDR_MAX 2500

//! Convenience
#define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2)
#define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2)
#define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2)

/**
* Stochastical (IP) address manager
*/
Expand Down
14 changes: 8 additions & 6 deletions src/bench/base58.cpp
Expand Up @@ -7,20 +7,22 @@
#include "validation.h"
#include "base58.h"

#include <array>
#include <vector>
#include <string>


static void Base58Encode(benchmark::State& state)
{
unsigned char buff[32] = {
17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
200, 24
static const std::array<unsigned char, 32> buff = {
{
17, 79, 8, 99, 150, 189, 208, 162, 22, 23, 203, 163, 36, 58, 147,
227, 139, 2, 215, 100, 91, 38, 11, 141, 253, 40, 117, 21, 16, 90,
200, 24
}
};
unsigned char* b = buff;
while (state.KeepRunning()) {
EncodeBase58(b, b + 32);
EncodeBase58(buff.data(), buff.data() + buff.size());
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/bench/bench_bitcoin.cpp
Expand Up @@ -5,10 +5,13 @@
#include "bench.h" // for BenchRunner
#include "key.h" // for ECC_Start, ECC_Stop
#include "util.h" // for SetupEnvironment, fPrintToDebugLog
#include "validation.h"
#include "random.h"

int
main(int argc, char** argv)
{
RandomInit();
ECC_Start();
SetupEnvironment();
fPrintToDebugLog = false; // don't want to write to debug.log file
Expand Down