Skip to content

Commit

Permalink
Fixed int to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinOnFrontEnd committed Jan 21, 2024
1 parent acf485c commit a3d6bc1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
59 changes: 32 additions & 27 deletions CLVMDotNet/src/CLVM/Casts.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Sockets;
using System.Numerics;

namespace CLVMDotNet.CLVM
Expand Down Expand Up @@ -29,20 +30,34 @@ public static BigInteger IntFromBytes(byte[] blob)
/// <returns></returns>
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)
if (v >= sbyte.MinValue && v <= sbyte.MaxValue)
{
sbyte sbyteValue = (sbyte)v;
byte byteValue = (byte)sbyteValue;

byte[] byteArray = new[] { byteValue };
return byteArray;
}
Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down
17 changes: 15 additions & 2 deletions CLVMDotNet/tests/CLVM/Casts/IntToBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })]
Expand All @@ -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);
Expand Down
15 changes: 3 additions & 12 deletions CLVMDotNet/tests/CLVM/Operators/OperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ public void OpDivideThrowsExceptionIfDividingByZero()
);
Assert.Contains("div with 0", errorMessage.Message);
}

/// <summary>
/// TODO: This test needs further clarification on how to represent signed or unsigned numbers
/// </summary>
[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 =
Expand Down Expand Up @@ -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
/// <summary>
/// TODO: Review ByteOrder on Casts.IntToBytes
/// </summary>
[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 }));
Expand Down

0 comments on commit a3d6bc1

Please sign in to comment.