|
3 | 3 | using System.Buffers.Binary;
|
4 | 4 | #endif
|
5 | 5 | using System.Diagnostics;
|
| 6 | +#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER |
6 | 7 | using System.Runtime.CompilerServices;
|
| 8 | +#endif |
7 | 9 |
|
8 | 10 | namespace Org.BouncyCastle.Crypto.Utilities
|
9 | 11 | {
|
@@ -124,6 +126,24 @@ internal static void UInt32_To_BE(uint n, byte[] bs, int off)
|
124 | 126 | #endif
|
125 | 127 | }
|
126 | 128 |
|
| 129 | + internal static void UInt32_To_BE_High(uint n, byte[] bs, int off, int len) |
| 130 | + { |
| 131 | + Debug.Assert(1 <= len && len <= 4); |
| 132 | + |
| 133 | + int pos = 24; |
| 134 | + bs[off] = (byte)(n >> pos); |
| 135 | + for (int i = 1; i < len; ++i) |
| 136 | + { |
| 137 | + pos -= 8; |
| 138 | + bs[off + i] = (byte)(n >> pos); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + internal static void UInt32_To_BE_Low(uint n, byte[] bs, int off, int len) |
| 143 | + { |
| 144 | + UInt32_To_BE_High(n << ((4 - len) << 3), bs, off, len); |
| 145 | + } |
| 146 | + |
127 | 147 | internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
|
128 | 148 | {
|
129 | 149 | for (int i = 0; i < ns.Length; ++i)
|
@@ -180,7 +200,12 @@ internal static uint BE_To_UInt32(byte[] bs, int off)
|
180 | 200 | #endif
|
181 | 201 | }
|
182 | 202 |
|
183 |
| - internal static uint BE_To_UInt32_Partial(byte[] bs, int off, int len) |
| 203 | + internal static uint BE_To_UInt32_High(byte[] bs, int off, int len) |
| 204 | + { |
| 205 | + return BE_To_UInt32_Low(bs, off, len) << ((4 - len) << 3); |
| 206 | + } |
| 207 | + |
| 208 | + internal static uint BE_To_UInt32_Low(byte[] bs, int off, int len) |
184 | 209 | {
|
185 | 210 | Debug.Assert(1 <= len && len <= 4);
|
186 | 211 |
|
@@ -238,6 +263,24 @@ internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
|
238 | 263 | #endif
|
239 | 264 | }
|
240 | 265 |
|
| 266 | + internal static void UInt64_To_BE_High(ulong n, byte[] bs, int off, int len) |
| 267 | + { |
| 268 | + Debug.Assert(1 <= len && len <= 8); |
| 269 | + |
| 270 | + int pos = 56; |
| 271 | + bs[off] = (byte)(n >> pos); |
| 272 | + for (int i = 1; i < len; ++i) |
| 273 | + { |
| 274 | + pos -= 8; |
| 275 | + bs[off + i] = (byte)(n >> pos); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + internal static void UInt64_To_BE_Low(ulong n, byte[] bs, int off, int len) |
| 280 | + { |
| 281 | + UInt64_To_BE_High(n << ((8 - len) << 3), bs, off, len); |
| 282 | + } |
| 283 | + |
241 | 284 | internal static byte[] UInt64_To_BE(ulong[] ns)
|
242 | 285 | {
|
243 | 286 | byte[] bs = new byte[8 * ns.Length];
|
@@ -285,7 +328,12 @@ internal static ulong BE_To_UInt64(byte[] bs, int off)
|
285 | 328 | #endif
|
286 | 329 | }
|
287 | 330 |
|
288 |
| - internal static ulong BE_To_UInt64_Partial(byte[] bs, int off, int len) |
| 331 | + internal static ulong BE_To_UInt64_High(byte[] bs, int off, int len) |
| 332 | + { |
| 333 | + return BE_To_UInt64_Low(bs, off, len) << ((8 - len) << 3); |
| 334 | + } |
| 335 | + |
| 336 | + internal static ulong BE_To_UInt64_Low(byte[] bs, int off, int len) |
289 | 337 | {
|
290 | 338 | Debug.Assert(1 <= len && len <= 8);
|
291 | 339 |
|
@@ -591,7 +639,12 @@ internal static void BE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
|
591 | 639 | }
|
592 | 640 |
|
593 | 641 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
594 |
| - internal static uint BE_To_UInt32_Partial(ReadOnlySpan<byte> bs) |
| 642 | + internal static uint BE_To_UInt32_High(ReadOnlySpan<byte> bs) |
| 643 | + { |
| 644 | + return BE_To_UInt32_Low(bs) << ((4 - bs.Length) << 3); |
| 645 | + } |
| 646 | + |
| 647 | + internal static uint BE_To_UInt32_Low(ReadOnlySpan<byte> bs) |
595 | 648 | {
|
596 | 649 | int len = bs.Length;
|
597 | 650 | Debug.Assert(1 <= len && len <= 4);
|
@@ -622,7 +675,12 @@ internal static void BE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
|
622 | 675 | }
|
623 | 676 |
|
624 | 677 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
625 |
| - internal static ulong BE_To_UInt64_Partial(ReadOnlySpan<byte> bs) |
| 678 | + internal static ulong BE_To_UInt64_High(ReadOnlySpan<byte> bs) |
| 679 | + { |
| 680 | + return BE_To_UInt64_Low(bs) << ((8 - bs.Length) << 3); |
| 681 | + } |
| 682 | + |
| 683 | + internal static ulong BE_To_UInt64_Low(ReadOnlySpan<byte> bs) |
626 | 684 | {
|
627 | 685 | int len = bs.Length;
|
628 | 686 | Debug.Assert(1 <= len && len <= 8);
|
@@ -692,6 +750,26 @@ internal static void UInt32_To_BE(uint n, Span<byte> bs)
|
692 | 750 | BinaryPrimitives.WriteUInt32BigEndian(bs, n);
|
693 | 751 | }
|
694 | 752 |
|
| 753 | + internal static void UInt32_To_BE_High(uint n, Span<byte> bs) |
| 754 | + { |
| 755 | + int len = bs.Length; |
| 756 | + Debug.Assert(1 <= len && len <= 4); |
| 757 | + |
| 758 | + int pos = 24; |
| 759 | + bs[0] = (byte)(n >> pos); |
| 760 | + for (int i = 1; i < len; ++i) |
| 761 | + { |
| 762 | + pos -= 8; |
| 763 | + bs[i] = (byte)(n >> pos); |
| 764 | + } |
| 765 | + } |
| 766 | + |
| 767 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 768 | + internal static void UInt32_To_BE_Low(uint n, Span<byte> bs) |
| 769 | + { |
| 770 | + UInt32_To_BE_High(n << ((4 - bs.Length) << 3), bs); |
| 771 | + } |
| 772 | + |
695 | 773 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
696 | 774 | internal static void UInt32_To_BE(ReadOnlySpan<uint> ns, Span<byte> bs)
|
697 | 775 | {
|
@@ -724,6 +802,26 @@ internal static void UInt64_To_BE(ulong n, Span<byte> bs)
|
724 | 802 | BinaryPrimitives.WriteUInt64BigEndian(bs, n);
|
725 | 803 | }
|
726 | 804 |
|
| 805 | + internal static void UInt64_To_BE_High(ulong n, Span<byte> bs) |
| 806 | + { |
| 807 | + int len = bs.Length; |
| 808 | + Debug.Assert(1 <= len && len <= 8); |
| 809 | + |
| 810 | + int pos = 56; |
| 811 | + bs[0] = (byte)(n >> pos); |
| 812 | + for (int i = 1; i < len; ++i) |
| 813 | + { |
| 814 | + pos -= 8; |
| 815 | + bs[i] = (byte)(n >> pos); |
| 816 | + } |
| 817 | + } |
| 818 | + |
| 819 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 820 | + internal static void UInt64_To_BE_Low(ulong n, Span<byte> bs) |
| 821 | + { |
| 822 | + UInt64_To_BE_High(n << ((8 - bs.Length) << 3), bs); |
| 823 | + } |
| 824 | + |
727 | 825 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
728 | 826 | internal static void UInt64_To_BE(ReadOnlySpan<ulong> ns, Span<byte> bs)
|
729 | 827 | {
|
|
0 commit comments