From b09f910bf2c289fea5c05a175bd591994cc7968a Mon Sep 17 00:00:00 2001 From: EmelyanenkoK Date: Mon, 4 Mar 2024 17:37:42 +0300 Subject: [PATCH] Fix converting int to int256 (#925) Co-authored-by: SpyCheese --- crypto/common/bigint.hpp | 27 +++++++++++++++++++++------ crypto/common/refint.cpp | 4 ---- crypto/common/refint.h | 2 -- crypto/vm/cells/CellSlice.cpp | 4 ++-- test/regression-tests.ans | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/crypto/common/bigint.hpp b/crypto/common/bigint.hpp index 5fa8e2da..f8756d9f 100644 --- a/crypto/common/bigint.hpp +++ b/crypto/common/bigint.hpp @@ -169,8 +169,6 @@ class PropagateConstSpan { size_t size_{0}; }; -struct Normalize {}; - template class AnyIntView { public: @@ -290,6 +288,7 @@ class BigIntG { public: enum { word_bits = Tr::word_bits, word_shift = Tr::word_shift, max_bits = len, word_cnt = len / word_shift + 1 }; typedef typename Tr::word_t word_t; + typedef typename Tr::uword_t uword_t; typedef Tr Traits; typedef BigIntG DoubleInt; @@ -312,9 +311,6 @@ class BigIntG { BigIntG() : n(0) { } explicit BigIntG(word_t x) : n(1) { - digits[0] = x; - } - BigIntG(Normalize, word_t x) : n(1) { if (x >= -Tr::Half && x < Tr::Half) { digits[0] = x; } else if (len <= 1) { @@ -325,6 +321,25 @@ class BigIntG { digits[n++] = (x >> Tr::word_shift) + (digits[0] < 0); } } + explicit BigIntG(uword_t x) : n(1) { + if (x < (uword_t)Tr::Half) { + digits[0] = x; + } else if (len <= 1) { + digits[0] = x; + normalize_bool(); + } else { + digits[0] = ((x ^ Tr::Half) & (Tr::Base - 1)) - Tr::Half; + digits[n++] = (x >> Tr::word_shift) + (digits[0] < 0); + } + } + explicit BigIntG(unsigned x) : BigIntG(uword_t(x)) { + } + explicit BigIntG(int x) : BigIntG(word_t(x)) { + } + explicit BigIntG(unsigned long x) : BigIntG(uword_t(x)) { + } + explicit BigIntG(long x) : BigIntG(word_t(x)) { + } BigIntG(const BigIntG& x) : n(x.n) { std::memcpy(digits, x.digits, n * sizeof(word_t)); ///std::cout << "(BiCC " << (const void*)&x << "->" << (void*)this << ")"; @@ -2556,7 +2571,7 @@ typedef BigIntG<257, BigIntInfo> BigInt256; template BigIntG make_bigint(long long x) { - return BigIntG{Normalize(), x}; + return BigIntG{x}; } namespace literals { diff --git a/crypto/common/refint.cpp b/crypto/common/refint.cpp index 8e06da5f..a2ccdaa6 100644 --- a/crypto/common/refint.cpp +++ b/crypto/common/refint.cpp @@ -261,10 +261,6 @@ int sgn(RefInt256 x) { return x->sgn(); } -RefInt256 make_refint(long long x) { - return td::RefInt256{true, td::Normalize(), x}; -} - RefInt256 zero_refint() { // static RefInt256 Zero = td::RefInt256{true, 0}; // return Zero; diff --git a/crypto/common/refint.h b/crypto/common/refint.h index 488ae97f..e3305c2f 100644 --- a/crypto/common/refint.h +++ b/crypto/common/refint.h @@ -113,8 +113,6 @@ RefInt256 make_refint(Args&&... args) { return td::RefInt256{true, std::forward(args)...}; } -extern RefInt256 make_refint(long long x); - extern RefInt256 zero_refint(); extern RefInt256 bits_to_refint(td::ConstBitPtr bits, int n, bool sgnd = false); diff --git a/crypto/vm/cells/CellSlice.cpp b/crypto/vm/cells/CellSlice.cpp index ee5f6941..4be1667e 100644 --- a/crypto/vm/cells/CellSlice.cpp +++ b/crypto/vm/cells/CellSlice.cpp @@ -595,7 +595,7 @@ td::RefInt256 CellSlice::fetch_int256(unsigned bits, bool sgnd) { if (!have(bits)) { return {}; } else if (bits < td::BigInt256::word_shift) { - return td::make_refint(sgnd ? fetch_long(bits) : fetch_ulong(bits)); + return td::make_refint(td::int64(sgnd ? fetch_long(bits) : fetch_ulong(bits))); } else { td::RefInt256 res{true}; res.unique_write().import_bits(data_bits(), bits, sgnd); @@ -608,7 +608,7 @@ td::RefInt256 CellSlice::prefetch_int256(unsigned bits, bool sgnd) const { if (!have(bits)) { return {}; } else if (bits < td::BigInt256::word_shift) { - return td::make_refint(sgnd ? prefetch_long(bits) : prefetch_ulong(bits)); + return td::make_refint(td::int64(sgnd ? prefetch_long(bits) : prefetch_ulong(bits))); } else { td::RefInt256 res{true}; res.unique_write().import_bits(data_bits(), bits, sgnd); diff --git a/test/regression-tests.ans b/test/regression-tests.ans index ec013bf7..96117ee9 100644 --- a/test/regression-tests.ans +++ b/test/regression-tests.ans @@ -1,5 +1,5 @@ abce -Test_Bigint_main_default 0327a04f1252c37f77b6706b902ab2c3235c47738bca3f183c837a2c5d22bb6f +Test_Bigint_main_default 76f38492ec19464a1d0eac51d389023a31ce10396b3894061361d159567ce8cd Test_Bitstrings_main_default a8b08af3116923c4c2a14e138d168375abd0c059f2f780d3267b294929a1110e Test_Cells_simple_default 832502642fe4fe5db70de82681aedb7d54d7f3530e0069861fff405fe6f6cf23 Test_Fift_bug_div_default 1ac42861ce96b2896001c587f65e9afe1617db48859f19c2f4e3063a20ea60b0