From 62ee966e9ce816512ee032fbacf9e8cbd0d9ab31 Mon Sep 17 00:00:00 2001 From: Mark J Roberts Date: Fri, 17 Aug 2001 22:21:02 +0000 Subject: BigInteger.java (randBytes): New method. 2001-08-17 Mark J Roberts * java/math/BigInteger.java (randBytes): New method. (BigInteger(int,Random)): Use randBytes. From-SVN: r44984 --- libjava/java/math/BigInteger.java | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'libjava/java/math/BigInteger.java') diff --git a/libjava/java/math/BigInteger.java b/libjava/java/math/BigInteger.java index e74c5e1..b9bfee6 100644 --- a/libjava/java/math/BigInteger.java +++ b/libjava/java/math/BigInteger.java @@ -144,21 +144,20 @@ public class BigInteger extends Number implements Comparable public BigInteger(int numBits, Random rnd) { + this(1, randBytes(numBits, rnd)); + } + + private static byte[] randBytes(int numBits, Random rnd) + { if (numBits < 0) throw new IllegalArgumentException(); - // Result is always positive so tack on an extra zero word, it will be - // canonicalized out later if necessary. - int nwords = numBits / 32 + 2; - words = new int[nwords]; - words[--nwords] = 0; - words[--nwords] = rnd.nextInt() >>> (numBits % 32); - while (--nwords >= 0) - words[nwords] = rnd.nextInt(); - - BigInteger result = make(words, words.length); - this.ival = result.ival; - this.words = result.words; + int extra = numBits % 8; + byte[] b = new byte[numBits / 8 + (extra > 0 ? 1 : 0)]; + rnd.nextBytes(b); + if (extra > 0) + b[0] &= ~((~0) << (8 - extra)); + return b; } public BigInteger(int bitLength, int certainty, Random rnd) -- cgit v1.1