Skip to content

Commit

Permalink
merge bitcoin#27479: BIP324: ElligatorSwift integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Sep 28, 2023
1 parent 7d08152 commit 3cab540
Show file tree
Hide file tree
Showing 89 changed files with 4,198 additions and 1,175 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bench_bench_dash_SOURCES = \
bench/bench_bitcoin.cpp \
bench/bench.cpp \
bench/bench.h \
bench/bip324_ecdh.cpp \
bench/block_assemble.cpp \
bench/bls.cpp \
bench/bls_dkg.cpp \
Expand All @@ -26,6 +27,7 @@ bench_bench_dash_SOURCES = \
bench/data.cpp \
bench/duplicate_inputs.cpp \
bench/ecdsa.cpp \
bench/ellswift.cpp \
bench/examples.cpp \
bench/rollingbloom.cpp \
bench/chacha20.cpp \
Expand Down
51 changes: 51 additions & 0 deletions src/bench/bip324_ecdh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 <span.h>

#include <array>
#include <cstddef>

static void BIP324_ECDH(benchmark::Bench& bench)
{
ECC_Start();
FastRandomContext rng;

std::array<std::byte, 32> key_data;
std::array<std::byte, EllSwiftPubKey::size()> our_ellswift_data;
std::array<std::byte, EllSwiftPubKey::size()> their_ellswift_data;

rng.fillrand(key_data);
rng.fillrand(our_ellswift_data);
rng.fillrand(their_ellswift_data);

bench.batch(1).unit("ecdh").run([&] {
CKey key;
key.Set(UCharCast(key_data.data()), UCharCast(key_data.data()) + 32, true);
EllSwiftPubKey our_ellswift(our_ellswift_data);
EllSwiftPubKey their_ellswift(their_ellswift_data);

auto ret = key.ComputeBIP324ECDHSecret(their_ellswift, our_ellswift, true);

// To make sure that the computation is not the same on every iteration (ellswift decoding
// is variable-time), distribute bytes from the shared secret over the 3 inputs. The most
// important one is their_ellswift, because that one is actually decoded, so it's given most
// bytes. The data is copied into the middle, so that both halves are affected:
// - Copy 8 bytes from the resulting shared secret into middle of the private key.
std::copy(ret.begin(), ret.begin() + 8, key_data.begin() + 12);
// - Copy 8 bytes from the resulting shared secret into the middle of our ellswift key.
std::copy(ret.begin() + 8, ret.begin() + 16, our_ellswift_data.begin() + 28);
// - Copy 16 bytes from the resulting shared secret into the middle of their ellswift key.
std::copy(ret.begin() + 16, ret.end(), their_ellswift_data.begin() + 24);
});

ECC_Stop();
}

BENCHMARK(BIP324_ECDH);
31 changes: 31 additions & 0 deletions src/bench/ellswift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022-2023 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 <random.h>

static void EllSwiftCreate(benchmark::Bench& bench)
{
ECC_Start();

CKey key;
key.MakeNewKey(true);

uint256 entropy = GetRandHash();

bench.batch(1).unit("pubkey").run([&] {
auto ret = key.EllSwiftCreate(AsBytes(Span{entropy}));
/* Use the first 32 bytes of the ellswift encoded public key as next private key. */
key.Set(UCharCast(ret.data()), UCharCast(ret.data()) + 32, true);
assert(key.IsValid());
/* Use the last 32 bytes of the ellswift encoded public key as next entropy. */
std::copy(ret.begin() + 32, ret.begin() + 64, BytePtr(entropy.data()));
});

ECC_Stop();
}

BENCHMARK(EllSwiftCreate);
37 changes: 37 additions & 0 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <random.h>

#include <secp256k1.h>
#include <secp256k1_ellswift.h>
#include <secp256k1_recovery.h>

static secp256k1_context* secp256k1_context_sign = nullptr;
Expand Down Expand Up @@ -304,6 +305,42 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
return ret;
}

EllSwiftPubKey CKey::EllSwiftCreate(Span<const std::byte> ent32) const
{
assert(fValid);
assert(ent32.size() == 32);
std::array<std::byte, EllSwiftPubKey::size()> encoded_pubkey;

auto success = secp256k1_ellswift_create(secp256k1_context_sign,
UCharCast(encoded_pubkey.data()),
keydata.data(),
UCharCast(ent32.data()));

// Should always succeed for valid keys (asserted above).
assert(success);
return {encoded_pubkey};
}

ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, const EllSwiftPubKey& our_ellswift, bool initiating) const
{
assert(fValid);

ECDHSecret output;
// BIP324 uses the initiator as party A, and the responder as party B. Remap the inputs
// accordingly:
bool success = secp256k1_ellswift_xdh(secp256k1_context_sign,
UCharCast(output.data()),
UCharCast(initiating ? our_ellswift.data() : their_ellswift.data()),
UCharCast(initiating ? their_ellswift.data() : our_ellswift.data()),
keydata.data(),
initiating ? 0 : 1,
secp256k1_ellswift_xdh_hash_function_bip324,
nullptr);
// Should always succeed for valid keys (assert above).
assert(success);
return output;
}

bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
out.nDepth = nDepth + 1;
CKeyID id = key.GetPubKey().GetID();
Expand Down
27 changes: 27 additions & 0 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
*/
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;

/** Size of ECDH shared secrets. */
constexpr static size_t ECDH_SECRET_SIZE = CSHA256::OUTPUT_SIZE;

// Used to represent ECDH shared secret (ECDH_SECRET_SIZE bytes)
using ECDHSecret = std::array<std::byte, ECDH_SECRET_SIZE>;

/** An encapsulated private key. */
class CKey
{
Expand Down Expand Up @@ -139,6 +145,27 @@ class CKey

//! Load private key and check that public key matches.
bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);

/** Create an ellswift-encoded public key for this key, with specified entropy.
*
* entropy must be a 32-byte span with additional entropy to use in the encoding. Every
* public key has ~2^256 different encodings, and this function will deterministically pick
* one of them, based on entropy. Note that even without truly random entropy, the
* resulting encoding will be indistinguishable from uniform to any adversary who does not
* know the private key (because the private key itself is always used as entropy as well).
*/
EllSwiftPubKey EllSwiftCreate(Span<const std::byte> entropy) const;

/** Compute a BIP324-style ECDH shared secret.
*
* - their_ellswift: EllSwiftPubKey that was received from the other side.
* - our_ellswift: EllSwiftPubKey that was sent to the other side (must have been generated
* from *this using EllSwiftCreate()).
* - initiating: whether we are the initiating party (true) or responding party (false).
*/
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift,
const EllSwiftPubKey& our_ellswift,
bool initiating) const;
};

struct CExtKey {
Expand Down
15 changes: 15 additions & 0 deletions src/pubkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <pubkey.h>

#include <secp256k1.h>
#include <secp256k1_ellswift.h>
#include <secp256k1_recovery.h>

namespace {
Expand Down Expand Up @@ -251,6 +252,20 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
return true;
}

CPubKey EllSwiftPubKey::Decode() const
{
secp256k1_pubkey pubkey;
secp256k1_ellswift_decode(secp256k1_context_static, &pubkey, UCharCast(m_pubkey.data()));

size_t sz = CPubKey::COMPRESSED_SIZE;
std::array<uint8_t, CPubKey::COMPRESSED_SIZE> vch_bytes;

secp256k1_ec_pubkey_serialize(secp256k1_context_static, vch_bytes.data(), &sz, &pubkey, SECP256K1_EC_COMPRESSED);
assert(sz == vch_bytes.size());

return CPubKey{vch_bytes.begin(), vch_bytes.end()};
}

void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
code[0] = nDepth;
memcpy(code+1, vchFingerprint, 4);
Expand Down
32 changes: 32 additions & 0 deletions src/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,38 @@ class CPubKey
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
};

/** An ElligatorSwift-encoded public key. */
struct EllSwiftPubKey
{
private:
static constexpr size_t SIZE = 64;
std::array<std::byte, SIZE> m_pubkey;

public:
/** Construct a new ellswift public key from a given serialization. */
EllSwiftPubKey(const std::array<std::byte, SIZE>& ellswift) :
m_pubkey(ellswift) {}

/** Decode to normal compressed CPubKey (for debugging purposes). */
CPubKey Decode() const;

// Read-only access for serialization.
const std::byte* data() const { return m_pubkey.data(); }
static constexpr size_t size() { return SIZE; }
auto begin() const { return m_pubkey.cbegin(); }
auto end() const { return m_pubkey.cend(); }

bool friend operator==(const EllSwiftPubKey& a, const EllSwiftPubKey& b)
{
return a.m_pubkey == b.m_pubkey;
}

bool friend operator!=(const EllSwiftPubKey& a, const EllSwiftPubKey& b)
{
return a.m_pubkey != b.m_pubkey;
}
};

struct CExtPubKey {
unsigned char nDepth;
unsigned char vchFingerprint[4];
Expand Down
6 changes: 6 additions & 0 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
return ret;
}

void FastRandomContext::fillrand(Span<std::byte> output)
{
if (requires_seed) RandomSeed();
rng.Keystream(UCharCast(output.data()), output.size());
}

FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bytebuf_size(0), bitbuf_size(0)
{
rng.SetKey(seed.begin(), 32);
Expand Down
4 changes: 4 additions & 0 deletions src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <crypto/chacha20.h>
#include <crypto/common.h>
#include <span.h>
#include <uint256.h>

#include <chrono> // For std::chrono::microseconds
Expand Down Expand Up @@ -199,6 +200,9 @@ class FastRandomContext
/** Generate random bytes. */
std::vector<unsigned char> randbytes(size_t len);

/** Fill a byte Span with random bytes. */
void fillrand(Span<std::byte> output);

/** Generate a random 32-bit integer. */
uint32_t rand32() noexcept { return randbits(32); }

Expand Down
22 changes: 15 additions & 7 deletions src/secp256k1/.cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
ECDH: no
RECOVERY: no
SCHNORRSIG: no
ELLSWIFT: no
### test options
SECP256K1_TEST_ITERS:
BENCH: yes
Expand Down Expand Up @@ -74,12 +75,12 @@ task:
<< : *LINUX_CONTAINER
matrix: &ENV_MATRIX
- env: {WIDEMUL: int64, RECOVERY: yes}
- env: {WIDEMUL: int64, ECDH: yes, SCHNORRSIG: yes}
- env: {WIDEMUL: int64, ECDH: yes, SCHNORRSIG: yes, ELLSWIFT: yes}
- env: {WIDEMUL: int128}
- env: {WIDEMUL: int128_struct}
- env: {WIDEMUL: int128, RECOVERY: yes, SCHNORRSIG: yes}
- env: {WIDEMUL: int128_struct, ELLSWIFT: yes}
- env: {WIDEMUL: int128, RECOVERY: yes, SCHNORRSIG: yes, ELLSWIFT: yes}
- env: {WIDEMUL: int128, ECDH: yes, SCHNORRSIG: yes}
- env: {WIDEMUL: int128, ASM: x86_64}
- env: {WIDEMUL: int128, ASM: x86_64 , ELLSWIFT: yes}
- env: { RECOVERY: yes, SCHNORRSIG: yes}
- env: {CTIMETESTS: no, RECOVERY: yes, ECDH: yes, SCHNORRSIG: yes, CPPFLAGS: -DVERIFY}
- env: {BUILD: distcheck, WITH_VALGRIND: no, CTIMETESTS: no, BENCH: no}
Expand Down Expand Up @@ -154,6 +155,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
<< : *MERGE_BASE
test_script:
Expand All @@ -173,10 +175,11 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
matrix:
- env: {}
- env: {EXPERIMENTAL: yes, ASM: arm}
- env: {EXPERIMENTAL: yes, ASM: arm32}
<< : *MERGE_BASE
test_script:
- ./ci/cirrus.sh
Expand All @@ -193,6 +196,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
<< : *MERGE_BASE
test_script:
Expand All @@ -210,6 +214,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
<< : *MERGE_BASE
test_script:
Expand Down Expand Up @@ -247,6 +252,7 @@ task:
RECOVERY: yes
EXPERIMENTAL: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
# Use a MinGW-w64 host to tell ./configure we're building for Windows.
# This will detect some MinGW-w64 tools but then make will need only
Expand Down Expand Up @@ -286,6 +292,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
CTIMETESTS: no
matrix:
- name: "Valgrind (memcheck)"
Expand Down Expand Up @@ -361,6 +368,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
ELLSWIFT: yes
<< : *MERGE_BASE
test_script:
- ./ci/cirrus.sh
Expand Down Expand Up @@ -397,13 +405,13 @@ task:
- PowerShell -NoLogo -Command if ($env:CIRRUS_PR -ne $null) { git fetch $env:CIRRUS_REPO_CLONE_URL pull/$env:CIRRUS_PR/merge; git reset --hard FETCH_HEAD; }
configure_script:
- '%x64_NATIVE_TOOLS%'
- cmake -G "Visual Studio 17 2022" -A x64 -S . -B build -DSECP256K1_ENABLE_MODULE_RECOVERY=ON -DSECP256K1_BUILD_EXAMPLES=ON
- cmake -E env CFLAGS="/WX" cmake -G "Visual Studio 17 2022" -A x64 -S . -B build -DSECP256K1_ENABLE_MODULE_RECOVERY=ON -DSECP256K1_BUILD_EXAMPLES=ON
build_script:
- '%x64_NATIVE_TOOLS%'
- cmake --build build --config RelWithDebInfo -- -property:UseMultiToolTask=true;CL_MPcount=5
check_script:
- '%x64_NATIVE_TOOLS%'
- ctest --test-dir build -j 5
- ctest -C RelWithDebInfo --test-dir build -j 5
- build\src\RelWithDebInfo\bench_ecmult.exe
- build\src\RelWithDebInfo\bench_internal.exe
- build\src\RelWithDebInfo\bench.exe
Loading

0 comments on commit 3cab540

Please sign in to comment.