Skip to content

Commit

Permalink
Nullability context and code refactor (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Pieter-Jan Briers <[email protected]>
  • Loading branch information
AgentFire and PJB3005 authored Dec 12, 2023
1 parent f19cea8 commit e2a5762
Show file tree
Hide file tree
Showing 50 changed files with 1,292 additions and 1,209 deletions.
12 changes: 9 additions & 3 deletions Lidgren.Network/Lidgren.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<AssemblyName>SpaceWizards.Lidgren.Network</AssemblyName>
<IncludeSymbols>true</IncludeSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<!-- Uncomment the following line to not get statistics in RELEASE mode -->
<DefineConstants>$(DefineConstants);USE_RELEASE_STATISTICS</DefineConstants>
Expand All @@ -28,11 +30,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
48 changes: 23 additions & 25 deletions Lidgren.Network/NetBigInteger.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;

namespace Lidgren.Network
{
Expand Down Expand Up @@ -53,6 +53,7 @@ private static int GetByteLength(

private NetBigInteger()
{
m_magnitude = null!;
}

private NetBigInteger(
Expand Down Expand Up @@ -155,7 +156,7 @@ public NetBigInteger(
}

// strip leading zeros from the string str
while (index < str.Length && Int32.Parse(str[index].ToString(), style) == 0)
while (index < str.Length && int.Parse(str[index].ToString(), style) == 0)
{
index++;
}
Expand Down Expand Up @@ -555,7 +556,7 @@ public NetBigInteger And(

return result;
}

private int calcBitLength(
int indx,
int[] mag)
Expand Down Expand Up @@ -643,7 +644,7 @@ public int CompareTo(
return CompareTo((NetBigInteger)obj);
}


// unsigned comparison on two arrays - note the arrays may
// start with leading zeros.
private static int CompareTo(
Expand Down Expand Up @@ -889,12 +890,12 @@ public NetBigInteger[] DivideAndRemainder(
}

public override bool Equals(
object obj)
object? obj)
{
if (obj == this)
return true;

NetBigInteger biggie = obj as NetBigInteger;
NetBigInteger? biggie = obj as NetBigInteger;
if (biggie == null)
return false;

Expand Down Expand Up @@ -971,7 +972,7 @@ public int IntValue
: -m_magnitude[m_magnitude.Length - 1];
}
}

public NetBigInteger Max(
NetBigInteger value)
{
Expand Down Expand Up @@ -1001,8 +1002,7 @@ public NetBigInteger ModInverse(
if (m.m_sign < 1)
throw new ArithmeticException("Modulus must be positive");

NetBigInteger x = new NetBigInteger();
NetBigInteger gcd = ExtEuclid(this, m, x, null);
NetBigInteger gcd = ExtEuclid(this, m, out NetBigInteger x, null);

if (!gcd.Equals(One))
throw new ArithmeticException("Numbers not relatively prime.");
Expand All @@ -1020,8 +1020,8 @@ public NetBigInteger ModInverse(
private static NetBigInteger ExtEuclid(
NetBigInteger a,
NetBigInteger b,
NetBigInteger u1Out,
NetBigInteger u2Out)
out NetBigInteger u1Out,
NetBigInteger? u2Out)
{
NetBigInteger u1 = One;
NetBigInteger u3 = a;
Expand All @@ -1041,11 +1041,7 @@ private static NetBigInteger ExtEuclid(
v3 = q[1];
}

if (u1Out != null)
{
u1Out.m_sign = u1.m_sign;
u1Out.m_magnitude = u1.m_magnitude;
}
u1Out = new NetBigInteger(u1.m_sign, u1.m_magnitude, false);

if (u2Out != null)
{
Expand Down Expand Up @@ -1081,8 +1077,8 @@ public NetBigInteger ModPow(
if (m_sign == 0)
return Zero;

int[] zVal = null;
int[] yAccum = null;
int[]? zVal = null;
int[]? yAccum = null;
int[] yVal;

// Montgomery exponentiation is only possible if the modulus is odd,
Expand Down Expand Up @@ -1136,6 +1132,9 @@ public NetBigInteger ModPow(

yVal = new int[m.m_magnitude.Length];

NetException.Assert(yAccum != null);
NetException.Assert(zVal != null);

//
// from LSW to MSW
//
Expand Down Expand Up @@ -1189,8 +1188,7 @@ public NetBigInteger ModPow(
{
Multiply(yAccum, yVal, zVal);
Remainder(yAccum, m.m_magnitude);
Array.Copy(yAccum, yAccum.Length - yVal.Length, yVal, 0,
yVal.Length);
Array.Copy(yAccum, yAccum.Length - yVal.Length, yVal, 0, yVal.Length);
ZeroOut(yAccum);
}
}
Expand Down Expand Up @@ -1588,7 +1586,7 @@ public NetBigInteger Pow(int exp)

return y;
}

private int Remainder(
int m)
{
Expand Down Expand Up @@ -2186,7 +2184,7 @@ public string ToString(
else
{
// This is algorithm 1a from chapter 4.4 in Seminumerical Algorithms, slow but it works
Stack S = new Stack();
Stack<string> S = new Stack<string>();
NetBigInteger bs = ValueOf(radix);

NetBigInteger u = Abs();
Expand All @@ -2210,7 +2208,7 @@ public string ToString(
// Then pop the stack
while (S.Count != 0)
{
sb.Append((string)S.Pop());
sb.Append(S.Pop());
}
}

Expand Down Expand Up @@ -2344,7 +2342,7 @@ public bool TestBit(
return ((word >> (n % 32)) & 1) > 0;
}
}

#if WINDOWS_RUNTIME
internal sealed class Stack
{
Expand Down
Loading

0 comments on commit e2a5762

Please sign in to comment.