diff options
author | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-08-17 22:21:02 +0000 |
---|---|---|
committer | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-08-17 22:21:02 +0000 |
commit | 5c6b5fe5d6fa26ebe255a668e09e4f5471b02fdb (patch) | |
tree | 0b1fec8feb4b6c2e03281102ecfbcbd66e06c5c6 /libjava | |
parent | 1be87b7252bb4b5936fc2ebebdcb9f18bed35bae (diff) | |
download | ppe42-gcc-5c6b5fe5d6fa26ebe255a668e09e4f5471b02fdb.tar.gz ppe42-gcc-5c6b5fe5d6fa26ebe255a668e09e4f5471b02fdb.zip |
2001-08-17 Mark J Roberts <mjr@anarcast.net>
* java/math/BigInteger.java (randBytes): New method.
(BigInteger(int,Random)): Use randBytes.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@44984 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/ChangeLog | 5 | ||||
-rw-r--r-- | libjava/java/math/BigInteger.java | 23 |
2 files changed, 16 insertions, 12 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 12c86fc5cc7..1567a3d1af7 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,8 @@ +2001-08-17 Mark J Roberts <mjr@anarcast.net> + + * java/math/BigInteger.java (randBytes): New method. + (BigInteger(int,Random)): Use randBytes. + 2001-08-17 Tom Tromey <tromey@redhat.com> * gnu/gcj/convert/IOConverter.java: Add `646' alias. diff --git a/libjava/java/math/BigInteger.java b/libjava/java/math/BigInteger.java index e74c5e1c8ea..b9bfee695b5 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) |