-
Notifications
You must be signed in to change notification settings - Fork 0
chacha20 #88
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
base: master
Are you sure you want to change the base?
chacha20 #88
Changes from all commits
a535ff1
3c4a209
0d8d440
9d0a168
2b44fd0
b40a327
6a16369
3b26d2e
842a88e
0182ceb
bac9377
12b92cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,15 @@ | |
|
|
||
| #include <crypto/common.h> | ||
| #include <crypto/chacha20.h> | ||
| #include <crypto/chacha20_vec.h> | ||
| #include <support/cleanse.h> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another direct |
||
|
|
||
| #include <algorithm> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3c4a209: |
||
| #include <bit> | ||
| #include <cassert> | ||
| #include <limits> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. direct comment from global |
||
|
|
||
| static_assert(ChaCha20Aligned::BLOCKLEN == CHACHA20_VEC_BLOCKLEN); | ||
|
|
||
| #define QUARTERROUND(a,b,c,d) \ | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3c4a209:
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3c4a209: |
||
| a += b; d = std::rotl(d ^ a, 16); \ | ||
|
|
@@ -157,13 +161,14 @@ inline void ChaCha20Aligned::Keystream(std::span<std::byte> output) noexcept | |
| } | ||
| } | ||
|
|
||
| inline void ChaCha20Aligned::Crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes) noexcept | ||
| static inline void chacha20_crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes, uint32_t input[12]) noexcept | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pending
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pending
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a535ff1: |
||
| { | ||
| assert(in_bytes.size() == out_bytes.size()); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a535ff1:
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a535ff1: |
||
| const std::byte* m = in_bytes.data(); | ||
| std::byte* c = out_bytes.data(); | ||
| size_t blocks = out_bytes.size() / BLOCKLEN; | ||
| assert(blocks * BLOCKLEN == out_bytes.size()); | ||
| size_t blocks = out_bytes.size() / ChaCha20Aligned::BLOCKLEN; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. direct again |
||
| assert(blocks * ChaCha20Aligned::BLOCKLEN == out_bytes.size()); | ||
|
|
||
|
|
||
| uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; | ||
| uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; | ||
|
|
@@ -273,8 +278,29 @@ inline void ChaCha20Aligned::Crypt(std::span<const std::byte> in_bytes, std::spa | |
| return; | ||
| } | ||
| blocks -= 1; | ||
| c += BLOCKLEN; | ||
| m += BLOCKLEN; | ||
| c += ChaCha20Aligned::BLOCKLEN; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| m += ChaCha20Aligned::BLOCKLEN; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| inline void ChaCha20Aligned::Crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes) noexcept | ||
| { | ||
| assert(in_bytes.size() == out_bytes.size()); | ||
| size_t blocks = out_bytes.size() / ChaCha20Aligned::BLOCKLEN; | ||
| assert(blocks * ChaCha20Aligned::BLOCKLEN == out_bytes.size()); | ||
| #ifdef ENABLE_CHACHA20_VEC | ||
| // Only use the vectorized implementations if the counter will not overflow. | ||
| const bool overflow = static_cast<uint64_t>(input[8]) + blocks > std::numeric_limits<uint32_t>::max(); | ||
| if (blocks > 1 && !overflow) { | ||
| const auto state = std::to_array(input); | ||
| chacha20_vec_base::chacha20_crypt_vectorized(in_bytes, out_bytes, state); | ||
| const size_t blocks_written = blocks - (out_bytes.size() / ChaCha20Aligned::BLOCKLEN); | ||
| input[8] += blocks_written; | ||
| } | ||
| #endif | ||
| if (in_bytes.size()) { | ||
| chacha20_crypt(in_bytes, out_bytes, input); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) 2025-present The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #ifndef BITCOIN_CRYPTO_CHACHA20_VEC_H | ||
| #define BITCOIN_CRYPTO_CHACHA20_VEC_H | ||
|
|
||
| #include <array> | ||
| #include <cstdint> | ||
| #include <cstddef> | ||
| #include <span> | ||
|
|
||
| static constexpr size_t CHACHA20_VEC_BLOCKLEN = 64; | ||
|
|
||
| #ifdef __has_builtin | ||
| #if __has_builtin(__builtin_shufflevector) | ||
| #define ENABLE_CHACHA20_VEC 1 | ||
| #endif | ||
| #endif | ||
|
|
||
| #ifdef ENABLE_CHACHA20_VEC | ||
|
|
||
| namespace chacha20_vec_base | ||
| { | ||
| void chacha20_crypt_vectorized(std::span<const std::byte>& in_bytes, std::span<std::byte>& out_bytes, const std::array<uint32_t, 12>& input) noexcept; | ||
| } | ||
|
|
||
| #endif // ENABLE_CHACHA20_VEC | ||
|
|
||
| #endif // BITCOIN_CRYPTO_CHACHA20_VEC_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3c4a209:
direct