From 7882d6e0544344eff7ebfa44f468a4c6d3ed21fd Mon Sep 17 00:00:00 2001 From: dEajL3kA Date: Mon, 13 Nov 2023 13:00:12 +0100 Subject: [PATCH] Increased output block size from 64 to 96 bytes; now using 8 counter values, instead of just 6. --- build.xml | 21 +++++--- etc/io/github/deajl3ka/fast_key_erasure/64.txt | 18 +++++++ .../deajl3ka/fast_key_erasure/GenerateCounter.java | 59 +++++++++++----------- .../fast_key_erasure/FastKeyErasureRNG.java | 16 +++--- src/io/github/deajl3ka/fast_key_erasure/Main.java | 2 + .../fast_key_erasure/FastKeyErasureRNGTest.java | 26 +++++----- 6 files changed, 87 insertions(+), 55 deletions(-) create mode 100644 etc/io/github/deajl3ka/fast_key_erasure/64.txt diff --git a/build.xml b/build.xml index 8ef4abe..13ff472 100644 --- a/build.xml +++ b/build.xml @@ -1,5 +1,5 @@ - + @@ -65,10 +65,11 @@ - + +
@@ -81,10 +82,11 @@ - + +
@@ -97,11 +99,18 @@ - - - + + + + +
+ + + +
+
diff --git a/etc/io/github/deajl3ka/fast_key_erasure/64.txt b/etc/io/github/deajl3ka/fast_key_erasure/64.txt new file mode 100644 index 0000000..79d7928 --- /dev/null +++ b/etc/io/github/deajl3ka/fast_key_erasure/64.txt @@ -0,0 +1,18 @@ +[64] +431e1216e050b5bc906d06c37eae8ded +a4392f4508724863d257e7174895b841 +365108a319e61f18ab0d318d29c9353a +4923293ac4bd4b2a20fb94b21b7564f2 +fbac80d4351784d84e451b7f45eb5264 +3efacd9ebe74e025fd0127f10614e396 +97a451236bc67d67c4f69a5087ea040f +d076fe6930b82a9518740cafe1016b58 + +{ (byte)0x43, (byte)0x1E, (byte)0x12, (byte)0x16, (byte)0xE0, (byte)0x50, (byte)0xB5, (byte)0xBC, (byte)0x90, (byte)0x6D, (byte)0x06, (byte)0xC3, (byte)0x7E, (byte)0xAE, (byte)0x8D, (byte)0xED } +{ (byte)0xA4, (byte)0x39, (byte)0x2F, (byte)0x45, (byte)0x08, (byte)0x72, (byte)0x48, (byte)0x63, (byte)0xD2, (byte)0x57, (byte)0xE7, (byte)0x17, (byte)0x48, (byte)0x95, (byte)0xB8, (byte)0x41 } +{ (byte)0x36, (byte)0x51, (byte)0x08, (byte)0xA3, (byte)0x19, (byte)0xE6, (byte)0x1F, (byte)0x18, (byte)0xAB, (byte)0x0D, (byte)0x31, (byte)0x8D, (byte)0x29, (byte)0xC9, (byte)0x35, (byte)0x3A } +{ (byte)0x49, (byte)0x23, (byte)0x29, (byte)0x3A, (byte)0xC4, (byte)0xBD, (byte)0x4B, (byte)0x2A, (byte)0x20, (byte)0xFB, (byte)0x94, (byte)0xB2, (byte)0x1B, (byte)0x75, (byte)0x64, (byte)0xF2 } +{ (byte)0xFB, (byte)0xAC, (byte)0x80, (byte)0xD4, (byte)0x35, (byte)0x17, (byte)0x84, (byte)0xD8, (byte)0x4E, (byte)0x45, (byte)0x1B, (byte)0x7F, (byte)0x45, (byte)0xEB, (byte)0x52, (byte)0x64 } +{ (byte)0x3E, (byte)0xFA, (byte)0xCD, (byte)0x9E, (byte)0xBE, (byte)0x74, (byte)0xE0, (byte)0x25, (byte)0xFD, (byte)0x01, (byte)0x27, (byte)0xF1, (byte)0x06, (byte)0x14, (byte)0xE3, (byte)0x96 } +{ (byte)0x97, (byte)0xA4, (byte)0x51, (byte)0x23, (byte)0x6B, (byte)0xC6, (byte)0x7D, (byte)0x67, (byte)0xC4, (byte)0xF6, (byte)0x9A, (byte)0x50, (byte)0x87, (byte)0xEA, (byte)0x04, (byte)0x0F } +{ (byte)0xD0, (byte)0x76, (byte)0xFE, (byte)0x69, (byte)0x30, (byte)0xB8, (byte)0x2A, (byte)0x95, (byte)0x18, (byte)0x74, (byte)0x0C, (byte)0xAF, (byte)0xE1, (byte)0x01, (byte)0x6B, (byte)0x58 } diff --git a/etc/io/github/deajl3ka/fast_key_erasure/GenerateCounter.java b/etc/io/github/deajl3ka/fast_key_erasure/GenerateCounter.java index 79ba622..124386b 100644 --- a/etc/io/github/deajl3ka/fast_key_erasure/GenerateCounter.java +++ b/etc/io/github/deajl3ka/fast_key_erasure/GenerateCounter.java @@ -21,18 +21,19 @@ import java.util.Locale; import java.util.SplittableRandom; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.atomic.AtomicBoolean; public class GenerateCounter { - private static final int THREAD_COUNT = 8, WORDS = 6, EXPECTED_DISTANCE = 68; + private static final int THREAD_COUNT = 8, WORDS = 8, EXPECTED_DISTANCE = 64; - private static final SplittableRandom splittableRandom = new SplittableRandom(0x93C467E37DB0C7A4L); - - private static int bestDistance = -1; + private static int bestDistance = -1, bestThreadId = Integer.MAX_VALUE; private static final Object mutex = new Object(); private static final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT); + + private static final AtomicBoolean stop = new AtomicBoolean(false); private static class Int128 { final long hi, lo; @@ -71,10 +72,13 @@ public class GenerateCounter { } public static void main(String[] args) { + final SplittableRandom splittableRandom = new SplittableRandom(0x93C467E37DB0C7A4L); final Thread[] threads = new Thread[THREAD_COUNT]; - for (int tid = 0; tid < THREAD_COUNT; ++tid) { - threads[tid] = new Thread(GenerateCounter::threadMain); - threads[tid].start(); + for (int i = 0; i < THREAD_COUNT; ++i) { + final int threadId = i; + final SplittableRandom threadRandom = splittableRandom.split(); + threads[threadId] = new Thread(() -> threadMain(threadId, threadRandom)); + threads[threadId].start(); } for (final Thread thread : threads) { try { @@ -85,16 +89,11 @@ public class GenerateCounter { } } - private static void threadMain() { + private static void threadMain(final int threadId, final SplittableRandom random) { final Int128[] values = new Int128[WORDS]; final char[][] nibble = new char[WORDS][]; - final SplittableRandom random; - synchronized (mutex) { - random = splittableRandom.split(); - } - - for (;;) { + do { for (int i = 0; i < WORDS; ++i) { generatorLoop: for (;;) { @@ -115,14 +114,22 @@ public class GenerateCounter { continue generatorLoop; } } - for (int j = 0; j < i; ++j) { - final char[] otherNibbles = nibble[j]; + if (i > 0) { + final char[] otherNibbles = nibble[i - 1]; for (int k = 0; k < 32; ++k) { if (thisNibbles[k] == otherNibbles[k]) { continue generatorLoop; } } } + for (int j = 0; j < i; ++j) { + final char[] otherNibbles = nibble[j]; + for (int k = 0; k < 32; k += 2) { + if ((thisNibbles[k] == otherNibbles[k]) && (thisNibbles[k+1] == otherNibbles[k+1])) { + continue generatorLoop; + } + } + } break; /*okay*/ } } @@ -138,15 +145,12 @@ public class GenerateCounter { } synchronized (mutex) { - if (minDistance >= bestDistance) { + if ((minDistance > bestDistance) || ((minDistance == bestDistance) && (threadId < bestThreadId))) { bestDistance = minDistance; + bestThreadId = threadId; System.out.printf("[%d]%n", bestDistance); for (int i = 0; i < WORDS; ++i) { - final String word = new String(nibble[i]).toUpperCase(Locale.ENGLISH); - if (!values[i].toString().equalsIgnoreCase(word)) { - throw new AssertionError("Whoops!"); - } - System.out.println(word); + System.out.println(values[i].toString()); } System.out.println(); if (bestDistance >= EXPECTED_DISTANCE) { @@ -156,6 +160,9 @@ public class GenerateCounter { System.out.println(); } } + if (bestDistance >= EXPECTED_DISTANCE) { + stop.set(true); + } } try { @@ -163,13 +170,7 @@ public class GenerateCounter { } catch (InterruptedException | BrokenBarrierException e) { return; } - - synchronized (mutex) { - if (bestDistance >= EXPECTED_DISTANCE) { - return; - } - } - } + } while(!stop.get()); } private static String longToHexStr(final long value) { diff --git a/src/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNG.java b/src/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNG.java index 99262ab..4318b84 100644 --- a/src/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNG.java +++ b/src/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNG.java @@ -40,20 +40,22 @@ import javax.crypto.SecretKey; @SuppressWarnings("serial") public class FastKeyErasureRNG extends Random { - private static final int KEY_SIZE = 32, OUT_SIZE = 64, RESEED_INTERVAL = 257; + private static final int KEY_SIZE = 32, OUT_SIZE = 96, RESEED_INTERVAL = 257; /** * 128-bit (16 bytes) words to be used as “plaintext” counter values, generated reproducibly to maximize the pairwise hamming-distance, cf. {@link GenerateCounter} */ private static final byte[] PLAINTEXT_K = new byte[] { - (byte)0x57, (byte)0xC3, (byte)0xEB, (byte)0xFC, (byte)0x21, (byte)0xE4, (byte)0x7F, (byte)0xE0, (byte)0xCD, (byte)0x1E, (byte)0x21, (byte)0x59, (byte)0x5C, (byte)0x92, (byte)0x3C, (byte)0x19, - (byte)0xA0, (byte)0xBD, (byte)0xC8, (byte)0xB0, (byte)0x94, (byte)0x9A, (byte)0xB0, (byte)0x9E, (byte)0x7C, (byte)0xE0, (byte)0xA6, (byte)0xB7, (byte)0x4F, (byte)0x4C, (byte)0x61, (byte)0x9D + (byte)0x43, (byte)0x1E, (byte)0x12, (byte)0x16, (byte)0xE0, (byte)0x50, (byte)0xB5, (byte)0xBC, (byte)0x90, (byte)0x6D, (byte)0x06, (byte)0xC3, (byte)0x7E, (byte)0xAE, (byte)0x8D, (byte)0xED, + (byte)0xA4, (byte)0x39, (byte)0x2F, (byte)0x45, (byte)0x08, (byte)0x72, (byte)0x48, (byte)0x63, (byte)0xD2, (byte)0x57, (byte)0xE7, (byte)0x17, (byte)0x48, (byte)0x95, (byte)0xB8, (byte)0x41 }, PLAINTEXT_V = new byte[] { - (byte)0x92, (byte)0x12, (byte)0xDC, (byte)0xD8, (byte)0xDB, (byte)0x73, (byte)0x47, (byte)0x49, (byte)0x82, (byte)0xC3, (byte)0xF3, (byte)0xC0, (byte)0x6A, (byte)0x6D, (byte)0x90, (byte)0xFC, - (byte)0x2C, (byte)0x37, (byte)0x97, (byte)0x61, (byte)0xA0, (byte)0xD2, (byte)0x05, (byte)0xD7, (byte)0xAE, (byte)0x2F, (byte)0x95, (byte)0x68, (byte)0xA5, (byte)0xA0, (byte)0xA6, (byte)0xC5, - (byte)0x63, (byte)0xF4, (byte)0x79, (byte)0x45, (byte)0x13, (byte)0x8C, (byte)0x89, (byte)0x62, (byte)0xB8, (byte)0xF7, (byte)0xBE, (byte)0xA6, (byte)0x78, (byte)0xF9, (byte)0xDF, (byte)0x6A, - (byte)0xFD, (byte)0x4C, (byte)0x2E, (byte)0x2A, (byte)0x76, (byte)0x0B, (byte)0xA2, (byte)0xC3, (byte)0x0F, (byte)0x71, (byte)0x7A, (byte)0x0B, (byte)0xE0, (byte)0xD6, (byte)0x58, (byte)0xB7 + (byte)0x36, (byte)0x51, (byte)0x08, (byte)0xA3, (byte)0x19, (byte)0xE6, (byte)0x1F, (byte)0x18, (byte)0xAB, (byte)0x0D, (byte)0x31, (byte)0x8D, (byte)0x29, (byte)0xC9, (byte)0x35, (byte)0x3A, + (byte)0x49, (byte)0x23, (byte)0x29, (byte)0x3A, (byte)0xC4, (byte)0xBD, (byte)0x4B, (byte)0x2A, (byte)0x20, (byte)0xFB, (byte)0x94, (byte)0xB2, (byte)0x1B, (byte)0x75, (byte)0x64, (byte)0xF2, + (byte)0xFB, (byte)0xAC, (byte)0x80, (byte)0xD4, (byte)0x35, (byte)0x17, (byte)0x84, (byte)0xD8, (byte)0x4E, (byte)0x45, (byte)0x1B, (byte)0x7F, (byte)0x45, (byte)0xEB, (byte)0x52, (byte)0x64, + (byte)0x3E, (byte)0xFA, (byte)0xCD, (byte)0x9E, (byte)0xBE, (byte)0x74, (byte)0xE0, (byte)0x25, (byte)0xFD, (byte)0x01, (byte)0x27, (byte)0xF1, (byte)0x06, (byte)0x14, (byte)0xE3, (byte)0x96, + (byte)0x97, (byte)0xA4, (byte)0x51, (byte)0x23, (byte)0x6B, (byte)0xC6, (byte)0x7D, (byte)0x67, (byte)0xC4, (byte)0xF6, (byte)0x9A, (byte)0x50, (byte)0x87, (byte)0xEA, (byte)0x04, (byte)0x0F, + (byte)0xD0, (byte)0x76, (byte)0xFE, (byte)0x69, (byte)0x30, (byte)0xB8, (byte)0x2A, (byte)0x95, (byte)0x18, (byte)0x74, (byte)0x0C, (byte)0xAF, (byte)0xE1, (byte)0x01, (byte)0x6B, (byte)0x58 }; private static final SecureRandom strongRandom; diff --git a/src/io/github/deajl3ka/fast_key_erasure/Main.java b/src/io/github/deajl3ka/fast_key_erasure/Main.java index a787450..b45b549 100644 --- a/src/io/github/deajl3ka/fast_key_erasure/Main.java +++ b/src/io/github/deajl3ka/fast_key_erasure/Main.java @@ -24,6 +24,8 @@ import java.io.IOException; public class Main { public static void main(String[] args) { + final short[] libVersion = FastKeyErasureRNG.getVersion(); + System.err.printf("%s [v%d.%d]%n", FastKeyErasureRNG.class.getSimpleName(), libVersion[0], libVersion[1]); final FastKeyErasureRNG random = FastKeyErasureRNG.current(); final byte[] buffer = new byte[4096]; try (final FileOutputStream output = new FileOutputStream(FileDescriptor.out)) { diff --git a/test/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNGTest.java b/test/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNGTest.java index 618f7af..6370af8 100644 --- a/test/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNGTest.java +++ b/test/io/github/deajl3ka/fast_key_erasure/FastKeyErasureRNGTest.java @@ -104,7 +104,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { final byte[] plaintext_v = getField(FastKeyErasureRNG.class, byte[].class, "PLAINTEXT_V"); assertNotNull(plaintext_v); - assertEquals(80, (V = Ascii85.encode(plaintext_v)).length()); + assertEquals(120, (V = Ascii85.encode(plaintext_v)).length()); } @AfterEach @@ -189,7 +189,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -228,7 +228,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -293,7 +293,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -333,7 +333,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { assertTrue(ratio >= 0.99); // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -385,7 +385,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { assertTrue(average < 98304.0); // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -417,7 +417,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -449,7 +449,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -481,7 +481,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -525,7 +525,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -571,7 +571,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { } // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -614,7 +614,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { assertTrue(ratio >= 0.95); // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats @@ -690,7 +690,7 @@ public class FastKeyErasureRNGTest extends AbstractUnitTest { assertTrue(ratio >= 0.99); // Print stats - final long expectedBlocks = (totalBytes + 63) / 64; + final long expectedBlocks = (totalBytes + 95) / 96; System.out.printf("Total bytes generated: %d (total blocks generated: %d)%n", totalBytes, expectedBlocks); // Verify stats -- 2.11.4.GIT