Skip to content

Commit

Permalink
core-serializers: toColl extension method moved to core
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Sep 12, 2023
1 parent 44d2463 commit 2d00c56
Show file tree
Hide file tree
Showing 51 changed files with 116 additions and 102 deletions.
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/sigma/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 4 additions & 0 deletions core/shared/src/main/scala/sigma/util/Extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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._
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ 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
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
Expand Down Expand Up @@ -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)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProveDlogSerializerSpec extends SerializationSpecification {

property("ProveDlog: Serializer round trip") {
forAll { pd: ProveDlog =>
roundTripTest(pd.toSigmaProp)
roundTripTest(pd.toSigmaPropValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 2d00c56

Please sign in to comment.