-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin/bitcoin#27479: BIP324: ElligatorSwift integrations
3168b08 Bench test for EllSwift ECDH (Pieter Wuille) 42d759f Bench tests for CKey->EllSwift (dhruv) 2e5a8a4 Fuzz test for Ellswift ECDH (dhruv) c3ac9f5 Fuzz test for CKey->EllSwift->CPubKey creation/decoding (dhruv) aae432a Unit test for ellswift creation/decoding roundtrip (dhruv) eff72a0 Add ElligatorSwift key creation and ECDH logic (Pieter Wuille) 42239f8 Enable ellswift module in libsecp256k1 (dhruv) 901336e Squashed 'src/secp256k1/' changes from 4258c54f4e..705ce7ed8c (Pieter Wuille) Pull request description: This replaces #23432 and part of #23561. This PR introduces all of the ElligatorSwift-related changes (libsecp256k1 updates, generation, decoding, ECDH, tests, fuzzing, benchmarks) needed for BIP324. ElligatorSwift is a special 64-byte encoding format for public keys introduced in libsecp256k1 in bitcoin-core/secp256k1#1129. It has the property that *every* 64-byte array is a valid encoding for some public key, and every key has approximately $2^{256}$ encodings. Furthermore, it is possible to efficiently generate a uniformly random encoding for a given public key or private key. This is used for the key exchange phase in BIP324, to achieve a byte stream that is entirely pseudorandom, even before the shared encryption key is established. ACKs for top commit: instagibbs: reACK bitcoin/bitcoin@3168b08 achow101: ACK 3168b08 theStack: re-ACK 3168b08 Tree-SHA512: 308ac3d33e9a2deecb65826cbf0390480a38de201918429c35c796f3421cdf94c5501d027a043ae8f012cfaa0584656da1de6393bfba3532ab4c20f9533f06a6
- Loading branch information
Showing
90 changed files
with
4,198 additions
and
1,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, benchmark::PriorityLevel::HIGH); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, AsBytePtr(entropy.data())); | ||
}); | ||
|
||
ECC_Stop(); | ||
} | ||
|
||
BENCHMARK(EllSwiftCreate, benchmark::PriorityLevel::HIGH); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.