From fc1e876cedc4537e133f6f21242c3400a50a444d Mon Sep 17 00:00:00 2001 From: sowle Date: Mon, 12 Apr 2021 13:09:53 +0300 Subject: [PATCH] experimental crypto: pod to str conversions for 256 bit pods, small fixes --- tests/functional_tests/crypto_tests.cpp | 52 ++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/tests/functional_tests/crypto_tests.cpp b/tests/functional_tests/crypto_tests.cpp index 67bc904b..3c8a8046 100644 --- a/tests/functional_tests/crypto_tests.cpp +++ b/tests/functional_tests/crypto_tests.cpp @@ -155,6 +155,40 @@ std::string pod_to_hex_big_endian(const pod_t &h) return s; } +template +std::string pod_to_hex_comma_separated_bytes(const pod_t &h) +{ + std::stringstream ss; + ss << std::hex << std::setfill('0'); + size_t len = sizeof h; + const unsigned char* p = (const unsigned char*)&h; + for (size_t i = 0; i < len; ++i) + { + ss << "0x" << std::setw(2) << static_cast(p[i]); + if (i + 1 != len) + ss << ", "; + } + return ss.str(); +} + +template +std::string pod_to_hex_comma_separated_uint64(const pod_t &h) +{ + static_assert((sizeof h) % 8 == 0, "size of h should be a multiple of 64 bit"); + size_t len = (sizeof h) / 8; + std::stringstream ss; + ss << std::hex << std::setfill('0'); + const uint64_t* p = (const uint64_t*)&h; + for (size_t i = 0; i < len; ++i) + { + ss << "0x" << std::setw(16) << static_cast(p[i]); + if (i + 1 != len) + ss << ", "; + } + return ss.str(); +} + + uint64_t rand_in_range(uint64_t from_including, uint64_t to_not_including) { uint64_t result = 0; @@ -570,6 +604,12 @@ struct point_t return result; } + point_t& modify_mul8() + { + ge_mul8_p3(&m_p3, &m_p3); + return *this; + } + // returns a * this + G point_t mul_plus_G(const scalar_t& a) const { @@ -591,7 +631,7 @@ struct point_t friend bool operator==(const point_t& lhs, const point_t& rhs) { - // convert to xy form, then compare components (because (z, y, z, t) representation is not unique) + // convert to xy form, then compare components (because (x, y, z, t) representation is not unique) fe lrecip, lx, ly; fe rrecip, rx, ry; @@ -982,10 +1022,6 @@ uint64_t hash_64(const void* data, size_t size) } -//////////////////////////////////////////////////////////////////////////////// -#include "L2S.h" -//////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////// @@ -1003,7 +1039,7 @@ uint64_t hash_64(const void* data, size_t size) static bool test_name_a ## _ ## test_name_b() #define ASSERT_TRUE(expr) CHECK_AND_ASSERT_MES(expr, false, "This is not true: " #expr) #define ASSERT_FALSE(expr) CHECK_AND_ASSERT_MES((expr) == false, false, "This is not false: " #expr) -#define ASSERT_EQ(a, b) CHECK_AND_ASSERT_MES(a == b, false, #a " != " #b) +#define ASSERT_EQ(a, b) CHECK_AND_ASSERT_MES(a == b, false, #a " != " #b "\n " << a << " != " << b) typedef bool(*bool_func_ptr_t)(); static std::vector> g_tests; @@ -1016,6 +1052,10 @@ struct test_keeper_t }; +//////////////////////////////////////////////////////////////////////////////// +#include "L2S.h" +//////////////////////////////////////////////////////////////////////////////// + //