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

Implement Unit support in compiler #822

Open
wants to merge 1 commit into
base: v5.0-finalize
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ trait RuntimeCosting extends CostingRules { IR: IRContext =>
case SInt => IntElement
case SLong => LongElement
case SString => StringElement
case SUnit => UnitElement
case SAny => AnyElement
case SBigInt => bigIntElement
case SBox => boxElement
Expand All @@ -829,6 +830,7 @@ trait RuntimeCosting extends CostingRules { IR: IRContext =>
case IntElement => SInt
case LongElement => SLong
case StringElement => SString
case UnitElement => SUnit
case AnyElement => SAny
case _: BigIntElem[_] => SBigInt
case _: GroupElementElem[_] => SGroupElement
Expand Down
3 changes: 3 additions & 0 deletions sigmastate/src/main/scala/sigmastate/eval/Sized.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ object Sized extends SizedLowPriority {
val SizeByte: Size[Byte] = new CSizePrim(1L, ByteType)
val SizeShort: Size[Short] = new CSizePrim(2L, ShortType)
val SizeInt: Size[Int] = new CSizePrim(4L, IntType)
val SizeUnit: Size[Unit] = new CSizePrim(1L, UnitType)
val SizeLong: Size[Long] = new CSizePrim(8L, LongType)
val SizeBigInt: Size[BigInt] = new CSizePrim(SBigInt.MaxSizeInBytes, BigIntRType)
val SizeGroupElement: Size[GroupElement] = new CSizePrim(CryptoConstants.EncodedGroupElementLength, GroupElementRType)
Expand All @@ -64,6 +65,7 @@ object Sized extends SizedLowPriority {
implicit val ByteIsSized: Sized[Byte] = Sized.instance((_: Byte) => SizeByte)
implicit val ShortIsSized: Sized[Short] = Sized.instance((_: Short) => SizeShort)
implicit val IntIsSized: Sized[Int] = Sized.instance((_: Int) => SizeInt)
implicit val UnitIsSized: Sized[Unit] = Sized.instance((_: Unit) => SizeUnit)
implicit val LongIsSized: Sized[Long] = Sized.instance((_: Long) => SizeLong)
implicit val BigIntIsSized: Sized[BigInt] = Sized.instance((_: BigInt) => SizeBigInt)
implicit val GroupElementIsSized: Sized[GroupElement] = Sized.instance((_: GroupElement) => SizeGroupElement)
Expand All @@ -82,6 +84,7 @@ object Sized extends SizedLowPriority {
case ByteType => ByteIsSized
case ShortType => ShortIsSized
case IntType => IntIsSized
case UnitType => UnitIsSized
case LongType => LongIsSized
case BigIntRType => BigIntIsSized
case GroupElementRType => GroupElementIsSized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,16 @@ class BasicOpsSpecification extends SigmaTestingCommons
VersionContext.withVersions(activatedVersionInTests, ergoTreeVersionInTests) {
if (VersionContext.current.isJitActivated) {

// TODO frontend: implement missing Unit support in compiler
// https://github.com/ScorexFoundation/sigmastate-interpreter/issues/820
test("R1", env, ext,
script = "", /* means cannot be compiled
the corresponding script is { SELF.R4[Unit].isDefined } */
script = "{ SELF.R4[Unit].isDefined }",
ExtractRegisterAs[SUnit.type](Self, reg1)(SUnit).isDefined.toSigmaProp,
additionalRegistersOpt = Some(Map(
reg1 -> UnitConstant.instance
))
)

test("R2", env, ext,
script = "", /* means cannot be compiled
the corresponding script is "{ SELF.R4[Unit].get == () }" */
script = "{ SELF.R4[Unit].get == () }",
EQ(ExtractRegisterAs[SUnit.type](Self, reg1)(SUnit).get, UnitConstant.instance).toSigmaProp,
additionalRegistersOpt = Some(Map(
reg1 -> UnitConstant.instance
Expand All @@ -167,19 +163,15 @@ class BasicOpsSpecification extends SigmaTestingCommons
reg2 -> UnitConstant.instance
))
),
rootCauseLike[RuntimeException]("Don't know how to compute Sized for type PrimitiveType(Unit,")
rootCauseLike[AssertionError]("Tree root should be real but was UnprovenSchnorr(ProveDlog((")
)
assertExceptionThrown(
test("R2", env, ext,
"", /* the test script "{ SELF.R4[Unit].isDefined }" cannot be compiled with SigmaCompiler,
but we nevertheless want to test how interpreter process the tree,
so we use the explicitly given tree below */
ExtractRegisterAs[SUnit.type](Self, reg1)(SUnit).isDefined.toSigmaProp,
additionalRegistersOpt = Some(Map(
reg1 -> UnitConstant.instance
))
),
rootCauseLike[CosterException]("Don't know how to convert SType SUnit to Elem")

test("R2", env, ext,
"{ SELF.R4[Unit].isDefined }",
ExtractRegisterAs[SUnit.type](Self, reg1)(SUnit).isDefined.toSigmaProp,
additionalRegistersOpt = Some(Map(
reg1 -> UnitConstant.instance
))
)
}
}
Expand Down