Skip to content

Commit f8c41ca

Browse files
committed
[Refactor] zerocoin: use arith_uint256 where needed
1 parent bce0583 commit f8c41ca

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

src/legacy/validation_zerocoin_legacy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void DataBaseAccChecksum(const CBlockIndex* pindex, bool fWrite)
5555
pindex->nAccumulatorCheckpoint == pindex->pprev->nAccumulatorCheckpoint)
5656
return;
5757

58-
uint256 accCurr = pindex->nAccumulatorCheckpoint;
59-
uint256 accPrev = pindex->pprev->nAccumulatorCheckpoint;
58+
arith_uint256 accCurr = UintToArith256(pindex->nAccumulatorCheckpoint);
59+
arith_uint256 accPrev = UintToArith256(pindex->pprev->nAccumulatorCheckpoint);
6060
// add/remove changed checksums to/from DB
6161
for (int i = (int)libzerocoin::zerocoinDenomList.size()-1; i >= 0; i--) {
6262
const uint32_t& nChecksum = accCurr.Get32();

src/libzerocoin/Coin.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ int ExtractVersionFromSerial(const CBigNum& bnSerial)
6262
{
6363
try {
6464
//Serial is marked as v2 only if the first byte is 0xF
65-
uint256 nMark = bnSerial.getuint256() >> (256 - V2_BITSHIFT);
66-
if (nMark == 0xf)
65+
arith_uint256 nMark = bnSerial.getuint256() >> (256 - V2_BITSHIFT);
66+
if (nMark == arith_uint256(0xf))
6767
return PUBKEY_VERSION;
6868
} catch (const std::range_error& e) {
6969
//std::cout << "ExtractVersionFromSerial(): " << e.what() << std::endl;
@@ -77,8 +77,7 @@ int ExtractVersionFromSerial(const CBigNum& bnSerial)
7777
//Remove the first four bits for V2 serials
7878
CBigNum GetAdjustedSerial(const CBigNum& bnSerial)
7979
{
80-
uint256 serial = bnSerial.getuint256();
81-
serial &= ~UINT256_ZERO >> V2_BITSHIFT;
80+
const uint256& serial = ArithToUint256(bnSerial.getuint256() & (~ARITH_UINT256_ZERO >> V2_BITSHIFT));
8281
CBigNum bnSerialAdjusted;
8382
bnSerialAdjusted.setuint256(serial);
8483
return bnSerialAdjusted;
@@ -108,9 +107,9 @@ bool IsValidCommitmentToCoinRange(const ZerocoinParams* params, const CBigNum& b
108107

109108
CBigNum ExtractSerialFromPubKey(const CPubKey pubkey)
110109
{
111-
uint256 hashedPubkey = Hash(pubkey.begin(), pubkey.end()) >> V2_BITSHIFT;
112-
uint256 uintSerial = (uint256(0xF) << (256 - V2_BITSHIFT)) | hashedPubkey;
113-
return CBigNum(uintSerial);
110+
const arith_uint256& hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT;
111+
arith_uint256 uintSerial = (arith_uint256(0xF) << (256 - V2_BITSHIFT)) | hashedPubkey;
112+
return CBigNum(ArithToUint256(uintSerial));
114113
}
115114

116115

src/libzerocoin/CoinSpend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool CoinSpend::HasValidSignature() const
5252

5353
try {
5454
//V2 serial requires that the signature hash be signed by the public key associated with the serial
55-
uint256 hashedPubkey = Hash(pubkey.begin(), pubkey.end()) >> V2_BITSHIFT;
55+
arith_uint256 hashedPubkey = UintToArith256(Hash(pubkey.begin(), pubkey.end())) >> V2_BITSHIFT;
5656
if (hashedPubkey != GetAdjustedSerial(coinSerialNumber).getuint256()) {
5757
//cout << "CoinSpend::HasValidSignature() hashedpubkey is not equal to the serial!\n";
5858
return false;

src/libzerocoin/bignum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ void CBigNum::setuint256(uint256 n)
110110
mpz_import(bn, n.size(), -1, 1, 0, 0, (unsigned char*)&n);
111111
}
112112

113-
uint256 CBigNum::getuint256() const
113+
arith_uint256 CBigNum::getuint256() const
114114
{
115115
if(bitSize() > 256) {
116116
throw std::range_error("cannot convert to uint256, bignum longer than 256 bits");
117117
}
118118
uint256 n = UINT256_ZERO;
119119
mpz_export((unsigned char*)&n, NULL, -1, 1, 0, 0, bn);
120-
return n;
120+
return UintToArith256(n);
121121
}
122122

123123
void CBigNum::setvch(const std::vector<unsigned char>& vch)

src/libzerocoin/bignum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <vector>
1818
#include <limits.h>
1919

20+
#include "arith_uint256.h"
2021
#include "serialize.h"
2122
#include "uint256.h"
2223
#include "version.h"
@@ -72,7 +73,7 @@ class CBigNum
7273
void setint64(int64_t sn);
7374
void setuint64(uint64_t n);
7475
void setuint256(uint256 n);
75-
uint256 getuint256() const;
76+
arith_uint256 getuint256() const;
7677
void setvch(const std::vector<unsigned char>& vch);
7778
std::vector<unsigned char> getvch() const;
7879
void SetDec(const std::string& str);

src/zpiv/zpos.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ uint32_t ParseAccChecksum(uint256 nCheckpoint, const libzerocoin::CoinDenominati
1818
{
1919
int pos = std::distance(libzerocoin::zerocoinDenomList.begin(),
2020
find(libzerocoin::zerocoinDenomList.begin(), libzerocoin::zerocoinDenomList.end(), denom));
21-
nCheckpoint = nCheckpoint >> (32*((libzerocoin::zerocoinDenomList.size() - 1) - pos));
22-
return nCheckpoint.Get32();
21+
return (UintToArith256(nCheckpoint) >> (32*((libzerocoin::zerocoinDenomList.size() - 1) - pos))).Get32();
2322
}
2423

2524
bool CLegacyZPivStake::InitFromTxIn(const CTxIn& txin)
@@ -47,7 +46,7 @@ CLegacyZPivStake::CLegacyZPivStake(const libzerocoin::CoinSpend& spend) : CStake
4746
{
4847
this->nChecksum = spend.getAccumulatorChecksum();
4948
this->denom = spend.getDenomination();
50-
uint256 nSerial = spend.getCoinSerialNumber().getuint256();
49+
arith_uint256 nSerial = spend.getCoinSerialNumber().getuint256();
5150
this->hashSerial = Hash(nSerial.begin(), nSerial.end());
5251
}
5352

0 commit comments

Comments
 (0)