Remove default ctors from SecretKey and PublicKey: (#4607)

* It is now an invariant that all constructed Public Keys are valid,
  non-empty and contain 33 bytes of data.
* Additionally, the memory footprint of the PublicKey class is reduced.
  The size_ data member is declared as static.
* Distinguish and identify the PublisherList retrieved from the local
  config file, versus the ones obtained from other validators.
* Fixes #2942
This commit is contained in:
Chenna Keshava B S
2024-03-05 09:02:53 -08:00
committed by GitHub
parent 97863e0b62
commit 62dae3c6c6
39 changed files with 545 additions and 349 deletions

View File

@@ -61,13 +61,17 @@ namespace ripple {
class PublicKey
{
protected:
std::size_t size_ = 0;
std::uint8_t buf_[33]; // should be large enough
// All the constructed public keys are valid, non-empty and contain 33
// bytes of data.
static constexpr std::size_t size_ = 33;
std::uint8_t buf_[size_]; // should be large enough
public:
using const_iterator = std::uint8_t const*;
PublicKey() = default;
public:
PublicKey() = delete;
PublicKey(PublicKey const& other);
PublicKey&
operator=(PublicKey const& other);
@@ -115,12 +119,6 @@ public:
return buf_ + size_;
}
bool
empty() const noexcept
{
return size_ == 0;
}
Slice
slice() const noexcept
{
@@ -141,8 +139,7 @@ operator<<(std::ostream& os, PublicKey const& pk);
inline bool
operator==(PublicKey const& lhs, PublicKey const& rhs)
{
return lhs.size() == rhs.size() &&
std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
return std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
}
inline bool

View File

@@ -41,7 +41,7 @@ private:
public:
using const_iterator = std::uint8_t const*;
SecretKey() = default;
SecretKey() = delete;
SecretKey(SecretKey const&) = default;
SecretKey&
operator=(SecretKey const&) = default;

View File

@@ -24,7 +24,6 @@
#include <ripple/protocol/impl/secp256k1.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <ed25519.h>
#include <type_traits>
namespace ripple {
@@ -176,16 +175,19 @@ ed25519Canonical(Slice const& sig)
PublicKey::PublicKey(Slice const& slice)
{
if (slice.size() < size_)
LogicError(
"PublicKey::PublicKey - Input slice cannot be an undersized "
"buffer");
if (!publicKeyType(slice))
LogicError("PublicKey::PublicKey invalid type");
size_ = slice.size();
std::memcpy(buf_, slice.data(), size_);
}
PublicKey::PublicKey(PublicKey const& other) : size_(other.size_)
PublicKey::PublicKey(PublicKey const& other)
{
if (size_)
std::memcpy(buf_, other.buf_, size_);
std::memcpy(buf_, other.buf_, size_);
}
PublicKey&
@@ -193,9 +195,7 @@ PublicKey::operator=(PublicKey const& other)
{
if (this != &other)
{
size_ = other.size_;
if (size_)
std::memcpy(buf_, other.buf_, size_);
std::memcpy(buf_, other.buf_, size_);
}
return *this;