Interleave.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw
  5. {
  6. internal abstract class Interleave
  7. {
  8. private const ulong M32 = 0x55555555UL;
  9. private const ulong M64 = 0x5555555555555555UL;
  10. private const ulong M64R = 0xAAAAAAAAAAAAAAAAUL;
  11. /*
  12. * This expands 8 bit indices into 16 bit contents (high bit 14), by inserting 0s between bits.
  13. * In a binary field, this operation is the same as squaring an 8 bit number.
  14. */
  15. //private static readonly ushort[] INTERLEAVE2_TABLE = new ushort[]
  16. //{
  17. // 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
  18. // 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
  19. // 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
  20. // 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
  21. // 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
  22. // 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
  23. // 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
  24. // 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
  25. // 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
  26. // 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
  27. // 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
  28. // 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
  29. // 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
  30. // 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
  31. // 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
  32. // 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
  33. // 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
  34. // 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
  35. // 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
  36. // 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
  37. // 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
  38. // 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
  39. // 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
  40. // 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
  41. // 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
  42. // 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
  43. // 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
  44. // 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
  45. // 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
  46. // 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
  47. // 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
  48. // 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
  49. //};
  50. internal static uint Expand8to16(uint x)
  51. {
  52. x &= 0xFFU;
  53. x = (x | (x << 4)) & 0x0F0FU;
  54. x = (x | (x << 2)) & 0x3333U;
  55. x = (x | (x << 1)) & 0x5555U;
  56. return x;
  57. }
  58. internal static uint Expand16to32(uint x)
  59. {
  60. x &= 0xFFFFU;
  61. x = (x | (x << 8)) & 0x00FF00FFU;
  62. x = (x | (x << 4)) & 0x0F0F0F0FU;
  63. x = (x | (x << 2)) & 0x33333333U;
  64. x = (x | (x << 1)) & 0x55555555U;
  65. return x;
  66. }
  67. internal static ulong Expand32to64(uint x)
  68. {
  69. // "shuffle" low half to even bits and high half to odd bits
  70. uint t;
  71. t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
  72. t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
  73. t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
  74. t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
  75. return ((x >> 1) & M32) << 32 | (x & M32);
  76. }
  77. internal static void Expand64To128(ulong x, ulong[] z, int zOff)
  78. {
  79. // "shuffle" low half to even bits and high half to odd bits
  80. ulong t;
  81. t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
  82. t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
  83. t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
  84. t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
  85. t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
  86. z[zOff ] = (x ) & M64;
  87. z[zOff + 1] = (x >> 1) & M64;
  88. }
  89. internal static void Expand64To128Rev(ulong x, ulong[] z, int zOff)
  90. {
  91. // "shuffle" low half to even bits and high half to odd bits
  92. ulong t;
  93. t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
  94. t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
  95. t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
  96. t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
  97. t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
  98. z[zOff] = (x ) & M64R;
  99. z[zOff + 1] = (x << 1) & M64R;
  100. }
  101. internal static uint Shuffle(uint x)
  102. {
  103. // "shuffle" low half to even bits and high half to odd bits
  104. uint t;
  105. t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
  106. t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
  107. t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
  108. t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
  109. return x;
  110. }
  111. internal static ulong Shuffle(ulong x)
  112. {
  113. // "shuffle" low half to even bits and high half to odd bits
  114. ulong t;
  115. t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
  116. t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
  117. t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
  118. t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
  119. t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
  120. return x;
  121. }
  122. internal static uint Shuffle2(uint x)
  123. {
  124. // "shuffle" (twice) low half to even bits and high half to odd bits
  125. uint t;
  126. t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
  127. t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
  128. t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
  129. t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
  130. return x;
  131. }
  132. internal static uint Unshuffle(uint x)
  133. {
  134. // "unshuffle" even bits to low half and odd bits to high half
  135. uint t;
  136. t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
  137. t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
  138. t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
  139. t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
  140. return x;
  141. }
  142. internal static ulong Unshuffle(ulong x)
  143. {
  144. // "unshuffle" even bits to low half and odd bits to high half
  145. ulong t;
  146. t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
  147. t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
  148. t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
  149. t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
  150. t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
  151. return x;
  152. }
  153. internal static uint Unshuffle2(uint x)
  154. {
  155. // "unshuffle" (twice) even bits to low half and odd bits to high half
  156. uint t;
  157. t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
  158. t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
  159. t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
  160. t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
  161. return x;
  162. }
  163. }
  164. }
  165. #pragma warning restore
  166. #endif