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

Remove Linq from ECDSA and UInt classes #1208

Closed
wants to merge 19 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Optimize Reverse
shargon committed Nov 7, 2019
commit 44dacd6cc0195e23f5921e9393a8f4fa7b898b1e
5 changes: 2 additions & 3 deletions neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
@@ -19,8 +19,7 @@ public static byte[] Decode(string input)
throw new FormatException();
bi += index * BigInteger.Pow(58, input.Length - 1 - i);
}
byte[] bytes = bi.ToByteArray();
Array.Reverse(bytes);
byte[] bytes = bi.ToByteArray().Reverse();
bool stripSignByte = bytes.Length > 1 && bytes[0] == 0 && bytes[1] >= 0x80;
int leadingZeros = 0;
for (int i = 0; i < input.Length && input[i] == Alphabet[0]; i++)
@@ -35,7 +34,7 @@ public static byte[] Decode(string input)

public static string Encode(byte[] input)
{
BigInteger value = new BigInteger(new byte[1].Concat(input).Reverse().ToArray());
BigInteger value = new BigInteger(new byte[1].Concat(input).ToArray().Reverse());
StringBuilder sb = new StringBuilder();
while (value >= 58)
{
2 changes: 1 addition & 1 deletion neo/Cryptography/ECC/ECFieldElement.cs
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@ public byte[] ToByteArray()
{
byte[] data = Value.ToByteArray();
if (data.Length == 32)
return data.Reverse().ToArray();
return data.Reverse();
if (data.Length > 32)
return data.Take(32).Reverse().ToArray();
return Enumerable.Repeat<byte>(0, 32 - data.Length).Concat(data.Reverse()).ToArray();
6 changes: 3 additions & 3 deletions neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
@@ -141,10 +141,10 @@ public byte[] EncodePoint(bool commpressed)
else
{
data = new byte[65];
byte[] yBytes = Y.Value.ToByteArray().ReverseBuffer();
byte[] yBytes = Y.Value.ToByteArray().Reverse();
Buffer.BlockCopy(yBytes, 0, data, 65 - yBytes.Length, yBytes.Length);
}
byte[] xBytes = X.Value.ToByteArray().ReverseBuffer();
byte[] xBytes = X.Value.ToByteArray().Reverse();
Buffer.BlockCopy(xBytes, 0, data, 33 - xBytes.Length, xBytes.Length);
data[0] = commpressed ? Y.Value.IsEven ? (byte)0x02 : (byte)0x03 : (byte)0x04;
return data;
@@ -377,7 +377,7 @@ private static sbyte[] WindowNaf(sbyte width, BigInteger k)
throw new ArgumentException();
if (p.IsInfinity)
return p;
BigInteger k = new BigInteger(n.ReverseBuffer().Concat(new byte[1]).ToArray());
BigInteger k = new BigInteger(n.Reverse().Concat(new byte[1]).ToArray());
if (k.Sign == 0)
return p.Curve.Infinity;
return Multiply(p, k);
4 changes: 2 additions & 2 deletions neo/Helper.cs
Original file line number Diff line number Diff line change
@@ -34,9 +34,9 @@ private static int BitLen(int w)
: (w < 1 << 29 ? (w < 1 << 28 ? 28 : 29) : (w < 1 << 30 ? 30 : 31)))));
}

public static byte[] ReverseBuffer(this byte[] origin)
public static T[] Reverse<T>(this T[] origin)
{
byte[] ret = new byte[origin.Length];
T[] ret = new T[origin.Length];
for (int x = 0, m = ret.Length - 1; x <= m; x++)
{
ret[x] = origin[m - x];
3 changes: 1 addition & 2 deletions neo/Network/P2P/Peer.cs
Original file line number Diff line number Diff line change
@@ -90,8 +90,7 @@ protected void ConnectToPeer(IPEndPoint endPoint, bool isTrusted = false)

private static bool IsIntranetAddress(IPAddress address)
{
byte[] data = address.MapToIPv4().GetAddressBytes();
Array.Reverse(data);
byte[] data = address.MapToIPv4().GetAddressBytes().Reverse();
uint value = data.ToUInt32(0);
return (value & 0xff000000) == 0x0a000000 || (value & 0xff000000) == 0x7f000000 || (value & 0xfff00000) == 0xac100000 || (value & 0xffff0000) == 0xc0a80000 || (value & 0xffff0000) == 0xa9fe0000;
}
4 changes: 2 additions & 2 deletions neo/UInt160.cs
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ public unsafe bool Equals(UInt160 other)
value = value.Substring(2);
if (value.Length != Length * 2)
throw new FormatException();
return new UInt160(value.HexToBytes().Reverse().ToArray());
return new UInt160(value.HexToBytes().Reverse());
}

/// <summary>
@@ -110,7 +110,7 @@ public static bool TryParse(string s, out UInt160 result)
result = null;
return false;
}
result = new UInt160(data.Reverse().ToArray());
result = new UInt160(data.Reverse());
return true;
}

4 changes: 2 additions & 2 deletions neo/UInt256.cs
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ public unsafe bool Equals(UInt256 other)
s = s.Substring(2);
if (s.Length != Length * 2)
throw new FormatException();
return new UInt256(s.HexToBytes().Reverse().ToArray());
return new UInt256(s.HexToBytes().Reverse());
}

/// <summary>
@@ -111,7 +111,7 @@ public static bool TryParse(string s, out UInt256 result)
result = null;
return false;
}
result = new UInt256(data.Reverse().ToArray());
result = new UInt256(data.Reverse());
return true;
}