Access base_uint through public members (RIPD-898):

* Updates many (but probably not all) locations that access base_uint
  private storage.
* More calls to access base_uint through members.
* Use an iterator to write Serializer collections.
This commit is contained in:
Edward Hennis
2016-04-29 17:02:48 -04:00
committed by seelabs
parent e38f01d1f4
commit 7e3dbce3d2
8 changed files with 44 additions and 23 deletions

View File

@@ -54,10 +54,10 @@ protected:
enum { WIDTH = Bits / 32 }; enum { WIDTH = Bits / 32 };
// This is really big-endian in byte order. // This is really big-endian in byte order.
// We sometimes use unsigned int for speed. // We sometimes use std::uint32_t for speed.
// NIKB TODO: migrate to std::array // NIKB TODO: migrate to std::array
unsigned int pn[WIDTH]; std::uint32_t pn[WIDTH];
public: public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -268,7 +268,7 @@ public:
{ {
for (int i = WIDTH - 1; i >= 0; --i) for (int i = WIDTH - 1; i >= 0; --i)
{ {
std::uint32_t prev = pn[i]; auto prev = pn[i];
pn[i] = hostToBigend (bigendToHost (pn[i]) - 1); pn[i] = hostToBigend (bigendToHost (pn[i]) - 1);
if (prev != 0) if (prev != 0)
@@ -320,6 +320,7 @@ public:
{ {
unsigned char* pOut = begin (); unsigned char* pOut = begin ();
assert(sizeof(pn) == bytes);
for (int i = 0; i < sizeof (pn); ++i) for (int i = 0; i < sizeof (pn); ++i)
{ {
auto hi = charUnHex(*psz++); auto hi = charUnHex(*psz++);
@@ -406,9 +407,9 @@ public:
return SetHexExact (str.c_str ()); return SetHexExact (str.c_str ());
} }
unsigned int size () const constexpr static std::size_t size ()
{ {
return sizeof (pn); return bytes;
} }
base_uint<Bits, Tag>& operator=(Zero) base_uint<Bits, Tag>& operator=(Zero)

View File

@@ -95,7 +95,7 @@ static bignum generateRootDeterministicKey (uint128 const& seed)
copy_uint32 (buf.begin() + 16, seq++); copy_uint32 (buf.begin() + 16, seq++);
auto root = sha512Half(buf); auto root = sha512Half(buf);
std::fill (buf.begin(), buf.end(), 0); // security erase std::fill (buf.begin(), buf.end(), 0); // security erase
privKey.assign ((unsigned char const*) &root, sizeof (root)); privKey.assign (root.data(), root.size());
root.zero(); // security erase root.zero(); // security erase
} }
while (privKey.is_zero() || privKey >= secp256k1curve.order); while (privKey.is_zero() || privKey >= secp256k1curve.order);
@@ -154,7 +154,7 @@ static bignum makeHash (Blob const& pubGen, int seq, bignum const& order)
copy_uint32 (buf.begin() + 37, subSeq++); copy_uint32 (buf.begin() + 37, subSeq++);
auto root = sha512Half_s(buf); auto root = sha512Half_s(buf);
std::fill(buf.begin(), buf.end(), 0); // security erase std::fill(buf.begin(), buf.end(), 0); // security erase
result.assign ((unsigned char const*) &root, sizeof (root)); result.assign (root.data(), root.size());
} }
while (result.is_zero() || result >= order); while (result.is_zero() || result >= order);

View File

@@ -107,6 +107,8 @@ public:
int addVL (Blob const& vector); int addVL (Blob const& vector);
int addVL (Slice const& slice); int addVL (Slice const& slice);
template<class Iter>
int addVL (Iter begin, Iter end, int len);
int addVL (const void* ptr, int len); int addVL (const void* ptr, int len);
// disassemble functions // disassemble functions
@@ -280,6 +282,21 @@ private:
int addEncoded (int length); int addEncoded (int length);
}; };
template<class Iter>
int Serializer::addVL(Iter begin, Iter end, int len)
{
int ret = addEncoded(len);
for (; begin != end; ++begin)
{
addRaw(begin->data(), begin->size());
#ifndef NDEBUG
len -= begin->size();
#endif
}
assert(len == 0);
return ret;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DEPRECATED // DEPRECATED

View File

@@ -147,7 +147,7 @@ calcAccountID (PublicKey const& pk)
auto const d = static_cast< auto const d = static_cast<
ripesha_hasher::result_type>(rsh); ripesha_hasher::result_type>(rsh);
AccountID id; AccountID id;
static_assert(sizeof(d) == sizeof(id), ""); static_assert(sizeof(d) == id.size(), "");
std::memcpy(id.data(), d.data(), d.size()); std::memcpy(id.data(), d.data(), d.size());
return id; return id;
} }

View File

@@ -48,7 +48,7 @@ STVector256::add (Serializer& s) const
{ {
assert (fName->isBinary ()); assert (fName->isBinary ());
assert (fName->fieldType == STI_VECTOR256); assert (fName->fieldType == STI_VECTOR256);
s.addVL (mValue.empty () ? nullptr : mValue[0].begin (), mValue.size () * (256 / 8)); s.addVL (mValue.begin(), mValue.end(), mValue.size () * (256 / 8));
} }
bool bool

View File

@@ -203,7 +203,7 @@ public:
auto const& uints1 = object1.getFieldV256(sfTestV256); auto const& uints1 = object1.getFieldV256(sfTestV256);
auto const& uints3 = object3.getFieldV256(sfTestV256); auto const& uints3 = object3.getFieldV256(sfTestV256);
expect(uints1 == uints3); expect(uints1 == uints3, "STObject vector mismatch");
} }
} }

View File

@@ -147,7 +147,7 @@ class SHAMapInnerNodeV2;
class SHAMapInnerNode class SHAMapInnerNode
: public SHAMapAbstractNode : public SHAMapAbstractNode
{ {
SHAMapHash mHashes[16]; std::array<SHAMapHash, 16> mHashes;
std::shared_ptr<SHAMapAbstractNode> mChildren[16]; std::shared_ptr<SHAMapAbstractNode> mChildren[16];
int mIsBranch = 0; int mIsBranch = 0;
std::uint32_t mFullBelowGen = 0; std::uint32_t mFullBelowGen = 0;

View File

@@ -43,7 +43,7 @@ SHAMapInnerNode::clone(std::uint32_t seq) const
p->mHash = mHash; p->mHash = mHash;
p->mIsBranch = mIsBranch; p->mIsBranch = mIsBranch;
p->mFullBelowGen = mFullBelowGen; p->mFullBelowGen = mFullBelowGen;
std::memcpy(p->mHashes, mHashes, sizeof(mHashes)); p->mHashes = mHashes;
std::unique_lock <std::mutex> lock(childLock); std::unique_lock <std::mutex> lock(childLock);
for (int i = 0; i < 16; ++i) for (int i = 0; i < 16; ++i)
{ {
@@ -60,7 +60,7 @@ SHAMapInnerNodeV2::clone(std::uint32_t seq) const
p->mHash = mHash; p->mHash = mHash;
p->mIsBranch = mIsBranch; p->mIsBranch = mIsBranch;
p->mFullBelowGen = mFullBelowGen; p->mFullBelowGen = mFullBelowGen;
std::memcpy(p->mHashes, mHashes, sizeof(mHashes)); p->mHashes = mHashes;
p->common_ = common_; p->common_ = common_;
p->depth_ = depth_; p->depth_ = depth_;
std::unique_lock <std::mutex> lock(childLock); std::unique_lock <std::mutex> lock(childLock);
@@ -365,10 +365,13 @@ SHAMapInnerNode::updateHash()
uint256 nh; uint256 nh;
if (mIsBranch != 0) if (mIsBranch != 0)
{ {
// VFALCO This code assumes the layout of a base_uint sha512_half_hasher h;
nh = sha512Half(HashPrefix::innerNode, using beast::hash_append;
Slice(reinterpret_cast<unsigned char const*>(mHashes), hash_append(h, HashPrefix::innerNode);
sizeof (mHashes))); for(auto const& hh : mHashes)
hash_append(h, hh);
nh = static_cast<typename
sha512_half_hasher::result_type>(h);
} }
if (nh == mHash.as_uint256()) if (nh == mHash.as_uint256())
return false; return false;
@@ -438,15 +441,15 @@ SHAMapInnerNode::addRaw(Serializer& s, SHANodeFormat format) const
{ {
s.add32 (HashPrefix::innerNode); s.add32 (HashPrefix::innerNode);
for (int i = 0; i < 16; ++i) for (auto const& hh : mHashes)
s.add256 (mHashes[i].as_uint256()); s.add256 (hh.as_uint256());
} }
else // format == snfWIRE else // format == snfWIRE
{ {
if (getBranchCount () < 12) if (getBranchCount () < 12)
{ {
// compressed node // compressed node
for (int i = 0; i < 16; ++i) for (int i = 0; i < mHashes.size(); ++i)
if (!isEmptyBranch (i)) if (!isEmptyBranch (i))
{ {
s.add256 (mHashes[i].as_uint256()); s.add256 (mHashes[i].as_uint256());
@@ -457,8 +460,8 @@ SHAMapInnerNode::addRaw(Serializer& s, SHANodeFormat format) const
} }
else else
{ {
for (int i = 0; i < 16; ++i) for (auto const& hh : mHashes)
s.add256 (mHashes[i].as_uint256()); s.add256 (hh.as_uint256());
s.add8 (2); s.add8 (2);
} }
@@ -606,7 +609,7 @@ std::string
SHAMapInnerNode::getString(const SHAMapNodeID & id) const SHAMapInnerNode::getString(const SHAMapNodeID & id) const
{ {
std::string ret = SHAMapAbstractNode::getString(id); std::string ret = SHAMapAbstractNode::getString(id);
for (int i = 0; i < 16; ++i) for (int i = 0; i < mHashes.size(); ++i)
{ {
if (!isEmptyBranch (i)) if (!isEmptyBranch (i))
{ {