Skip to content

Commit

Permalink
[Zerocoin] include 0 in randBignum() range
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 17, 2019
1 parent daeb752 commit 253c63e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/libzerocoin/bignum_gmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ CBigNum::CBigNum(const std::vector<unsigned char>& vch)

/** PRNGs use OpenSSL for consistency with seed initialization **/

/** Generates a cryptographically secure random number between zero and range exclusive
* i.e. 0 < returned number < range
/** Generates a cryptographically secure random number between zero and range-1 (inclusive)
* i.e. 0 <= returned number < range
* @param range The upper bound on the number.
* @return
*/
Expand All @@ -69,7 +69,7 @@ CBigNum CBigNum::randBignum(const CBigNum& range)
CBigNum ret(buf);
if (ret < 0)
mpz_neg(ret.bn, ret.bn);
return 1 + (ret % (range-1));
return (ret % range);
}

/** Generates a cryptographically secure random k-bit number
Expand Down
4 changes: 2 additions & 2 deletions src/libzerocoin/bignum_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ CBigNum::CBigNum(const std::vector<unsigned char>& vch)
setvch(vch);
}

/** Generates a cryptographically secure random number between zero and range exclusive
* i.e. 0 < returned number < range
/** Generates a cryptographically secure random number between zero and range-1 (inclusive)
* i.e. 0 <= returned number < range
* @param range The upper bound on the number.
* @return
*/
Expand Down
6 changes: 2 additions & 4 deletions src/test/zerocoin_bignum_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ bool testRandKBitBignum(int k_bits)
bool testRandBignum(CBigNum limit)
{
CBigNum x = CBigNum::randBignum(limit);
if (limit < 2)
return x == CBigNum(0);
return 0 < x && x < limit;
return 0 <= x && x < limit;
}

BOOST_AUTO_TEST_SUITE(zerocoin_bignum_tests)
Expand Down Expand Up @@ -84,7 +82,7 @@ BOOST_AUTO_TEST_CASE(bignum_random_generation_tests)
}

for(int i=1; i<3000; i++) {
CBigNum x = CBigNum::randKBitBignum(i);
CBigNum x = 1 + CBigNum::randKBitBignum(i);
BOOST_CHECK_MESSAGE(testRandBignum(x), strprintf("CBigNum::randBignum(x) failed with x=%s", x.ToString()));
}
}
Expand Down

0 comments on commit 253c63e

Please sign in to comment.