Non-linear random number generator based on the AES block cipher in counter mode. Uses the @@ -177,9 +173,8 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot } @Override - @EnsuresNonNull({"counter", "counterInput", "cipher", "lock"}) protected void initTransientFields( - @UnknownInitialization AesCounterRandom this) { + AesCounterRandom this) { super.initTransientFields(); if (counter == null) { counter = new byte[COUNTER_SIZE_BYTES]; @@ -290,7 +285,6 @@ public void setSeed(final byte[] seed) { } catch (final NoSuchAlgorithmException e) { throw new RuntimeException(e); } - this.seed = castNonNull(this.seed); } /** @@ -298,16 +292,15 @@ public void setSeed(final byte[] seed) { */ @Override @SuppressWarnings("contracts.postcondition.not.satisfied") - public synchronized void setSeed(@UnknownInitialization(Random.class)AesCounterRandom this, + public synchronized void setSeed(AesCounterRandom this, final long seed) { if (superConstructorFinished) { super.setSeed(seed); } } - @EnsuresNonNull({"counter", "this.seed", "entropyBits"}) @Override - protected void setSeedInternal(@UnknownInitialization(Random.class)AesCounterRandom this, + protected void setSeedInternal(AesCounterRandom this, final byte[] seed) { final int seedLength = seed.length; if ((seedLength < 16) || (seedLength > MAX_TOTAL_SEED_LENGTH_BYTES)) { @@ -322,7 +315,7 @@ protected void setSeedInternal(@UnknownInitialization(Random.class)AesCounterRan counter = new byte[COUNTER_SIZE_BYTES]; System.arraycopy(seed, keyLength, counter, 0, seedLength - keyLength); try { - castNonNull(cipher).init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); } catch (final InvalidKeyException e) { throw new RuntimeException("Invalid key: " + Arrays.toString(key), e); } @@ -331,7 +324,7 @@ protected void setSeedInternal(@UnknownInitialization(Random.class)AesCounterRan /** Returns the longest supported seed length. */ @Override - public int getNewSeedLength(@UnknownInitialization AesCounterRandom this) { + public int getNewSeedLength(AesCounterRandom this) { return MAX_TOTAL_SEED_LENGTH_BYTES; } } diff --git a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/BaseRandom.java b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/BaseRandom.java index 4ef05c1dc..a1b052b78 100644 --- a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/BaseRandom.java +++ b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/BaseRandom.java @@ -1,7 +1,6 @@ package io.github.pr0methean.betterrandom.prng; import static java.lang.Integer.toUnsignedLong; -import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; @@ -33,10 +32,7 @@ import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.StreamSupport; -import org.checkerframework.checker.initialization.qual.UnderInitialization; -import org.checkerframework.checker.initialization.qual.UnknownInitialization; -import org.checkerframework.checker.nullness.qual.EnsuresNonNull; -import org.checkerframework.checker.nullness.qual.Nullable; +import javax.annotation.Nullable; /** * Abstract {@link Random} with a seed field and an implementation of entropy counting. @@ -104,7 +100,8 @@ protected BaseRandom(final int seedSizeBytes) throws SeedException { * @param seedLength The seed length in bytes. * @throws SeedException If there is a problem generating a seed. */ - protected BaseRandom(final SeedGenerator seedGenerator, final int seedLength) throws SeedException { + protected BaseRandom(final SeedGenerator seedGenerator, final int seedLength) + throws SeedException { this(seedGenerator.generateSeed(seedLength)); entropyBits = new AtomicLong(0); } @@ -114,7 +111,6 @@ protected BaseRandom(final SeedGenerator seedGenerator, final int seedLength) th * * @param seed the seed. */ - @EnsuresNonNull("this.seed") protected BaseRandom(final byte[] seed) { superConstructorFinished = true; if (seed == null) { @@ -631,7 +627,6 @@ public byte[] getSeed() { } } - @EnsuresNonNull({"this.seed", "entropyBits"}) @Override public void setSeed(final byte[] seed) { lock.lock(); @@ -650,9 +645,8 @@ public void setSeed(final byte[] seed) { * input with the existing seed as {@link java.security.SecureRandom#setSeed(long)} does. */ @SuppressWarnings("method.invocation.invalid") - @EnsuresNonNull({"this.seed", "entropyBits"}) @Override - public synchronized void setSeed(@UnknownInitialization(Random.class)BaseRandom this, + public synchronized void setSeed(BaseRandom this, final long seed) { final byte[] seedBytes = BinaryUtils.convertLongToBytes(seed); if (superConstructorFinished) { @@ -703,8 +697,7 @@ public boolean preferSeedWithLong() { * * @param seed The new seed. */ - @EnsuresNonNull({"this.seed", "entropyBits"}) - protected void setSeedInternal(@UnknownInitialization(Random.class)BaseRandom this, + protected void setSeedInternal(BaseRandom this, final byte[] seed) { if (this.seed == null || this.seed.length != seed.length) { this.seed = seed.clone(); @@ -721,20 +714,18 @@ protected void setSeedInternal(@UnknownInitialization(Random.class)BaseRandom th /** * Called in constructor and readObject to initialize transient fields. */ - @EnsuresNonNull("lock") - protected void initTransientFields(@UnknownInitialization BaseRandom this) { + protected void initTransientFields(BaseRandom this) { if (lock == null) { lock = new ReentrantLock(); } superConstructorFinished = true; } - @EnsuresNonNull({"lock", "seed", "entropyBits"}) - private void readObject(@UnderInitialization(BaseRandom.class)BaseRandom this, + private void readObject(BaseRandom this, final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); initTransientFields(); - setSeedInternal(castNonNull(seed)); + setSeedInternal(seed); } @Override @@ -768,7 +759,6 @@ private void asyncReseedIfPossible() { * * @throws InvalidObjectException if the {@link DefaultSeedGenerator} fails. */ - @EnsuresNonNull({"lock", "seed", "entropyBits"}) @SuppressWarnings("OverriddenMethodCallDuringObjectConstruction") private void readObjectNoData() throws InvalidObjectException { LOG.warn("BaseRandom.readObjectNoData() invoked; using DefaultSeedGenerator"); @@ -789,8 +779,7 @@ private void readObjectNoData() throws InvalidObjectException { * subclasses that can't actually use an 8-byte seed. Also used in {@link #readObjectNoData()}. */ @SuppressWarnings("LockAcquiredButNotSafelyReleased") - @EnsuresNonNull({"seed", "entropyBits"}) - protected void fallbackSetSeed(@UnknownInitialization BaseRandom this) { + protected void fallbackSetSeed(BaseRandom this) { boolean locked = false; if (lock != null) { lock.lock(); @@ -815,5 +804,5 @@ protected void fallbackSetSeed(@UnknownInitialization BaseRandom this) { } @Override - public abstract int getNewSeedLength(@UnknownInitialization BaseRandom this); + public abstract int getNewSeedLength(BaseRandom this); } diff --git a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/CellularAutomatonRandom.java b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/CellularAutomatonRandom.java index 103ea9c28..612aec921 100644 --- a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/CellularAutomatonRandom.java +++ b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/CellularAutomatonRandom.java @@ -23,10 +23,6 @@ import io.github.pr0methean.betterrandom.seed.SeedGenerator; import io.github.pr0methean.betterrandom.util.BinaryUtils; import java.util.Arrays; -import java.util.Random; -import org.checkerframework.checker.initialization.qual.UnknownInitialization; -import org.checkerframework.checker.nullness.qual.EnsuresNonNull; -import org.checkerframework.checker.nullness.qual.RequiresNonNull; /** * Java port of the A Java version of George Marsaglia's Complementary @@ -82,7 +78,6 @@ public Cmwc4096Random(final SeedGenerator seedGenerator) throws SeedException { */ public Cmwc4096Random(final byte[] seed) { super(seed); - state = castNonNull(state); } @Override @@ -102,14 +97,13 @@ public byte[] getSeed() { * @param seed ignored */ @Override - public synchronized void setSeed(@UnknownInitialization(Random.class)Cmwc4096Random this, + public synchronized void setSeed(Cmwc4096Random this, final long seed) { fallbackSetSeed(); } - @EnsuresNonNull({"this.seed", "state", "entropyBits"}) @Override - protected void setSeedInternal(@UnknownInitialization(Random.class)Cmwc4096Random this, + protected void setSeedInternal(Cmwc4096Random this, final byte[] seed) { if ((seed == null) || (seed.length != SEED_SIZE_BYTES)) { throw new IllegalArgumentException("CMWC RNG requires 16kb of seed data."); @@ -142,7 +136,7 @@ protected int next(final int bits) { /** Returns the only supported seed length. */ @Override - public int getNewSeedLength(@UnknownInitialization Cmwc4096Random this) { + public int getNewSeedLength(Cmwc4096Random this) { return SEED_SIZE_BYTES; } } diff --git a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/MersenneTwisterRandom.java b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/MersenneTwisterRandom.java index 0b2a23c07..f3d178957 100644 --- a/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/MersenneTwisterRandom.java +++ b/betterrandom/src/main/java/io/github/pr0methean/betterrandom/prng/MersenneTwisterRandom.java @@ -15,8 +15,6 @@ // ============================================================================ package io.github.pr0methean.betterrandom.prng; -import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull; - import com.google.common.base.MoreObjects.ToStringHelper; import io.github.pr0methean.betterrandom.seed.DefaultSeedGenerator; import io.github.pr0methean.betterrandom.seed.SeedException; @@ -24,8 +22,6 @@ import io.github.pr0methean.betterrandom.util.BinaryUtils; import java.util.Arrays; import java.util.Random; -import org.checkerframework.checker.initialization.qual.UnknownInitialization; -import org.checkerframework.checker.nullness.qual.EnsuresNonNull; /** *