Skip to content

Commit b223f08

Browse files
WarrowsFuzzbawls
authored andcommitted
[Crypto] Remove BigNum constructor implicitly calling SetHex
1 parent 1b0cf3d commit b223f08

File tree

7 files changed

+54
-51
lines changed

7 files changed

+54
-51
lines changed

src/accumulatorcheckpoints.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ namespace AccumulatorCheckpoints
5353
if (!vDenomValue.isStr()) {
5454
return false;
5555
}
56-
checkpoint.insert(make_pair(denom, vDenomValue.get_str()));
56+
CBigNum bn = 0;
57+
if (!bn)
58+
bn.SetHex(vDenomValue.get_str());
59+
checkpoint.insert(std::make_pair(denom, bn));
5760
}
5861

5962
mapCheckpoints.insert(make_pair(nHeight, checkpoint));

src/chainparams.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ static const Checkpoints::CCheckpointData dataRegtest = {
9494
libzerocoin::ZerocoinParams* CChainParams::Zerocoin_Params(bool useModulusV1) const
9595
{
9696
assert(this);
97-
static CBigNum bnHexModulus(zerocoinModulus);
97+
static CBigNum bnHexModulus = 0;
98+
if (!bnHexModulus)
99+
bnHexModulus.SetHex(zerocoinModulus);
98100
static libzerocoin::ZerocoinParams ZCParamsHex = libzerocoin::ZerocoinParams(bnHexModulus);
99101
static CBigNum bnDecModulus = 0;
100102
if (!bnDecModulus)

src/invalid.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ namespace invalid_out
6363
if (!vSerial.isStr())
6464
return false;
6565

66-
CBigNum bnSerial(vSerial.get_str());
66+
CBigNum bnSerial = 0;
67+
if (!bnSerial)
68+
bnSerial.SetHex(vSerial.get_str());
6769
if (bnSerial == 0)
6870
return false;
6971
setInvalidSerials.insert(bnSerial);

src/libzerocoin/bignum.h

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ class CBigNum
5959
bn = BN_new();
6060
}
6161

62-
// Initialize from a Hex String (for zerocoin modulus)
63-
CBigNum(const std::string& str) {
64-
bn = BN_new();
65-
SetHexBool(str);
66-
}
67-
68-
6962
CBigNum(const CBigNum& b)
7063
{
7164
bn = BN_new();
@@ -381,32 +374,7 @@ class CBigNum
381374

382375
void SetHex(const std::string& str)
383376
{
384-
// skip 0x
385-
const char* psz = str.c_str();
386-
while (isspace(*psz))
387-
psz++;
388-
bool fNegative = false;
389-
if (*psz == '-')
390-
{
391-
fNegative = true;
392-
psz++;
393-
}
394-
if (psz[0] == '0' && tolower(psz[1]) == 'x')
395-
psz += 2;
396-
while (isspace(*psz))
397-
psz++;
398-
399-
// hex string to bignum
400-
static const signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
401-
*this = 0;
402-
while (isxdigit(*psz))
403-
{
404-
*this <<= 4;
405-
int n = phexdigit[(unsigned char)*psz++];
406-
*this += n;
407-
}
408-
if (fNegative)
409-
*this = 0 - *this;
377+
SetHexBool(str);
410378
}
411379

412380
bool SetHexBool(const std::string& str)
@@ -447,7 +415,7 @@ class CBigNum
447415
CAutoBN_CTX pctx;
448416
CBigNum bnBase = nBase;
449417
CBigNum bn0 = 0;
450-
CBigNum locBn = *this;
418+
CBigNum locBn = *this;
451419
std::string str;
452420
BN_set_negative(locBn.bn, false);
453421
CBigNum dv;
@@ -473,6 +441,11 @@ class CBigNum
473441
return ToString(16);
474442
}
475443

444+
std::string GetDec() const
445+
{
446+
return ToString(10);
447+
}
448+
476449
unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
477450
{
478451
return ::GetSerializeSize(getvch(), nType, nVersion);

src/rpcblockchain.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,9 @@ UniValue findserial(const UniValue& params, bool fHelp)
918918
HelpExampleCli("findserial", "\"serial\"") + HelpExampleRpc("findserial", "\"serial\""));
919919

920920
std::string strSerial = params[0].get_str();
921-
CBigNum bnSerial(strSerial);
921+
CBigNum bnSerial = 0;
922+
if (!bnSerial)
923+
bnSerial.SetHex(strSerial);
922924

923925
uint256 txid = 0;
924926
bool fSuccess = zerocoinDB->ReadCoinSpend(bnSerial, txid);

src/rpcwallet.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,9 +3187,15 @@ UniValue importzerocoins(const UniValue& params, bool fHelp)
31873187
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, d must be positive");
31883188

31893189
libzerocoin::CoinDenomination denom = libzerocoin::IntToZerocoinDenomination(d);
3190-
CBigNum bnValue = CBigNum(find_value(o, "p").get_str());
3191-
CBigNum bnSerial = CBigNum(find_value(o, "s").get_str());
3192-
CBigNum bnRandom = CBigNum(find_value(o, "r").get_str());
3190+
CBigNum bnValue = 0;
3191+
if (!bnValue)
3192+
bnValue.SetHex(find_value(o, "p").get_str());
3193+
CBigNum bnSerial = 0;
3194+
if (!bnSerial)
3195+
bnSerial.SetHex(find_value(o, "s").get_str());
3196+
CBigNum bnRandom = 0;
3197+
if (!bnRandom)
3198+
bnRandom.SetHex(find_value(o, "r").get_str());
31933199
uint256 txid(find_value(o, "t").get_str());
31943200

31953201
int nHeight = find_value(o, "h").get_int();

src/test/zerocoin_implementation_tests.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ BOOST_AUTO_TEST_CASE(zcparams_test)
4040
}
4141

4242
std::string zerocoinModulus = "25195908475657893494027183240048398571429282126204032027777137836043662020707595556264018525880784"
43-
"4069182906412495150821892985591491761845028084891200728449926873928072877767359714183472702618963750149718246911"
44-
"6507761337985909570009733045974880842840179742910064245869181719511874612151517265463228221686998754918242243363"
45-
"7259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133"
46-
"8441436038339044149526344321901146575444541784240209246165157233507787077498171257724679629263863563732899121548"
47-
"31438167899885040445364023527381951378636564391212010397122822120720357";
48-
CBigNum bnTrustedModulus(zerocoinModulus);
49-
libzerocoin::ZerocoinParams zerocoinParams = libzerocoin::ZerocoinParams(bnTrustedModulus);
43+
"4069182906412495150821892985591491761845028084891200728449926873928072877767359714183472702618963750149718246911"
44+
"6507761337985909570009733045974880842840179742910064245869181719511874612151517265463228221686998754918242243363"
45+
"7259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133"
46+
"8441436038339044149526344321901146575444541784240209246165157233507787077498171257724679629263863563732899121548"
47+
"31438167899885040445364023527381951378636564391212010397122822120720357";
5048

5149
//ZQ_ONE mints
5250
std::string rawTx1 = "0100000001983d5fd91685bb726c0ebc3676f89101b16e663fd896fea53e19972b95054c49000000006a473044022010fbec3e78f9c46e58193d481caff715ceb984df44671d30a2c0bde95c54055f0220446a97d9340da690eaf2658e5b2bf6a0add06f1ae3f1b40f37614c7079ce450d012103cb666bd0f32b71cbf4f32e95fa58e05cd83869ac101435fcb8acee99123ccd1dffffffff0200e1f5050000000086c10280004c80c3a01f94e71662f2ae8bfcd88dfc5b5e717136facd6538829db0c7f01e5fd793cccae7aa1958564518e0223d6d9ce15b1e38e757583546e3b9a3f85bd14408120cd5192a901bb52152e8759fdd194df230d78477706d0e412a66398f330be38a23540d12ab147e9fb19224913f3fe552ae6a587fb30a68743e52577150ff73042c0f0d8f000000001976a914d6042025bd1fff4da5da5c432d85d82b3f26a01688ac00000000";
@@ -207,6 +205,11 @@ bool CheckZerocoinSpendNoDB(const CTransaction tx, string& strError)
207205

208206
BOOST_AUTO_TEST_CASE(checkzerocoinspend_test)
209207
{
208+
CBigNum bnTrustedModulus = 0;
209+
if (!bnTrustedModulus)
210+
bnTrustedModulus.SetDec(zerocoinModulus);
211+
libzerocoin::ZerocoinParams zerocoinParams = libzerocoin::ZerocoinParams(bnTrustedModulus);
212+
210213
cout << "Running check_zerocoinspend_test...\n";
211214

212215
//load our serialized pubcoin
@@ -239,8 +242,14 @@ BOOST_AUTO_TEST_CASE(checkzerocoinspend_test)
239242
// Create a New Zerocoin with specific denomination given by pubCoin
240243
PrivateCoin privateCoin(Params().Zerocoin_Params(true), pubCoin.getDenomination());
241244
privateCoin.setPublicCoin(pubCoin);
242-
privateCoin.setRandomness(CBigNum(rawTxRand1));
243-
privateCoin.setSerialNumber(CBigNum(rawTxSerial1));
245+
CBigNum bn = 0;
246+
if (!bn)
247+
bn.SetHex(rawTxRand1);
248+
privateCoin.setRandomness(bn);
249+
CBigNum bn2 = 0;
250+
if (!bn2)
251+
bn2.SetHex(rawTxSerial1);
252+
privateCoin.setSerialNumber(bn2);
244253
privateCoin.setVersion(1);
245254

246255
//Get the checksum of the accumulator we use for the spend and also add it to our checksum map
@@ -359,6 +368,11 @@ BOOST_AUTO_TEST_CASE(checkzerocoinspend_test)
359368

360369
BOOST_AUTO_TEST_CASE(setup_exceptions_test)
361370
{
371+
CBigNum bnTrustedModulus = 0;
372+
if (!bnTrustedModulus)
373+
bnTrustedModulus.SetDec(zerocoinModulus);
374+
libzerocoin::ZerocoinParams zerocoinParams = libzerocoin::ZerocoinParams(bnTrustedModulus);
375+
362376
cout << "Running check_unitialized parameters,etc for setup exceptions...\n";
363377

364378
CBigNum bnpubcoin;
@@ -447,7 +461,8 @@ BOOST_AUTO_TEST_CASE(bignum_setdecimal)
447461
{
448462
CBigNum bnDec;
449463
bnDec.SetDec(zerocoinModulus);
450-
CBigNum bnHex(strHexModulus);
464+
CBigNum bnHex;
465+
bnHex.SetHex(strHexModulus);
451466
BOOST_CHECK_MESSAGE(bnDec == bnHex, "CBigNum.SetDec() does not work correctly");
452467
}
453468

0 commit comments

Comments
 (0)