Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.0] Global.serialize method #989

Merged
merged 19 commits into from
Sep 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
merging w. 6.0.0
kushti committed Sep 20, 2024
commit 9c310c7aef17e978880c920dd751ce79c6a0216d
Original file line number Diff line number Diff line change
@@ -1157,7 +1157,7 @@ trait GraphBuilding extends Base with DefRewriting { IR: IRContext =>
case SGlobalMethods.serializeMethod.name =>
val value = asRep[Any](argsV(0))
g.serialize(value)
case _ => throwError
case _ => throwError()
}
case (x: Ref[tNum], _: SNumericTypeMethods) => method.name match {
case SNumericTypeMethods.ToBytesMethod.name =>
96 changes: 79 additions & 17 deletions sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala
Original file line number Diff line number Diff line change
@@ -323,24 +323,86 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit with C
Table(
("typeId", "methods", "CanHaveMethods"),
(SBoolean.typeId, Seq.empty[MInfo], true),
(SByte.typeId, Seq.empty[MInfo], false),
(SShort.typeId, Seq.empty[MInfo], false),
(SInt.typeId, Seq.empty[MInfo], false),
(SLong.typeId, Seq.empty[MInfo], false),

(if(!isV6Activated) {
{ // SNumericType.typeId is erroneously shadowed by SGlobal.typeId
// this should be preserved in v3.x and fixed in v4.0
(SNumericType.typeId, Seq(
MInfo(methodId = 1, SGlobalMethods.groupGeneratorMethod),
MInfo(2, SGlobalMethods.xorMethod)
{
if (isV6Activated)
(SByte.typeId, Seq(
MInfo(methodId = 1, ToByteMethod),
MInfo(2, ToShortMethod),
MInfo(3, ToIntMethod),
MInfo(4, ToLongMethod),
MInfo(5, ToBigIntMethod),
MInfo(6, ToBytesMethod),
MInfo(7, ToBitsMethod),
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)
), true)
}
} else {
// rechecking boolean again, after merging w. https://github.com/ScorexFoundation/sigmastate-interpreter/pull/1017
// there will be check against numerics in 6.0 here
(SBoolean.typeId, Seq.empty[MInfo], true)
}),
else
(SByte.typeId, Seq.empty[MInfo], false)
},
{
if (isV6Activated)
(SShort.typeId, Seq(
MInfo(methodId = 1, ToByteMethod),
MInfo(2, ToShortMethod),
MInfo(3, ToIntMethod),
MInfo(4, ToLongMethod),
MInfo(5, ToBigIntMethod),
MInfo(6, ToBytesMethod),
MInfo(7, ToBitsMethod),
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)
), true)
else
(SShort.typeId, Seq.empty[MInfo], false)
},
{
if (isV6Activated)
(SInt.typeId, Seq(
MInfo(methodId = 1, ToByteMethod),
MInfo(2, ToShortMethod),
MInfo(3, ToIntMethod),
MInfo(4, ToLongMethod),
MInfo(5, ToBigIntMethod),
MInfo(6, ToBytesMethod),
MInfo(7, ToBitsMethod),
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)
), true)
else
(SInt.typeId, Seq.empty[MInfo], false)
},
{
if (isV6Activated)
(SLong.typeId, Seq(
MInfo(methodId = 1, ToByteMethod),
MInfo(2, ToShortMethod),
MInfo(3, ToIntMethod),
MInfo(4, ToLongMethod),
MInfo(5, ToBigIntMethod),
MInfo(6, ToBytesMethod),
MInfo(7, ToBitsMethod),
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)
), true)
else
(SLong.typeId, Seq.empty[MInfo], false)
},

// { // SNumericType.typeId is erroneously shadowed by SGlobal.typeId
// // this should be preserved in v3.x and fixed in v4.0
Original file line number Diff line number Diff line change
@@ -555,6 +555,71 @@ class BasicOpsSpecification extends CompilerTestingCommons
)
}

property("Int.toBytes") {
def toBytesTest() = test("Int.toBytes", env, ext,
"""{
| val l = 1
| l.toBytes == Coll(0.toByte, 0.toByte, 0.toByte, 1.toByte)
| }""".stripMargin,
null
)

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

property("Int.toBits") {
def toBytesTest() = test("Int.toBytes", env, ext,
"""{
| val l = 1477959696
| l.toBits == Coll(false, true, false, true, true, false, false, false, false, false, false, true, false, true, true ,true, true, true, true, false, false, false, false, false, false, false, false, true, false, false, false, false)
| }""".stripMargin,
null
)

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

property("Byte.toBytes") {
def toBytesTest() = test("Byte.toBytes", env, ext,
"""{
| val l = 10.toByte
| l.toBytes == Coll(10.toByte)
| }""".stripMargin,
null
)

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


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

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

property("serialize - byte array") {
def deserTest() = test("serialize", env, ext,
s"""{
@@ -605,7 +670,7 @@ class BasicOpsSpecification extends CompilerTestingCommons
deserTest()
}
}

property("serialize(long) is producing different result from longToByteArray()") {
def deserTest() = test("serialize", env, ext,
s"""{
You are viewing a condensed version of this merge commit. You can view the full changes here.