Skip to content

Commit fd64191

Browse files
committed
Replace _Partial methods with _High/_Low variants
1 parent b1bf9f1 commit fd64191

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

crypto/src/crypto/util/Pack.cs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Buffers.Binary;
44
#endif
55
using System.Diagnostics;
6+
#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
67
using System.Runtime.CompilerServices;
8+
#endif
79

810
namespace Org.BouncyCastle.Crypto.Utilities
911
{
@@ -124,6 +126,24 @@ internal static void UInt32_To_BE(uint n, byte[] bs, int off)
124126
#endif
125127
}
126128

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+
127147
internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
128148
{
129149
for (int i = 0; i < ns.Length; ++i)
@@ -180,7 +200,12 @@ internal static uint BE_To_UInt32(byte[] bs, int off)
180200
#endif
181201
}
182202

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)
184209
{
185210
Debug.Assert(1 <= len && len <= 4);
186211

@@ -238,6 +263,24 @@ internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
238263
#endif
239264
}
240265

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+
241284
internal static byte[] UInt64_To_BE(ulong[] ns)
242285
{
243286
byte[] bs = new byte[8 * ns.Length];
@@ -285,7 +328,12 @@ internal static ulong BE_To_UInt64(byte[] bs, int off)
285328
#endif
286329
}
287330

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)
289337
{
290338
Debug.Assert(1 <= len && len <= 8);
291339

@@ -591,7 +639,12 @@ internal static void BE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
591639
}
592640

593641
[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)
595648
{
596649
int len = bs.Length;
597650
Debug.Assert(1 <= len && len <= 4);
@@ -622,7 +675,12 @@ internal static void BE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
622675
}
623676

624677
[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)
626684
{
627685
int len = bs.Length;
628686
Debug.Assert(1 <= len && len <= 8);
@@ -692,6 +750,26 @@ internal static void UInt32_To_BE(uint n, Span<byte> bs)
692750
BinaryPrimitives.WriteUInt32BigEndian(bs, n);
693751
}
694752

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+
695773
[MethodImpl(MethodImplOptions.AggressiveInlining)]
696774
internal static void UInt32_To_BE(ReadOnlySpan<uint> ns, Span<byte> bs)
697775
{
@@ -724,6 +802,26 @@ internal static void UInt64_To_BE(ulong n, Span<byte> bs)
724802
BinaryPrimitives.WriteUInt64BigEndian(bs, n);
725803
}
726804

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+
727825
[MethodImpl(MethodImplOptions.AggressiveInlining)]
728826
internal static void UInt64_To_BE(ReadOnlySpan<ulong> ns, Span<byte> bs)
729827
{

crypto/src/pqc/crypto/sphincsplus/SPHINCSPlusEngine.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ public override IndexedDigest H_msg(byte[] prf, byte[] pkSeed, byte[] pkRoot, by
281281

282282
// tree index
283283
// currently, only indexes up to 64 bits are supported
284-
ulong treeIndex = Pack.BE_To_UInt64_Partial(output, forsMsgBytes, (int)treeBytes)
284+
ulong treeIndex = Pack.BE_To_UInt64_Low(output, forsMsgBytes, (int)treeBytes)
285285
& ulong.MaxValue >> (64 - (int)treeBits);
286286

287-
uint leafIndex = Pack.BE_To_UInt32_Partial(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
287+
uint leafIndex = Pack.BE_To_UInt32_Low(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
288288
& uint.MaxValue >> (32 - (int)leafBits);
289289

290290
return new IndexedDigest(treeIndex, leafIndex, Arrays.CopyOfRange(output, 0, forsMsgBytes));
@@ -492,10 +492,10 @@ public override IndexedDigest H_msg(byte[] R, byte[] pkSeed, byte[] pkRoot, byte
492492

493493
// tree index
494494
// currently, only indexes up to 64 bits are supported
495-
ulong treeIndex = Pack.BE_To_UInt64_Partial(output, forsMsgBytes, (int)treeBytes)
495+
ulong treeIndex = Pack.BE_To_UInt64_Low(output, forsMsgBytes, (int)treeBytes)
496496
& ulong.MaxValue >> (64 - (int)treeBits);
497497

498-
uint leafIndex = Pack.BE_To_UInt32_Partial(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
498+
uint leafIndex = Pack.BE_To_UInt32_Low(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
499499
& uint.MaxValue >> (32 - (int)leafBits);
500500

501501
return new IndexedDigest(treeIndex, leafIndex, Arrays.CopyOfRange(output, 0, forsMsgBytes));
@@ -681,10 +681,10 @@ public override IndexedDigest H_msg(byte[] prf, byte[] pkSeed, byte[] pkRoot, by
681681

682682
// tree index
683683
// currently, only indexes up to 64 bits are supported
684-
ulong treeIndex = Pack.BE_To_UInt64_Partial(output, forsMsgBytes, (int)treeBytes)
684+
ulong treeIndex = Pack.BE_To_UInt64_Low(output, forsMsgBytes, (int)treeBytes)
685685
& ulong.MaxValue >> (64 - (int)treeBits);
686686

687-
uint leafIndex = Pack.BE_To_UInt32_Partial(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
687+
uint leafIndex = Pack.BE_To_UInt32_Low(output, forsMsgBytes + (int)treeBytes, (int)leafBytes)
688688
& uint.MaxValue >> (32 - (int)leafBits);
689689

690690
return new IndexedDigest(treeIndex, leafIndex, Arrays.CopyOfRange(output, 0, forsMsgBytes));
@@ -839,10 +839,10 @@ public override IndexedDigest H_msg(byte[] prf, byte[] pkSeed, byte[] pkRoot, by
839839

840840
// tree index
841841
// currently, only indexes up to 64 bits are supported
842-
ulong treeIndex = Pack.BE_To_UInt64_Partial(indices[..treeBytes])
842+
ulong treeIndex = Pack.BE_To_UInt64_Low(indices[..treeBytes])
843843
& ulong.MaxValue >> (64 - treeBits);
844844

845-
uint leafIndex = Pack.BE_To_UInt32_Partial(indices[treeBytes..])
845+
uint leafIndex = Pack.BE_To_UInt32_Low(indices[treeBytes..])
846846
& uint.MaxValue >> (32 - leafBits);
847847

848848
return new IndexedDigest(treeIndex, leafIndex, output);

0 commit comments

Comments
 (0)