message: [Zerocoin] Fix limits for random number generators in GMP bignum
diff: @@ -130,7 +130,7 @@ public:
  * @param k The bit length of the number.
  * @return
  */
  - static CBigNum RandKBitBigum(const uint32_t k){
  + static CBigNum randKBitBignum(const uint32_t k){
  CBigNum ret;
  if(!BN_rand(ret.bn, k, -1, 0)){
  throw bignum_error("CBigNum:rand element : BN_rand failed");
  @@ -778,12 +778,18 @@ public:
  setvch(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
  + * (returns 0 if range = 0 or 1)
  * @param range The upper bound on the number.
  * @return
  */
  static CBigNum randBignum(const CBigNum& range) {
  + if (range < 2)
  + return 0;
  +
  size_t size = (mpz_sizeinbase (range.bn, 2) + CHAR_BIT-1) / CHAR_BIT;
  std::vector<unsigned char> buf(size);
  @@ -793,14 +799,14 @@ public:
  CBigNum ret(buf);
  if (ret < 0)
  mpz_neg(ret.bn, ret.bn);
  - return ret;
  + return 1 + (ret % (range-1));
  }
  /** Generates a cryptographically secure random k-bit number
  * @param k The bit length of the number.
  * @return
  */
  - static CBigNum RandKBitBigum(const uint32_t k){
  + static CBigNum randKBitBignum(const uint32_t k){
  std::vector<unsigned char> buf((k+7)/8);
  RandAddSeed();
  @@ -809,7 +815,7 @@ public:
  CBigNum ret(buf);
  if (ret < 0)
  mpz_neg(ret.bn, ret.bn);
  - return ret;
  + return ret % (CBigNum(1) << k);
  }
  /**Returns the size in bits of the underlying bignum.
  @@ -1033,7 +1039,7 @@ public:
  * @return the prime
  */
  static CBigNum generatePrime(const unsigned int numBits, bool safe = false) {
  - CBigNum rand = RandKBitBigum(numBits);
  + CBigNum rand = randKBitBignum(numBits);
  CBigNum prime;
  mpz_nextprime(prime.bn, rand.bn);
  return prime;
  