From 08b8d058c924763cb68f835655d4b39ac300f379 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 18 Oct 2020 23:42:54 +0700 Subject: Add Bits and Longs classes from bc-java --- crypto/BouncyCastle.Android.csproj | 2 + crypto/BouncyCastle.csproj | 4 +- crypto/BouncyCastle.iOS.csproj | 2 + crypto/crypto.csproj | 10 ++++ crypto/src/math/raw/Bits.cs | 29 +++++++++++ crypto/src/math/raw/Interleave.cs | 101 +++++++++++++++---------------------- crypto/src/util/Integers.cs | 16 ++++++ crypto/src/util/Longs.cs | 65 ++++++++++++++++++++++++ 8 files changed, 169 insertions(+), 60 deletions(-) create mode 100644 crypto/src/math/raw/Bits.cs create mode 100644 crypto/src/util/Longs.cs diff --git a/crypto/BouncyCastle.Android.csproj b/crypto/BouncyCastle.Android.csproj index a27c5e8ae..fe25eec6b 100644 --- a/crypto/BouncyCastle.Android.csproj +++ b/crypto/BouncyCastle.Android.csproj @@ -1395,6 +1395,7 @@ + @@ -1557,6 +1558,7 @@ + diff --git a/crypto/BouncyCastle.csproj b/crypto/BouncyCastle.csproj index 3285296bc..315f6cd46 100644 --- a/crypto/BouncyCastle.csproj +++ b/crypto/BouncyCastle.csproj @@ -1389,6 +1389,7 @@ + @@ -1551,6 +1552,7 @@ + @@ -1662,4 +1664,4 @@ - + \ No newline at end of file diff --git a/crypto/BouncyCastle.iOS.csproj b/crypto/BouncyCastle.iOS.csproj index 4be7052f9..b8d43cb27 100644 --- a/crypto/BouncyCastle.iOS.csproj +++ b/crypto/BouncyCastle.iOS.csproj @@ -1390,6 +1390,7 @@ + @@ -1552,6 +1553,7 @@ + diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj index a940fbd0b..797c6ae9e 100644 --- a/crypto/crypto.csproj +++ b/crypto/crypto.csproj @@ -6833,6 +6833,11 @@ SubType = "Code" BuildAction = "Compile" /> + + > s)) & m; + return (t ^ (t << s)) ^ x; + } + + internal static ulong BitPermuteStep(ulong x, ulong m, int s) + { + ulong t = (x ^ (x >> s)) & m; + return (t ^ (t << s)) ^ x; + } + + internal static uint BitPermuteStepSimple(uint x, uint m, int s) + { + return ((x & m) << s) | ((x >> s) & m); + } + + internal static ulong BitPermuteStepSimple(ulong x, ulong m, int s) + { + return ((x & m) << s) | ((x >> s) & m); + } + } +} diff --git a/crypto/src/math/raw/Interleave.cs b/crypto/src/math/raw/Interleave.cs index 49d3768d7..8e98eac32 100644 --- a/crypto/src/math/raw/Interleave.cs +++ b/crypto/src/math/raw/Interleave.cs @@ -70,11 +70,10 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Expand32to64(uint x) { // "shuffle" low half to even bits and high half to odd bits - uint t; - t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1)); + x = Bits.BitPermuteStep(x, 0x0000FF00U, 8); + x = Bits.BitPermuteStep(x, 0x00F000F0U, 4); + x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2); + x = Bits.BitPermuteStep(x, 0x22222222U, 1); return ((x >> 1) & M32) << 32 | (x & M32); } @@ -82,12 +81,11 @@ namespace Org.BouncyCastle.Math.Raw internal static void Expand64To128(ulong x, ulong[] z, int zOff) { // "shuffle" low half to even bits and high half to odd bits - ulong t; - t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16)); - t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1)); + x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16); + x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8); + x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4); + x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2); + x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1); z[zOff ] = (x ) & M64; z[zOff + 1] = (x >> 1) & M64; @@ -97,28 +95,19 @@ namespace Org.BouncyCastle.Math.Raw { for (int i = 0; i < xsLen; ++i) { - // "shuffle" low half to even bits and high half to odd bits - ulong x = xs[xsOff + i], t; - t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16)); - t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1)); - - zs[zsOff++] = (x ) & M64; - zs[zsOff++] = (x >> 1) & M64; + Expand64To128(xs[xsOff + i], zs, zsOff); + zsOff += 2; } } internal static void Expand64To128Rev(ulong x, ulong[] z, int zOff) { // "shuffle" low half to even bits and high half to odd bits - ulong t; - t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16)); - t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1)); + x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16); + x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8); + x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4); + x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2); + x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1); z[zOff] = (x ) & M64R; z[zOff + 1] = (x << 1) & M64R; @@ -127,68 +116,62 @@ namespace Org.BouncyCastle.Math.Raw internal static uint Shuffle(uint x) { // "shuffle" low half to even bits and high half to odd bits - uint t; - t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1)); + x = Bits.BitPermuteStep(x, 0x0000FF00U, 8); + x = Bits.BitPermuteStep(x, 0x00F000F0U, 4); + x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2); + x = Bits.BitPermuteStep(x, 0x22222222U, 1); return x; } internal static ulong Shuffle(ulong x) { // "shuffle" low half to even bits and high half to odd bits - ulong t; - t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16)); - t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1)); + x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16); + x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8); + x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4); + x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2); + x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1); return x; } internal static uint Shuffle2(uint x) { // "shuffle" (twice) low half to even bits and high half to odd bits - uint t; - t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7)); - t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14)); - t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8)); + x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7); + x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14); + x = Bits.BitPermuteStep(x, 0x00F000F0U, 4); + x = Bits.BitPermuteStep(x, 0x0000FF00U, 8); return x; } internal static uint Unshuffle(uint x) { // "unshuffle" even bits to low half and odd bits to high half - uint t; - t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1)); - t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8)); + x = Bits.BitPermuteStep(x, 0x22222222U, 1); + x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2); + x = Bits.BitPermuteStep(x, 0x00F000F0U, 4); + x = Bits.BitPermuteStep(x, 0x0000FF00U, 8); return x; } internal static ulong Unshuffle(ulong x) { // "unshuffle" even bits to low half and odd bits to high half - ulong t; - t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1)); - t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2)); - t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16)); + x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1); + x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2); + x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4); + x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8); + x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16); return x; } internal static uint Unshuffle2(uint x) { // "unshuffle" (twice) even bits to low half and odd bits to high half - uint t; - t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8)); - t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4)); - t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14)); - t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7)); + x = Bits.BitPermuteStep(x, 0x0000FF00U, 8); + x = Bits.BitPermuteStep(x, 0x00F000F0U, 4); + x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14); + x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7); return x; } } diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs index 7d98de586..b243b88b2 100644 --- a/crypto/src/util/Integers.cs +++ b/crypto/src/util/Integers.cs @@ -1,5 +1,7 @@ using System; +using Org.BouncyCastle.Math.Raw; + namespace Org.BouncyCastle.Utilities { public abstract class Integers @@ -29,6 +31,20 @@ namespace Org.BouncyCastle.Utilities return DeBruijnTZ[(uint)((i & -i) * 0x04D7651F) >> 27]; } + public static int Reverse(int i) + { + i = (int)Bits.BitPermuteStepSimple((uint)i, 0x55555555U, 1); + i = (int)Bits.BitPermuteStepSimple((uint)i, 0x33333333U, 2); + i = (int)Bits.BitPermuteStepSimple((uint)i, 0x0F0F0F0FU, 4); + return ReverseBytes(i); + } + + public static int ReverseBytes(int i) + { + return RotateLeft((int)((uint)i & 0xFF00FF00U), 8) | + RotateLeft((int)((uint)i & 0x00FF00FFU), 24); + } + public static int RotateLeft(int i, int distance) { return (i << distance) ^ (int)((uint)i >> -distance); diff --git a/crypto/src/util/Longs.cs b/crypto/src/util/Longs.cs new file mode 100644 index 000000000..d206c1f81 --- /dev/null +++ b/crypto/src/util/Longs.cs @@ -0,0 +1,65 @@ +using System; + +using Org.BouncyCastle.Math.Raw; + +namespace Org.BouncyCastle.Utilities +{ + public abstract class Longs + { + public static long Reverse(long i) + { + i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x5555555555555555UL, 1); + i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x3333333333333333UL, 2); + i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x0F0F0F0F0F0F0F0FUL, 4); + return ReverseBytes(i); + } + + [CLSCompliantAttribute(false)] + public static ulong Reverse(ulong i) + { + i = Bits.BitPermuteStepSimple(i, 0x5555555555555555UL, 1); + i = Bits.BitPermuteStepSimple(i, 0x3333333333333333UL, 2); + i = Bits.BitPermuteStepSimple(i, 0x0F0F0F0F0F0F0F0FUL, 4); + return ReverseBytes(i); + } + + public static long ReverseBytes(long i) + { + return RotateLeft((long)((ulong)i & 0xFF000000FF000000UL), 8) | + RotateLeft((long)((ulong)i & 0x00FF000000FF0000UL), 24) | + RotateLeft((long)((ulong)i & 0x0000FF000000FF00UL), 40) | + RotateLeft((long)((ulong)i & 0x000000FF000000FFUL), 56); + } + + [CLSCompliantAttribute(false)] + public static ulong ReverseBytes(ulong i) + { + return RotateLeft(i & 0xFF000000FF000000UL, 8) | + RotateLeft(i & 0x00FF000000FF0000UL, 24) | + RotateLeft(i & 0x0000FF000000FF00UL, 40) | + RotateLeft(i & 0x000000FF000000FFUL, 56); + } + + public static long RotateLeft(long i, int distance) + { + return (i << distance) ^ (long)((ulong)i >> -distance); + } + + [CLSCompliantAttribute(false)] + public static ulong RotateLeft(ulong i, int distance) + { + return (i << distance) ^ (i >> -distance); + } + + public static long RotateRight(long i, int distance) + { + return (long)((ulong)i >> distance) ^ (i << -distance); + } + + [CLSCompliantAttribute(false)] + public static ulong RotateRight(ulong i, int distance) + { + return (i >> distance) ^ (i << -distance); + } + } +} -- cgit 1.4.1