Skip to content

Commit

Permalink
Merge pull request #400 from ScorexFoundation/i324-predef-funcs
Browse files Browse the repository at this point in the history
Implement missing ops and predefined functions for 2.0
  • Loading branch information
aslesarenko authored Feb 27, 2019
2 parents 2e5b285 + 2a5524a commit 8c93f79
Show file tree
Hide file tree
Showing 38 changed files with 728 additions and 231 deletions.
2 changes: 1 addition & 1 deletion docs/LangSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ def allOf(conditions: Coll[Boolean]): Boolean
/** Returns true if at least on element of the conditions is true */
def anyOf(conditions: Coll[Boolean]): Boolean

/** Similar to allOf, but performing logical XOR operation instead of `||`
/** Similar to allOf, but performing logical XOR operation instead of `&&`
* @since 2.0
*/
def xorOf(conditions: Coll[Boolean]): Boolean
Expand Down
2 changes: 2 additions & 0 deletions sigma-api/src/main/resources/special/sigma/SigmaDsl.scalan
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,14 @@ package special.sigma {
def allZK(conditions: Rep[Coll[SigmaProp]]): Rep[SigmaProp];
def anyOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean];
def anyZK(conditions: Rep[Coll[SigmaProp]]): Rep[SigmaProp];
def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean];
def PubKey(base64String: Rep[String]): Rep[SigmaProp];
def sigmaProp(b: Rep[Boolean]): Rep[SigmaProp];
def blake2b256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]];
def sha256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]];
def byteArrayToBigInt(bytes: Rep[Coll[Byte]]): Rep[BigInt];
def longToByteArray(l: Rep[Long]): Rep[Coll[Byte]];
def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long];
def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp];
def proveDHTuple(g: Rep[GroupElement], h: Rep[GroupElement], u: Rep[GroupElement], v: Rep[GroupElement]): Rep[SigmaProp];
def isMember(tree: Rep[AvlTree], key: Rep[Coll[Byte]], proof: Rep[Coll[Byte]]): Rep[Boolean];
Expand Down
5 changes: 5 additions & 0 deletions sigma-api/src/main/scala/sigma/types/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ trait Boolean extends PrimView[scala.Boolean] {
* @since 2.0
*/
def toByte: Byte

/** Logical XOR
* @since 2.0
*/
def xor(y: Boolean): Boolean
}

trait Byte extends PrimView[scala.Byte] {
Expand Down
6 changes: 6 additions & 0 deletions sigma-api/src/main/scala/special/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ trait SigmaContract {
def anyOf(conditions: Coll[Boolean]): Boolean = this.builder.anyOf(conditions)
def anyZK(conditions: Coll[SigmaProp]): SigmaProp = this.builder.anyZK(conditions)

def xorOf(conditions: Coll[Boolean]): Boolean = this.builder.xorOf(conditions)

def PubKey(base64String: String): SigmaProp = this.builder.PubKey(base64String)

def sigmaProp(b: Boolean): SigmaProp = this.builder.sigmaProp(b)
Expand All @@ -443,6 +445,7 @@ trait SigmaContract {

def byteArrayToBigInt(bytes: Coll[Byte]): BigInt = this.builder.byteArrayToBigInt(bytes)
def longToByteArray(l: Long): Coll[Byte] = this.builder.longToByteArray(l)
def byteArrayToLong(bytes: Coll[Byte]): Long = this.builder.byteArrayToLong(bytes)

def proveDlog(g: GroupElement): SigmaProp = this.builder.proveDlog(g)
def proveDHTuple(g: GroupElement, h: GroupElement, u: GroupElement, v: GroupElement): SigmaProp = this.builder.proveDHTuple(g, h, u, v)
Expand Down Expand Up @@ -482,6 +485,8 @@ trait SigmaDslBuilder {
def anyOf(conditions: Coll[Boolean]): Boolean
def anyZK(conditions: Coll[SigmaProp]): SigmaProp

def xorOf(conditions: Coll[Boolean]): Boolean

def PubKey(base64String: String): SigmaProp

def sigmaProp(b: Boolean): SigmaProp
Expand All @@ -491,6 +496,7 @@ trait SigmaDslBuilder {

def byteArrayToBigInt(bytes: Coll[Byte]): BigInt
def longToByteArray(l: Long): Coll[Byte]
def byteArrayToLong(bytes: Coll[Byte]): Long

def proveDlog(g: GroupElement): SigmaProp
def proveDHTuple(g: GroupElement, h: GroupElement, u: GroupElement, v: GroupElement): SigmaProp
Expand Down
2 changes: 2 additions & 0 deletions sigma-api/src/main/scala/special/sigma/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package sigma {

case class WrapperType[Wrapper](cWrapper: ClassTag[Wrapper]) extends RType[Wrapper] {
override def classTag: ClassTag[Wrapper] = cWrapper
override def toString: String = cWrapper.toString
override def name: String = cWrapper.runtimeClass.getSimpleName
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ package special.sigma {
@NeverInline def anyOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = delayInvoke;
@NeverInline def allZK(props: Rep[Coll[SigmaProp]]): Rep[SigmaProp] = delayInvoke;
@NeverInline def anyZK(props: Rep[Coll[SigmaProp]]): Rep[SigmaProp] = delayInvoke;
@NeverInline override def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = delayInvoke;
@NeverInline def sigmaProp(b: Rep[Boolean]): Rep[SigmaProp] = delayInvoke;
@NeverInline def blake2b256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def sha256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def PubKey(base64String: Rep[String]): Rep[SigmaProp] = delayInvoke;
@NeverInline def byteArrayToBigInt(bytes: Rep[Coll[Byte]]): Rep[BigInt] = delayInvoke;
@NeverInline def longToByteArray(l: Rep[Long]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long] = delayInvoke;
@NeverInline def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp] = delayInvoke;
@NeverInline def proveDHTuple(g: Rep[GroupElement], h: Rep[GroupElement], u: Rep[GroupElement], v: Rep[GroupElement]): Rep[SigmaProp] = delayInvoke;
@NeverInline def isMember(tree: Rep[AvlTree], key: Rep[Coll[Byte]], proof: Rep[Coll[Byte]]): Rep[Boolean] = delayInvoke;
Expand Down
1 change: 1 addition & 0 deletions sigma-impl/src/main/scala/sigma/types/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import special.collection.{Coll, Builder}

case class CBoolean(value: scala.Boolean) extends Boolean {
override def toByte: Byte = CByte(if (value) 1 else 0)
override def xor(y: Boolean): Boolean = CBoolean(value ^ y.value)
}

case class CByte(value: scala.Byte) extends Byte {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class TestSigmaDslBuilder extends SigmaDslBuilder {
@NeverInline
def anyZK(props: Coll[SigmaProp]): SigmaProp = MockSigma(props.exists(p => p.isValid))

@NeverInline
override def xorOf(conditions: Coll[Boolean]): Boolean = conditions.toArray.distinct.length == 2

@NeverInline
def sigmaProp(b: Boolean): SigmaProp = MockSigma(b)

Expand All @@ -98,6 +101,8 @@ class TestSigmaDslBuilder extends SigmaDslBuilder {

@NeverInline
def longToByteArray(l: Long): Coll[Byte] = Colls.fromArray(Longs.toByteArray(l))
@NeverInline
def byteArrayToLong(bytes: Coll[Byte]): Long = Longs.fromByteArray(bytes.toArray)

@NeverInline
def proveDlog(g: GroupElement): SigmaProp = MockProveDlog(true, Colls.emptyColl[Byte])
Expand Down
2 changes: 2 additions & 0 deletions sigma-library/src/main/scala/special/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ package special.sigma {
def allZK(conditions: Rep[Coll[SigmaProp]]): Rep[SigmaProp];
def anyOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean];
def anyZK(conditions: Rep[Coll[SigmaProp]]): Rep[SigmaProp];
def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean];
def PubKey(base64String: Rep[String]): Rep[SigmaProp];
def sigmaProp(b: Rep[Boolean]): Rep[SigmaProp];
def blake2b256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]];
def sha256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]];
def byteArrayToBigInt(bytes: Rep[Coll[Byte]]): Rep[BigInt];
def longToByteArray(l: Rep[Long]): Rep[Coll[Byte]];
def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long];
def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp];
def proveDHTuple(g: Rep[GroupElement], h: Rep[GroupElement], u: Rep[GroupElement], v: Rep[GroupElement]): Rep[SigmaProp];
def isMember(tree: Rep[AvlTree], key: Rep[Coll[Byte]], proof: Rep[Coll[Byte]]): Rep[Boolean];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ package special.sigma {
@NeverInline def anyOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = delayInvoke;
@NeverInline def allZK(props: Rep[Coll[SigmaProp]]): Rep[SigmaProp] = delayInvoke;
@NeverInline def anyZK(props: Rep[Coll[SigmaProp]]): Rep[SigmaProp] = delayInvoke;
@NeverInline override def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = delayInvoke;
@NeverInline def sigmaProp(b: Rep[Boolean]): Rep[SigmaProp] = delayInvoke;
@NeverInline def blake2b256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def sha256(bytes: Rep[Coll[Byte]]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def PubKey(base64String: Rep[String]): Rep[SigmaProp] = delayInvoke;
@NeverInline def byteArrayToBigInt(bytes: Rep[Coll[Byte]]): Rep[BigInt] = delayInvoke;
@NeverInline def longToByteArray(l: Rep[Long]): Rep[Coll[Byte]] = delayInvoke;
@NeverInline def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long] = delayInvoke;
@NeverInline def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp] = delayInvoke;
@NeverInline def proveDHTuple(g: Rep[GroupElement], h: Rep[GroupElement], u: Rep[GroupElement], v: Rep[GroupElement]): Rep[SigmaProp] = delayInvoke;
@NeverInline def isMember(tree: Rep[AvlTree], key: Rep[Coll[Byte]], proof: Rep[Coll[Byte]]): Rep[Boolean] = delayInvoke;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4222,6 +4222,13 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, false, element[SigmaProp]))
}

override def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = {
asRep[Boolean](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("xorOf", classOf[Sym]),
List(conditions),
true, false, element[Boolean]))
}

override def PubKey(base64String: Rep[String]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("PubKey", classOf[Sym]),
Expand Down Expand Up @@ -4264,6 +4271,13 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, false, element[Coll[Byte]]))
}

override def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long] = {
asRep[Long](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("byteArrayToLong", classOf[Sym]),
List(bytes),
true, false, element[Long]))
}

override def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("proveDlog", classOf[Sym]),
Expand Down Expand Up @@ -4450,6 +4464,13 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, true, element[SigmaProp]))
}

def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = {
asRep[Boolean](mkMethodCall(source,
thisClass.getMethod("xorOf", classOf[Sym]),
List(conditions),
true, true, element[Boolean]))
}

def PubKey(base64String: Rep[String]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(source,
thisClass.getMethod("PubKey", classOf[Sym]),
Expand Down Expand Up @@ -4492,6 +4513,13 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, true, element[Coll[Byte]]))
}

def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long] = {
asRep[Long](mkMethodCall(source,
thisClass.getMethod("byteArrayToLong", classOf[Sym]),
List(bytes),
true, true, element[Long]))
}

def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(source,
thisClass.getMethod("proveDlog", classOf[Sym]),
Expand Down Expand Up @@ -4579,7 +4607,7 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
override protected def collectMethods: Map[java.lang.reflect.Method, MethodDesc] = {
super.collectMethods ++
Elem.declaredMethods(classOf[SigmaDslBuilder], classOf[SSigmaDslBuilder], Set(
"Colls", "Monoids", "Costing", "CostModel", "costBoxes", "costColWithConstSizedItem", "costOption", "verifyZK", "atLeast", "allOf", "allZK", "anyOf", "anyZK", "PubKey", "sigmaProp", "blake2b256", "sha256", "byteArrayToBigInt", "longToByteArray", "proveDlog", "proveDHTuple", "isMember", "treeLookup", "treeModifications", "groupGenerator", "substConstants", "decodePoint", "BigInt", "toBigInteger"
"Colls", "Monoids", "Costing", "CostModel", "costBoxes", "costColWithConstSizedItem", "costOption", "verifyZK", "atLeast", "allOf", "allZK", "anyOf", "anyZK", "xorOf", "PubKey", "sigmaProp", "blake2b256", "sha256", "byteArrayToBigInt", "longToByteArray", "byteArrayToLong", "proveDlog", "proveDHTuple", "isMember", "treeLookup", "treeModifications", "groupGenerator", "substConstants", "decodePoint", "BigInt", "toBigInteger"
))
}

Expand Down Expand Up @@ -4791,6 +4819,19 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
}
}

object xorOf {
def unapply(d: Def[_]): Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Boolean]])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[SigmaDslBuilderElem[_]] && method.getName == "xorOf" =>
val res = (receiver, args(0))
Nullable(res).asInstanceOf[Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Boolean]])]]
case _ => Nullable.None
}
def unapply(exp: Sym): Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Boolean]])] = exp match {
case Def(d) => unapply(d)
case _ => Nullable.None
}
}

object PubKey {
def unapply(d: Def[_]): Nullable[(Rep[SigmaDslBuilder], Rep[String])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[SigmaDslBuilderElem[_]] && method.getName == "PubKey" =>
Expand Down Expand Up @@ -4869,6 +4910,19 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
}
}

object byteArrayToLong {
def unapply(d: Def[_]): Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Byte]])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[SigmaDslBuilderElem[_]] && method.getName == "byteArrayToLong" =>
val res = (receiver, args(0))
Nullable(res).asInstanceOf[Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Byte]])]]
case _ => Nullable.None
}
def unapply(exp: Sym): Nullable[(Rep[SigmaDslBuilder], Rep[Coll[Byte]])] = exp match {
case Def(d) => unapply(d)
case _ => Nullable.None
}
}

object proveDlog {
def unapply(d: Def[_]): Nullable[(Rep[SigmaDslBuilder], Rep[GroupElement])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[SigmaDslBuilderElem[_]] && method.getName == "proveDlog" =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ object TestSigmaDslBuilder extends EntityObject("TestSigmaDslBuilder") {
true, false, element[SigmaProp]))
}

override def xorOf(conditions: Rep[Coll[Boolean]]): Rep[Boolean] = {
asRep[Boolean](mkMethodCall(self,
thisClass.getMethod("xorOf", classOf[Sym]),
List(conditions),
true, false, element[Boolean]))
}

override def sigmaProp(b: Rep[Boolean]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(self,
thisClass.getMethod("sigmaProp", classOf[Sym]),
Expand Down Expand Up @@ -314,6 +321,13 @@ object TestSigmaDslBuilder extends EntityObject("TestSigmaDslBuilder") {
true, false, element[Coll[Byte]]))
}

override def byteArrayToLong(bytes: Rep[Coll[Byte]]): Rep[Long] = {
asRep[Long](mkMethodCall(self,
thisClass.getMethod("byteArrayToLong", classOf[Sym]),
List(bytes),
true, false, element[Long]))
}

override def proveDlog(g: Rep[GroupElement]): Rep[SigmaProp] = {
asRep[SigmaProp](mkMethodCall(self,
thisClass.getMethod("proveDlog", classOf[Sym]),
Expand Down Expand Up @@ -662,6 +676,19 @@ object TestSigmaDslBuilder extends EntityObject("TestSigmaDslBuilder") {
}
}

object xorOf {
def unapply(d: Def[_]): Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Boolean]])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[TestSigmaDslBuilderElem] && method.getName == "xorOf" =>
val res = (receiver, args(0))
Nullable(res).asInstanceOf[Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Boolean]])]]
case _ => Nullable.None
}
def unapply(exp: Sym): Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Boolean]])] = exp match {
case Def(d) => unapply(d)
case _ => Nullable.None
}
}

object sigmaProp {
def unapply(d: Def[_]): Nullable[(Rep[TestSigmaDslBuilder], Rep[Boolean])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[TestSigmaDslBuilderElem] && method.getName == "sigmaProp" =>
Expand Down Expand Up @@ -740,6 +767,19 @@ object TestSigmaDslBuilder extends EntityObject("TestSigmaDslBuilder") {
}
}

object byteArrayToLong {
def unapply(d: Def[_]): Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Byte]])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[TestSigmaDslBuilderElem] && method.getName == "byteArrayToLong" =>
val res = (receiver, args(0))
Nullable(res).asInstanceOf[Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Byte]])]]
case _ => Nullable.None
}
def unapply(exp: Sym): Nullable[(Rep[TestSigmaDslBuilder], Rep[Coll[Byte]])] = exp match {
case Def(d) => unapply(d)
case _ => Nullable.None
}
}

object proveDlog {
def unapply(d: Def[_]): Nullable[(Rep[TestSigmaDslBuilder], Rep[GroupElement])] = d match {
case MethodCall(receiver, method, args, _) if receiver.elem.isInstanceOf[TestSigmaDslBuilderElem] && method.getName == "proveDlog" =>
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/sigmastate/eval/Evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ object Evaluation {
case StringType => SString
case AnyType => SAny
case BigIntegerRType => SBigInt
case BigIntRType => SBigInt
case ECPointRType => SGroupElement
case AvlTreeRType => SAvlTree
case ot: OptionType[_] => sigmastate.SOption(rtypeToSType(ot.tA))
Expand Down
Loading

0 comments on commit 8c93f79

Please sign in to comment.