Skip to content

Commit 253c63e

Browse files
committed
[Zerocoin] include 0 in randBignum() range
1 parent daeb752 commit 253c63e

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

src/libzerocoin/bignum_gmp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ CBigNum::CBigNum(const std::vector<unsigned char>& vch)
5050

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

53-
/** Generates a cryptographically secure random number between zero and range exclusive
54-
* i.e. 0 < returned number < range
53+
/** Generates a cryptographically secure random number between zero and range-1 (inclusive)
54+
* i.e. 0 <= returned number < range
5555
* @param range The upper bound on the number.
5656
* @return
5757
*/
@@ -69,7 +69,7 @@ CBigNum CBigNum::randBignum(const CBigNum& range)
6969
CBigNum ret(buf);
7070
if (ret < 0)
7171
mpz_neg(ret.bn, ret.bn);
72-
return 1 + (ret % (range-1));
72+
return (ret % range);
7373
}
7474

7575
/** Generates a cryptographically secure random k-bit number

src/libzerocoin/bignum_openssl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ CBigNum::CBigNum(const std::vector<unsigned char>& vch)
5252
setvch(vch);
5353
}
5454

55-
/** Generates a cryptographically secure random number between zero and range exclusive
56-
* i.e. 0 < returned number < range
55+
/** Generates a cryptographically secure random number between zero and range-1 (inclusive)
56+
* i.e. 0 <= returned number < range
5757
* @param range The upper bound on the number.
5858
* @return
5959
*/

src/test/zerocoin_bignum_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ bool testRandKBitBignum(int k_bits)
2121
bool testRandBignum(CBigNum limit)
2222
{
2323
CBigNum x = CBigNum::randBignum(limit);
24-
if (limit < 2)
25-
return x == CBigNum(0);
26-
return 0 < x && x < limit;
24+
return 0 <= x && x < limit;
2725
}
2826

2927
BOOST_AUTO_TEST_SUITE(zerocoin_bignum_tests)
@@ -84,7 +82,7 @@ BOOST_AUTO_TEST_CASE(bignum_random_generation_tests)
8482
}
8583

8684
for(int i=1; i<3000; i++) {
87-
CBigNum x = CBigNum::randKBitBignum(i);
85+
CBigNum x = 1 + CBigNum::randKBitBignum(i);
8886
BOOST_CHECK_MESSAGE(testRandBignum(x), strprintf("CBigNum::randBignum(x) failed with x=%s", x.ToString()));
8987
}
9088
}

0 commit comments

Comments
 (0)