Example #1
0
        public void Embedded_full_sequence_in_order_detection_test()
        {
            var values = GenerateBaseValues();

            var monobitScore = MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01);
            var blockScore   = MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01);
            var runsScore    = MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01);

            int c     = 0;
            int count = 0;

            for (int i = 0; i < 100; c++)
            {
                values[c] = new byte[byteLength];
                Array.Copy(BitConverter.GetBytes(i), values[c], 4);
                i++;
                count++;
            }

            Console.WriteLine($"Inserted {count} values spread over {c} positions");

            var monobitScoreAfter = MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01);
            var blockScoreAfter   = MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01);
            var runsScoreAfter    = MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01);

            Console.WriteLine($"Monobit: Passed {monobitScoreAfter} bits out of total {byteLength*8}");
            Console.WriteLine($"Poker:   Passed {blockScoreAfter} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {runsScoreAfter} bits out of total {byteLength*8}");

            Assert.Less(monobitScoreAfter, monobitScore);
            Assert.Less(blockScoreAfter, blockScore);
            Assert.Less(runsScoreAfter, runsScore);
        }
Example #2
0
        public void Humanize_sequence_test()
        {
            // Test entropy by generating perfect random, and then change the order randomly
            // in order to simulate human usage of generated codes. To simulate usage, we only
            // we also only use 1% of the generated values in the test.
            // Markow says this should not degrade the entropy, however the volume of statistics
            // is not close to required. So this is more a "reality check" than anything else.

            var values = GenerateBaseValues();

            Console.WriteLine($"Monobit: Passed {MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Poker:   Passed {MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");

            var random = new Random(globalSeed);

            byte[][] humanOrderedValues = new byte[(int)(values.Length * 0.01)][];
            for (int i = 0; i < humanOrderedValues.Length;)
            {
                int j = random.Next(values.Length);
                if (values[j] == null)
                {
                    continue;                                    // we already used this value
                }
                humanOrderedValues[i] = values[j];
                values[j]             = null;

                i++;
            }

            Console.WriteLine($"Monobit: Passed {MathAdvanced.FrequencyTest(humanOrderedValues).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Poker:   Passed {MathAdvanced.BlockTest(humanOrderedValues).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {MathAdvanced.RunsTest(humanOrderedValues).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
        }
Example #3
0
        public void RNGCrypto_generator_base_test()
        {
            var values = GenerateBaseValues();

            Console.WriteLine($"Monobit: Passed {MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Block:   Passed {MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
        }
        public void runs_test_should_match_standard()
        {
            byte[]   bytes  = GetBytes("1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000");
            byte[][] values = new byte[bytes.Length][];
            for (int i = 0; i < bytes.Length; i++)
            {
                values[i] = new[] { bytes[i] };
            }

            var result = MathAdvanced.RunsTest(values);

            Assert.AreEqual(100, bytes.Length);
            Assert.AreEqual(0.500798, Math.Round(result[0], 6));
        }
        public void frequency_with_block_test_should_match_standard()
        {
            byte[]   bytes  = GetBytes("1100100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000");
            byte[][] values = new byte[bytes.Length][];
            for (int i = 0; i < bytes.Length; i++)
            {
                values[i] = new[] { bytes[i] };
            }

            var result = MathAdvanced.BlockTest(values, 10);

            Assert.AreEqual(100, bytes.Length);
            Assert.AreEqual(0.706438, Math.Round(result[0], 6));
        }
Example #6
0
        public void Random_generator_used_wrong_test()
        {
            var rnd         = new Random();
            int sampleCount = 20_000;
            int keyLen      = byteLength;

            byte[][] values = new byte[sampleCount][];
            for (int i = 0; i < sampleCount; i++)
            {
                values[i] = new byte[keyLen];
                rnd.NextBytes(values[i]);
            }

            Console.WriteLine($"Monobit: Passed {MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Poker:   Passed {MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01)} bits out of total {byteLength*8}");
        }
Example #7
0
        public void Advanced_embedded_full_sequence_in_random_order_detection_test()
        {
            var values = GenerateBaseValues();

            var monobitScore = MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01);
            var blockScore   = MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01);
            var runsScore    = MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01);

            List <int> numbers = new List <int>();

            for (int i = 0; i < 200; i++)
            {
                numbers.Add(i);
            }
            numbers.Shuffle(new Random(globalSeed));

            int c     = 0;
            int count = 0;

            for (int i = 0; i < numbers.Count; i++, c++)
            {
                values[c] = new byte[byteLength];
                Array.Copy(BitConverter.GetBytes(numbers[i]), values[c], 4);
                i++;
                count++;
            }

            Console.WriteLine($"Inserted {count} values spread over {c} positions, but in random order");

            var monobitScoreAfter = MathAdvanced.FrequencyTest(values).Count(pVal => pVal.Value >= 0.01);
            var blockScoreAfter   = MathAdvanced.BlockTest(values).Count(pVal => pVal.Value >= 0.01);
            var runsScoreAfter    = MathAdvanced.RunsTest(values).Count(pVal => pVal.Value >= 0.01);

            Console.WriteLine($"Monobit: Passed {monobitScoreAfter} bits out of total {byteLength*8}");
            Console.WriteLine($"Poker:   Passed {blockScoreAfter} bits out of total {byteLength*8}");
            Console.WriteLine($"Runs:    Passed {runsScoreAfter} bits out of total {byteLength*8}");

            Assert.Less(monobitScoreAfter, monobitScore);
            Assert.Less(blockScoreAfter, blockScore);
            Assert.Less(runsScoreAfter, runsScore);
        }