LibCrypto: Allow moving SignedBigInteger / UnsignedBigInteger

We defined copy operations but not move operations, so every existing
move() resulted in a copy.
This commit is contained in:
Timothy Flynn 2025-07-21 07:31:33 -04:00 committed by Jelle Raaijmakers
commit 8600c5149b
Notes: github-actions[bot] 2025-07-21 13:23:41 +00:00
5 changed files with 69 additions and 0 deletions

View file

@ -52,6 +52,14 @@ SignedBigInteger::SignedBigInteger(SignedBigInteger const& other)
MP_MUST(mp_init_copy(&m_mp, &other.m_mp)); MP_MUST(mp_init_copy(&m_mp, &other.m_mp));
} }
SignedBigInteger::SignedBigInteger(SignedBigInteger&& other)
: m_mp(other.m_mp)
, m_hash(other.m_hash)
{
other.m_mp = {};
other.m_hash.clear();
}
SignedBigInteger& SignedBigInteger::operator=(SignedBigInteger const& other) SignedBigInteger& SignedBigInteger::operator=(SignedBigInteger const& other)
{ {
if (this == &other) if (this == &other)
@ -64,6 +72,21 @@ SignedBigInteger& SignedBigInteger::operator=(SignedBigInteger const& other)
return *this; return *this;
} }
SignedBigInteger& SignedBigInteger::operator=(SignedBigInteger&& other)
{
if (this == &other)
return *this;
mp_clear(&m_mp);
m_mp = other.m_mp;
m_hash = other.m_hash;
other.m_mp = {};
other.m_hash.clear();
return *this;
}
SignedBigInteger::SignedBigInteger() SignedBigInteger::SignedBigInteger()
{ {
MP_MUST(mp_init(&m_mp)); MP_MUST(mp_init(&m_mp));

View file

@ -29,7 +29,10 @@ public:
explicit SignedBigInteger(i64 value); explicit SignedBigInteger(i64 value);
SignedBigInteger(SignedBigInteger const&); SignedBigInteger(SignedBigInteger const&);
SignedBigInteger(SignedBigInteger&&);
SignedBigInteger& operator=(SignedBigInteger const&); SignedBigInteger& operator=(SignedBigInteger const&);
SignedBigInteger& operator=(SignedBigInteger&&);
SignedBigInteger(); SignedBigInteger();
~SignedBigInteger(); ~SignedBigInteger();

View file

@ -55,6 +55,14 @@ UnsignedBigInteger::UnsignedBigInteger(UnsignedBigInteger const& other)
MP_MUST(mp_init_copy(&m_mp, &other.m_mp)); MP_MUST(mp_init_copy(&m_mp, &other.m_mp));
} }
UnsignedBigInteger::UnsignedBigInteger(UnsignedBigInteger&& other)
: m_mp(other.m_mp)
, m_hash(other.m_hash)
{
other.m_mp = {};
other.m_hash.clear();
}
UnsignedBigInteger& UnsignedBigInteger::operator=(UnsignedBigInteger const& other) UnsignedBigInteger& UnsignedBigInteger::operator=(UnsignedBigInteger const& other)
{ {
if (this == &other) if (this == &other)
@ -67,6 +75,21 @@ UnsignedBigInteger& UnsignedBigInteger::operator=(UnsignedBigInteger const& othe
return *this; return *this;
} }
UnsignedBigInteger& UnsignedBigInteger::operator=(UnsignedBigInteger&& other)
{
if (this == &other)
return *this;
mp_clear(&m_mp);
m_mp = other.m_mp;
m_hash = other.m_hash;
other.m_mp = {};
other.m_hash.clear();
return *this;
}
UnsignedBigInteger::UnsignedBigInteger() UnsignedBigInteger::UnsignedBigInteger()
{ {
MP_MUST(mp_init(&m_mp)); MP_MUST(mp_init(&m_mp));

View file

@ -31,7 +31,10 @@ public:
explicit UnsignedBigInteger(u64 value); explicit UnsignedBigInteger(u64 value);
UnsignedBigInteger(UnsignedBigInteger const&); UnsignedBigInteger(UnsignedBigInteger const&);
UnsignedBigInteger(UnsignedBigInteger&&);
UnsignedBigInteger& operator=(UnsignedBigInteger const&); UnsignedBigInteger& operator=(UnsignedBigInteger const&);
UnsignedBigInteger& operator=(UnsignedBigInteger&&);
UnsignedBigInteger(); UnsignedBigInteger();
~UnsignedBigInteger(); ~UnsignedBigInteger();

View file

@ -901,6 +901,23 @@ TEST_CASE(unsigned_bigint_double_comparisons)
#undef EXPECT_EQUAL_TO #undef EXPECT_EQUAL_TO
} }
TEST_CASE(moving_bigints)
{
Crypto::UnsignedBigInteger bigint1;
{
auto bigint2 = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"_bigint;
bigint1 = move(bigint2);
EXPECT(bigint2.is_zero());
}
EXPECT_EQ(MUST(bigint1.to_base(10)), "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"sv);
Crypto::UnsignedBigInteger bigint3 { move(bigint1) };
EXPECT(bigint1.is_zero());
EXPECT_EQ(MUST(bigint3.to_base(10)), "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"sv);
}
namespace AK { namespace AK {
template<> template<>