Skip to content

Commit

Permalink
ErgoTreeSpecification update
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 31, 2024
1 parent a160997 commit fdf712e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
16 changes: 8 additions & 8 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ case object SBigIntMethods extends SNumericTypeMethods {
private val ToUnsignedCostKind = FixedCost(JitCost(5))

//id = 8 to make it after toBits
val ToUnsigned = SMethod(this, "toUnsigned", SFunc(this.ownerType, SUnsignedBigInt), 19, ToUnsignedCostKind)
val ToUnsigned = SMethod(this, "toUnsigned", SFunc(this.ownerType, SUnsignedBigInt), 14, ToUnsignedCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -508,7 +508,7 @@ case object SBigIntMethods extends SNumericTypeMethods {
}


val ToUnsignedMod = SMethod(this, "toUnsignedMod", SFunc(Array(this.ownerType, SUnsignedBigInt), SUnsignedBigInt), 20, ToUnsignedCostKind)
val ToUnsignedMod = SMethod(this, "toUnsignedMod", SFunc(Array(this.ownerType, SUnsignedBigInt), SUnsignedBigInt), 15, ToUnsignedCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand Down Expand Up @@ -541,7 +541,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
final val ModInverseCostInfo = ToNBitsCostInfo

// todo: check ids before and after merging with other PRs introducing new methods for Numeric
val ModInverseMethod = SMethod(this, "modInverse", SFunc(Array(this.ownerType, this.ownerType), this.ownerType), 19, ModInverseCostInfo.costKind)
val ModInverseMethod = SMethod(this, "modInverse", SFunc(Array(this.ownerType, this.ownerType), this.ownerType), 14, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -552,7 +552,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
}

// todo: costing
val PlusModMethod = SMethod(this, "plusMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 20, ModInverseCostInfo.costKind)
val PlusModMethod = SMethod(this, "plusMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 15, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -562,7 +562,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
bi.plusMod(bi2, m)
}

val SubtractModMethod = SMethod(this, "subtractMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 21, ModInverseCostInfo.costKind)
val SubtractModMethod = SMethod(this, "subtractMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 16, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -572,7 +572,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
bi.subtractMod(bi2, m)
}

val MultiplyModMethod = SMethod(this, "multiplyMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 22, ModInverseCostInfo.costKind)
val MultiplyModMethod = SMethod(this, "multiplyMod", SFunc(Array(this.ownerType, this.ownerType, this.ownerType), this.ownerType), 17, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -582,7 +582,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
bi.multiplyMod(bi2, m)
}

val ModMethod = SMethod(this, "mod", SFunc(Array(this.ownerType, this.ownerType), this.ownerType), 23, ModInverseCostInfo.costKind)
val ModMethod = SMethod(this, "mod", SFunc(Array(this.ownerType, this.ownerType), this.ownerType), 18, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand All @@ -592,7 +592,7 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
bi.mod(m)
}

val ToSignedMethod = SMethod(this, "toSigned", SFunc(Array(this.ownerType), SBigInt), 24, ModInverseCostInfo.costKind)
val ToSignedMethod = SMethod(this, "toSigned", SFunc(Array(this.ownerType), SBigInt), 19, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

Expand Down
48 changes: 44 additions & 4 deletions sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import sigma.ast.SCollection.SByteArray
import sigma.ast._
import sigma.ast.syntax.{SValue, SigmaPropValue, TrueSigmaProp}
import sigma.data.RType.asType
import sigma.data.{CBox, Nullable, RType, TrivialProp}
import sigma.data.{Nullable, RType, TrivialProp}
import sigma.validation.ValidationException
import sigma.validation.ValidationRules.CheckTypeCode
import ErgoTree.HeaderType
import SCollectionMethods.checkValidFlatmap
import sigma.ast.SBigIntMethods.{ToUnsigned, ToUnsignedMod}
import sigma.ast.SUnsignedBigIntMethods.{ModInverseMethod, ModMethod, MultiplyModMethod, PlusModMethod, SubtractModMethod, ToSignedMethod}
import sigmastate.eval.CProfiler
import sigmastate.helpers.{ErgoLikeContextTesting, SigmaPPrint}
import sigmastate.interpreter.Interpreter.ReductionResult
Expand Down Expand Up @@ -431,17 +433,55 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
MInfo(10, BitwiseAndMethod, isResolvableFromIds = true),
MInfo(11, BitwiseXorMethod, isResolvableFromIds = true),
MInfo(12, ShiftLeftMethod, isResolvableFromIds = true),
MInfo(13, ShiftRightMethod, isResolvableFromIds = true)
MInfo(13, ShiftRightMethod, isResolvableFromIds = true),
MInfo(14, ToUnsigned, isResolvableFromIds = true),
MInfo(15, ToUnsignedMod, isResolvableFromIds = true)
) else Seq.empty)
, true)
},
{
if (isV6Activated) {
// SBigInt inherit methods from SNumericType.methods
// however they are not resolvable via SBigInt.typeId before v6.0
import SNumericTypeMethods._
(SUnsignedBigInt.typeId, Seq(
MInfo(methodId = 1, ToByteMethod, isResolvableFromIds = true),
MInfo(2, ToShortMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(3, ToIntMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(4, ToLongMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(5, ToBigIntMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(6, ToBytesMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(7, ToBitsMethod, isResolvableFromIds = if (isV6Activated) true else false),
MInfo(8, BitwiseInverseMethod, isResolvableFromIds = true),
MInfo(9, BitwiseOrMethod, isResolvableFromIds = true),
MInfo(10, BitwiseAndMethod, isResolvableFromIds = true),
MInfo(11, BitwiseXorMethod, isResolvableFromIds = true),
MInfo(12, ShiftLeftMethod, isResolvableFromIds = true),
MInfo(13, ShiftRightMethod, isResolvableFromIds = true),
MInfo(14, ModInverseMethod, true),
MInfo(15, PlusModMethod, true),
MInfo(16, SubtractModMethod, true),
MInfo(17, MultiplyModMethod, true),
MInfo(18, ModMethod, true),
MInfo(19, ToSignedMethod, true)
), true)
} else {
(SUnsignedBigInt.typeId, Seq.empty, false)
}
},
{ import SGroupElementMethods._
(SGroupElement.typeId, Seq(
MInfo(2, GetEncodedMethod),
MInfo(3, ExponentiateMethod),
MInfo(4, MultiplyMethod),
MInfo(5, NegateMethod)
), true)
) ++ {
if(VersionContext.current.isV6SoftForkActivated) {
Seq(MInfo(6, ExponentiateUnsignedMethod))
} else {
Seq.empty
}
}, true)
},
{ import SSigmaPropMethods._
(SSigmaProp.typeId, Seq(
Expand Down Expand Up @@ -626,7 +666,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
}

property("MethodCall on numerics") {
forAll(Table[STypeCompanion]("type", SByte, SShort, SInt, SLong, SBigInt)) { t =>
forAll(Table[STypeCompanion]("type", SByte, SShort, SInt, SLong, SBigInt, SUnsignedBigInt)) { t =>
// this methods are expected to fail resolution in before v6.0
if (!isV6Activated) {
(1 to 7).foreach { methodId =>
Expand Down

0 comments on commit fdf712e

Please sign in to comment.