Add compiler-level box verification util functions (closes #1037) - #1176
Open
Ergologica wants to merge 1 commit into
Open
Add compiler-level box verification util functions (closes #1037)#1176Ergologica wants to merge 1 commit into
Ergologica wants to merge 1 commit into
Conversation
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add compiler-level utility functions for common box verification patterns
Closes #1037
Summary
This PR adds the 6 utility functions proposed in #1037 as predefined global functions of the ErgoScript frontend (
SigmaPredef.scala). Each function is expanded at compile time into ErgoTree nodes that are already supported by the interpreter, so no changes to the consensus-critical ErgoTree level (serializers, opcodes, evaluator,SigmaDsl) are involved — addressing the review feedback that led to closing #1086.verifySameForBasicRequiredRegisters(inBox: Box, outBox: Box): BooleaninBox.value == outBox.value && inBox.propositionBytes == outBox.propositionBytesverifySameForRequiredRegisters(inBox: Box, outBox: Box): Boolean&& inBox.tokens == outBox.tokensverifyUsedAdditionalRegisters(box: Box, used: Int): Boolean(used >= 1 || box.R4[Any].isEmpty) && ... && (used >= 6 || box.R9[Any].isEmpty)verifyBoxHasMarkerToken(box: Box, token: Coll[Byte]): Booleanbox.tokens.exists { t => t._1 == token && t._2 >= 1L }verifyBoxHasNoMarkerToken(box: Box, token: Coll[Byte]): Booleanbox.tokens.forall { t => t._1 != token }verifySpentToken(inBox: Box, outBox: Box, token: Coll[Byte], amount: Long): BooleaninBox.tokens.forall { it => if (it._1 == token) outBox.tokens.exists { ot => it._1 == ot._1 && it._2 == ot._2 + amount } else outBox.tokens.exists { ot => it._1 == ot._1 && it._2 == ot._2 } }The semantics follow the reference implementations given in #1037 (uncurried argument lists are used, consistently with the other predefined global functions).
Implementation notes
globalFuncsofPredefinedFuncRegistryand are expanded by the typer via the existingPredefinedFuncApplymechanism (PredefFuncInfo.irBuilder), likeallOf,xorOf, etc.EQ,BinAnd/BinOr(lazy),Exists/ForAll,ExtractAmount,ExtractScriptBytes,ExtractRegisterAs,MethodCall(Box.tokens),SelectField,If,ArithOp.Plus), so the resultingErgoTreeserializes and evaluates in all script versions.verifyUsedAdditionalRegisters— one behavioral caveat, documented in the function'sOperationInfo: the emptiness check is expressed asR{i}[Any].isEmpty(as in the issue's reference code). When a register beyond the firstusedones is defined,Box.getReg[Any]throwsInvalidTypeinstead of returningSome(_), so the script evaluation fails rather than returningfalse. Used as a guarding predicate this is the desired outcome (the spending attempt is rejected), but the value cannot be negated/composed to recover from the failing branch. As far as I can tell, current ErgoTree has no type-agnostic "register is defined" primitive that could avoid this.verifySpentTokenfollows the reference semantics literally: it requires the remainderot._2 == it._2 - amountto be present in the output box, therefore spending a token completely (down to zero) yieldsfalseby design (covered by a test).Testing
New
UtilFunctionsSpecification(insc/shared/src/test/scala/sigmastate/utxo/) with 20 properties run across script versions viaCompilerCrossVersionProps. Each function is covered with positive and negative cases; every script is compiled, wrapped into anErgoTree, proven and verified throughContextEnrichingTestProvingInterpreter/ErgoLikeTestInterpreteragainst a real spending context (SELF+OUTPUTS(0)with configurable values, tokens and registers). The ErgoTree serialization roundtrip is exercised implicitly by proving/verification.