Skip to content

Commit

Permalink
LSV6 tests, predefined method
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 26, 2024
1 parent 1999a2a commit 586aadd
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ object ReflectionData {
},
mkMethod(clazz, "decodePoint", Array[Class[_]](cColl)) { (obj, args) =>
obj.asInstanceOf[SigmaDslBuilder].decodePoint(args(0).asInstanceOf[Coll[Byte]])
},
mkMethod(clazz, "fromBigEndianBytes", Array[Class[_]](cColl, classOf[RType[_]])) { (obj, args) =>
obj.asInstanceOf[SigmaDslBuilder].fromBigEndianBytes(args(0).asInstanceOf[Coll[Byte]])(args(1).asInstanceOf[RType[_]])
}
)
)
Expand Down
22 changes: 21 additions & 1 deletion data/shared/src/main/scala/sigma/ast/SigmaPredef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,25 @@ object SigmaPredef {
)
)

val FromBigEndianBytesFunc = PredefinedFunc("fromBigEndianBytes",
Lambda(Seq(paramT), Array("bytes" -> SByteArray), tT, None),
irInfo = PredefFuncInfo(
irBuilder = { case (u, args) =>
val resType = u.opType.tRange.asInstanceOf[SFunc].tRange
MethodCall(
Global,
SGlobalMethods.fromBigEndianBytesMethod.withConcreteTypes(Map(tT -> resType)),
args.toIndexedSeq,
Map(tT -> resType)
)
}),
docInfo = OperationInfo(MethodCall,
"""Deserializes provided big endian bytes into a numeric value of given type.
""".stripMargin,
Seq(ArgInfo("bytes", "bytes to deserialize"))
)
)

val globalFuncs: Map[String, PredefinedFunc] = Seq(
AllOfFunc,
AnyOfFunc,
Expand Down Expand Up @@ -448,7 +467,8 @@ object SigmaPredef {
SubstConstantsFunc,
ExecuteFromVarFunc,
ExecuteFromSelfRegFunc,
SerializeFunc
SerializeFunc,
FromBigEndianBytesFunc
).map(f => f.name -> f).toMap

def comparisonOp(symbolName: String, opDesc: ValueCompanion, desc: String, args: Seq[ArgInfo]) = {
Expand Down
94 changes: 91 additions & 3 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import sigma.ast.ErgoTree.ZeroHeader
import sigma.ast.SCollection.SByteArray
import sigma.ast.syntax.TrueSigmaProp
import sigma.ast.{SInt, _}
import sigma.data.{CBigInt, CBox, CHeader, ExactNumeric}
import sigma.data.{CBigInt, CBox, CHeader, CSigmaDslBuilder, ExactNumeric, RType}
import sigma.eval.{CostDetails, SigmaDsl, TracedCost}
import sigma.serialization.ValueCodes.OpCode
import sigma.data.{RType}
import sigma.util.Extensions.{BooleanOps, ByteOps, IntOps, LongOps}
import sigma.util.Extensions.{BooleanOps, IntOps}
import sigmastate.exceptions.MethodNotFound
import sigmastate.utils.Extensions.ByteOpsForSigma
import sigmastate.utils.Helpers
Expand Down Expand Up @@ -1523,4 +1522,93 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
)
}


property("Global - fromBigEndianBytes") {
import sigma.data.OrderingOps.BigIntOrdering

def byteFromBigEndianBytes: Feature[Byte, Boolean] = {
newFeature(
{ (x: Byte) => CSigmaDslBuilder.fromBigEndianBytes[Byte](Colls.fromArray(Array(x))) == x},
"{ (x: Byte) => fromBigEndianBytes[Byte](x.toBytes) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
}

verifyCases(
Seq(
5.toByte -> new Expected(ExpectedResult(Success(true), None)),
Byte.MaxValue -> new Expected(ExpectedResult(Success(true), None)),
Byte.MinValue -> new Expected(ExpectedResult(Success(true), None))
),
byteFromBigEndianBytes
)

def shortFromBigEndianBytes: Feature[Short, Boolean] = {
newFeature(
{ (x: Short) => CSigmaDslBuilder.fromBigEndianBytes[Short](Colls.fromArray(Shorts.toByteArray(x))) == x},
"{ (x: Short) => fromBigEndianBytes[Short](x.toBytes) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
}

verifyCases(
Seq(
5.toShort -> new Expected(ExpectedResult(Success(true), None)),
Short.MaxValue -> new Expected(ExpectedResult(Success(true), None)),
Short.MinValue -> new Expected(ExpectedResult(Success(true), None))
),
shortFromBigEndianBytes
)

def intFromBigEndianBytes: Feature[Int, Boolean] = {
newFeature(
{ (x: Int) => CSigmaDslBuilder.fromBigEndianBytes[Int](Colls.fromArray(Ints.toByteArray(x))) == x},
"{ (x: Int) => fromBigEndianBytes[Int](x.toBytes) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
}

verifyCases(
Seq(
5 -> new Expected(ExpectedResult(Success(true), None)),
Int.MaxValue -> new Expected(ExpectedResult(Success(true), None))
),
intFromBigEndianBytes
)

def longFromBigEndianBytes: Feature[Long, Boolean] = {
newFeature(
{ (x: Long) => CSigmaDslBuilder.fromBigEndianBytes[Long](Colls.fromArray(Longs.toByteArray(x))) == x},
"{ (x: Long) => fromBigEndianBytes[Long](x.toBytes) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
}

verifyCases(
Seq(
5L -> new Expected(ExpectedResult(Success(true), None)),
Long.MinValue -> new Expected(ExpectedResult(Success(true), None))
),
longFromBigEndianBytes
)

def bigIntFromBigEndianBytes: Feature[BigInt, Boolean] = {
newFeature(
{ (x: BigInt) => CSigmaDslBuilder.fromBigEndianBytes[BigInt](x.toBytes) == x},
"{ (x: BigInt) => Global.fromBigEndianBytes[BigInt](x.toBytes) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
}

verifyCases(
Seq(
CBigInt(BigInteger.valueOf(50)) -> new Expected(ExpectedResult(Success(true), None)),
CBigInt(BigInteger.valueOf(-500000000000L)) -> new Expected(ExpectedResult(Success(true), None)),
CBigInt(sigma.crypto.CryptoConstants.groupOrder.divide(BigInteger.valueOf(2))) -> new Expected(ExpectedResult(Success(true), None))
),
bigIntFromBigEndianBytes
)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
(SGlobal.typeId, Seq(
MInfo(1, groupGeneratorMethod), MInfo(2, xorMethod)
) ++ (if (isV6Activated) {
Seq(MInfo(3, serializeMethod)) // methods added in v6.0
Seq(MInfo(3, serializeMethod), MInfo(5, fromBigEndianBytesMethod)) // methods added in v6.0
} else {
Seq.empty[MInfo]
}), true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Global.fromBigEndianBytes - Long.toBytes") {
def fromTest() = test("fromBigEndianBytes - long", env, ext,
s"""{
| val l = 1088800L
| val ba = l.toBytes
| Global.fromBigEndianBytes[Long](ba) == l
|}
|""".stripMargin,
null
)
if(VersionContext.current.isV6SoftForkActivated) {
fromTest()
} else {
an[Exception] should be thrownBy(fromTest())
}
}

property("Global.fromBigEndianBytes - bigInt") {
val bi = new BigInteger("9785856985394593489356430476450674590674598659865986594859056865984690568904")
def fromTest() = test("fromBigEndianBytes - bigInt", env, ext,
Expand All @@ -639,8 +656,6 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

// todo: roundtrip with .toBytes

property("Int.toBytes") {
def toBytesTest() = test("Int.toBytes", env, ext,
"""{
Expand Down

0 comments on commit 586aadd

Please sign in to comment.