From f8c41cadebda3d536e10b43becc865c27b3727cc Mon Sep 17 00:00:00 2001 From: random-zebra Date: Wed, 14 Apr 2021 21:53:44 +0200 Subject: [PATCH] [Refactor] zerocoin: use arith_uint256 where needed --- src/legacy/validation_zerocoin_legacy.cpp | 4 ++-- src/libzerocoin/Coin.cpp | 13 ++++++------- src/libzerocoin/CoinSpend.cpp | 2 +- src/libzerocoin/bignum.cpp | 4 ++-- src/libzerocoin/bignum.h | 3 ++- src/zpiv/zpos.cpp | 5 ++--- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/legacy/validation_zerocoin_legacy.cpp b/src/legacy/validation_zerocoin_legacy.cpp index db58fb0bc3169..6bd16a1d1bb99 100644 --- a/src/legacy/validation_zerocoin_legacy.cpp +++ b/src/legacy/validation_zerocoin_legacy.cpp @@ -55,8 +55,8 @@ void DataBaseAccChecksum(const CBlockIndex* pindex, bool fWrite) pindex->nAccumulatorCheckpoint == pindex->pprev->nAccumulatorCheckpoint) return; - uint256 accCurr = pindex->nAccumulatorCheckpoint; - uint256 accPrev = pindex->pprev->nAccumulatorCheckpoint; + arith_uint256 accCurr = UintToArith256(pindex->nAccumulatorCheckpoint); + arith_uint256 accPrev = UintToArith256(pindex->pprev->nAccumulatorCheckpoint); // add/remove changed checksums to/from DB for (int i = (int)libzerocoin::zerocoinDenomList.size()-1; i >= 0; i--) { const uint32_t& nChecksum = accCurr.Get32(); diff --git a/src/libzerocoin/Coin.cpp b/src/libzerocoin/Coin.cpp index d5cb6c2614915..d889e78f336ce 100644 --- a/src/libzerocoin/Coin.cpp +++ b/src/libzerocoin/Coin.cpp @@ -62,8 +62,8 @@ int ExtractVersionFromSerial(const CBigNum& bnSerial) { try { //Serial is marked as v2 only if the first byte is 0xF - uint256 nMark = bnSerial.getuint256() >> (256 - V2_BITSHIFT); - if (nMark == 0xf) + arith_uint256 nMark = bnSerial.getuint256() >> (256 - V2_BITSHIFT); + if (nMark == arith_uint256(0xf)) return PUBKEY_VERSION; } catch (const std::range_error& e) { //std::cout << "ExtractVersionFromSerial(): " << e.what() << std::endl; @@ -77,8 +77,7 @@ int ExtractVersionFromSerial(const CBigNum& bnSerial) //Remove the first four bits for V2 serials CBigNum GetAdjustedSerial(const CBigNum& bnSerial) { - uint256 serial = bnSerial.getuint256(); - serial &= ~UINT256_ZERO >> V2_BITSHIFT; + const uint256& serial = ArithToUint256(bnSerial.getuint256() & (~ARITH_UINT256_ZERO >> V2_BITSHIFT)); CBigNum bnSerialAdjusted; bnSerialAdjusted.setuint256(serial); return bnSerialAdjusted; @@ -108,9 +107,9 @@ bool IsValidCommitmentToCoinRange(const ZerocoinParams* params, const CBigNum& b CBigNum ExtractSerialFromPubKey(const CPubKey pubkey) { - uint256 hashedPubkey = Hash(pubkey.begin(), pubkey.end()) >> V2_BITSHIFT; - uint256 uintSerial = (uint256(0xF) << (256 - V2_BITSHIFT)) | hashedPubkey; - return CBigNum(uintSerial); + const arith_uint256& hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT; + arith_uint256 uintSerial = (arith_uint256(0xF) << (256 - V2_BITSHIFT)) | hashedPubkey; + return CBigNum(ArithToUint256(uintSerial)); } diff --git a/src/libzerocoin/CoinSpend.cpp b/src/libzerocoin/CoinSpend.cpp index d0caf8e901595..e13c25b442fa3 100644 --- a/src/libzerocoin/CoinSpend.cpp +++ b/src/libzerocoin/CoinSpend.cpp @@ -52,7 +52,7 @@ bool CoinSpend::HasValidSignature() const try { //V2 serial requires that the signature hash be signed by the public key associated with the serial - uint256 hashedPubkey = Hash(pubkey.begin(), pubkey.end()) >> V2_BITSHIFT; + arith_uint256 hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT; if (hashedPubkey != GetAdjustedSerial(coinSerialNumber).getuint256()) { //cout << "CoinSpend::HasValidSignature() hashedpubkey is not equal to the serial!\n"; return false; diff --git a/src/libzerocoin/bignum.cpp b/src/libzerocoin/bignum.cpp index 20de4431744ad..3a05b2a9fd27a 100644 --- a/src/libzerocoin/bignum.cpp +++ b/src/libzerocoin/bignum.cpp @@ -110,14 +110,14 @@ void CBigNum::setuint256(uint256 n) mpz_import(bn, n.size(), -1, 1, 0, 0, (unsigned char*)&n); } -uint256 CBigNum::getuint256() const +arith_uint256 CBigNum::getuint256() const { if(bitSize() > 256) { throw std::range_error("cannot convert to uint256, bignum longer than 256 bits"); } uint256 n = UINT256_ZERO; mpz_export((unsigned char*)&n, NULL, -1, 1, 0, 0, bn); - return n; + return UintToArith256(n); } void CBigNum::setvch(const std::vector& vch) diff --git a/src/libzerocoin/bignum.h b/src/libzerocoin/bignum.h index 4cacb2f8104b6..4978d589f46ba 100755 --- a/src/libzerocoin/bignum.h +++ b/src/libzerocoin/bignum.h @@ -17,6 +17,7 @@ #include #include +#include "arith_uint256.h" #include "serialize.h" #include "uint256.h" #include "version.h" @@ -72,7 +73,7 @@ class CBigNum void setint64(int64_t sn); void setuint64(uint64_t n); void setuint256(uint256 n); - uint256 getuint256() const; + arith_uint256 getuint256() const; void setvch(const std::vector& vch); std::vector getvch() const; void SetDec(const std::string& str); diff --git a/src/zpiv/zpos.cpp b/src/zpiv/zpos.cpp index 26867ef709fe2..8b887b4c49f41 100644 --- a/src/zpiv/zpos.cpp +++ b/src/zpiv/zpos.cpp @@ -18,8 +18,7 @@ uint32_t ParseAccChecksum(uint256 nCheckpoint, const libzerocoin::CoinDenominati { int pos = std::distance(libzerocoin::zerocoinDenomList.begin(), find(libzerocoin::zerocoinDenomList.begin(), libzerocoin::zerocoinDenomList.end(), denom)); - nCheckpoint = nCheckpoint >> (32*((libzerocoin::zerocoinDenomList.size() - 1) - pos)); - return nCheckpoint.Get32(); + return (UintToArith256(nCheckpoint) >> (32*((libzerocoin::zerocoinDenomList.size() - 1) - pos))).Get32(); } bool CLegacyZPivStake::InitFromTxIn(const CTxIn& txin) @@ -47,7 +46,7 @@ CLegacyZPivStake::CLegacyZPivStake(const libzerocoin::CoinSpend& spend) : CStake { this->nChecksum = spend.getAccumulatorChecksum(); this->denom = spend.getDenomination(); - uint256 nSerial = spend.getCoinSerialNumber().getuint256(); + arith_uint256 nSerial = spend.getCoinSerialNumber().getuint256(); this->hashSerial = Hash(nSerial.begin(), nSerial.end()); }