Skip to content

Commit

Permalink
UnsignedBigInt support in DataSerializerSpecification
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 31, 2024
1 parent 06fff23 commit a160997
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/sigma/ast/SType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ object SPrimType {
def unapply(t: SType): Option[SType] = SType.allPredefTypes.find(_ == t)

/** Type code of the last valid prim type so that (1 to LastPrimTypeCode) is a range of valid codes. */
final val LastPrimTypeCode: Byte = 8: Byte
final val LastPrimTypeCode: Byte = 9: Byte

/** Upper limit of the interval of valid type codes for primitive types */
final val MaxPrimTypeCode: Byte = 11: Byte
Expand Down
8 changes: 8 additions & 0 deletions core/shared/src/main/scala/sigma/util/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ object Extensions {

/** Converts `x` to [[sigma.BigInt]] */
def toBigInt: sigma.BigInt = CBigInt(x)

/** Converts `x` to [[sigma.UnsignedBigInt]] */
def toUnsignedBigInt: sigma.UnsignedBigInt = {
if(x.compareTo(BigInteger.ZERO) < 0){
throw new IllegalArgumentException("toUnsignedBigInt arg < 0")
}
CUnsignedBigInt(x)
}
}

implicit class BigIntOps(val x: sigma.BigInt) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ object DataValueComparer {
okEqual = bi == r
}

// todo: check costing
case ubi: UnsignedBigInt => /** case 5 (see [[EQ_BigInt]]) */
E.addFixedCost(EQ_BigInt) {
okEqual = ubi == r
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DataSerializerSpecification extends SerializationSpecification {
implicit val tagT = tT.classTag
implicit val tAny = sigma.AnyType

val withVersion = if (tpe == SHeader) {
val withVersion = if (tpe == SHeader || tpe == SUnsignedBigInt) {
Some(VersionContext.V6SoftForkVersion)
} else {
None
Expand Down Expand Up @@ -148,6 +148,7 @@ class DataSerializerSpecification extends SerializationSpecification {
forAll { x: Long => roundtrip[SLong.type](x, SLong) }
forAll { x: String => roundtrip[SString.type](x, SString) }
forAll { x: BigInteger => roundtrip[SBigInt.type](x.toBigInt, SBigInt) }
forAll { x: BigInteger => roundtrip[SUnsignedBigInt.type](x.abs().toUnsignedBigInt, SUnsignedBigInt, Some(VersionContext.V6SoftForkVersion)) }
forAll { x: EcPointType => roundtrip[SGroupElement.type](x.toGroupElement, SGroupElement) }
forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x.toSigmaProp, SSigmaProp) }
forAll { x: ErgoBox => roundtrip[SBox.type](x, SBox) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ trait ObjectGenerators extends TypeGenerators
implicit lazy val arbRegisterIdentifier: Arbitrary[RegisterId] = Arbitrary(registerIdentifierGen)
implicit lazy val arbBigInteger: Arbitrary[BigInteger] = Arbitrary(Arbitrary.arbBigInt.arbitrary.map(_.bigInteger))
implicit lazy val arbBigInt: Arbitrary[BigInt] = Arbitrary(arbBigInteger.arbitrary.map(SigmaDsl.BigInt(_)))
implicit lazy val arbUnsignedBigInt: Arbitrary[UnsignedBigInt] = Arbitrary(arbBigInteger.arbitrary.map(_.abs()).map(SigmaDsl.UnsignedBigInt(_)))
implicit lazy val arbEcPointType: Arbitrary[dlogGroup.ElemType] = Arbitrary(Gen.const(()).flatMap(_ => CryptoConstants.dlogGroup.createRandomGenerator()))
implicit lazy val arbGroupElement: Arbitrary[GroupElement] = Arbitrary(arbEcPointType.arbitrary.map(SigmaDsl.GroupElement(_)))
implicit lazy val arbSigmaBoolean: Arbitrary[SigmaBoolean] = Arbitrary(Gen.oneOf(proveDHTGen, proveDHTGen))
Expand Down Expand Up @@ -305,6 +306,7 @@ trait ObjectGenerators extends TypeGenerators
case SInt => arbInt
case SLong => arbLong
case SBigInt => arbBigInt
case SUnsignedBigInt => arbUnsignedBigInt
case SGroupElement => arbGroupElement
case SSigmaProp => arbSigmaProp
case SBox => arbBox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait TypeGenerators {
implicit val intTypeGen: Gen[SInt.type] = Gen.const(SInt)
implicit val longTypeGen: Gen[SLong.type] = Gen.const(SLong)
implicit val bigIntTypeGen: Gen[SBigInt.type] = Gen.const(SBigInt)
implicit val unsignedBigIntTypeGen: Gen[SUnsignedBigInt.type] = Gen.const(SUnsignedBigInt)
implicit val groupElementTypeGen: Gen[SGroupElement.type] = Gen.const(SGroupElement)
implicit val sigmaPropTypeGen: Gen[SSigmaProp.type] = Gen.const(SSigmaProp)
implicit val boxTypeGen: Gen[SBox.type] = Gen.const(SBox)
Expand All @@ -19,10 +20,10 @@ trait TypeGenerators {
implicit val headerTypeGen: Gen[SHeader.type] = Gen.const(SHeader)

implicit val primTypeGen: Gen[SPrimType] =
Gen.oneOf[SPrimType](SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp, SUnit)
Gen.oneOf[SPrimType](SBoolean, SByte, SShort, SInt, SLong, SBigInt, SUnsignedBigInt, SGroupElement, SSigmaProp, SUnit)
implicit val arbPrimType: Arbitrary[SPrimType] = Arbitrary(primTypeGen)
implicit val predefTypeGen: Gen[SPredefType] =
Gen.oneOf[SPredefType](SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp, SUnit, SBox, SAvlTree, SHeader)
Gen.oneOf[SPredefType](SBoolean, SByte, SShort, SInt, SLong, SBigInt, SUnsignedBigInt, SGroupElement, SSigmaProp, SUnit, SBox, SAvlTree, SHeader)
implicit val arbPredefType: Arbitrary[SPredefType] = Arbitrary(predefTypeGen)

implicit def genToArbitrary[T: Gen]: Arbitrary[T] = Arbitrary(implicitly[Gen[T]])
Expand All @@ -34,7 +35,8 @@ trait TypeGenerators {
shortTypeGen,
intTypeGen,
longTypeGen,
bigIntTypeGen
bigIntTypeGen,
unsignedBigIntTypeGen
))
} yield STuple(values.toIndexedSeq)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C

val typeCodes = Table(
("constant", "expectedValue"),
(SPrimType.LastPrimTypeCode, 8),
(SPrimType.LastPrimTypeCode, 9),
(SPrimType.MaxPrimTypeCode, 11)
)

Expand All @@ -285,6 +285,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
(SBigInt, 6, true, true, true),
(SGroupElement, 7, true, true, false),
(SSigmaProp, 8, true, true, false),
(SUnsignedBigInt, 9, true, true, true),
(SBox, 99, false, false, false),
(SAvlTree, 100, false, false, false),
(SContext, 101, false, false, false),
Expand Down

0 comments on commit a160997

Please sign in to comment.