Skip to content

Commit

Permalink
minimize-modules: introduced sigma.Colls
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 25, 2023
1 parent ec37de7 commit fdeda45
Show file tree
Hide file tree
Showing 43 changed files with 101 additions and 117 deletions.
4 changes: 2 additions & 2 deletions common/shared/src/main/scala/sigma/data/CollsOverArrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CollOverArray[@specialized A](val toArray: Array[A], val builder: CollBuil
override def hashCode() = CollectionUtil.deepHashCode(toArray)
}

class CollOverArrayBuilder extends CollBuilder { builder =>
private[sigma] class CollOverArrayBuilder extends CollBuilder { builder =>

@inline override def pairColl[@specialized A, @specialized B](as: Coll[A], bs: Coll[B]): PairColl[A, B] = {
if (VersionContext.current.isJitActivated) {
Expand Down Expand Up @@ -250,7 +250,7 @@ class PairOfCols[@specialized L, @specialized R](val ls: Coll[L], val rs: Coll[R
RType.pairRType(tL, tR)
}

override def builder: CollBuilder = new CollOverArrayBuilder
override def builder: CollBuilder = ls.builder
override def toArray: Array[(L, R)] = ls.toArray.zip(rs.toArray)
@inline override def length: Int = if (ls.length <= rs.length) ls.length else rs.length
@inline override def apply(i: Int): (L, R) = (ls(i), rs(i))
Expand Down
6 changes: 6 additions & 0 deletions common/shared/src/main/scala/sigma/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ package object sigma {
* @return An RType object for the tuple type.
*/
def tupleRType(types: Array[SomeType]): RType[TupleData] = TupleType(types)

/** The primary reference to global Coll operations. Can be used to create collections from Array etc.
*
* @see CollBuilder
*/
val Colls: CollBuilder = new CollOverArrayBuilder
}
9 changes: 4 additions & 5 deletions common/shared/src/test/scala/sigma/CollGens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import spire.scalacompat.BuilderCompat

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.language.implicitConversions

trait CollGens { testSuite =>
import Gen._

val builder: CollBuilder = new CollOverArrayBuilder
val builder: CollBuilder = sigma.Colls
val valGen = choose(-100, 100)
val byteGen = choose[Byte](-100, 100)
val indexGen = choose(0, 100)
Expand Down Expand Up @@ -115,8 +114,8 @@ trait CollGens { testSuite =>
val superGenByte = getSuperGen(1, Gen.oneOf(bytesOverArrayGen, replBytesCollGen))
val superGen = Gen.oneOf(superGenInt, superGenByte)
val allGen = Gen.oneOf(superGen, collGen)
implicit val arbColl = Arbitrary(collGen)
implicit val arbBytes = Arbitrary(bytesGen)
implicit val arbColl : Arbitrary[Coll[Int]] = Arbitrary(collGen)
implicit val arbBytes: Arbitrary[Coll[Byte]] = Arbitrary(bytesGen)

def eq0(x: Int) = x == 0

Expand All @@ -139,7 +138,7 @@ trait CollGens { testSuite =>
res
}

implicit def buildableColl[T: RType] = new Buildable[T, Coll[T]] {
implicit def buildableColl[T: RType]: Buildable[T, Coll[T]] = new Buildable[T, Coll[T]] {
def builder = new BuilderCompat[T, Coll[T]] {
val al = new ArrayBuffer[T]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import sigmastate.eval._
import sigmastate.serialization.SigmaSerializer
import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter}
import sigmastate.utxo.ExtractCreationInfo
import sigma._
import sigma.{Colls, _}

/**
* Box (aka coin, or an unspent output) is a basic concept of a UTXO-based cryptocurrency. In Bitcoin, such an object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package org.ergoplatform

import java.util
import debox.cfor
import org.ergoplatform.ErgoBox._
import org.ergoplatform.settings.ErgoAlgos
import scorex.util.{ModifierId, bytesToId}
import sigma.Extensions.CollOps
import sigma.util.safeNewArray
import sigma.{Coll, Colls}
import sigmastate.SType.AnyOps
import sigmastate.Values._
import sigmastate._
import sigmastate.SType.AnyOps
import sigmastate.serialization.{SigmaSerializer, ValueSerializer}
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import sigma.Coll
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.eval._
import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer
import sigma.util.safeNewArray
import debox.cfor
import sigma.Extensions.CollOps
import sigmastate.serialization.SigmaSerializer
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}

import scala.collection.{immutable, mutable}
import scala.runtime.ScalaRunTime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.ergoplatform

import debox.cfor
import org.ergoplatform.ErgoBox.TokenId
import scorex.crypto.authds.ADKey
import scorex.crypto.hash.Blake2b256
import scorex.util._
import sigma.Colls
import sigma.util.safeNewArray
import sigmastate.SType._
import sigmastate.eval.Extensions._
import sigmastate.eval._
import sigmastate.interpreter.ProverResult
import sigmastate.serialization.{SigmaSerializer, ValueSerializer}
import sigma.util.safeNewArray
import sigmastate.serialization.SigmaSerializer
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import debox.cfor

import scala.util.Try

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package sigmastate

import sigmastate.crypto.CryptoConstants
import sigmastate.eval.Colls
import sigmastate.serialization.SigmaSerializer
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import sigma.Coll
import sigma.{Coll, Colls}


case class AvlTreeFlags(insertAllowed: Boolean, updateAllowed: Boolean, removeAllowed: Boolean) {
Expand Down
3 changes: 1 addition & 2 deletions interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import sigmastate.lang.CheckingSigmaBuilder._
import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer
import sigmastate.serialization.transformers.ProveDHTupleSerializer
import sigmastate.utils.{SigmaByteReader, SigmaByteWriter}
import sigma.{AvlTree, Header, PreHeader, _}
import sigma.{AvlTree, Coll, Colls, Header, PreHeader, _}
import sigmastate.lang.SourceContext
import sigma.util.safeNewArray
import sigma.Coll

import scala.collection.compat.immutable.ArraySeq
import scala.collection.mutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ class CAvlTreeVerifier(startingDigest: ADDigest,
* @see [[AvlTree]] for detailed descriptions
*/
case class CAvlTree(treeData: AvlTreeData) extends AvlTree with WrapperOf[AvlTreeData] {
val builder = CostingSigmaDslBuilder
val Colls = builder.Colls

override def wrappedValue: AvlTreeData = treeData

override def keyLength: Int = treeData.keyLength
Expand Down Expand Up @@ -494,7 +491,7 @@ class CostingSigmaDslBuilder extends SigmaDslBuilder { dsl =>
implicit val validationSettings: SigmaValidationSettings = ValidationRules.currentSettings

// manual fix
override val Colls: CollBuilder = new CollOverArrayBuilder
override val Colls: CollBuilder = sigma.Colls

override def BigInt(n: BigInteger): BigInt = CBigInt(n)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import sigma._
import java.math.BigInteger

object Extensions {
private val Colls = CostingSigmaDslBuilder.Colls

implicit class ByteExt(val b: Byte) extends AnyVal {
@inline def toBigInt: BigInt = CostingSigmaDslBuilder.BigInt(BigInteger.valueOf(b.toLong))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ package object eval {
*/
val SigmaDsl = CostingSigmaDslBuilder

/** The primary reference to global Coll operations. Can be used to create collections from Array etc.
* @see CollBuilder
*/
val Colls: CollBuilder = SigmaDsl.Colls

/** Constructor of tuple value with more than 2 items.
* Such long tuples are represented as Coll[Any].
* This representaion of tuples is different from representation of pairs (x, y),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sigmastate.lang
import debox.cfor
import org.ergoplatform.ErgoBox.RegisterId
import sigma.data.Nullable
import sigma.Environment
import sigma.{AnyValue, Coll, Colls, Environment}
import sigmastate.SCollection.{SByteArray, SIntArray}
import sigmastate.SOption.SIntOption
import sigmastate.Values._
Expand All @@ -15,8 +15,6 @@ import sigmastate.lang.Terms.{STypeSubst, _}
import sigmastate.serialization.OpCodes
import sigmastate.serialization.OpCodes.OpCode
import sigmastate.utxo._
import sigma.Coll
import sigma.AnyValue

import scala.util.DynamicVariable

Expand Down
6 changes: 2 additions & 4 deletions interpreter/shared/src/main/scala/sigmastate/trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import sigma.data.ExactOrdering._
import sigma.data.OverloadHack.Overloaded1
import sigma.data.{ExactIntegral, ExactOrdering}
import scorex.crypto.hash.{Blake2b256, CryptographicHash32, Sha256}
import sigma.VersionContext
import sigma.{Coll, Colls, GroupElement, SigmaProp, VersionContext}
import sigmastate.ArithOp.OperationImpl
import sigmastate.Operations._
import sigmastate.SCollection.{SByteArray, SIntArray}
import sigmastate.SOption.SIntOption
import sigmastate.Values._
import sigmastate.eval.Extensions.EvalCollOps
import sigmastate.eval.NumericOps.{BigIntIsExactIntegral, BigIntIsExactOrdering}
import sigmastate.eval.{Colls, SigmaDsl}
import sigmastate.eval.SigmaDsl
import sigmastate.interpreter.ErgoTreeEvaluator
import sigmastate.interpreter.ErgoTreeEvaluator.DataEnv
import sigmastate.serialization.OpCodes._
import sigmastate.serialization._
import sigmastate.utxo.{SimpleTransformerCompanion, Transformer}
import sigma.Coll
import sigma.{GroupElement, SigmaProp}

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.ergoplatform.ErgoBox.TokenId
import scorex.util.{ModifierId, idToBytes}
import scorex.utils.{Ints, Longs, Shorts}
import sigmastate.eval.{Digest32Coll, SigmaDsl}
import sigma.Coll
import sigma.{Coll, Colls}

object Extensions {
implicit class ByteOpsForSigma(val b: Byte) extends AnyVal {
Expand All @@ -25,7 +25,7 @@ object Extensions {
* For example, the Short value {@code 0x1213} would yield the
* byte array {@code {0x12, 0x13}}.
*/
def toBytes: Coll[Byte] = SigmaDsl.Colls.fromArray(Shorts.toByteArray(x))
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.
Expand All @@ -38,7 +38,7 @@ object Extensions {
* For example, the Int value {@code 0x12131415} would yield the
* byte array {@code {0x12, 0x13, 0x14, 0x15}}.
*/
def toBytes: Coll[Byte] = SigmaDsl.Colls.fromArray(Ints.toByteArray(x))
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.
Expand All @@ -51,7 +51,7 @@ object Extensions {
* For example, the Long value {@code 0x1213141516171819} would yield the
* byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}}.
*/
def toBytes: Coll[Byte] = SigmaDsl.Colls.fromArray(Longs.toByteArray(x))
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.
Expand All @@ -67,7 +67,7 @@ object Extensions {
*/
implicit class ModifierIdOps(val id: ModifierId) extends AnyVal {
/** @return a `Coll[Byte]` representation of the `ModifierId` (decodes using Base16). */
def toColl: Coll[Byte] = SigmaDsl.Colls.fromArray(idToBytes(id))
def toColl: Coll[Byte] = Colls.fromArray(idToBytes(id))

/** Converts this modifier id to to token id. */
def toTokenId: TokenId = Digest32Coll @@ toColl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import io.circe.Decoder
import org.ergoplatform.settings.ErgoAlgos
import sigma.data.{OverloadHack, RType}
import scorex.utils.Ints
import sigma.Environment
import sigmastate.eval.{CAnyValue, Colls, SigmaDsl}
import sigma.{Coll, Colls, Environment, GroupElement}
import sigmastate.eval.{CAnyValue, SigmaDsl}
import sigmastate.crypto.CryptoConstants.EcPointType
import sigma.Coll
import sigma.GroupElement

import java.util
import java.util.concurrent.locks.Lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import sigmastate.eval._
import sigmastate.interpreter.ContextExtension
import sigmastate.interpreter.ErgoTreeEvaluator.DefaultEvalSettings
import sigmastate.serialization.{GroupElementSerializer, SigmaSerializer}
import sigma.Coll
import sigma.{Box, Header, PreHeader}
import sigma.{Box, Coll, Colls, Header, PreHeader}

object ErgoLikeContextTesting {
/* NO HF PROOF:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sigmastate.serialization
import scorex.crypto.authds.{ADKey, ADValue}
import scorex.crypto.authds.avltree.batch.{BatchAVLProver, Insert}
import scorex.crypto.hash.{Blake2b256, Digest32}
import sigma.Colls
import sigmastate.Values.AvlTreeConstant
import sigmastate.AvlTreeFlags
import sigmastate._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.Values._
import sigmastate.eval.Evaluation
import sigma.AvlTree
import sigma.{AvlTree, Colls}
import SType.AnyOps
import scorex.util.encode.Base16
import sigmastate.exceptions.SerializerException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sigmastate.eval.Evaluation
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.crypto.CryptoConstants.EcPointType
import sigma.AvlTree
import sigma.{AvlTree, Colls}
import SType.AnyOps
import sigmastate.exceptions.SerializerException
import sigmastate.interpreter.{CostAccumulator, ErgoTreeEvaluator}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import sigmastate.Values.{ByteArrayConstant, ConcreteCollection, ConstantPlaceho
import sigmastate.crypto.CryptoConstants.EcPointType
import sigmastate.crypto.DLogProtocol.ProveDlog
import sigmastate.crypto.ProveDHTuple
import sigmastate.eval.{Colls, _}
import sigmastate.eval._
import sigmastate.eval.Extensions._
import sigmastate.eval.{CAvlTree, CBigInt, CHeader, CPreHeader, CSigmaProp, CostingBox, CostingSigmaDslBuilder, SigmaDsl}
import sigmastate.helpers.TestingCommons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package org.ergoplatform.dsl
import org.ergoplatform.ErgoBox.{BoxId, NonMandatoryRegisterId, TokenId}
import sigmastate.interpreter.{CostedProverResult, ProverResult}
import sigma.data.RType
import org.ergoplatform.{ErgoLikeContext, ErgoBox}
import sigma.{SigmaDslBuilder, AnyValue, SigmaProp}
import org.ergoplatform.{ErgoBox, ErgoLikeContext}
import sigma.{AnyValue, Coll, SigmaDslBuilder, SigmaProp}
import sigmastate.Values.ErgoTree
import sigmastate.eval.{IRContext, CostingSigmaDslBuilder}
import sigmastate.eval.{CostingSigmaDslBuilder, IRContext}

import scala.util.Try
import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition, Token}
Expand All @@ -17,7 +17,7 @@ trait ContractSpec {
val dsl: SigmaDslBuilder = CostingSigmaDslBuilder
val Colls = dsl.Colls

implicit def Coll[T](items: Array[T])(implicit cT: RType[T]) = Colls.fromArray(items)
implicit def Coll[T](items: Array[T])(implicit cT: RType[T]): Coll[T] = Colls.fromArray(items)

val IR: IRContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.ergoplatform.dsl.ContractSyntax.{ErgoScript, Proposition}
import sigmastate.eval.{CostingSigmaDslBuilder, Evaluation}
import sigmastate.interpreter.Interpreter.ScriptEnv
import sigma.{SigmaProp, SigmaContract, Context, SigmaDslBuilder}
import scala.language.implicitConversions

/** Defines methods to be used in contract implementations based on [[SigmaContract]]. */
trait ContractSyntax { contract: SigmaContract =>
Expand Down
4 changes: 2 additions & 2 deletions sc/shared/src/main/scala/scalan/Base.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sigma.util.StringUtil
import sigma.reflection.RConstructor

import java.util.Arrays
import scala.annotation.implicitNotFound
import scala.annotation.{implicitNotFound, unused}
import scala.annotation.unchecked.uncheckedVariance
import scala.collection.compat.immutable.ArraySeq
import scala.collection.mutable
Expand Down Expand Up @@ -453,7 +453,7 @@ abstract class Base { scalan: Scalan =>
}
/** Apply this transformer to the nodes present in the sequence,
* and leave non-Ref items unchanged. */
final def apply(xs: Seq[Any])(implicit o: Overloaded1): Seq[Any] = {
final def apply(xs: Seq[Any])(implicit @unused o: Overloaded1): Seq[Any] = {
val len = xs.length
if (len == 0) ArraySeq.empty
else {
Expand Down
Loading

0 comments on commit fdeda45

Please sign in to comment.