Skip to content

Commit

Permalink
tests for .toBytes, UnsignedBigInt support added to to new Numeric me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
kushti committed Nov 5, 2024
1 parent f84bce5 commit 1496770
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import sigma.ast.syntax.{SValue, ValueOps}
import sigma.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIsExactIntegral, ShortIsExactIntegral}
import sigma.data.NumericOps.BigIntIsExactIntegral
import sigma.data.OverloadHack.Overloaded1
import sigma.data.UnsignedBigIntNumericOps.UnsignedBigIntIsExactIntegral
import sigma.data.{DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.eval.{CostDetails, ErgoTreeEvaluator, TracedCost}
import sigma.reflection.RClass
Expand Down Expand Up @@ -295,6 +296,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.toBits(obj.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.toBits(obj.asInstanceOf[Long])
case SBigIntMethods => BigIntIsExactIntegral.toBits(obj.asInstanceOf[BigInt])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.toBits(obj.asInstanceOf[UnsignedBigInt])
}
})
.withInfo(PropertyCall,
Expand All @@ -315,6 +317,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.bitwiseInverse(obj.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.bitwiseInverse(obj.asInstanceOf[Long])
case SBigIntMethods => BigIntIsExactIntegral.bitwiseInverse(obj.asInstanceOf[BigInt])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.bitwiseInverse(obj.asInstanceOf[UnsignedBigInt])
}
})
.withInfo(PropertyCall, desc = "Returns bitwise inverse of this numeric. ")
Expand All @@ -329,6 +332,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.bitwiseOr(obj.asInstanceOf[Int], other.head.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.bitwiseOr(obj.asInstanceOf[Long], other.head.asInstanceOf[Long])
case SBigIntMethods => BigIntIsExactIntegral.bitwiseOr(obj.asInstanceOf[BigInt], other.head.asInstanceOf[BigInt])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.bitwiseOr(obj.asInstanceOf[UnsignedBigInt], other.head.asInstanceOf[UnsignedBigInt])
}
})
.withInfo(MethodCall,
Expand All @@ -345,6 +349,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.bitwiseAnd(obj.asInstanceOf[Int], other.head.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.bitwiseAnd(obj.asInstanceOf[Long], other.head.asInstanceOf[Long])
case SBigIntMethods => BigIntIsExactIntegral.bitwiseAnd(obj.asInstanceOf[BigInt], other.head.asInstanceOf[BigInt])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.bitwiseAnd(obj.asInstanceOf[UnsignedBigInt], other.head.asInstanceOf[UnsignedBigInt])
}
})
.withInfo(MethodCall,
Expand All @@ -361,6 +366,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.bitwiseXor(obj.asInstanceOf[Int], other.head.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.bitwiseXor(obj.asInstanceOf[Long], other.head.asInstanceOf[Long])
case SBigIntMethods => BigIntIsExactIntegral.bitwiseXor(obj.asInstanceOf[BigInt], other.head.asInstanceOf[BigInt])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.bitwiseXor(obj.asInstanceOf[UnsignedBigInt], other.head.asInstanceOf[UnsignedBigInt])
}
})
.withInfo(MethodCall,
Expand All @@ -377,6 +383,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.shiftLeft(obj.asInstanceOf[Int], other.head.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.shiftLeft(obj.asInstanceOf[Long], other.head.asInstanceOf[Int])
case SBigIntMethods => BigIntIsExactIntegral.shiftLeft(obj.asInstanceOf[BigInt], other.head.asInstanceOf[Int])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.shiftLeft(obj.asInstanceOf[UnsignedBigInt], other.head.asInstanceOf[Int])
}
})
.withInfo(MethodCall,
Expand All @@ -396,6 +403,7 @@ object SNumericTypeMethods extends MethodsContainer {
case SIntMethods => IntIsExactIntegral.shiftRight(obj.asInstanceOf[Int], other.head.asInstanceOf[Int])
case SLongMethods => LongIsExactIntegral.shiftRight(obj.asInstanceOf[Long], other.head.asInstanceOf[Int])
case SBigIntMethods => BigIntIsExactIntegral.shiftRight(obj.asInstanceOf[BigInt], other.head.asInstanceOf[Int])
case SUnsignedBigIntMethods => UnsignedBigIntIsExactIntegral.shiftRight(obj.asInstanceOf[UnsignedBigInt], other.head.asInstanceOf[Int])
}
})
.withInfo(MethodCall,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import sigmastate.interpreter.Interpreter._
import sigma.ast.Apply
import sigma.eval.EvalSettings
import sigma.exceptions.InvalidType
import sigma.serialization.ErgoTreeSerializer
import sigma.serialization.{ErgoTreeSerializer, SerializerException}
import sigma.interpreter.{ContextExtension, ProverResult}
import sigmastate.utils.Helpers
import sigmastate.utils.Helpers._
Expand Down Expand Up @@ -793,6 +793,24 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}


property("UnsignedBigInt.toBits") {
def toBitsTest() = test("UnsignedBigInt.toBits", env, ext,
s"""{
| val b = bigInt("${CryptoConstants.groupOrder}")
| val ba = b.toBits
| ba.size == 256
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
toBitsTest()
} else {
an[SerializerException] shouldBe thrownBy(toBitsTest())
}
}

property("BigInt.bitwiseInverse") {
def bitwiseInverseTest(): Assertion = test("BigInt.bitwiseInverse", env, ext,
s"""{
Expand Down Expand Up @@ -1497,6 +1515,22 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("UnsignedBigInt.toBytes") {
def toBytesTest() = test("UnsignedBigInt.toBytes", env, ext,
s"""{
| val l = bigInt("${CryptoConstants.groupOrder}")
| l.toBytes.size == 32
| }""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
toBytesTest()
} else {
an[SerializerException] shouldBe thrownBy(toBytesTest())
}
}

property("serialize - byte array") {
def deserTest() = test("serialize", env, ext,
s"""{
Expand Down

0 comments on commit 1496770

Please sign in to comment.