diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-09 15:59:34 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-09 15:59:34 +0700 |
commit | e077bfbd6cb3b7a8480293a6b209960b62f64303 (patch) | |
tree | 77dcdc5b08efa1e5135c9faf45b6d5d553d9ad86 | |
parent | Optimise the initialisation of Ascon Hash (diff) | |
download | BouncyCastle.NET-ed25519-e077bfbd6cb3b7a8480293a6b209960b62f64303.tar.xz |
Add new Pack methods
-rw-r--r-- | crypto/src/crypto/util/Pack.cs | 93 |
1 files changed, 88 insertions, 5 deletions
diff --git a/crypto/src/crypto/util/Pack.cs b/crypto/src/crypto/util/Pack.cs index 2a551fcab..e00ba39fa 100644 --- a/crypto/src/crypto/util/Pack.cs +++ b/crypto/src/crypto/util/Pack.cs @@ -391,6 +391,31 @@ namespace Org.BouncyCastle.Crypto.Utilities return bs; } + internal static byte[] UInt16_To_LE(ushort[] ns) + { + byte[] bs = new byte[2 * ns.Length]; + UInt16_To_LE(ns, bs, 0); + return bs; + } + + internal static void UInt16_To_LE(ushort[] ns, byte[] bs, int off) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt16_To_LE(ns[i], bs, off); + off += 2; + } + } + + internal static void UInt16_To_LE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) + { + for (int i = 0; i < nsLen; ++i) + { + UInt16_To_LE(ns[nsOff + i], bs, bsOff); + bsOff += 2; + } + } + internal static ushort LE_To_UInt16(byte[] bs) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -413,6 +438,31 @@ namespace Org.BouncyCastle.Crypto.Utilities #endif } + internal static void LE_To_UInt16(byte[] bs, int off, ushort[] ns) + { + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = LE_To_UInt16(bs, off); + off += 2; + } + } + + internal static void LE_To_UInt16(byte[] bs, int bOff, ushort[] ns, int nOff, int count) + { + for (int i = 0; i < count; ++i) + { + ns[nOff + i] = LE_To_UInt16(bs, bOff); + bOff += 2; + } + } + + internal static ushort[] LE_To_UInt16(byte[] bs, int off, int count) + { + ushort[] ns = new ushort[count]; + LE_To_UInt16(bs, off, ns); + return ns; + } + internal static byte[] UInt32_To_LE(uint n) { byte[] bs = new byte[4]; @@ -521,11 +571,7 @@ namespace Org.BouncyCastle.Crypto.Utilities internal static uint[] LE_To_UInt32(byte[] bs, int off, int count) { uint[] ns = new uint[count]; - for (int i = 0; i < ns.Length; ++i) - { - ns[i] = LE_To_UInt32(bs, off); - off += 4; - } + LE_To_UInt32(bs, off, ns); return ns; } @@ -621,6 +667,13 @@ namespace Org.BouncyCastle.Crypto.Utilities } } + internal static ulong[] LE_To_UInt64(byte[] bs, int off, int count) + { + ulong[] ns = new ulong[count]; + LE_To_UInt64(bs, off, ns); + return ns; + } + #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint BE_To_UInt32(ReadOnlySpan<byte> bs) @@ -701,6 +754,16 @@ namespace Org.BouncyCastle.Crypto.Utilities } [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void LE_To_UInt16(ReadOnlySpan<byte> bs, Span<ushort> ns) + { + for (int i = 0; i < ns.Length; ++i) + { + ns[i] = LE_To_UInt16(bs); + bs = bs[2..]; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint LE_To_UInt32(ReadOnlySpan<byte> bs) { return BinaryPrimitives.ReadUInt32LittleEndian(bs); @@ -739,12 +802,32 @@ namespace Org.BouncyCastle.Crypto.Utilities } [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void UInt16_To_BE(ReadOnlySpan<ushort> ns, Span<byte> bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt16_To_BE(ns[i], bs); + bs = bs[2..]; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt16_To_LE(ushort n, Span<byte> bs) { BinaryPrimitives.WriteUInt16LittleEndian(bs, n); } [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void UInt16_To_LE(ReadOnlySpan<ushort> ns, Span<byte> bs) + { + for (int i = 0; i < ns.Length; ++i) + { + UInt16_To_LE(ns[i], bs); + bs = bs[2..]; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void UInt32_To_BE(uint n, Span<byte> bs) { BinaryPrimitives.WriteUInt32BigEndian(bs, n); |