Skip to content

Commit e200ecf

Browse files
authored
Merge pull request #514 from ckormanyos/tuning_and_tests
More tuning and add test depth
2 parents 9451293 + e9aa272 commit e200ecf

3 files changed

Lines changed: 65 additions & 28 deletions

File tree

examples/example012_rsa_crypto.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ namespace local_rsa
8888
} // namespace detail
8989

9090
template<const std::size_t RsaBitCount,
91-
typename LimbType = std::uint32_t,
92-
typename AllocatorType = std::allocator<void>>
91+
#if defined(WIDE_INTEGER_NAMESPACE)
92+
typename LimbType = WIDE_INTEGER_NAMESPACE::math::wide_integer::uint_defaultlimb_t,
93+
#else
94+
typename LimbType = ::math::wide_integer::uint_defaultlimb_t,
95+
#endif
96+
typename AllocatorType = std::allocator<LimbType>>
9397
class rsa_base
9498
{
9599
public:
@@ -342,7 +346,11 @@ namespace local_rsa
342346
};
343347

344348
template<const std::size_t RsaBitCount,
345-
typename LimbType = std::uint32_t>
349+
#if defined(WIDE_INTEGER_NAMESPACE)
350+
typename LimbType = WIDE_INTEGER_NAMESPACE::math::wide_integer::uint_defaultlimb_t>
351+
#else
352+
typename LimbType = ::math::wide_integer::uint_defaultlimb_t>
353+
#endif
346354
class rsa_fips : public rsa_base<RsaBitCount, LimbType>
347355
{
348356
private:
@@ -384,7 +392,11 @@ namespace local_rsa
384392
};
385393

386394
template<const std::size_t RsaBitCount,
387-
typename LimbType = std::uint32_t>
395+
#if defined(WIDE_INTEGER_NAMESPACE)
396+
typename LimbType = WIDE_INTEGER_NAMESPACE::math::wide_integer::uint_defaultlimb_t>
397+
#else
398+
typename LimbType = ::math::wide_integer::uint_defaultlimb_t>
399+
#endif
388400
class rsa_traditional : public rsa_base<RsaBitCount, LimbType>
389401
{
390402
private:

math/wide_integer/uintwide_t.h

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,7 @@
28242824

28252825
using string_storage_oct_type =
28262826
std::conditional_t
2827-
<my_width2 <= static_cast<size_t>(UINT32_C(2048)),
2827+
<my_width2 <= static_cast<size_t>(UINT32_C(512)),
28282828
detail::fixed_static_array <char,
28292829
wr_string_max_buffer_size_oct()>,
28302830
detail::fixed_dynamic_array<char,
@@ -2916,7 +2916,7 @@
29162916

29172917
using string_storage_dec_type =
29182918
std::conditional_t
2919-
<my_width2 <= static_cast<size_t>(UINT32_C(2048)),
2919+
<my_width2 <= static_cast<size_t>(UINT32_C(512)),
29202920
detail::fixed_static_array <char,
29212921
wr_string_max_buffer_size_dec()>,
29222922
detail::fixed_dynamic_array<char,
@@ -3009,7 +3009,7 @@
30093009

30103010
using string_storage_hex_type =
30113011
std::conditional_t
3012-
<my_width2 <= static_cast<size_t>(UINT32_C(2048)),
3012+
<my_width2 <= static_cast<size_t>(UINT32_C(512)),
30133013
detail::fixed_static_array <char,
30143014
wr_string_max_buffer_size_hex()>,
30153015
detail::fixed_dynamic_array<char,
@@ -5757,7 +5757,7 @@
57575757
{
57585758
using string_storage_oct_type =
57595759
std::conditional_t
5760-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
5760+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
57615761
detail::fixed_static_array <char,
57625762
local_wide_integer_type::wr_string_max_buffer_size_oct()>,
57635763
detail::fixed_dynamic_array<char,
@@ -5766,8 +5766,10 @@
57665766
std::allocator<void>,
57675767
AllocatorType>>::template rebind_alloc<typename local_wide_integer_type::limb_type>>>;
57685768

5769-
// TBD: There is redundant storage of this kind both here
5769+
// There is redundant storage of this kind both here
57705770
// in this subroutine as well as in the wr_string method.
5771+
// This is on purpose for convenience but might be
5772+
// changed in the future.
57715773
string_storage_oct_type str_result { }; // LCOV_EXCL_LINE
57725774

57735775
str_result.fill('\0');
@@ -5780,7 +5782,7 @@
57805782
{
57815783
using string_storage_dec_type =
57825784
std::conditional_t
5783-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
5785+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
57845786
detail::fixed_static_array <char,
57855787
local_wide_integer_type::wr_string_max_buffer_size_dec()>,
57865788
detail::fixed_dynamic_array<char,
@@ -5789,8 +5791,10 @@
57895791
std::allocator<void>,
57905792
AllocatorType>>::template rebind_alloc<typename local_wide_integer_type::limb_type>>>;
57915793

5792-
// TBD: There is redundant storage of this kind both here
5794+
// There is redundant storage of this kind both here
57935795
// in this subroutine as well as in the wr_string method.
5796+
// This is on purpose for convenience but might be
5797+
// changed in the future.
57945798
string_storage_dec_type str_result { };
57955799

57965800
str_result.fill('\0');
@@ -5803,7 +5807,7 @@
58035807
{
58045808
using string_storage_hex_type =
58055809
std::conditional_t
5806-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
5810+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
58075811
detail::fixed_static_array <char,
58085812
local_wide_integer_type::wr_string_max_buffer_size_hex()>,
58095813
detail::fixed_dynamic_array<char,
@@ -7261,7 +7265,7 @@
72617265
{
72627266
using string_storage_oct_type =
72637267
std::conditional_t
7264-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
7268+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
72657269
detail::fixed_static_array <char,
72667270
local_wide_integer_type::wr_string_max_buffer_size_oct()>,
72677271
detail::fixed_dynamic_array<char,
@@ -7270,14 +7274,14 @@
72707274
std::allocator<void>,
72717275
AllocatorType>>::template rebind_alloc<typename local_wide_integer_type::limb_type>>>;
72727276

7273-
using local_size_type = typename string_storage_oct_type::size_type;
7274-
72757277
string_storage_oct_type str_temp { };
72767278

72777279
const auto wr_string_is_ok = x.wr_string(str_temp.begin(), base_rep, show_base, show_pos, is_uppercase);
72787280

72797281
if(wr_string_is_ok)
72807282
{
7283+
using local_size_type = typename string_storage_oct_type::size_type;
7284+
72817285
const auto chars_retrieved = static_cast<local_size_type>(detail::strlen_unsafe(str_temp.data()));
72827286

72837287
const auto chars_to_get = static_cast<local_size_type>(detail::distance_unsafe(first, last));
@@ -7286,14 +7290,17 @@
72867290
str_temp.cbegin() + (detail::min_unsafe)(chars_retrieved, chars_to_get),
72877291
first);
72887292

7289-
result.ec = std::errc();
7293+
if(chars_retrieved <= chars_to_get)
7294+
{
7295+
result.ec = std::errc();
7296+
}
72907297
}
72917298
}
72927299
else if(base_rep == static_cast<std::uint_fast8_t>(UINT8_C(16)))
72937300
{
72947301
using string_storage_hex_type =
72957302
std::conditional_t
7296-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
7303+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
72977304
detail::fixed_static_array <char,
72987305
local_wide_integer_type::wr_string_max_buffer_size_hex()>,
72997306
detail::fixed_dynamic_array<char,
@@ -7302,14 +7309,14 @@
73027309
std::allocator<void>,
73037310
AllocatorType>>::template rebind_alloc<typename local_wide_integer_type::limb_type>>>;
73047311

7305-
using local_size_type = typename string_storage_hex_type::size_type;
7306-
73077312
string_storage_hex_type str_temp { };
73087313

73097314
const auto wr_string_is_ok = x.wr_string(str_temp.begin(), base_rep, show_base, show_pos, is_uppercase);
73107315

73117316
if(wr_string_is_ok)
73127317
{
7318+
using local_size_type = typename string_storage_hex_type::size_type;
7319+
73137320
const auto chars_retrieved = static_cast<local_size_type>(detail::strlen_unsafe(str_temp.data()));
73147321

73157322
const auto chars_to_get = static_cast<local_size_type>(detail::distance_unsafe(first, last));
@@ -7318,14 +7325,17 @@
73187325
str_temp.cbegin() + (detail::min_unsafe)(chars_retrieved, chars_to_get),
73197326
first);
73207327

7321-
result.ec = std::errc();
7328+
if(chars_retrieved <= chars_to_get)
7329+
{
7330+
result.ec = std::errc();
7331+
}
73227332
}
73237333
}
73247334
else
73257335
{
73267336
using string_storage_dec_type =
73277337
std::conditional_t
7328-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
7338+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
73297339
detail::fixed_static_array <char,
73307340
local_wide_integer_type::wr_string_max_buffer_size_dec()>,
73317341
detail::fixed_dynamic_array<char,
@@ -7334,14 +7344,14 @@
73347344
std::allocator<void>,
73357345
AllocatorType>>::template rebind_alloc<typename local_wide_integer_type::limb_type>>>;
73367346

7337-
using local_size_type = typename string_storage_dec_type::size_type;
7338-
73397347
string_storage_dec_type str_temp { };
73407348

73417349
const auto wr_string_is_ok = x.wr_string(str_temp.begin(), base_rep, show_base, show_pos, is_uppercase);
73427350

73437351
if(wr_string_is_ok)
73447352
{
7353+
using local_size_type = typename string_storage_dec_type::size_type;
7354+
73457355
const auto chars_retrieved = static_cast<local_size_type>(detail::strlen_unsafe(str_temp.data()));
73467356

73477357
const auto chars_to_get = static_cast<local_size_type>(detail::distance_unsafe(first, last));
@@ -7350,7 +7360,10 @@
73507360
str_temp.cbegin() + (detail::min_unsafe)(chars_retrieved, chars_to_get),
73517361
first);
73527362

7353-
result.ec = std::errc();
7363+
if(chars_retrieved <= chars_to_get)
7364+
{
7365+
result.ec = std::errc();
7366+
}
73547367
}
73557368
}
73567369

@@ -7415,7 +7428,7 @@
74157428

74167429
using string_storage_dec_type =
74177430
std::conditional_t
7418-
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(2048)),
7431+
<local_wide_integer_type::my_width2 <= static_cast<size_t>(UINT32_C(512)),
74197432
detail::fixed_static_array <char,
74207433
local_wide_integer_type::wr_string_max_buffer_size_dec()>,
74217434
detail::fixed_dynamic_array<char,

test/test_uintwide_t_edge_cases.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,18 @@ auto test_to_and_from_chars_and_to_string() -> bool // NOLINT(readability-functi
19031903

19041904
using std::to_chars;
19051905

1906+
const auto result_oct_as_chars_value_too_large = to_chars(arr_oct.data(), arr_oct.data() + std::size_t { UINT8_C(2) }, u_gen, 8);
1907+
const auto result_dec_as_chars_value_too_large = to_chars(arr_dec.data(), arr_dec.data() + std::size_t { UINT8_C(2) }, u_gen, 10);
1908+
const auto result_hex_as_chars_value_too_large = to_chars(arr_hex.data(), arr_hex.data() + std::size_t { UINT8_C(2) }, u_gen, 16);
1909+
1910+
const bool result_oct_as_chars_value_too_large_is_ok { (result_oct_as_chars_value_too_large.ec == std::errc::value_too_large) };
1911+
const bool result_dec_as_chars_value_too_large_is_ok { (result_dec_as_chars_value_too_large.ec == std::errc::value_too_large) };
1912+
const bool result_hex_as_chars_value_too_large_is_ok { (result_hex_as_chars_value_too_large.ec == std::errc::value_too_large) };
1913+
1914+
result_is_ok = (result_oct_as_chars_value_too_large_is_ok && result_is_ok);
1915+
result_is_ok = (result_dec_as_chars_value_too_large_is_ok && result_is_ok);
1916+
result_is_ok = (result_hex_as_chars_value_too_large_is_ok && result_is_ok);
1917+
19061918
const auto result_oct_as_chars = to_chars(arr_oct.data(), arr_oct.data() + arr_oct.size(), u_gen, 8);
19071919
const auto result_dec_as_chars = to_chars(arr_dec.data(), arr_dec.data() + arr_dec.size(), u_gen, 10);
19081920
const auto result_hex_as_chars = to_chars(arr_hex.data(), arr_hex.data() + arr_hex.size(), u_gen, 16);
@@ -1915,7 +1927,7 @@ auto test_to_and_from_chars_and_to_string() -> bool // NOLINT(readability-functi
19151927

19161928
std::string str_u_gen(arr_oct.data(), result_oct_as_chars.ptr);
19171929

1918-
const bool result_stream_and_to_chars_is_ok { (str_u_gen == strm.str()) };
1930+
const bool result_stream_and_to_chars_is_ok { (str_u_gen == strm.str()) && (result_oct_as_chars.ec == std::errc()) };
19191931

19201932
result_is_ok = (result_stream_and_to_chars_is_ok && result_is_ok);
19211933
}
@@ -1932,7 +1944,7 @@ auto test_to_and_from_chars_and_to_string() -> bool // NOLINT(readability-functi
19321944

19331945
result_is_ok = (result_stream_and_to_chars_is_ok && result_is_ok);
19341946

1935-
const bool result_to_chars_and_to_string_is_ok { str_u_gen == to_string(u_gen) };
1947+
const bool result_to_chars_and_to_string_is_ok { (str_u_gen == to_string(u_gen)) && (result_dec_as_chars.ec == std::errc()) };
19361948

19371949
result_is_ok = (result_to_chars_and_to_string_is_ok && result_is_ok);
19381950
}
@@ -1945,7 +1957,7 @@ auto test_to_and_from_chars_and_to_string() -> bool // NOLINT(readability-functi
19451957

19461958
std::string str_u_gen(arr_hex.data(), result_hex_as_chars.ptr);
19471959

1948-
const bool result_stream_and_to_chars_is_ok { (str_u_gen == strm.str()) };
1960+
const bool result_stream_and_to_chars_is_ok { (str_u_gen == strm.str()) && (result_hex_as_chars.ec == std::errc()) };
19491961

19501962
result_is_ok = (result_stream_and_to_chars_is_ok && result_is_ok);
19511963
}

0 commit comments

Comments
 (0)