From f5bea821aaf4cca5e3a1660160c284950b6256cd Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Sat, 21 Nov 2020 01:00:36 +0800 Subject: [PATCH 1/4] .NET 5.0 --- src/neo/Cryptography/ECC/ECCurve.cs | 2 +- src/neo/Cryptography/ECC/ECDsa.cs | 4 +- src/neo/Cryptography/ECC/ECFieldElement.cs | 4 +- src/neo/Cryptography/ECC/ECPoint.cs | 2 +- src/neo/Helper.cs | 7 ---- src/neo/IO/ReferenceEqualityComparer.cs | 25 ------------- src/neo/VM/Helper.cs | 4 +- src/neo/neo.csproj | 12 +++--- .../IO/UT_ReferenceEqualityComparer.cs | 37 ------------------- tests/neo.UnitTests/UT_Helper.cs | 17 +-------- tests/neo.UnitTests/neo.UnitTests.csproj | 14 +++---- 11 files changed, 22 insertions(+), 106 deletions(-) delete mode 100644 src/neo/IO/ReferenceEqualityComparer.cs delete mode 100644 tests/neo.UnitTests/IO/UT_ReferenceEqualityComparer.cs diff --git a/src/neo/Cryptography/ECC/ECCurve.cs b/src/neo/Cryptography/ECC/ECCurve.cs index 6fc476ff84..546c794070 100644 --- a/src/neo/Cryptography/ECC/ECCurve.cs +++ b/src/neo/Cryptography/ECC/ECCurve.cs @@ -17,7 +17,7 @@ public class ECCurve private ECCurve(BigInteger Q, BigInteger A, BigInteger B, BigInteger N, byte[] G) { this.Q = Q; - this.ExpectedECPointLength = (Q.GetBitLength() + 7) / 8; + this.ExpectedECPointLength = ((int)Q.GetBitLength() + 7) / 8; this.A = new ECFieldElement(A, this); this.B = new ECFieldElement(B, this); this.N = N; diff --git a/src/neo/Cryptography/ECC/ECDsa.cs b/src/neo/Cryptography/ECC/ECDsa.cs index b14fc67ead..1c0a17d7a9 100644 --- a/src/neo/Cryptography/ECC/ECDsa.cs +++ b/src/neo/Cryptography/ECC/ECDsa.cs @@ -46,7 +46,7 @@ public BigInteger[] GenerateSignature(ReadOnlySpan message) { do { - k = rng.NextBigInteger(curve.N.GetBitLength()); + k = rng.NextBigInteger((int)curve.N.GetBitLength()); } while (k.Sign == 0 || k.CompareTo(curve.N) >= 0); ECPoint p = ECPoint.Multiply(curve.G, k); @@ -67,7 +67,7 @@ public BigInteger[] GenerateSignature(ReadOnlySpan message) private static ECPoint SumOfTwoMultiplies(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) { - int m = Math.Max(k.GetBitLength(), l.GetBitLength()); + int m = (int)Math.Max(k.GetBitLength(), l.GetBitLength()); ECPoint Z = P + Q; ECPoint R = P.Curve.Infinity; for (int i = m - 1; i >= 0; --i) diff --git a/src/neo/Cryptography/ECC/ECFieldElement.cs b/src/neo/Cryptography/ECC/ECFieldElement.cs index 9ebd48082d..edfdc497b0 100644 --- a/src/neo/Cryptography/ECC/ECFieldElement.cs +++ b/src/neo/Cryptography/ECC/ECFieldElement.cs @@ -43,7 +43,7 @@ public bool Equals(ECFieldElement other) private static BigInteger[] FastLucasSequence(BigInteger p, BigInteger P, BigInteger Q, BigInteger k) { - int n = k.GetBitLength(); + int n = (int)k.GetBitLength(); int s = k.GetLowestSetBit(); BigInteger Uh = 1; @@ -115,7 +115,7 @@ public ECFieldElement Sqrt() BigInteger P; do { - P = rand.NextBigInteger(curve.Q.GetBitLength()); + P = rand.NextBigInteger((int)curve.Q.GetBitLength()); } while (P >= curve.Q || BigInteger.ModPow(P * P - fourQ, legendreExponent, curve.Q) != qMinusOne); BigInteger[] result = FastLucasSequence(curve.Q, P, Q, k); diff --git a/src/neo/Cryptography/ECC/ECPoint.cs b/src/neo/Cryptography/ECC/ECPoint.cs index f42c465793..49a3c16074 100644 --- a/src/neo/Cryptography/ECC/ECPoint.cs +++ b/src/neo/Cryptography/ECC/ECPoint.cs @@ -203,7 +203,7 @@ public override int GetHashCode() internal static ECPoint Multiply(ECPoint p, BigInteger k) { // floor(log2(k)) - int m = k.GetBitLength(); + int m = (int)k.GetBitLength(); // width of the Window NAF sbyte width; diff --git a/src/neo/Helper.cs b/src/neo/Helper.cs index dafeba7643..69fd7ddba4 100644 --- a/src/neo/Helper.cs +++ b/src/neo/Helper.cs @@ -58,13 +58,6 @@ public static byte[] Concat(ReadOnlySpan a, ReadOnlySpan b) return buffer; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static int GetBitLength(this BigInteger i) - { - byte[] b = i.ToByteArray(); - return (b.Length - 1) * 8 + BitLen(i.Sign > 0 ? b[b.Length - 1] : 255 - b[b.Length - 1]); - } - internal static int GetLowestSetBit(this BigInteger i) { if (i.Sign == 0) diff --git a/src/neo/IO/ReferenceEqualityComparer.cs b/src/neo/IO/ReferenceEqualityComparer.cs deleted file mode 100644 index 4c736c1874..0000000000 --- a/src/neo/IO/ReferenceEqualityComparer.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System.Runtime.CompilerServices; - -namespace Neo.IO -{ - internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer - { - public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer(); - - private ReferenceEqualityComparer() - { - } - - public new bool Equals(object x, object y) - { - return x == y; - } - - public int GetHashCode(object obj) - { - return RuntimeHelpers.GetHashCode(obj); - } - } -} diff --git a/src/neo/VM/Helper.cs b/src/neo/VM/Helper.cs index 6437b75118..98c373317e 100644 --- a/src/neo/VM/Helper.cs +++ b/src/neo/VM/Helper.cs @@ -198,7 +198,7 @@ private static JObject ToJson(StackItem item, HashSet context) switch (item) { case Array array: - context ??= new HashSet(ReferenceEqualityComparer.Default); + context ??= new HashSet(ReferenceEqualityComparer.Instance); if (!context.Add(array)) throw new InvalidOperationException(); json["value"] = new JArray(array.Select(p => ToJson(p, context))); break; @@ -213,7 +213,7 @@ private static JObject ToJson(StackItem item, HashSet context) json["value"] = integer.GetInteger().ToString(); break; case Map map: - context ??= new HashSet(ReferenceEqualityComparer.Default); + context ??= new HashSet(ReferenceEqualityComparer.Instance); if (!context.Add(map)) throw new InvalidOperationException(); json["value"] = new JArray(map.Select(p => { diff --git a/src/neo/neo.csproj b/src/neo/neo.csproj index a848dc6e7b..b72a393459 100644 --- a/src/neo/neo.csproj +++ b/src/neo/neo.csproj @@ -6,7 +6,7 @@ 3.0.0 preview3 The Neo Project - netstandard2.1 + net5.0 true Neo Neo @@ -21,14 +21,14 @@ - + - + - - - + + + diff --git a/tests/neo.UnitTests/IO/UT_ReferenceEqualityComparer.cs b/tests/neo.UnitTests/IO/UT_ReferenceEqualityComparer.cs deleted file mode 100644 index 1db8dcabb9..0000000000 --- a/tests/neo.UnitTests/IO/UT_ReferenceEqualityComparer.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Neo.IO; -using System; -using System.Diagnostics.CodeAnalysis; - -namespace Neo.UnitTests -{ - [TestClass] - public class UT_ReferenceEqualityComparer - { - private class FakeEquals : IEquatable - { - public bool Equals([AllowNull] FakeEquals other) - { - return true; - } - - public override int GetHashCode() - { - return 123; - } - } - - [TestMethod] - public void TestEqual() - { - var a = new FakeEquals(); - var b = new FakeEquals(); - var check = ReferenceEqualityComparer.Default; - - Assert.IsFalse(check.Equals(a, b)); - Assert.AreNotEqual(check.GetHashCode(a), check.GetHashCode(b)); - Assert.AreNotEqual(123, check.GetHashCode(a)); - Assert.AreNotEqual(123, check.GetHashCode(b)); - } - } -} diff --git a/tests/neo.UnitTests/UT_Helper.cs b/tests/neo.UnitTests/UT_Helper.cs index 50964a3ce9..9151d50c13 100644 --- a/tests/neo.UnitTests/UT_Helper.cs +++ b/tests/neo.UnitTests/UT_Helper.cs @@ -56,21 +56,6 @@ public void TestGetLowestSetBit() big4.GetLowestSetBit().Should().Be(63); } - [TestMethod] - public void TestGetBitLength() - { - new BigInteger(100).GetBitLength().Should().Be(7); - new BigInteger(-100).GetBitLength().Should().Be(7); - new BigInteger(0).GetBitLength().Should().Be(8); - new BigInteger(512).GetBitLength().Should().Be(10); - new BigInteger(short.MinValue).GetBitLength().Should().Be(15); - new BigInteger(short.MaxValue).GetBitLength().Should().Be(15); - new BigInteger(int.MinValue).GetBitLength().Should().Be(31); - new BigInteger(int.MaxValue).GetBitLength().Should().Be(31); - new BigInteger(long.MinValue).GetBitLength().Should().Be(63); - new BigInteger(long.MaxValue).GetBitLength().Should().Be(63); - } - [TestMethod] public void TestHexToBytes() { @@ -187,7 +172,7 @@ public void TestToHexString() public void TestGetVersion() { string version = typeof(TestMethodAttribute).Assembly.GetVersion(); - version.Should().Be("14.0.4701.02"); + version.Should().Be("14.0.4908.02"); // assembly without version diff --git a/tests/neo.UnitTests/neo.UnitTests.csproj b/tests/neo.UnitTests/neo.UnitTests.csproj index c1d5a7200d..32b91b429b 100644 --- a/tests/neo.UnitTests/neo.UnitTests.csproj +++ b/tests/neo.UnitTests/neo.UnitTests.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + net5.0 Neo.UnitTests Neo.UnitTests true @@ -16,12 +16,12 @@ - - - - - - + + + + + + From c283f9c1a625082632871f63c1588ac0d35119ae Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Sat, 21 Nov 2020 01:02:36 +0800 Subject: [PATCH 2/4] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2b2faaeaf7..959db1fe8a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,7 +6,7 @@ on: pull_request: env: - DOTNET_VERSION: 3.1.402 + DOTNET_VERSION: 5.0.100 jobs: From 784a1cb0b3b4d3ed423b21727548eb2cf3f24dfa Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 24 Nov 2020 15:12:58 +0800 Subject: [PATCH 3/4] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 959db1fe8a..5236e3fd4b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,7 +21,7 @@ jobs: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Check format run: | - dotnet tool install --version 3.2.111002 --tool-path ./ dotnet-format --add-source https://dotnet.myget.org/F/format/api/v3/index.json + dotnet tool install --version 5.0.142902 --tool-path ./ dotnet-format --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json ./dotnet-format --check --dry-run -v diagnostic - name: Test run: | From fefd386acd4299fa37028983a27f4d5a242b501a Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 24 Nov 2020 15:21:10 +0800 Subject: [PATCH 4/4] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5236e3fd4b..f1751fe36a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,7 +22,7 @@ jobs: - name: Check format run: | dotnet tool install --version 5.0.142902 --tool-path ./ dotnet-format --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json - ./dotnet-format --check --dry-run -v diagnostic + ./dotnet-format --check -v diagnostic - name: Test run: | find tests -name *.csproj | xargs -I % dotnet add % package coverlet.msbuild