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

Enhance security (1) #1403

Merged
merged 11 commits into from
Jan 14, 2020
Merged
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
1 change: 1 addition & 0 deletions src/neo/Cryptography/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class Base58

public static byte[] Base58CheckDecode(this string input)
{
if (input is null) throw new ArgumentNullException(nameof(input));
byte[] buffer = Decode(input);
if (buffer.Length < 4) throw new FormatException();
byte[] checksum = buffer.Sha256(0, buffer.Length - 4).Sha256();
Expand Down
2 changes: 2 additions & 0 deletions src/neo/Cryptography/BloomFilter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Linq;

Expand All @@ -16,6 +17,7 @@ public class BloomFilter

public BloomFilter(int m, int k, uint nTweak, byte[] elements = null)
{
if (k < 0 || m < 0) throw new ArgumentOutOfRangeException();
this.seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray();
this.bits = elements == null ? new BitArray(m) : new BitArray(elements);
this.bits.Length = m;
Expand Down
107 changes: 0 additions & 107 deletions src/neo/Cryptography/ECC/ECDsa.cs

This file was deleted.

5 changes: 4 additions & 1 deletion src/neo/Cryptography/ECC/ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class ECFieldElement : IComparable<ECFieldElement>, IEquatable<ECFieldE

public ECFieldElement(BigInteger value, ECCurve curve)
{
if (curve is null)
throw new ArgumentNullException(nameof(curve));
if (value >= curve.Q)
throw new ArgumentException("x value too large in field element");
this.Value = value;
Expand All @@ -19,6 +21,7 @@ public ECFieldElement(BigInteger value, ECCurve curve)
public int CompareTo(ECFieldElement other)
{
if (ReferenceEquals(this, other)) return 0;
if (!curve.Equals(other.curve)) throw new InvalidOperationException("Invalid comparision for points with different curves");
return Value.CompareTo(other.Value);
}

Expand All @@ -35,7 +38,7 @@ public override bool Equals(object obj)

public bool Equals(ECFieldElement other)
{
return Value.Equals(other.Value);
return Value.Equals(other.Value) && curve.Equals(other.curve);
}

private static BigInteger[] FastLucasSequence(BigInteger p, BigInteger P, BigInteger Q, BigInteger k)
Expand Down
3 changes: 2 additions & 1 deletion src/neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ECPoint() : this(null, null, ECCurve.Secp256r1) { }

internal ECPoint(ECFieldElement x, ECFieldElement y, ECCurve curve)
{
if ((x != null && y == null) || (x == null && y != null))
if ((x is null ^ y is null) || (curve is null))
throw new ArgumentException("Exactly one of the field elements is null");
this.X = x;
this.Y = y;
Expand All @@ -31,6 +31,7 @@ internal ECPoint(ECFieldElement x, ECFieldElement y, ECCurve curve)

public int CompareTo(ECPoint other)
{
if (!Curve.Equals(other.Curve)) throw new InvalidOperationException("Invalid comparision for points with different curves");
if (ReferenceEquals(this, other)) return 0;
int result = X.CompareTo(other.X);
if (result != 0) return result;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Cryptography/MerkleTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MerkleTree

internal MerkleTree(UInt256[] hashes)
{
if (hashes.Length == 0) throw new ArgumentException();
if (hashes is null || hashes.Length == 0) throw new ArgumentException();
this.root = Build(hashes.Select(p => new MerkleTreeNode { Hash = p }).ToArray());
int depth = 1;
for (MerkleTreeNode i = root; i.LeftChild != null; i = i.LeftChild)
Expand Down
55 changes: 0 additions & 55 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECDsa.cs

This file was deleted.

24 changes: 24 additions & 0 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,37 @@ public void TestECFieldElementConstructor()
action.Should().Throw<ArgumentException>();
}

[TestMethod]
public void TestCompareTo()
{
ECFieldElement X1 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1);
ECFieldElement Y1 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1);
ECFieldElement X2 = new ECFieldElement(new BigInteger(300), ECCurve.Secp256k1);
ECFieldElement Y2 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256k1);
ECFieldElement X3 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256r1);
ECFieldElement Y3 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256r1);
ECPoint point1 = new ECPoint(X1, Y1, ECCurve.Secp256k1);
ECPoint point2 = new ECPoint(X2, Y1, ECCurve.Secp256k1);
ECPoint point3 = new ECPoint(X1, Y2, ECCurve.Secp256k1);
ECPoint point4 = new ECPoint(X3, Y3, ECCurve.Secp256r1);

point1.CompareTo(point1).Should().Be(0);
point1.CompareTo(point2).Should().Be(-1);
point2.CompareTo(point1).Should().Be(1);
point1.CompareTo(point3).Should().Be(-1);
point3.CompareTo(point1).Should().Be(1);
Action action = () => point3.CompareTo(point4);
action.Should().Throw<InvalidOperationException>();
}

[TestMethod]
public void TestEquals()
{
BigInteger input = new BigInteger(100);
object element = new ECFieldElement(input, ECCurve.Secp256k1);
element.Equals(element).Should().BeTrue();
element.Equals(1).Should().BeFalse();
element.Equals(new ECFieldElement(input, ECCurve.Secp256r1)).Should().BeFalse();

input = new BigInteger(200);
element.Equals(new ECFieldElement(input, ECCurve.Secp256k1)).Should().BeFalse();
Expand Down
18 changes: 7 additions & 11 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ public static byte[] generatePrivateKey(int privateKeyLength)
public void TestCompareTo()
{
ECFieldElement X1 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1);
ECFieldElement Y1 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1);
ECFieldElement X2 = new ECFieldElement(new BigInteger(300), ECCurve.Secp256k1);
ECFieldElement Y2 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256k1);
ECPoint point1 = new ECPoint(X1, Y1, ECCurve.Secp256k1);
ECPoint point2 = new ECPoint(X2, Y1, ECCurve.Secp256k1);
ECPoint point3 = new ECPoint(X1, Y2, ECCurve.Secp256k1);
ECFieldElement X2 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1);
ECFieldElement X3 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256r1);

point1.CompareTo(point1).Should().Be(0);
point1.CompareTo(point2).Should().Be(-1);
point2.CompareTo(point1).Should().Be(1);
point1.CompareTo(point3).Should().Be(-1);
point3.CompareTo(point1).Should().Be(1);
X1.CompareTo(X2).Should().Be(-1);
Action action = () => X1.CompareTo(X3);
action.Should().Throw<InvalidOperationException>();
}

[TestMethod]
Expand All @@ -60,6 +54,8 @@ public void TestECPointConstructor()
action.Should().Throw<ArgumentException>();
action = () => new ECPoint(null, Y, ECCurve.Secp256k1);
action.Should().Throw<ArgumentException>();
action = () => new ECPoint(null, Y, null);
action.Should().Throw<ArgumentException>();
}

[TestMethod]
Expand Down