Skip to content

Commit

Permalink
towards-data-module: some Extensions moved to sigma.eval
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Oct 8, 2023
1 parent 2a77625 commit cd088ca
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import sigma.Extensions.ArrayOps
import sigma.ast.SType.{AnyOps, TypeCode}
import sigma.ast._
import sigma.data.{AvlTreeData, CAvlTree, CSigmaDslBuilder, SigmaConstants}
import sigma.eval.Extensions.toAnyValue
import sigma.exceptions.InterpreterException
import sigma.validation.SigmaValidationSettings
import sigma.{AnyValue, Coll, Header, PreHeader}
import sigmastate.eval.Extensions._
import sigmastate.eval._
import sigmastate.interpreter.{ContextExtension, Interpreter, InterpreterContext}
import sigmastate.interpreter.{ContextExtension, InterpreterContext}

/** Represents a script evaluation context to be passed to a prover and a verifier to execute and
* validate guarding proposition of input boxes of a transaction.
Expand Down
2 changes: 1 addition & 1 deletion interpreter/shared/src/main/scala/sigma/ast/ErgoTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import sigma.ast.defs._
import sigma.data.SigmaBoolean
import sigma.kiama.rewriting.Rewriter.{everywherebu, strategy}
import sigma.validation.ValidationException
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigma.ast.defs.ValueOps
import sigma.eval.Extensions.SigmaBooleanOps
import sigma.serialization.ErgoTreeSerializer.DefaultSerializer
import sigma.serialization.{ConstantStore, ErgoTreeSerializer, SigmaSerializer, ValueSerializer}
import supertagged.TaggedType
Expand Down
2 changes: 1 addition & 1 deletion interpreter/shared/src/main/scala/sigma/ast/trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import sigma.data._
import sigma.serialization.CoreByteWriter.ArgInfo
import sigma.validation.SigmaValidationSettings
import sigma.{Coll, Colls, GroupElement, SigmaProp, VersionContext}
import sigmastate.eval.Extensions.EvalCollOps
import NumericOps.{BigIntIsExactIntegral, BigIntIsExactOrdering}
import sigma.eval.Extensions.EvalCollOps
import sigma.eval.SigmaDsl
import sigmastate.interpreter.ErgoTreeEvaluator
import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.data

import sigma._
import sigmastate.eval.Extensions._
import sigma.eval.Extensions.IntExt

import scala.math.{Integral, Ordering}

Expand Down
3 changes: 2 additions & 1 deletion interpreter/shared/src/main/scala/sigma/data/CBox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import sigma.ast.SCollection.SByteArray
import sigma.ast.defs._
import sigma.ast.{ConstantNode, EvaluatedValue, SInt, STuple, SType}
import sigma.data.CBox.regs
import sigma.eval.Extensions.toAnyValue
import sigma.exceptions.InvalidType
import sigma.{AnyValue, Box, Coll, Colls}
import sigmastate.eval.Extensions.toAnyValue

import java.util.Arrays

/** A default implementation of [[Box]] interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import scorex.crypto.hash.{Blake2b256, Sha256}
import scorex.utils.Longs
import sigma.ast.{AtLeast, SubstConstants}
import sigma.crypto.{CryptoConstants, EcPointType, Ecp}
import sigma.eval.Extensions.EvalCollOps
import sigma.serialization.{GroupElementSerializer, SigmaSerializer}
import sigma.util.Extensions.BigIntegerOps
import sigma.validation.SigmaValidationSettings
import sigma.{AvlTree, BigInt, Box, Coll, CollBuilder, GroupElement, SigmaDslBuilder, SigmaProp, VersionContext}
import sigmastate.eval.Extensions.EvalCollOps

import java.math.BigInteger

Expand Down
73 changes: 73 additions & 0 deletions interpreter/shared/src/main/scala/sigma/eval/Extensions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package sigma.eval

import sigma.ast.defs.SigmaPropValue
import sigma.data.{CAnyValue, CSigmaDslBuilder, Nullable, RType, SigmaBoolean}
import sigma.{BigInt, Coll, Evaluation}
import sigma.ast.{Constant, ConstantNode, SBoolean, SCollection, SCollectionType, SType, SigmaPropConstant, SigmaPropIsProven, TransformingSigmaBuilder, Value}
import sigmastate.Platform
import sigmastate.utils.Helpers

import java.math.BigInteger

object Extensions {
implicit class ByteExt(val b: Byte) extends AnyVal {
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
}

implicit class ShortExt(val b: Short) extends AnyVal {
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
}

implicit class IntExt(val x: Int) extends AnyVal {
/** Convert this value to BigInt. */
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(x.toLong))
}

implicit class LongExt(val x: Long) extends AnyVal {
/** Convert this value to BigInt. */
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(x))
}

def toAnyValue[A: RType](x: A) = new CAnyValue(x, RType[A].asInstanceOf[RType[Any]])

implicit class EvalCollOps[T](val coll: Coll[T]) extends AnyVal {
/** Helper type synonym. */
type ElemTpe = SType {type WrappedType = T}

/** Wraps the collection into ConstantNode using collection's element type. */
def toConstant: Constant[SCollection[ElemTpe]] = {
val elemTpe = Evaluation.rtypeToSType(coll.tItem).asInstanceOf[ElemTpe]
ConstantNode[SCollection[ElemTpe]](coll, SCollectionType(elemTpe))
}

/** Transforms this collection into array of constants.
*
* This method have exactly the same semantics on JS and JVM IF `coll.tItem`
* precisely describes the type of elements in `call`. (Which is the case for all
* collections created by ErgoTree interpreter).
*
* However, if it is not the case, then JVM and JS will have different semantics for Byte and Short.
*
* The JVM version preserves v5.0 consensus protocol semantics.
* The JS version is a reasonable approximation of the JVM version.
*/
def toArrayOfConstants: Array[Constant[SType]] = {
val constants = coll.toArray.map { v =>
// see ScalaDoc for ensureTypeCarringValue
val valToLift = Helpers.ensureTypeCarringValue(v, coll.tItem.asInstanceOf[RType[Any]])
// call platform-specific method to transform the value to a Constant
Platform.liftToConstant(valToLift, TransformingSigmaBuilder) match {
case Nullable(c) => c
case _ => sys.error(s"Cannot liftToConstant($valToLift)")
}
}
constants
}
}

implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal {
def toSigmaPropValue: SigmaPropValue = SigmaPropConstant(sb)

def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb))
}
}
71 changes: 3 additions & 68 deletions interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,14 @@ import scorex.crypto.authds.avltree.batch.{Insert, Lookup, Remove, Update}
import scorex.crypto.authds.{ADKey, ADValue}
import scorex.util.encode.Base16
import sigma.ast.SType.AnyOps
import sigma.data.{CAnyValue, CBox, CSigmaDslBuilder, Digest32Coll, Nullable, RType, SigmaBoolean}
import sigma.{Coll, _}
import sigmastate.Platform
import sigma.ast.{CheckingSigmaBuilder, SigmaPropIsProven, TransformingSigmaBuilder, _}
import sigma.ast.defs._
import sigmastate.utils.Helpers
import sigma.ast._
import sigma.data.{CBox, Digest32Coll, RType}
import sigma._

import java.math.BigInteger
import scala.util.{Failure, Success}

object Extensions {

implicit class ByteExt(val b: Byte) extends AnyVal {
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
}

implicit class ShortExt(val b: Short) extends AnyVal {
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
}

implicit class IntExt(val x: Int) extends AnyVal {
/** Convert this value to BigInt. */
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(x.toLong))
}

implicit class LongExt(val x: Long) extends AnyVal {
/** Convert this value to BigInt. */
@inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(x))
}

/** Extension methods for `Array[Byte]` not available for generic `Array[T]`. */
implicit class ArrayByteOps(val arr: Array[Byte]) extends AnyVal {
/** Wraps array into TokenId instance. The source array in not cloned. */
Expand All @@ -49,49 +27,12 @@ object Extensions {
@inline def toColl: Coll[T] = Colls.fromArray[T](seq.toArray(RType[T].classTag))
}

implicit class EvalCollOps[T](val coll: Coll[T]) extends AnyVal {
/** Helper type synonym. */
type ElemTpe = SType { type WrappedType = T}

/** Wraps the collection into ConstantNode using collection's element type. */
def toConstant: Constant[SCollection[ElemTpe]] = {
val elemTpe = Evaluation.rtypeToSType(coll.tItem).asInstanceOf[ElemTpe]
ConstantNode[SCollection[ElemTpe]](coll, SCollectionType(elemTpe))
}

/** Transforms this collection into array of constants.
*
* This method have exactly the same semantics on JS and JVM IF `coll.tItem`
* precisely describes the type of elements in `call`. (Which is the case for all
* collections created by ErgoTree interpreter).
*
* However, if it is not the case, then JVM and JS will have different semantics for Byte and Short.
*
* The JVM version preserves v5.0 consensus protocol semantics.
* The JS version is a reasonable approximation of the JVM version.
*/
def toArrayOfConstants: Array[Constant[SType]] = {
val constants = coll.toArray.map { v =>
// see ScalaDoc for ensureTypeCarringValue
val valToLift = Helpers.ensureTypeCarringValue(v, coll.tItem.asInstanceOf[RType[Any]])
// call platform-specific method to transform the value to a Constant
Platform.liftToConstant(valToLift, TransformingSigmaBuilder) match {
case Nullable(c) => c
case _ => sys.error(s"Cannot liftToConstant($valToLift)")
}
}
constants
}
}

implicit class DslDataOps[A](data: A)(implicit tA: RType[A]) {
def toTreeData: Constant[SType] = {
CheckingSigmaBuilder.mkConstant(data.asWrappedType, Evaluation.rtypeToSType(tA))
}
}

def toAnyValue[A:RType](x: A) = new CAnyValue(x, RType[A].asInstanceOf[RType[Any]])

implicit class ErgoBoxOps(val ebox: ErgoBox) extends AnyVal {
def toTestBox: Box = {
/* NOHF PROOF:
Expand All @@ -104,12 +45,6 @@ object Extensions {
}
}

implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal {
def toSigmaPropValue: SigmaPropValue = SigmaPropConstant(sb)

def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb))
}

implicit class AvlTreeOps(val tree: AvlTree) extends AnyVal {

def contains(key: Coll[Byte], proof: Coll[Byte]): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.serialization

import sigma.data.ProveDHTuple
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigma.eval.Extensions.SigmaBooleanOps

class PDHTSerializerSpecification extends SerializationSpecification {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.serialization

import sigma.data.ProveDlog
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigma.eval.Extensions.SigmaBooleanOps

class ProveDlogSerializerSpec extends SerializationSpecification {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.Assertion
import sigma.Extensions.ArrayOps
import sigma.data.{AvlTreeData, CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, TrivialProp}
import sigma.eval.Extensions.SigmaBooleanOps
import sigmastate._
import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage
import sigmastate.crypto.VerifierMessage.Challenge
import sigmastate.crypto.SecondDHTupleProverMessage
import sigmastate.crypto.GF2_192_Poly
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTransactionTesting, TestingCommons}
import sigmastate.interpreter.Interpreter
import sigma.serialization.generators.ObjectGenerators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import sigma.util.Extensions.EcpOps
import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus}
import sigma.validation.ValidationRules.FirstRuleId
import ErgoTree.ZeroHeader
import sigma.eval.Extensions.SigmaBooleanOps
import sigma.eval.SigmaDsl

import java.math.BigInteger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import sigma.SigmaDslTesting
import sigma.Extensions._
import sigma.ast.defs.{ErgoBoxCandidateRType, TrueSigmaProp}
import sigma.data.{CSigmaProp, Digest32Coll, TrivialProp}
import sigma.eval.Extensions.EvalCollOps

class ErgoLikeTransactionSpec extends SigmaDslTesting {

Expand Down
3 changes: 2 additions & 1 deletion sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import sigma.{VersionContext, ast, data, _}
import ErgoTree.{HeaderType, ZeroHeader}
import sigma.eval.{CostDetails, SigmaDsl, TracedCost}
import sigmastate._
import sigmastate.eval.Extensions.{AvlTreeOps, ByteExt, IntExt, LongExt, ShortExt}
import sigmastate.eval.Extensions.AvlTreeOps
import sigma.eval.Extensions.{ByteExt, IntExt, LongExt, ShortExt}
import OrderingOps._
import sigmastate.eval._
import sigmastate.helpers.TestingHelpers._
Expand Down
2 changes: 1 addition & 1 deletion sc/shared/src/test/scala/sigma/SigmaDslStaginTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigma
import org.scalatest.BeforeAndAfterAll
import scalan.{BaseCtxTests, BaseLiftableTests}
import sigma.data.TrivialProp
import sigmastate.eval.Extensions._
import sigma.eval.Extensions.toAnyValue
import sigmastate.eval._

import scala.language.reflectiveCalls
Expand Down
7 changes: 3 additions & 4 deletions sc/shared/src/test/scala/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ import sigma.ast._
import sigma.eval.{CostDetails, SigmaDsl}
import sigmastate.crypto.DLogProtocol.DLogProverInput
import sigmastate.crypto.SigmaProtocolPrivateInput
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigmastate.eval.{CContext, CompiletimeIRContext, IRContext}
import sigmastate.helpers.TestingHelpers._
import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, SigmaPPrint}
import sigmastate.interpreter.Interpreter.{ScriptEnv, VerificationResult}
import sigmastate.interpreter._
import sigma.ast.Apply
import sigma.eval.Extensions.SigmaBooleanOps
import sigma.serialization.ValueSerializer
import sigma.serialization.generators.ObjectGenerators
import sigmastate.utils.Helpers._
import sigma.validation.ValidationRules.CheckSerializableTypeCode
import sigma.validation.{SigmaValidationSettings, ValidationException}
import sigmastate.eval

import scala.collection.mutable
import scala.reflect.ClassTag
Expand Down Expand Up @@ -352,8 +351,8 @@ class SigmaDslTesting extends AnyPropSpec
)

// We add ctx as it's own variable with id = 1
val ctxVar = sigmastate.eval.Extensions.toAnyValue[sigma.Context](ctx)(sigma.ContextRType)
val carolVar = sigmastate.eval.Extensions.toAnyValue[Coll[Byte]](pkCarolBytes.toColl)(RType[Coll[Byte]])
val ctxVar = sigma.eval.Extensions.toAnyValue[sigma.Context](ctx)(sigma.ContextRType)
val carolVar = sigma.eval.Extensions.toAnyValue[Coll[Byte]](pkCarolBytes.toColl)(RType[Coll[Byte]])
val newCtx = ctx
.withUpdatedVars(1 -> ctxVar, 2 -> carolVar)
.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import sigma.data.{RType, SigmaBoolean}
import sigma.validation.ValidationException
import sigma.validation.ValidationRules.CheckSerializableTypeCode
import sigma.ast.defs.{SValue, SigmaPropValue}
import sigma.eval.{CostDetails, GivenCost, TracedCost}
import sigmastate.eval._
import sigma.eval.{CostDetails, Extensions, GivenCost, TracedCost}
import sigmastate.helpers.TestingHelpers._
import sigmastate.interpreter.ContextExtension.VarBinding
import sigmastate.interpreter.ErgoTreeEvaluator.DefaultProfiler
Expand All @@ -22,6 +21,7 @@ import sigmastate.interpreter._
import sigmastate.lang.{CompilerSettings, SigmaCompiler}
import sigma.serialization.SigmaSerializer
import sigmastate.CompilerTestsBase
import sigmastate.eval.{CContext, IRContext}

import scala.language.implicitConversions
import scala.reflect.ClassTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import sigma.ast._
import sigma.ast.defs._
import sigmastate._
import sigma.Extensions.ArrayOps
import sigma.eval.Extensions.SigmaBooleanOps
import sigma.eval.SigmaDsl
import sigmastate.eval.Extensions.{EvalIterableOps, SigmaBooleanOps}
import sigmastate.eval.Extensions.EvalIterableOps
import sigmastate.eval._
import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter}
import sigmastate.interpreter.{ContextExtension, CostedProverResult}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import sigma.util.Extensions.SigmaPropOps
import sigma.validation.ValidationException
import ErgoTree.EmptyConstants
import ErgoTree.HeaderType
import sigma.eval.Extensions.SigmaBooleanOps
import sigmastate._
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigmastate.eval.IRContext
import sigmastate.helpers.CompilerTestingCommons
import sigma.serialization.ErgoTreeSerializer.DefaultSerializer

import java.math.BigInteger

class ErgoTreeSerializerSpecification extends SerializationSpecification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import sigma.ast.SCollection._
import sigma.data.AvlTreeData
import SCollectionMethods.{FlatMapMethod, IndexOfMethod, IndicesMethod, PatchMethod, UpdateManyMethod, UpdatedMethod}
import sigma.ast
import sigmastate.eval.Extensions.SigmaBooleanOps
import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv}
import sigma.serialization.OpCodes._
import sigma.ast.MethodCall
import sigma.eval.Extensions.SigmaBooleanOps
import sigmastate.utils.Helpers._

class CollectionOperationsSpecification extends CompilerTestingCommons
Expand Down
Loading

0 comments on commit cd088ca

Please sign in to comment.