Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference System.Memory and use it in math/raw #569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crypto/src/BouncyCastle.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<AssemblyOriginatorKeyFile>..\..\BouncyCastle.NET.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<NoWarn>1591</NoWarn>
<LangVersion>10.0</LangVersion>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only needed for Index/Range syntax


<AssemblyName>BouncyCastle.Cryptography</AssemblyName>
<AssemblyTitle>BouncyCastle.NET Cryptography ($(TargetFramework))</AssemblyTitle>
Expand Down Expand Up @@ -87,6 +88,13 @@
<None Include="..\..\packageIcon.png" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' or '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
Comment on lines +95 to +97
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only needed for Index/Range syntax


<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
Expand All @@ -101,6 +109,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Comment on lines +112 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only needed for Index/Range syntax

</ItemGroup>

<Target Name="FixAssemblyAttributes" AfterTargets="GetBuildVersion">
Expand Down
6 changes: 0 additions & 6 deletions crypto/src/math/raw/Bits.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics;
#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif

namespace Org.BouncyCastle.Math.Raw
{
Expand Down Expand Up @@ -36,9 +34,7 @@ internal static ulong BitPermuteStep(ulong x, ulong m, int s)
#endif
internal static void BitPermuteStep2(ref uint hi, ref uint lo, uint m, int s)
{
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP1_1_OR_GREATER
Debug.Assert(!Unsafe.AreSame(ref hi, ref lo) || (m & (m << s)) == 0U);
#endif
Debug.Assert((m << s) >> s == m);

uint t = ((lo >> s) ^ hi) & m;
Expand All @@ -51,9 +47,7 @@ internal static void BitPermuteStep2(ref uint hi, ref uint lo, uint m, int s)
#endif
internal static void BitPermuteStep2(ref ulong hi, ref ulong lo, ulong m, int s)
{
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP1_1_OR_GREATER
Debug.Assert(!Unsafe.AreSame(ref hi, ref lo) || (m & (m << s)) == 0UL);
#endif
Debug.Assert((m << s) >> s == m);

ulong t = ((lo >> s) ^ hi) & m;
Expand Down
30 changes: 2 additions & 28 deletions crypto/src/math/raw/Interleave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,9 @@ internal static ulong Expand32to64(uint x)

internal static void Expand64To128(ulong x, ulong[] z, int zOff)
{
#if NETCOREAPP3_0_OR_GREATER
if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled)
{
z[zOff ] = Bmi2.X64.ParallelBitDeposit(x , 0x5555555555555555UL);
z[zOff + 1] = Bmi2.X64.ParallelBitDeposit(x >> 32, 0x5555555555555555UL);
return;
}
#endif

// "shuffle" low half to even bits and high half to odd bits
x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);

z[zOff ] = (x ) & M64;
z[zOff + 1] = (x >> 1) & M64;
Expand64To128(x, z.AsSpan(zOff));
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
internal static void Expand64To128(ulong x, Span<ulong> z)
{
#if NETCOREAPP3_0_OR_GREATER
Expand All @@ -108,19 +90,12 @@ internal static void Expand64To128(ulong x, Span<ulong> z)
z[0] = (x ) & M64;
z[1] = (x >> 1) & M64;
}
#endif

internal static void Expand64To128(ulong[] xs, int xsOff, int xsLen, ulong[] zs, int zsOff)
{
int xsPos = xsLen, zsPos = zsOff + (xsLen << 1);
while (--xsPos >= 0)
{
zsPos -= 2;
Expand64To128(xs[xsOff + xsPos], zs, zsPos);
}
Expand64To128(xs.AsSpan(xsOff, xsLen), zs.AsSpan(zsOff));
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
internal static void Expand64To128(ReadOnlySpan<ulong> xs, Span<ulong> zs)
{
int xsPos = xs.Length, zsPos = xs.Length << 1;
Expand All @@ -131,7 +106,6 @@ internal static void Expand64To128(ReadOnlySpan<ulong> xs, Span<ulong> zs)
Expand64To128(xs[xsPos], zs[zsPos..]);
}
}
#endif

internal static ulong Expand64To128Rev(ulong x, out ulong low)
{
Expand Down
Loading