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 missing ops and predefined functions for 2.0 #400

Merged
merged 30 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
49dbe17
add byteArrayToLong;
greenhat Feb 19, 2019
0bc2c2b
add xorOf;
greenhat Feb 19, 2019
60fde65
add (de)serialization roundtrip into compilation pipeline in tests;
greenhat Feb 20, 2019
6f3f4e4
fix numeric casts test (got reduced to constants);
greenhat Feb 20, 2019
9cc74ba
add MethodCall.typeSubst (map of concrete types for each generic type…
greenhat Feb 20, 2019
699d55c
fix SOption.method to have generic types (don't substitute for SOptio…
greenhat Feb 21, 2019
3899508
fix build after rebase;
greenhat Feb 21, 2019
4a2f89b
enable tests for Coll.patch, .updated, .updateMany;
greenhat Feb 21, 2019
cd0d4e8
add BitOp serialization;
greenhat Feb 21, 2019
6fbeb9b
add BinXor serialization;
greenhat Feb 21, 2019
a8625a0
add LogicalNotSerializer;
greenhat Feb 21, 2019
be490bf
add SigmaDslBuilder.byteArrayToLong;
greenhat Feb 21, 2019
3745d2e
add SigmaDslBuilder.byteArrayToLong implementation (codegen);
greenhat Feb 21, 2019
9668338
disable (de)serialization roundtrip for ZProofBlock test;
greenhat Feb 22, 2019
2f3cd15
add sigma API for xorOf, logicalNot, negation, logicalXor;
greenhat Feb 22, 2019
edcf2c0
add codegen for sigma API xorOf, logicalNot, negation, logicalXor;
greenhat Feb 22, 2019
24de39f
add xorOf handling in costing;
greenhat Feb 22, 2019
fcf3058
add logicalNot full implementation;
greenhat Feb 22, 2019
2abd8ec
add numeric negate full implementation;
greenhat Feb 22, 2019
f4e6ba6
fix SigmaCompilerTest;
greenhat Feb 22, 2019
823e0cd
disable SigmaDslTest xor test;
greenhat Feb 22, 2019
1daf285
build fix after rebase;
greenhat Feb 22, 2019
a81ffea
remove adaptSigmaBoolean in XorOf costing;
greenhat Feb 22, 2019
55eb2d1
add special.sigma.BigInt test (failing);
greenhat Feb 23, 2019
237de80
fix byteArrayToLong and xorOf tests to test against SigmaDsl;
greenhat Feb 23, 2019
391cec0
make SOption a SGenericType;
greenhat Feb 24, 2019
eb4b3c1
remove type param index in MethodCall serialization (only concrete ty…
greenhat Feb 25, 2019
37804f6
add Box.getReg handling via MethodCall throughout the whole pipeline
greenhat Feb 26, 2019
772a030
fix build;
greenhat Feb 26, 2019
2a5524a
fix and enable special.sigma.BigInt negation SigmaDsl test;
greenhat Feb 26, 2019
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
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
3 changes: 3 additions & 0 deletions sigma-api/src/main/scala/special/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,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 +493,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
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
45 changes: 43 additions & 2 deletions src/main/scala/sigmastate/eval/RuntimeCosting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,10 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
withDefaultSize(ApplyBinOp(binop, x.value, y.value), x.cost + y.cost + costOf(op))
}

case LogicalNot(input) =>
val inputC = evalNode(ctx, env, input)
withDefaultSize(ApplyUnOp(Not, inputC.value), inputC.cost + costOf(node))

// case ModQ(input) =>
// val inputC = asRep[Costed[WBigInteger]](eval(input))
// val v = inputC.value.modQ
Expand Down Expand Up @@ -1415,6 +1419,20 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
withDefaultSize(res, cost)
}

case XorOf(input) => input match {
case ConcreteCollection(items, tpe) =>
val itemsC = items.map(item => eval(adaptSigmaBoolean(item)))
greenhat marked this conversation as resolved.
Show resolved Hide resolved
val res = sigmaDslBuilder.xorOf(colBuilder.fromItems(itemsC.map(_.value): _*))
val costs = colBuilder.fromItems(itemsC.map(_.cost): _*)
val cost = costs.sum(intPlusMonoid) + perItemCostOf(node, costs.length)
withDefaultSize(res, cost)
case _ =>
val inputC = asRep[CostedColl[Boolean]](eval(input))
val res = sigmaDslBuilder.xorOf(inputC.value)
val cost = inputC.cost + perItemCostOf(node, inputC.sizes.length)
withDefaultSize(res, cost)
}

case BinOr(l, r) =>
val lC = evalNode(ctx, env, l)
val rC = RCostedThunk(Thunk(evalNode(ctx, env, r)), 0)
Expand All @@ -1430,6 +1448,22 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
val c = lC.cost + rC.cost + costOf(node)
withDefaultSize(v, c)

// case BinXor(l, r) =>
// val lC = evalNode(ctx, env, l)
// val rC = RCostedThunk(Thunk(evalNode(ctx, env, r)), 0)
// val v = sigmaDslBuilder.binXor(lC.value, rC.value)
// val c = lC.cost + rC.cost + costOf(node)
// withDefaultSize(v, c)

case neg: Negation[t] =>
val tpe = neg.input.tpe
val et = stypeToElem(tpe)
val op = NumericNegate(elemToNumeric(et))(et)
val inputC = evalNode(ctx, env, neg.input)
inputC match { case x: RCosted[a] =>
withDefaultSize(ApplyUnOp(op, x.value), x.cost + costOf(neg))
}

case SigmaAnd(items) =>
val itemsC = items.map(eval)
val res = sigmaDslBuilder.allZK(colBuilder.fromItems(itemsC.map(_.value.asRep[SigmaProp]): _*))
Expand Down Expand Up @@ -1520,6 +1554,13 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
val cost = inputC.cost + costOf(node)
withDefaultSize(res, cost)

case ByteArrayToLong(In(arr)) =>
val arrC = asRep[Costed[Coll[Byte]]](arr)
val value = sigmaDslBuilder.byteArrayToLong(arrC.value)
val size = arrC.dataSize
greenhat marked this conversation as resolved.
Show resolved Hide resolved
val cost = arrC.cost + costOf(node)
RCCostedPrim(value, cost, size)

case Xor(InCollByte(l), InCollByte(r)) =>
val values = colBuilder.xor(l.value, r.value)
val sizes = r.sizes
Expand Down Expand Up @@ -1548,7 +1589,7 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
val res = sigmaDslBuilder.decodePoint(bytes.values)
withDefaultSize(res, costOf(node))

case Terms.MethodCall(obj, method, args) if obj.tpe.isCollectionLike =>
case Terms.MethodCall(obj, method, args, _) if obj.tpe.isCollectionLike =>
val xsC = asRep[CostedColl[Any]](evalNode(ctx, env, obj))
val (argsVals, argsCosts) = args.map {
case sfunc: Value[SFunc]@unchecked if sfunc.tpe.isFunc =>
Expand Down Expand Up @@ -1585,7 +1626,7 @@ trait RuntimeCosting extends SigmaLibrary with DataCosting with Slicing { IR: Ev
}
withDefaultSize(value, cost)

case Terms.MethodCall(obj, method, args) if obj.tpe.isOption =>
case Terms.MethodCall(obj, method, args, _) if obj.tpe.isOption =>
val optC = asRep[CostedOption[Any]](eval(obj))
val argsC = args.map(eval)
(method.name, argsC) match {
Expand Down
Loading