diff options
Diffstat (limited to 'src/main/java/gtPlusPlus/api/objects')
9 files changed, 0 insertions, 1466 deletions
diff --git a/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java b/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java deleted file mode 100644 index eff314f9ae..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java +++ /dev/null @@ -1,243 +0,0 @@ -package gtPlusPlus.api.objects.data; - -import java.util.Arrays; - -/** - * Object-2-object map based on IntIntMap4a - */ -public class ObjMap<K, V> { - - private static final Object FREE_KEY = new Object(); - private static final Object REMOVED_KEY = new Object(); - - /** Keys and values */ - private Object[] m_data; - - /** Value for the null key (if inserted into a map) */ - private Object m_nullValue; - - private boolean m_hasNull; - - /** Fill factor, must be between (0 and 1) */ - private final float m_fillFactor; - /** We will resize a map once it reaches this size */ - private int m_threshold; - /** Current map size */ - private int m_size; - /** Mask to calculate the original position */ - private int m_mask; - /** Mask to wrap the actual array pointer */ - private int m_mask2; - - public ObjMap(final int size, final float fillFactor) { - if (fillFactor <= 0 || fillFactor >= 1) throw new IllegalArgumentException("FillFactor must be in (0, 1)"); - if (size <= 0) throw new IllegalArgumentException("Size must be positive!"); - final int capacity = arraySize(size, fillFactor); - m_mask = capacity - 1; - m_mask2 = capacity * 2 - 1; - m_fillFactor = fillFactor; - - m_data = new Object[capacity * 2]; - Arrays.fill(m_data, FREE_KEY); - - m_threshold = (int) (capacity * fillFactor); - } - - @SuppressWarnings("unchecked") - public V get(final K key) { - if (key == null) return (V) m_nullValue; // we null it on remove, so safe not to check a flag here - - int ptr = (key.hashCode() & m_mask) << 1; - Object k = m_data[ptr]; - - if (k == FREE_KEY) return null; // end of chain already - if (k.equals(key)) // we check FREE and REMOVED prior to this call - return (V) m_data[ptr + 1]; - while (true) { - ptr = (ptr + 2) & m_mask2; // that's next index - k = m_data[ptr]; - if (k == FREE_KEY) return null; - if (k.equals(key)) return (V) m_data[ptr + 1]; - } - } - - @SuppressWarnings("unchecked") - public V put(final K key, final V value) { - if (key == null) return insertNullKey(value); - - int ptr = getStartIndex(key) << 1; - Object k = m_data[ptr]; - - if (k == FREE_KEY) // end of chain already - { - m_data[ptr] = key; - m_data[ptr + 1] = value; - if (m_size >= m_threshold) rehash(m_data.length * 2); // size is set inside - else++m_size; - return null; - } else if (k.equals(key)) // we check FREE and REMOVED prior to this call - { - final Object ret = m_data[ptr + 1]; - m_data[ptr + 1] = value; - return (V) ret; - } - - int firstRemoved = -1; - if (k == REMOVED_KEY) firstRemoved = ptr; // we may find a key later - - while (true) { - ptr = (ptr + 2) & m_mask2; // that's next index calculation - k = m_data[ptr]; - if (k == FREE_KEY) { - if (firstRemoved != -1) ptr = firstRemoved; - m_data[ptr] = key; - m_data[ptr + 1] = value; - if (m_size >= m_threshold) rehash(m_data.length * 2); // size is set inside - else++m_size; - return null; - } else if (k.equals(key)) { - final Object ret = m_data[ptr + 1]; - m_data[ptr + 1] = value; - return (V) ret; - } else if (k == REMOVED_KEY) { - if (firstRemoved == -1) firstRemoved = ptr; - } - } - } - - @SuppressWarnings("unchecked") - public V remove(final K key) { - if (key == null) return removeNullKey(); - - int ptr = getStartIndex(key) << 1; - Object k = m_data[ptr]; - if (k == FREE_KEY) return null; // end of chain already - else if (k.equals(key)) // we check FREE and REMOVED prior to this call - { - --m_size; - if (m_data[(ptr + 2) & m_mask2] == FREE_KEY) m_data[ptr] = FREE_KEY; - else m_data[ptr] = REMOVED_KEY; - final V ret = (V) m_data[ptr + 1]; - m_data[ptr + 1] = null; - return ret; - } - while (true) { - ptr = (ptr + 2) & m_mask2; // that's next index calculation - k = m_data[ptr]; - if (k == FREE_KEY) return null; - else if (k.equals(key)) { - --m_size; - if (m_data[(ptr + 2) & m_mask2] == FREE_KEY) m_data[ptr] = FREE_KEY; - else m_data[ptr] = REMOVED_KEY; - final V ret = (V) m_data[ptr + 1]; - m_data[ptr + 1] = null; - return ret; - } - } - } - - @SuppressWarnings("unchecked") - private V insertNullKey(final V value) { - if (m_hasNull) { - final Object ret = m_nullValue; - m_nullValue = value; - return (V) ret; - } else { - m_nullValue = value; - ++m_size; - return null; - } - } - - @SuppressWarnings("unchecked") - private V removeNullKey() { - if (m_hasNull) { - final Object ret = m_nullValue; - m_nullValue = null; - m_hasNull = false; - --m_size; - return (V) ret; - } else { - return null; - } - } - - public int size() { - return m_size; - } - - @SuppressWarnings("unchecked") - private void rehash(final int newCapacity) { - m_threshold = (int) (newCapacity / 2 * m_fillFactor); - m_mask = newCapacity / 2 - 1; - m_mask2 = newCapacity - 1; - - final int oldCapacity = m_data.length; - final Object[] oldData = m_data; - - m_data = new Object[newCapacity]; - Arrays.fill(m_data, FREE_KEY); - - m_size = m_hasNull ? 1 : 0; - - for (int i = 0; i < oldCapacity; i += 2) { - final Object oldKey = oldData[i]; - if (oldKey != FREE_KEY && oldKey != REMOVED_KEY) put((K) oldKey, (V) oldData[i + 1]); - } - } - - public int getStartIndex(final Object key) { - // key is not null here - return key.hashCode() & m_mask; - } - - public Object[] values() { - return m_data; - } - - /** Taken from FastUtil implementation */ - - /** - * Return the least power of two greater than or equal to the specified value. - * - * <p> - * Note that this function will return 1 when the argument is 0. - * - * @param x a long integer smaller than or equal to 2<sup>62</sup>. - * @return the least power of two greater than or equal to the specified value. - */ - public static long nextPowerOfTwo(long x) { - if (x == 0) return 1; - x--; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - return (x | x >> 32) + 1; - } - - /** - * Returns the least power of two smaller than or equal to 2<sup>30</sup> and larger than or equal to - * <code>Math.ceil( expected / f )</code>. - * - * @param expected the expected number of elements in a hash table. - * @param f the load factor. - * @return the minimum possible size for a backing array. - * @throws IllegalArgumentException if the necessary size is larger than 2<sup>30</sup>. - */ - public static int arraySize(final int expected, final float f) { - final long s = Math.max(2, nextPowerOfTwo((long) Math.ceil(expected / f))); - if (s > (1 << 30)) throw new IllegalArgumentException( - "Too large (" + expected + " expected elements with load factor " + f + ")"); - return (int) s; - } - - // taken from FastUtil - private static final int INT_PHI = 0x9E3779B9; - - public static int phiMix(final int x) { - final int h = x * INT_PHI; - return h ^ (h >> 16); - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java deleted file mode 100644 index 275dad9d42..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java +++ /dev/null @@ -1,176 +0,0 @@ -package gtPlusPlus.api.objects.data; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -public class ReverseAutoMap<N> extends AutoMap<N> { - - /** - * The Internal Map - */ - private Map<N, Integer> mInternalMapReverseLookup = new HashMap<N, Integer>(); - - /** - * The Internal ID - */ - private int mInternalID = 0; - - private static final long serialVersionUID = 3771412318075131790L; - - @Override - public Iterator<N> iterator() { - return values().iterator(); - } - - @Override - public synchronized boolean setValue(N object) { - int mOriginalID = this.mInternalID; - put(object); - if (this.mInternalMap.get(mOriginalID).equals(object) || mOriginalID > this.mInternalID) { - return true; - } else { - return false; - } - } - - @Override - public synchronized N put(N object) { - return set(object); - } - - @Override - public synchronized N set(N object) { - int newID = getNextFreeMapID(); - mInternalMapReverseLookup.put(object, newID); - return mInternalMap.put(newID, object); - } - - public synchronized int putToInternalMap(N object) { - return setInternalMap(object); - } - - public synchronized int setInternalMap(N object) { - int newID = getNextFreeMapID(); - mInternalMap.put(newID, object); - mInternalMapReverseLookup.put(object, newID); - return newID; - } - - public synchronized boolean injectCleanDataToAutoMap(Integer g, N object) { - if (!mInternalMap.containsKey(g) && !mInternalMapReverseLookup.containsKey(object)) { - int a1 = 0, a2 = 0, a11 = 0, a22 = 0; - a1 = mInternalMap.size(); - a2 = mInternalMapReverseLookup.size(); - a11 = a1; - a22 = a2; - mInternalMap.put(g, object); - a1 = mInternalMap.size(); - mInternalMapReverseLookup.put(object, g); - a2 = mInternalMapReverseLookup.size(); - if (a1 > a11 && a2 > a22) return true; - } - return false; - } - - public synchronized boolean injectDataToAutoMap(Integer g, N object) { - int a1 = 0, a2 = 0, a11 = 0, a22 = 0; - a1 = mInternalMap.size(); - a2 = mInternalMapReverseLookup.size(); - a11 = a1; - a22 = a2; - mInternalMap.put(g, object); - a1 = mInternalMap.size(); - mInternalMapReverseLookup.put(object, g); - a2 = mInternalMapReverseLookup.size(); - if (a1 > a11 && a2 > a22) return true; - return false; - } - - private boolean raiseInternalID() { - int mOld = mInternalID; - mInternalID++; - return mInternalID > mOld; - } - - public synchronized int getNextFreeMapID() { - if (raiseInternalID()) { - return mInternalID; - } - return Short.MIN_VALUE; - } - - @Override - public synchronized N get(int id) { - return mInternalMap.get(id); - } - - public synchronized int get(N key) { - return mInternalMapReverseLookup.get(key); - } - - @Override - public synchronized Collection<N> values() { - return mInternalMap.values(); - } - - public synchronized Collection<Integer> keys() { - return mInternalMapReverseLookup.values(); - } - - @Override - public synchronized int size() { - return mInternalMap.size(); - } - - @Override - public synchronized int hashCode() { - return mInternalMap.hashCode() + mInternalMapReverseLookup.hashCode(); - } - - @Override - public synchronized boolean containsKey(int key) { - return mInternalMap.containsKey(key); - } - - @Override - public synchronized boolean containsValue(N value) { - return mInternalMap.containsValue(value); - } - - public synchronized boolean containsKey(N key) { - return mInternalMapReverseLookup.containsKey(key); - } - - public synchronized boolean containsValue(int value) { - return mInternalMapReverseLookup.containsValue(value); - } - - @Override - public synchronized boolean isEmpty() { - return mInternalMap.isEmpty() && mInternalMapReverseLookup.isEmpty(); - } - - @Override - public synchronized void clear() { - this.mInternalID = 0; - this.mInternalMap.clear(); - this.mInternalMapReverseLookup.clear(); - return; - } - - @Override - public synchronized N[] toArray() { - Collection<N> col = this.mInternalMap.values(); - @SuppressWarnings("unchecked") - N[] val = (N[]) col.toArray(); - return val; - } - - public synchronized Integer[] toArrayInternalMap() { - Collection<Integer> col = this.mInternalMapReverseLookup.values(); - Integer[] val = col.toArray(new Integer[col.size()]); - return val; - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/DimChunkPos.java b/src/main/java/gtPlusPlus/api/objects/minecraft/DimChunkPos.java deleted file mode 100644 index 6748b58537..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/minecraft/DimChunkPos.java +++ /dev/null @@ -1,50 +0,0 @@ -package gtPlusPlus.api.objects.minecraft; - -import net.minecraft.client.Minecraft; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; - -public class DimChunkPos { - - public final int dimension; - public final int xPos; - public final int zPos; - public final Chunk mainChunk; - - public DimChunkPos(World world, BlockPos block) { - this.dimension = world.provider.dimensionId; - this.mainChunk = world.getChunkFromBlockCoords(block.xPos, block.zPos); - this.xPos = this.mainChunk.xPosition; - this.zPos = this.mainChunk.zPosition; - } - - public DimChunkPos(TileEntity tile) { - this.dimension = tile.getWorldObj().provider.dimensionId; - this.mainChunk = tile.getWorldObj().getChunkFromBlockCoords(tile.xCoord, tile.zCoord); - this.xPos = this.mainChunk.xPosition; - this.zPos = this.mainChunk.zPosition; - } - - public DimChunkPos(int dim, int x, int z) { - this.dimension = dim; - this.xPos = x; - this.zPos = z; - Chunk h = Minecraft.getMinecraft().getIntegratedServer().worldServerForDimension(dim) - .getChunkFromChunkCoords(xPos, zPos); - if (h == null) { - this.mainChunk = null; - } else { - this.mainChunk = h; - } - } - - public Chunk getChunk() { - if (this.mainChunk != null) { - return this.mainChunk; - } - Chunk h = Minecraft.getMinecraft().getIntegratedServer().worldServerForDimension(this.dimension) - .getChunkFromChunkCoords(xPos, zPos); - return h; - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/GenericStack.java b/src/main/java/gtPlusPlus/api/objects/minecraft/GenericStack.java deleted file mode 100644 index b49114e610..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/minecraft/GenericStack.java +++ /dev/null @@ -1,41 +0,0 @@ -package gtPlusPlus.api.objects.minecraft; - -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - -public class GenericStack { - - private ItemStack mItemStack; - private FluidStack mFluidStack; - - public GenericStack(ItemStack s) { - this.mItemStack = s; - this.mFluidStack = null; - } - - public GenericStack(FluidStack f) { - this.mItemStack = null; - this.mFluidStack = f; - } - - public GenericStack() { - this.mItemStack = null; - this.mFluidStack = null; - } - - public final synchronized FluidStack getFluidStack() { - return mFluidStack; - } - - public final synchronized ItemStack getItemStack() { - return mItemStack; - } - - public final synchronized void setItemStack(ItemStack mItemStack) { - this.mItemStack = mItemStack; - } - - public final synchronized void setFluidStack(FluidStack mFluidStack) { - this.mFluidStack = mFluidStack; - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/NoConflictGTRecipeMap.java b/src/main/java/gtPlusPlus/api/objects/minecraft/NoConflictGTRecipeMap.java deleted file mode 100644 index df48c24c06..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/minecraft/NoConflictGTRecipeMap.java +++ /dev/null @@ -1,122 +0,0 @@ -package gtPlusPlus.api.objects.minecraft; - -import java.util.Collection; -import java.util.Iterator; - -import gregtech.api.interfaces.tileentity.IGregTechTileEntity; -import gregtech.api.util.GT_Recipe; -import gtPlusPlus.api.objects.data.AutoMap; - -public class NoConflictGTRecipeMap implements Collection<GT_Recipe> { - - private AutoMap<GT_Recipe> mRecipeCache = new AutoMap<GT_Recipe>(); - private final IGregTechTileEntity mMachineType; - - public NoConflictGTRecipeMap() { - this(null); - } - - public NoConflictGTRecipeMap(IGregTechTileEntity tile0) { - this.mMachineType = tile0; - } - - public boolean put(GT_Recipe recipe) { - return add(recipe); - } - - @Override - public boolean add(GT_Recipe recipe) { - return mRecipeCache.setValue(recipe); - } - - public Collection<GT_Recipe> getRecipeMap() { - return mRecipeCache.values(); - } - - public boolean isMapValidForMachine(IGregTechTileEntity tile) { - return tile == mMachineType; - } - - @Override - public boolean addAll(Collection<? extends GT_Recipe> arg0) { - int a = 0; - for (Object v : arg0) { - if (!this.mRecipeCache.containsValue((GT_Recipe) v)) { - this.mRecipeCache.put((GT_Recipe) v); - a++; - } - } - return a > 0; - } - - @Override - public void clear() { - mRecipeCache.clear(); - } - - @Override - public boolean contains(Object arg0) { - return mRecipeCache.containsValue((GT_Recipe) arg0); - } - - @Override - public boolean containsAll(Collection<?> arg0) { - int a = 0; - for (Object v : arg0) { - if (this.mRecipeCache.containsValue((GT_Recipe) v)) { - a++; - } - } - return a == arg0.size(); - } - - @Override - public boolean isEmpty() { - return mRecipeCache.isEmpty(); - } - - @Override - public Iterator<GT_Recipe> iterator() { - return mRecipeCache.iterator(); - } - - @Override - public boolean remove(Object arg0) { - return mRecipeCache.remove((GT_Recipe) arg0); - } - - @Override - public boolean removeAll(Collection<?> arg0) { - int a = 0; - for (Object v : arg0) { - if (this.mRecipeCache.containsValue((GT_Recipe) v)) { - this.mRecipeCache.remove((GT_Recipe) v); - a++; - } - } - return a > 0; - } - - @Override - public boolean retainAll(Collection<?> arg0) { - int mStartSize = this.mRecipeCache.size(); - this.mRecipeCache = (AutoMap<GT_Recipe>) arg0; - int mEndsize = this.mRecipeCache.size(); - return mStartSize != mEndsize; - } - - @Override - public int size() { - return this.mRecipeCache.size(); - } - - @Override - public Object[] toArray() { - return this.mRecipeCache.toArray(); - } - - @Override - public <T> T[] toArray(T[] arg0) { - return (T[]) this.mRecipeCache.toArray(); - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/TexturePackage.java b/src/main/java/gtPlusPlus/api/objects/minecraft/TexturePackage.java deleted file mode 100644 index 4e4e0e9780..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/minecraft/TexturePackage.java +++ /dev/null @@ -1,54 +0,0 @@ -package gtPlusPlus.api.objects.minecraft; - -import java.util.LinkedHashMap; -import java.util.Set; - -import net.minecraft.util.IIcon; - -import gtPlusPlus.api.objects.data.AutoMap; - -public class TexturePackage { - - private AutoMap<IIcon> mAnimationArray = new AutoMap<IIcon>(); - - public IIcon getFrame(int aFrame) { - if (aFrame < 0 || aFrame >= mAnimationArray.size()) { - return mAnimationArray.get(0); - } - return mAnimationArray.get(aFrame); - } - - public boolean addFrame(IIcon aFrame) { - if (aFrame != null) { - return mAnimationArray.add(aFrame); - } - return false; - } - - public boolean addFrames(AutoMap<IIcon> aFrames) { - for (IIcon h : aFrames) { - if (!addFrame(h)) { - return false; - } - } - return true; - } - - public boolean addFrames(LinkedHashMap<?, IIcon> aFrames) { - for (IIcon h : aFrames.values()) { - if (!addFrame(h)) { - return false; - } - } - return true; - } - - public boolean addFrames(Set<IIcon> aFrames) { - for (IIcon h : aFrames) { - if (!addFrame(h)) { - return false; - } - } - return true; - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/minecraft/ThreadPooCollector.java b/src/main/java/gtPlusPlus/api/objects/minecraft/ThreadPooCollector.java deleted file mode 100644 index f38a960044..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/minecraft/ThreadPooCollector.java +++ /dev/null @@ -1,108 +0,0 @@ -package gtPlusPlus.api.objects.minecraft; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; - -import net.minecraft.entity.passive.EntityAnimal; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; - -import gtPlusPlus.GTplusplus; -import gtPlusPlus.GTplusplus.INIT_PHASE; -import gtPlusPlus.api.objects.data.Pair; -import gtPlusPlus.core.tileentities.machines.TileEntityPooCollector; -import gtPlusPlus.core.util.Utils; - -public class ThreadPooCollector extends Thread { - - public boolean canRun = true; - public boolean isRunning = false; - - private static final long INIT_TIME; - private static long internalTickCounter = 0; - - private static final ThreadPooCollector mThread; - private static final HashMap<String, Pair<BlockPos, TileEntityPooCollector>> mPooCollectors = new LinkedHashMap<String, Pair<BlockPos, TileEntityPooCollector>>(); - - static { - mThread = new ThreadPooCollector(); - INIT_TIME = (System.currentTimeMillis()); - } - - public ThreadPooCollector() { - setName("gtpp.handler.poop"); - start(); - } - - public static ThreadPooCollector getInstance() { - return mThread; - } - - public static void addTask(TileEntityPooCollector aTile) { - BlockPos aTempPos = new BlockPos(aTile); - mPooCollectors.put(aTempPos.getUniqueIdentifier(), new Pair<BlockPos, TileEntityPooCollector>(aTempPos, aTile)); - } - - public static void stopThread() { - mThread.canRun = false; - } - - @Override - public void run() { - - if (!isRunning) { - isRunning = true; - } else { - return; - } - - while (canRun) { - if (mPooCollectors.isEmpty() || GTplusplus.CURRENT_LOAD_PHASE != INIT_PHASE.STARTED) { - continue; - } else { - internalTickCounter = Utils.getTicksFromSeconds( - Utils.getSecondsFromMillis(Utils.getMillisSince(INIT_TIME, System.currentTimeMillis()))); - if (internalTickCounter % 100 == 0) { - for (Pair<BlockPos, TileEntityPooCollector> pair : mPooCollectors.values()) { - if (pair != null) { - BlockPos p = pair.getKey(); - if (p != null) { - if (p.world != null) { - World w = p.world; - if (w == null) { - continue; - } - Chunk c = w.getChunkFromBlockCoords(p.xPos, p.zPos); - if (c != null) { - if (c.isChunkLoaded) { - int startX = p.xPos - 2; - int startY = p.yPos; - int startZ = p.zPos - 2; - int endX = p.xPos + 3; - int endY = p.yPos + 5; - int endZ = p.zPos + 3; - AxisAlignedBB box = AxisAlignedBB - .getBoundingBox(startX, startY, startZ, endX, endY, endZ); - if (box != null) { - @SuppressWarnings("unchecked") - List<EntityAnimal> animals = w - .getEntitiesWithinAABB(EntityAnimal.class, box); - if (animals != null && !animals.isEmpty()) { - pair.getValue().onPostTick(animals); - } - } else { - continue; - } - } - } - } - } - } - } - } - } - } - } -} diff --git a/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java b/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java deleted file mode 100644 index 07c8c3609b..0000000000 --- a/src/main/java/gtPlusPlus/api/objects/random/CSPRNG_DO_NOT_USE.java +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright 2005, Nick Galbreath -- nickg [at] modp [dot] com All rights reserved. Redistribution and use in source and - * binary forms, with or without modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of - * the modp.com nor the names of its contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. This is the standard "new" BSD license: - * http://www.opensource.org/licenses/bsd-license.php - */ - -package gtPlusPlus.api.objects.random; - -import java.math.BigInteger; -import java.security.SecureRandom; -import java.util.Random; - -import gtPlusPlus.api.interfaces.IRandomGenerator; -import gtPlusPlus.core.util.Utils; - -/** - * The Blum-Blum-Shub random number generator. - * - * <p> - * The Blum-Blum-Shub is a "cryptographically secure" random number generator. It has been proven that predicting the - * ouput is equivalent to factoring <i>n</i>, a large integer generated from two prime numbers. - * </p> - * - * <p> - * The Algorithm: - * </p> - * <ol> - * <li>(setup) generate two secret prime numbers <i>p</i>, <i>q</i> such that <i>p</i> ≠ <i>q</i>, <i>p</i> ≡ 3 - * mod 4, <i>q</i> ≡ 3 mod 4.</li> - * <li>(setup) compute <i>n</i> = <i>pq</i>. <i>n</i> can be re-used, but <i>p</i>, and <i>q</i> are secret and should - * be disposed of.</li> - * <li>Generate a (secure) random seed <i>s</i> in the range [1, <i>n</i> -1] such that gcd(<i>s</i>, <i>n</i>) = 1. - * <li>Compute <i>x</i> = <i>s</i><sup>2</sup> mod <i>n</i></li> - * <li>Compute a single random bit with: - * <ol> - * <li><i>x</i> = <i>x</i><sup>2</sup> mod <i>n</i></li> - * <li>return Least-Significant-Bit(<i>x</i>) (i.e. <i>x</i> & 1)</li> - * </ol> - * Repeat as necessary.</li> - * </ol> - * - * <p> - * The code originally appeared in <a href="http://modp.com/cida/"><i>Cryptography for Internet and Database - * Applications </i>, Chapter 4, pages 174-177</a> - * </p> - * <p> - * More details are in the <a href="http://www.cacr.math.uwaterloo.ca/hac/"><i>Handbook of Applied Cryptography</i></a>, - * <a href="http://www.cacr.math.uwaterloo.ca/hac/about/chap5.pdf">Section 5.5.2</a> - * </p> - * - * @author Nick Galbreath -- nickg [at] modp [dot] com - * @version 3 -- 06-Jul-2005 - * - */ -public class CSPRNG_DO_NOT_USE extends Random implements IRandomGenerator { - - // pre-compute a few values - private static final BigInteger two = BigInteger.valueOf(2L); - - private static final BigInteger three = BigInteger.valueOf(3L); - - private static final BigInteger four = BigInteger.valueOf(4L); - - /** - * main parameter - */ - private BigInteger n; - - private BigInteger state; - - /** - * Generate appropriate prime number for use in Blum-Blum-Shub. - * - * This generates the appropriate primes (p = 3 mod 4) needed to compute the "n-value" for Blum-Blum-Shub. - * - * @param bits Number of bits in prime - * @param rand A source of randomness - */ - private static BigInteger getPrime(int bits, Random rand) { - BigInteger p; - while (true) { - p = new BigInteger(bits, 100, rand); - if (p.mod(four).equals(three)) break; - } - return p; - } - - /** - * This generates the "n value" -- the multiplication of two equally sized random prime numbers -- for use in the - * Blum-Blum-Shub algorithm. - * - * @param bits The number of bits of security - * @param rand A random instance to aid in generating primes - * @return A BigInteger, the <i>n</i>. - */ - public static BigInteger g |
