From 4852871b65a89b82b5df69c02bc61315c8b69019 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 25 Jul 2022 16:13:30 +0700 Subject: Round out Span variants of Pack methods --- crypto/src/crypto/util/Pack.cs | 133 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 117 insertions(+), 16 deletions(-) diff --git a/crypto/src/crypto/util/Pack.cs b/crypto/src/crypto/util/Pack.cs index 9fd533cfc..fd6ee1d9f 100644 --- a/crypto/src/crypto/util/Pack.cs +++ b/crypto/src/crypto/util/Pack.cs @@ -441,33 +441,134 @@ namespace Org.BouncyCastle.Crypto.Utilities } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - internal static uint LE_To_UInt32(Span s) + internal static uint BE_To_UInt32(ReadOnlySpan bs) { - return s[0] - | (uint)s[1] << 8 - | (uint)s[2] << 16 - | (uint)s[3] << 24; + return (uint)bs[0] << 24 + | (uint)bs[1] << 16 + | (uint)bs[2] << 8 + | bs[3]; + } + + internal static void BE_To_UInt32(ReadOnlySpan bs, Span ns) + { + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = BE_To_UInt32(bs); + bs = bs[4..]; + } + } + + internal static ulong BE_To_UInt64(ReadOnlySpan bs) + { + uint hi = BE_To_UInt32(bs); + uint lo = BE_To_UInt32(bs[4..]); + return ((ulong)hi << 32) | lo; } - internal static ulong LE_To_UInt64(Span s) + internal static void BE_To_UInt64(ReadOnlySpan bs, Span ns) { - uint lo = LE_To_UInt32(s); - uint hi = LE_To_UInt32(s.Slice(4)); + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = BE_To_UInt64(bs); + bs = bs[8..]; + } + } + + internal static uint LE_To_UInt32(ReadOnlySpan bs) + { + return bs[0] + | (uint)bs[1] << 8 + | (uint)bs[2] << 16 + | (uint)bs[3] << 24; + } + + internal static void LE_To_UInt32(ReadOnlySpan bs, Span ns) + { + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = LE_To_UInt32(bs); + bs = bs[4..]; + } + } + + internal static ulong LE_To_UInt64(ReadOnlySpan bs) + { + uint lo = LE_To_UInt32(bs); + uint hi = LE_To_UInt32(bs[4..]); return (ulong)hi << 32 | lo; } - internal static void UInt32_To_LE(uint n, Span s) + internal static void LE_To_UInt64(ReadOnlySpan bs, Span ns) { - s[0] = (byte)n; - s[1] = (byte)(n >> 8); - s[2] = (byte)(n >> 16); - s[3] = (byte)(n >> 24); + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = LE_To_UInt64(bs); + bs = bs[8..]; + } } - internal static void UInt64_To_LE(ulong n, Span s) + internal static void UInt32_To_BE(uint n, Span bs) { - UInt32_To_LE((uint)n, s); - UInt32_To_LE((uint)(n >> 32), s.Slice(4)); + bs[0] = (byte)(n >> 24); + bs[1] = (byte)(n >> 16); + bs[2] = (byte)(n >> 8); + bs[3] = (byte) n; + } + + internal static void UInt32_To_BE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt32_To_BE(ns[i], bs); + bs = bs[4..]; + } + } + + internal static void UInt32_To_LE(uint n, Span bs) + { + bs[0] = (byte) n; + bs[1] = (byte)(n >> 8); + bs[2] = (byte)(n >> 16); + bs[3] = (byte)(n >> 24); + } + + internal static void UInt32_To_LE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt32_To_LE(ns[i], bs); + bs = bs[4..]; + } + } + + internal static void UInt64_To_BE(ulong n, Span bs) + { + UInt32_To_BE((uint)(n >> 32), bs); + UInt32_To_BE((uint)n, bs[4..]); + } + + internal static void UInt64_To_BE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt64_To_BE(ns[i], bs); + bs = bs[8..]; + } + } + + internal static void UInt64_To_LE(ulong n, Span bs) + { + UInt32_To_LE((uint)n, bs); + UInt32_To_LE((uint)(n >> 32), bs[4..]); + } + + internal static void UInt64_To_LE(ReadOnlySpan ns, Span bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt64_To_LE(ns[i], bs); + bs = bs[8..]; + } } #endif } -- cgit 1.5.1