From ecf25df5894a530d6c1cc6edc6f934c1a293145a Mon Sep 17 00:00:00 2001 From: Crt Vavros - smlu Date: Tue, 21 Nov 2023 17:46:52 +0100 Subject: [PATCH] Add integer to NAF conversion Added new `bigint` methods `to_naf` & `to_rnaf` which converts integer to non-adjacent form (NAF). --- include/ack/bigint.hpp | 38 ++++- tests/include/ack/tests/bigint_test.hpp | 175 +++++++++++++++++++++++- 2 files changed, 211 insertions(+), 2 deletions(-) diff --git a/include/ack/bigint.hpp b/include/ack/bigint.hpp index eb82d42..2ebb146 100644 --- a/include/ack/bigint.hpp +++ b/include/ack/bigint.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include namespace ack { @@ -1448,6 +1449,41 @@ namespace ack { return data; } + /** + * Converts this integer to non-adjacent form (NAF). + * @return NAF representation of this integer. + */ + [[nodiscard]] inline std::vector to_naf() const + { + std::vector nafv; + nafv.reserve( bit_length() + 1 ); + auto num = *this; + while ( num > 0U ) { + if ( num.is_odd() ) { + int32_t nd; + (num % 4).get_int32( nd ); + nd = 2 - nd; + nafv.push_back( nd ); + num -= nd; + } else { + nafv.push_back( 0 ); + } + num >>= 1; + } + return nafv; + } + + /** + * Converts this integer to reversed non-adjacent form (NAF). + * @return reversed NAF representation of this integer. + */ + [[nodiscard]] inline std::vector to_rnaf() const + { + auto naf = to_naf(); + std::reverse(naf.begin(), naf.end()); + return naf; + } + constexpr void clear() { *this = 0; @@ -1623,7 +1659,7 @@ namespace ack { * @param n - Reference of type int32_t to receive extracted integer. * @return true if integer could be extracted otherwise false. */ - constexpr const bool get_int32(int32_t& n) const + constexpr bool get_int32(int32_t& n) const { static_assert( sizeof(word_t) == sizeof(uint32_t) ); if (word_length() > 1) { diff --git a/tests/include/ack/tests/bigint_test.hpp b/tests/include/ack/tests/bigint_test.hpp index fc6463d..6b7befe 100644 --- a/tests/include/ack/tests/bigint_test.hpp +++ b/tests/include/ack/tests/bigint_test.hpp @@ -5272,7 +5272,180 @@ namespace ack::tests { n = -n; REQUIRE_EQUAL( n.get_int32( n32 ), false ) } - EOSIO_TEST_END + + // Test converting number to NAF + { + using bn_t = fixed_bigint<264>; + using vec = std::vector; + + REQUIRE_EQUAL( bn_t().to_naf(), (vec{}) ) + REQUIRE_EQUAL( bn_t( 0 ).to_naf(), (vec{}) ) + REQUIRE_EQUAL( bn_t().to_rnaf(), (vec{}) ) + REQUIRE_EQUAL( bn_t( 0 ).to_rnaf(), (vec{}) ) + + REQUIRE_EQUAL( bn_t( 1 ).to_naf(), (vec{ 1 }) ) + REQUIRE_EQUAL( bn_t( 1 ).to_rnaf(), (vec{ 1 }) ) + + REQUIRE_EQUAL( bn_t( 2 ).to_naf(), (vec{ 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 2 ).to_rnaf(), (vec{ 1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 3 ).to_naf(), (vec{ -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 3 ).to_rnaf(), (vec{ 1, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 4 ).to_naf(), (vec{ 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 4 ).to_rnaf(), (vec{ 1, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 5 ).to_naf(), (vec{ 1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 5 ).to_rnaf(), (vec{ 1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 6 ).to_naf(), (vec{ 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 6 ).to_rnaf(), (vec{ 1, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 7 ).to_naf(), (vec{ -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 7 ).to_rnaf(), (vec{ 1, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 8 ).to_naf(), (vec{ 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 8 ).to_rnaf(), (vec{ 1, 0, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 9 ).to_naf(), (vec{ 1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 9 ).to_rnaf(), (vec{ 1, 0, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 10 ).to_naf(), (vec{ 0, 1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 10 ).to_rnaf(), (vec{ 1, 0, 1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 11 ).to_naf(), (vec{ -1, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 11 ).to_rnaf(), (vec{ 1, 0, -1, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 12 ).to_naf(), (vec{ 0, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 12 ).to_rnaf(), (vec{ 1, 0, -1, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 13 ).to_naf(), (vec{ 1, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 13 ).to_rnaf(), (vec{ 1, 0, -1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 14 ).to_naf(), (vec{ 0, -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 14 ).to_rnaf(), (vec{ 1, 0, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 15 ).to_naf(), (vec{ -1, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 15 ).to_rnaf(), (vec{ 1, 0, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 16 ).to_naf(), (vec{ 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 16 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 21 ).to_naf(), (vec{ 1, 0, 1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 21 ).to_rnaf(), (vec{ 1, 0, 1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 22 ).to_naf(), (vec{ 0, -1, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 22 ).to_rnaf(), (vec{ 1, 0, -1, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 23 ).to_naf(), (vec{ -1, 0, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 23 ).to_rnaf(), (vec{ 1, 0, -1, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 24 ).to_naf(), (vec{ 0, 0, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 24 ).to_rnaf(), (vec{ 1, 0, -1, 0, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 25 ).to_naf(), (vec{ 1, 0, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 25 ).to_rnaf(), (vec{ 1, 0, -1, 0, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 26 ).to_naf(), (vec{ 0, 1, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 26 ).to_rnaf(), (vec{ 1, 0, -1, 0, 1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 27 ).to_naf(), (vec{ -1, 0, -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 27 ).to_rnaf(), (vec{ 1, 0, 0, -1, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 28 ).to_naf(), (vec{ 0, 0, -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 28 ).to_rnaf(), (vec{ 1, 0, 0, -1, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 29 ).to_naf(), (vec{ 1, 0, -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 29 ).to_rnaf(), (vec{ 1, 0, 0, -1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 30 ).to_naf(), (vec{ 0, -1, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 30 ).to_rnaf(), (vec{ 1, 0, 0, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 31 ).to_naf(), (vec{ -1, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 31 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 32 ).to_naf(), (vec{ 0, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 32 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 33 ).to_naf(), (vec{ 1, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 33 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 34 ).to_naf(), (vec{ 0, 1, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 34 ).to_rnaf(), (vec{ 1, 0, 0, 0, 1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 61 ).to_naf(), (vec{ 1, 0, -1, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 61 ).to_rnaf(), (vec{ 1, 0, 0, 0, -1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( 62 ).to_naf(), (vec{ 0, -1, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 62 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 63 ).to_naf(), (vec{ -1, 0, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 63 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( 64 ).to_naf(), (vec{ 0, 0, 0, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 64 ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, 0, 0 }) ) + + REQUIRE_EQUAL( bn_t( 137 ).to_naf(), (vec{ 1, 0, 0, 1, 0, 0, 0, 1 }) ) + REQUIRE_EQUAL( b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-1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 0x6060606060606060ULL ).to_rnaf(), (vec{ 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0}) ) + + REQUIRE_EQUAL( bn_t( 0x6666666666666666ULL ).to_naf(), (vec{ 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( 0x6666666666666666ULL ).to_rnaf(), (vec{ 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0 }) ) + + REQUIRE_EQUAL( bn_t( 0x9999999999999999ULL ).to_naf(), (vec{ 1, 0, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0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FFFFULL ).to_rnaf(), (vec{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( "0000000000000000000000000000000000000000000000000000000000000007" ).to_naf(), (vec{ -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( "0000000000000000000000000000000000000000000000000000000000000007" ).to_rnaf(), (vec{ 1, 0, 0, -1 }) ) + + REQUIRE_EQUAL( bn_t( "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5" ).to_naf(), (vec{ 1, 0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 1, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, 1, 0, -1, 0, -1, 0, 0, 0, 1, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 1, 0, 0, -1, 0, 1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, 0, 1, 0, -1, 0, 1, 0, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, -1, 0, -1, 0, 1, 0, 0, -1, 0, -1, 0, 1, 0, 0, 0, -1, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, -1, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1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 0, -1, 0, 1, 0, 1 }) ) + + REQUIRE_EQUAL( bn_t( "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296" ).to_naf(), (vec{ 0, -1, 0, -1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 0, 1, 0, 1, 0, 0, 0, -1, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, 1, 0, 1, 0, 0, -1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 1, 0, -1, 0, 1, 0, -1, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, 0, -1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, -1, 0, 0, 1, 0, 0, -1, 0, -1, 0, -1, 0, 1, 0, -1, 0, 0, -1, 0, 0, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, -1, 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 0, -1, 0, 1, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 0, -1, 0, -1, 0, 0, 1 }) ) + REQUIRE_EQUAL( bn_t( "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296" ).to_rnaf(), (vec{ 1, 0, 0, -1, 0, -1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 1, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, -1, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 1, 0, 1, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 }) ) + + // Test negative integers result in empty array + for ( int i = -1; i > -128; i-- ) { + REQUIRE_EQUAL( bn_t( i ).to_naf(), vec{} ) + REQUIRE_EQUAL( bn_t( i ).to_rnaf(), vec{} ) + } + } + EOSIO_TEST_END // bigint_miscellaneous_test EOSIO_TEST_BEGIN( bigint_arithmetic_test ) using namespace detail;