private static ulong[] GenerateKeys(ulong baseKey) { ulong key64 = 0; for (int i = 0; i < 8; ++i) { byte bitSequence = (byte)((baseKey >> (i * 7)) & 0b01111111); byte xorRes = Bitops.XorBits(bitSequence, 3); key64 = (key64 << 1) | (byte)(~xorRes & 1); key64 = (key64 << 7) | bitSequence; } uint C = (uint)(Bitops.SwapBitsMasks64(key64, _C0PermMasks) & 0xf_ff_ff_ff); uint D = (uint)(Bitops.SwapBitsMasks64(key64, _D0PermMasks) & 0xf_ff_ff_ff); ulong[] keys = new ulong[16]; for (int i = 0; i < 16; ++i) { C = Bitops.CycleShiftLeft(C, 28, _cycleShiftsCount[i]); D = Bitops.CycleShiftLeft(D, 28, _cycleShiftsCount[i]); keys[i] = ((ulong)C << 28) | D; keys[i] = Bitops.SwapBitsMasks64(keys[i], _keyFinalPermMasks) & 0xff_ff_ff_ff_ff_ff; } return(keys); }
public void Test2_IPInvPermutation() { byte[] IPInvPermutation = new byte[64] { 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24 }; ulong[] masks = (ulong[])typeof(DES_).GetField("_IPInvPermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); Random random = new Random(123); byte[] buf = new byte[8]; for (int i = 0; i < 1000; ++i) { random.NextBytes(buf); ulong value = BitConverter.ToUInt64(buf, 0); ulong expected = Bitops.BitPermutation(value, IPInvPermutation); ulong actual = Bitops.SwapBitsMasks64(value, masks); Assert.AreEqual(expected, actual); } }
public void TestRightmostOneIndex() { Assert.AreEqual(-1, Bitops.RightmostOneIndex((long)0)); Assert.AreEqual(-1, Bitops.RightmostOneIndex((short)0)); Assert.AreEqual(-1, Bitops.RightmostOneIndex((byte)0)); Assert.AreEqual(-1, Bitops.RightmostOneIndex(0)); Assert.AreEqual(-1, Bitops.RightmostOneIndex(new BigInteger(0))); Assert.AreEqual(0, Bitops.RightmostOneIndex((long)(-1))); Assert.AreEqual(0, Bitops.RightmostOneIndex((short)(-1))); Assert.AreEqual(0, Bitops.RightmostOneIndex((byte)(0xff))); Assert.AreEqual(0, Bitops.RightmostOneIndex(-1)); Assert.AreEqual(0, Bitops.RightmostOneIndex(new BigInteger(-1))); for (long i = 0, l = 1; l != 0; l <<= 1, i++) { Assert.AreEqual(i, Bitops.RightmostOneIndex(l)); } for (int i = 0, l = 1; l != 0; l <<= 1, i++) { Assert.AreEqual(i, Bitops.RightmostOneIndex(l)); } for (short i = 0, l = 1; l != 0; l <<= 1, i++) { Assert.AreEqual(i, Bitops.RightmostOneIndex(l)); } for (byte i = 0, l = 1; l != 0; l <<= 1, i++) { Assert.AreEqual(i, Bitops.RightmostOneIndex(l)); } Assert.AreEqual(8, Bitops.RightmostOneIndex(new BigInteger(256))); }
public void Test5_KeyFinalPermutation() { byte[] keyFinalPermutation = new byte[48] { 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 }; //Type DESCrTransType = typeof(DES).GetNestedType("DESCryptoTransform", BindingFlags.NonPublic); //ulong[] masks = (ulong[])DESCrTransType.GetField("_keyFinalPermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); ulong[] masks = (ulong[])typeof(DES_).GetField("_keyFinalPermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); Random random = new Random(123); byte[] buf = new byte[8]; for (int i = 0; i < 1000; ++i) { random.NextBytes(buf); ulong value = BitConverter.ToUInt64(buf, 0); ulong expected = Bitops.BitPermutation(value, keyFinalPermutation); ulong actual = Bitops.SwapBitsMasks64(value, masks) & 0xff_ff_ff_ff_ff_ff; Assert.AreEqual(expected, actual); } }
public void Test6_PPermutation() { byte[] PPermutation = new byte[32] { 15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9, 1, 7, 23, 13, 31, 26, 2, 8, 18, 12, 29, 5, 21, 10, 3, 24 }; //Type DESCrTransType = typeof(DES).GetNestedType("DESCryptoTransform", BindingFlags.NonPublic); //ulong[] masks = (ulong[])DESCrTransType.GetField("_PPermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); ulong[] masks = (ulong[])typeof(DES_).GetField("_PPermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); Random random = new Random(123); byte[] buf = new byte[8]; for (int i = 0; i < 1000; ++i) { random.NextBytes(buf); ulong value = BitConverter.ToUInt64(buf, 0); ulong expected = Bitops.BitPermutation(value, PPermutation); ulong actual = Bitops.SwapBitsMasks64(value, masks) & 0xff_ff_ff_ff; Assert.AreEqual(expected, actual); } }
public void Test3_C0Permutation() { byte[] C0Permutation = new byte[28] { 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35 }; //Type DESCrTransType = typeof(DES).GetNestedType("DESCryptoTransform", BindingFlags.NonPublic); //ulong[] masks = (ulong[])DESCrTransType.GetField("_C0PermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); ulong[] masks = (ulong[])typeof(DES_).GetField("_C0PermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); Random random = new Random(123); byte[] buf = new byte[8]; for (int i = 0; i < 1000; ++i) { random.NextBytes(buf); ulong value = BitConverter.ToUInt64(buf, 0); ulong expected = Bitops.BitPermutation(value, C0Permutation); ulong actual = Bitops.SwapBitsMasks64(value, masks) & 0xf_ff_ff_ff; Assert.AreEqual(expected, actual); } }
public void Test4_D0Permutation() { byte[] D0Permutation = new byte[28] { 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 }; //Type DESCrTransType = typeof(DES).GetNestedType("DESCryptoTransform", BindingFlags.NonPublic); //ulong[] masks = (ulong[])DESCrTransType.GetField("_D0PermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); ulong[] masks = (ulong[])typeof(DES_).GetField("_D0PermMasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); Random random = new Random(123); byte[] buf = new byte[8]; for (int i = 0; i < 1000; ++i) { random.NextBytes(buf); ulong value = BitConverter.ToUInt64(buf, 0); ulong expected = Bitops.BitPermutation(value, D0Permutation); ulong actual = Bitops.SwapBitsMasks64(value, masks) & 0xf_ff_ff_ff; Assert.AreEqual(expected, actual); } }
public void TestTask21() { uint[] value = new uint[] { 0b1101_0010, 0b1101_0010, 0b1010_0011 }; int[] len = new int[] { 8, 8, 8 }; int[] i = new int[] { 2, 7, 3 }; uint[] expected = new uint[] { 0b1110, 0b11_0100_1101_0010, 0b10_1011 }; for (int j = 0; j < value.Length; ++j) { Assert.AreEqual(expected[j], Bitops.ConcatExtremeBits(value[j], len[j], i[j])); } }
public static byte[] GenerateSBox() { byte[] matrix = new byte[] { 0b1111_0001, 0b1110_0011, 0b1100_0111, 0b1000_1111, 0b0001_1111, 0b0011_1110, 0b0111_1100, 0b1111_1000 }; byte[] sBox = new byte[256]; for (int i = 0; i < 256; ++i) { byte inv = GF.Inverse((byte)i); for (int j = 0; j < 8; j++) { byte conj = (byte)(matrix[j] & inv); byte xorSum = Bitops.XorBits(conj); sBox[i] |= (byte)(xorSum << j); } sBox[i] ^= 0x63; } return(sBox); }
public void TestTask14() { uint[] values = new uint[] { 0b1000, 0b1111, 0b1010_1110 }; int[] m = new int[] { 4, 2, 7 }; uint[] expected = new uint[] { 0, 0b1100, 0b1000_0000 }; for (int i = 0; i < values.Length; ++i) { Assert.AreEqual(expected[i], Bitops.NullifyMLowBits(values[i], m[i])); } }
public void LocateBitsTest() { Assert.AreEqual(0UL, Bitops.LocateZeroes(0x123456789abcdef0UL)); ulong result = Bitops.LocateZeroes(0x100400009abc00f0UL); Assert.AreEqual(0x0000808000008000UL, result); }
public void TestTask8() { uint[] a = new uint[] { 0b10101110, 0b1011, 0b11010100 }; byte[] n = new byte[] { 8, 4, 8 }; byte[][] transposition = new byte[][] { new byte[] { 5, 3, 7, 1, 4, 0, 6, 2 }, new byte[] { 0, 2 }, new byte[] { 2, 0, 0, 7, 4 } }; uint[] expected = new uint[] { 0b11110001, 0b10, 0b10011 }; for (int i = 0; i < a.Length; ++i) { Assert.AreEqual(expected[i], Bitops.BitPermutation(a[i], transposition[i])); } }
public void TestLeftmostOneIndex() { Assert.AreEqual(36, Bitops.LeftmostOneIndex(0x10ccccccccL)); Assert.AreEqual(-1, Bitops.LeftmostOneIndex((long)0)); Assert.AreEqual(10, Bitops.LeftmostOneIndex(new BigInteger(1024))); Assert.AreEqual(-1, Bitops.LeftmostOneIndex(BigInteger.Zero)); }
public void TestTask6() { uint[] a = new uint[] { 0b1101_0100, 0b11001010_10001000_11101111_11110000, 0b1, 0b10101000_11010100, 0b1, 0b1010101 }; byte[] p = new byte[] { 3, 5, 0, 3, 1, 0 }; byte[] expected = new byte[] { 0, 1, 1, 0, 1, 1 }; for (int i = 0; i < a.Length; ++i) { Assert.AreEqual(expected[i], Bitops.XorBits(a[i], p[i])); } }
public void TestRemoveRightmostOnes() { BigInteger n = new BigInteger(22); BigInteger r = new BigInteger(16); Assert.AreEqual(16, Bitops.RemoveRightmostOnes(22)); Assert.AreEqual(r, Bitops.RemoveRightmostOnes(n)); }
public void TestSmearRightmostOneRight() { BigInteger n = new BigInteger(12); BigInteger r = new BigInteger(15); Assert.AreEqual(15, Bitops.SmearRightmostOneRight(12)); Assert.AreEqual(r, Bitops.SmearRightmostOneRight(n)); }
public void TestExtractRemoveSmearRightmostOneRight() { BigInteger n = new BigInteger(12); BigInteger r = new BigInteger(3); Assert.AreEqual(3, Bitops.ExtractRemoveSmearRightmostOneRight(12)); Assert.AreEqual(r, Bitops.ExtractRemoveSmearRightmostOneRight(n)); }
public void TestExtractRightmostOne() { BigInteger n = new BigInteger(12); BigInteger r = new BigInteger(4); Assert.AreEqual(4, Bitops.ExtractRightmostOne(12)); Assert.AreEqual(r, Bitops.ExtractRightmostOne(n)); }
public void TestRemoveAndSmearRightmostOneLeft() { BigInteger n = new BigInteger(12); BigInteger r = new BigInteger(-8); Assert.AreEqual(-8, Bitops.RemoveAndSmearRightmostOneLeft(12)); Assert.AreEqual(r, Bitops.RemoveAndSmearRightmostOneLeft(n)); }
public void CompareBitsTest() { // 0x0011110011001100 // 0x0000ff1111001111 // ------------------ // 0x0000808000000080 Assert.AreEqual(0x0000808000000080UL, Bitops.CompareBytes(0x0011110011001100UL, 0x0000ff1111001111UL)); }
public void AddBytesTest() { // 0xff 00 f0 32 20 30 40 50 // + 0x02 33 10 24 f0 e0 d0 c0 // ------------------------- // 0x01 33 00 56 10 10 10 10 Assert.AreEqual(0x0133005610101010UL, Bitops.AddBytes(0xff00f03220304050UL, 0x02331024f0e0d0c0UL)); }
public void TestSwapBits() { Assert.AreEqual(1ul, Bitops.SwapBits(2, 0, 1)); Assert.AreEqual(1ul, Bitops.SwapBits(2, 1, 0)); Assert.AreEqual(3ul, Bitops.SwapBits(3, 0, 1)); Assert.AreEqual(3ul, Bitops.SwapBits(3, 1, 0)); Assert.AreEqual(5ul, Bitops.SwapBits(6, 0, 1)); Assert.AreEqual(8ul, Bitops.SwapBits(8, 0, 1)); }
public void IndexTValuesTest() { // 0x00 11 11 00 11 00 11 00 // 0x00 00 ff 11 11 00 11 11 // ------------------------- // 0x00 00 80 80 00 00 00 80 Assert.AreEqual(0, Bitops.RightIndexOfTValue(Bitops.CompareBytes(0x0011110011001100UL, 0x0000ff1111001111UL))); Assert.AreEqual(5, Bitops.LeftIndexOfTValue(Bitops.CompareBytes(0x0011110011001100UL, 0x0000ff1111001111UL))); }
public void TestTask12() { uint value = 0b1100_1001; Assert.AreEqual(0b1110_1001u, Bitops.SwitchKthBit(value, 5, true)); Assert.AreEqual(0b1100_1001u, Bitops.SwitchKthBit(value, 0, true)); Assert.AreEqual(0b1100_1000u, Bitops.SwitchKthBit(value, 0, false)); Assert.AreEqual(0b0100_1001u, Bitops.SwitchKthBit(value, 7, false)); }
public void TestTask13() { uint value = 0b1100_1001; Assert.AreEqual(0b1100_1010u, Bitops.SwapBits(value, 0, 1), "0"); Assert.AreEqual(0b1100_1001u, Bitops.SwapBits(value, 1, 2)); Assert.AreEqual(0b1110_1000u, Bitops.SwapBits(value, 0, 5)); Assert.AreEqual(0b1110_1000u, Bitops.SwapBits(value, 5, 0)); Assert.AreEqual(0b1100_1001u, Bitops.SwapBits(value, 0, 7)); }
public void AverageBytesTest() { // 0x10f0302030e0a003 // 0xf03020f040202005 // ------------------ // 0x8090288838806004 ulong result = Bitops.AverageBytes(0x10f0302030e0a003, 0xf03020f040202005); Assert.AreEqual(0x8090288838806004UL, result); }
public void SubtractBytesTest() { // 0xff 00 f0 32 20 30 40 50 // - 0x02 33 10 24 f0 e0 d0 c0 // ------------------------- // 0xfd cd e0 0e 30 50 70 90 ulong result = Bitops.SubtractBytes(0xff00f03220304050UL, 0x02331024f0e0d0c0UL); Assert.AreEqual(0xfdcde00e30507090UL, result); }
private static ulong EExpansion(uint value) { ulong result = 0; for (int i = 0; i < 8; ++i) { result = (result << 6) | (Bitops.CycleShiftLeft(value, 32, (byte)(5 + i * 4)) & 0b111111); } return(result); }
public void AddFragmentedFieldsTest() { // 0x10f0302030e0a003 // + 0xf03020f040202005 // mask 0xf0f0f0f0f0f0f00f // ------------------ // 0x102050108000c008 ulong result = Bitops.AddFragmentedFields(0x10f0302030e0a003, 0xf03020f040202005, 0xf0f0f0f0f0f0f00f); Assert.AreEqual(0x102060108000c008UL, result); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Scrunches the input according to the mask passed to the constructor. </summary> /// /// <remarks> Darrellp, 12/12/2011. </remarks> /// /// <param name="x"> The value to be scrunched. </param> /// /// <returns> The scrunched result. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public ulong Scrunch(ulong x) { int iShift = 1; for (int iMask = 0; iMask < _masks.Length; iMask++, iShift <<= 1) { if (iMask != 0) { x = Bitops.ShiftBitsMask(x, iShift, _masks[iMask]); } } return(x); }