diff options
author | Pieter Wuille <pieter@wuille.net> | 2020-08-30 12:54:45 -0700 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2020-09-06 18:35:18 -0700 |
commit | 2ac8bf95834c8a43ebf365f09fb610829733134b (patch) | |
tree | 558d5cad90b566a2ceb82b3f6b2fa088f16a818e /src/crypto/sha3.h | |
parent | 56d47e19edca0c3f9898d904de271251de6d6dc5 (diff) | |
download | bitcoin-2ac8bf95834c8a43ebf365f09fb610829733134b.tar.xz |
Implement keccak-f[1600] and SHA3-256
Diffstat (limited to 'src/crypto/sha3.h')
-rw-r--r-- | src/crypto/sha3.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/crypto/sha3.h b/src/crypto/sha3.h new file mode 100644 index 0000000000..88d8c1204d --- /dev/null +++ b/src/crypto/sha3.h @@ -0,0 +1,41 @@ +// Copyright (c) 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. + +#ifndef BITCOIN_CRYPTO_SHA3_H +#define BITCOIN_CRYPTO_SHA3_H + +#include <span.h> + +#include <stdint.h> +#include <stdlib.h> + +//! The Keccak-f[1600] transform. +void KeccakF(uint64_t (&st)[25]); + +class SHA3_256 +{ +private: + uint64_t m_state[25] = {0}; + unsigned char m_buffer[8]; + unsigned m_bufsize = 0; + unsigned m_pos = 0; + + //! Sponge rate in bits. + static constexpr unsigned RATE_BITS = 1088; + + //! Sponge rate expressed as a multiple of the buffer size. + static constexpr unsigned RATE_BUFFERS = RATE_BITS / (8 * sizeof(m_buffer)); + + static_assert(RATE_BITS % (8 * sizeof(m_buffer)) == 0, "Rate must be a multiple of 8 bytes"); + +public: + static constexpr size_t OUTPUT_SIZE = 32; + + SHA3_256() {} + SHA3_256& Write(Span<const unsigned char> data); + SHA3_256& Finalize(Span<unsigned char> output); + SHA3_256& Reset(); +}; + +#endif // BITCOIN_CRYPTO_SHA3_H |