diff --git a/src/Neo.Extensions/BigIntegerExtensions.cs b/src/Neo.Extensions/BigIntegerExtensions.cs index 1507fe1686..8b86cb9d76 100644 --- a/src/Neo.Extensions/BigIntegerExtensions.cs +++ b/src/Neo.Extensions/BigIntegerExtensions.cs @@ -160,6 +160,12 @@ public static BigInteger Sqrt(this BigInteger value) return z; } + internal static BigInteger GetLowPart(this BigInteger value, int bitCount) + { + var mask = (BigInteger.One << bitCount) - 1; + return value & mask; + } + /// /// Gets the number of bits required for shortest two's complement representation of the current instance without the sign bit. /// Note: This method is imprecise and might not work as expected with integers larger than 256 bits if less than .NET5. diff --git a/src/Neo.Extensions/Factories/RandomNumberFactory.cs b/src/Neo.Extensions/Factories/RandomNumberFactory.cs new file mode 100644 index 0000000000..08bcd5a0c4 --- /dev/null +++ b/src/Neo.Extensions/Factories/RandomNumberFactory.cs @@ -0,0 +1,291 @@ +// Copyright (C) 2015-2025 The Neo Project. +// +// RandomNumberFactory.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; +using System.Buffers.Binary; +using System.Numerics; +using System.Security.Cryptography; + +namespace Neo.Extensions.Factories +{ + public static class RandomNumberFactory + { + public static sbyte NextSByte() => + NextSByte(0, sbyte.MaxValue); + + public static sbyte NextSByte(sbyte maxValue) + { + if (maxValue < 0) + throw new ArgumentOutOfRangeException(nameof(maxValue)); + + return NextSByte(0, maxValue); + } + + public static sbyte NextSByte(sbyte minValue, sbyte maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (sbyte)(NextUInt32((uint)(maxValue - minValue)) + minValue); + } + + public static byte NextByte() => + NextByte(0, byte.MaxValue); + + public static byte NextByte(byte maxValue) => + NextByte(0, maxValue); + + public static byte NextByte(byte minValue, byte maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (byte)(NextUInt32((uint)(maxValue - minValue)) + minValue); + } + + public static short NextInt16() => + NextInt16(0, short.MaxValue); + + public static short NextInt16(short maxValue) + { + if (maxValue < 0) + throw new ArgumentOutOfRangeException(nameof(maxValue)); + + return NextInt16(0, maxValue); + } + + public static short NextInt16(short minValue, short maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (short)(NextUInt32((uint)(maxValue - minValue)) + minValue); + } + + public static ushort NextUInt16() => + NextUInt16(0, ushort.MaxValue); + + public static ushort NextUInt16(ushort maxValue) => + NextUInt16(0, maxValue); + + public static ushort NextUInt16(ushort minValue, ushort maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (ushort)(NextUInt32((uint)(maxValue - minValue)) + minValue); + } + + public static int NextInt32() => + NextInt32(0, int.MaxValue); + + public static int NextInt32(int maxValue) + { + if (maxValue < 0) + throw new ArgumentOutOfRangeException(nameof(maxValue)); + + return NextInt32(0, maxValue); + } + + public static int NextInt32(int minValue, int maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (int)NextUInt32((uint)(maxValue - minValue)) + minValue; + } + + public static uint NextUInt32() + { + Span longBytes = stackalloc byte[4]; + RandomNumberGenerator.Fill(longBytes); + return BinaryPrimitives.ReadUInt32LittleEndian(longBytes); + } + + public static uint NextUInt32(uint maxValue) + { + var randomProduct = (ulong)maxValue * NextUInt32(); + var lowPart = (uint)randomProduct; + + if (lowPart < maxValue) + { + var remainder = (0u - maxValue) % maxValue; + + while (lowPart < remainder) + { + randomProduct = (ulong)maxValue * NextUInt32(); + lowPart = (uint)randomProduct; + } + } + + return (uint)(randomProduct >> 32); + } + + public static uint NextUInt32(uint minValue, uint maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return NextUInt32(maxValue - minValue) + minValue; + } + + public static long NextInt64() => + NextInt64(0L, long.MaxValue); + + public static long NextInt64(long maxValue) + { + return NextInt64(0L, maxValue); + } + + public static long NextInt64(long minValue, long maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return (long)NextUInt64((ulong)(maxValue - minValue)) + minValue; + } + + public static ulong NextUInt64() + { + Span longBytes = stackalloc byte[8]; + RandomNumberGenerator.Fill(longBytes); + return BinaryPrimitives.ReadUInt64LittleEndian(longBytes); + } + + public static ulong NextUInt64(ulong maxValue) + { + var randomProduct = BigMul(maxValue, NextUInt64(), out var lowPart); + + if (lowPart < maxValue) + { + var remainder = (0ul - maxValue) % maxValue; + + while (lowPart < remainder) + { + randomProduct = BigMul(maxValue, NextUInt64(), out lowPart); + } + } + + return randomProduct; + } + + public static ulong NextUInt64(ulong minValue, ulong maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return NextUInt64(maxValue - minValue) + minValue; + } + + public static BigInteger NextBigInteger(BigInteger minValue, BigInteger maxValue) + { + if (minValue == maxValue) return maxValue; + + if (minValue > maxValue) + throw new ArgumentOutOfRangeException(nameof(minValue)); + + return NextBigInteger(maxValue - minValue) + minValue; + } + + public static BigInteger NextBigInteger(BigInteger maxValue) + { + if (maxValue.Sign < 0) + throw new ArgumentOutOfRangeException(nameof(maxValue)); + + var maxValueBits = maxValue.GetByteCount() * 8; + var maxValueSize = BigInteger.Pow(2, maxValueBits); + + var randomProduct = maxValue * NextBigInteger(maxValueBits); + var randomProductBits = randomProduct.GetByteCount() * 8; + + var lowPart = randomProduct.GetLowPart(maxValueBits); + + if (lowPart < maxValue) + { + var remainder = (maxValueSize - maxValue) % maxValue; + + while (lowPart < remainder) + { + randomProduct = maxValue * NextBigInteger(maxValueBits); + randomProductBits = randomProduct.GetByteCount() * 8; + lowPart = randomProduct.GetLowPart(maxValueBits); + } + } + + var result = randomProduct >> (randomProductBits - maxValueBits); + + // Since BigInteger doesn't have a max value or bit size + // anything over 'maxValue' return zero + if (result >= maxValue) + return BigInteger.Zero; + + return result; + } + + public static BigInteger NextBigInteger(int sizeInBits) + { + if (sizeInBits < 0) + throw new ArgumentException("sizeInBits must be non-negative."); + + if (sizeInBits == 0) + return BigInteger.Zero; + + Span b = stackalloc byte[sizeInBits / 8 + 1]; + RandomNumberGenerator.Fill(b); + + if (sizeInBits % 8 == 0) + b[^1] = 0; + else + b[^1] &= (byte)((1 << sizeInBits % 8) - 1); + + return new BigInteger(b); + } + + private static ulong BigMul(ulong a, ulong b, out ulong low) + { + // Adaptation of algorithm for multiplication + // of 32-bit unsigned integers described + // in Hacker's Delight by Henry S. Warren, Jr. (ISBN 0-201-91465-4), Chapter 8 + // Basically, it's an optimized version of FOIL method applied to + // low and high dwords of each operand + + // Use 32-bit uints to optimize the fallback for 32-bit platforms. + var al = (uint)a; + var ah = (uint)(a >> 32); + var bl = (uint)b; + var bh = (uint)(b >> 32); + + var mull = ((ulong)al) * bl; + var t = ((ulong)ah) * bl + (mull >> 32); + var tl = ((ulong)al) * bh + (uint)t; + + low = (tl << 32) | (uint)mull; + + return ((ulong)ah) * bh + (t >> 32) + (tl >> 32); + } + } +} diff --git a/src/Neo.Extensions/RandomExtensions.cs b/src/Neo.Extensions/RandomExtensions.cs deleted file mode 100644 index 7706cb9277..0000000000 --- a/src/Neo.Extensions/RandomExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2015-2025 The Neo Project. -// -// RandomExtensions.cs file belongs to the neo project and is free -// software distributed under the MIT software license, see the -// accompanying file LICENSE in the main directory of the -// repository or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using System; -using System.Numerics; - -namespace Neo.Extensions -{ - public static class RandomExtensions - { - public static BigInteger NextBigInteger(this Random rand, int sizeInBits) - { - if (sizeInBits < 0) - throw new ArgumentException($"sizeInBits must be non-negative, but received {sizeInBits}.", nameof(sizeInBits)); - if (sizeInBits == 0) - return 0; - Span b = stackalloc byte[sizeInBits / 8 + 1]; - rand.NextBytes(b); - if (sizeInBits % 8 == 0) - b[^1] = 0; - else - b[^1] &= (byte)((1 << sizeInBits % 8) - 1); - return new BigInteger(b); - } - } -} diff --git a/src/Neo/Cryptography/ECC/ECFieldElement.cs b/src/Neo/Cryptography/ECC/ECFieldElement.cs index 8f3cf448ed..33eb2a52bd 100644 --- a/src/Neo/Cryptography/ECC/ECFieldElement.cs +++ b/src/Neo/Cryptography/ECC/ECFieldElement.cs @@ -12,6 +12,7 @@ #nullable enable using Neo.Extensions; +using Neo.Extensions.Factories; using System; using System.Numerics; @@ -121,11 +122,10 @@ public override int GetHashCode() BigInteger U, V; do { - Random rand = new(); - BigInteger P; + var P = BigInteger.Zero; do { - P = rand.NextBigInteger((int)_curve.Q.GetBitLength()); + P = RandomNumberFactory.NextBigInteger((int)_curve.Q.GetBitLength()); } while (P >= _curve.Q || BigInteger.ModPow(P * P - fourQ, legendreExponent, _curve.Q) != qMinusOne); var result = FastLucasSequence(_curve.Q, P, Q, k); diff --git a/tests/Neo.Extensions.Tests/Factories/UT_RandomNumberFactory.cs b/tests/Neo.Extensions.Tests/Factories/UT_RandomNumberFactory.cs new file mode 100644 index 0000000000..a1e72b3075 --- /dev/null +++ b/tests/Neo.Extensions.Tests/Factories/UT_RandomNumberFactory.cs @@ -0,0 +1,291 @@ +// Copyright (C) 2015-2025 The Neo Project. +// +// UT_RandomNumberFactory.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + + +// Copyright (C) 2015-2025 The Neo Project. +// +// RandomNumberFactory.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.Extensions.Factories; +using System; +using System.Numerics; + +namespace Neo.Extensions.Tests.Factories +{ + [TestClass] + public class UT_RandomNumberFactory + { + [TestMethod] + public void CheckNextSByteInRange() + { + var expectedMax = sbyte.MaxValue; + sbyte expectedMin = 0; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextSByte(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextSByte(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextSByte(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextSByte(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextSByteNegative() + { + sbyte expectedMax = 0; + var expectedMin = sbyte.MinValue; + + var actualValue = RandomNumberFactory.NextSByte(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextSByteExceptions() + { + Assert.ThrowsExactly(() => RandomNumberFactory.NextSByte(-1)); + Assert.ThrowsExactly(() => RandomNumberFactory.NextSByte(-1, -2)); + } + + [TestMethod] + public void CheckNextByteInRange() + { + var expectedMax = byte.MaxValue; + var expectedMin = byte.MinValue; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextByte(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextByte(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextByte(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextByte(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt16InRange() + { + var expectedMax = short.MaxValue; + short expectedMin = 0; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextInt16(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextInt16(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextInt16(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextInt16(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt16InNegative() + { + short expectedMax = 0; + var expectedMin = short.MinValue; + + var actualValue = RandomNumberFactory.NextInt16(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextInt16(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt16Exceptions() + { + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt16(-1)); + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt16(-1, -2)); + } + + [TestMethod] + public void CheckNextUInt16InRange() + { + var expectedMax = ushort.MaxValue; + var expectedMin = ushort.MinValue; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextUInt16(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextUInt16(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextUInt16(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextUInt16(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt32InRange() + { + var expectedMax = int.MaxValue; + var expectedMin = 0; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextInt32(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextInt32(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextInt32(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextInt32(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt32InNegative() + { + var expectedMax = 0; + var expectedMin = int.MinValue; + + var actualValue = RandomNumberFactory.NextInt32(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt32Exceptions() + { + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt32(-1)); + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt32(-1, -2)); + } + + [TestMethod] + public void CheckNextUInt32InRange() + { + var expectedMax = uint.MaxValue; + var expectedMin = uint.MinValue; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextUInt32(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextUInt32(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextUInt32(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextUInt32(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt64InRange() + { + var expectedMax = long.MaxValue; + var expectedMin = 0L; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextInt64(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextInt64(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextInt64(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextInt64(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt64InNegative() + { + var expectedMax = 0L; + var expectedMin = long.MinValue; + + var actualValue = RandomNumberFactory.NextInt64(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextInt64Exceptions() + { + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt64(-1L)); + Assert.ThrowsExactly(() => RandomNumberFactory.NextInt64(-1L, -2L)); + } + + [TestMethod] + public void CheckNextUInt64InRange() + { + var expectedMax = ulong.MaxValue; + var expectedMin = ulong.MinValue; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextUInt64(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextUInt64(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextUInt64(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextUInt64(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextBigIntegerSizeInBits() + { + var actualValue = RandomNumberFactory.NextBigInteger(byte.MaxValue); + Assert.IsTrue(actualValue >= BigInteger.Zero); + + actualValue = RandomNumberFactory.NextBigInteger(0); + Assert.AreEqual(BigInteger.Zero, actualValue); + + Assert.ThrowsExactly(() => RandomNumberFactory.NextBigInteger(-1)); + } + + [TestMethod] + public void CheckNextBigIntegerInRange() + { + var expectedMax = BigInteger.Pow(2, 100); + var expectedMin = BigInteger.Zero; + + Assert.AreEqual(expectedMax, RandomNumberFactory.NextBigInteger(expectedMax, expectedMax)); + Assert.AreEqual(expectedMin, RandomNumberFactory.NextBigInteger(expectedMin, expectedMin)); + + var actualValue = RandomNumberFactory.NextBigInteger(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextBigInteger(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + + [TestMethod] + public void CheckNextBigIntegerInNegative() + { + var expectedMax = BigInteger.Zero; + var expectedMin = BigInteger.Pow(2, 100) * BigInteger.MinusOne; + + var actualValue = RandomNumberFactory.NextBigInteger(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + Assert.IsTrue(actualValue.Sign < 0); + } + + [TestMethod] + public void CheckNextBigIntegerMaxNegative() + { + Assert.ThrowsExactly(() => RandomNumberFactory.NextBigInteger(BigInteger.MinusOne)); + Assert.ThrowsExactly(() => RandomNumberFactory.NextBigInteger(BigInteger.MinusOne, -2)); + } + + [TestMethod] + public void CheckNextBigIntegerSmallValues() + { + var expectedMax = (BigInteger)10; + var expectedMin = BigInteger.Zero; + + var actualValue = RandomNumberFactory.NextBigInteger(expectedMin, expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + + actualValue = RandomNumberFactory.NextBigInteger(expectedMax); + Assert.IsTrue(actualValue >= expectedMin && actualValue <= expectedMax); + } + } +} diff --git a/tests/Neo.Extensions.Tests/UT_RandomExtensions.cs b/tests/Neo.Extensions.Tests/UT_RandomExtensions.cs deleted file mode 100644 index 220c17143d..0000000000 --- a/tests/Neo.Extensions.Tests/UT_RandomExtensions.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (C) 2015-2025 The Neo Project. -// -// UT_RandomExtensions.cs file belongs to the neo project and is free -// software distributed under the MIT software license, see the -// accompanying file LICENSE in the main directory of the -// repository or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using System; - -namespace Neo.Extensions.Tests -{ - [TestClass] - public class UT_RandomExtensions - { - [TestMethod] - public void TestNextBigIntegerForRandom() - { - Random ran = new(); - Action action1 = () => ran.NextBigInteger(-1); - Assert.ThrowsExactly(action1); - - Assert.AreEqual(0, ran.NextBigInteger(0)); - Assert.IsNotNull(ran.NextBigInteger(8)); - Assert.IsNotNull(ran.NextBigInteger(9)); - } - } -} diff --git a/tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs b/tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs index 138a9c90d3..0a15e78953 100644 --- a/tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs +++ b/tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs @@ -14,6 +14,7 @@ using Moq; using Neo.Cryptography; using Neo.Extensions; +using Neo.Extensions.Factories; using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.Persistence; @@ -66,13 +67,6 @@ public void TestSetup() Assert.AreEqual(0, _unit.Count); } - private static long LongRandom(long min, long max, Random rand) - { - // Only returns positive random long values. - long longRand = (long)rand.NextBigInteger(63); - return longRand % (max - min) + min; - } - private Transaction CreateTransactionWithFee(long fee) { Random random = new(); @@ -112,7 +106,7 @@ private Transaction CreateTransaction(long fee = -1) { if (fee != -1) return CreateTransactionWithFee(fee); - return CreateTransactionWithFee(LongRandom(100000, 100000000, TestUtils.TestRandom)); + return CreateTransactionWithFee(RandomNumberFactory.NextInt64(100000, 100000000)); } private void AddTransactions(int count) diff --git a/tests/Neo.UnitTests/UT_Helper.cs b/tests/Neo.UnitTests/UT_Helper.cs index 4dd4b73fa0..2838483f43 100644 --- a/tests/Neo.UnitTests/UT_Helper.cs +++ b/tests/Neo.UnitTests/UT_Helper.cs @@ -114,18 +114,6 @@ public void TestToByteArrayStandard() Assert.AreEqual("01", number.ToByteArrayStandard().ToHexString()); } - [TestMethod] - public void TestNextBigIntegerForRandom() - { - Random ran = new(); - Action action1 = () => ran.NextBigInteger(-1); - Assert.ThrowsExactly(() => action1()); - - Assert.AreEqual(0, ran.NextBigInteger(0)); - Assert.IsNotNull(ran.NextBigInteger(8)); - Assert.IsNotNull(ran.NextBigInteger(9)); - } - [TestMethod] public void TestUnmapForIPAddress() {