From 1f8253e1b5e427ac0ee6ce41bb4b57f7a97d04cb Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 2 Mar 2021 00:35:58 +0300 Subject: [PATCH] experimental crypto: scalar_t conversion to/from boost::mp::cpp_int, hash helpers added --- tests/functional_tests/crypto_tests.cpp | 39 +++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/functional_tests/crypto_tests.cpp b/tests/functional_tests/crypto_tests.cpp index 7539a74b..2f1f3f4e 100644 --- a/tests/functional_tests/crypto_tests.cpp +++ b/tests/functional_tests/crypto_tests.cpp @@ -221,6 +221,17 @@ struct scalar_t // do not need to call reduce as 2^64 < L } + // copy at most 32 bytes and reduce + scalar_t(const boost::multiprecision::cpp_int &bigint) + { + zero(); + unsigned int bytes_to_copy = bigint.backend().size() * bigint.backend().limb_bits / 8; + if (bytes_to_copy > sizeof *this) + bytes_to_copy = sizeof *this; + memcpy(&m_s[0], bigint.backend().limbs(), bytes_to_copy); + sc_reduce32(&m_s[0]); + } + unsigned char* data() { return &m_s[0]; @@ -409,6 +420,18 @@ struct scalar_t return epee::string_tools::pod_to_hex(*this); } + template + MP_type as_boost_mp_type() const + { + MP_type result = 0; + static_assert(sizeof result >= sizeof *this, "size missmatch"); // to avoid using types less than uint256_t + unsigned int sz = sizeof *this / sizeof(boost::multiprecision::limb_type); + result.backend().resize(sz, sz); + memcpy(result.backend().limbs(), &m_s[0], sizeof *this); + result.backend().normalize(); + return result; + } + }; // struct scalar_t @@ -613,9 +636,12 @@ struct hash_helper_t { static scalar_t hs(const scalar_t& s) { - crypto::hash hash; - crypto::cn_fast_hash(s.data(), sizeof s, hash); - return scalar_t(hash); // will reduce mod L + return scalar_t(crypto::cn_fast_hash(s.data(), sizeof s)); // will reduce mod L + } + + static scalar_t hs(const void* data, size_t size) + { + return scalar_t(crypto::cn_fast_hash(data, size)); // will reduce mod L } struct hs_t @@ -893,6 +919,13 @@ bool generate_test_ring_and_sec_keys(size_t N, size_t L, std::vector