From e629124736be41f1a1eefcf45134774f792ab738 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Fri, 8 Sep 2023 22:43:50 +0200 Subject: [PATCH 01/34] sigma-ast: TypeSerializer code moved to CoreTypeSerializer --- .../src/main/scala/sigma/SigmaException.scala | 17 ++ .../sigma/serialization/CoreByteReader.scala | 173 +++++++++++++ .../sigma/serialization/CoreSerializer.scala | 68 +++++ .../serialization/CoreTypeSerializer.scala | 220 ++++++++++++++++ .../serialization/SerializerException.scala | 39 +++ .../sigma/serialization/SigmaByteWriter.scala | 240 ++++++++++++++++++ .../scala/sigma/validation/RuleStatus.scala | 53 ++++ .../validation/SigmaValidationSettings.scala | 83 ++++++ .../sigma/validation/SoftForkChecker.scala | 42 +++ .../sigma/validation/ValidationRules.scala | 215 ++++++++++++++++ 10 files changed, 1150 insertions(+) create mode 100644 core/shared/src/main/scala/sigma/SigmaException.scala create mode 100644 core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala create mode 100644 core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala create mode 100644 core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala create mode 100644 core/shared/src/main/scala/sigma/serialization/SerializerException.scala create mode 100644 core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala create mode 100644 core/shared/src/main/scala/sigma/validation/RuleStatus.scala create mode 100644 core/shared/src/main/scala/sigma/validation/SigmaValidationSettings.scala create mode 100644 core/shared/src/main/scala/sigma/validation/SoftForkChecker.scala create mode 100644 core/shared/src/main/scala/sigma/validation/ValidationRules.scala diff --git a/core/shared/src/main/scala/sigma/SigmaException.scala b/core/shared/src/main/scala/sigma/SigmaException.scala new file mode 100644 index 0000000000..60bf3403e9 --- /dev/null +++ b/core/shared/src/main/scala/sigma/SigmaException.scala @@ -0,0 +1,17 @@ +package sigma + +import scala.collection.immutable.ArraySeq + +/** Base class for Sigma-related exceptions. + * + * @param message the error message + * @param cause an optional cause for the exception + * @param args an optional sequence of arguments to be passed with the exception + */ +class SigmaException( + val message: String, + val cause: Option[Throwable] = None, + val args: Seq[Any] = ArraySeq.empty) extends Exception(message, cause.orNull) + + + diff --git a/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala b/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala new file mode 100644 index 0000000000..b8b66c717a --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala @@ -0,0 +1,173 @@ +package sigma.serialization + +import scorex.util.Extensions._ +import scorex.util.serialization.Reader +import sigma.util.safeNewArray +import debox.cfor +import sigma.ast.SType +import sigma.validation.ValidationRules.CheckPositionLimit +import sigmastate.serialization.CoreTypeSerializer + +/** Reader used in the concrete implementations of [[SigmaSerializer]]. + * It decorates the given reader, delegates most of the methods to it, but also adds new + * methods. + * + * @param r the underlying reader this reader reads from + * @param constantStore the store of constants which is used to resolve + * [[sigmastate.Values.ConstantPlaceholder]] + * @param resolvePlaceholdersToConstants if true then resolved constants will be + * substituted in the tree instead of the placeholder. + * @param maxTreeDepth limit on the tree depth (recursive invocations) + * of the deserializer + */ +class CoreByteReader(val r: Reader, + val maxTreeDepth: Int = CoreSerializer.MaxTreeDepth) + extends Reader { + + /** Checks that the current reader position is <= positionLimit. + * NOTE, since v5.0 the same check is done via validation rule, which wraps the + * original exception into ValidationException. This makes this condition soft-forkable, + * other than that both v4.x and v5.x will work and fail at the same time, so the + * change is consensus-safe. + */ + @inline private def checkPositionLimit(): Unit = { + CheckPositionLimit(position, positionLimit) + } + + override type CH = r.CH + + @inline + override def newReader(chunk: CH): Reader.Aux[CH] = r.newReader(chunk) + + @inline override def getChunk(size: Int): CH = r.getChunk(size) + + @inline override def getShortString(): String = { + checkPositionLimit() + r.getShortString() + } + + @inline override def peekByte(): Byte = r.peekByte() + + @inline override def getByte(): Byte = { + checkPositionLimit() + r.getByte() + } + + @inline override def getUByte(): Int = { + checkPositionLimit() + r.getUByte() + } + + @inline override def getShort(): Short = { + checkPositionLimit() + r.getShort() + } + + @inline override def getUShort(): Int = { + checkPositionLimit() + r.getUShort() + } + + @inline override def getInt(): Int = { + checkPositionLimit() + r.getInt() + } + + @inline override def getUInt(): Long = { + checkPositionLimit() + r.getUInt() + } + + @inline def getUIntExact: Int = getUInt().toIntExact + + @inline override def getLong(): Long = { + checkPositionLimit() + r.getLong() + } + + @inline override def getULong(): Long = { + checkPositionLimit() + r.getULong() + } + + @inline override def getBytes(size: Int): Array[Byte] = { + checkPositionLimit() + r.getBytes(size) + } + + /** Reads either the given of remaining number of bytes from this reader. + * This method is `unsafe` because it may return less than requested number of bytes. + * @param numRequestedBytes + */ + @inline final def getBytesUnsafe(numRequestedBytes: Int): Array[Byte] = { + checkPositionLimit() + val bytesToRead = Math.min(numRequestedBytes, remaining) + r.getBytes(bytesToRead) + } + + /** Returns all bytes of the underlying ByteBuffer. */ + private[sigma] def getAllBufferBytes: Array[Byte] = { + val savedPos = position + position = 0 + val res = getBytesUnsafe(remaining) + position = savedPos + res + } + + @inline override def getBits(size: Int): Array[Boolean] = { + checkPositionLimit() + r.getBits(size) + } + + @inline override def getOption[T](getValue: => T): Option[T] = { + checkPositionLimit() + r.getOption(getValue) + } + + @inline override def consumed: Int = r.consumed + + @inline override def position: Int = r.position + + @inline override def position_=(p: Int): Unit = r.position_=(p) + + @inline override def remaining: Int = r.remaining + + @inline override def mark(): this.type = { + r.mark() + this + } + + @inline def getType(): SType = CoreTypeSerializer.deserialize(this) + + private var lvl: Int = 0 + @inline def level: Int = lvl + @inline def level_=(v: Int): Unit = { + if (v > maxTreeDepth) + throw new DeserializeCallDepthExceeded(s"nested value deserialization call depth($v) exceeds allowed maximum $maxTreeDepth") + lvl = v + } + + private var positionLmt: Int = r.position + r.remaining + @inline final def positionLimit: Int = positionLmt + @inline final def positionLimit_=(v: Int): Unit = { + positionLmt = v + } + + private var _complexity: Int = 0 + /** Helper property which is used to accumulate complexity during parsing. */ + @inline final def complexity: Int = _complexity + @inline final def complexity_=(v: Int): Unit = { + _complexity = v + } + + @inline final def addComplexity(delta: Int): Unit = { + _complexity += delta + } + + private var _wasDeserialize: Boolean = false + /** Helper property which is used to track deserialization operations during parsing. */ + @inline final def wasDeserialize: Boolean = _wasDeserialize + @inline final def wasDeserialize_=(v: Boolean): Unit = { + _wasDeserialize = v + } +} diff --git a/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala b/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala new file mode 100644 index 0000000000..aec82eb9df --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala @@ -0,0 +1,68 @@ +package sigma.serialization + +import scorex.util.ByteArrayBuilder +import scorex.util.serialization.{Serializer, VLQByteBufferReader, VLQByteBufferWriter} +import sigma.data.SigmaConstants + +import java.nio.ByteBuffer + +abstract class CoreSerializer[TFamily, T <: TFamily] extends Serializer[TFamily, T, CoreByteReader, CoreByteWriter] { + + def error(msg: String) = throw new SerializerException(msg, None) + + final def toBytes(obj: T): Array[Byte] = { + val w = CoreSerializer.startWriter() + serialize(obj, w) + w.toBytes + } + + final def fromBytes(bytes: Array[Byte]): TFamily = { + parse(CoreSerializer.startReader(bytes)) + } +} + +object CoreSerializer { + type Position = Int + + val MaxPropositionSize: Int = SigmaConstants.MaxPropositionBytes.value + val MaxTreeDepth: Int = SigmaConstants.MaxTreeDepth.value + + /** Helper function to be use in serializers. + * Starting position is marked and then used to compute number of consumed bytes. + * val r = Serializer.startReader(bytes, pos) + * val obj = r.getValue() + * obj -> r.consumed + */ + def startReader(bytes: Array[Byte], + pos: Int = 0): CoreByteReader = { + val buf = ByteBuffer.wrap(bytes) + buf.position(pos) + val r = new CoreByteReader( + new VLQByteBufferReader(buf), + maxTreeDepth = MaxTreeDepth + ).mark() + r + } + + /** Helper function to be use in serializers. */ + def startReader(bytes: Array[Byte]): CoreByteReader = { + val buf = ByteBuffer.wrap(bytes) + val r = new CoreByteReader(new VLQByteBufferReader(buf), + maxTreeDepth = MaxTreeDepth).mark() + r + } + + /** Helper function to be use in serializers. + * val w = Serializer.startWriter() + * w.putLong(l) + * val res = w.toBytes + * res */ + def startWriter(): CoreByteWriter = { + val b = new ByteArrayBuilder() + val wi = new VLQByteBufferWriter(b) + val w = new CoreByteWriter(wi) + w + } + +} + diff --git a/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala b/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala new file mode 100644 index 0000000000..4177723768 --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala @@ -0,0 +1,220 @@ +package sigmastate.serialization + +import debox.cfor +import sigma.ast.SCollectionType.{CollectionTypeCode, NestedCollectionTypeCode} +import sigma.ast._ +import sigma.serialization.{CoreByteReader, CoreByteWriter, InvalidTypePrefix} +import sigma.util.safeNewArray +import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckTypeCode} + +import java.nio.charset.StandardCharsets + +/** Serialization of types according to specification in TypeSerialization.md. */ +class CoreTypeSerializer { + import CoreTypeSerializer._ + + def getEmbeddableType(code: Int): SType = { + CheckPrimitiveTypeCode(code.toByte) + embeddableIdToType(code) + } + + def serialize(tpe: SType, w: CoreByteWriter): Unit = tpe match { + case p: SEmbeddable => w.put(p.typeCode) + case SString => w.put(SString.typeCode) + case SAny => w.put(SAny.typeCode) + case SUnit => w.put(SUnit.typeCode) + case SBox => w.put(SBox.typeCode) + case SAvlTree => w.put(SAvlTree.typeCode) + case SContext => w.put(SContext.typeCode) + case SGlobal => w.put(SGlobal.typeCode) + case SHeader => w.put(SHeader.typeCode) + case SPreHeader => w.put(SPreHeader.typeCode) + case c: SCollectionType[a] => c.elemType match { + case p: SEmbeddable => + val code = p.embedIn(CollectionTypeCode) + w.put(code) + case cn: SCollectionType[a] => cn.elemType match { + case p: SEmbeddable => + val code = p.embedIn(NestedCollectionTypeCode) + w.put(code) + case _ => + w.put(CollectionTypeCode) + serialize(cn, w) + } + case t => + w.put(CollectionTypeCode) + serialize(t, w) + } + case o: SOption[a] => o.elemType match { + case p: SEmbeddable => + val code = p.embedIn(SOption.OptionTypeCode) + w.put(code) + case c: SCollectionType[a] => c.elemType match { + case p: SEmbeddable => + val code = p.embedIn(SOption.OptionCollectionTypeCode) + w.put(code) + case _ => + w.put(SOption.OptionTypeCode) + serialize(c, w) + } + case t => + w.put(SOption.OptionTypeCode) + serialize(t, w) + } + case _ @ STuple(Seq(t1, t2)) => (t1, t2) match { + case (p: SEmbeddable, _) => + if (p == t2) { + // Symmetric pair of primitive types (`(Int, Int)`, `(Byte,Byte)`, etc.) + val code = p.embedIn(STuple.PairSymmetricTypeCode) + w.put(code) + } else { + // Pair of types where first is primitive (`(_, Int)`) + val code = p.embedIn(STuple.Pair1TypeCode) + w.put(code) + serialize(t2, w) + } + case (_, p: SEmbeddable) => + // Pair of types where second is primitive (`(Int, _)`) + val code = p.embedIn(STuple.Pair2TypeCode) + w.put(code) + serialize(t1, w) + case _ => + // Pair of non-primitive types (`((Int, Byte), (Boolean,Box))`, etc.) + w.put(STuple.Pair1TypeCode) + serialize(t1, w) + serialize(t2, w) + } + case STuple(items) if items.length < 2 => + sys.error(s"Invalid Tuple type with less than 2 items $items") + case tup: STuple => tup.items.length match { + case 3 => + // Triple of types + w.put(STuple.TripleTypeCode) + for (i <- tup.items) + serialize(i, w) + case 4 => + // Quadruple of types + w.put(STuple.QuadrupleTypeCode) + for (i <- tup.items) + serialize(i, w) + case _ => + // `Tuple` type with more than 4 items `(Int, Byte, Box, Boolean, Int)` + serializeTuple(tup, w) + } + case typeIdent: STypeVar => { + w.put(typeIdent.typeCode) + val bytes = typeIdent.name.getBytes(StandardCharsets.UTF_8) + w.putUByte(bytes.length) + .putBytes(bytes) + } + } + + def deserialize(r: CoreByteReader): SType = deserialize(r, 0) + + private def deserialize(r: CoreByteReader, depth: Int): SType = { + val c = r.getUByte() + if (c <= 0) + throw new InvalidTypePrefix(s"Cannot deserialize type prefix $c. Unexpected buffer $r with bytes ${r.getBytes(r.remaining)}") + val tpe: SType = if (c < STuple.TupleTypeCode) { + val constrId = c / SPrimType.PrimRange + val primId = c % SPrimType.PrimRange + constrId match { + case 0 => // primitive + getEmbeddableType(c) + case 1 => // Array[_] + val tElem = getArgType(r, primId, depth) + SCollection(tElem) + case 2 => // Array[Array[_]] + val tElem = getArgType(r, primId, depth) + SCollection(SCollection(tElem)) + case 3 => // Option[_] + val tElem = getArgType(r, primId, depth) + SOption(tElem) + case 4 => // Option[Collection[_]] + val tElem = getArgType(r, primId, depth) + SOption(SCollection(tElem)) + case STuple.Pair1TypeConstrId => // (_, t2) + val (t1, t2) = if (primId == 0) { + // Pair of non-primitive types (`((Int, Byte), (Boolean,Box))`, etc.) + (deserialize(r, depth + 1), deserialize(r, depth + 1)) + } else { + // Pair of types where first is primitive (`(_, Int)`) + (getEmbeddableType(primId), deserialize(r, depth + 1)) + } + STuple(t1, t2) + case STuple.Pair2TypeConstrId => // (t1, _) + if (primId == 0) { + // Triple of types + val (t1, t2, t3) = (deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1)) + STuple(t1, t2, t3) + } else { + // Pair of types where second is primitive (`(Int, _)`) + val t2 = getEmbeddableType(primId) + val t1 = deserialize(r, depth + 1) + STuple(t1, t2) + } + case STuple.PairSymmetricTypeConstrId => // (_, _) + if (primId == 0) { + // Quadruple of types + val (t1, t2, t3, t4) = (deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1)) + STuple(t1, t2, t3, t4) + } else { + // Symmetric pair of primitive types (`(Int, Int)`, `(Byte,Byte)`, etc.) + val t = getEmbeddableType(primId) + STuple(t, t) + } + } + } + else { + c match { + case STuple.TupleTypeCode => { + val len = r.getUByte() + val items = safeNewArray[SType](len) + cfor(0)(_ < len, _ + 1) { i => + items(i) = deserialize(r, depth + 1) + } + STuple(items) + } + case SAny.typeCode => SAny + case SUnit.typeCode => SUnit + case SBox.typeCode => SBox + case SAvlTree.typeCode => SAvlTree + case SContext.typeCode => SContext + case SString.typeCode => SString + case STypeVar.TypeCode => { + val nameLength = r.getUByte() + val name = new String(r.getBytes(nameLength), StandardCharsets.UTF_8) + STypeVar(name) + } + case SHeader.typeCode => SHeader + case SPreHeader.typeCode => SPreHeader + case SGlobal.typeCode => SGlobal + case _ => + CheckTypeCode(c.toByte) + NoType + } + } + tpe + } + + private def getArgType(r: CoreByteReader, primId: Int, depth: Int) = + if (primId == 0) + deserialize(r, depth + 1) + else + getEmbeddableType(primId) + + private def serializeTuple(t: STuple, w: CoreByteWriter) = { + assert(t.items.length <= 255) + w.put(STuple.TupleTypeCode) + w.putUByte(t.items.length) + for (i <- t.items) + serialize(i, w) + } +} + +object CoreTypeSerializer extends CoreTypeSerializer { + /** The list of embeddable types, i.e. types that can be combined with type constructor for optimized encoding. + * For each embeddable type `T`, and type constructor `C`, the type `C[T]` can be represented by single byte. */ + val embeddableIdToType = Array[SType](null, SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp) + +} \ No newline at end of file diff --git a/core/shared/src/main/scala/sigma/serialization/SerializerException.scala b/core/shared/src/main/scala/sigma/serialization/SerializerException.scala new file mode 100644 index 0000000000..cd82c5b6fe --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/SerializerException.scala @@ -0,0 +1,39 @@ +package sigma.serialization + +import sigma.SigmaException + +import scala.collection.immutable.ArraySeq + +/** Exception thrown during serialization. + * + * @param message the error message + * @param cause an optional cause for the exception + */ +case class SerializerException( + override val message: String, + override val cause: Option[Throwable] = None, + override val args: Seq[Any] = ArraySeq.empty +) + extends SigmaException(message, cause, args) + +/** Thrown by TypeSerializer when type prefix <= 0. */ +final class InvalidTypePrefix(message: String, cause: Option[Throwable] = None) + extends SerializerException(message, cause) + +/** Thrown when the current reader position > positionLimit which is set in the Reader. + * @see [[org.ergoplatform.validation.ValidationRules.CheckPositionLimit]] + */ +final class ReaderPositionLimitExceeded( + message: String, + val position: Int, + val positionLimit: Int, + cause: Option[Throwable] = None) + extends SerializerException(message, cause) + +/** Thrown when the current depth level > maxDepthLevel which is set in the Reader. */ +final class DeserializeCallDepthExceeded(message: String, cause: Option[Throwable] = None) + extends SerializerException(message, cause) + +/** Thrown by [[org.ergoplatform.validation.ValidationRules.CheckValidOpCode]] validation rule. */ +final class InvalidOpCode(message: String, cause: Option[Throwable] = None) + extends SerializerException(message, cause) diff --git a/core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala b/core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala new file mode 100644 index 0000000000..ef682a8df5 --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala @@ -0,0 +1,240 @@ +package sigma.serialization + +import scorex.util.serialization.{VLQByteBufferWriter, Writer} +import scorex.util.serialization.Writer.Aux +import sigma.ast.SType +import sigma.serialization.SigmaByteWriter.{Bits, DataInfo, U, Vlq, ZigZag} +import sigmastate.serialization.CoreTypeSerializer + +class CoreByteWriter(val w: Writer) extends Writer { + type CH = w.CH + + @inline override def length(): Int = w.length() + + @inline override def newWriter(): Aux[CH] = w.newWriter() + + @inline override def putChunk(chunk: CH): this.type = { w.putChunk(chunk); this } + + @inline override def result(): CH = w.result() + + @inline def put(x: Byte): this.type = { w.put(x); this } + @inline def put(x: Byte, info: DataInfo[Byte]): this.type = { + w.put(x); this + } + + override def putUByte(x: Int): this.type = { + super.putUByte(x) + } + def putUByte(x: Int, info: DataInfo[U[Byte]]): this.type = { + super.putUByte(x) + } + + @inline def putBoolean(x: Boolean): this.type = { w.putBoolean(x); this } + @inline def putBoolean(x: Boolean, info: DataInfo[Boolean]): this.type = { + w.putBoolean(x); this + } + + @inline def putShort(x: Short): this.type = { w.putShort(x); this } + @inline def putShort(x: Short, info: DataInfo[Short]): this.type = { + w.putShort(x); this + } + + @inline def putUShort(x: Int): this.type = { w.putUShort(x); this } + @inline def putUShort(x: Int, info: DataInfo[Vlq[U[Short]]]): this.type = { + w.putUShort(x); this + } + + @inline def putInt(x: Int): this.type = { w.putInt(x); this } + @inline def putInt(x: Int, info: DataInfo[Int]): this.type = { + w.putInt(x); this + } + + @inline def putUInt(x: Long): this.type = { w.putUInt(x); this } + @inline def putUInt(x: Long, info: DataInfo[Vlq[U[Int]]]): this.type = { + w.putUInt(x); this + } + + @inline def putLong(x: Long): this.type = { w.putLong(x); this } + @inline def putLong(x: Long, info: DataInfo[Vlq[ZigZag[Long]]]): this.type = { + w.putLong(x); this + } + + @inline def putULong(x: Long): this.type = { w.putULong(x); this } + @inline def putULong(x: Long, info: DataInfo[Vlq[U[Long]]]): this.type = { + w.putULong(x); this + } + + override def putBytes(xs: Array[Byte], + offset: Int, + length: Int): this.type = { + w.putBytes(xs, offset, length); this + } + @inline def putBytes(xs: Array[Byte]): this.type = { w.putBytes(xs); this } + @inline def putBytes(xs: Array[Byte], info: DataInfo[Array[Byte]]): this.type = { + w.putBytes(xs); this + } + + /** Put the two bytes of the big-endian representation of the Short value into the + * writer. */ + @inline def putShortBytes(value: Short): this.type = { + w.put((value >> 8).toByte) + w.put(value.toByte) + this + } + + @inline def putBits(xs: Array[Boolean]): this.type = { w.putBits(xs); this } + @inline def putBits(xs: Array[Boolean], info: DataInfo[Bits]): this.type = { + w.putBits(xs); + this + } + + @inline def putOption[T](x: Option[T])(putValueC: (this.type, T) => Unit): this.type = { + w.putOption(x) { (_, v) => + putValueC(this, v) + } + this + } + + @inline def putShortString(s: String): this.type = { w.putShortString(s); this } + + // TODO refactor: move to Writer + @inline def toBytes: Array[Byte] = w match { + case wr: VLQByteBufferWriter => wr.toBytes + } + + @inline def putType[T <: SType](x: T): this.type = { CoreTypeSerializer.serialize(x, this); this } + @inline def putType[T <: SType](x: T, info: DataInfo[SType]): this.type = { + CoreTypeSerializer.serialize(x, this); this + } + +} + +object SigmaByteWriter { + import scala.language.implicitConversions + + /** Format descriptor type family. */ + trait FormatDescriptor[T] { + /** Size formula associated with this format */ + def size: String + } + + /** Marker type to automatically resolve correct implicit format descriptor + * in Writer methods. + * This is type-level type, since no instances of it are ever created. */ + trait Vlq[T] + + /** Marker type to automatically resolve correct implicit format descriptor + * in Writer methods. + * This is type-level type, since no instances of it are ever created. */ + trait ZigZag[T] + + /** Marker type for Unsigned types to automatically resolve correct implicit format descriptor + * in Writer methods. + * This is type-level type, since no instances of it are ever created. */ + trait U[T] + + /** Marker type for bits representation of Coll[Boolean]. + * Should be used only as argument for FormatDescriptor. + * This is type-level type, since no instances of it are ever created. + */ + trait Bits + + implicit case object TypeFmt extends FormatDescriptor[SType] { + override def size: String = "[1, *]" + override def toString: String = "Type" + } + + case object BitsFmt extends FormatDescriptor[Bits] { + override def size: String = "[1, *]" + override def toString: String = "Bits" + } + + case class MaxBitsFmt(maxBits: Int) extends FormatDescriptor[Bits] { + override def size: String = { + val maxBytes = (maxBits - 1) / 8 + 1 + if (maxBytes == 1) "1" + else s"[1, $maxBytes]" + } + override def toString: String = "Bits" + } + + implicit object BooleanFmt extends FormatDescriptor[Boolean] { + override def size: String = "1" + override def toString: String = "Boolean" + } + implicit object ByteFmt extends FormatDescriptor[Byte] { + override def size: String = "1" + override def toString: String = "Byte" + } + implicit object ShortFmt extends FormatDescriptor[Short] { + override def size: String = "2" + override def toString: String = "Short" + } + implicit object IntFmt extends FormatDescriptor[Int] { + override def size: String = "4" + override def toString: String = "Int" + } + implicit object LongFmt extends FormatDescriptor[Long] { + override def size: String = "8" + override def toString: String = "Long" + } + + implicit object UByteFmt extends FormatDescriptor[U[Byte]] { + override def size: String = "1" + override def toString: String = "UByte" + } + implicit object UShortFmt extends FormatDescriptor[U[Short]] { + override def size: String = "2" + override def toString: String = "UShort" + } + implicit object UIntFmt extends FormatDescriptor[U[Int]] { + override def size: String = "4" + override def toString: String = "UInt" + } + implicit object ULongFmt extends FormatDescriptor[U[Long]] { + override def size: String = "8" + override def toString: String = "ULong" + } + + case class ZigZagFmt[T](fmt: FormatDescriptor[T]) extends FormatDescriptor[ZigZag[T]] { + override def size: String = s"[1, *]" + override def toString: String = s"ZigZag($fmt)" + } + case class UVlqFmt[T](fmt: FormatDescriptor[U[T]]) extends FormatDescriptor[Vlq[U[T]]] { + override def size: String = s"[1, *]" + override def toString: String = s"VLQ($fmt)" + } + case class ZigZagVlqFmt[T](fmt: FormatDescriptor[ZigZag[T]]) extends FormatDescriptor[Vlq[ZigZag[T]]] { + override def size: String = s"[1, *]" + override def toString: String = s"VLQ($fmt)" + } + case class SeqFmt[T](fmt: FormatDescriptor[T]) extends FormatDescriptor[Seq[T]] { + override def size: String = s"n * ${fmt.size}" + override def toString: String = s"Seq($fmt)" + } + + implicit def toZigZagFmt[T](implicit fmt: FormatDescriptor[T]): FormatDescriptor[ZigZag[T]] = ZigZagFmt(fmt) + implicit def toUVlqFmt[T](implicit fmt: FormatDescriptor[U[T]]): FormatDescriptor[Vlq[U[T]]] = UVlqFmt(fmt) + implicit def toZigZagVlqFmt[T](implicit fmt: FormatDescriptor[ZigZag[T]]): FormatDescriptor[Vlq[ZigZag[T]]] = ZigZagVlqFmt(fmt) + implicit def toSeqFmt[T](implicit fmt: FormatDescriptor[T]): FormatDescriptor[Seq[T]] = SeqFmt(fmt) + + /** Meta information which can be attached to each argument of SMethod. + * + * @param name name of the argument + * @param description argument description. */ + case class ArgInfo(name: String, description: String) + + case class DataInfo[T](info: ArgInfo, format: FormatDescriptor[T]) + + def bitsInfo(name: String, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), BitsFmt) + def maxBitsInfo(name: String, maxBits: Int, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), MaxBitsFmt(maxBits)) + + val valuesLengthInfo: DataInfo[Vlq[U[Int]]] = ArgInfo("\\#items", "number of items in the collection") + + + implicit def argInfoToDataInfo[T](arg: ArgInfo)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = DataInfo(arg, fmt) + + // TODO refactor: remove this conversion and make it explicit + /**Helper conversion */ + implicit def nameToDataInfo[T](name: String)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = ArgInfo(name, "") +} diff --git a/core/shared/src/main/scala/sigma/validation/RuleStatus.scala b/core/shared/src/main/scala/sigma/validation/RuleStatus.scala new file mode 100644 index 0000000000..3d0451b240 --- /dev/null +++ b/core/shared/src/main/scala/sigma/validation/RuleStatus.scala @@ -0,0 +1,53 @@ +package sigma.validation + +/** Base trait for rule status information. */ +sealed trait RuleStatus { + def statusCode: Byte +} +object RuleStatus { + val EnabledRuleCode: Byte = 1.toByte + val DisabledRuleCode: Byte = 2.toByte + val ReplacedRuleCode: Byte = 3.toByte + val ChangedRuleCode: Byte = 4.toByte +} + +/** This is a default status of a rule which is registered in the table + * and not yet altered by soft-forks. + */ +case object EnabledRule extends RuleStatus { + val statusCode: Byte = RuleStatus.EnabledRuleCode +} + +/** This is a status of a rule which is disabled in current version + * and not yet altered by soft-forks. + * The rule can be disabled via block extensions and voting process. + */ +case object DisabledRule extends RuleStatus { + val statusCode: Byte = RuleStatus.DisabledRuleCode +} + +/** The status of the rule which is replaced by a new rule via soft-fork extensions. + * This is similar to DisabledRule, but in addition require the new rule to be enabled + * at the same time (i.e. atomically) + * @see `ValidationSettings.isSoftFork` + * @param newRuleId id of a new rule which replaces the rule marked with this status + */ +case class ReplacedRule(newRuleId: Short) extends RuleStatus { + val statusCode: Byte = RuleStatus.ReplacedRuleCode +} + +/** The status of the rule whose parameters are changed via soft-fork extensions. + * The same rule can be changed many times via voting. + * @param newValue new value of block extension value with key == rule.id + */ +case class ChangedRule(newValue: Array[Byte]) extends RuleStatus { + val statusCode: Byte = RuleStatus.ChangedRuleCode + + override def hashCode(): Int = java.util.Arrays.hashCode(newValue) + + override def canEqual(that: Any): Boolean = that.isInstanceOf[ChangedRule] + + override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match { + case that: ChangedRule => java.util.Arrays.equals(newValue, that.newValue) + }) +} diff --git a/core/shared/src/main/scala/sigma/validation/SigmaValidationSettings.scala b/core/shared/src/main/scala/sigma/validation/SigmaValidationSettings.scala new file mode 100644 index 0000000000..8e8ee00f14 --- /dev/null +++ b/core/shared/src/main/scala/sigma/validation/SigmaValidationSettings.scala @@ -0,0 +1,83 @@ +package sigma.validation + +/** + * Configuration of validation. Each `ValidationRule` instance should be + * implemented as an `object` to facilitate type-safe usage. It then should be + * registered in `ValidationRules.currentSettings` to be used in the code to + * perform validation. Added to `currentSettings` the rule has EnabledRule + * status by default, but only in a current version of the code. Thus, the + * value `currentSettings` represents the validation settings of the current + * version of the code. The set of rules in `currentSettings` is fixed in the + * current version of the code and thus only rule status can be changed (as + * described below) + * + * Old versions of the code don't have access to the rules added in newer + * versions. The implementation of the specific rule, once released under + * specific ruleId, should never be changed, hence ruleId denotes that + * implementation. However, the behavior of rules (released with code) can be + * altered by changing their status in block extensions section via voting. + * + * The status changes are represented in ValidationSettings using the + * RuleStatus type. Each descendant class represent a particular change in the + * rule status. Rule ids are used as keys of the status values stored in the + * block extension section. RuleStatus instances are deserialized from the + * block extension values. Deserialized (ruleId, status) pairs are joined with + * the (ruleId,status) pairs in `currentSettings`, and for matching ruleIds the + * default statues stored in `currentSettings` are replaced with the new + * statuses obtained from the blockchain. Deserialized (ruleId,status) pairs + * which don't match with `currentSettings` are ignored. + * + * Each rule has associated check of soft-fork condition by implementing + * `isSoftFork` method. If `isSoftFork` returns true, then ValidationException + * raised by the rule is interpreted as *soft-fork condition*. Depending on the + * use case, soft-fork condition allows some operations performed by an old + * code to succeed which otherwise would fail due to ValidationException raised + * by the validation rule. One notable use case is Box.ergoTree validation in + * which old code can skip ValidationExceptions under soft-fork condition (i.e. + * when isSoftFork returns true), for example when a new opCode is added in the + * newer version of the protocol, and this fact can be recognized by the old + * code. + * + * @see SoftForkWhenCodeAdded + */ +abstract class SigmaValidationSettings extends Iterable[(Short, (ValidationRule, RuleStatus))] { + def get(id: Short): Option[(ValidationRule, RuleStatus)] + def getStatus(id: Short): Option[RuleStatus] + def updated(id: Short, newStatus: RuleStatus): SigmaValidationSettings + def isSoftFork(ve: ValidationException): Boolean = isSoftFork(ve.rule.id, ve) + def isSoftFork(ruleId: Short, ve: ValidationException): Boolean = { + val infoOpt = get(ruleId) + infoOpt match { + case Some((_, ReplacedRule(_))) => true + case Some((rule, status)) => rule.isSoftFork(this, rule.id, status, ve.args) + case None => false + } + } +} + +/** Default representation of validation settings. */ +sealed class MapSigmaValidationSettings(private val map: Map[Short, (ValidationRule, RuleStatus)]) extends SigmaValidationSettings { + override def iterator: Iterator[(Short, (ValidationRule, RuleStatus))] = map.iterator + override def get(id: Short): Option[(ValidationRule, RuleStatus)] = map.get(id) + + /** HOTSPOT: don't beautify this code */ + override def getStatus(id: Short): Option[RuleStatus] = { + val statusOpt = map.get(id) + val res = if (statusOpt.isDefined) Some(statusOpt.get._2) else None + res + } + override def updated(id: Short, newStatus: RuleStatus): MapSigmaValidationSettings = { + val (rule,_) = map(id) + new MapSigmaValidationSettings(map.updated(id, (rule, newStatus))) + } + + override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match { + case that: MapSigmaValidationSettings => + map.size == that.map.size && + map.iterator.forall { case (id, v) => that.map.get(id).exists(_ == v) } + case _ => false + }) + + override def hashCode(): Int = map.toIndexedSeq.sortBy(_._1).hashCode() +} + diff --git a/core/shared/src/main/scala/sigma/validation/SoftForkChecker.scala b/core/shared/src/main/scala/sigma/validation/SoftForkChecker.scala new file mode 100644 index 0000000000..e26926ae9b --- /dev/null +++ b/core/shared/src/main/scala/sigma/validation/SoftForkChecker.scala @@ -0,0 +1,42 @@ +package sigma.validation + +/** Interface implemented by objects capable of checking soft-fork conditions. */ +trait SoftForkChecker { + /** Check soft-fork condition. + * @param vs ValidationSettings actualized from blockchain extension sections + * @param ruleId id of the rule which raised ValidationException + * @param status status of the rule in the blockchain (agreed upon via voting) + * @param args arguments of Validation rule with which the rule has risen the exception + * @return true if `args` and `status` can be interpreted as valid soft-fork condition. + */ + def isSoftFork(vs: SigmaValidationSettings, ruleId: Short, status: RuleStatus, args: Seq[Any]): Boolean = false +} + +/** Checks that the failed validation rule has ReplacedRule status in block extensions section. + * This means the rule given by `ruleId` is not used in newer versions of the protocol. + * Instead it has been replaced by the new rule given by ReplacedRule status. + */ +trait SoftForkWhenReplaced extends SoftForkChecker { + override def isSoftFork(vs: SigmaValidationSettings, + ruleId: Short, + status: RuleStatus, + args: Seq[Any]): Boolean = (status, args) match { + case (ReplacedRule(_), _) => true + case _ => false + } +} + +/** Checks that the unknown `code` is however present in the ChangedRule new value + * stored in block extensions section. This is interpreted as soft-fork condition, + * i.e. the unknown `code` is not arbitrary, but explicitly added to the blockchain + * configuration and implemented in newer versions of the protocol. + */ +trait SoftForkWhenCodeAdded extends SoftForkChecker { + override def isSoftFork(vs: SigmaValidationSettings, + ruleId: Short, + status: RuleStatus, + args: Seq[Any]): Boolean = (status, args) match { + case (ChangedRule(newValue), Seq(code: Byte)) => newValue.contains(code) + case _ => false + } +} diff --git a/core/shared/src/main/scala/sigma/validation/ValidationRules.scala b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala new file mode 100644 index 0000000000..239b18509d --- /dev/null +++ b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala @@ -0,0 +1,215 @@ +package sigma.validation + +import sigma.SigmaException +import sigma.ast.{SGlobal, SOption, TypeCodes} +import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} +import sigma.util.Extensions.toUByte +import sigmastate.serialization.CoreTypeSerializer.embeddableIdToType + +/** Base class for different validation rules registered in ValidationRules.currentSettings. + * Each rule is identified by `id` and have a description. + * Validation logic is implemented by `apply` methods of derived classes. + */ +case class ValidationRule( + id: Short, + description: String +) extends SoftForkChecker { + + /** Whether the status of this rule was checked on first invocation. */ + private var _checked: Boolean = false + + /** Check the rule is registered and enabled. + * Since it is easy to forget to register new rule, we need to do this check. + * But because it is hotspot, we do this check only once for each rule. + * HOTSPOT: executed on every typeCode and opCode during script deserialization + */ + @inline protected final def checkRule(): Unit = { + if (!_checked) { + if (ValidationRules.currentSettings.getStatus(this.id).isEmpty) { + throw new SigmaException(s"ValidationRule $this not found in validation settings") + } + _checked = true // prevent this check on every call (only first call is checked) + } + // upon successful return we know the rule is registered with EnabledRule status + } + + /** Throws ValidationException with the given cause and args. + * Should be used in all validation rules to unify ValidationException instances + * which can be thrown (to simplify handling). + */ + def throwValidationException(cause: Throwable, args: Seq[Any]): Nothing = { + if (cause.isInstanceOf[ValidationException]) { + throw cause + } + else { + throw ValidationException(s"Validation failed on $this with args $args", this, args, Option(cause)) + } + } + +} + +/** Base class for all exceptions which may be thrown by validation rules. + * Instances of this class are used as messages to communicate soft-fork information, + * from the context where the soft-fork condition is detected (such as in ValidationRules), + * up the stack to the point where it is clear how to handle it. + * Some messages of this kind are not handled, in which case a new Exception is thrown + * and this instance should be attached as a `cause` parameter. + * + * This exception should typically always come with Some(cause). As result, stack trace is not + * filled in when this instance is create. The `cause` parameter should be examined for the source + * of the exception. + */ +case class ValidationException(message: String, rule: ValidationRule, args: Seq[Any], cause: Option[Throwable] = None) + extends Exception(message, cause.orNull) { + override def fillInStackTrace(): Throwable = this // to avoid spending time on recording stack trace +} + +/** All validation rules which are used to check soft-forkable conditions. Each validation + * rule throws a [[org.ergoplatform.validation.ValidationException]]. Each + * ValidationException can be caught and handled with respect to + * [[SigmaValidationSettings]], which can be changed by miners via voting. + * Thus, the behavior of the rules can be overridden without breaking consensus. + */ +object ValidationRules { + /** The id of the first validation rule. Can be used as the beginning of the rules id range. */ + val FirstRuleId = 1000.toShort + + object CheckPrimitiveTypeCode extends ValidationRule(1007, + "Check the primitive type code is supported or is added via soft-fork") + with SoftForkWhenCodeAdded { + final def apply[T](code: Byte): Unit = { + checkRule() + val ucode = toUByte(code) + if (ucode <= 0 || ucode >= embeddableIdToType.length) { + throwValidationException( + new SerializerException(s"Cannot deserialize primitive type with code $ucode"), + Array(code)) + } + } + } + + object CheckTypeCode extends ValidationRule(1008, + "Check the non-primitive type code is supported or is added via soft-fork") + with SoftForkWhenCodeAdded { + final def apply[T](typeCode: Byte): Unit = { + checkRule() + val ucode = toUByte(typeCode) + if (ucode > toUByte(SGlobal.typeCode)) { + throwValidationException( + new SerializerException(s"Cannot deserialize the non-primitive type with code $ucode"), + Array(typeCode)) + } + } + } + + object CheckSerializableTypeCode extends ValidationRule(1009, + "Check the data values of the type (given by type code) can be serialized") + with SoftForkWhenReplaced { + + /** Creates an exception which is used as a cause when throwing a ValidationException. */ + def throwValidationException(typeCode: Byte): Nothing = { + val ex = new SerializerException( + s"Data value of the type with the code ${toUByte(typeCode)} cannot be deserialized.") + throwValidationException(ex, Array(typeCode)) + } + + final def apply[T](typeCode: Byte): Unit = { + checkRule() + val ucode = toUByte(typeCode) + if (typeCode == SOption.OptionTypeCode || ucode > toUByte(TypeCodes.LastDataType)) { + // the `typeCode == SOption.OptionTypeCode` condition is added in v5.0 and we + // throw ValidationException for Option type as well in order to be able to + // interpret it as soft-fork condition. + // This will allow to add Option serialization in DataSerializer via v6.0 soft-fork. + // This is in contrast to v4.x of this rule where Option type is not checked and + // ordinary SigmaSerializer exception is thrown by the fallback case of DataSerializer. + // This change is consensus-safe as v4.x and v5.x nodes will both throw exceptions + // (albeit different ones) while attempting deserialization of an Option value. + throwValidationException(typeCode) + } + } + } + + object CheckTypeWithMethods extends ValidationRule(1010, + "Check the type (given by type code) supports methods") + with SoftForkWhenCodeAdded { + final def apply[T](typeCode: Byte, cond: Boolean): Unit = { + checkRule() + val ucode = toUByte(typeCode) + if (!cond) { + throwValidationException( + new SerializerException(s"Type with code $ucode doesn't support methods."), + Array(typeCode)) + } + } + } + + /** Introduced in v5.0, this rule it is used in creation of ValidationExceptions, which + * in turn can be checked for soft-fork condition using `this.isSoftFork`. Thus, this + * rule can be replaced with a new rule and the limit can be increased. + */ + object CheckPositionLimit extends ValidationRule(1014, + "Check that the Reader has not exceeded the position limit.") with SoftForkWhenReplaced { + + /** Wraps the given cause into [[ValidationException]] and throws it. */ + def throwValidationException(cause: ReaderPositionLimitExceeded): Nothing = { + throwValidationException(cause, args = Nil) + } + + /** Throws a [[ValidationException]] with the given parameters. */ + def throwValidationException(position: Int, positionLimit: Int): Nothing = { + throwValidationException( + new ReaderPositionLimitExceeded( + s"SigmaByteReader position limit $positionLimit is reached at position $position", + position, positionLimit)) + } + + final def apply(position: Int, positionLimit: Int): Unit = { + checkRule() + if (position > positionLimit) { + throwValidationException(position, positionLimit) + } + } + } + + val ruleSpecs: Seq[ValidationRule] = Seq( + CheckPrimitiveTypeCode, + CheckTypeCode, + CheckSerializableTypeCode, + CheckTypeWithMethods, + CheckPositionLimit, + ) + + /** Validation settings that correspond to the current version of the ErgoScript implementation. + * Different version of the code will have a different set of rules here. + * This variable is globally available and can be use wherever checking of the rules is necessary. + * This is immutable data structure, it can be augmented with RuleStates from block extension + * sections of the blockchain, but that augmentation is only available in stateful context. + */ + val currentSettings: SigmaValidationSettings = new MapSigmaValidationSettings({ + val map = ruleSpecs.map(r => r.id -> (r, EnabledRule)).toMap + assert(map.size == ruleSpecs.size, s"Duplicate ruleIds ${ruleSpecs.groupBy(_.id).filter(g => g._2.length > 1)}") + map + }) + + /** Executes the given `block` catching [[ValidationException]] and checking possible + * soft-fork condition in the context of the given [[SigmaValidationSettings]]. + * If soft-fork condition is recognized the `whenSoftFork` is executed and its result + * is returned. + * + * @param whenSoftFork executed when soft-fork condition is detected + * @param block block of code, which may throw [[ValidationException]] + * @param vs set of [[SigmaValidationSettings]] which can be used to recognize soft-fork conditions. + * @return result of `block` if no ValidationException was thrown, or the result of + * `whenSoftFork` if soft-fork condition is detected. + * @throws ValidationException if soft-fork condition is not recognized by the given `vs` + */ + def trySoftForkable[T](whenSoftFork: => T)(block: => T)(implicit vs: SigmaValidationSettings): T = { + try block + catch { + case ve: ValidationException => + if (vs.isSoftFork(ve)) whenSoftFork + else throw ve + } + } +} From 142fcc8dbcbc835336ec8b1a0e15b65f78a8f0a1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sat, 9 Sep 2023 22:34:39 +0200 Subject: [PATCH 02/34] sigma-ast: TypeSerializer code moved to CoreTypeSerializer (fix compilation of interpreter) --- .../sigma/serialization/CoreByteReader.scala | 18 +- ...aByteWriter.scala => CoreByteWriter.scala} | 26 ++- .../serialization/CoreTypeSerializer.scala | 220 ------------------ .../sigma}/serialization/TypeSerializer.scala | 36 +-- .../sigma/validation/ValidationRules.scala | 23 +- .../scala/org/ergoplatform/ErgoAddress.scala | 3 +- .../org/ergoplatform/ErgoLikeContext.scala | 4 +- .../ergoplatform/validation/RuleStatus.scala | 55 ----- .../validation/RuleStatusSerializer.scala | 1 + .../validation/SigmaValidationSettings.scala | 83 ------- .../SigmaValidationSettingsSerializer.scala | 3 +- .../validation/SoftForkChecker.scala | 42 ---- .../validation/ValidationRules.scala | 216 +++-------------- .../main/scala/sigmastate/Operations.scala | 1 + .../src/main/scala/sigmastate/SMethod.scala | 10 +- .../main/scala/sigmastate/SigSerializer.scala | 2 +- .../src/main/scala/sigmastate/Values.scala | 2 +- .../sigmastate/eval/CostingDataContext.scala | 19 +- .../exceptions/CompilerExceptions.scala | 1 + .../exceptions/SigmaExceptions.scala | 19 +- .../SigmaSerializerExceptions.scala | 23 -- .../sigmastate/interpreter/Interpreter.scala | 3 +- .../interpreter/InterpreterContext.scala | 2 +- .../scala/sigmastate/lang/SigmaPredef.scala | 1 + .../serialization/ApplySerializer.scala | 4 +- .../serialization/BlockValueSerializer.scala | 3 +- .../BoolToSigmaPropSerializer.scala | 5 +- ...eCollectionBooleanConstantSerializer.scala | 3 +- .../ConcreteCollectionSerializer.scala | 4 +- .../CreateAvlTreeSerializer.scala | 3 +- .../serialization/DataSerializer.scala | 4 +- .../serialization/ErgoTreeSerializer.scala | 7 +- .../serialization/FuncValueSerializer.scala | 3 +- .../serialization/GetVarSerializer.scala | 3 +- .../serialization/LogicalNotSerializer.scala | 3 +- .../serialization/MethodCallSerializer.scala | 3 +- .../serialization/ModQArithOpSerializer.scala | 7 +- .../serialization/ModQSerializer.scala | 3 +- .../OneArgumentOperationSerializer.scala | 5 +- .../OptionGetOrElseSerializer.scala | 3 +- .../PropertyCallSerializer.scala | 3 +- .../serialization/ProveDlogSerializer.scala | 5 +- .../serialization/SelectFieldSerializer.scala | 3 +- .../SigmaPropBytesSerializer.scala | 3 +- .../serialization/SigmaSerializer.scala | 4 +- .../SubstConstantsSerializer.scala | 7 +- .../serialization/TupleSerializer.scala | 6 +- .../TwoArgumentsSerializer.scala | 7 +- .../serialization/ValueSerializer.scala | 3 +- .../transformers/AppendSerializer.scala | 1 + .../transformers/AtLeastSerializer.scala | 1 + .../BooleanTransformerSerializer.scala | 7 +- .../transformers/ByIndexSerializer.scala | 7 +- .../DeserializeContextSerializer.scala | 3 +- .../DeserializeRegisterSerializer.scala | 6 +- .../ExtractRegisterAsSerializer.scala | 6 +- .../transformers/FoldSerializer.scala | 7 +- .../LogicalTransformerSerializer.scala | 5 +- .../MapCollectionSerializer.scala | 7 +- .../transformers/NumericCastSerializer.scala | 3 +- .../transformers/ProveDHTupleSerializer.scala | 1 + .../SigmaTransformerSerializer.scala | 7 +- .../SimpleTransformerSerializer.scala | 3 +- .../transformers/SliceSerializer.scala | 7 +- .../trees/QuadrupleSerializer.scala | 3 +- .../trees/Relation2Serializer.scala | 1 + .../src/main/scala/sigmastate/trees.scala | 3 +- .../src/main/scala/sigmastate/types.scala | 1 + .../sigmastate/utils/SigmaByteReader.scala | 148 +----------- .../sigmastate/utils/SigmaByteWriter.scala | 210 ++--------------- .../scala/sigmastate/utxo/transformers.scala | 1 + .../validation/ValidationSpecification.scala | 2 + .../helpers/ErgoLikeContextTesting.scala | 3 +- .../sigmastate/helpers/TestingHelpers.scala | 2 +- .../ConstantSerializerSpecification.scala | 2 +- .../DataSerializerSpecification.scala | 2 +- .../generators/ObjectGenerators.scala | 2 + .../ergoplatform/sdk/DataJsonEncoder.scala | 1 + 78 files changed, 257 insertions(+), 1112 deletions(-) rename core/shared/src/main/scala/sigma/serialization/{SigmaByteWriter.scala => CoreByteWriter.scala} (92%) delete mode 100644 core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma}/serialization/TypeSerializer.scala (92%) delete mode 100644 interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatus.scala delete mode 100644 interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettings.scala delete mode 100644 interpreter/shared/src/main/scala/org/ergoplatform/validation/SoftForkChecker.scala delete mode 100644 interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaSerializerExceptions.scala diff --git a/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala b/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala index b8b66c717a..62c39749e1 100644 --- a/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala +++ b/core/shared/src/main/scala/sigma/serialization/CoreByteReader.scala @@ -6,7 +6,7 @@ import sigma.util.safeNewArray import debox.cfor import sigma.ast.SType import sigma.validation.ValidationRules.CheckPositionLimit -import sigmastate.serialization.CoreTypeSerializer +import sigmastate.serialization.TypeSerializer /** Reader used in the concrete implementations of [[SigmaSerializer]]. * It decorates the given reader, delegates most of the methods to it, but also adds new @@ -20,8 +20,7 @@ import sigmastate.serialization.CoreTypeSerializer * @param maxTreeDepth limit on the tree depth (recursive invocations) * of the deserializer */ -class CoreByteReader(val r: Reader, - val maxTreeDepth: Int = CoreSerializer.MaxTreeDepth) +class CoreByteReader(val r: Reader, val maxTreeDepth: Int = CoreSerializer.MaxTreeDepth) extends Reader { /** Checks that the current reader position is <= positionLimit. @@ -30,7 +29,7 @@ class CoreByteReader(val r: Reader, * other than that both v4.x and v5.x will work and fail at the same time, so the * change is consensus-safe. */ - @inline private def checkPositionLimit(): Unit = { + @inline protected def checkPositionLimit(): Unit = { CheckPositionLimit(position, positionLimit) } @@ -105,15 +104,6 @@ class CoreByteReader(val r: Reader, r.getBytes(bytesToRead) } - /** Returns all bytes of the underlying ByteBuffer. */ - private[sigma] def getAllBufferBytes: Array[Byte] = { - val savedPos = position - position = 0 - val res = getBytesUnsafe(remaining) - position = savedPos - res - } - @inline override def getBits(size: Int): Array[Boolean] = { checkPositionLimit() r.getBits(size) @@ -137,7 +127,7 @@ class CoreByteReader(val r: Reader, this } - @inline def getType(): SType = CoreTypeSerializer.deserialize(this) + @inline def getType(): SType = TypeSerializer.deserialize(this) private var lvl: Int = 0 @inline def level: Int = lvl diff --git a/core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala b/core/shared/src/main/scala/sigma/serialization/CoreByteWriter.scala similarity index 92% rename from core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala rename to core/shared/src/main/scala/sigma/serialization/CoreByteWriter.scala index ef682a8df5..d9ab716e19 100644 --- a/core/shared/src/main/scala/sigma/serialization/SigmaByteWriter.scala +++ b/core/shared/src/main/scala/sigma/serialization/CoreByteWriter.scala @@ -3,8 +3,8 @@ package sigma.serialization import scorex.util.serialization.{VLQByteBufferWriter, Writer} import scorex.util.serialization.Writer.Aux import sigma.ast.SType -import sigma.serialization.SigmaByteWriter.{Bits, DataInfo, U, Vlq, ZigZag} -import sigmastate.serialization.CoreTypeSerializer +import sigma.serialization.CoreByteWriter.{Bits, DataInfo, U, Vlq, ZigZag} +import sigmastate.serialization.TypeSerializer class CoreByteWriter(val w: Writer) extends Writer { type CH = w.CH @@ -102,14 +102,14 @@ class CoreByteWriter(val w: Writer) extends Writer { case wr: VLQByteBufferWriter => wr.toBytes } - @inline def putType[T <: SType](x: T): this.type = { CoreTypeSerializer.serialize(x, this); this } + @inline def putType[T <: SType](x: T): this.type = { TypeSerializer.serialize(x, this); this } @inline def putType[T <: SType](x: T, info: DataInfo[SType]): this.type = { - CoreTypeSerializer.serialize(x, this); this + TypeSerializer.serialize(x, this); this } } -object SigmaByteWriter { +object CoreByteWriter { import scala.language.implicitConversions /** Format descriptor type family. */ @@ -226,15 +226,17 @@ object SigmaByteWriter { case class DataInfo[T](info: ArgInfo, format: FormatDescriptor[T]) + object DataInfo { + implicit def argnfoToDataInfo[T](arg: ArgInfo)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = DataInfo(arg, fmt) + + // TODO refactor: remove this conversion and make it explicit + /** Helper conversion */ + implicit def nameToDataInfo[T](name: String) + (implicit fmt: FormatDescriptor[T]): DataInfo[T] = ArgInfo(name, "") + } + def bitsInfo(name: String, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), BitsFmt) def maxBitsInfo(name: String, maxBits: Int, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), MaxBitsFmt(maxBits)) val valuesLengthInfo: DataInfo[Vlq[U[Int]]] = ArgInfo("\\#items", "number of items in the collection") - - - implicit def argInfoToDataInfo[T](arg: ArgInfo)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = DataInfo(arg, fmt) - - // TODO refactor: remove this conversion and make it explicit - /**Helper conversion */ - implicit def nameToDataInfo[T](name: String)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = ArgInfo(name, "") } diff --git a/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala b/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala deleted file mode 100644 index 4177723768..0000000000 --- a/core/shared/src/main/scala/sigma/serialization/CoreTypeSerializer.scala +++ /dev/null @@ -1,220 +0,0 @@ -package sigmastate.serialization - -import debox.cfor -import sigma.ast.SCollectionType.{CollectionTypeCode, NestedCollectionTypeCode} -import sigma.ast._ -import sigma.serialization.{CoreByteReader, CoreByteWriter, InvalidTypePrefix} -import sigma.util.safeNewArray -import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckTypeCode} - -import java.nio.charset.StandardCharsets - -/** Serialization of types according to specification in TypeSerialization.md. */ -class CoreTypeSerializer { - import CoreTypeSerializer._ - - def getEmbeddableType(code: Int): SType = { - CheckPrimitiveTypeCode(code.toByte) - embeddableIdToType(code) - } - - def serialize(tpe: SType, w: CoreByteWriter): Unit = tpe match { - case p: SEmbeddable => w.put(p.typeCode) - case SString => w.put(SString.typeCode) - case SAny => w.put(SAny.typeCode) - case SUnit => w.put(SUnit.typeCode) - case SBox => w.put(SBox.typeCode) - case SAvlTree => w.put(SAvlTree.typeCode) - case SContext => w.put(SContext.typeCode) - case SGlobal => w.put(SGlobal.typeCode) - case SHeader => w.put(SHeader.typeCode) - case SPreHeader => w.put(SPreHeader.typeCode) - case c: SCollectionType[a] => c.elemType match { - case p: SEmbeddable => - val code = p.embedIn(CollectionTypeCode) - w.put(code) - case cn: SCollectionType[a] => cn.elemType match { - case p: SEmbeddable => - val code = p.embedIn(NestedCollectionTypeCode) - w.put(code) - case _ => - w.put(CollectionTypeCode) - serialize(cn, w) - } - case t => - w.put(CollectionTypeCode) - serialize(t, w) - } - case o: SOption[a] => o.elemType match { - case p: SEmbeddable => - val code = p.embedIn(SOption.OptionTypeCode) - w.put(code) - case c: SCollectionType[a] => c.elemType match { - case p: SEmbeddable => - val code = p.embedIn(SOption.OptionCollectionTypeCode) - w.put(code) - case _ => - w.put(SOption.OptionTypeCode) - serialize(c, w) - } - case t => - w.put(SOption.OptionTypeCode) - serialize(t, w) - } - case _ @ STuple(Seq(t1, t2)) => (t1, t2) match { - case (p: SEmbeddable, _) => - if (p == t2) { - // Symmetric pair of primitive types (`(Int, Int)`, `(Byte,Byte)`, etc.) - val code = p.embedIn(STuple.PairSymmetricTypeCode) - w.put(code) - } else { - // Pair of types where first is primitive (`(_, Int)`) - val code = p.embedIn(STuple.Pair1TypeCode) - w.put(code) - serialize(t2, w) - } - case (_, p: SEmbeddable) => - // Pair of types where second is primitive (`(Int, _)`) - val code = p.embedIn(STuple.Pair2TypeCode) - w.put(code) - serialize(t1, w) - case _ => - // Pair of non-primitive types (`((Int, Byte), (Boolean,Box))`, etc.) - w.put(STuple.Pair1TypeCode) - serialize(t1, w) - serialize(t2, w) - } - case STuple(items) if items.length < 2 => - sys.error(s"Invalid Tuple type with less than 2 items $items") - case tup: STuple => tup.items.length match { - case 3 => - // Triple of types - w.put(STuple.TripleTypeCode) - for (i <- tup.items) - serialize(i, w) - case 4 => - // Quadruple of types - w.put(STuple.QuadrupleTypeCode) - for (i <- tup.items) - serialize(i, w) - case _ => - // `Tuple` type with more than 4 items `(Int, Byte, Box, Boolean, Int)` - serializeTuple(tup, w) - } - case typeIdent: STypeVar => { - w.put(typeIdent.typeCode) - val bytes = typeIdent.name.getBytes(StandardCharsets.UTF_8) - w.putUByte(bytes.length) - .putBytes(bytes) - } - } - - def deserialize(r: CoreByteReader): SType = deserialize(r, 0) - - private def deserialize(r: CoreByteReader, depth: Int): SType = { - val c = r.getUByte() - if (c <= 0) - throw new InvalidTypePrefix(s"Cannot deserialize type prefix $c. Unexpected buffer $r with bytes ${r.getBytes(r.remaining)}") - val tpe: SType = if (c < STuple.TupleTypeCode) { - val constrId = c / SPrimType.PrimRange - val primId = c % SPrimType.PrimRange - constrId match { - case 0 => // primitive - getEmbeddableType(c) - case 1 => // Array[_] - val tElem = getArgType(r, primId, depth) - SCollection(tElem) - case 2 => // Array[Array[_]] - val tElem = getArgType(r, primId, depth) - SCollection(SCollection(tElem)) - case 3 => // Option[_] - val tElem = getArgType(r, primId, depth) - SOption(tElem) - case 4 => // Option[Collection[_]] - val tElem = getArgType(r, primId, depth) - SOption(SCollection(tElem)) - case STuple.Pair1TypeConstrId => // (_, t2) - val (t1, t2) = if (primId == 0) { - // Pair of non-primitive types (`((Int, Byte), (Boolean,Box))`, etc.) - (deserialize(r, depth + 1), deserialize(r, depth + 1)) - } else { - // Pair of types where first is primitive (`(_, Int)`) - (getEmbeddableType(primId), deserialize(r, depth + 1)) - } - STuple(t1, t2) - case STuple.Pair2TypeConstrId => // (t1, _) - if (primId == 0) { - // Triple of types - val (t1, t2, t3) = (deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1)) - STuple(t1, t2, t3) - } else { - // Pair of types where second is primitive (`(Int, _)`) - val t2 = getEmbeddableType(primId) - val t1 = deserialize(r, depth + 1) - STuple(t1, t2) - } - case STuple.PairSymmetricTypeConstrId => // (_, _) - if (primId == 0) { - // Quadruple of types - val (t1, t2, t3, t4) = (deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1), deserialize(r, depth + 1)) - STuple(t1, t2, t3, t4) - } else { - // Symmetric pair of primitive types (`(Int, Int)`, `(Byte,Byte)`, etc.) - val t = getEmbeddableType(primId) - STuple(t, t) - } - } - } - else { - c match { - case STuple.TupleTypeCode => { - val len = r.getUByte() - val items = safeNewArray[SType](len) - cfor(0)(_ < len, _ + 1) { i => - items(i) = deserialize(r, depth + 1) - } - STuple(items) - } - case SAny.typeCode => SAny - case SUnit.typeCode => SUnit - case SBox.typeCode => SBox - case SAvlTree.typeCode => SAvlTree - case SContext.typeCode => SContext - case SString.typeCode => SString - case STypeVar.TypeCode => { - val nameLength = r.getUByte() - val name = new String(r.getBytes(nameLength), StandardCharsets.UTF_8) - STypeVar(name) - } - case SHeader.typeCode => SHeader - case SPreHeader.typeCode => SPreHeader - case SGlobal.typeCode => SGlobal - case _ => - CheckTypeCode(c.toByte) - NoType - } - } - tpe - } - - private def getArgType(r: CoreByteReader, primId: Int, depth: Int) = - if (primId == 0) - deserialize(r, depth + 1) - else - getEmbeddableType(primId) - - private def serializeTuple(t: STuple, w: CoreByteWriter) = { - assert(t.items.length <= 255) - w.put(STuple.TupleTypeCode) - w.putUByte(t.items.length) - for (i <- t.items) - serialize(i, w) - } -} - -object CoreTypeSerializer extends CoreTypeSerializer { - /** The list of embeddable types, i.e. types that can be combined with type constructor for optimized encoding. - * For each embeddable type `T`, and type constructor `C`, the type `C[T]` can be represented by single byte. */ - val embeddableIdToType = Array[SType](null, SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp) - -} \ No newline at end of file diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/TypeSerializer.scala b/core/shared/src/main/scala/sigma/serialization/TypeSerializer.scala similarity index 92% rename from interpreter/shared/src/main/scala/sigmastate/serialization/TypeSerializer.scala rename to core/shared/src/main/scala/sigma/serialization/TypeSerializer.scala index 265c7e36a3..27b125fa02 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/TypeSerializer.scala +++ b/core/shared/src/main/scala/sigma/serialization/TypeSerializer.scala @@ -1,30 +1,24 @@ package sigmastate.serialization -import java.nio.charset.StandardCharsets -import org.ergoplatform.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckTypeCode} -import sigmastate._ -import sigma.util.safeNewArray -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import debox.cfor import sigma.ast.SCollectionType.{CollectionTypeCode, NestedCollectionTypeCode} import sigma.ast._ -import sigmastate.exceptions.InvalidTypePrefix - -/** Serialization of types according to specification in TypeSerialization.md. */ -object TypeSerializer { - +import sigma.serialization.{CoreByteReader, CoreByteWriter, InvalidTypePrefix} +import sigma.util.safeNewArray +import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckTypeCode} - /** The list of embeddable types, i.e. types that can be combined with type constructor for optimized encoding. - * For each embeddable type `T`, and type constructor `C`, the type `C[T]` can be represented by single byte. */ - val embeddableIdToType = Array[SType](null, SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp) +import java.nio.charset.StandardCharsets +/** Serialization of types according to specification in TypeSerialization.md. */ +class TypeSerializer { + import TypeSerializer._ def getEmbeddableType(code: Int): SType = { CheckPrimitiveTypeCode(code.toByte) embeddableIdToType(code) } - def serialize(tpe: SType, w: SigmaByteWriter): Unit = tpe match { + def serialize(tpe: SType, w: CoreByteWriter): Unit = tpe match { case p: SEmbeddable => w.put(p.typeCode) case SString => w.put(SString.typeCode) case SAny => w.put(SAny.typeCode) @@ -115,9 +109,9 @@ object TypeSerializer { } } - def deserialize(r: SigmaByteReader): SType = deserialize(r, 0) + def deserialize(r: CoreByteReader): SType = deserialize(r, 0) - private def deserialize(r: SigmaByteReader, depth: Int): SType = { + private def deserialize(r: CoreByteReader, depth: Int): SType = { val c = r.getUByte() if (c <= 0) throw new InvalidTypePrefix(s"Cannot deserialize type prefix $c. Unexpected buffer $r with bytes ${r.getBytes(r.remaining)}") @@ -203,13 +197,13 @@ object TypeSerializer { tpe } - private def getArgType(r: SigmaByteReader, primId: Int, depth: Int) = + private def getArgType(r: CoreByteReader, primId: Int, depth: Int) = if (primId == 0) deserialize(r, depth + 1) else getEmbeddableType(primId) - private def serializeTuple(t: STuple, w: SigmaByteWriter) = { + private def serializeTuple(t: STuple, w: CoreByteWriter) = { assert(t.items.length <= 255) w.put(STuple.TupleTypeCode) w.putUByte(t.items.length) @@ -218,3 +212,9 @@ object TypeSerializer { } } +object TypeSerializer extends TypeSerializer { + /** The list of embeddable types, i.e. types that can be combined with type constructor for optimized encoding. + * For each embeddable type `T`, and type constructor `C`, the type `C[T]` can be represented by single byte. */ + val embeddableIdToType = Array[SType](null, SBoolean, SByte, SShort, SInt, SLong, SBigInt, SGroupElement, SSigmaProp) + +} \ No newline at end of file diff --git a/core/shared/src/main/scala/sigma/validation/ValidationRules.scala b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala index 239b18509d..bb68d585a4 100644 --- a/core/shared/src/main/scala/sigma/validation/ValidationRules.scala +++ b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala @@ -4,17 +4,20 @@ import sigma.SigmaException import sigma.ast.{SGlobal, SOption, TypeCodes} import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} import sigma.util.Extensions.toUByte -import sigmastate.serialization.CoreTypeSerializer.embeddableIdToType +import sigmastate.serialization.TypeSerializer.embeddableIdToType /** Base class for different validation rules registered in ValidationRules.currentSettings. * Each rule is identified by `id` and have a description. * Validation logic is implemented by `apply` methods of derived classes. */ -case class ValidationRule( +abstract case class ValidationRule( id: Short, description: String ) extends SoftForkChecker { - + /** Validation settings defining the status of this rule. + * @see checkRule() + */ + protected def settings: SigmaValidationSettings /** Whether the status of this rule was checked on first invocation. */ private var _checked: Boolean = false @@ -25,7 +28,7 @@ case class ValidationRule( */ @inline protected final def checkRule(): Unit = { if (!_checked) { - if (ValidationRules.currentSettings.getStatus(this.id).isEmpty) { + if (settings.getStatus(this.id).isEmpty) { throw new SigmaException(s"ValidationRule $this not found in validation settings") } _checked = true // prevent this check on every call (only first call is checked) @@ -77,6 +80,8 @@ object ValidationRules { object CheckPrimitiveTypeCode extends ValidationRule(1007, "Check the primitive type code is supported or is added via soft-fork") with SoftForkWhenCodeAdded { + override protected lazy val settings: SigmaValidationSettings = coreSettings + final def apply[T](code: Byte): Unit = { checkRule() val ucode = toUByte(code) @@ -91,6 +96,8 @@ object ValidationRules { object CheckTypeCode extends ValidationRule(1008, "Check the non-primitive type code is supported or is added via soft-fork") with SoftForkWhenCodeAdded { + override protected lazy val settings: SigmaValidationSettings = coreSettings + final def apply[T](typeCode: Byte): Unit = { checkRule() val ucode = toUByte(typeCode) @@ -105,6 +112,7 @@ object ValidationRules { object CheckSerializableTypeCode extends ValidationRule(1009, "Check the data values of the type (given by type code) can be serialized") with SoftForkWhenReplaced { + override protected lazy val settings: SigmaValidationSettings = coreSettings /** Creates an exception which is used as a cause when throwing a ValidationException. */ def throwValidationException(typeCode: Byte): Nothing = { @@ -133,6 +141,8 @@ object ValidationRules { object CheckTypeWithMethods extends ValidationRule(1010, "Check the type (given by type code) supports methods") with SoftForkWhenCodeAdded { + override protected lazy val settings: SigmaValidationSettings = coreSettings + final def apply[T](typeCode: Byte, cond: Boolean): Unit = { checkRule() val ucode = toUByte(typeCode) @@ -150,6 +160,7 @@ object ValidationRules { */ object CheckPositionLimit extends ValidationRule(1014, "Check that the Reader has not exceeded the position limit.") with SoftForkWhenReplaced { + override protected lazy val settings: SigmaValidationSettings = coreSettings /** Wraps the given cause into [[ValidationException]] and throws it. */ def throwValidationException(cause: ReaderPositionLimitExceeded): Nothing = { @@ -172,7 +183,7 @@ object ValidationRules { } } - val ruleSpecs: Seq[ValidationRule] = Seq( + private val ruleSpecs: Seq[ValidationRule] = Seq( CheckPrimitiveTypeCode, CheckTypeCode, CheckSerializableTypeCode, @@ -186,7 +197,7 @@ object ValidationRules { * This is immutable data structure, it can be augmented with RuleStates from block extension * sections of the blockchain, but that augmentation is only available in stateful context. */ - val currentSettings: SigmaValidationSettings = new MapSigmaValidationSettings({ + val coreSettings: SigmaValidationSettings = new MapSigmaValidationSettings({ val map = ruleSpecs.map(r => r.id -> (r, EnabledRule)).toMap assert(map.size == ruleSpecs.size, s"Duplicate ruleIds ${ruleSpecs.groupBy(_.id).filter(g => g._2.length > 1)}") map diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala index bd84dfdd4a..13fae39be0 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala @@ -7,10 +7,9 @@ import scorex.util.encode.Base58 import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.DLogProtocol.{ProveDlog, ProveDlogProp} -import sigmastate.exceptions.SigmaException import sigmastate.serialization._ import sigmastate.utxo.{DeserializeContext, Slice} -import sigma.Coll +import sigma.{Coll, SigmaException} import sigma.ast.{SInt, SSigmaProp} import scala.util.Try diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 137c91fded..8bf6ff9ff0 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -1,6 +1,5 @@ package org.ergoplatform -import org.ergoplatform.validation.SigmaValidationSettings import sigmastate.Values._ import sigmastate._ import sigmastate.eval.Extensions._ @@ -13,8 +12,9 @@ import sigma.Coll import sigma.{AnyValue, Header, PreHeader} import debox.cfor import sigma.ast.{SBox, SCollection, SContext, SFunc, SGlobal, SInt, SType, SUnit} -import sigma.ast.SType.{TypeCode, AnyOps} +import sigma.ast.SType.{AnyOps, TypeCode} import sigma.data.SigmaConstants +import sigma.validation.SigmaValidationSettings import sigmastate.serialization.ValueCodes.OpCode /** Represents a script evaluation context to be passed to a prover and a verifier to execute and diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatus.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatus.scala deleted file mode 100644 index 789016abfb..0000000000 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatus.scala +++ /dev/null @@ -1,55 +0,0 @@ -package org.ergoplatform.validation - -import java.util - -/** Base trait for rule status information. */ -sealed trait RuleStatus { - def statusCode: Byte -} -object RuleStatus { - val EnabledRuleCode: Byte = 1.toByte - val DisabledRuleCode: Byte = 2.toByte - val ReplacedRuleCode: Byte = 3.toByte - val ChangedRuleCode: Byte = 4.toByte -} - -/** This is a default status of a rule which is registered in the table - * and not yet altered by soft-forks. - */ -case object EnabledRule extends RuleStatus { - val statusCode: Byte = RuleStatus.EnabledRuleCode -} - -/** This is a status of a rule which is disabled in current version - * and not yet altered by soft-forks. - * The rule can be disabled via block extensions and voting process. - */ -case object DisabledRule extends RuleStatus { - val statusCode: Byte = RuleStatus.DisabledRuleCode -} - -/** The status of the rule which is replaced by a new rule via soft-fork extensions. - * This is similar to DisabledRule, but in addition require the new rule to be enabled - * at the same time (i.e. atomically) - * @see `ValidationSettings.isSoftFork` - * @param newRuleId id of a new rule which replaces the rule marked with this status - */ -case class ReplacedRule(newRuleId: Short) extends RuleStatus { - val statusCode: Byte = RuleStatus.ReplacedRuleCode -} - -/** The status of the rule whose parameters are changed via soft-fork extensions. - * The same rule can be changed many times via voting. - * @param newValue new value of block extension value with key == rule.id - */ -case class ChangedRule(newValue: Array[Byte]) extends RuleStatus { - val statusCode: Byte = RuleStatus.ChangedRuleCode - - override def hashCode(): Int = java.util.Arrays.hashCode(newValue) - - override def canEqual(that: Any): Boolean = that.isInstanceOf[ChangedRule] - - override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match { - case that: ChangedRule => java.util.Arrays.equals(newValue, that.newValue) - }) -} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatusSerializer.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatusSerializer.scala index e032f0f647..deff794ba6 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatusSerializer.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/RuleStatusSerializer.scala @@ -1,5 +1,6 @@ package org.ergoplatform.validation +import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus} import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettings.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettings.scala deleted file mode 100644 index fac738ce5a..0000000000 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettings.scala +++ /dev/null @@ -1,83 +0,0 @@ -package org.ergoplatform.validation - -/** - * Configuration of validation. Each `ValidationRule` instance should be - * implemented as an `object` to facilitate type-safe usage. It then should be - * registered in `ValidationRules.currentSettings` to be used in the code to - * perform validation. Added to `currentSettings` the rule has EnabledRule - * status by default, but only in a current version of the code. Thus, the - * value `currentSettings` represents the validation settings of the current - * version of the code. The set of rules in `currentSettings` is fixed in the - * current version of the code and thus only rule status can be changed (as - * described below) - * - * Old versions of the code don't have access to the rules added in newer - * versions. The implementation of the specific rule, once released under - * specific ruleId, should never be changed, hence ruleId denotes that - * implementation. However, the behavior of rules (released with code) can be - * altered by changing their status in block extensions section via voting. - * - * The status changes are represented in ValidationSettings using the - * RuleStatus type. Each descendant class represent a particular change in the - * rule status. Rule ids are used as keys of the status values stored in the - * block extension section. RuleStatus instances are deserialized from the - * block extension values. Deserialized (ruleId, status) pairs are joined with - * the (ruleId,status) pairs in `currentSettings`, and for matching ruleIds the - * default statues stored in `currentSettings` are replaced with the new - * statuses obtained from the blockchain. Deserialized (ruleId,status) pairs - * which don't match with `currentSettings` are ignored. - * - * Each rule has associated check of soft-fork condition by implementing - * `isSoftFork` method. If `isSoftFork` returns true, then ValidationException - * raised by the rule is interpreted as *soft-fork condition*. Depending on the - * use case, soft-fork condition allows some operations performed by an old - * code to succeed which otherwise would fail due to ValidationException raised - * by the validation rule. One notable use case is Box.ergoTree validation in - * which old code can skip ValidationExceptions under soft-fork condition (i.e. - * when isSoftFork returns true), for example when a new opCode is added in the - * newer version of the protocol, and this fact can be recognized by the old - * code. - * - * @see SoftForkWhenCodeAdded - */ -abstract class SigmaValidationSettings extends Iterable[(Short, (ValidationRule, RuleStatus))] { - def get(id: Short): Option[(ValidationRule, RuleStatus)] - def getStatus(id: Short): Option[RuleStatus] - def updated(id: Short, newStatus: RuleStatus): SigmaValidationSettings - def isSoftFork(ve: ValidationException): Boolean = isSoftFork(ve.rule.id, ve) - def isSoftFork(ruleId: Short, ve: ValidationException): Boolean = { - val infoOpt = get(ruleId) - infoOpt match { - case Some((_, ReplacedRule(_))) => true - case Some((rule, status)) => rule.isSoftFork(this, rule.id, status, ve.args) - case None => false - } - } -} - -/** Default representation of validation settings. */ -sealed class MapSigmaValidationSettings(private val map: Map[Short, (ValidationRule, RuleStatus)]) extends SigmaValidationSettings { - override def iterator: Iterator[(Short, (ValidationRule, RuleStatus))] = map.iterator - override def get(id: Short): Option[(ValidationRule, RuleStatus)] = map.get(id) - - /** HOTSPOT: don't beautify this code */ - override def getStatus(id: Short): Option[RuleStatus] = { - val statusOpt = map.get(id) - val res = if (statusOpt.isDefined) Some(statusOpt.get._2) else None - res - } - override def updated(id: Short, newStatus: RuleStatus): MapSigmaValidationSettings = { - val (rule,_) = map(id) - new MapSigmaValidationSettings(map.updated(id, (rule, newStatus))) - } - - override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match { - case that: MapSigmaValidationSettings => - map.size == that.map.size && - map.iterator.forall { case (id, v) => that.map.get(id).exists(_ == v) } - case _ => false - }) - - override def hashCode(): Int = map.toIndexedSeq.sortBy(_._1).hashCode() -} - diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializer.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializer.scala index a3b89f294b..aec5726593 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializer.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializer.scala @@ -1,9 +1,10 @@ package org.ergoplatform.validation +import sigma.serialization.SerializerException import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigma.util.Extensions.IntOps -import sigmastate.exceptions.SerializerException +import sigma.validation.{MapSigmaValidationSettings, SigmaValidationSettings} // TODO v5.x: remove unused class and related json encoders /** The rules are serialized ordered by ruleId. diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SoftForkChecker.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/SoftForkChecker.scala deleted file mode 100644 index 3af05c6f6d..0000000000 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/SoftForkChecker.scala +++ /dev/null @@ -1,42 +0,0 @@ -package org.ergoplatform.validation - -/** Interface implemented by objects capable of checking soft-fork conditions. */ -trait SoftForkChecker { - /** Check soft-fork condition. - * @param vs ValidationSettings actualized from blockchain extension sections - * @param ruleId id of the rule which raised ValidationException - * @param status status of the rule in the blockchain (agreed upon via voting) - * @param args arguments of Validation rule with which the rule has risen the exception - * @return true if `args` and `status` can be interpreted as valid soft-fork condition. - */ - def isSoftFork(vs: SigmaValidationSettings, ruleId: Short, status: RuleStatus, args: Seq[Any]): Boolean = false -} - -/** Checks that the failed validation rule has ReplacedRule status in block extensions section. - * This means the rule given by `ruleId` is not used in newer versions of the protocol. - * Instead it has been replaced by the new rule given by ReplacedRule status. - */ -trait SoftForkWhenReplaced extends SoftForkChecker { - override def isSoftFork(vs: SigmaValidationSettings, - ruleId: Short, - status: RuleStatus, - args: Seq[Any]): Boolean = (status, args) match { - case (ReplacedRule(_), _) => true - case _ => false - } -} - -/** Checks that the unknown `code` is however present in the ChangedRule new value - * stored in block extensions section. This is interpreted as soft-fork condition, - * i.e. the unknown `code` is not arbitrary, but explicitly added to the blockchain - * configuration and implemented in newer versions of the protocol. - */ -trait SoftForkWhenCodeAdded extends SoftForkChecker { - override def isSoftFork(vs: SigmaValidationSettings, - ruleId: Short, - status: RuleStatus, - args: Seq[Any]): Boolean = (status, args) match { - case (ChangedRule(newValue), Seq(code: Byte)) => newValue.contains(code) - case _ => false - } -} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala index 4191421b10..c58bff6da2 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala @@ -1,73 +1,17 @@ package org.ergoplatform.validation -import sigma.ast.{SGlobal, SOption, TypeCodes} +import sigma.SigmaException +import sigma.serialization.{InvalidOpCode, SerializerException} import sigma.util.Extensions.toUByte +import sigma.validation.ValidationRules._ +import sigma.validation._ import sigmastate.Values.{ErgoTree, SValue} import sigmastate._ import sigmastate.exceptions._ -import sigmastate.serialization.TypeSerializer.embeddableIdToType import sigmastate.serialization.ValueCodes.OpCode -import sigmastate.serialization.{OpCodes, ValueCodes, ValueSerializer} +import sigmastate.serialization.{ValueCodes, ValueSerializer} import sigmastate.utxo.DeserializeContext -/** Base class for different validation rules registered in ValidationRules.currentSettings. - * Each rule is identified by `id` and have a description. - * Validation logic is implemented by `apply` methods of derived classes. - */ -case class ValidationRule( - id: Short, - description: String -) extends SoftForkChecker { - - /** Whether the status of this rule was checked on first invocation. */ - private var _checked: Boolean = false - - /** Check the rule is registered and enabled. - * Since it is easy to forget to register new rule, we need to do this check. - * But because it is hotspot, we do this check only once for each rule. - * HOTSPOT: executed on every typeCode and opCode during script deserialization - */ - @inline protected final def checkRule(): Unit = { - if (!_checked) { - if (ValidationRules.currentSettings.getStatus(this.id).isEmpty) { - throw new SigmaException(s"ValidationRule $this not found in validation settings") - } - _checked = true // prevent this check on every call (only first call is checked) - } - // upon successful return we know the rule is registered with EnabledRule status - } - - /** Throws ValidationException with the given cause and args. - * Should be used in all validation rules to unify ValidationException instances - * which can be thrown (to simplify handling). - */ - def throwValidationException(cause: Throwable, args: Seq[Any]): Nothing = { - if (cause.isInstanceOf[ValidationException]) { - throw cause - } - else { - throw ValidationException(s"Validation failed on $this with args $args", this, args, Option(cause)) - } - } - -} - -/** Base class for all exceptions which may be thrown by validation rules. - * Instances of this class are used as messages to communicate soft-fork information, - * from the context where the soft-fork condition is detected (such as in ValidationRules), - * up the stack to the point where it is clear how to handle it. - * Some messages of this kind are not handled, in which case a new Exception is thrown - * and this instance should be attached as a `cause` parameter. - * - * This exception should typically always come with Some(cause). As result, stack trace is not - * filled in when this instance is create. The `cause` parameter should be examined for the source - * of the exception. - */ -case class ValidationException(message: String, rule: ValidationRule, args: Seq[Any], cause: Option[Throwable] = None) - extends Exception(message, cause.orNull) { - override def fillInStackTrace(): Throwable = this // to avoid spending time on recording stack trace -} - /** All validation rules which are used to check soft-forkable conditions. Each validation * rule throws a [[org.ergoplatform.validation.ValidationException]]. Each * ValidationException can be caught and handled with respect to @@ -75,11 +19,11 @@ case class ValidationException(message: String, rule: ValidationRule, args: Seq[ * Thus, the behavior of the rules can be overridden without breaking consensus. */ object ValidationRules { - /** The id of the first validation rule. Can be used as the beginning of the rules id range. */ - val FirstRuleId = 1000.toShort object CheckDeserializedScriptType extends ValidationRule(FirstRuleId, "Deserialized script should have expected type") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + final def apply[T](d: DeserializeContext[_], script: SValue): Unit = { checkRule() if (d.tpe != script.tpe) { @@ -93,6 +37,8 @@ object ValidationRules { object CheckDeserializedScriptIsSigmaProp extends ValidationRule(1001, "Deserialized script should have SigmaProp type") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + /** @param root candidate node before it is added as a root of ErgoTree */ final def apply[T](root: SValue): Unit = { checkRule() @@ -107,6 +53,8 @@ object ValidationRules { object CheckValidOpCode extends ValidationRule(1002, "Check the opcode is supported by registered serializer or is added via soft-fork") with SoftForkWhenCodeAdded { + override protected lazy val settings: SigmaValidationSettings = currentSettings + final def apply[T](ser: ValueSerializer[_], opCode: OpCode): Unit = { checkRule() if (ser == null || ser.opCode != opCode) { @@ -119,20 +67,28 @@ object ValidationRules { /** Not used since v5.0.1. */ object CheckIsSupportedIndexExpression extends ValidationRule(1003, - "Check the index expression for accessing collection element is supported.") + "Check the index expression for accessing collection element is supported.") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + } /** Not used since v5.0.3 */ object CheckCostFunc extends ValidationRule(1004, - "Cost function should contain only operations from specified list.") + "Cost function should contain only operations from specified list.") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + } object CheckCalcFunc extends ValidationRule(1005, - "If SigmaProp.isProven method calls exists in the given function,\n then it is the last operation") + "If SigmaProp.isProven method calls exists in the given function,\n then it is the last operation") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + } /** This rule is not use in v5.x, keep the commented code below as a precise * documentation of its semantics. */ object CheckTupleType extends ValidationRule(1006, "Supported tuple type.") with SoftForkWhenReplaced { + override protected lazy val settings: SigmaValidationSettings = currentSettings + // final def apply[Ctx <: IRContext, T](ctx: Ctx)(e: ctx.Elem[_]): Unit = { // checkRule() // val condition = e match { @@ -145,78 +101,10 @@ object ValidationRules { // } } - object CheckPrimitiveTypeCode extends ValidationRule(1007, - "Check the primitive type code is supported or is added via soft-fork") - with SoftForkWhenCodeAdded { - final def apply[T](code: Byte): Unit = { - checkRule() - val ucode = toUByte(code) - if (ucode <= 0 || ucode >= embeddableIdToType.length) { - throwValidationException( - new SerializerException(s"Cannot deserialize primitive type with code $ucode"), - Array(code)) - } - } - } - - object CheckTypeCode extends ValidationRule(1008, - "Check the non-primitive type code is supported or is added via soft-fork") - with SoftForkWhenCodeAdded { - final def apply[T](typeCode: Byte): Unit = { - checkRule() - val ucode = toUByte(typeCode) - if (ucode > toUByte(SGlobal.typeCode)) { - throwValidationException( - new SerializerException(s"Cannot deserialize the non-primitive type with code $ucode"), - Array(typeCode)) - } - } - } - - object CheckSerializableTypeCode extends ValidationRule(1009, - "Check the data values of the type (given by type code) can be serialized") - with SoftForkWhenReplaced { - - /** Creates an exception which is used as a cause when throwing a ValidationException. */ - def throwValidationException(typeCode: Byte): Nothing = { - val ex = new SerializerException( - s"Data value of the type with the code ${toUByte(typeCode)} cannot be deserialized.") - throwValidationException(ex, Array(typeCode)) - } - - final def apply[T](typeCode: Byte): Unit = { - checkRule() - val ucode = toUByte(typeCode) - if (typeCode == SOption.OptionTypeCode || ucode > toUByte(TypeCodes.LastDataType)) { - // the `typeCode == SOption.OptionTypeCode` condition is added in v5.0 and we - // throw ValidationException for Option type as well in order to be able to - // interpret it as soft-fork condition. - // This will allow to add Option serialization in DataSerializer via v6.0 soft-fork. - // This is in contrast to v4.x of this rule where Option type is not checked and - // ordinary SigmaSerializer exception is thrown by the fallback case of DataSerializer. - // This change is consensus-safe as v4.x and v5.x nodes will both throw exceptions - // (albeit different ones) while attempting deserialization of an Option value. - throwValidationException(typeCode) - } - } - } - - object CheckTypeWithMethods extends ValidationRule(1010, - "Check the type (given by type code) supports methods") - with SoftForkWhenCodeAdded { - final def apply[T](typeCode: Byte, cond: Boolean): Unit = { - checkRule() - val ucode = toUByte(typeCode) - if (!cond) { - throwValidationException( - new SerializerException(s"Type with code $ucode doesn't support methods."), - Array(typeCode)) - } - } - } - object CheckAndGetMethod extends ValidationRule(1011, "Check the type has the declared method.") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + final def apply[T](objType: MethodsContainer, methodId: Byte): SMethod = { checkRule() val methodOpt = objType.getMethodById(methodId) @@ -241,6 +129,8 @@ object ValidationRules { object CheckHeaderSizeBit extends ValidationRule(1012, "For version greater then 0, size bit should be set.") with SoftForkWhenReplaced { + override protected lazy val settings: SigmaValidationSettings = currentSettings + final def apply(header: Byte): Unit = { checkRule() val version = ErgoTree.getVersion(header) @@ -254,39 +144,15 @@ object ValidationRules { /** Not used since v5.0.3 */ object CheckCostFuncOperation extends ValidationRule(1013, - "Check the opcode is allowed in cost function") - - /** Introduced in v5.0, this rule it is used in creation of ValidationExceptions, which - * in turn can be checked for soft-fork condition using `this.isSoftFork`. Thus, this - * rule can be replaced with a new rule and the limit can be increased. - */ - object CheckPositionLimit extends ValidationRule(1014, - "Check that the Reader has not exceeded the position limit.") with SoftForkWhenReplaced { - - /** Wraps the given cause into [[ValidationException]] and throws it. */ - def throwValidationException(cause: ReaderPositionLimitExceeded): Nothing = { - throwValidationException(cause, args = Nil) - } - - /** Throws a [[ValidationException]] with the given parameters. */ - def throwValidationException(position: Int, positionLimit: Int): Nothing = { - throwValidationException( - new ReaderPositionLimitExceeded( - s"SigmaByteReader position limit $positionLimit is reached at position $position", - position, positionLimit)) - } - - final def apply(position: Int, positionLimit: Int): Unit = { - checkRule() - if (position > positionLimit) { - throwValidationException(position, positionLimit) - } - } + "Check the opcode is allowed in cost function") { + override protected lazy val settings: SigmaValidationSettings = currentSettings } /** Not used since v5.0.1 */ object CheckLoopLevelInCostFunction extends ValidationRule(1015, - "Check that loop level is not exceeded.") + "Check that loop level is not exceeded.") { + override protected lazy val settings: SigmaValidationSettings = currentSettings + } val ruleSpecs: Seq[ValidationRule] = Seq( CheckDeserializedScriptType, @@ -319,24 +185,4 @@ object ValidationRules { map }) - /** Executes the given `block` catching [[ValidationException]] and checking possible - * soft-fork condition in the context of the given [[SigmaValidationSettings]]. - * If soft-fork condition is recognized the `whenSoftFork` is executed and its result - * is returned. - * - * @param whenSoftFork executed when soft-fork condition is detected - * @param block block of code, which may throw [[ValidationException]] - * @param vs set of [[SigmaValidationSettings]] which can be used to recognize soft-fork conditions. - * @return result of `block` if no ValidationException was thrown, or the result of - * `whenSoftFork` if soft-fork condition is detected. - * @throws ValidationException if soft-fork condition is not recognized by the given `vs` - */ - def trySoftForkable[T](whenSoftFork: => T)(block: => T)(implicit vs: SigmaValidationSettings): T = { - try block - catch { - case ve: ValidationException => - if (vs.isSoftFork(ve)) whenSoftFork - else throw ve - } - } } diff --git a/interpreter/shared/src/main/scala/sigmastate/Operations.scala b/interpreter/shared/src/main/scala/sigmastate/Operations.scala index 51ae277757..0459062c8e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Operations.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Operations.scala @@ -1,5 +1,6 @@ package sigmastate +import sigma.serialization.CoreByteWriter.ArgInfo import sigmastate.lang.SigmaPredef.PredefinedFuncRegistry import sigmastate.lang.StdSigmaBuilder diff --git a/interpreter/shared/src/main/scala/sigmastate/SMethod.scala b/interpreter/shared/src/main/scala/sigmastate/SMethod.scala index 0ee6df43aa..06ae29ea3e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SMethod.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SMethod.scala @@ -6,6 +6,8 @@ import sigma.{Coll, Evaluation} import sigma.ast._ import sigma.data.RType import sigma.reflection.{RClass, RMethod} +import sigma.serialization.CoreByteWriter.ArgInfo +import sigma.validation.ValidationRules.CheckTypeWithMethods import sigmastate.SMethod.{InvokeDescBuilder, MethodCostFunc} import sigmastate.Values.{SValue, ValueCompanion} import sigmastate.interpreter.{CostDetails, ErgoTreeEvaluator, FixedCostItem, GivenCost, MethodDesc, SeqCostItem, TracedCost} @@ -15,12 +17,6 @@ import sigmastate.lang.Terms.{MethodCall, OperationId, STypeSubst} import scala.collection.compat.immutable.ArraySeq import scala.reflect.ClassTag -/** Meta information which can be attached to each argument of SMethod. - * - * @param name name of the argument - * @param description argument description. */ -case class ArgInfo(name: String, description: String) - /** Meta information which can be attached to SMethod. * @param opDesc optional operation descriptor * @param description human readable description of the method @@ -315,7 +311,7 @@ object SMethod { * `parse` method and hence it is part of consensus protocol */ def fromIds(typeId: Byte, methodId: Byte): SMethod = { - ValidationRules.CheckTypeWithMethods(typeId, MethodsContainer.contains(typeId)) + CheckTypeWithMethods(typeId, MethodsContainer.contains(typeId)) val container = MethodsContainer(typeId) val method = container.methodById(methodId) method diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index 20341cbcc6..329ecc993b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -12,8 +12,8 @@ import sigmastate.serialization.SigmaSerializer import sigma.util.safeNewArray import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} import debox.cfor +import sigma.serialization.SerializerException import sigmastate.eval.Extensions.ArrayOps -import sigmastate.exceptions.SerializerException /** Contains implementation of signature (aka proof) serialization. * diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index bad3f62660..457419a4ff 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -4,7 +4,6 @@ import java.math.BigInteger import java.util.{Arrays, Objects} import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} import org.ergoplatform.settings.ErgoAlgos -import org.ergoplatform.validation.ValidationException import sigma.data.{Nullable, RType} import sigma.util.CollectionUtil._ import sigmastate.crypto.CryptoConstants.EcPointType @@ -35,6 +34,7 @@ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} import sigmastate.lang.SourceContext import sigma.util.safeNewArray +import sigma.validation.ValidationException import sigmastate.serialization.ValueCodes.{ConstantCode, OpCode} import scala.collection.compat.immutable.ArraySeq diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index e1ff679028..2a04240ed4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -1,30 +1,29 @@ package sigmastate.eval import debox.cfor -import org.ergoplatform.validation.{SigmaValidationSettings, ValidationRules} import org.ergoplatform.ErgoBox -import sigma.data.OverloadHack.Overloaded1 -import sigma.data.{CBigInt, CollOverArrayBuilder, RType, SigmaConstants, WrapperOf} -import sigma.util.Extensions.BigIntegerOps +import org.ergoplatform.validation.ValidationRules import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADDigest, ADKey, ADValue, SerializedAdProof} import scorex.crypto.hash.{Blake2b256, Digest32, Sha256} import scorex.utils.{Ints, Longs} -import sigma.VersionContext +import sigma.{VersionContext, _} +import sigma.ast.SCollection.SByteArray +import sigma.ast.{SInt, STuple, SType} +import sigma.data.OverloadHack.Overloaded1 +import sigma.data.{CBigInt, RType, SigmaConstants, WrapperOf} +import sigma.util.Extensions.BigIntegerOps +import sigma.validation.SigmaValidationSettings import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue, SigmaBoolean} import sigmastate._ import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, ProveDHTuple} -import sigmastate.crypto.{CryptoFacade, Ecp} +import sigmastate.crypto.{CryptoConstants, CryptoFacade, Ecp, ProveDHTuple} import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer} -import sigma._ -import sigma.ast.SCollection.SByteArray -import sigma.ast.{SInt, STuple, SType} import java.math.BigInteger import java.util.Arrays diff --git a/interpreter/shared/src/main/scala/sigmastate/exceptions/CompilerExceptions.scala b/interpreter/shared/src/main/scala/sigmastate/exceptions/CompilerExceptions.scala index 8c331766af..3fe149170c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/exceptions/CompilerExceptions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/exceptions/CompilerExceptions.scala @@ -1,5 +1,6 @@ package sigmastate.exceptions +import sigma.SigmaException import sigmastate.lang.SourceContext /** Base class for exceptions thrown by the compiler. diff --git a/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaExceptions.scala b/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaExceptions.scala index 8014fccda9..4183646296 100644 --- a/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaExceptions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaExceptions.scala @@ -1,25 +1,8 @@ package sigmastate.exceptions +import sigma.SigmaException import sigmastate.JitCost -/** Base class for Sigma-related exceptions. - * - * @param message the error message - * @param cause an optional cause for the exception - */ -class SigmaException(val message: String, val cause: Option[Throwable] = None) - extends Exception(message, cause.orNull) - -/** Exception thrown during serialization. - * - * @param message the error message - * @param cause an optional cause for the exception - */ -case class SerializerException( - override val message: String, - override val cause: Option[Throwable] = None) - extends SigmaException(message, cause) - /** Exception thrown by [[sigmastate.interpreter.Interpreter]]. * * @param message the error message diff --git a/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaSerializerExceptions.scala b/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaSerializerExceptions.scala deleted file mode 100644 index ee49cb433a..0000000000 --- a/interpreter/shared/src/main/scala/sigmastate/exceptions/SigmaSerializerExceptions.scala +++ /dev/null @@ -1,23 +0,0 @@ -package sigmastate.exceptions - -/** Thrown by TypeSerializer when type prefix <= 0. */ -final class InvalidTypePrefix(message: String, cause: Option[Throwable] = None) - extends SerializerException(message, cause) - -/** Thrown when the current reader position > positionLimit which is set in the Reader. - * @see [[org.ergoplatform.validation.ValidationRules.CheckPositionLimit]] - */ -final class ReaderPositionLimitExceeded( - message: String, - val position: Int, - val positionLimit: Int, - cause: Option[Throwable] = None) - extends SerializerException(message, cause) - -/** Thrown when the current depth level > maxDepthLevel which is set in the Reader. */ -final class DeserializeCallDepthExceeded(message: String, cause: Option[Throwable] = None) - extends SerializerException(message, cause) - -/** Thrown by [[org.ergoplatform.validation.ValidationRules.CheckValidOpCode]] validation rule. */ -final class InvalidOpCode(message: String, cause: Option[Throwable] = None) - extends SerializerException(message, cause) diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala index 7f4be148c9..b0f467d6bd 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala @@ -3,7 +3,7 @@ package sigmastate.interpreter import java.util import sigma.kiama.rewriting.Rewriter.{everywherebu, rule, strategy} import org.ergoplatform.ErgoLikeContext -import org.ergoplatform.validation.SigmaValidationSettings +import sigma.validation.SigmaValidationSettings import org.ergoplatform.validation.ValidationRules._ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.Values._ @@ -24,6 +24,7 @@ import sigma.ast.SCollection.SByteArray import sigma.ast.{SBoolean, SSigmaProp, SType} import sigma.{Evaluation, VersionContext} import sigma.kiama.rewriting.Strategy +import sigma.validation.ValidationRules.trySoftForkable import sigmastate.exceptions.{CostLimitException, InterpreterException} import scala.util.{Success, Try} diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/InterpreterContext.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/InterpreterContext.scala index c64c1a5e6d..3388defe1a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/InterpreterContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/InterpreterContext.scala @@ -1,12 +1,12 @@ package sigmastate.interpreter -import org.ergoplatform.validation.SigmaValidationSettings import sigmastate.Values.EvaluatedValue import sigmastate.interpreter.ContextExtension.VarBinding import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigma.AnyValue import sigma.ast.SType +import sigma.validation.SigmaValidationSettings import scala.collection.mutable diff --git a/interpreter/shared/src/main/scala/sigmastate/lang/SigmaPredef.scala b/interpreter/shared/src/main/scala/sigmastate/lang/SigmaPredef.scala index 74b1d45992..b4f6408c2f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/lang/SigmaPredef.scala +++ b/interpreter/shared/src/main/scala/sigmastate/lang/SigmaPredef.scala @@ -7,6 +7,7 @@ import scorex.util.encode.{Base16, Base58, Base64} import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.SOption.SIntOption import sigma.ast._ +import sigma.serialization.CoreByteWriter.ArgInfo import sigmastate.Values.{BoolValue, ByteArrayConstant, Constant, ConstantPlaceholder, EvaluatedValue, IntValue, SValue, SigmaPropConstant, SigmaPropValue, StringConstant, Value, ValueCompanion} import sigmastate._ import sigmastate.lang.Terms._ diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ApplySerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ApplySerializer.scala index 9145491518..bbf50c045e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ApplySerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ApplySerializer.scala @@ -2,10 +2,10 @@ package sigmastate.serialization import sigma.ast.SType import sigmastate.Values._ -import sigmastate._ import sigmastate.lang.Terms.Apply -import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigma.serialization.CoreByteWriter._ +import sigmastate.utils.SigmaByteWriter._ case class ApplySerializer(cons: (Value[SType], IndexedSeq[Value[SType]]) => Value[SType]) extends ValueSerializer[Apply] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/BlockValueSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/BlockValueSerializer.scala index ea0fad49d2..ea517c0f1c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/BlockValueSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/BlockValueSerializer.scala @@ -5,7 +5,8 @@ import sigmastate._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import ValueSerializer._ import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, U, Vlq} +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo, U, Vlq} +import sigmastate.utils.SigmaByteWriter._ import debox.cfor import sigma.ast.SType diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/BoolToSigmaPropSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/BoolToSigmaPropSerializer.scala index 742355f61f..199ed8ac60 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/BoolToSigmaPropSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/BoolToSigmaPropSerializer.scala @@ -1,9 +1,10 @@ package sigmastate.serialization import sigma.ast.SType -import sigmastate.Values.{BoolValue, SigmaPropValue, SValue} +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.Values.{BoolValue, SValue, SigmaPropValue} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.{BoolToSigmaProp, Values} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionBooleanConstantSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionBooleanConstantSerializer.scala index c901f81285..ad168cc61c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionBooleanConstantSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionBooleanConstantSerializer.scala @@ -2,11 +2,10 @@ package sigmastate.serialization import sigmastate.Values._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import SigmaByteWriter._ import sigma.util.safeNewArray import debox.cfor import sigma.ast.{SBoolean, SCollection} -import sigmastate.ArgInfo +import sigma.serialization.CoreByteWriter.{ArgInfo, Bits, DataInfo, U, Vlq, maxBitsInfo} case class ConcreteCollectionBooleanConstantSerializer(cons: (IndexedSeq[Value[SBoolean.type]], SBoolean.type) => Value[SCollection[SBoolean.type]]) extends ValueSerializer[ConcreteCollection[SBoolean.type]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionSerializer.scala index 5419599b61..9eef813960 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ConcreteCollectionSerializer.scala @@ -1,13 +1,13 @@ package sigmastate.serialization -import sigmastate.{ArgInfo} import sigmastate.Values._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import ValueSerializer._ import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, U, Vlq} +import sigmastate.utils.SigmaByteWriter._ import debox.cfor import sigma.ast.{SCollection, SType} +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo, U, Vlq} case class ConcreteCollectionSerializer(cons: (IndexedSeq[Value[SType]], SType) => Value[SCollection[SType]]) extends ValueSerializer[ConcreteCollection[_ <: SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/CreateAvlTreeSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/CreateAvlTreeSerializer.scala index a8aaac50d3..f623be7588 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/CreateAvlTreeSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/CreateAvlTreeSerializer.scala @@ -3,11 +3,12 @@ package sigmastate.serialization import sigma.ast.SCollection.SByteArray import sigma.ast.SInt import sigma.ast.SOption.SIntOption +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate._ import sigmastate.Values._ import sigmastate.lang.Terms.ValueOps -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ case class CreateAvlTreeSerializer( cons: (ByteValue, Value[SByteArray], IntValue, Value[SIntOption]) => AvlTreeValue diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 4f41419425..1bf8c5e2dc 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -3,7 +3,6 @@ package sigmastate.serialization import java.math.BigInteger import java.nio.charset.StandardCharsets import org.ergoplatform.ErgoBox -import org.ergoplatform.validation.ValidationRules.CheckSerializableTypeCode import sigma.data.RType import sigmastate.Values.SigmaBoolean import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} @@ -12,7 +11,8 @@ import sigmastate.eval._ import sigma.{Evaluation, _} import debox.cfor import sigma.ast._ -import sigmastate.exceptions.SerializerException +import sigma.serialization.SerializerException +import sigma.validation.ValidationRules.CheckSerializableTypeCode import scala.collection.mutable diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala index 85c0c77e04..c26e1b727a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala @@ -1,7 +1,6 @@ package sigmastate.serialization -import org.ergoplatform.validation.ValidationRules.{CheckDeserializedScriptIsSigmaProp, CheckHeaderSizeBit, CheckPositionLimit} -import org.ergoplatform.validation.{SigmaValidationSettings, ValidationException} +import org.ergoplatform.validation.ValidationRules.{CheckDeserializedScriptIsSigmaProp, CheckHeaderSizeBit} import sigmastate.Values.{Constant, ErgoTree, UnparsedErgoTree} import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.lang.Terms.ValueOps @@ -12,7 +11,9 @@ import sigmastate.utxo.ComplexityTable import debox.cfor import sigma.VersionContext import sigma.ast.SType -import sigmastate.exceptions.{ReaderPositionLimitExceeded, SerializerException} +import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} +import sigma.validation.{SigmaValidationSettings, ValidationException} +import sigma.validation.ValidationRules.CheckPositionLimit import java.util diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/FuncValueSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/FuncValueSerializer.scala index 5ff6659317..1ae6cab038 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/FuncValueSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/FuncValueSerializer.scala @@ -5,9 +5,10 @@ import sigmastate._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import ValueSerializer._ import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, U, Vlq} +import sigmastate.utils.SigmaByteWriter._ import debox.cfor import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo, U, Vlq} case class FuncValueSerializer(cons: (IndexedSeq[(Int, SType)], Value[SType]) => Value[SType]) extends ValueSerializer[FuncValue] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/GetVarSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/GetVarSerializer.scala index d3c797f896..2e449c47c8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/GetVarSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/GetVarSerializer.scala @@ -1,9 +1,10 @@ package sigmastate.serialization import sigma.ast.{SOption, SType} +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Values._ import sigmastate._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.GetVar diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/LogicalNotSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/LogicalNotSerializer.scala index 667e8a699f..6bc395a97e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/LogicalNotSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/LogicalNotSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.LogicalNot import sigmastate.Operations.LogicalNotInfo.inputArg import sigmastate.Values.{BoolValue, SValue} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} case class LogicalNotSerializer(cons: BoolValue => BoolValue) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala index 2892463755..3d9518d2d9 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala @@ -5,11 +5,12 @@ import sigmastate._ import sigmastate.lang.Terms.STypeSubst import sigmastate.lang.Terms.MethodCall import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, valuesItemInfo} +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ComplexityTable import debox.cfor import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} case class MethodCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value[SType]], STypeSubst) => Value[SType]) extends ValueSerializer[MethodCall] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ModQArithOpSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ModQArithOpSerializer.scala index 2cde4929b2..bad08986a1 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ModQArithOpSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ModQArithOpSerializer.scala @@ -1,11 +1,12 @@ package sigmastate.serialization import sigma.ast.SType -import sigmastate.Values.{BigIntValue, Value, SValue} +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.Values.{BigIntValue, SValue, Value} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.{ModQArithOpCompanion, ModQArithOp} +import sigmastate.{ModQArithOp, ModQArithOpCompanion} // TODO v6.0: make sure it is covered with tests (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/327) case class ModQArithOpSerializer(override val opDesc: ModQArithOpCompanion, cons: (BigIntValue, BigIntValue) => BigIntValue) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ModQSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ModQSerializer.scala index 374115c585..676b9bc43f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ModQSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ModQSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization import sigma.ast.SType +import sigmastate.ModQ import sigmastate.Values.Value import sigmastate.lang.Terms._ +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.ModQ // TODO v6.0: make sure it is covered with tests (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/327) object ModQSerializer extends ValueSerializer[ModQ] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/OneArgumentOperationSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/OneArgumentOperationSerializer.scala index c70eae922a..e522fcb23f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/OneArgumentOperationSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/OneArgumentOperationSerializer.scala @@ -1,9 +1,10 @@ package sigmastate.serialization import sigma.ast.SType -import sigmastate.Values.{Value, SValue} +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.{OneArgumentOperation, OneArgumentOperationCompanion} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/OptionGetOrElseSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/OptionGetOrElseSerializer.scala index 1c6bda46d0..0ad016c3b4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/OptionGetOrElseSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/OptionGetOrElseSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization import sigma.ast.{SOption, SType} +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.Values._ import sigmastate._ import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.OptionGetOrElse diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala index 00fc16268f..097825e1aa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Values._ import sigmastate._ import sigmastate.lang.Terms import sigmastate.lang.Terms.STypeSubst import sigmastate.lang.Terms.{MethodCall, PropertyCall} -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ComplexityTable diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala index f15b98cfc4..1ea7057bd3 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization import sigma.ast.SGroupElement +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.CreateProveDlog -import sigmastate.Values.{Value, SValue, SigmaPropValue} +import sigmastate.Values.{SValue, SigmaPropValue, Value} import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} case class ProveDlogSerializer(cons: EcPointType => ProveDlog) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/SelectFieldSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/SelectFieldSerializer.scala index 61085fa5c2..488bfe08f9 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/SelectFieldSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/SelectFieldSerializer.scala @@ -7,7 +7,8 @@ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.SelectField import SelectFieldInfo._ import sigma.ast.{STuple, SType} -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ case class SelectFieldSerializer(cons: (Value[STuple], Byte) => Value[SType]) extends ValueSerializer[SelectField] { override def opDesc = SelectField diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaPropBytesSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaPropBytesSerializer.scala index 658e4cdae8..839727f705 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaPropBytesSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaPropBytesSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization import sigma.ast.SType +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.Values.SValue import sigmastate.Values import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.SigmaPropBytes diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaSerializer.scala index 6e3671a00b..78c60afba9 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/SigmaSerializer.scala @@ -1,12 +1,12 @@ package sigmastate.serialization import java.nio.ByteBuffer -import org.ergoplatform.validation.SigmaValidationSettings import scorex.util.ByteArrayBuilder import sigmastate.utils._ import scorex.util.serialization._ import sigma.data.SigmaConstants -import sigmastate.exceptions.SerializerException +import sigma.serialization.SerializerException +import sigma.validation.SigmaValidationSettings import sigmastate.serialization.ValueCodes.OpCode object SigmaSerializer { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/SubstConstantsSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/SubstConstantsSerializer.scala index 127d3f0593..94d7e20dc2 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/SubstConstantsSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/SubstConstantsSerializer.scala @@ -1,11 +1,12 @@ package sigmastate.serialization -import sigma.ast.SCollection.{SIntArray, SByteArray} -import sigmastate.Values.{Value, SValue} +import sigma.ast.SCollection.{SByteArray, SIntArray} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigma.ast.{SCollection, SType} +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.SubstConstants object SubstConstantsSerializer extends ValueSerializer[SubstConstants[SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/TupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/TupleSerializer.scala index 3ba830f349..1ab9c83094 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/TupleSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/TupleSerializer.scala @@ -1,13 +1,13 @@ package sigmastate.serialization -import sigmastate.{ArgInfo} import sigmastate.Values._ -import sigmastate.utils.{SigmaByteWriter, SigmaByteReader} +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.serialization.ValueSerializer._ import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, U} +import sigmastate.utils.SigmaByteWriter._ import debox.cfor import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo, U} case class TupleSerializer(cons: Seq[Value[SType]] => Value[SType]) extends ValueSerializer[Tuple] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/TwoArgumentsSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/TwoArgumentsSerializer.scala index ec6c917e03..4fcba5c174 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/TwoArgumentsSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/TwoArgumentsSerializer.scala @@ -1,11 +1,12 @@ package sigmastate.serialization import sigma.ast.SType -import sigmastate.Values.{Value, SValue} +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.{TwoArgumentsOperation, TwoArgumentOperationCompanion} +import sigmastate.{TwoArgumentOperationCompanion, TwoArgumentsOperation} case class TwoArgumentsSerializer[LIV <: SType, RIV <: SType, OV <: Value[SType]] (override val opDesc: TwoArgumentOperationCompanion, constructor: (Value[LIV], Value[RIV]) => Value[SType]) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala index 14ea622323..56445ac24b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala @@ -4,6 +4,7 @@ import org.ergoplatform.validation.ValidationRules.CheckValidOpCode import org.ergoplatform._ import sigma.ast.SCollection.SByteArray import sigma.ast._ +import sigma.serialization.CoreByteWriter.DataInfo import sigma.util.Extensions.toUByte import sigmastate.Values._ import sigmastate._ @@ -11,7 +12,7 @@ import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.serialization.ValueCodes.{LastConstantCode, OpCode} import sigmastate.serialization.transformers._ import sigmastate.serialization.trees.{QuadrupleSerializer, Relation2Serializer} -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils._ import sigmastate.utxo.ComplexityTable._ import sigmastate.utxo._ diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AppendSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AppendSerializer.scala index 000a33bbdb..3cd438cab8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AppendSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AppendSerializer.scala @@ -5,6 +5,7 @@ import sigmastate.Values.Value import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utxo.Append import sigma.ast.{SCollection, SType} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AtLeastSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AtLeastSerializer.scala index 120a7455d2..9cde3c1437 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AtLeastSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/AtLeastSerializer.scala @@ -7,6 +7,7 @@ import sigmastate.lang.Terms._ import sigmastate._ import sigmastate.serialization.ValueSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigmastate.utils.SigmaByteWriter._ case class AtLeastSerializer(cons: (Value[SInt.type], Value[SCollection[SSigmaProp.type]]) => SigmaPropValue) extends ValueSerializer[AtLeast] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/BooleanTransformerSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/BooleanTransformerSerializer.scala index db32edae3f..746435c3cd 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/BooleanTransformerSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/BooleanTransformerSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization.transformers -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.{BooleanTransformer, BooleanTransformerCompanion} -import sigma.ast.{SCollection, SBoolean, SType, SFunc} +import sigma.ast.{SBoolean, SCollection, SFunc, SType} +import sigma.serialization.CoreByteWriter.DataInfo case class BooleanTransformerSerializer[T <: SType] (opDesc: BooleanTransformerCompanion, diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ByIndexSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ByIndexSerializer.scala index ef374782bc..99e24b4288 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ByIndexSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ByIndexSerializer.scala @@ -1,14 +1,15 @@ package sigmastate.serialization.transformers -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer import ValueSerializer._ import sigmastate.Operations.ByIndexInfo._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ByIndex -import sigma.ast.{SInt, SCollection, SType} +import sigma.ast.{SCollection, SInt, SType} +import sigma.serialization.CoreByteWriter.DataInfo case class ByIndexSerializer(cons: (Value[SCollection[SType]], Value[SInt.type], Option[Value[SType]]) => Value[SType]) extends ValueSerializer[ByIndex[SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeContextSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeContextSerializer.scala index 807d1212c8..f482341b7f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeContextSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeContextSerializer.scala @@ -1,12 +1,11 @@ package sigmastate.serialization.transformers -import sigmastate.ArgInfo import sigmastate.Values._ import sigmastate.serialization.ValueSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.DeserializeContext -import SigmaByteWriter._ import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Operations.DeserializeContextInfo case class DeserializeContextSerializer(cons: (Byte, SType) => Value[SType]) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeRegisterSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeRegisterSerializer.scala index 4287380e4d..436302f357 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeRegisterSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/DeserializeRegisterSerializer.scala @@ -2,13 +2,13 @@ package sigmastate.serialization.transformers import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.RegisterId -import sigmastate.ArgInfo -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.serialization.ValueSerializer import ValueSerializer._ import sigma.ast.SType +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Operations.DeserializeRegisterInfo._ -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.DeserializeRegister diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ExtractRegisterAsSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ExtractRegisterAsSerializer.scala index 004c602117..f93db97178 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ExtractRegisterAsSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ExtractRegisterAsSerializer.scala @@ -2,13 +2,13 @@ package sigmastate.serialization.transformers import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.RegisterId -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ExtractRegisterAs import sigma.ast.{SBox, SOption, SType} -import sigmastate.ArgInfo +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} case class ExtractRegisterAsSerializer(cons: (Value[SBox.type], RegisterId, SOption[SType]) => Value[SType]) extends ValueSerializer[ExtractRegisterAs[SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/FoldSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/FoldSerializer.scala index 81bfa0e796..df55f68650 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/FoldSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/FoldSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization.transformers -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.Fold -import sigma.ast.{SCollection, SType, SFunc} +import sigma.ast.{SCollection, SFunc, SType} +import sigma.serialization.CoreByteWriter.DataInfo case class FoldSerializer(cons: (Value[SCollection[SType]], Value[SType], Value[SFunc]) => Value[SType]) extends ValueSerializer[Fold[SType, SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/LogicalTransformerSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/LogicalTransformerSerializer.scala index 128fd07331..0e0ace10c8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/LogicalTransformerSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/LogicalTransformerSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization.transformers import sigma.ast.{SBoolean, SCollection} -import sigmastate.Values.{Value, SValue} +import sigma.serialization.CoreByteWriter.DataInfo +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.Transformer import sigmastate.LogicalTransformerCompanion diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/MapCollectionSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/MapCollectionSerializer.scala index ad830a8638..906d33976d 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/MapCollectionSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/MapCollectionSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization.transformers -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.MapCollection -import sigma.ast.{SCollection, SType, SFunc} +import sigma.ast.{SCollection, SFunc, SType} +import sigma.serialization.CoreByteWriter.DataInfo case class MapCollectionSerializer(cons: (Value[SCollection[SType]], Value[SFunc]) => Value[SType]) extends ValueSerializer[MapCollection[SType, SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/NumericCastSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/NumericCastSerializer.scala index 758bed499d..59ad911600 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/NumericCastSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/NumericCastSerializer.scala @@ -1,11 +1,12 @@ package sigmastate.serialization.transformers import sigma.ast.{SNumericType, SType} +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Values.{SValue, Value} import sigmastate._ import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.Transformer diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala index a5108d0b71..b559bcf61d 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala @@ -7,6 +7,7 @@ import sigmastate.crypto.ProveDHTuple import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.lang.Terms._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigmastate.utils.SigmaByteWriter._ import sigmastate.serialization._ case class ProveDHTupleSerializer( diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SigmaTransformerSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SigmaTransformerSerializer.scala index e7981bb725..bc19a1c8fa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SigmaTransformerSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SigmaTransformerSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization.transformers -import sigmastate.{SigmaTransformerCompanion, SigmaTransformer} -import sigmastate.Values.{SigmaPropValue, SValue} +import sigmastate.{SigmaTransformer, SigmaTransformerCompanion} +import sigmastate.Values.{SValue, SigmaPropValue} import sigmastate.serialization.ValueSerializer import sigma.util.safeNewArray -import sigmastate.utils.SigmaByteWriter.{DataInfo, valuesItemInfo} +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import debox.cfor +import sigma.serialization.CoreByteWriter.DataInfo case class SigmaTransformerSerializer[I <: SigmaPropValue, O <: SigmaPropValue] (opDesc: SigmaTransformerCompanion, cons: Seq[SigmaPropValue] => SigmaPropValue) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SimpleTransformerSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SimpleTransformerSerializer.scala index 933ac7ce07..236e4bfcaf 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SimpleTransformerSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SimpleTransformerSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization.transformers import sigma.ast.SType +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.{SimpleTransformerCompanion, Transformer} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SliceSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SliceSerializer.scala index 615e199fc5..8ac06ec05e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SliceSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/SliceSerializer.scala @@ -1,12 +1,13 @@ package sigmastate.serialization.transformers -import sigmastate.Values.{Value, SValue} +import sigmastate.Values.{SValue, Value} import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.Slice -import sigma.ast.{SInt, SCollection, SType} +import sigma.ast.{SCollection, SInt, SType} +import sigma.serialization.CoreByteWriter.DataInfo case class SliceSerializer(cons: (Value[SCollection[SType]], Value[SInt.type], Value[SInt.type]) => Value[SCollection[SType]]) extends ValueSerializer[Slice[SType]] { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/trees/QuadrupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/trees/QuadrupleSerializer.scala index 5a7b0c714d..586a353f0c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/trees/QuadrupleSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/trees/QuadrupleSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization.trees import sigma.ast.SType +import sigma.serialization.CoreByteWriter.DataInfo import sigmastate.Values._ import sigmastate.lang.Terms._ import sigmastate.serialization.ValueSerializer -import sigmastate.utils.SigmaByteWriter.DataInfo +import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.{Quadruple, _} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/trees/Relation2Serializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/trees/Relation2Serializer.scala index f9e8abaf2d..4887a1a1ae 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/trees/Relation2Serializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/trees/Relation2Serializer.scala @@ -1,6 +1,7 @@ package sigmastate.serialization.trees import sigma.ast.{SBoolean, SType} +import sigma.serialization.CoreByteWriter.{ArgInfo, Bits, DataInfo, maxBitsInfo} import sigmastate.Values._ import sigmastate._ import sigmastate.serialization.OpCodes._ diff --git a/interpreter/shared/src/main/scala/sigmastate/trees.scala b/interpreter/shared/src/main/scala/sigmastate/trees.scala index 33d35dfaef..4d7e831580 100644 --- a/interpreter/shared/src/main/scala/sigmastate/trees.scala +++ b/interpreter/shared/src/main/scala/sigmastate/trees.scala @@ -1,7 +1,6 @@ package sigmastate import debox.{cfor, Map => DMap} -import org.ergoplatform.validation.SigmaValidationSettings import sigma.data.ExactIntegral._ import sigma.data.ExactOrdering._ import sigma.data.OverloadHack.Overloaded1 @@ -14,6 +13,8 @@ import sigma.{Coll, Colls, GroupElement, SigmaProp, VersionContext} import sigmastate.ArithOp.OperationImpl import sigmastate.Operations._ import sigma.ast._ +import sigma.serialization.CoreByteWriter.ArgInfo +import sigma.validation.SigmaValidationSettings import sigmastate.Values._ import sigmastate.eval.Extensions.EvalCollOps import sigmastate.eval.NumericOps.{BigIntIsExactIntegral, BigIntIsExactOrdering} diff --git a/interpreter/shared/src/main/scala/sigmastate/types.scala b/interpreter/shared/src/main/scala/sigmastate/types.scala index 25bcd21070..6fcb07b5eb 100644 --- a/interpreter/shared/src/main/scala/sigmastate/types.scala +++ b/interpreter/shared/src/main/scala/sigmastate/types.scala @@ -10,6 +10,7 @@ import sigma.ast.SType.TypeCode import sigma.ast._ import sigma.data.{Nullable, RType, SigmaConstants} import sigma.reflection.RClass +import sigma.serialization.CoreByteWriter.ArgInfo import sigma.{Coll, _} import sigmastate.SMethod.{MethodCallIrBuilder, MethodCostFunc, javaMethodOf} import sigmastate.Values._ diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteReader.scala b/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteReader.scala index da2b442235..c937fcf12d 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteReader.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteReader.scala @@ -1,14 +1,11 @@ package sigmastate.utils -import org.ergoplatform.validation.ValidationRules.CheckPositionLimit -import scorex.util.Extensions._ +import debox.cfor import scorex.util.serialization.Reader +import sigma.serialization.CoreByteReader +import sigma.util.safeNewArray import sigmastate.Values.{SValue, Value} import sigmastate.serialization._ -import sigma.util.safeNewArray -import debox.cfor -import sigma.ast.SType -import sigmastate.exceptions.DeserializeCallDepthExceeded /** Reader used in the concrete implementations of [[SigmaSerializer]]. * It decorates the given reader, delegates most of the methods to it, but also adds new @@ -22,21 +19,11 @@ import sigmastate.exceptions.DeserializeCallDepthExceeded * @param maxTreeDepth limit on the tree depth (recursive invocations) * of the deserializer */ -class SigmaByteReader(val r: Reader, +class SigmaByteReader(override val r: Reader, var constantStore: ConstantStore, var resolvePlaceholdersToConstants: Boolean, - val maxTreeDepth: Int = SigmaSerializer.MaxTreeDepth) - extends Reader { - - /** Checks that the current reader position is <= positionLimit. - * NOTE, since v5.0 the same check is done via validation rule, which wraps the - * original exception into ValidationException. This makes this condition soft-forkable, - * other than that both v4.x and v5.x will work and fail at the same time, so the - * change is consensus-safe. - */ - @inline private def checkPositionLimit(): Unit = { - CheckPositionLimit(position, positionLimit) - } + override val maxTreeDepth: Int = SigmaSerializer.MaxTreeDepth) + extends CoreByteReader(r, maxTreeDepth) { /** The reader should be lightweight to create. In most cases ErgoTrees don't have * ValDef nodes hence the store is not necessary and it's initialization dominates the @@ -47,74 +34,6 @@ class SigmaByteReader(val r: Reader, override type CH = r.CH - @inline - override def newReader(chunk: CH): Reader.Aux[CH] = r.newReader(chunk) - - @inline override def getChunk(size: Int): CH = r.getChunk(size) - - @inline override def getShortString(): String = { - checkPositionLimit() - r.getShortString() - } - - @inline override def peekByte(): Byte = r.peekByte() - - @inline override def getByte(): Byte = { - checkPositionLimit() - r.getByte() - } - - @inline override def getUByte(): Int = { - checkPositionLimit() - r.getUByte() - } - - @inline override def getShort(): Short = { - checkPositionLimit() - r.getShort() - } - - @inline override def getUShort(): Int = { - checkPositionLimit() - r.getUShort() - } - - @inline override def getInt(): Int = { - checkPositionLimit() - r.getInt() - } - - @inline override def getUInt(): Long = { - checkPositionLimit() - r.getUInt() - } - - @inline def getUIntExact: Int = getUInt().toIntExact - - @inline override def getLong(): Long = { - checkPositionLimit() - r.getLong() - } - - @inline override def getULong(): Long = { - checkPositionLimit() - r.getULong() - } - - @inline override def getBytes(size: Int): Array[Byte] = { - checkPositionLimit() - r.getBytes(size) - } - - /** Reads either the given of remaining number of bytes from this reader. - * This method is `unsafe` because it may return less than requested number of bytes. - * @param numRequestedBytes - */ - @inline final def getBytesUnsafe(numRequestedBytes: Int): Array[Byte] = { - checkPositionLimit() - val bytesToRead = Math.min(numRequestedBytes, remaining) - r.getBytes(bytesToRead) - } /** Returns all bytes of the underlying ByteBuffer. */ private[sigmastate] def getAllBufferBytes: Array[Byte] = { @@ -125,40 +44,8 @@ class SigmaByteReader(val r: Reader, res } - @inline override def getBits(size: Int): Array[Boolean] = { - checkPositionLimit() - r.getBits(size) - } - - @inline override def getOption[T](getValue: => T): Option[T] = { - checkPositionLimit() - r.getOption(getValue) - } - - @inline override def consumed: Int = r.consumed - - @inline override def position: Int = r.position - - @inline override def position_=(p: Int): Unit = r.position_=(p) - - @inline override def remaining: Int = r.remaining - - @inline override def mark(): this.type = { - r.mark() - this - } - - @inline def getType(): SType = TypeSerializer.deserialize(this) @inline def getValue(): SValue = ValueSerializer.deserialize(this) - private var lvl: Int = 0 - @inline def level: Int = lvl - @inline def level_=(v: Int): Unit = { - if (v > maxTreeDepth) - throw new DeserializeCallDepthExceeded(s"nested value deserialization call depth($v) exceeds allowed maximum $maxTreeDepth") - lvl = v - } - /** Read sequence of values from this reader. * It first reads the number of values and then reads each value using `getValue` method. * @@ -176,27 +63,4 @@ class SigmaByteReader(val r: Reader, } } - private var positionLmt: Int = r.position + r.remaining - @inline final def positionLimit: Int = positionLmt - @inline final def positionLimit_=(v: Int): Unit = { - positionLmt = v - } - - private var _complexity: Int = 0 - /** Helper property which is used to accumulate complexity during parsing. */ - @inline final def complexity: Int = _complexity - @inline final def complexity_=(v: Int): Unit = { - _complexity = v - } - - @inline final def addComplexity(delta: Int): Unit = { - _complexity += delta - } - - private var _wasDeserialize: Boolean = false - /** Helper property which is used to track deserialization operations during parsing. */ - @inline final def wasDeserialize: Boolean = _wasDeserialize - @inline final def wasDeserialize_=(v: Boolean): Unit = { - _wasDeserialize = v - } } diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteWriter.scala b/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteWriter.scala index f30f1130bb..802cd56e32 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteWriter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/SigmaByteWriter.scala @@ -1,124 +1,75 @@ package sigmastate.utils -import scorex.util.serialization.{VLQByteBufferWriter, Writer} -import scorex.util.serialization.Writer.Aux +import scorex.util.serialization.Writer import sigma.ast.SType -import sigmastate.ArgInfo +import sigma.serialization.CoreByteWriter +import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo, FormatDescriptor, SeqFmt} import sigmastate.Values.{SValue, Value} import sigmastate.serialization.{ConstantStore, TypeSerializer, ValueSerializer} -class SigmaByteWriter(val w: Writer, - val constantExtractionStore: Option[ConstantStore]) extends Writer { - import SigmaByteWriter._ +class SigmaByteWriter(override val w: Writer, + val constantExtractionStore: Option[ConstantStore]) + extends CoreByteWriter(w) { + import CoreByteWriter._ import ValueSerializer._ - type CH = w.CH - @inline override def length(): Int = w.length() - - @inline override def newWriter(): Aux[CH] = w.newWriter() - - @inline override def putChunk(chunk: CH): this.type = { w.putChunk(chunk); this } - - @inline override def result(): CH = w.result() - - @inline def put(x: Byte): this.type = { w.put(x); this } - @inline def put(x: Byte, info: DataInfo[Byte]): this.type = { + override def put(x: Byte, info: DataInfo[Byte]): this.type = { ValueSerializer.addArgInfo(info) w.put(x); this } - override def putUByte(x: Int): this.type = { - super.putUByte(x) - } - def putUByte(x: Int, info: DataInfo[U[Byte]]): this.type = { + override def putUByte(x: Int, info: DataInfo[U[Byte]]): this.type = { ValueSerializer.addArgInfo(info) super.putUByte(x) } - @inline def putBoolean(x: Boolean): this.type = { w.putBoolean(x); this } - @inline def putBoolean(x: Boolean, info: DataInfo[Boolean]): this.type = { + @inline override def putBoolean(x: Boolean, info: DataInfo[Boolean]): this.type = { ValueSerializer.addArgInfo(info) w.putBoolean(x); this } - @inline def putShort(x: Short): this.type = { w.putShort(x); this } - @inline def putShort(x: Short, info: DataInfo[Short]): this.type = { + @inline override def putShort(x: Short, info: DataInfo[Short]): this.type = { ValueSerializer.addArgInfo(info) w.putShort(x); this } - @inline def putUShort(x: Int): this.type = { w.putUShort(x); this } - @inline def putUShort(x: Int, info: DataInfo[Vlq[U[Short]]]): this.type = { + @inline override def putUShort(x: Int, info: DataInfo[Vlq[U[Short]]]): this.type = { ValueSerializer.addArgInfo(info) w.putUShort(x); this } - @inline def putInt(x: Int): this.type = { w.putInt(x); this } - @inline def putInt(x: Int, info: DataInfo[Int]): this.type = { + @inline override def putInt(x: Int, info: DataInfo[Int]): this.type = { ValueSerializer.addArgInfo(info) w.putInt(x); this } - @inline def putUInt(x: Long): this.type = { w.putUInt(x); this } - @inline def putUInt(x: Long, info: DataInfo[Vlq[U[Int]]]): this.type = { + @inline override def putUInt(x: Long, info: DataInfo[Vlq[U[Int]]]): this.type = { ValueSerializer.addArgInfo(info) w.putUInt(x); this } - @inline def putLong(x: Long): this.type = { w.putLong(x); this } - @inline def putLong(x: Long, info: DataInfo[Vlq[ZigZag[Long]]]): this.type = { + @inline override def putLong(x: Long, info: DataInfo[Vlq[ZigZag[Long]]]): this.type = { ValueSerializer.addArgInfo(info) w.putLong(x); this } - @inline def putULong(x: Long): this.type = { w.putULong(x); this } - @inline def putULong(x: Long, info: DataInfo[Vlq[U[Long]]]): this.type = { + @inline override def putULong(x: Long, info: DataInfo[Vlq[U[Long]]]): this.type = { ValueSerializer.addArgInfo(info) w.putULong(x); this } - override def putBytes(xs: Array[Byte], - offset: Int, - length: Int): this.type = { - w.putBytes(xs, offset, length); this - } - @inline def putBytes(xs: Array[Byte]): this.type = { w.putBytes(xs); this } - @inline def putBytes(xs: Array[Byte], info: DataInfo[Array[Byte]]): this.type = { + @inline override def putBytes(xs: Array[Byte], info: DataInfo[Array[Byte]]): this.type = { ValueSerializer.addArgInfo(info) w.putBytes(xs); this } - /** Put the two bytes of the big-endian representation of the Short value into the - * writer. */ - @inline def putShortBytes(value: Short): this.type = { - w.put((value >> 8).toByte) - w.put(value.toByte) - this - } - - @inline def putBits(xs: Array[Boolean]): this.type = { w.putBits(xs); this } - @inline def putBits(xs: Array[Boolean], info: DataInfo[Bits]): this.type = { + @inline override def putBits(xs: Array[Boolean], info: DataInfo[Bits]): this.type = { ValueSerializer.addArgInfo(info) w.putBits(xs); this } - @inline def putOption[T](x: Option[T])(putValueC: (this.type, T) => Unit): this.type = { - w.putOption(x) { (_, v) => - putValueC(this, v) - } - this - } - - @inline def putShortString(s: String): this.type = { w.putShortString(s); this } - - // TODO refactor: move to Writer - @inline def toBytes: Array[Byte] = w match { - case wr: VLQByteBufferWriter => wr.toBytes - } - - @inline def putType[T <: SType](x: T): this.type = { TypeSerializer.serialize(x, this); this } - @inline def putType[T <: SType](x: T, info: DataInfo[SType]): this.type = { + @inline override def putType[T <: SType](x: T, info: DataInfo[SType]): this.type = { ValueSerializer.addArgInfo(info) TypeSerializer.serialize(x, this); this } @@ -143,132 +94,13 @@ class SigmaByteWriter(val w: Writer, } object SigmaByteWriter { - import scala.language.implicitConversions - - /** Format descriptor type family. */ - trait FormatDescriptor[T] { - /** Size formula associated with this format */ - def size: String - } - - /** Marker type to automatically resolve correct implicit format descriptor - * in Writer methods. - * This is type-level type, since no instances of it are ever created. */ - trait Vlq[T] - - /** Marker type to automatically resolve correct implicit format descriptor - * in Writer methods. - * This is type-level type, since no instances of it are ever created. */ - trait ZigZag[T] - - /** Marker type for Unsigned types to automatically resolve correct implicit format descriptor - * in Writer methods. - * This is type-level type, since no instances of it are ever created. */ - trait U[T] - - /** Marker type for bits representation of Coll[Boolean]. - * Should be used only as argument for FormatDescriptor. - * This is type-level type, since no instances of it are ever created. - */ - trait Bits - implicit case object ValueFmt extends FormatDescriptor[SValue] { override def size: String = "[1, *]" override def toString: String = "Expr" } - implicit case object TypeFmt extends FormatDescriptor[SType] { - override def size: String = "[1, *]" - override def toString: String = "Type" - } - - case object BitsFmt extends FormatDescriptor[Bits] { - override def size: String = "[1, *]" - override def toString: String = "Bits" - } - - case class MaxBitsFmt(maxBits: Int) extends FormatDescriptor[Bits] { - override def size: String = { - val maxBytes = (maxBits - 1) / 8 + 1 - if (maxBytes == 1) "1" - else s"[1, $maxBytes]" - } - override def toString: String = "Bits" - } - - implicit object BooleanFmt extends FormatDescriptor[Boolean] { - override def size: String = "1" - override def toString: String = "Boolean" - } - implicit object ByteFmt extends FormatDescriptor[Byte] { - override def size: String = "1" - override def toString: String = "Byte" - } - implicit object ShortFmt extends FormatDescriptor[Short] { - override def size: String = "2" - override def toString: String = "Short" - } - implicit object IntFmt extends FormatDescriptor[Int] { - override def size: String = "4" - override def toString: String = "Int" - } - implicit object LongFmt extends FormatDescriptor[Long] { - override def size: String = "8" - override def toString: String = "Long" - } - - implicit object UByteFmt extends FormatDescriptor[U[Byte]] { - override def size: String = "1" - override def toString: String = "UByte" - } - implicit object UShortFmt extends FormatDescriptor[U[Short]] { - override def size: String = "2" - override def toString: String = "UShort" - } - implicit object UIntFmt extends FormatDescriptor[U[Int]] { - override def size: String = "4" - override def toString: String = "UInt" - } - implicit object ULongFmt extends FormatDescriptor[U[Long]] { - override def size: String = "8" - override def toString: String = "ULong" - } - - case class ZigZagFmt[T](fmt: FormatDescriptor[T]) extends FormatDescriptor[ZigZag[T]] { - override def size: String = s"[1, *]" - override def toString: String = s"ZigZag($fmt)" - } - case class UVlqFmt[T](fmt: FormatDescriptor[U[T]]) extends FormatDescriptor[Vlq[U[T]]] { - override def size: String = s"[1, *]" - override def toString: String = s"VLQ($fmt)" - } - case class ZigZagVlqFmt[T](fmt: FormatDescriptor[ZigZag[T]]) extends FormatDescriptor[Vlq[ZigZag[T]]] { - override def size: String = s"[1, *]" - override def toString: String = s"VLQ($fmt)" - } - case class SeqFmt[T](fmt: FormatDescriptor[T]) extends FormatDescriptor[Seq[T]] { - override def size: String = s"n * ${fmt.size}" - override def toString: String = s"Seq($fmt)" - } - - implicit def toZigZagFmt[T](implicit fmt: FormatDescriptor[T]): FormatDescriptor[ZigZag[T]] = ZigZagFmt(fmt) - implicit def toUVlqFmt[T](implicit fmt: FormatDescriptor[U[T]]): FormatDescriptor[Vlq[U[T]]] = UVlqFmt(fmt) - implicit def toZigZagVlqFmt[T](implicit fmt: FormatDescriptor[ZigZag[T]]): FormatDescriptor[Vlq[ZigZag[T]]] = ZigZagVlqFmt(fmt) - implicit def toSeqFmt[T](implicit fmt: FormatDescriptor[T]): FormatDescriptor[Seq[T]] = SeqFmt(fmt) - case class DataInfo[T](info: ArgInfo, format: FormatDescriptor[T]) - - def bitsInfo(name: String, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), BitsFmt) - def maxBitsInfo(name: String, maxBits: Int, desc: String = ""): DataInfo[Bits] = DataInfo(ArgInfo(name, desc), MaxBitsFmt(maxBits)) - - val valuesLengthInfo: DataInfo[Vlq[U[Int]]] = ArgInfo("\\#items", "number of items in the collection") def valuesItemInfo(info: DataInfo[Seq[SValue]]): DataInfo[SValue] = { val itemFmt = info.format.asInstanceOf[SeqFmt[SValue]].fmt - DataInfo(ArgInfo(info.info.name+"_i", s"i-th item in the ${info.info.description}"), itemFmt) + DataInfo(ArgInfo(info.info.name + "_i", s"i-th item in the ${info.info.description}"), itemFmt) } - - implicit def argInfoToDataInfo[T](arg: ArgInfo)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = DataInfo(arg, fmt) - - // TODO refactor: remove this conversion and make it explicit - /**Helper conversion */ - implicit def nameToDataInfo[T](name: String)(implicit fmt: FormatDescriptor[T]): DataInfo[T] = ArgInfo(name, "") -} +} \ No newline at end of file diff --git a/interpreter/shared/src/main/scala/sigmastate/utxo/transformers.scala b/interpreter/shared/src/main/scala/sigmastate/utxo/transformers.scala index 59948ef826..b1d156d904 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utxo/transformers.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utxo/transformers.scala @@ -8,6 +8,7 @@ import org.ergoplatform.ErgoBox.RegisterId import sigma.ast.SCollection.SByteArray import sigma.ast._ import sigma.data.RType +import sigma.serialization.CoreByteWriter.ArgInfo import sigmastate.Operations._ import sigmastate.eval.SigmaDsl import sigmastate.exceptions.InterpreterException diff --git a/interpreter/shared/src/test/scala/org/ergoplatform/validation/ValidationSpecification.scala b/interpreter/shared/src/test/scala/org/ergoplatform/validation/ValidationSpecification.scala index 1e30653d9b..b223355c5b 100644 --- a/interpreter/shared/src/test/scala/org/ergoplatform/validation/ValidationSpecification.scala +++ b/interpreter/shared/src/test/scala/org/ergoplatform/validation/ValidationSpecification.scala @@ -1,5 +1,7 @@ package org.ergoplatform.validation +import sigma.validation.SigmaValidationSettings + trait ValidationSpecification { implicit val vs: SigmaValidationSettings = ValidationRules.currentSettings } diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index 70f644bd66..2146bfb034 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -2,7 +2,8 @@ package sigmastate.helpers import org.ergoplatform.ErgoLikeContext.Height import org.ergoplatform._ -import org.ergoplatform.validation.{SigmaValidationSettings, ValidationRules} +import org.ergoplatform.validation.ValidationRules +import sigma.validation.SigmaValidationSettings import sigmastate.AvlTreeData import sigmastate.crypto.CryptoConstants import sigmastate.eval._ diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala index c0255b6adc..40689412cc 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala @@ -1,7 +1,6 @@ package sigmastate.helpers import org.ergoplatform.ErgoBox.{AdditionalRegisters, Token, allZerosModifierId} -import org.ergoplatform.validation.SigmaValidationSettings import org.ergoplatform._ import scorex.util.ModifierId import sigma.data.{CollOverArray, PairOfCols} @@ -10,6 +9,7 @@ import sigmastate.Values.ErgoTree import sigmastate.eval._ import sigmastate.interpreter.ContextExtension import sigma.Coll +import sigma.validation.SigmaValidationSettings import sigma.{Header, PreHeader} import scala.collection.compat.immutable.ArraySeq diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala index 8b67f7907a..08d3771e7f 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala @@ -15,7 +15,7 @@ import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps import scorex.util.encode.Base16 import sigma.ast._ -import sigmastate.exceptions.SerializerException +import sigma.serialization.SerializerException import sigmastate.lang.DeserializationSigmaBuilder import scala.annotation.nowarn diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index aa5b158472..2a31b12ecc 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -14,7 +14,7 @@ import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps import sigma.ast._ import org.scalacheck.Gen -import sigmastate.exceptions.SerializerException +import sigma.serialization.SerializerException import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator} import sigmastate.interpreter.ErgoTreeEvaluator.DefaultProfiler import sigmastate.utils.Helpers diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 5943e8913b..98672bab52 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -25,6 +25,8 @@ import sigmastate.utxo._ import sigma.Coll import sigma._ import sigma.ast._ +import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus} +import sigma.validation.ValidationRules.FirstRuleId import java.math.BigInteger import scala.collection.compat.immutable.ArraySeq diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala index ac54f1d84f..0ded1dbb5a 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala @@ -18,6 +18,7 @@ import sigmastate.exceptions.SerializerException import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable import sigma.ast._ +import sigma.serialization.SerializerException import sigmastate.serialization.SigmaSerializer import sigmastate.serialization.DataSerializer import sigmastate.serialization.ErgoTreeSerializer From 01190bbc9b381afa21784ec71437e1ee49918503 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 11:25:04 +0200 Subject: [PATCH 03/34] sigma-ast: AvlTreeData moved to core --- .../src/main/scala/sigma/crypto/package.scala | 9 +++++++++ .../main/scala/sigma/data}/AvlTreeData.scala | 18 ++++++++---------- .../src/main/scala/sigmastate/Platform.scala | 2 +- .../scala/sigmastate/crypto/Platform.scala | 10 +++++----- .../src/main/scala/sigmastate/Platform.scala | 2 +- .../org/ergoplatform/ErgoLikeContext.scala | 2 +- .../org/ergoplatform/dsl/AvlTreeHelpers.scala | 2 +- .../sigmastate/crypto/CryptoConstants.scala | 6 ------ .../sigmastate/eval/CostingDataContext.scala | 2 +- .../main/scala/sigmastate/eval/package.scala | 2 +- .../serialization/DataSerializer.scala | 2 +- .../src/main/scala/sigmastate/sigmastate.scala | 2 +- .../helpers/ErgoLikeContextTesting.scala | 4 ++-- .../sigmastate/helpers/TestingHelpers.scala | 8 +++----- .../serialization/AvlTreeSpecification.scala | 5 ++--- .../SigSerializerSpecification.scala | 1 + .../generators/ObjectGenerators.scala | 6 +++--- .../scala/special/sigma/ContractsTestkit.scala | 4 ++-- .../scala/special/sigma/SigmaTestingData.scala | 2 +- 19 files changed, 44 insertions(+), 45 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/crypto/package.scala rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma/data}/AvlTreeData.scala (85%) diff --git a/core/shared/src/main/scala/sigma/crypto/package.scala b/core/shared/src/main/scala/sigma/crypto/package.scala new file mode 100644 index 0000000000..5697fce9fe --- /dev/null +++ b/core/shared/src/main/scala/sigma/crypto/package.scala @@ -0,0 +1,9 @@ +package sigma + +package object crypto { + /** Length of hash function used in the signature scheme. Blake2b hash function is used. */ + val hashLengthBits = 256 + + /** Length of hash in bytes. */ + val hashLength: Int = hashLengthBits / 8 +} diff --git a/interpreter/shared/src/main/scala/sigmastate/AvlTreeData.scala b/core/shared/src/main/scala/sigma/data/AvlTreeData.scala similarity index 85% rename from interpreter/shared/src/main/scala/sigmastate/AvlTreeData.scala rename to core/shared/src/main/scala/sigma/data/AvlTreeData.scala index 83f9b756c7..bddb0e5c44 100644 --- a/interpreter/shared/src/main/scala/sigmastate/AvlTreeData.scala +++ b/core/shared/src/main/scala/sigma/data/AvlTreeData.scala @@ -1,9 +1,7 @@ -package sigmastate +package sigma.data -import sigmastate.crypto.CryptoConstants -import sigmastate.serialization.SigmaSerializer -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigma.{Coll, Colls} +import sigma.serialization.{CoreByteReader, CoreByteWriter, CoreSerializer} +import sigma.{Coll, Colls, crypto} case class AvlTreeFlags(insertAllowed: Boolean, updateAllowed: Boolean, removeAllowed: Boolean) { @@ -56,7 +54,7 @@ case class AvlTreeData(digest: Coll[Byte], valueLengthOpt: Option[Int] = None) object AvlTreeData { - val DigestSize: Int = CryptoConstants.hashLength + 1 //please read class comments above for details + val DigestSize: Int = crypto.hashLength + 1 //please read class comments above for details val TreeDataSize = DigestSize + 3 + 4 + 4 val dummy = new AvlTreeData( @@ -67,12 +65,12 @@ object AvlTreeData { /** Create [[AvlTreeData]] with the given digest and all operations enabled. */ def avlTreeFromDigest(digest: Coll[Byte]): AvlTreeData = { val flags = AvlTreeFlags(insertAllowed = true, updateAllowed = true, removeAllowed = true) - AvlTreeData(digest, flags, CryptoConstants.hashLength) + AvlTreeData(digest, flags, crypto.hashLength) } - object serializer extends SigmaSerializer[AvlTreeData, AvlTreeData] { + object serializer extends CoreSerializer[AvlTreeData, AvlTreeData] { - override def serialize(data: AvlTreeData, w: SigmaByteWriter): Unit = { + override def serialize(data: AvlTreeData, w: CoreByteWriter): Unit = { val tf = AvlTreeFlags.serializeFlags(data.treeFlags) w.putBytes(data.digest.toArray) .putUByte(tf) @@ -80,7 +78,7 @@ object AvlTreeData { .putOption(data.valueLengthOpt)(_.putUInt(_)) } - override def parse(r: SigmaByteReader): AvlTreeData = { + override def parse(r: CoreByteReader): AvlTreeData = { val digest = r.getBytes(DigestSize) val tf = AvlTreeFlags(r.getByte()) val keyLength = r.getUInt().toInt diff --git a/interpreter/js/src/main/scala/sigmastate/Platform.scala b/interpreter/js/src/main/scala/sigmastate/Platform.scala index 07d11d81c0..19fc7c1161 100644 --- a/interpreter/js/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/js/src/main/scala/sigmastate/Platform.scala @@ -1,7 +1,7 @@ package sigmastate import org.ergoplatform.ErgoBox -import sigma.data.Nullable +import sigma.data.{AvlTreeData, Nullable} import sigma.{Evaluation, VersionContext} import sigmastate.Values.{Constant, FalseLeaf, SigmaBoolean, TrueLeaf} import sigmastate.eval.SigmaDsl diff --git a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala b/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala index 5116af38a9..0d713270c3 100644 --- a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala +++ b/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala @@ -185,28 +185,28 @@ object Platform { private val ctx = new CryptoContextJs /** The underlying elliptic curve descriptor. */ - override def curve: crypto.Curve = ??? + override def curve: Curve = ??? override def fieldCharacteristic: BigInteger = Convert.bigIntToBigInteger(ctx.getModulus()) override def order: BigInteger = Convert.bigIntToBigInteger(ctx.getOrder()) - override def validatePoint(x: BigInteger, y: BigInteger): crypto.Ecp = { + override def validatePoint(x: BigInteger, y: BigInteger): Ecp = { val point = ctx.validatePoint(Convert.bigIntegerToBigInt(x), Convert.bigIntegerToBigInt(y)) new Ecp(point) } - override def infinity(): crypto.Ecp = + override def infinity(): Ecp = new Ecp(ctx.getInfinity()) - override def decodePoint(encoded: Array[Byte]): crypto.Ecp = { + override def decodePoint(encoded: Array[Byte]): Ecp = { if (encoded(0) == 0) { return infinity() } new Ecp(ctx.decodePoint(Base16.encode(encoded))) } - override def generator: crypto.Ecp = + override def generator: Ecp = new Ecp(ctx.getGenerator()) } diff --git a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala index a6e9f6a580..af510785d8 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala @@ -1,7 +1,7 @@ package sigmastate import org.ergoplatform.ErgoBox -import sigma.data.Nullable +import sigma.data.{AvlTreeData, Nullable} import sigma.{Evaluation, VersionContext} import sigmastate.Values.{Constant, FalseLeaf, SigmaBoolean, TrueLeaf} import sigmastate.eval.SigmaDsl diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 8bf6ff9ff0..32de8630a9 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -13,7 +13,7 @@ import sigma.{AnyValue, Header, PreHeader} import debox.cfor import sigma.ast.{SBox, SCollection, SContext, SFunc, SGlobal, SInt, SType, SUnit} import sigma.ast.SType.{AnyOps, TypeCode} -import sigma.data.SigmaConstants +import sigma.data.{AvlTreeData, SigmaConstants} import sigma.validation.SigmaValidationSettings import sigmastate.serialization.ValueCodes.OpCode diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala index 627b0a3f54..f12f087028 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala @@ -4,10 +4,10 @@ import sigma.Coll import sigmastate.eval.{CAvlTree, CostingSigmaDslBuilder} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Digest32, Blake2b256} -import sigmastate.{AvlTreeData, AvlTreeFlags} import sigma.AvlTree import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert} import CostingSigmaDslBuilder.Colls +import sigma.data.{AvlTreeData, AvlTreeFlags} object AvlTreeHelpers { diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala index 59525bec7e..46d2424f6e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala @@ -25,12 +25,6 @@ object CryptoConstants { /** Group order, i.e. number of elements in the group */ val groupOrder: BigInteger = dlogGroup.order - /** Length of hash function used in the signature scheme. Blake2b hash function is used. */ - val hashLengthBits = 256 - - /** Length of hash in bytes. */ - val hashLength: Int = hashLengthBits / 8 - /** A size of challenge in Sigma protocols, in bits. * If this anything but 192, threshold won't work, because we have polynomials over GF(2^192) and no others. * So DO NOT change the value without implementing polynomials over GF(2^soundnessBits) first diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 2a04240ed4..687ff7b6e6 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -11,7 +11,7 @@ import sigma.{VersionContext, _} import sigma.ast.SCollection.SByteArray import sigma.ast.{SInt, STuple, SType} import sigma.data.OverloadHack.Overloaded1 -import sigma.data.{CBigInt, RType, SigmaConstants, WrapperOf} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType, SigmaConstants, WrapperOf} import sigma.util.Extensions.BigIntegerOps import sigma.validation.SigmaValidationSettings import sigmastate.Values.ErgoTree.EmptyConstants diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index 65b187c480..f02b722c8a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -2,7 +2,7 @@ package sigmastate import java.math.BigInteger import org.ergoplatform.ErgoBox -import sigma.data.RType +import sigma.data.{AvlTreeData, RType} import scorex.crypto.hash.Digest32 import sigmastate.Values.SigmaBoolean import sigmastate.crypto.CryptoConstants.EcPointType diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 1bf8c5e2dc..bfe2737442 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -3,7 +3,7 @@ package sigmastate.serialization import java.math.BigInteger import java.nio.charset.StandardCharsets import org.ergoplatform.ErgoBox -import sigma.data.RType +import sigma.data.{AvlTreeData, RType} import sigmastate.Values.SigmaBoolean import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate._ diff --git a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala index 275ae5a41d..d1a547bbc0 100644 --- a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala +++ b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala @@ -1,6 +1,6 @@ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoLikeContext} import sigma.ast._ -import sigma.data.{GeneralType, RType} +import sigma.data.{AvlTreeData, GeneralType, RType} import sigmastate.Values._ import sigmastate.lang.{CheckingSigmaBuilder, Terms} diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index 2146bfb034..6dfc62b55a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -3,14 +3,14 @@ package sigmastate.helpers import org.ergoplatform.ErgoLikeContext.Height import org.ergoplatform._ import org.ergoplatform.validation.ValidationRules +import sigma.data.AvlTreeData import sigma.validation.SigmaValidationSettings -import sigmastate.AvlTreeData +import sigma.{Box, Coll, Colls, Header, PreHeader} import sigmastate.crypto.CryptoConstants import sigmastate.eval._ import sigmastate.interpreter.ContextExtension import sigmastate.interpreter.ErgoTreeEvaluator.DefaultEvalSettings import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer} -import sigma.{Box, Coll, Colls, Header, PreHeader} object ErgoLikeContextTesting { /* NO HF PROOF: diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala index 40689412cc..c3e9c2e920 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala @@ -3,14 +3,12 @@ package sigmastate.helpers import org.ergoplatform.ErgoBox.{AdditionalRegisters, Token, allZerosModifierId} import org.ergoplatform._ import scorex.util.ModifierId -import sigma.data.{CollOverArray, PairOfCols} -import sigmastate.AvlTreeData +import sigma.data.{AvlTreeData, CollOverArray, PairOfCols} +import sigma.validation.SigmaValidationSettings +import sigma.{Coll, Header, PreHeader} import sigmastate.Values.ErgoTree import sigmastate.eval._ import sigmastate.interpreter.ContextExtension -import sigma.Coll -import sigma.validation.SigmaValidationSettings -import sigma.{Header, PreHeader} import scala.collection.compat.immutable.ArraySeq diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala index 8a0b4b9255..0dda4b81d2 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala @@ -1,12 +1,11 @@ package sigmastate.serialization -import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert} +import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values.AvlTreeConstant -import sigmastate.AvlTreeFlags -import sigmastate._ import sigmastate.eval.Extensions._ import sigmastate.eval._ diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index 52aaa89070..c9728478dd 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -4,6 +4,7 @@ import java.math.BigInteger import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} import org.scalatest.Assertion +import sigma.data.AvlTreeData import sigmastate.Values.SigmaBoolean import sigmastate._ import sigmastate.crypto.DLogProtocol.{ProveDlog, SecondDLogProverMessage} diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 98672bab52..a0bf169f31 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -8,7 +8,7 @@ import org.scalacheck.Arbitrary._ import org.scalacheck.Gen.{choose, frequency} import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.RType +import sigma.data.{AvlTreeData, AvlTreeFlags, RType} import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.encode.{Base58, Base64} import scorex.util.{ModifierId, bytesToId} @@ -332,11 +332,11 @@ trait ObjectGenerators extends TypeGenerators } yield mkTuple(values).asInstanceOf[Tuple] lazy val modifierIdGen: Gen[ModifierId] = - arrayOfN(CryptoConstants.hashLength, arbByte.arbitrary) + arrayOfN(sigma.crypto.hashLength, arbByte.arbitrary) .map(bytesToId) lazy val modifierIdBytesGen: Gen[Coll[Byte]] = - collOfN(CryptoConstants.hashLength, arbByte.arbitrary) + collOfN(sigma.crypto.hashLength, arbByte.arbitrary) val MaxTokens = 10 diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index a1226b1d03..6cc938c810 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -1,8 +1,8 @@ package sigma -import sigma.data.RType +import sigma.data.{AvlTreeData, RType} import sigmastate.Values.ErgoTree -import sigmastate.{AvlTreeData, Values} +import sigmastate.Values import sigmastate.eval._ import sigmastate.eval.Extensions._ import sigmastate.helpers.TestingHelpers._ diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 979634f36c..9a05a24567 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -6,7 +6,7 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.containerOfN import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{CBigInt, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId From f5d01ff09d21869049633cea1e18bc9e8a7a33ea Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 12:46:47 +0200 Subject: [PATCH 04/34] sigma-ast: everything compiles --- build.sbt | 22 +++++++++---------- .../sigmastate/helpers/SigmaPPrintSpec.scala | 2 +- .../org/ergoplatform/dsl/ContractSyntax.scala | 3 +-- .../scala/sigmastate/eval/GraphBuilding.scala | 3 ++- .../ErgoAddressSpecification.scala | 7 ++++-- .../org/ergoplatform/ErgoTreePredefSpec.scala | 1 + .../ergoplatform/dsl/TestContractSpec.scala | 7 +++--- .../validation/RuleStatusSerializerSpec.scala | 3 ++- ...igmaValidationSettingsSerializerSpec.scala | 4 +++- .../scala/sigma/SigmaDslSpecification.scala | 2 +- .../test/scala/sigma/SigmaDslTesting.scala | 6 +++-- .../sigmastate/ErgoTreeSpecification.scala | 6 +++-- .../scala/sigmastate/FailingToProveSpec.scala | 1 + .../SoftForkabilitySpecification.scala | 6 ++++- .../TestingInterpreterSpecification.scala | 1 + .../sigmastate/eval/ErgoScriptTestkit.scala | 3 ++- .../helpers/CompilerTestingCommons.scala | 5 +++-- .../sigmastate/helpers/SigmaPPrint.scala | 2 +- .../DeserializationResilience.scala | 9 ++++---- .../ErgoTreeSerializerSpecification.scala | 4 ++-- .../test/scala/sigmastate/utils/SpecGen.scala | 1 + .../utxo/AVLTreeScriptsSpecification.scala | 1 + .../utxo/BasicOpsSpecification.scala | 2 +- .../CollectionOperationsSpecification.scala | 1 + .../utxo/ComplexSigSpecification.scala | 1 + .../ErgoLikeInterpreterSpecification.scala | 5 +++-- .../utxo/ThresholdSpecification.scala | 1 + .../BlockchainSimulationTestingCommons.scala | 3 ++- .../AtomicSwapExampleSpecification.scala | 1 + ...alletAdvContractExampleSpecification.scala | 3 ++- ...ldWalletContractExampleSpecification.scala | 7 +++--- .../examples/CoopExampleSpecification.scala | 3 ++- .../DHTupleExampleSpecification.scala | 3 ++- .../DemurrageExampleSpecification.scala | 3 ++- .../examples/FsmExampleSpecification.scala | 1 + .../sigmastate/utxo/examples/IcoExample.scala | 1 + .../utxo/examples/LetsSpecification.scala | 3 ++- .../examples/MASTExampleSpecification.scala | 1 + .../examples/MixExampleSpecification.scala | 3 ++- .../OracleExamplesSpecification.scala | 1 + .../RPSGameExampleSpecification.scala | 3 ++- .../ReversibleTxExampleSpecification.scala | 3 ++- .../utxo/examples/Rule110Specification.scala | 1 + .../TimedPaymentExampleSpecification.scala | 3 ++- .../XorGameExampleSpecification.scala | 3 ++- .../scala/org/ergoplatform/sdk/js/Isos.scala | 3 +-- .../sdk/AppkitProvingInterpreter.scala | 4 ++-- .../ergoplatform/sdk/ContractTemplate.scala | 2 +- .../ergoplatform/sdk/DataJsonEncoder.scala | 1 - .../org/ergoplatform/sdk/JsonCodecs.scala | 10 ++++----- .../sdk/ReducingInterpreter.scala | 2 +- .../org/ergoplatform/sdk/utils/Zero.scala | 4 ++-- .../sdk/DataJsonEncoderSpecification.scala | 2 +- .../sdk/JsonSerializationSpec.scala | 5 +++-- 54 files changed, 115 insertions(+), 73 deletions(-) diff --git a/build.sbt b/build.sbt index a76c73d885..26d0fdcb27 100644 --- a/build.sbt +++ b/build.sbt @@ -212,6 +212,17 @@ lazy val core = crossProject(JVMPlatform, JSPlatform) ) lazy val coreJS = core.js .enablePlugins(ScalaJSBundlerPlugin) + .enablePlugins(ScalablyTypedConverterGenSourcePlugin) + .settings( + stOutputPackage := "sigmastate", + scalaJSLinkerConfig ~= { conf => + conf.withSourceMap(false) + }, + Compile / npmDependencies ++= Seq( + "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion, + "@fleet-sdk/common" -> "0.1.3" + ) + ) lazy val interpreter = crossProject(JVMPlatform, JSPlatform) .in(file("interpreter")) @@ -233,17 +244,6 @@ lazy val interpreter = crossProject(JVMPlatform, JSPlatform) ) lazy val interpreterJS = interpreter.js .enablePlugins(ScalaJSBundlerPlugin) - .enablePlugins(ScalablyTypedConverterGenSourcePlugin) - .settings( - stOutputPackage := "sigmastate", - scalaJSLinkerConfig ~= { conf => - conf.withSourceMap(false) - }, - Compile / npmDependencies ++= Seq( - "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion, - "@fleet-sdk/common" -> "0.1.3" - ) - ) lazy val parsers = crossProject(JVMPlatform, JSPlatform) .in(file("parsers")) diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index 895b0fbe53..cd50653365 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -2,7 +2,7 @@ package sigmastate.helpers import org.ergoplatform.settings.ErgoAlgos import org.ergoplatform.{ErgoBox, Outputs} -import sigma.data.{CollType, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, RType} import scorex.util.ModifierId import sigmastate.Values._ import sigmastate._ diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala index 10f702579c..a063554063 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala @@ -2,7 +2,7 @@ package org.ergoplatform.dsl import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.TokenId -import sigma.data.RType +import sigma.data.{AvlTreeData, RType} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition} import org.ergoplatform.sdk.JavaHelpers.collRType import sigmastate.eval.CostingSigmaDslBuilder @@ -10,7 +10,6 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigma._ import sigma.ast.SType import sigma.ast.SType.AnyOps -import sigmastate.AvlTreeData import sigmastate.Values.SigmaBoolean import scala.reflect.ClassTag diff --git a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala index d323225b8d..c7865364e0 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala @@ -2,6 +2,7 @@ package sigmastate.eval import org.ergoplatform._ import scalan.MutableLazy +import sigma.SigmaException import sigma.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIsExactIntegral, ShortIsExactIntegral} import sigma.data.ExactOrdering.{ByteIsExactOrdering, IntIsExactOrdering, LongIsExactOrdering, ShortIsExactOrdering} import sigma.util.Extensions.ByteOps @@ -15,7 +16,7 @@ import sigmastate.serialization.{OpCodes, ValueCodes} import sigmastate.utxo._ import sigma.ast._ import sigmastate.crypto.CryptoConstants.EcPointType -import sigmastate.exceptions.{GraphBuildingException, SigmaException} +import sigmastate.exceptions.GraphBuildingException import scala.collection.mutable.ArrayBuffer diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 26cf0cf82c..85906ab1a6 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -1,7 +1,7 @@ package org.ergoplatform import org.ergoplatform.ErgoAddressEncoder.{MainnetNetworkPrefix, TestnetNetworkPrefix, hash256} -import org.ergoplatform.validation.{ValidationException, ValidationRules} +import org.ergoplatform.validation.ValidationRules import org.scalatest.{Assertion, TryValues} import scorex.crypto.hash.Blake2b256 import scorex.util.encode.Base58 @@ -23,6 +23,9 @@ import sigmastate.utils.Helpers._ import sigmastate.{CompilerCrossVersionProps, SigmaAnd} import sigma.SigmaDslTesting import sigma.ast.SType +import sigma.validation.ValidationException +import sigma.validation.ValidationRules.CheckTypeCode + import java.math.BigInteger class ErgoAddressSpecification extends SigmaDslTesting @@ -194,7 +197,7 @@ class ErgoAddressSpecification extends SigmaDslTesting val unparsedTree = new ErgoTree( (ErgoTree.ConstantSegregationHeader | ergoTreeHeaderInTests).toByte, Array[Constant[SType]](), - Left(UnparsedErgoTree(Array[Byte](), ValidationException("", ValidationRules.CheckTypeCode, Seq()))) + Left(UnparsedErgoTree(Array[Byte](), ValidationException("", CheckTypeCode, Seq()))) ) assertExceptionThrown( ergoAddressEncoder.fromProposition(unparsedTree).getOrThrow, diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index 33a0eb1d89..fca6b2cf09 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -9,6 +9,7 @@ import scorex.crypto.hash.Blake2b256 import scorex.util.Random import sigma.Colls import sigma.ast.SByte +import sigma.data.AvlTreeData import sigma.util.BenchmarkUtil.measure import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index 5a17f01448..cbd9124e05 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -7,16 +7,15 @@ import sigmastate.interpreter.{CostedProverResult, ProverResult} import scala.collection.mutable.ArrayBuffer import org.ergoplatform.ErgoBox.{NonMandatoryRegisterId, TokenId} -import sigma.data.Nullable +import sigma.data.{AvlTreeData, Nullable} import scala.util.Try import org.ergoplatform.{ErgoBox, ErgoLikeContext} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import sigma.ast.SType -import sigmastate.AvlTreeData import sigmastate.Values.{ErgoTree, EvaluatedValue} -import sigmastate.eval.{CSigmaProp, IRContext, CAnyValue} -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} +import sigmastate.eval.{CAnyValue, CSigmaProp, IRContext} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps import sigma.{AnyValue, Evaluation, SigmaProp} diff --git a/sc/shared/src/test/scala/org/ergoplatform/validation/RuleStatusSerializerSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/validation/RuleStatusSerializerSpec.scala index 711827fe1e..972a34f017 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/validation/RuleStatusSerializerSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/validation/RuleStatusSerializerSpec.scala @@ -1,8 +1,9 @@ package org.ergoplatform.validation import org.scalatest.Assertion +import sigma.validation.{ReplacedRule, RuleStatus} import sigmastate.helpers.CompilerTestingCommons -import sigmastate.serialization.{SigmaSerializer, SerializationSpecification} +import sigmastate.serialization.{SerializationSpecification, SigmaSerializer} class RuleStatusSerializerSpec extends SerializationSpecification with CompilerTestingCommons { diff --git a/sc/shared/src/test/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializerSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializerSpec.scala index ee4ec75c3c..c4fef501aa 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializerSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/validation/SigmaValidationSettingsSerializerSpec.scala @@ -1,7 +1,9 @@ package org.ergoplatform.validation -import org.ergoplatform.validation.ValidationRules.{FirstRuleId, currentSettings} +import org.ergoplatform.validation.ValidationRules.currentSettings import org.scalatest.Assertion +import sigma.validation.{DisabledRule, SigmaValidationSettings} +import sigma.validation.ValidationRules.FirstRuleId import sigmastate.helpers.CompilerTestingCommons import sigmastate.serialization.SerializationSpecification diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index af14fca2a9..a8f2daf7ed 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4,7 +4,7 @@ import java.math.BigInteger import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{CBigInt, ExactIntegral, ExactNumeric, ExactOrdering, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, ExactIntegral, ExactNumeric, ExactOrdering, RType} import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index d95cc68fba..396fb1132f 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -3,8 +3,7 @@ package sigma import debox.cfor import org.ergoplatform._ import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, TestContractSpec} -import org.ergoplatform.validation.ValidationRules.CheckSerializableTypeCode -import org.ergoplatform.validation.{SigmaValidationSettings, ValidationException, ValidationRules} +import org.ergoplatform.validation.ValidationRules import org.scalacheck.Arbitrary._ import org.scalacheck.Gen.frequency import org.scalacheck.{Arbitrary, Gen} @@ -34,7 +33,10 @@ import sigmastate.serialization.generators.ObjectGenerators import sigmastate.utils.Helpers._ import sigmastate.utxo.{DeserializeContext, DeserializeRegister, GetVar, OptionGet} import sigma.ast.{SOption, SSigmaProp, SType} +import sigma.validation.ValidationRules.CheckSerializableTypeCode +import sigma.validation.{SigmaValidationSettings, ValidationException} import sigmastate.{SigmaLeaf, eval} + import scala.collection.mutable import scala.reflect.ClassTag import scala.util.{Failure, Success, Try} diff --git a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala index be3dc1aa08..349409c574 100644 --- a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala @@ -1,7 +1,7 @@ package sigmastate import org.ergoplatform.settings.ErgoAlgos -import org.ergoplatform.validation.{ValidationException, ValidationRules} +import org.ergoplatform.validation.ValidationRules import org.ergoplatform.{ErgoAddressEncoder, ErgoBox, ErgoLikeContext, Self} import sigma.data.RType.asType import sigma.data.{Nullable, RType} @@ -21,6 +21,8 @@ import sigmastate.utils.Helpers.TryOps import sigmastate.utxo._ import sigma._ import sigma.ast._ +import sigma.validation.ValidationException +import sigma.validation.ValidationRules.CheckTypeCode import sigma.{ContractsTestkit, SigmaDslTesting} import sigmastate.SCollectionMethods.checkValidFlatmap @@ -69,7 +71,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit { val t = new ErgoTree( 16.toByte, Array(IntConstant(1)), - Left(UnparsedErgoTree(t1.bytes, ValidationException("", ValidationRules.CheckTypeCode, Seq()))) + Left(UnparsedErgoTree(t1.bytes, ValidationException("", CheckTypeCode, Seq()))) ) assertExceptionThrown( t.toProposition(true), diff --git a/sc/shared/src/test/scala/sigmastate/FailingToProveSpec.scala b/sc/shared/src/test/scala/sigmastate/FailingToProveSpec.scala index 775785b709..29e57bace7 100644 --- a/sc/shared/src/test/scala/sigmastate/FailingToProveSpec.scala +++ b/sc/shared/src/test/scala/sigmastate/FailingToProveSpec.scala @@ -4,6 +4,7 @@ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingIn import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ import org.scalatest.TryValues._ +import sigma.data.AvlTreeData import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import scala.util.Success diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index b5680ec507..41ce087649 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -6,9 +6,13 @@ import org.ergoplatform.validation._ import org.scalatest.BeforeAndAfterAll import sigma.ast.{SBoolean, SCollection, SContext, SFunc, SGlobal, SInt} import sigma.ast.SPrimType.MaxPrimTypeCode +import sigma.data.AvlTreeData +import sigma.serialization.SerializerException +import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckSerializableTypeCode, CheckTypeCode, CheckTypeWithMethods, trySoftForkable} +import sigma.validation.{ChangedRule, ReplacedRule, SigmaValidationSettings, ValidationException, ValidationRule} import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{ByteArrayConstant, ErgoTree, IntConstant, NotReadyValueInt, UnparsedErgoTree, ValueCompanion} -import sigmastate.exceptions.{InterpreterException, SerializerException} +import sigmastate.exceptions.InterpreterException import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv diff --git a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala index 2398a48535..1be026724b 100644 --- a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala @@ -9,6 +9,7 @@ import sigmastate.lang.Terms._ import org.ergoplatform._ import org.scalatest.BeforeAndAfterAll import scorex.util.encode.Base58 +import sigma.data.AvlTreeData import sigma.util.Extensions.IntOps import sigmastate.crypto.CryptoConstants import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala index c84ab8748f..231545d16b 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala @@ -6,6 +6,7 @@ import org.ergoplatform.{Context => _, _} import scalan.BaseCtxTests import sigma.VersionContext import sigma.ast.SType +import sigma.data.AvlTreeData import sigmastate.Values.{BigIntArrayConstant, EvaluatedValue, SValue, SigmaPropConstant, Value} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting} @@ -14,7 +15,7 @@ import sigmastate.interpreter.{ContextExtension, ErgoTreeEvaluator} import sigmastate.lang.Terms.ValueOps import sigmastate.lang.{CompilerResult, CompilerSettings, LangTests, SigmaCompiler} import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.{AvlTreeData, CompilerTestsBase} +import sigmastate.CompilerTestsBase import sigma.{ContractsTestkit, Context => DContext} import scala.annotation.unused diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 2f49d5a0be..37e5784292 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -1,8 +1,7 @@ package sigmastate.helpers import org.ergoplatform._ -import org.ergoplatform.validation.ValidationRules.CheckSerializableTypeCode -import org.ergoplatform.validation.{ValidationException, ValidationSpecification} +import org.ergoplatform.validation.ValidationSpecification import org.scalacheck.Arbitrary.arbByte import org.scalacheck.Gen import org.scalatest.Assertion @@ -11,6 +10,8 @@ import scalan.TestContexts import sigma.ast.{SOption, SType} import sigma.{Colls, Evaluation, TestUtils} import sigma.data.RType +import sigma.validation.ValidationException +import sigma.validation.ValidationRules.CheckSerializableTypeCode import sigmastate.Values.{Constant, ErgoTree, SValue, SigmaBoolean, SigmaPropValue} import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index ef92f62d51..1fda430646 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -6,7 +6,7 @@ import org.ergoplatform.settings.ErgoAlgos import pprint.{PPrinter, Tree} import sigma.ast.SCollection.{SBooleanArray, SByteArray, SByteArray2} import sigma.ast._ -import sigma.data.{CollType, PrimitiveType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, PrimitiveType} import sigma.{Coll, GroupElement} import sigmastate.Values.{ConstantNode, ErgoTree, FuncValue, ValueCompanion} import sigmastate._ diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index a146dcd00e..dd55f00854 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -1,7 +1,5 @@ package sigmastate.serialization -import org.ergoplatform.validation.ValidationException -import org.ergoplatform.validation.ValidationRules.CheckPositionLimit import org.ergoplatform.{ErgoBoxCandidate, Outputs} import org.scalacheck.Gen import sigma.util.BenchmarkUtil @@ -10,17 +8,20 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.serialization.{Reader, VLQByteBufferReader} import sigma.ast.{SBoolean, SInt} +import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigma.serialization.{DeserializeCallDepthExceeded, InvalidTypePrefix, ReaderPositionLimitExceeded, SerializerException} import sigma.{Colls, Environment} import sigmastate.Values.{BlockValue, GetVarInt, IntConstant, SValue, SigmaBoolean, SigmaPropValue, Tuple, ValDef, ValUse} import sigmastate._ import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions._ import sigmastate.eval._ -import sigmastate.exceptions.{DeserializeCallDepthExceeded, InvalidTypePrefix, ReaderPositionLimitExceeded, SerializerException} import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.{ContextExtension, CostedProverResult} import sigmastate.serialization.OpCodes._ import sigma.util.safeNewArray +import sigma.validation.ValidationException +import sigma.validation.ValidationRules.CheckPositionLimit import sigmastate.utils.Helpers._ import sigmastate.utils.SigmaByteReader import sigmastate.utxo.SizeOf @@ -138,7 +139,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { { case SerializerException(_, Some(ValidationException(_,CheckPositionLimit,_, - Some(_: ReaderPositionLimitExceeded)))) => true + Some(_: ReaderPositionLimitExceeded))), _) => true case _ => false }) case _ => diff --git a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala index 2ccabd953a..a9bc820806 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala @@ -2,14 +2,14 @@ package sigmastate.serialization import java.math.BigInteger import org.ergoplatform.ErgoBox -import org.ergoplatform.validation.ValidationException import org.ergoplatform.validation.ValidationRules.CheckDeserializedScriptIsSigmaProp import sigma.ast.SInt import sigma.data.CBigInt +import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} +import sigma.validation.ValidationException import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, ErgoTree, IntConstant, ShortConstant, SigmaPropValue, UnparsedErgoTree} import sigmastate._ import sigmastate.eval.IRContext -import sigmastate.exceptions.{ReaderPositionLimitExceeded, SerializerException} import sigmastate.helpers.CompilerTestingCommons import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.utxo.{DeserializeContext, DeserializeRegister} diff --git a/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala b/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala index 09b45220e9..8b57320b91 100644 --- a/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala +++ b/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala @@ -4,6 +4,7 @@ import sigma.util.PrintExtensions.IterableExtensions import sigmastate._ import sigma.Evaluation._ import sigma.ast.{SBigInt, SBoolean, SCollection, SEmbeddable, SGlobal, SGroupElement, SNumericType, SOption, SPrimType, SString, SType, STypeCompanion} +import sigma.serialization.CoreByteWriter.ArgInfo import sigma.util.Extensions.ByteOps import sigma.util.CollectionUtil import sigma.util.PrintExtensions._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index 769bf25423..452e40c957 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -19,6 +19,7 @@ import sigmastate.interpreter.ProverResult import sigmastate.lang.Terms._ import sigma.Coll import sigma.ast.SAvlTree +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigma.{AvlTree, Context} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala index a409e98fe1..68a48d4e95 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala @@ -3,7 +3,7 @@ package sigmastate.utxo import java.math.BigInteger import org.ergoplatform.ErgoBox.{AdditionalRegisters, R6, R8} import org.ergoplatform._ -import sigma.data.RType +import sigma.data.{AvlTreeData, RType} import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigmastate._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala index 5696163fc1..24a4c27636 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala @@ -8,6 +8,7 @@ import sigmastate.lang.Terms._ import org.ergoplatform._ import sigma.ast.SCollection._ import sigma.ast._ +import sigma.data.AvlTreeData import sigmastate.SCollectionMethods.{FlatMapMethod, IndexOfMethod, IndicesMethod, PatchMethod, UpdateManyMethod, UpdatedMethod} import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.serialization.OpCodes._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala index 89e734a6e8..bf2d793b28 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala @@ -2,6 +2,7 @@ package sigmastate.utxo import org.ergoplatform.Height import org.scalacheck.Gen +import sigma.data.AvlTreeData import sigmastate.Values.IntConstant import sigmastate._ import sigmastate.helpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index be105d3b8a..b36ee92fcb 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -3,13 +3,14 @@ package sigmastate.utxo import scorex.utils.Bytes import org.ergoplatform.ErgoBox.R4 import org.ergoplatform._ -import org.ergoplatform.validation.ValidationException import org.scalatest.TryValues._ import scorex.crypto.hash.Blake2b256 import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigmastate._ import sigma.ast._ +import sigma.data.AvlTreeData +import sigma.validation.ValidationException import sigmastate.eval._ import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter._ @@ -20,7 +21,7 @@ import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.ContextExtension.VarBinding import sigmastate.interpreter.{ContextExtension, CostedProverResult} import sigmastate.lang.Terms._ -import sigmastate.serialization.{ValueSerializer, SerializationSpecification} +import sigmastate.serialization.{SerializationSpecification, ValueSerializer} import sigmastate.utils.Helpers._ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala index ea1ed7dcc3..c8567a0ba0 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala @@ -1,6 +1,7 @@ package sigmastate.utxo import sigma.ast.SSigmaProp +import sigma.data.AvlTreeData import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} import sigmastate.Values.{ConcreteCollection, FalseLeaf, IntConstant, SigmaPropConstant, SigmaPropValue, TrueLeaf} import sigmastate._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala index e9cf82616b..faa95b7e84 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala @@ -5,7 +5,7 @@ import org.ergoplatform._ import scorex.crypto.authds.{ADDigest, ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert, Remove} import scorex.crypto.hash.{Blake2b256, Digest32} -import sigmastate.{AvlTreeData, AvlTreeFlags, Values} +import sigmastate.Values import sigmastate.Values.{ErgoTree, LongConstant} import sigmastate.eval._ import sigmastate.helpers.{BlockchainState, CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestProvingInterpreter, ErgoTransactionValidator} @@ -16,6 +16,7 @@ import scala.collection.mutable import scala.util.{Random, Try} import scorex.util._ import sigma.Colls +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.interpreter.ContextExtension import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo.blockchain.BlockchainSimulationTestingCommons.{FullBlock, ValidationState} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/AtomicSwapExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/AtomicSwapExampleSpecification.scala index e5603342f3..1f8f8041d3 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/AtomicSwapExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/AtomicSwapExampleSpecification.scala @@ -4,6 +4,7 @@ import org.ergoplatform.Height import scorex.crypto.hash.Blake2b256 import scorex.utils.Random import sigma.ast.{SBoolean, SByte, SCollection} +import sigma.data.AvlTreeData import sigmastate.Values._ import sigmastate._ import sigmastate.interpreter.Interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala index 1230177437..31e8d8224b 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala @@ -2,7 +2,8 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R6, R4, R5} import org.ergoplatform._ -import sigmastate.{AvlTreeData, CompilerCrossVersionProps} +import sigma.data.AvlTreeData +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.{LongConstant, IntConstant} import sigmastate.helpers.{ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, CompilerTestingCommons, ContextEnrichingTestProvingInterpreter} import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala index ffd4b17018..035d4b4b46 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala @@ -2,10 +2,11 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5} import org.ergoplatform._ -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} +import sigma.data.AvlTreeData +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ -import sigmastate.{AvlTreeData, CompilerCrossVersionProps} -import sigmastate.Values.{LongConstant, IntConstant} +import sigmastate.CompilerCrossVersionProps +import sigmastate.Values.{IntConstant, LongConstant} import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index 44b69db572..4a164fc3f1 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -5,11 +5,12 @@ import org.scalatest.Assertion import org.scalatest.TryValues._ import sigmastate.crypto.DLogProtocol.{ProveDlog, DLogProverInput} import scorex.crypto.hash.Blake2b256 +import sigma.data.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ErgoTree, BooleanConstant} import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ -import sigmastate.{AvlTreeData, CompilerCrossVersionProps} +import sigmastate.CompilerCrossVersionProps class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala index 92f6e6c4cb..b774f42617 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala @@ -4,7 +4,8 @@ package sigmastate.utxo.examples import java.math.BigInteger import org.ergoplatform.ErgoBox.{R4, R5} -import sigmastate.{AvlTreeData, CompilerCrossVersionProps} +import sigma.data.AvlTreeData +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple, CryptoConstants} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/DemurrageExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/DemurrageExampleSpecification.scala index 8eb5b3b04c..ec5ddcdb57 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/DemurrageExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/DemurrageExampleSpecification.scala @@ -2,9 +2,10 @@ package sigmastate.utxo.examples import sigmastate.interpreter.Interpreter._ import org.ergoplatform._ +import sigma.data.AvlTreeData import sigmastate.Values.ShortConstant import sigmastate._ -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.ContextExtension import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala index 5f89ac5129..56bc58a826 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala @@ -10,6 +10,7 @@ import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigmastate._ import sigma.ast._ +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.eval._ import sigmastate.lang.Terms._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala index c36982d947..ab1c963a54 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala @@ -10,6 +10,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls import sigma.ast.SByte +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.CryptoConstants diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala index 22f7f1c7c9..109682e42f 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala @@ -6,7 +6,8 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert, Lookup} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls -import sigmastate.{AvlTreeData, AvlTreeFlags, CompilerCrossVersionProps, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigmastate.{CompilerCrossVersionProps, TrivialProp} import sigmastate.Values.{AvlTreeConstant, ByteArrayConstant, LongConstant, SigmaPropConstant} import sigmastate.eval.Extensions.ArrayOps import sigmastate.eval.{Digest32Coll, IRContext, SigmaDsl} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala index 15605481db..ae34e2b151 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala @@ -6,6 +6,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.ast.{SAvlTree, SBoolean, SLong} import sigma.ast.SCollection.SByteArray +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values._ import sigmastate._ import sigmastate.eval.Extensions.ArrayOps diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index 6bbf410dc1..1fe4fda743 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -4,7 +4,8 @@ import java.math.BigInteger import org.ergoplatform.ErgoBox.{R4, R5} import scorex.crypto.hash.Blake2b256 -import sigmastate.{AvlTreeData, CompilerCrossVersionProps} +import sigma.data.AvlTreeData +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple, CryptoConstants} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala index eb76496c82..194d7cc492 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala @@ -21,6 +21,7 @@ import sigmastate.eval.Extensions.ArrayOps import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo._ import sigma.Context +import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.utils.Helpers._ class OracleExamplesSpecification extends CompilerTestingCommons diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala index 1f00dbfc3c..dd7db35890 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala @@ -4,10 +4,11 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5, R6, R7} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random +import sigma.data.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala index f68c97079b..8009de94cd 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala @@ -3,9 +3,10 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5} import org.ergoplatform._ import scorex.crypto.hash.Blake2b256 +import sigma.data.AvlTreeData import sigmastate.Values.{IntConstant, SigmaPropConstant} import sigmastate._ -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/Rule110Specification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/Rule110Specification.scala index 51644ce34d..401e8f87ae 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/Rule110Specification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/Rule110Specification.scala @@ -5,6 +5,7 @@ import scorex.crypto.hash.Blake2b256 import scorex.util._ import sigma.Colls import sigma.ast.{SBoolean, SByte, SInt, SLong} +import sigma.data.AvlTreeData import sigmastate.Values._ import sigmastate._ import sigmastate.eval._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala index 04390272e8..e0c69afba0 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala @@ -1,10 +1,11 @@ package sigmastate.utxo.examples import org.ergoplatform._ +import sigma.data.AvlTreeData import sigmastate.Values.IntConstant import sigmastate._ import sigmastate.exceptions.InterpreterException -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala index 436115e9c2..04baae33fc 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala @@ -4,10 +4,11 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5, R6} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random +import sigma.data.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 257e18f7dd..d8fba7543d 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -6,7 +6,7 @@ import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.ergoplatform.sdk.wallet.protocol.context import org.ergoplatform._ import org.ergoplatform.sdk.Iso.inverseIso -import sigma.data.{CBigInt, Iso, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, Iso, RType} import scorex.crypto.authds.ADKey import scorex.util.ModifierId import scorex.util.encode.Base16 @@ -21,7 +21,6 @@ import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{SignedTransaction, import sigmastate.fleetSdkCommon.{distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesProverResultMod => proverResultMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod} import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} -import sigmastate.{AvlTreeData, AvlTreeFlags} import sigma.{Coll, Colls, Evaluation, GroupElement} import sigma.Extensions.CollBytesOps import sigma.ast.SType diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala index b3b470c604..1321680f99 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala @@ -8,8 +8,9 @@ import org.ergoplatform.sdk.utils.ArithUtils import org.ergoplatform.sdk.wallet.protocol.context.{BlockchainStateContext, TransactionContext} import org.ergoplatform.sdk.wallet.secrets.ExtendedSecretKey import org.ergoplatform.validation.ValidationRules -import sigma.util.Extensions.LongOps import sigma.VersionContext +import sigma.data.AvlTreeData +import sigma.util.Extensions.LongOps import sigmastate.Values.SigmaBoolean import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} import sigmastate.crypto.{DiffieHellmanTupleProverInput, SigmaProtocolPrivateInput} @@ -17,7 +18,6 @@ import sigmastate.interpreter.Interpreter.{ReductionResult, estimateCryptoVerify import sigmastate.interpreter._ import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.AvlTreeData import java.util import java.util.{Objects, List => JList} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala index ac8da8f398..9ce66f5729 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala @@ -8,12 +8,12 @@ import org.ergoplatform.sdk.utils.SerializationUtils.{parseString, serializeStri import org.ergoplatform.sdk.utils.Zero import sigma.Evaluation import sigma.ast.SType +import sigma.serialization.SerializerException import sigma.util.safeNewArray import sigmastate.Values.ErgoTree.headerWithVersion import sigmastate.Values.{ErgoTree, _} import sigmastate._ import sigmastate.eval._ -import sigmastate.exceptions.SerializerException import sigmastate.lang.{DeserializationSigmaBuilder, StdSigmaBuilder} import sigmastate.serialization._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala index 0ded1dbb5a..4b679b2750 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala @@ -13,7 +13,6 @@ import sigmastate.lang.SigmaParser import sigmastate.eval._ import sigma._ import debox.cfor -import sigmastate.exceptions.SerializerException import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 61b791b371..83d8245878 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -6,17 +6,15 @@ import io.circe._ import io.circe.syntax._ import org.ergoplatform.ErgoBox.{BoxId, NonMandatoryRegisterId, Token, TokenId} import org.ergoplatform.settings.ErgoAlgos -import org.ergoplatform.validation.{SigmaValidationSettings, SigmaValidationSettingsSerializer} +import org.ergoplatform.validation.SigmaValidationSettingsSerializer import scorex.crypto.authds.{ADDigest, ADKey} import scorex.crypto.hash.Digest32 import scorex.util.ModifierId import sigmastate.Values.{ErgoTree, EvaluatedValue} import sigmastate.eval.Extensions._ import sigmastate.eval._ -import sigmastate.exceptions.SigmaException import sigmastate.interpreter.{ContextExtension, ProverResult} -import sigmastate.{AvlTreeData, AvlTreeFlags} -import sigma.{AnyValue, Coll, Colls, Header, PreHeader} +import sigma.{AnyValue, Coll, Colls, Header, PreHeader, SigmaException} import scala.util.Try import sigmastate.utils.Helpers._ @@ -32,7 +30,9 @@ import org.ergoplatform.ErgoLikeTransactionTemplate import org.ergoplatform.ErgoBoxCandidate import org.ergoplatform.ErgoLikeContext import sigma.ast.SType -import sigma.data.{CBigInt, WrapperOf} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, WrapperOf} +import sigma.validation.SigmaValidationSettings + import scala.collection.mutable trait JsonCodecs { diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala index 64b4d72efb..3aa1458e3f 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala @@ -6,8 +6,8 @@ import org.ergoplatform.sdk.utils.ArithUtils import org.ergoplatform.sdk.wallet.protocol.context.{BlockchainStateContext, TransactionContext} import org.ergoplatform.validation.ValidationRules import org.ergoplatform.{ErgoLikeContext, ErgoLikeInterpreter} +import sigma.data.AvlTreeData import sigma.util.Extensions.LongOps -import sigmastate.AvlTreeData import sigmastate.Values.ErgoTree import sigmastate.eval.addCostChecked import sigmastate.exceptions.CostLimitException diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index 7b31553038..acbfb75401 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.utils import org.ergoplatform.ErgoBox -import sigma.data.{CBigInt, CollType, FuncType, OptionType, PairType, RType, TupleType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CollType, FuncType, OptionType, PairType, RType, TupleType} import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} @@ -9,7 +9,7 @@ import scorex.util.ModifierId import sigmastate.Values.ErgoTree import sigmastate.crypto.CryptoConstants import sigmastate.eval._ -import sigmastate.{AvlTreeData, AvlTreeFlags, TrivialProp} +import sigmastate.TrivialProp import sigma._ import java.math.BigInteger diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index d6d90b9edf..310a089cab 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -8,11 +8,11 @@ import sigma.data.{RType, TupleColl} import sigma.ast._ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps +import sigma.serialization.SerializerException import sigmastate.Values.SigmaBoolean import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.crypto.CryptoConstants.EcPointType -import sigmastate.exceptions.SerializerException import sigma.{AvlTree, Box, Colls, Evaluation} import sigmastate.serialization.SerializationSpecification diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index e94d1293a5..4bc855c2b3 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -9,18 +9,19 @@ import org.scalacheck.Arbitrary.arbitrary import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.ModifierId import scorex.util.encode.Base16 -import sigmastate.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} import sigmastate.crypto.CryptoConstants import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.eval.Digest32Coll import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SerializationSpecification -import sigmastate.utils.Helpers.DecoderResultOps // required for Scala 2.11 (extension method toTry) +import sigmastate.utils.Helpers.DecoderResultOps import sigma.Coll import sigma.{Header, PreHeader} import org.ergoplatform.{DataInput, ErgoBox, ErgoLikeContext, ErgoLikeTransaction, ErgoLikeTransactionTemplate, Input, UnsignedErgoLikeTransaction, UnsignedInput} import sigma.ast.SType +import sigma.data.AvlTreeData + import scala.collection.mutable class JsonSerializationSpec extends SerializationSpecification with JsonCodecs { From e1626652fedba1de359a75a2af61371d1396d49a Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 18:01:34 +0200 Subject: [PATCH 05/34] sigma-ast: CryptoContext moved to core --- build.sbt | 7 +- .../main/scala/sigma}/crypto/Platform.scala | 4 +- .../sigma}/crypto/SigmaJsCryptoFacade.scala | 2 +- .../sigma}/crypto/CryptoContextJvm.scala | 2 +- .../main/scala/sigma}/crypto/HmacSHA512.scala | 2 +- .../main/scala/sigma}/crypto/Platform.scala | 6 +- .../src/main/scala/sigma/ast/package.scala | 157 ++++++++++++++++++ .../scala/sigma}/crypto/CryptoContext.scala | 2 +- .../scala/sigma}/crypto/CryptoFacade.scala | 2 +- .../src/main/scala/sigma/crypto/package.scala | 12 ++ .../crypto/CryptoContextJsSpec.scala | 1 + .../crypto/CryptoFacadeJsSpec.scala | 11 +- .../scala/sigmastate/crypto/package.scala | 11 ++ .../src/main/scala/sigmastate/SMethod.scala | 4 +- .../src/main/scala/sigmastate/Values.scala | 2 +- .../scala/sigmastate/crypto/BcDlogGroup.scala | 2 +- .../scala/sigmastate/crypto/BigIntegers.scala | 2 +- .../sigmastate/crypto/CryptoConstants.scala | 4 +- .../scala/sigmastate/crypto/DlogGroup.scala | 2 + .../scala/sigmastate/crypto/package.scala | 15 -- .../sigmastate/eval/CostingDataContext.scala | 3 +- .../scala/sigmastate/eval/Extensions.scala | 2 +- .../sigmastate/interpreter/Interpreter.scala | 3 + .../scala/sigmastate/lang/SigmaBuilder.scala | 4 +- .../main/scala/sigmastate/lang/Terms.scala | 101 +---------- .../GroupElementSerializer.scala | 2 +- .../serialization/MethodCallSerializer.scala | 3 +- .../PropertyCallSerializer.scala | 5 +- .../main/scala/sigmastate/sigmastate.scala | 55 ------ .../src/main/scala/sigmastate/types.scala | 4 +- .../CryptoFacadeSpecification.scala | 2 +- .../crypto/GroupLawsSpecification.scala | 1 + .../GroupElementSerializerSpecification.scala | 2 +- .../sigmastate/lang/js/SigmaCompiler.scala | 2 +- .../scala/sigmastate/ErgoTreeBenchmarks.scala | 3 +- .../scala/sigmastate/eval/TreeBuilding.scala | 2 +- .../sigmastate/helpers/SigmaPPrint.scala | 2 +- .../sigmastate/lang/SigmaCompilerTest.scala | 6 +- .../OracleExamplesSpecification.scala | 3 +- .../ergoplatform/sdk/js/GroupElement.scala | 2 +- .../scala/org/ergoplatform/sdk/js/Value.scala | 2 +- .../org/ergoplatform/sdk/js/ValueSpec.scala | 2 +- .../org/ergoplatform/sdk/JavaHelpers.scala | 3 +- .../wallet/secrets/ExtendedPublicKey.scala | 2 +- .../wallet/secrets/ExtendedSecretKey.scala | 3 +- .../sdk/wallet/utils/Generators.scala | 2 +- 46 files changed, 249 insertions(+), 222 deletions(-) rename {interpreter/js/src/main/scala/sigmastate => core/js/src/main/scala/sigma}/crypto/Platform.scala (99%) rename {interpreter/js/src/main/scala/sigmastate => core/js/src/main/scala/sigma}/crypto/SigmaJsCryptoFacade.scala (99%) rename {interpreter/jvm/src/main/scala/sigmastate => core/jvm/src/main/scala/sigma}/crypto/CryptoContextJvm.scala (97%) rename {interpreter/jvm/src/main/scala/sigmastate => core/jvm/src/main/scala/sigma}/crypto/HmacSHA512.scala (96%) rename {interpreter/jvm/src/main/scala/sigmastate => core/jvm/src/main/scala/sigma}/crypto/Platform.scala (96%) rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma}/crypto/CryptoContext.scala (97%) rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma}/crypto/CryptoFacade.scala (99%) create mode 100644 interpreter/jvm/src/main/scala/sigmastate/crypto/package.scala delete mode 100644 interpreter/shared/src/main/scala/sigmastate/crypto/package.scala diff --git a/build.sbt b/build.sbt index 26d0fdcb27..abfb17ae44 100644 --- a/build.sbt +++ b/build.sbt @@ -196,7 +196,12 @@ lazy val core = crossProject(JVMPlatform, JSPlatform) scorexUtilDependency, publish / skip := true ) - .jvmSettings( crossScalaSettings ) + .jvmSettings( + crossScalaSettings, + libraryDependencies ++= Seq( + bouncycastleBcprov + ) + ) .jsSettings( crossScalaSettingsJS, scalacOptions ++= Seq( diff --git a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala b/core/js/src/main/scala/sigma/crypto/Platform.scala similarity index 99% rename from interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala rename to core/js/src/main/scala/sigma/crypto/Platform.scala index 0d713270c3..558ebba9e6 100644 --- a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala +++ b/core/js/src/main/scala/sigma/crypto/Platform.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import sigma.data.RType import scorex.util.encode.Base16 @@ -129,7 +129,7 @@ object Platform { class Curve // TODO JS: Use JS library for secure source of randomness - type SecureRandom = sigmastate.crypto.SecureRandomJS + type SecureRandom = sigma.crypto.SecureRandomJS /** Opaque point type. */ @js.native diff --git a/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala b/core/js/src/main/scala/sigma/crypto/SigmaJsCryptoFacade.scala similarity index 99% rename from interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala rename to core/js/src/main/scala/sigma/crypto/SigmaJsCryptoFacade.scala index fa60015ed2..4d269534cc 100644 --- a/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala +++ b/core/js/src/main/scala/sigma/crypto/SigmaJsCryptoFacade.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import debox.cfor diff --git a/interpreter/jvm/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala b/core/jvm/src/main/scala/sigma/crypto/CryptoContextJvm.scala similarity index 97% rename from interpreter/jvm/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala rename to core/jvm/src/main/scala/sigma/crypto/CryptoContextJvm.scala index 7b789f1db6..939e43619e 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala +++ b/core/jvm/src/main/scala/sigma/crypto/CryptoContextJvm.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import org.bouncycastle.asn1.x9.X9ECParameters diff --git a/interpreter/jvm/src/main/scala/sigmastate/crypto/HmacSHA512.scala b/core/jvm/src/main/scala/sigma/crypto/HmacSHA512.scala similarity index 96% rename from interpreter/jvm/src/main/scala/sigmastate/crypto/HmacSHA512.scala rename to core/jvm/src/main/scala/sigma/crypto/HmacSHA512.scala index 237ca9e176..c0c699921d 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/crypto/HmacSHA512.scala +++ b/core/jvm/src/main/scala/sigma/crypto/HmacSHA512.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec diff --git a/interpreter/jvm/src/main/scala/sigmastate/crypto/Platform.scala b/core/jvm/src/main/scala/sigma/crypto/Platform.scala similarity index 96% rename from interpreter/jvm/src/main/scala/sigmastate/crypto/Platform.scala rename to core/jvm/src/main/scala/sigma/crypto/Platform.scala index c213d7a718..c5123e6c18 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/crypto/Platform.scala +++ b/core/jvm/src/main/scala/sigma/crypto/Platform.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import org.bouncycastle.crypto.digests.SHA512Digest import org.bouncycastle.crypto.ec.CustomNamedCurves @@ -207,8 +207,4 @@ object Platform { def multiply(n: BigInteger): Ecp = CryptoFacade.exponentiatePoint(p, n) } - /** This JVM specific methods are used in Ergo node which won't be JS cross-compiled. */ - implicit class BcDlogGroupOps(val group: BcDlogGroup) extends AnyVal { - def curve: Curve = group.ctx.asInstanceOf[CryptoContextJvm].curve - } } diff --git a/core/shared/src/main/scala/sigma/ast/package.scala b/core/shared/src/main/scala/sigma/ast/package.scala index 34cd1522bb..63a2cfbcba 100644 --- a/core/shared/src/main/scala/sigma/ast/package.scala +++ b/core/shared/src/main/scala/sigma/ast/package.scala @@ -1,5 +1,162 @@ package sigma +import sigma.kiama.rewriting.Rewriter.{everywherebu, rewrite, rule} + package object ast { + /** Type alias for a substitution of type variables with their corresponding types. */ + type STypeSubst = Map[STypeVar, SType] + + /** Immutable and sharable empty substitution. */ + val EmptySubst = Map.empty[STypeVar, SType] + + /** Performs pairwise type unification making sure each type variable is equally + * substituted in all items. */ + def unifyTypeLists(items1: Seq[SType], items2: Seq[SType]): Option[STypeSubst] = { + // unify items pairwise independently + val itemsUni = (items1, items2).zipped.map((t1, t2) => unifyTypes(t1, t2)) + if (itemsUni.forall(_.isDefined)) { + // merge substitutions making sure the same id is equally substituted in all items + val merged = itemsUni.foldLeft(EmptySubst)((acc, subst) => { + var res = acc + for ( (id, t) <- subst.get ) { + if (res.contains(id) && res(id) != t) return None + res = res + (id -> t) + } + res + }) + Some(merged) + } else + None + } + + private val unifiedWithoutSubst = Some(EmptySubst) + + /** Finds a substitution `subst` of type variables such that unifyTypes(applySubst(t1, subst), t2) shouldBe Some(emptySubst) */ + def unifyTypes(t1: SType, t2: SType): Option[STypeSubst] = (t1, t2) match { + case (_ @ STypeVar(n1), _ @ STypeVar(n2)) => + if (n1 == n2) unifiedWithoutSubst else None + case (id1 @ STypeVar(_), _) => + Some(Map(id1 -> t2)) + case (e1: SCollectionType[_], e2: SCollectionType[_]) => + unifyTypes(e1.elemType, e2.elemType) + case (e1: SCollectionType[_], _: STuple) => + unifyTypes(e1.elemType, SAny) + case (e1: SOption[_], e2: SOption[_]) => + unifyTypes(e1.elemType, e2.elemType) + case (e1: STuple, e2: STuple) if e1.items.length == e2.items.length => + unifyTypeLists(e1.items, e2.items) + case (e1: SFunc, e2: SFunc) if e1.tDom.length == e2.tDom.length => + unifyTypeLists(e1.tDom :+ e1.tRange, e2.tDom :+ e2.tRange) + case (STypeApply(name1, args1), STypeApply(name2, args2)) + if name1 == name2 && args1.length == args2.length => + unifyTypeLists(args1, args2) + case (SBoolean, SSigmaProp) => // it is necessary for implicit conversion in Coll(bool, prop, bool) + unifiedWithoutSubst + case (SPrimType(e1), SPrimType(e2)) if e1 == e2 => + unifiedWithoutSubst + case (SAny, _) => + unifiedWithoutSubst + case _ => None + } + + /** Applies a type substitution to a given type. + * + * @param tpe the type to apply the substitution to + * @param subst the type substitution to apply + * @return the type after applying the substitution + */ + def applySubst(tpe: SType, subst: STypeSubst): SType = tpe match { + case SFunc(args, res, tparams) => + val remainingVars = tparams.filterNot { p => subst.contains(p.ident) } + SFunc(args.map(applySubst(_, subst)), applySubst(res, subst), remainingVars) + case _ => + val substRule = rule[Any] { + case id: STypeVar if subst.contains(id) => subst(id) + } + rewrite(everywherebu(substRule))(tpe) + } + + /** Computes the most general type given two types. + * + * @param t1 the first type + * @param t2 the second type + * @return the most general type if it exists, otherwise None + */ + def msgType(t1: SType, t2: SType): Option[SType] = unifyTypes(t1, t2) match { + case Some(_) => Some(t1) + case None => unifyTypes(t2, t1).map(_ => t2) + } + + /** Most Specific Generalized (MSG) type of ts. + * Currently just the type of the first element as long as all the elements have the same type. */ + def msgTypeOf(ts: Seq[SType]): Option[SType] = { + if (ts.isEmpty) None + else { + var res: SType = ts.head + for ( t <- ts.iterator.drop(1) ) { + msgType(t, res) match { + case Some(msg) => res = msg //assign new + case None => return None + } + } + Some(res) + } + } + + implicit class STypeOps(val tpe: SType) extends AnyVal { + def isCollectionLike: Boolean = tpe.isInstanceOf[SCollection[_]] + + def isCollection: Boolean = tpe.isInstanceOf[SCollectionType[_]] + + def isOption: Boolean = tpe.isInstanceOf[SOption[_]] + + def isBox: Boolean = tpe.isInstanceOf[SBox.type] + + def isGroupElement: Boolean = tpe.isInstanceOf[SGroupElement.type] + + def isSigmaProp: Boolean = tpe.isInstanceOf[SSigmaProp.type] + + def isAvlTree: Boolean = tpe.isInstanceOf[SAvlTree.type] + + def isFunc: Boolean = tpe.isInstanceOf[SFunc] + + def isTuple: Boolean = tpe.isInstanceOf[STuple] + + /** Returns true if this type is numeric (Byte, Short, etc.) + * + * @see [[sigmastate.SNumericType]] + */ + def isNumType: Boolean = tpe.isInstanceOf[SNumericType] + + /** Returns true if this type is either numeric (Byte, Short, etc.) or is NoType. + * + * @see [[sigmastate.SNumericType]] + */ + def isNumTypeOrNoType: Boolean = isNumType || tpe == NoType + + def asNumType: SNumericType = tpe.asInstanceOf[SNumericType] + + def asFunc: SFunc = tpe.asInstanceOf[SFunc] + + def asProduct: SProduct = tpe.asInstanceOf[SProduct] + + def asTuple: STuple = tpe.asInstanceOf[STuple] + + def asOption[T <: SType]: SOption[T] = tpe.asInstanceOf[SOption[T]] + + def whenFunc[T](action: SFunc => Unit) = if (tpe.isInstanceOf[SFunc]) action(tpe.asFunc) + + def asCollection[T <: SType] = tpe.asInstanceOf[SCollection[T]] + + /** Applies a type substitution to this type. + * + * @param subst the type substitution to apply + * @return the type after applying the substitution + */ + def withSubstTypes(subst: Map[STypeVar, SType]): SType = + if (subst.isEmpty) tpe + else + applySubst(tpe, subst) + } } diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoContext.scala b/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala similarity index 97% rename from interpreter/shared/src/main/scala/sigmastate/crypto/CryptoContext.scala rename to core/shared/src/main/scala/sigma/crypto/CryptoContext.scala index c0190ba093..c92ddba3dc 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoContext.scala +++ b/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoFacade.scala b/core/shared/src/main/scala/sigma/crypto/CryptoFacade.scala similarity index 99% rename from interpreter/shared/src/main/scala/sigmastate/crypto/CryptoFacade.scala rename to core/shared/src/main/scala/sigma/crypto/CryptoFacade.scala index 3ce337d695..683ec3f1b7 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoFacade.scala +++ b/core/shared/src/main/scala/sigma/crypto/CryptoFacade.scala @@ -1,4 +1,4 @@ -package sigmastate.crypto +package sigma.crypto import java.math.BigInteger diff --git a/core/shared/src/main/scala/sigma/crypto/package.scala b/core/shared/src/main/scala/sigma/crypto/package.scala index 5697fce9fe..20bddf66e8 100644 --- a/core/shared/src/main/scala/sigma/crypto/package.scala +++ b/core/shared/src/main/scala/sigma/crypto/package.scala @@ -6,4 +6,16 @@ package object crypto { /** Length of hash in bytes. */ val hashLength: Int = hashLengthBits / 8 + + /** Instance of Elliptic Curve descriptor. */ + type Curve = Platform.Curve + + /** Instance of Elliptic Curve point. */ + type Ecp = Platform.Ecp + + /** Instance of Elliptic Curve field element. */ + type ECFieldElem = Platform.ECFieldElem + + /** A cryptographically strong random number generator. */ + type SecureRandom = Platform.SecureRandom } diff --git a/interpreter/js/src/test/scala/sigmastate/crypto/CryptoContextJsSpec.scala b/interpreter/js/src/test/scala/sigmastate/crypto/CryptoContextJsSpec.scala index e6d8c93009..4fe1e23726 100644 --- a/interpreter/js/src/test/scala/sigmastate/crypto/CryptoContextJsSpec.scala +++ b/interpreter/js/src/test/scala/sigmastate/crypto/CryptoContextJsSpec.scala @@ -2,6 +2,7 @@ package sigmastate.crypto import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec +import sigma.crypto.CryptoContextJs import scala.scalajs.js diff --git a/interpreter/js/src/test/scala/sigmastate/crypto/CryptoFacadeJsSpec.scala b/interpreter/js/src/test/scala/sigmastate/crypto/CryptoFacadeJsSpec.scala index 63839e67ef..d80e4fec51 100644 --- a/interpreter/js/src/test/scala/sigmastate/crypto/CryptoFacadeJsSpec.scala +++ b/interpreter/js/src/test/scala/sigmastate/crypto/CryptoFacadeJsSpec.scala @@ -2,6 +2,9 @@ package sigmastate.crypto import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec +import sigma.crypto.Platform.Ecp +import sigma.crypto.utils.bytesToHex +import sigma.crypto.{CryptoFacade, CryptoFacadeJs, Point} import scala.scalajs.js import scala.scalajs.js.typedarray.Uint8Array @@ -68,9 +71,9 @@ class CryptoFacadeJsSpec extends AnyPropSpec with Matchers with CryptoTesting { } property("CryptoFacade.getEncodedOfFieldElem") { - utils.bytesToHex(CryptoFacadeJs.getEncodedOfFieldElem(p1.x)) shouldEqual + bytesToHex(CryptoFacadeJs.getEncodedOfFieldElem(p1.x)) shouldEqual "81c5275b1d50c39a0c36c4561c3a37bff1d87e37a9ad69eab029e426c0b1a8ac" - utils.bytesToHex(CryptoFacadeJs.getEncodedOfFieldElem(p1.y)) shouldBe + bytesToHex(CryptoFacadeJs.getEncodedOfFieldElem(p1.y)) shouldBe "db5d999704ec84b62962f3e35889901d04a619cd6d81d251c69d0f625c2dc4f3" } @@ -87,14 +90,14 @@ class CryptoFacadeJsSpec extends AnyPropSpec with Matchers with CryptoTesting { property("CryptoFacade.hashHmacSHA512") { val key = Uint8Array.from(bytesToJsShorts(CryptoFacade.BitcoinSeed)) val data = Uint8Array.from(bytesToJsShorts("abc".getBytes(CryptoFacade.Encoding))) - val res = utils.bytesToHex(CryptoFacadeJs.hashHmacSHA512(key, data)) + val res = bytesToHex(CryptoFacadeJs.hashHmacSHA512(key, data)) res shouldBe "2c15e87cde0f876fd8f060993748330cbe5f37c8bb3355e8ef44cea57890ec1d9b3274ef2b67bbe046cf8a012fba69796ec7803b1cc227521b9f5191e80a7da2" } property("CryptoFacade.generatePbkdf2Key") { val mnemonic = "slow silly start wash bundle suffer bulb ancient height spin express remind today effort helmet" val password = "pwd" - val res = utils.bytesToHex(CryptoFacadeJs.generatePbkdf2Key(mnemonic, password)) + val res = bytesToHex(CryptoFacadeJs.generatePbkdf2Key(mnemonic, password)) res shouldBe "0a8ea2ea0c4c12a9df88b005bda00c4de51ff36834b5fcd6a83667c371ad1da94bca1798690d87f2603b8f51d5ae025209e31f6cf81e12b84e4c543d236e58d0" } } diff --git a/interpreter/jvm/src/main/scala/sigmastate/crypto/package.scala b/interpreter/jvm/src/main/scala/sigmastate/crypto/package.scala new file mode 100644 index 0000000000..1b262e6639 --- /dev/null +++ b/interpreter/jvm/src/main/scala/sigmastate/crypto/package.scala @@ -0,0 +1,11 @@ +package sigmastate + +import sigma.crypto.CryptoContextJvm +import sigma.crypto.Platform.Curve + +package object crypto { + /** This JVM specific methods are used in Ergo node which won't be JS cross-compiled. */ + implicit class BcDlogGroupOps(val group: BcDlogGroup) extends AnyVal { + def curve: Curve = group.ctx.asInstanceOf[CryptoContextJvm].curve + } +} diff --git a/interpreter/shared/src/main/scala/sigmastate/SMethod.scala b/interpreter/shared/src/main/scala/sigmastate/SMethod.scala index 06ae29ea3e..8b7e600cfb 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SMethod.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SMethod.scala @@ -12,7 +12,7 @@ import sigmastate.SMethod.{InvokeDescBuilder, MethodCostFunc} import sigmastate.Values.{SValue, ValueCompanion} import sigmastate.interpreter.{CostDetails, ErgoTreeEvaluator, FixedCostItem, GivenCost, MethodDesc, SeqCostItem, TracedCost} import sigmastate.lang.{SigmaBuilder, Terms} -import sigmastate.lang.Terms.{MethodCall, OperationId, STypeSubst} +import sigmastate.lang.Terms.{MethodCall, OperationId} import scala.collection.compat.immutable.ArraySeq import scala.reflect.ClassTag @@ -180,7 +180,7 @@ case class SMethod( * @consensus */ def specializeFor(objTpe: SType, args: Seq[SType]): SMethod = { - Terms.unifyTypeLists(stype.tDom, objTpe +: args) match { + unifyTypeLists(stype.tDom, objTpe +: args) match { case Some(subst) if subst.nonEmpty => withConcreteTypes(subst) case _ => this diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 457419a4ff..62f0f4785a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -325,7 +325,7 @@ object Values { * @see Constant */ case class ConstantNode[S <: SType](value: S#WrappedType, tpe: S) extends Constant[S] { - require(sigmastate.crypto.Platform.isCorrectType(value, tpe), + require(sigma.crypto.Platform.isCorrectType(value, tpe), s"Invalid type of constant value $value, expected type $tpe") override def companion: ValueCompanion = Constant override def opCode: OpCode = companion.opCode diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/BcDlogGroup.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/BcDlogGroup.scala index 030b333032..afe7e95c49 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/BcDlogGroup.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/BcDlogGroup.scala @@ -3,7 +3,7 @@ package sigmastate.crypto import java.math.BigInteger import sigmastate.crypto.BigIntegers import debox.cfor -import sigmastate.crypto.{CryptoContext, CryptoFacade} +import sigma.crypto.{CryptoContext, CryptoFacade} import scala.collection.mutable diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/BigIntegers.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/BigIntegers.scala index 43bded8e09..f3fa71a418 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/BigIntegers.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/BigIntegers.scala @@ -1,7 +1,7 @@ package sigmastate.crypto import java.math.BigInteger -import sigmastate.crypto.SecureRandom +import sigma.crypto.SecureRandom /** Re-implementation in Scala of select set of utility methods from * org.bouncycastle.util.BigIntegers. diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala index 46d2424f6e..6429d42096 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala @@ -1,5 +1,7 @@ package sigmastate.crypto +import sigma.crypto.Ecp + import java.math.BigInteger /** Constants used in crypto operations implementation. */ @@ -14,7 +16,7 @@ object CryptoConstants { val dlogGroup: BcDlogGroup = SecP256K1Group /** Secure random generator used in the signature scheme. */ - val secureRandom: sigmastate.crypto.SecureRandom = dlogGroup.secureRandom + val secureRandom: sigma.crypto.SecureRandom = dlogGroup.secureRandom /** Size of the binary representation of any group element (2 ^ groupSizeBits == ) */ val groupSizeBits: Int = 256 diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DlogGroup.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DlogGroup.scala index 80ab2e372d..ef0d6941fa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DlogGroup.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DlogGroup.scala @@ -1,5 +1,7 @@ package sigmastate.crypto +import sigma.crypto.{CryptoFacade, Ecp, SecureRandom} + import java.math.BigInteger /** This is the general interface for the discrete logarithm prime-order group. diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/package.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/package.scala deleted file mode 100644 index c7ef439d87..0000000000 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/package.scala +++ /dev/null @@ -1,15 +0,0 @@ -package sigmastate - -package object crypto { - /** Instance of Elliptic Curve descriptor. */ - type Curve = Platform.Curve - - /** Instance of Elliptic Curve point. */ - type Ecp = Platform.Ecp - - /** Instance of Elliptic Curve field element. */ - type ECFieldElem = Platform.ECFieldElem - - /** A cryptographically strong random number generator. */ - type SecureRandom = Platform.SecureRandom -} diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 687ff7b6e6..f65e336834 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -10,6 +10,7 @@ import scorex.utils.{Ints, Longs} import sigma.{VersionContext, _} import sigma.ast.SCollection.SByteArray import sigma.ast.{SInt, STuple, SType} +import sigma.crypto.{CryptoFacade, Ecp} import sigma.data.OverloadHack.Overloaded1 import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType, SigmaConstants, WrapperOf} import sigma.util.Extensions.BigIntegerOps @@ -19,7 +20,7 @@ import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue, SigmaB import sigmastate._ import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, CryptoFacade, Ecp, ProveDHTuple} +import sigmastate.crypto.{CryptoConstants, ProveDHTuple} import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 6294912a20..687968087c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -10,7 +10,7 @@ import sigma.{Coll, _} import sigma.data.{Nullable, RType} import sigmastate.Platform import sigmastate.Values.{Constant, ConstantNode} -import sigmastate.crypto.{CryptoFacade, Ecp} +import sigma.crypto.{CryptoFacade, Ecp} import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala index b0f467d6bd..0016084716 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala @@ -53,6 +53,9 @@ trait Interpreter { type ProofT = UncheckedTree + /** Force initialization of reflection. */ + private val _ = InterpreterReflection + /** Evaluation settings used by [[ErgoTreeEvaluator]] which is used by this * interpreter to perform fullReduction. */ diff --git a/interpreter/shared/src/main/scala/sigmastate/lang/SigmaBuilder.scala b/interpreter/shared/src/main/scala/sigmastate/lang/SigmaBuilder.scala index 3c763fe395..16cbe0396f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/lang/SigmaBuilder.scala +++ b/interpreter/shared/src/main/scala/sigmastate/lang/SigmaBuilder.scala @@ -4,14 +4,14 @@ import debox.cfor import org.ergoplatform.ErgoBox.RegisterId import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.SOption.SIntOption -import sigma.ast._ +import sigma.ast.{STypeSubst, _} import sigma.data.Nullable import sigma.{AnyValue, Coll, Colls, Environment, Evaluation} import sigmastate.Values._ import sigmastate._ import sigmastate.exceptions.ConstraintFailed import sigmastate.lang.Constraints._ -import sigmastate.lang.Terms.{STypeSubst, _} +import sigmastate.lang.Terms._ import sigmastate.serialization.OpCodes import sigmastate.serialization.ValueCodes.OpCode import sigmastate.utxo._ diff --git a/interpreter/shared/src/main/scala/sigmastate/lang/Terms.scala b/interpreter/shared/src/main/scala/sigmastate/lang/Terms.scala index a3f4076cb2..04403a3f83 100644 --- a/interpreter/shared/src/main/scala/sigmastate/lang/Terms.scala +++ b/interpreter/shared/src/main/scala/sigmastate/lang/Terms.scala @@ -165,7 +165,7 @@ object Terms { override lazy val tpe: SType = input.tpe match { case funcType: SFunc => val subst = funcType.tpeParams.map(_.ident).zip(tpeArgs).toMap - Terms.applySubst(input.tpe, subst) + applySubst(input.tpe, subst) case _ => input.tpe } /** This is not used as operation, but rather to form a program structure */ @@ -358,103 +358,4 @@ object Terms { } - /** Type alias for a substitution of type variables with their corresponding types. */ - type STypeSubst = Map[STypeVar, SType] - - /** Immutable and sharable empty substitution. */ - val EmptySubst = Map.empty[STypeVar, SType] - - /** Performs pairwise type unification making sure each type variable is equally - * substituted in all items. */ - def unifyTypeLists(items1: Seq[SType], items2: Seq[SType]): Option[Terms.STypeSubst] = { - // unify items pairwise independently - val itemsUni = (items1, items2).zipped.map((t1, t2) => unifyTypes(t1, t2)) - if (itemsUni.forall(_.isDefined)) { - // merge substitutions making sure the same id is equally substituted in all items - val merged = itemsUni.foldLeft(EmptySubst)((acc, subst) => { - var res = acc - for ( (id, t) <- subst.get ) { - if (res.contains(id) && res(id) != t) return None - res = res + (id -> t) - } - res - }) - Some(merged) - } else - None - } - - private val unifiedWithoutSubst = Some(EmptySubst) - - /** Finds a substitution `subst` of type variables such that unifyTypes(applySubst(t1, subst), t2) shouldBe Some(emptySubst) */ - def unifyTypes(t1: SType, t2: SType): Option[Terms.STypeSubst] = (t1, t2) match { - case (_ @ STypeVar(n1), _ @ STypeVar(n2)) => - if (n1 == n2) unifiedWithoutSubst else None - case (id1 @ STypeVar(_), _) => - Some(Map(id1 -> t2)) - case (e1: SCollectionType[_], e2: SCollectionType[_]) => - unifyTypes(e1.elemType, e2.elemType) - case (e1: SCollectionType[_], _: STuple) => - unifyTypes(e1.elemType, SAny) - case (e1: SOption[_], e2: SOption[_]) => - unifyTypes(e1.elemType, e2.elemType) - case (e1: STuple, e2: STuple) if e1.items.length == e2.items.length => - unifyTypeLists(e1.items, e2.items) - case (e1: SFunc, e2: SFunc) if e1.tDom.length == e2.tDom.length => - unifyTypeLists(e1.tDom :+ e1.tRange, e2.tDom :+ e2.tRange) - case (STypeApply(name1, args1), STypeApply(name2, args2)) - if name1 == name2 && args1.length == args2.length => - unifyTypeLists(args1, args2) - case (SBoolean, SSigmaProp) => // it is necessary for implicit conversion in Coll(bool, prop, bool) - unifiedWithoutSubst - case (SPrimType(e1), SPrimType(e2)) if e1 == e2 => - unifiedWithoutSubst - case (SAny, _) => - unifiedWithoutSubst - case _ => None - } - - /** Applies a type substitution to a given type. - * - * @param tpe the type to apply the substitution to - * @param subst the type substitution to apply - * @return the type after applying the substitution - */ - def applySubst(tpe: SType, subst: Terms.STypeSubst): SType = tpe match { - case SFunc(args, res, tparams) => - val remainingVars = tparams.filterNot { p => subst.contains(p.ident) } - SFunc(args.map(applySubst(_, subst)), applySubst(res, subst), remainingVars) - case _ => - val substRule = rule[Any] { - case id: STypeVar if subst.contains(id) => subst(id) - } - rewrite(everywherebu(substRule))(tpe) - } - - /** Computes the most general type given two types. - * - * @param t1 the first type - * @param t2 the second type - * @return the most general type if it exists, otherwise None - */ - def msgType(t1: SType, t2: SType): Option[SType] = unifyTypes(t1, t2) match { - case Some(_) => Some(t1) - case None => unifyTypes(t2, t1).map(_ => t2) - } - - /** Most Specific Generalized (MSG) type of ts. - * Currently just the type of the first element as long as all the elements have the same type. */ - def msgTypeOf(ts: Seq[SType]): Option[SType] = { - if (ts.isEmpty) None - else { - var res: SType = ts.head - for ( t <- ts.iterator.drop(1) ) { - msgType(t, res) match { - case Some(msg) => res = msg //assign new - case None => return None - } - } - Some(res) - } - } } diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala index e54c41e426..f3c75381de 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala @@ -1,7 +1,7 @@ package sigmastate.serialization import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.CryptoFacade +import sigma.crypto.CryptoFacade import CryptoConstants.EcPointType import sigma.util.safeNewArray import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala index 3d9518d2d9..9a4a0c6690 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/MethodCallSerializer.scala @@ -2,14 +2,13 @@ package sigmastate.serialization import sigmastate.Values._ import sigmastate._ -import sigmastate.lang.Terms.STypeSubst import sigmastate.lang.Terms.MethodCall import sigma.util.safeNewArray import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ComplexityTable import debox.cfor -import sigma.ast.SType +import sigma.ast.{SType, STypeSubst} import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} case class MethodCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value[SType]], STypeSubst) => Value[SType]) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala index 097825e1aa..e3c27d3277 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/PropertyCallSerializer.scala @@ -1,11 +1,10 @@ package sigmastate.serialization -import sigma.ast.SType +import sigma.ast.{EmptySubst, SType, STypeSubst} import sigma.serialization.CoreByteWriter.{ArgInfo, DataInfo} import sigmastate.Values._ import sigmastate._ import sigmastate.lang.Terms -import sigmastate.lang.Terms.STypeSubst import sigmastate.lang.Terms.{MethodCall, PropertyCall} import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} @@ -33,6 +32,6 @@ case class PropertyCallSerializer(cons: (Value[SType], SMethod, IndexedSeq[Value val complexity = ComplexityTable.MethodCallComplexity.getOrElse((typeId, methodId), ComplexityTable.MinimalComplexity) r.addComplexity(complexity) val specMethod = method.specializeFor(obj.tpe, SType.EmptySeq) - cons(obj, specMethod, Value.EmptySeq, Terms.EmptySubst) + cons(obj, specMethod, Value.EmptySeq, EmptySubst) } } diff --git a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala index d1a547bbc0..e07eb34401 100644 --- a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala +++ b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala @@ -22,61 +22,6 @@ package object sigmastate { implicit val ErgoLikeContextRType : RType[ErgoLikeContext] = RType.fromClassTag(classTag[ErgoLikeContext]) - implicit class STypeOps(val tpe: SType) extends AnyVal { - def isCollectionLike: Boolean = tpe.isInstanceOf[SCollection[_]] - - def isCollection: Boolean = tpe.isInstanceOf[SCollectionType[_]] - - def isOption: Boolean = tpe.isInstanceOf[SOption[_]] - - def isBox: Boolean = tpe.isInstanceOf[SBox.type] - - def isGroupElement: Boolean = tpe.isInstanceOf[SGroupElement.type] - - def isSigmaProp: Boolean = tpe.isInstanceOf[SSigmaProp.type] - - def isAvlTree: Boolean = tpe.isInstanceOf[SAvlTree.type] - - def isFunc: Boolean = tpe.isInstanceOf[SFunc] - - def isTuple: Boolean = tpe.isInstanceOf[STuple] - - /** Returns true if this type is numeric (Byte, Short, etc.) - * - * @see [[sigmastate.SNumericType]] - */ - def isNumType: Boolean = tpe.isInstanceOf[SNumericType] - - /** Returns true if this type is either numeric (Byte, Short, etc.) or is NoType. - * - * @see [[sigmastate.SNumericType]] - */ - def isNumTypeOrNoType: Boolean = isNumType || tpe == NoType - - def asNumType: SNumericType = tpe.asInstanceOf[SNumericType] - - def asFunc: SFunc = tpe.asInstanceOf[SFunc] - - def asProduct: SProduct = tpe.asInstanceOf[SProduct] - - def asTuple: STuple = tpe.asInstanceOf[STuple] - - def asOption[T <: SType]: SOption[T] = tpe.asInstanceOf[SOption[T]] - - def whenFunc[T](action: SFunc => Unit) = if (tpe.isInstanceOf[SFunc]) action(tpe.asFunc) - - def asCollection[T <: SType] = tpe.asInstanceOf[SCollection[T]] - - /** Applies a type substitution to this type. - * - * @param subst the type substitution to apply - * @return the type after applying the substitution - */ - def withSubstTypes(subst: Map[STypeVar, SType]): SType = - if (subst.isEmpty) tpe - else - Terms.applySubst(tpe, subst) - } /** Helper method to create "+" operation node. */ def Plus[T <: SNumericType](left: Value[T], right: Value[T]): Value[T] = diff --git a/interpreter/shared/src/main/scala/sigmastate/types.scala b/interpreter/shared/src/main/scala/sigmastate/types.scala index 6fcb07b5eb..e1fd94af19 100644 --- a/interpreter/shared/src/main/scala/sigmastate/types.scala +++ b/interpreter/shared/src/main/scala/sigmastate/types.scala @@ -167,7 +167,7 @@ trait SNumericTypeMethods extends MonoTypeMethods { import SNumericTypeMethods.tNum protected override def getMethods(): Seq[SMethod] = { super.getMethods() ++ SNumericTypeMethods.methods.map { - m => m.copy(stype = Terms.applySubst(m.stype, Map(tNum -> this.ownerType)).asFunc) + m => m.copy(stype = applySubst(m.stype, Map(tNum -> this.ownerType)).asFunc) } } } @@ -972,7 +972,7 @@ object STupleMethods extends MethodsContainer { // TODO: implement other methods val activeMethods = Set(1.toByte /*Coll.size*/, 10.toByte /*Coll.apply*/) SCollectionMethods.methods.filter(m => activeMethods.contains(m.methodId)).map { m => - m.copy(stype = Terms.applySubst(m.stype, subst).asFunc) + m.copy(stype = applySubst(m.stype, subst).asFunc) } } diff --git a/interpreter/shared/src/test/scala/sigmastate/CryptoFacadeSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/CryptoFacadeSpecification.scala index 49fa4534bd..375ef3f4e5 100644 --- a/interpreter/shared/src/test/scala/sigmastate/CryptoFacadeSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/CryptoFacadeSpecification.scala @@ -5,7 +5,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scorex.util.encode.Base16 -import sigmastate.crypto.CryptoFacade +import sigma.crypto.CryptoFacade import java.math.BigInteger diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala index d177a26e4c..dea863487f 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala @@ -6,6 +6,7 @@ import sigmastate.crypto.CryptoConstants import CryptoConstants.EcPointType import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks +import sigma.crypto.{CryptoFacade, Ecp} import sigmastate.TestsBase import sigmastate.utils.Helpers diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala index 04c4a8e128..a6c3df7f06 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala @@ -1,7 +1,7 @@ package sigmastate.serialization import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.CryptoFacade +import sigma.crypto.CryptoFacade import sigmastate.eval._ class GroupElementSerializerSpecification extends SerializationSpecification { diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index 7880c801ae..d428a75294 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -7,7 +7,7 @@ import org.scalablytyped.runtime.StringDictionary import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel import org.ergoplatform.sdk.js.{ErgoTree, Value} -import sigmastate.{STypeOps, Values} +import sigmastate.Values import sigmastate.eval.CompiletimeIRContext import sigmastate.lang.Terms.ValueOps diff --git a/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala b/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala index cacbf72a3e..8e65b602dc 100644 --- a/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala +++ b/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala @@ -4,6 +4,7 @@ import debox.cfor import org.scalameter.api.Bench import sigma.BenchmarkGens import sigma.ast.SType +import sigma.crypto.Platform import sigmastate.Values.{IntConstant, SValue} import sigmastate.serialization.OpCodes.PlusCode @@ -58,7 +59,7 @@ object ErgoTreeBenchmarks extends Bench.LocalTime with BenchmarkGens { suite: Be measure method "isCorrectType" in { using(sizes) in { size => cfor(0)(_ < size, _ + 1) { i => - sigmastate.crypto.Platform.isCorrectType(i, SType.allPredefTypes(i % 10)) + Platform.isCorrectType(i, SType.allPredefTypes(i % 10)) } } } diff --git a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala index 657e73cc1e..7283a7e2b4 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala @@ -282,7 +282,7 @@ trait TreeBuilding extends SigmaLibrary { IR: IRContext => case (mth @ SCollectionMethods.ZipMethod, Seq(coll)) => val typeSubst = Map(SCollection.tOV -> coll.asCollection[SType].tpe.elemType) typeSubst - case (mth, _) => Terms.EmptySubst + case (mth, _) => EmptySubst } val specMethod = method.withConcreteTypes(typeSubst + (SCollection.tIV -> colTpe.elemType)) builder.mkMethodCall(col, specMethod, args.toIndexedSeq, Map()) diff --git a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index 1fda430646..e11f9708bf 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -248,7 +248,7 @@ object SigmaPPrint extends PPrinter { case mc @ MethodCall(obj, method, args, typeSubst) => val objType = apply(method.objType).plainText val methodTemplate = method.objType.getMethodByName(method.name) - val methodT = Terms.unifyTypeLists(methodTemplate.stype.tDom, obj.tpe +: args.map(_.tpe)) match { + val methodT = unifyTypeLists(methodTemplate.stype.tDom, obj.tpe +: args.map(_.tpe)) match { case Some(subst) if subst.nonEmpty => val getMethod = s"""$objType.getMethodByName("${method.name}").withConcreteTypes""" Tree.Apply(getMethod, treeifySeq(Seq(subst))) diff --git a/sc/shared/src/test/scala/sigmastate/lang/SigmaCompilerTest.scala b/sc/shared/src/test/scala/sigmastate/lang/SigmaCompilerTest.scala index 0f0d6c8688..2d25adcbd3 100644 --- a/sc/shared/src/test/scala/sigmastate/lang/SigmaCompilerTest.scala +++ b/sc/shared/src/test/scala/sigmastate/lang/SigmaCompilerTest.scala @@ -3,7 +3,7 @@ package sigmastate.lang import org.ergoplatform.ErgoAddressEncoder.TestnetNetworkPrefix import org.ergoplatform._ import scorex.util.encode.Base58 -import sigma.ast.{SAvlTree, SBoolean, SBox, SByte, SCollection, SInt, SSigmaProp, SType} +import sigma.ast.{EmptySubst, SAvlTree, SBoolean, SBox, SByte, SCollection, SInt, SSigmaProp, SType} import sigmastate.Values._ import sigmastate._ import sigmastate.exceptions.{GraphBuildingException, InvalidArguments, TyperException} @@ -81,8 +81,8 @@ class SigmaCompilerTest extends CompilerTestingCommons with LangTests with Objec } property("global methods") { - comp(env, "{ groupGenerator }") shouldBe MethodCall(Global, SGlobalMethods.groupGeneratorMethod, IndexedSeq(), Terms.EmptySubst) - comp(env, "{ Global.groupGenerator }") shouldBe MethodCall(Global, SGlobalMethods.groupGeneratorMethod, IndexedSeq(), Terms.EmptySubst) + comp(env, "{ groupGenerator }") shouldBe MethodCall(Global, SGlobalMethods.groupGeneratorMethod, IndexedSeq(), EmptySubst) + comp(env, "{ Global.groupGenerator }") shouldBe MethodCall(Global, SGlobalMethods.groupGeneratorMethod, IndexedSeq(), EmptySubst) comp(env, "{ Global.xor(arr1, arr2) }") shouldBe Xor(ByteArrayConstant(arr1), ByteArrayConstant(arr2)) comp(env, "{ xor(arr1, arr2) }") shouldBe Xor(ByteArrayConstant(arr1), ByteArrayConstant(arr2)) } diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala index 194d7cc492..d161016615 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala @@ -16,11 +16,12 @@ import sigmastate.helpers.TestingHelpers._ import org.ergoplatform._ import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, StdContracts, TestContractSpec} import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.{BigIntegers, CryptoFacade} +import sigmastate.crypto.BigIntegers import sigmastate.eval.Extensions.ArrayOps import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo._ import sigma.Context +import sigma.crypto.CryptoFacade import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.utils.Helpers._ diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala index 857592d4a2..3c710b7587 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala @@ -1,6 +1,6 @@ package org.ergoplatform.sdk.js -import sigmastate.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform} +import sigma.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform} import sigmastate.eval.Extensions.ArrayByteOps import scala.scalajs.js diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index e08e794fd6..dd2808e001 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -6,7 +6,7 @@ import sigma.data.PairType import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType -import sigmastate.crypto.Platform +import sigma.crypto.Platform import sigmastate.eval.{CAvlTree, CGroupElement, CSigmaProp, CostingBox, SigmaDsl} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala index 7ef2476289..062c57adab 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala @@ -8,7 +8,7 @@ import sigma.ast.SType import sigmastate.Values.{AvlTreeConstant, BigIntConstant, BooleanConstant, BoxConstant, ByteConstant, Constant, GroupElementConstant, IntConstant, LongConstant, ShortConstant, SigmaPropConstant, UnitConstant} import sigmastate.crypto.CryptoConstants.dlogGroup import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.CryptoFacade +import sigma.crypto.CryptoFacade import sigmastate.eval.CSigmaProp import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.serialization.ConstantSerializer diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index 89add3fd12..f93af6b1d4 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -13,6 +13,7 @@ import sigmastate.utils.Helpers._ import scorex.util.encode.Base16 import scorex.util.{ModifierId, bytesToId, idToBytes} import sigma.ast.SType +import sigma.crypto.CryptoFacade import sigma.data.ExactIntegral.LongIsExactIntegral import sigma.data.{InverseIso, Iso, RType, SigmaConstants} import sigma.util.StringUtil.StringUtilExtensions @@ -20,7 +21,7 @@ import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaBoolean, SigmaPropConstant} import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoFacade, DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple} import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} import sigmastate.serialization.{ErgoTreeSerializer, GroupElementSerializer, SigmaSerializer, ValueSerializer} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala index 2ad287972e..b709fdaf1c 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala @@ -1,8 +1,8 @@ package org.ergoplatform.sdk.wallet.secrets +import sigma.crypto.CryptoFacade import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.CryptoFacade import sigmastate.crypto.BigIntegers import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala index 4b87e31e13..8783567e89 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala @@ -1,11 +1,12 @@ package org.ergoplatform.sdk.wallet.secrets +import sigma.crypto.CryptoFacade + import java.math.BigInteger import sigmastate.crypto.BigIntegers import sigmastate.crypto.DLogProtocol import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.CryptoFacade import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index 3df10bf2ce..87a1599ab3 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -15,9 +15,9 @@ import scorex.util._ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgoLikeTransaction, UnsignedInput} import sigmastate.eval.Extensions._ import scorex.util.{ModifierId, bytesToId} +import sigma.crypto.CryptoFacade import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ -import sigmastate.crypto.CryptoFacade trait Generators { From a22282d4ff09040a9ae3ba785c386842be437014 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 20:32:53 +0200 Subject: [PATCH 06/34] sigma-ast: GroupElementSerializer moved to core --- .../scala/sigma/crypto/CryptoContext.scala | 5 +++++ .../src/main/scala/sigma/crypto/package.scala | 6 +++++ .../GroupElementSerializer.scala | 22 ++++++++----------- .../scala/org/ergoplatform/ErgoAddress.scala | 1 + .../scala/sigmastate/DataValueComparer.scala | 2 +- .../main/scala/sigmastate/SigSerializer.scala | 2 +- .../src/main/scala/sigmastate/Values.scala | 2 +- .../sigmastate/crypto/CryptoConstants.scala | 5 ----- .../sigmastate/crypto/DLogProtocol.scala | 6 +++-- .../crypto/DiffieHellmanTupleProtocol.scala | 5 +++-- .../sigmastate/eval/CostingDataContext.scala | 6 ++--- .../main/scala/sigmastate/eval/package.scala | 2 +- .../serialization/DataSerializer.scala | 2 +- .../serialization/ProveDlogSerializer.scala | 3 ++- .../transformers/ProveDHTupleSerializer.scala | 5 +++-- .../main/scala/sigmastate/utils/Helpers.scala | 2 +- .../crypto/GroupLawsSpecification.scala | 4 +--- .../helpers/ErgoLikeContextTesting.scala | 3 ++- .../sigmastate/helpers/TestingCommons.scala | 4 ++-- .../ConstantSerializerSpecification.scala | 2 +- .../DataSerializerSpecification.scala | 2 +- .../GroupElementSerializerSpecification.scala | 1 + .../generators/ObjectGenerators.scala | 3 ++- .../special/sigma/SigmaTestingData.scala | 2 +- .../scala/sigmastate/eval/GraphBuilding.scala | 2 +- .../ErgoAddressSpecification.scala | 3 ++- .../sigmastate/helpers/SigmaPPrint.scala | 4 ++-- .../DeserializationResilience.scala | 6 ++--- .../org/ergoplatform/sdk/JavaHelpers.scala | 6 ++--- .../sdk/DataJsonEncoderSpecification.scala | 2 +- .../sdk/JsonSerializationSpec.scala | 3 ++- 31 files changed, 67 insertions(+), 56 deletions(-) rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma}/serialization/GroupElementSerializer.scala (65%) diff --git a/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala b/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala index c92ddba3dc..988a8e9283 100644 --- a/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala +++ b/core/shared/src/main/scala/sigma/crypto/CryptoContext.scala @@ -31,3 +31,8 @@ abstract class CryptoContext { def generator: Ecp } +object CryptoContext { + /** Default context for cryptographic operations. */ + val default: CryptoContext = Platform.createContext() +} + diff --git a/core/shared/src/main/scala/sigma/crypto/package.scala b/core/shared/src/main/scala/sigma/crypto/package.scala index 20bddf66e8..cc2598f62a 100644 --- a/core/shared/src/main/scala/sigma/crypto/package.scala +++ b/core/shared/src/main/scala/sigma/crypto/package.scala @@ -7,6 +7,9 @@ package object crypto { /** Length of hash in bytes. */ val hashLength: Int = hashLengthBits / 8 + /** Number of bytes to represent any group element as byte array */ + val groupSize: Int = 256 / 8 //32 bytes + /** Instance of Elliptic Curve descriptor. */ type Curve = Platform.Curve @@ -18,4 +21,7 @@ package object crypto { /** A cryptographically strong random number generator. */ type SecureRandom = Platform.SecureRandom + + /** Type of group elements used in the signature scheme. */ + type EcPointType = Ecp } diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala b/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala similarity index 65% rename from interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala rename to core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala index f3c75381de..03f3a7b9ae 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/GroupElementSerializer.scala +++ b/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala @@ -1,10 +1,7 @@ -package sigmastate.serialization +package sigma.serialization -import sigmastate.crypto.CryptoConstants -import sigma.crypto.CryptoFacade -import CryptoConstants.EcPointType +import sigma.crypto.{CryptoContext, CryptoFacade, EcPointType} import sigma.util.safeNewArray -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} /** * A serializer which encodes group elements, so elliptic curve points in our case, to bytes, and decodes points @@ -15,13 +12,12 @@ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} * Special case is infinity point, which is encoded by 33 zeroes. * Thus elliptic curve point is always encoded with 33 bytes. */ -object GroupElementSerializer extends SigmaSerializer[EcPointType, EcPointType] { +object GroupElementSerializer extends CoreSerializer[EcPointType, EcPointType] { - private val curve = CryptoConstants.dlogGroup - private val encodingSize = 1 + CryptoConstants.groupSize + private val encodingSize = 1 + sigma.crypto.groupSize private lazy val identityPointEncoding = Array.fill(encodingSize)(0: Byte) - override def serialize(point: EcPointType, w: SigmaByteWriter): Unit = { + override def serialize(point: EcPointType, w: CoreByteWriter): Unit = { val bytes = if (CryptoFacade.isInfinityPoint(point)) { identityPointEncoding } else { @@ -36,15 +32,15 @@ object GroupElementSerializer extends SigmaSerializer[EcPointType, EcPointType] w.putBytes(bytes) } - override def parse(r: SigmaByteReader): EcPointType = { + override def parse(r: CoreByteReader): EcPointType = { val encoded = r.getBytes(encodingSize) if (encoded(0) != 0) { - curve.ctx.decodePoint(encoded) + CryptoContext.default.decodePoint(encoded) } else { - curve.identity + CryptoContext.default.infinity // identity point of multiplicative group } } - def parse(bytes: Array[Byte]): EcPointType = parse(SigmaSerializer.startReader(bytes)) + def parse(bytes: Array[Byte]): EcPointType = parse(CoreSerializer.startReader(bytes)) } diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala index 13fae39be0..88dc9de17f 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala @@ -11,6 +11,7 @@ import sigmastate.serialization._ import sigmastate.utxo.{DeserializeContext, Slice} import sigma.{Coll, SigmaException} import sigma.ast.{SInt, SSigmaProp} +import sigma.serialization.GroupElementSerializer import scala.util.Try diff --git a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala index 0bba6c0dc4..bbb47702c1 100644 --- a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala @@ -6,9 +6,9 @@ import sigmastate.Values.SigmaBoolean import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.ProveDHTuple import sigmastate.eval.SigmaDsl -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} import sigma._ +import sigma.crypto.EcPointType /** Implementation of data equality for two arbitrary ErgoTree data types. * @see [[DataValueComparer.equalDataValues]] diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index 329ecc993b..410f49b446 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -27,7 +27,7 @@ class SigSerializer { val hashSize = CryptoConstants.soundnessBits / 8 /** Number of bytes to represent any group element as byte array */ - val order = CryptoConstants.groupSize + val order = sigma.crypto.groupSize /** Recursively traverses the given node and serializes challenges and prover messages * to the given writer. diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 62f0f4785a..748384b4fe 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -6,7 +6,6 @@ import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} import org.ergoplatform.settings.ErgoAlgos import sigma.data.{Nullable, RType} import sigma.util.CollectionUtil._ -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.interpreter.{CompanionDesc, ErgoTreeEvaluator, Interpreter, NamedDesc} import sigmastate.serialization._ import sigmastate.serialization.OpCodes._ @@ -24,6 +23,7 @@ import debox.cfor import scorex.util.encode.Base16 import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast._ +import sigma.crypto.EcPointType import sigmastate.exceptions.InterpreterException import scala.language.implicitConversions diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala index 6429d42096..dd348a1903 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/CryptoConstants.scala @@ -6,9 +6,6 @@ import java.math.BigInteger /** Constants used in crypto operations implementation. */ object CryptoConstants { - /** Type of group elements used in the signature scheme. */ - type EcPointType = Ecp - /** Length of encoded group element in bytes. */ val EncodedGroupElementLength: Byte = 33 @@ -21,8 +18,6 @@ object CryptoConstants { /** Size of the binary representation of any group element (2 ^ groupSizeBits == ) */ val groupSizeBits: Int = 256 - /** Number of bytes to represent any group element as byte array */ - val groupSize: Int = 256 / 8 //32 bytes /** Group order, i.e. number of elements in the group */ val groupOrder: BigInteger = dlogGroup.order diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala index 6c10bda1a4..31f877b4fa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala @@ -7,9 +7,11 @@ import scorex.util.encode.Base16 import sigmastate._ import sigmastate.eval._ import sigmastate.crypto.VerifierMessage.Challenge -import CryptoConstants.{EcPointType, dlogGroup} -import sigmastate.serialization.{GroupElementSerializer, OpCodes} +import CryptoConstants.dlogGroup +import sigmastate.serialization.OpCodes import sigma.SigmaProp +import sigma.crypto.EcPointType +import sigma.serialization.GroupElementSerializer import sigmastate.serialization.ValueCodes.OpCode object DLogProtocol { diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala index 7d5f439862..1e4c2ee8b1 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala @@ -5,9 +5,10 @@ import sigmastate.Values.Value.PropositionCode import sigmastate._ import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.eval.SigmaDsl -import CryptoConstants.EcPointType -import sigmastate.serialization.{GroupElementSerializer, OpCodes} +import sigmastate.serialization.OpCodes import sigma.SigmaProp +import sigma.crypto.EcPointType +import sigma.serialization.GroupElementSerializer import sigmastate.serialization.ValueCodes.OpCode diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index f65e336834..4036221d33 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -10,21 +10,21 @@ import scorex.utils.{Ints, Longs} import sigma.{VersionContext, _} import sigma.ast.SCollection.SByteArray import sigma.ast.{SInt, STuple, SType} -import sigma.crypto.{CryptoFacade, Ecp} +import sigma.crypto.{CryptoFacade, EcPointType, Ecp} import sigma.data.OverloadHack.Overloaded1 import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType, SigmaConstants, WrapperOf} +import sigma.serialization.GroupElementSerializer import sigma.util.Extensions.BigIntegerOps import sigma.validation.SigmaValidationSettings import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue, SigmaBoolean} import sigmastate._ -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{CryptoConstants, ProveDHTuple} import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer} +import sigmastate.serialization.SigmaSerializer import java.math.BigInteger import java.util.Arrays diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index f02b722c8a..6b2e33125c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -5,9 +5,9 @@ import org.ergoplatform.ErgoBox import sigma.data.{AvlTreeData, RType} import scorex.crypto.hash.Digest32 import sigmastate.Values.SigmaBoolean -import sigmastate.crypto.CryptoConstants.EcPointType import sigma.{Coll, CollBuilder} import sigma._ +import sigma.crypto.EcPointType import sigmastate.exceptions.CostLimitException import supertagged.TaggedType diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index bfe2737442..082d1a9b17 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -11,7 +11,7 @@ import sigmastate.eval._ import sigma.{Evaluation, _} import debox.cfor import sigma.ast._ -import sigma.serialization.SerializerException +import sigma.serialization.{GroupElementSerializer, SerializerException} import sigma.validation.ValidationRules.CheckSerializableTypeCode import scala.collection.mutable diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala index 1ea7057bd3..5bf55887ab 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala @@ -1,11 +1,12 @@ package sigmastate.serialization import sigma.ast.SGroupElement +import sigma.crypto.EcPointType import sigma.serialization.CoreByteWriter.DataInfo +import sigma.serialization.GroupElementSerializer import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.CreateProveDlog import sigmastate.Values.{SValue, SigmaPropValue, Value} -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.lang.Terms._ import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala index b559bcf61d..0df646afd8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala @@ -1,10 +1,11 @@ package sigmastate.serialization.transformers import sigma.ast.SGroupElement +import sigma.crypto.EcPointType +import sigma.serialization.GroupElementSerializer import sigmastate.CreateProveDHTuple -import sigmastate.Values.{Value, SigmaPropValue} +import sigmastate.Values.{SigmaPropValue, Value} import sigmastate.crypto.ProveDHTuple -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.lang.Terms._ import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.utils.SigmaByteWriter._ diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala b/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala index 5693def9af..2f3f057204 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala @@ -5,9 +5,9 @@ import io.circe.Decoder import org.ergoplatform.settings.ErgoAlgos import sigma.data.{OverloadHack, RType} import scorex.utils.Ints +import sigma.crypto.EcPointType import sigma.{Coll, Colls, Environment, GroupElement} import sigmastate.eval.{CAnyValue, SigmaDsl} -import sigmastate.crypto.CryptoConstants.EcPointType import java.util import java.util.concurrent.locks.Lock diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala index dea863487f..caa2bc1619 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/GroupLawsSpecification.scala @@ -2,11 +2,9 @@ package sigmastate.crypto import java.math.BigInteger import org.scalacheck.Gen -import sigmastate.crypto.CryptoConstants -import CryptoConstants.EcPointType import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks -import sigma.crypto.{CryptoFacade, Ecp} +import sigma.crypto.{CryptoFacade, EcPointType, Ecp} import sigmastate.TestsBase import sigmastate.utils.Helpers diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index 6dfc62b55a..eeb249699c 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -4,13 +4,14 @@ import org.ergoplatform.ErgoLikeContext.Height import org.ergoplatform._ import org.ergoplatform.validation.ValidationRules import sigma.data.AvlTreeData +import sigma.serialization.GroupElementSerializer import sigma.validation.SigmaValidationSettings import sigma.{Box, Coll, Colls, Header, PreHeader} import sigmastate.crypto.CryptoConstants import sigmastate.eval._ import sigmastate.interpreter.ContextExtension import sigmastate.interpreter.ErgoTreeEvaluator.DefaultEvalSettings -import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer} +import sigmastate.serialization.SigmaSerializer object ErgoLikeContextTesting { /* NO HF PROOF: diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingCommons.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingCommons.scala index 19a8efe608..06c38d93dc 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingCommons.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingCommons.scala @@ -4,12 +4,12 @@ import sigmastate.Values.GroupElementConstant import org.scalatest.propspec.AnyPropSpec import sigmastate.TestsBase import sigmastate.helpers.TestingHelpers.createBox -import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks, ScalaCheckDrivenPropertyChecks} +import org.scalatestplus.scalacheck.{ScalaCheckDrivenPropertyChecks, ScalaCheckPropertyChecks} import sigmastate.eval.SigmaDsl import scorex.crypto.hash.Blake2b256 import org.ergoplatform.{ErgoBox, ErgoLikeContext} import org.scalatest.matchers.should.Matchers -import sigmastate.crypto.CryptoConstants.EcPointType +import sigma.crypto.EcPointType trait TestingCommons extends AnyPropSpec with ScalaCheckPropertyChecks diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala index 08d3771e7f..9ee6c96144 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala @@ -6,7 +6,6 @@ import org.scalacheck.Arbitrary._ import sigma.data.{RType, TupleColl} import sigma.ast.SCollection.SByteArray import sigmastate.Values.{BigIntConstant, ByteArrayConstant, Constant, FalseLeaf, GroupGenerator, LongConstant, SValue, TrueLeaf} -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate._ import sigmastate.eval._ import sigmastate.eval.Extensions._ @@ -15,6 +14,7 @@ import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps import scorex.util.encode.Base16 import sigma.ast._ +import sigma.crypto.EcPointType import sigma.serialization.SerializerException import sigmastate.lang.DeserializationSigmaBuilder diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 2a31b12ecc..3156588415 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -9,11 +9,11 @@ import sigmastate.Values.{ErgoTree, SigmaBoolean} import sigmastate._ import sigmastate.eval._ import sigmastate.eval.Extensions._ -import sigmastate.crypto.CryptoConstants.EcPointType import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps import sigma.ast._ import org.scalacheck.Gen +import sigma.crypto.EcPointType import sigma.serialization.SerializerException import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator} import sigmastate.interpreter.ErgoTreeEvaluator.DefaultProfiler diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala index a6c3df7f06..eac2913cc6 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala @@ -2,6 +2,7 @@ package sigmastate.serialization import sigmastate.crypto.CryptoConstants import sigma.crypto.CryptoFacade +import sigma.serialization.GroupElementSerializer import sigmastate.eval._ class GroupElementSerializerSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index a0bf169f31..079ffaf3a0 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -17,7 +17,7 @@ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{CryptoConstants, ProveDHTuple} import sigmastate.eval.Extensions._ import sigmastate.eval._ -import sigmastate.crypto.CryptoConstants.{EcPointType, dlogGroup} +import sigmastate.crypto.CryptoConstants.dlogGroup import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.lang.TransformingSigmaBuilder._ import sigmastate._ @@ -25,6 +25,7 @@ import sigmastate.utxo._ import sigma.Coll import sigma._ import sigma.ast._ +import sigma.crypto.EcPointType import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus} import sigma.validation.ValidationRules.FirstRuleId diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 9a05a24567..dfe1ff4b86 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -11,7 +11,6 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.ProveDHTuple import sigmastate.eval._ @@ -24,6 +23,7 @@ import sigmastate.utils.Helpers import sigmastate._ import sigma.Coll import sigma.ast.{SBoolean, SSigmaProp} +import sigma.crypto.EcPointType import java.math.BigInteger import scala.reflect.ClassTag diff --git a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala index c7865364e0..5fff26aa45 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala @@ -15,7 +15,7 @@ import sigmastate.lang.Terms.{Ident, Select, Val, ValueOps} import sigmastate.serialization.{OpCodes, ValueCodes} import sigmastate.utxo._ import sigma.ast._ -import sigmastate.crypto.CryptoConstants.EcPointType +import sigma.crypto.EcPointType import sigmastate.exceptions.GraphBuildingException import scala.collection.mutable.ArrayBuffer diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 85906ab1a6..b6c3b23afa 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -18,11 +18,12 @@ import sigmastate.interpreter.Interpreter.{ScriptEnv, ScriptNameProp} import sigmastate.interpreter.{ContextExtension, CostedProverResult} import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.serialization.{GroupElementSerializer, ValueSerializer} +import sigmastate.serialization.ValueSerializer import sigmastate.utils.Helpers._ import sigmastate.{CompilerCrossVersionProps, SigmaAnd} import sigma.SigmaDslTesting import sigma.ast.SType +import sigma.serialization.GroupElementSerializer import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckTypeCode diff --git a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index e11f9708bf..a89246654e 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -6,16 +6,16 @@ import org.ergoplatform.settings.ErgoAlgos import pprint.{PPrinter, Tree} import sigma.ast.SCollection.{SBooleanArray, SByteArray, SByteArray2} import sigma.ast._ +import sigma.crypto.EcPointType import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, PrimitiveType} +import sigma.serialization.GroupElementSerializer import sigma.{Coll, GroupElement} import sigmastate.Values.{ConstantNode, ErgoTree, FuncValue, ValueCompanion} import sigmastate._ -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.GF2_192_Poly import sigmastate.interpreter.{CompanionDesc, FixedCostItem, MethodDesc} import sigmastate.lang.Terms import sigmastate.lang.Terms.MethodCall -import sigmastate.serialization.GroupElementSerializer import sigmastate.utxo.SelectField import java.math.BigInteger diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index dd55f00854..ff6346feb3 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -122,7 +122,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("exceeding ergo box propositionBytes max size check") { val oversizedTree = mkTestErgoTree(SigmaAnd( - Gen.listOfN(SigmaSerializer.MaxPropositionSize / CryptoConstants.groupSize, + Gen.listOfN(SigmaSerializer.MaxPropositionSize / sigma.crypto.groupSize, proveDlogGen.map(_.toSigmaProp)).sample.get)) val b = new ErgoBoxCandidate(1L, oversizedTree, 1) val w = SigmaSerializer.startWriter() @@ -161,7 +161,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("ergo box propositionBytes max size check") { val bigTree = mkTestErgoTree(SigmaAnd( - Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / CryptoConstants.groupSize, + Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / sigma.crypto.groupSize, proveDlogGen.map(_.toSigmaProp)).sample.get)) val b = new ErgoBoxCandidate(1L, bigTree, 1) val w = SigmaSerializer.startWriter() @@ -272,7 +272,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("exceed ergo box max size check") { val bigTree = mkTestErgoTree(SigmaAnd( - Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / CryptoConstants.groupSize, + Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / sigma.crypto.groupSize, proveDlogGen.map(_.toSigmaProp)).sample.get)) val tokens = additionalTokensGen(127).sample.get.map(_.sample.get).toColl val b = new ErgoBoxCandidate(1L, bigTree, 1, tokens) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index f93af6b1d4..0a513a1ff5 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -13,17 +13,17 @@ import sigmastate.utils.Helpers._ import scorex.util.encode.Base16 import scorex.util.{ModifierId, bytesToId, idToBytes} import sigma.ast.SType -import sigma.crypto.CryptoFacade +import sigma.crypto.{CryptoFacade, EcPointType} import sigma.data.ExactIntegral.LongIsExactIntegral import sigma.data.{InverseIso, Iso, RType, SigmaConstants} +import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaBoolean, SigmaPropConstant} -import sigmastate.crypto.CryptoConstants.EcPointType import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple} import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} -import sigmastate.serialization.{ErgoTreeSerializer, GroupElementSerializer, SigmaSerializer, ValueSerializer} +import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} import java.lang.{Boolean => JBoolean, Byte => JByte, Integer => JInt, Long => JLong, Short => JShort, String => JString} import java.math.BigInteger diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index 310a089cab..eb2b6de700 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -8,11 +8,11 @@ import sigma.data.{RType, TupleColl} import sigma.ast._ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps +import sigma.crypto.EcPointType import sigma.serialization.SerializerException import sigmastate.Values.SigmaBoolean import sigmastate.eval.Extensions._ import sigmastate.eval._ -import sigmastate.crypto.CryptoConstants.EcPointType import sigma.{AvlTree, Box, Colls, Evaluation} import sigmastate.serialization.SerializationSpecification diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index 4bc855c2b3..a412aa5ac6 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -20,6 +20,7 @@ import sigma.Coll import sigma.{Header, PreHeader} import org.ergoplatform.{DataInput, ErgoBox, ErgoLikeContext, ErgoLikeTransaction, ErgoLikeTransactionTemplate, Input, UnsignedErgoLikeTransaction, UnsignedInput} import sigma.ast.SType +import sigma.crypto.EcPointType import sigma.data.AvlTreeData import scala.collection.mutable @@ -135,7 +136,7 @@ class JsonSerializationSpec extends SerializationSpecification with JsonCodecs { val minerPkHex = "0326df75ea615c18acc6bb4b517ac82795872f388d5d180aac90eaa84de750b942" val minerPk = Base16.decode(minerPkHex).map { point => ProveDlog( - CryptoConstants.dlogGroup.ctx.decodePoint(point).asInstanceOf[CryptoConstants.EcPointType] + CryptoConstants.dlogGroup.ctx.decodePoint(point) ) }.get val regs = scala.collection.Map( From 0e4551368591ac877ede33c98f0c47fca718d172 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 21:14:01 +0200 Subject: [PATCH 07/34] sigma-ast: fix after merge --- sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 22a5af92de..14b56a1e49 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -21,7 +21,7 @@ import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{SignedTransaction, import sigmastate.fleetSdkCommon.{distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesProverResultMod => proverResultMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod} import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} -import sigma.{Coll, Colls, Evaluation, GroupElement} +import sigma.{Coll, Colls, Evaluation} import sigma.Extensions.CollBytesOps import sigma.ast.SType From 1a86c84ba80b81a8e0f05f4a03df7965f50a70a0 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 10 Sep 2023 22:06:29 +0200 Subject: [PATCH 08/34] sigma-ast: fix Reflection for SigmaCompiler --- .../scala/sigmastate/lang/js/SigmaCompiler.scala | 2 +- .../scala/org/ergoplatform/ErgoScriptPredef.scala | 2 +- .../scala/sigmastate/lang/SigmaCompiler.scala | 15 ++++++++++++--- .../scala/sigmastate/eval/ErgoScriptTestkit.scala | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index d428a75294..8dd45e0063 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -58,7 +58,7 @@ object SigmaCompiler extends js.Object { * @return SigmaCompiler instance */ private def create(networkPrefix: Byte): SigmaCompiler = { - val compiler = new sigmastate.lang.SigmaCompiler(networkPrefix) + val compiler = sigmastate.lang.SigmaCompiler(networkPrefix) new SigmaCompiler(compiler) } } diff --git a/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala b/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala index 906266f6b9..9ce000448d 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala @@ -12,7 +12,7 @@ object ErgoScriptPredef { /** Compiles the given ErgoScript `code` into ErgoTree expression. */ def compileWithCosting(env: ScriptEnv, code: String, networkPrefix: NetworkPrefix)(implicit IR: IRContext): Value[SType] = { - val compiler = new SigmaCompiler(networkPrefix) + val compiler = SigmaCompiler(networkPrefix) val res = compiler.compile(env, code) res.buildTree } diff --git a/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala b/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala index a598bf643f..865d58df51 100644 --- a/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala +++ b/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala @@ -5,6 +5,7 @@ import fastparse.Parsed.Success import sigma.kiama.rewriting.Rewriter.{everywherebu, rewrite, rule} import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix import org.ergoplatform.Global +import scalan.GraphIRReflection import sigmastate.Values.{SValue, Value} import sigmastate.eval.IRContext import sigmastate.interpreter.Interpreter.ScriptEnv @@ -14,7 +15,7 @@ import sigmastate.lang.syntax.ParserException import sigmastate.utxo._ import sigma.ast._ import sigmastate.SCollectionMethods.{ExistsMethod, ForallMethod, MapMethod} -import sigmastate.{Exponentiate, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor} +import sigmastate.{Exponentiate, InterpreterReflection, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor} /** * @param networkPrefix network prefix to decode an ergo address from string (PK op) @@ -51,9 +52,9 @@ case class CompilerResult[Ctx <: IRContext]( /** Compiler which compiles ErgoScript source code into ErgoTree. * @param settings compilation parameters \ */ -class SigmaCompiler(settings: CompilerSettings) { +class SigmaCompiler private(settings: CompilerSettings) { /** Constructs an instance for the given network type and with default settings. */ - def this(networkPrefix: Byte) = this( + private def this(networkPrefix: Byte) = this( CompilerSettings(networkPrefix, TransformingSigmaBuilder, lowerMethodCalls = true) ) @@ -141,6 +142,14 @@ class SigmaCompiler(settings: CompilerSettings) { } object SigmaCompiler { + /** Force initialization of reflection before any instance of SigmaCompiler is used. */ + val _ = (InterpreterReflection, GraphIRReflection) + + /** Constructs an instance for the given settings. */ def apply(settings: CompilerSettings): SigmaCompiler = new SigmaCompiler(settings) + + /** Constructs an instance for the given network type. */ + def apply(networkPrefix: Byte): SigmaCompiler = + new SigmaCompiler(networkPrefix) } diff --git a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala index 231545d16b..a63053b9ed 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala @@ -32,7 +32,7 @@ trait ErgoScriptTestkit extends ContractsTestkit with LangTests import Context._ import Liftables._ - override lazy val compiler = new SigmaCompiler(CompilerSettings( + override lazy val compiler = SigmaCompiler(CompilerSettings( TestnetNetworkPrefix, IR.builder, lowerMethodCalls = true From 61f2bda309d20a31e9387e38614cf8047e0a4cc2 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 12:51:13 +0200 Subject: [PATCH 09/34] core-serializers: CGroupElement moved to core --- .../main/scala/sigma/data/CGroupElement.scala | 29 ++++++++++++++ .../main/scala/sigma/util/Extensions.scala | 38 +++++++++++++++++++ .../src/main/scala/sigmastate/Values.scala | 8 ++-- .../sigmastate/eval/CostingDataContext.scala | 33 ++-------------- .../scala/sigmastate/eval/Extensions.scala | 35 +---------------- .../main/scala/sigmastate/eval/Profiler.scala | 2 +- .../scala/sigma/SigmaDslSpecification.scala | 2 +- .../ErgoLikeInterpreterSpecification.scala | 2 +- .../DHTupleExampleSpecification.scala | 7 ++-- .../examples/MixExampleSpecification.scala | 6 +-- .../scala/org/ergoplatform/sdk/js/Isos.scala | 4 +- .../scala/org/ergoplatform/sdk/js/Value.scala | 4 +- .../org/ergoplatform/sdk/utils/Zero.scala | 2 +- 13 files changed, 89 insertions(+), 83 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/data/CGroupElement.scala diff --git a/core/shared/src/main/scala/sigma/data/CGroupElement.scala b/core/shared/src/main/scala/sigma/data/CGroupElement.scala new file mode 100644 index 0000000000..ed4849f0d7 --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/CGroupElement.scala @@ -0,0 +1,29 @@ +package sigma.data + +import sigma.crypto.{CryptoFacade, Ecp} +import sigma.serialization.GroupElementSerializer +import sigma.util.Extensions.EcpOps +import sigma.{BigInt, Coll, Colls, GroupElement} + +/** A default implementation of [[GroupElement]] interface. + * + * @see [[GroupElement]] for detailed descriptions + */ +case class CGroupElement(override val wrappedValue: Ecp) extends GroupElement with WrapperOf[Ecp] { + + override def toString: String = s"GroupElement(${wrappedValue.showECPoint})" + + override def getEncoded: Coll[Byte] = + Colls.fromArray(GroupElementSerializer.toBytes(wrappedValue)) + + override def isIdentity: Boolean = CryptoFacade.isInfinityPoint(wrappedValue) + + override def exp(k: BigInt): GroupElement = + CGroupElement(CryptoFacade.exponentiatePoint(wrappedValue, k.asInstanceOf[CBigInt].wrappedValue)) + + override def multiply(that: GroupElement): GroupElement = + CGroupElement(CryptoFacade.multiplyPoints(wrappedValue, that.asInstanceOf[CGroupElement].wrappedValue)) + + override def negate: GroupElement = + CGroupElement(CryptoFacade.negatePoint(wrappedValue)) +} diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index 1d38173e1f..1e35838368 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -1,5 +1,10 @@ package sigma.util +import sigma.GroupElement +import sigma.crypto.{CryptoFacade, Ecp} +import sigma.data.CGroupElement +import debox.{cfor, Buffer => DBuffer} + import java.math.BigInteger import java.nio.ByteBuffer @@ -269,4 +274,37 @@ object Extensions { self } } + + implicit class EcpOps(val source: Ecp) extends AnyVal { + /** Extracts [[sigma.GroupElement]] from the Ecp instance. */ + def toGroupElement: GroupElement = CGroupElement(source) + + /** Shortened String representation of `source` GroupElement. */ + def showECPoint: String = { + if (source.toGroupElement.isIdentity) { + "IDENTITY" + } + else { + CryptoFacade.showPoint(source) + } + } + } + + implicit class GroupElementOps(val source: GroupElement) extends AnyVal { + def toECPoint: Ecp = source.asInstanceOf[CGroupElement].wrappedValue + /** Shortened String representation of `source` GroupElement. */ + def showToString: String = toECPoint.showECPoint + } + + implicit class DBufferOps[A](val buf: DBuffer[A]) extends AnyVal { + /** Sum all values in `buf` using the given Numeric. */ + def sumAll(implicit n: Numeric[A]): A = { + val limit = buf.length + var result: A = n.zero + cfor(0)(_ < limit, _ + 1) { i => + result = n.plus(result, buf.elems(i)) + } + result + } + } } diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 2a39cd792d..cef6917f51 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -17,7 +17,7 @@ import sigmastate.lang.Terms._ import sigmastate.utxo._ import sigmastate.eval._ import sigmastate.eval.Extensions._ -import sigma.util.Extensions.ByteOps +import sigma.util.Extensions._ import sigmastate.interpreter.ErgoTreeEvaluator._ import debox.cfor import scorex.util.encode.Base16 @@ -345,7 +345,7 @@ object Values { override def toString: String = tpe.asInstanceOf[SType] match { case SGroupElement if value.isInstanceOf[GroupElement] => - s"ConstantNode(${showECPoint(value.asInstanceOf[GroupElement])},$tpe)" + s"ConstantNode(${value.asInstanceOf[GroupElement].showToString},$tpe)" case SGroupElement => sys.error(s"Invalid value in Constant($value, $tpe)") case SInt => s"IntConstant($value)" @@ -976,9 +976,9 @@ object Values { def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb)) def showToString: String = sb match { case ProveDlog(v) => - s"ProveDlog(${showECPoint(v)})" + s"ProveDlog(${v.showECPoint})" case ProveDHTuple(gv, hv, uv, vv) => - s"ProveDHTuple(${showECPoint(gv)}, ${showECPoint(hv)}, ${showECPoint(uv)}, ${showECPoint(vv)})" + s"ProveDHTuple(${gv.showECPoint}, ${hv.showECPoint}, ${uv.showECPoint}, ${vv.showECPoint})" case _ => sb.toString } } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 4036221d33..ae22d09018 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -7,15 +7,15 @@ import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADDigest, ADKey, ADValue, SerializedAdProof} import scorex.crypto.hash.{Blake2b256, Digest32, Sha256} import scorex.utils.{Ints, Longs} -import sigma.{VersionContext, _} import sigma.ast.SCollection.SByteArray import sigma.ast.{SInt, STuple, SType} -import sigma.crypto.{CryptoFacade, EcPointType, Ecp} +import sigma.crypto.{EcPointType, Ecp} import sigma.data.OverloadHack.Overloaded1 -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType, SigmaConstants, WrapperOf} +import sigma.data._ import sigma.serialization.GroupElementSerializer import sigma.util.Extensions.BigIntegerOps import sigma.validation.SigmaValidationSettings +import sigma.{VersionContext, _} import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue, SigmaBoolean} import sigmastate._ @@ -32,33 +32,6 @@ import scala.annotation.unused import scala.reflect.ClassTag import scala.util.{Failure, Success} - - - - -/** A default implementation of [[GroupElement]] interface. - * @see [[GroupElement]] for detailed descriptions - */ -case class CGroupElement(override val wrappedValue: Ecp) extends GroupElement with WrapperOf[Ecp] { - val dsl = CostingSigmaDslBuilder - - override def toString: String = s"GroupElement(${sigmastate.eval.Extensions.showECPoint(wrappedValue)})" - - override def getEncoded: Coll[Byte] = - dsl.Colls.fromArray(GroupElementSerializer.toBytes(wrappedValue)) - - override def isIdentity: Boolean = CryptoFacade.isInfinityPoint(wrappedValue) - - override def exp(k: BigInt): GroupElement = - dsl.GroupElement(CryptoFacade.exponentiatePoint(wrappedValue, k.asInstanceOf[CBigInt].wrappedValue)) - - override def multiply(that: GroupElement): GroupElement = - dsl.GroupElement(CryptoFacade.multiplyPoints(wrappedValue, that.asInstanceOf[CGroupElement].wrappedValue)) - - override def negate: GroupElement = - dsl.GroupElement(CryptoFacade.negatePoint(wrappedValue)) -} - /** A default implementation of [[SigmaProp]] interface. * @see [[SigmaProp]] for detailed descriptions */ diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 687968087c..eebc409a7a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -1,16 +1,14 @@ package sigmastate.eval -import debox.{cfor, Buffer => DBuffer} import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.TokenId import scorex.util.encode.Base16 import sigma.ast.SType.AnyOps import sigma.ast.{SCollection, SCollectionType, SType} -import sigma.{Coll, _} import sigma.data.{Nullable, RType} +import sigma.{Coll, _} import sigmastate.Platform import sigmastate.Values.{Constant, ConstantNode} -import sigma.crypto.{CryptoFacade, Ecp} import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers @@ -104,35 +102,4 @@ object Extensions { } } - /** Shortened String representation of `source` GroupElement. */ - def showECPoint(p: Ecp): String = { - if (p.isIdentity) { - "IDENTITY" - } - else { - CryptoFacade.showPoint(p) - } - } - - implicit class EcpOps(val source: Ecp) extends AnyVal { - /** Extracts [[GroupElement]] from the Ecp instance. */ - def toGroupElement: GroupElement = SigmaDsl.GroupElement(source) - } - - implicit class GroupElementOps(val source: GroupElement) extends AnyVal { - /** Shortened String representation of `source` GroupElement. */ - def showToString: String = showECPoint(source.asInstanceOf[CGroupElement].wrappedValue) - } - - implicit class DBufferOps[A](val buf: DBuffer[A]) extends AnyVal { - /** Sum all values in `buf` using the given Numeric. */ - def sumAll(implicit n: Numeric[A]): A = { - val limit = buf.length - var result: A = n.zero - cfor(0)(_ < limit, _ + 1) { i => - result = n.plus(result, buf.elems(i)) - } - result - } - } } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala index fb127568b2..c4b9d5b8b4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala @@ -7,7 +7,7 @@ import sigmastate.serialization.ValueSerializer.getSerializer import sigma.util.Extensions.ByteOps import debox.{Buffer => DBuffer, Map => DMap} import debox.sp -import sigmastate.eval.Extensions.DBufferOps +import sigma.util.Extensions.DBufferOps import sigmastate.interpreter.{CostItem, FixedCostItem, SeqCostItem, TypeBasedCostItem} import sigmastate.lang.Terms.{MethodCall, PropertyCall} import sigmastate.serialization.ValueCodes.OpCode diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index a8f2daf7ed..7c7f6f32d8 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4,7 +4,7 @@ import java.math.BigInteger import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, ExactIntegral, ExactNumeric, ExactOrdering, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, ExactIntegral, ExactNumeric, ExactOrdering, RType} import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index b36ee92fcb..213b0eb204 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -10,9 +10,9 @@ import sigmastate.Values._ import sigmastate._ import sigma.ast._ import sigma.data.AvlTreeData +import sigma.util.Extensions.EcpOps import sigma.validation.ValidationException import sigmastate.eval._ -import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter._ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.ProveDHTuple diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala index b774f42617..809d691836 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala @@ -2,18 +2,17 @@ package sigmastate.utxo.examples import java.math.BigInteger - import org.ergoplatform.ErgoBox.{R4, R5} import sigma.data.AvlTreeData +import sigma.util.Extensions.EcpOps import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple, CryptoConstants} -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} +import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ -import sigmastate.eval.Extensions._ class DHTupleExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index 1fe4fda743..17a7abce14 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -1,15 +1,15 @@ package sigmastate.utxo.examples import java.math.BigInteger - import org.ergoplatform.ErgoBox.{R4, R5} import scorex.crypto.hash.Blake2b256 import sigma.data.AvlTreeData +import sigma.util.Extensions.EcpOps import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple, CryptoConstants} -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} +import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 14b56a1e49..32e57fb0d5 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -6,13 +6,13 @@ import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.ergoplatform.sdk.wallet.protocol.context import org.ergoplatform._ import org.ergoplatform.sdk.Iso.inverseIso -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, Iso, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, Iso, RType} import scorex.crypto.authds.ADKey import scorex.util.ModifierId import scorex.util.encode.Base16 import sigmastate.Values.{Constant, GroupElementConstant} import sigmastate.eval.Extensions.ArrayOps -import sigmastate.eval.{CAvlTree, CGroupElement, CHeader, CPreHeader, Digest32Coll} +import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, Digest32Coll} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index dd2808e001..c1c89d4b9a 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -1,13 +1,13 @@ package org.ergoplatform.sdk.js import org.ergoplatform.sdk.js.Value.toRuntimeData -import sigma.data.{CollType, RType} +import sigma.data.{CGroupElement, CollType, RType} import sigma.data.PairType import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType import sigma.crypto.Platform -import sigmastate.eval.{CAvlTree, CGroupElement, CSigmaProp, CostingBox, SigmaDsl} +import sigmastate.eval.{CAvlTree, CSigmaProp, CostingBox, SigmaDsl} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index acbfb75401..b6cce164a3 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.utils import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CollType, FuncType, OptionType, PairType, RType, TupleType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CollType, FuncType, OptionType, PairType, RType, TupleType} import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} From 0c7e09a8f2df710a2724f6c84d75abc13ded5ba1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 12:54:53 +0200 Subject: [PATCH 10/34] core-serializers: fix compilation Scala 2.11 --- .../src/main/scala/sigma/validation/ValidationRules.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shared/src/main/scala/sigma/validation/ValidationRules.scala b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala index bb68d585a4..6e5d84f0a3 100644 --- a/core/shared/src/main/scala/sigma/validation/ValidationRules.scala +++ b/core/shared/src/main/scala/sigma/validation/ValidationRules.scala @@ -188,7 +188,7 @@ object ValidationRules { CheckTypeCode, CheckSerializableTypeCode, CheckTypeWithMethods, - CheckPositionLimit, + CheckPositionLimit ) /** Validation settings that correspond to the current version of the ErgoScript implementation. From 198e02839c5ed5dfb61d74f1703fc0ca0d3eec26 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 13:06:20 +0200 Subject: [PATCH 11/34] core-serializers: removed toBits unfinished method --- .../src/main/scala/sigma/SigmaDsl.scala | 6 ------ .../src/main/scala/sigma/data/CBigInt.scala | 2 -- .../scala/sigmastate/utils/Extensions.scala | 21 ------------------- .../scala/sigma/SigmaDslSpecification.scala | 16 +++++--------- 4 files changed, 5 insertions(+), 40 deletions(-) diff --git a/core/shared/src/main/scala/sigma/SigmaDsl.scala b/core/shared/src/main/scala/sigma/SigmaDsl.scala index 7609d38356..a8827db0fa 100644 --- a/core/shared/src/main/scala/sigma/SigmaDsl.scala +++ b/core/shared/src/main/scala/sigma/SigmaDsl.scala @@ -37,12 +37,6 @@ trait BigInt { */ def toBytes: Coll[Byte] - /** Returns a big-endian representation of this BigInt in a collection of Booleans. - * Each boolean corresponds to one bit of the representation. - * @since 2.0 - */ - def toBits: Coll[Boolean] - /** Absolute value of this numeric value. * @since 2.0 */ diff --git a/core/shared/src/main/scala/sigma/data/CBigInt.scala b/core/shared/src/main/scala/sigma/data/CBigInt.scala index bcc9e8c09e..6d759a34ce 100644 --- a/core/shared/src/main/scala/sigma/data/CBigInt.scala +++ b/core/shared/src/main/scala/sigma/data/CBigInt.scala @@ -26,8 +26,6 @@ case class CBigInt(override val wrappedValue: BigInteger) extends BigInt with Wr override def compareTo(that: BigInt): Int = wrappedValue.compareTo(that.asInstanceOf[CBigInt].wrappedValue) - override def toBits: Coll[Boolean] = ??? - override def modQ: BigInt = ??? override def plusModQ(other: BigInt): BigInt = ??? diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala index 9490f76965..111dd53303 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala @@ -14,10 +14,6 @@ object Extensions { */ def toBytes: Coll[Byte] = SigmaDsl.Colls.fromItems(b) - /** Returns a big-endian representation of this numeric in a collection of Booleans. - * Each boolean corresponds to one bit. - */ - def toBits: Coll[Boolean] = ??? } implicit class ShortOpsForSigma(val x: Short) extends AnyVal { @@ -26,11 +22,6 @@ object Extensions { * byte array {@code {0x12, 0x13}}. */ def toBytes: Coll[Byte] = Colls.fromArray(Shorts.toByteArray(x)) - - /** Returns a big-endian representation of this numeric in a collection of Booleans. - * Each boolean corresponds to one bit. - */ - def toBits: Coll[Boolean] = ??? } implicit class IntOpsForSigma(val x: Int) extends AnyVal { @@ -39,11 +30,6 @@ object Extensions { * byte array {@code {0x12, 0x13, 0x14, 0x15}}. */ def toBytes: Coll[Byte] = Colls.fromArray(Ints.toByteArray(x)) - - /** Returns a big-endian representation of this numeric in a collection of Booleans. - * Each boolean corresponds to one bit. - */ - def toBits: Coll[Boolean] = ??? } implicit class LongOpsForSigma(val x: Long) extends AnyVal { @@ -52,13 +38,6 @@ object Extensions { * byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}}. */ def toBytes: Coll[Byte] = Colls.fromArray(Longs.toByteArray(x)) - - /** Returns a big-endian representation of this numeric in a collection of Booleans. - * Each boolean corresponds to one bit. - * - * @since 2.0 - */ - def toBits: Coll[Boolean] = ??? } /** Provides extension methods for `ModifierId` instances. diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 7c7f6f32d8..b46f0b6675 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -1067,7 +1067,6 @@ class SigmaDslSpecification extends SigmaDslTesting property("Byte methods equivalence (new features)") { lazy val toBytes = newFeature((x: Byte) => x.toBytes, "{ (x: Byte) => x.toBytes }") - lazy val toBits = newFeature((x: Byte) => x.toBits, "{ (x: Byte) => x.toBits }") lazy val toAbs = newFeature((x: Byte) => x.toAbs, "{ (x: Byte) => x.toAbs }") lazy val compareTo = newFeature( (x: (Byte, Byte)) => x._1.compareTo(x._2), @@ -1082,7 +1081,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: (Byte, Byte)) => (x._1 & x._2).toByteExact }") forAll { x: Byte => - Seq(toBytes, toBits, toAbs).foreach(f => f.checkEquality(x)) + Seq(toBytes, toAbs).foreach(f => f.checkEquality(x)) } forAll { x: (Byte, Byte) => @@ -1372,7 +1371,6 @@ class SigmaDslSpecification extends SigmaDslTesting property("Short methods equivalence (new features)") { lazy val toBytes = newFeature((x: Short) => x.toBytes, "{ (x: Short) => x.toBytes }") - lazy val toBits = newFeature((x: Short) => x.toBits, "{ (x: Short) => x.toBits }") lazy val toAbs = newFeature((x: Short) => x.toAbs, "{ (x: Short) => x.toAbs }") lazy val compareTo = newFeature((x: (Short, Short)) => x._1.compareTo(x._2), @@ -1387,7 +1385,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: (Short, Short)) => x._1 & x._2 }") forAll { x: Short => - Seq(toBytes, toBits, toAbs).foreach(_.checkEquality(x)) + Seq(toBytes, toAbs).foreach(_.checkEquality(x)) } forAll { x: (Short, Short) => Seq(compareTo, bitOr, bitAnd).foreach(_.checkEquality(x)) @@ -1676,7 +1674,6 @@ class SigmaDslSpecification extends SigmaDslTesting property("Int methods equivalence (new features)") { lazy val toBytes = newFeature((x: Int) => x.toBytes, "{ (x: Int) => x.toBytes }") - lazy val toBits = newFeature((x: Int) => x.toBits, "{ (x: Int) => x.toBits }") lazy val toAbs = newFeature((x: Int) => x.toAbs, "{ (x: Int) => x.toAbs }") lazy val compareTo = newFeature((x: (Int, Int)) => x._1.compareTo(x._2), "{ (x: (Int, Int)) => x._1.compareTo(x._2) }") @@ -1690,7 +1687,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: (Int, Int)) => x._1 & x._2 }") forAll { x: Int => - Seq(toBytes, toBits, toAbs).foreach(_.checkEquality(x)) + Seq(toBytes, toAbs).foreach(_.checkEquality(x)) } forAll { x: (Int, Int) => Seq(compareTo, bitOr, bitAnd).foreach(_.checkEquality(x)) @@ -1996,9 +1993,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("Long methods equivalence (new features)") { lazy val toBytes = newFeature((x: Long) => x.toBytes, "{ (x: Long) => x.toBytes }") - lazy val toBits = newFeature((x: Long) => x.toBits, "{ (x: Long) => x.toBits }") lazy val toAbs = newFeature((x: Long) => x.toAbs, "{ (x: Long) => x.toAbs }") - lazy val compareTo = newFeature((x: (Long, Long)) => x._1.compareTo(x._2), "{ (x: (Long, Long)) => x._1.compareTo(x._2) }") @@ -2011,7 +2006,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: (Long, Long)) => x._1 & x._2 }") forAll { x: Long => - Seq(toBytes, toBits, toAbs).foreach(_.checkEquality(x)) + Seq(toBytes, toAbs).foreach(_.checkEquality(x)) } forAll { x: (Long, Long) => Seq(compareTo, bitOr, bitAnd).foreach(_.checkEquality(x)) @@ -2310,7 +2305,6 @@ class SigmaDslSpecification extends SigmaDslTesting FuncValue(Vector((1, SBigInt)), Downcast(ValUse(1, SBigInt), SLong))) lazy val toBytes = newFeature((x: BigInt) => x.toBytes, "{ (x: BigInt) => x.toBytes }") - lazy val toBits = newFeature((x: BigInt) => x.toBits, "{ (x: BigInt) => x.toBits }") lazy val toAbs = newFeature((x: BigInt) => x.toAbs, "{ (x: BigInt) => x.toAbs }") lazy val compareTo = newFeature((x: (BigInt, BigInt)) => x._1.compareTo(x._2), @@ -2323,7 +2317,7 @@ class SigmaDslSpecification extends SigmaDslTesting "{ (x: (BigInt, BigInt)) => x._1 & x._2 }") forAll { x: BigInt => - Seq(toByte, toShort, toInt, toLong, toBytes, toBits, toAbs).foreach(_.checkEquality(x)) + Seq(toByte, toShort, toInt, toLong, toBytes, toAbs).foreach(_.checkEquality(x)) } forAll { x: (BigInt, BigInt) => Seq(compareTo, bitOr, bitAnd).foreach(_.checkEquality(x)) From 5b07d30ae0766a7b4a2a455daa027f232f09ea15 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 13:08:58 +0200 Subject: [PATCH 12/34] core-serializers: removed all xxxQ methods from BigInt --- .../src/main/scala/sigma/SigmaDsl.scala | 25 ------------------- .../src/main/scala/sigma/data/CBigInt.scala | 10 -------- 2 files changed, 35 deletions(-) diff --git a/core/shared/src/main/scala/sigma/SigmaDsl.scala b/core/shared/src/main/scala/sigma/SigmaDsl.scala index a8827db0fa..818ebafb22 100644 --- a/core/shared/src/main/scala/sigma/SigmaDsl.scala +++ b/core/shared/src/main/scala/sigma/SigmaDsl.scala @@ -47,31 +47,6 @@ trait BigInt { */ def compareTo(that: BigInt): Int - /** Returns this `mod` Q, i.e. remainder of division by Q, where Q is an order of the cryprographic group. - * @since 2.0 - */ - def modQ: BigInt - - /** Adds this number with `other` by module Q. - * @since 2.0 - */ - def plusModQ(other: BigInt): BigInt - - /** Subtracts this number with `other` by module Q. - * @since 2.0 - */ - def minusModQ(other: BigInt): BigInt - - /** Multiply this number with `other` by module Q. - * @since 2.0 - */ - def multModQ(other: BigInt): BigInt - - /** Multiply this number with `other` by module Q. - * @since Mainnet - */ - def inverseModQ: BigInt // ??? @kushti do we need it - /** Returns the signum function of this BigInt. * * @return -1, 0 or 1 as the value of this BigInt is negative, zero or diff --git a/core/shared/src/main/scala/sigma/data/CBigInt.scala b/core/shared/src/main/scala/sigma/data/CBigInt.scala index 6d759a34ce..bbf1a85e46 100644 --- a/core/shared/src/main/scala/sigma/data/CBigInt.scala +++ b/core/shared/src/main/scala/sigma/data/CBigInt.scala @@ -26,16 +26,6 @@ case class CBigInt(override val wrappedValue: BigInteger) extends BigInt with Wr override def compareTo(that: BigInt): Int = wrappedValue.compareTo(that.asInstanceOf[CBigInt].wrappedValue) - override def modQ: BigInt = ??? - - override def plusModQ(other: BigInt): BigInt = ??? - - override def minusModQ(other: BigInt): BigInt = ??? - - override def multModQ(other: BigInt): BigInt = ??? - - override def inverseModQ: BigInt = ??? - override def signum: Int = wrappedValue.signum() override def add(that: BigInt): BigInt = CBigInt(wrappedValue.add(that.asInstanceOf[CBigInt].wrappedValue).to256BitValueExact) From 637ecff8604c6a133a64870092c2cf2e141987ef Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 15:02:54 +0200 Subject: [PATCH 13/34] core-serializers: introduced SPCodes --- .../src/main/scala/sigma/ast/TypeCodes.scala | 15 ++++++++ .../main/scala/sigma/util/Extensions.scala | 10 +++++- .../validation/ValidationRules.scala | 3 +- .../src/main/scala/sigmastate/Values.scala | 8 +++-- .../sigmastate/crypto/DLogProtocol.scala | 23 +++++-------- .../crypto/DiffieHellmanTupleProtocol.scala | 15 ++++---- .../main/scala/sigmastate/eval/Profiler.scala | 3 +- .../serialization/DataSerializer.scala | 5 +-- .../sigmastate/serialization/OpCodes.scala | 34 ++++++++++--------- .../serialization/ValueSerializer.scala | 8 ++--- .../src/main/scala/sigmastate/trees.scala | 11 +++--- .../sigmastate/utxo/ComplexityTableStat.scala | 4 +-- .../scala/sigmastate/eval/GraphBuilding.scala | 3 +- .../SoftForkabilitySpecification.scala | 3 +- .../test/scala/sigmastate/utils/SpecGen.scala | 5 +-- 15 files changed, 88 insertions(+), 62 deletions(-) diff --git a/core/shared/src/main/scala/sigma/ast/TypeCodes.scala b/core/shared/src/main/scala/sigma/ast/TypeCodes.scala index 605378f5f5..1b7b6121d6 100644 --- a/core/shared/src/main/scala/sigma/ast/TypeCodes.scala +++ b/core/shared/src/main/scala/sigma/ast/TypeCodes.scala @@ -18,4 +18,19 @@ object TypeCodes { val FirstFuncType: TypeCode = TypeCode @@ (LastDataType + 1).toByte val LastFuncType : TypeCode = TypeCode @@ 255.toByte + + /** We use optimized encoding of constant values to save space in serialization. + * Since Box registers are stored as Constant nodes we save 1 byte for each register. + * This is due to convention that Value.opCode falling in [1..LastDataType] region is a constant. + * Thus, we can just decode an instance of SType and then decode data using DataSerializer. + * + * Decoding of constants depends on the first byte and in general is a recursive procedure + * consuming some number of bytes from Reader. + * */ + val ConstantCode: Byte = 0.toByte + + /** The last constant code is equal to FirstFuncType which represent generic function type. + * We use this single code to represent all functional constants, since we don't have enough space in single byte. + * Subsequent bytes have to be read from Reader in order to decode the type of the function and the corresponding data. */ + val LastConstantCode: Byte = (TypeCodes.LastDataType + 1).toByte } diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index 1e35838368..deef8c998e 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -2,7 +2,7 @@ package sigma.util import sigma.GroupElement import sigma.crypto.{CryptoFacade, Ecp} -import sigma.data.CGroupElement +import sigma.data.{CBigInt, CGroupElement} import debox.{cfor, Buffer => DBuffer} import java.math.BigInteger @@ -216,6 +216,14 @@ object Extensions { else throw new ArithmeticException("BigInteger out of 256 bit range"); } + + /** Converts `x` to [[sigma.BigInt]] */ + def toBigInt: sigma.BigInt = CBigInt(x) + } + + implicit class BigIntOps(val x: sigma.BigInt) extends AnyVal { + /** Converts `x` to [[java.math.BigInteger]] */ + def toBigInteger: BigInteger = x.asInstanceOf[CBigInt].wrappedValue } implicit class OptionOps[T](val opt: Option[T]) extends AnyVal { diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala index c58bff6da2..4825624117 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala @@ -1,6 +1,7 @@ package org.ergoplatform.validation import sigma.SigmaException +import sigma.ast.TypeCodes.LastConstantCode import sigma.serialization.{InvalidOpCode, SerializerException} import sigma.util.Extensions.toUByte import sigma.validation.ValidationRules._ @@ -59,7 +60,7 @@ object ValidationRules { checkRule() if (ser == null || ser.opCode != opCode) { throwValidationException( - new InvalidOpCode(s"Cannot find serializer for Value with opCode = LastConstantCode + ${toUByte(opCode) - ValueCodes.LastConstantCode}"), + new InvalidOpCode(s"Cannot find serializer for Value with opCode = LastConstantCode + ${toUByte(opCode) - LastConstantCode}"), Array(opCode)) } } diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index cef6917f51..fd27c3ce6f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -22,6 +22,7 @@ import sigmastate.interpreter.ErgoTreeEvaluator._ import debox.cfor import scorex.util.encode.Base16 import sigma.ast.SCollection.{SByteArray, SIntArray} +import sigma.ast.TypeCodes.ConstantCode import sigma.ast._ import sigma.crypto.EcPointType import sigmastate.exceptions.InterpreterException @@ -35,7 +36,8 @@ import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} import sigmastate.lang.SourceContext import sigma.util.safeNewArray import sigma.validation.ValidationException -import sigmastate.serialization.ValueCodes.{ConstantCode, OpCode} +import sigmastate.serialization.SigmaPropCodes.{ProveDlogCode, SPCode} +import sigmastate.serialization.ValueCodes.OpCode import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable @@ -357,7 +359,7 @@ object Values { } object Constant extends FixedCostValueCompanion { - override def opCode: OpCode = ConstantCode + override def opCode: OpCode = OpCode @@ ConstantCode /** Cost of: returning value from Constant node. */ override val costKind = FixedCost(JitCost(5)) @@ -734,7 +736,7 @@ object Values { */ trait SigmaBoolean { /** Unique id of the node class used in serialization of SigmaBoolean. */ - val opCode: OpCode + val opCode: SPCode /** Size of the proposition tree (number of nodes). */ def size: Int } diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala index 31f877b4fa..e41a395774 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala @@ -1,18 +1,17 @@ package sigmastate.crypto -import java.math.BigInteger -import sigmastate.Values._ -import Value.PropositionCode import scorex.util.encode.Base16 -import sigmastate._ -import sigmastate.eval._ -import sigmastate.crypto.VerifierMessage.Challenge -import CryptoConstants.dlogGroup -import sigmastate.serialization.OpCodes import sigma.SigmaProp import sigma.crypto.EcPointType import sigma.serialization.GroupElementSerializer -import sigmastate.serialization.ValueCodes.OpCode +import sigmastate._ +import sigmastate.crypto.CryptoConstants.dlogGroup +import sigmastate.crypto.VerifierMessage.Challenge +import sigmastate.eval._ +import sigmastate.serialization.SigmaPropCodes +import sigmastate.serialization.SigmaPropCodes.SPCode + +import java.math.BigInteger object DLogProtocol { @@ -24,15 +23,11 @@ object DLogProtocol { /** Construct a new SigmaBoolean value representing public key of discrete logarithm signature protocol. */ case class ProveDlog(value: EcPointType) extends SigmaLeaf { override def size: Int = 1 - override val opCode: OpCode = OpCodes.ProveDlogCode + override val opCode: SPCode = SigmaPropCodes.ProveDlogCode /** Serialized bytes of the elliptic curve point (using GroupElementSerializer). */ lazy val pkBytes: Array[Byte] = GroupElementSerializer.toBytes(value) } - object ProveDlog { - val Code: PropositionCode = 102: Byte - } - /** Helper extractor to match SigmaProp values and extract ProveDlog out of it. */ object ProveDlogProp { def unapply(p: SigmaProp): Option[ProveDlog] = SigmaDsl.toSigmaBoolean(p) match { diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala index 1e4c2ee8b1..9a850ed323 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala @@ -1,15 +1,16 @@ package sigmastate.crypto -import java.math.BigInteger +import sigma.SigmaProp +import sigma.crypto.EcPointType +import sigma.serialization.GroupElementSerializer import sigmastate.Values.Value.PropositionCode import sigmastate._ import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.eval.SigmaDsl -import sigmastate.serialization.OpCodes -import sigma.SigmaProp -import sigma.crypto.EcPointType -import sigma.serialization.GroupElementSerializer -import sigmastate.serialization.ValueCodes.OpCode +import sigmastate.serialization.SigmaPropCodes +import sigmastate.serialization.SigmaPropCodes.SPCode + +import java.math.BigInteger trait DiffieHellmanTupleProtocol extends SigmaProtocol[DiffieHellmanTupleProtocol] { @@ -70,7 +71,7 @@ case class SecondDHTupleProverMessage(z: BigInteger) extends SecondProverMessage * Common input: (g,h,u,v) */ case class ProveDHTuple(gv: EcPointType, hv: EcPointType, uv: EcPointType, vv: EcPointType) extends SigmaLeaf { - override val opCode: OpCode = OpCodes.ProveDiffieHellmanTupleCode + override val opCode: SPCode = SigmaPropCodes.ProveDiffieHellmanTupleCode override def size: Int = 4 // one node for each EcPoint lazy val g = gv lazy val h = hv diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala index c4b9d5b8b4..5a28c5ea0b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Profiler.scala @@ -7,6 +7,7 @@ import sigmastate.serialization.ValueSerializer.getSerializer import sigma.util.Extensions.ByteOps import debox.{Buffer => DBuffer, Map => DMap} import debox.sp +import sigma.ast.TypeCodes.LastConstantCode import sigma.util.Extensions.DBufferOps import sigmastate.interpreter.{CostItem, FixedCostItem, SeqCostItem, TypeBasedCostItem} import sigmastate.lang.Terms.{MethodCall, PropertyCall} @@ -256,7 +257,7 @@ class Profiler { val suggestedCost = suggestCost(time) val warn = if (suggestedCost > cost) "!!!" else "" val comment = s"count: $count, suggestedCost: $suggestedCost, actualCost: $cost$warn" - (opName, (opCode.toUByte - ValueCodes.LastConstantCode).toString, time, comment) + (opName, (opCode.toUByte - LastConstantCode).toString, time, comment) }.filter(line => line != null && line._1.nonEmpty) .sortBy(_._3)(Ordering[Long].reverse) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 0a08e26082..2e5deba2d5 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -12,6 +12,7 @@ import sigma.{Evaluation, _} import debox.cfor import sigma.ast._ import sigma.serialization.{GroupElementSerializer, SerializerException} +import sigma.util.Extensions.{BigIntOps, GroupElementOps} import sigma.validation.ValidationRules.CheckSerializableTypeCode import scala.collection.mutable @@ -35,11 +36,11 @@ object DataSerializer { w.putUInt(bytes.length) w.putBytes(bytes) case SBigInt => - val data = SigmaDsl.toBigInteger(v.asInstanceOf[BigInt]).toByteArray + val data = v.asInstanceOf[BigInt].toBigInteger.toByteArray w.putUShort(data.length) w.putBytes(data) case SGroupElement => - GroupElementSerializer.serialize(groupElementToECPoint(v.asInstanceOf[GroupElement]), w) + GroupElementSerializer.serialize(v.asInstanceOf[GroupElement].toECPoint, w) case SSigmaProp => val p = v.asInstanceOf[SigmaProp] SigmaBoolean.serializer.serialize(sigmaPropToSigmaBoolean(p), w) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala index 2e0469ffce..e6c74eb24e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala @@ -1,28 +1,30 @@ package sigmastate.serialization -import sigma.ast.TypeCodes -import sigmastate.serialization.ValueCodes.OpCode +import sigma.ast.TypeCodes.LastConstantCode import supertagged.TaggedType +/** Encoding of sigma proposition nodes. + * @see SigmaBoolean.opCode + */ +object SigmaPropCodes { + object SPCode extends TaggedType[Byte] + type SPCode = SPCode.Type + private def newOpCode(shift: Short): SPCode = SPCode @@ (LastConstantCode + shift).toByte + + val AndCode : SPCode = newOpCode(38) + val OrCode : SPCode = newOpCode(39) + val AtLeastCode : SPCode = newOpCode(40) + val ProveDlogCode : SPCode = newOpCode(93) + val ProveDiffieHellmanTupleCode: SPCode = newOpCode(94) + val TrivialPropFalseCode : SPCode = newOpCode(98) + val TrivialPropTrueCode : SPCode = newOpCode(99) +} + /** Encoding of values for serialization. */ object ValueCodes { object OpCode extends TaggedType[Byte] type OpCode = OpCode.Type - /** We use optimized encoding of constant values to save space in serialization. - * Since Box registers are stored as Constant nodes we save 1 byte for each register. - * This is due to convention that Value.opCode falling in [1..LastDataType] region is a constant. - * Thus, we can just decode an instance of SType and then decode data using DataSerializer. - * - * Decoding of constants depends on the first byte and in general is a recursive procedure - * consuming some number of bytes from Reader. - * */ - val ConstantCode: OpCode = OpCode @@ 0.toByte - - /** The last constant code is equal to FirstFuncType which represent generic function type. - * We use this single code to represent all functional constants, since we don't have enough space in single byte. - * Subsequent bytes have to be read from Reader in order to decode the type of the function and the corresponding data. */ - val LastConstantCode: OpCode = OpCode @@ (TypeCodes.LastDataType + 1).toByte } /** The set of all possible IR graph nodes can be split in two subsets: diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala index 62dc1637be..0efbd5234b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ValueSerializer.scala @@ -1,25 +1,23 @@ package sigmastate.serialization -import org.ergoplatform.validation.ValidationRules.CheckValidOpCode import org.ergoplatform._ +import org.ergoplatform.validation.ValidationRules.CheckValidOpCode import sigma.ast.SCollection.SByteArray +import sigma.ast.TypeCodes.LastConstantCode import sigma.ast._ import sigma.serialization.CoreByteWriter.DataInfo import sigma.util.Extensions.toUByte import sigmastate.Values._ import sigmastate._ import sigmastate.lang.DeserializationSigmaBuilder -import sigmastate.serialization.ValueCodes.{LastConstantCode, OpCode} +import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization.transformers._ import sigmastate.serialization.trees.{QuadrupleSerializer, Relation2Serializer} -import sigmastate.utils.SigmaByteWriter._ import sigmastate.utils._ import sigmastate.utxo.ComplexityTable._ import sigmastate.utxo._ -import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable -import scala.collection.mutable.{HashMap, Map} abstract class ValueSerializer[V <: Value[SType]] extends SigmaSerializer[Value[SType], V] { import scala.language.implicitConversions diff --git a/interpreter/shared/src/main/scala/sigmastate/trees.scala b/interpreter/shared/src/main/scala/sigmastate/trees.scala index 4d7e831580..6b29c34cfe 100644 --- a/interpreter/shared/src/main/scala/sigmastate/trees.scala +++ b/interpreter/shared/src/main/scala/sigmastate/trees.scala @@ -22,6 +22,7 @@ import sigmastate.eval.SigmaDsl import sigmastate.interpreter.ErgoTreeEvaluator import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv import sigmastate.serialization.OpCodes._ +import sigmastate.serialization.SigmaPropCodes.SPCode import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization._ import sigmastate.utxo.{SimpleTransformerCompanion, Transformer} @@ -51,7 +52,7 @@ trait SigmaLeaf extends SigmaBoolean */ case class CAND(override val children: Seq[SigmaBoolean]) extends SigmaConjecture { /** The same code is used for AND operation, but they belong to different type hierarchies. */ - override val opCode: OpCode = OpCodes.AndCode + override val opCode: SPCode = SigmaPropCodes.AndCode override val size: Int = SigmaBoolean.totalSize(children) + 1 } @@ -87,7 +88,7 @@ object CAND { */ case class COR(children: Seq[SigmaBoolean]) extends SigmaConjecture { /** The same code is also used for OR operation, but they belong to different type hierarchies. */ - override val opCode: OpCode = OpCodes.OrCode + override val opCode: SPCode = SigmaPropCodes.OrCode override val size: Int = SigmaBoolean.totalSize(children) + 1 } @@ -125,7 +126,7 @@ case class CTHRESHOLD(k: Int, children: Seq[SigmaBoolean]) extends SigmaConjectu // Our polynomial arithmetic can take only byte inputs require(k >= 0 && k <= children.length && children.length <= 255) - override val opCode: OpCode = OpCodes.AtLeastCode + override val opCode: SPCode = SigmaPropCodes.AtLeastCode override val size: Int = SigmaBoolean.totalSize(children) + 1 } @@ -146,12 +147,12 @@ object TrivialProp { def apply(b: Boolean): TrivialProp = if (b) TrueProp else FalseProp val FalseProp = new TrivialProp(false) { - override val opCode: OpCode = OpCodes.TrivialPropFalseCode + override val opCode: SPCode = SigmaPropCodes.TrivialPropFalseCode override def size: Int = 1 override def toString = "FalseProp" } val TrueProp = new TrivialProp(true) { - override val opCode: OpCode = OpCodes.TrivialPropTrueCode + override val opCode: SPCode = SigmaPropCodes.TrivialPropTrueCode override def size: Int = 1 override def toString = "TrueProp" } diff --git a/interpreter/shared/src/main/scala/sigmastate/utxo/ComplexityTableStat.scala b/interpreter/shared/src/main/scala/sigmastate/utxo/ComplexityTableStat.scala index ad3453a4c5..be98a6affa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utxo/ComplexityTableStat.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utxo/ComplexityTableStat.scala @@ -1,9 +1,9 @@ package sigmastate.utxo +import sigma.ast.TypeCodes.LastConstantCode import sigmastate.serialization.ValueSerializer.getSerializer import sigma.util.Extensions.ByteOps import sigmastate.SMethod -import sigmastate.serialization.{OpCodes, ValueCodes} import sigmastate.serialization.ValueCodes.OpCode import scala.collection.mutable @@ -51,7 +51,7 @@ object ComplexityTableStat { val time = avgTime / 1000 val ser = getSerializer(opCode) val opName = ser.opDesc.typeName - (opName, (opCode.toUByte - ValueCodes.LastConstantCode).toString, time, item.count.toString) + (opName, (opCode.toUByte - LastConstantCode).toString, time, item.count.toString) }.toList.sortBy(_._3)(Ordering[Long].reverse) val mcLines = mcStat.map { case (id @ (typeId, methodId), item) => diff --git a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala index 5fff26aa45..974629ff8a 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala @@ -3,6 +3,7 @@ package sigmastate.eval import org.ergoplatform._ import scalan.MutableLazy import sigma.SigmaException +import sigma.ast.TypeCodes.LastConstantCode import sigma.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIsExactIntegral, ShortIsExactIntegral} import sigma.data.ExactOrdering.{ByteIsExactOrdering, IntIsExactOrdering, LongIsExactOrdering, ShortIsExactOrdering} import sigma.util.Extensions.ByteOps @@ -391,7 +392,7 @@ trait GraphBuilding extends SigmaLibrary { IR: IRContext => case OpCodes.LtCode => OrderingLT[A](elemToExactOrdering(eA)) case OpCodes.GeCode => OrderingGTEQ[A](elemToExactOrdering(eA)) case OpCodes.LeCode => OrderingLTEQ[A](elemToExactOrdering(eA)) - case _ => error(s"Cannot find BinOp for opcode newOpCode(${opCode.toUByte - ValueCodes.LastConstantCode}) and type $eA") + case _ => error(s"Cannot find BinOp for opcode newOpCode(${opCode.toUByte - LastConstantCode}) and type $eA") } import sigmastate._ diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index 41ce087649..c62dd1dc2d 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -2,10 +2,10 @@ package sigmastate import org.ergoplatform._ import org.ergoplatform.validation.ValidationRules._ -import org.ergoplatform.validation._ import org.scalatest.BeforeAndAfterAll import sigma.ast.{SBoolean, SCollection, SContext, SFunc, SGlobal, SInt} import sigma.ast.SPrimType.MaxPrimTypeCode +import sigma.ast.TypeCodes.LastConstantCode import sigma.data.AvlTreeData import sigma.serialization.SerializerException import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckSerializableTypeCode, CheckTypeCode, CheckTypeWithMethods, trySoftForkable} @@ -19,7 +19,6 @@ import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.interpreter.{ContextExtension, ErgoTreeEvaluator, ProverResult} import sigmastate.lang.Terms._ -import sigmastate.serialization.ValueCodes.LastConstantCode import sigmastate.serialization.SigmaSerializer.startReader import sigmastate.serialization._ import sigmastate.utils.Helpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala b/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala index 8b57320b91..b1584e8b8d 100644 --- a/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala +++ b/sc/shared/src/test/scala/sigmastate/utils/SpecGen.scala @@ -3,6 +3,7 @@ package sigmastate.utils import sigma.util.PrintExtensions.IterableExtensions import sigmastate._ import sigma.Evaluation._ +import sigma.ast.TypeCodes.LastConstantCode import sigma.ast.{SBigInt, SBoolean, SCollection, SEmbeddable, SGlobal, SGroupElement, SNumericType, SOption, SPrimType, SString, SType, STypeCompanion} import sigma.serialization.CoreByteWriter.ArgInfo import sigma.util.Extensions.ByteOps @@ -32,7 +33,7 @@ trait SpecGen { args: Seq[ArgInfo], op: Either[PredefinedFunc, SMethod]) def collectSerializers(): Seq[ValueSerializer[_ <: Values.Value[SType]]] = { - ((ValueCodes.LastConstantCode + 1) to 255).collect { + ((LastConstantCode + 1) to 255).collect { case i if ValueSerializer.serializers(i.toByte) != null => val ser = ValueSerializer.serializers(i.toByte) assert(i == ser.opDesc.opCode.toUByte) @@ -41,7 +42,7 @@ trait SpecGen { } def collectFreeCodes(): Seq[Int] = { - ((ValueCodes.LastConstantCode + 1) to 255).collect { + ((LastConstantCode + 1) to 255).collect { case i if ValueSerializer.serializers(i.toByte) == null => i } } From d12e488d451e11fd215b50803346b43888e97008 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 16:14:14 +0200 Subject: [PATCH 14/34] core-serializers: SigmaBoolean moved to a separate file --- .../src/main/scala/sigmastate/Platform.scala | 2 +- .../src/main/scala/sigmastate/Platform.scala | 2 +- .../scala/sigmastate/DataValueComparer.scala | 7 +- .../main/scala/sigmastate/SigSerializer.scala | 1 - .../main/scala/sigmastate/SigmaBoolean.scala | 241 ++++++++++++++++++ .../main/scala/sigmastate/UnprovenTree.scala | 2 +- .../src/main/scala/sigmastate/Values.scala | 158 ++---------- .../sigmastate/eval/CostingDataContext.scala | 2 +- .../scala/sigmastate/eval/Extensions.scala | 23 +- .../main/scala/sigmastate/eval/package.scala | 9 +- .../interpreter/ErgoTreeEvaluator.scala | 2 +- .../scala/sigmastate/interpreter/Hint.scala | 2 +- .../sigmastate/interpreter/ProverUtils.scala | 2 +- .../serialization/DataSerializer.scala | 3 +- .../src/main/scala/sigmastate/trees.scala | 129 ---------- .../src/test/scala/sigmastate/TestsBase.scala | 2 +- .../crypto/SigningSpecification.scala | 2 +- .../DataSerializerSpecification.scala | 2 +- .../PDHTSerializerSpecification.scala | 1 + .../ProveDlogSerializerSpec.scala | 1 + .../SigSerializerSpecification.scala | 4 +- .../sigmastate/utxo/ProverSpecification.scala | 2 +- .../scala/sigmastate/lang/LangTests.scala | 2 +- .../org/ergoplatform/dsl/ContractSyntax.scala | 2 +- .../helpers/CompilerTestingCommons.scala | 4 +- .../DeserializationResilience.scala | 10 +- .../CollectionOperationsSpecification.scala | 1 + .../ErgoLikeInterpreterSpecification.scala | 1 + .../utxo/ThresholdSpecification.scala | 1 + .../BlockchainSimulationTestingCommons.scala | 1 + .../examples/FsmExampleSpecification.scala | 1 + .../org/ergoplatform/sdk/js/SigmaProp.scala | 2 +- .../sdk/AppkitProvingInterpreter.scala | 2 +- .../org/ergoplatform/sdk/JavaHelpers.scala | 3 +- .../sdk/DataJsonEncoderSpecification.scala | 2 +- 35 files changed, 322 insertions(+), 309 deletions(-) create mode 100644 interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala diff --git a/interpreter/js/src/main/scala/sigmastate/Platform.scala b/interpreter/js/src/main/scala/sigmastate/Platform.scala index 19fc7c1161..1579cf6345 100644 --- a/interpreter/js/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/js/src/main/scala/sigmastate/Platform.scala @@ -3,7 +3,7 @@ package sigmastate import org.ergoplatform.ErgoBox import sigma.data.{AvlTreeData, Nullable} import sigma.{Evaluation, VersionContext} -import sigmastate.Values.{Constant, FalseLeaf, SigmaBoolean, TrueLeaf} +import sigmastate.Values.{Constant, FalseLeaf, TrueLeaf} import sigmastate.eval.SigmaDsl import sigmastate.lang.SigmaBuilder import sigma.Coll diff --git a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala index af510785d8..b5c614f5e3 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala @@ -3,7 +3,7 @@ package sigmastate import org.ergoplatform.ErgoBox import sigma.data.{AvlTreeData, Nullable} import sigma.{Evaluation, VersionContext} -import sigmastate.Values.{Constant, FalseLeaf, SigmaBoolean, TrueLeaf} +import sigmastate.Values.{Constant, FalseLeaf, TrueLeaf} import sigmastate.eval.SigmaDsl import sigmastate.lang.SigmaBuilder import sigma.Coll diff --git a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala index bbb47702c1..97c4cd9003 100644 --- a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala @@ -1,14 +1,13 @@ package sigmastate -import sigma.data.{AVHashMap, CollOverArray, Nullable, PairOfCols, RType} import debox.{cfor, sp} -import sigmastate.Values.SigmaBoolean +import sigma._ +import sigma.crypto.EcPointType +import sigma.data._ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.ProveDHTuple import sigmastate.eval.SigmaDsl import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} -import sigma._ -import sigma.crypto.EcPointType /** Implementation of data equality for two arbitrary ErgoTree data types. * @see [[DataValueComparer.equalDataValues]] diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index 410f49b446..f93f72021f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -2,7 +2,6 @@ package sigmastate import sigmastate.crypto.{BigIntegers, GF2_192_Poly} import scorex.util.encode.Base16 -import sigmastate.Values.SigmaBoolean import sigmastate.crypto.DLogProtocol.{ProveDlog, SecondDLogProverMessage} import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{CryptoConstants, ProveDHTuple, SecondDHTupleProverMessage} diff --git a/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala b/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala new file mode 100644 index 0000000000..e7b0641356 --- /dev/null +++ b/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala @@ -0,0 +1,241 @@ +package sigmastate + +import debox.cfor +import sigma.util.safeNewArray +import sigmastate.TrivialProp.{FalseProp, TrueProp} +import sigmastate.crypto.DLogProtocol.ProveDlog +import sigmastate.crypto.ProveDHTuple +import sigmastate.serialization.SigmaPropCodes.{AndCode, AtLeastCode, OrCode, ProveDiffieHellmanTupleCode, ProveDlogCode, SPCode} +import sigmastate.serialization.{ProveDlogSerializer, SigmaPropCodes, SigmaSerializer} +import sigmastate.serialization.transformers.ProveDHTupleSerializer +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} + +import scala.collection.mutable.ArrayBuffer + +/** Algebraic data type of sigma proposition expressions. + * Values of this type are used as values of SigmaProp type of SigmaScript and SigmaDsl + */ +trait SigmaBoolean { + /** Unique id of the node class used in serialization of SigmaBoolean. */ + val opCode: SPCode + + /** Size of the proposition tree (number of nodes). */ + def size: Int +} + +object SigmaBoolean { + /** Compute total size of the trees in the collection of children. */ + def totalSize(children: Seq[SigmaBoolean]): Int = { + var res = 0 + val len = children.length + cfor(0)(_ < len, _ + 1) { i => + res += children(i).size + } + res + } + + /** HOTSPOT: don't beautify this code */ + object serializer extends SigmaSerializer[SigmaBoolean, SigmaBoolean] { + val dhtSerializer = ProveDHTupleSerializer(ProveDHTuple.apply) + + val dlogSerializer = ProveDlogSerializer(ProveDlog.apply) + + override def serialize(data: SigmaBoolean, w: SigmaByteWriter): Unit = { + w.put(data.opCode) + data match { + case dlog: ProveDlog => dlogSerializer.serialize(dlog, w) + case dht: ProveDHTuple => dhtSerializer.serialize(dht, w) + case _: TrivialProp => // besides opCode no additional bytes + case and: CAND => + val nChildren = and.children.length + w.putUShort(nChildren) + cfor(0)(_ < nChildren, _ + 1) { i => + val c = and.children(i) + serializer.serialize(c, w) + } + case or: COR => + val nChildren = or.children.length + w.putUShort(nChildren) + cfor(0)(_ < nChildren, _ + 1) { i => + val c = or.children(i) + serializer.serialize(c, w) + } + case th: CTHRESHOLD => + w.putUShort(th.k) + val nChildren = th.children.length + w.putUShort(nChildren) + cfor(0)(_ < nChildren, _ + 1) { i => + val c = th.children(i) + serializer.serialize(c, w) + } + } + } + + override def parse(r: SigmaByteReader): SigmaBoolean = { + val depth = r.level + r.level = depth + 1 + val opCode = r.getByte() + val res = opCode match { + case FalseProp.opCode => FalseProp + case TrueProp.opCode => TrueProp + case ProveDlogCode => dlogSerializer.parse(r) + case ProveDiffieHellmanTupleCode => dhtSerializer.parse(r) + case AndCode => + val n = r.getUShort() + val children = safeNewArray[SigmaBoolean](n) + cfor(0)(_ < n, _ + 1) { i => + children(i) = serializer.parse(r) + } + CAND(children) + case OrCode => + val n = r.getUShort() + val children = safeNewArray[SigmaBoolean](n) + cfor(0)(_ < n, _ + 1) { i => + children(i) = serializer.parse(r) + } + COR(children) + case AtLeastCode => + val k = r.getUShort() + val n = r.getUShort() + val children = safeNewArray[SigmaBoolean](n) + cfor(0)(_ < n, _ + 1) { i => + children(i) = serializer.parse(r) + } + CTHRESHOLD(k, children) + } + r.level = r.level - 1 + res + } + } + +} + +/** + * Basic trait for inner nodes of crypto-trees, so AND/OR/THRESHOLD sigma-protocol connectives + */ +trait SigmaConjecture extends SigmaBoolean { + def children: Seq[SigmaBoolean] +} + +/** + * Basic trait for leafs of crypto-trees, such as + * [[sigmastate.crypto.DLogProtocol.ProveDlog]] and [[sigmastate.crypto.ProveDHTuple]] + * instances. + * It plays the same role as [[SigmaConjecture]]. It used in prover to distinguish leafs from + * other nodes and have logic common to leaves regardless of the concrete leaf type. + */ +trait SigmaLeaf extends SigmaBoolean + + +/** + * AND conjunction for sigma propositions + */ +case class CAND(override val children: Seq[SigmaBoolean]) extends SigmaConjecture { + /** The same code is used for AND operation, but they belong to different type hierarchies. */ + override val opCode: SPCode = SigmaPropCodes.AndCode + override val size: Int = SigmaBoolean.totalSize(children) + 1 +} + +object CAND { + import TrivialProp._ + + /** Connects the given sigma propositions into CAND proposition performing + * partial evaluation when some of them are trivial propositioins. + * + * @param items propositions to combine into CAND + * @return CAND, TrueProp, FalseProp or even one of the items depending on partial evaluation + */ + def normalized(items: Seq[SigmaBoolean]): SigmaBoolean = { + require(items.nonEmpty) + val res = new ArrayBuffer[SigmaBoolean]() + val nItems = items.length + cfor(0)(_ < nItems, _ + 1) { i => + val x = items(i) + x match { + case FalseProp => return FalseProp + case TrueProp => // skip + case _ => res += x + } + } + if (res.isEmpty) TrueProp + else if (res.length == 1) res(0) + else CAND(res.toSeq) + } +} + +/** + * OR disjunction for sigma propositions + */ +case class COR(children: Seq[SigmaBoolean]) extends SigmaConjecture { + /** The same code is also used for OR operation, but they belong to different type hierarchies. */ + override val opCode: SPCode = SigmaPropCodes.OrCode + override val size: Int = SigmaBoolean.totalSize(children) + 1 +} + +object COR { + import TrivialProp._ + + /** Connects the given sigma propositions into COR proposition performing + * partial evaluation when some of them are trivial propositioins. + * + * @param items propositions to combine into COR + * @return COR, TrueProp, FalseProp or even one of the items depending on partial evaluation + */ + def normalized(items: Seq[SigmaBoolean]): SigmaBoolean = { + require(items.nonEmpty) + val res = new ArrayBuffer[SigmaBoolean]() + val nItems = items.length + cfor(0)(_ < nItems, _ + 1) { i => + val x = items(i) + x match { + case FalseProp => // skip + case TrueProp => return TrueProp + case _ => res += x + } + } + if (res.isEmpty) FalseProp + else if (res.length == 1) res(0) + else COR(res.toSeq) + } +} + +/** + * THRESHOLD connector for sigma propositions + */ +case class CTHRESHOLD(k: Int, children: Seq[SigmaBoolean]) extends SigmaConjecture { + // Our polynomial arithmetic can take only byte inputs + require(k >= 0 && k <= children.length && children.length <= 255) + + override val opCode: SPCode = SigmaPropCodes.AtLeastCode + override val size: Int = SigmaBoolean.totalSize(children) + 1 +} + + +/** Represents boolean values (true/false) in SigmaBoolean tree. + * Participates in evaluation of CAND, COR, THRESHOLD connectives over SigmaBoolean values. + * See CAND.normalized, COR.normalized and AtLeast.reduce. */ +abstract class TrivialProp(val condition: Boolean) extends SigmaBoolean with Product1[Boolean] { + override def _1: Boolean = condition + override def canEqual(that: Any): Boolean = that != null && that.isInstanceOf[TrivialProp] +} +object TrivialProp { + // NOTE: the corresponding unapply is missing because any implementation (even using Nullable) + // will lead to Boolean boxing, which we want to avoid + // So, instead of `case TrivialProp(b) => ... b ...` use more efficient + // `case p: TrivialProp => ... p.condition ... + + def apply(b: Boolean): TrivialProp = if (b) TrueProp else FalseProp + + val FalseProp = new TrivialProp(false) { + override val opCode: SPCode = SigmaPropCodes.TrivialPropFalseCode + override def size: Int = 1 + override def toString = "FalseProp" + } + val TrueProp = new TrivialProp(true) { + override val opCode: SPCode = SigmaPropCodes.TrivialPropTrueCode + override def size: Int = 1 + override def toString = "TrueProp" + } +} + + diff --git a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala index 4e45c230f2..ebbd571e2e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala @@ -1,7 +1,7 @@ package sigmastate import java.math.BigInteger -import sigmastate.Values.{ErgoTree, SigmaBoolean, SigmaPropConstant} +import sigmastate.Values.{ErgoTree, SigmaPropConstant} import sigmastate.crypto.DLogProtocol.{FirstDLogProverMessage, ProveDlog} import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{FirstDHTupleProverMessage, FirstProverMessage, ProveDHTuple} diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index fd27c3ce6f..decd468391 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -1,46 +1,39 @@ package sigmastate -import java.math.BigInteger -import java.util.{Arrays, Objects} -import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} -import org.ergoplatform.settings.ErgoAlgos -import sigma.data.{Nullable, RType} -import sigma.util.CollectionUtil._ -import sigmastate.interpreter.{CompanionDesc, ErgoTreeEvaluator, Interpreter, NamedDesc} -import sigmastate.serialization._ -import sigmastate.serialization.OpCodes._ -import sigmastate.TrivialProp.{FalseProp, TrueProp} -import sigmastate.Values.ErgoTree.substConstants -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, ProveDHTuple} -import sigmastate.lang.Terms._ -import sigmastate.utxo._ -import sigmastate.eval._ -import sigmastate.eval.Extensions._ -import sigma.util.Extensions._ -import sigmastate.interpreter.ErgoTreeEvaluator._ import debox.cfor +import org.ergoplatform.settings.ErgoAlgos import scorex.util.encode.Base16 import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.TypeCodes.ConstantCode import sigma.ast._ import sigma.crypto.EcPointType +import sigma.data.{Nullable, RType} +import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} +import sigma.util.CollectionUtil._ +import sigma.util.Extensions._ +import sigma.validation.ValidationException +import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} +import sigmastate.Values.ErgoTree.substConstants +import sigmastate.crypto.CryptoConstants +import sigmastate.eval.Extensions._ +import sigmastate.eval._ import sigmastate.exceptions.InterpreterException - -import scala.language.implicitConversions +import sigmastate.interpreter.ErgoTreeEvaluator._ +import sigmastate.interpreter.{CompanionDesc, ErgoTreeEvaluator, Interpreter, NamedDesc} import sigmastate.lang.CheckingSigmaBuilder._ -import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.serialization.transformers.ProveDHTupleSerializer -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} import sigmastate.lang.SourceContext -import sigma.util.safeNewArray -import sigma.validation.ValidationException -import sigmastate.serialization.SigmaPropCodes.{ProveDlogCode, SPCode} +import sigmastate.lang.Terms._ +import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer +import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ValueCodes.OpCode +import sigmastate.serialization._ +import sigmastate.utxo._ +import java.math.BigInteger +import java.util.{Arrays, Objects} import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable +import scala.language.implicitConversions object Values { /** Force initialization of reflection. */ @@ -731,103 +724,6 @@ object Values { override def tpe = SBoolean } - /** Algebraic data type of sigma proposition expressions. - * Values of this type are used as values of SigmaProp type of SigmaScript and SigmaDsl - */ - trait SigmaBoolean { - /** Unique id of the node class used in serialization of SigmaBoolean. */ - val opCode: SPCode - /** Size of the proposition tree (number of nodes). */ - def size: Int - } - - object SigmaBoolean { - /** Compute total size of the trees in the collection of children. */ - def totalSize(children: Seq[SigmaBoolean]): Int = { - var res = 0 - val len = children.length - cfor(0)(_ < len, _ + 1) { i => - res += children(i).size - } - res - } - - /** HOTSPOT: don't beautify this code */ - object serializer extends SigmaSerializer[SigmaBoolean, SigmaBoolean] { - val dhtSerializer = ProveDHTupleSerializer(ProveDHTuple.apply) - val dlogSerializer = ProveDlogSerializer(ProveDlog.apply) - - override def serialize(data: SigmaBoolean, w: SigmaByteWriter): Unit = { - w.put(data.opCode) - data match { - case dlog: ProveDlog => dlogSerializer.serialize(dlog, w) - case dht: ProveDHTuple => dhtSerializer.serialize(dht, w) - case _: TrivialProp => // besides opCode no additional bytes - case and: CAND => - val nChildren = and.children.length - w.putUShort(nChildren) - cfor(0)(_ < nChildren, _ + 1) { i => - val c = and.children(i) - serializer.serialize(c, w) - } - - case or: COR => - val nChildren = or.children.length - w.putUShort(nChildren) - cfor(0)(_ < nChildren, _ + 1) { i => - val c = or.children(i) - serializer.serialize(c, w) - } - - case th: CTHRESHOLD => - w.putUShort(th.k) - val nChildren = th.children.length - w.putUShort(nChildren) - cfor(0)(_ < nChildren, _ + 1) { i => - val c = th.children(i) - serializer.serialize(c, w) - } - } - } - - override def parse(r: SigmaByteReader): SigmaBoolean = { - val depth = r.level - r.level = depth + 1 - val opCode = r.getByte() - val res = opCode match { - case FalseProp.opCode => FalseProp - case TrueProp.opCode => TrueProp - case ProveDlogCode => dlogSerializer.parse(r) - case ProveDiffieHellmanTupleCode => dhtSerializer.parse(r) - case AndCode => - val n = r.getUShort() - val children = safeNewArray[SigmaBoolean](n) - cfor(0)(_ < n, _ + 1) { i => - children(i) = serializer.parse(r) - } - CAND(children) - case OrCode => - val n = r.getUShort() - val children = safeNewArray[SigmaBoolean](n) - cfor(0)(_ < n, _ + 1) { i => - children(i) = serializer.parse(r) - } - COR(children) - case AtLeastCode => - val k = r.getUShort() - val n = r.getUShort() - val children = safeNewArray[SigmaBoolean](n) - cfor(0)(_ < n, _ + 1) { i => - children(i) = serializer.parse(r) - } - CTHRESHOLD(k, children) - } - r.level = r.level - 1 - res - } - } - } - trait NotReadyValueBox extends NotReadyValue[SBox.type] { def tpe = SBox } @@ -973,18 +869,6 @@ object Values { ErgoTree.withSegregation(headerFlags, p) } - implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { - def toSigmaProp: SigmaPropValue = SigmaPropConstant(sb) - def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb)) - def showToString: String = sb match { - case ProveDlog(v) => - s"ProveDlog(${v.showECPoint})" - case ProveDHTuple(gv, hv, uv, vv) => - s"ProveDHTuple(${gv.showECPoint}, ${hv.showECPoint}, ${uv.showECPoint}, ${vv.showECPoint})" - case _ => sb.toString - } - } - sealed trait BlockItem extends NotReadyValue[SType] { def id: Int def rhs: SValue diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index ae22d09018..4e881aabe3 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -17,7 +17,7 @@ import sigma.util.Extensions.BigIntegerOps import sigma.validation.SigmaValidationSettings import sigma.{VersionContext, _} import sigmastate.Values.ErgoTree.EmptyConstants -import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue, SigmaBoolean} +import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue} import sigmastate._ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{CryptoConstants, ProveDHTuple} diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index eebc409a7a..8e2a4b6f13 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -4,13 +4,17 @@ import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.TokenId import scorex.util.encode.Base16 import sigma.ast.SType.AnyOps -import sigma.ast.{SCollection, SCollectionType, SType} +import sigma.ast.{SBoolean, SCollection, SCollectionType, SType} import sigma.data.{Nullable, RType} +import sigma.util.Extensions.EcpOps import sigma.{Coll, _} -import sigmastate.Platform -import sigmastate.Values.{Constant, ConstantNode} +import sigmastate.{Platform, SigmaBoolean} +import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} +import sigmastate.crypto.DLogProtocol.ProveDlog +import sigmastate.crypto.ProveDHTuple import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers +import sigmastate.utxo.SigmaPropIsProven import java.math.BigInteger @@ -102,4 +106,17 @@ object Extensions { } } + implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { + def toSigmaProp: SigmaPropValue = SigmaPropConstant(sb) + + def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb)) + + def showToString: String = sb match { + case ProveDlog(v) => + s"ProveDlog(${v.showECPoint})" + case ProveDHTuple(gv, hv, uv, vv) => + s"ProveDHTuple(${gv.showECPoint}, ${hv.showECPoint}, ${uv.showECPoint}, ${vv.showECPoint})" + case _ => sb.toString + } + } } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index 6b2e33125c..eadf216732 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -1,16 +1,13 @@ package sigmastate -import java.math.BigInteger import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, RType} -import scorex.crypto.hash.Digest32 -import sigmastate.Values.SigmaBoolean -import sigma.{Coll, CollBuilder} -import sigma._ +import sigma.{Coll, _} import sigma.crypto.EcPointType +import sigma.data.{AvlTreeData, RType} import sigmastate.exceptions.CostLimitException import supertagged.TaggedType +import java.math.BigInteger import scala.language.implicitConversions package object eval { diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala index 73a2fa24e6..2a6790a16a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala @@ -1,7 +1,7 @@ package sigmastate.interpreter import org.ergoplatform.ErgoLikeContext -import sigmastate.{FixedCost, JitCost, PerItemCost, TypeBasedCost} +import sigmastate.{FixedCost, JitCost, PerItemCost, SigmaBoolean, TypeBasedCost} import sigmastate.Values._ import sigmastate.eval.Profiler import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala index 2e894502f4..026393eb58 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala @@ -2,7 +2,7 @@ package sigmastate.interpreter import java.math.BigInteger import sigmastate.{NodePosition, SigmaLeaf, UncheckedTree} -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.crypto.FirstProverMessage import sigmastate.crypto.VerifierMessage.Challenge diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala index 0e66cb55c5..80fdc4eede 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala @@ -1,7 +1,7 @@ package sigmastate.interpreter import sigmastate._ -import sigmastate.Values.{ErgoTree, SigmaBoolean} +import sigmastate.Values.ErgoTree import sigmastate.crypto.DLogProtocol.{DLogInteractiveProver, ProveDlog} import sigmastate.crypto.{DiffieHellmanTupleInteractiveProver, ProveDHTuple} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 2e5deba2d5..3c855532ab 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -4,9 +4,8 @@ import java.math.BigInteger import java.nio.charset.StandardCharsets import org.ergoplatform.ErgoBox import sigma.data.{AvlTreeData, RType} -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate._ import sigmastate.eval._ import sigma.{Evaluation, _} import debox.cfor diff --git a/interpreter/shared/src/main/scala/sigmastate/trees.scala b/interpreter/shared/src/main/scala/sigmastate/trees.scala index 6b29c34cfe..e058a2d05b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/trees.scala +++ b/interpreter/shared/src/main/scala/sigmastate/trees.scala @@ -28,135 +28,6 @@ import sigmastate.serialization._ import sigmastate.utxo.{SimpleTransformerCompanion, Transformer} import scala.collection.mutable -import scala.collection.mutable.ArrayBuffer - -/** - * Basic trait for inner nodes of crypto-trees, so AND/OR/THRESHOLD sigma-protocol connectives - */ -trait SigmaConjecture extends SigmaBoolean { - def children: Seq[SigmaBoolean] -} - -/** - * Basic trait for leafs of crypto-trees, such as - * [[sigmastate.crypto.DLogProtocol.ProveDlog]] and [[sigmastate.crypto.ProveDHTuple]] - * instances. - * It plays the same role as [[SigmaConjecture]]. It used in prover to distinguish leafs from - * other nodes and have logic common to leaves regardless of the concrete leaf type. - */ -trait SigmaLeaf extends SigmaBoolean - - -/** - * AND conjunction for sigma propositions - */ -case class CAND(override val children: Seq[SigmaBoolean]) extends SigmaConjecture { - /** The same code is used for AND operation, but they belong to different type hierarchies. */ - override val opCode: SPCode = SigmaPropCodes.AndCode - override val size: Int = SigmaBoolean.totalSize(children) + 1 -} - -object CAND { - import TrivialProp._ - - /** Connects the given sigma propositions into CAND proposition performing - * partial evaluation when some of them are trivial propositioins. - * - * @param items propositions to combine into CAND - * @return CAND, TrueProp, FalseProp or even one of the items depending on partial evaluation - */ - def normalized(items: Seq[SigmaBoolean]): SigmaBoolean = { - require(items.nonEmpty) - val res = new ArrayBuffer[SigmaBoolean]() - val nItems = items.length - cfor(0)(_ < nItems, _ + 1) { i => - val x = items(i) - x match { - case FalseProp => return FalseProp - case TrueProp => // skip - case _ => res += x - } - } - if (res.isEmpty) TrueProp - else if (res.length == 1) res(0) - else CAND(res.toSeq) - } -} - -/** - * OR disjunction for sigma propositions - */ -case class COR(children: Seq[SigmaBoolean]) extends SigmaConjecture { - /** The same code is also used for OR operation, but they belong to different type hierarchies. */ - override val opCode: SPCode = SigmaPropCodes.OrCode - override val size: Int = SigmaBoolean.totalSize(children) + 1 -} - -object COR { - import TrivialProp._ - - /** Connects the given sigma propositions into COR proposition performing - * partial evaluation when some of them are trivial propositioins. - * - * @param items propositions to combine into COR - * @return COR, TrueProp, FalseProp or even one of the items depending on partial evaluation - */ - def normalized(items: Seq[SigmaBoolean]): SigmaBoolean = { - require(items.nonEmpty) - val res = new ArrayBuffer[SigmaBoolean]() - val nItems = items.length - cfor(0)(_ < nItems, _ + 1) { i => - val x = items(i) - x match { - case FalseProp => // skip - case TrueProp => return TrueProp - case _ => res += x - } - } - if (res.isEmpty) FalseProp - else if (res.length == 1) res(0) - else COR(res.toSeq) - } -} - -/** - * THRESHOLD connector for sigma propositions - */ -case class CTHRESHOLD(k: Int, children: Seq[SigmaBoolean]) extends SigmaConjecture { - // Our polynomial arithmetic can take only byte inputs - require(k >= 0 && k <= children.length && children.length <= 255) - - override val opCode: SPCode = SigmaPropCodes.AtLeastCode - override val size: Int = SigmaBoolean.totalSize(children) + 1 -} - - -/** Represents boolean values (true/false) in SigmaBoolean tree. - * Participates in evaluation of CAND, COR, THRESHOLD connectives over SigmaBoolean values. - * See CAND.normalized, COR.normalized and AtLeast.reduce. */ -abstract class TrivialProp(val condition: Boolean) extends SigmaBoolean with Product1[Boolean] { - override def _1: Boolean = condition - override def canEqual(that: Any): Boolean = that != null && that.isInstanceOf[TrivialProp] -} -object TrivialProp { - // NOTE: the corresponding unapply is missing because any implementation (even using Nullable) - // will lead to Boolean boxing, which we want to avoid - // So, instead of `case TrivialProp(b) => ... b ...` use more efficient - // `case p: TrivialProp => ... p.condition ... - - def apply(b: Boolean): TrivialProp = if (b) TrueProp else FalseProp - - val FalseProp = new TrivialProp(false) { - override val opCode: SPCode = SigmaPropCodes.TrivialPropFalseCode - override def size: Int = 1 - override def toString = "FalseProp" - } - val TrueProp = new TrivialProp(true) { - override val opCode: SPCode = SigmaPropCodes.TrivialPropTrueCode - override def size: Int = 1 - override def toString = "TrueProp" - } -} /** Embedding of Boolean values to SigmaProp values. As an example, this operation allows boolean experesions * to be used as arguments of `atLeast(..., sigmaProp(boolExpr), ...)` operation. diff --git a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala index 57ecd5e5e8..90b98b7f04 100644 --- a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala +++ b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala @@ -1,7 +1,7 @@ package sigmastate import org.scalatest.matchers.should.Matchers -import sigmastate.Values.{ErgoTree, SigmaBoolean, SigmaPropValue} +import sigmastate.Values.{ErgoTree, SigmaPropValue} import org.ergoplatform.ErgoTreePredef import sigma.VersionTesting diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala index 724a0171dc..812b8c97f4 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala @@ -3,7 +3,7 @@ package sigmastate.crypto import org.scalacheck.Gen import scorex.util.encode.Base16 import sigmastate.{AtLeast, COR, CAND} -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, TestingCommons} import sigmastate.interpreter.{HintsBag, ContextExtension, ProverResult} diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 3156588415..3fa1d2691e 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -5,7 +5,7 @@ import org.ergoplatform.ErgoBox import org.scalacheck.Arbitrary._ import sigma.data.{RType, TupleColl} import sigma.ast.SCollection.SByteArray -import sigmastate.Values.{ErgoTree, SigmaBoolean} +import sigmastate.Values.ErgoTree import sigmastate._ import sigmastate.eval._ import sigmastate.eval.Extensions._ diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala index dbca6fb190..c65503e202 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala @@ -1,6 +1,7 @@ package sigmastate.serialization import sigmastate.crypto.ProveDHTuple +import sigmastate.eval.Extensions.SigmaBooleanOps class PDHTSerializerSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala index df791f248a..6fe75c0af9 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala @@ -1,6 +1,7 @@ package sigmastate.serialization import sigmastate.crypto.DLogProtocol.ProveDlog +import sigmastate.eval.Extensions.SigmaBooleanOps class ProveDlogSerializerSpec extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index c9728478dd..dd782a8f1c 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -5,13 +5,13 @@ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} import org.scalatest.Assertion import sigma.data.AvlTreeData -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate._ import sigmastate.crypto.DLogProtocol.{ProveDlog, SecondDLogProverMessage} import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{ProveDHTuple, SecondDHTupleProverMessage} import sigmastate.crypto.GF2_192_Poly -import sigmastate.eval.Extensions.ArrayOps +import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTransactionTesting, TestingCommons} import sigmastate.interpreter.Interpreter import sigmastate.serialization.generators.ObjectGenerators diff --git a/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala index 1334ee520f..a703f88824 100644 --- a/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala @@ -2,7 +2,7 @@ package sigmastate.utxo import org.ergoplatform.ErgoLikeInterpreter import scorex.crypto.hash.Blake2b256 -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate._ import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.{FirstDHTupleProverMessage, SecP256K1Group} diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index 1c99f8a8f8..4c27397c0b 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -2,7 +2,7 @@ package sigmastate.lang import org.scalatest.matchers.should.Matchers import sigmastate.lang.Terms.{Ident, MethodCallLike} -import sigmastate.Values.{ConcreteCollection, LongConstant, SValue, SigmaBoolean, Value} +import sigmastate.Values.{ConcreteCollection, LongConstant, SValue, Value} import sigmastate._ import java.math.BigInteger diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala index a063554063..ea28230e89 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala @@ -10,7 +10,7 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigma._ import sigma.ast.SType import sigma.ast.SType.AnyOps -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import scala.reflect.ClassTag import scala.util.Try diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 43186978a8..2de7090561 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -12,7 +12,7 @@ import sigma.{Colls, Evaluation, TestUtils} import sigma.data.RType import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckSerializableTypeCode -import sigmastate.Values.{Constant, ErgoTree, SValue, SigmaBoolean, SigmaPropValue} +import sigmastate.Values.{Constant, ErgoTree, SValue, SigmaPropValue} import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.ContextExtension.VarBinding @@ -21,7 +21,7 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.interpreter._ import sigmastate.lang.{CompilerSettings, SigmaCompiler, Terms} import sigmastate.serialization.SigmaSerializer -import sigmastate.{CompilerTestsBase, JitCost} +import sigmastate.{CompilerTestsBase, JitCost, SigmaBoolean} import scala.language.implicitConversions import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index ff6346feb3..2fc3b6ace2 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -2,7 +2,6 @@ package sigmastate.serialization import org.ergoplatform.{ErgoBoxCandidate, Outputs} import org.scalacheck.Gen -import sigma.util.BenchmarkUtil import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} @@ -10,18 +9,17 @@ import scorex.util.serialization.{Reader, VLQByteBufferReader} import sigma.ast.{SBoolean, SInt} import sigma.data.{AvlTreeData, AvlTreeFlags} import sigma.serialization.{DeserializeCallDepthExceeded, InvalidTypePrefix, ReaderPositionLimitExceeded, SerializerException} +import sigma.util.{BenchmarkUtil, safeNewArray} +import sigma.validation.ValidationException +import sigma.validation.ValidationRules.CheckPositionLimit import sigma.{Colls, Environment} -import sigmastate.Values.{BlockValue, GetVarInt, IntConstant, SValue, SigmaBoolean, SigmaPropValue, Tuple, ValDef, ValUse} +import sigmastate.Values.{BlockValue, GetVarInt, IntConstant, SValue, SigmaPropValue, Tuple, ValDef, ValUse} import sigmastate._ -import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.{ContextExtension, CostedProverResult} import sigmastate.serialization.OpCodes._ -import sigma.util.safeNewArray -import sigma.validation.ValidationException -import sigma.validation.ValidationRules.CheckPositionLimit import sigmastate.utils.Helpers._ import sigmastate.utils.SigmaByteReader import sigmastate.utxo.SizeOf diff --git a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala index 24a4c27636..f9f7bb5417 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala @@ -10,6 +10,7 @@ import sigma.ast.SCollection._ import sigma.ast._ import sigma.data.AvlTreeData import sigmastate.SCollectionMethods.{FlatMapMethod, IndexOfMethod, IndicesMethod, PatchMethod, UpdateManyMethod, UpdatedMethod} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.serialization.OpCodes._ import sigmastate.utils.Helpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index 213b0eb204..8e9a6297c4 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -16,6 +16,7 @@ import sigmastate.eval._ import sigmastate.interpreter.Interpreter._ import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.ProveDHTuple +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.helpers._ import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.ContextExtension.VarBinding diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala index c8567a0ba0..d8d1aa96f4 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala @@ -5,6 +5,7 @@ import sigma.data.AvlTreeData import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} import sigmastate.Values.{ConcreteCollection, FalseLeaf, IntConstant, SigmaPropConstant, SigmaPropValue, TrueLeaf} import sigmastate._ +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTransactionTesting} import sigmastate.exceptions.GraphBuildingException diff --git a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala index faa95b7e84..5c744dd77e 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala @@ -17,6 +17,7 @@ import scala.util.{Random, Try} import scorex.util._ import sigma.Colls import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.interpreter.ContextExtension import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo.blockchain.BlockchainSimulationTestingCommons.{FullBlock, ValidationState} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala index 56bc58a826..8f885c0afe 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala @@ -11,6 +11,7 @@ import sigmastate.Values._ import sigmastate._ import sigma.ast._ import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval._ import sigmastate.lang.Terms._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala index 2dc665d918..bf00d38442 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala @@ -1,6 +1,6 @@ package org.ergoplatform.sdk.js -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.crypto.DLogProtocol.ProveDlog import scala.scalajs.js diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala index 1321680f99..2862315a63 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala @@ -11,7 +11,7 @@ import org.ergoplatform.validation.ValidationRules import sigma.VersionContext import sigma.data.AvlTreeData import sigma.util.Extensions.LongOps -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} import sigmastate.crypto.{DiffieHellmanTupleProverInput, SigmaProtocolPrivateInput} import sigmastate.interpreter.Interpreter.{ReductionResult, estimateCryptoVerifyCost} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index 0a513a1ff5..dc44b44e13 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -19,7 +19,8 @@ import sigma.data.{InverseIso, Iso, RType, SigmaConstants} import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} -import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaBoolean, SigmaPropConstant} +import sigmastate.SigmaBoolean +import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple} import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index eb2b6de700..168e3dee44 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -10,7 +10,7 @@ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps import sigma.crypto.EcPointType import sigma.serialization.SerializerException -import sigmastate.Values.SigmaBoolean +import sigmastate.SigmaBoolean import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigma.{AvlTree, Box, Colls, Evaluation} From e16540d12ff9512a04c78f244e54720f280353fb Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 17:08:25 +0200 Subject: [PATCH 15/34] core-serializers: SigmaBoolean became sealed --- .../scala/org/ergoplatform/ErgoAddress.scala | 2 +- .../org/ergoplatform/ErgoTreePredef.scala | 1 - .../scala/sigmastate/DataValueComparer.scala | 2 -- .../main/scala/sigmastate/SigSerializer.scala | 13 ++++---- .../main/scala/sigmastate/SigmaBoolean.scala | 26 +++++++++++++-- .../main/scala/sigmastate/UncheckedTree.scala | 5 ++- .../main/scala/sigmastate/UnprovenTree.scala | 12 +++---- .../sigmastate/crypto/DLogProtocol.scala | 8 ----- .../crypto/DiffieHellmanTupleProtocol.scala | 16 --------- .../sigmastate/eval/CostingDataContext.scala | 3 +- .../scala/sigmastate/eval/Extensions.scala | 4 +-- .../sigmastate/interpreter/Interpreter.scala | 3 +- .../sigmastate/interpreter/ProverUtils.scala | 4 +-- .../CreateProveDlogSerializer.scala | 27 +++++++++++++++ .../serialization/ProveDlogSerializer.scala | 22 +------------ .../CreateProveDHTupleSerializer.scala | 33 +++++++++++++++++++ .../transformers/ProveDHTupleSerializer.scala | 32 ++---------------- .../crypto/SigningSpecification.scala | 6 ++-- .../PDHTSerializerSpecification.scala | 2 +- .../ProveDlogSerializerSpec.scala | 2 +- .../SigSerializerSpecification.scala | 4 +-- .../generators/ObjectGenerators.scala | 3 +- .../special/sigma/SigmaTestingData.scala | 2 -- .../scala/sigmastate/lang/LangTests.scala | 3 +- .../scala/sigmastate/eval/TreeBuilding.scala | 3 -- .../ErgoAddressSpecification.scala | 12 +++---- .../org/ergoplatform/ErgoTreePredefSpec.scala | 2 +- .../scala/sigma/SigmaDslSpecification.scala | 1 - .../test/scala/sigma/SigmaDslTesting.scala | 4 +-- .../TestingInterpreterSpecification.scala | 2 +- .../sigmastate/eval/EvaluationTest.scala | 2 +- .../sigmastate/lang/SigmaTyperTest.scala | 2 +- .../ErgoLikeInterpreterSpecification.scala | 2 -- .../utxo/ThresholdSpecification.scala | 2 +- .../examples/CoopExampleSpecification.scala | 10 +++--- .../DHTupleExampleSpecification.scala | 7 ++-- .../examples/MixExampleSpecification.scala | 5 ++- .../RPSGameExampleSpecification.scala | 1 - .../XorGameExampleSpecification.scala | 1 - .../org/ergoplatform/sdk/js/SigmaProp.scala | 3 +- .../org/ergoplatform/sdk/js/ValueSpec.scala | 2 +- .../sdk/AppkitProvingInterpreter.scala | 4 +-- .../org/ergoplatform/sdk/JavaHelpers.scala | 5 ++- .../wallet/secrets/ExtendedPublicKey.scala | 3 +- .../wallet/secrets/ExtendedSecretKey.scala | 4 +-- .../sdk/JsonSerializationSpec.scala | 15 +++------ .../sdk/wallet/utils/Generators.scala | 2 +- 47 files changed, 152 insertions(+), 177 deletions(-) create mode 100644 interpreter/shared/src/main/scala/sigmastate/serialization/CreateProveDlogSerializer.scala create mode 100644 interpreter/shared/src/main/scala/sigmastate/serialization/transformers/CreateProveDHTupleSerializer.scala diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala index 88dc9de17f..6443ebe40e 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala @@ -6,7 +6,7 @@ import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.encode.Base58 import sigmastate.Values._ import sigmastate._ -import sigmastate.crypto.DLogProtocol.{ProveDlog, ProveDlogProp} +import sigmastate.crypto.DLogProtocol.ProveDlogProp import sigmastate.serialization._ import sigmastate.utxo.{DeserializeContext, Slice} import sigma.{Coll, SigmaException} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala index fb4dd6b5d1..a14907fd2e 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala @@ -3,7 +3,6 @@ package org.ergoplatform import org.ergoplatform.settings.MonetarySettings import sigma.ast.SCollection.SByteArray import sigma.ast.{SBox, SInt, SLong, SSigmaProp} -import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.crypto.CryptoConstants import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} diff --git a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala index 97c4cd9003..a8b25b25ec 100644 --- a/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/DataValueComparer.scala @@ -4,8 +4,6 @@ import debox.{cfor, sp} import sigma._ import sigma.crypto.EcPointType import sigma.data._ -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import sigmastate.eval.SigmaDsl import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index f93f72021f..002089397c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -1,18 +1,17 @@ package sigmastate -import sigmastate.crypto.{BigIntegers, GF2_192_Poly} +import debox.cfor import scorex.util.encode.Base16 -import sigmastate.crypto.DLogProtocol.{ProveDlog, SecondDLogProverMessage} +import sigma.serialization.SerializerException +import sigma.util.safeNewArray +import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge -import sigmastate.crypto.{CryptoConstants, ProveDHTuple, SecondDHTupleProverMessage} +import sigmastate.crypto.{BigIntegers, CryptoConstants, GF2_192_Poly, SecondDHTupleProverMessage} +import sigmastate.eval.Extensions.ArrayOps import sigmastate.interpreter.ErgoTreeEvaluator.{fixedCostOp, perItemCostOp} import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} import sigmastate.serialization.SigmaSerializer -import sigma.util.safeNewArray import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} -import debox.cfor -import sigma.serialization.SerializerException -import sigmastate.eval.Extensions.ArrayOps /** Contains implementation of signature (aka proof) serialization. * diff --git a/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala b/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala index e7b0641356..b11cac4741 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala @@ -1,10 +1,10 @@ package sigmastate import debox.cfor +import sigma.crypto.EcPointType +import sigma.serialization.GroupElementSerializer import sigma.util.safeNewArray import sigmastate.TrivialProp.{FalseProp, TrueProp} -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import sigmastate.serialization.SigmaPropCodes.{AndCode, AtLeastCode, OrCode, ProveDiffieHellmanTupleCode, ProveDlogCode, SPCode} import sigmastate.serialization.{ProveDlogSerializer, SigmaPropCodes, SigmaSerializer} import sigmastate.serialization.transformers.ProveDHTupleSerializer @@ -15,7 +15,7 @@ import scala.collection.mutable.ArrayBuffer /** Algebraic data type of sigma proposition expressions. * Values of this type are used as values of SigmaProp type of SigmaScript and SigmaDsl */ -trait SigmaBoolean { +sealed trait SigmaBoolean { /** Unique id of the node class used in serialization of SigmaBoolean. */ val opCode: SPCode @@ -126,6 +126,26 @@ trait SigmaConjecture extends SigmaBoolean { */ trait SigmaLeaf extends SigmaBoolean +/** Construct a new SigmaBoolean value representing public key of discrete logarithm signature protocol. */ +case class ProveDlog(value: EcPointType) extends SigmaLeaf { + override def size: Int = 1 + override val opCode : SPCode = SigmaPropCodes.ProveDlogCode + + /** Serialized bytes of the elliptic curve point (using GroupElementSerializer). */ + lazy val pkBytes: Array[Byte] = GroupElementSerializer.toBytes(value) +} + +/** Construct a new SigmaProp value representing public key of Diffie Hellman signature protocol. + * Common input: (g,h,u,v) */ +case class ProveDHTuple(gv: EcPointType, hv: EcPointType, uv: EcPointType, vv: EcPointType) + extends SigmaLeaf { + override val opCode: SPCode = SigmaPropCodes.ProveDiffieHellmanTupleCode + override def size: Int = 4 // one node for each EcPoint + lazy val g = gv + lazy val h = hv + lazy val u = uv + lazy val v = vv +} /** * AND conjunction for sigma propositions diff --git a/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala b/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala index a8b9bf4faf..343c80f1e6 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala @@ -1,9 +1,8 @@ package sigmastate -import sigmastate.crypto.DLogProtocol.{FirstDLogProverMessage, ProveDlog, SecondDLogProverMessage} +import sigmastate.crypto.DLogProtocol.{FirstDLogProverMessage, SecondDLogProverMessage} import sigmastate.crypto.VerifierMessage.Challenge -import sigmastate.crypto.{FirstDHTupleProverMessage, ProveDHTuple, SecondDHTupleProverMessage} -import sigmastate.crypto.GF2_192_Poly +import sigmastate.crypto.{FirstDHTupleProverMessage, GF2_192_Poly, SecondDHTupleProverMessage} sealed trait UncheckedTree extends ProofTree diff --git a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala index ebbd571e2e..922a675cba 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala @@ -1,17 +1,17 @@ package sigmastate -import java.math.BigInteger +import debox.cfor import sigmastate.Values.{ErgoTree, SigmaPropConstant} -import sigmastate.crypto.DLogProtocol.{FirstDLogProverMessage, ProveDlog} +import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge -import sigmastate.crypto.{FirstDHTupleProverMessage, FirstProverMessage, ProveDHTuple} -import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} +import sigmastate.crypto.{FirstDHTupleProverMessage, FirstProverMessage, GF2_192_Poly} import sigmastate.interpreter.ErgoTreeEvaluator.fixedCostOp +import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer import sigmastate.utils.SigmaByteWriter -import debox.cfor -import sigmastate.crypto.GF2_192_Poly + +import java.math.BigInteger import scala.language.existentials object ConjectureType extends Enumeration { diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala index e41a395774..2a6cc7cc7c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala @@ -20,14 +20,6 @@ object DLogProtocol { override type Z = SecondDLogProverMessage } - /** Construct a new SigmaBoolean value representing public key of discrete logarithm signature protocol. */ - case class ProveDlog(value: EcPointType) extends SigmaLeaf { - override def size: Int = 1 - override val opCode: SPCode = SigmaPropCodes.ProveDlogCode - /** Serialized bytes of the elliptic curve point (using GroupElementSerializer). */ - lazy val pkBytes: Array[Byte] = GroupElementSerializer.toBytes(value) - } - /** Helper extractor to match SigmaProp values and extract ProveDlog out of it. */ object ProveDlogProp { def unapply(p: SigmaProp): Option[ProveDlog] = SigmaDsl.toSigmaBoolean(p) match { diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala index 9a850ed323..96e10eb1f4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala @@ -67,22 +67,6 @@ case class SecondDHTupleProverMessage(z: BigInteger) extends SecondProverMessage override type SP = DiffieHellmanTupleProtocol } -/** Construct a new SigmaProp value representing public key of Diffie Hellman signature protocol. - * Common input: (g,h,u,v) */ -case class ProveDHTuple(gv: EcPointType, hv: EcPointType, uv: EcPointType, vv: EcPointType) - extends SigmaLeaf { - override val opCode: SPCode = SigmaPropCodes.ProveDiffieHellmanTupleCode - override def size: Int = 4 // one node for each EcPoint - lazy val g = gv - lazy val h = hv - lazy val u = uv - lazy val v = vv -} - -object ProveDHTuple { - val Code: PropositionCode = 103: Byte -} - /** Helper extractor to match SigmaProp values and extract ProveDHTuple out of it. */ object ProveDHTupleProp { def unapply(p: SigmaProp): Option[ProveDHTuple] = SigmaDsl.toSigmaBoolean(p) match { diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 4e881aabe3..2e08949a9f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -19,8 +19,7 @@ import sigma.{VersionContext, _} import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue} import sigmastate._ -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, ProveDHTuple} +import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions._ import sigmastate.interpreter.Interpreter import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 8e2a4b6f13..84f7c5f7d4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -8,13 +8,11 @@ import sigma.ast.{SBoolean, SCollection, SCollectionType, SType} import sigma.data.{Nullable, RType} import sigma.util.Extensions.EcpOps import sigma.{Coll, _} -import sigmastate.{Platform, SigmaBoolean} import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers import sigmastate.utxo.SigmaPropIsProven +import sigmastate.{Platform, ProveDHTuple, ProveDlog, SigmaBoolean} import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala index 0016084716..ef0ab20eb9 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala @@ -5,9 +5,8 @@ import sigma.kiama.rewriting.Rewriter.{everywherebu, rule, strategy} import org.ergoplatform.ErgoLikeContext import sigma.validation.SigmaValidationSettings import org.ergoplatform.validation.ValidationRules._ -import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.Values._ -import sigmastate.crypto.DLogProtocol.{DLogInteractiveProver, FirstDLogProverMessage, ProveDlog} +import sigmastate.crypto.DLogProtocol.{DLogInteractiveProver, FirstDLogProverMessage} import sigmastate.crypto._ import sigmastate.interpreter.Interpreter._ import sigmastate.serialization.{SigmaSerializer, ValueSerializer} diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala index 80fdc4eede..82ae451eae 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala @@ -2,8 +2,8 @@ package sigmastate.interpreter import sigmastate._ import sigmastate.Values.ErgoTree -import sigmastate.crypto.DLogProtocol.{DLogInteractiveProver, ProveDlog} -import sigmastate.crypto.{DiffieHellmanTupleInteractiveProver, ProveDHTuple} +import sigmastate.crypto.DLogProtocol.DLogInteractiveProver +import sigmastate.crypto.DiffieHellmanTupleInteractiveProver trait ProverUtils extends Interpreter { diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/CreateProveDlogSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/CreateProveDlogSerializer.scala new file mode 100644 index 0000000000..bc59806dac --- /dev/null +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/CreateProveDlogSerializer.scala @@ -0,0 +1,27 @@ +package sigmastate.serialization + +import sigma.ast.SGroupElement +import sigma.serialization.CoreByteWriter._ +import sigmastate.CreateProveDlog +import sigmastate.Values.{SValue, SigmaPropValue, Value} +import sigmastate.lang.Terms.ValueOps +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigmastate.utils.SigmaByteWriter._ + +case class CreateProveDlogSerializer(cons: Value[SGroupElement.type] => SigmaPropValue) + extends ValueSerializer[CreateProveDlog] { + import sigmastate.Operations.CreateProveDlogInfo._ + + override def opDesc = CreateProveDlog + + val valueInfo: DataInfo[SValue] = valueArg + + override def serialize(obj: CreateProveDlog, w: SigmaByteWriter): Unit = { + w.putValue(obj.value, valueInfo) + } + + override def parse(r: SigmaByteReader) = { + val v = r.getValue().asValue[SGroupElement.type] + cons(v) + } +} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala index 5bf55887ab..5148d9a85b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala @@ -1,14 +1,8 @@ package sigmastate.serialization -import sigma.ast.SGroupElement import sigma.crypto.EcPointType -import sigma.serialization.CoreByteWriter.DataInfo import sigma.serialization.GroupElementSerializer -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.CreateProveDlog -import sigmastate.Values.{SValue, SigmaPropValue, Value} -import sigmastate.lang.Terms._ -import sigmastate.utils.SigmaByteWriter._ +import sigmastate.ProveDlog import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} case class ProveDlogSerializer(cons: EcPointType => ProveDlog) @@ -23,20 +17,6 @@ case class ProveDlogSerializer(cons: EcPointType => ProveDlog) } } -case class CreateProveDlogSerializer(cons: Value[SGroupElement.type] => SigmaPropValue) - extends ValueSerializer[CreateProveDlog] { - import sigmastate.Operations.CreateProveDlogInfo._ - override def opDesc = CreateProveDlog - val valueInfo: DataInfo[SValue] = valueArg - override def serialize(obj: CreateProveDlog, w: SigmaByteWriter): Unit = { - w.putValue(obj.value, valueInfo) - } - - override def parse(r: SigmaByteReader) = { - val v = r.getValue().asValue[SGroupElement.type] - cons(v) - } -} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/CreateProveDHTupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/CreateProveDHTupleSerializer.scala new file mode 100644 index 0000000000..67b6dfb990 --- /dev/null +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/CreateProveDHTupleSerializer.scala @@ -0,0 +1,33 @@ +package sigmastate.serialization.transformers + +import sigma.ast.SGroupElement +import sigmastate.CreateProveDHTuple +import sigmastate.Values.{SigmaPropValue, Value} +import sigmastate.lang.Terms._ +import sigmastate.serialization._ +import sigmastate.utils.SigmaByteWriter._ +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} + +case class CreateProveDHTupleSerializer(cons: (Value[SGroupElement.type], + Value[SGroupElement.type], + Value[SGroupElement.type], + Value[SGroupElement.type]) => SigmaPropValue) + extends ValueSerializer[CreateProveDHTuple] { + import sigmastate.Operations.CreateProveDHTupleInfo._ + override def opDesc = CreateProveDHTuple + + override def serialize(obj: CreateProveDHTuple, w: SigmaByteWriter): Unit = { + w.putValue(obj.gv, gArg) + w.putValue(obj.hv, hArg) + w.putValue(obj.uv, uArg) + w.putValue(obj.vv, vArg) + } + + override def parse(r: SigmaByteReader) = { + val gv = r.getValue().asValue[SGroupElement.type] + val hv = r.getValue().asValue[SGroupElement.type] + val uv = r.getValue().asValue[SGroupElement.type] + val vv = r.getValue().asValue[SGroupElement.type] + cons(gv, hv, uv, vv) + } +} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala index 0df646afd8..df8e5b6d71 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala @@ -1,15 +1,10 @@ package sigmastate.serialization.transformers -import sigma.ast.SGroupElement import sigma.crypto.EcPointType import sigma.serialization.GroupElementSerializer -import sigmastate.CreateProveDHTuple -import sigmastate.Values.{SigmaPropValue, Value} -import sigmastate.crypto.ProveDHTuple -import sigmastate.lang.Terms._ -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.utils.SigmaByteWriter._ +import sigmastate.ProveDHTuple import sigmastate.serialization._ +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} case class ProveDHTupleSerializer( cons: (EcPointType, EcPointType, EcPointType, EcPointType) => ProveDHTuple @@ -31,26 +26,3 @@ case class ProveDHTupleSerializer( } } -case class CreateProveDHTupleSerializer(cons: (Value[SGroupElement.type], - Value[SGroupElement.type], - Value[SGroupElement.type], - Value[SGroupElement.type]) => SigmaPropValue) - extends ValueSerializer[CreateProveDHTuple] { - import sigmastate.Operations.CreateProveDHTupleInfo._ - override def opDesc = CreateProveDHTuple - - override def serialize(obj: CreateProveDHTuple, w: SigmaByteWriter): Unit = { - w.putValue(obj.gv, gArg) - w.putValue(obj.hv, hArg) - w.putValue(obj.uv, uArg) - w.putValue(obj.vv, vArg) - } - - override def parse(r: SigmaByteReader) = { - val gv = r.getValue().asValue[SGroupElement.type] - val hv = r.getValue().asValue[SGroupElement.type] - val uv = r.getValue().asValue[SGroupElement.type] - val vv = r.getValue().asValue[SGroupElement.type] - cons(gv, hv, uv, vv) - } -} diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala index 812b8c97f4..631c0247cc 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala @@ -2,13 +2,11 @@ package sigmastate.crypto import org.scalacheck.Gen import scorex.util.encode.Base16 -import sigmastate.{AtLeast, COR, CAND} -import sigmastate.SigmaBoolean +import sigmastate.{AtLeast, CAND, COR, ProveDHTuple, SigmaBoolean} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, TestingCommons} -import sigmastate.interpreter.{HintsBag, ContextExtension, ProverResult} +import sigmastate.interpreter.{ContextExtension, HintsBag, ProverResult} import sigmastate.serialization.transformers.ProveDHTupleSerializer -import sigmastate.crypto.ProveDHTuple class SigningSpecification extends TestingCommons { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala index c65503e202..782d97e3a1 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala @@ -1,6 +1,6 @@ package sigmastate.serialization -import sigmastate.crypto.ProveDHTuple +import sigmastate.ProveDHTuple import sigmastate.eval.Extensions.SigmaBooleanOps class PDHTSerializerSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala index 6fe75c0af9..2a45b18e47 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala @@ -1,6 +1,6 @@ package sigmastate.serialization -import sigmastate.crypto.DLogProtocol.ProveDlog +import sigmastate.ProveDlog import sigmastate.eval.Extensions.SigmaBooleanOps class ProveDlogSerializerSpec extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index dd782a8f1c..f12036b95a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -7,9 +7,9 @@ import org.scalatest.Assertion import sigma.data.AvlTreeData import sigmastate.SigmaBoolean import sigmastate._ -import sigmastate.crypto.DLogProtocol.{ProveDlog, SecondDLogProverMessage} +import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge -import sigmastate.crypto.{ProveDHTuple, SecondDHTupleProverMessage} +import sigmastate.crypto.SecondDHTupleProverMessage import sigmastate.crypto.GF2_192_Poly import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTransactionTesting, TestingCommons} diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 079ffaf3a0..339755895a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -13,8 +13,7 @@ import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.encode.{Base58, Base64} import scorex.util.{ModifierId, bytesToId} import sigmastate.Values._ -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, ProveDHTuple} +import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.crypto.CryptoConstants.dlogGroup diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index dfe1ff4b86..5ac45656ed 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -11,8 +11,6 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import sigmastate.eval._ import sigmastate.eval.Extensions._ import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, CSigmaProp, CostingBox, CostingSigmaDslBuilder, SigmaDsl} diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index 4c27397c0b..e9b5c1dac1 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -6,9 +6,8 @@ import sigmastate.Values.{ConcreteCollection, LongConstant, SValue, Value} import sigmastate._ import java.math.BigInteger -import sigmastate.crypto.DLogProtocol.ProveDlog import sigma.ast.SCollection.SByteArray -import sigmastate.crypto.{CryptoConstants, ProveDHTuple} +import sigmastate.crypto.CryptoConstants import sigmastate.interpreter.Interpreter.ScriptEnv import sigma._ import sigmastate.eval._ diff --git a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala index 7283a7e2b4..edcfac3f28 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala @@ -8,11 +8,8 @@ import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ConstantStore import sigma.ast._ -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import scala.collection.mutable.ArrayBuffer -import sigmastate.lang.Terms import sigmastate.serialization.ValueCodes.OpCode /** Implementation of IR-graph to ErgoTree expression translation. diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index b6c3b23afa..6fc8bb00ce 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -1,13 +1,11 @@ package org.ergoplatform import org.ergoplatform.ErgoAddressEncoder.{MainnetNetworkPrefix, TestnetNetworkPrefix, hash256} -import org.ergoplatform.validation.ValidationRules import org.scalatest.{Assertion, TryValues} import scorex.crypto.hash.Blake2b256 import scorex.util.encode.Base58 import sigmastate.Values.{ByteArrayConstant, Constant, ErgoTree, IntConstant, UnparsedErgoTree} -import sigmastate.crypto.DLogProtocol -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.eval.InvalidType import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers._ @@ -20,7 +18,7 @@ import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.ValueSerializer import sigmastate.utils.Helpers._ -import sigmastate.{CompilerCrossVersionProps, SigmaAnd} +import sigmastate.{CompilerCrossVersionProps, ProveDlog, SigmaAnd} import sigma.SigmaDslTesting import sigma.ast.SType import sigma.serialization.GroupElementSerializer @@ -80,8 +78,8 @@ class ErgoAddressSpecification extends SigmaDslTesting def testFromProposition(scriptVersion: Byte, expectedP2S: String, expectedP2SH: String, expectedP2PK: String) = { - val pk: DLogProtocol.ProveDlog = DLogProverInput(BigInteger.ONE).publicImage - val pk10: DLogProtocol.ProveDlog = DLogProverInput(BigInteger.TEN).publicImage + val pk: ProveDlog = DLogProverInput(BigInteger.ONE).publicImage + val pk10: ProveDlog = DLogProverInput(BigInteger.TEN).publicImage val p2s: Pay2SAddress = Pay2SAddress( ErgoTree.fromProposition( @@ -179,7 +177,7 @@ class ErgoAddressSpecification extends SigmaDslTesting } { - val pk: DLogProtocol.ProveDlog = DLogProverInput(BigInteger.ONE).publicImage + val pk: ProveDlog = DLogProverInput(BigInteger.ONE).publicImage val p2pk = P2PKAddress(pk)(ergoAddressEncoder) val invalidAddrType = 4.toByte diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index fca6b2cf09..356ba3ea19 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -13,7 +13,7 @@ import sigma.data.AvlTreeData import sigma.util.BenchmarkUtil.measure import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index b46f0b6675..72f78a0e3c 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -34,7 +34,6 @@ import org.scalacheck.Gen.frequency import org.scalatest.{BeforeAndAfterAll, Tag} import sigma.data.RType._ import scorex.util.ModifierId -import sigmastate.crypto.ProveDHTuple import sigmastate.interpreter._ import org.scalactic.source.Position import sigma.VersionContext diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index ca9b365aa0..04b49387a2 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -19,7 +19,7 @@ import sigma.util.Extensions._ import sigma.util.StringUtil.StringUtilExtensions import sigma.ast.SType.AnyOps import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, IntConstant, SValue} -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput import sigmastate.eval.Extensions._ import sigmastate.eval.{CompiletimeIRContext, CostingBox, CostingDataContext, IRContext, SigmaDsl} @@ -35,7 +35,7 @@ import sigmastate.utxo.{DeserializeContext, DeserializeRegister, GetVar, OptionG import sigma.ast.{SOption, SSigmaProp, SType} import sigma.validation.ValidationRules.CheckSerializableTypeCode import sigma.validation.{SigmaValidationSettings, ValidationException} -import sigmastate.{SigmaLeaf, eval} +import sigmastate.{ProveDlog, SigmaLeaf, eval} import scala.collection.mutable import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala index 1be026724b..5c6ee9e43a 100644 --- a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala @@ -1,6 +1,6 @@ package sigmastate -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import scorex.crypto.hash.Blake2b256 import sigmastate.Values._ import sigmastate.interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala index c2d6143748..b20772ad44 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala @@ -10,7 +10,7 @@ import sigma.ast.SSigmaProp import sigmastate.lang.LangTests import sigma.util.BenchmarkUtil._ import sigmastate._ -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer class EvaluationTest extends BaseCtxTests diff --git a/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala b/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala index 90c54def15..68807ddc4f 100644 --- a/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala +++ b/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala @@ -11,7 +11,7 @@ import sigmastate.Values._ import sigmastate._ import sigma.ast._ import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.exceptions.TyperException import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.lang.SigmaPredef._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index 8e9a6297c4..cfa6bd9d96 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -14,8 +14,6 @@ import sigma.util.Extensions.EcpOps import sigma.validation.ValidationException import sigmastate.eval._ import sigmastate.interpreter.Interpreter._ -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.ProveDHTuple import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.helpers._ import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala index d8d1aa96f4..eec366fd76 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala @@ -2,7 +2,7 @@ package sigmastate.utxo import sigma.ast.SSigmaProp import sigma.data.AvlTreeData -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.Values.{ConcreteCollection, FalseLeaf, IntConstant, SigmaPropConstant, SigmaPropValue, TrueLeaf} import sigmastate._ import sigmastate.eval.Extensions.SigmaBooleanOps diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index 4a164fc3f1..2efde3ac0c 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -1,16 +1,16 @@ package sigmastate.utxo.examples -import org.ergoplatform.{ErgoLikeContext, ErgoLikeTransaction, ErgoBox} +import org.ergoplatform.{ErgoBox, ErgoLikeContext, ErgoLikeTransaction} import org.scalatest.Assertion import org.scalatest.TryValues._ -import sigmastate.crypto.DLogProtocol.{ProveDlog, DLogProverInput} +import sigmastate.crypto.DLogProtocol.DLogProverInput import scorex.crypto.hash.Blake2b256 import sigma.data.AvlTreeData -import sigmastate.Values.{ByteArrayConstant, ErgoTree, BooleanConstant} -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter} +import sigmastate.Values.{BooleanConstant, ByteArrayConstant, ErgoTree} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ -import sigmastate.CompilerCrossVersionProps +import sigmastate.{CompilerCrossVersionProps, ProveDlog} class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala index 809d691836..05b2c9a996 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala @@ -5,10 +5,9 @@ import java.math.BigInteger import org.ergoplatform.ErgoBox.{R4, R5} import sigma.data.AvlTreeData import sigma.util.Extensions.EcpOps -import sigmastate.CompilerCrossVersionProps +import sigmastate.{CompilerCrossVersionProps, ProveDHTuple, ProveDlog} import sigmastate.Values.GroupElementConstant -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ @@ -31,7 +30,7 @@ class DHTupleExampleSpecification extends CompilerTestingCommons val g = dlogGroup.generator val alice = new ContextEnrichingTestProvingInterpreter - val alicePubKey:ProveDlog = alice.dlogSecrets.head.publicImage + val alicePubKey: ProveDlog = alice.dlogSecrets.head.publicImage val x:BigInteger = alice.dlogSecrets.head.w // x is Alice's private key diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index 17a7abce14..a12687e907 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -5,10 +5,9 @@ import org.ergoplatform.ErgoBox.{R4, R5} import scorex.crypto.hash.Blake2b256 import sigma.data.AvlTreeData import sigma.util.Extensions.EcpOps -import sigmastate.CompilerCrossVersionProps +import sigmastate.{CompilerCrossVersionProps, ProveDHTuple, ProveDlog} import sigmastate.Values.GroupElementConstant -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala index dd7db35890..ce6ccb7de5 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala @@ -7,7 +7,6 @@ import scorex.utils.Random import sigma.data.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ -import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala index 04baae33fc..5e3e8ea446 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala @@ -7,7 +7,6 @@ import scorex.utils.Random import sigma.data.AvlTreeData import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ -import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala index bf00d38442..17b25c1321 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala @@ -1,7 +1,6 @@ package org.ergoplatform.sdk.js -import sigmastate.SigmaBoolean -import sigmastate.crypto.DLogProtocol.ProveDlog +import sigmastate.{ProveDlog, SigmaBoolean} import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala index 062c57adab..12ba83be9c 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala @@ -7,13 +7,13 @@ import scorex.util.encode.Base16 import sigma.ast.SType import sigmastate.Values.{AvlTreeConstant, BigIntConstant, BooleanConstant, BoxConstant, ByteConstant, Constant, GroupElementConstant, IntConstant, LongConstant, ShortConstant, SigmaPropConstant, UnitConstant} import sigmastate.crypto.CryptoConstants.dlogGroup -import sigmastate.crypto.DLogProtocol.ProveDlog import sigma.crypto.CryptoFacade import sigmastate.eval.CSigmaProp import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.serialization.ConstantSerializer import sigmastate.utils.Helpers import sigma.SigmaTestingData +import sigmastate.ProveDlog import java.math.BigInteger diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala index 2862315a63..ed24634c77 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala @@ -11,8 +11,8 @@ import org.ergoplatform.validation.ValidationRules import sigma.VersionContext import sigma.data.AvlTreeData import sigma.util.Extensions.LongOps -import sigmastate.SigmaBoolean -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.{ProveDlog, SigmaBoolean} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.{DiffieHellmanTupleProverInput, SigmaProtocolPrivateInput} import sigmastate.interpreter.Interpreter.{ReductionResult, estimateCryptoVerifyCost} import sigmastate.interpreter._ diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index dc44b44e13..f4c777651e 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -19,10 +19,9 @@ import sigma.data.{InverseIso, Iso, RType, SigmaConstants} import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} -import sigmastate.SigmaBoolean +import sigmastate.{ProveDHTuple, ProveDlog, SigmaBoolean} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.crypto.{DiffieHellmanTupleProverInput, ProveDHTuple} +import sigmastate.crypto.DiffieHellmanTupleProverInput import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala index b709fdaf1c..b681f62d75 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala @@ -1,7 +1,8 @@ package org.ergoplatform.sdk.wallet.secrets import sigma.crypto.CryptoFacade -import sigmastate.crypto.DLogProtocol.{DLogProverInput, ProveDlog} +import sigmastate.ProveDlog +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.CryptoConstants import sigmastate.crypto.BigIntegers import sigmastate.serialization.SigmaSerializer diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala index 8783567e89..a09f34014b 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala @@ -1,10 +1,10 @@ package org.ergoplatform.sdk.wallet.secrets import sigma.crypto.CryptoFacade +import sigmastate.ProveDlog import java.math.BigInteger import sigmastate.crypto.BigIntegers -import sigmastate.crypto.DLogProtocol import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.CryptoConstants import sigmastate.serialization.SigmaSerializer @@ -24,7 +24,7 @@ final class ExtendedSecretKey(/*private[secrets]*/ val keyBytes: Array[Byte], override def privateInput: DLogProverInput = DLogProverInput(BigIntegers.fromUnsignedByteArray(keyBytes)) - def publicImage: DLogProtocol.ProveDlog = privateInput.publicImage + def publicImage: ProveDlog = privateInput.publicImage def child(idx: Int): ExtendedSecretKey = ExtendedSecretKey.deriveChildSecretKey(this, idx) diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index a412aa5ac6..affaa96c4b 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -5,25 +5,20 @@ import io.circe._ import io.circe.syntax._ import org.ergoplatform.ErgoBox._ import org.ergoplatform.validation.ValidationRules +import org.ergoplatform._ import org.scalacheck.Arbitrary.arbitrary import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.ModifierId import scorex.util.encode.Base16 +import sigma.ast.SType +import sigma.data.AvlTreeData +import sigma.{Coll, Header, PreHeader} +import sigmastate.ProveDlog import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} import sigmastate.crypto.CryptoConstants -import sigmastate.crypto.DLogProtocol.ProveDlog import sigmastate.eval.Digest32Coll import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SerializationSpecification -import sigmastate.utils.Helpers.DecoderResultOps -import sigma.Coll -import sigma.{Header, PreHeader} -import org.ergoplatform.{DataInput, ErgoBox, ErgoLikeContext, ErgoLikeTransaction, ErgoLikeTransactionTemplate, Input, UnsignedErgoLikeTransaction, UnsignedInput} -import sigma.ast.SType -import sigma.crypto.EcPointType -import sigma.data.AvlTreeData - -import scala.collection.mutable class JsonSerializationSpec extends SerializationSpecification with JsonCodecs { diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index 87a1599ab3..caf593084c 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -9,13 +9,13 @@ import org.scalacheck.Arbitrary.arbByte import org.scalacheck.{Arbitrary, Gen} import scorex.crypto.authds.ADKey import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, EvaluatedValue, FalseLeaf, TrueLeaf} -import sigmastate.crypto.DLogProtocol.ProveDlog import sigma.ast.{SByte, SType} import scorex.util._ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgoLikeTransaction, UnsignedInput} import sigmastate.eval.Extensions._ import scorex.util.{ModifierId, bytesToId} import sigma.crypto.CryptoFacade +import sigmastate.ProveDlog import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ From 728895b4182ae1fe9a43b406e69adda2ac2fd6e3 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 17:43:33 +0200 Subject: [PATCH 16/34] core-serializers: SigmaBoolean moved to core --- .../main/scala/sigma/data}/SigmaBoolean.scala | 18 ++++++--------- .../scala/sigma/data/SigmaPropCodes.scala | 23 +++++++++++++++++++ .../ProveDHTupleSerializer.scala | 13 ++++------- .../serialization/ProveDlogSerializer.scala | 20 ++++++++++++++++ .../src/main/scala/sigmastate/Platform.scala | 2 +- .../src/main/scala/sigmastate/Platform.scala | 2 +- .../scala/org/ergoplatform/ErgoAddress.scala | 1 + .../org/ergoplatform/ErgoTreePredef.scala | 1 + .../sigmastate/InterpreterReflection.scala | 1 + .../main/scala/sigmastate/SigSerializer.scala | 1 + .../main/scala/sigmastate/UncheckedTree.scala | 1 + .../main/scala/sigmastate/UnprovenTree.scala | 1 + .../src/main/scala/sigmastate/Values.scala | 2 +- .../sigmastate/crypto/DLogProtocol.scala | 3 +-- .../crypto/DiffieHellmanTupleProtocol.scala | 3 +-- .../crypto/SigmaProtocolFunctions.scala | 2 +- .../scala/sigmastate/eval/Extensions.scala | 4 ++-- .../main/scala/sigmastate/eval/package.scala | 2 +- .../interpreter/ErgoTreeEvaluator.scala | 4 ++-- .../scala/sigmastate/interpreter/Hint.scala | 4 ++-- .../sigmastate/interpreter/Interpreter.scala | 1 + .../interpreter/ProverInterpreter.scala | 3 ++- .../sigmastate/interpreter/ProverUtils.scala | 1 + .../serialization/DataSerializer.scala | 3 +-- .../sigmastate/serialization/OpCodes.scala | 17 -------------- .../serialization/ProveDlogSerializer.scala | 22 ------------------ .../main/scala/sigmastate/sigmastate.scala | 2 +- .../src/main/scala/sigmastate/trees.scala | 5 ++-- .../sigmastate/CalcSha256Specification.scala | 1 + .../src/test/scala/sigmastate/TestsBase.scala | 1 + .../crypto/SigningSpecification.scala | 5 ++-- .../scala/sigmastate/eval/BasicOpsTests.scala | 3 +-- .../ConstantSerializerSpecification.scala | 2 +- .../DataSerializerSpecification.scala | 2 +- .../PDHTSerializerSpecification.scala | 2 +- .../ProveDlogSerializerSpec.scala | 2 +- .../SigSerializerSpecification.scala | 3 +-- .../generators/ObjectGenerators.scala | 2 +- .../sigmastate/utxo/ProverSpecification.scala | 2 +- .../special/sigma/SigmaTestingData.scala | 2 +- .../scala/sigmastate/lang/LangTests.scala | 1 + .../org/ergoplatform/dsl/ContractSyntax.scala | 4 +--- .../scala/sigmastate/eval/TreeBuilding.scala | 1 + .../scala/sigmastate/lang/SigmaTyper.scala | 2 +- .../ErgoAddressSpecification.scala | 3 ++- .../ErgoLikeTransactionSpec.scala | 3 ++- .../org/ergoplatform/ErgoTreePredefSpec.scala | 2 +- .../DataValueComparerSpecification.scala | 4 ++-- .../scala/sigma/SigmaDslSpecification.scala | 7 +++--- .../scala/sigma/SigmaDslStaginTests.scala | 3 ++- .../test/scala/sigma/SigmaDslTesting.scala | 4 ++-- .../sigmastate/ErgoTreeSpecification.scala | 2 +- .../TestingInterpreterSpecification.scala | 2 +- .../sigmastate/eval/EvaluationTest.scala | 1 + .../helpers/CompilerTestingCommons.scala | 4 ++-- .../sigmastate/helpers/SigmaPPrint.scala | 2 +- .../sigmastate/lang/SigmaTyperTest.scala | 1 + .../DeserializationResilience.scala | 2 +- .../utxo/AVLTreeScriptsSpecification.scala | 2 +- .../utxo/ComplexSigSpecification.scala | 2 +- .../utxo/DistributedSigSpecification.scala | 3 ++- .../ErgoLikeInterpreterSpecification.scala | 2 +- .../utxo/ThresholdSpecification.scala | 4 ++-- .../UsingContextPropertiesSpecification.scala | 3 ++- .../examples/AssetsAtomicExchangeTests.scala | 3 ++- .../examples/CoopExampleSpecification.scala | 4 ++-- .../DHTupleExampleSpecification.scala | 4 ++-- .../utxo/examples/LetsSpecification.scala | 4 ++-- .../examples/MixExampleSpecification.scala | 4 ++-- .../RPSGameExampleSpecification.scala | 2 +- .../XorGameExampleSpecification.scala | 2 +- .../org/ergoplatform/sdk/js/SigmaProp.scala | 3 +-- .../org/ergoplatform/sdk/js/ValueSpec.scala | 3 +-- .../sdk/AppkitProvingInterpreter.scala | 3 +-- .../org/ergoplatform/sdk/JavaHelpers.scala | 3 +-- .../org/ergoplatform/sdk/utils/Zero.scala | 3 +-- .../wallet/secrets/ExtendedPublicKey.scala | 2 +- .../wallet/secrets/ExtendedSecretKey.scala | 3 +-- .../sdk/DataJsonEncoderSpecification.scala | 3 +-- .../sdk/JsonSerializationSpec.scala | 3 +-- .../sdk/wallet/utils/Generators.scala | 2 +- 81 files changed, 153 insertions(+), 151 deletions(-) rename {interpreter/shared/src/main/scala/sigmastate => core/shared/src/main/scala/sigma/data}/SigmaBoolean.scala (92%) create mode 100644 core/shared/src/main/scala/sigma/data/SigmaPropCodes.scala rename {interpreter/shared/src/main/scala/sigmastate/serialization/transformers => core/shared/src/main/scala/sigma/serialization}/ProveDHTupleSerializer.scala (58%) create mode 100644 core/shared/src/main/scala/sigma/serialization/ProveDlogSerializer.scala delete mode 100644 interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala diff --git a/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala b/core/shared/src/main/scala/sigma/data/SigmaBoolean.scala similarity index 92% rename from interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala rename to core/shared/src/main/scala/sigma/data/SigmaBoolean.scala index b11cac4741..681a7d40d2 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigmaBoolean.scala +++ b/core/shared/src/main/scala/sigma/data/SigmaBoolean.scala @@ -1,14 +1,11 @@ -package sigmastate +package sigma.data import debox.cfor import sigma.crypto.EcPointType -import sigma.serialization.GroupElementSerializer +import sigma.data.SigmaPropCodes.{AndCode, AtLeastCode, OrCode, ProveDiffieHellmanTupleCode, ProveDlogCode, SPCode} +import sigma.data.TrivialProp.{FalseProp, TrueProp} +import sigma.serialization.{CoreByteReader, CoreByteWriter, CoreSerializer, GroupElementSerializer, ProveDHTupleSerializer, ProveDlogSerializer} import sigma.util.safeNewArray -import sigmastate.TrivialProp.{FalseProp, TrueProp} -import sigmastate.serialization.SigmaPropCodes.{AndCode, AtLeastCode, OrCode, ProveDiffieHellmanTupleCode, ProveDlogCode, SPCode} -import sigmastate.serialization.{ProveDlogSerializer, SigmaPropCodes, SigmaSerializer} -import sigmastate.serialization.transformers.ProveDHTupleSerializer -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import scala.collection.mutable.ArrayBuffer @@ -35,12 +32,12 @@ object SigmaBoolean { } /** HOTSPOT: don't beautify this code */ - object serializer extends SigmaSerializer[SigmaBoolean, SigmaBoolean] { + object serializer extends CoreSerializer[SigmaBoolean, SigmaBoolean] { val dhtSerializer = ProveDHTupleSerializer(ProveDHTuple.apply) val dlogSerializer = ProveDlogSerializer(ProveDlog.apply) - override def serialize(data: SigmaBoolean, w: SigmaByteWriter): Unit = { + override def serialize(data: SigmaBoolean, w: CoreByteWriter): Unit = { w.put(data.opCode) data match { case dlog: ProveDlog => dlogSerializer.serialize(dlog, w) @@ -71,7 +68,7 @@ object SigmaBoolean { } } - override def parse(r: SigmaByteReader): SigmaBoolean = { + override def parse(r: CoreByteReader): SigmaBoolean = { val depth = r.level r.level = depth + 1 val opCode = r.getByte() @@ -107,7 +104,6 @@ object SigmaBoolean { res } } - } /** diff --git a/core/shared/src/main/scala/sigma/data/SigmaPropCodes.scala b/core/shared/src/main/scala/sigma/data/SigmaPropCodes.scala new file mode 100644 index 0000000000..17257c9ce5 --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/SigmaPropCodes.scala @@ -0,0 +1,23 @@ +package sigma.data + +import sigma.ast.TypeCodes.LastConstantCode +import supertagged.TaggedType + +/** Encoding of sigma proposition nodes. + * + * @see SigmaBoolean.opCode + */ +object SigmaPropCodes { + object SPCode extends TaggedType[Byte] + type SPCode = SPCode.Type + private def newOpCode(shift: Short): SPCode = SPCode @@ (LastConstantCode + shift).toByte + + val AndCode : SPCode = newOpCode(38) + val OrCode : SPCode = newOpCode(39) + val AtLeastCode : SPCode = newOpCode(40) + val ProveDlogCode : SPCode = newOpCode(93) + val ProveDiffieHellmanTupleCode: SPCode = newOpCode(94) + val TrivialPropFalseCode : SPCode = newOpCode(98) + val TrivialPropTrueCode : SPCode = newOpCode(99) +} + diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala b/core/shared/src/main/scala/sigma/serialization/ProveDHTupleSerializer.scala similarity index 58% rename from interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala rename to core/shared/src/main/scala/sigma/serialization/ProveDHTupleSerializer.scala index df8e5b6d71..3211e90b76 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/transformers/ProveDHTupleSerializer.scala +++ b/core/shared/src/main/scala/sigma/serialization/ProveDHTupleSerializer.scala @@ -1,23 +1,20 @@ -package sigmastate.serialization.transformers +package sigma.serialization import sigma.crypto.EcPointType -import sigma.serialization.GroupElementSerializer -import sigmastate.ProveDHTuple -import sigmastate.serialization._ -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} +import sigma.data.ProveDHTuple case class ProveDHTupleSerializer( cons: (EcPointType, EcPointType, EcPointType, EcPointType) => ProveDHTuple - ) extends SigmaSerializer[ProveDHTuple, ProveDHTuple] { + ) extends CoreSerializer[ProveDHTuple, ProveDHTuple] { - override def serialize(obj: ProveDHTuple, w: SigmaByteWriter): Unit = { + override def serialize(obj: ProveDHTuple, w: CoreByteWriter): Unit = { GroupElementSerializer.serialize(obj.gv, w) GroupElementSerializer.serialize(obj.hv, w) GroupElementSerializer.serialize(obj.uv, w) GroupElementSerializer.serialize(obj.vv, w) } - override def parse(r: SigmaByteReader) = { + override def parse(r: CoreByteReader) = { val gv = GroupElementSerializer.parse(r) val hv = GroupElementSerializer.parse(r) val uv = GroupElementSerializer.parse(r) diff --git a/core/shared/src/main/scala/sigma/serialization/ProveDlogSerializer.scala b/core/shared/src/main/scala/sigma/serialization/ProveDlogSerializer.scala new file mode 100644 index 0000000000..af849e8eed --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/ProveDlogSerializer.scala @@ -0,0 +1,20 @@ +package sigma.serialization + +import sigma.crypto.EcPointType +import sigma.data.ProveDlog + +case class ProveDlogSerializer(cons: EcPointType => ProveDlog) + extends CoreSerializer[ProveDlog, ProveDlog] { + + override def serialize(obj: ProveDlog, w: CoreByteWriter): Unit = + GroupElementSerializer.serialize(obj.value, w) + + override def parse(r: CoreByteReader) = { + val res = GroupElementSerializer.parse(r) + cons(res) + } +} + + + + diff --git a/interpreter/js/src/main/scala/sigmastate/Platform.scala b/interpreter/js/src/main/scala/sigmastate/Platform.scala index 1579cf6345..eff021c35f 100644 --- a/interpreter/js/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/js/src/main/scala/sigmastate/Platform.scala @@ -1,7 +1,7 @@ package sigmastate import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, Nullable} +import sigma.data.{AvlTreeData, Nullable, SigmaBoolean} import sigma.{Evaluation, VersionContext} import sigmastate.Values.{Constant, FalseLeaf, TrueLeaf} import sigmastate.eval.SigmaDsl diff --git a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala index b5c614f5e3..6db096b2da 100644 --- a/interpreter/jvm/src/main/scala/sigmastate/Platform.scala +++ b/interpreter/jvm/src/main/scala/sigmastate/Platform.scala @@ -1,7 +1,7 @@ package sigmastate import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, Nullable} +import sigma.data.{AvlTreeData, Nullable, SigmaBoolean} import sigma.{Evaluation, VersionContext} import sigmastate.Values.{Constant, FalseLeaf, TrueLeaf} import sigmastate.eval.SigmaDsl diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala index 6443ebe40e..74c56957d6 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala @@ -11,6 +11,7 @@ import sigmastate.serialization._ import sigmastate.utxo.{DeserializeContext, Slice} import sigma.{Coll, SigmaException} import sigma.ast.{SInt, SSigmaProp} +import sigma.data.ProveDlog import sigma.serialization.GroupElementSerializer import scala.util.Try diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala index a14907fd2e..8e60f2f7b6 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala @@ -3,6 +3,7 @@ package org.ergoplatform import org.ergoplatform.settings.MonetarySettings import sigma.ast.SCollection.SByteArray import sigma.ast.{SBox, SInt, SLong, SSigmaProp} +import sigma.data.ProveDlog import sigmastate.crypto.CryptoConstants import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} diff --git a/interpreter/shared/src/main/scala/sigmastate/InterpreterReflection.scala b/interpreter/shared/src/main/scala/sigmastate/InterpreterReflection.scala index 217903a619..e66547222a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/InterpreterReflection.scala +++ b/interpreter/shared/src/main/scala/sigmastate/InterpreterReflection.scala @@ -6,6 +6,7 @@ import sigma.reflection.{ReflectionData, mkConstructor, mkMethod} import sigma.Coll import sigma.ast.SCollection.{SBooleanArray, SByteArray, SIntArray} import sigma.ast.{SAny, SAvlTree, SCollectionType, SFunc, SNumericType, SOption, SSigmaProp, STuple, SType, STypeParam, STypeVar} +import sigma.data.{CAND, COR, CTHRESHOLD} import sigma.{AvlTree, SigmaDslBuilder} import sigmastate.SAvlTreeMethods.KeyValueColl import sigmastate.Values._ diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index 002089397c..5d802b38ff 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -2,6 +2,7 @@ package sigmastate import debox.cfor import scorex.util.encode.Base16 +import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean} import sigma.serialization.SerializerException import sigma.util.safeNewArray import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage diff --git a/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala b/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala index 343c80f1e6..2072b2e647 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UncheckedTree.scala @@ -1,5 +1,6 @@ package sigmastate +import sigma.data.{ProveDHTuple, ProveDlog} import sigmastate.crypto.DLogProtocol.{FirstDLogProverMessage, SecondDLogProverMessage} import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{FirstDHTupleProverMessage, GF2_192_Poly, SecondDHTupleProverMessage} diff --git a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala index 922a675cba..3df9ef1624 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala @@ -1,6 +1,7 @@ package sigmastate import debox.cfor +import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, SigmaLeaf} import sigmastate.Values.{ErgoTree, SigmaPropConstant} import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index decd468391..51f37e31da 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -7,7 +7,7 @@ import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.TypeCodes.ConstantCode import sigma.ast._ import sigma.crypto.EcPointType -import sigma.data.{Nullable, RType} +import sigma.data.{Nullable, RType, SigmaBoolean, TrivialProp} import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} import sigma.util.CollectionUtil._ import sigma.util.Extensions._ diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala index 2a6cc7cc7c..f78578d4b1 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DLogProtocol.scala @@ -3,13 +3,12 @@ package sigmastate.crypto import scorex.util.encode.Base16 import sigma.SigmaProp import sigma.crypto.EcPointType +import sigma.data.ProveDlog import sigma.serialization.GroupElementSerializer import sigmastate._ import sigmastate.crypto.CryptoConstants.dlogGroup import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.eval._ -import sigmastate.serialization.SigmaPropCodes -import sigmastate.serialization.SigmaPropCodes.SPCode import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala index 96e10eb1f4..1a02d92020 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/DiffieHellmanTupleProtocol.scala @@ -2,13 +2,12 @@ package sigmastate.crypto import sigma.SigmaProp import sigma.crypto.EcPointType +import sigma.data.ProveDHTuple import sigma.serialization.GroupElementSerializer import sigmastate.Values.Value.PropositionCode import sigmastate._ import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.eval.SigmaDsl -import sigmastate.serialization.SigmaPropCodes -import sigmastate.serialization.SigmaPropCodes.SPCode import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/crypto/SigmaProtocolFunctions.scala b/interpreter/shared/src/main/scala/sigmastate/crypto/SigmaProtocolFunctions.scala index 176edb201c..edfcf6bd68 100644 --- a/interpreter/shared/src/main/scala/sigmastate/crypto/SigmaProtocolFunctions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/crypto/SigmaProtocolFunctions.scala @@ -1,9 +1,9 @@ package sigmastate.crypto -import sigmastate.SigmaLeaf import sigmastate.crypto.CryptoConstants.dlogGroup import sigmastate.crypto.VerifierMessage.Challenge import sigma.Coll +import sigma.data.SigmaLeaf import supertagged.TaggedType import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 84f7c5f7d4..4027a69052 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -5,14 +5,14 @@ import org.ergoplatform.ErgoBox.TokenId import scorex.util.encode.Base16 import sigma.ast.SType.AnyOps import sigma.ast.{SBoolean, SCollection, SCollectionType, SType} -import sigma.data.{Nullable, RType} +import sigma.data.{Nullable, ProveDHTuple, ProveDlog, RType, SigmaBoolean} import sigma.util.Extensions.EcpOps import sigma.{Coll, _} import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers import sigmastate.utxo.SigmaPropIsProven -import sigmastate.{Platform, ProveDHTuple, ProveDlog, SigmaBoolean} +import sigmastate.Platform import java.math.BigInteger diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index eadf216732..4cf1680d1e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -3,7 +3,7 @@ package sigmastate import org.ergoplatform.ErgoBox import sigma.{Coll, _} import sigma.crypto.EcPointType -import sigma.data.{AvlTreeData, RType} +import sigma.data.{AvlTreeData, RType, SigmaBoolean} import sigmastate.exceptions.CostLimitException import supertagged.TaggedType diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala index 2a6790a16a..db9dcf8a93 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala @@ -1,7 +1,7 @@ package sigmastate.interpreter import org.ergoplatform.ErgoLikeContext -import sigmastate.{FixedCost, JitCost, PerItemCost, SigmaBoolean, TypeBasedCost} +import sigmastate.{FixedCost, JitCost, PerItemCost, TypeBasedCost} import sigmastate.Values._ import sigmastate.eval.Profiler import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv @@ -13,7 +13,7 @@ import supertagged.TaggedType import debox.{Buffer => DBuffer} import sigma.VersionContext import sigma.ast.SType - +import sigma.data.SigmaBoolean import scala.collection.compat.immutable.ArraySeq import scala.util.DynamicVariable diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala index 026393eb58..d4ce5bf155 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Hint.scala @@ -1,8 +1,8 @@ package sigmastate.interpreter +import sigma.data.{SigmaBoolean, SigmaLeaf} import java.math.BigInteger -import sigmastate.{NodePosition, SigmaLeaf, UncheckedTree} -import sigmastate.SigmaBoolean +import sigmastate.{NodePosition, UncheckedTree} import sigmastate.crypto.FirstProverMessage import sigmastate.crypto.VerifierMessage.Challenge diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala index ef0ab20eb9..1cc7dddd19 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/Interpreter.scala @@ -21,6 +21,7 @@ import sigmastate.lang.Terms.ValueOps import debox.cfor import sigma.ast.SCollection.SByteArray import sigma.ast.{SBoolean, SSigmaProp, SType} +import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, TrivialProp} import sigma.{Evaluation, VersionContext} import sigma.kiama.rewriting.Strategy import sigma.validation.ValidationRules.trySoftForkable diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala index 2953e8ed0c..8c4ab8c4a9 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala @@ -4,7 +4,7 @@ import sigma.kiama.rewriting.Rewriter.{everywherebu, everywheretd, rule} import sigma.util.CollectionUtil._ import sigma.VersionContext import sigma.kiama.rewriting.Strategy -import sigmastate.TrivialProp.{FalseProp, TrueProp} +import sigma.data.TrivialProp.{FalseProp, TrueProp} import sigmastate.Values._ import sigma.VersionContext.MaxSupportedScriptVersion import sigmastate._ @@ -16,6 +16,7 @@ import sigmastate.eval.Extensions.ArrayOps import sigmastate.exceptions.InterpreterException import sigmastate.utils.Helpers import sigma.Coll +import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean} import java.math.BigInteger import scala.util.Try diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala index 82ae451eae..1a6c21d420 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala @@ -1,5 +1,6 @@ package sigmastate.interpreter +import sigma.data.{ProveDHTuple, ProveDlog, SigmaBoolean, SigmaConjecture, SigmaLeaf} import sigmastate._ import sigmastate.Values.ErgoTree import sigmastate.crypto.DLogProtocol.DLogInteractiveProver diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 3c855532ab..d3af7af754 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -3,8 +3,7 @@ package sigmastate.serialization import java.math.BigInteger import java.nio.charset.StandardCharsets import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, RType} -import sigmastate.SigmaBoolean +import sigma.data.{AvlTreeData, RType, SigmaBoolean} import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import sigmastate.eval._ import sigma.{Evaluation, _} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala index e6c74eb24e..6a05091e3e 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/OpCodes.scala @@ -3,23 +3,6 @@ package sigmastate.serialization import sigma.ast.TypeCodes.LastConstantCode import supertagged.TaggedType -/** Encoding of sigma proposition nodes. - * @see SigmaBoolean.opCode - */ -object SigmaPropCodes { - object SPCode extends TaggedType[Byte] - type SPCode = SPCode.Type - private def newOpCode(shift: Short): SPCode = SPCode @@ (LastConstantCode + shift).toByte - - val AndCode : SPCode = newOpCode(38) - val OrCode : SPCode = newOpCode(39) - val AtLeastCode : SPCode = newOpCode(40) - val ProveDlogCode : SPCode = newOpCode(93) - val ProveDiffieHellmanTupleCode: SPCode = newOpCode(94) - val TrivialPropFalseCode : SPCode = newOpCode(98) - val TrivialPropTrueCode : SPCode = newOpCode(99) -} - /** Encoding of values for serialization. */ object ValueCodes { object OpCode extends TaggedType[Byte] diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala deleted file mode 100644 index 5148d9a85b..0000000000 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ProveDlogSerializer.scala +++ /dev/null @@ -1,22 +0,0 @@ -package sigmastate.serialization - -import sigma.crypto.EcPointType -import sigma.serialization.GroupElementSerializer -import sigmastate.ProveDlog -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} - -case class ProveDlogSerializer(cons: EcPointType => ProveDlog) - extends SigmaSerializer[ProveDlog, ProveDlog] { - - override def serialize(obj: ProveDlog, w: SigmaByteWriter): Unit = - GroupElementSerializer.serialize(obj.value, w) - - override def parse(r: SigmaByteReader) = { - val res = GroupElementSerializer.parse(r) - cons(res) - } -} - - - - diff --git a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala index e07eb34401..b08c2927b6 100644 --- a/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala +++ b/interpreter/shared/src/main/scala/sigmastate/sigmastate.scala @@ -1,6 +1,6 @@ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoLikeContext} import sigma.ast._ -import sigma.data.{AvlTreeData, GeneralType, RType} +import sigma.data.{AvlTreeData, GeneralType, RType, SigmaBoolean} import sigmastate.Values._ import sigmastate.lang.{CheckingSigmaBuilder, Terms} diff --git a/interpreter/shared/src/main/scala/sigmastate/trees.scala b/interpreter/shared/src/main/scala/sigmastate/trees.scala index e058a2d05b..fb5f178389 100644 --- a/interpreter/shared/src/main/scala/sigmastate/trees.scala +++ b/interpreter/shared/src/main/scala/sigmastate/trees.scala @@ -4,7 +4,7 @@ import debox.{cfor, Map => DMap} import sigma.data.ExactIntegral._ import sigma.data.ExactOrdering._ import sigma.data.OverloadHack.Overloaded1 -import sigma.data.{ExactIntegral, ExactOrdering, SigmaConstants} +import sigma.data.{CAND, COR, CTHRESHOLD, ExactIntegral, ExactOrdering, SigmaBoolean, SigmaConstants} import scorex.crypto.hash.{Blake2b256, CryptographicHash32, Sha256} import sigma.ast.SCollection import sigma.ast.SCollection.{SByteArray, SIntArray} @@ -22,7 +22,6 @@ import sigmastate.eval.SigmaDsl import sigmastate.interpreter.ErgoTreeEvaluator import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv import sigmastate.serialization.OpCodes._ -import sigmastate.serialization.SigmaPropCodes.SPCode import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization._ import sigmastate.utxo.{SimpleTransformerCompanion, Transformer} @@ -343,7 +342,7 @@ object AtLeast extends ValueCompanion { /** HOTSPOT: don't beautify this code */ def reduce(bound: Int, children: Seq[SigmaBoolean]): SigmaBoolean = { - import sigmastate.TrivialProp._ + import sigma.data.TrivialProp._ if (bound <= 0) return TrueProp val nChildren = children.length if (bound > nChildren) return FalseProp diff --git a/interpreter/shared/src/test/scala/sigmastate/CalcSha256Specification.scala b/interpreter/shared/src/test/scala/sigmastate/CalcSha256Specification.scala index db4312872d..637a84785a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/CalcSha256Specification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/CalcSha256Specification.scala @@ -3,6 +3,7 @@ package sigmastate import org.scalatest.prop.TableFor2 import scorex.util.encode.Base16 import sigma.ast.SByte +import sigma.data.TrivialProp import sigmastate.Values.{ByteArrayConstant, CollectionConstant} import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, TestingCommons} diff --git a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala index 90b98b7f04..c4ed244170 100644 --- a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala +++ b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala @@ -4,6 +4,7 @@ import org.scalatest.matchers.should.Matchers import sigmastate.Values.{ErgoTree, SigmaPropValue} import org.ergoplatform.ErgoTreePredef import sigma.VersionTesting +import sigma.data.SigmaBoolean trait TestsBase extends Matchers with VersionTesting { /** Set this to true to enable debug console output in tests */ diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala index 631c0247cc..d65c5d1dce 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala @@ -2,11 +2,12 @@ package sigmastate.crypto import org.scalacheck.Gen import scorex.util.encode.Base16 -import sigmastate.{AtLeast, CAND, COR, ProveDHTuple, SigmaBoolean} +import sigma.data.{CAND, COR, ProveDHTuple, SigmaBoolean} +import sigma.serialization.ProveDHTupleSerializer +import sigmastate.AtLeast import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, TestingCommons} import sigmastate.interpreter.{ContextExtension, HintsBag, ProverResult} -import sigmastate.serialization.transformers.ProveDHTupleSerializer class SigningSpecification extends TestingCommons { diff --git a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala index 07b8fdcddb..17e0ba34dd 100644 --- a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala +++ b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala @@ -2,9 +2,8 @@ package sigmastate.eval import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers - +import sigma.data.TrivialProp import java.math.BigInteger -import sigmastate.TrivialProp import sigmastate.crypto.SecP256K1Group import sigma.{ContractsTestkit, SigmaDslBuilder, SigmaProp} diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala index 9ee6c96144..2eddd79e84 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala @@ -3,7 +3,7 @@ package sigmastate.serialization import java.math.BigInteger import org.ergoplatform._ import org.scalacheck.Arbitrary._ -import sigma.data.{RType, TupleColl} +import sigma.data.{RType, SigmaBoolean, TupleColl} import sigma.ast.SCollection.SByteArray import sigmastate.Values.{BigIntConstant, ByteArrayConstant, Constant, FalseLeaf, GroupGenerator, LongConstant, SValue, TrueLeaf} import sigmastate._ diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 3fa1d2691e..014c7ae4e8 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -3,7 +3,7 @@ package sigmastate.serialization import java.math.BigInteger import org.ergoplatform.ErgoBox import org.scalacheck.Arbitrary._ -import sigma.data.{RType, TupleColl} +import sigma.data.{RType, SigmaBoolean, TupleColl} import sigma.ast.SCollection.SByteArray import sigmastate.Values.ErgoTree import sigmastate._ diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala index 782d97e3a1..93c5c7a715 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala @@ -1,6 +1,6 @@ package sigmastate.serialization -import sigmastate.ProveDHTuple +import sigma.data.ProveDHTuple import sigmastate.eval.Extensions.SigmaBooleanOps class PDHTSerializerSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala index 2a45b18e47..a9472adcf0 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala @@ -1,6 +1,6 @@ package sigmastate.serialization -import sigmastate.ProveDlog +import sigma.data.ProveDlog import sigmastate.eval.Extensions.SigmaBooleanOps class ProveDlogSerializerSpec extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index f12036b95a..84c4d12c91 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -4,8 +4,7 @@ import java.math.BigInteger import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} import org.scalatest.Assertion -import sigma.data.AvlTreeData -import sigmastate.SigmaBoolean +import sigma.data.{AvlTreeData, CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, TrivialProp} import sigmastate._ import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 339755895a..fd962c3ee5 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -8,7 +8,7 @@ import org.scalacheck.Arbitrary._ import org.scalacheck.Gen.{choose, frequency} import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, RType, SigmaBoolean} import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.encode.{Base58, Base64} import scorex.util.{ModifierId, bytesToId} diff --git a/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala index a703f88824..c15b00deea 100644 --- a/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/utxo/ProverSpecification.scala @@ -2,7 +2,7 @@ package sigmastate.utxo import org.ergoplatform.ErgoLikeInterpreter import scorex.crypto.hash.Blake2b256 -import sigmastate.SigmaBoolean +import sigma.data.{CAND, COR, CTHRESHOLD, SigmaBoolean, TrivialProp} import sigmastate._ import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.{FirstDHTupleProverMessage, SecP256K1Group} diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 5ac45656ed..9d9c17127a 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -6,7 +6,7 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.containerOfN import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, COR, ProveDHTuple, ProveDlog, RType} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index e9b5c1dac1..5c7d3f8d96 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -14,6 +14,7 @@ import sigmastate.eval._ import sigmastate.helpers.NegativeTesting import sigma.Coll import sigma.ast._ +import sigma.data.{ProveDHTuple, ProveDlog, SigmaBoolean} trait LangTests extends Matchers with NegativeTesting { diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala index ea28230e89..61046b22d0 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala @@ -2,7 +2,7 @@ package org.ergoplatform.dsl import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.TokenId -import sigma.data.{AvlTreeData, RType} +import sigma.data.{AvlTreeData, RType, SigmaBoolean} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition} import org.ergoplatform.sdk.JavaHelpers.collRType import sigmastate.eval.CostingSigmaDslBuilder @@ -10,8 +10,6 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigma._ import sigma.ast.SType import sigma.ast.SType.AnyOps -import sigmastate.SigmaBoolean - import scala.reflect.ClassTag import scala.util.Try diff --git a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala index edcfac3f28..f6b0b2fd2e 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/TreeBuilding.scala @@ -8,6 +8,7 @@ import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ConstantStore import sigma.ast._ +import sigma.data.{ProveDHTuple, ProveDlog} import scala.collection.mutable.ArrayBuffer import sigmastate.serialization.ValueCodes.OpCode diff --git a/sc/shared/src/main/scala/sigmastate/lang/SigmaTyper.scala b/sc/shared/src/main/scala/sigmastate/lang/SigmaTyper.scala index 2c9f5bd92d..768eda4fc4 100644 --- a/sc/shared/src/main/scala/sigmastate/lang/SigmaTyper.scala +++ b/sc/shared/src/main/scala/sigmastate/lang/SigmaTyper.scala @@ -5,7 +5,7 @@ import sigma.ast.SCollection.{SBooleanArray, SByteArray} import sigmastate.Values._ import sigmastate._ import sigma.ast._ -import sigma.data.Nullable +import sigma.data.{Nullable, SigmaBoolean} import sigma.util.Extensions.Ensuring import sigmastate.lang.Terms._ import sigmastate.exceptions._ diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 6fc8bb00ce..65b369a496 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -18,9 +18,10 @@ import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.ValueSerializer import sigmastate.utils.Helpers._ -import sigmastate.{CompilerCrossVersionProps, ProveDlog, SigmaAnd} +import sigmastate.{CompilerCrossVersionProps, SigmaAnd} import sigma.SigmaDslTesting import sigma.ast.SType +import sigma.data.ProveDlog import sigma.serialization.GroupElementSerializer import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckTypeCode diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala index 7f66d3f7d6..d8a5589986 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala @@ -11,12 +11,13 @@ import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SigmaSerializer import sigmastate.eval._ import sigmastate.eval.Extensions._ -import sigmastate.{TrivialProp, _} +import sigmastate._ import sigma.ast.SType._ import sigmastate.helpers.TestingHelpers.copyTransaction import sigmastate.utils.Helpers import sigma.SigmaDslTesting import sigma.Extensions._ +import sigma.data.TrivialProp class ErgoLikeTransactionSpec extends SigmaDslTesting { diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index 356ba3ea19..07ae812798 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -9,7 +9,7 @@ import scorex.crypto.hash.Blake2b256 import scorex.util.Random import sigma.Colls import sigma.ast.SByte -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog, TrivialProp} import sigma.util.BenchmarkUtil.measure import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ diff --git a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala index df3c5dc7ed..37574855c8 100644 --- a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala +++ b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala @@ -1,9 +1,9 @@ package sigma import org.scalatest.BeforeAndAfterAll -import sigma.data.RType +import sigma.data.{RType, TrivialProp} import sigma.util.BenchmarkUtil -import sigmastate.{DataValueComparer, JitCost, TrivialProp} +import sigmastate.{DataValueComparer, JitCost} import sigmastate.Values.ErgoTree import sigmastate.eval.{CSigmaProp, Profiler, SigmaDsl} import sigmastate.helpers.SigmaPPrint diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 72f78a0e3c..0b741ed8ee 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4,7 +4,7 @@ import java.math.BigInteger import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, ExactIntegral, ExactNumeric, ExactOrdering, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, CGroupElement, COR, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} @@ -20,7 +20,7 @@ import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.lang.Terms.{MethodCall, PropertyCall} import sigmastate.utxo._ -import sigma._ +import sigma.{VersionContext, data, _} import sigma.Extensions._ import sigmastate.utils.Helpers import sigmastate.utils.Helpers._ @@ -36,7 +36,6 @@ import sigma.data.RType._ import scorex.util.ModifierId import sigmastate.interpreter._ import org.scalactic.source.Position -import sigma.VersionContext import sigma.ast._ import sigmastate.helpers.SigmaPPrint import sigmastate.exceptions.GraphBuildingException @@ -9518,7 +9517,7 @@ class SigmaDslSpecification extends SigmaDslTesting "0008980204cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) ), cost = 1780, newDetails(18), expectedNewCost = 1780), - CSigmaProp(COR(Array(pk, dht, and, or, threshold))) -> Expected(Success( + CSigmaProp(data.COR(Array(pk, dht, and, or, threshold))) -> Expected(Success( Helpers.decodeBytes( "00089705cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441980204cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419702cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b04419602cd039d0b1e46c21540d033143440d2fb7dd5d650cf89981c99ee53c6e0374d2b1b6fce03c046fccb95549910767d0543f5e8ce41d66ae6a8720a46f4049cac3b3d26dafb023479c9c3b86a0d3c8be3db0a2d186788e9af1db76d55f3dad127d15185d83d0303d7898641cb6653585a8e1dabfa7f665e61e0498963e329e6e3744bd764db2d72037ae057d89ec0b46ff8e9ff4c37e85c12acddb611c3f636421bef1542c11b0441" ) diff --git a/sc/shared/src/test/scala/sigma/SigmaDslStaginTests.scala b/sc/shared/src/test/scala/sigma/SigmaDslStaginTests.scala index ea29c47745..ed0f97ec24 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslStaginTests.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslStaginTests.scala @@ -2,9 +2,10 @@ package sigma import org.scalatest.BeforeAndAfterAll import scalan.{BaseCtxTests, BaseLiftableTests} +import sigma.data.TrivialProp import sigmastate.eval.Extensions._ import sigmastate.eval._ -import sigmastate.{TrivialProp, eval} +import sigmastate.eval import scala.language.reflectiveCalls diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index 04b49387a2..1b179956f6 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -12,7 +12,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scalan.Platform.threadSleepOrNoOp -import sigma.data.{CollType, OptionType, PairType, RType} +import sigma.data.{CollType, OptionType, PairType, ProveDlog, RType, SigmaLeaf} import sigma.util.BenchmarkUtil import sigma.util.CollectionUtil._ import sigma.util.Extensions._ @@ -35,7 +35,7 @@ import sigmastate.utxo.{DeserializeContext, DeserializeRegister, GetVar, OptionG import sigma.ast.{SOption, SSigmaProp, SType} import sigma.validation.ValidationRules.CheckSerializableTypeCode import sigma.validation.{SigmaValidationSettings, ValidationException} -import sigmastate.{ProveDlog, SigmaLeaf, eval} +import sigmastate.eval import scala.collection.mutable import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala index 69de85f901..259ffc3c15 100644 --- a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform.settings.ErgoAlgos import org.ergoplatform.validation.ValidationRules import org.ergoplatform.{ErgoAddressEncoder, ErgoBox, ErgoLikeContext, Self} import sigma.data.RType.asType -import sigma.data.{Nullable, RType} +import sigma.data.{Nullable, RType, TrivialProp} import sigma.{Evaluation, VersionContext} import sigma.ast.SCollection.SByteArray import sigmastate.Values._ diff --git a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala index 5c6ee9e43a..145af35401 100644 --- a/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/TestingInterpreterSpecification.scala @@ -9,7 +9,7 @@ import sigmastate.lang.Terms._ import org.ergoplatform._ import org.scalatest.BeforeAndAfterAll import scorex.util.encode.Base58 -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, CAND, ProveDlog, SigmaBoolean, TrivialProp} import sigma.util.Extensions.IntOps import sigmastate.crypto.CryptoConstants import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala index b20772ad44..8a32e105c8 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala @@ -7,6 +7,7 @@ import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import scalan.BaseCtxTests import sigma.ast.SSigmaProp +import sigma.data.ProveDlog import sigmastate.lang.LangTests import sigma.util.BenchmarkUtil._ import sigmastate._ diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 2de7090561..98e241dfcb 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -9,7 +9,7 @@ import sigma.util.BenchmarkUtil import scalan.TestContexts import sigma.ast.{SOption, SType} import sigma.{Colls, Evaluation, TestUtils} -import sigma.data.RType +import sigma.data.{RType, SigmaBoolean} import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckSerializableTypeCode import sigmastate.Values.{Constant, ErgoTree, SValue, SigmaPropValue} @@ -21,7 +21,7 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.interpreter._ import sigmastate.lang.{CompilerSettings, SigmaCompiler, Terms} import sigmastate.serialization.SigmaSerializer -import sigmastate.{CompilerTestsBase, JitCost, SigmaBoolean} +import sigmastate.{CompilerTestsBase, JitCost} import scala.language.implicitConversions import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index a89246654e..3460090424 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -7,7 +7,7 @@ import pprint.{PPrinter, Tree} import sigma.ast.SCollection.{SBooleanArray, SByteArray, SByteArray2} import sigma.ast._ import sigma.crypto.EcPointType -import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, PrimitiveType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, PrimitiveType, TrivialProp} import sigma.serialization.GroupElementSerializer import sigma.{Coll, GroupElement} import sigmastate.Values.{ConstantNode, ErgoTree, FuncValue, ValueCompanion} diff --git a/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala b/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala index 68807ddc4f..a81feda76b 100644 --- a/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala +++ b/sc/shared/src/test/scala/sigmastate/lang/SigmaTyperTest.scala @@ -10,6 +10,7 @@ import sigma.ast.SCollection._ import sigmastate.Values._ import sigmastate._ import sigma.ast._ +import sigma.data.ProveDlog import sigmastate.crypto.CryptoConstants import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.exceptions.TyperException diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index 2fc3b6ace2..4291096f0e 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -7,7 +7,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.serialization.{Reader, VLQByteBufferReader} import sigma.ast.{SBoolean, SInt} -import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, SigmaBoolean} import sigma.serialization.{DeserializeCallDepthExceeded, InvalidTypePrefix, ReaderPositionLimitExceeded, SerializerException} import sigma.util.{BenchmarkUtil, safeNewArray} import sigma.validation.ValidationException diff --git a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index 452e40c957..2bf3726191 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -19,7 +19,7 @@ import sigmastate.interpreter.ProverResult import sigmastate.lang.Terms._ import sigma.Coll import sigma.ast.SAvlTree -import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigma.data.{AvlTreeData, AvlTreeFlags, TrivialProp} import sigma.{AvlTree, Context} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala index bf2d793b28..984afbcd37 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ComplexSigSpecification.scala @@ -2,7 +2,7 @@ package sigmastate.utxo import org.ergoplatform.Height import org.scalacheck.Gen -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, CAND, COR, CTHRESHOLD} import sigmastate.Values.IntConstant import sigmastate._ import sigmastate.helpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala index 9276413e1e..e60cb6878d 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala @@ -1,7 +1,8 @@ package sigmastate.utxo +import sigma.data.CAND import sigmastate._ -import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeTestProvingInterpreter, CompilerTestingCommons} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeTestProvingInterpreter} import sigmastate.interpreter._ import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index cfa6bd9d96..363b48f13f 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -9,7 +9,7 @@ import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigmastate._ import sigma.ast._ -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog, TrivialProp} import sigma.util.Extensions.EcpOps import sigma.validation.ValidationException import sigmastate.eval._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala index eec366fd76..9bc564f13e 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala @@ -1,7 +1,7 @@ package sigmastate.utxo import sigma.ast.SSigmaProp -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, CAND, COR, ProveDlog, TrivialProp} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.Values.{ConcreteCollection, FalseLeaf, IntConstant, SigmaPropConstant, SigmaPropValue, TrueLeaf} import sigmastate._ @@ -116,7 +116,7 @@ class ThresholdSpecification extends CompilerTestingCommons } property("threshold reduce to crypto") { - import TrivialProp._ + import sigma.data.TrivialProp._ val prover = new ContextEnrichingTestProvingInterpreter val ctx = ErgoLikeContextTesting( currentHeight = 1, diff --git a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala index e83d9a8569..8cbf3f3312 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala @@ -1,6 +1,6 @@ package sigmastate.utxo -import sigmastate.{TrivialProp, CompilerCrossVersionProps} +import sigmastate.CompilerCrossVersionProps import sigmastate.eval.{IRContext, CSigmaProp} import sigmastate.eval.Extensions._ import sigma.Context @@ -8,6 +8,7 @@ import sigmastate.helpers.CompilerTestingCommons import org.ergoplatform.dsl.{SigmaContractSyntax, ContractSpec, TestContractSpec} import org.ergoplatform.ErgoBox import scorex.crypto.hash.Blake2b256 +import sigma.data.TrivialProp class UsingContextPropertiesSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { suite => diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala index b80794c3f2..394f2cfbe8 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala @@ -1,6 +1,6 @@ package sigmastate.utxo.examples -import org.ergoplatform.{Height, Outputs, ErgoBox, Self} +import org.ergoplatform.{ErgoBox, Height, Outputs, Self} import org.ergoplatform.ErgoBox.R4 import sigmastate.helpers.CompilerTestingCommons import org.ergoplatform.dsl.ContractSyntax.Token @@ -9,6 +9,7 @@ import scorex.crypto.hash.Blake2b256 import sigma.ast.SCollection.SByteArray import sigmastate._ import sigma.ast._ +import sigma.data.TrivialProp import sigmastate.Values.{BlockValue, ByteArrayConstant, LongConstant, ValDef, ValUse, Value} import sigmastate.eval.{CSigmaProp, Digest32Coll} import sigmastate.eval.Extensions._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index 2efde3ac0c..74345ceafe 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -5,12 +5,12 @@ import org.scalatest.Assertion import org.scalatest.TryValues._ import sigmastate.crypto.DLogProtocol.DLogProverInput import scorex.crypto.hash.Blake2b256 -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog} import sigmastate.Values.{BooleanConstant, ByteArrayConstant, ErgoTree} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ -import sigmastate.{CompilerCrossVersionProps, ProveDlog} +import sigmastate.CompilerCrossVersionProps class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala index 05b2c9a996..05ad14c969 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/DHTupleExampleSpecification.scala @@ -3,9 +3,9 @@ package sigmastate.utxo.examples import java.math.BigInteger import org.ergoplatform.ErgoBox.{R4, R5} -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog} import sigma.util.Extensions.EcpOps -import sigmastate.{CompilerCrossVersionProps, ProveDHTuple, ProveDlog} +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala index 109682e42f..4d18c98ef4 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala @@ -6,8 +6,8 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert, Lookup} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls -import sigma.data.{AvlTreeData, AvlTreeFlags} -import sigmastate.{CompilerCrossVersionProps, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags, TrivialProp} +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.{AvlTreeConstant, ByteArrayConstant, LongConstant, SigmaPropConstant} import sigmastate.eval.Extensions.ArrayOps import sigmastate.eval.{Digest32Coll, IRContext, SigmaDsl} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index a12687e907..9fb10bf45f 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -3,9 +3,9 @@ package sigmastate.utxo.examples import java.math.BigInteger import org.ergoplatform.ErgoBox.{R4, R5} import scorex.crypto.hash.Blake2b256 -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog} import sigma.util.Extensions.EcpOps -import sigmastate.{CompilerCrossVersionProps, ProveDHTuple, ProveDlog} +import sigmastate.CompilerCrossVersionProps import sigmastate.Values.GroupElementConstant import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala index ce6ccb7de5..0a89c40b0a 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala @@ -4,7 +4,7 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5, R6, R7} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog} import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala index 5e3e8ea446..a98e6410e3 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/XorGameExampleSpecification.scala @@ -4,7 +4,7 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5, R6} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog} import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala index 17b25c1321..d53ff9f82f 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala @@ -1,7 +1,6 @@ package org.ergoplatform.sdk.js -import sigmastate.{ProveDlog, SigmaBoolean} - +import sigma.data.{ProveDlog, SigmaBoolean} import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala index 12ba83be9c..6b35a79289 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala @@ -13,8 +13,7 @@ import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.serialization.ConstantSerializer import sigmastate.utils.Helpers import sigma.SigmaTestingData -import sigmastate.ProveDlog - +import sigma.data.ProveDlog import java.math.BigInteger class ValueSpec extends AnyPropSpec with Matchers with SigmaTestingData with ScalaCheckPropertyChecks { diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala index ed24634c77..c5890cbf4c 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala @@ -9,9 +9,8 @@ import org.ergoplatform.sdk.wallet.protocol.context.{BlockchainStateContext, Tra import org.ergoplatform.sdk.wallet.secrets.ExtendedSecretKey import org.ergoplatform.validation.ValidationRules import sigma.VersionContext -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog, SigmaBoolean} import sigma.util.Extensions.LongOps -import sigmastate.{ProveDlog, SigmaBoolean} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.{DiffieHellmanTupleProverInput, SigmaProtocolPrivateInput} import sigmastate.interpreter.Interpreter.{ReductionResult, estimateCryptoVerifyCost} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index f4c777651e..55a01286b4 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -15,11 +15,10 @@ import scorex.util.{ModifierId, bytesToId, idToBytes} import sigma.ast.SType import sigma.crypto.{CryptoFacade, EcPointType} import sigma.data.ExactIntegral.LongIsExactIntegral -import sigma.data.{InverseIso, Iso, RType, SigmaConstants} +import sigma.data.{InverseIso, Iso, ProveDHTuple, ProveDlog, RType, SigmaBoolean, SigmaConstants} import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} -import sigmastate.{ProveDHTuple, ProveDlog, SigmaBoolean} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DiffieHellmanTupleProverInput import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index b6cce164a3..ed1cde1774 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.utils import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CollType, FuncType, OptionType, PairType, RType, TupleType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CollType, FuncType, OptionType, PairType, RType, TrivialProp, TupleType} import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} @@ -9,7 +9,6 @@ import scorex.util.ModifierId import sigmastate.Values.ErgoTree import sigmastate.crypto.CryptoConstants import sigmastate.eval._ -import sigmastate.TrivialProp import sigma._ import java.math.BigInteger diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala index b681f62d75..99179e73d0 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedPublicKey.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.wallet.secrets import sigma.crypto.CryptoFacade -import sigmastate.ProveDlog +import sigma.data.ProveDlog import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.CryptoConstants import sigmastate.crypto.BigIntegers diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala index a09f34014b..9e871f7bfb 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/wallet/secrets/ExtendedSecretKey.scala @@ -1,8 +1,7 @@ package org.ergoplatform.sdk.wallet.secrets import sigma.crypto.CryptoFacade -import sigmastate.ProveDlog - +import sigma.data.ProveDlog import java.math.BigInteger import sigmastate.crypto.BigIntegers import sigmastate.crypto.DLogProtocol.DLogProverInput diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index 168e3dee44..87fedce55a 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -4,13 +4,12 @@ package org.ergoplatform.sdk import java.math.BigInteger import org.scalacheck.Arbitrary._ import org.scalacheck.Gen -import sigma.data.{RType, TupleColl} +import sigma.data.{RType, SigmaBoolean, TupleColl} import sigma.ast._ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps import sigma.crypto.EcPointType import sigma.serialization.SerializerException -import sigmastate.SigmaBoolean import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigma.{AvlTree, Box, Colls, Evaluation} diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index affaa96c4b..0a5d101b70 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -11,9 +11,8 @@ import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.ModifierId import scorex.util.encode.Base16 import sigma.ast.SType -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, ProveDlog} import sigma.{Coll, Header, PreHeader} -import sigmastate.ProveDlog import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} import sigmastate.crypto.CryptoConstants import sigmastate.eval.Digest32Coll diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index caf593084c..1469551299 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -15,7 +15,7 @@ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgo import sigmastate.eval.Extensions._ import scorex.util.{ModifierId, bytesToId} import sigma.crypto.CryptoFacade -import sigmastate.ProveDlog +import sigma.data.ProveDlog import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ From c8de98333633ca1cb1597840447f32f851012af5 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 11 Sep 2023 20:12:12 +0200 Subject: [PATCH 17/34] core-serializers: CSigmaProp moved to core --- .../main/scala/sigma/data/CSigmaProp.scala | 42 +++++++++++++++++++ .../main/scala/sigma/util/Extensions.scala | 12 +++++- .../src/main/scala/sigmastate/Values.scala | 2 +- .../sigmastate/eval/CostingDataContext.scala | 35 ---------------- .../scala/sigmastate/eval/Extensions.scala | 8 ---- .../special/sigma/SigmaTestingData.scala | 4 +- .../ErgoLikeTransactionSpec.scala | 6 +-- .../ergoplatform/dsl/TestContractSpec.scala | 4 +- .../DataValueComparerSpecification.scala | 4 +- .../scala/sigma/SigmaDslSpecification.scala | 2 +- .../test/scala/sigma/SigmaDslTesting.scala | 2 +- .../scala/sigmastate/TypesSpecification.scala | 3 +- .../utxo/AVLTreeScriptsSpecification.scala | 4 +- .../UsingContextPropertiesSpecification.scala | 6 +-- .../examples/AssetsAtomicExchangeTests.scala | 4 +- .../scala/org/ergoplatform/sdk/js/Value.scala | 5 +-- .../org/ergoplatform/sdk/utils/Zero.scala | 2 +- 17 files changed, 77 insertions(+), 68 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/data/CSigmaProp.scala diff --git a/core/shared/src/main/scala/sigma/data/CSigmaProp.scala b/core/shared/src/main/scala/sigma/data/CSigmaProp.scala new file mode 100644 index 0000000000..d20f550abc --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/CSigmaProp.scala @@ -0,0 +1,42 @@ +package sigma.data + +import sigma.util.Extensions.SigmaBooleanOps +import sigma.{Coll, Colls, SigmaProp} + +/** A default implementation of [[SigmaProp]] interface. + * + * @see [[SigmaProp]] for detailed descriptions + */ +case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[SigmaBoolean] { + override def wrappedValue: SigmaBoolean = sigmaTree + + // TODO refactor: remove this (it shouldn't be used in interpreter) + override def isValid: Boolean = sigmaTree match { + case p: TrivialProp => p.condition + case _ => sys.error(s"Method CostingSigmaProp.isValid is not defined for $sigmaTree") + } + + override def propBytes: Coll[Byte] = { + // in order to have comparisons like `box.propositionBytes == pk.propBytes` we need to make sure + // the same serialization method is used in both cases + // TODO v6.0: add `pk.propBytes(version)` (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/903) +// val root = sigmaTree.toSigmaProp +// val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(root), 0, null, None) +// val bytes = DefaultSerializer.serializeErgoTree(ergoTree) +// Colls.fromArray(bytes) + Colls.emptyColl + } + + override def &&(other: SigmaProp): SigmaProp = other match { + case other: CSigmaProp => + CSigmaProp(CAND.normalized(Array(sigmaTree, other.sigmaTree))) + } + + override def ||(other: SigmaProp): SigmaProp = other match { + case other: CSigmaProp => + CSigmaProp(COR.normalized(Array(sigmaTree, other.sigmaTree))) + } + + override def toString: String = s"SigmaProp(${wrappedValue.showToString})" +} + diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index deef8c998e..2da61aeccf 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -2,7 +2,7 @@ package sigma.util import sigma.GroupElement import sigma.crypto.{CryptoFacade, Ecp} -import sigma.data.{CBigInt, CGroupElement} +import sigma.data.{CBigInt, CGroupElement, ProveDHTuple, ProveDlog, SigmaBoolean} import debox.{cfor, Buffer => DBuffer} import java.math.BigInteger @@ -315,4 +315,14 @@ object Extensions { result } } + + implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { + def showToString: String = sb match { + case ProveDlog(v) => + s"ProveDlog(${v.showECPoint})" + case ProveDHTuple(gv, hv, uv, vv) => + s"ProveDHTuple(${gv.showECPoint}, ${hv.showECPoint}, ${uv.showECPoint}, ${vv.showECPoint})" + case _ => sb.toString + } + } } diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 51f37e31da..6e13aa17e4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -15,7 +15,7 @@ import sigma.validation.ValidationException import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} import sigmastate.Values.ErgoTree.substConstants import sigmastate.crypto.CryptoConstants -import sigmastate.eval.Extensions._ +import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} import sigmastate.eval._ import sigmastate.exceptions.InterpreterException import sigmastate.interpreter.ErgoTreeEvaluator._ diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 2e08949a9f..55fb2989ac 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -31,41 +31,6 @@ import scala.annotation.unused import scala.reflect.ClassTag import scala.util.{Failure, Success} -/** A default implementation of [[SigmaProp]] interface. - * @see [[SigmaProp]] for detailed descriptions - */ -case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[SigmaBoolean] { - override def wrappedValue: SigmaBoolean = sigmaTree - - // TODO refactor: remove this (it shouldn't be used in interpreter) - override def isValid: Boolean = sigmaTree match { - case p: TrivialProp => p.condition - case _ => sys.error(s"Method CostingSigmaProp.isValid is not defined for $sigmaTree") - } - - override def propBytes: Coll[Byte] = { - // in order to have comparisons like `box.propositionBytes == pk.propBytes` we need to make sure - // the same serialization method is used in both cases - // TODO v6.0: add `pk.propBytes(version)` (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/903) - val root = sigmaTree.toSigmaProp - val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(root), 0, null, None) - val bytes = DefaultSerializer.serializeErgoTree(ergoTree) - Colls.fromArray(bytes) - } - - override def &&(other: SigmaProp): SigmaProp = other match { - case other: CSigmaProp => - CSigmaProp(CAND.normalized(Array(sigmaTree, other.sigmaTree))) - } - - override def ||(other: SigmaProp): SigmaProp = other match { - case other: CSigmaProp => - CSigmaProp(COR.normalized(Array(sigmaTree, other.sigmaTree))) - } - - override def toString: String = s"SigmaProp(${wrappedValue.showToString})" -} - /** Implements operations of AVL tree verifier based on * [[scorex.crypto.authds.avltree.batch.BatchAVLVerifier]]. * diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 4027a69052..300481ace8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -108,13 +108,5 @@ object Extensions { def toSigmaProp: SigmaPropValue = SigmaPropConstant(sb) def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb)) - - def showToString: String = sb match { - case ProveDlog(v) => - s"ProveDlog(${v.showECPoint})" - case ProveDHTuple(gv, hv, uv, vv) => - s"ProveDHTuple(${gv.showECPoint}, ${hv.showECPoint}, ${uv.showECPoint}, ${vv.showECPoint})" - case _ => sb.toString - } } } diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 9d9c17127a..36b224fae9 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -6,14 +6,14 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.containerOfN import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, COR, ProveDHTuple, ProveDlog, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, COR, CSigmaProp, ProveDHTuple, ProveDlog, RType} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigmastate.eval._ import sigmastate.eval.Extensions._ -import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, CSigmaProp, CostingBox, CostingSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, CostingBox, CostingSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala index d8a5589986..dd5ca0bee9 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala @@ -3,9 +3,9 @@ package org.ergoplatform import org.ergoplatform.ErgoBox.TokenId import org.ergoplatform.settings.ErgoAlgos import scorex.util.encode.Base16 -import scorex.util.{Random, ModifierId} +import scorex.util.{ModifierId, Random} import sigma.ast.SCollection.SByteArray -import sigma.ast.{SSigmaProp, SType, SPair, SInt} +import sigma.ast.{SInt, SPair, SSigmaProp, SType} import sigmastate.Values._ import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SigmaSerializer @@ -17,7 +17,7 @@ import sigmastate.helpers.TestingHelpers.copyTransaction import sigmastate.utils.Helpers import sigma.SigmaDslTesting import sigma.Extensions._ -import sigma.data.TrivialProp +import sigma.data.{CSigmaProp, TrivialProp} class ErgoLikeTransactionSpec extends SigmaDslTesting { diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index cbd9124e05..0b26809e3b 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -7,14 +7,14 @@ import sigmastate.interpreter.{CostedProverResult, ProverResult} import scala.collection.mutable.ArrayBuffer import org.ergoplatform.ErgoBox.{NonMandatoryRegisterId, TokenId} -import sigma.data.{AvlTreeData, Nullable} +import sigma.data.{AvlTreeData, CSigmaProp, Nullable} import scala.util.Try import org.ergoplatform.{ErgoBox, ErgoLikeContext} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import sigma.ast.SType import sigmastate.Values.{ErgoTree, EvaluatedValue} -import sigmastate.eval.{CAnyValue, CSigmaProp, IRContext} +import sigmastate.eval.{CAnyValue, IRContext} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps diff --git a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala index 37574855c8..ea1102d893 100644 --- a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala +++ b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala @@ -1,11 +1,11 @@ package sigma import org.scalatest.BeforeAndAfterAll -import sigma.data.{RType, TrivialProp} +import sigma.data.{CSigmaProp, RType, TrivialProp} import sigma.util.BenchmarkUtil import sigmastate.{DataValueComparer, JitCost} import sigmastate.Values.ErgoTree -import sigmastate.eval.{CSigmaProp, Profiler, SigmaDsl} +import sigmastate.eval.{Profiler, SigmaDsl} import sigmastate.helpers.SigmaPPrint import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator, EvalSettings, TracedCost} import sigma.Coll diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 0b741ed8ee..9f47024b38 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4,7 +4,7 @@ import java.math.BigInteger import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, CGroupElement, COR, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, CGroupElement, COR, CSigmaProp, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index 1b179956f6..e727907bf5 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -21,7 +21,7 @@ import sigma.ast.SType.AnyOps import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, IntConstant, SValue} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput -import sigmastate.eval.Extensions._ +import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} import sigmastate.eval.{CompiletimeIRContext, CostingBox, CostingDataContext, IRContext, SigmaDsl} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, SigmaPPrint} diff --git a/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala b/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala index 64a4d8a0d5..9e4e784d25 100644 --- a/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala @@ -2,9 +2,10 @@ package sigmastate import sigma.Environment import sigma.ast.SType.isValueOfType -import sigmastate.eval.{CSigmaProp, CostingSigmaDslBuilder} +import sigmastate.eval.CostingSigmaDslBuilder import sigma.SigmaTestingData import sigma.ast._ +import sigma.data.CSigmaProp class TypesSpecification extends SigmaTestingData { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index 2bf3726191..4d91fd63b1 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -9,7 +9,7 @@ import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigmastate._ -import sigmastate.eval.{CSigmaProp, IRContext} +import sigmastate.eval.IRContext import sigmastate.eval._ import sigmastate.eval.Extensions._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} @@ -19,7 +19,7 @@ import sigmastate.interpreter.ProverResult import sigmastate.lang.Terms._ import sigma.Coll import sigma.ast.SAvlTree -import sigma.data.{AvlTreeData, AvlTreeFlags, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags, CSigmaProp, TrivialProp} import sigma.{AvlTree, Context} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala index 8cbf3f3312..5c4a0828f9 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala @@ -1,14 +1,14 @@ package sigmastate.utxo import sigmastate.CompilerCrossVersionProps -import sigmastate.eval.{IRContext, CSigmaProp} +import sigmastate.eval.IRContext import sigmastate.eval.Extensions._ import sigma.Context import sigmastate.helpers.CompilerTestingCommons -import org.ergoplatform.dsl.{SigmaContractSyntax, ContractSpec, TestContractSpec} +import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, TestContractSpec} import org.ergoplatform.ErgoBox import scorex.crypto.hash.Blake2b256 -import sigma.data.TrivialProp +import sigma.data.{CSigmaProp, TrivialProp} class UsingContextPropertiesSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { suite => diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala index 394f2cfbe8..03aee196aa 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala @@ -9,9 +9,9 @@ import scorex.crypto.hash.Blake2b256 import sigma.ast.SCollection.SByteArray import sigmastate._ import sigma.ast._ -import sigma.data.TrivialProp +import sigma.data.{CSigmaProp, TrivialProp} import sigmastate.Values.{BlockValue, ByteArrayConstant, LongConstant, ValDef, ValUse, Value} -import sigmastate.eval.{CSigmaProp, Digest32Coll} +import sigmastate.eval.Digest32Coll import sigmastate.eval.Extensions._ import sigmastate.lang.Terms.ValueOps import sigmastate.utxo._ diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index c1c89d4b9a..bb2c594bb0 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -1,13 +1,12 @@ package org.ergoplatform.sdk.js import org.ergoplatform.sdk.js.Value.toRuntimeData -import sigma.data.{CGroupElement, CollType, RType} -import sigma.data.PairType +import sigma.data.{CGroupElement, CSigmaProp, CollType, PairType, RType} import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType import sigma.crypto.Platform -import sigmastate.eval.{CAvlTree, CSigmaProp, CostingBox, SigmaDsl} +import sigmastate.eval.{CAvlTree, CostingBox, SigmaDsl} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index ed1cde1774..926515fb0f 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.utils import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CollType, FuncType, OptionType, PairType, RType, TrivialProp, TupleType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CSigmaProp, CollType, FuncType, OptionType, PairType, RType, TrivialProp, TupleType} import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} From 44d2463428822b70898d3b7cabee1e99c68eb59c Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Tue, 12 Sep 2023 10:16:07 +0200 Subject: [PATCH 18/34] core-serializers: CSigmaProp.propBytes implemented without ErgoTree --- .../src/main/scala/sigma/SigmaException.scala | 2 +- .../src/main/scala/sigma/data/CSigmaProp.scala | 13 ++++++++----- .../sigma/serialization/SerializerException.scala | 2 +- .../src/main/scala/sigma/util/Extensions.scala | 15 ++++++++++++--- .../sigmastate/serialization/DataSerializer.scala | 4 ++-- .../ErgoTreeSerializerSpecification.scala | 13 +++++++++++++ .../scala/org/ergoplatform/sdk/js/ValueSpec.scala | 4 ++-- 7 files changed, 39 insertions(+), 14 deletions(-) diff --git a/core/shared/src/main/scala/sigma/SigmaException.scala b/core/shared/src/main/scala/sigma/SigmaException.scala index 60bf3403e9..146844b583 100644 --- a/core/shared/src/main/scala/sigma/SigmaException.scala +++ b/core/shared/src/main/scala/sigma/SigmaException.scala @@ -1,6 +1,6 @@ package sigma -import scala.collection.immutable.ArraySeq +import scala.collection.compat.immutable.ArraySeq /** Base class for Sigma-related exceptions. * diff --git a/core/shared/src/main/scala/sigma/data/CSigmaProp.scala b/core/shared/src/main/scala/sigma/data/CSigmaProp.scala index d20f550abc..8a37705f8c 100644 --- a/core/shared/src/main/scala/sigma/data/CSigmaProp.scala +++ b/core/shared/src/main/scala/sigma/data/CSigmaProp.scala @@ -1,5 +1,7 @@ package sigma.data +import sigma.ast.SSigmaProp +import sigma.serialization.CoreSerializer import sigma.util.Extensions.SigmaBooleanOps import sigma.{Coll, Colls, SigmaProp} @@ -20,11 +22,11 @@ case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[ // in order to have comparisons like `box.propositionBytes == pk.propBytes` we need to make sure // the same serialization method is used in both cases // TODO v6.0: add `pk.propBytes(version)` (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/903) -// val root = sigmaTree.toSigmaProp -// val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(root), 0, null, None) -// val bytes = DefaultSerializer.serializeErgoTree(ergoTree) -// Colls.fromArray(bytes) - Colls.emptyColl + val w = CoreSerializer.startWriter() + w.put(0) // ErgoTree.header + w.putType(SSigmaProp) + SigmaBoolean.serializer.serialize(wrappedValue, w) + Colls.fromArray(w.toBytes) } override def &&(other: SigmaProp): SigmaProp = other match { @@ -40,3 +42,4 @@ case class CSigmaProp(sigmaTree: SigmaBoolean) extends SigmaProp with WrapperOf[ override def toString: String = s"SigmaProp(${wrappedValue.showToString})" } + diff --git a/core/shared/src/main/scala/sigma/serialization/SerializerException.scala b/core/shared/src/main/scala/sigma/serialization/SerializerException.scala index cd82c5b6fe..7373bf5ce1 100644 --- a/core/shared/src/main/scala/sigma/serialization/SerializerException.scala +++ b/core/shared/src/main/scala/sigma/serialization/SerializerException.scala @@ -2,7 +2,7 @@ package sigma.serialization import sigma.SigmaException -import scala.collection.immutable.ArraySeq +import scala.collection.compat.immutable.ArraySeq /** Exception thrown during serialization. * diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index 2da61aeccf..2478203985 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -1,9 +1,9 @@ package sigma.util -import sigma.GroupElement -import sigma.crypto.{CryptoFacade, Ecp} -import sigma.data.{CBigInt, CGroupElement, ProveDHTuple, ProveDlog, SigmaBoolean} import debox.{cfor, Buffer => DBuffer} +import sigma.crypto.{CryptoFacade, Ecp} +import sigma.data._ +import sigma.{GroupElement, SigmaProp} import java.math.BigInteger import java.nio.ByteBuffer @@ -325,4 +325,13 @@ object Extensions { case _ => sb.toString } } + + implicit class SigmaPropOps(val sb: SigmaProp) extends AnyVal { + /** Extracts [[sigma.SigmaBoolean]] from the SigmaProp instance. */ + def toSigmaBoolean: SigmaBoolean = sb.asInstanceOf[CSigmaProp].wrappedValue + } + +// implicit class AvlTreeOps(val tree: AvlTree) extends AnyVal { +// def toAvlTreeData: AvlTreeData = tree.asInstanceOf[CAvlTree].wrappedValue +// } } diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index d3af7af754..140fa0b507 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -10,7 +10,7 @@ import sigma.{Evaluation, _} import debox.cfor import sigma.ast._ import sigma.serialization.{GroupElementSerializer, SerializerException} -import sigma.util.Extensions.{BigIntOps, GroupElementOps} +import sigma.util.Extensions.{BigIntOps, GroupElementOps, SigmaPropOps} import sigma.validation.ValidationRules.CheckSerializableTypeCode import scala.collection.mutable @@ -41,7 +41,7 @@ object DataSerializer { GroupElementSerializer.serialize(v.asInstanceOf[GroupElement].toECPoint, w) case SSigmaProp => val p = v.asInstanceOf[SigmaProp] - SigmaBoolean.serializer.serialize(sigmaPropToSigmaBoolean(p), w) + SigmaBoolean.serializer.serialize(p.toSigmaBoolean, w) case SBox => val b = v.asInstanceOf[Box] ErgoBox.sigmaSerializer.serialize(boxToErgoBox(b), w) diff --git a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala index a9bc820806..9517ac9821 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala @@ -3,12 +3,16 @@ package sigmastate.serialization import java.math.BigInteger import org.ergoplatform.ErgoBox import org.ergoplatform.validation.ValidationRules.CheckDeserializedScriptIsSigmaProp +import sigma.SigmaProp import sigma.ast.SInt import sigma.data.CBigInt import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} +import sigma.util.Extensions.SigmaPropOps import sigma.validation.ValidationException +import sigmastate.Values.ErgoTree.EmptyConstants import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, ErgoTree, IntConstant, ShortConstant, SigmaPropValue, UnparsedErgoTree} import sigmastate._ +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval.IRContext import sigmastate.helpers.CompilerTestingCommons import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer @@ -209,4 +213,13 @@ class ErgoTreeSerializerSpecification extends SerializationSpecification test(positions = Array(1, 2), expected = Array(-1, 0, 1, -1, -1)) test(positions = Array(1, 2, 4), expected = Array(-1, 0, 1, -1, 2)) } + + property("SigmaProp.propBytes vs ErgoTree.serializer equivalence") { + forAll(MinSuccessful(100)) { sp: SigmaProp => + val propBytes = sp.propBytes + val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(sp.toSigmaBoolean.toSigmaProp), 0, null, None) + val treeBytes = DefaultSerializer.serializeErgoTree(ergoTree) + treeBytes shouldBe propBytes.toArray + } + } } diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala index 6b35a79289..c6f64ab319 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala @@ -8,12 +8,12 @@ import sigma.ast.SType import sigmastate.Values.{AvlTreeConstant, BigIntConstant, BooleanConstant, BoxConstant, ByteConstant, Constant, GroupElementConstant, IntConstant, LongConstant, ShortConstant, SigmaPropConstant, UnitConstant} import sigmastate.crypto.CryptoConstants.dlogGroup import sigma.crypto.CryptoFacade -import sigmastate.eval.CSigmaProp import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.serialization.ConstantSerializer import sigmastate.utils.Helpers import sigma.SigmaTestingData -import sigma.data.ProveDlog +import sigma.data.{CSigmaProp, ProveDlog} + import java.math.BigInteger class ValueSpec extends AnyPropSpec with Matchers with SigmaTestingData with ScalaCheckPropertyChecks { From 2d00c561091421a6d3ebe5daeef6fd60a72babd2 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Tue, 12 Sep 2023 16:58:23 +0200 Subject: [PATCH 19/34] core-serializers: toColl extension method moved to core --- .../src/main/scala/sigma/Extensions.scala | 6 ++++++ .../main/scala/sigma/util/Extensions.scala | 4 ++++ .../main/scala/org/ergoplatform/ErgoBox.scala | 3 +-- .../org/ergoplatform/ErgoBoxCandidate.scala | 4 +--- .../org/ergoplatform/ErgoLikeContext.scala | 1 + .../main/scala/sigmastate/SigSerializer.scala | 2 +- .../src/main/scala/sigmastate/Values.scala | 7 ++++--- .../sigmastate/eval/CostingDataContext.scala | 1 + .../scala/sigmastate/eval/Extensions.scala | 7 +------ .../main/scala/sigmastate/eval/package.scala | 6 ------ .../interpreter/ProverInterpreter.scala | 2 +- .../scala/sigmastate/eval/BasicOpsTests.scala | 4 +++- .../helpers/ErgoLikeContextTesting.scala | 3 ++- .../sigmastate/lang/SigmaBuilderTest.scala | 2 +- .../AndSerializerSpecification.scala | 2 +- .../serialization/AvlTreeSpecification.scala | 2 +- .../ConstantSerializerSpecification.scala | 10 ++++----- .../DataSerializerSpecification.scala | 9 ++++---- .../GroupElementSerializerSpecification.scala | 10 ++++----- .../OrSerializerSpecification.scala | 2 +- .../PDHTSerializerSpecification.scala | 2 +- .../ProveDlogSerializerSpec.scala | 2 +- .../SigSerializerSpecification.scala | 5 +++-- .../generators/ObjectGenerators.scala | 12 ++++++----- .../scala/sigmastate/utils/HelpersTests.scala | 2 +- .../special/sigma/ContractsTestkit.scala | 1 + .../special/sigma/SigmaTestingData.scala | 3 +-- .../scala/sigmastate/lang/LangTests.scala | 5 +++-- .../scala/sigmastate/ErgoTreeBenchmarks.scala | 2 +- .../sigmastate/helpers/SigmaPPrintSpec.scala | 8 +++---- .../test/scala/sigma/SigmaDslTesting.scala | 9 ++++---- .../DeserializationResilience.scala | 9 ++++---- .../ErgoTreeSerializerSpecification.scala | 2 +- .../utxo/AVLTreeScriptsSpecification.scala | 2 +- .../utxo/BasicOpsSpecification.scala | 21 ++++++++++--------- .../CollectionOperationsSpecification.scala | 4 ++-- .../ErgoLikeInterpreterSpecification.scala | 2 +- .../utxo/ThresholdSpecification.scala | 2 +- .../UsingContextPropertiesSpecification.scala | 2 +- .../BlockchainSimulationTestingCommons.scala | 2 +- .../examples/FsmExampleSpecification.scala | 6 +++--- .../sigmastate/utxo/examples/IcoExample.scala | 2 +- .../utxo/examples/LetsSpecification.scala | 2 +- .../examples/MASTExampleSpecification.scala | 2 +- .../OracleExamplesSpecification.scala | 2 +- .../scala/org/ergoplatform/sdk/js/Isos.scala | 2 +- .../org/ergoplatform/sdk/JsonCodecs.scala | 1 + .../sdk/UnsignedTransactionBuilder.scala | 2 +- .../sdk/DataJsonEncoderSpecification.scala | 9 ++++---- .../sdk/JsonSerializationSpec.scala | 1 + .../sdk/wallet/utils/Generators.scala | 5 +++-- 51 files changed, 116 insertions(+), 102 deletions(-) diff --git a/core/shared/src/main/scala/sigma/Extensions.scala b/core/shared/src/main/scala/sigma/Extensions.scala index 08087eced0..c4590eabae 100644 --- a/core/shared/src/main/scala/sigma/Extensions.scala +++ b/core/shared/src/main/scala/sigma/Extensions.scala @@ -3,8 +3,14 @@ package sigma import debox.cfor import scorex.util.encode.Base16 import scorex.util.{ModifierId, bytesToId} +import sigma.data.RType object Extensions { + implicit class ArrayOps[T: RType](arr: Array[T]) { + /** Wraps array into Coll instance. The source array in not cloned. */ + @inline def toColl: Coll[T] = Colls.fromArray(arr) + } + /** Extension methods for `Coll[T]`. */ implicit class CollOps[T](val source: Coll[T]) extends AnyVal { /** Applies a function `f` to each element of the `source` collection. */ diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index 2478203985..cd92221b89 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -317,6 +317,10 @@ object Extensions { } implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { + /** Wraps SigmaBoolean into SigmaProp. */ + def toSigmaProp: SigmaProp = CSigmaProp(sb) + + /** Human readable string representation of the proposition. */ def showToString: String = sb match { case ProveDlog(v) => s"ProveDlog(${v.showECPoint})" diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala index e4ee583ffa..b0e46dc866 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala @@ -6,13 +6,12 @@ import scorex.crypto.authds.ADKey import scorex.crypto.hash.Blake2b256 import scorex.util._ import scorex.utils.{Ints, Shorts} +import sigma.Extensions.ArrayOps import sigma.ast.SCollection.SByteArray import sigma.ast.{SCollection, SLong, STuple, SType} import sigma.ast.SType.AnyOps import sigma.data.SigmaConstants import sigmastate.Values._ -import sigmastate._ -import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index c174e04d00..72cd413c4e 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala @@ -4,14 +4,12 @@ import debox.cfor import org.ergoplatform.ErgoBox._ import org.ergoplatform.settings.ErgoAlgos import scorex.util.{ModifierId, bytesToId} -import sigma.Extensions.CollOps +import sigma.Extensions.{ArrayOps, CollOps} import sigma.ast.SType import sigma.ast.SType.AnyOps import sigma.util.safeNewArray import sigma.{Coll, Colls} import sigmastate.Values._ -import sigmastate._ -import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 32de8630a9..9a5f105df3 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -11,6 +11,7 @@ import sigmastate.serialization.OpCodes import sigma.Coll import sigma.{AnyValue, Header, PreHeader} import debox.cfor +import sigma.Extensions.ArrayOps import sigma.ast.{SBox, SCollection, SContext, SFunc, SGlobal, SInt, SType, SUnit} import sigma.ast.SType.{AnyOps, TypeCode} import sigma.data.{AvlTreeData, SigmaConstants} diff --git a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala index 5d802b38ff..1823189bd0 100644 --- a/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/SigSerializer.scala @@ -2,13 +2,13 @@ package sigmastate import debox.cfor import scorex.util.encode.Base16 +import sigma.Extensions.ArrayOps import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean} import sigma.serialization.SerializerException import sigma.util.safeNewArray import sigmastate.crypto.DLogProtocol.SecondDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{BigIntegers, CryptoConstants, GF2_192_Poly, SecondDHTupleProverMessage} -import sigmastate.eval.Extensions.ArrayOps import sigmastate.interpreter.ErgoTreeEvaluator.{fixedCostOp, perItemCostOp} import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo} import sigmastate.serialization.SigmaSerializer diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 6e13aa17e4..2d71c995eb 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -3,6 +3,7 @@ package sigmastate import debox.cfor import org.ergoplatform.settings.ErgoAlgos import scorex.util.encode.Base16 +import sigma.Extensions.ArrayOps import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.TypeCodes.ConstantCode import sigma.ast._ @@ -15,7 +16,7 @@ import sigma.validation.ValidationException import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} import sigmastate.Values.ErgoTree.substConstants import sigmastate.crypto.CryptoConstants -import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval._ import sigmastate.exceptions.InterpreterException import sigmastate.interpreter.ErgoTreeEvaluator._ @@ -1321,14 +1322,14 @@ object Values { * without performing constant segregation. */ implicit def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = { - withoutSegregation(pk.toSigmaProp) + withoutSegregation(pk.toSigmaPropValue) } /** Create new ErgoTree for the given sigma proposition using the given header flags * and without performing constant segregation. */ def fromSigmaBoolean(headerFlags: Byte, pk: SigmaBoolean): ErgoTree = { - withoutSegregation(headerFlags, pk.toSigmaProp) + withoutSegregation(headerFlags, pk.toSigmaPropValue) } /** Build ErgoTree via serialization of the value with ConstantSegregationHeader, constants segregated diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 55fb2989ac..429eed5979 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -7,6 +7,7 @@ import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADDigest, ADKey, ADValue, SerializedAdProof} import scorex.crypto.hash.{Blake2b256, Digest32, Sha256} import scorex.utils.{Ints, Longs} +import sigma.Extensions.ArrayOps import sigma.ast.SCollection.SByteArray import sigma.ast.{SInt, STuple, SType} import sigma.crypto.{EcPointType, Ecp} diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 300481ace8..c0dfbc3d39 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -32,11 +32,6 @@ object Extensions { @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(x)) } - implicit class ArrayOps[T: RType](arr: Array[T]) { - /** Wraps array into Coll instance. The source array in not cloned. */ - @inline def toColl: Coll[T] = Colls.fromArray(arr) - } - /** Extension methods for `Coll[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. */ @@ -105,7 +100,7 @@ object Extensions { } implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { - def toSigmaProp: SigmaPropValue = SigmaPropConstant(sb) + def toSigmaPropValue: SigmaPropValue = SigmaPropConstant(sb) def isProven: Value[SBoolean.type] = SigmaPropIsProven(SigmaPropConstant(sb)) } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index 4cf1680d1e..e0cad6ab19 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -30,12 +30,6 @@ package object eval { * Here BigInt is Dsl type and BigInteger is wrapped type. * @see `sigma.CBigInt` */ - implicit def bigIntegerToBigInt(bi: BigInteger): BigInt = SigmaDsl.BigInt(bi) - implicit def bigIntToBigInteger(bi: BigInt): BigInteger = SigmaDsl.toBigInteger(bi) - - implicit def ecPointToGroupElement(p: EcPointType): GroupElement = SigmaDsl.GroupElement(p) - implicit def groupElementToECPoint(p: GroupElement): EcPointType = SigmaDsl.toECPoint(p).asInstanceOf[EcPointType] - implicit def sigmaBooleanToSigmaProp(p: SigmaBoolean): SigmaProp = SigmaDsl.SigmaProp(p) implicit def sigmaPropToSigmaBoolean(p: SigmaProp): SigmaBoolean = SigmaDsl.toSigmaBoolean(p) diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala index 8c4ab8c4a9..26593f0160 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala @@ -12,10 +12,10 @@ import sigmastate.crypto.DLogProtocol._ import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto._ import sigmastate.crypto.{GF2_192, GF2_192_Poly} -import sigmastate.eval.Extensions.ArrayOps import sigmastate.exceptions.InterpreterException import sigmastate.utils.Helpers import sigma.Coll +import sigma.Extensions.ArrayOps import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean} import java.math.BigInteger diff --git a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala index 17e0ba34dd..7a1887a384 100644 --- a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala +++ b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala @@ -3,6 +3,8 @@ package sigmastate.eval import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers import sigma.data.TrivialProp +import sigma.util.Extensions.SigmaBooleanOps + import java.math.BigInteger import sigmastate.crypto.SecP256K1Group import sigma.{ContractsTestkit, SigmaDslBuilder, SigmaProp} @@ -12,7 +14,7 @@ import scala.language.implicitConversions class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers { override val SigmaDsl: SigmaDslBuilder = CostingSigmaDslBuilder - implicit def boolToSigma(b: Boolean): SigmaProp = TrivialProp(b) + implicit def boolToSigma(b: Boolean): SigmaProp = TrivialProp(b).toSigmaProp test("atLeast") { val props = Colls.fromArray(Array[SigmaProp](false, true, true, false)) diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index eeb249699c..eac35d5ba0 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -5,6 +5,7 @@ import org.ergoplatform._ import org.ergoplatform.validation.ValidationRules import sigma.data.AvlTreeData import sigma.serialization.GroupElementSerializer +import sigma.util.Extensions.EcpOps import sigma.validation.SigmaValidationSettings import sigma.{Box, Coll, Colls, Header, PreHeader} import sigmastate.crypto.CryptoConstants @@ -31,7 +32,7 @@ object ErgoLikeContextTesting { timestamp = 3, nBits = 0, height = currentHeight, - minerPk = GroupElementSerializer.parse(SigmaSerializer.startReader(minerPk)), + minerPk = GroupElementSerializer.parse(SigmaSerializer.startReader(minerPk)).toGroupElement, votes = Colls.emptyColl[Byte] ) diff --git a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala index d850308c7b..35bc8bdc07 100644 --- a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala +++ b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala @@ -7,7 +7,7 @@ import sigma.data.{Nullable, RType} import sigma.{Environment, VersionContext} import sigmastate.Values._ import sigmastate._ -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.eval.{CAnyValue, CAvlTree, CostingBox, SigmaDsl} import sigmastate.exceptions.ConstraintFailed import sigmastate.serialization.OpCodes diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/AndSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/AndSerializerSpecification.scala index 9911583dc5..71e8c607ce 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/AndSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/AndSerializerSpecification.scala @@ -2,9 +2,9 @@ package sigmastate.serialization import sigmastate.Values.{BooleanConstant, Constant, IntConstant} import sigmastate._ -import sigmastate.eval.Extensions._ import sigmastate.serialization.OpCodes._ import scorex.util.encode.ZigZagEncoder.encodeZigZagInt +import sigma.Extensions.ArrayOps import sigma.ast.{SBoolean, SCollection, SCollectionType, SInt} class AndSerializerSpecification extends TableSerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala index 0dda4b81d2..485f23f87c 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala @@ -6,8 +6,8 @@ import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values.AvlTreeConstant -import sigmastate.eval.Extensions._ import sigmastate.eval._ +import sigma.Extensions.ArrayOps class AvlTreeSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala index 2eddd79e84..788090fed6 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ConstantSerializerSpecification.scala @@ -6,9 +6,8 @@ import org.scalacheck.Arbitrary._ import sigma.data.{RType, SigmaBoolean, TupleColl} import sigma.ast.SCollection.SByteArray import sigmastate.Values.{BigIntConstant, ByteArrayConstant, Constant, FalseLeaf, GroupGenerator, LongConstant, SValue, TrueLeaf} -import sigmastate._ import sigmastate.eval._ -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps import sigmastate.Values._ import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps @@ -16,6 +15,7 @@ import scorex.util.encode.Base16 import sigma.ast._ import sigma.crypto.EcPointType import sigma.serialization.SerializerException +import sigma.util.Extensions.{BigIntegerOps, EcpOps, SigmaBooleanOps} import sigmastate.lang.DeserializationSigmaBuilder import scala.annotation.nowarn @@ -67,9 +67,9 @@ class ConstantSerializerSpecification extends TableSerializationSpecification { forAll { x: Boolean => roundTripTest(BooleanConstant.fromBoolean(x)) } forAll { x: Long => roundTripTest(Constant[SLong.type](x, SLong)) } forAll { x: String => roundTripTest(Constant[SString.type](x, SString)) } - forAll { x: BigInteger => roundTripTest(Constant[SBigInt.type](x, SBigInt)) } - forAll { x: EcPointType => roundTripTest(Constant[SGroupElement.type](x, SGroupElement)) } - forAll { x: SigmaBoolean => roundTripTest(Constant[SSigmaProp.type](x, SSigmaProp)) } + forAll { x: BigInteger => roundTripTest(Constant[SBigInt.type](x.toBigInt, SBigInt)) } + forAll { x: EcPointType => roundTripTest(Constant[SGroupElement.type](x.toGroupElement, SGroupElement)) } + forAll { x: SigmaBoolean => roundTripTest(Constant[SSigmaProp.type](x.toSigmaProp, SSigmaProp)) } forAll { x: ErgoBox => roundTripTest(Constant[SBox.type](x, SBox)) } forAll { x: AvlTree => roundTripTest(Constant[SAvlTree.type](x, SAvlTree)) } forAll { x: Array[Byte] => roundTripTest(Constant[SByteArray](x.toColl, SByteArray)) } diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 014c7ae4e8..98085fbb3f 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -8,13 +8,14 @@ import sigma.ast.SCollection.SByteArray import sigmastate.Values.ErgoTree import sigmastate._ import sigmastate.eval._ -import sigmastate.eval.Extensions._ import sigma.{AvlTree, Colls, Evaluation} import sigma.ast.SType.AnyOps import sigma.ast._ import org.scalacheck.Gen +import sigma.Extensions.ArrayOps import sigma.crypto.EcPointType import sigma.serialization.SerializerException +import sigma.util.Extensions.{BigIntegerOps, EcpOps, SigmaBooleanOps} import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator} import sigmastate.interpreter.ErgoTreeEvaluator.DefaultProfiler import sigmastate.utils.Helpers @@ -98,9 +99,9 @@ class DataSerializerSpecification extends SerializationSpecification { forAll { x: Boolean => roundtrip[SBoolean.type](x, SBoolean) } forAll { x: Long => roundtrip[SLong.type](x, SLong) } forAll { x: String => roundtrip[SString.type](x, SString) } - forAll { x: BigInteger => roundtrip[SBigInt.type](x, SBigInt) } - forAll { x: EcPointType => roundtrip[SGroupElement.type](x, SGroupElement) } - forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x, SSigmaProp) } + forAll { x: BigInteger => roundtrip[SBigInt.type](x.toBigInt, SBigInt) } + forAll { x: EcPointType => roundtrip[SGroupElement.type](x.toGroupElement, SGroupElement) } + forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x.toSigmaProp, SSigmaProp) } forAll { x: ErgoBox => roundtrip[SBox.type](x, SBox) } forAll { x: AvlTree => roundtrip[SAvlTree.type](x, SAvlTree) } forAll { x: Array[Byte] => roundtrip[SByteArray](x.toColl, SByteArray) } diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala index eac2913cc6..c4e6bfd9e8 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/GroupElementSerializerSpecification.scala @@ -3,7 +3,7 @@ package sigmastate.serialization import sigmastate.crypto.CryptoConstants import sigma.crypto.CryptoFacade import sigma.serialization.GroupElementSerializer -import sigmastate.eval._ +import sigma.util.Extensions.{EcpOps, GroupElementOps} class GroupElementSerializerSpecification extends SerializationSpecification { @@ -14,18 +14,18 @@ class GroupElementSerializerSpecification extends SerializationSpecification { val bytes = GroupElementSerializer.toBytes(identity) bytes.length shouldBe CryptoConstants.EncodedGroupElementLength bytes.forall(_ == 0) shouldBe true - GroupElementSerializer.parse(SigmaSerializer.startReader(bytes, 0)).isIdentity shouldBe true + GroupElementSerializer.parse(SigmaSerializer.startReader(bytes, 0)).toGroupElement.isIdentity shouldBe true } property("point roundtrip") { forAll(groupElementConstGen){ge => - val bytes = GroupElementSerializer.toBytes(ge.value) + val bytes = GroupElementSerializer.toBytes(ge.value.toECPoint) bytes.length shouldBe CryptoConstants.EncodedGroupElementLength val restored = GroupElementSerializer.parse(SigmaSerializer.startReader(bytes, 0)) CryptoFacade.getAffineXCoord(CryptoFacade.normalizePoint(restored)) shouldBe - CryptoFacade.getAffineXCoord(CryptoFacade.normalizePoint(ge.value)) + CryptoFacade.getAffineXCoord(CryptoFacade.normalizePoint(ge.value.toECPoint)) CryptoFacade.getAffineYCoord(CryptoFacade.normalizePoint(restored)) shouldBe - CryptoFacade.getAffineYCoord(CryptoFacade.normalizePoint(ge.value)) + CryptoFacade.getAffineYCoord(CryptoFacade.normalizePoint(ge.value.toECPoint)) } } } diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/OrSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/OrSerializerSpecification.scala index 38d13054e9..d2853f1985 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/OrSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/OrSerializerSpecification.scala @@ -2,9 +2,9 @@ package sigmastate.serialization import sigmastate.Values.{BooleanConstant, Constant, IntConstant} import sigmastate._ -import sigmastate.eval.Extensions._ import sigmastate.serialization.OpCodes._ import scorex.util.encode.ZigZagEncoder.encodeZigZagInt +import sigma.Extensions.ArrayOps import sigma.ast.{SBoolean, SCollection, SCollectionType, SInt} class OrSerializerSpecification extends TableSerializationSpecification { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala index 93c5c7a715..1023f641ee 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/PDHTSerializerSpecification.scala @@ -7,7 +7,7 @@ class PDHTSerializerSpecification extends SerializationSpecification { property("ProveDiffieHellmanTupleSerializer: Serializer round trip") { forAll { i: ProveDHTuple => - roundTripTest(i.toSigmaProp) + roundTripTest(i.toSigmaPropValue) } // In IntelliJ IDEA this test is executed last, at this point all statistics has been collected diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala index a9472adcf0..06c4ebd865 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/ProveDlogSerializerSpec.scala @@ -7,7 +7,7 @@ class ProveDlogSerializerSpec extends SerializationSpecification { property("ProveDlog: Serializer round trip") { forAll { pd: ProveDlog => - roundTripTest(pd.toSigmaProp) + roundTripTest(pd.toSigmaPropValue) } } diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index 84c4d12c91..c5f2837c99 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -4,13 +4,14 @@ import java.math.BigInteger import org.ergoplatform.settings.ErgoAlgos 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 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.{ArrayOps, SigmaBooleanOps} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTransactionTesting, TestingCommons} import sigmastate.interpreter.Interpreter import sigmastate.serialization.generators.ObjectGenerators @@ -72,7 +73,7 @@ class SigSerializerSpecification extends TestingCommons property("SigSerializer round trip") { forAll(configParams = MinSuccessful(100)) { sb: SigmaBoolean => - val expr = sb.toSigmaProp + val expr = sb.toSigmaPropValue val challenge = Array.fill(32)(Random.nextInt(100).toByte) val ctx = ErgoLikeContextTesting( diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index fd962c3ee5..3b58f4c3dc 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -22,9 +22,11 @@ import sigmastate.lang.TransformingSigmaBuilder._ import sigmastate._ import sigmastate.utxo._ import sigma.Coll +import sigma.Extensions.ArrayOps import sigma._ import sigma.ast._ import sigma.crypto.EcPointType +import sigma.util.Extensions.EcpOps import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus} import sigma.validation.ValidationRules.FirstRuleId @@ -158,7 +160,7 @@ trait ObjectGenerators extends TypeGenerators lazy val groupElementConstGen: Gen[GroupElementConstant] = for { p <- groupElementGen - } yield mkConstant[SGroupElement.type](p, SGroupElement) + } yield mkConstant[SGroupElement.type](p.toGroupElement, SGroupElement) lazy val constantGen: Gen[Constant[SType]] = Gen.oneOf(booleanConstGen, byteConstGen, @@ -681,7 +683,7 @@ trait ObjectGenerators extends TypeGenerators lazy val ergoTreeGen: Gen[ErgoTree] = for { sigmaBoolean <- Gen.delay(sigmaBooleanGen) propWithConstants <- Gen.delay(logicalExprTreeNodeGen(Seq(AND.apply, OR.apply, XorOf.apply)).map(_.toSigmaProp)) - prop <- Gen.oneOf(propWithConstants, sigmaBoolean.toSigmaProp) + prop <- Gen.oneOf(propWithConstants, sigmaBoolean.toSigmaPropValue) treeBuilder <- Gen.oneOf(Seq[SigmaPropValue => ErgoTree](ErgoTree.withSegregation, ErgoTree.withoutSegregation)) } yield treeBuilder(prop) @@ -689,7 +691,7 @@ trait ObjectGenerators extends TypeGenerators lazy val ergoTreeWithSegregationGen: Gen[ErgoTree] = for { sigmaBoolean <- Gen.delay(sigmaBooleanGen) propWithConstants <- Gen.delay(logicalExprTreeNodeGen(Seq(AND.apply, OR.apply, XorOf.apply)).map(_.toSigmaProp)) - prop <- Gen.oneOf(propWithConstants, sigmaBoolean.toSigmaProp) + prop <- Gen.oneOf(propWithConstants, sigmaBoolean.toSigmaPropValue) } yield ErgoTree.withSegregation(prop) def headerGen(stateRoot: AvlTree, parentId: Coll[Byte]): Gen[Header] = for { @@ -707,7 +709,7 @@ trait ObjectGenerators extends TypeGenerators powDistance <- arbBigInt.arbitrary votes <- minerVotesGen } yield CHeader(id, version, parentId, adProofsRoot, stateRoot, transactionRoot, timestamp, nBits, - height, extensionRoot, minerPk, powOnetimePk, powNonce, powDistance, votes) + height, extensionRoot, minerPk.toGroupElement, powOnetimePk.toGroupElement, powNonce, powDistance, votes) lazy val headerGen: Gen[Header] = for { stateRoot <- avlTreeGen @@ -737,7 +739,7 @@ trait ObjectGenerators extends TypeGenerators height <- heightGen minerPk <- groupElementGen votes <- minerVotesGen - } yield CPreHeader(version, parentId, timestamp, nBits, height, minerPk, votes) + } yield CPreHeader(version, parentId, timestamp, nBits, height, minerPk.toGroupElement, votes) lazy val preHeaderGen: Gen[PreHeader] = for { parentId <- modifierIdBytesGen diff --git a/interpreter/shared/src/test/scala/sigmastate/utils/HelpersTests.scala b/interpreter/shared/src/test/scala/sigmastate/utils/HelpersTests.scala index 3325035bbd..83a13e2e5a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/utils/HelpersTests.scala +++ b/interpreter/shared/src/test/scala/sigmastate/utils/HelpersTests.scala @@ -5,7 +5,7 @@ import Helpers._ import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps class HelpersTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers with ObjectGenerators { property("xorU") { diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 6cc938c810..793104bb42 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -1,5 +1,6 @@ package sigma +import sigma.Extensions.ArrayOps import sigma.data.{AvlTreeData, RType} import sigmastate.Values.ErgoTree import sigmastate.Values diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 36b224fae9..4d2986126a 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -12,14 +12,13 @@ import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigmastate.eval._ -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, CostingBox, CostingSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators import sigmastate.utils.Helpers import sigmastate._ -import sigma.Coll import sigma.ast.{SBoolean, SSigmaProp} import sigma.crypto.EcPointType diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index 5c7d3f8d96..7f5094b0c9 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -15,6 +15,7 @@ import sigmastate.helpers.NegativeTesting import sigma.Coll import sigma.ast._ import sigma.data.{ProveDHTuple, ProveDlog, SigmaBoolean} +import sigma.util.Extensions.BigIntegerOps trait LangTests extends Matchers with NegativeTesting { @@ -43,8 +44,8 @@ trait LangTests extends Matchers with NegativeTesting { val g3 = CostingSigmaDslBuilder.GroupElement(ecp3) val g4 = CostingSigmaDslBuilder.GroupElement(ecp4) - protected val n1: BigInt = BigInt(10).underlying() - protected val n2: BigInt = BigInt(20).underlying() + protected val n1: BigInt = BigInt(10).underlying().toBigInt + protected val n2: BigInt = BigInt(20).underlying().toBigInt protected val bigIntegerArr1: Coll[BigInt] = Colls.fromItems(n1, n2) protected val big: BigInteger = BigInt(Long.MaxValue).underlying().pow(2) protected val p1: SigmaBoolean = ProveDlog(ecp1) diff --git a/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala b/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala index 8e65b602dc..bae67b3248 100644 --- a/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala +++ b/sc/jvm/src/test/scala/sigmastate/ErgoTreeBenchmarks.scala @@ -59,7 +59,7 @@ object ErgoTreeBenchmarks extends Bench.LocalTime with BenchmarkGens { suite: Be measure method "isCorrectType" in { using(sizes) in { size => cfor(0)(_ < size, _ + 1) { i => - Platform.isCorrectType(i, SType.allPredefTypes(i % 10)) + sigma.crypto.Platform.isCorrectType(i, SType.allPredefTypes(i % 10)) } } } diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index cd50653365..ef800a58e0 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -2,18 +2,18 @@ package sigmastate.helpers import org.ergoplatform.settings.ErgoAlgos import org.ergoplatform.{ErgoBox, Outputs} -import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, RType} import scorex.util.ModifierId +import sigma.Extensions.ArrayOps +import sigma.SigmaDslTesting +import sigma.ast._ +import sigma.data.{AvlTreeData, AvlTreeFlags, CollType} import sigmastate.Values._ import sigmastate._ -import sigmastate.eval.Extensions.ArrayOps import sigmastate.eval._ import sigmastate.lang.Terms.MethodCall import sigmastate.serialization.OpCodes import sigmastate.utils.Helpers import sigmastate.utxo.SelectField -import sigma.SigmaDslTesting -import sigma.ast._ import java.math.BigInteger import scala.collection.mutable.ArrayBuffer diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index e727907bf5..e077975018 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -12,6 +12,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scalan.Platform.threadSleepOrNoOp +import sigma.Extensions.ArrayOps import sigma.data.{CollType, OptionType, PairType, ProveDlog, RType, SigmaLeaf} import sigma.util.BenchmarkUtil import sigma.util.CollectionUtil._ @@ -21,7 +22,7 @@ import sigma.ast.SType.AnyOps import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, IntConstant, SValue} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput -import sigmastate.eval.Extensions.{ArrayOps, SigmaBooleanOps} +import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval.{CompiletimeIRContext, CostingBox, CostingDataContext, IRContext, SigmaDsl} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, SigmaPPrint} @@ -315,7 +316,7 @@ class SigmaDslTesting extends AnyPropSpec """.stripMargin val IR = new CompiletimeIRContext - val pkAlice = prover.pubKeys.head.toSigmaProp + val pkAlice = prover.pubKeys.head.toSigmaPropValue val env = Map("pkAlice" -> pkAlice) // Compile script the same way it is performed by applications (i.e. via Ergo Appkit) val prop = compile(env, code)(IR).asSigmaProp @@ -332,8 +333,8 @@ class SigmaDslTesting extends AnyPropSpec } def ergoCtx(prover: FeatureProvingInterpreter, compiledTree: ErgoTree, expectedValue: B) = { - val pkBobBytes = ValueSerializer.serialize(prover.pubKeys(1).toSigmaProp) - val pkCarolBytes = ValueSerializer.serialize(prover.pubKeys(2).toSigmaProp) + val pkBobBytes = ValueSerializer.serialize(prover.pubKeys(1).toSigmaPropValue) + val pkCarolBytes = ValueSerializer.serialize(prover.pubKeys(2).toSigmaPropValue) val newRegisters = Map( ErgoBox.R4 -> Constant[SType](expectedValue.asInstanceOf[SType#WrappedType], tpeB), ErgoBox.R5 -> ByteArrayConstant(pkBobBytes) diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index 4291096f0e..6b895f1a71 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -15,7 +15,8 @@ import sigma.validation.ValidationRules.CheckPositionLimit import sigma.{Colls, Environment} import sigmastate.Values.{BlockValue, GetVarInt, IntConstant, SValue, SigmaPropValue, Tuple, ValDef, ValUse} import sigmastate._ -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps +import sigmastate.eval.Extensions.{EvalIterableOps, SigmaBooleanOps} import sigmastate.eval._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.{ContextExtension, CostedProverResult} @@ -121,7 +122,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("exceeding ergo box propositionBytes max size check") { val oversizedTree = mkTestErgoTree(SigmaAnd( Gen.listOfN(SigmaSerializer.MaxPropositionSize / sigma.crypto.groupSize, - proveDlogGen.map(_.toSigmaProp)).sample.get)) + proveDlogGen.map(_.toSigmaPropValue)).sample.get)) val b = new ErgoBoxCandidate(1L, oversizedTree, 1) val w = SigmaSerializer.startWriter() ErgoBoxCandidate.serializer.serialize(b, w) @@ -160,7 +161,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("ergo box propositionBytes max size check") { val bigTree = mkTestErgoTree(SigmaAnd( Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / sigma.crypto.groupSize, - proveDlogGen.map(_.toSigmaProp)).sample.get)) + proveDlogGen.map(_.toSigmaPropValue)).sample.get)) val b = new ErgoBoxCandidate(1L, bigTree, 1) val w = SigmaSerializer.startWriter() ErgoBoxCandidate.serializer.serialize(b, w) @@ -271,7 +272,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("exceed ergo box max size check") { val bigTree = mkTestErgoTree(SigmaAnd( Gen.listOfN((SigmaSerializer.MaxPropositionSize / 2) / sigma.crypto.groupSize, - proveDlogGen.map(_.toSigmaProp)).sample.get)) + proveDlogGen.map(_.toSigmaPropValue)).sample.get)) val tokens = additionalTokensGen(127).sample.get.map(_.sample.get).toColl val b = new ErgoBoxCandidate(1L, bigTree, 1, tokens) val w = SigmaSerializer.startWriter() diff --git a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala index 9517ac9821..b13e1e48f2 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala @@ -217,7 +217,7 @@ class ErgoTreeSerializerSpecification extends SerializationSpecification property("SigmaProp.propBytes vs ErgoTree.serializer equivalence") { forAll(MinSuccessful(100)) { sp: SigmaProp => val propBytes = sp.propBytes - val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(sp.toSigmaBoolean.toSigmaProp), 0, null, None) + val ergoTree = new ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, Right(sp.toSigmaBoolean.toSigmaPropValue), 0, null, None) val treeBytes = DefaultSerializer.serializeErgoTree(ergoTree) treeBytes shouldBe propBytes.toArray } diff --git a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index 4d91fd63b1..6931a2920b 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -11,7 +11,7 @@ import sigmastate.Values._ import sigmastate._ import sigmastate.eval.IRContext import sigmastate.eval._ -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp diff --git a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala index 68a48d4e95..3d35322a38 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala @@ -1,26 +1,27 @@ package sigmastate.utxo -import java.math.BigInteger import org.ergoplatform.ErgoBox.{AdditionalRegisters, R6, R8} import org.ergoplatform._ -import sigma.data.{AvlTreeData, RType} +import sigma.Extensions.ArrayOps import sigma.ast.SCollection.SByteArray -import sigmastate.Values._ -import sigmastate._ -import sigmastate.eval.Extensions._ -import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} -import sigmastate.helpers.TestingHelpers._ -import sigmastate.interpreter.Interpreter._ -import sigmastate.lang.Terms._ import sigma.ast.SType.AnyOps import sigma.ast._ +import sigma.data.AvlTreeData +import sigma.util.StringUtil._ +import sigmastate.Values._ +import sigmastate._ import sigmastate.crypto.CryptoConstants import sigmastate.eval.{CAnyValue, InvalidType} +import sigmastate.helpers.TestingHelpers._ +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.ContextExtension.VarBinding import sigmastate.interpreter.ErgoTreeEvaluator.DefaultEvalSettings import sigmastate.interpreter.EvalSettings +import sigmastate.interpreter.Interpreter._ +import sigmastate.lang.Terms._ import sigmastate.utils.Helpers._ -import sigma.util.StringUtil._ + +import java.math.BigInteger class BasicOpsSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala index f9f7bb5417..484518a8bb 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/CollectionOperationsSpecification.scala @@ -91,7 +91,7 @@ class CollectionOperationsSpecification extends CompilerTestingCommons val prover = new ContextEnrichingTestProvingInterpreter val verifier = new ErgoLikeTestInterpreter - val pubkey = prover.dlogSecrets.head.publicImage.toSigmaProp + val pubkey = prover.dlogSecrets.head.publicImage.toSigmaPropValue val pubkeyTree = mkTestErgoTree(pubkey) val prop = compile(Map(), "OUTPUTS.exists({ (box: Box) => box.value + 5 > 10 })").asBoolValue.toSigmaProp @@ -206,7 +206,7 @@ class CollectionOperationsSpecification extends CompilerTestingCommons val prover = new ContextEnrichingTestProvingInterpreter val verifier = new ErgoLikeTestInterpreter - val pubkey = prover.dlogSecrets.head.publicImage.toSigmaProp + val pubkey = prover.dlogSecrets.head.publicImage.toSigmaPropValue val pubkeyTree = mkTestErgoTree(pubkey) val prop = compile(Map(), diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala index 363b48f13f..8ce3ce6e1d 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ErgoLikeInterpreterSpecification.scala @@ -412,7 +412,7 @@ class ErgoLikeInterpreterSpecification extends CompilerTestingCommons val prover0 = new ContextEnrichingTestProvingInterpreter() - val customScript = prover0.dlogSecrets.head.publicImage.toSigmaProp + val customScript = prover0.dlogSecrets.head.publicImage.toSigmaPropValue val scriptBytes = ValueSerializer.serialize(customScript) val scriptHash = Blake2b256(scriptBytes).take(bytesCount) diff --git a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala index 9bc564f13e..c239a38d30 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/ThresholdSpecification.scala @@ -91,7 +91,7 @@ class ThresholdSpecification extends CompilerTestingCommons proverD.prove(compiledTree3, ctx, fakeMessage).isFailure shouldBe true { - val prop3Or = COR(Seq(pubkeyA, pubkeyB, pubkeyC)).toSigmaProp + val prop3Or = COR(Seq(pubkeyA, pubkeyB, pubkeyC)).toSigmaPropValue val res1 = testReduce(proverA)(ctx, compiledProp3) val res2 = testReduce(proverA)(ctx, prop3Or) res1 shouldBe res2 diff --git a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala index 5c4a0828f9..e3d43ab868 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/UsingContextPropertiesSpecification.scala @@ -2,7 +2,7 @@ package sigmastate.utxo import sigmastate.CompilerCrossVersionProps import sigmastate.eval.IRContext -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps import sigma.Context import sigmastate.helpers.CompilerTestingCommons import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, TestContractSpec} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala index 5c744dd77e..99f355eb27 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala @@ -47,7 +47,7 @@ trait BlockchainSimulationTestingCommons extends CompilerTestingCommons { propOpt: Option[ErgoTree] = None, extension: ContextExtension = ContextExtension.empty): FullBlock = { val prop: ErgoTree = propOpt.getOrElse( - mkTestErgoTree(prover.dlogSecrets.head.publicImage.toSigmaProp)) + mkTestErgoTree(prover.dlogSecrets.head.publicImage.toSigmaPropValue)) val minerPubkey = prover.dlogSecrets.head.publicImage.pkBytes val boxesToSpend = state.boxesReader.randomBoxes(30 + height) diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala index 8f885c0afe..6f36d29b74 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/FsmExampleSpecification.scala @@ -51,10 +51,10 @@ class FsmExampleSpecification extends CompilerTestingCommons val prover = new ContextEnrichingTestProvingInterpreter - val script1 = prover.dlogSecrets.head.publicImage.toSigmaProp - val script2 = prover.dhSecrets.head.publicImage.toSigmaProp + val script1 = prover.dlogSecrets.head.publicImage.toSigmaPropValue + val script2 = prover.dhSecrets.head.publicImage.toSigmaPropValue val script3 = SigmaAnd(script1, script2) - val script4 = prover.dlogSecrets.tail.head.publicImage.toSigmaProp //a script to leave FSM + val script4 = prover.dlogSecrets.tail.head.publicImage.toSigmaPropValue //a script to leave FSM val script1Hash = hash.Blake2b256(ValueSerializer.serialize(script1)) val script2Hash = hash.Blake2b256(ValueSerializer.serialize(script2)) diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala index ab1c963a54..26f00d6fe5 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala @@ -14,7 +14,7 @@ import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.CryptoConstants -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestProvingInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala index 4d18c98ef4..c90ebe1523 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala @@ -9,7 +9,7 @@ import sigma.Colls import sigma.data.{AvlTreeData, AvlTreeFlags, TrivialProp} import sigmastate.CompilerCrossVersionProps import sigmastate.Values.{AvlTreeConstant, ByteArrayConstant, LongConstant, SigmaPropConstant} -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.eval.{Digest32Coll, IRContext, SigmaDsl} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestProvingInterpreter} import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala index ae34e2b151..e6e4b49ff0 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala @@ -9,7 +9,7 @@ import sigma.ast.SCollection.SByteArray import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values._ import sigmastate._ -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala index d161016615..3eeb2264da 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala @@ -17,7 +17,7 @@ import org.ergoplatform._ import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, StdContracts, TestContractSpec} import sigmastate.crypto.CryptoConstants import sigmastate.crypto.BigIntegers -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo._ import sigma.Context diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 32e57fb0d5..37ce5f1ae6 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -11,7 +11,7 @@ import scorex.crypto.authds.ADKey import scorex.util.ModifierId import scorex.util.encode.Base16 import sigmastate.Values.{Constant, GroupElementConstant} -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, Digest32Coll} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 83d8245878..174dcbdf3d 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -29,6 +29,7 @@ import org.ergoplatform.UnsignedErgoLikeTransaction import org.ergoplatform.ErgoLikeTransactionTemplate import org.ergoplatform.ErgoBoxCandidate import org.ergoplatform.ErgoLikeContext +import sigma.Extensions.ArrayOps import sigma.ast.SType import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, WrapperOf} import sigma.validation.SigmaValidationSettings diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala index dab4a73efd..cffc96267a 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala @@ -7,7 +7,7 @@ import org.ergoplatform.sdk.BoxSelection.InputBoxesValidator import org.ergoplatform.sdk.Extensions.HeaderOps import org.ergoplatform.sdk.wallet.{AssetUtils, TokensMap} import scorex.util.{ModifierId, bytesToId} -import sigmastate.eval.Extensions.ArrayOps +import sigma.Extensions.ArrayOps import sigmastate.utils.Extensions.ModifierIdOps import sigma.Coll import sigma.Extensions.CollBytesOps diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index 87fedce55a..f3afb17553 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -10,7 +10,8 @@ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps import sigma.crypto.EcPointType import sigma.serialization.SerializerException -import sigmastate.eval.Extensions._ +import sigma.util.Extensions.{BigIntegerOps, EcpOps, SigmaBooleanOps} +import sigma.Extensions.ArrayOps import sigmastate.eval._ import sigma.{AvlTree, Box, Colls, Evaluation} import sigmastate.serialization.SerializationSpecification @@ -91,9 +92,9 @@ class DataJsonEncoderSpecification extends SerializationSpecification { forAll { x: Boolean => roundtrip[SBoolean.type](x, SBoolean) } forAll { x: Long => roundtrip[SLong.type](x, SLong) } forAll { x: String => roundtrip[SString.type](x, SString) } - forAll { x: BigInteger => roundtrip[SBigInt.type](x, SBigInt) } - forAll { x: EcPointType => roundtrip[SGroupElement.type](x, SGroupElement) } - forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x, SSigmaProp) } + forAll { x: BigInteger => roundtrip[SBigInt.type](x.toBigInt, SBigInt) } + forAll { x: EcPointType => roundtrip[SGroupElement.type](x.toGroupElement, SGroupElement) } + forAll { x: SigmaBoolean => roundtrip[SSigmaProp.type](x.toSigmaProp, SSigmaProp) } forAll { x: AvlTree => roundtrip[SAvlTree.type](x, SAvlTree) } forAll { x: Array[Byte] => roundtrip[SByteArray](x.toColl, SByteArray) } forAll { x: Box => roundtrip[SBox.type](x, SBox) } diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index 0a5d101b70..9e10535927 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -18,6 +18,7 @@ import sigmastate.crypto.CryptoConstants import sigmastate.eval.Digest32Coll import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SerializationSpecification +import sigmastate.utils.Helpers.DecoderResultOps // required for Scala 2.11 class JsonSerializationSpec extends SerializationSpecification with JsonCodecs { diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index 1469551299..99d5d4526a 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -12,10 +12,11 @@ import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, Evalu import sigma.ast.{SByte, SType} import scorex.util._ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgoLikeTransaction, UnsignedInput} -import sigmastate.eval.Extensions._ +import sigma.Extensions.ArrayOps import scorex.util.{ModifierId, bytesToId} import sigma.crypto.CryptoFacade import sigma.data.ProveDlog +import sigmastate.eval.Extensions.{EvalIterableOps, SigmaBooleanOps} import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ @@ -147,7 +148,7 @@ trait Generators { def unsignedTxGen(secret: SecretKey): Gen[(IndexedSeq[ErgoBox], UnsignedErgoLikeTransaction)] = { - val dlog: Gen[ErgoTree] = Gen.const(secret.privateInput.publicImage.asInstanceOf[ProveDlog].toSigmaProp) + val dlog: Gen[ErgoTree] = Gen.const(secret.privateInput.publicImage.asInstanceOf[ProveDlog].toSigmaPropValue) for { ins <- Gen.listOfN(2, ergoBoxGen(dlog)) From d9b6b231d405667adc3d34847557ff55c0f38618 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 13 Sep 2023 11:52:47 +0200 Subject: [PATCH 20/34] core-serializers: CAvlTree moved to core --- .../src/main/scala/sigma/SigmaDsl.scala | 126 +++++++++--------- .../src/main/scala/sigma/data/CAvlTree.scala | 36 +++++ .../sigma/reflection/ReflectionData.scala | 46 +++---- .../org/ergoplatform/ErgoLikeContext.scala | 2 +- .../org/ergoplatform/dsl/AvlTreeHelpers.scala | 6 +- .../sigmastate/eval/CostingDataContext.scala | 118 ---------------- .../scala/sigmastate/eval/Extensions.scala | 106 ++++++++++++++- .../sigmastate/lang/SigmaBuilderTest.scala | 4 +- .../serialization/AvlTreeSpecification.scala | 1 + .../special/sigma/SigmaTestingData.scala | 4 +- .../scala/sigma/SigmaDslSpecification.scala | 2 +- .../utxo/AVLTreeScriptsSpecification.scala | 1 + .../scala/org/ergoplatform/sdk/js/Isos.scala | 4 +- .../scala/org/ergoplatform/sdk/js/Value.scala | 4 +- .../org/ergoplatform/sdk/utils/Zero.scala | 2 +- 15 files changed, 241 insertions(+), 221 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/data/CAvlTree.scala diff --git a/core/shared/src/main/scala/sigma/SigmaDsl.scala b/core/shared/src/main/scala/sigma/SigmaDsl.scala index 818ebafb22..8785a32fa8 100644 --- a/core/shared/src/main/scala/sigma/SigmaDsl.scala +++ b/core/shared/src/main/scala/sigma/SigmaDsl.scala @@ -384,70 +384,70 @@ trait AvlTree { */ def updateOperations(newOperations: Byte): AvlTree - /** Checks if an entry with key `key` exists in this tree using proof `proof`. - * Throws exception if proof is incorrect - * - * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. - * Return `true` if a leaf with the key `key` exists - * Return `false` if leaf with provided key does not exist. - * @param key a key of an element of this authenticated dictionary. - * @param proof data to reconstruct part of the tree enough to perform the check - */ - def contains(key: Coll[Byte], proof: Coll[Byte]): Boolean - - /** Perform a lookup of key `key` in this tree using proof `proof`. - * Throws exception if proof is incorrect - * - * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. - * Return Some(bytes) of leaf with key `key` if it exists - * Return None if leaf with provided key does not exist. - * @param key a key of an element of this authenticated dictionary. - * @param proof data to reconstruct part of the tree enough to get the value - * by the key - */ - def get(key: Coll[Byte], proof: Coll[Byte]): Option[Coll[Byte]] - - /** Perform a lookup of many keys `keys` in this tree using proof `proof`. - * - * @note CAUTION! Keys must be ordered the same way they were in lookup before proof was generated. - * For each key return Some(bytes) of leaf if it exists and None if is doesn't. - * @param keys keys of elements of this authenticated dictionary. - * @param proof - */ - def getMany(keys: Coll[Coll[Byte]], proof: Coll[Byte]): Coll[Option[Coll[Byte]]] - - /** Perform insertions of key-value entries into this tree using proof `proof`. - * Throws exception if proof is incorrect - * - * @note CAUTION! Pairs must be ordered the same way they were in insert ops before proof was generated. - * Return Some(newTree) if successful - * Return None if operations were not performed. - * @param operations collection of key-value pairs to insert in this authenticated dictionary. - * @param proof data to reconstruct part of the tree - */ - def insert(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] +// /** Checks if an entry with key `key` exists in this tree using proof `proof`. +// * Throws exception if proof is incorrect +// * +// * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. +// * Return `true` if a leaf with the key `key` exists +// * Return `false` if leaf with provided key does not exist. +// * @param key a key of an element of this authenticated dictionary. +// * @param proof data to reconstruct part of the tree enough to perform the check +// */ +// def contains(key: Coll[Byte], proof: Coll[Byte]): Boolean +// +// /** Perform a lookup of key `key` in this tree using proof `proof`. +// * Throws exception if proof is incorrect +// * +// * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. +// * Return Some(bytes) of leaf with key `key` if it exists +// * Return None if leaf with provided key does not exist. +// * @param key a key of an element of this authenticated dictionary. +// * @param proof data to reconstruct part of the tree enough to get the value +// * by the key +// */ +// def get(key: Coll[Byte], proof: Coll[Byte]): Option[Coll[Byte]] +// +// /** Perform a lookup of many keys `keys` in this tree using proof `proof`. +// * +// * @note CAUTION! Keys must be ordered the same way they were in lookup before proof was generated. +// * For each key return Some(bytes) of leaf if it exists and None if is doesn't. +// * @param keys keys of elements of this authenticated dictionary. +// * @param proof +// */ +// def getMany(keys: Coll[Coll[Byte]], proof: Coll[Byte]): Coll[Option[Coll[Byte]]] +// +// /** Perform insertions of key-value entries into this tree using proof `proof`. +// * Throws exception if proof is incorrect +// * +// * @note CAUTION! Pairs must be ordered the same way they were in insert ops before proof was generated. +// * Return Some(newTree) if successful +// * Return None if operations were not performed. +// * @param operations collection of key-value pairs to insert in this authenticated dictionary. +// * @param proof data to reconstruct part of the tree +// */ +// def insert(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] +// +// /** Perform updates of key-value entries into this tree using proof `proof`. +// * Throws exception if proof is incorrect +// * +// * @note CAUTION! Pairs must be ordered the same way they were in update ops before proof was generated. +// * Return Some(newTree) if successful +// * Return None if operations were not performed. +// * @param operations collection of key-value pairs to update in this authenticated dictionary. +// * @param proof data to reconstruct part of the tree +// */ +// def update(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] - /** Perform updates of key-value entries into this tree using proof `proof`. - * Throws exception if proof is incorrect - * - * @note CAUTION! Pairs must be ordered the same way they were in update ops before proof was generated. - * Return Some(newTree) if successful - * Return None if operations were not performed. - * @param operations collection of key-value pairs to update in this authenticated dictionary. - * @param proof data to reconstruct part of the tree - */ - def update(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] - - /** Perform removal of entries into this tree using proof `proof`. - * Throws exception if proof is incorrect - * Return Some(newTree) if successful - * Return None if operations were not performed. - * - * @note CAUTION! Keys must be ordered the same way they were in remove ops before proof was generated. - * @param operations collection of keys to remove from this authenticated dictionary. - * @param proof data to reconstruct part of the tree - */ - def remove(operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] +// /** Perform removal of entries into this tree using proof `proof`. +// * Throws exception if proof is incorrect +// * Return Some(newTree) if successful +// * Return None if operations were not performed. +// * +// * @note CAUTION! Keys must be ordered the same way they were in remove ops before proof was generated. +// * @param operations collection of keys to remove from this authenticated dictionary. +// * @param proof data to reconstruct part of the tree +// */ +// def remove(operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] // /** Creates a new instance of [[AvlTreeVerifier]] with the given `proof` and using // * properties of this AvlTree (digest, keyLength, valueLengthOpt) for constructor diff --git a/core/shared/src/main/scala/sigma/data/CAvlTree.scala b/core/shared/src/main/scala/sigma/data/CAvlTree.scala new file mode 100644 index 0000000000..a37e5096bb --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/CAvlTree.scala @@ -0,0 +1,36 @@ +package sigma.data + +import sigma.{AvlTree, Coll} + +/** A default implementation of [[AvlTree]] interface. + * + * @see [[AvlTree]] for detailed descriptions + */ +case class CAvlTree(treeData: AvlTreeData) extends AvlTree with WrapperOf[AvlTreeData] { + override def wrappedValue: AvlTreeData = treeData + + override def keyLength: Int = treeData.keyLength + + override def enabledOperations = treeData.treeFlags.serializeToByte + + override def isInsertAllowed: Boolean = treeData.treeFlags.insertAllowed + + override def isUpdateAllowed: Boolean = treeData.treeFlags.updateAllowed + + override def isRemoveAllowed: Boolean = treeData.treeFlags.removeAllowed + + override def updateOperations(newOperations: Byte): AvlTree = { + val td = treeData.copy(treeFlags = AvlTreeFlags(newOperations)) + this.copy(treeData = td) + } + + override def valueLengthOpt: Option[Int] = treeData.valueLengthOpt + + override def digest: Coll[Byte] = treeData.digest + + override def updateDigest(newDigest: Coll[Byte]): AvlTree = { + val td = treeData.copy(digest = newDigest) + this.copy(treeData = td) + } +} + diff --git a/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala b/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala index 01949b89ff..1b8afab03e 100644 --- a/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala +++ b/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala @@ -175,14 +175,14 @@ object ReflectionData { mkMethod(clazz, "updateOperations", Array[Class[_]](classOf[Byte])) { (obj, args) => obj.asInstanceOf[AvlTree].updateOperations(args(0).asInstanceOf[Byte]) }, - mkMethod(clazz, "getMany", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].getMany(args(0).asInstanceOf[Coll[Coll[Byte]]], - args(1).asInstanceOf[Coll[Byte]]) - }, - mkMethod(clazz, "update", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].update(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], - args(1).asInstanceOf[Coll[Byte]]) - }, +// mkMethod(clazz, "getMany", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].getMany(args(0).asInstanceOf[Coll[Coll[Byte]]], +// args(1).asInstanceOf[Coll[Byte]]) +// }, +// mkMethod(clazz, "update", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].update(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], +// args(1).asInstanceOf[Coll[Byte]]) +// }, mkMethod(clazz, "keyLength", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].keyLength }, @@ -195,27 +195,27 @@ object ReflectionData { mkMethod(clazz, "digest", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].digest }, - mkMethod(clazz, "insert", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].insert(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], - args(1).asInstanceOf[Coll[Byte]]) - }, +// mkMethod(clazz, "insert", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].insert(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], +// args(1).asInstanceOf[Coll[Byte]]) +// }, mkMethod(clazz, "isRemoveAllowed", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].isRemoveAllowed }, mkMethod(clazz, "valueLengthOpt", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].valueLengthOpt }, - mkMethod(clazz, "get", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].get(args(0).asInstanceOf[Coll[Byte]], - args(1).asInstanceOf[Coll[Byte]]) - }, - mkMethod(clazz, "remove", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].remove(args(0).asInstanceOf[Coll[Coll[Byte]]], args(1).asInstanceOf[Coll[Byte]]) - }, - mkMethod(clazz, "contains", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => - obj.asInstanceOf[AvlTree].contains(args(0).asInstanceOf[Coll[Byte]], - args(1).asInstanceOf[Coll[Byte]]) - }, +// mkMethod(clazz, "get", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].get(args(0).asInstanceOf[Coll[Byte]], +// args(1).asInstanceOf[Coll[Byte]]) +// }, +// mkMethod(clazz, "remove", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].remove(args(0).asInstanceOf[Coll[Coll[Byte]]], args(1).asInstanceOf[Coll[Byte]]) +// }, +// mkMethod(clazz, "contains", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => +// obj.asInstanceOf[AvlTree].contains(args(0).asInstanceOf[Coll[Byte]], +// args(1).asInstanceOf[Coll[Byte]]) +// }, mkMethod(clazz, "isUpdateAllowed", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].isUpdateAllowed }, diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 9a5f105df3..d1d6090f04 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -14,7 +14,7 @@ import debox.cfor import sigma.Extensions.ArrayOps import sigma.ast.{SBox, SCollection, SContext, SFunc, SGlobal, SInt, SType, SUnit} import sigma.ast.SType.{AnyOps, TypeCode} -import sigma.data.{AvlTreeData, SigmaConstants} +import sigma.data.{AvlTreeData, CAvlTree, SigmaConstants} import sigma.validation.SigmaValidationSettings import sigmastate.serialization.ValueCodes.OpCode diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala index f12f087028..4896ad61e2 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala @@ -1,13 +1,13 @@ package org.ergoplatform.dsl import sigma.Coll -import sigmastate.eval.{CAvlTree, CostingSigmaDslBuilder} +import sigmastate.eval.CostingSigmaDslBuilder import scorex.crypto.authds.{ADKey, ADValue} -import scorex.crypto.hash.{Digest32, Blake2b256} +import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.AvlTree import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert} import CostingSigmaDslBuilder.Colls -import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree} object AvlTreeHelpers { diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 429eed5979..e1a34ec6d1 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -58,124 +58,6 @@ object AvlTreeVerifier { } } -/** A default implementation of [[AvlTree]] interface. - * @see [[AvlTree]] for detailed descriptions - */ -case class CAvlTree(treeData: AvlTreeData) extends AvlTree with WrapperOf[AvlTreeData] { - override def wrappedValue: AvlTreeData = treeData - - override def keyLength: Int = treeData.keyLength - - override def enabledOperations = treeData.treeFlags.serializeToByte - - override def isInsertAllowed: Boolean = treeData.treeFlags.insertAllowed - - override def isUpdateAllowed: Boolean = treeData.treeFlags.updateAllowed - - override def isRemoveAllowed: Boolean = treeData.treeFlags.removeAllowed - - override def updateOperations(newOperations: Byte): AvlTree = { - val td = treeData.copy(treeFlags = AvlTreeFlags(newOperations)) - this.copy(treeData = td) - } - - override def valueLengthOpt: Option[Int] = treeData.valueLengthOpt - - override def digest: Coll[Byte] = treeData.digest - - override def updateDigest(newDigest: Coll[Byte]): AvlTree = { - val td = treeData.copy(digest = newDigest) - this.copy(treeData = td) - } - - override def contains(key: Coll[Byte], proof: Coll[Byte]): Boolean = { - val keyBytes = key.toArray - val bv = AvlTreeVerifier(this, proof) - bv.performOneOperation(Lookup(ADKey @@ keyBytes)) match { - case Success(r) => r match { - case Some(_) => true - case _ => false - } - case Failure(_) => false - } - } - - override def get(key: Coll[Byte], proof: Coll[Byte]): Option[Coll[Byte]] = { - val keyBytes = key.toArray - val bv = AvlTreeVerifier(this, proof) - bv.performOneOperation(Lookup(ADKey @@ keyBytes)) match { - case Success(r) => r match { - case Some(v) => Some(Colls.fromArray(v)) - case _ => None - } - case Failure(_) => Interpreter.error(s"Tree proof is incorrect $treeData") - } - } - - override def getMany(keys: Coll[Coll[Byte]], proof: Coll[Byte]): Coll[Option[Coll[Byte]]] = { - val bv = AvlTreeVerifier(this, proof) - keys.map { key => - bv.performOneOperation(Lookup(ADKey @@ key.toArray)) match { - case Success(r) => r match { - case Some(v) => Some(Colls.fromArray(v)) - case _ => None - } - case Failure(_) => Interpreter.error(s"Tree proof is incorrect $treeData") - } - } - } - - override def insert(entries: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] = { - if (!isInsertAllowed) { - None - } else { - val bv = AvlTreeVerifier(this, proof) - entries.forall { case (key, value) => - val insertRes = bv.performOneOperation(Insert(ADKey @@ key.toArray, ADValue @@ value.toArray)) - if (insertRes.isFailure) { - Interpreter.error(s"Incorrect insert for $treeData (key: $key, value: $value, digest: $digest): ${insertRes.failed.get}}") - } - insertRes.isSuccess - } - bv.digest match { - case Some(d) => Some(updateDigest(Colls.fromArray(d))) - case _ => None - } - } - } - - override def update(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] = { - if (!isUpdateAllowed) { - None - } else { - val bv = AvlTreeVerifier(this, proof) - operations.forall { case (key, value) => - bv.performOneOperation(Update(ADKey @@ key.toArray, ADValue @@ value.toArray)).isSuccess - } - bv.digest match { - case Some(d) => Some(updateDigest(Colls.fromArray(d))) - case _ => None - } - } - } - - override def remove(operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] = { - if (!isRemoveAllowed) { - None - } else { - val bv = AvlTreeVerifier(this, proof) - cfor(0)(_ < operations.length, _ + 1) { i => - val key = operations(i).toArray - bv.performOneOperation(Remove(ADKey @@ key)) - } - bv.digest match { - case Some(v) => Some(updateDigest(Colls.fromArray(v))) - case _ => None - } - } - } -} - /** Default implementation of AnyValue interface. */ case class CAnyValue[A](value: A, tVal: RType[Any]) extends AnyValue { def tA: RType[A] = tVal.asInstanceOf[RType[A]] diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index c0dfbc3d39..0d40d40978 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -1,20 +1,24 @@ package sigmastate.eval +import debox.cfor import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.TokenId +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.ast.{SBoolean, SCollection, SCollectionType, SType} -import sigma.data.{Nullable, ProveDHTuple, ProveDlog, RType, SigmaBoolean} -import sigma.util.Extensions.EcpOps +import sigma.data.{Nullable, RType, SigmaBoolean} import sigma.{Coll, _} +import sigmastate.Platform import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} +import sigmastate.interpreter.Interpreter import sigmastate.lang.{CheckingSigmaBuilder, TransformingSigmaBuilder} import sigmastate.utils.Helpers import sigmastate.utxo.SigmaPropIsProven -import sigmastate.Platform import java.math.BigInteger +import scala.util.{Failure, Success} object Extensions { @@ -104,4 +108,100 @@ object Extensions { 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 = { + val keyBytes = key.toArray + val bv = AvlTreeVerifier(tree, proof) + bv.performOneOperation(Lookup(ADKey @@ keyBytes)) match { + case Success(r) => r match { + case Some(_) => true + case _ => false + } + case Failure(_) => false + } + } + + def get(key: Coll[Byte], proof: Coll[Byte]): Option[Coll[Byte]] = { + val keyBytes = key.toArray + val bv = AvlTreeVerifier(tree, proof) + bv.performOneOperation(Lookup(ADKey @@ keyBytes)) match { + case Success(r) => r match { + case Some(v) => Some(Colls.fromArray(v)) + case _ => None + } + case Failure(_) => Interpreter.error(s"Tree proof is incorrect $tree") + } + } + + def getMany( + keys: Coll[Coll[Byte]], + proof: Coll[Byte]): Coll[Option[Coll[Byte]]] = { + val bv = AvlTreeVerifier(tree, proof) + keys.map { key => + bv.performOneOperation(Lookup(ADKey @@ key.toArray)) match { + case Success(r) => r match { + case Some(v) => Some(Colls.fromArray(v)) + case _ => None + } + case Failure(_) => Interpreter.error(s"Tree proof is incorrect $tree") + } + } + } + + def insert( + entries: Coll[(Coll[Byte], Coll[Byte])], + proof: Coll[Byte]): Option[AvlTree] = { + if (!tree.isInsertAllowed) { + None + } else { + val bv = AvlTreeVerifier(tree, proof) + entries.forall { case (key, value) => + val insertRes = bv.performOneOperation(Insert(ADKey @@ key.toArray, ADValue @@ value.toArray)) + if (insertRes.isFailure) { + Interpreter.error(s"Incorrect insert for $tree (key: $key, value: $value, digest: ${tree.digest}): ${insertRes.failed.get}}") + } + insertRes.isSuccess + } + bv.digest match { + case Some(d) => Some(tree.updateDigest(Colls.fromArray(d))) + case _ => None + } + } + } + + def update( + operations: Coll[(Coll[Byte], Coll[Byte])], + proof: Coll[Byte]): Option[AvlTree] = { + if (!tree.isUpdateAllowed) { + None + } else { + val bv = AvlTreeVerifier(tree, proof) + operations.forall { case (key, value) => + bv.performOneOperation(Update(ADKey @@ key.toArray, ADValue @@ value.toArray)).isSuccess + } + bv.digest match { + case Some(d) => Some(tree.updateDigest(Colls.fromArray(d))) + case _ => None + } + } + } + + def remove(operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] = { + if (!tree.isRemoveAllowed) { + None + } else { + val bv = AvlTreeVerifier(tree, proof) + cfor(0)(_ < operations.length, _ + 1) { i => + val key = operations(i).toArray + bv.performOneOperation(Remove(ADKey @@ key)) + } + bv.digest match { + case Some(v) => Some(tree.updateDigest(Colls.fromArray(v))) + case _ => None + } + } + } + } } diff --git a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala index 35bc8bdc07..ba5a016142 100644 --- a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala +++ b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala @@ -3,12 +3,12 @@ package sigmastate.lang import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks -import sigma.data.{Nullable, RType} +import sigma.data.{CAvlTree, Nullable, RType} import sigma.{Environment, VersionContext} import sigmastate.Values._ import sigmastate._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CAnyValue, CAvlTree, CostingBox, SigmaDsl} +import sigmastate.eval.{CAnyValue, CostingBox, SigmaDsl} import sigmastate.exceptions.ConstraintFailed import sigmastate.serialization.OpCodes import sigma.SigmaTestingData diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala index 485f23f87c..d068492625 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala @@ -8,6 +8,7 @@ import sigma.data.{AvlTreeData, AvlTreeFlags} import sigmastate.Values.AvlTreeConstant import sigmastate.eval._ import sigma.Extensions.ArrayOps +import sigmastate.eval.Extensions.AvlTreeOps class AvlTreeSpecification extends SerializationSpecification { diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 4d2986126a..a5374456b5 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -6,14 +6,14 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.containerOfN import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, COR, CSigmaProp, ProveDHTuple, ProveDlog, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CAvlTree, CBigInt, COR, CSigmaProp, ProveDHTuple, ProveDlog, RType} import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigmastate.eval._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, CostingBox, CostingSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CHeader, CPreHeader, CostingBox, CostingSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 9f47024b38..63c93f0ed0 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4,7 +4,7 @@ import java.math.BigInteger import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CBigInt, CGroupElement, COR, CSigmaProp, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CAvlTree, CBigInt, CGroupElement, COR, CSigmaProp, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index 6931a2920b..9b86611a70 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -21,6 +21,7 @@ import sigma.Coll import sigma.ast.SAvlTree import sigma.data.{AvlTreeData, AvlTreeFlags, CSigmaProp, TrivialProp} import sigma.{AvlTree, Context} +import sigmastate.eval.Extensions.AvlTreeOps class AVLTreeScriptsSpecification extends CompilerTestingCommons diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 37ce5f1ae6..dfe4bbe3d6 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -6,13 +6,13 @@ import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.ergoplatform.sdk.wallet.protocol.context import org.ergoplatform._ import org.ergoplatform.sdk.Iso.inverseIso -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, Iso, RType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree, CBigInt, CGroupElement, Iso, RType} import scorex.crypto.authds.ADKey import scorex.util.ModifierId import scorex.util.encode.Base16 import sigmastate.Values.{Constant, GroupElementConstant} import sigma.Extensions.ArrayOps -import sigmastate.eval.{CAvlTree, CHeader, CPreHeader, Digest32Coll} +import sigmastate.eval.{CHeader, CPreHeader, Digest32Coll} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index bb2c594bb0..59a033d65a 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -1,12 +1,12 @@ package org.ergoplatform.sdk.js import org.ergoplatform.sdk.js.Value.toRuntimeData -import sigma.data.{CGroupElement, CSigmaProp, CollType, PairType, RType} +import sigma.data.{CAvlTree, CGroupElement, CSigmaProp, CollType, PairType, RType} import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType import sigma.crypto.Platform -import sigmastate.eval.{CAvlTree, CostingBox, SigmaDsl} +import sigmastate.eval.{CostingBox, SigmaDsl} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index 926515fb0f..c86f0f2125 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -1,7 +1,7 @@ package org.ergoplatform.sdk.utils import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, CGroupElement, CSigmaProp, CollType, FuncType, OptionType, PairType, RType, TrivialProp, TupleType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree, CBigInt, CGroupElement, CSigmaProp, CollType, FuncType, OptionType, PairType, RType, TrivialProp, TupleType} import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} From dd91fa3a926beaae91bd786b51fa4eecf341d371 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 13 Sep 2023 17:07:42 +0200 Subject: [PATCH 21/34] core-serializers: CoreDataSerializer introduced --- .../serialization/CoreDataSerializer.scala | 155 ++++++++++++++++++ .../GroupElementSerializer.scala | 2 - .../main/scala/sigma/util/Extensions.scala | 14 +- .../scala/sigmastate/eval/Extensions.scala | 4 + .../serialization/DataSerializer.scala | 148 ++--------------- .../scala/sigma/SigmaDslSpecification.scala | 48 +++--- .../SoftForkabilitySpecification.scala | 2 +- .../DeserializationResilience.scala | 12 +- .../ergoplatform/sdk/ContractTemplate.scala | 3 +- .../ergoplatform/sdk/DataJsonEncoder.scala | 3 +- 10 files changed, 216 insertions(+), 175 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/serialization/CoreDataSerializer.scala diff --git a/core/shared/src/main/scala/sigma/serialization/CoreDataSerializer.scala b/core/shared/src/main/scala/sigma/serialization/CoreDataSerializer.scala new file mode 100644 index 0000000000..479b199da5 --- /dev/null +++ b/core/shared/src/main/scala/sigma/serialization/CoreDataSerializer.scala @@ -0,0 +1,155 @@ +package sigma.serialization + +import debox.cfor +import sigma.ast._ +import sigma.data._ +import sigma.util.Extensions.{CoreAvlTreeOps, BigIntOps, GroupElementOps, SigmaPropOps} +import sigma.validation.ValidationRules.CheckSerializableTypeCode +import sigma.{Evaluation, _} + +import java.math.BigInteger +import java.nio.charset.StandardCharsets +import scala.collection.mutable + +/** This works in tandem with ConstantSerializer, if you change one make sure to check the other.*/ +class CoreDataSerializer { + + /** Use type descriptor `tpe` to deconstruct type structure and recursively serialize subcomponents. + * Primitive types are leaves of the type tree, and they are served as basis of recursion. + * The data value `v` is expected to conform to the type described by `tpe`. + */ + def serialize[T <: SType](v: T#WrappedType, tpe: T, w: CoreByteWriter): Unit = tpe match { + case SUnit => // don't need to save anything + case SBoolean => w.putBoolean(v.asInstanceOf[Boolean]) + case SByte => w.put(v.asInstanceOf[Byte]) + case SShort => w.putShort(v.asInstanceOf[Short]) + case SInt => w.putInt(v.asInstanceOf[Int]) + case SLong => w.putLong(v.asInstanceOf[Long]) + case SString => + val bytes = v.asInstanceOf[String].getBytes(StandardCharsets.UTF_8) + w.putUInt(bytes.length) + w.putBytes(bytes) + case SBigInt => + val data = v.asInstanceOf[BigInt].toBigInteger.toByteArray + w.putUShort(data.length) + w.putBytes(data) + case SGroupElement => + GroupElementSerializer.serialize(v.asInstanceOf[GroupElement].toECPoint, w) + case SSigmaProp => + val p = v.asInstanceOf[SigmaProp] + SigmaBoolean.serializer.serialize(p.toSigmaBoolean, w) + case SAvlTree => + AvlTreeData.serializer.serialize(v.asInstanceOf[AvlTree].toAvlTreeData, w) + case tColl: SCollectionType[a] => + val coll = v.asInstanceOf[tColl.WrappedType] + w.putUShort(coll.length) + tColl.elemType match { + case SBoolean => + w.putBits(coll.asInstanceOf[Coll[Boolean]].toArray) + case SByte => + w.putBytes(coll.asInstanceOf[Coll[Byte]].toArray) + case _ => + val arr = coll.toArray + cfor(0)(_ < arr.length, _ + 1) { i => + val x = arr(i) + serialize(x, tColl.elemType, w) + } + } + + case t: STuple => + val arr = Evaluation.fromDslTuple(v, t).asInstanceOf[t.WrappedType] + val len = arr.length + assert(arr.length == t.items.length, s"Type $t doesn't correspond to value $arr") + if (len > 0xFFFF) + sys.error(s"Length of tuple ${arr.length} exceeds ${0xFFFF} limit.") + var i = 0 + while (i < arr.length) { + serialize[SType](arr(i), t.items(i), w) + i += 1 + } + + // TODO v6.0 (3h): support Option[T] (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/659) + case _ => + CheckSerializableTypeCode(tpe.typeCode) + throw new SerializerException(s"Don't know how to serialize ($v, $tpe)") + } + + /** Reads a data value from Reader. The data value bytes is expected to confirm + * to the type descriptor `tpe`. + * The data structure depth is limited by r.maxTreeDepth which is + * SigmaSerializer.MaxTreeDepth by default. + */ + def deserialize[T <: SType](tpe: T, r: CoreByteReader): T#WrappedType = { + val depth = r.level + r.level = depth + 1 + val res = (tpe match { + case SUnit => () + case SBoolean => r.getUByte() != 0 + case SByte => r.getByte() + case SShort => r.getShort() + case SInt => r.getInt() + case SLong => r.getLong() + case SString => + val size = r.getUIntExact + // NO-FORK: in v5.x getUIntExact may throw Int overflow exception + // in v4.x r.getUInt().toInt is used and may return negative Int instead of the overflow + // in which case the getBytes will throw NegativeArraySizeException + val bytes = r.getBytes(size) + new String(bytes, StandardCharsets.UTF_8) + case SBigInt => + val size: Short = r.getUShort().toShort + if (size > SBigInt.MaxSizeInBytes) { + throw new SerializerException(s"BigInt value doesn't not fit into ${SBigInt.MaxSizeInBytes} bytes: $size") + } + val valueBytes = r.getBytes(size) + CBigInt(new BigInteger(valueBytes)) + case SGroupElement => + CGroupElement(GroupElementSerializer.parse(r)) + case SSigmaProp => + CSigmaProp(SigmaBoolean.serializer.parse(r)) + case SAvlTree => + CAvlTree(AvlTreeData.serializer.parse(r)) + case tColl: SCollectionType[a] => + val len = r.getUShort() + deserializeColl(len, tColl.elemType, r) + case tuple: STuple => + val arr = tuple.items.map { t => + deserialize(t, r) + }.toArray[Any] + val coll = Colls.fromArray(arr)(sigma.AnyType) + Evaluation.toDslTuple(coll, tuple) + case t => + CheckSerializableTypeCode(t.typeCode) + throw new SerializerException(s"Not defined DataSerializer for type $t") + }).asInstanceOf[T#WrappedType] + r.level = r.level - 1 + res + } + + def deserializeColl[T <: SType](len: Int, tpeElem: T, r: CoreByteReader): Coll[T#WrappedType] = + tpeElem match { + case SBoolean => + Colls.fromArray(r.getBits(len)).asInstanceOf[Coll[T#WrappedType]] + case SByte => + Colls.fromArray(r.getBytes(len)).asInstanceOf[Coll[T#WrappedType]] + case _ => + implicit val tItem = (tpeElem match { + case tTup: STuple if tTup.items.length == 2 => + Evaluation.stypeToRType(tpeElem) + case _: STuple => + collRType(sigma.AnyType) + case _ => + Evaluation.stypeToRType(tpeElem) + }).asInstanceOf[RType[T#WrappedType]] + val b = { // this code works both for Scala 2.12 and 2.13 + implicit val ct = tItem.classTag + mutable.ArrayBuilder.make[T#WrappedType] + } + for (_ <- 0 until len) { + b += deserialize(tpeElem, r) + } + Colls.fromArray(b.result()) + } +} + +object CoreDataSerializer extends CoreDataSerializer \ No newline at end of file diff --git a/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala b/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala index 03f3a7b9ae..4fd826ace1 100644 --- a/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala +++ b/core/shared/src/main/scala/sigma/serialization/GroupElementSerializer.scala @@ -41,6 +41,4 @@ object GroupElementSerializer extends CoreSerializer[EcPointType, EcPointType] { } } - def parse(bytes: Array[Byte]): EcPointType = parse(CoreSerializer.startReader(bytes)) - } diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index cd92221b89..624b3f5d6b 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -3,7 +3,7 @@ package sigma.util import debox.{cfor, Buffer => DBuffer} import sigma.crypto.{CryptoFacade, Ecp} import sigma.data._ -import sigma.{GroupElement, SigmaProp} +import sigma.{AvlTree, GroupElement, SigmaProp} import java.math.BigInteger import java.nio.ByteBuffer @@ -335,7 +335,13 @@ object Extensions { def toSigmaBoolean: SigmaBoolean = sb.asInstanceOf[CSigmaProp].wrappedValue } -// implicit class AvlTreeOps(val tree: AvlTree) extends AnyVal { -// def toAvlTreeData: AvlTreeData = tree.asInstanceOf[CAvlTree].wrappedValue -// } + implicit class AvlTreeDataOps(val treeData: AvlTreeData) extends AnyVal { + /** Wrap [[sigma.AvlTreeData]] to [[sigma.AvlTree]]. */ + def toAvlTree: AvlTree= CAvlTree(treeData) + } + + implicit class CoreAvlTreeOps(val tree: AvlTree) extends AnyVal { + /** Extracts [[sigma.AvlTreeData]] from the AvlTree instance. */ + def toAvlTreeData: AvlTreeData = tree.asInstanceOf[CAvlTree].wrappedValue + } } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 0d40d40978..f8f0afd02b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -26,6 +26,10 @@ object Extensions { @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong)) } + implicit class ShortExt(val b: Short) extends AnyVal { + @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong)) + } + implicit class IntExt(val x: Int) extends AnyVal { /** Convert this value to BigInt. */ @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(x.toLong)) diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala index 140fa0b507..491ffb2be8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/DataSerializer.scala @@ -1,84 +1,25 @@ package sigmastate.serialization -import java.math.BigInteger -import java.nio.charset.StandardCharsets import org.ergoplatform.ErgoBox -import sigma.data.{AvlTreeData, RType, SigmaBoolean} -import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.eval._ -import sigma.{Evaluation, _} -import debox.cfor +import sigma._ import sigma.ast._ -import sigma.serialization.{GroupElementSerializer, SerializerException} -import sigma.util.Extensions.{BigIntOps, GroupElementOps, SigmaPropOps} -import sigma.validation.ValidationRules.CheckSerializableTypeCode - -import scala.collection.mutable +import sigma.serialization.{CoreByteReader, CoreByteWriter, CoreDataSerializer} +import sigmastate.eval.SigmaDsl +import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} /** This works in tandem with ConstantSerializer, if you change one make sure to check the other.*/ -object DataSerializer { +object DataSerializer extends CoreDataSerializer { /** Use type descriptor `tpe` to deconstruct type structure and recursively serialize subcomponents. * Primitive types are leaves of the type tree, and they are served as basis of recursion. * The data value `v` is expected to conform to the type described by `tpe`. */ - def serialize[T <: SType](v: T#WrappedType, tpe: T, w: SigmaByteWriter): Unit = tpe match { - case SUnit => // don't need to save anything - case SBoolean => w.putBoolean(v.asInstanceOf[Boolean]) - case SByte => w.put(v.asInstanceOf[Byte]) - case SShort => w.putShort(v.asInstanceOf[Short]) - case SInt => w.putInt(v.asInstanceOf[Int]) - case SLong => w.putLong(v.asInstanceOf[Long]) - case SString => - val bytes = v.asInstanceOf[String].getBytes(StandardCharsets.UTF_8) - w.putUInt(bytes.length) - w.putBytes(bytes) - case SBigInt => - val data = v.asInstanceOf[BigInt].toBigInteger.toByteArray - w.putUShort(data.length) - w.putBytes(data) - case SGroupElement => - GroupElementSerializer.serialize(v.asInstanceOf[GroupElement].toECPoint, w) - case SSigmaProp => - val p = v.asInstanceOf[SigmaProp] - SigmaBoolean.serializer.serialize(p.toSigmaBoolean, w) + override def serialize[T <: SType](v: T#WrappedType, tpe: T, w: CoreByteWriter): Unit = tpe match { case SBox => val b = v.asInstanceOf[Box] - ErgoBox.sigmaSerializer.serialize(boxToErgoBox(b), w) - case SAvlTree => - AvlTreeData.serializer.serialize(avlTreeToAvlTreeData(v.asInstanceOf[AvlTree]), w) - case tColl: SCollectionType[a] => - val coll = v.asInstanceOf[tColl.WrappedType] - w.putUShort(coll.length) - tColl.elemType match { - case SBoolean => - w.putBits(coll.asInstanceOf[Coll[Boolean]].toArray) - case SByte => - w.putBytes(coll.asInstanceOf[Coll[Byte]].toArray) - case _ => - val arr = coll.toArray - cfor(0)(_ < arr.length, _ + 1) { i => - val x = arr(i) - serialize(x, tColl.elemType, w) - } - } - - case t: STuple => - val arr = Evaluation.fromDslTuple(v, t).asInstanceOf[t.WrappedType] - val len = arr.length - assert(arr.length == t.items.length, s"Type $t doesn't correspond to value $arr") - if (len > 0xFFFF) - sys.error(s"Length of tuple ${arr.length} exceeds ${0xFFFF} limit.") - var i = 0 - while (i < arr.length) { - serialize[SType](arr(i), t.items(i), w) - i += 1 - } - - // TODO v6.0 (3h): support Option[T] (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/659) + ErgoBox.sigmaSerializer.serialize(SigmaDsl.toErgoBox(b), w.asInstanceOf[SigmaByteWriter]) case _ => - CheckSerializableTypeCode(tpe.typeCode) - throw new SerializerException(s"Don't know how to serialize ($v, $tpe)") + super.serialize(v, tpe, w) } /** Reads a data value from Reader. The data value bytes is expected to confirm @@ -86,77 +27,18 @@ object DataSerializer { * The data structure depth is limited by r.maxTreeDepth which is * SigmaSerializer.MaxTreeDepth by default. */ - def deserialize[T <: SType](tpe: T, r: SigmaByteReader): T#WrappedType = { - val depth = r.level - r.level = depth + 1 + override def deserialize[T <: SType](tpe: T, r: CoreByteReader): T#WrappedType = { val res = (tpe match { - case SUnit => () - case SBoolean => r.getUByte() != 0 - case SByte => r.getByte() - case SShort => r.getShort() - case SInt => r.getInt() - case SLong => r.getLong() - case SString => - val size = r.getUIntExact - // NO-FORK: in v5.x getUIntExact may throw Int overflow exception - // in v4.x r.getUInt().toInt is used and may return negative Int instead of the overflow - // in which case the getBytes will throw NegativeArraySizeException - val bytes = r.getBytes(size) - new String(bytes, StandardCharsets.UTF_8) - case SBigInt => - val size: Short = r.getUShort().toShort - if (size > SBigInt.MaxSizeInBytes) { - throw new SerializerException(s"BigInt value doesn't not fit into ${SBigInt.MaxSizeInBytes} bytes: $size") - } - val valueBytes = r.getBytes(size) - SigmaDsl.BigInt(new BigInteger(valueBytes)) - case SGroupElement => - SigmaDsl.GroupElement(GroupElementSerializer.parse(r)) - case SSigmaProp => - SigmaDsl.SigmaProp(SigmaBoolean.serializer.parse(r)) case SBox => - SigmaDsl.Box(ErgoBox.sigmaSerializer.parse(r)) - case SAvlTree => - SigmaDsl.avlTree(AvlTreeData.serializer.parse(r)) - case tColl: SCollectionType[a] => - val len = r.getUShort() - deserializeColl(len, tColl.elemType, r) - case tuple: STuple => - val arr = tuple.items.map { t => - deserialize(t, r) - }.toArray[Any] - val coll = Colls.fromArray(arr)(sigma.AnyType) - Evaluation.toDslTuple(coll, tuple) + val depth = r.level + r.level = depth + 1 + val res = SigmaDsl.Box(ErgoBox.sigmaSerializer.parse(r.asInstanceOf[SigmaByteReader])) + r.level = r.level - 1 + res case t => - CheckSerializableTypeCode(t.typeCode) - throw new SerializerException(s"Not defined DataSerializer for type $t") + super.deserialize(t, r) }).asInstanceOf[T#WrappedType] - r.level = r.level - 1 res } - def deserializeColl[T <: SType](len: Int, tpeElem: T, r: SigmaByteReader): Coll[T#WrappedType] = - tpeElem match { - case SBoolean => - Colls.fromArray(r.getBits(len)).asInstanceOf[Coll[T#WrappedType]] - case SByte => - Colls.fromArray(r.getBytes(len)).asInstanceOf[Coll[T#WrappedType]] - case _ => - implicit val tItem = (tpeElem match { - case tTup: STuple if tTup.items.length == 2 => - Evaluation.stypeToRType(tpeElem) - case _: STuple => - collRType(sigma.AnyType) - case _ => - Evaluation.stypeToRType(tpeElem) - }).asInstanceOf[RType[T#WrappedType]] - val b = { // this code works both for Scala 2.12 and 2.13 - implicit val ct = tItem.classTag - mutable.ArrayBuilder.make[T#WrappedType] - } - for (_ <- 0 until len) { - b += deserialize(tpeElem, r) - } - Colls.fromArray(b.result()) - } } diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 63c93f0ed0..80362036dd 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -1,47 +1,39 @@ package special.sigma -import java.math.BigInteger +import org.ergoplatform.ErgoBox.AdditionalRegisters import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos +import org.scalacheck.Arbitrary._ import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CAvlTree, CBigInt, CGroupElement, COR, CSigmaProp, CTHRESHOLD, ExactIntegral, ExactNumeric, ExactOrdering, ProveDHTuple, ProveDlog, RType, TrivialProp} +import org.scalatest.BeforeAndAfterAll import scorex.crypto.authds.avltree.batch._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} -import sigma.util.Extensions._ -import sigmastate.utils.Extensions._ +import scorex.util.ModifierId +import sigma.Extensions.{ArrayOps, CollOps} import sigma.ast.SCollection._ -import sigmastate.Values.IntConstant +import sigma.ast._ +import sigma.data.RType._ +import sigma.data._ +import sigma.util.Extensions.{BooleanOps, IntOps, LongOps} +import sigma.{VersionContext, data, _} +import sigmastate.Values.{IntConstant, _} import sigmastate._ -import sigmastate.crypto.DLogProtocol._ -import sigmastate.Values._ -import sigmastate.lang.Terms.Apply -import sigmastate.eval.Extensions._ +import sigmastate.eval.Extensions.{AvlTreeOps, ByteExt, IntExt, LongExt, ShortExt} +import sigmastate.eval.OrderingOps._ import sigmastate.eval._ -import sigmastate.lang.Terms.{MethodCall, PropertyCall} -import sigmastate.utxo._ -import sigma.{VersionContext, data, _} -import sigma.Extensions._ -import sigmastate.utils.Helpers -import sigmastate.utils.Helpers._ import sigmastate.helpers.TestingHelpers._ - -import scala.util.{Failure, Success, Try} -import OrderingOps._ -import org.ergoplatform.ErgoBox.AdditionalRegisters -import org.scalacheck.Arbitrary._ -import org.scalacheck.Gen.frequency -import org.scalatest.{BeforeAndAfterAll, Tag} -import sigma.data.RType._ -import scorex.util.ModifierId import sigmastate.interpreter._ -import org.scalactic.source.Position -import sigma.ast._ -import sigmastate.helpers.SigmaPPrint -import sigmastate.exceptions.GraphBuildingException +import sigmastate.lang.Terms.{Apply, MethodCall, PropertyCall} import sigmastate.serialization.ValueCodes.OpCode +import sigmastate.utils.Extensions._ +import sigmastate.utils.Helpers +import sigmastate.utils.Helpers._ +import sigmastate.utxo._ +import java.math.BigInteger import scala.collection.compat.immutable.ArraySeq +import scala.util.{Failure, Success} /** This suite tests every method of every SigmaDsl type to be equivalent to * the evaluation of the corresponding ErgoScript operation. diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index c62dd1dc2d..28ff4fafb0 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -20,7 +20,7 @@ import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.interpreter.{ContextExtension, ErgoTreeEvaluator, ProverResult} import sigmastate.lang.Terms._ import sigmastate.serialization.SigmaSerializer.startReader -import sigmastate.serialization._ +import sigmastate.serialization.{DataSerializer, _} import sigmastate.utils.Helpers._ import sigmastate.utxo.DeserializeContext import sigma.{Colls, SigmaTestingData} diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index 6b895f1a71..39003b4365 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -233,7 +233,9 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("reader.level is updated in DataSerializer.deserialize") { val expr = IntConstant(1) val (callDepths, levels) = traceReaderCallDepth(expr) - callDepths shouldEqual levels + if (Environment.current.isJVM) { + callDepths shouldEqual levels // on JS stacktrace differs from JVM + } callDepths shouldEqual IndexedSeq(1, 2, 2, 1) } @@ -246,7 +248,9 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("reader.level is updated in SigmaBoolean.serializer.parse") { val expr = CAND(Seq(proveDlogGen.sample.get, proveDHTGen.sample.get)) val (callDepths, levels) = traceReaderCallDepth(expr) - callDepths shouldEqual levels + if (Environment.current.isJVM) { + callDepths shouldEqual levels + } callDepths shouldEqual IndexedSeq(1, 2, 3, 4, 4, 4, 4, 3, 2, 1) } @@ -259,7 +263,9 @@ class DeserializationResilience extends DeserializationResilienceTesting { property("reader.level is updated in TypeSerializer") { val expr = Tuple(Tuple(IntConstant(1), IntConstant(1)), IntConstant(1)) val (callDepths, levels) = traceReaderCallDepth(expr) - callDepths shouldEqual levels + if (Environment.current.isJVM) { + callDepths shouldEqual levels + } callDepths shouldEqual IndexedSeq(1, 2, 3, 4, 4, 3, 3, 4, 4, 3, 2, 2, 3, 3, 2, 1) } diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala index 9ce66f5729..4a3577c993 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala @@ -13,9 +13,8 @@ import sigma.util.safeNewArray import sigmastate.Values.ErgoTree.headerWithVersion import sigmastate.Values.{ErgoTree, _} import sigmastate._ -import sigmastate.eval._ import sigmastate.lang.{DeserializationSigmaBuilder, StdSigmaBuilder} -import sigmastate.serialization._ +import sigmastate.serialization.{DataSerializer, _} import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} import java.util.Objects diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala index 4b679b2750..a384129ec3 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala @@ -18,8 +18,7 @@ import scala.collection.compat.immutable.ArraySeq import scala.collection.mutable import sigma.ast._ import sigma.serialization.SerializerException -import sigmastate.serialization.SigmaSerializer -import sigmastate.serialization.DataSerializer +import sigmastate.serialization.{DataSerializer, SigmaSerializer} import sigmastate.serialization.ErgoTreeSerializer object DataJsonEncoder { From c973adbd905b8e9ebc2c987c2b1783c3efbd2795 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 13 Sep 2023 20:21:28 +0200 Subject: [PATCH 22/34] core-serializers: js.Type moved to core.js --- build.sbt | 23 +++---------------- .../js/src/main/scala/sigma}/js/Type.scala | 4 ++-- .../DeserializationResilience.scala | 4 ++-- .../scala/org/ergoplatform/sdk/js/Isos.scala | 1 + .../scala/org/ergoplatform/sdk/js/Value.scala | 1 + 5 files changed, 9 insertions(+), 24 deletions(-) rename {sdk/js/src/main/scala/org/ergoplatform/sdk => core/js/src/main/scala/sigma}/js/Type.scala (95%) diff --git a/build.sbt b/build.sbt index abfb17ae44..607f69f7fe 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ import scala.language.postfixOps -import scala.sys.process._ -import org.scalajs.linker.interface.CheckedBehavior +import scala.sys.process.* +import org.scalajs.linker.interface.{CheckedBehavior, ModuleSplitStyle} organization := "org.scorexfoundation" @@ -222,6 +222,7 @@ lazy val coreJS = core.js stOutputPackage := "sigmastate", scalaJSLinkerConfig ~= { conf => conf.withSourceMap(false) + .withModuleKind(ModuleKind.CommonJSModule) }, Compile / npmDependencies ++= Seq( "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion, @@ -242,9 +243,6 @@ lazy val interpreter = crossProject(JVMPlatform, JSPlatform) .jvmSettings( crossScalaSettings ) .jsSettings( crossScalaSettingsJS, - libraryDependencies ++= Seq ( - "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0" - ), useYarn := true ) lazy val interpreterJS = interpreter.js @@ -265,9 +263,6 @@ lazy val parsers = crossProject(JVMPlatform, JSPlatform) ) .jsSettings( crossScalaSettingsJS, - libraryDependencies ++= Seq( - "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0" - ), useYarn := true ) lazy val parsersJS = parsers.js @@ -276,9 +271,6 @@ lazy val parsersJS = parsers.js scalaJSLinkerConfig ~= { conf => conf.withSourceMap(false) }, - Compile / npmDependencies ++= Seq( - "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion - ) ) lazy val sdk = crossProject(JVMPlatform, JSPlatform) @@ -295,9 +287,6 @@ lazy val sdk = crossProject(JVMPlatform, JSPlatform) ) .jsSettings( crossScalaSettingsJS, - libraryDependencies ++= Seq( - "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0" - ), useYarn := true ) lazy val sdkJS = sdk.js @@ -307,9 +296,6 @@ lazy val sdkJS = sdk.js conf.withSourceMap(false) .withModuleKind(ModuleKind.CommonJSModule) }, - Compile / npmDependencies ++= Seq( - "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion - ) ) lazy val sc = crossProject(JVMPlatform, JSPlatform) @@ -353,9 +339,6 @@ lazy val scJS = sc.js .withArrayIndexOutOfBounds(CheckedBehavior.Compliant) ) }, - Compile / npmDependencies ++= Seq( - "sigmajs-crypto-facade" -> sigmajsCryptoFacadeVersion - ) ) diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Type.scala b/core/js/src/main/scala/sigma/js/Type.scala similarity index 95% rename from sdk/js/src/main/scala/org/ergoplatform/sdk/js/Type.scala rename to core/js/src/main/scala/sigma/js/Type.scala index a0930cc99a..67e24736b9 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Type.scala +++ b/core/js/src/main/scala/sigma/js/Type.scala @@ -1,4 +1,4 @@ -package org.ergoplatform.sdk.js +package sigma.js import sigma.data.RType @@ -10,7 +10,7 @@ import scala.scalajs.js.annotation.JSExportTopLevel * wrapper around {@link RType} type descriptor. */ @JSExportTopLevel("Type") -class Type(private[js] final val rtype: RType[_]) extends js.Object { +class Type(final val rtype: RType[_]) extends js.Object { /** Syntactically correct type name (type expression as String) */ def name: String = rtype.name diff --git a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index 39003b4365..1d03555d9b 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -249,7 +249,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { val expr = CAND(Seq(proveDlogGen.sample.get, proveDHTGen.sample.get)) val (callDepths, levels) = traceReaderCallDepth(expr) if (Environment.current.isJVM) { - callDepths shouldEqual levels + callDepths shouldEqual levels // on JS stacktrace differs from JVM } callDepths shouldEqual IndexedSeq(1, 2, 3, 4, 4, 4, 4, 3, 2, 1) } @@ -264,7 +264,7 @@ class DeserializationResilience extends DeserializationResilienceTesting { val expr = Tuple(Tuple(IntConstant(1), IntConstant(1)), IntConstant(1)) val (callDepths, levels) = traceReaderCallDepth(expr) if (Environment.current.isJVM) { - callDepths shouldEqual levels + callDepths shouldEqual levels // on JS stacktrace differs from JVM } callDepths shouldEqual IndexedSeq(1, 2, 3, 4, 4, 3, 3, 4, 4, 3, 2, 2, 3, 3, 2, 1) } diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index dfe4bbe3d6..3bdda049bf 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -24,6 +24,7 @@ import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} import sigma.{Coll, Colls, Evaluation} import sigma.Extensions.CollBytesOps import sigma.ast.SType +import sigma.js.Type import java.math.BigInteger import scala.collection.immutable.ListMap diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala index 59a033d65a..068871a861 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala @@ -6,6 +6,7 @@ import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType import sigma.crypto.Platform +import sigma.js.Type import sigmastate.eval.{CostingBox, SigmaDsl} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod From 5695687917c1d0c52976b8fd8e1656d98a673505 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 13 Sep 2023 20:31:04 +0200 Subject: [PATCH 23/34] core-serializers: fixes after merge --- .../main/scala/sigmastate/eval/CostingDataContext.scala | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 47025116b5..1935bc105f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -4,7 +4,7 @@ import debox.cfor import org.ergoplatform.ErgoBox import org.ergoplatform.validation.ValidationRules import scorex.crypto.authds.avltree.batch._ -import scorex.crypto.authds.{SerializedAdProof, ADDigest, ADValue, ADKey} +import scorex.crypto.authds.{ADDigest, SerializedAdProof} import scorex.crypto.hash.{Blake2b256, Digest32, Sha256} import scorex.utils.{Ints, Longs} import sigma.Extensions.ArrayOps @@ -17,20 +17,16 @@ import sigma.serialization.GroupElementSerializer import sigma.util.Extensions.BigIntegerOps import sigma.validation.SigmaValidationSettings import sigma.{VersionContext, _} -import sigmastate.Values.ErgoTree.EmptyConstants -import sigmastate.Values.{ConstantNode, ErgoTree, EvaluatedValue, SValue} +import sigmastate.Values.{ConstantNode, EvaluatedValue, SValue} import sigmastate._ import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions._ -import sigmastate.interpreter.Interpreter -import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer import java.math.BigInteger import java.util.Arrays import scala.annotation.unused import scala.reflect.ClassTag -import scala.util.{Success, Failure} /** Implements operations of AVL tree verifier based on * [[scorex.crypto.authds.avltree.batch.BatchAVLVerifier]]. From 4fcd2a71f94d6a0e5a1922817dba02e5657558e1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 13 Sep 2023 22:46:12 +0200 Subject: [PATCH 24/34] core-serializers: Value moved to core.js --- core/js/src/main/scala/sigma/js/AvlTree.scala | 50 ++++++++ .../main/scala/sigma}/js/GroupElement.scala | 4 +- core/js/src/main/scala/sigma/js/Isos.scala | 29 +++++ .../src/main/scala/sigma}/js/SigmaProp.scala | 3 +- .../js/src/main/scala/sigma}/js/Value.scala | 56 ++++----- .../src/main/scala/sigma/Extensions.scala | 6 + .../src/main/scala/sigma/data/Iso.scala | 23 ++++ .../scala/sigmastate/eval/Extensions.scala | 2 +- .../sigmastate/lang/js/SigmaCompiler.scala | 3 +- .../org/ergoplatform/sdk/js/AvlTree.scala | 16 --- .../org/ergoplatform/sdk/js/ErgoTree.scala | 1 + .../org/ergoplatform/sdk/js/Header.scala | 2 + .../scala/org/ergoplatform/sdk/js/Isos.scala | 113 +++++------------- .../org/ergoplatform/sdk/js/PreHeader.scala | 2 + .../ergoplatform/sdk/js/ProverBuilder.scala | 1 + .../org/ergoplatform/sdk/js/SigmaProver.scala | 6 +- .../org/ergoplatform/sdk/js/IsosSpec.scala | 13 +- .../org/ergoplatform/sdk/js/ValueSpec.scala | 4 +- .../sdk/AppkitProvingInterpreter.scala | 2 +- .../org/ergoplatform/sdk/JavaHelpers.scala | 10 +- .../org/ergoplatform/sdk/OutBoxBuilder.scala | 2 +- .../sdk/ReducingInterpreter.scala | 2 +- .../sdk/UnsignedTransactionBuilder.scala | 2 +- sigma-js/tests/js/Value.spec.js | 9 +- 24 files changed, 197 insertions(+), 164 deletions(-) create mode 100644 core/js/src/main/scala/sigma/js/AvlTree.scala rename {sdk/js/src/main/scala/org/ergoplatform/sdk => core/js/src/main/scala/sigma}/js/GroupElement.scala (92%) create mode 100644 core/js/src/main/scala/sigma/js/Isos.scala rename {sdk/js/src/main/scala/org/ergoplatform/sdk => core/js/src/main/scala/sigma}/js/SigmaProp.scala (95%) rename {sdk/js/src/main/scala/org/ergoplatform/sdk => core/js/src/main/scala/sigma}/js/Value.scala (82%) delete mode 100644 sdk/js/src/main/scala/org/ergoplatform/sdk/js/AvlTree.scala diff --git a/core/js/src/main/scala/sigma/js/AvlTree.scala b/core/js/src/main/scala/sigma/js/AvlTree.scala new file mode 100644 index 0000000000..f882ef0863 --- /dev/null +++ b/core/js/src/main/scala/sigma/js/AvlTree.scala @@ -0,0 +1,50 @@ +package sigma.js + +import sigma.Extensions.ArrayOps +import sigma.data.Iso.{isoStringToArray, isoStringToColl} +import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree, Iso} + +import scala.scalajs.js +import scala.scalajs.js.UndefOr +import scala.scalajs.js.annotation.JSExportTopLevel + +/** Equivalent of [[sigma.AvlTree]] available from JS. */ +@JSExportTopLevel("AvlTree") +class AvlTree( + val digest: String, + val insertAllowed: Boolean, + val updateAllowed: Boolean, + val removeAllowed: Boolean, + val keyLength: Int, + val valueLengthOpt: UndefOr[Int] +) extends js.Object + +object AvlTree { + + implicit val isoAvlTree: Iso[AvlTree, sigma.AvlTree] = new Iso[AvlTree, sigma.AvlTree] { + override def to(x: AvlTree): sigma.AvlTree = { + CAvlTree( + AvlTreeData( + digest = isoStringToArray.to(x.digest).toColl, + treeFlags = AvlTreeFlags(x.insertAllowed, x.updateAllowed, x.removeAllowed), + x.keyLength, + valueLengthOpt = sigma.js.Isos.isoUndefOr(Iso.identityIso[Int]).to(x.valueLengthOpt), + ), + ) + } + + override def from(x: sigma.AvlTree): AvlTree = { + val tree = x.asInstanceOf[CAvlTree] + val data = tree.treeData + new AvlTree( + digest = isoStringToColl.from(tree.digest), + insertAllowed = data.treeFlags.insertAllowed, + updateAllowed = data.treeFlags.updateAllowed, + removeAllowed = data.treeFlags.removeAllowed, + keyLength = data.keyLength, + valueLengthOpt = sigma.js.Isos.isoUndefOr(Iso.identityIso[Int]).from(data.valueLengthOpt), + ) + } + } + +} \ No newline at end of file diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala b/core/js/src/main/scala/sigma/js/GroupElement.scala similarity index 92% rename from sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala rename to core/js/src/main/scala/sigma/js/GroupElement.scala index 3c710b7587..e6e472347e 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/GroupElement.scala +++ b/core/js/src/main/scala/sigma/js/GroupElement.scala @@ -1,7 +1,7 @@ -package org.ergoplatform.sdk.js +package sigma.js +import sigma.Extensions.CoreArrayByteOps import sigma.crypto.{CryptoFacade, CryptoFacadeJs, Ecp, Platform} -import sigmastate.eval.Extensions.ArrayByteOps import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/core/js/src/main/scala/sigma/js/Isos.scala b/core/js/src/main/scala/sigma/js/Isos.scala new file mode 100644 index 0000000000..b349406d2b --- /dev/null +++ b/core/js/src/main/scala/sigma/js/Isos.scala @@ -0,0 +1,29 @@ +package sigma.js + +import sigma.{Coll, Colls} +import sigma.data.{Iso, RType} + +import scala.reflect.ClassTag +import scala.scalajs.js +import scala.scalajs.js.JSConverters.JSRichOption + +object Isos { + + implicit def isoUndefOr[A, B](implicit iso: Iso[A, B]): Iso[js.UndefOr[A], Option[B]] = new Iso[js.UndefOr[A], Option[B]] { + override def to(x: js.UndefOr[A]): Option[B] = x.toOption.map(iso.to) + override def from(x: Option[B]): js.UndefOr[A] = x.map(iso.from).orUndefined + } + + implicit def isoArrayToColl[A, B](iso: Iso[A, B]) + (implicit ctA: ClassTag[A], tB: RType[B]): Iso[js.Array[A], Coll[B]] = new Iso[js.Array[A], Coll[B]] { + override def to(x: js.Array[A]): Coll[B] = Colls.fromArray(x.map(iso.to).toArray(tB.classTag)) + override def from(x: Coll[B]): js.Array[A] = js.Array(x.toArray.map(iso.from): _*) + } + + implicit def isoArrayToIndexed[A, B](iso: Iso[A, B]) + (implicit cB: ClassTag[B]): Iso[js.Array[A], IndexedSeq[B]] = new Iso[js.Array[A], IndexedSeq[B]] { + override def to(x: js.Array[A]): IndexedSeq[B] = x.map(iso.to).toArray(cB).toIndexedSeq + override def from(x: IndexedSeq[B]): js.Array[A] = js.Array(x.map(iso.from): _*) + } + +} diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala b/core/js/src/main/scala/sigma/js/SigmaProp.scala similarity index 95% rename from sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala rename to core/js/src/main/scala/sigma/js/SigmaProp.scala index d53ff9f82f..dd89929ca9 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProp.scala +++ b/core/js/src/main/scala/sigma/js/SigmaProp.scala @@ -1,6 +1,7 @@ -package org.ergoplatform.sdk.js +package sigma.js import sigma.data.{ProveDlog, SigmaBoolean} + import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala b/core/js/src/main/scala/sigma/js/Value.scala similarity index 82% rename from sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala rename to core/js/src/main/scala/sigma/js/Value.scala index 068871a861..234ef64203 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Value.scala +++ b/core/js/src/main/scala/sigma/js/Value.scala @@ -1,18 +1,14 @@ -package org.ergoplatform.sdk.js +package sigma +package js -import org.ergoplatform.sdk.js.Value.toRuntimeData -import sigma.data.{CAvlTree, CGroupElement, CSigmaProp, CollType, PairType, RType} import scorex.util.Extensions.{IntOps, LongOps} import scorex.util.encode.Base16 import sigma.ast.SType import sigma.crypto.Platform -import sigma.js.Type -import sigmastate.eval.{CostingBox, SigmaDsl} -import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box -import sigmastate.fleetSdkCommon.distEsmTypesCommonMod -import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters -import sigmastate.lang.DeserializationSigmaBuilder -import sigmastate.serialization.{ConstantSerializer, DataSerializer, SigmaSerializer} +import sigma.data._ +import sigma.js.Value.toRuntimeData +import sigma.serialization.{CoreDataSerializer, CoreSerializer} +import sigma.util.Extensions.BigIntOps import sigma.{Coll, Colls, Evaluation} import java.math.BigInteger @@ -46,7 +42,7 @@ class Value(val data: Any, val tpe: Type) extends js.Object { /** Get Sigma runtime value which can be passed to interpreter, saved in register and * [[sigmastate.Values.Constant]] nodes. */ - final private[js] def runtimeData: Any = toRuntimeData(data, tpe.rtype) + final def runtimeData: Any = toRuntimeData(data, tpe.rtype) /** * Encode this value as Base16 hex string. @@ -57,15 +53,11 @@ class Value(val data: Any, val tpe: Type) extends js.Object { * @return hex string of serialized bytes */ def toHex(): String = { - // this can be implemented using ConstantSerializer and isoValueToConstant, but this - // will add dependence on Constant and Values, which we want to avoid facilitate - // module splitting - // TODO simplify if module splitting fails val stype = Evaluation.rtypeToSType(tpe.rtype) val value = runtimeData.asInstanceOf[SType#WrappedType] - val w = SigmaSerializer.startWriter() + val w = CoreSerializer.startWriter() w.putType(stype) - DataSerializer.serialize(value, stype, w) + CoreDataSerializer.serialize(value, stype, w) Base16.encode(w.toBytes) } } @@ -87,19 +79,16 @@ object Value extends js.Object { case sigma.LongType => java.lang.Long.parseLong(data.asInstanceOf[js.BigInt].toString(10)) case sigma.BigIntRType => val v = data.asInstanceOf[js.BigInt] - SigmaDsl.BigInt(new BigInteger(v.toString(16), 16)) + CBigInt(new BigInteger(v.toString(16), 16)) case sigma.GroupElementRType => val ge = data.asInstanceOf[GroupElement] - SigmaDsl.GroupElement(ge.point) + CGroupElement(ge.point) case sigma.SigmaPropRType => val p = data.asInstanceOf[SigmaProp] - SigmaDsl.SigmaProp(p.sigmaBoolean) + CSigmaProp(p.sigmaBoolean) case sigma.AvlTreeRType => val t = data.asInstanceOf[AvlTree] - Isos.isoAvlTree.to(t) - case sigma.BoxRType => - val t = data.asInstanceOf[Box[distEsmTypesCommonMod.Amount, NonMandatoryRegisters]] - SigmaDsl.Box(Isos.isoBox.to(t)) + AvlTree.isoAvlTree.to(t) case ct: CollType[a] => val xs = data.asInstanceOf[js.Array[Any]] implicit val cT = ct.tItem.classTag @@ -121,12 +110,12 @@ object Value extends js.Object { * @param value runtime value of type given by `rtype` * @param rtype type descriptor of Sigma runtime value */ - final private[js] def fromRuntimeData(value: Any, rtype: RType[_]): Any = rtype match { + final def fromRuntimeData(value: Any, rtype: RType[_]): Any = rtype match { case sigma.BooleanType => value case sigma.ByteType | sigma.ShortType | sigma.IntType => value case sigma.LongType => js.BigInt(value.asInstanceOf[Long].toString) case sigma.BigIntRType => - val hex = SigmaDsl.toBigInteger(value.asInstanceOf[sigma.BigInt]).toString(10) + val hex = value.asInstanceOf[sigma.BigInt].toBigInteger.toString(10) js.BigInt(hex) case sigma.GroupElementRType => val point = value.asInstanceOf[CGroupElement].wrappedValue.asInstanceOf[Platform.Ecp] @@ -134,9 +123,7 @@ object Value extends js.Object { case sigma.SigmaPropRType => new SigmaProp(value.asInstanceOf[CSigmaProp].wrappedValue) case sigma.AvlTreeRType => - Isos.isoAvlTree.from(value.asInstanceOf[CAvlTree]) - case sigma.BoxRType => - Isos.isoBox.from(value.asInstanceOf[CostingBox].wrappedValue) + AvlTree.isoAvlTree.from(value.asInstanceOf[CAvlTree]) case ct: CollType[a] => val arr = value.asInstanceOf[Coll[a]].toArray js.Array(arr.map(x => fromRuntimeData(x, ct.tItem)):_*) @@ -263,9 +250,12 @@ object Value extends js.Object { * - and [[Type]] descriptor in its `tpe` field */ def fromHex(hex: String): Value = { - val bytes = Base16.decode(hex).fold(t => throw t, identity) - val S = ConstantSerializer(DeserializationSigmaBuilder) - val c = S.deserialize(SigmaSerializer.startReader(bytes)) - Isos.isoValueToConstant.from(c) + val bytes = Base16.decode(hex).fold(t => throw t, identity) + val r = CoreSerializer.startReader(bytes) + val stype = r.getType() + val value = CoreDataSerializer.deserialize(stype, r) + val rtype = Evaluation.stypeToRType(stype) + val jsvalue = Value.fromRuntimeData(value, rtype) + new Value(jsvalue, new Type(rtype)) } } diff --git a/core/shared/src/main/scala/sigma/Extensions.scala b/core/shared/src/main/scala/sigma/Extensions.scala index c4590eabae..865d488803 100644 --- a/core/shared/src/main/scala/sigma/Extensions.scala +++ b/core/shared/src/main/scala/sigma/Extensions.scala @@ -6,6 +6,12 @@ import scorex.util.{ModifierId, bytesToId} import sigma.data.RType object Extensions { + /** Extension methods for `Array[Byte]` not available for generic `Array[T]`. */ + implicit class CoreArrayByteOps(val arr: Array[Byte]) extends AnyVal { + /** Encodes array into hex string */ + @inline def toHex: String = Base16.encode(arr) + } + implicit class ArrayOps[T: RType](arr: Array[T]) { /** Wraps array into Coll instance. The source array in not cloned. */ @inline def toColl: Coll[T] = Colls.fromArray(arr) diff --git a/core/shared/src/main/scala/sigma/data/Iso.scala b/core/shared/src/main/scala/sigma/data/Iso.scala index 224b784772..9b90d63bb0 100644 --- a/core/shared/src/main/scala/sigma/data/Iso.scala +++ b/core/shared/src/main/scala/sigma/data/Iso.scala @@ -1,5 +1,9 @@ package sigma.data +import scorex.util.encode.Base16 +import sigma.Extensions.CollBytesOps +import sigma.{Coll, Colls} + /** Type-class of isomorphisms between types. * Isomorphism between two types `A` and `B` essentially say that both types * represents the same information (entity) but in a different way. @@ -27,3 +31,22 @@ final case class ComposeIso[A, B, C](iso2: Iso[B, C], iso1: Iso[A, B]) extends I def to(a: A): C = iso2.to(iso1.to(a)) } +object Iso { + implicit def identityIso[A]: Iso[A, A] = new Iso[A, A] { + override def to(a: A): A = a + override def from(b: A): A = b + } + + implicit def inverseIso[A, B](implicit iso: Iso[A, B]): Iso[B, A] = InverseIso[A, B](iso) + + val isoStringToArray: Iso[String, Array[Byte]] = new Iso[String, Array[Byte]] { + override def to(x: String): Array[Byte] = Base16.decode(x).get + override def from(x: Array[Byte]): String = Base16.encode(x) + } + + val isoStringToColl: Iso[String, Coll[Byte]] = new Iso[String, Coll[Byte]] { + override def to(x: String): Coll[Byte] = Colls.fromArray(Base16.decode(x).get) + override def from(x: Coll[Byte]): String = x.toHex + } + +} \ No newline at end of file diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index f8f0afd02b..2a61d73261 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -40,7 +40,7 @@ object Extensions { @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(x)) } - /** Extension methods for `Coll[Byte]` not available for generic `Array[T]`. */ + /** 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. */ @inline def toTokenId: TokenId = Digest32Coll @@ Colls.fromArray(arr) diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index 8dd45e0063..ec446fbb72 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -6,7 +6,8 @@ import org.scalablytyped.runtime.StringDictionary import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel -import org.ergoplatform.sdk.js.{ErgoTree, Value} +import org.ergoplatform.sdk.js.ErgoTree +import sigma.js.Value import sigmastate.Values import sigmastate.eval.CompiletimeIRContext import sigmastate.lang.Terms.ValueOps diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/AvlTree.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/AvlTree.scala deleted file mode 100644 index f1cf64377c..0000000000 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/AvlTree.scala +++ /dev/null @@ -1,16 +0,0 @@ -package org.ergoplatform.sdk.js - -import scala.scalajs.js -import scala.scalajs.js.UndefOr -import scala.scalajs.js.annotation.JSExportTopLevel - -/** Equivalent of [[sigma.AvlTree]] available from JS. */ -@JSExportTopLevel("AvlTree") -class AvlTree( - val digest: String, - val insertAllowed: Boolean, - val updateAllowed: Boolean, - val removeAllowed: Boolean, - val keyLength: Int, - val valueLengthOpt: UndefOr[Int] -) extends js.Object diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala index 8469545d95..2a433c411d 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala @@ -1,5 +1,6 @@ package org.ergoplatform.sdk.js +import sigma.js.Value import sigmastate.Values import scala.scalajs.js diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Header.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Header.scala index 4d10c9f3dd..ef53e13dbd 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Header.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Header.scala @@ -1,5 +1,7 @@ package org.ergoplatform.sdk.js +import sigma.js.{AvlTree, GroupElement} + import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index 3bdda049bf..ec306e9240 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -1,17 +1,20 @@ package org.ergoplatform.sdk.js import org.ergoplatform.ErgoBox._ +import org.ergoplatform._ +import org.ergoplatform.sdk.ExtendedInputBox import org.ergoplatform.sdk.JavaHelpers.UniversalConverter -import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.ergoplatform.sdk.wallet.protocol.context -import org.ergoplatform._ -import org.ergoplatform.sdk.Iso.inverseIso -import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree, CBigInt, CGroupElement, Iso, RType} import scorex.crypto.authds.ADKey import scorex.util.ModifierId import scorex.util.encode.Base16 +import sigma.Extensions.CollBytesOps +import sigma.ast.SType +import sigma.data.Iso.{isoStringToArray, isoStringToColl} +import sigma.data.{CBigInt, CGroupElement, Iso} +import sigma.js.{AvlTree, GroupElement, Type, Value} +import sigma.{Coll, Colls, Evaluation} import sigmastate.Values.{Constant, GroupElementConstant} -import sigma.Extensions.ArrayOps import sigmastate.eval.{CHeader, CPreHeader, Digest32Coll} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString @@ -21,16 +24,10 @@ import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{SignedTransaction, import sigmastate.fleetSdkCommon.{distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesProverResultMod => proverResultMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod} import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} -import sigma.{Coll, Colls, Evaluation} -import sigma.Extensions.CollBytesOps -import sigma.ast.SType -import sigma.js.Type import java.math.BigInteger import scala.collection.immutable.ListMap -import scala.reflect.ClassTag import scala.scalajs.js -import scala.scalajs.js.JSConverters.JSRichOption import scala.scalajs.js.Object /** Definitions of isomorphisms. */ @@ -47,16 +44,6 @@ object Isos { } } - val isoStringToArray: Iso[String, Array[Byte]] = new Iso[String, Array[Byte]] { - override def to(x: String): Array[Byte] = Base16.decode(x).get - override def from(x: Array[Byte]): String = Base16.encode(x) - } - - val isoStringToColl: Iso[String, Coll[Byte]] = new Iso[String, Coll[Byte]] { - override def to(x: String): Coll[Byte] = Colls.fromArray(Base16.decode(x).get) - override def from(x: Coll[Byte]): String = x.toHex - } - val isoStringToGroupElement: Iso[String, sigma.GroupElement] = new Iso[String, sigma.GroupElement] { override def to(x: String): sigma.GroupElement = { val bytes = Base16.decode(x).get @@ -95,31 +82,6 @@ object Isos { } } - implicit val isoAvlTree: Iso[AvlTree, sigma.AvlTree] = new Iso[AvlTree, sigma.AvlTree] { - override def to(x: AvlTree): sigma.AvlTree = { - CAvlTree( - AvlTreeData( - digest = isoStringToArray.to(x.digest).toColl, - treeFlags = AvlTreeFlags(x.insertAllowed, x.updateAllowed, x.removeAllowed), - x.keyLength, - valueLengthOpt = isoUndefOr(Iso.identityIso[Int]).to(x.valueLengthOpt), - ), - ) - } - override def from(x: sigma.AvlTree): AvlTree = { - val tree = x.asInstanceOf[CAvlTree] - val data = tree.treeData - new AvlTree( - digest = isoStringToColl.from(tree.digest), - insertAllowed = data.treeFlags.insertAllowed, - updateAllowed = data.treeFlags.updateAllowed, - removeAllowed = data.treeFlags.removeAllowed, - keyLength = data.keyLength, - valueLengthOpt = isoUndefOr(Iso.identityIso[Int]).from(data.valueLengthOpt), - ) - } - } - implicit val isoHeader: Iso[Header, sigma.Header] = new Iso[Header, sigma.Header] { override def to(a: Header): sigma.Header = { CHeader( @@ -127,7 +89,7 @@ object Isos { version = a.version, parentId = isoStringToColl.to(a.parentId), ADProofsRoot = isoStringToColl.to(a.ADProofsRoot), - stateRoot = isoAvlTree.to(a.stateRoot), + stateRoot = AvlTree.isoAvlTree.to(a.stateRoot), transactionsRoot = isoStringToColl.to(a.transactionsRoot), timestamp = isoBigIntToLong.to(a.timestamp), nBits = isoBigIntToLong.to(a.nBits), @@ -147,7 +109,7 @@ object Isos { version = header.version, parentId = isoStringToColl.from(header.parentId), ADProofsRoot = isoStringToColl.from(header.ADProofsRoot), - stateRoot = isoAvlTree.from(header.stateRoot), + stateRoot = AvlTree.isoAvlTree.from(header.stateRoot), transactionsRoot = isoStringToColl.from(header.transactionsRoot), timestamp = isoBigIntToLong.from(header.timestamp), nBits = isoBigIntToLong.from(header.nBits), @@ -199,8 +161,8 @@ object Isos { dataInputCost = a.dataInputCost, outputCost = a.outputCost, maxBlockCost = a.maxBlockCost, - softForkStartingHeight = Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkStartingHeight), - softForkVotesCollected = Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkVotesCollected), + softForkStartingHeight = sigma.js.Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkStartingHeight), + softForkVotesCollected = sigma.js.Isos.isoUndefOr[Int, Int](Iso.identityIso).to(a.softForkVotesCollected), blockVersion = a.blockVersion ) } @@ -214,8 +176,8 @@ object Isos { dataInputCost = b.dataInputCost, outputCost = b.outputCost, maxBlockCost = b.maxBlockCost, - softForkStartingHeight = Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkStartingHeight), - softForkVotesCollected = Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkVotesCollected), + softForkStartingHeight = sigma.js.Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkStartingHeight), + softForkVotesCollected = sigma.js.Isos.isoUndefOr[Int, Int](Iso.identityIso).from(b.softForkVotesCollected), blockVersion = b.blockVersion ) } @@ -224,7 +186,7 @@ object Isos { implicit val isoBlockchainStateContext: Iso[BlockchainStateContext, context.BlockchainStateContext] = new Iso[BlockchainStateContext, context.BlockchainStateContext] { override def to(a: BlockchainStateContext): context.BlockchainStateContext = { context.BlockchainStateContext( - sigmaLastHeaders = isoArrayToColl(isoHeader).to(a.sigmaLastHeaders), + sigmaLastHeaders = sigma.js.Isos.isoArrayToColl(isoHeader).to(a.sigmaLastHeaders), previousStateDigest = isoStringToColl.to(a.previousStateDigest), sigmaPreHeader = isoPreHeader.to(a.sigmaPreHeader) ) @@ -232,7 +194,7 @@ object Isos { override def from(b: context.BlockchainStateContext): BlockchainStateContext = { new BlockchainStateContext( - sigmaLastHeaders = isoArrayToColl(isoHeader).from(b.sigmaLastHeaders), + sigmaLastHeaders = sigma.js.Isos.isoArrayToColl(isoHeader).from(b.sigmaLastHeaders), previousStateDigest = isoStringToColl.from(b.previousStateDigest), sigmaPreHeader = isoPreHeader.from(b.sigmaPreHeader) ) @@ -330,28 +292,13 @@ object Isos { tokenMod.TokenAmount[commonMod.Amount](isoAmount.from(x._2), x._1.toHex) } - implicit def isoUndefOr[A, B](implicit iso: Iso[A, B]): Iso[js.UndefOr[A], Option[B]] = new Iso[js.UndefOr[A], Option[B]] { - override def to(x: js.UndefOr[A]): Option[B] = x.toOption.map(iso.to) - override def from(x: Option[B]): js.UndefOr[A] = x.map(iso.from).orUndefined - } - - implicit def isoArrayToColl[A, B](iso: Iso[A, B])(implicit ctA: ClassTag[A], tB: RType[B]): Iso[js.Array[A], Coll[B]] = new Iso[js.Array[A], Coll[B]] { - override def to(x: js.Array[A]): Coll[B] = Colls.fromArray(x.map(iso.to).toArray(tB.classTag)) - override def from(x: Coll[B]): js.Array[A] = js.Array(x.toArray.map(iso.from):_*) - } - - implicit def isoArrayToIndexed[A, B](iso: Iso[A, B])(implicit cB: ClassTag[B]): Iso[js.Array[A], IndexedSeq[B]] = new Iso[js.Array[A], IndexedSeq[B]] { - override def to(x: js.Array[A]): IndexedSeq[B] = x.map(iso.to).toArray(cB).toIndexedSeq - override def from(x: IndexedSeq[B]): js.Array[A] = js.Array(x.map(iso.from):_*) - } - val isoTokenArray: Iso[js.Array[tokenMod.TokenAmount[commonMod.Amount]], Coll[Token]] = new Iso[js.Array[tokenMod.TokenAmount[commonMod.Amount]], Coll[Token]] { override def to(x: js.Array[tokenMod.TokenAmount[commonMod.Amount]]): Coll[Token] = { - isoArrayToColl(isoToken).to(x) + sigma.js.Isos.isoArrayToColl(isoToken).to(x) } override def from(x: Coll[Token]): js.Array[tokenMod.TokenAmount[commonMod.Amount]] = { - isoArrayToColl(isoToken).from(x) + sigma.js.Isos.isoArrayToColl(isoToken).from(x) } } @@ -486,17 +433,17 @@ object Isos { new Iso[UnsignedTransaction, UnsignedErgoLikeTransaction] { override def to(a: UnsignedTransaction): UnsignedErgoLikeTransaction = { new UnsignedErgoLikeTransaction( - inputs = isoArrayToIndexed(isoUnsignedInput).to(a.inputs), - dataInputs = isoArrayToIndexed(isoDataInput).to(a.dataInputs), - outputCandidates = isoArrayToIndexed(isoBoxCandidate).to(a.outputs), + inputs = sigma.js.Isos.isoArrayToIndexed(isoUnsignedInput).to(a.inputs), + dataInputs = sigma.js.Isos.isoArrayToIndexed(isoDataInput).to(a.dataInputs), + outputCandidates = sigma.js.Isos.isoArrayToIndexed(isoBoxCandidate).to(a.outputs), ) } override def from(b: UnsignedErgoLikeTransaction): UnsignedTransaction = { UnsignedTransaction( - inputs = isoArrayToIndexed(isoUnsignedInput).from(b.inputs), - dataInputs = isoArrayToIndexed(isoDataInput).from(b.dataInputs), - outputs = isoArrayToIndexed(isoBoxCandidate).from(b.outputCandidates) + inputs = sigma.js.Isos.isoArrayToIndexed(isoUnsignedInput).from(b.inputs), + dataInputs = sigma.js.Isos.isoArrayToIndexed(isoDataInput).from(b.dataInputs), + outputs = sigma.js.Isos.isoArrayToIndexed(isoBoxCandidate).from(b.outputCandidates) ) } } @@ -505,16 +452,16 @@ object Isos { new Iso[SignedTransaction, ErgoLikeTransaction] { override def to(a: SignedTransaction): ErgoLikeTransaction = { new ErgoLikeTransaction( - inputs = isoArrayToIndexed(isoSignedInput).to(a.inputs), - dataInputs = isoArrayToIndexed(isoDataInput).to(a.dataInputs), - outputCandidates = isoArrayToIndexed(isoBox).to(a.outputs), + inputs = sigma.js.Isos.isoArrayToIndexed(isoSignedInput).to(a.inputs), + dataInputs = sigma.js.Isos.isoArrayToIndexed(isoDataInput).to(a.dataInputs), + outputCandidates = sigma.js.Isos.isoArrayToIndexed(isoBox).to(a.outputs), ) } override def from(tx: ErgoLikeTransaction): SignedTransaction = { - val inputs = isoArrayToIndexed(isoSignedInput).from(tx.inputs) - val dataInputs = isoArrayToIndexed(isoDataInput).from(tx.dataInputs) - val outputs = isoArrayToIndexed(isoBox).from(tx.outputs) + val inputs = sigma.js.Isos.isoArrayToIndexed(isoSignedInput).from(tx.inputs) + val dataInputs = sigma.js.Isos.isoArrayToIndexed(isoDataInput).from(tx.dataInputs) + val outputs = sigma.js.Isos.isoArrayToIndexed(isoBox).from(tx.outputs) SignedTransaction(dataInputs, tx.id, inputs, outputs) } } diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/PreHeader.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/PreHeader.scala index 08a8ace0d9..c2c0c993bf 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/PreHeader.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/PreHeader.scala @@ -1,5 +1,7 @@ package org.ergoplatform.sdk.js +import sigma.js.GroupElement + import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala index 8d3b3cd2e3..485f2fbf88 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ProverBuilder.scala @@ -6,6 +6,7 @@ import org.ergoplatform.sdk.SecretString import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel import Isos._ +import sigma.js.GroupElement import sigmastate.eval.SigmaDsl /** Equivalent of [[sdk.ProverBuilder]] available from JS. diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala index b7ca33b1de..e52ae39ef6 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/SigmaProver.scala @@ -50,9 +50,9 @@ class SigmaProver(_prover: sdk.SigmaProver) extends js.Object { baseCost: Int): ReducedTransaction = { val unreducedTx = sdk.UnreducedTransaction( unsignedTx = isoUnsignedTransaction.to(unsignedTx), - boxesToSpend = isoArrayToIndexed(isoEIP12UnsignedInput).to(boxesToSpend), - dataInputs = isoArrayToIndexed(isoBox).to(dataInputs), - tokensToBurn = isoArrayToIndexed(isoToken.andThen(sdk.Iso.isoErgoTokenToPair.inverse)).to(tokensToBurn) + boxesToSpend = sigma.js.Isos.isoArrayToIndexed(isoEIP12UnsignedInput).to(boxesToSpend), + dataInputs = sigma.js.Isos.isoArrayToIndexed(isoBox).to(dataInputs), + tokensToBurn = sigma.js.Isos.isoArrayToIndexed(isoToken.andThen(sdk.SdkIsos.isoErgoTokenToPair.inverse)).to(tokensToBurn) ) val ctx = isoBlockchainStateContext.to(stateCtx) val reducedTx = _prover.reduce(ctx, unreducedTx, baseCost) diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala index 503f9130ac..53c93fd1ca 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala @@ -2,18 +2,19 @@ package org.ergoplatform.sdk.js import org.ergoplatform.ErgoBox.{AdditionalRegisters, BoxId, TokenId} import org.ergoplatform._ +import org.ergoplatform.sdk.ExtendedInputBox import org.ergoplatform.sdk.wallet.protocol.context.BlockchainStateContext -import org.ergoplatform.sdk.{ExtendedInputBox, Iso} import org.scalacheck.{Arbitrary, Gen} import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import sigma.ast.SType import sigma.data.Iso +import sigma.js.AvlTree +import sigma.{Coll, Colls, GroupElement} import sigmastate.Values.Constant import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.generators.ObjectGenerators -import sigma.{Coll, Colls, GroupElement} import scala.scalajs.js @@ -42,13 +43,13 @@ class IsosSpec extends AnyPropSpec with Matchers with ObjectGenerators with Sca property("Iso.isoStringToArray") { forAll() { (bytes: Array[Byte]) => - roundtrip(Isos.isoStringToArray)(bytes) + roundtrip(Iso.isoStringToArray)(bytes) } } property("Iso.isoStringToColl") { forAll() { (bytes: Coll[Byte]) => - roundtrip(Isos.isoStringToColl)(bytes) + roundtrip(Iso.isoStringToColl)(bytes) } } @@ -72,7 +73,7 @@ class IsosSpec extends AnyPropSpec with Matchers with ObjectGenerators with Sca property("Iso.avlTree") { forAll { (c: sigma.AvlTree) => - roundtrip(Isos.isoAvlTree)(c) + roundtrip(AvlTree.isoAvlTree)(c) } } @@ -157,7 +158,7 @@ class IsosSpec extends AnyPropSpec with Matchers with ObjectGenerators with Sca property("Iso.isoUndefOr") { forAll { opt: Option[Long] => - roundtrip(Isos.isoUndefOr(Iso.identityIso[Long]))(opt) + roundtrip(sigma.js.Isos.isoUndefOr(Iso.identityIso[Long]))(opt) } } diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala index c6f64ab319..fffecbf9db 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/ValueSpec.scala @@ -13,6 +13,7 @@ import sigmastate.serialization.ConstantSerializer import sigmastate.utils.Helpers import sigma.SigmaTestingData import sigma.data.{CSigmaProp, ProveDlog} +import sigma.js.Value import java.math.BigInteger @@ -60,7 +61,8 @@ class ValueSpec extends AnyPropSpec with Matchers with SigmaTestingData with Sca test(AvlTreeConstant(TestData.t3), "643100d2e101ff01fc047c7f6f00ff80129df69a5090012f01ffca99f5bfff0c803601800100") } - property("Box toHex()/fromHex()") { + // TODO turn on when Value.fromHex is implemented for Box + ignore("Box toHex()/fromHex()") { test(BoxConstant(TestData.b2), "63b96000d1968302010100ff83020193040204020100c0843d000401010e32297000800b80f1d56c809a8c6affbed864b87f007f6f007f00ac00018c01c4fdff011088807f0100657f00f9ab0101ff6d6505a4a7b5a2e7a4a4dd3a05feffffffffffffffff01003bd5c630803cfff6c1ff7f7fb980ff136afc011f8080b8b04ad4dbda2d7f4e01") } diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala index c5890cbf4c..9b09c487f3 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/AppkitProvingInterpreter.scala @@ -39,7 +39,7 @@ class AppkitProvingInterpreter( extends ReducingInterpreter(params) with ProverInterpreter { override type CTX = ErgoLikeContext - import org.ergoplatform.sdk.Iso._ + import org.ergoplatform.sdk.SdkIsos._ /** All secrets available to this interpreter including [[ExtendedSecretKey]], dlog and * dht secrets. diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index 55a01286b4..ced9e907fe 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -33,15 +33,7 @@ import scala.collection.{JavaConverters, mutable} trait LowPriorityIsos { } -object Iso extends LowPriorityIsos { - implicit def identityIso[A]: Iso[A, A] = new Iso[A, A] { - override def to(a: A): A = a - - override def from(b: A): A = b - } - - implicit def inverseIso[A,B](implicit iso: Iso[A,B]): Iso[B,A] = InverseIso[A,B](iso) - +object SdkIsos extends LowPriorityIsos { implicit val jbyteToByte: Iso[JByte, Byte] = new Iso[JByte, Byte] { override def to(b: JByte): Byte = b override def from(a: Byte): JByte = a diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala index 50afb74e5b..7244867d6d 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala @@ -69,7 +69,7 @@ object OutBoxBuilder { require(nRegs <= nonMandatoryRegisters.length, s"Too many additional registers $nRegs. Max allowed ${nonMandatoryRegisters.length}") implicit val TokenIdRType: RType[TokenId] = collRType(sigma.ByteType).asInstanceOf[RType[TokenId]] - val ts = Colls.fromItems(tokens.map(Iso.isoErgoTokenToPair.to(_)): _*) + val ts = Colls.fromItems(tokens.map(SdkIsos.isoErgoTokenToPair.to(_)): _*) val rs = registers.zipWithIndex.map { case (c, i) => val id = ErgoBox.nonMandatoryRegisters(i) id -> c.asInstanceOf[EvaluatedValue[_ <: SType]] diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala index 3aa1458e3f..1448a2b4b2 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala @@ -21,7 +21,7 @@ import scala.collection.mutable /** Interpreter that can reduce transactions with given chain parameters. */ class ReducingInterpreter(params: BlockchainParameters) extends ErgoLikeInterpreter { override type CTX = ErgoLikeContext - import org.ergoplatform.sdk.Iso._ + import org.ergoplatform.sdk.SdkIsos._ /** Reduces the given ErgoTree in the given context to the sigma proposition. * diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala index cffc96267a..f5768638e0 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala @@ -87,7 +87,7 @@ class UnsignedTransactionBuilder(val ctx: BlockchainContext) { val changeAddress = getDefined(_changeAddress, "Change address is not defined") val inputBoxesSeq = boxesToSpend.map(eb => eb.box) val requestedToBurn = _tokensToBurn.fold(IndexedSeq.empty[ErgoToken])(_.toIndexedSeq) - val burnTokens = Iso.isoErgoTokenSeqToLinkedMap.to(requestedToBurn).toMap + val burnTokens = SdkIsos.isoErgoTokenSeqToLinkedMap.to(requestedToBurn).toMap val rewardDelay = ctx.networkType match { case NetworkType.Mainnet => BlockchainParameters.MinerRewardDelay_Mainnet case NetworkType.Testnet => BlockchainParameters.MinerRewardDelay_Testnet diff --git a/sigma-js/tests/js/Value.spec.js b/sigma-js/tests/js/Value.spec.js index e687f8aecb..7ff5a62f78 100644 --- a/sigma-js/tests/js/Value.spec.js +++ b/sigma-js/tests/js/Value.spec.js @@ -91,10 +91,11 @@ describe("Smoke tests for Values", () => { expect(v.toHex()).toEqual(avlTreeHex) }); - it("Box Value.toHex", () => { - let v = ValueObj.fromHex(boxHex) - expect(v.toHex()).toEqual(boxHex) - }); + // TODO uncomment when Box is implemented + // ignore("Box Value.toHex", () => { + // let v = ValueObj.fromHex(boxHex) + // expect(v.toHex()).toEqual(boxHex) + // }); it("Coll Value.toHex", () => { let arr = [ [1, 2, 3], [10, 20] ] From feebe24b59bf69ed435749c88253b1c7a59f07aa Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 10:11:30 +0200 Subject: [PATCH 25/34] core-serializers2: renamed CostingBox -> CBox --- .../sigmastate/eval/CostingDataContext.scala | 10 +++++----- .../scala/sigmastate/eval/Extensions.scala | 2 +- .../sigmastate/lang/SigmaBuilderTest.scala | 4 ++-- .../generators/ObjectGenerators.scala | 2 +- .../scala/special/sigma/ContractsTestkit.scala | 2 +- .../scala/special/sigma/SigmaTestingData.scala | 6 +++--- .../sigmastate/helpers/SigmaPPrintSpec.scala | 4 ++-- .../scala/sigma/SigmaDslSpecification.scala | 18 +++++++++--------- .../src/test/scala/sigma/SigmaDslTesting.scala | 4 ++-- .../sigmastate/ErgoTreeSpecification.scala | 4 ++-- .../ScriptVersionSwitchSpecification.scala | 2 +- 11 files changed, 29 insertions(+), 29 deletions(-) diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index 1935bc105f..d37e3bc71b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -71,12 +71,12 @@ object CAnyValue { new CAnyValue(value, t.asInstanceOf[RType[Any]]) } -import sigmastate.eval.CostingBox._ +import sigmastate.eval.CBox._ /** A default implementation of [[Box]] interface. * @see [[Box]] for detailed descriptions */ -case class CostingBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { +case class CBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { val builder = CostingSigmaDslBuilder val value = ebox.value @@ -131,7 +131,7 @@ case class CostingBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { }) } -object CostingBox { +object CBox { import Evaluation._ @@ -299,10 +299,10 @@ class CostingSigmaDslBuilder extends SigmaDslBuilder { dsl => * @param ebox the value to be wrapped * @see [[sigmastate.SBox]], [[sigma.Box]] */ - def Box(ebox: ErgoBox): Box = CostingBox(ebox) + def Box(ebox: ErgoBox): Box = CBox(ebox) /** Extracts [[ErgoBox]] from the given [[Box]] instance. This is inverse to the Box method. */ - def toErgoBox(b: Box): ErgoBox = b.asInstanceOf[CostingBox].ebox + def toErgoBox(b: Box): ErgoBox = b.asInstanceOf[CBox].ebox /** HOTSPOT: don't beautify this code */ private def toSigmaTrees(props: Array[SigmaProp]): Array[SigmaBoolean] = { diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 2a61d73261..beb5fdc583 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -103,7 +103,7 @@ object Extensions { Safety: used in ErgoLikeContext where boxes cannot be null Examined ergo code: all that leads to ErgoLikeContext creation. */ - CostingBox(ebox) + CBox(ebox) } } diff --git a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala index ba5a016142..ad8eea574d 100644 --- a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala +++ b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala @@ -8,7 +8,7 @@ import sigma.{Environment, VersionContext} import sigmastate.Values._ import sigmastate._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CAnyValue, CostingBox, SigmaDsl} +import sigmastate.eval.{CAnyValue, CBox, SigmaDsl} import sigmastate.exceptions.ConstraintFailed import sigmastate.serialization.OpCodes import sigma.SigmaTestingData @@ -208,7 +208,7 @@ class SigmaBuilderTest extends AnyPropSpec with ScalaCheckPropertyChecks with Ma } property("liftToConstant ErgoBox") { - val v = TestData.b2.asInstanceOf[CostingBox].wrappedValue + val v = TestData.b2.asInstanceOf[CBox].wrappedValue val c = BoxConstant(TestData.b2) testSuccess(v, c) // TODO v6.0: ErgoBox should not be liftable directly (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905) testFailure(Array.fill(10)(v)) diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 3b58f4c3dc..17ba63ef5b 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -377,7 +377,7 @@ trait ObjectGenerators extends TypeGenerators creationHeight <- heightGen } yield new ErgoBoxCandidate(l, b, creationHeight, tokens, ar) - lazy val boxConstantGen: Gen[BoxConstant] = ergoBoxGen.map { v => BoxConstant(CostingBox(v)) } + lazy val boxConstantGen: Gen[BoxConstant] = ergoBoxGen.map { v => BoxConstant(CBox(v)) } lazy val digest32Gen: Gen[TokenId] = for { bytes <- collOfN(TokenId.size, arbByte.arbitrary) diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 793104bb42..223b916bb8 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -65,7 +65,7 @@ trait ContractsTestkit { val ergoBox = testBox(value, ErgoTree.fromProposition(Values.TrueSigmaProp), creationHeight = 0, additionalTokens = Seq(), additionalRegisters = Map()) - new CostingBox(ergoBox) + new CBox(ergoBox) } def testContext( diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index a5374456b5..29a7a00919 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -13,7 +13,7 @@ import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigmastate.eval._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CHeader, CPreHeader, CostingBox, CostingSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CHeader, CPreHeader, CBox, CostingSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators @@ -206,7 +206,7 @@ trait SigmaTestingData extends TestingCommons with ObjectGenerators { ) ) - val b1_instances = new CloneSet(1000, CostingBox( + val b1_instances = new CloneSet(1000, CBox( new ErgoBox( 9223372036854775807L, new ErgoTree( @@ -244,7 +244,7 @@ trait SigmaTestingData extends TestingCommons with ObjectGenerators { val b1: Box = create_b1() - val b2: Box = CostingBox( + val b2: Box = CBox( new ErgoBox( 12345L, new ErgoTree( diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index ef800a58e0..96153685a9 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -99,7 +99,7 @@ class SigmaPPrintSpec extends SigmaDslTesting { | Right(BoolToSigmaProp(TrueLeaf)) |)""".stripMargin) test( - CostingBox( + CBox( new ErgoBox( 9223372036854775807L, new ErgoTree(0.toByte, Vector(), Right(BoolToSigmaProp(FalseLeaf))), @@ -112,7 +112,7 @@ class SigmaPPrintSpec extends SigmaDslTesting { 1000000 ) ), - """CostingBox( + """CBox( | new ErgoBox( | 9223372036854775807L, | new ErgoTree(0.toByte, Vector(), Right(BoolToSigmaProp(FalseLeaf))), diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index 80362036dd..c2b967b025 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -2339,7 +2339,7 @@ class SigmaDslSpecification extends SigmaDslTesting property("NEQ of pre-defined types") { verifyNeq(ge1, ge2, 1783, Array[CostItem](FixedCostItem(NamedDesc("EQ_GroupElement"), FixedCost(JitCost(172)))), 1783)(_.asInstanceOf[CGroupElement].copy()) verifyNeq(t1, t2, 1767, Array[CostItem](FixedCostItem(NamedDesc("EQ_AvlTree"), FixedCost(JitCost(6)))), 1767)(_.asInstanceOf[CAvlTree].copy()) - verifyNeq(b1, b2, 1767, Array[CostItem](), 1767)(_.asInstanceOf[CostingBox].copy()) + verifyNeq(b1, b2, 1767, Array[CostItem](), 1767)(_.asInstanceOf[CBox].copy()) verifyNeq(preH1, preH2, 1766, Array[CostItem](FixedCostItem(NamedDesc("EQ_PreHeader"), FixedCost(JitCost(4)))), 1766)(_.asInstanceOf[CPreHeader].copy()) verifyNeq(h1, h2, 1767, Array[CostItem](FixedCostItem(NamedDesc("EQ_Header"), FixedCost(JitCost(6)))), 1767)(_.asInstanceOf[CHeader].copy()) } @@ -4593,7 +4593,7 @@ class SigmaDslSpecification extends SigmaDslTesting } def contextData() = { - val input = CostingBox( + val input = CBox( new ErgoBox( 80946L, new ErgoTree( @@ -4623,7 +4623,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) - val dataBox = CostingBox( + val dataBox = CBox( new ErgoBox( -1L, new ErgoTree( @@ -4689,7 +4689,7 @@ class SigmaDslSpecification extends SigmaDslTesting ), inputs = Coll[Box](input), outputs = Coll[Box]( - CostingBox( + CBox( new ErgoBox( 1000000L, new ErgoTree( @@ -4726,7 +4726,7 @@ class SigmaDslSpecification extends SigmaDslTesting 11 ) ), - CostingBox( + CBox( new ErgoBox( 2769336982721999022L, new ErgoTree( @@ -4771,7 +4771,7 @@ class SigmaDslSpecification extends SigmaDslTesting def ctxWithRegsInOutput(ctx: CostingDataContext, regs: AdditionalRegisters) = { ctx.copy( outputs = Coll({ - val box = ctx.outputs(0).asInstanceOf[CostingBox] + val box = ctx.outputs(0).asInstanceOf[CBox] box.copy(ebox = copyBox(box.ebox)(additionalRegisters = regs)) }) ) @@ -4780,7 +4780,7 @@ class SigmaDslSpecification extends SigmaDslTesting def ctxWithRegsInDataInput(ctx: CostingDataContext, regs: AdditionalRegisters) = { ctx.copy( _dataInputs = Coll({ - val box = ctx.dataInputs(0).asInstanceOf[CostingBox] + val box = ctx.dataInputs(0).asInstanceOf[CBox] box.copy(ebox = copyBox(box.ebox)(additionalRegisters = regs)) }) ) @@ -6558,7 +6558,7 @@ class SigmaDslSpecification extends SigmaDslTesting def sampleCollBoxes = genSamples[Coll[Box]](collOfN[Box](5, arbitrary[Box]), MinSuccessful(20)) - def create_b1 = CostingBox( + def create_b1 = CBox( new ErgoBox( 1L, new ErgoTree( @@ -6594,7 +6594,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) ) - def create_b2 = CostingBox( + def create_b2 = CBox( new ErgoBox( 1000000000L, new ErgoTree( diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index e077975018..e93a975045 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -23,7 +23,7 @@ import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, I import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput import sigmastate.eval.Extensions.SigmaBooleanOps -import sigmastate.eval.{CompiletimeIRContext, CostingBox, CostingDataContext, IRContext, SigmaDsl} +import sigmastate.eval.{CompiletimeIRContext, CBox, CostingDataContext, IRContext, SigmaDsl} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, SigmaPPrint} import sigmastate.interpreter.Interpreter.{ScriptEnv, VerificationResult} @@ -345,7 +345,7 @@ class SigmaDslTesting extends AnyPropSpec // the context is passed as function argument (see func in the script) // Since Context is singleton, we should use this instance as the basis // for execution of verify instead of a new dummy context. - val self = ctx.selfBox.asInstanceOf[CostingBox] + val self = ctx.selfBox.asInstanceOf[CBox] val newSelf = self.copy( ebox = updatedRegisters(self.ebox, newRegisters) ) diff --git a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala index 259ffc3c15..0a6ea200ce 100644 --- a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala @@ -9,7 +9,7 @@ import sigma.{Evaluation, VersionContext} import sigma.ast.SCollection.SByteArray import sigmastate.Values._ import sigma.VersionContext._ -import sigmastate.eval.{CostingBox, Profiler} +import sigmastate.eval.{CBox, Profiler} import sigmastate.exceptions.{CostLimitException, InterpreterException} import sigmastate.helpers.{ErgoLikeContextTesting, SigmaPPrint} import sigmastate.interpreter.{ErgoTreeEvaluator, EvalSettings} @@ -691,7 +691,7 @@ class ErgoTreeSpecification extends SigmaDslTesting with ContractsTestkit { ) def createCtx: ErgoLikeContext = ErgoLikeContextTesting - .dummy(CostingBox(fakeSelf), VersionContext.current.activatedVersion) + .dummy(CBox(fakeSelf), VersionContext.current.activatedVersion) .withErgoTreeVersion(tree.version) VersionContext.withVersions(activatedVersion = 1, tree.version) { diff --git a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala index 77a18188c1..9e4d9c2c1e 100644 --- a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala @@ -32,7 +32,7 @@ class ScriptVersionSwitchSpecification extends SigmaDslTesting { implicit def IR: IRContext = createIR() - lazy val b1 = CostingBox( + lazy val b1 = CBox( new ErgoBox( 1L, new ErgoTree( From d503dbec105694f1e606aaa669740a6c84b7dd25 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 11:04:49 +0200 Subject: [PATCH 26/34] core-serializers2: renamed CostingSigmaDslBuilder -> CSigmaDslBuilder --- .../src/main/scala/org/ergoplatform/ErgoLikeContext.scala | 4 ++-- .../main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala | 4 ++-- .../src/main/scala/sigmastate/eval/BigIntegerOps.scala | 4 ++-- .../main/scala/sigmastate/eval/CostingDataContext.scala | 8 ++++---- .../src/main/scala/sigmastate/eval/Extensions.scala | 8 ++++---- .../shared/src/main/scala/sigmastate/eval/package.scala | 2 +- .../src/test/scala/sigmastate/eval/BasicOpsTests.scala | 2 +- .../scala/sigmastate/helpers/ErgoLikeContextTesting.scala | 2 +- .../test/scala/sigmastate/helpers/TestingHelpers.scala | 2 +- .../src/test/scala/special/sigma/ContractsTestkit.scala | 4 ++-- .../src/test/scala/special/sigma/SigmaTestingData.scala | 4 ++-- .../shared/src/test/scala/sigmastate/lang/LangTests.scala | 8 ++++---- .../main/scala/org/ergoplatform/dsl/ContractSpec.scala | 4 ++-- .../main/scala/org/ergoplatform/dsl/ContractSyntax.scala | 4 ++-- .../src/main/scala/sigmastate/eval/GraphBuilding.scala | 2 +- sc/shared/src/main/scala/sigmastate/eval/IRContext.scala | 2 +- .../src/test/scala/sigmastate/TypesSpecification.scala | 6 +++--- .../src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala | 4 ++-- .../src/main/scala/org/ergoplatform/sdk/SigmaProver.scala | 4 ++-- .../test/scala/org/ergoplatform/sdk/ExtensionsSpec.scala | 4 ++-- 20 files changed, 41 insertions(+), 41 deletions(-) diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index d1d6090f04..16cf77fdde 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -148,7 +148,7 @@ class ErgoLikeContext(val lastBlockUtxoRoot: AvlTreeData, for ((id, v) <- m) { res(id) = v } - CostingSigmaDslBuilder.Colls.fromArray(res) + CSigmaDslBuilder.Colls.fromArray(res) } val dataInputs = this.dataBoxes.toArray.map(_.toTestBox).toColl @@ -359,6 +359,6 @@ case object Global extends NotReadyValue[SGlobal.type] with FixedCostValueCompan override val opType: SFunc = SFunc(SUnit, SGlobal) protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = { addCost(this.costKind) - CostingSigmaDslBuilder + CSigmaDslBuilder } } diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala index 4896ad61e2..69fea7f015 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/dsl/AvlTreeHelpers.scala @@ -1,12 +1,12 @@ package org.ergoplatform.dsl import sigma.Coll -import sigmastate.eval.CostingSigmaDslBuilder +import sigmastate.eval.CSigmaDslBuilder import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.AvlTree import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert} -import CostingSigmaDslBuilder.Colls +import CSigmaDslBuilder.Colls import sigma.data.{AvlTreeData, AvlTreeFlags, CAvlTree} object AvlTreeHelpers { diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/BigIntegerOps.scala b/interpreter/shared/src/main/scala/sigmastate/eval/BigIntegerOps.scala index 1a465e67e9..55d0d0e8f7 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/BigIntegerOps.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/BigIntegerOps.scala @@ -47,8 +47,8 @@ object NumericOps { def fromInt(x: Int): BigInt = x.toBigInt def toInt(x: BigInt): Int = x.toInt def toLong(x: BigInt): Long = x.toLong - def toFloat(x: BigInt): Float = CostingSigmaDslBuilder.toBigInteger(x).floatValue() - def toDouble(x: BigInt): Double = CostingSigmaDslBuilder.toBigInteger(x).doubleValue() + def toFloat(x: BigInt): Float = CSigmaDslBuilder.toBigInteger(x).floatValue() + def toDouble(x: BigInt): Double = CSigmaDslBuilder.toBigInteger(x).doubleValue() } /** The instance of Integral for BigInt. diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala index d37e3bc71b..dcb3bd4fd7 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala @@ -77,7 +77,7 @@ import sigmastate.eval.CBox._ * @see [[Box]] for detailed descriptions */ case class CBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { - val builder = CostingSigmaDslBuilder + val builder = CSigmaDslBuilder val value = ebox.value lazy val id: Coll[Byte] = Colls.fromArray(ebox.id) @@ -255,7 +255,7 @@ object CHeader { /** A default implementation of [[SigmaDslBuilder]] interface. * @see [[SigmaDslBuilder]] for detailed descriptions */ -class CostingSigmaDslBuilder extends SigmaDslBuilder { dsl => +class CSigmaDslBuilder extends SigmaDslBuilder { dsl => implicit val validationSettings: SigmaValidationSettings = ValidationRules.currentSettings override val Colls: CollBuilder = sigma.Colls @@ -428,7 +428,7 @@ class CostingSigmaDslBuilder extends SigmaDslBuilder { dsl => } /** Default singleton instance of Global object, which implements global ErgoTree functions. */ -object CostingSigmaDslBuilder extends CostingSigmaDslBuilder +object CSigmaDslBuilder extends CSigmaDslBuilder /** A default implementation of [[Context]] interface. * @see [[Context]] for detailed descriptions @@ -449,7 +449,7 @@ case class CostingDataContext( override val currentErgoTreeVersion: Byte ) extends Context { - @inline override def builder: SigmaDslBuilder = CostingSigmaDslBuilder + @inline override def builder: SigmaDslBuilder = CSigmaDslBuilder @inline override def HEIGHT: Int = height diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index beb5fdc583..9ec065942b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -23,21 +23,21 @@ import scala.util.{Failure, Success} object Extensions { implicit class ByteExt(val b: Byte) extends AnyVal { - @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong)) + @inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong)) } implicit class ShortExt(val b: Short) extends AnyVal { - @inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong)) + @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 = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(x.toLong)) + @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 = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(x)) + @inline def toBigInt: BigInt = CSigmaDslBuilder.BigInt(BigInteger.valueOf(x)) } /** Extension methods for `Array[Byte]` not available for generic `Array[T]`. */ diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index e0cad6ab19..9c086fe102 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -17,7 +17,7 @@ package object eval { * For example methods like `Box`, `toErgoBox` are available here, but not available in Dsl. * @see SigmaDslBuilder */ - val SigmaDsl = CostingSigmaDslBuilder + val SigmaDsl = CSigmaDslBuilder trait BaseDigestColl extends TaggedType[Coll[Byte]] diff --git a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala index 7a1887a384..8dcfe7d9c8 100644 --- a/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala +++ b/interpreter/shared/src/test/scala/sigmastate/eval/BasicOpsTests.scala @@ -12,7 +12,7 @@ import sigma.{ContractsTestkit, SigmaDslBuilder, SigmaProp} import scala.language.implicitConversions class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers { - override val SigmaDsl: SigmaDslBuilder = CostingSigmaDslBuilder + override val SigmaDsl: SigmaDslBuilder = CSigmaDslBuilder implicit def boolToSigma(b: Boolean): SigmaProp = TrivialProp(b).toSigmaProp diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala index eac35d5ba0..1e0a22874a 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ErgoLikeContextTesting.scala @@ -25,7 +25,7 @@ object ErgoLikeContextTesting { val dummyPubkey: Array[Byte] = GroupElementSerializer.toBytes(CryptoConstants.dlogGroup.generator) val noBoxes: IndexedSeq[ErgoBox] = IndexedSeq.empty[ErgoBox] - val noHeaders: Coll[Header] = CostingSigmaDslBuilder.Colls.emptyColl[Header] + val noHeaders: Coll[Header] = CSigmaDslBuilder.Colls.emptyColl[Header] def dummyPreHeader(currentHeight: Height, minerPk: Array[Byte]): PreHeader = CPreHeader(0, parentId = Colls.emptyColl[Byte], diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala index c3e9c2e920..8e4a459c2c 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala @@ -26,7 +26,7 @@ object TestingHelpers { transactionId: ModifierId = allZerosModifierId, boxIndex: Short = 0): ErgoBox = new ErgoBox(value, ergoTree, - CostingSigmaDslBuilder.Colls.fromArray(additionalTokens.toArray[Token]), + CSigmaDslBuilder.Colls.fromArray(additionalTokens.toArray[Token]), additionalRegisters, transactionId, boxIndex, creationHeight) diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 223b916bb8..7348cf3251 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -23,7 +23,7 @@ trait ContractsTestkit { val R8 = 8.toByte; val R9 = 9.toByte; val Colls = new CollOverArrayBuilder - val SigmaDsl: SigmaDslBuilder = CostingSigmaDslBuilder + val SigmaDsl: SigmaDslBuilder = CSigmaDslBuilder val noRegisters = collection[AnyValue]() val noBytes = collection[Byte]() val noInputs = Array[Box]() @@ -31,7 +31,7 @@ trait ContractsTestkit { val dummyPubkey: Array[Byte] = Array.fill(32)(0: Byte) val dummyADDigest: Coll[Byte] = Colls.fromArray(Array.fill(33)(0: Byte)) val emptyAvlTree = new CAvlTree(AvlTreeData.dummy) - val noHeaders = CostingSigmaDslBuilder.Colls.emptyColl[Header] + val noHeaders = CSigmaDslBuilder.Colls.emptyColl[Header] val dummyPreHeader: PreHeader = null /** Create collection from array of items */ diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 29a7a00919..32efb16770 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -13,7 +13,7 @@ import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigmastate.eval._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CHeader, CPreHeader, CBox, CostingSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CHeader, CPreHeader, CBox, CSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators @@ -28,7 +28,7 @@ import scala.reflect.ClassTag trait SigmaTestingData extends TestingCommons with ObjectGenerators { /** Creates a [[sigma.Coll]] with the given `items`. */ def Coll[T](items: T*)(implicit cT: RType[T]): Coll[T] = - CostingSigmaDslBuilder.Colls.fromItems(items: _*) + CSigmaDslBuilder.Colls.fromItems(items: _*) /** Generator of random collection with `n` elements. */ def collOfN[T: RType : Arbitrary](n: Int) diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index 7f5094b0c9..55e3759089 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -39,10 +39,10 @@ trait LangTests extends Matchers with NegativeTesting { val ecp2 = dlog.multiplyGroupElements(ecp1, ecp1) val ecp3 = dlog.multiplyGroupElements(ecp2, ecp2) val ecp4 = dlog.multiplyGroupElements(ecp3, ecp3) - val g1 = CostingSigmaDslBuilder.GroupElement(ecp1) - val g2 = CostingSigmaDslBuilder.GroupElement(ecp2) - val g3 = CostingSigmaDslBuilder.GroupElement(ecp3) - val g4 = CostingSigmaDslBuilder.GroupElement(ecp4) + val g1 = CSigmaDslBuilder.GroupElement(ecp1) + val g2 = CSigmaDslBuilder.GroupElement(ecp2) + val g3 = CSigmaDslBuilder.GroupElement(ecp3) + val g4 = CSigmaDslBuilder.GroupElement(ecp4) protected val n1: BigInt = BigInt(10).underlying().toBigInt protected val n2: BigInt = BigInt(20).underlying().toBigInt diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala index 5950b8fcdb..23fb1754ac 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala @@ -6,7 +6,7 @@ import sigma.data.RType import org.ergoplatform.{ErgoBox, ErgoLikeContext} import sigma.{AnyValue, Coll, SigmaDslBuilder, SigmaProp} import sigmastate.Values.ErgoTree -import sigmastate.eval.{CostingSigmaDslBuilder, IRContext} +import sigmastate.eval.{CSigmaDslBuilder, IRContext} import scala.util.Try import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} @@ -14,7 +14,7 @@ import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import scala.language.implicitConversions trait ContractSpec { - val dsl: SigmaDslBuilder = CostingSigmaDslBuilder + val dsl: SigmaDslBuilder = CSigmaDslBuilder val Colls = dsl.Colls implicit def Coll[T](items: Array[T])(implicit cT: RType[T]): Coll[T] = Colls.fromArray(items) diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala index 61046b22d0..b01914eed6 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSyntax.scala @@ -5,7 +5,7 @@ import org.ergoplatform.ErgoBox.TokenId import sigma.data.{AvlTreeData, RType, SigmaBoolean} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition} import org.ergoplatform.sdk.JavaHelpers.collRType -import sigmastate.eval.CostingSigmaDslBuilder +import sigmastate.eval.CSigmaDslBuilder import sigmastate.interpreter.Interpreter.ScriptEnv import sigma._ import sigma.ast.SType @@ -15,7 +15,7 @@ import scala.util.Try /** Defines methods to be used in contract implementations based on [[SigmaContract]]. */ trait ContractSyntax { contract: SigmaContract => - override def builder: SigmaDslBuilder = CostingSigmaDslBuilder + override def builder: SigmaDslBuilder = CSigmaDslBuilder /** Instance of contract specification DSL, which can be imported in the body of * [[SigmaContract]] implementations. */ diff --git a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala index 974629ff8a..521e1ca905 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/GraphBuilding.scala @@ -397,7 +397,7 @@ trait GraphBuilding extends SigmaLibrary { IR: IRContext => import sigmastate._ - protected implicit def groupElementToECPoint(g: sigma.GroupElement): EcPointType = CostingSigmaDslBuilder.toECPoint(g).asInstanceOf[EcPointType] + protected implicit def groupElementToECPoint(g: sigma.GroupElement): EcPointType = CSigmaDslBuilder.toECPoint(g).asInstanceOf[EcPointType] def error(msg: String) = throw new GraphBuildingException(msg, None) def error(msg: String, srcCtx: Option[SourceContext]) = throw new GraphBuildingException(msg, srcCtx) diff --git a/sc/shared/src/main/scala/sigmastate/eval/IRContext.scala b/sc/shared/src/main/scala/sigmastate/eval/IRContext.scala index a3338eaae6..5a588633b9 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/IRContext.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/IRContext.scala @@ -28,7 +28,7 @@ trait IRContext extends TreeBuilding with GraphBuilding { Pass.defaultPassConfig.copy(constantPropagation = false)) /** The value of Global ErgoTree operation */ - val sigmaDslBuilderValue = CostingSigmaDslBuilder + val sigmaDslBuilderValue = CSigmaDslBuilder /** Finds SigmaProp.isProven method calls in the given Lambda `f` */ def findIsProven[T](f: Ref[Context => T]): Option[Sym] = { diff --git a/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala b/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala index 9e4e784d25..befafbe3de 100644 --- a/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/TypesSpecification.scala @@ -2,7 +2,7 @@ package sigmastate import sigma.Environment import sigma.ast.SType.isValueOfType -import sigmastate.eval.CostingSigmaDslBuilder +import sigmastate.eval.CSigmaDslBuilder import sigma.SigmaTestingData import sigma.ast._ import sigma.data.CSigmaProp @@ -98,8 +98,8 @@ class TypesSpecification extends SigmaTestingData { assertValidType(t1, SAvlTree) assertInvalidType(t1, SShort) - assertValidType(CostingSigmaDslBuilder, SGlobal) - assertInvalidType(CostingSigmaDslBuilder, SShort) + assertValidType(CSigmaDslBuilder, SGlobal) + assertInvalidType(CSigmaDslBuilder, SShort) assertValidType(h1, SHeader) assertInvalidType(h1, SShort) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index ced9e907fe..c562133126 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -21,7 +21,7 @@ import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DiffieHellmanTupleProverInput -import sigmastate.eval.{CostingSigmaDslBuilder, Digest32Coll} +import sigmastate.eval.{CSigmaDslBuilder, Digest32Coll} import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} import java.lang.{Boolean => JBoolean, Byte => JByte, Integer => JInt, Long => JLong, Short => JShort, String => JString} @@ -332,7 +332,7 @@ object JavaHelpers { def BoxRType: RType[sigma.Box] = sigma.BoxRType - def SigmaDsl: CostingSigmaDslBuilder = sigmastate.eval.SigmaDsl + def SigmaDsl: CSigmaDslBuilder = sigmastate.eval.SigmaDsl def collFrom(arr: Array[Byte]): Coll[Byte] = { Colls.fromArray(arr) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/SigmaProver.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/SigmaProver.scala index aa281f5b10..3d1b4f97b9 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/SigmaProver.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/SigmaProver.scala @@ -3,7 +3,7 @@ package org.ergoplatform.sdk import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix import org.ergoplatform._ import org.ergoplatform.sdk.wallet.protocol.context.BlockchainStateContext -import sigmastate.eval.{CostingSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CSigmaDslBuilder, SigmaDsl} import sigmastate.interpreter.HintsBag import sigmastate.utils.Helpers.TryOps import sigma.{BigInt, SigmaProp} @@ -27,7 +27,7 @@ class SigmaProver(_prover: AppkitProvingInterpreter, networkPrefix: NetworkPrefi /** Returns the prover's secret key. */ def getSecretKey: BigInt = - CostingSigmaDslBuilder.BigInt(_prover.secretKeys(0).privateInput.w) + CSigmaDslBuilder.BigInt(_prover.secretKeys(0).privateInput.w) /** Returns a sequence of EIP-3 addresses associated with the prover's secret keys. */ def getEip3Addresses: Seq[P2PKAddress] = { diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/ExtensionsSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/ExtensionsSpec.scala index f250b9fce5..a272f00cd4 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/ExtensionsSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/ExtensionsSpec.scala @@ -6,11 +6,11 @@ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import sigma.{Coll, CollGens} import org.ergoplatform.sdk.Extensions.{CollBuilderOps, CollOps, GenIterableOps, PairCollOps} import sigma.data.RType -import sigmastate.eval.CostingSigmaDslBuilder +import sigmastate.eval.CSigmaDslBuilder class ExtensionsSpec extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers with CollGens { def Coll[T](items: T*)(implicit cT: RType[T]) = - CostingSigmaDslBuilder.Colls.fromItems(items: _*) + CSigmaDslBuilder.Colls.fromItems(items: _*) val items: Iterable[(Int, String)] = Array((1, "a"), (2, "b"), (1, "c")) From 094beeb7796be3be1e0a3001a282ccf337762ba5 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 11:12:08 +0200 Subject: [PATCH 27/34] core-serializers2: renamed CostingDataContext -> CContext --- .../org/ergoplatform/ErgoLikeContext.scala | 2 +- ...ostingDataContext.scala => CContext.scala} | 33 +++++++++---------- .../special/sigma/ContractsTestkit.scala | 6 ++-- .../scala/sigma/SigmaDslSpecification.scala | 6 ++-- .../test/scala/sigma/SigmaDslTesting.scala | 8 ++--- .../helpers/CompilerTestingCommons.scala | 4 +-- 6 files changed, 29 insertions(+), 30 deletions(-) rename interpreter/shared/src/main/scala/sigmastate/eval/{CostingDataContext.scala => CContext.scala} (95%) diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index 16cf77fdde..d28afd59e9 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -171,7 +171,7 @@ class ErgoLikeContext(val lastBlockUtxoRoot: AvlTreeData, val selfBox = boxesToSpend(selfIndex).toTestBox val ergoTreeVersion = currentErgoTreeVersion.getOrElse( Interpreter.error(s"Undefined context property: currentErgoTreeVersion")) - CostingDataContext( + CContext( dataInputs, headers, preHeader, inputs, outputs, preHeader.height, selfBox, selfIndex, avlTree, preHeader.minerPk.getEncoded, vars, activatedScriptVersion, ergoTreeVersion) } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala similarity index 95% rename from interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala rename to interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala index dcb3bd4fd7..d87082a99b 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CostingDataContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala @@ -433,22 +433,21 @@ object CSigmaDslBuilder extends CSigmaDslBuilder /** A default implementation of [[Context]] interface. * @see [[Context]] for detailed descriptions */ -case class CostingDataContext( - _dataInputs: Coll[Box], - override val headers: Coll[Header], - override val preHeader: PreHeader, - inputs: Coll[Box], - outputs: Coll[Box], - height: Int, - selfBox: Box, - private val selfIndex: Int, - lastBlockUtxoRootHash: AvlTree, - _minerPubKey: Coll[Byte], - vars: Coll[AnyValue], - override val activatedScriptVersion: Byte, - override val currentErgoTreeVersion: Byte - ) - extends Context { +case class CContext( + _dataInputs: Coll[Box], + override val headers: Coll[Header], + override val preHeader: PreHeader, + inputs: Coll[Box], + outputs: Coll[Box], + height: Int, + selfBox: Box, + private val selfIndex: Int, + lastBlockUtxoRootHash: AvlTree, + _minerPubKey: Coll[Byte], + vars: Coll[AnyValue], + override val activatedScriptVersion: Byte, + override val currentErgoTreeVersion: Byte +) extends Context { @inline override def builder: SigmaDslBuilder = CSigmaDslBuilder @inline override def HEIGHT: Int = height @@ -496,7 +495,7 @@ case class CostingDataContext( * @return a new instance (if `bindings` non-empty) with the specified bindings. * other existing bindings are copied to the new instance */ - def withUpdatedVars(bindings: (Int, AnyValue)*): CostingDataContext = { + def withUpdatedVars(bindings: (Int, AnyValue)*): CContext = { if (bindings.isEmpty) return this val ids = bindings.map(_._1).toArray diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 7348cf3251..343d5bfc42 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -72,7 +72,7 @@ trait ContractsTestkit { inputs: Array[Box], outputs: Array[Box], height: Int, self: Box, tree: AvlTree, minerPk: Array[Byte], activatedScriptVersion: Byte, currErgoTreeVersion: Byte, vars: Array[AnyValue]) = - new CostingDataContext( + new CContext( noInputs.toColl, noHeaders, dummyPreHeader, inputs.toColl, outputs.toColl, height, self, inputs.indexOf(self), tree, minerPk.toColl, vars.toColl, activatedScriptVersion, currErgoTreeVersion) @@ -82,13 +82,13 @@ trait ContractsTestkit { self: Box, activatedScriptVersion: Byte, currErgoTreeVersion: Byte, - vars: AnyValue*): CostingDataContext = { + vars: AnyValue*): CContext = { testContext( noInputs, noOutputs, height, self, emptyAvlTree, dummyPubkey, activatedScriptVersion, currErgoTreeVersion, vars.toArray) } - implicit class TestContextOps(ctx: CostingDataContext) { + implicit class TestContextOps(ctx: CContext) { def withInputs(inputs: Box*) = ctx.copy(inputs = inputs.toArray.toColl) def withOutputs(outputs: Box*) = ctx.copy(outputs = outputs.toArray.toColl) diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index c2b967b025..cd71651504 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -4675,7 +4675,7 @@ class SigmaDslSpecification extends SigmaDslTesting Helpers.decodeBytes("01ff13") ) - val ctx = CostingDataContext( + val ctx = CContext( _dataInputs = Coll[Box](dataBox), headers = Coll[Header](header), preHeader = CPreHeader( @@ -4768,7 +4768,7 @@ class SigmaDslSpecification extends SigmaDslTesting (input, dataBox, header, ctx, ctx2, ctx3) } - def ctxWithRegsInOutput(ctx: CostingDataContext, regs: AdditionalRegisters) = { + def ctxWithRegsInOutput(ctx: CContext, regs: AdditionalRegisters) = { ctx.copy( outputs = Coll({ val box = ctx.outputs(0).asInstanceOf[CBox] @@ -4777,7 +4777,7 @@ class SigmaDslSpecification extends SigmaDslTesting ) } - def ctxWithRegsInDataInput(ctx: CostingDataContext, regs: AdditionalRegisters) = { + def ctxWithRegsInDataInput(ctx: CContext, regs: AdditionalRegisters) = { ctx.copy( _dataInputs = Coll({ val box = ctx.dataInputs(0).asInstanceOf[CBox] diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index e93a975045..3ab02ebcad 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -23,7 +23,7 @@ import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, I import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput import sigmastate.eval.Extensions.SigmaBooleanOps -import sigmastate.eval.{CompiletimeIRContext, CBox, CostingDataContext, IRContext, SigmaDsl} +import sigmastate.eval.{CompiletimeIRContext, CBox, CContext, IRContext, SigmaDsl} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, SigmaPPrint} import sigmastate.interpreter.Interpreter.{ScriptEnv, VerificationResult} @@ -257,11 +257,11 @@ class SigmaDslTesting extends AnyPropSpec } } - /** Creates a new ErgoLikeContext using given [[CostingDataContext]] as template. + /** Creates a new ErgoLikeContext using given [[CContext]] as template. * Copies most of the data from ctx and the missing data is taken from the args. * This is a helper method to be used in tests only. */ - def createErgoLikeContext(ctx: CostingDataContext, + def createErgoLikeContext(ctx: CContext, validationSettings: SigmaValidationSettings, costLimit: Long, initCost: Long @@ -341,7 +341,7 @@ class SigmaDslTesting extends AnyPropSpec ) val ctx = input match { - case ctx: CostingDataContext => + case ctx: CContext => // the context is passed as function argument (see func in the script) // Since Context is singleton, we should use this instance as the basis // for execution of verify instead of a new dummy context. diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 98e241dfcb..76cc532937 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -50,7 +50,7 @@ trait CompilerTestingCommons extends TestingCommons def createContexts[A](in: A, bindings: Seq[VarBinding])(implicit tA: RType[A]) = { val tpeA = Evaluation.rtypeToSType(tA) in match { - case ctx: CostingDataContext => + case ctx: CContext => // the context is passed as function argument (this is for testing only) // This is to overcome non-functional semantics of context operations // (such as Inputs, Height, etc which don't have arguments and refer to the @@ -85,7 +85,7 @@ trait CompilerTestingCommons extends TestingCommons .withErgoTreeVersion(ergoTreeVersionInTests) .withBindings(1.toByte -> Constant[SType](in.asInstanceOf[SType#WrappedType], tpeA)) .withBindings(bindings: _*) - val calcCtx = ergoCtx.toSigmaContext().asInstanceOf[CostingDataContext] + val calcCtx = ergoCtx.toSigmaContext().asInstanceOf[CContext] calcCtx } } From 3f854333ccd55f5975711770ffe8c7435153f7bf Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 11:28:24 +0200 Subject: [PATCH 28/34] core-serializers2: moved CAnyValue to core --- .../src/main/scala/sigma/data/CAnyValue.scala | 18 ++++ .../src/main/scala/sigmastate/eval/CBox.scala | 95 ++++++++++++++++++ .../main/scala/sigmastate/eval/CContext.scala | 97 ------------------- .../scala/sigmastate/eval/Extensions.scala | 2 +- .../main/scala/sigmastate/utils/Helpers.scala | 4 +- .../sigmastate/lang/SigmaBuilderTest.scala | 4 +- .../scala/sigmastate/lang/LangTests.scala | 2 +- .../ergoplatform/dsl/TestContractSpec.scala | 4 +- .../sigmastate/eval/ExampleContracts.scala | 1 + .../utxo/BasicOpsSpecification.scala | 4 +- .../utxo/SigmaCompilerSpecification.scala | 2 +- .../ergoplatform/sdk/DataJsonEncoder.scala | 2 +- .../sdk/DataJsonEncoderSpecification.scala | 2 +- 13 files changed, 127 insertions(+), 110 deletions(-) create mode 100644 core/shared/src/main/scala/sigma/data/CAnyValue.scala create mode 100644 interpreter/shared/src/main/scala/sigmastate/eval/CBox.scala diff --git a/core/shared/src/main/scala/sigma/data/CAnyValue.scala b/core/shared/src/main/scala/sigma/data/CAnyValue.scala new file mode 100644 index 0000000000..9c10dfbbe2 --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/CAnyValue.scala @@ -0,0 +1,18 @@ +package sigma.data + +import sigma.AnyValue +import sigma.data.OverloadHack.Overloaded1 + +import scala.annotation.unused + +/** Default implementation of AnyValue interface. */ +case class CAnyValue[A](value: A, tVal: RType[Any]) extends AnyValue { + def tA: RType[A] = tVal.asInstanceOf[RType[A]] + + override def toString = s"TestValue($value)" +} + +object CAnyValue { + def apply[A](value: A)(implicit t: RType[A], @unused o: Overloaded1): CAnyValue[A] = + new CAnyValue(value, t.asInstanceOf[RType[Any]]) +} \ No newline at end of file diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CBox.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CBox.scala new file mode 100644 index 0000000000..e544faea31 --- /dev/null +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CBox.scala @@ -0,0 +1,95 @@ +package sigmastate.eval + +import org.ergoplatform.ErgoBox +import scorex.utils.Ints +import sigma.Evaluation.stypeToRType +import sigma.ast.SCollection.SByteArray +import sigma.ast.{SInt, STuple, SType} +import sigma.data.{CAnyValue, RType, WrapperOf} +import sigma.{AnyValue, Box, Coll, Colls} +import sigmastate.Values.{ConstantNode, EvaluatedValue, SValue} +import sigmastate.eval.CBox.regs +import sigmastate.eval.Extensions.toAnyValue + +import java.util.Arrays + +/** A default implementation of [[Box]] interface. + * + * @see [[Box]] for detailed descriptions + */ +case class CBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { + val builder = CSigmaDslBuilder + + val value = ebox.value + lazy val id : Coll[Byte] = Colls.fromArray(ebox.id) + lazy val bytes : Coll[Byte] = Colls.fromArray(ebox.bytes) + lazy val bytesWithoutRef : Coll[Byte] = Colls.fromArray(ebox.bytesWithNoRef) + lazy val propositionBytes: Coll[Byte] = Colls.fromArray(ebox.propositionBytes) + lazy val registers : Coll[AnyValue] = regs(ebox) + + override def wrappedValue: ErgoBox = ebox + + override def getReg[T](i: Int)(implicit tT: RType[T]): Option[T] = { + if (i < 0 || i >= registers.length) return None + val value = registers(i) + if (value != null) { + // once the value is not null it should be of the right type + value match { + case value: CAnyValue[_] if value.value != null && value.tA == tT => + Some(value.value.asInstanceOf[T]) + case _ => + throw new InvalidType(s"Cannot getReg[${tT.name}]($i): invalid type of value $value at id=$i") + } + } else None + } + + override def creationInfo: (Int, Coll[Byte]) = { + this.getReg[(Int, Coll[Byte])](3).get.asInstanceOf[Any] match { + case info: Tuple2[Int, Coll[Byte]]@unchecked => info + case ConstantNode(arr: Array[Any], STuple(IndexedSeq(SInt, SByteArray))) if arr.length == 2 => + (arr(0).asInstanceOf[Int], builder.Colls.fromArray(arr(1).asInstanceOf[Array[Byte]])) + case v => + sys.error(s"Invalid value $v of creationInfo register R3") + } + } + + override def tokens: Coll[(Coll[Byte], Long)] = { + this.getReg[Coll[(Coll[Byte], Long)]](ErgoBox.R2.asIndex).get + } + + override def executeFromRegister[T](regId: Byte) + (implicit cT: RType[T]): T = ??? // TODO implement + + override def hashCode(): Int = Ints.fromByteArray(id.toArray) + + override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj != null && { + obj match { + case obj: Box => Arrays.equals(id.toArray, obj.id.toArray) + case _ => + // this case was missing in v4.x, however has never been a problem + // Thus, v5.0 interpreter will not fail (while v4.x would fail here) + false + } + }) +} + +object CBox { + def regs(ebox: ErgoBox): Coll[AnyValue] = { + val res = new Array[AnyValue](ErgoBox.maxRegisters) + + def checkNotYetDefined(id: Int, newValue: SValue) = + require(res(id) == null, s"register $id is defined more then once: previous value ${res(id)}, new value $newValue") + + for ( (k, v: EvaluatedValue[t]) <- ebox.additionalRegisters ) { + checkNotYetDefined(k.number, v) + res(k.number) = toAnyValue(v.value)(stypeToRType(v.tpe)) + } + for ( r <- ErgoBox.mandatoryRegisters ) { + val regId = r.number + val v = ebox.get(r).get.asInstanceOf[EvaluatedValue[SType]] + checkNotYetDefined(regId, v) + res(regId) = toAnyValue(v.value)(stypeToRType(v.tpe)) + } + Colls.fromArray(res) + } +} \ No newline at end of file diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala index d87082a99b..a89ff4b4d4 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CContext.scala @@ -60,103 +60,6 @@ object AvlTreeVerifier { } } -/** Default implementation of AnyValue interface. */ -case class CAnyValue[A](value: A, tVal: RType[Any]) extends AnyValue { - def tA: RType[A] = tVal.asInstanceOf[RType[A]] - override def toString = s"TestValue($value)" -} - -object CAnyValue { - def apply[A](value: A)(implicit t: RType[A], @unused o: Overloaded1): CAnyValue[A] = - new CAnyValue(value, t.asInstanceOf[RType[Any]]) -} - -import sigmastate.eval.CBox._ - -/** A default implementation of [[Box]] interface. - * @see [[Box]] for detailed descriptions - */ -case class CBox(ebox: ErgoBox) extends Box with WrapperOf[ErgoBox] { - val builder = CSigmaDslBuilder - - val value = ebox.value - lazy val id: Coll[Byte] = Colls.fromArray(ebox.id) - lazy val bytes: Coll[Byte] = Colls.fromArray(ebox.bytes) - lazy val bytesWithoutRef: Coll[Byte] = Colls.fromArray(ebox.bytesWithNoRef) - lazy val propositionBytes: Coll[Byte] = Colls.fromArray(ebox.propositionBytes) - lazy val registers: Coll[AnyValue] = regs(ebox) - - override def wrappedValue: ErgoBox = ebox - - override def getReg[T](i: Int)(implicit tT: RType[T]): Option[T] = { - if (i < 0 || i >= registers.length) return None - val value = registers(i) - if (value != null ) { - // once the value is not null it should be of the right type - value match { - case value: CAnyValue[_] if value.value != null && value.tA == tT => - Some(value.value.asInstanceOf[T]) - case _ => - throw new InvalidType(s"Cannot getReg[${tT.name}]($i): invalid type of value $value at id=$i") - } - } else None - } - - override def creationInfo: (Int, Coll[Byte]) = { - this.getReg[(Int, Coll[Byte])](3).get.asInstanceOf[Any] match { - case info: Tuple2[Int, Coll[Byte]]@unchecked => info - case ConstantNode(arr: Array[Any], STuple(IndexedSeq(SInt, SByteArray))) if arr.length == 2 => - (arr(0).asInstanceOf[Int], builder.Colls.fromArray(arr(1).asInstanceOf[Array[Byte]])) - case v => - sys.error(s"Invalid value $v of creationInfo register R3") - } - } - - override def tokens: Coll[(Coll[Byte], Long)] = { - this.getReg[Coll[(Coll[Byte], Long)]](ErgoBox.R2.asIndex).get - } - - override def executeFromRegister[T](regId: Byte)(implicit cT: RType[T]): T = ??? // TODO implement - - override def hashCode(): Int = Ints.fromByteArray(id.toArray) - - override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj != null && { - obj match { - case obj: Box => Arrays.equals(id.toArray, obj.id.toArray) - case _ => - // this case was missing in v4.x, however has never been a problem - // Thus, v5.0 interpreter will not fail (while v4.x would fail here) - false - } - }) -} - -object CBox { - - import Evaluation._ - - def regs(ebox: ErgoBox): Coll[AnyValue] = { - val res = new Array[AnyValue](ErgoBox.maxRegisters) - - def checkNotYetDefined(id: Int, newValue: SValue) = - require(res(id) == null, s"register $id is defined more then once: previous value ${res(id)}, new value $newValue") - - for ((k, v: EvaluatedValue[t]) <- ebox.additionalRegisters) { - checkNotYetDefined(k.number, v) - res(k.number) = toAnyValue(v.value)(stypeToRType(v.tpe)) - } - - for (r <- ErgoBox.mandatoryRegisters) { - val regId = r.number - val v = ebox.get(r).get.asInstanceOf[EvaluatedValue[SType]] - checkNotYetDefined(regId, v) - res(regId) = toAnyValue(v.value)(stypeToRType(v.tpe)) - } - Colls.fromArray(res) - } - -} - /** This class represents context variable and register value of a functional type A => B. * When variable or register is accessed using `getVar[A => B](id).get` or * `box.getReg[A => B].get an instance of this class is returned. diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index 9ec065942b..f8a58964e8 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -8,7 +8,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.util.encode.Base16 import sigma.ast.SType.AnyOps import sigma.ast.{SBoolean, SCollection, SCollectionType, SType} -import sigma.data.{Nullable, RType, SigmaBoolean} +import sigma.data.{CAnyValue, Nullable, RType, SigmaBoolean} import sigma.{Coll, _} import sigmastate.Platform import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala b/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala index 2f3f057204..1e867cff28 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/Helpers.scala @@ -3,11 +3,11 @@ package sigmastate.utils import debox.cfor import io.circe.Decoder import org.ergoplatform.settings.ErgoAlgos -import sigma.data.{OverloadHack, RType} +import sigma.data.{CAnyValue, OverloadHack, RType} import scorex.utils.Ints import sigma.crypto.EcPointType import sigma.{Coll, Colls, Environment, GroupElement} -import sigmastate.eval.{CAnyValue, SigmaDsl} +import sigmastate.eval.SigmaDsl import java.util import java.util.concurrent.locks.Lock diff --git a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala index ad8eea574d..95ee94c680 100644 --- a/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala +++ b/interpreter/shared/src/test/scala/sigmastate/lang/SigmaBuilderTest.scala @@ -3,12 +3,12 @@ package sigmastate.lang import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks -import sigma.data.{CAvlTree, Nullable, RType} +import sigma.data.{CAnyValue, CAvlTree, Nullable, RType} import sigma.{Environment, VersionContext} import sigmastate.Values._ import sigmastate._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CAnyValue, CBox, SigmaDsl} +import sigmastate.eval.{CBox, SigmaDsl} import sigmastate.exceptions.ConstraintFailed import sigmastate.serialization.OpCodes import sigma.SigmaTestingData diff --git a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala index 55e3759089..ee3d046753 100644 --- a/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala +++ b/parsers/shared/src/test/scala/sigmastate/lang/LangTests.scala @@ -14,7 +14,7 @@ import sigmastate.eval._ import sigmastate.helpers.NegativeTesting import sigma.Coll import sigma.ast._ -import sigma.data.{ProveDHTuple, ProveDlog, SigmaBoolean} +import sigma.data.{CAnyValue, ProveDHTuple, ProveDlog, SigmaBoolean} import sigma.util.Extensions.BigIntegerOps trait LangTests extends Matchers with NegativeTesting { diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index 0b26809e3b..90bf013059 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -7,14 +7,14 @@ import sigmastate.interpreter.{CostedProverResult, ProverResult} import scala.collection.mutable.ArrayBuffer import org.ergoplatform.ErgoBox.{NonMandatoryRegisterId, TokenId} -import sigma.data.{AvlTreeData, CSigmaProp, Nullable} +import sigma.data.{AvlTreeData, CAnyValue, CSigmaProp, Nullable} import scala.util.Try import org.ergoplatform.{ErgoBox, ErgoLikeContext} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import sigma.ast.SType import sigmastate.Values.{ErgoTree, EvaluatedValue} -import sigmastate.eval.{CAnyValue, IRContext} +import sigmastate.eval.IRContext import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps diff --git a/sc/shared/src/test/scala/sigmastate/eval/ExampleContracts.scala b/sc/shared/src/test/scala/sigmastate/eval/ExampleContracts.scala index 913a82167f..ac9e6aac07 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/ExampleContracts.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/ExampleContracts.scala @@ -1,6 +1,7 @@ package sigmastate.eval import scalan.BaseCtxTests +import sigma.data.CAnyValue trait ExampleContracts extends ErgoScriptTestkit { self: BaseCtxTests => diff --git a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala index 3d35322a38..1a236fa31a 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/BasicOpsSpecification.scala @@ -6,12 +6,12 @@ import sigma.Extensions.ArrayOps import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps import sigma.ast._ -import sigma.data.AvlTreeData +import sigma.data.{AvlTreeData, CAnyValue} import sigma.util.StringUtil._ import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.CryptoConstants -import sigmastate.eval.{CAnyValue, InvalidType} +import sigmastate.eval.InvalidType import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.ContextExtension.VarBinding diff --git a/sc/shared/src/test/scala/sigmastate/utxo/SigmaCompilerSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/SigmaCompilerSpecification.scala index 07565bbff9..8809e22836 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/SigmaCompilerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/SigmaCompilerSpecification.scala @@ -1,8 +1,8 @@ package sigmastate.utxo import sigma.ast.SType +import sigma.data.CAnyValue import sigmastate.Values._ -import sigmastate.eval.CAnyValue import sigmastate.helpers.CompilerTestingCommons import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.lang.Terms._ diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala index a384129ec3..fef67a2704 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/DataJsonEncoder.scala @@ -6,7 +6,7 @@ import io.circe.syntax._ import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.{NonMandatoryRegisterId, Token} import org.ergoplatform.settings.ErgoAlgos -import sigma.data.RType +import sigma.data.{CAnyValue, RType} import scorex.util._ import sigmastate.Values.{Constant, EvaluatedValue} import sigmastate.lang.SigmaParser diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala index f3afb17553..0b35fd2462 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/DataJsonEncoderSpecification.scala @@ -4,7 +4,7 @@ package org.ergoplatform.sdk import java.math.BigInteger import org.scalacheck.Arbitrary._ import org.scalacheck.Gen -import sigma.data.{RType, SigmaBoolean, TupleColl} +import sigma.data.{CAnyValue, RType, SigmaBoolean, TupleColl} import sigma.ast._ import sigma.ast.SCollection.SByteArray import sigma.ast.SType.AnyOps From 2ddc80dfa56680da12fc7eb98958b86b82365cec Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 16:17:22 +0200 Subject: [PATCH 29/34] core-serializers2: moved Digest32Coll to core --- core/shared/src/main/scala/sigma/data/package.scala | 9 +++++++++ .../shared/src/main/scala/org/ergoplatform/ErgoBox.scala | 3 +-- .../main/scala/org/ergoplatform/ErgoBoxCandidate.scala | 2 +- .../scala/org/ergoplatform/ErgoLikeTransaction.scala | 1 + .../src/main/scala/sigmastate/eval/Extensions.scala | 2 +- .../shared/src/main/scala/sigmastate/eval/package.scala | 7 ------- .../src/main/scala/sigmastate/utils/Extensions.scala | 3 ++- .../serialization/generators/ObjectGenerators.scala | 2 +- .../src/test/scala/special/sigma/SigmaTestingData.scala | 5 ++--- .../test/scala/sigmastate/helpers/SigmaPPrintSpec.scala | 2 +- .../scala/org/ergoplatform/ErgoLikeTransactionSpec.scala | 2 +- .../test/scala/org/ergoplatform/ErgoTreePredefSpec.scala | 3 +-- .../utxo/examples/AssetsAtomicExchangeErgoTests.scala | 2 +- .../utxo/examples/AssetsAtomicExchangeTests.scala | 3 +-- .../test/scala/sigmastate/utxo/examples/IcoExample.scala | 2 +- .../sigmastate/utxo/examples/LetsSpecification.scala | 4 ++-- .../examples/OracleDataInputsExamplesSpecification.scala | 2 +- .../utxo/examples/OracleTokenExamplesSpecification.scala | 2 +- sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala | 6 +++--- .../main/scala/org/ergoplatform/sdk/JavaHelpers.scala | 4 ++-- .../src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala | 2 +- .../ergoplatform/sdk/UnsignedTransactionBuilder.scala | 1 + .../org/ergoplatform/sdk/JsonSerializationSpec.scala | 3 +-- .../org/ergoplatform/sdk/wallet/utils/Generators.scala | 3 +-- 24 files changed, 37 insertions(+), 38 deletions(-) diff --git a/core/shared/src/main/scala/sigma/data/package.scala b/core/shared/src/main/scala/sigma/data/package.scala index b6088de1ad..a0fb219af8 100644 --- a/core/shared/src/main/scala/sigma/data/package.scala +++ b/core/shared/src/main/scala/sigma/data/package.scala @@ -1,5 +1,7 @@ package sigma +import supertagged.TaggedType + import scala.annotation.nowarn import scala.reflect.classTag @@ -47,4 +49,11 @@ package object data { type KeyValueColl = Coll[(Coll[Byte], Coll[Byte])] + trait BaseDigestColl extends TaggedType[Coll[Byte]] + + object Digest32Coll extends BaseDigestColl + + type Digest32Coll = Digest32Coll.Type + + implicit val Digest32CollRType: RType[data.Digest32Coll] = RType[Coll[Byte]].asInstanceOf[RType[data.Digest32Coll]] } diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala index b0e46dc866..909fa515af 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala @@ -10,9 +10,8 @@ import sigma.Extensions.ArrayOps import sigma.ast.SCollection.SByteArray import sigma.ast.{SCollection, SLong, STuple, SType} import sigma.ast.SType.AnyOps -import sigma.data.SigmaConstants +import sigma.data.{Digest32Coll, SigmaConstants} import sigmastate.Values._ -import sigmastate.eval._ import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ExtractCreationInfo diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index 72cd413c4e..1268341830 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala @@ -7,10 +7,10 @@ import scorex.util.{ModifierId, bytesToId} import sigma.Extensions.{ArrayOps, CollOps} import sigma.ast.SType import sigma.ast.SType.AnyOps +import sigma.data.Digest32Coll import sigma.util.safeNewArray import sigma.{Coll, Colls} import sigmastate.Values._ -import sigmastate.eval._ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeTransaction.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeTransaction.scala index fcfdbc922b..469a5fb1df 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeTransaction.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeTransaction.scala @@ -6,6 +6,7 @@ import scorex.crypto.authds.ADKey import scorex.crypto.hash.Blake2b256 import scorex.util._ import sigma.Colls +import sigma.data.Digest32Coll import sigma.util.safeNewArray import sigmastate._ import sigmastate.eval.Extensions._ diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala index f8a58964e8..6aea56fc5d 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/Extensions.scala @@ -8,7 +8,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.util.encode.Base16 import sigma.ast.SType.AnyOps import sigma.ast.{SBoolean, SCollection, SCollectionType, SType} -import sigma.data.{CAnyValue, Nullable, RType, SigmaBoolean} +import sigma.data.{CAnyValue, Digest32Coll, Nullable, RType, SigmaBoolean} import sigma.{Coll, _} import sigmastate.Platform import sigmastate.Values.{Constant, ConstantNode, SigmaPropConstant, SigmaPropValue, Value} diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala index 9c086fe102..6b97167d59 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/package.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/package.scala @@ -19,13 +19,6 @@ package object eval { */ val SigmaDsl = CSigmaDslBuilder - trait BaseDigestColl extends TaggedType[Coll[Byte]] - - object Digest32Coll extends BaseDigestColl - - type Digest32Coll = Digest32Coll.Type - implicit val Digest32CollRType: RType[Digest32Coll] = RType[Coll[Byte]].asInstanceOf[RType[Digest32Coll] ] - /** Implicit conversions between Dsl type and the type wrapped by the corresponding type Dsl type. * Here BigInt is Dsl type and BigInteger is wrapped type. * @see `sigma.CBigInt` diff --git a/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala b/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala index 111dd53303..efc0db041a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala +++ b/interpreter/shared/src/main/scala/sigmastate/utils/Extensions.scala @@ -3,7 +3,8 @@ package sigmastate.utils import org.ergoplatform.ErgoBox.TokenId import scorex.util.{ModifierId, idToBytes} import scorex.utils.{Ints, Longs, Shorts} -import sigmastate.eval.{Digest32Coll, SigmaDsl} +import sigma.data.Digest32Coll +import sigmastate.eval.SigmaDsl import sigma.{Coll, Colls} object Extensions { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 17ba63ef5b..0050bb5777 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -8,7 +8,7 @@ import org.scalacheck.Arbitrary._ import org.scalacheck.Gen.{choose, frequency} import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, RType, SigmaBoolean} +import sigma.data._ import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.encode.{Base58, Base64} import scorex.util.{ModifierId, bytesToId} diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 32efb16770..f88fb0c10d 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -6,14 +6,13 @@ import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen.containerOfN import org.scalacheck.util.Buildable import org.scalacheck.{Arbitrary, Gen} -import sigma.data.{AvlTreeData, AvlTreeFlags, CAND, CAvlTree, CBigInt, COR, CSigmaProp, ProveDHTuple, ProveDlog, RType} +import sigma.data._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} -import sigmastate.eval._ import sigma.Extensions.ArrayOps -import sigmastate.eval.{CHeader, CPreHeader, CBox, CSigmaDslBuilder, SigmaDsl} +import sigmastate.eval.{CBox, CHeader, CPreHeader, CSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index 96153685a9..f759c93672 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -6,7 +6,7 @@ import scorex.util.ModifierId import sigma.Extensions.ArrayOps import sigma.SigmaDslTesting import sigma.ast._ -import sigma.data.{AvlTreeData, AvlTreeFlags, CollType} +import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, Digest32Coll} import sigmastate.Values._ import sigmastate._ import sigmastate.eval._ diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala index dd5ca0bee9..cfb2bc4330 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoLikeTransactionSpec.scala @@ -17,7 +17,7 @@ import sigmastate.helpers.TestingHelpers.copyTransaction import sigmastate.utils.Helpers import sigma.SigmaDslTesting import sigma.Extensions._ -import sigma.data.{CSigmaProp, TrivialProp} +import sigma.data.{CSigmaProp, Digest32Coll, TrivialProp} class ErgoLikeTransactionSpec extends SigmaDslTesting { diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index 07ae812798..683c6adc42 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -9,7 +9,7 @@ import scorex.crypto.hash.Blake2b256 import scorex.util.Random import sigma.Colls import sigma.ast.SByte -import sigma.data.{AvlTreeData, ProveDlog, TrivialProp} +import sigma.data.{AvlTreeData, Digest32Coll, ProveDlog, TrivialProp} import sigma.util.BenchmarkUtil.measure import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ @@ -21,7 +21,6 @@ import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ValueSerializer import sigmastate.utxo.{ByIndex, ExtractCreationInfo, SelectField} -import sigmastate.eval.Digest32Coll import sigmastate.utils.Helpers._ import scala.util.Try diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeErgoTests.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeErgoTests.scala index 5eb852265d..7d9258bc5c 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeErgoTests.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeErgoTests.scala @@ -5,7 +5,7 @@ import org.ergoplatform.dsl.ContractSyntax.Token import org.ergoplatform.dsl.ErgoContractSpec import sigma.Coll import scorex.crypto.hash.Blake2b256 -import sigmastate.eval.Digest32Coll +import sigma.data.Digest32Coll class AssetsAtomicExchangeErgoTests extends CompilerTestingCommons { suite => lazy val spec = new ErgoContractSpec()(new TestingIRContext) diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala index 03aee196aa..10dc4b7607 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/AssetsAtomicExchangeTests.scala @@ -9,9 +9,8 @@ import scorex.crypto.hash.Blake2b256 import sigma.ast.SCollection.SByteArray import sigmastate._ import sigma.ast._ -import sigma.data.{CSigmaProp, TrivialProp} +import sigma.data.{CSigmaProp, Digest32Coll, TrivialProp} import sigmastate.Values.{BlockValue, ByteArrayConstant, LongConstant, ValDef, ValUse, Value} -import sigmastate.eval.Digest32Coll import sigmastate.eval.Extensions._ import sigmastate.lang.Terms.ValueOps import sigmastate.utxo._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala index 26f00d6fe5..601b02fe96 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/IcoExample.scala @@ -10,7 +10,7 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls import sigma.ast.SByte -import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigma.data.{AvlTreeData, AvlTreeFlags, Digest32Coll} import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.CryptoConstants diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala index c90ebe1523..1a41a656d9 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala @@ -6,11 +6,11 @@ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert, Lookup} import scorex.crypto.hash.{Blake2b256, Digest32} import sigma.Colls -import sigma.data.{AvlTreeData, AvlTreeFlags, TrivialProp} +import sigma.data.{AvlTreeData, AvlTreeFlags, Digest32Coll, TrivialProp} import sigmastate.CompilerCrossVersionProps import sigmastate.Values.{AvlTreeConstant, ByteArrayConstant, LongConstant, SigmaPropConstant} import sigma.Extensions.ArrayOps -import sigmastate.eval.{Digest32Coll, IRContext, SigmaDsl} +import sigmastate.eval.{IRContext, SigmaDsl} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestProvingInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleDataInputsExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleDataInputsExamplesSpecification.scala index 564385fd96..3207245fbb 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleDataInputsExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleDataInputsExamplesSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform._ import org.ergoplatform.dsl.ContractSyntax.Token import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, StdContracts, TestContractSpec} import scorex.crypto.hash.Blake2b256 -import sigmastate.eval.Digest32Coll +import sigma.data.Digest32Coll import sigmastate.helpers.CompilerTestingCommons import sigma.Coll import sigma.Context diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleTokenExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleTokenExamplesSpecification.scala index 5a30cd6689..e14f47f1c7 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleTokenExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleTokenExamplesSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform._ import org.ergoplatform.dsl.ContractSyntax.Token import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, StdContracts, TestContractSpec} import scorex.crypto.hash.Blake2b256 -import sigmastate.eval.Digest32Coll +import sigma.data.Digest32Coll import sigmastate.helpers.CompilerTestingCommons import sigma.Coll import sigma.Context diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala index ec306e9240..54fa591444 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala @@ -11,11 +11,11 @@ import scorex.util.encode.Base16 import sigma.Extensions.CollBytesOps import sigma.ast.SType import sigma.data.Iso.{isoStringToArray, isoStringToColl} -import sigma.data.{CBigInt, CGroupElement, Iso} +import sigma.data.{CBigInt, CGroupElement, Digest32Coll, Iso} import sigma.js.{AvlTree, GroupElement, Type, Value} import sigma.{Coll, Colls, Evaluation} import sigmastate.Values.{Constant, GroupElementConstant} -import sigmastate.eval.{CHeader, CPreHeader, Digest32Coll} +import sigmastate.eval.{CHeader, CPreHeader} import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box import sigmastate.fleetSdkCommon.distEsmTypesCommonMod.HexString import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters @@ -24,7 +24,7 @@ import sigmastate.fleetSdkCommon.distEsmTypesTransactionsMod.{SignedTransaction, import sigmastate.fleetSdkCommon.{distEsmTypesBoxesMod => boxesMod, distEsmTypesCommonMod => commonMod, distEsmTypesContextExtensionMod => contextExtensionMod, distEsmTypesInputsMod => inputsMod, distEsmTypesProverResultMod => proverResultMod, distEsmTypesRegistersMod => registersMod, distEsmTypesTokenMod => tokenMod} import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} - +import sigma.data.Digest32CollRType import java.math.BigInteger import scala.collection.immutable.ListMap import scala.scalajs.js diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index c562133126..d000a9f060 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -15,13 +15,13 @@ import scorex.util.{ModifierId, bytesToId, idToBytes} import sigma.ast.SType import sigma.crypto.{CryptoFacade, EcPointType} import sigma.data.ExactIntegral.LongIsExactIntegral -import sigma.data.{InverseIso, Iso, ProveDHTuple, ProveDlog, RType, SigmaBoolean, SigmaConstants} +import sigma.data.{Digest32Coll, InverseIso, Iso, ProveDHTuple, ProveDlog, RType, SigmaBoolean, SigmaConstants} import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DiffieHellmanTupleProverInput -import sigmastate.eval.{CSigmaDslBuilder, Digest32Coll} +import sigmastate.eval.CSigmaDslBuilder import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} import java.lang.{Boolean => JBoolean, Byte => JByte, Integer => JInt, Long => JLong, Short => JShort, String => JString} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 174dcbdf3d..e087a0589c 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -31,7 +31,7 @@ import org.ergoplatform.ErgoBoxCandidate import org.ergoplatform.ErgoLikeContext import sigma.Extensions.ArrayOps import sigma.ast.SType -import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, WrapperOf} +import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, Digest32Coll, WrapperOf} import sigma.validation.SigmaValidationSettings import scala.collection.mutable diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala index f5768638e0..0f18b5e74b 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/UnsignedTransactionBuilder.scala @@ -12,6 +12,7 @@ import sigmastate.utils.Extensions.ModifierIdOps import sigma.Coll import sigma.Extensions.CollBytesOps import sigma.PreHeader +import sigma.data.Digest32CollRType import scala.collection.mutable.ArrayBuffer import scala.util.Try diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index 9e10535927..718332943b 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -11,11 +11,10 @@ import scorex.crypto.authds.{ADDigest, ADKey} import scorex.util.ModifierId import scorex.util.encode.Base16 import sigma.ast.SType -import sigma.data.{AvlTreeData, ProveDlog} +import sigma.data.{AvlTreeData, Digest32Coll, ProveDlog} import sigma.{Coll, Header, PreHeader} import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} import sigmastate.crypto.CryptoConstants -import sigmastate.eval.Digest32Coll import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SerializationSpecification import sigmastate.utils.Helpers.DecoderResultOps // required for Scala 2.11 diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index 99d5d4526a..c727352035 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -15,9 +15,8 @@ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgo import sigma.Extensions.ArrayOps import scorex.util.{ModifierId, bytesToId} import sigma.crypto.CryptoFacade -import sigma.data.ProveDlog +import sigma.data.{Digest32Coll, ProveDlog} import sigmastate.eval.Extensions.{EvalIterableOps, SigmaBooleanOps} -import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ trait Generators { From 42541ac78d40af0f79e9ace17ccf1c71880f3f5a Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 17:51:39 +0200 Subject: [PATCH 30/34] core-serializers2: ErgoTree moved out of Values object --- .../main/scala/org/ergoplatform/ErgoBox.scala | 1 + .../org/ergoplatform/ErgoBoxCandidate.scala | 1 + .../org/ergoplatform/ErgoTreePredef.scala | 4 +- .../settings/MonetarySettings.scala | 2 +- .../validation/ValidationRules.scala | 4 +- .../src/main/scala/sigmastate/ErgoTree.scala | 373 ++++++++++++++++++ .../main/scala/sigmastate/UnprovenTree.scala | 2 +- .../src/main/scala/sigmastate/Values.scala | 356 +---------------- .../interpreter/ErgoTreeEvaluator.scala | 3 +- .../sigmastate/interpreter/ProverUtils.scala | 2 +- .../serialization/ErgoTreeSerializer.scala | 5 +- .../src/test/scala/sigmastate/TestsBase.scala | 2 +- .../ContextEnrichingProverInterpreter.scala | 3 +- .../sigmastate/helpers/TestingHelpers.scala | 2 +- .../DataSerializerSpecification.scala | 2 +- .../special/sigma/ContractsTestkit.scala | 2 +- .../special/sigma/SigmaTestingData.scala | 4 +- .../sigmastate/lang/js/SigmaCompiler.scala | 4 +- .../org/ergoplatform/dsl/ContractSpec.scala | 2 +- .../ErgoAddressSpecification.scala | 4 +- .../org/ergoplatform/ErgoTreePredefSpec.scala | 2 +- .../ergoplatform/dsl/TestContractSpec.scala | 3 +- .../DataValueComparerSpecification.scala | 2 +- .../test/scala/sigma/SigmaDslTesting.scala | 4 +- .../scala/sigmastate/CompilerTestsBase.scala | 2 +- .../ScriptVersionSwitchSpecification.scala | 2 +- .../SoftForkabilitySpecification.scala | 4 +- .../helpers/CompilerTestingCommons.scala | 5 +- .../sigmastate/helpers/SigmaPPrint.scala | 3 +- .../ErgoTreeSerializerSpecification.scala | 4 +- .../BlockchainSimulationTestingCommons.scala | 4 +- .../examples/CoinEmissionSpecification.scala | 5 +- .../examples/CoopExampleSpecification.scala | 4 +- .../org/ergoplatform/sdk/js/ErgoTree.scala | 6 +- .../ergoplatform/sdk/ContractTemplate.scala | 6 +- .../org/ergoplatform/sdk/JavaHelpers.scala | 6 +- .../org/ergoplatform/sdk/JsonCodecs.scala | 32 +- .../org/ergoplatform/sdk/OutBoxBuilder.scala | 3 +- .../sdk/ReducingInterpreter.scala | 2 +- .../org/ergoplatform/sdk/utils/Zero.scala | 2 +- .../sdk/JsonSerializationSpec.scala | 3 +- .../sdk/generators/ObjectGenerators.scala | 2 +- .../sdk/wallet/utils/Generators.scala | 3 +- 43 files changed, 451 insertions(+), 436 deletions(-) create mode 100644 interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala index 909fa515af..bda49eb6ed 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBox.scala @@ -16,6 +16,7 @@ import sigmastate.serialization.SigmaSerializer import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} import sigmastate.utxo.ExtractCreationInfo import sigma.{Colls, _} +import sigmastate.ErgoTree /** * Box (aka coin, or an unspent output) is a basic concept of a UTXO-based cryptocurrency. In Bitcoin, such an object diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala index 1268341830..2c73b119b3 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoBoxCandidate.scala @@ -10,6 +10,7 @@ import sigma.ast.SType.AnyOps import sigma.data.Digest32Coll import sigma.util.safeNewArray import sigma.{Coll, Colls} +import sigmastate.ErgoTree import sigmastate.Values._ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala index 8e60f2f7b6..15c10f45bb 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala @@ -6,9 +6,9 @@ import sigma.ast.{SBox, SInt, SLong, SSigmaProp} import sigma.data.ProveDlog import sigmastate.crypto.CryptoConstants import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} +import sigmastate.Values.{FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} import sigmastate.utxo._ -import sigmastate._ +import sigmastate.{ErgoTree, _} import sigmastate.lang.Terms.ValueOps object ErgoTreePredef { diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/settings/MonetarySettings.scala b/interpreter/shared/src/main/scala/org/ergoplatform/settings/MonetarySettings.scala index 15f7b9edbb..a20bdd8baa 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/settings/MonetarySettings.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/settings/MonetarySettings.scala @@ -1,6 +1,6 @@ package org.ergoplatform.settings -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import org.ergoplatform.ErgoTreePredef import org.ergoplatform.mining.emission.EmissionRules diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala index 4825624117..d7c1656151 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala @@ -6,8 +6,8 @@ import sigma.serialization.{InvalidOpCode, SerializerException} import sigma.util.Extensions.toUByte import sigma.validation.ValidationRules._ import sigma.validation._ -import sigmastate.Values.{ErgoTree, SValue} -import sigmastate._ +import sigmastate.Values.SValue +import sigmastate.{ErgoTree, _} import sigmastate.exceptions._ import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization.{ValueCodes, ValueSerializer} diff --git a/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala b/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala new file mode 100644 index 0000000000..8fe2bfbba5 --- /dev/null +++ b/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala @@ -0,0 +1,373 @@ +package sigmastate + +import org.ergoplatform.settings.ErgoAlgos +import scorex.util.encode.Base16 +import sigma.ast.SType +import sigma.data.SigmaBoolean +import sigma.kiama.rewriting.Rewriter.{everywherebu, strategy} +import sigma.validation.ValidationException +import sigmastate.ErgoTree.substConstants +import sigmastate.Values.{Constant, ConstantPlaceholder, SValue, SigmaPropConstant, SigmaPropValue, Value} +import sigmastate.eval.Extensions.SigmaBooleanOps +import sigmastate.lang.Terms.ValueOps +import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer +import sigmastate.serialization.{ConstantStore, ErgoTreeSerializer, SigmaSerializer, ValueSerializer} + +import java.util.Objects +import scala.collection.mutable + +/** This is alternative representation of ErgoTree expression when it cannot be parsed + * due to `error`. This is used by the nodes running old versions of code to recognize + * soft-fork conditions and skip validation of box propositions which are unparsable. */ +case class UnparsedErgoTree(bytes: mutable.WrappedArray[Byte], error: ValidationException) + +/** The root of ErgoScript IR. Serialized instances of this class are self sufficient and can be passed around. + * ErgoTreeSerializer defines top-level serialization format of the scripts. + * The interpretation of the byte array depend on the first `header` byte, which uses VLQ encoding up to 30 bits. + * Currently we define meaning for only first byte, which may be extended in future versions. + * 7 6 5 4 3 2 1 0 + * ------------------------- + * | | | | | | | | | + * ------------------------- + * Bit 7 == 1 if the header contains more than 1 byte (default == 0) + * Bit 6 - reserved for GZIP compression (should be 0) + * Bit 5 == 1 - reserved for context dependent costing (should be = 0) + * Bit 4 == 1 if constant segregation is used for this ErgoTree (default = 0) + * (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/264) + * Bit 3 == 1 if size of the whole tree is serialized after the header byte (default = 0) + * Bits 2-0 - language version (current version == 0) + * + * Currently we don't specify interpretation for the second and other bytes of the header. + * We reserve the possibility to extend header by using Bit 7 == 1 and chain additional bytes as in VLQ. + * Once the new bytes are required, a new version of the language should be created and implemented. + * That new language will give an interpretation for the new bytes. + * + * Consistency between fields is ensured by private constructor and factory methods in `ErgoTree` object. + * For performance reasons, ErgoTreeSerializer can be configured to perform additional constant segregation. + * In such a case after deserialization there may be more constants segregated. This is done for example to + * support caching optimization described in #264 mentioned above. + * + * The default behavior of ErgoTreeSerializer is to preserve original structure of ErgoTree and check + * consistency. In case of any inconsistency the serializer throws exception. + * + * @param header the first byte of serialized byte array which determines interpretation of the rest of the array + * @param constants If isConstantSegregation == true contains the constants for which there may be + * ConstantPlaceholders in the tree. + * If isConstantSegregation == false this array should be empty and any placeholder in + * the tree will lead to exception. + * @param root On the right side it has valid expression of `SigmaProp` type. Or alternatively, + * on the left side, it has unparsed bytes along with the ValidationException, + * which caused the deserializer to fail. + * `Right(tree)` if isConstantSegregation == true contains ConstantPlaceholder + * instead of some Constant nodes. Otherwise, it may not contain placeholders. + * It is possible to have both constants and placeholders in the tree, but for every placeholder + * there should be a constant in `constants` array. + * @param givenComplexity structural complexity of the tree or 0 if is not specified at construction time. + * Access to this private value is provided via `complexity` property. + * In case of 0, the complexity is computed using ErgoTree deserializer, which can do this. + * When specified it should be computed as the sum of complexity values taken + * from ComplexityTable for all tree nodes. It approximates the time needed to process + * the tree by sigma compiler to obtain cost formula. Overly complex trees can thus + * be rejected even before compiler starts working. + * @param propositionBytes original bytes of this tree from which it has been deserialized. + * If null then the bytes are not provided, and will be lazily generated when `bytes` + * method is called. + * These bytes are obtained in two ways: + * 1) in the ErgoTreeSerializer from Reader + * 2) in the alternative constructor using ErgoTreeSerializer.serializeErgoTree + * @param givenDeserialize optional flag, which contains information about presence of + * deserialization operations in the tree. If it is None, the information is not + * available. If Some(true) then there are deserialization operations, otherwise + * the tree doesn't contain deserialization and is eligible + * for optimized execution. + * ErgoTreeSerializer parsing method computes the value of + * this flag and provides it to the constructor. + */ +case class ErgoTree private[sigmastate]( + header: Byte, + constants: IndexedSeq[Constant[SType]], + root: Either[UnparsedErgoTree, SigmaPropValue], + private val givenComplexity: Int, + private val propositionBytes: Array[Byte], + private val givenDeserialize: Option[Boolean] +) { + def this( + header: Byte, + constants: IndexedSeq[Constant[SType]], + root: Either[UnparsedErgoTree, SigmaPropValue]) = + this( + header, constants, root, 0, + propositionBytes = DefaultSerializer.serializeErgoTree( + ErgoTree(header, constants, root, 0, null, None) + ), + givenDeserialize = None + ) + + require(isConstantSegregation || constants.isEmpty) + + require(version == 0 || hasSize, s"For newer version the size bit is required: $this") + + /** Then it throws the error from UnparsedErgoTree. + * It does so on every usage of `proposition` because the lazy value remains uninitialized. + */ + @deprecated("Use toProposition instead", "v2.1") + lazy val proposition: SigmaPropValue = toProposition(isConstantSegregation) + + @inline final def version: Byte = ErgoTree.getVersion(header) + + @inline final def isRightParsed: Boolean = root.isRight + + @inline final def isConstantSegregation: Boolean = ErgoTree.isConstantSegregation(header) + + @inline final def hasSize: Boolean = ErgoTree.hasSize(header) + + private[sigmastate] var _bytes: Array[Byte] = propositionBytes + + /** Serialized bytes of this tree. */ + final def bytes: Array[Byte] = { + if (_bytes == null) { + _bytes = DefaultSerializer.serializeErgoTree(this) + } + _bytes + } + + /** Hexadecimal encoded string of ErgoTree.bytes. */ + final def bytesHex: String = ErgoAlgos.encode(bytes) + + private var _complexity: Int = givenComplexity + + /** Structural complexity estimation of this tree. + * + * @see ComplexityTable + */ + lazy val complexity: Int = { + if (_complexity == 0) { + _complexity = DefaultSerializer.deserializeErgoTree(bytes).complexity + } + _complexity + } + + private[sigmastate] var _hasDeserialize: Option[Boolean] = givenDeserialize + + /** Returns true if the tree contains at least one deserialization operation, + * false otherwise. + */ + lazy val hasDeserialize: Boolean = { + if (_hasDeserialize.isEmpty) { + _hasDeserialize = Some(root match { + case Right(p) => Value.hasDeserialize(p) + case _ => false + }) + } + _hasDeserialize.get + } + + /** Serialized proposition expression of SigmaProp type with + * ConstantPlaceholder nodes not replaced by Constant nodes. + */ + lazy val template: Array[Byte] = { + val r = SigmaSerializer.startReader(bytes) + DefaultSerializer.deserializeHeaderWithTreeBytes(r)._4 + } + + /** Base16 encoding of `template` bytes. */ + def templateHex: String = Base16.encode(template) + + /** Get proposition expression from this contract. + * When root.isRight then + * if replaceConstants == false this is the same as `root.right.get`. + * Otherwise, it is equivalent to `root.right.get` where all placeholders are replaced by Constants. + * When root.isLeft then + * throws the error from UnparsedErgoTree. + * It does so on every usage of `proposition` because the lazy value remains uninitialized. + */ + def toProposition(replaceConstants: Boolean): SigmaPropValue = root match { + case Right(tree) => + val prop = if (replaceConstants) + substConstants(tree, constants).asSigmaProp + else + tree + prop + case Left(UnparsedErgoTree(_, error)) => + throw error + } + + /** The default equality of case class is overridden to exclude `complexity`. */ + override def canEqual(that: Any): Boolean = that.isInstanceOf[ErgoTree] + + override def hashCode(): Int = header * 31 + Objects.hash(constants, root) + + override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || + ((obj.asInstanceOf[AnyRef] != null) && (obj match { + case other: ErgoTree => + other.header == header && other.constants == constants && other.root == root + case _ => false + })) +} + +object ErgoTree { + /** Current version of ErgoTree serialization format (aka bite-code language version) */ + val VersionFlag: Byte = 0 + + /** Default value of ErgoTree.header byte */ + val DefaultHeader: Byte = (0 | VersionFlag).toByte + + /** Header flag to indicate that constant segregation should be applied. */ + val ConstantSegregationFlag: Byte = 0x10 + + /** Header flag to indicate that whole size of ErgoTree should be saved before tree content. */ + val SizeFlag: Byte = 0x08 + + /** Header mask to extract version bits. */ + val VersionMask: Byte = 0x07 + + /** Default header with constant segregation enabled. */ + val ConstantSegregationHeader: Byte = (DefaultHeader | ConstantSegregationFlag).toByte + + /** @return true if the constant segregation flag is set to 1 in the given header byte. */ + @inline final def isConstantSegregation(header: Byte): Boolean = (header & ConstantSegregationFlag) != 0 + + /** @return true if the size flag is set to 1 in the given header byte. */ + @inline final def hasSize(header: Byte): Boolean = (header & SizeFlag) != 0 + + /** @return a value of the version bits from the given header byte. */ + @inline final def getVersion(header: Byte): Byte = (header & VersionMask).toByte + + /** Update the version bits of the given header byte with the given version value. */ + @inline final def updateVersionBits(header: Byte, version: Byte): Byte = { + require(version < 8, s"ErgoTree.version should be < 8: $version") + (header | version).toByte + } + + /** Creates valid header byte with the given version. + * The SizeFlag is set if version > 0 */ + @inline def headerWithVersion(version: Byte): Byte = { + // take default header and embedd the given version in it + var h = updateVersionBits(DefaultHeader, version) + if (version > 0) { + // set SizeFlag if version is greater then 0 (see require() in ErgoTree constructor) + h = (h | ErgoTree.SizeFlag).toByte + } + h + } + + /** Substitute [[ConstantPlaceholder]] nodes in the given expression with the constants + * taken from the given collection. + * + * @param root expression to transform + * @param constants collection of constants to replace placeholders + * @return new expression without placeholders + */ + def substConstants(root: SValue, constants: IndexedSeq[Constant[SType]]): SValue = { + val store = new ConstantStore(constants) + val substRule = strategy[Any] { + case ph: ConstantPlaceholder[_] => + Some(store.get(ph.id)) + case _ => None + } + everywherebu(substRule)(root).fold(root)(_.asInstanceOf[SValue]) + } + + /** Create an ErgoTree with the given parameters. */ + def apply( + header: Byte, + constants: IndexedSeq[Constant[SType]], + root: SigmaPropValue): ErgoTree = { + new ErgoTree(header, constants, Right(root)) + } + + val EmptyConstants: IndexedSeq[Constant[SType]] = Array[Constant[SType]]() + + /** Create new ErgoTree for the given proposition using the given header flags and + * without performing constant segregation. + */ + def withoutSegregation(root: SigmaPropValue): ErgoTree = + ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, root) + + /** Create new ErgoTree for the given proposition using the given header flags and + * without performing constant segregation. + */ + def withoutSegregation(headerFlags: Byte, root: SigmaPropValue): ErgoTree = + ErgoTree((ErgoTree.DefaultHeader | headerFlags).toByte, EmptyConstants, root) + + /** Create new ErgoTree for the given proposition using default header. + * If the property is not a simple constant, then constant segregation is performed. + */ + implicit def fromProposition(prop: SigmaPropValue): ErgoTree = { + fromProposition(ErgoTree.DefaultHeader, prop) + } + + /** Create new ErgoTree for the given proposition using the given header flags. + * If the property is not a simple constant, then constant segregation is performed. + */ + def fromProposition(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { + prop match { + case SigmaPropConstant(_) => withoutSegregation(headerFlags, prop) + case _ => withSegregation(headerFlags, prop) + } + } + + /** Create new ErgoTree for the given sigma proposition using default header and + * without performing constant segregation. + */ + implicit def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = { + withoutSegregation(pk.toSigmaPropValue) + } + + /** Create new ErgoTree for the given sigma proposition using the given header flags + * and without performing constant segregation. + */ + def fromSigmaBoolean(headerFlags: Byte, pk: SigmaBoolean): ErgoTree = { + withoutSegregation(headerFlags, pk.toSigmaPropValue) + } + + /** Build ErgoTree via serialization of the value with ConstantSegregationHeader, constants segregated + * from the tree and ConstantPlaceholders referring to the segregated constants. + * + * This method uses single traverse of the tree to: + * 1) find and segregate all constants; + * 2) replace constants with ConstantPlaceholders in the `tree`; + * 3) write the `tree` to the Writer's buffer obtaining `treeBytes`; + * 4) deserialize `tree` with ConstantPlaceholders. + * + * @param headerFlags additional header flags to combine with + * ConstantSegregationHeader flag. + * @param prop expression to be transformed into ErgoTree + * */ + def withSegregation(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { + val constantStore = new ConstantStore() + val byteWriter = SigmaSerializer.startWriter(constantStore) + // serialize value and segregate constants into constantStore + ValueSerializer.serialize(prop, byteWriter) + val extractedConstants = constantStore.getAll + val r = SigmaSerializer.startReader(byteWriter.toBytes) + r.constantStore = new ConstantStore(extractedConstants) + // deserialize value with placeholders + val valueWithPlaceholders = ValueSerializer.deserialize(r).asSigmaProp + val header = (ErgoTree.ConstantSegregationHeader | headerFlags).toByte + new ErgoTree(header, extractedConstants, Right(valueWithPlaceholders)) + } + + /** Create new ErgoTree for the given sigma proposition using default header and + * also performing constant segregation. + */ + def withSegregation(prop: SigmaPropValue): ErgoTree = + withSegregation(DefaultHeader, prop) + + /** Deserializes an ErgoTree instance from a hexadecimal string. + * + * @param hex a hexadecimal string representing the serialized ErgoTree + */ + def fromHex(hex: String): ErgoTree = { + val bytes = Base16.decode(hex).get + fromBytes(bytes) + } + + /** Deserializes an ErgoTree instance from an array of bytes. + * + * @param bytes an array of bytes representing the serialized ErgoTree + */ + def fromBytes(bytes: Array[Byte]): ErgoTree = { + ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(bytes) + } +} diff --git a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala index 3df9ef1624..1a0b70889a 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala @@ -2,7 +2,7 @@ package sigmastate import debox.cfor import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, SigmaLeaf} -import sigmastate.Values.{ErgoTree, SigmaPropConstant} +import sigmastate.Values.SigmaPropConstant import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge import sigmastate.crypto.{FirstDHTupleProverMessage, FirstProverMessage, GF2_192_Poly} diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 2d71c995eb..985d1f01aa 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -1,22 +1,17 @@ package sigmastate import debox.cfor -import org.ergoplatform.settings.ErgoAlgos -import scorex.util.encode.Base16 import sigma.Extensions.ArrayOps import sigma.ast.SCollection.{SByteArray, SIntArray} import sigma.ast.TypeCodes.ConstantCode import sigma.ast._ import sigma.crypto.EcPointType import sigma.data.{Nullable, RType, SigmaBoolean, TrivialProp} -import sigma.kiama.rewriting.Rewriter.{count, everywherebu, strategy} +import sigma.kiama.rewriting.Rewriter.count import sigma.util.CollectionUtil._ import sigma.util.Extensions._ -import sigma.validation.ValidationException import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} -import sigmastate.Values.ErgoTree.substConstants import sigmastate.crypto.CryptoConstants -import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval._ import sigmastate.exceptions.InterpreterException import sigmastate.interpreter.ErgoTreeEvaluator._ @@ -24,7 +19,6 @@ import sigmastate.interpreter.{CompanionDesc, ErgoTreeEvaluator, Interpreter, Na import sigmastate.lang.CheckingSigmaBuilder._ import sigmastate.lang.SourceContext import sigmastate.lang.Terms._ -import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization._ @@ -1034,353 +1028,5 @@ object Values { def GetVarByteArray(varId: Byte): GetVar[SCollection[SByte.type]] = GetVar(varId, SByteArray) def GetVarIntArray(varId: Byte): GetVar[SCollection[SInt.type]] = GetVar(varId, SIntArray) - /** This is alternative representation of ErgoTree expression when it cannot be parsed - * due to `error`. This is used by the nodes running old versions of code to recognize - * soft-fork conditions and skip validation of box propositions which are unparsable. */ - case class UnparsedErgoTree(bytes: mutable.WrappedArray[Byte], error: ValidationException) - - /** The root of ErgoScript IR. Serialized instances of this class are self sufficient and can be passed around. - * ErgoTreeSerializer defines top-level serialization format of the scripts. - * The interpretation of the byte array depend on the first `header` byte, which uses VLQ encoding up to 30 bits. - * Currently we define meaning for only first byte, which may be extended in future versions. - * 7 6 5 4 3 2 1 0 - * ------------------------- - * | | | | | | | | | - * ------------------------- - * Bit 7 == 1 if the header contains more than 1 byte (default == 0) - * Bit 6 - reserved for GZIP compression (should be 0) - * Bit 5 == 1 - reserved for context dependent costing (should be = 0) - * Bit 4 == 1 if constant segregation is used for this ErgoTree (default = 0) - * (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/264) - * Bit 3 == 1 if size of the whole tree is serialized after the header byte (default = 0) - * Bits 2-0 - language version (current version == 0) - * - * Currently we don't specify interpretation for the second and other bytes of the header. - * We reserve the possibility to extend header by using Bit 7 == 1 and chain additional bytes as in VLQ. - * Once the new bytes are required, a new version of the language should be created and implemented. - * That new language will give an interpretation for the new bytes. - * - * Consistency between fields is ensured by private constructor and factory methods in `ErgoTree` object. - * For performance reasons, ErgoTreeSerializer can be configured to perform additional constant segregation. - * In such a case after deserialization there may be more constants segregated. This is done for example to - * support caching optimization described in #264 mentioned above. - * - * The default behavior of ErgoTreeSerializer is to preserve original structure of ErgoTree and check - * consistency. In case of any inconsistency the serializer throws exception. - * - * @param header the first byte of serialized byte array which determines interpretation of the rest of the array - * - * @param constants If isConstantSegregation == true contains the constants for which there may be - * ConstantPlaceholders in the tree. - * If isConstantSegregation == false this array should be empty and any placeholder in - * the tree will lead to exception. - * - * @param root On the right side it has valid expression of `SigmaProp` type. Or alternatively, - * on the left side, it has unparsed bytes along with the ValidationException, - * which caused the deserializer to fail. - * `Right(tree)` if isConstantSegregation == true contains ConstantPlaceholder - * instead of some Constant nodes. Otherwise, it may not contain placeholders. - * It is possible to have both constants and placeholders in the tree, but for every placeholder - * there should be a constant in `constants` array. - * @param givenComplexity structural complexity of the tree or 0 if is not specified at construction time. - * Access to this private value is provided via `complexity` property. - * In case of 0, the complexity is computed using ErgoTree deserializer, which can do this. - * When specified it should be computed as the sum of complexity values taken - * from ComplexityTable for all tree nodes. It approximates the time needed to process - * the tree by sigma compiler to obtain cost formula. Overly complex trees can thus - * be rejected even before compiler starts working. - * @param propositionBytes original bytes of this tree from which it has been deserialized. - * If null then the bytes are not provided, and will be lazily generated when `bytes` - * method is called. - * These bytes are obtained in two ways: - * 1) in the ErgoTreeSerializer from Reader - * 2) in the alternative constructor using ErgoTreeSerializer.serializeErgoTree - * @param givenDeserialize optional flag, which contains information about presence of - * deserialization operations in the tree. If it is None, the information is not - * available. If Some(true) then there are deserialization operations, otherwise - * the tree doesn't contain deserialization and is eligible - * for optimized execution. - * ErgoTreeSerializer parsing method computes the value of - * this flag and provides it to the constructor. - */ - case class ErgoTree private[sigmastate]( - header: Byte, - constants: IndexedSeq[Constant[SType]], - root: Either[UnparsedErgoTree, SigmaPropValue], - private val givenComplexity: Int, - private val propositionBytes: Array[Byte], - private val givenDeserialize: Option[Boolean] - ) { - - def this(header: Byte, - constants: IndexedSeq[Constant[SType]], - root: Either[UnparsedErgoTree, SigmaPropValue]) = - this( - header, constants, root, 0, - propositionBytes = DefaultSerializer.serializeErgoTree( - ErgoTree(header, constants, root, 0, null, None) - ), - givenDeserialize = None - ) - - require(isConstantSegregation || constants.isEmpty) - require(version == 0 || hasSize, s"For newer version the size bit is required: $this") - - /** Then it throws the error from UnparsedErgoTree. - * It does so on every usage of `proposition` because the lazy value remains uninitialized. - */ - @deprecated("Use toProposition instead", "v2.1") - lazy val proposition: SigmaPropValue = toProposition(isConstantSegregation) - - @inline final def version: Byte = ErgoTree.getVersion(header) - @inline final def isRightParsed: Boolean = root.isRight - @inline final def isConstantSegregation: Boolean = ErgoTree.isConstantSegregation(header) - @inline final def hasSize: Boolean = ErgoTree.hasSize(header) - - private[sigmastate] var _bytes: Array[Byte] = propositionBytes - - /** Serialized bytes of this tree. */ - final def bytes: Array[Byte] = { - if (_bytes == null) { - _bytes = DefaultSerializer.serializeErgoTree(this) - } - _bytes - } - - /** Hexadecimal encoded string of ErgoTree.bytes. */ - final def bytesHex: String = ErgoAlgos.encode(bytes) - - private var _complexity: Int = givenComplexity - - /** Structural complexity estimation of this tree. - * @see ComplexityTable - */ - lazy val complexity: Int = { - if (_complexity == 0) { - _complexity = DefaultSerializer.deserializeErgoTree(bytes).complexity - } - _complexity - } - - private[sigmastate] var _hasDeserialize: Option[Boolean] = givenDeserialize - - /** Returns true if the tree contains at least one deserialization operation, - * false otherwise. - */ - lazy val hasDeserialize: Boolean = { - if (_hasDeserialize.isEmpty) { - _hasDeserialize = Some(root match { - case Right(p) => Value.hasDeserialize(p) - case _ => false - }) - } - _hasDeserialize.get - } - - /** Serialized proposition expression of SigmaProp type with - * ConstantPlaceholder nodes not replaced by Constant nodes. - */ - lazy val template: Array[Byte] = { - val r = SigmaSerializer.startReader(bytes) - DefaultSerializer.deserializeHeaderWithTreeBytes(r)._4 - } - - /** Base16 encoding of `template` bytes. */ - def templateHex: String = Base16.encode(template) - - /** Get proposition expression from this contract. - * When root.isRight then - * if replaceConstants == false this is the same as `root.right.get`. - * Otherwise, it is equivalent to `root.right.get` where all placeholders are replaced by Constants. - * When root.isLeft then - * throws the error from UnparsedErgoTree. - * It does so on every usage of `proposition` because the lazy value remains uninitialized. - */ - def toProposition(replaceConstants: Boolean): SigmaPropValue = root match { - case Right(tree) => - val prop = if (replaceConstants) - substConstants(tree, constants).asSigmaProp - else - tree - prop - case Left(UnparsedErgoTree(_, error)) => - throw error - } - - /** The default equality of case class is overridden to exclude `complexity`. */ - override def canEqual(that: Any): Boolean = that.isInstanceOf[ErgoTree] - - override def hashCode(): Int = header * 31 + Objects.hash(constants, root) - - override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || - ((obj.asInstanceOf[AnyRef] != null) && (obj match { - case other: ErgoTree => - other.header == header && other.constants == constants && other.root == root - case _ => false - })) - } - - object ErgoTree { - /** Current version of ErgoTree serialization format (aka bite-code language version)*/ - val VersionFlag: Byte = 0 - - /** Default value of ErgoTree.header byte */ - val DefaultHeader: Byte = (0 | VersionFlag).toByte - - /** Header flag to indicate that constant segregation should be applied. */ - val ConstantSegregationFlag: Byte = 0x10 - - /** Header flag to indicate that whole size of ErgoTree should be saved before tree content. */ - val SizeFlag: Byte = 0x08 - - /** Header mask to extract version bits. */ - val VersionMask: Byte = 0x07 - - /** Default header with constant segregation enabled. */ - val ConstantSegregationHeader: Byte = (DefaultHeader | ConstantSegregationFlag).toByte - - /** @return true if the constant segregation flag is set to 1 in the given header byte. */ - @inline final def isConstantSegregation(header: Byte): Boolean = (header & ConstantSegregationFlag) != 0 - - /** @return true if the size flag is set to 1 in the given header byte. */ - @inline final def hasSize(header: Byte): Boolean = (header & SizeFlag) != 0 - - /** @return a value of the version bits from the given header byte. */ - @inline final def getVersion(header: Byte): Byte = (header & VersionMask).toByte - - /** Update the version bits of the given header byte with the given version value. */ - @inline final def updateVersionBits(header: Byte, version: Byte): Byte = { - require(version < 8, s"ErgoTree.version should be < 8: $version") - (header | version).toByte - } - - /** Creates valid header byte with the given version. - * The SizeFlag is set if version > 0 */ - @inline def headerWithVersion(version: Byte): Byte = { - // take default header and embedd the given version in it - var h = updateVersionBits(DefaultHeader, version) - if (version > 0) { - // set SizeFlag if version is greater then 0 (see require() in ErgoTree constructor) - h = (h | ErgoTree.SizeFlag).toByte - } - h - } - - /** Substitute [[ConstantPlaceholder]] nodes in the given expression with the constants - * taken from the given collection. - * @param root expression to transform - * @param constants collection of constants to replace placeholders - * @return new expression without placeholders - */ - def substConstants(root: SValue, constants: IndexedSeq[Constant[SType]]): SValue = { - val store = new ConstantStore(constants) - val substRule = strategy[Any] { - case ph: ConstantPlaceholder[_] => - Some(store.get(ph.id)) - case _ => None - } - everywherebu(substRule)(root).fold(root)(_.asInstanceOf[SValue]) - } - - /** Create an ErgoTree with the given parameters. */ - def apply(header: Byte, constants: IndexedSeq[Constant[SType]], root: SigmaPropValue): ErgoTree = { - new ErgoTree(header, constants, Right(root)) - } - - val EmptyConstants: IndexedSeq[Constant[SType]] = Array[Constant[SType]]() - - /** Create new ErgoTree for the given proposition using the given header flags and - * without performing constant segregation. - */ - def withoutSegregation(root: SigmaPropValue): ErgoTree = - ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, root) - - /** Create new ErgoTree for the given proposition using the given header flags and - * without performing constant segregation. - */ - def withoutSegregation(headerFlags: Byte, root: SigmaPropValue): ErgoTree = - ErgoTree((ErgoTree.DefaultHeader | headerFlags).toByte, EmptyConstants, root) - - /** Create new ErgoTree for the given proposition using default header. - * If the property is not a simple constant, then constant segregation is performed. - */ - implicit def fromProposition(prop: SigmaPropValue): ErgoTree = { - fromProposition(ErgoTree.DefaultHeader, prop) - } - - /** Create new ErgoTree for the given proposition using the given header flags. - * If the property is not a simple constant, then constant segregation is performed. - */ - def fromProposition(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { - prop match { - case SigmaPropConstant(_) => withoutSegregation(headerFlags, prop) - case _ => withSegregation(headerFlags, prop) - } - } - - /** Create new ErgoTree for the given sigma proposition using default header and - * without performing constant segregation. - */ - implicit def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = { - withoutSegregation(pk.toSigmaPropValue) - } - - /** Create new ErgoTree for the given sigma proposition using the given header flags - * and without performing constant segregation. - */ - def fromSigmaBoolean(headerFlags: Byte, pk: SigmaBoolean): ErgoTree = { - withoutSegregation(headerFlags, pk.toSigmaPropValue) - } - - /** Build ErgoTree via serialization of the value with ConstantSegregationHeader, constants segregated - * from the tree and ConstantPlaceholders referring to the segregated constants. - * - * This method uses single traverse of the tree to: - * 1) find and segregate all constants; - * 2) replace constants with ConstantPlaceholders in the `tree`; - * 3) write the `tree` to the Writer's buffer obtaining `treeBytes`; - * 4) deserialize `tree` with ConstantPlaceholders. - * @param headerFlags additional header flags to combine with - * ConstantSegregationHeader flag. - * @param prop expression to be transformed into ErgoTree - **/ - def withSegregation(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { - val constantStore = new ConstantStore() - val byteWriter = SigmaSerializer.startWriter(constantStore) - // serialize value and segregate constants into constantStore - ValueSerializer.serialize(prop, byteWriter) - val extractedConstants = constantStore.getAll - val r = SigmaSerializer.startReader(byteWriter.toBytes) - r.constantStore = new ConstantStore(extractedConstants) - // deserialize value with placeholders - val valueWithPlaceholders = ValueSerializer.deserialize(r).asSigmaProp - val header = (ErgoTree.ConstantSegregationHeader | headerFlags).toByte - new ErgoTree(header, extractedConstants, Right(valueWithPlaceholders)) - } - - /** Create new ErgoTree for the given sigma proposition using default header and - * also performing constant segregation. - */ - def withSegregation(prop: SigmaPropValue): ErgoTree = - withSegregation(DefaultHeader, prop) - - /** Deserializes an ErgoTree instance from a hexadecimal string. - * - * @param hex a hexadecimal string representing the serialized ErgoTree - */ - def fromHex(hex: String): ErgoTree = { - val bytes = Base16.decode(hex).get - fromBytes(bytes) - } - - /** Deserializes an ErgoTree instance from an array of bytes. - * - * @param bytes an array of bytes representing the serialized ErgoTree - */ - def fromBytes(bytes: Array[Byte]): ErgoTree = { - ErgoTreeSerializer.DefaultSerializer.deserializeErgoTree(bytes) - } - - } } diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala index db9dcf8a93..5e4f278b5d 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ErgoTreeEvaluator.scala @@ -1,7 +1,7 @@ package sigmastate.interpreter import org.ergoplatform.ErgoLikeContext -import sigmastate.{FixedCost, JitCost, PerItemCost, TypeBasedCost} +import sigmastate.{ErgoTree, FixedCost, JitCost, PerItemCost, TypeBasedCost} import sigmastate.Values._ import sigmastate.eval.Profiler import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv @@ -14,6 +14,7 @@ import debox.{Buffer => DBuffer} import sigma.VersionContext import sigma.ast.SType import sigma.data.SigmaBoolean + import scala.collection.compat.immutable.ArraySeq import scala.util.DynamicVariable diff --git a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala index 1a6c21d420..64652b5158 100644 --- a/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala +++ b/interpreter/shared/src/main/scala/sigmastate/interpreter/ProverUtils.scala @@ -2,7 +2,7 @@ package sigmastate.interpreter import sigma.data.{ProveDHTuple, ProveDlog, SigmaBoolean, SigmaConjecture, SigmaLeaf} import sigmastate._ -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.crypto.DLogProtocol.DLogInteractiveProver import sigmastate.crypto.DiffieHellmanTupleInteractiveProver diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala index c26e1b727a..c540d53a0f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala @@ -1,11 +1,11 @@ package sigmastate.serialization import org.ergoplatform.validation.ValidationRules.{CheckDeserializedScriptIsSigmaProp, CheckHeaderSizeBit} -import sigmastate.Values.{Constant, ErgoTree, UnparsedErgoTree} +import sigmastate.Values.Constant import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.lang.Terms.ValueOps import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.Values.ErgoTree.EmptyConstants +import sigmastate.ErgoTree.EmptyConstants import sigma.util.safeNewArray import sigmastate.utxo.ComplexityTable import debox.cfor @@ -14,6 +14,7 @@ import sigma.ast.SType import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} import sigma.validation.{SigmaValidationSettings, ValidationException} import sigma.validation.ValidationRules.CheckPositionLimit +import sigmastate.{ErgoTree, UnparsedErgoTree} import java.util diff --git a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala index c4ed244170..be7fae9a1e 100644 --- a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala +++ b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala @@ -1,7 +1,7 @@ package sigmastate import org.scalatest.matchers.should.Matchers -import sigmastate.Values.{ErgoTree, SigmaPropValue} +import sigmastate.Values.SigmaPropValue import org.ergoplatform.ErgoTreePredef import sigma.VersionTesting import sigma.data.SigmaBoolean diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/ContextEnrichingProverInterpreter.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/ContextEnrichingProverInterpreter.scala index 91155d96ca..25b1c796c6 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/ContextEnrichingProverInterpreter.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/ContextEnrichingProverInterpreter.scala @@ -1,7 +1,8 @@ package sigmastate.helpers import sigma.ast.SType -import sigmastate.Values.{ErgoTree, EvaluatedValue} +import sigmastate.ErgoTree +import sigmastate.Values.EvaluatedValue import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.interpreter.{ContextExtension, CostedProverResult, HintsBag, ProverInterpreter} diff --git a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala index 8e4a459c2c..27c8db01e5 100644 --- a/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala +++ b/interpreter/shared/src/test/scala/sigmastate/helpers/TestingHelpers.scala @@ -6,7 +6,7 @@ import scorex.util.ModifierId import sigma.data.{AvlTreeData, CollOverArray, PairOfCols} import sigma.validation.SigmaValidationSettings import sigma.{Coll, Header, PreHeader} -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.eval._ import sigmastate.interpreter.ContextExtension diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala index 98085fbb3f..fbad1b6320 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/DataSerializerSpecification.scala @@ -5,7 +5,7 @@ import org.ergoplatform.ErgoBox import org.scalacheck.Arbitrary._ import sigma.data.{RType, SigmaBoolean, TupleColl} import sigma.ast.SCollection.SByteArray -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate._ import sigmastate.eval._ import sigma.{AvlTree, Colls, Evaluation} diff --git a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala index 343d5bfc42..2872e3e6ea 100644 --- a/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala +++ b/interpreter/shared/src/test/scala/special/sigma/ContractsTestkit.scala @@ -2,7 +2,7 @@ package sigma import sigma.Extensions.ArrayOps import sigma.data.{AvlTreeData, RType} -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.Values import sigmastate.eval._ import sigmastate.eval.Extensions._ diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index f88fb0c10d..927a5197d1 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -10,14 +10,14 @@ import sigma.data._ import scorex.crypto.authds.{ADKey, ADValue} import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId -import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, ErgoTree, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} +import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceholder, FalseLeaf, IntConstant, LongConstant, SigmaPropConstant, TrueLeaf} import sigma.Extensions.ArrayOps import sigmastate.eval.{CBox, CHeader, CPreHeader, CSigmaDslBuilder, SigmaDsl} import sigmastate.helpers.TestingCommons import sigmastate.serialization.ErgoTreeSerializer import sigmastate.serialization.generators.ObjectGenerators import sigmastate.utils.Helpers -import sigmastate._ +import sigmastate.{ErgoTree, _} import sigma.ast.{SBoolean, SSigmaProp} import sigma.crypto.EcPointType diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index ec446fbb72..d5ecd08fee 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -37,9 +37,9 @@ class SigmaCompiler(_compiler: sigmastate.lang.SigmaCompiler) extends js.Object require(prop.tpe.isSigmaProp, s"Expected SigmaProp expression type bue got ${prop.tpe}: $prop") val tree = if (segregateConstants) { - Values.ErgoTree.withSegregation(additionalHeaderFlags, prop.asSigmaProp) + sigmastate.ErgoTree.withSegregation(additionalHeaderFlags, prop.asSigmaProp) } else { - Values.ErgoTree.withoutSegregation(additionalHeaderFlags, prop.asSigmaProp) + sigmastate.ErgoTree.withoutSegregation(additionalHeaderFlags, prop.asSigmaProp) } new ErgoTree(tree) } diff --git a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala index 23fb1754ac..587fb8b76f 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/dsl/ContractSpec.scala @@ -5,7 +5,7 @@ import sigmastate.interpreter.{CostedProverResult, ProverResult} import sigma.data.RType import org.ergoplatform.{ErgoBox, ErgoLikeContext} import sigma.{AnyValue, Coll, SigmaDslBuilder, SigmaProp} -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.eval.{CSigmaDslBuilder, IRContext} import scala.util.Try diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 65b369a496..3da2e95979 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform.ErgoAddressEncoder.{MainnetNetworkPrefix, TestnetNetwork import org.scalatest.{Assertion, TryValues} import scorex.crypto.hash.Blake2b256 import scorex.util.encode.Base58 -import sigmastate.Values.{ByteArrayConstant, Constant, ErgoTree, IntConstant, UnparsedErgoTree} +import sigmastate.Values.{ByteArrayConstant, Constant, IntConstant} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.eval.InvalidType import sigmastate.helpers.TestingHelpers._ @@ -18,7 +18,7 @@ import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.ValueSerializer import sigmastate.utils.Helpers._ -import sigmastate.{CompilerCrossVersionProps, SigmaAnd} +import sigmastate.{CompilerCrossVersionProps, ErgoTree, SigmaAnd, UnparsedErgoTree} import sigma.SigmaDslTesting import sigma.ast.SType import sigma.data.ProveDlog diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala index 683c6adc42..7ca1e50956 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoTreePredefSpec.scala @@ -11,7 +11,7 @@ import sigma.Colls import sigma.ast.SByte import sigma.data.{AvlTreeData, Digest32Coll, ProveDlog, TrivialProp} import sigma.util.BenchmarkUtil.measure -import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, IntConstant, SigmaPropConstant} +import sigmastate.Values.{ByteArrayConstant, CollectionConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index 90bf013059..4b6650eb8e 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -13,12 +13,13 @@ import scala.util.Try import org.ergoplatform.{ErgoBox, ErgoLikeContext} import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import sigma.ast.SType -import sigmastate.Values.{ErgoTree, EvaluatedValue} +import sigmastate.Values.EvaluatedValue import sigmastate.eval.IRContext import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps import sigma.{AnyValue, Evaluation, SigmaProp} +import sigmastate.ErgoTree case class TestContractSpec(testSuite: CompilerTestingCommons)(implicit val IR: IRContext) extends ContractSpec { diff --git a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala index ea1102d893..98035bee6d 100644 --- a/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala +++ b/sc/shared/src/test/scala/sigma/DataValueComparerSpecification.scala @@ -4,7 +4,7 @@ import org.scalatest.BeforeAndAfterAll import sigma.data.{CSigmaProp, RType, TrivialProp} import sigma.util.BenchmarkUtil import sigmastate.{DataValueComparer, JitCost} -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.eval.{Profiler, SigmaDsl} import sigmastate.helpers.SigmaPPrint import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator, EvalSettings, TracedCost} diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index 3ab02ebcad..4ab524c701 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -19,7 +19,7 @@ import sigma.util.CollectionUtil._ import sigma.util.Extensions._ import sigma.util.StringUtil.StringUtilExtensions import sigma.ast.SType.AnyOps -import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, ErgoTree, IntConstant, SValue} +import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, IntConstant, SValue} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.crypto.SigmaProtocolPrivateInput import sigmastate.eval.Extensions.SigmaBooleanOps @@ -36,7 +36,7 @@ import sigmastate.utxo.{DeserializeContext, DeserializeRegister, GetVar, OptionG import sigma.ast.{SOption, SSigmaProp, SType} import sigma.validation.ValidationRules.CheckSerializableTypeCode import sigma.validation.{SigmaValidationSettings, ValidationException} -import sigmastate.eval +import sigmastate.{ErgoTree, eval} import scala.collection.mutable import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/CompilerTestsBase.scala b/sc/shared/src/test/scala/sigmastate/CompilerTestsBase.scala index fd012b3493..e53cec4c41 100644 --- a/sc/shared/src/test/scala/sigmastate/CompilerTestsBase.scala +++ b/sc/shared/src/test/scala/sigmastate/CompilerTestsBase.scala @@ -3,7 +3,7 @@ package sigmastate import scala.util.DynamicVariable import sigmastate.lang.{CompilerResult, CompilerSettings, SigmaCompiler, TransformingSigmaBuilder} import sigmastate.interpreter.Interpreter.ScriptEnv -import sigmastate.Values.{ErgoTree, SValue, SigmaPropValue, Value} +import sigmastate.Values.{SValue, SigmaPropValue, Value} import org.ergoplatform.ErgoAddressEncoder.TestnetNetworkPrefix import sigma.ast.SType import sigmastate.serialization.ValueSerializer diff --git a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala index 9e4d9c2c1e..1dd7157349 100644 --- a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala @@ -3,7 +3,7 @@ package sigmastate import org.ergoplatform.ErgoBox.AdditionalRegisters import org.ergoplatform._ import scorex.util.ModifierId -import sigmastate.Values.ErgoTree.{DefaultHeader, updateVersionBits} +import sigmastate.ErgoTree.{DefaultHeader, updateVersionBits} import sigmastate.Values._ import sigma.VersionContext.MaxSupportedScriptVersion import sigma.ast.{SBoolean, SBox, SCollection, SType} diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index 28ff4fafb0..483799c98e 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -10,8 +10,8 @@ import sigma.data.AvlTreeData import sigma.serialization.SerializerException import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckSerializableTypeCode, CheckTypeCode, CheckTypeWithMethods, trySoftForkable} import sigma.validation.{ChangedRule, ReplacedRule, SigmaValidationSettings, ValidationException, ValidationRule} -import sigmastate.Values.ErgoTree.EmptyConstants -import sigmastate.Values.{ByteArrayConstant, ErgoTree, IntConstant, NotReadyValueInt, UnparsedErgoTree, ValueCompanion} +import sigmastate.ErgoTree.EmptyConstants +import sigmastate.Values.{ByteArrayConstant, IntConstant, NotReadyValueInt, ValueCompanion} import sigmastate.exceptions.InterpreterException import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala index 76cc532937..0be1c73a0a 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/CompilerTestingCommons.scala @@ -4,7 +4,6 @@ import org.ergoplatform._ import org.ergoplatform.validation.ValidationSpecification import org.scalacheck.Arbitrary.arbByte import org.scalacheck.Gen -import org.scalatest.Assertion import sigma.util.BenchmarkUtil import scalan.TestContexts import sigma.ast.{SOption, SType} @@ -12,7 +11,7 @@ import sigma.{Colls, Evaluation, TestUtils} import sigma.data.{RType, SigmaBoolean} import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckSerializableTypeCode -import sigmastate.Values.{Constant, ErgoTree, SValue, SigmaPropValue} +import sigmastate.Values.{Constant, SValue, SigmaPropValue} import sigmastate.eval._ import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.ContextExtension.VarBinding @@ -21,7 +20,7 @@ import sigmastate.interpreter.Interpreter.ScriptEnv import sigmastate.interpreter._ import sigmastate.lang.{CompilerSettings, SigmaCompiler, Terms} import sigmastate.serialization.SigmaSerializer -import sigmastate.{CompilerTestsBase, JitCost} +import sigmastate.{CompilerTestsBase, ErgoTree, JitCost} import scala.language.implicitConversions import scala.reflect.ClassTag diff --git a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index 3460090424..eea742e49e 100644 --- a/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sc/shared/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -10,11 +10,10 @@ import sigma.crypto.EcPointType import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, PrimitiveType, TrivialProp} import sigma.serialization.GroupElementSerializer import sigma.{Coll, GroupElement} -import sigmastate.Values.{ConstantNode, ErgoTree, FuncValue, ValueCompanion} +import sigmastate.Values.{ConstantNode, FuncValue, ValueCompanion} import sigmastate._ import sigmastate.crypto.GF2_192_Poly import sigmastate.interpreter.{CompanionDesc, FixedCostItem, MethodDesc} -import sigmastate.lang.Terms import sigmastate.lang.Terms.MethodCall import sigmastate.utxo.SelectField diff --git a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala index b13e1e48f2..582aefca5a 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala @@ -9,8 +9,8 @@ import sigma.data.CBigInt import sigma.serialization.{ReaderPositionLimitExceeded, SerializerException} import sigma.util.Extensions.SigmaPropOps import sigma.validation.ValidationException -import sigmastate.Values.ErgoTree.EmptyConstants -import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, ErgoTree, IntConstant, ShortConstant, SigmaPropValue, UnparsedErgoTree} +import sigmastate.ErgoTree.EmptyConstants +import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, IntConstant, ShortConstant, SigmaPropValue} import sigmastate._ import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval.IRContext diff --git a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala index 99f355eb27..46a11a5568 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala @@ -5,8 +5,8 @@ import org.ergoplatform._ import scorex.crypto.authds.{ADDigest, ADKey, ADValue} import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert, Remove} import scorex.crypto.hash.{Blake2b256, Digest32} -import sigmastate.Values -import sigmastate.Values.{ErgoTree, LongConstant} +import sigmastate.{ErgoTree, Values} +import sigmastate.Values.LongConstant import sigmastate.eval._ import sigmastate.helpers.{BlockchainState, CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestProvingInterpreter, ErgoTransactionValidator} import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoinEmissionSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoinEmissionSpecification.scala index a9db5642cc..83128dc234 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoinEmissionSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoinEmissionSpecification.scala @@ -4,9 +4,8 @@ import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import sigma.Colls import sigma.ast.{SBoolean, SBox, SInt, SLong, SOption} -import sigmastate.Values.{BlockValue, ErgoTree, IntConstant, LongConstant, ValDef, ValUse} -import sigmastate._ -import sigmastate.eval._ +import sigmastate.Values.{BlockValue, IntConstant, LongConstant, ValDef, ValUse} +import sigmastate.{ErgoTree, _} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting} import sigmastate.interpreter.ContextExtension diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index 74345ceafe..689586f555 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -6,11 +6,11 @@ import org.scalatest.TryValues._ import sigmastate.crypto.DLogProtocol.DLogProverInput import scorex.crypto.hash.Blake2b256 import sigma.data.{AvlTreeData, ProveDlog} -import sigmastate.Values.{BooleanConstant, ByteArrayConstant, ErgoTree} +import sigmastate.Values.{BooleanConstant, ByteArrayConstant} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ -import sigmastate.CompilerCrossVersionProps +import sigmastate.{CompilerCrossVersionProps, ErgoTree} class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala index 2a433c411d..39f33ef403 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ErgoTree.scala @@ -9,7 +9,7 @@ import scala.scalajs.js.annotation.JSExportTopLevel /** An exported JavaScript class wrapping the Scala `Values.ErgoTree` type. */ @JSExportTopLevel("ErgoTree") -class ErgoTree(tree: Values.ErgoTree) extends js.Object { +class ErgoTree(tree: sigmastate.ErgoTree) extends js.Object { /** The first byte of serialized byte array which determines interpretation of the rest of the array. */ def header(): Byte = tree.header @@ -50,13 +50,13 @@ object ErgoTree extends js.Object { * @param hex a hexadecimal string representing the serialized ErgoTree */ def fromHex(hex: String): ErgoTree = - new ErgoTree(Values.ErgoTree.fromHex(hex)) + new ErgoTree(sigmastate.ErgoTree.fromHex(hex)) /** Deserializes an ErgoTree instance from an array of bytes. * * @param bytes an array of bytes representing the serialized ErgoTree */ def fromBytes(bytes: Array[Byte]): ErgoTree = { - new ErgoTree(Values.ErgoTree.fromBytes(bytes)) + new ErgoTree(sigmastate.ErgoTree.fromBytes(bytes)) } } diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala index 4a3577c993..6abacd09eb 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala @@ -10,8 +10,8 @@ import sigma.Evaluation import sigma.ast.SType import sigma.serialization.SerializerException import sigma.util.safeNewArray -import sigmastate.Values.ErgoTree.headerWithVersion -import sigmastate.Values.{ErgoTree, _} +import sigmastate.ErgoTree.headerWithVersion +import sigmastate.Values._ import sigmastate._ import sigmastate.lang.{DeserializationSigmaBuilder, StdSigmaBuilder} import sigmastate.serialization.{DataSerializer, _} @@ -146,7 +146,7 @@ case class ContractTemplate( * provided in the template. * @return `ErgoTree` generated by replacing the template parameters with the value provided in `paramValues`. */ - def applyTemplate(version: Option[Byte], paramValues: Map[String, Values.Constant[SType]]): Values.ErgoTree = { + def applyTemplate(version: Option[Byte], paramValues: Map[String, Values.Constant[SType]]): ErgoTree = { require(treeVersion.isDefined || version.isDefined, "ErgoTreeVersion must be provided to generate the ErgoTree.") val nConsts = constTypes.size val requiredParameterNames = diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index d000a9f060..4b4d4a4a4f 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -9,17 +9,17 @@ import org.ergoplatform.sdk.wallet.secrets.{DerivationPath, ExtendedSecretKey} import org.ergoplatform.sdk.wallet.{Constants, TokensMap} import org.ergoplatform.settings.ErgoAlgos import scorex.crypto.authds.ADKey -import sigmastate.utils.Helpers._ import scorex.util.encode.Base16 import scorex.util.{ModifierId, bytesToId, idToBytes} import sigma.ast.SType import sigma.crypto.{CryptoFacade, EcPointType} import sigma.data.ExactIntegral.LongIsExactIntegral -import sigma.data.{Digest32Coll, InverseIso, Iso, ProveDHTuple, ProveDlog, RType, SigmaBoolean, SigmaConstants} +import sigma.data.{Digest32Coll, Iso, ProveDHTuple, ProveDlog, RType, SigmaBoolean, SigmaConstants} import sigma.serialization.GroupElementSerializer import sigma.util.StringUtil.StringUtilExtensions import sigma.{AnyValue, AvlTree, Coll, Colls, Evaluation, GroupElement, Header} -import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant} +import sigmastate.ErgoTree +import sigmastate.Values.{Constant, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DiffieHellmanTupleProverInput import sigmastate.eval.CSigmaDslBuilder import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index e087a0589c..37643dc9e4 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -1,40 +1,30 @@ package org.ergoplatform.sdk -import java.math.BigInteger import cats.syntax.either._ import io.circe._ import io.circe.syntax._ -import org.ergoplatform.ErgoBox.{BoxId, NonMandatoryRegisterId, Token, TokenId} +import org.ergoplatform.ErgoBox.{NonMandatoryRegisterId, Token, TokenId} +import org.ergoplatform._ import org.ergoplatform.settings.ErgoAlgos import org.ergoplatform.validation.SigmaValidationSettingsSerializer import scorex.crypto.authds.{ADDigest, ADKey} import scorex.crypto.hash.Digest32 import scorex.util.ModifierId -import sigmastate.Values.{ErgoTree, EvaluatedValue} -import sigmastate.eval.Extensions._ -import sigmastate.eval._ -import sigmastate.interpreter.{ContextExtension, ProverResult} -import sigma.{AnyValue, Coll, Colls, Header, PreHeader, SigmaException} - -import scala.util.Try -import sigmastate.utils.Helpers._ -import org.ergoplatform.ErgoBox -import sigmastate.serialization.ValueSerializer -import org.ergoplatform.DataInput -import org.ergoplatform.Input -import org.ergoplatform.UnsignedInput -import sigmastate.serialization.ErgoTreeSerializer -import org.ergoplatform.ErgoLikeTransaction -import org.ergoplatform.UnsignedErgoLikeTransaction -import org.ergoplatform.ErgoLikeTransactionTemplate -import org.ergoplatform.ErgoBoxCandidate -import org.ergoplatform.ErgoLikeContext import sigma.Extensions.ArrayOps import sigma.ast.SType import sigma.data.{AvlTreeData, AvlTreeFlags, CBigInt, Digest32Coll, WrapperOf} import sigma.validation.SigmaValidationSettings +import sigma.{AnyValue, Coll, Colls, Header, PreHeader, SigmaException} +import sigmastate.ErgoTree +import sigmastate.Values.EvaluatedValue +import sigmastate.eval.Extensions._ +import sigmastate.eval._ +import sigmastate.interpreter.{ContextExtension, ProverResult} +import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} +import java.math.BigInteger import scala.collection.mutable +import scala.util.Try trait JsonCodecs { diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala index 7244867d6d..89314fe97d 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/OutBoxBuilder.scala @@ -6,7 +6,8 @@ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate} import sigma.Colls import sigma.ast.SType import sigma.data.{RType, SigmaConstants} -import sigmastate.Values.{Constant, ErgoTree, EvaluatedValue} +import sigmastate.ErgoTree +import sigmastate.Values.{Constant, EvaluatedValue} import scala.collection.mutable.ArrayBuffer diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala index 1448a2b4b2..148f25bc43 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ReducingInterpreter.scala @@ -8,7 +8,7 @@ import org.ergoplatform.validation.ValidationRules import org.ergoplatform.{ErgoLikeContext, ErgoLikeInterpreter} import sigma.data.AvlTreeData import sigma.util.Extensions.LongOps -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.eval.addCostChecked import sigmastate.exceptions.CostLimitException import sigmastate.interpreter.Interpreter diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index c86f0f2125..ae93ea809a 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -6,7 +6,7 @@ import sigma.data.RType._ import scorex.crypto.authds.avltree.batch.BatchAVLProver import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.ModifierId -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.crypto.CryptoConstants import sigmastate.eval._ import sigma._ diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala index 718332943b..5fe0cb8dee 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/JsonSerializationSpec.scala @@ -13,7 +13,8 @@ import scorex.util.encode.Base16 import sigma.ast.SType import sigma.data.{AvlTreeData, Digest32Coll, ProveDlog} import sigma.{Coll, Header, PreHeader} -import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} +import sigmastate.ErgoTree +import sigmastate.Values.{ByteArrayConstant, ByteConstant, EvaluatedValue, IntConstant, LongArrayConstant, SigmaPropConstant} import sigmastate.crypto.CryptoConstants import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.SerializationSpecification diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/generators/ObjectGenerators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/generators/ObjectGenerators.scala index cab8d1df1a..6fb22792b6 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/generators/ObjectGenerators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/generators/ObjectGenerators.scala @@ -5,7 +5,7 @@ import org.ergoplatform.validation.ValidationSpecification import org.scalacheck.Gen import sigma.ast.SType import sigmastate.TestsBase -import sigmastate.Values.ErgoTree +import sigmastate.ErgoTree import sigmastate.serialization.generators.{ConcreteCollectionGenerators, TypeGenerators, ObjectGenerators => InterpreterObjectGenerators} import scala.util.Random diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala index c727352035..bf490ce448 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/wallet/utils/Generators.scala @@ -8,7 +8,7 @@ import org.ergoplatform.sdk.wallet.settings.EncryptionSettings import org.scalacheck.Arbitrary.arbByte import org.scalacheck.{Arbitrary, Gen} import scorex.crypto.authds.ADKey -import sigmastate.Values.{ByteArrayConstant, CollectionConstant, ErgoTree, EvaluatedValue, FalseLeaf, TrueLeaf} +import sigmastate.Values.{ByteArrayConstant, CollectionConstant, EvaluatedValue, FalseLeaf, TrueLeaf} import sigma.ast.{SByte, SType} import scorex.util._ import org.ergoplatform.{ErgoBox, ErgoBoxCandidate, ErgoTreePredef, UnsignedErgoLikeTransaction, UnsignedInput} @@ -16,6 +16,7 @@ import sigma.Extensions.ArrayOps import scorex.util.{ModifierId, bytesToId} import sigma.crypto.CryptoFacade import sigma.data.{Digest32Coll, ProveDlog} +import sigmastate.ErgoTree import sigmastate.eval.Extensions.{EvalIterableOps, SigmaBooleanOps} import sigmastate.helpers.TestingHelpers._ From 731409f162dc6e5dd44b50fa16442583b47ad43b Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 20 Sep 2023 22:44:56 +0200 Subject: [PATCH 31/34] core-serializers2: fix Scala 2.11 compilation --- sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala | 1 + sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala | 1 + 2 files changed, 2 insertions(+) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala index 4b4d4a4a4f..1f3359f3d7 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JavaHelpers.scala @@ -23,6 +23,7 @@ import sigmastate.Values.{Constant, EvaluatedValue, SValue, SigmaPropConstant} import sigmastate.crypto.DiffieHellmanTupleProverInput import sigmastate.eval.CSigmaDslBuilder import sigmastate.serialization.{ErgoTreeSerializer, SigmaSerializer, ValueSerializer} +import sigmastate.utils.Helpers._ // required for Scala 2.11 import java.lang.{Boolean => JBoolean, Byte => JByte, Integer => JInt, Long => JLong, Short => JShort, String => JString} import java.math.BigInteger diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala index 37643dc9e4..38e666d093 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/JsonCodecs.scala @@ -21,6 +21,7 @@ import sigmastate.eval.Extensions._ import sigmastate.eval._ import sigmastate.interpreter.{ContextExtension, ProverResult} import sigmastate.serialization.{ErgoTreeSerializer, ValueSerializer} +import sigmastate.utils.Helpers._ // required for Scala 2.11 import java.math.BigInteger import scala.collection.mutable From a8ba147292988328c41db54fd058d0390c53bf2f Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 4 Oct 2023 13:40:03 +0200 Subject: [PATCH 32/34] core-serializers: fixes after merge --- interpreter/shared/src/main/scala/sigmastate/Values.scala | 3 ++- .../scala/sigmastate/crypto/SigningSpecification.scala | 1 + .../test/scala/sigmastate/helpers/SigmaPPrintSpec.scala | 1 + .../test/scala/org/ergoplatform/dsl/TestContractSpec.scala | 4 +--- sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala | 1 + .../scala/sigmastate/SoftForkabilitySpecification.scala | 7 ++++--- .../ColdWalletAdvContractExampleSpecification.scala | 4 ++-- .../examples/ColdWalletContractExampleSpecification.scala | 2 +- .../utxo/examples/CoopExampleSpecification.scala | 1 + .../sigmastate/utxo/examples/MixExampleSpecification.scala | 3 +-- .../utxo/examples/RPSGameExampleSpecification.scala | 6 ++---- .../utxo/examples/ReversibleTxExampleSpecification.scala | 2 +- .../src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala | 2 +- 13 files changed, 19 insertions(+), 18 deletions(-) diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index 2c758fa023..7c55c26805 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -14,7 +14,7 @@ import sigma.util.CollectionUtil._ import sigma.util.Extensions._ import sigma.validation.ValidationException import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} -import sigmastate.Values.ErgoTree.substConstants +import sigmastate.Values.ErgoTree.{HeaderType, substConstants} import sigmastate.crypto.CryptoConstants import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval._ @@ -29,6 +29,7 @@ import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization._ import sigmastate.utxo._ +import supertagged.TaggedType import java.math.BigInteger import java.util.{Arrays, Objects} diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala index d679f2e23b..004b4eadc5 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala @@ -5,6 +5,7 @@ import scorex.util.encode.Base16 import sigma.data.{CAND, COR, ProveDHTuple, SigmaBoolean} import sigma.serialization.ProveDHTupleSerializer import sigmastate.AtLeast +import sigmastate.Values.ErgoTree import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, TestingCommons} import sigmastate.interpreter.{ContextExtension, HintsBag, ProverResult} diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index 4810e35f6f..221a45592a 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -7,6 +7,7 @@ import sigma.Extensions.ArrayOps import sigma.SigmaDslTesting import sigma.ast._ import sigma.data.{AvlTreeData, AvlTreeFlags, CollType} +import sigmastate.Values.ErgoTree.HeaderType import sigmastate.Values._ import sigmastate._ import sigmastate.eval._ diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index 974d98b8bd..4176d13f66 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -15,10 +15,8 @@ import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token} import sigma.ast.SType import sigmastate.Values.{ErgoTree, EvaluatedValue} import sigmastate.eval.{CAnyValue, IRContext} -import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} -import sigmastate.eval.{CSigmaProp, IRContext, CAnyValue} +import sigmastate.eval.{IRContext, CAnyValue} import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, CompilerTestingCommons} -import sigmastate.eval.{CAnyValue, CSigmaProp, IRContext} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index ea4cb0c014..e5e78b4132 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -17,6 +17,7 @@ import sigma.data.RType._ import sigma.data._ import sigma.util.Extensions.{BooleanOps, IntOps, LongOps} import sigma.{VersionContext, data, _} +import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader} import sigmastate.Values.{IntConstant, _} import sigmastate._ import sigmastate.eval.Extensions.{AvlTreeOps, ByteExt, IntExt, LongExt, ShortExt} diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index b396eb9725..44329c074c 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -3,13 +3,15 @@ package sigmastate import org.ergoplatform._ import org.ergoplatform.validation.ValidationRules._ import org.scalatest.BeforeAndAfterAll +import sigma.{Colls, SigmaTestingData} +import sigma.ast.{SBoolean, SCollection, SContext, SFunc, SGlobal, SInt} import sigma.ast.SPrimType.MaxPrimTypeCode import sigma.ast.TypeCodes.LastConstantCode import sigma.data.AvlTreeData import sigma.serialization.SerializerException import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckSerializableTypeCode, CheckTypeCode, CheckTypeWithMethods, trySoftForkable} import sigma.validation.{ChangedRule, ReplacedRule, SigmaValidationSettings, ValidationException, ValidationRule} -import sigmastate.Values.ErgoTree.EmptyConstants +import sigmastate.Values.ErgoTree.{EmptyConstants, HeaderType, ZeroHeader, setSizeBit} import sigmastate.Values.{ByteArrayConstant, ErgoTree, IntConstant, NotReadyValueInt, UnparsedErgoTree, ValueCompanion} import sigmastate.exceptions.InterpreterException import sigmastate.helpers.TestingHelpers._ @@ -20,8 +22,7 @@ import sigmastate.interpreter.{ContextExtension, ErgoTreeEvaluator, ProverResult import sigmastate.lang.Terms._ import sigmastate.serialization.SigmaSerializer.startReader import sigmastate.serialization.{DataSerializer, _} -import sigmastate.serialization.ValueCodes.{LastConstantCode, OpCode} -import sigmastate.serialization._ +import sigmastate.serialization.ValueCodes.OpCode import sigmastate.utils.Helpers._ import sigmastate.utxo.DeserializeContext diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala index 82527da48a..fe5f090f73 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala @@ -4,8 +4,8 @@ import org.ergoplatform.ErgoBox.{R4, R5, R6} import org.ergoplatform._ import sigma.data.AvlTreeData import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.{LongConstant, IntConstant} -import sigmastate.helpers.{ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, CompilerTestingCommons, ContextEnrichingTestProvingInterpreter} +import sigmastate.Values.{ErgoTree, IntConstant, LongConstant} +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala index ff2597cbe1..021da2bafd 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala @@ -6,7 +6,7 @@ import sigma.data.AvlTreeData import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.{IntConstant, LongConstant} +import sigmastate.Values.{ErgoTree, IntConstant, LongConstant} import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index 58b7fa82f4..b750a56e10 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -11,6 +11,7 @@ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingIn import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms._ import sigmastate.CompilerCrossVersionProps +import sigmastate.Values.ErgoTree.ZeroHeader class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index 84d2d4d769..cc37d018b8 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -6,14 +6,13 @@ import scorex.crypto.hash.Blake2b256 import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog} import sigma.util.Extensions.EcpOps import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.GroupElementConstant +import sigmastate.Values.{ErgoTree, GroupElementConstant} import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ import sigmastate.eval._ -import sigmastate.eval.Extensions._ class MixExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala index 5630e84b5d..7c58ffe0d4 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala @@ -5,12 +5,10 @@ import org.ergoplatform.ErgoBox.{R4, R5, R6, R7} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random import sigma.data.{AvlTreeData, ProveDlog} -import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} +import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ -import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} -import sigmastate.crypto.DLogProtocol.ProveDlog -import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.interpreter.Interpreter._ import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala index 5ce5163b33..cf5c8a992c 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform.ErgoBox.{R4, R5} import org.ergoplatform._ import scorex.crypto.hash.Blake2b256 import sigma.data.AvlTreeData -import sigmastate.Values.{IntConstant, SigmaPropConstant} +import sigmastate.Values.{ErgoTree, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ diff --git a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala index 9540ea2ec2..32a49802fc 100644 --- a/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala +++ b/sdk/js/src/test/scala/org/ergoplatform/sdk/js/IsosSpec.scala @@ -3,7 +3,7 @@ package org.ergoplatform.sdk.js import org.ergoplatform.ErgoBox.{AdditionalRegisters, BoxId, TokenId} import org.ergoplatform._ import org.ergoplatform.sdk.wallet.protocol.context.{BlockchainStateContext, CBlockchainStateContext} -import org.ergoplatform.sdk.{ExtendedInputBox, Iso} +import org.ergoplatform.sdk.ExtendedInputBox import org.scalacheck.{Arbitrary, Gen} import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec From 69a914805cd908d7edca7d3d50370f7fb32ee224 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 5 Oct 2023 00:17:08 +0200 Subject: [PATCH 33/34] core-serializers2: fixes after merge --- .../scala/org/ergoplatform/ErgoAddress.scala | 13 +- .../org/ergoplatform/ErgoTreePredef.scala | 3 +- .../validation/ValidationRules.scala | 8 +- .../src/main/scala/sigmastate/ErgoTree.scala | 140 +++++++++++------- .../main/scala/sigmastate/UnprovenTree.scala | 5 +- .../src/main/scala/sigmastate/Values.scala | 3 +- .../serialization/ErgoTreeSerializer.scala | 3 +- .../src/test/scala/sigmastate/TestsBase.scala | 2 +- .../crypto/SigningSpecification.scala | 4 +- .../generators/ObjectGenerators.scala | 2 +- .../special/sigma/SigmaTestingData.scala | 2 +- .../sigmastate/lang/js/SigmaCompiler.scala | 6 +- .../sigmastate/helpers/SigmaPPrintSpec.scala | 3 +- .../ErgoAddressSpecification.scala | 2 +- .../ergoplatform/dsl/TestContractSpec.scala | 1 + .../scala/sigma/SigmaDslSpecification.scala | 2 +- .../test/scala/sigma/SigmaDslTesting.scala | 2 +- .../sigmastate/ErgoTreeSpecification.scala | 2 +- .../ScriptVersionSwitchSpecification.scala | 4 +- .../SoftForkabilitySpecification.scala | 4 +- .../sigmastate/eval/ErgoScriptTestkit.scala | 5 +- .../sigmastate/eval/EvaluationTest.scala | 2 +- .../ErgoTreeSerializerSpecification.scala | 4 +- .../utxo/DistributedSigSpecification.scala | 2 - .../BlockchainSimulationTestingCommons.scala | 2 +- ...alletAdvContractExampleSpecification.scala | 4 +- ...ldWalletContractExampleSpecification.scala | 4 +- .../examples/CoopExampleSpecification.scala | 7 +- .../examples/MixExampleSpecification.scala | 4 +- .../RPSGameExampleSpecification.scala | 2 +- .../ReversibleTxExampleSpecification.scala | 2 +- .../TimedPaymentExampleSpecification.scala | 2 +- .../ergoplatform/sdk/ContractTemplate.scala | 4 +- .../org/ergoplatform/sdk/utils/Zero.scala | 2 +- .../sdk/ContractTemplateSpecification.scala | 2 +- 35 files changed, 131 insertions(+), 128 deletions(-) diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala index e204a5ce6a..458dee4d66 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoAddress.scala @@ -1,20 +1,19 @@ package org.ergoplatform -import scorex.utils.Ints import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix import scorex.crypto.hash.{Blake2b256, Digest32} import scorex.util.encode.Base58 +import scorex.utils.Ints +import sigma.ast.{SInt, SSigmaProp} +import sigma.data.ProveDlog +import sigma.serialization.GroupElementSerializer +import sigma.{Coll, SigmaException, VersionContext} +import sigmastate.ErgoTree.{ZeroHeader, setVersionBits} import sigmastate.Values._ import sigmastate._ import sigmastate.crypto.DLogProtocol.ProveDlogProp import sigmastate.serialization._ import sigmastate.utxo.{DeserializeContext, Slice} -import sigma.{Coll, SigmaException} -import sigma.{Coll, VersionContext} -import sigma.ast.{SInt, SSigmaProp} -import sigma.data.ProveDlog -import sigma.serialization.GroupElementSerializer -import sigmastate.Values.ErgoTree.{ZeroHeader, setVersionBits} import scala.util.Try diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala index 490c12193f..29a4b2738f 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoTreePredef.scala @@ -4,11 +4,10 @@ import org.ergoplatform.settings.MonetarySettings import sigma.ast.SCollection.SByteArray import sigma.ast.{SBox, SInt, SLong, SSigmaProp} import sigma.data.ProveDlog +import sigmastate.ErgoTree.{HeaderType, ZeroHeader} import sigmastate.crypto.CryptoConstants import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.Values.{FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} -import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader} -import sigmastate.Values.{ErgoTree, FalseSigmaProp, IntArrayConstant, IntConstant, LongConstant, SigmaPropConstant, SigmaPropValue, TrueSigmaProp, Value} import sigmastate.utxo._ import sigmastate.{ErgoTree, _} import sigmastate.lang.Terms.ValueOps diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala index cc7e8fdc00..e1d1993a21 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/validation/ValidationRules.scala @@ -6,15 +6,13 @@ import sigma.serialization.{InvalidOpCode, SerializerException} import sigma.util.Extensions.toUByte import sigma.validation.ValidationRules._ import sigma.validation._ +import sigmastate.ErgoTree.HeaderType import sigmastate.Values.SValue -import sigmastate.{ErgoTree, _} -import sigmastate.Values.ErgoTree.HeaderType -import sigmastate.Values.{ErgoTree, SValue} -import sigmastate._ import sigmastate.exceptions._ import sigmastate.serialization.ValueCodes.OpCode -import sigmastate.serialization.{ValueCodes, ValueSerializer} +import sigmastate.serialization.ValueSerializer import sigmastate.utxo.DeserializeContext +import sigmastate.{ErgoTree, _} /** All validation rules which are used to check soft-forkable conditions. Each validation * rule throws a [[org.ergoplatform.validation.ValidationException]]. Each diff --git a/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala b/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala index 8fe2bfbba5..9305b5f79c 100644 --- a/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/ErgoTree.scala @@ -2,16 +2,18 @@ package sigmastate import org.ergoplatform.settings.ErgoAlgos import scorex.util.encode.Base16 +import sigma.VersionContext import sigma.ast.SType import sigma.data.SigmaBoolean import sigma.kiama.rewriting.Rewriter.{everywherebu, strategy} import sigma.validation.ValidationException -import sigmastate.ErgoTree.substConstants +import sigmastate.ErgoTree.{HeaderType, substConstants} import sigmastate.Values.{Constant, ConstantPlaceholder, SValue, SigmaPropConstant, SigmaPropValue, Value} import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.lang.Terms.ValueOps import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.{ConstantStore, ErgoTreeSerializer, SigmaSerializer, ValueSerializer} +import supertagged.TaggedType import java.util.Objects import scala.collection.mutable @@ -84,7 +86,7 @@ case class UnparsedErgoTree(bytes: mutable.WrappedArray[Byte], error: Validation * this flag and provides it to the constructor. */ case class ErgoTree private[sigmastate]( - header: Byte, + header: HeaderType, constants: IndexedSeq[Constant[SType]], root: Either[UnparsedErgoTree, SigmaPropValue], private val givenComplexity: Int, @@ -92,7 +94,7 @@ case class ErgoTree private[sigmastate]( private val givenDeserialize: Option[Boolean] ) { def this( - header: Byte, + header: HeaderType, constants: IndexedSeq[Constant[SType]], root: Either[UnparsedErgoTree, SigmaPropValue]) = this( @@ -206,11 +208,13 @@ case class ErgoTree private[sigmastate]( } object ErgoTree { - /** Current version of ErgoTree serialization format (aka bite-code language version) */ - val VersionFlag: Byte = 0 + /** Represents information in ErgoTree header. */ + object HeaderType extends TaggedType[Byte] - /** Default value of ErgoTree.header byte */ - val DefaultHeader: Byte = (0 | VersionFlag).toByte + type HeaderType = HeaderType.Type + + /** Current version of ErgoTree serialization format (aka bite-code language version) */ + val VersionFlag: Byte = VersionContext.MaxSupportedScriptVersion /** Header flag to indicate that constant segregation should be applied. */ val ConstantSegregationFlag: Byte = 0x10 @@ -221,34 +225,66 @@ object ErgoTree { /** Header mask to extract version bits. */ val VersionMask: Byte = 0x07 + /** Header with all the flags set to 0 and version 0. */ + val ZeroHeader: HeaderType = HeaderType @@ 0.toByte + + /** Default value of ErgoTree.header byte */ + val DefaultHeader: HeaderType = ZeroHeader //HeaderType @@ (VersionFlag | SizeFlag).toByte + /** Default header with constant segregation enabled. */ - val ConstantSegregationHeader: Byte = (DefaultHeader | ConstantSegregationFlag).toByte + val ConstantSegregationHeader: HeaderType = HeaderType @@ (DefaultHeader | ConstantSegregationFlag).toByte /** @return true if the constant segregation flag is set to 1 in the given header byte. */ - @inline final def isConstantSegregation(header: Byte): Boolean = (header & ConstantSegregationFlag) != 0 + @inline final def isConstantSegregation(header: HeaderType): Boolean = (header & ConstantSegregationFlag) != 0 /** @return true if the size flag is set to 1 in the given header byte. */ - @inline final def hasSize(header: Byte): Boolean = (header & SizeFlag) != 0 + @inline final def hasSize(header: HeaderType): Boolean = (header & SizeFlag) != 0 /** @return a value of the version bits from the given header byte. */ - @inline final def getVersion(header: Byte): Byte = (header & VersionMask).toByte + @inline final def getVersion(header: HeaderType): Byte = (header & VersionMask).toByte - /** Update the version bits of the given header byte with the given version value. */ - @inline final def updateVersionBits(header: Byte, version: Byte): Byte = { + /** Update the version bits of the given header byte with the given version value, + * leaving all other bits unchanged. + */ + @inline final def setVersionBits(header: HeaderType, version: Byte): HeaderType = { require(version < 8, s"ErgoTree.version should be < 8: $version") - (header | version).toByte + val h = header & (~VersionMask) // clear version bits + HeaderType @@ (h | version).toByte + } + + /** Sets the required bit in the given header: + * - The SizeFlag is set if version > 0 + */ + @inline final def setRequiredBits(header: HeaderType): HeaderType = { + if (getVersion(header) > 0) { + // set SizeFlag if version is greater then 0 (see require() in ErgoTree constructor) + HeaderType @@ (header | ErgoTree.SizeFlag).toByte + } else + header + } + + /** Sets the ConstantSegregationFlag in the given header */ + @inline final def setConstantSegregation(header: HeaderType): HeaderType = { + HeaderType @@ (header | ConstantSegregationFlag).toByte + } + + /** Sets the ConstantSegregationFlag in the given header */ + @inline final def setSizeBit(header: HeaderType): HeaderType = { + HeaderType @@ (header | SizeFlag).toByte } /** Creates valid header byte with the given version. * The SizeFlag is set if version > 0 */ - @inline def headerWithVersion(version: Byte): Byte = { - // take default header and embedd the given version in it - var h = updateVersionBits(DefaultHeader, version) - if (version > 0) { - // set SizeFlag if version is greater then 0 (see require() in ErgoTree constructor) - h = (h | ErgoTree.SizeFlag).toByte - } - h + @inline final def defaultHeaderWithVersion(version: Byte): HeaderType = { + headerWithVersion(DefaultHeader, version) + } + + /** Creates valid header byte with the given version. + * The SizeFlag is set if version > 0 */ + @inline final def headerWithVersion(header: HeaderType, version: Byte): HeaderType = { + // take the header and embedd the given version in it + val h = setVersionBits(header, version) + setRequiredBits(h) } /** Substitute [[ConstantPlaceholder]] nodes in the given expression with the constants @@ -270,57 +306,51 @@ object ErgoTree { /** Create an ErgoTree with the given parameters. */ def apply( - header: Byte, + header: HeaderType, constants: IndexedSeq[Constant[SType]], root: SigmaPropValue): ErgoTree = { - new ErgoTree(header, constants, Right(root)) + new ErgoTree(setRequiredBits(header), constants, Right(root)) } val EmptyConstants: IndexedSeq[Constant[SType]] = Array[Constant[SType]]() - /** Create new ErgoTree for the given proposition using the given header flags and - * without performing constant segregation. - */ - def withoutSegregation(root: SigmaPropValue): ErgoTree = - ErgoTree(ErgoTree.DefaultHeader, EmptyConstants, root) - - /** Create new ErgoTree for the given proposition using the given header flags and - * without performing constant segregation. - */ - def withoutSegregation(headerFlags: Byte, root: SigmaPropValue): ErgoTree = - ErgoTree((ErgoTree.DefaultHeader | headerFlags).toByte, EmptyConstants, root) - /** Create new ErgoTree for the given proposition using default header. * If the property is not a simple constant, then constant segregation is performed. */ - implicit def fromProposition(prop: SigmaPropValue): ErgoTree = { - fromProposition(ErgoTree.DefaultHeader, prop) + def fromProposition(prop: SigmaPropValue): ErgoTree = { + fromProposition(ErgoTree.ZeroHeader, prop) } /** Create new ErgoTree for the given proposition using the given header flags. * If the property is not a simple constant, then constant segregation is performed. */ - def fromProposition(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { + def fromProposition(header: HeaderType, prop: SigmaPropValue): ErgoTree = { prop match { - case SigmaPropConstant(_) => withoutSegregation(headerFlags, prop) - case _ => withSegregation(headerFlags, prop) + case SigmaPropConstant(_) => withoutSegregation(header, prop) + case _ => withSegregation(header, prop) } } /** Create new ErgoTree for the given sigma proposition using default header and * without performing constant segregation. */ - implicit def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = { - withoutSegregation(pk.toSigmaPropValue) + def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = { + withoutSegregation(ZeroHeader, pk.toSigmaPropValue) } /** Create new ErgoTree for the given sigma proposition using the given header flags * and without performing constant segregation. */ - def fromSigmaBoolean(headerFlags: Byte, pk: SigmaBoolean): ErgoTree = { - withoutSegregation(headerFlags, pk.toSigmaPropValue) + def fromSigmaBoolean(header: HeaderType, pk: SigmaBoolean): ErgoTree = { + withoutSegregation(header, pk.toSigmaPropValue) } + /** Create new ErgoTree for the given proposition using the given header flags and + * without performing constant segregation. + */ + def withoutSegregation(header: HeaderType, root: SigmaPropValue): ErgoTree = + ErgoTree(setRequiredBits(header), EmptyConstants, root) + /** Build ErgoTree via serialization of the value with ConstantSegregationHeader, constants segregated * from the tree and ConstantPlaceholders referring to the segregated constants. * @@ -334,26 +364,22 @@ object ErgoTree { * ConstantSegregationHeader flag. * @param prop expression to be transformed into ErgoTree * */ - def withSegregation(headerFlags: Byte, prop: SigmaPropValue): ErgoTree = { + def withSegregation(header: HeaderType, prop: SigmaPropValue): ErgoTree = { val constantStore = new ConstantStore() - val byteWriter = SigmaSerializer.startWriter(constantStore) + val w = SigmaSerializer.startWriter(constantStore) // serialize value and segregate constants into constantStore - ValueSerializer.serialize(prop, byteWriter) + ValueSerializer.serialize(prop, w) val extractedConstants = constantStore.getAll - val r = SigmaSerializer.startReader(byteWriter.toBytes) + val r = SigmaSerializer.startReader(w.toBytes) r.constantStore = new ConstantStore(extractedConstants) // deserialize value with placeholders val valueWithPlaceholders = ValueSerializer.deserialize(r).asSigmaProp - val header = (ErgoTree.ConstantSegregationHeader | headerFlags).toByte - new ErgoTree(header, extractedConstants, Right(valueWithPlaceholders)) + new ErgoTree( + header = setRequiredBits(setConstantSegregation(header)), + constants = extractedConstants, + root = Right(valueWithPlaceholders)) } - /** Create new ErgoTree for the given sigma proposition using default header and - * also performing constant segregation. - */ - def withSegregation(prop: SigmaPropValue): ErgoTree = - withSegregation(DefaultHeader, prop) - /** Deserializes an ErgoTree instance from a hexadecimal string. * * @param hex a hexadecimal string representing the serialized ErgoTree diff --git a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala index 8c882da7f3..b3d72e9d87 100644 --- a/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala +++ b/interpreter/shared/src/main/scala/sigmastate/UnprovenTree.scala @@ -2,6 +2,7 @@ package sigmastate import debox.cfor import sigma.data.{CAND, COR, CTHRESHOLD, ProveDHTuple, ProveDlog, SigmaBoolean, SigmaLeaf} +import sigmastate.ErgoTree.ZeroHeader import sigmastate.Values.SigmaPropConstant import sigmastate.crypto.DLogProtocol.FirstDLogProverMessage import sigmastate.crypto.VerifierMessage.Challenge @@ -13,10 +14,6 @@ import sigmastate.serialization.SigmaSerializer import sigmastate.utils.SigmaByteWriter import java.math.BigInteger -import debox.cfor -import sigmastate.Values.ErgoTree.ZeroHeader -import sigmastate.crypto.GF2_192_Poly - import scala.language.existentials object ConjectureType extends Enumeration { diff --git a/interpreter/shared/src/main/scala/sigmastate/Values.scala b/interpreter/shared/src/main/scala/sigmastate/Values.scala index a5c7dc8e48..8fc8c0de1f 100644 --- a/interpreter/shared/src/main/scala/sigmastate/Values.scala +++ b/interpreter/shared/src/main/scala/sigmastate/Values.scala @@ -11,7 +11,7 @@ import sigma.kiama.rewriting.Rewriter.count import sigma.util.CollectionUtil._ import sigma.util.Extensions._ import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _} -import sigmastate.Values.ErgoTree.{HeaderType, substConstants} +import sigmastate.ErgoTree.HeaderType import sigmastate.crypto.CryptoConstants import sigmastate.eval._ import sigmastate.exceptions.InterpreterException @@ -24,7 +24,6 @@ import sigmastate.serialization.OpCodes._ import sigmastate.serialization.ValueCodes.OpCode import sigmastate.serialization._ import sigmastate.utxo._ -import supertagged.TaggedType import java.math.BigInteger import java.util.{Arrays, Objects} diff --git a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala index 422a58d6ce..45cbc863eb 100644 --- a/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala +++ b/interpreter/shared/src/main/scala/sigmastate/serialization/ErgoTreeSerializer.scala @@ -5,8 +5,7 @@ import sigmastate.Values.Constant import sigmastate.lang.DeserializationSigmaBuilder import sigmastate.lang.Terms.ValueOps import sigmastate.utils.{SigmaByteReader, SigmaByteWriter} -import sigmastate.ErgoTree.EmptyConstants -import sigmastate.Values.ErgoTree.{EmptyConstants, HeaderType} +import sigmastate.ErgoTree.{EmptyConstants, HeaderType} import sigma.util.safeNewArray import sigmastate.utxo.ComplexityTable import debox.cfor diff --git a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala index 01fd1401ce..9ec6fae7c8 100644 --- a/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala +++ b/interpreter/shared/src/test/scala/sigmastate/TestsBase.scala @@ -5,7 +5,7 @@ import sigmastate.Values.SigmaPropValue import org.ergoplatform.ErgoTreePredef import sigma.VersionTesting import sigma.data.SigmaBoolean -import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader} +import sigmastate.ErgoTree.{HeaderType, ZeroHeader} trait TestsBase extends Matchers with VersionTesting { /** Set this to true to enable debug console output in tests */ diff --git a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala index 004b4eadc5..204147006b 100644 --- a/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala +++ b/interpreter/shared/src/test/scala/sigmastate/crypto/SigningSpecification.scala @@ -4,12 +4,10 @@ import org.scalacheck.Gen import scorex.util.encode.Base16 import sigma.data.{CAND, COR, ProveDHTuple, SigmaBoolean} import sigma.serialization.ProveDHTupleSerializer -import sigmastate.AtLeast -import sigmastate.Values.ErgoTree +import sigmastate.{AtLeast, ErgoTree} import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.{ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter, TestingCommons} import sigmastate.interpreter.{ContextExtension, HintsBag, ProverResult} -import sigmastate.interpreter.{ContextExtension, HintsBag, ProverResult} class SigningSpecification extends TestingCommons { diff --git a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index a48f81f981..33a319d075 100644 --- a/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/interpreter/shared/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -29,7 +29,7 @@ import sigma.crypto.EcPointType import sigma.util.Extensions.EcpOps import sigma.validation.{ChangedRule, DisabledRule, EnabledRule, ReplacedRule, RuleStatus} import sigma.validation.ValidationRules.FirstRuleId -import sigmastate.Values.ErgoTree.ZeroHeader +import sigmastate.ErgoTree.ZeroHeader import java.math.BigInteger import scala.collection.compat.immutable.ArraySeq diff --git a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala index 97ee54625c..8a98279273 100644 --- a/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala +++ b/interpreter/shared/src/test/scala/special/sigma/SigmaTestingData.scala @@ -20,7 +20,7 @@ import sigmastate.utils.Helpers import sigmastate.{ErgoTree, _} import sigma.ast.{SBoolean, SSigmaProp} import sigma.crypto.EcPointType -import sigmastate.Values.ErgoTree.HeaderType +import sigmastate.ErgoTree.HeaderType import java.math.BigInteger import scala.reflect.ClassTag diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index 5b6d2db45a..34a3b1cc6a 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -8,8 +8,8 @@ import scala.scalajs.js import scala.scalajs.js.annotation.JSExportTopLevel import org.ergoplatform.sdk.js.ErgoTree import sigma.js.Value +import sigmastate.ErgoTree.HeaderType import sigmastate.Values -import sigmastate.Values.ErgoTree.HeaderType import sigmastate.eval.CompiletimeIRContext import sigmastate.lang.Terms.ValueOps @@ -38,9 +38,9 @@ class SigmaCompiler(_compiler: sigmastate.lang.SigmaCompiler) extends js.Object require(prop.tpe.isSigmaProp, s"Expected SigmaProp expression type bue got ${prop.tpe}: $prop") val tree = if (segregateConstants) { - Values.ErgoTree.withSegregation(HeaderType @@ treeHeader, prop.asSigmaProp) + sigmastate.ErgoTree.withSegregation(HeaderType @@ treeHeader, prop.asSigmaProp) } else { - Values.ErgoTree.withoutSegregation(HeaderType @@ treeHeader, prop.asSigmaProp) + sigmastate.ErgoTree.withoutSegregation(HeaderType @@ treeHeader, prop.asSigmaProp) } new ErgoTree(tree) } diff --git a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala index e54b45aead..e81ace9575 100644 --- a/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala +++ b/sc/jvm/src/test/scala/sigmastate/helpers/SigmaPPrintSpec.scala @@ -7,8 +7,7 @@ import sigma.Extensions.ArrayOps import sigma.SigmaDslTesting import sigma.ast._ import sigma.data.{AvlTreeData, AvlTreeFlags, CollType, Digest32Coll} -import sigma.data.{AvlTreeData, AvlTreeFlags, CollType} -import sigmastate.Values.ErgoTree.HeaderType +import sigmastate.ErgoTree.HeaderType import sigmastate.Values._ import sigmastate._ import sigmastate.eval._ diff --git a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala index 9a3ee8434a..c4660f3c43 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/ErgoAddressSpecification.scala @@ -20,7 +20,7 @@ import sigmastate.serialization.ValueSerializer import sigmastate.utils.Helpers._ import sigmastate.{CompilerCrossVersionProps, ErgoTree, SigmaAnd, UnparsedErgoTree} import sigma.SigmaDslTesting -import sigmastate.Values.ErgoTree.{ZeroHeader, setConstantSegregation} +import sigmastate.ErgoTree.{ZeroHeader, setConstantSegregation} import sigma.ast.SType import sigma.data.ProveDlog diff --git a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala index a61660e37b..92ce39f5b5 100644 --- a/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala +++ b/sc/shared/src/test/scala/org/ergoplatform/dsl/TestContractSpec.scala @@ -20,6 +20,7 @@ import sigmastate.helpers.TestingHelpers._ import sigmastate.lang.Terms.ValueOps import sigma.{AnyValue, Evaluation, SigmaProp} import sigmastate.ErgoTree +import sigmastate.ErgoTree.ZeroHeader case class TestContractSpec(testSuite: CompilerTestingCommons)(implicit val IR: IRContext) extends ContractSpec { diff --git a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala index cc860edecc..3b9cf4a76e 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala @@ -17,7 +17,7 @@ import sigma.data.RType._ import sigma.data._ import sigma.util.Extensions.{BooleanOps, IntOps, LongOps} import sigma.{VersionContext, data, _} -import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader} +import sigmastate.ErgoTree.{HeaderType, ZeroHeader} import sigmastate.Values.{IntConstant, _} import sigmastate._ import sigmastate.eval.Extensions.{AvlTreeOps, ByteExt, IntExt, LongExt, ShortExt} diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index 5cf3d4070c..afe1c1521c 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -18,7 +18,7 @@ import sigma.util.BenchmarkUtil import sigma.util.CollectionUtil._ import sigma.util.Extensions._ import sigma.util.StringUtil.StringUtilExtensions -import sigmastate.Values.ErgoTree.ZeroHeader +import sigmastate.ErgoTree.ZeroHeader import sigma.ast.SType.AnyOps import sigmastate.Values.{ByteArrayConstant, Constant, ConstantNode, IntConstant, SValue} import sigmastate.crypto.DLogProtocol.DLogProverInput diff --git a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala index 65d92d49af..a5addae4f8 100644 --- a/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ErgoTreeSpecification.scala @@ -24,7 +24,7 @@ import sigma.ast._ import sigma.validation.ValidationException import sigma.validation.ValidationRules.CheckTypeCode import sigma.{ContractsTestkit, SigmaDslTesting} -import sigmastate.Values.ErgoTree.HeaderType +import sigmastate.ErgoTree.HeaderType import sigmastate.SCollectionMethods.checkValidFlatmap diff --git a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala index 4d2d0f4f4c..e660c505a6 100644 --- a/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/ScriptVersionSwitchSpecification.scala @@ -3,12 +3,10 @@ package sigmastate import org.ergoplatform.ErgoBox.AdditionalRegisters import org.ergoplatform._ import scorex.util.ModifierId -import sigmastate.ErgoTree.{DefaultHeader, updateVersionBits} +import sigmastate.ErgoTree.{DefaultHeader, HeaderType, ZeroHeader, setConstantSegregation, setVersionBits} import sigma.VersionContext.MaxSupportedScriptVersion import sigma.{Box, SigmaDslTesting} -import sigmastate.Values.ErgoTree.{HeaderType, ZeroHeader, setConstantSegregation, setVersionBits} import sigmastate.Values._ -import sigma.VersionContext.MaxSupportedScriptVersion import sigma.ast.{SBoolean, SBox, SCollection, SType} import sigmastate.eval._ import sigmastate.exceptions.InterpreterException diff --git a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala index adf1bfbd4e..354583f7b7 100644 --- a/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/SoftForkabilitySpecification.scala @@ -11,10 +11,8 @@ import sigma.data.AvlTreeData import sigma.serialization.SerializerException import sigma.validation.ValidationRules.{CheckPrimitiveTypeCode, CheckSerializableTypeCode, CheckTypeCode, CheckTypeWithMethods, trySoftForkable} import sigma.validation.{ChangedRule, ReplacedRule, SigmaValidationSettings, ValidationException, ValidationRule} -import sigmastate.ErgoTree.EmptyConstants +import sigmastate.ErgoTree.{EmptyConstants, HeaderType, ZeroHeader, setSizeBit} import sigmastate.Values.{ByteArrayConstant, IntConstant, NotReadyValueInt, ValueCompanion} -import sigmastate.Values.ErgoTree.{EmptyConstants, HeaderType, ZeroHeader, setSizeBit} -import sigmastate.Values.{ByteArrayConstant, ErgoTree, IntConstant, NotReadyValueInt, UnparsedErgoTree, ValueCompanion} import sigmastate.exceptions.InterpreterException import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala index 620dd5ec94..86a7e30533 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala @@ -5,10 +5,9 @@ import org.ergoplatform.validation.ValidationSpecification import org.ergoplatform.{Context => _, _} import scalan.BaseCtxTests import sigma.VersionContext -import sigmastate.Values.{BigIntArrayConstant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant, Value} +import sigmastate.Values.{BigIntArrayConstant, EvaluatedValue, SValue, SigmaPropConstant, Value} import sigma.ast.SType import sigma.data.AvlTreeData -import sigmastate.Values.{BigIntArrayConstant, EvaluatedValue, SValue, SigmaPropConstant, Value} import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting} import sigmastate.interpreter.Interpreter.ScriptEnv @@ -16,7 +15,7 @@ import sigmastate.interpreter.{ContextExtension, ErgoTreeEvaluator} import sigmastate.lang.Terms.ValueOps import sigmastate.lang.{CompilerResult, CompilerSettings, LangTests, SigmaCompiler} import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer -import sigmastate.CompilerTestsBase +import sigmastate.{CompilerTestsBase, ErgoTree} import sigma.{ContractsTestkit, Context => DContext} import scala.annotation.unused diff --git a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala index ec904518e8..dd6117b603 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala @@ -1,7 +1,7 @@ package sigmastate.eval import org.ergoplatform.ErgoBox -import sigmastate.Values.{ConcreteCollection, ErgoTree, IntArrayConstant, IntConstant, SigmaPropConstant, SigmaPropValue} +import sigmastate.Values.{ConcreteCollection, IntArrayConstant, IntConstant, SigmaPropConstant, SigmaPropValue} import sigmastate.helpers.ContextEnrichingTestProvingInterpreter import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala index 635036e30f..d837dfb3f0 100644 --- a/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/serialization/ErgoTreeSerializerSpecification.scala @@ -10,9 +10,7 @@ import sigma.util.Extensions.SigmaPropOps import sigma.validation.ValidationException import sigmastate.ErgoTree.EmptyConstants import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, IntConstant, ShortConstant, SigmaPropValue} -import sigmastate.Values.ErgoTree.EmptyConstants -import sigmastate.Values.ErgoTree.HeaderType -import sigmastate.Values.{BigIntConstant, ByteConstant, ConstantPlaceholder, ErgoTree, IntConstant, ShortConstant, SigmaPropValue, UnparsedErgoTree} +import sigmastate.ErgoTree.HeaderType import sigmastate._ import sigmastate.eval.Extensions.SigmaBooleanOps import sigmastate.eval.IRContext diff --git a/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala index 76b607888f..c61aec900a 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/DistributedSigSpecification.scala @@ -1,8 +1,6 @@ package sigmastate.utxo import sigma.data.CAND -import sigmastate.Values.ErgoTree -import sigmastate.Values.ErgoTree.ZeroHeader import sigmastate._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeTestProvingInterpreter} import sigmastate.interpreter._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala index 41772cf634..4e435a6d12 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/blockchain/BlockchainSimulationTestingCommons.scala @@ -17,8 +17,8 @@ import scala.util.{Random, Try} import scorex.util._ import sigma.Colls import sigma.data.{AvlTreeData, AvlTreeFlags} +import sigmastate.ErgoTree.ZeroHeader import sigmastate.eval.Extensions.SigmaBooleanOps -import sigmastate.Values.ErgoTree.ZeroHeader import sigmastate.interpreter.ContextExtension import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo.blockchain.BlockchainSimulationTestingCommons.{FullBlock, ValidationState} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala index fe5f090f73..db6f9dfad5 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletAdvContractExampleSpecification.scala @@ -3,8 +3,8 @@ package sigmastate.utxo.examples import org.ergoplatform.ErgoBox.{R4, R5, R6} import org.ergoplatform._ import sigma.data.AvlTreeData -import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.{ErgoTree, IntConstant, LongConstant} +import sigmastate.{CompilerCrossVersionProps, ErgoTree} +import sigmastate.Values.{IntConstant, LongConstant} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter, ErgoLikeTestProvingInterpreter} import sigmastate.helpers.TestingHelpers._ import sigmastate.interpreter.Interpreter.ScriptNameProp diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala index 021da2bafd..c3efd21709 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ColdWalletContractExampleSpecification.scala @@ -5,8 +5,8 @@ import org.ergoplatform._ import sigma.data.AvlTreeData import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ -import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.{ErgoTree, IntConstant, LongConstant} +import sigmastate.{CompilerCrossVersionProps, ErgoTree} +import sigmastate.Values.{IntConstant, LongConstant} import sigmastate.interpreter.Interpreter.ScriptNameProp import sigmastate.lang.Terms._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala index af3f271b8a..41720491dc 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/CoopExampleSpecification.scala @@ -3,16 +3,15 @@ package sigmastate.utxo.examples import org.ergoplatform.{ErgoBox, ErgoLikeContext, ErgoLikeTransaction} import org.scalatest.Assertion import org.scalatest.TryValues._ -import sigmastate.crypto.DLogProtocol.DLogProverInput import scorex.crypto.hash.Blake2b256 import sigma.data.{AvlTreeData, ProveDlog} +import sigmastate.ErgoTree.ZeroHeader import sigmastate.Values.{BooleanConstant, ByteArrayConstant} -import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} +import sigmastate.crypto.DLogProtocol.DLogProverInput import sigmastate.helpers.TestingHelpers._ +import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.lang.Terms._ import sigmastate.{CompilerCrossVersionProps, ErgoTree} -import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.ErgoTree.ZeroHeader class CoopExampleSpecification extends CompilerTestingCommons with CompilerCrossVersionProps { diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala index cc37d018b8..878229982f 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/MixExampleSpecification.scala @@ -5,8 +5,8 @@ import org.ergoplatform.ErgoBox.{R4, R5} import scorex.crypto.hash.Blake2b256 import sigma.data.{AvlTreeData, ProveDHTuple, ProveDlog} import sigma.util.Extensions.EcpOps -import sigmastate.CompilerCrossVersionProps -import sigmastate.Values.{ErgoTree, GroupElementConstant} +import sigmastate.{CompilerCrossVersionProps, ErgoTree} +import sigmastate.Values.GroupElementConstant import sigmastate.crypto.{CryptoConstants, DiffieHellmanTupleProverInput} import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala index 7c58ffe0d4..be2d852b46 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/RPSGameExampleSpecification.scala @@ -5,7 +5,7 @@ import org.ergoplatform.ErgoBox.{R4, R5, R6, R7} import scorex.crypto.hash.Blake2b256 import scorex.utils.Random import sigma.data.{AvlTreeData, ProveDlog} -import sigmastate.Values.{ByteArrayConstant, ByteConstant, ErgoTree, IntConstant, SigmaPropConstant} +import sigmastate.Values.{ByteArrayConstant, ByteConstant, IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.helpers.TestingHelpers._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala index cf5c8a992c..5ce5163b33 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/ReversibleTxExampleSpecification.scala @@ -4,7 +4,7 @@ import org.ergoplatform.ErgoBox.{R4, R5} import org.ergoplatform._ import scorex.crypto.hash.Blake2b256 import sigma.data.AvlTreeData -import sigmastate.Values.{ErgoTree, IntConstant, SigmaPropConstant} +import sigmastate.Values.{IntConstant, SigmaPropConstant} import sigmastate._ import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter} import sigmastate.helpers.TestingHelpers._ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala index ca4c4d1efa..a60cf35a7c 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/TimedPaymentExampleSpecification.scala @@ -1,7 +1,7 @@ package sigmastate.utxo.examples import org.ergoplatform._ -import sigmastate.Values.{ErgoTree, IntConstant} +import sigmastate.Values.IntConstant import sigma.data.AvlTreeData import sigmastate._ import sigmastate.exceptions.InterpreterException diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala index 08f268de44..7042791a93 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala @@ -10,9 +10,7 @@ import sigma.Evaluation import sigma.ast.SType import sigma.serialization.SerializerException import sigma.util.safeNewArray -import sigmastate.ErgoTree.headerWithVersion -import sigmastate.Values._ -import sigmastate.Values.ErgoTree.{ZeroHeader, headerWithVersion, setConstantSegregation} +import sigmastate.ErgoTree.{ZeroHeader, headerWithVersion, setConstantSegregation} import sigmastate.Values._ import sigmastate._ import sigmastate.lang.{DeserializationSigmaBuilder, StdSigmaBuilder} diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala index ce4b2bdbe6..4d083e9ff4 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/utils/Zero.scala @@ -10,7 +10,7 @@ import sigmastate.ErgoTree import sigmastate.crypto.CryptoConstants import sigmastate.eval._ import sigma._ -import sigmastate.Values.ErgoTree.HeaderType +import sigmastate.ErgoTree.HeaderType import java.math.BigInteger import scala.language.implicitConversions diff --git a/sdk/shared/src/test/scala/org/ergoplatform/sdk/ContractTemplateSpecification.scala b/sdk/shared/src/test/scala/org/ergoplatform/sdk/ContractTemplateSpecification.scala index 44f2725a61..ef83bfd6d0 100644 --- a/sdk/shared/src/test/scala/org/ergoplatform/sdk/ContractTemplateSpecification.scala +++ b/sdk/shared/src/test/scala/org/ergoplatform/sdk/ContractTemplateSpecification.scala @@ -10,7 +10,7 @@ import sigmastate.serialization.{SerializationSpecification, SigmaSerializer} import sigma.ContractsTestkit import sigma.ast.{SByte, SInt, SType} import sigma.data.CBigInt -import sigmastate.Values.ErgoTree.setConstantSegregation +import sigmastate.ErgoTree.setConstantSegregation import java.math.BigInteger From dc23717b7dd21ecfff9f602e578bcff743e299b4 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 5 Oct 2023 21:37:22 +0200 Subject: [PATCH 34/34] core-serializers2: cleanup code --- .../src/main/scala/sigma/SigmaDsl.scala | 73 ------------------- .../sigma/reflection/ReflectionData.scala | 23 ------ 2 files changed, 96 deletions(-) diff --git a/core/shared/src/main/scala/sigma/SigmaDsl.scala b/core/shared/src/main/scala/sigma/SigmaDsl.scala index 8785a32fa8..df2b419273 100644 --- a/core/shared/src/main/scala/sigma/SigmaDsl.scala +++ b/core/shared/src/main/scala/sigma/SigmaDsl.scala @@ -383,79 +383,6 @@ trait AvlTree { * replaced by `newOperations` */ def updateOperations(newOperations: Byte): AvlTree - -// /** Checks if an entry with key `key` exists in this tree using proof `proof`. -// * Throws exception if proof is incorrect -// * -// * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. -// * Return `true` if a leaf with the key `key` exists -// * Return `false` if leaf with provided key does not exist. -// * @param key a key of an element of this authenticated dictionary. -// * @param proof data to reconstruct part of the tree enough to perform the check -// */ -// def contains(key: Coll[Byte], proof: Coll[Byte]): Boolean -// -// /** Perform a lookup of key `key` in this tree using proof `proof`. -// * Throws exception if proof is incorrect -// * -// * @note CAUTION! Does not support multiple keys check, use [[getMany]] instead. -// * Return Some(bytes) of leaf with key `key` if it exists -// * Return None if leaf with provided key does not exist. -// * @param key a key of an element of this authenticated dictionary. -// * @param proof data to reconstruct part of the tree enough to get the value -// * by the key -// */ -// def get(key: Coll[Byte], proof: Coll[Byte]): Option[Coll[Byte]] -// -// /** Perform a lookup of many keys `keys` in this tree using proof `proof`. -// * -// * @note CAUTION! Keys must be ordered the same way they were in lookup before proof was generated. -// * For each key return Some(bytes) of leaf if it exists and None if is doesn't. -// * @param keys keys of elements of this authenticated dictionary. -// * @param proof -// */ -// def getMany(keys: Coll[Coll[Byte]], proof: Coll[Byte]): Coll[Option[Coll[Byte]]] -// -// /** Perform insertions of key-value entries into this tree using proof `proof`. -// * Throws exception if proof is incorrect -// * -// * @note CAUTION! Pairs must be ordered the same way they were in insert ops before proof was generated. -// * Return Some(newTree) if successful -// * Return None if operations were not performed. -// * @param operations collection of key-value pairs to insert in this authenticated dictionary. -// * @param proof data to reconstruct part of the tree -// */ -// def insert(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] -// -// /** Perform updates of key-value entries into this tree using proof `proof`. -// * Throws exception if proof is incorrect -// * -// * @note CAUTION! Pairs must be ordered the same way they were in update ops before proof was generated. -// * Return Some(newTree) if successful -// * Return None if operations were not performed. -// * @param operations collection of key-value pairs to update in this authenticated dictionary. -// * @param proof data to reconstruct part of the tree -// */ -// def update(operations: Coll[(Coll[Byte], Coll[Byte])], proof: Coll[Byte]): Option[AvlTree] - -// /** Perform removal of entries into this tree using proof `proof`. -// * Throws exception if proof is incorrect -// * Return Some(newTree) if successful -// * Return None if operations were not performed. -// * -// * @note CAUTION! Keys must be ordered the same way they were in remove ops before proof was generated. -// * @param operations collection of keys to remove from this authenticated dictionary. -// * @param proof data to reconstruct part of the tree -// */ -// def remove(operations: Coll[Coll[Byte]], proof: Coll[Byte]): Option[AvlTree] - -// /** Creates a new instance of [[AvlTreeVerifier]] with the given `proof` and using -// * properties of this AvlTree (digest, keyLength, valueLengthOpt) for constructor -// * arguments. -// * -// * @param proof bytes of the serialized proof which is used to represent the tree. -// */ -// def createVerifier(proof: Coll[Byte]): AvlTreeVerifier } /** Only header fields that can be predicted by a miner. */ diff --git a/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala b/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala index 1b8afab03e..2aac1a5670 100644 --- a/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala +++ b/core/shared/src/main/scala/sigma/reflection/ReflectionData.scala @@ -175,14 +175,6 @@ object ReflectionData { mkMethod(clazz, "updateOperations", Array[Class[_]](classOf[Byte])) { (obj, args) => obj.asInstanceOf[AvlTree].updateOperations(args(0).asInstanceOf[Byte]) }, -// mkMethod(clazz, "getMany", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].getMany(args(0).asInstanceOf[Coll[Coll[Byte]]], -// args(1).asInstanceOf[Coll[Byte]]) -// }, -// mkMethod(clazz, "update", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].update(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], -// args(1).asInstanceOf[Coll[Byte]]) -// }, mkMethod(clazz, "keyLength", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].keyLength }, @@ -195,27 +187,12 @@ object ReflectionData { mkMethod(clazz, "digest", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].digest }, -// mkMethod(clazz, "insert", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].insert(args(0).asInstanceOf[Coll[(Coll[Byte], Coll[Byte])]], -// args(1).asInstanceOf[Coll[Byte]]) -// }, mkMethod(clazz, "isRemoveAllowed", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].isRemoveAllowed }, mkMethod(clazz, "valueLengthOpt", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].valueLengthOpt }, -// mkMethod(clazz, "get", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].get(args(0).asInstanceOf[Coll[Byte]], -// args(1).asInstanceOf[Coll[Byte]]) -// }, -// mkMethod(clazz, "remove", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].remove(args(0).asInstanceOf[Coll[Coll[Byte]]], args(1).asInstanceOf[Coll[Byte]]) -// }, -// mkMethod(clazz, "contains", Array[Class[_]](classOf[Coll[_]], classOf[Coll[_]])) { (obj, args) => -// obj.asInstanceOf[AvlTree].contains(args(0).asInstanceOf[Coll[Byte]], -// args(1).asInstanceOf[Coll[Byte]]) -// }, mkMethod(clazz, "isUpdateAllowed", Array[Class[_]]()) { (obj, _) => obj.asInstanceOf[AvlTree].isUpdateAllowed },