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

#24545 bip324-enable #14

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0c7b7d5
Split ChaCha20 into aligned/unaligned variants
sipa Sep 21, 2022
162d85d
Rename ChaCha20::Seek -> Seek64 to clarify multiple of 64
sipa Sep 21, 2022
df2aa81
Make unrestricted ChaCha20 cipher not waste keystream bytes
sipa Sep 21, 2022
6464ce0
Add xoroshiro128++ PRNG
martinus Jun 10, 2022
ef1d31c
Add fuzz test for testing that ChaCha20 works as a stream
sipa Jun 13, 2022
224b273
Use ChaCha20 caching in FastRandomContext
sipa Sep 21, 2022
1c65aee
Use ChaCha20Aligned in MuHash3072 code
sipa Sep 21, 2022
48855b4
Only support 32-byte keys in ChaCha20{,Aligned}
sipa Sep 21, 2022
7e928af
Inline ChaCha20 32-byte specific constants
sipa Sep 21, 2022
4fce8c2
Improve test vectors for ChaCha20
sipa Sep 21, 2022
271850e
Merge remote-tracking branch 'sipa/202209_chacha20' into bip324-ciphe…
dhruv Oct 20, 2022
46b1c17
RFC8439 nonce and counter for ChaCha20
dhruv May 10, 2022
b4ef2e5
RFC8439 implementation and tests
dhruv Jun 9, 2022
9fa90bd
Adding forward secure FSChaCha20
dhruv Jun 9, 2022
ce6ad41
BIP324 Cipher Suite
dhruv Jan 6, 2021
f8b313e
Allow for RFC8439 AD in cipher suite interface
dhruv Aug 11, 2022
1d5e5bc
Allow for RFC8439 AD in cipher suite interface
dhruv Aug 11, 2022
d721148
Add BIP324 short-IDs to protocol.cpp
dhruv Aug 9, 2019
ddee1e4
Add BIP324 v2 transport serializer and deserializer
dhruv Mar 2, 2020
ac7e114
fuzz: Add fuzz test for v2 transport {de}serialization
dhruv Oct 1, 2021
ef929a5
Expose BIP324CipherSuite AAD via transport classes
dhruv Sep 1, 2022
d6bcb10
Squashed 'src/secp256k1/' changes from 44c2452fd3..e46f81abd6
dhruv Oct 20, 2022
c813cd7
Merge commit 'd6bcb105c35e2e53c4a580cb398fba1d88efbba9' into bip324-h…
dhruv Oct 20, 2022
ab27e55
Merge branch 'bip324-cipher-suite' into bip324-handshake
dhruv Oct 20, 2022
24af9a7
Enable ECDH computation on secp256k1 keys
dhruv Jul 22, 2022
76f7898
Bench test for ECDH
dhruv Jul 22, 2022
f2d1136
Fuzz test for ECDH
dhruv Jul 22, 2022
9a87370
HKDF key derivation from ECDH secret for BIP324
dhruv Nov 16, 2021
8adec3a
Fuzz test for BIP324 key derivation
dhruv Nov 18, 2021
c27eb1e
Squashed 'src/secp256k1/' changes from 44c2452fd3..e46f81abd6
dhruv Oct 20, 2022
0fafefb
Merge commit 'c27eb1e66ae32dadcc49d49d5676e6e0b37a2feb' into bip324-e…
dhruv Oct 20, 2022
3fbc703
Encode CKey to ElligatorSwift representation
dhruv Nov 2, 2021
d314684
Bench tests for CKey->EllSwift
dhruv Nov 3, 2021
3856507
Fuzz tests for CKey->EllSwift
dhruv Nov 3, 2021
24827c4
Merge branch 'bip324-handshake' into bip324-enable
dhruv Oct 20, 2022
48774c0
Merge branch 'bip324-net-v2' into bip324-enable
dhruv Oct 20, 2022
e3d1555
scripted-diff: rename use_v2 to use_addr_v2 in src/protocol.h
dhruv Feb 14, 2022
4668af3
p2p: Advertise v2 transport if CLI arg is on
dhruv Dec 15, 2021
32d2279
rpc: addnode arg to use BIP324 v2 p2p
dhruv Dec 28, 2021
72eb084
refactor: Add InitP2P() to NetEventsInterface
dhruv Feb 16, 2022
4f44bf4
p2p: Use v2 transport between supportive peers
dhruv Feb 14, 2022
720ed41
p2p: BIP324 transport version messages
dhruv Feb 15, 2022
2e45bf3
p2p: BIP324 v2.0 clients retry with v1 protocol
dhruv Feb 16, 2022
310149a
test: Functional test for opportunistic encryption
dhruv Sep 7, 2022
ff91db0
p2p: BIP324 shapable key exchange
dhruv Jul 29, 2022
0395fc8
test: BIP324 test vectors
dhruv Oct 19, 2022
b635a65
rpc: Expose transport type via getpeerinfo
dhruv Oct 20, 2022
530f480
rpc: Expose BIP324 session id via getpeerinfo
dhruv Oct 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_msvc/libsecp256k1/libsecp256k1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>ENABLE_MODULE_ECDH;ENABLE_MODULE_RECOVERY;ENABLE_MODULE_EXTRAKEYS;ENABLE_MODULE_SCHNORRSIG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>ENABLE_MODULE_ECDH;ENABLE_MODULE_RECOVERY;ENABLE_MODULE_EXTRAKEYS;ENABLE_MODULE_SCHNORRSIG;ENABLE_MODULE_ELLSWIFT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\..\src\secp256k1;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4146;4244;4267;4334</DisableSpecificWarnings>
</ClCompile>
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ LIBS_TEMP="$LIBS"
unset LIBS
LIBS="$LIBS_TEMP"

ac_configure_args="${ac_configure_args} --disable-shared --with-pic --enable-benchmark=no --enable-module-recovery --enable-module-schnorrsig"
ac_configure_args="${ac_configure_args} --disable-shared --with-pic --enable-benchmark=no --enable-module-recovery --enable-module-schnorrsig --enable-experimental --enable-module-ellswift"
AC_CONFIG_SUBDIRS([src/secp256k1])

AC_OUTPUT
Expand Down
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ crypto_libbitcoin_crypto_base_la_LDFLAGS = $(AM_LDFLAGS) -static
crypto_libbitcoin_crypto_base_la_SOURCES = \
crypto/aes.cpp \
crypto/aes.h \
crypto/chacha_poly_aead.h \
crypto/chacha_poly_aead.cpp \
crypto/bip324_suite.h \
crypto/bip324_suite.cpp \
crypto/chacha20.h \
crypto/chacha20.cpp \
crypto/common.h \
Expand All @@ -529,6 +529,8 @@ crypto_libbitcoin_crypto_base_la_SOURCES = \
crypto/poly1305.cpp \
crypto/muhash.h \
crypto/muhash.cpp \
crypto/rfc8439.h \
crypto/rfc8439.cpp \
crypto/ripemd160.cpp \
crypto/ripemd160.h \
crypto/sha1.cpp \
Expand Down
5 changes: 4 additions & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ bench_bench_bitcoin_SOURCES = \
bench/bench.cpp \
bench/bench.h \
bench/bench_bitcoin.cpp \
bench/bip324_ecdh.cpp \
bench/bip324_suite.cpp \
bench/block_assemble.cpp \
bench/ccoins_caching.cpp \
bench/chacha20.cpp \
bench/chacha_poly_aead.cpp \
bench/checkblock.cpp \
bench/checkqueue.cpp \
bench/crypto_hash.cpp \
bench/data.cpp \
bench/data.h \
bench/descriptors.cpp \
bench/duplicate_inputs.cpp \
bench/ellswift.cpp \
bench/examples.cpp \
bench/gcs_filter.cpp \
bench/hashpadding.cpp \
Expand All @@ -42,6 +44,7 @@ bench_bench_bitcoin_SOURCES = \
bench/peer_eviction.cpp \
bench/poly1305.cpp \
bench/prevector.cpp \
bench/rfc8439.cpp \
bench/rollingbloom.cpp \
bench/rpc_blockchain.cpp \
bench/rpc_mempool.cpp \
Expand Down
8 changes: 6 additions & 2 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ FUZZ_BINARY=test/fuzz/fuzz$(EXEEXT)

JSON_TEST_FILES = \
test/data/script_tests.json \
test/data/bip324_vectors.json \
test/data/bip341_wallet_vectors.json \
test/data/base58_encode_decode.json \
test/data/blockfilters.json \
Expand Down Expand Up @@ -161,7 +162,8 @@ BITCOIN_TESTS =\
test/validation_flush_tests.cpp \
test/validation_tests.cpp \
test/validationinterface_tests.cpp \
test/versionbits_tests.cpp
test/versionbits_tests.cpp \
test/xoroshiro128plusplus_tests.cpp

if ENABLE_WALLET
BITCOIN_TESTS += \
Expand Down Expand Up @@ -254,12 +256,13 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/crypto.cpp \
test/fuzz/crypto_aes256.cpp \
test/fuzz/crypto_aes256cbc.cpp \
test/fuzz/crypto_bip324_suite.cpp \
test/fuzz/crypto_chacha20.cpp \
test/fuzz/crypto_chacha20_poly1305_aead.cpp \
test/fuzz/crypto_common.cpp \
test/fuzz/crypto_diff_fuzz_chacha20.cpp \
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \
test/fuzz/crypto_poly1305.cpp \
test/fuzz/crypto_rfc8439.cpp \
test/fuzz/cuckoocache.cpp \
test/fuzz/decode_tx.cpp \
test/fuzz/descriptor_parse.cpp \
Expand Down Expand Up @@ -291,6 +294,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/netbase_dns_lookup.cpp \
test/fuzz/node_eviction.cpp \
test/fuzz/p2p_transport_serialization.cpp \
test/fuzz/p2p_v2_transport_serialization.cpp \
test/fuzz/parse_hd_keypath.cpp \
test/fuzz/parse_numbers.cpp \
test/fuzz/parse_script.cpp \
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ TEST_UTIL_H = \
test/util/transaction_utils.h \
test/util/txmempool.h \
test/util/validation.h \
test/util/wallet.h
test/util/wallet.h \
test/util/xoroshiro128plusplus.h

libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
Expand Down
52 changes: 52 additions & 0 deletions src/bench/bip324_ecdh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>

#include <key.h>
#include <pubkey.h>
#include <random.h>
#include <secp256k1_ellswift.h>

#include <cstddef>

CKey GetRandomKey()
{
CKey key;
key.MakeNewKey(true);
return key;
}

int GetEll64(const CKey& key, unsigned char* ell64)
{
std::array<unsigned char, 32> rnd32;
GetRandBytes(rnd32);
return secp256k1_ellswift_create(GetVerifyContext(), ell64, reinterpret_cast<const unsigned char*>(key.data()), rnd32.data());
}

static void BIP324_ECDH(benchmark::Bench& bench)
{
ECC_Start();
auto our_key = GetRandomKey();
auto their_key = GetRandomKey();

unsigned char our_ell64[64], their_ell64[64];
if (!GetEll64(our_key, our_ell64)) {
assert(false);
}

if (!GetEll64(their_key, their_ell64)) {
assert(false);
}

bench.batch(1).unit("ecdh").run([&] {
assert(our_key.ComputeBIP324ECDHSecret({reinterpret_cast<std::byte*>(their_ell64), 64},
{reinterpret_cast<std::byte*>(our_ell64), 64},
true)
.has_value());
});
ECC_Stop();
}

BENCHMARK(BIP324_ECDH);
120 changes: 120 additions & 0 deletions src/bench/bip324_suite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2019-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.


#include <assert.h>
#include <bench/bench.h>
#include <crypto/bip324_suite.h>
#include <crypto/rfc8439.h> // for the RFC8439_EXPANSION constant
#include <hash.h>

#include <array>
#include <cstddef>
#include <vector>

/* Number of bytes to process per iteration */
static constexpr uint64_t BUFFER_SIZE_TINY = 64;
static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
static constexpr uint64_t BUFFER_SIZE_LARGE = 1024 * 1024;

static const std::vector<std::byte> zero_vec(BIP324_KEY_LEN, std::byte{0x00});

static void BIP324_CIPHER_SUITE(benchmark::Bench& bench, size_t contents_len, bool include_decryption)
{
BIP324Key zero_arr;
std::array<std::byte, BIP324_REKEY_SALT_LEN> zero_rekey_salt;
memcpy(zero_arr.data(), zero_vec.data(), BIP324_KEY_LEN);
memcpy(zero_rekey_salt.data(), zero_vec.data(), BIP324_REKEY_SALT_LEN);

BIP324CipherSuite enc{zero_arr, zero_arr, zero_rekey_salt};
BIP324CipherSuite dec{zero_arr, zero_arr, zero_rekey_salt};

auto packet_len = BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + contents_len + RFC8439_EXPANSION;

std::vector<std::byte> in(contents_len, std::byte{0x00});
std::vector<std::byte> out(packet_len, std::byte{0x00});

BIP324HeaderFlags flags{BIP324_NONE};

bench.batch(contents_len).unit("byte").run([&] {
// encrypt or decrypt the buffer with a static key
const bool crypt_ok_1 = enc.Crypt({}, in, out, flags, true);
assert(crypt_ok_1);

if (include_decryption) {
// if we decrypt, we need to decrypt the length first
std::array<std::byte, BIP324_LENGTH_FIELD_LEN> encrypted_pkt_len;
memcpy(encrypted_pkt_len.data(), out.data(), BIP324_LENGTH_FIELD_LEN);
(void)dec.DecryptLength(encrypted_pkt_len);
const bool crypt_ok_2 = dec.Crypt({}, {out.data() + BIP324_LENGTH_FIELD_LEN, out.size() - BIP324_LENGTH_FIELD_LEN}, in, flags, false);
assert(crypt_ok_2);
}
});
}

static void BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, false);
}

static void BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, false);
}

static void BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, false);
}

static void BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, true);
}

static void BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, true);
}

static void BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT(benchmark::Bench& bench)
{
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, true);
}

// Add Hash() (dbl-sha256) bench for comparison

static void HASH(benchmark::Bench& bench, size_t buffersize)
{
uint8_t hash[CHash256::OUTPUT_SIZE];
std::vector<uint8_t> in(buffersize, 0);
bench.batch(in.size()).unit("byte").run([&] {
CHash256().Write(in).Finalize(hash);
});
}

static void HASH_64BYTES(benchmark::Bench& bench)
{
HASH(bench, BUFFER_SIZE_TINY);
}

static void HASH_256BYTES(benchmark::Bench& bench)
{
HASH(bench, BUFFER_SIZE_SMALL);
}

static void HASH_1MB(benchmark::Bench& bench)
{
HASH(bench, BUFFER_SIZE_LARGE);
}

BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT);
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT);
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT);
BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT);
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT);
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT);
BENCHMARK(HASH_64BYTES);
BENCHMARK(HASH_256BYTES);
BENCHMARK(HASH_1MB);
4 changes: 2 additions & 2 deletions src/bench/chacha20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ static const uint64_t BUFFER_SIZE_LARGE = 1024*1024;
static void CHACHA20(benchmark::Bench& bench, size_t buffersize)
{
std::vector<uint8_t> key(32,0);
ChaCha20 ctx(key.data(), key.size());
ChaCha20 ctx(key.data());
ctx.SetIV(0);
ctx.Seek(0);
ctx.Seek64(0);
std::vector<uint8_t> in(buffersize,0);
std::vector<uint8_t> out(buffersize,0);
bench.batch(in.size()).unit("byte").run([&] {
Expand Down
Loading