Skip to content

Commit b804aba

Browse files
committed
Update ref_app hash sha256
1 parent 0431f6d commit b804aba

File tree

9 files changed

+195
-141
lines changed

9 files changed

+195
-141
lines changed

examples/chapter11_07a/src/app/pi_spigot/pi_spigot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ namespace local
4949
return instance;
5050
}
5151

52-
auto pi_output_digits10 = static_cast<std::uint32_t>(UINT8_C(0));
52+
auto pi_output_digits10 = std::uint32_t { UINT8_C(0) };
5353

5454
using benchmark_port_type = ::mcal::benchmark::benchmark_port_type;
5555

56-
constexpr auto pi_spigot_input_start_address = static_cast<mcal_sram_uintptr_t>(UINT8_C(0));
56+
constexpr auto pi_spigot_input_start_address = mcal_sram_uintptr_t { UINT8_C(0) };
5757

5858
using pi_spigot_input_container_type = mcal::memory::sram::array<std::uint32_t,
5959
pi_spigot_type::get_input_static_size(),

ref_app/ref_app.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,7 @@
14771477
<ClInclude Include="src\math\checksums\hash\hash_detail.h" />
14781478
<ClInclude Include="src\math\checksums\hash\hash_sha1.h" />
14791479
<ClInclude Include="src\math\checksums\hash\hash_sha256.h" />
1480+
<ClInclude Include="src\math\checksums\hash\hash_stream_base.h" />
14801481
<ClInclude Include="src\math\constants\constants.h" />
14811482
<ClInclude Include="src\math\constants\constant_functions.h" />
14821483
<ClInclude Include="src\math\constants\pi_spigot_base.h" />

ref_app/ref_app.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,9 @@
30423042
<ClInclude Include="src\mcal_memory\mcal_memory_sram_types.h">
30433043
<Filter>src\mcal_memory</Filter>
30443044
</ClInclude>
3045+
<ClInclude Include="src\math\checksums\hash\hash_stream_base.h">
3046+
<Filter>src\math\checksums\hash</Filter>
3047+
</ClInclude>
30453048
</ItemGroup>
30463049
<ItemGroup>
30473050
<None Include="src\util\STL\algorithm">

ref_app/src/app/benchmark/app_benchmark_hash_sha256.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2013 - 2023.
2+
// Copyright Christopher Kormanyos 2013 - 2026.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -14,7 +14,7 @@
1414

1515
namespace
1616
{
17-
using app_benchmark_hash_type = math::checksums::hash::hash_sha256<std::uint_fast16_t>;
17+
using app_benchmark_hash_type = math::checksums::hash::hash_sha256;
1818

1919
app_benchmark_hash_type app_benchmark_hash_object;
2020

@@ -33,17 +33,19 @@ namespace
3333

3434
auto app::benchmark::run_hash_sha256() -> bool
3535
{
36-
static constexpr std::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(3))> app_benchmark_hash_data =
36+
using app_benchmark_msg_array_type = std::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(3))>;
37+
38+
constexpr app_benchmark_msg_array_type app_benchmark_hash_msg =
3739
{
3840
static_cast<std::uint8_t>('a'),
3941
static_cast<std::uint8_t>('b'),
4042
static_cast<std::uint8_t>('c')
4143
};
4244

43-
app_benchmark_hash_object.hash(app_benchmark_hash_data.data(),
44-
static_cast<app_benchmark_hash_count_type>(app_benchmark_hash_data.size()));
45+
app_benchmark_hash_object.hash(app_benchmark_hash_msg.data(),
46+
static_cast<app_benchmark_hash_count_type>(std::tuple_size<app_benchmark_msg_array_type>::value));
4547

46-
auto local_hash_result = app_benchmark_hash_result_type { };
48+
app_benchmark_hash_result_type local_hash_result { };
4749

4850
app_benchmark_hash_object.get_result(local_hash_result.data());
4951

ref_app/src/math/checksums/hash/hash_base.h

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2013 - 2025.
2+
// Copyright Christopher Kormanyos 2013 - 2026.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -8,58 +8,62 @@
88
#ifndef HASH_BASE_2013_09_05_H
99
#define HASH_BASE_2013_09_05_H
1010

11-
#include <limits>
12-
1311
#include <math/checksums/hash/hash_detail.h>
12+
#include <math/checksums/hash/hash_stream_base.h>
13+
14+
#include <array>
15+
#include <cstddef>
16+
#include <cstdint>
17+
#include <limits>
1418

1519
namespace math { namespace checksums { namespace hash {
1620

17-
template<typename CountType,
18-
const std::uint16_t ResultBitCount,
19-
const std::uint16_t MessageBufferSize,
20-
const std::uint16_t MessageLengthTotalBitCount>
21-
class hash_base
21+
template<const std::uint16_t ResultBitCount,
22+
const std::uint16_t MessageBlockBufferSize,
23+
const std::uint16_t MessageBlockBitCount>
24+
class hash_base : public hash_stream_base
2225
{
23-
public:
24-
using count_type = CountType;
26+
private:
27+
using base_class_type = hash_stream_base;
2528

29+
public:
2630
using result_type = std::array<std::uint8_t, static_cast<std::size_t>(ResultBitCount / static_cast<std::uint16_t>(UINT8_C(8)))>;
2731

2832
static_assert
2933
(
30-
std::numeric_limits<count_type>::is_specialized
31-
&& std::numeric_limits<count_type>::is_integer
32-
&& (!std::numeric_limits<count_type>::is_signed)
33-
&& (std::numeric_limits<count_type>::radix == static_cast<int>(INT8_C(2)))
34-
&& (std::numeric_limits<count_type>::digits >= static_cast<int>(INT8_C(16)))
35-
&& (static_cast<int>(std::numeric_limits<count_type>::digits % static_cast<int>(INT8_C(8))) == static_cast<int>(INT8_C(0))),
36-
"Error: The count type must be an unsigned integer with radix 2, that is 16 bits or wider, and having a multiple of 8 bits"
34+
std::numeric_limits<base_class_type::count_type>::is_specialized
35+
&& std::numeric_limits<base_class_type::count_type>::is_integer
36+
&& (!std::numeric_limits<base_class_type::count_type>::is_signed)
37+
&& (std::numeric_limits<base_class_type::count_type>::radix == static_cast<int>(INT8_C(2)))
38+
&& (std::numeric_limits<base_class_type::count_type>::digits >= static_cast<int>(INT8_C(32)))
39+
&& (static_cast<int>(std::numeric_limits<base_class_type::count_type>::digits % static_cast<int>(INT8_C(8))) == static_cast<int>(INT8_C(0))),
40+
"Error: The count type must be an unsigned integer with radix 2, that is 32 bits or wider, and having a multiple of 8 bits"
3741
);
3842

39-
virtual ~hash_base() = default;
43+
~hash_base() override = default;
4044

41-
virtual auto initialize() -> void
45+
auto initialize() -> void override
4246
{
43-
message_index = static_cast<std::uint_least16_t>(UINT8_C(0));
47+
message_index = static_cast<std::uint_least32_t>(UINT8_C(0));
4448
message_length_total = static_cast<count_type>(UINT8_C(0));
4549

4650
message_buffer.fill(static_cast<std::uint8_t>(UINT8_C(0)));
4751
}
4852

49-
auto process(const std::uint8_t* message, const count_type count) -> void
53+
auto process(const std::uint8_t* message, const count_type count) -> void override
5054
{
5155
auto process_index = count_type { };
5256
auto process_chunk_size = count_type { };
5357

5458
while(process_index < count)
5559
{
56-
message_index = static_cast<std::uint_least16_t>(message_index + process_chunk_size);
60+
message_index = static_cast<std::uint_least32_t>(message_index + process_chunk_size);
5761
message_length_total = static_cast<count_type> (message_length_total + process_chunk_size);
5862
process_index = static_cast<count_type> (process_index + process_chunk_size);
5963

6064
if(message_index == message_buffer_static_size())
6165
{
62-
my_perform_algorithm();
66+
perform_algorithm();
6367
}
6468

6569
process_chunk_size = (std::min)(static_cast<count_type>(count - process_index),
@@ -72,10 +76,10 @@
7276
}
7377
}
7478

75-
auto finalize() -> void
79+
auto finalize() -> void override
7680
{
7781
// Create the padding. Begin by setting the leading padding byte to 0x80.
78-
message_buffer[message_index] = static_cast<std::uint8_t>(UINT8_C(0x80));
82+
message_buffer[static_cast<std::size_t>(message_index)] = static_cast<std::uint8_t>(UINT8_C(0x80));
7983

8084
++message_index;
8185

@@ -86,12 +90,12 @@
8690
const auto message_top =
8791
static_cast<std::uint16_t>
8892
(
89-
message_index + static_cast<std::uint_least16_t>(message_length_total_width())
93+
message_index + static_cast<std::uint_least32_t>(message_length_total_width())
9094
);
9195

9296
if(message_top > message_buffer_static_size())
9397
{
94-
my_perform_algorithm();
98+
perform_algorithm();
9599
}
96100

97101
// Encode the total number of bits in the final transform buffer.
@@ -105,10 +109,10 @@
105109
ri != message_buffer.rbegin() + static_cast<std::size_t>(message_length_total_width());
106110
++ri)
107111
{
108-
const std::uint_least16_t the_word =
109-
static_cast<std::uint_least16_t>
112+
const std::uint_least32_t the_word =
113+
static_cast<std::uint_least32_t>
110114
(
111-
static_cast<std::uint_least16_t>(message_length_total) << static_cast<unsigned>(UINT8_C(3))
115+
static_cast<std::uint_least32_t>(message_length_total) << static_cast<unsigned>(UINT8_C(3))
112116
);
113117

114118
*ri = static_cast<std::uint8_t>(the_word | carry);
@@ -127,7 +131,7 @@
127131
);
128132
}
129133

130-
my_perform_algorithm();
134+
perform_algorithm();
131135
}
132136

133137
auto get_result(typename result_type::pointer result) -> void
@@ -149,7 +153,7 @@
149153
}
150154

151155
protected:
152-
using message_block_type = std::array<std::uint8_t, static_cast<std::size_t>(MessageBufferSize)>;
156+
using message_block_type = std::array<std::uint8_t, static_cast<std::size_t>(MessageBlockBufferSize)>;
153157

154158
using context_type = std::array<std::uint32_t, static_cast<std::size_t>(std::tuple_size<result_type>::value / sizeof(std::uint32_t))>;
155159

@@ -158,38 +162,38 @@
158162
return static_cast<std::uint16_t>(std::tuple_size<message_block_type>::value);
159163
}
160164

161-
std::uint_least16_t message_index { };
165+
std::uint_least32_t message_index { };
162166
count_type message_length_total { };
163167
message_block_type message_buffer { };
164168
context_type transform_context { };
165169

166170
hash_base() = default;
167171

168172
hash_base(const hash_base&) = default;
169-
hash_base(hash_base&&) noexcept = default;
173+
hash_base(hash_base&&) = default;
170174

171175
auto operator=(const hash_base& other) -> hash_base& = default;
172-
auto operator=(hash_base&& other) noexcept -> hash_base& = default;
176+
auto operator=(hash_base&& other) -> hash_base& = default;
173177

174178
private:
175-
static constexpr auto message_length_total_width() noexcept -> std::uint16_t
179+
static constexpr auto message_length_total_width() -> std::uint16_t
176180
{
177181
return
178182
static_cast<std::uint16_t>
179183
(
180-
MessageLengthTotalBitCount / static_cast<std::uint16_t>(UINT8_C(8))
184+
MessageBlockBitCount / static_cast<std::uint16_t>(UINT8_C(8))
181185
);
182186
}
183187

184-
virtual auto perform_algorithm() -> void = 0;
188+
virtual auto my_perform_algorithm() -> void = 0;
185189

186-
auto my_perform_algorithm() -> void
190+
auto perform_algorithm() -> void
187191
{
188-
this->perform_algorithm();
192+
this->my_perform_algorithm();
189193

190-
message_index = static_cast<std::uint_least16_t>(UINT8_C(0));
194+
message_index = std::uint_least32_t { UINT8_C(0) };
191195

192-
message_buffer.fill(static_cast<std::uint8_t>(UINT8_C(0)));
196+
message_buffer.fill(std::uint8_t { UINT8_C(0) });
193197
}
194198
};
195199

ref_app/src/math/checksums/hash/hash_detail.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright Christopher Kormanyos 2013 - 2025.
1+
// Copyright Christopher Kormanyos 2013 - 2026.
22
// Distributed under the Boost Software License,
33
// Version 1.0. (See accompanying file LICENSE_1_0.txt
44
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -9,6 +9,7 @@
99

1010
#include <cstddef>
1111
#include <cstdint>
12+
#include <limits>
1213

1314
namespace math { namespace checksums { namespace hash { namespace detail {
1415

@@ -36,7 +37,7 @@
3637
);
3738
}
3839

39-
inline auto convert_uint8_input_to_uint32_output(const std::uint8_t* in_begin, const std::uint8_t* in_end, std::uint32_t* out_begin) -> void
40+
constexpr auto convert_uint8_input_to_uint32_output(const std::uint8_t* in_begin, const std::uint8_t* in_end, std::uint32_t* out_begin) -> void
4041
{
4142
// Decode the input uint8_t source into the output uint32_t destination.
4243
// This subroutine assumes that the length of the input is a multiple of 4.
@@ -60,7 +61,7 @@
6061
}
6162
}
6263

63-
inline auto convert_uint8_input_to_uint32_output_reverse(const std::uint8_t* in_begin, const std::uint8_t* in_end, std::uint32_t* out_begin) -> void
64+
constexpr auto convert_uint8_input_to_uint32_output_reverse(const std::uint8_t* in_begin, const std::uint8_t* in_end, std::uint32_t* out_begin) -> void
6465
{
6566
// Decode the input uint8_t source into the output uint32_t destination.
6667
// This subroutine assumes that the length of the input is a multiple of 4.
@@ -84,7 +85,7 @@
8485
}
8586
}
8687

87-
inline auto convert_uint32_input_to_uint8_output(const std::uint32_t* in_begin, const std::uint32_t* in_end, std::uint8_t* out_begin) -> void
88+
constexpr auto convert_uint32_input_to_uint8_output(const std::uint32_t* in_begin, const std::uint32_t* in_end, std::uint8_t* out_begin) -> void
8889
{
8990
// Encode the input uint32_t source into the output uint8_t destination.
9091
// This subroutine assumes that the length of the output is a multiple of 4.
@@ -104,7 +105,7 @@
104105
}
105106
}
106107

107-
inline auto convert_uint32_input_to_uint8_output_reverse(const std::uint32_t* in_begin, const std::uint32_t* in_end, std::uint8_t* out_begin) -> void
108+
constexpr auto convert_uint32_input_to_uint8_output_reverse(const std::uint32_t* in_begin, const std::uint32_t* in_end, std::uint8_t* out_begin) -> void
108109
{
109110
// Encode the input uint32_t source into the output uint8_t destination.
110111
// This subroutine assumes that the length of the output is a multiple of 4.

0 commit comments

Comments
 (0)