Skip to content

Commit 342b9bc

Browse files
committed
Merge #9792: FastRandomContext improvements and switch to ChaCha20
4fd2d2f Add a FastRandomContext::randrange and use it (Pieter Wuille) 1632922 Switch FastRandomContext to ChaCha20 (Pieter Wuille) e04326f Add ChaCha20 (Pieter Wuille) 663fbae FastRandom benchmark (Pieter Wuille) c21cbe6 Introduce FastRandomContext::randbool() (Pieter Wuille) Tree-SHA512: 7fff61e3f6d6dc6ac846ca643d877b377db609646dd401a0e8f50b052c6b9bcd2f5fc34de6bbf28f04afd1724f6279ee163ead5f37d724fb782a00239f35db1d
2 parents 1b25b6d + 4fd2d2f commit 342b9bc

20 files changed

+482
-44
lines changed

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
549549
#include <byteswap.h>
550550
#endif])
551551

552+
AC_CHECK_DECLS([__builtin_clz, __builtin_clzl, __builtin_clzll])
553+
552554
dnl Check for MSG_NOSIGNAL
553555
AC_MSG_CHECKING(for MSG_NOSIGNAL)
554556
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]],

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ crypto_libbitcoin_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
246246
crypto_libbitcoin_crypto_a_SOURCES = \
247247
crypto/aes.cpp \
248248
crypto/aes.h \
249+
crypto/chacha20.h \
250+
crypto/chacha20.cpp \
249251
crypto/common.h \
250252
crypto/hmac_sha256.cpp \
251253
crypto/hmac_sha256.h \

src/Makefile.test.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ BITCOIN_TESTS =\
5757
test/policyestimator_tests.cpp \
5858
test/pow_tests.cpp \
5959
test/prevector_tests.cpp \
60-
test/random_tests.cpp \
6160
test/raii_event_tests.cpp \
61+
test/random_tests.cpp \
6262
test/reverselock_tests.cpp \
6363
test/rpc_tests.cpp \
6464
test/sanity_tests.cpp \

src/addrman.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
351351
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
352352
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
353353
while (vvTried[nKBucket][nKBucketPos] == -1) {
354-
nKBucket = (nKBucket + insecure_rand.rand32()) % ADDRMAN_TRIED_BUCKET_COUNT;
355-
nKBucketPos = (nKBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
354+
nKBucket = (nKBucket + insecure_rand.randbits(ADDRMAN_TRIED_BUCKET_COUNT_LOG2)) % ADDRMAN_TRIED_BUCKET_COUNT;
355+
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
356356
}
357357
int nId = vvTried[nKBucket][nKBucketPos];
358358
assert(mapInfo.count(nId) == 1);
@@ -368,8 +368,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
368368
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
369369
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
370370
while (vvNew[nUBucket][nUBucketPos] == -1) {
371-
nUBucket = (nUBucket + insecure_rand.rand32()) % ADDRMAN_NEW_BUCKET_COUNT;
372-
nUBucketPos = (nUBucketPos + insecure_rand.rand32()) % ADDRMAN_BUCKET_SIZE;
371+
nUBucket = (nUBucket + insecure_rand.randbits(ADDRMAN_NEW_BUCKET_COUNT_LOG2)) % ADDRMAN_NEW_BUCKET_COUNT;
372+
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
373373
}
374374
int nId = vvNew[nUBucket][nUBucketPos];
375375
assert(mapInfo.count(nId) == 1);

src/addrman.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ class CAddrInfo : public CAddress
136136
*/
137137

138138
//! total number of buckets for tried addresses
139-
#define ADDRMAN_TRIED_BUCKET_COUNT 256
139+
#define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8
140140

141141
//! total number of buckets for new addresses
142-
#define ADDRMAN_NEW_BUCKET_COUNT 1024
142+
#define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10
143143

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

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

174+
//! Convenience
175+
#define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2)
176+
#define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2)
177+
#define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2)
178+
174179
/**
175180
* Stochastical (IP) address manager
176181
*/

src/bench/checkqueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::State& state)
6868
PrevectorJob(){
6969
}
7070
PrevectorJob(FastRandomContext& insecure_rand){
71-
p.resize(insecure_rand.rand32() % (PREVECTOR_SIZE*2));
71+
p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
7272
}
7373
bool operator()()
7474
{

src/bench/crypto_hash.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "bench.h"
88
#include "bloom.h"
99
#include "hash.h"
10+
#include "random.h"
1011
#include "uint256.h"
1112
#include "utiltime.h"
1213
#include "crypto/ripemd160.h"
@@ -69,10 +70,34 @@ static void SipHash_32b(benchmark::State& state)
6970
}
7071
}
7172

73+
static void FastRandom_32bit(benchmark::State& state)
74+
{
75+
FastRandomContext rng(true);
76+
uint32_t x;
77+
while (state.KeepRunning()) {
78+
for (int i = 0; i < 1000000; i++) {
79+
x += rng.rand32();
80+
}
81+
}
82+
}
83+
84+
static void FastRandom_1bit(benchmark::State& state)
85+
{
86+
FastRandomContext rng(true);
87+
uint32_t x;
88+
while (state.KeepRunning()) {
89+
for (int i = 0; i < 1000000; i++) {
90+
x += rng.randbool();
91+
}
92+
}
93+
}
94+
7295
BENCHMARK(RIPEMD160);
7396
BENCHMARK(SHA1);
7497
BENCHMARK(SHA256);
7598
BENCHMARK(SHA512);
7699

77100
BENCHMARK(SHA256_32b);
78101
BENCHMARK(SipHash_32b);
102+
BENCHMARK(FastRandom_32bit);
103+
BENCHMARK(FastRandom_1bit);

src/crypto/chacha20.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright (c) 2017 The Bitcoin 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+
// Based on the public domain implementation 'merged' by D. J. Bernstein
6+
// See https://cr.yp.to/chacha.html.
7+
8+
#include "crypto/common.h"
9+
#include "crypto/chacha20.h"
10+
11+
#include <string.h>
12+
13+
constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (v >> (32 - c)); }
14+
15+
#define QUARTERROUND(a,b,c,d) \
16+
a += b; d = rotl32(d ^ a, 16); \
17+
c += d; b = rotl32(b ^ c, 12); \
18+
a += b; d = rotl32(d ^ a, 8); \
19+
c += d; b = rotl32(b ^ c, 7);
20+
21+
static const unsigned char sigma[] = "expand 32-byte k";
22+
static const unsigned char tau[] = "expand 16-byte k";
23+
24+
void ChaCha20::SetKey(const unsigned char* k, size_t keylen)
25+
{
26+
const unsigned char *constants;
27+
28+
input[4] = ReadLE32(k + 0);
29+
input[5] = ReadLE32(k + 4);
30+
input[6] = ReadLE32(k + 8);
31+
input[7] = ReadLE32(k + 12);
32+
if (keylen == 32) { /* recommended */
33+
k += 16;
34+
constants = sigma;
35+
} else { /* keylen == 16 */
36+
constants = tau;
37+
}
38+
input[8] = ReadLE32(k + 0);
39+
input[9] = ReadLE32(k + 4);
40+
input[10] = ReadLE32(k + 8);
41+
input[11] = ReadLE32(k + 12);
42+
input[0] = ReadLE32(constants + 0);
43+
input[1] = ReadLE32(constants + 4);
44+
input[2] = ReadLE32(constants + 8);
45+
input[3] = ReadLE32(constants + 12);
46+
input[12] = 0;
47+
input[13] = 0;
48+
input[14] = 0;
49+
input[15] = 0;
50+
}
51+
52+
ChaCha20::ChaCha20()
53+
{
54+
memset(input, 0, sizeof(input));
55+
}
56+
57+
ChaCha20::ChaCha20(const unsigned char* k, size_t keylen)
58+
{
59+
SetKey(k, keylen);
60+
}
61+
62+
void ChaCha20::SetIV(uint64_t iv)
63+
{
64+
input[14] = iv;
65+
input[15] = iv >> 32;
66+
}
67+
68+
void ChaCha20::Seek(uint64_t pos)
69+
{
70+
input[12] = pos;
71+
input[13] = pos >> 32;
72+
}
73+
74+
void ChaCha20::Output(unsigned char* c, size_t bytes)
75+
{
76+
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
77+
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
78+
unsigned char *ctarget = NULL;
79+
unsigned char tmp[64];
80+
unsigned int i;
81+
82+
if (!bytes) return;
83+
84+
j0 = input[0];
85+
j1 = input[1];
86+
j2 = input[2];
87+
j3 = input[3];
88+
j4 = input[4];
89+
j5 = input[5];
90+
j6 = input[6];
91+
j7 = input[7];
92+
j8 = input[8];
93+
j9 = input[9];
94+
j10 = input[10];
95+
j11 = input[11];
96+
j12 = input[12];
97+
j13 = input[13];
98+
j14 = input[14];
99+
j15 = input[15];
100+
101+
for (;;) {
102+
if (bytes < 64) {
103+
ctarget = c;
104+
c = tmp;
105+
}
106+
x0 = j0;
107+
x1 = j1;
108+
x2 = j2;
109+
x3 = j3;
110+
x4 = j4;
111+
x5 = j5;
112+
x6 = j6;
113+
x7 = j7;
114+
x8 = j8;
115+
x9 = j9;
116+
x10 = j10;
117+
x11 = j11;
118+
x12 = j12;
119+
x13 = j13;
120+
x14 = j14;
121+
x15 = j15;
122+
for (i = 20;i > 0;i -= 2) {
123+
QUARTERROUND( x0, x4, x8,x12)
124+
QUARTERROUND( x1, x5, x9,x13)
125+
QUARTERROUND( x2, x6,x10,x14)
126+
QUARTERROUND( x3, x7,x11,x15)
127+
QUARTERROUND( x0, x5,x10,x15)
128+
QUARTERROUND( x1, x6,x11,x12)
129+
QUARTERROUND( x2, x7, x8,x13)
130+
QUARTERROUND( x3, x4, x9,x14)
131+
}
132+
x0 += j0;
133+
x1 += j1;
134+
x2 += j2;
135+
x3 += j3;
136+
x4 += j4;
137+
x5 += j5;
138+
x6 += j6;
139+
x7 += j7;
140+
x8 += j8;
141+
x9 += j9;
142+
x10 += j10;
143+
x11 += j11;
144+
x12 += j12;
145+
x13 += j13;
146+
x14 += j14;
147+
x15 += j15;
148+
149+
++j12;
150+
if (!j12) ++j13;
151+
152+
WriteLE32(c + 0, x0);
153+
WriteLE32(c + 4, x1);
154+
WriteLE32(c + 8, x2);
155+
WriteLE32(c + 12, x3);
156+
WriteLE32(c + 16, x4);
157+
WriteLE32(c + 20, x5);
158+
WriteLE32(c + 24, x6);
159+
WriteLE32(c + 28, x7);
160+
WriteLE32(c + 32, x8);
161+
WriteLE32(c + 36, x9);
162+
WriteLE32(c + 40, x10);
163+
WriteLE32(c + 44, x11);
164+
WriteLE32(c + 48, x12);
165+
WriteLE32(c + 52, x13);
166+
WriteLE32(c + 56, x14);
167+
WriteLE32(c + 60, x15);
168+
169+
if (bytes <= 64) {
170+
if (bytes < 64) {
171+
for (i = 0;i < bytes;++i) ctarget[i] = c[i];
172+
}
173+
input[12] = j12;
174+
input[13] = j13;
175+
return;
176+
}
177+
bytes -= 64;
178+
c += 64;
179+
}
180+
}

src/crypto/chacha20.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2017 The Bitcoin 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_CHACHA20_H
6+
#define BITCOIN_CRYPTO_CHACHA20_H
7+
8+
#include <stdint.h>
9+
#include <stdlib.h>
10+
11+
/** A PRNG class for ChaCha20. */
12+
class ChaCha20
13+
{
14+
private:
15+
uint32_t input[16];
16+
17+
public:
18+
ChaCha20();
19+
ChaCha20(const unsigned char* key, size_t keylen);
20+
void SetKey(const unsigned char* key, size_t keylen);
21+
void SetIV(uint64_t iv);
22+
void Seek(uint64_t pos);
23+
void Output(unsigned char* output, size_t bytes);
24+
};
25+
26+
#endif // BITCOIN_CRYPTO_CHACHA20_H

src/crypto/common.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
7979
memcpy(ptr, (char*)&v, 8);
8080
}
8181

82+
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
83+
uint64_t static inline CountBits(uint64_t x)
84+
{
85+
#ifdef HAVE_DECL___BUILTIN_CLZL
86+
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
87+
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
88+
}
89+
#endif
90+
#ifdef HAVE_DECL___BUILTIN_CLZLL
91+
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
92+
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
93+
}
94+
#endif
95+
int ret = 0;
96+
while (x) {
97+
x >>= 1;
98+
++ret;
99+
}
100+
return ret;
101+
}
102+
82103
#endif // BITCOIN_CRYPTO_COMMON_H

0 commit comments

Comments
 (0)