Skip to content

Commit

Permalink
Merge pull request #737 from thub1271/add-from-base-16
Browse files Browse the repository at this point in the history
Adding fromBase16 to the frontend
  • Loading branch information
aslesarenko authored May 16, 2022
2 parents 5212765 + 9bee61f commit c59139b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions docs/LangSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,19 @@ def proveDHTuple(g: GroupElement, h: GroupElement,
*/
def proveDlog(value: GroupElement): SigmaProp

/** Transforms Base58 encoded string litereal into constant of type Coll[Byte].
/** Transforms Base16 encoded string literal into constant of type Coll[Byte].
* It is a compile-time operation and only string literal (constant) can be its
* argument.
*/
def fromBase16(input: String): Coll[Byte]

/** Transforms Base58 encoded string literal into constant of type Coll[Byte].
* It is a compile-time operation and only string literal (constant) can be its
* argument.
*/
def fromBase58(input: String): Coll[Byte]

/** Transforms Base64 encoded string litereal into constant of type Coll[Byte].
/** Transforms Base64 encoded string literal into constant of type Coll[Byte].
* It is a compile-time operation and only string literal (constant) can be its
* argument.
*/
Expand Down
13 changes: 12 additions & 1 deletion sigmastate/src/main/scala/sigmastate/lang/SigmaPredef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigmastate.lang
import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix
import org.ergoplatform.{ErgoAddressEncoder, P2PKAddress}
import scalan.Nullable
import scorex.util.encode.{Base64, Base58}
import scorex.util.encode.{Base64, Base58, Base16}
import sigmastate.SCollection.{SIntArray, SByteArray}
import sigmastate.SOption._
import sigmastate.Values.{StringConstant, Constant, EvaluatedValue, SValue, IntValue, SigmaPropConstant, ConstantPlaceholder, BoolValue, Value, ByteArrayConstant, SigmaPropValue, ValueCompanion}
Expand Down Expand Up @@ -180,6 +180,16 @@ object SigmaPredef {
Seq(ArgInfo("", "")))
)

val FromBase16Func = PredefinedFunc("fromBase16",
Lambda(Array("input" -> SString), SByteArray, None),
PredefFuncInfo(
{ case (_, Seq(arg: EvaluatedValue[SString.type]@unchecked)) =>
ByteArrayConstant(Base16.decode(arg.value).get)
}),
OperationInfo(Constant, "",
Seq(ArgInfo("", "")))
)

val FromBase58Func = PredefinedFunc("fromBase58",
Lambda(Array("input" -> SString), SByteArray, None),
PredefFuncInfo(
Expand Down Expand Up @@ -381,6 +391,7 @@ object SigmaPredef {
SigmaPropFunc,
GetVarFunc,
DeserializeFunc,
FromBase16Func,
FromBase64Func,
FromBase58Func,
Blake2b256Func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class SigmaCompilerTest extends SigmaTestingCommons with LangTests with ObjectGe
}

property("fromBaseX") {
comp(""" fromBase16("31") """) shouldBe ByteArrayConstant(Array[Byte](49))
comp(""" fromBase58("r") """) shouldBe ByteArrayConstant(Array[Byte](49))
comp(""" fromBase64("MQ") """) shouldBe ByteArrayConstant(Array[Byte](49))
comp(""" fromBase64("M" + "Q") """) shouldBe ByteArrayConstant(Array[Byte](49))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La
}

property("fromBaseX string decoding") {
parse("""fromBase16("1111")""") shouldBe Apply(FromBase16Func.symNoType, IndexedSeq(StringConstant("1111")))
parse("""fromBase58("111")""") shouldBe Apply(FromBase58Func.symNoType, IndexedSeq(StringConstant("111")))
parse("""fromBase64("111")""") shouldBe Apply(FromBase64Func.symNoType, IndexedSeq(StringConstant("111")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class SigmaTyperTest extends PropSpec with PropertyChecks with Matchers with Lan
typecheck(env, "min(HEIGHT, INPUTS.size)") shouldBe SInt
typecheck(env, "max(1, 2)") shouldBe SInt
typecheck(env, "max(1L, 2)") shouldBe SLong
typecheck(env, """fromBase16("1111")""") shouldBe SByteArray
typecheck(env, """fromBase58("111")""") shouldBe SByteArray
typecheck(env, """fromBase64("111")""") shouldBe SByteArray

Expand Down

0 comments on commit c59139b

Please sign in to comment.