Skip to content

Commit

Permalink
Merge pull request #384 from ckormanyos/syntax_improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos authored Oct 11, 2023
2 parents cf900fd + 5b114ef commit 7548dbe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
26 changes: 13 additions & 13 deletions examples/example013_ecdsa_sign_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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 =
(
Expand All @@ -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);
Expand All @@ -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")
)
);

Expand Down
79 changes: 39 additions & 40 deletions math/wide_integer/uintwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename ArithmeticType>
WIDE_INTEGER_CONSTEXPR auto abs_unsafe(ArithmeticType val) -> ArithmeticType
{
return ((val > static_cast<int>(INT8_C(0))) ? val : -val);
}

// Use a local, constexpr, unsafe implementation of the fill-function.
template<typename DestinationIterator,
typename ValueType>
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)
{
Expand All @@ -167,7 +174,7 @@
// Use a local, constexpr, unsafe implementation of the copy-function.
template<typename InputIterator,
typename DestinationIterator>
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)
{
Expand All @@ -191,35 +198,35 @@
{
using local_unsigned_integral_type = UnsignedIntegralType;

auto yy = static_cast<local_unsigned_integral_type>(UINT8_C(0));
auto yy_val = static_cast<local_unsigned_integral_type>(UINT8_C(0));

auto nn = static_cast<unsigned>(std::numeric_limits<local_unsigned_integral_type>::digits);
auto nn_val = static_cast<unsigned>(std::numeric_limits<local_unsigned_integral_type>::digits);

auto cc = // NOLINT(altera-id-dependent-backward-branch)
auto cc_val = // NOLINT(altera-id-dependent-backward-branch)
static_cast<unsigned>
(
std::numeric_limits<local_unsigned_integral_type>::digits / static_cast<int>(INT8_C(2))
);

do
{
yy = static_cast<local_unsigned_integral_type>(v >> cc);
yy_val = static_cast<local_unsigned_integral_type>(v >> cc_val);

if(yy != static_cast<local_unsigned_integral_type>(UINT8_C(0)))
if(yy_val != static_cast<local_unsigned_integral_type>(UINT8_C(0)))
{
nn -= cc;
nn_val -= cc_val;

v = yy;
v = yy_val;
}

cc >>= static_cast<unsigned>(UINT8_C(1));
cc_val >>= static_cast<unsigned>(UINT8_C(1));
}
while(cc != static_cast<unsigned>(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch)
while(cc_val != static_cast<unsigned>(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch)

return
static_cast<unsigned>
(
static_cast<unsigned>(nn) - static_cast<unsigned>(v)
static_cast<unsigned>(nn_val) - static_cast<unsigned>(v)
);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -5738,11 +5745,7 @@
: static_cast<unsigned_fast_type>(1U + static_cast<unsigned_fast_type>((msb_pos + static_cast<unsigned_fast_type>(UINT8_C(1))) / 2U)))
);

local_wide_integer_type
u
(
local_wide_integer_type(static_cast<std::uint_fast8_t>(1U)) << left_shift_amount
);
auto u = static_cast<local_wide_integer_type>(static_cast<std::uint_fast8_t>(UINT8_C(1))) << left_shift_amount;

// Perform the iteration for the square root.
// See Algorithm 1.13 SqrtInt, Sect. 1.5.1
Expand Down Expand Up @@ -5799,7 +5802,7 @@
: static_cast<unsigned_fast_type>(static_cast<unsigned_fast_type>(UINT8_C(1)) + static_cast<unsigned_fast_type>(static_cast<unsigned_fast_type>(msb_pos + static_cast<unsigned_fast_type>(static_cast<unsigned_fast_type>(UINT8_C(3)) - msb_pos_mod_3)) / 3U))
);

local_wide_integer_type u(local_wide_integer_type(static_cast<std::uint_fast8_t>(1U)) << left_shift_amount);
auto u = static_cast<local_wide_integer_type>(static_cast<std::uint_fast8_t>(UINT8_C(1))) << left_shift_amount;

// Perform the iteration for the k'th root.
// See Algorithm 1.14 RootInt, Sect. 1.5.2
Expand Down Expand Up @@ -5880,7 +5883,7 @@
: static_cast<unsigned_fast_type>(UINT8_C(1)) + static_cast<unsigned_fast_type>(static_cast<unsigned_fast_type>(msb_pos + static_cast<unsigned_fast_type>(k - msb_pos_mod_k)) / k))
);

local_wide_integer_type u(local_wide_integer_type(static_cast<std::uint_fast8_t>(1U)) << left_shift_amount);
auto u = static_cast<local_wide_integer_type>(static_cast<std::uint_fast8_t>(UINT8_C(1))) << left_shift_amount;

// Perform the iteration for the k'th root.
// See Algorithm 1.14 RootInt, Sect. 1.5.2
Expand Down Expand Up @@ -5933,11 +5936,11 @@
{
result = local_wide_integer_type(static_cast<std::uint8_t>(UINT8_C(1)));
}
else if((p0 == static_cast<local_limb_type>(UINT8_C(1))) && (p == static_cast<OtherIntegralTypeP>(1)))
else if((p0 == static_cast<local_limb_type>(UINT8_C(1))) && (p == static_cast<OtherIntegralTypeP>(UINT8_C(1))))
{
result = b;
}
else if((p0 == static_cast<local_limb_type>(UINT8_C(2))) && (p == static_cast<OtherIntegralTypeP>(2)))
else if((p0 == static_cast<local_limb_type>(UINT8_C(2))) && (p == static_cast<OtherIntegralTypeP>(UINT8_C(2))))
{
result = b;
result *= b;
Expand Down Expand Up @@ -6072,12 +6075,12 @@
// This handles cases having (u = v) and also (u = v = 0).
result = u; // LCOV_EXCL_LINE
}
else if((static_cast<local_ushort_type>(v) == static_cast<local_ushort_type>(UINT8_C(0))) && (v == 0U))
else if((static_cast<local_ushort_type>(v) == static_cast<local_ushort_type>(UINT8_C(0))) && (v == static_cast<unsigned>(UINT8_C(0))))
{
// This handles cases having (v = 0) with (u != 0).
result = u; // LCOV_EXCL_LINE
}
else if((static_cast<local_ushort_type>(u) == static_cast<local_ushort_type>(UINT8_C(0))) && (u == 0U))
else if((static_cast<local_ushort_type>(u) == static_cast<local_ushort_type>(UINT8_C(0))) && (u == static_cast<unsigned>(UINT8_C(0))))
{
// This handles cases having (u = 0) with (v != 0).
result = v;
Expand Down Expand Up @@ -6126,15 +6129,15 @@
const auto my_v_hi =
static_cast<local_ushort_type>
(
(v.crepresentation().size() >= static_cast<typename local_wide_integer_type::representation_type::size_type>(2U))
(v.crepresentation().size() >= static_cast<typename local_wide_integer_type::representation_type::size_type>(UINT8_C(2)))
? static_cast<local_ushort_type>(*detail::advance_and_point(v.crepresentation().cbegin(), static_cast<local_size_type>(UINT8_C(1))))
: static_cast<local_ushort_type>(UINT8_C(0))
);

const auto my_u_hi =
static_cast<local_ushort_type>
(
(u.crepresentation().size() >= static_cast<typename local_wide_integer_type::representation_type::size_type>(2U))
(u.crepresentation().size() >= static_cast<typename local_wide_integer_type::representation_type::size_type>(UINT8_C(2)))
? static_cast<local_ushort_type>(*detail::advance_and_point(u.crepresentation().cbegin(), static_cast<local_size_type>(UINT8_C(1))))
: static_cast<local_ushort_type>(UINT8_C(0))
);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -6270,7 +6271,7 @@
}
else
{
const auto division_is_exact = (ur == 0);
const auto division_is_exact = (ur == static_cast<unsigned>(UINT8_C(0)));

if(!division_is_exact) { ++ua; }

Expand Down Expand Up @@ -6468,17 +6469,15 @@
value = input_generator();
}

const auto next_byte =
static_cast<std::uint8_t>
const auto right_shift_amount =
static_cast<unsigned>
(
value
>> static_cast<unsigned>
(
static_cast<unsigned_fast_type>(j % digits_gtor_ratio)
* static_cast<unsigned_fast_type>(UINT8_C(8))
)
static_cast<unsigned_fast_type>(j % digits_gtor_ratio)
* static_cast<unsigned_fast_type>(UINT8_C(8))
);

const auto next_byte = static_cast<std::uint8_t>(value >> right_shift_amount);

*it =
static_cast<typename result_type::limb_type>
(
Expand All @@ -6497,7 +6496,7 @@
|| (input_params.get_b() != (std::numeric_limits<result_type>::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());

Expand Down
22 changes: 16 additions & 6 deletions test/test_uintwide_t_spot_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>(UINT8_C(24))) && result_is_ok);

#if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1)
static_assert(gd == static_cast<unsigned>(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).
Expand Down

0 comments on commit 7548dbe

Please sign in to comment.