Skip to content

Commit 2ac8bf9

Browse files
committed
Implement keccak-f[1600] and SHA3-256
1 parent 56d47e1 commit 2ac8bf9

File tree

4 files changed

+278
-0
lines changed

4 files changed

+278
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ crypto_libbitcoin_crypto_base_a_SOURCES = \
403403
crypto/sha1.h \
404404
crypto/sha256.cpp \
405405
crypto/sha256.h \
406+
crypto/sha3.cpp \
407+
crypto/sha3.h \
406408
crypto/sha512.cpp \
407409
crypto/sha512.h \
408410
crypto/siphash.cpp \

src/crypto/sha3.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
// Based on https://github.com/mjosaarinen/tiny_sha3/blob/master/sha3.c
6+
// by Markku-Juhani O. Saarinen <[email protected]>
7+
8+
#include <crypto/sha3.h>
9+
#include <crypto/common.h>
10+
#include <span.h>
11+
12+
#include <algorithm>
13+
#include <array> // For std::begin and std::end.
14+
15+
#include <stdint.h>
16+
17+
// Internal implementation code.
18+
namespace
19+
{
20+
uint64_t Rotl(uint64_t x, int n) { return (x << n) | (x >> (64 - n)); }
21+
} // namespace
22+
23+
void KeccakF(uint64_t (&st)[25])
24+
{
25+
static constexpr uint64_t RNDC[24] = {
26+
0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
27+
0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
28+
0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
29+
0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
30+
0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
31+
0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
32+
};
33+
static constexpr int ROTC[24] = {
34+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
35+
27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
36+
};
37+
static constexpr int PILN[24] = {
38+
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
39+
15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
40+
};
41+
static constexpr int ROUNDS = 24;
42+
43+
for (int round = 0; round < ROUNDS; ++round) {
44+
uint64_t bc[5], t;
45+
46+
// Theta
47+
for (int i = 0; i < 5; i++) {
48+
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
49+
}
50+
51+
for (int i = 0; i < 5; i++) {
52+
t = bc[(i + 4) % 5] ^ Rotl(bc[(i + 1) % 5], 1);
53+
for (int j = 0; j < 25; j += 5) st[j + i] ^= t;
54+
}
55+
56+
// Rho Pi
57+
t = st[1];
58+
for (int i = 0; i < 24; i++) {
59+
int j = PILN[i];
60+
bc[0] = st[j];
61+
st[j] = Rotl(t, ROTC[i]);
62+
t = bc[0];
63+
}
64+
65+
// Chi
66+
for (int j = 0; j < 25; j += 5) {
67+
for (int i = 0; i < 5; i++) bc[i] = st[j + i];
68+
for (int i = 0; i < 5; i++) {
69+
st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
70+
}
71+
}
72+
73+
// Iota
74+
st[0] ^= RNDC[round];
75+
}
76+
}
77+
78+
SHA3_256& SHA3_256::Write(Span<const unsigned char> data)
79+
{
80+
if (m_bufsize && m_bufsize + data.size() >= sizeof(m_buffer)) {
81+
// Fill the buffer and process it.
82+
std::copy(data.begin(), data.begin() + sizeof(m_buffer) - m_bufsize, m_buffer + m_bufsize);
83+
data = data.subspan(sizeof(m_buffer) - m_bufsize);
84+
m_state[m_pos++] ^= ReadLE64(m_buffer);
85+
m_bufsize = 0;
86+
if (m_pos == RATE_BUFFERS) {
87+
KeccakF(m_state);
88+
m_pos = 0;
89+
}
90+
}
91+
while (data.size() >= sizeof(m_buffer)) {
92+
// Process chunks directly from the buffer.
93+
m_state[m_pos++] ^= ReadLE64(data.data());
94+
data = data.subspan(8);
95+
if (m_pos == RATE_BUFFERS) {
96+
KeccakF(m_state);
97+
m_pos = 0;
98+
}
99+
}
100+
if (data.size()) {
101+
// Keep the remainder in the buffer.
102+
std::copy(data.begin(), data.end(), m_buffer + m_bufsize);
103+
m_bufsize += data.size();
104+
}
105+
return *this;
106+
}
107+
108+
SHA3_256& SHA3_256::Finalize(Span<unsigned char> output)
109+
{
110+
assert(output.size() == OUTPUT_SIZE);
111+
std::fill(m_buffer + m_bufsize, m_buffer + sizeof(m_buffer), 0);
112+
m_buffer[m_bufsize] ^= 0x06;
113+
m_state[m_pos] ^= ReadLE64(m_buffer);
114+
m_state[RATE_BUFFERS - 1] ^= 0x8000000000000000;
115+
KeccakF(m_state);
116+
for (unsigned i = 0; i < 4; ++i) {
117+
WriteLE64(output.data() + 8 * i, m_state[i]);
118+
}
119+
return *this;
120+
}
121+
122+
SHA3_256& SHA3_256::Reset()
123+
{
124+
m_bufsize = 0;
125+
m_pos = 0;
126+
std::fill(std::begin(m_state), std::end(m_state), 0);
127+
return *this;
128+
}

src/crypto/sha3.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_CRYPTO_SHA3_H
6+
#define BITCOIN_CRYPTO_SHA3_H
7+
8+
#include <span.h>
9+
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
13+
//! The Keccak-f[1600] transform.
14+
void KeccakF(uint64_t (&st)[25]);
15+
16+
class SHA3_256
17+
{
18+
private:
19+
uint64_t m_state[25] = {0};
20+
unsigned char m_buffer[8];
21+
unsigned m_bufsize = 0;
22+
unsigned m_pos = 0;
23+
24+
//! Sponge rate in bits.
25+
static constexpr unsigned RATE_BITS = 1088;
26+
27+
//! Sponge rate expressed as a multiple of the buffer size.
28+
static constexpr unsigned RATE_BUFFERS = RATE_BITS / (8 * sizeof(m_buffer));
29+
30+
static_assert(RATE_BITS % (8 * sizeof(m_buffer)) == 0, "Rate must be a multiple of 8 bytes");
31+
32+
public:
33+
static constexpr size_t OUTPUT_SIZE = 32;
34+
35+
SHA3_256() {}
36+
SHA3_256& Write(Span<const unsigned char> data);
37+
SHA3_256& Finalize(Span<unsigned char> output);
38+
SHA3_256& Reset();
39+
};
40+
41+
#endif // BITCOIN_CRYPTO_SHA3_H

0 commit comments

Comments
 (0)