From dfe36b64a56f592b278a9d6f4f440778cc8fe55b Mon Sep 17 00:00:00 2001 From: zzz Date: Sat, 26 Nov 2022 09:36:30 -0500 Subject: [PATCH] Util: Fast check for random nextInt(1) and nextLong(1) Document differences from Java's random for n=0 Entropy harvester cleanups, we're not going to add a factory as jrandom implied. --- .../src/net/i2p/util/FortunaRandomSource.java | 16 +++++++++++----- core/java/src/net/i2p/util/RandomSource.java | 6 +----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/java/src/net/i2p/util/FortunaRandomSource.java b/core/java/src/net/i2p/util/FortunaRandomSource.java index 11dc35526..07d90c752 100644 --- a/core/java/src/net/i2p/util/FortunaRandomSource.java +++ b/core/java/src/net/i2p/util/FortunaRandomSource.java @@ -78,10 +78,14 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste * Ok, so we're going to have it return between 0 and n (including 0, excluding n), since * thats what it has been used for. * + * Unlike Java's Random, which throws IAE, this returns 0 if n is 0. + * + * @param n non-negative + * @throws IllegalArgumentException if n is negative */ @Override public int nextInt(int n) { - if (n == 0) return 0; + if (n == 1 || n == 0) return 0; int rv = signedNextInt(n); if (rv < 0) rv = 0 - rv; @@ -134,10 +138,15 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste /** * Like the modified nextInt, nextLong(n) returns a random number from 0 through n, * including 0, excluding n. + * + * Unlike Java's Random, which throws IAE, this returns 0 if n is 0. + * + * @param n non-negative + * @throws IllegalArgumentException if n is negative */ @Override public long nextLong(long n) { - if (n == 0) return 0; + if (n == 1 || n == 0) return 0; long rv = nextLong(); if (rv < 0) rv = 0 - rv; @@ -260,9 +269,6 @@ public class FortunaRandomSource extends RandomSource implements EntropyHarveste return (int)rv; } - @Override - public EntropyHarvester harvester() { return this; } - /** reseed the fortuna */ @Override public void feedEntropy(String source, long data, int bitoffset, int bits) { diff --git a/core/java/src/net/i2p/util/RandomSource.java b/core/java/src/net/i2p/util/RandomSource.java index 85555f005..f4a3c68a3 100644 --- a/core/java/src/net/i2p/util/RandomSource.java +++ b/core/java/src/net/i2p/util/RandomSource.java @@ -27,7 +27,6 @@ import net.i2p.crypto.EntropyHarvester; public class RandomSource extends SecureRandom implements EntropyHarvester { private static final long serialVersionUID = 1L; - private final EntropyHarvester _entropyHarvester; protected transient final I2PAppContext _context; /** @@ -39,9 +38,6 @@ public class RandomSource extends SecureRandom implements EntropyHarvester { public RandomSource(I2PAppContext context) { super(); _context = context; - // when we replace to have hooks for fortuna (etc), replace with - // a factory (or just a factory method) - _entropyHarvester = this; } /** @@ -113,7 +109,7 @@ public class RandomSource extends SecureRandom implements EntropyHarvester { } } - public EntropyHarvester harvester() { return _entropyHarvester; } + public EntropyHarvester harvester() { return this; } public void feedEntropy(String source, long data, int bitoffset, int bits) { if (bitoffset == 0)