Skip to content

Commit 2051639

Browse files
authored
Merge pull request #1568 from evoskuil/master
Add sha256-224 and sha512-224/256/384 template specializations.
2 parents f732eab + 7034fac commit 2051639

10 files changed

Lines changed: 748 additions & 192 deletions

File tree

include/bitcoin/system/hash/algorithms.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ using rmd160_320 = rmd::algorithm<rmd::h160<320>>; // not fully implemented
3333

3434
/// bc::system sha algorithm aliases (compressed, vectorized, cached).
3535
using sha160 = sha::algorithm<sha::h160>;
36-
using sha256_224 = sha::algorithm<sha::h256<224>>; // not fully implemented
36+
using sha256_224 = sha::algorithm<sha::h256<224>>;
3737
using sha256 = sha::algorithm<sha::h256<>>;
38-
using sha512_256 = sha::algorithm<sha::h512<256>>; // not fully implemented
39-
using sha512_224 = sha::algorithm<sha::h512<224>>; // not fully implemented
40-
using sha512_384 = sha::algorithm<sha::h512<384>>; // not fully implemented
38+
using sha512_256 = sha::algorithm<sha::h512<256>>;
39+
using sha512_224 = sha::algorithm<sha::h512<224>>;
40+
using sha512_384 = sha::algorithm<sha::h512<384>>;
4141
using sha512 = sha::algorithm<sha::h512<>>;
4242

4343
} // namespace system

include/bitcoin/system/hash/sha/sha256.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,19 @@ struct k256
5858
};
5959
};
6060

61-
// Digest 224 changes IV (specialize template).
6261
template <size_t Digest = 256,
6362
bool_if<Digest == 224 || Digest == 256> = true>
6463
struct h256
6564
: public h<k256, Digest>
6665
{
67-
using state_t = typename h<k256, Digest>::state_t;
66+
// Digest 224/256 have unique IV (specialize template).
67+
};
68+
69+
template <>
70+
struct h256<256>
71+
: public h<k256, 256>
72+
{
73+
using state_t = typename h<k256, 256>::state_t;
6874

6975
// initial value (H)
7076
static constexpr state_t get
@@ -80,6 +86,26 @@ struct h256
8086
};
8187
};
8288

89+
template <>
90+
struct h256<224>
91+
: public h<k256, 224>
92+
{
93+
using state_t = typename h<k256, 224>::state_t;
94+
95+
// initial value (H)
96+
static constexpr state_t get
97+
{
98+
0xc1059ed8,
99+
0x367cd507,
100+
0x3070dd17,
101+
0xf70e5939,
102+
0xffc00b31,
103+
0x68581511,
104+
0x64f98fa7,
105+
0xbefa4fa4
106+
};
107+
};
108+
83109
} // namespace sha
84110
} // namespace system
85111
} // namespace libbitcoin

include/bitcoin/system/hash/sha/sha512.hpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ struct k512
8484
};
8585
};
8686

87-
// Digest 224/256/384 change IV (specialize template).
8887
template <size_t Digest = 512,
8988
bool_if<Digest == 224 || Digest == 256 ||
9089
Digest == 384 || Digest == 512> = true>
9190
struct h512
9291
: public h<k512, Digest>
9392
{
94-
using state_t = typename h<k512, Digest>::state_t;
93+
// Digest 224/256/384/512 have unique IV (specialize template).
94+
};
95+
96+
template <>
97+
struct h512<512>
98+
: public h<k512, 512>
99+
{
100+
using state_t = typename h<k512, 512>::state_t;
95101

96102
// initial value (H)
97103
static constexpr state_t get
@@ -107,6 +113,66 @@ struct h512
107113
};
108114
};
109115

116+
template <>
117+
struct h512<384>
118+
: public h<k512, 384>
119+
{
120+
using state_t = typename h<k512, 384>::state_t;
121+
122+
// initial value (H)
123+
static constexpr state_t get
124+
{
125+
0xcbbb9d5dc1059ed8,
126+
0x629a292a367cd507,
127+
0x9159015a3070dd17,
128+
0x152fecd8f70e5939,
129+
0x67332667ffc00b31,
130+
0x8eb44a8768581511,
131+
0xdb0c2e0d64f98fa7,
132+
0x47b5481dbefa4fa4
133+
};
134+
};
135+
136+
template <>
137+
struct h512<256>
138+
: public h<k512, 256>
139+
{
140+
using state_t = typename h<k512, 256>::state_t;
141+
142+
// initial value (H)
143+
static constexpr state_t get
144+
{
145+
0x22312194fc2bf72c,
146+
0x9f555fa3c84c64c2,
147+
0x2393b86b6f53b151,
148+
0x963877195940eabd,
149+
0x96283ee2a88effe3,
150+
0xbe5e1e2553863992,
151+
0x2b0199fc2c85b8aa,
152+
0x0eb72ddc81c52ca2
153+
};
154+
};
155+
156+
template <>
157+
struct h512<224>
158+
: public h<k512, 224>
159+
{
160+
using state_t = typename h<k512, 224>::state_t;
161+
162+
// initial value (H)
163+
static constexpr state_t get
164+
{
165+
0x8c3d37c819544da2,
166+
0x73e1996689dcd4d6,
167+
0x1dfab7ae32ff9c82,
168+
0x679dd514582f9fcf,
169+
0x0f6d2b697bd44da8,
170+
0x77e36f7304c48942,
171+
0x3f9d85a86a1d36c8,
172+
0x1112e6ad91d692a1
173+
};
174+
};
175+
110176
} // namespace sha
111177
} // namespace system
112178
} // namespace libbitcoin

include/bitcoin/system/impl/data/array_cast.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ template <typename To, typename From,
178178
if_integral_integer<From>,
179179
if_integral_array<To>>
180180
inline std_vector<std::reference_wrapper<To>>
181-
unsafe_vector_cast(From* bytes, size_t count) NOEXCEPT
181+
unsafe_vector_cast(From* bytes, size_t count) NOEXCEPT
182182
{
183183
using inner_type = array_element<To>;
184184
constexpr auto inner_count = array_count<To>;

include/bitcoin/system/impl/endian/integrals.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <bitcoin/system/define.hpp>
2424
#include <bitcoin/system/math/math.hpp>
2525

26-
// These are euqally-efficient generalizations of the common practice of
26+
// These are equally-efficient generalizations of the common practice of
2727
// shifting with or-ing to construct integers from bytes, and shifting with
2828
// masking to obtain bytes from integers. The byte<offset> template performs
2929
// integer byte extraction (zero offset is low order byte). These avoid byte

0 commit comments

Comments
 (0)