aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-03-16 18:42:49 -0400
committerJack Lloyd <[email protected]>2016-03-16 18:42:49 -0400
commitb5d8783fccbd4b6686708fd4f2f84eaada3e8fed (patch)
tree75e6615a0c972da54f29593dd19f468b3a1fe0c3 /src/lib/math
parentf209329d885310fb510742317a20d1f51099b29e (diff)
Use rejection sampling in BigInt::random_integer
Avoids the test vector contortions in RSA-KEM
Diffstat (limited to 'src/lib/math')
-rw-r--r--src/lib/math/bigint/big_rand.cpp18
1 files changed, 8 insertions, 10 deletions
diff --git a/src/lib/math/bigint/big_rand.cpp b/src/lib/math/bigint/big_rand.cpp
index cfc1facee..73f3cf070 100644
--- a/src/lib/math/bigint/big_rand.cpp
+++ b/src/lib/math/bigint/big_rand.cpp
@@ -45,19 +45,17 @@ void BigInt::randomize(RandomNumberGenerator& rng,
BigInt BigInt::random_integer(RandomNumberGenerator& rng,
const BigInt& min, const BigInt& max)
{
- BigInt delta_upper_bound = max - min - 1;
+ BigInt r;
- if(delta_upper_bound < 0)
- throw Invalid_Argument("random_integer: invalid min/max values");
+ const size_t bits = max.bits();
- // Choose x in [0, delta_upper_bound]
- BigInt x;
- do {
- auto bitsize = delta_upper_bound.bits();
- x.randomize(rng, bitsize, false);
- } while(x > delta_upper_bound);
+ do
+ {
+ r.randomize(rng, bits, false);
+ }
+ while(r < min || r >= max);
- return min + x;
+ return r;
}
}