Skip to content
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
28 changes: 19 additions & 9 deletions src/Neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ public ReadOnlySpan<byte> GetSpan()
if (BitConverter.IsLittleEndian)
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ulong, byte>(ref _value1), Length);

return GetSpanLittleEndian();
}

internal Span<byte> GetSpanLittleEndian()
{
Span<byte> buffer = new byte[Length];
Serialize(buffer);
SerializeSafeLittleEndian(buffer);
return buffer; // Keep the same output as Serialize when BigEndian
}

Expand All @@ -123,17 +128,22 @@ public void Serialize(Span<byte> destination)
}
else
{
const int IxValue2 = sizeof(ulong);
const int IxValue3 = sizeof(ulong) * 2;

Span<byte> buffer = stackalloc byte[Length];
BinaryPrimitives.WriteUInt64LittleEndian(buffer, _value1);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue2..], _value2);
BinaryPrimitives.WriteUInt32LittleEndian(buffer[IxValue3..], _value3);
buffer.CopyTo(destination);
SerializeSafeLittleEndian(destination);
}
}

internal void SerializeSafeLittleEndian(Span<byte> destination)
{
const int IxValue2 = sizeof(ulong);
const int IxValue3 = sizeof(ulong) * 2;

Span<byte> buffer = stackalloc byte[Length];
BinaryPrimitives.WriteUInt64LittleEndian(buffer, _value1);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue2..], _value2);
BinaryPrimitives.WriteUInt32LittleEndian(buffer[IxValue3..], _value3);
buffer.CopyTo(destination);
}

/// <summary>
/// Parses an <see cref="UInt160"/> from the specified <see cref="string"/>.
/// </summary>
Expand Down
84 changes: 49 additions & 35 deletions src/Neo/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class UInt256 : IComparable<UInt256>, IEquatable<UInt256>, ISerializable,
/// </summary>
public static readonly UInt256 Zero = new();

[FieldOffset(0)] private ulong value1;
[FieldOffset(8)] private ulong value2;
[FieldOffset(16)] private ulong value3;
[FieldOffset(24)] private ulong value4;
[FieldOffset(0)] private ulong _value1;
[FieldOffset(8)] private ulong _value2;
[FieldOffset(16)] private ulong _value3;
[FieldOffset(24)] private ulong _value4;

public int Size => Length;

Expand All @@ -57,27 +57,27 @@ public UInt256(ReadOnlySpan<byte> value)
if (value.Length != Length)
throw new FormatException($"Invalid length: {value.Length}");

var span = MemoryMarshal.CreateSpan(ref Unsafe.As<ulong, byte>(ref value1), Length);
var span = MemoryMarshal.CreateSpan(ref Unsafe.As<ulong, byte>(ref _value1), Length);
value.CopyTo(span);
}

public int CompareTo(UInt256 other)
{
int result = value4.CompareTo(other.value4);
var result = _value4.CompareTo(other._value4);
if (result != 0) return result;
result = value3.CompareTo(other.value3);
result = _value3.CompareTo(other._value3);
if (result != 0) return result;
result = value2.CompareTo(other.value2);
result = _value2.CompareTo(other._value2);
if (result != 0) return result;
return value1.CompareTo(other.value1);
return _value1.CompareTo(other._value1);
}

public void Deserialize(ref MemoryReader reader)
{
value1 = reader.ReadUInt64();
value2 = reader.ReadUInt64();
value3 = reader.ReadUInt64();
value4 = reader.ReadUInt64();
_value1 = reader.ReadUInt64();
_value2 = reader.ReadUInt64();
_value3 = reader.ReadUInt64();
_value4 = reader.ReadUInt64();
}

public override bool Equals(object obj)
Expand All @@ -89,15 +89,15 @@ public override bool Equals(object obj)
public bool Equals(UInt256 other)
{
if (other is null) return false;
return value1 == other.value1
&& value2 == other.value2
&& value3 == other.value3
&& value4 == other.value4;
return _value1 == other._value1
&& _value2 == other._value2
&& _value3 == other._value3
&& _value4 == other._value4;
}

public override int GetHashCode()
{
return (int)value1;
return (int)_value1;
}

/// <summary>
Expand All @@ -108,10 +108,19 @@ public override int GetHashCode()
public ReadOnlySpan<byte> GetSpan()
{
if (BitConverter.IsLittleEndian)
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ulong, byte>(ref value1), Length);
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ulong, byte>(ref _value1), Length);

return GetSpanLittleEndian();
}

/// <summary>
/// Get the output as Serialize when BigEndian
/// </summary>
/// <returns>A Span that represents the ourput as Serialize when BigEndian.</returns>
internal Span<byte> GetSpanLittleEndian()
{
Span<byte> buffer = new byte[Length];
Serialize(buffer);
SerializeSafeLittleEndian(buffer);
return buffer; // Keep the same output as Serialize when BigEndian
}

Expand All @@ -129,34 +138,39 @@ public static UInt256 Parse(string value)

public void Serialize(BinaryWriter writer)
{
writer.Write(value1);
writer.Write(value2);
writer.Write(value3);
writer.Write(value4);
writer.Write(_value1);
writer.Write(_value2);
writer.Write(_value3);
writer.Write(_value4);
}

public void Serialize(Span<byte> destination)
{
if (BitConverter.IsLittleEndian)
{
var buffer = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ulong, byte>(ref value1), Length);
var buffer = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ulong, byte>(ref _value1), Length);
buffer.CopyTo(destination);
}
else
{
const int IxValue2 = sizeof(ulong);
const int IxValue3 = sizeof(ulong) * 2;
const int IxValue4 = sizeof(ulong) * 3;

Span<byte> buffer = stackalloc byte[Length];
BinaryPrimitives.WriteUInt64LittleEndian(buffer, value1);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue2..], value2);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue3..], value3);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue4..], value4);
buffer.CopyTo(destination);
SerializeSafeLittleEndian(destination);
}
}

internal void SerializeSafeLittleEndian(Span<byte> destination)
{
const int IxValue2 = sizeof(ulong);
const int IxValue3 = sizeof(ulong) * 2;
const int IxValue4 = sizeof(ulong) * 3;

Span<byte> buffer = stackalloc byte[Length];
BinaryPrimitives.WriteUInt64LittleEndian(buffer, _value1);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue2..], _value2);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue3..], _value3);
BinaryPrimitives.WriteUInt64LittleEndian(buffer[IxValue4..], _value4);
buffer.CopyTo(destination);
}

public override string ToString()
{
return "0x" + this.ToArray().ToHexString(reverse: true);
Expand Down
24 changes: 24 additions & 0 deletions tests/Neo.UnitTests/UT_UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ public void TestSpanAndSerialize()
((ISerializableSpan)value).Serialize(data.AsSpan());
CollectionAssert.AreEqual(data, value.ToArray());
}

[TestMethod]
public void TestSpanAndSerializeBigEndian()
{
// random data
var random = new Random();
var data = new byte[UInt160.Length];
random.NextBytes(data);

var valueBigEndian = new UInt160(data);
var valueLittleEndian = new UInt160(data);

var span = valueBigEndian.GetSpanLittleEndian();
Assert.IsTrue(span.SequenceEqual(valueBigEndian.ToArray()));

data = new byte[UInt160.Length];
valueBigEndian.SerializeSafeLittleEndian(data.AsSpan());
CollectionAssert.AreEqual(data, valueBigEndian.ToArray());

// Check that Serialize LittleEndian and Serialize BigEndian are equals
data = new byte[UInt160.Length];
valueLittleEndian.Serialize(data.AsSpan());
CollectionAssert.AreEqual(valueLittleEndian.ToArray(), valueBigEndian.ToArray());
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Neo.UnitTests/UT_UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,28 @@ public void TestSpanAndSerialize()
((ISerializableSpan)value).Serialize(data.AsSpan());
CollectionAssert.AreEqual(data, value.ToArray());
}

[TestMethod]
public void TestSpanAndSerializeBigEndian()
{
var random = new Random();
var data = new byte[UInt256.Length];
random.NextBytes(data);

var valueBigEndian = new UInt256(data);
var valueLittleEndian = new UInt256(data);

var span = valueBigEndian.GetSpanLittleEndian();
Assert.IsTrue(span.SequenceEqual(valueBigEndian.ToArray()));

data = new byte[UInt256.Length];
valueBigEndian.SerializeSafeLittleEndian(data.AsSpan());
CollectionAssert.AreEqual(data, valueBigEndian.ToArray());

// Check that Serialize LittleEndian and Serialize BigEndian are equals
data = new byte[UInt256.Length];
valueLittleEndian.Serialize(data.AsSpan());
CollectionAssert.AreEqual(valueLittleEndian.ToArray(), valueBigEndian.ToArray());
}
}
}