diff --git a/examples/example013_ecdsa_sign_verify.cpp b/examples/example013_ecdsa_sign_verify.cpp index c58ff6e..98369b5 100644 --- a/examples/example013_ecdsa_sign_verify.cpp +++ b/examples/example013_ecdsa_sign_verify.cpp @@ -37,11 +37,11 @@ THE SOFTWARE. -----------------------------------------------------------------------------*/ -// For algorithm description of ECDSA, see -// D. Hankerson, A. Menezes, S. Vanstone, "Guide to Elliptic -// Curve Cryptography", Springer 2004, Chapter 4, in particular -// Algorithm 4.24 (keygen on page 180), and Algorithms 4.29 and 4.30 -// (sign/verify on page 184). +// For algorithm description of ECDSA, please consult also: +// D. Hankerson, A. Menezes, S. Vanstone, "Guide to Elliptic +// Curve Cryptography", Springer 2004, Chapter 4, in particular +// Algorithm 4.24 (keygen on page 180), and Algorithms 4.29 and 4.30. +// Complete descriptions of sign/verify are featured on page 184. // For another algorithm description of ECDSA, // see also: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf @@ -824,7 +824,7 @@ auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool const auto result_hash_is_ok = ( - hash_result == elliptic_curve_type::uint_type("0x334d016f755cd6dc58c53a86e183882f8ec14f52fb05345887c8a5edd42c87b7") + hash_result == elliptic_curve_type::uint_type("0x334D016F755CD6DC58C53A86E183882F8EC14F52FB05345887C8A5EDD42C87B7") ); result_is_ok = (result_hash_is_ok && result_is_ok); @@ -834,7 +834,7 @@ auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool // Test ECC key generation, sign and verify. In this case we use random (but pre-defined seeds // for both keygen as well as signing. - const auto seed_keygen = elliptic_curve_type::uint_type("0xc6455bf2f380f6b81f5fd1a1dbc2392b3783ed1e7d91b62942706e5584ba0b92"); + const auto seed_keygen = elliptic_curve_type::uint_type("0xC6455BF2F380F6B81F5FD1A1DBC2392B3783ED1E7D91B62942706E5584BA0B92"); const auto keypair = elliptic_curve_type::make_keypair(&seed_keygen); @@ -847,9 +847,9 @@ auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool } ); - const auto result_private_is_ok = (std::get<0>(keypair) == "0xc6455bf2f380f6b81f5fd1a1dbc2392b3783ed1e7d91b62942706e5584ba0b92"); - const auto result_public_x_is_ok = (std::get<1>(keypair).first == "0xc6235629f157690e1df37248256c4fb7eff073d0250f5bd85df40b9e127a8461"); - const auto result_public_y_is_ok = (std::get<1>(keypair).second == "0xcbaa679f07f9b98f915c1fb7d85a379d0559a9eee6735b1be0ce0e2e2b2e94de"); + const auto result_private_is_ok = (std::get<0>(keypair) == "0xC6455BF2F380F6B81F5FD1A1DBC2392B3783ED1E7D91B62942706E5584BA0B92"); + const auto result_public_x_is_ok = (std::get<1>(keypair).first == "0xC6235629F157690E1DF37248256C4FB7EFF073D0250F5BD85DF40B9E127A8461"); + const auto result_public_y_is_ok = (std::get<1>(keypair).second == "0xCBAA679F07F9B98F915C1FB7D85A379D0559A9EEE6735B1BE0CE0E2E2B2E94DE"); const auto result_keygen_is_ok = ( @@ -860,7 +860,7 @@ auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool result_is_ok = (result_is_on_curve_is_ok && result_keygen_is_ok && result_is_ok); - const auto priv = elliptic_curve_type::uint_type("0x6f73d8e95d6ddbf0eb352a9f0b2ce91931511edaf9ac8f128d5a4f877c4f0450"); + const auto priv = elliptic_curve_type::uint_type("0x6F73D8E95D6DDBF0EB352A9F0B2CE91931511EDAF9AC8F128D5A4F877C4F0450"); const auto sig = elliptic_curve_type::sign_message(std::get<0>(keypair), msg_as_string.cbegin(), msg_as_string.cend(), &priv); @@ -869,8 +869,8 @@ auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool ( sig == std::make_pair ( - elliptic_curve_type::uint_type("0x65717a860f315a21e6e23cde411c8940de42a69d8ab26c2465902be8f3b75e7b"), - elliptic_curve_type::uint_type("0xdb8b8e75a7b0c2f0d9eb8dbf1b5236edeb89b2116f5aebd40e770f8ccc3d6605") + elliptic_curve_type::uint_type("0x65717A860F315A21E6E23CDE411C8940DE42A69D8AB26C2465902BE8F3B75E7B"), + elliptic_curve_type::uint_type("0xDB8B8E75A7B0C2F0D9EB8DBF1B5236EDEB89B2116F5AEBD40E770F8CCC3D6605") ) ); diff --git a/math/wide_integer/uintwide_t.h b/math/wide_integer/uintwide_t.h index bbb5d05..3c92db2 100644 --- a/math/wide_integer/uintwide_t.h +++ b/math/wide_integer/uintwide_t.h @@ -151,10 +151,17 @@ namespace math { namespace wide_integer { namespace detail { // NOLINT(modernize-concat-nested-namespaces) #endif + // Use a local, constexpr, unsafe implementation of the abs-function. + template + WIDE_INTEGER_CONSTEXPR auto abs_unsafe(ArithmeticType val) -> ArithmeticType + { + return ((val > static_cast(INT8_C(0))) ? val : -val); + } + // Use a local, constexpr, unsafe implementation of the fill-function. template - inline WIDE_INTEGER_CONSTEXPR auto fill_unsafe(DestinationIterator first, DestinationIterator last, ValueType val) -> void + WIDE_INTEGER_CONSTEXPR auto fill_unsafe(DestinationIterator first, DestinationIterator last, ValueType val) -> void { while(first != last) { @@ -167,7 +174,7 @@ // Use a local, constexpr, unsafe implementation of the copy-function. template - inline WIDE_INTEGER_CONSTEXPR auto copy_unsafe(InputIterator first, InputIterator last, DestinationIterator dest) -> DestinationIterator + WIDE_INTEGER_CONSTEXPR auto copy_unsafe(InputIterator first, InputIterator last, DestinationIterator dest) -> DestinationIterator { while(first != last) { @@ -191,11 +198,11 @@ { using local_unsigned_integral_type = UnsignedIntegralType; - auto yy = static_cast(UINT8_C(0)); + auto yy_val = static_cast(UINT8_C(0)); - auto nn = static_cast(std::numeric_limits::digits); + auto nn_val = static_cast(std::numeric_limits::digits); - auto cc = // NOLINT(altera-id-dependent-backward-branch) + auto cc_val = // NOLINT(altera-id-dependent-backward-branch) static_cast ( std::numeric_limits::digits / static_cast(INT8_C(2)) @@ -203,23 +210,23 @@ do { - yy = static_cast(v >> cc); + yy_val = static_cast(v >> cc_val); - if(yy != static_cast(UINT8_C(0))) + if(yy_val != static_cast(UINT8_C(0))) { - nn -= cc; + nn_val -= cc_val; - v = yy; + v = yy_val; } - cc >>= static_cast(UINT8_C(1)); + cc_val >>= static_cast(UINT8_C(1)); } - while(cc != static_cast(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch) + while(cc_val != static_cast(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch) return static_cast ( - static_cast(nn) - static_cast(v) + static_cast(nn_val) - static_cast(v) ); } @@ -495,7 +502,7 @@ WIDE_INTEGER_CONSTEXPR auto swap(dynamic_array&& other) noexcept -> void { - auto tmp = std::move(*this); + const auto tmp = std::move(*this); *this = std::move(other); other = std::move(tmp); @@ -5738,11 +5745,7 @@ : static_cast(1U + static_cast((msb_pos + static_cast(UINT8_C(1))) / 2U))) ); - local_wide_integer_type - u - ( - local_wide_integer_type(static_cast(1U)) << left_shift_amount - ); + auto u = static_cast(static_cast(UINT8_C(1))) << left_shift_amount; // Perform the iteration for the square root. // See Algorithm 1.13 SqrtInt, Sect. 1.5.1 @@ -5799,7 +5802,7 @@ : static_cast(static_cast(UINT8_C(1)) + static_cast(static_cast(msb_pos + static_cast(static_cast(UINT8_C(3)) - msb_pos_mod_3)) / 3U)) ); - local_wide_integer_type u(local_wide_integer_type(static_cast(1U)) << left_shift_amount); + auto u = static_cast(static_cast(UINT8_C(1))) << left_shift_amount; // Perform the iteration for the k'th root. // See Algorithm 1.14 RootInt, Sect. 1.5.2 @@ -5880,7 +5883,7 @@ : static_cast(UINT8_C(1)) + static_cast(static_cast(msb_pos + static_cast(k - msb_pos_mod_k)) / k)) ); - local_wide_integer_type u(local_wide_integer_type(static_cast(1U)) << left_shift_amount); + auto u = static_cast(static_cast(UINT8_C(1))) << left_shift_amount; // Perform the iteration for the k'th root. // See Algorithm 1.14 RootInt, Sect. 1.5.2 @@ -5933,11 +5936,11 @@ { result = local_wide_integer_type(static_cast(UINT8_C(1))); } - else if((p0 == static_cast(UINT8_C(1))) && (p == static_cast(1))) + else if((p0 == static_cast(UINT8_C(1))) && (p == static_cast(UINT8_C(1)))) { result = b; } - else if((p0 == static_cast(UINT8_C(2))) && (p == static_cast(2))) + else if((p0 == static_cast(UINT8_C(2))) && (p == static_cast(UINT8_C(2)))) { result = b; result *= b; @@ -6072,12 +6075,12 @@ // This handles cases having (u = v) and also (u = v = 0). result = u; // LCOV_EXCL_LINE } - else if((static_cast(v) == static_cast(UINT8_C(0))) && (v == 0U)) + else if((static_cast(v) == static_cast(UINT8_C(0))) && (v == static_cast(UINT8_C(0)))) { // This handles cases having (v = 0) with (u != 0). result = u; // LCOV_EXCL_LINE } - else if((static_cast(u) == static_cast(UINT8_C(0))) && (u == 0U)) + else if((static_cast(u) == static_cast(UINT8_C(0))) && (u == static_cast(UINT8_C(0)))) { // This handles cases having (u = 0) with (v != 0). result = v; @@ -6126,7 +6129,7 @@ const auto my_v_hi = static_cast ( - (v.crepresentation().size() >= static_cast(2U)) + (v.crepresentation().size() >= static_cast(UINT8_C(2))) ? static_cast(*detail::advance_and_point(v.crepresentation().cbegin(), static_cast(UINT8_C(1)))) : static_cast(UINT8_C(0)) ); @@ -6134,7 +6137,7 @@ const auto my_u_hi = static_cast ( - (u.crepresentation().size() >= static_cast(2U)) + (u.crepresentation().size() >= static_cast(UINT8_C(2))) ? static_cast(*detail::advance_and_point(u.crepresentation().cbegin(), static_cast(UINT8_C(1)))) : static_cast(UINT8_C(0)) ); @@ -6172,10 +6175,8 @@ { using local_integer_type = IntegerType; - using std::abs; - - const local_integer_type ap = abs(a); - const local_integer_type bp = abs(b); + const local_integer_type ap = detail::abs_unsafe(a); + const local_integer_type bp = detail::abs_unsafe(b); const auto a_is_greater_than_b = (ap > bp); @@ -6270,7 +6271,7 @@ } else { - const auto division_is_exact = (ur == 0); + const auto division_is_exact = (ur == static_cast(UINT8_C(0))); if(!division_is_exact) { ++ua; } @@ -6468,17 +6469,15 @@ value = input_generator(); } - const auto next_byte = - static_cast + const auto right_shift_amount = + static_cast ( - value - >> static_cast - ( - static_cast(j % digits_gtor_ratio) - * static_cast(UINT8_C(8)) - ) + static_cast(j % digits_gtor_ratio) + * static_cast(UINT8_C(8)) ); + const auto next_byte = static_cast(value >> right_shift_amount); + *it = static_cast ( @@ -6497,7 +6496,7 @@ || (input_params.get_b() != (std::numeric_limits::max)())) { // Note that this restricts the range r to: - // r = {[input_generator() % ((b - a) + 1)] + a} + // r = { [input_generator() % ((b - a) + 1)] + a } result_type range(input_params.get_b() - input_params.get_a()); diff --git a/test/test_uintwide_t_spot_values.cpp b/test/test_uintwide_t_spot_values.cpp index 5c26fae..41fffca 100644 --- a/test/test_uintwide_t_spot_values.cpp +++ b/test/test_uintwide_t_spot_values.cpp @@ -1377,21 +1377,31 @@ auto local_test_spot_values::test() -> bool // NOLINT(readability-function-cogni // FromDigits["C9DD3EA24800F584CB28C25CC0E6FF1",16] // 16770224695321632575655872732632870897 - const uint256_t a("0xC9DD3EA24800F584CB28C25CC0E6FF1"); + WIDE_INTEGER_CONSTEXPR uint256_t a("0xC9DD3EA24800F584CB28C25CC0E6FF1"); // FromDigits["1E934A2EEA60A2AD14ECCAE7AD82C069",16] // 40641612127094559121321599356729737321 - const uint256_t b("0x1E934A2EEA60A2AD14ECCAE7AD82C069"); + WIDE_INTEGER_CONSTEXPR uint256_t b("0x1E934A2EEA60A2AD14ECCAE7AD82C069"); - const auto v = b - 1U; - const auto lm = lcm(a - 1U, v); - const auto gd = gcd(a - 1U, v); + WIDE_INTEGER_CONSTEXPR auto v = b - 1U; + WIDE_INTEGER_CONSTEXPR auto lm = lcm(a - 1U, v); + WIDE_INTEGER_CONSTEXPR auto gd = gcd(a - 1U, v); // LCM[16770224695321632575655872732632870897 - 1, 40641612127094559121321599356729737321 - 1] result_is_ok = ((lm == uint256_t("28398706972978513348490390087175345493497748446743697820448222113648043280")) && result_is_ok); + #if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1) + static_assert(lm == uint256_t("28398706972978513348490390087175345493497748446743697820448222113648043280"), + "Error: Rudimentary LCM calculation result is wrong"); + #endif + // GCD[16770224695321632575655872732632870897 - 1, 40641612127094559121321599356729737321 - 1] - result_is_ok = ((gd == 24U) && result_is_ok); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + result_is_ok = ((gd == static_cast(UINT8_C(24))) && result_is_ok); + + #if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1) + static_assert(gd == static_cast(UINT8_C(24)), + "Error: Rudimentary LCM calculation result is wrong"); + #endif { // Check GCD(0, v) to be equal to v (found mssing in code coverage analyses).