Skip to content

Commit

Permalink
[Refactor] zerocoin: use arith_uint256 where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 5, 2021
1 parent bce0583 commit f8c41ca
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/legacy/validation_zerocoin_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 6 additions & 7 deletions src/libzerocoin/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}


Expand Down
2 changes: 1 addition & 1 deletion src/libzerocoin/CoinSpend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/libzerocoin/bignum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>& vch)
Expand Down
3 changes: 2 additions & 1 deletion src/libzerocoin/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>
#include <limits.h>

#include "arith_uint256.h"
#include "serialize.h"
#include "uint256.h"
#include "version.h"
Expand Down Expand Up @@ -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<unsigned char>& vch);
std::vector<unsigned char> getvch() const;
void SetDec(const std::string& str);
Expand Down
5 changes: 2 additions & 3 deletions src/zpiv/zpos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit f8c41ca

Please sign in to comment.