Skip to content

Commit

Permalink
Merge pull request #114 from navcoin/improvements-serialization
Browse files Browse the repository at this point in the history
Changes in serialization of transactions
  • Loading branch information
alex v authored Mar 26, 2023
2 parents f55303b + 66c9743 commit 6d1c3ed
Show file tree
Hide file tree
Showing 18 changed files with 585 additions and 642 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Translations are periodically pulled from Transifex and merged into the git repo

**Important**: We do not accept translation changes as GitHub pull requests because the next
pull from Transifex would automatically overwrite them again.

5 changes: 0 additions & 5 deletions src/blsct/arith/mcl/mcl_g1point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,6 @@ std::string MclG1Point::GetString(const uint8_t& radix) const
return std::string(str);
}

size_t MclG1Point::GetSerializeSize() const
{
return SERIALIZATION_SIZE;
}

MclScalar MclG1Point::GetHashWithSalt(const uint64_t salt) const
{
CHashWriter hasher(0, 0);
Expand Down
15 changes: 7 additions & 8 deletions src/blsct/arith/mcl/mcl_g1point.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ class MclG1Point
std::string GetString(const uint8_t& radix = 16) const;
MclScalar GetHashWithSalt(const uint64_t salt) const;

size_t GetSerializeSize() const;

template <typename Stream>
void Serialize(Stream& s) const
{
::Serialize(s, GetVch());
};
auto vec = GetVch();
s.write(MakeByteSpan(vec));
}

template <typename Stream>
void Unserialize(Stream& s)
{
std::vector<uint8_t> vch;
::Unserialize(s, vch);
SetVch(vch);
};
std::vector<unsigned char> vec(SERIALIZATION_SIZE);
s.read(MakeWritableByteSpan(vec));
SetVch(vec);
}

using UnderlyingType = mclBnG1;
UnderlyingType m_p;
Expand Down
25 changes: 1 addition & 24 deletions src/blsct/arith/mcl/mcl_scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ uint64_t MclScalar::GetUint64() const

std::vector<uint8_t> MclScalar::GetVch(const bool trim_preceeding_zeros) const
{
auto seri_size = MclScalar::GetSerializeSize();
auto seri_size = MclScalar::SERIALIZATION_SIZE;
std::vector<uint8_t> vec(seri_size);
if (mclBnFr_serialize(&vec[0], seri_size, &m_fr) == 0) {
// We avoid throwing an exception as the fuzzer would crash when injecting random data
Expand Down Expand Up @@ -373,26 +373,3 @@ bool MclScalar::GetSeriBit(const uint8_t& n) const

return bit;
}

unsigned int MclScalar::GetSerializeSize() const
{
return SERIALIZATION_SIZE;
}

template <typename Stream>
void MclScalar::Serialize(Stream& s) const
{
::Serialize(s, GetVch());
}

template void MclScalar::Serialize(CHashWriter& s) const;
template void MclScalar::Serialize(CDataStream& s) const;

template <typename Stream>
void MclScalar::Unserialize(Stream& s)
{
std::vector<uint8_t> vch;
::Unserialize(s, vch);
SetVch(vch);
}
template void MclScalar::Unserialize(CDataStream& s);
15 changes: 11 additions & 4 deletions src/blsct/arith/mcl/mcl_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,20 @@ class MclScalar
*/
std::vector<bool> ToBinaryVec() const;

unsigned int GetSerializeSize() const;

template <typename Stream>
void Serialize(Stream& s) const;
void Serialize(Stream& s) const
{
auto vec = GetVch();
s.write(MakeByteSpan(vec));
}

template <typename Stream>
void Unserialize(Stream& s);
void Unserialize(Stream& s)
{
std::vector<unsigned char> vec(SERIALIZATION_SIZE);
s.read(MakeWritableByteSpan(vec));
SetVch(vec);
}

static constexpr int SERIALIZATION_SIZE = 32;

Expand Down
48 changes: 0 additions & 48 deletions src/blsct/range_proof/range_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,3 @@

#include <blsct/range_proof/range_proof.h>

template <typename T>
std::vector<uint8_t> SerializeRangeProof(const RangeProof<T>& proof)
{
CDataStream s(0, 0);

s << proof.Vs;
s << proof.Ls;
s << proof.Rs;
s << proof.A;
s << proof.S;
s << proof.T1;
s << proof.T2;
s << proof.tau_x;
s << proof.mu;
s << proof.a;
s << proof.b;
s << proof.t_hat;

Span spanStream(s);
std::vector<uint8_t> vRet(spanStream.size());
memcpy(&vRet[0], spanStream.begin(), spanStream.size());

return vRet;
}
template std::vector<uint8_t> SerializeRangeProof(const RangeProof<Mcl>& proof);

template <typename T>
RangeProof<T> UnserializeRangeProof(const std::vector<unsigned char>& vecIn)
{
RangeProof<T> retProof;
CDataStream s(vecIn, 0, 0);

s >> retProof.Vs;
s >> retProof.Ls;
s >> retProof.Rs;
s >> retProof.A;
s >> retProof.S;
s >> retProof.T1;
s >> retProof.T2;
s >> retProof.tau_x;
s >> retProof.mu;
s >> retProof.a;
s >> retProof.b;
s >> retProof.t_hat;

return retProof;
}
template RangeProof<Mcl> UnserializeRangeProof(const std::vector<unsigned char>& vecIn);
38 changes: 33 additions & 5 deletions src/blsct/range_proof/range_proof.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,40 @@ struct RangeProof {
Scalar t_hat; // inner product of l and r
Scalar a; // result of inner product argument
Scalar b; // result of inner product argument
};

template <typename T>
std::vector<uint8_t> SerializeRangeProof(const RangeProof<T>& proof);
template <typename Stream>
void Serialize(Stream& s) const
{
::Serialize(s, Vs);
::Serialize(s, Ls);
::Serialize(s, Rs);
::Serialize(s, A);
::Serialize(s, S);
::Serialize(s, T1);
::Serialize(s, T2);
::Serialize(s, tau_x);
::Serialize(s, mu);
::Serialize(s, a);
::Serialize(s, b);
::Serialize(s, t_hat);
}

template <typename T>
RangeProof<T> UnserializeRangeProof(const std::vector<unsigned char>& vecIn);
template <typename Stream>
void Unserialize(Stream& s)
{
::Unserialize(s, Vs);
::Unserialize(s, Ls);
::Unserialize(s, Rs);
::Unserialize(s, A);
::Unserialize(s, S);
::Unserialize(s, T1);
::Unserialize(s, T2);
::Unserialize(s, tau_x);
::Unserialize(s, mu);
::Unserialize(s, a);
::Unserialize(s, b);
::Unserialize(s, t_hat);
}
};

#endif // NAVCOIN_BLSCT_RANGE_PROOF_RANGE_PROOF_H
11 changes: 7 additions & 4 deletions src/blsct/signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ class Signature
template <typename Stream>
void Serialize(Stream& s) const
{
s << GetVch();
auto vec = GetVch();
s.write(MakeByteSpan(vec));
}

template <typename Stream>
void Unserialize(Stream& s)
{
std::vector<uint8_t> vch;
s >> vch;
SetVch(vch);
std::vector<unsigned char> vec(SERIALIZATION_SIZE);
s.read(MakeWritableByteSpan(vec));
SetVch(vec);
}

blsSignature m_data;

static constexpr int SERIALIZATION_SIZE = 96;
};

} // namespace blsct
Expand Down
1 change: 0 additions & 1 deletion src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry

if (tx.IsBLSCT()) {
entry.pushKV("txSig", HexStr(tx.txSig.GetVch()));
entry.pushKV("balanceSig", HexStr(tx.balanceSig.GetVch()));
}

if (have_undo) {
Expand Down
8 changes: 4 additions & 4 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::string CTxOut::ToString() const
}

CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig) {}
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), txSig(tx.txSig) {}

uint256 CMutableTransaction::GetHash() const
{
Expand All @@ -86,8 +86,8 @@ uint256 CTransaction::ComputeWitnessHash() const
return SerializeHash(*this, SER_GETHASH, 0);
}

CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), balanceSig(tx.balanceSig), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), txSig(tx.txSig), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}

CAmount CTransaction::GetValueOut() const
{
Expand Down Expand Up @@ -115,7 +115,7 @@ std::string CTransaction::ToString(bool fIncludeSignatures) const
vin.size(),
vout.size(),
nLockTime,
fIncludeSignatures ? strprintf(", balanceSig=%s, txSig=%s", HexStr(balanceSig.GetVch()), HexStr(txSig.GetVch())) : "");
fIncludeSignatures ? strprintf(", txSig=%s", HexStr(txSig.GetVch())) : "");
for (const auto& tx_in : vin)
str += " " + tx_in.ToString() + "\n";
for (const auto& tx_in : vin)
Expand Down
11 changes: 3 additions & 8 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class CTxOutBLSCTData
template <typename Stream>
void Serialize(Stream& s) const
{
::Serialize(s, SerializeRangeProof(rangeProof));
::Serialize(s, rangeProof);
::Serialize(s, spendingKey);
::Serialize(s, blindingKey);
::Serialize(s, ephemeralKey);
Expand All @@ -187,9 +187,7 @@ class CTxOutBLSCTData
template <typename Stream>
void Unserialize(Stream& s)
{
std::vector<uint8_t> rangeProofVec;
::Unserialize(s, rangeProofVec);
rangeProof = UnserializeRangeProof<Mcl>(rangeProofVec);
::Unserialize(s, rangeProof);
::Unserialize(s, spendingKey);
::Unserialize(s, blindingKey);
::Unserialize(s, ephemeralKey);
Expand Down Expand Up @@ -258,6 +256,7 @@ class CTxOut
::Unserialize(s, nValue);
uint64_t nFlags = 0;
if (nValue == std::numeric_limits<CAmount>::max()) {
nValue = 0;
::Unserialize(s, nFlags);
}
::Unserialize(s, scriptPubKey);
Expand Down Expand Up @@ -354,7 +353,6 @@ inline void UnserializeTransaction(TxType& tx, Stream& s)
}
s >> tx.nLockTime;
if (tx.IsBLSCT()) {
s >> tx.balanceSig;
s >> tx.txSig;
}
}
Expand Down Expand Up @@ -388,7 +386,6 @@ inline void SerializeTransaction(const TxType& tx, Stream& s)
}
s << tx.nLockTime;
if (tx.IsBLSCT()) {
s << tx.balanceSig;
s << tx.txSig;
}
}
Expand Down Expand Up @@ -419,7 +416,6 @@ class CTransaction
const std::vector<CTxOut> vout;
const int32_t nVersion;
const uint32_t nLockTime;
blsct::Signature balanceSig;
blsct::Signature txSig;

private:
Expand Down Expand Up @@ -505,7 +501,6 @@ struct CMutableTransaction {
std::vector<CTxOut> vout;
int32_t nVersion;
uint32_t nLockTime;
blsct::Signature balanceSig;
blsct::Signature txSig;

explicit CMutableTransaction();
Expand Down
1 change: 0 additions & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ static std::vector<RPCResult> DecodeTxDoc(const std::string& txid_field_doc)
{RPCResult::Type::NUM, "viewTag", /*optional=*/true, "output's view tag"},
}},
}},
{RPCResult::Type::STR_HEX, "balanceSig", /*optional=*/true, "hex-encoded balance signature"},
{RPCResult::Type::STR_HEX, "txSig", /*optional=*/true, "hex-encoded transaction signature"},
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,6 @@ class CTransactionSignatureSerializer
// Serialize nLockTime
::Serialize(s, txTo.nLockTime);
if (txTo.IsBLSCT()) {
::Serialize(s, txTo.balanceSig);
::Serialize(s, txTo.txSig);
}
}
Expand Down Expand Up @@ -1545,7 +1544,6 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
// Locktime
ss << txTo.nLockTime;
if (txTo.IsBLSCT()) {
ss << txTo.balanceSig;
ss << txTo.txSig;
}
// Sighash type
Expand Down
9 changes: 1 addition & 8 deletions src/test/blsct/arith/mcl/mcl_g1point_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,12 @@ BOOST_AUTO_TEST_CASE(test_get_string)
BOOST_CHECK(g_exp == g_act);
}

BOOST_AUTO_TEST_CASE(test_get_serialize_size)
{
MclG1Point p(uint256::ONE);
auto ser_size = p.GetSerializeSize();
BOOST_CHECK_EQUAL(ser_size, 48ul);
}

BOOST_AUTO_TEST_CASE(test_serialize_unserialize)
{
MclG1Point p(uint256::ONE);
CDataStream st(0, 0);
p.Serialize(st);
BOOST_CHECK_EQUAL(st.size(), 49ul); // TODO check why 49 instead of 48
BOOST_CHECK_EQUAL(st.size(), 48ul);

MclG1Point q;
BOOST_CHECK(p != q);
Expand Down
3 changes: 1 addition & 2 deletions src/test/blsct/arith/mcl/mcl_scalar_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,15 +787,14 @@ BOOST_AUTO_TEST_CASE(test_setpow2)
BOOST_AUTO_TEST_CASE(test_hash)
{
// upon generating the digest, following data is added to the hasher:
// - 1 byte storing the size of the following array
// - 32-byte big-endian array representing the Scalar value
// - 4-byte little-endian array representing the parameter of Hash function

MclScalar a(1);
const int n = 51;
uint256 digest = a.GetHashWithSalt(n);
auto act = digest.GetHex();
std::string exp("def41d150b8d183ab49b001838f5c824ceba560e68e3e1a5d43f62cbd30a37f8");
std::string exp("ccf3c17eeef9710908b3a66f5103cf97cc7313e0f49cf93429dc92d01f8fd5b2");
BOOST_CHECK(act == exp);
}

Expand Down
Loading

0 comments on commit 6d1c3ed

Please sign in to comment.