Fix missing files

This commit is contained in:
MrHua269
2026-07-09 15:57:19 +08:00
parent 0e56eddb8a
commit 717756b5b0
4 changed files with 877 additions and 0 deletions
@@ -0,0 +1,95 @@
package su.plo.matter;
import com.google.common.collect.Iterables;
import me.earthme.luminol.config.modules.function.SecureSeedConfig;
import net.minecraft.server.level.ServerLevel;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Optional;
public class Globals {
public static final int WORLD_SEED_LONGS = 16;
public static final int WORLD_SEED_BITS = WORLD_SEED_LONGS * 64;
public static final long[] worldSeed = new long[WORLD_SEED_LONGS];
public static final ThreadLocal<Integer> dimension = ThreadLocal.withInitial(() -> 0);
public enum Salt {
UNDEFINED,
BASTION_FEATURE,
WOODLAND_MANSION_FEATURE,
MINESHAFT_FEATURE,
BURIED_TREASURE_FEATURE,
NETHER_FORTRESS_FEATURE,
PILLAGER_OUTPOST_FEATURE,
GEODE_FEATURE,
NETHER_FOSSIL_FEATURE,
OCEAN_MONUMENT_FEATURE,
RUINED_PORTAL_FEATURE,
POTENTIONAL_FEATURE,
GENERATE_FEATURE,
JIGSAW_PLACEMENT,
STRONGHOLDS,
POPULATION,
DECORATION,
SLIME_CHUNK
}
public static void setupGlobals(ServerLevel world) {
if (!SecureSeedConfig.enabled) return;
long[] seed = world.worldGenSettings.options().featureSeed();
System.arraycopy(seed, 0, worldSeed, 0, WORLD_SEED_LONGS);
int worldIndex = Iterables.indexOf(world.getServer().levelKeys(), it -> it == world.dimension());
if (worldIndex == -1)
worldIndex = world.getServer().levelKeys().size(); // if we are in world construction it may not have been added to the map yet
dimension.set(worldIndex);
}
public static long[] createRandomWorldSeed() {
long[] seed = new long[WORLD_SEED_LONGS];
SecureRandom rand = new SecureRandom();
for (int i = 0; i < WORLD_SEED_LONGS; i++) {
seed[i] = rand.nextLong();
}
return seed;
}
// 1024-bit string -> 16 * 64 long[]
public static Optional<long[]> parseSeed(String seedStr) {
if (seedStr.isEmpty()) return Optional.empty();
if (seedStr.length() != WORLD_SEED_BITS) {
throw new IllegalArgumentException("Secure seed length must be " + WORLD_SEED_BITS + "-bit but found " + seedStr.length() + "-bit.");
}
long[] seed = new long[WORLD_SEED_LONGS];
for (int i = 0; i < WORLD_SEED_LONGS; i++) {
int start = i * 64;
int end = start + 64;
String seedSection = seedStr.substring(start, end);
BigInteger seedInDecimal = new BigInteger(seedSection, 2);
seed[i] = seedInDecimal.longValue();
}
return Optional.of(seed);
}
// 16 * 64 long[] -> 1024-bit string
public static String seedToString(long[] seed) {
StringBuilder sb = new StringBuilder();
for (long longV : seed) {
// Convert to 64-bit binary string per long
// Use format to keep 64-bit length, and use 0 to complete space
String binaryStr = String.format("%64s", Long.toBinaryString(longV)).replace(' ', '0');
sb.append(binaryStr);
}
return sb.toString();
}
}
@@ -0,0 +1,73 @@
package su.plo.matter;
public class Hashing {
// https://en.wikipedia.org/wiki/BLAKE_(hash_function)
// https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/crypto/digests/Blake2bDigest.java
private final static long[] blake2b_IV = {
0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL, 0x3c6ef372fe94f82bL,
0xa54ff53a5f1d36f1L, 0x510e527fade682d1L, 0x9b05688c2b3e6c1fL,
0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L
};
private final static byte[][] blake2b_sigma = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}
};
public static long[] hashWorldSeed(long[] worldSeed) {
long[] result = blake2b_IV.clone();
result[0] ^= 0x01010040;
hash(worldSeed, result, new long[16], 0, false);
return result;
}
public static void hash(long[] message, long[] chainValue, long[] internalState, long messageOffset, boolean isFinal) {
assert message.length == 16;
assert chainValue.length == 8;
assert internalState.length == 16;
System.arraycopy(chainValue, 0, internalState, 0, chainValue.length);
System.arraycopy(blake2b_IV, 0, internalState, chainValue.length, 4);
internalState[12] = messageOffset ^ blake2b_IV[4];
internalState[13] = blake2b_IV[5];
if (isFinal) internalState[14] = ~blake2b_IV[6];
internalState[15] = blake2b_IV[7];
for (int round = 0; round < 12; round++) {
G(message[blake2b_sigma[round][0]], message[blake2b_sigma[round][1]], 0, 4, 8, 12, internalState);
G(message[blake2b_sigma[round][2]], message[blake2b_sigma[round][3]], 1, 5, 9, 13, internalState);
G(message[blake2b_sigma[round][4]], message[blake2b_sigma[round][5]], 2, 6, 10, 14, internalState);
G(message[blake2b_sigma[round][6]], message[blake2b_sigma[round][7]], 3, 7, 11, 15, internalState);
G(message[blake2b_sigma[round][8]], message[blake2b_sigma[round][9]], 0, 5, 10, 15, internalState);
G(message[blake2b_sigma[round][10]], message[blake2b_sigma[round][11]], 1, 6, 11, 12, internalState);
G(message[blake2b_sigma[round][12]], message[blake2b_sigma[round][13]], 2, 7, 8, 13, internalState);
G(message[blake2b_sigma[round][14]], message[blake2b_sigma[round][15]], 3, 4, 9, 14, internalState);
}
for (int i = 0; i < 8; i++) {
chainValue[i] ^= internalState[i] ^ internalState[i + 8];
}
}
private static void G(long m1, long m2, int posA, int posB, int posC, int posD, long[] internalState) {
internalState[posA] = internalState[posA] + internalState[posB] + m1;
internalState[posD] = Long.rotateRight(internalState[posD] ^ internalState[posA], 32);
internalState[posC] = internalState[posC] + internalState[posD];
internalState[posB] = Long.rotateRight(internalState[posB] ^ internalState[posC], 24); // replaces 25 of BLAKE
internalState[posA] = internalState[posA] + internalState[posB] + m2;
internalState[posD] = Long.rotateRight(internalState[posD] ^ internalState[posA], 16);
internalState[posC] = internalState[posC] + internalState[posD];
internalState[posB] = Long.rotateRight(internalState[posB] ^ internalState[posC], 63); // replaces 11 of BLAKE
}
}
@@ -0,0 +1,519 @@
package su.plo.matter;
import me.earthme.luminol.config.modules.function.SecureSeedConfig;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
public class HashingV2 {
private static final int BLOCK_LEN = 64;
private static final int CHUNK_LEN = 1024;
private static final int OUT_LEN = 32;
private static final int KEY_LEN = 32;
private static final int CHUNK_START = 1 << 0;
private static final int CHUNK_END = 1 << 1;
private static final int PARENT = 1 << 2;
private static final int ROOT = 1 << 3;
private static final int KEYED_HASH = 1 << 4;
private static final int DERIVE_KEY_CONTEXT = 1 << 5;
private static final int DERIVE_KEY_MATERIAL = 1 << 6;
private static final int[] IV = {
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
};
private static final int[] MSG_PERMUTATION = {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8};
private static final ThreadLocal<SaltHolder> saltHolderLocal = ThreadLocal.withInitial(() -> {
final SaltHolder newHolder = new SaltHolder();
newHolder.init(SecureSeedConfig.salt);
return newHolder;
});
private static final ThreadLocal<byte[]> threadBuffer = ThreadLocal.withInitial(() -> new byte[128]);
private static final ThreadLocal<ByteBuffer> threadByteBuffer = ThreadLocal.withInitial(() -> ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN));
private static int rotr32(int x, int k) {
return ((x >>> k) | (x << (32 - k))) >>> 0;
}
private static void g(int[] state, int a, int b, int c, int d, int mx, int my) {
state[a] = (state[a] + state[b] + mx) >>> 0;
state[d] = rotr32(state[d] ^ state[a], 16);
state[c] = (state[c] + state[d]) >>> 0;
state[b] = rotr32(state[b] ^ state[c], 12);
state[a] = (state[a] + state[b] + my) >>> 0;
state[d] = rotr32(state[d] ^ state[a], 8);
state[c] = (state[c] + state[d]) >>> 0;
state[b] = rotr32(state[b] ^ state[c], 7);
}
private static void round(int[] state, int[] m) {
g(state, 0, 4, 8, 12, m[0], m[1]);
g(state, 1, 5, 9, 13, m[2], m[3]);
g(state, 2, 6, 10, 14, m[4], m[5]);
g(state, 3, 7, 11, 15, m[6], m[7]);
g(state, 0, 5, 10, 15, m[8], m[9]);
g(state, 1, 6, 11, 12, m[10], m[11]);
g(state, 2, 7, 8, 13, m[12], m[13]);
g(state, 3, 4, 9, 14, m[14], m[15]);
}
private static int[] permute(int[] m) {
int[] permuted = new int[16];
for (int i = 0; i < 16; i++) {
permuted[i] = m[MSG_PERMUTATION[i]];
}
return permuted;
}
private static int[] compress(int[] chainingValue, int[] blockWords, long counter, int blockLen, int flags) {
int counterLow = (int) (counter >>> 0);
int counterHigh = (int) ((counter / 0x100000000L) >>> 0);
int[] state = new int[16];
System.arraycopy(chainingValue, 0, state, 0, 8);
state[8] = IV[0];
state[9] = IV[1];
state[10] = IV[2];
state[11] = IV[3];
state[12] = counterLow;
state[13] = counterHigh;
state[14] = blockLen;
state[15] = flags;
int[] block = Arrays.copyOf(blockWords, 16);
for (int i = 0; i < 7; i++) {
round(state, block);
block = permute(block);
}
int[] output = new int[16];
for (int i = 0; i < 8; i++) {
output[i] = state[i] ^ state[i + 8];
output[i + 8] = state[i + 8] ^ chainingValue[i];
}
return output;
}
private static int[] first8Words(int[] compressionOutput) {
return Arrays.copyOfRange(compressionOutput, 0, 8);
}
private static int[] wordsFromLittleEndianBytes(byte[] bytes) {
int[] words = new int[bytes.length / 4];
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < words.length; i++) {
words[i] = buffer.getInt(i * 4);
}
return words;
}
private static byte[] wordsToLittleEndianBytes(int[] words, int outputLen) {
byte[] output = new byte[outputLen];
ByteBuffer buffer = ByteBuffer.wrap(output).order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < words.length && i * 4 < outputLen; i++) {
buffer.putInt(i * 4, words[i]);
}
return output;
}
private static class ChunkState {
int[] chainingValue;
long chunkCounter;
byte[] block;
int blockLen;
int blocksCompressed;
int flags;
ChunkState(int[] keyWords, long chunkCounter, int flags) {
this.chainingValue = keyWords.clone();
this.chunkCounter = chunkCounter;
this.block = new byte[BLOCK_LEN];
this.blockLen = 0;
this.blocksCompressed = 0;
this.flags = flags;
}
int len() {
return BLOCK_LEN * this.blocksCompressed + this.blockLen;
}
int startFlag() {
return this.blocksCompressed == 0 ? CHUNK_START : 0;
}
void update(byte[] input) {
int offset = 0;
while (offset < input.length) {
if (this.blockLen == BLOCK_LEN) {
int[] blockWords = wordsFromLittleEndianBytes(this.block);
this.chainingValue = first8Words(compress(
this.chainingValue,
blockWords,
this.chunkCounter,
BLOCK_LEN,
this.flags | this.startFlag()
));
this.blocksCompressed++;
this.block = new byte[BLOCK_LEN];
this.blockLen = 0;
}
int want = BLOCK_LEN - this.blockLen;
int take = Math.min(want, input.length - offset);
System.arraycopy(input, offset, this.block, this.blockLen, take);
this.blockLen += take;
offset += take;
}
}
int[] outputBlockWords() {
int[] blockWords = new int[16];
for (int i = 0; i < 16 && i * 4 < this.blockLen; i++) {
blockWords[i] = ((this.block[i * 4 + 0] & 0xFF)) |
((this.block[i * 4 + 1] & 0xFF) << 8) |
((this.block[i * 4 + 2] & 0xFF) << 16) |
((this.block[i * 4 + 3] & 0xFF) << 24);
}
return blockWords;
}
Output output() {
return new Output(
this.chainingValue,
outputBlockWords(),
this.chunkCounter,
this.blockLen,
this.flags | this.startFlag() | CHUNK_END
);
}
}
private static class Output {
int[] inputChainingValue;
int[] blockWords;
long counter;
int blockLen;
int flags;
Output(int[] inputChainingValue, int[] blockWords, long counter, int blockLen, int flags) {
this.inputChainingValue = inputChainingValue;
this.blockWords = blockWords;
this.counter = counter;
this.blockLen = blockLen;
this.flags = flags;
}
int[] chainingValue() {
return first8Words(compress(
this.inputChainingValue,
this.blockWords,
this.counter,
this.blockLen,
this.flags
));
}
byte[] rootOutputBytes(int outLen) {
byte[] output = new byte[outLen];
long outputBlockCounter = 0;
for (int offset = 0; offset < outLen; offset += 2 * OUT_LEN) {
int[] words = compress(
this.inputChainingValue,
this.blockWords,
outputBlockCounter,
this.blockLen,
this.flags | ROOT
);
byte[] blockOutput = wordsToLittleEndianBytes(words, 64);
int remaining = outLen - offset;
int copyLen = Math.min(remaining, 64);
System.arraycopy(blockOutput, 0, output, offset, copyLen);
outputBlockCounter++;
}
return output;
}
}
private static Output parentOutput(int[] leftChildCv, int[] rightChildCv, int[] keyWords, int flags) {
int[] blockWords = new int[16];
System.arraycopy(leftChildCv, 0, blockWords, 0, 8);
System.arraycopy(rightChildCv, 0, blockWords, 8, 8);
return new Output(
keyWords,
blockWords,
0,
BLOCK_LEN,
PARENT | flags
);
}
private static int[] parentCv(int[] leftChildCv, int[] rightChildCv, int[] keyWords, int flags) {
return parentOutput(leftChildCv, rightChildCv, keyWords, flags).chainingValue();
}
private static class SaltHolder {
private long[] cachedSaltHash = null;
private String lastSalt = null;
public void init(String currentSalt) {
if (cachedSaltHash == null || !currentSalt.equals(lastSalt)) {
byte[] saltBytes = currentSalt.getBytes();
byte[] hashBytes = blake3(saltBytes);
int[] hashInts = bytesToIntsLittleEndian(hashBytes);
cachedSaltHash = new long[8];
for (int i = 0; i < 8; i++) {
cachedSaltHash[i] = (hashInts[i] & 0xFFFFFFFFL);
}
lastSalt = currentSalt;
}
}
}
private static class Hasher {
ChunkState chunkState;
int[] keyWords;
java.util.ArrayList<int[]> cvStack;
int flags;
Hasher(int[] keyWords, int flags) {
this.chunkState = new ChunkState(keyWords, 0, flags);
this.keyWords = keyWords.clone();
this.cvStack = new java.util.ArrayList<>();
this.flags = flags;
}
static Hasher createNew() {
int[] iv = {IV[0], IV[1], IV[2], IV[3], IV[4], IV[5], IV[6], IV[7]};
return new Hasher(iv, 0);
}
static Hasher createKeyed(byte[] key) {
if (key.length != KEY_LEN) {
throw new IllegalArgumentException("Key must be " + KEY_LEN + " bytes");
}
int[] keyWords = wordsFromLittleEndianBytes(key);
return new Hasher(keyWords, KEYED_HASH);
}
void pushStack(int[] cv) {
cvStack.add(cv);
}
int[] popStack() {
return cvStack.remove(cvStack.size() - 1);
}
void addChunkChainingValue(int[] newCv, long totalChunks) {
int[] cv = newCv;
long chunks = totalChunks;
while ((chunks & 1) == 0) {
cv = parentCv(popStack(), cv, this.keyWords, this.flags);
chunks >>>= 1;
}
pushStack(cv);
}
void update(byte[] input) {
int offset = 0;
while (offset < input.length) {
if (this.chunkState.len() == CHUNK_LEN) {
int[] chunkCv = this.chunkState.output().chainingValue();
long totalChunks = this.chunkState.chunkCounter + 1;
addChunkChainingValue(chunkCv, totalChunks);
this.chunkState = new ChunkState(this.keyWords, totalChunks, this.flags);
}
int want = CHUNK_LEN - this.chunkState.len();
int take = Math.min(want, input.length - offset);
byte[] chunk = new byte[take];
System.arraycopy(input, offset, chunk, 0, take);
this.chunkState.update(chunk);
offset += take;
}
}
byte[] finalize(int outLen) {
Output output = this.chunkState.output();
int parentNodesRemaining = this.cvStack.size();
while (parentNodesRemaining > 0) {
parentNodesRemaining--;
output = parentOutput(
this.cvStack.get(parentNodesRemaining),
output.chainingValue(),
this.keyWords,
this.flags
);
}
return output.rootOutputBytes(outLen);
}
}
private static byte[] blake3(byte[] data) {
Hasher hasher = Hasher.createNew();
hasher.update(data);
return hasher.finalize(OUT_LEN);
}
private static byte[] blake3Keyed(byte[] data, byte[] key) {
Hasher hasher = Hasher.createKeyed(key);
hasher.update(data);
return hasher.finalize(OUT_LEN);
}
private static byte[] blake3Keyed(byte[] data, byte[] key, int outLen) {
Hasher hasher = Hasher.createKeyed(key);
hasher.update(data);
return hasher.finalize(outLen);
}
private static int[] bytesToIntsLittleEndian(byte[] bytes) {
int[] ints = new int[bytes.length / 4];
for (int i = 0; i < ints.length; i++) {
ints[i] = ((bytes[i * 4 + 0] & 0xFF)) |
((bytes[i * 4 + 1] & 0xFF) << 8) |
((bytes[i * 4 + 2] & 0xFF) << 16) |
((bytes[i * 4 + 3] & 0xFF) << 24);
}
return ints;
}
private static int[] blake3Ints(byte[] data, int outLen) {
byte[] result = blake3(data);
if (outLen <= 32) {
return bytesToIntsLittleEndian(Arrays.copyOf(result, outLen));
}
return bytesToIntsLittleEndian(result);
}
private static long[] getSaltHash() {
final SaltHolder currHolder = saltHolderLocal.get();
return currHolder.cachedSaltHash;
}
private static long[] hashWorldSeedInternal(long[] worldSeed) {
byte[] seedBytes = new byte[worldSeed.length * 8];
ByteBuffer buffer = ByteBuffer.wrap(seedBytes).order(ByteOrder.LITTLE_ENDIAN);
for (long l : worldSeed) {
buffer.putLong(l);
}
byte[] hashBytes = blake3(seedBytes);
int[] hashInts = bytesToIntsLittleEndian(hashBytes);
long[] result = new long[8];
for (int i = 0; i < 8; i++) {
result[i] = hashInts[i] & 0xFFFFFFFFL;
}
return result;
}
public static long[] hashWorldSeed(long[] worldSeed) {
if (!SecureSeedConfig.enabled && SecureSeedConfig.version == 2) {
return worldSeed.clone();
}
long[] saltHashValue = getSaltHash();
long[] saltedSeed = new long[worldSeed.length];
for (int i = 0; i < worldSeed.length; i++) {
saltedSeed[i] = worldSeed[i] ^ saltHashValue[i % saltHashValue.length];
}
return hashWorldSeedInternal(saltedSeed);
}
public static long[] expandLevelSeedTo1024Bits(long levelSeed) {
if (!SecureSeedConfig.enabled && SecureSeedConfig.version == 2) {
long[] result = new long[Globals.WORLD_SEED_LONGS];
for (int i = 0; i < Globals.WORLD_SEED_LONGS; i++) {
result[i] = levelSeed ^ (i * 0x9E3779B97F4A7C15L);
}
return result;
}
String salt = SecureSeedConfig.salt;
byte[] saltBytes = salt.getBytes(java.nio.charset.StandardCharsets.UTF_8);
byte[] saltKey = blake3(saltBytes);
long[] result = new long[Globals.WORLD_SEED_LONGS];
for (int segment = 0; segment < Globals.WORLD_SEED_LONGS; segment++) {
byte[] input = new byte[16];
ByteBuffer buf = ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN);
buf.putLong(0, levelSeed);
buf.putLong(8, segment);
byte[] hash = blake3Keyed(input, saltKey, 32);
int[] hashInts = bytesToIntsLittleEndian(hash);
result[segment] = ((long) hashInts[0] & 0xFFFFFFFFL) |
(((long) hashInts[1] & 0xFFFFFFFFL) << 32);
}
return result;
}
public static long getTerrainSeed(long[] hashedSeed, TerrainType type) {
return hashedSeed[type.ordinal() % hashedSeed.length];
}
public enum TerrainType {
BASE_TERRAIN,
BIOME_NOISE,
CLIMATE,
AQUIFER,
ORE,
SURFACE,
VEGETATION,
SHIFT
}
public static void hash(long[] message, long[] output, long[] state, int outputBytes, boolean finalBlock) {
int len = message.length * 8;
byte[] buffer = threadBuffer.get();
if (buffer.length < len) {
buffer = new byte[len];
threadBuffer.set(buffer);
}
ByteBuffer buf = threadByteBuffer.get();
if (buf.capacity() < len) {
buf = ByteBuffer.allocate(len).order(ByteOrder.LITTLE_ENDIAN);
threadByteBuffer.set(buf);
}
buf.clear();
for (long l : message) {
buf.putLong(l);
}
byte[] hashBytes = blake3(Arrays.copyOf(buffer, len));
int[] hashInts = bytesToIntsLittleEndian(hashBytes);
for (int i = 0; i < Math.min(hashInts.length, output.length); i++) {
output[i] = hashInts[i] & 0xFFFFFFFFL;
}
}
}
@@ -0,0 +1,190 @@
package su.plo.matter;
import me.earthme.luminol.config.modules.function.SecureSeedConfig;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.LegacyRandomSource;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class WorldgenCryptoRandom extends WorldgenRandom {
// hash the world seed to guard against badly chosen world seeds
private static final long[] HASHED_ZERO_SEED = Hashing.hashWorldSeed(new long[Globals.WORLD_SEED_LONGS]);
private static final ThreadLocal<long[]> LAST_SEEN_WORLD_SEED = ThreadLocal.withInitial(() -> new long[Globals.WORLD_SEED_LONGS]);
private static final ThreadLocal<long[]> HASHED_WORLD_SEED = ThreadLocal.withInitial(() -> HASHED_ZERO_SEED);
private final long[] worldSeed = new long[Globals.WORLD_SEED_LONGS];
private final long[] randomBits = new long[8];
private int randomBitIndex;
private static final int MAX_RANDOM_BIT_INDEX = 64 * 8;
private static final int LOG2_MAX_RANDOM_BIT_INDEX = 9;
private long counter;
private final long[] message = new long[16];
private final long[] cachedInternalState = new long[16];
public WorldgenCryptoRandom(int x, int z, Globals.Salt typeSalt, long salt) {
super(new LegacyRandomSource(0L));
if (typeSalt == null) {
return;
}
if (SecureSeedConfig.enabled) {
this.setSecureSeed(x, z, typeSalt, salt);
} else {
super.setSeed(((long) x << 32) | ((long) z & 0xffffffffL) ^ salt);
}
}
public void setSecureSeed(int x, int z, Globals.Salt typeSalt, long salt) {
if (!SecureSeedConfig.enabled) {
super.setSeed(((long) x << 32) | ((long) z & 0xffffffffL) ^ salt);
return;
}
System.arraycopy(Globals.worldSeed, 0, this.worldSeed, 0, Globals.WORLD_SEED_LONGS);
message[0] = ((long) x << 32) | ((long) z & 0xffffffffL);
message[1] = ((long) Globals.dimension.get() << 32) | ((long) salt & 0xffffffffL);
message[2] = typeSalt.ordinal();
message[3] = counter = 0;
randomBitIndex = MAX_RANDOM_BIT_INDEX;
}
private long[] getHashedWorldSeed() {
if (!Arrays.equals(worldSeed, LAST_SEEN_WORLD_SEED.get())) {
HASHED_WORLD_SEED.set(Hashing.hashWorldSeed(worldSeed));
System.arraycopy(worldSeed, 0, LAST_SEEN_WORLD_SEED.get(), 0, Globals.WORLD_SEED_LONGS);
}
return HASHED_WORLD_SEED.get();
}
private void moreRandomBits() {
message[3] = counter++;
System.arraycopy(getHashedWorldSeed(), 0, randomBits, 0, 8);
Hashing.hash(message, randomBits, cachedInternalState, 64, true);
}
private long getBits(int count) {
if (randomBitIndex >= MAX_RANDOM_BIT_INDEX) {
moreRandomBits();
randomBitIndex -= MAX_RANDOM_BIT_INDEX;
}
int alignment = randomBitIndex & 63;
if ((randomBitIndex >>> 6) == ((randomBitIndex + count) >>> 6)) {
long result = (randomBits[randomBitIndex >>> 6] >>> alignment) & ((1L << count) - 1);
randomBitIndex += count;
return result;
} else {
long result = (randomBits[randomBitIndex >>> 6] >>> alignment) & ((1L << (64 - alignment)) - 1);
randomBitIndex += count;
if (randomBitIndex >= MAX_RANDOM_BIT_INDEX) {
moreRandomBits();
randomBitIndex -= MAX_RANDOM_BIT_INDEX;
}
alignment = randomBitIndex & 63;
result <<= alignment;
result |= (randomBits[randomBitIndex >>> 6] >>> (64 - alignment)) & ((1L << alignment) - 1);
return result;
}
}
@Override
public @NotNull RandomSource fork() {
if (!SecureSeedConfig.enabled) {
return super.fork();
}
WorldgenCryptoRandom fork = new WorldgenCryptoRandom(0, 0, null, 0);
System.arraycopy(Globals.worldSeed, 0, fork.worldSeed, 0, Globals.WORLD_SEED_LONGS);
fork.message[0] = this.message[0];
fork.message[1] = this.message[1];
fork.message[2] = this.message[2];
fork.message[3] = this.message[3];
fork.randomBitIndex = this.randomBitIndex;
fork.counter = this.counter;
fork.nextLong();
return fork;
}
@Override
public int next(int bits) {
return SecureSeedConfig.enabled ? (int) getBits(bits) : super.next(bits);
}
@Override
public void consumeCount(int count) {
if (!SecureSeedConfig.enabled) {
return;
}
randomBitIndex += count;
if (randomBitIndex >= MAX_RANDOM_BIT_INDEX * 2) {
randomBitIndex -= MAX_RANDOM_BIT_INDEX;
counter += randomBitIndex >>> LOG2_MAX_RANDOM_BIT_INDEX;
randomBitIndex &= MAX_RANDOM_BIT_INDEX - 1;
randomBitIndex += MAX_RANDOM_BIT_INDEX;
}
}
@Override
public int nextInt(int bound) {
if (!SecureSeedConfig.enabled) {
return super.nextInt(bound);
}
int bits = Mth.ceillog2(bound);
int result;
do {
result = (int) getBits(bits);
} while (result >= bound);
return result;
}
@Override
public long nextLong() {
return SecureSeedConfig.enabled ? getBits(64) : super.nextLong();
}
@Override
public double nextDouble() {
return SecureSeedConfig.enabled ? (getBits(53) * 0x1.0p-53) : super.nextDouble();
}
@Override
public long setDecorationSeed(long worldSeed, int blockX, int blockZ) {
if (!SecureSeedConfig.enabled) {
return super.setDecorationSeed(worldSeed, blockX, blockZ);
}
setSecureSeed(blockX, blockZ, Globals.Salt.POPULATION, 0);
return ((long) blockX << 32) | ((long) blockZ & 0xffffffffL);
}
@Override
public void setFeatureSeed(long populationSeed, int index, int step) {
if (!SecureSeedConfig.enabled) {
super.setFeatureSeed(populationSeed, index, step);
return;
}
setSecureSeed((int) (populationSeed >> 32), (int) populationSeed, Globals.Salt.DECORATION, index + 10000L * step);
}
@Override
public void setLargeFeatureSeed(long worldSeed, int chunkX, int chunkZ) {
super.setLargeFeatureSeed(worldSeed, chunkX, chunkZ);
}
@Override
public void setLargeFeatureWithSalt(long worldSeed, int regionX, int regionZ, int salt) {
super.setLargeFeatureWithSalt(worldSeed, regionX, regionZ, salt);
}
public static RandomSource seedSlimeChunk(int chunkX, int chunkZ) {
return new WorldgenCryptoRandom(chunkX, chunkZ, Globals.Salt.SLIME_CHUNK, 0);
}
}