コード例 #1
0
        private static void TestTwoCycle(int i1, int i2, byte[] permutation)
        {
            Array.Copy(Identity, permutation, 64);
            permutation[i1] = (byte)i2;
            permutation[i2] = (byte)i1;
            PermutationNetwork pnwk = new PermutationNetwork(permutation);

            Assert.AreEqual(1UL << i2, pnwk.Permute(1UL << i1));
            Assert.AreEqual(1UL << i1, pnwk.Permute(1UL << i2));
        }
コード例 #2
0
        public void Test0_PermutationNetwork()
        {
            byte[] _IPTranspose = new byte[]
            {
                57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
                61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
                56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2,
                60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6
            };
            byte[] _IPTransposeReversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeReversed[i] = _IPTranspose[63 - i];
            }
            byte[] _IPTransposeInversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeInversed[_IPTranspose[i]] = (byte)i;
            }
            byte[] _IPTransposeInversedReversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeInversedReversed[_IPTranspose[63 - i]] = (byte)i;
            }
            var network = new PermutationNetwork(_IPTransposeInversedReversed);


            Random random = new Random(123);

            byte[] buf = new byte[8];
            for (int i = 0; i < 10000; ++i)
            {
                random.NextBytes(buf);
                ulong value    = BitConverter.ToUInt64(buf, 0);
                ulong expected = Bitops.BitPermutation(value, _IPTranspose);

                ulong actual = network.Permute(value);

                Assert.AreEqual(expected, actual);
            }
        }
コード例 #3
0
        public void TestPermutationNetwork()
        {
            byte[] permutation = new byte[64];

            // Identity permutation
            for (byte i = 0; i < 64; i++)
            {
                Identity[i] = i;
            }

            for (int i1 = 0; i1 < 62; i1++)
            {
                for (int i2 = i1 + 1; i2 < 64; i2++)
                {
                    TestTwoCycle(i1, i2, permutation);
                }
            }

            Array.Copy(Identity, permutation, 64);

            PermutationNetwork pnwk = new PermutationNetwork(permutation);

            Assert.AreEqual(56ul, pnwk.Permute(56));

            // Swap unit and two's bits
            permutation[0] = 1;
            permutation[1] = 0;
            pnwk           = new PermutationNetwork(permutation);
            Assert.AreEqual(1ul, pnwk.Permute(2));
            Assert.AreEqual(2ul, pnwk.Permute(1));
            Assert.AreEqual(3ul, pnwk.Permute(3));
            Assert.AreEqual(5ul, pnwk.Permute(6));
            Assert.AreEqual(6ul, pnwk.Permute(5));

            // Cycle the bottom four bits
            permutation[0] = 1;
            permutation[1] = 2;
            permutation[2] = 3;
            permutation[3] = 0;
            pnwk           = new PermutationNetwork(permutation);
            Assert.AreEqual(2ul, pnwk.Permute(1));
            Assert.AreEqual(4ul, pnwk.Permute(2));
            Assert.AreEqual(8ul, pnwk.Permute(4));
            Assert.AreEqual(1ul, pnwk.Permute(8));
            Assert.AreEqual(3ul, pnwk.Permute(9));

            // Random permutation
            Random rnd = new Random(0);

            for (int i = 0; i < 63; i++)
            {
                int  swapIndex = rnd.Next(64);
                byte b         = permutation[i];
                permutation[i]         = permutation[swapIndex];
                permutation[swapIndex] = b;
            }
            pnwk = new PermutationNetwork(permutation);
            ulong testVal   = ((ulong)rnd.Next() << 32) | (uint)rnd.Next();
            ulong resultVal = pnwk.Permute(testVal);

            TestPerm(permutation, testVal, resultVal);
        }