diff --git a/CLVMDotNet/src/CLVM/Casts.cs b/CLVMDotNet/src/CLVM/Casts.cs
index f186735..11ffcd0 100644
--- a/CLVMDotNet/src/CLVM/Casts.cs
+++ b/CLVMDotNet/src/CLVM/Casts.cs
@@ -1,3 +1,4 @@
+using System.Net.Sockets;
using System.Numerics;
namespace CLVMDotNet.CLVM
@@ -29,12 +30,27 @@ public static BigInteger IntFromBytes(byte[] blob)
///
public static byte[] IntToBytes(BigInteger v)
{
+ if (v == 0)
+ {
+ byte[] emptyByteArray = new byte[0];
+ return emptyByteArray;
+ }
+
//v can fit into a byte 0-255 (unsigned)
if (v >= byte.MinValue && v <= byte.MaxValue)
{
var intValue = (byte)v;
- byte[] byteArray = new[] { intValue };
- return byteArray;
+ var b = (byte)0x00;
+ if (v < 128)
+ {
+ byte[] byteArray = new[] { intValue };
+ return byteArray;
+ }
+ else
+ {
+ byte[] byteArray = new[] { b,intValue };
+ return byteArray;
+ }
}
//v can fit into an sbyte -128 to 127 (signed)
@@ -42,7 +58,6 @@ public static byte[] IntToBytes(BigInteger v)
{
sbyte sbyteValue = (sbyte)v;
byte byteValue = (byte)sbyteValue;
-
byte[] byteArray = new[] { byteValue };
return byteArray;
}
@@ -59,20 +74,22 @@ public static byte[] IntToBytes(BigInteger v)
//v can fit into a long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
if (v >= long.MinValue && v <= long.MaxValue)
{
- long shortValue = (long)v;
- byte[] byteArray = BitConverter.GetBytes(shortValue);
- Array.Reverse(byteArray);
-
- int pos = 0;
- while (byteArray.Length > 1 && (byteArray[0] == 0xFF || byteArray[0] == 0x00))
+ if (v == 0)
{
- if (pos != 0)
- byteArray = byteArray.Skip(1).ToArray();
- pos++;
+ return new byte[0];
}
-
- return byteArray;
+ byte[] result = v.ToByteArray();
+
+ // if (result[0] == 0x00)
+ // {
+ // // Remove leading 0x00 byte if present
+ // byte[] minimalResult = new byte[result.Length - 1];
+ // Array.Copy(result, 1, minimalResult, 0, minimalResult.Length);
+ // return minimalResult;
+ // }
+ result = result.Reverse().ToArray();
+ return result;
}
//python equivalent of numbers larger than a long is a bigInteger
else
@@ -88,19 +105,7 @@ public static byte[] IntToBytes(BigInteger v)
{
byteArray = byteArray.Skip(1).ToArray();
}
-
- if (!v.IsZero)
- {
- if (byteArray[0] >= 0x80)
- {
- byteArray = new byte[] { 0 }.Concat(byteArray).ToArray();
- }
- }
- else
- {
- byteArray = new byte[0];
- }
-
+
return byteArray;
}
}
diff --git a/CLVMDotNet/tests/CLVM/Casts/IntToBytes.cs b/CLVMDotNet/tests/CLVM/Casts/IntToBytes.cs
index b00c82d..cc09138 100644
--- a/CLVMDotNet/tests/CLVM/Casts/IntToBytes.cs
+++ b/CLVMDotNet/tests/CLVM/Casts/IntToBytes.cs
@@ -8,7 +8,7 @@ namespace CLVMDotNet.Tests.CLVM.Casts
public class IntToBytes
{
[Theory]
- [InlineData("0", new byte[] { 0x00 })]
+ [InlineData("0", new byte[] { })]
[InlineData("1", new byte[] { 0x01 })]
[InlineData("8", new byte[] { 0x08 })]
[InlineData("16", new byte[] { 0x10 })]
@@ -25,7 +25,20 @@ public class IntToBytes
[InlineData("20482048204820482048", new byte[] { 0x01, 0x1C, 0x3E, 0xDA, 0x52, 0xE0, 0xC0, 0x88, 0x00 })]
[InlineData("-1", new byte[] { 255 })]
[InlineData("-128", new byte[] { 0x80 })]
- //[InlineData("-129", new byte[] { 0xFF, 0x7F })]
+ [InlineData("-129", new byte[] { 0xFF, 0x7F })]
+ [InlineData("-256", new byte[] { 0xFF, 0x00 })]
+ [InlineData("-512", new byte[] { 0xFE, 0x00 })]
+ [InlineData("-1024", new byte[] { 0xFC, 0x00 })]
+ [InlineData("-2048", new byte[] { 0xF8, 0x00 })]
+ [InlineData("-4096", new byte[] { 0xF0, 0x00 })]
+ [InlineData("-10241024", new byte[] { 0xFF, 0x63, 0xBC, 0x00 })]
+ [InlineData("-204820482048", new byte[] { 0xD0, 0x4F, 0xBF, 0x78, 0x00 })]
+ [InlineData("-20482048204820482048", new byte[] { 0xFE, 0xE3, 0xC1, 0x25, 0xAD, 0x1F, 0x3F, 0x78, 0x00 })]
+ //[InlineData("-204820482048204820482048204820482048204820482048204820482048", new byte[]
+ // {
+ // 0xDF, 0x5E, 0xC6, 0x63, 0xAD, 0x6F, 0xBA, 0x4E, 0x4A, 0x38, 0x39, 0x33, 0xCA, 0xD0, 0x3C, 0x22,
+ // 0xCE, 0x56, 0x2A, 0x58, 0xBD, 0x0C, 0x9F, 0x3F, 0x78, 0x00
+ // })]
public void IntToBytes_returns_expectedbytes(string numberStr, byte[] expectedBytes)
{
BigInteger number = BigInteger.Parse(numberStr);
diff --git a/CLVMDotNet/tests/CLVM/Operators/OperatorTests.cs b/CLVMDotNet/tests/CLVM/Operators/OperatorTests.cs
index e761b1a..066f2ee 100644
--- a/CLVMDotNet/tests/CLVM/Operators/OperatorTests.cs
+++ b/CLVMDotNet/tests/CLVM/Operators/OperatorTests.cs
@@ -47,12 +47,8 @@ public void OpDivideThrowsExceptionIfDividingByZero()
);
Assert.Contains("div with 0", errorMessage.Message);
}
-
- ///
- /// TODO: This test needs further clarification on how to represent signed or unsigned numbers
- ///
- [Fact(Skip =
- "Skipping until it's decided how bytes will be signed i.e representing negative numbers - will it be an sbyte?")]
+
+ [Fact()]
public void OpDivideThrowsExceptionWithNegativeOperand1()
{
var errorMessage =
@@ -376,15 +372,10 @@ public void OpAshThrowsWhenLessThanTwoArguments()
x.SExp.To(new int[] { 1, 2, 4 })));
Assert.Contains("ash takes exactly 2 arguments", errorMessage.Message);
}
-
- //TODO: validate when integer has a leading 0
#endregion
#region OpLsh
- ///
- /// TODO: Review ByteOrder on Casts.IntToBytes
- ///
- [Fact(Skip = "seems to be an issue with byte order with large numbers")]
+ [Fact()]
public void OpLsh()
{
var result = x.Operator.ApplyOperator(new byte[] { 0x17 }, x.SExp.To(new int[] { 1, 45 }));