Skip to content

Commit

Permalink
ergotree-version: removed implicit conversions to ErgoTree
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Sep 25, 2023
1 parent 3c25033 commit 32a4652
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 64 deletions.
6 changes: 3 additions & 3 deletions interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,8 @@ object Values {
/** 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.
Expand All @@ -1453,7 +1453,7 @@ object Values {
/** Create new ErgoTree for the given sigma proposition using default header and
* without performing constant segregation.
*/
implicit def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = {
def fromSigmaBoolean(pk: SigmaBoolean): ErgoTree = {
withoutSegregation(ZeroHeader, pk.toSigmaProp)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package sigmastate.crypto

import org.scalacheck.Gen
import scorex.util.encode.Base16
import sigmastate.{AtLeast, COR, CAND}
import sigmastate.Values.SigmaBoolean
import sigmastate.{AtLeast, CAND, COR}
import sigmastate.Values.{ErgoTree, 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 {

Expand All @@ -24,7 +23,9 @@ class SigningSpecification extends TestingCommons {
// check that signature is correct
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
verifier.verify(sk.publicImage, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(
ErgoTree.fromSigmaBoolean(sk.publicImage),
fakeContext, proverResult, msg).get._1 shouldBe true

// print one more random vector for debug purposes
printSimpleSignature(msg: Array[Byte])
Expand All @@ -38,7 +39,7 @@ class SigningSpecification extends TestingCommons {
// check that signature is correct
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
verifier.verify(pdht, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(ErgoTree.fromSigmaBoolean(pdht), fakeContext, proverResult, msg).get._1 shouldBe true
}

property("handle improper signature") {
Expand All @@ -59,7 +60,7 @@ class SigningSpecification extends TestingCommons {
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
val sigmaTree: SigmaBoolean = CAND(Seq(sk1.publicImage, sk2.publicImage))
verifier.verify(sigmaTree, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(ErgoTree.fromSigmaBoolean(sigmaTree), fakeContext, proverResult, msg).get._1 shouldBe true
}

property("OR signature test vector") {
Expand All @@ -71,7 +72,9 @@ class SigningSpecification extends TestingCommons {
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
val sigmaTree: SigmaBoolean = COR(Seq(sk1.publicImage, sk2.publicImage))
verifier.verify(sigmaTree, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(
ErgoTree.fromSigmaBoolean(sigmaTree),
fakeContext, proverResult, msg).get._1 shouldBe true
}

property("OR with ProveDHT signature test vector") {
Expand All @@ -84,7 +87,9 @@ class SigningSpecification extends TestingCommons {
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
val sigmaTree: SigmaBoolean = COR(Seq(sk1.publicImage, pdht))
verifier.verify(sigmaTree, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(
ErgoTree.fromSigmaBoolean(sigmaTree),
fakeContext, proverResult, msg).get._1 shouldBe true
}

property("AND with OR signature test vector") {
Expand All @@ -97,7 +102,9 @@ class SigningSpecification extends TestingCommons {
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
val sigmaTree: SigmaBoolean = CAND(Seq(sk1.publicImage, COR(Seq(sk2.publicImage, sk3.publicImage))))
verifier.verify(sigmaTree, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(
ErgoTree.fromSigmaBoolean(sigmaTree),
fakeContext, proverResult, msg).get._1 shouldBe true
}

property("OR with AND signature test vector") {
Expand All @@ -110,7 +117,9 @@ class SigningSpecification extends TestingCommons {
val verifier = new ErgoLikeTestInterpreter
val proverResult = ProverResult(signature, ContextExtension.empty)
val sigmaTree: SigmaBoolean = COR(Seq(sk1.publicImage, CAND(Seq(sk2.publicImage, sk3.publicImage))))
verifier.verify(sigmaTree, fakeContext, proverResult, msg).get._1 shouldBe true
verifier.verify(
ErgoTree.fromSigmaBoolean(sigmaTree),
fakeContext, proverResult, msg).get._1 shouldBe true
}

property("threshold signature test vector") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
// unable to satisfy R4 conditions
checkSpending(remaining(height), height, prop, R4Prop(false)).isFailure shouldBe true
// incorrect new script
checkSpending(remaining(height), height, TrivialProp.TrueProp, R4Prop(true)).isFailure shouldBe true
checkSpending(remaining(height), height,
ErgoTree.fromProposition(TrivialProp.TrueProp),
R4Prop(true)).isFailure shouldBe true
// collect less coins then possible
checkSpending(remaining(height) + 1, height, prop, R4Prop(true)).isSuccess shouldBe true
// collect more coins then possible
Expand Down Expand Up @@ -188,7 +190,8 @@ class ErgoTreePredefSpec extends CompilerTestingCommons with CompilerCrossVersio
createRewardTx(currentRate, height, correctProp).isSuccess shouldBe true
createRewardTx(currentRate, height, incorrectDelay).isFailure shouldBe true
createRewardTx(currentRate, height, incorrectPk).isFailure shouldBe true
createRewardTx(currentRate, height, minerPk).isFailure shouldBe true
createRewardTx(currentRate, height,
ErgoTree.fromSigmaBoolean(minerPk)).isFailure shouldBe true
}

def createRewardTx(emissionAmount: Long, nextHeight: Int, minerProp: ErgoTree): Try[ErgoLikeTransaction] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.ergoplatform.validation.ValidationSpecification
import org.ergoplatform.{Context => _, _}
import scalan.BaseCtxTests
import sigma.VersionContext
import sigmastate.Values.{BigIntArrayConstant, EvaluatedValue, SValue, SigmaPropConstant, Value}
import sigmastate.Values.{BigIntArrayConstant, ErgoTree, EvaluatedValue, SValue, SigmaPropConstant, Value}
import sigmastate.helpers.TestingHelpers._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting}
import sigmastate.interpreter.Interpreter.ScriptEnv
Expand Down Expand Up @@ -67,8 +67,8 @@ trait ErgoScriptTestkit extends ContractsTestkit with LangTests

lazy val boxToSpend = testBox(10, TrueTree, 0,
additionalRegisters = Map(ErgoBox.R4 -> BigIntArrayConstant(bigIntegerArr1)))
lazy val tx1Output1 = testBox(minToRaise, projectPubKey, 0)
lazy val tx1Output2 = testBox(1, projectPubKey, 0)
lazy val tx1Output1 = testBox(minToRaise, ErgoTree.fromProposition(projectPubKey), 0)
lazy val tx1Output2 = testBox(1, ErgoTree.fromProposition(projectPubKey), 0)
lazy val tx1 = new ErgoLikeTransaction(IndexedSeq(), IndexedSeq(), IndexedSeq(tx1Output1, tx1Output2))
lazy val ergoCtx = ErgoLikeContextTesting(
currentHeight = timeout - 1,
Expand Down
4 changes: 2 additions & 2 deletions sc/shared/src/test/scala/sigmastate/eval/EvaluationTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigmastate.eval

import org.ergoplatform.ErgoBox
import sigmastate.Values.{ConcreteCollection, IntArrayConstant, IntConstant, SigmaPropConstant, SigmaPropValue}
import sigmastate.Values.{ConcreteCollection, ErgoTree, IntArrayConstant, IntConstant, SigmaPropConstant, SigmaPropValue}
import sigmastate.helpers.ContextEnrichingTestProvingInterpreter
import sigmastate.helpers.TestingHelpers._
import sigmastate.interpreter.Interpreter._
Expand Down Expand Up @@ -38,7 +38,7 @@ class EvaluationTest extends BaseCtxTests
test("lazy logical ops") {
val prover = new ContextEnrichingTestProvingInterpreter
val pk = prover.dlogSecrets.head.publicImage
val self = testBox(1, pk, 0, additionalRegisters = Map(ErgoBox.R4 -> IntConstant(10)))
val self = testBox(1, ErgoTree.fromSigmaBoolean(pk), 0, additionalRegisters = Map(ErgoBox.R4 -> IntConstant(10)))
val ctx = newErgoContext(height = 1, self)
// guarded register access: existing reg
reduce(emptyEnv, "lazy1", "SELF.R4[Int].isDefined && SELF.R4[Int].get == 10", ctx, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons
).asBoolValue.toSigmaProp
prop shouldBe propExp

val newBox1 = testBox(10, pubkey, 0)
val newBox1 = testBox(10, ErgoTree.fromSigmaBoolean(pubkey), 0)
val newBoxes = IndexedSeq(newBox1)

val spendingTransaction = createTransaction(newBoxes)
Expand Down Expand Up @@ -257,7 +257,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons
lastBlockUtxoRoot = AvlTreeData.dummy,
minerPubkey = ErgoLikeContextTesting.dummyPubkey,
boxesToSpend = IndexedSeq(selfBox),
createTransaction(testBox(1, recipientProposition, 0)),
createTransaction(testBox(1, ErgoTree.fromSigmaBoolean(recipientProposition), 0)),
self = selfBox, activatedVersionInTests)

avlProver.performOneOperation(Lookup(treeElements.head._1))
Expand Down Expand Up @@ -325,7 +325,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons
).asBoolValue.toSigmaProp
prop shouldBe propExp

val newBox1 = testBox(10, pubkey, 0)
val newBox1 = testBox(10, ErgoTree.fromSigmaBoolean(pubkey), 0)
val newBoxes = IndexedSeq(newBox1)

val spendingTransaction = createTransaction(newBoxes)
Expand Down Expand Up @@ -379,7 +379,7 @@ class AVLTreeScriptsSpecification extends CompilerTestingCommons

val propTree = ErgoTree.fromProposition(ergoTreeHeaderInTests, prop)

val newBox1 = testBox(10, pubkey, 0)
val newBox1 = testBox(10, ErgoTree.fromSigmaBoolean(pubkey), 0)
val newBoxes = IndexedSeq(newBox1)

val spendingTransaction = ErgoLikeTransaction(IndexedSeq(), newBoxes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ block 1600 in 1622 ms, 30000000000 coins remain, defs: 61661
height: Int): ErgoLikeTransaction = {
assert(state.state.currentHeight == height - 1)
val ut = if (emissionBox.value > s.oneEpochReduction) {
val minerBox = new ErgoBoxCandidate(emissionAtHeight(height), minerProp, height, Colls.emptyColl, Map())
val minerBox = new ErgoBoxCandidate(emissionAtHeight(height), ErgoTree.fromSigmaBoolean(minerProp), height, Colls.emptyColl, Map())
val newEmissionBox: ErgoBoxCandidate =
new ErgoBoxCandidate(emissionBox.value - minerBox.value, tree, height, Colls.emptyColl, Map(register -> IntConstant(height)))

Expand All @@ -176,8 +176,8 @@ block 1600 in 1622 ms, 30000000000 coins remain, defs: 61661
IndexedSeq(newEmissionBox, minerBox)
)
} else {
val minerBox1 = new ErgoBoxCandidate(emissionBox.value - 1, minerProp, height, Colls.emptyColl, Map(register -> IntConstant(height)))
val minerBox2 = new ErgoBoxCandidate(1, minerProp, height, Colls.emptyColl, Map(register -> IntConstant(height)))
val minerBox1 = new ErgoBoxCandidate(emissionBox.value - 1, ErgoTree.fromSigmaBoolean(minerProp), height, Colls.emptyColl, Map(register -> IntConstant(height)))
val minerBox2 = new ErgoBoxCandidate(1, ErgoTree.fromSigmaBoolean(minerProp), height, Colls.emptyColl, Map(register -> IntConstant(height)))
UnsignedErgoLikeTransaction(
IndexedSeq(new UnsignedInput(emissionBox.id)),
IndexedSeq(minerBox1, minerBox2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sigmastate.utxo.examples

import org.ergoplatform.ErgoBox.{R6, R4, R5}
import org.ergoplatform.ErgoBox.{R4, R5, R6}
import org.ergoplatform._
import sigmastate.{AvlTreeData, 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._
Expand Down Expand Up @@ -126,7 +126,7 @@ class ColdWalletAdvContractExampleSpecification extends CompilerTestingCommons
R6 -> LongConstant(avbl2Key) // new avbl2Key (= old avbl2Key)
)
)
val firstWithdrawOutput1Key = testBox(firstWithdrawAmount1Key, carolPubKey, firstWithdrawHeight)
val firstWithdrawOutput1Key = testBox(firstWithdrawAmount1Key, ErgoTree.fromSigmaBoolean(carolPubKey), firstWithdrawHeight)

//normally this transaction would be invalid, but we're not checking it in this test
val firstWithdrawTx1Key = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(firstChangeOutput1Key, firstWithdrawOutput1Key))
Expand Down Expand Up @@ -163,7 +163,7 @@ class ColdWalletAdvContractExampleSpecification extends CompilerTestingCommons
R6 -> LongConstant(avbl2Key - firstWithdrawAmount2Key) // new avbl2Key (= old avbl2Key)
)
)
val firstWithdrawOutput2Key = testBox(firstWithdrawAmount2Key, carolPubKey, firstWithdrawHeight)
val firstWithdrawOutput2Key = testBox(firstWithdrawAmount2Key, ErgoTree.fromSigmaBoolean(carolPubKey), firstWithdrawHeight)

//normally this transaction would be invalid, but we're not checking it in this test
val firstWithdrawTx2Key = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(firstChangeOutput2Key, firstWithdrawOutput2Key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package sigmastate.utxo.examples

import org.ergoplatform.ErgoBox.{R4, R5}
import org.ergoplatform._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, CompilerTestingCommons, ErgoLikeTestInterpreter}
import sigmastate.helpers.{CompilerTestingCommons, ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTestInterpreter}
import sigmastate.helpers.TestingHelpers._
import sigmastate.{AvlTreeData, CompilerCrossVersionProps}
import sigmastate.Values.{LongConstant, IntConstant}
import sigmastate.Values.{ErgoTree, IntConstant, LongConstant}
import sigmastate.interpreter.Interpreter.ScriptNameProp
import sigmastate.lang.Terms._

Expand Down Expand Up @@ -92,7 +92,7 @@ class ColdWalletContractExampleSpecification extends CompilerTestingCommons
// Both Alice ane Bob withdraw
val withdrawAmountFull = depositAmount // full amount is withdrawn

val withdrawOutputAliceAndBob = testBox(withdrawAmountFull, carolPubKey, firstWithdrawHeight)
val withdrawOutputAliceAndBob = testBox(withdrawAmountFull, ErgoTree.fromSigmaBoolean(carolPubKey), firstWithdrawHeight)

val withdrawTxAliceAndBob = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(withdrawOutputAliceAndBob))

Expand Down Expand Up @@ -122,7 +122,7 @@ class ColdWalletContractExampleSpecification extends CompilerTestingCommons
R5 -> LongConstant(min) // newMin (= old min) = 99000
)
)
val firstWithdrawOutput = testBox(firstWithdrawAmount, carolPubKey, firstWithdrawHeight)
val firstWithdrawOutput = testBox(firstWithdrawAmount, ErgoTree.fromSigmaBoolean(carolPubKey), firstWithdrawHeight)

//normally this transaction would be invalid, but we're not checking it in this test
val firstWithdrawTx = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(firstChangeOutput, firstWithdrawOutput))
Expand Down Expand Up @@ -152,7 +152,7 @@ class ColdWalletContractExampleSpecification extends CompilerTestingCommons
R5 -> LongConstant(min) // newMin (= old min)
)
)
val withdrawOutputInvalid = testBox(withdrawAmountInvalid, carolPubKey, firstWithdrawHeight)
val withdrawOutputInvalid = testBox(withdrawAmountInvalid, ErgoTree.fromSigmaBoolean(carolPubKey), firstWithdrawHeight)

// normally this transaction would be invalid, but we're not checking it in this test
val withdrawTxInvalid = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(changeOutputInvalid, withdrawOutputInvalid))
Expand Down Expand Up @@ -187,7 +187,7 @@ class ColdWalletContractExampleSpecification extends CompilerTestingCommons
R5 -> LongConstant(secondMin) // newMin
)
)
val secondWithdrawOutput = testBox(secondWithdrawAmount, carolPubKey, secondWithdrawHeight)
val secondWithdrawOutput = testBox(secondWithdrawAmount, ErgoTree.fromSigmaBoolean(carolPubKey), secondWithdrawHeight)

//normally this transaction would be invalid, but we're not checking it in this test
val secondWithdrawTx = ErgoLikeTransaction(IndexedSeq(), IndexedSeq(secondChangeOutput, secondWithdrawOutput))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class MASTExampleSpecification extends CompilerTestingCommons
lastBlockUtxoRoot = AvlTreeData.dummy,
minerPubkey = ErgoLikeContextTesting.dummyPubkey,
boxesToSpend = IndexedSeq(selfBox),
createTransaction(testBox(1, recipientProposition, 0)),
createTransaction(testBox(1, ErgoTree.fromSigmaBoolean(recipientProposition), 0)),
self = selfBox, activatedVersionInTests)

avlProver.performOneOperation(Lookup(knownSecretTreeKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package sigmastate.utxo.examples

import java.math.BigInteger

import org.ergoplatform.ErgoBox.{R4, R5}
import scorex.crypto.hash.Blake2b256
import sigmastate.{AvlTreeData, CompilerCrossVersionProps}
import sigmastate.Values.GroupElementConstant
import sigmastate.Values.{ErgoTree, 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._
Expand Down Expand Up @@ -185,7 +184,7 @@ class MixExampleSpecification extends CompilerTestingCommons
val carolPubKey: ProveDlog = carol.dlogSecrets.head.publicImage

val spendHeight = 90
val carolOutput = testBox(mixAmount, carolPubKey, spendHeight)
val carolOutput = testBox(mixAmount, ErgoTree.fromSigmaBoolean(carolPubKey), spendHeight)

// normally this transaction would be invalid, but we're not checking it in this test
val spendingTx = createTransaction(carolOutput)
Expand Down
Loading

0 comments on commit 32a4652

Please sign in to comment.