Skip to content

Commit

Permalink
fixing and improving test, powHit version, tree version check
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Aug 7, 2024
1 parent fb3c671 commit 0d3f4be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
6 changes: 5 additions & 1 deletion data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,8 @@ case object SGlobalMethods extends MonoTypeMethods {
ArgInfo("left", "left operand"), ArgInfo("right", "right operand"))

lazy val powHitMethod = SMethod(
this, "powHit", SFunc(Array(SGlobal, SInt, SByteArray, SByteArray, SByteArray, SInt), SBigInt), 3, PowHitCostKind)
this, "powHit", SFunc(Array(SGlobal, SInt, SByteArray, SByteArray, SByteArray, SInt), SBigInt), methodId = 3,
PowHitCostKind, sinceVersion = VersionContext.V6SoftForkVersion)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall,
"Calculating Proof-of-Work hit (Autolykos 2 hash value) for custom Autolykos 2 function",
Expand All @@ -1543,6 +1544,9 @@ case object SGlobalMethods extends MonoTypeMethods {

def powHit_eval(mc: MethodCall, G: SigmaDslBuilder, k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int)
(implicit E: ErgoTreeEvaluator): BigInt = {
if(E.context.currentErgoTreeVersion < VersionContext.V6SoftForkVersion) {
syntax.error(s"Global.powHit is called from a tree with version < 3")
}
val cost = PowHitCostKind.cost(k, msg, nonce, h)
E.addCost(FixedCost(cost), powHitMethod.opDesc)
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(k, msg.toArray, nonce.toArray, h.toArray, N).bigInteger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package sigmastate.eval
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import scorex.util.encode.Base16
import sigma.ast.{ByteArrayConstant, ErgoTree, Global, IntConstant, JitCost, MethodCall, SGlobal, SGlobalMethods}
import sigma.Extensions.ArrayOps
import sigma.ast.{ByteArrayConstant, ErgoTree, Global, IntConstant, JitCost, MethodCall, SGlobalMethods}
import sigma.crypto.SecP256K1Group
import sigma.data.{CBigInt, CSigmaDslBuilder, TrivialProp}
import sigma.data.{CSigmaDslBuilder => SigmaDsl, TrivialProp}
import sigma.data.{CBigInt, TrivialProp, CSigmaDslBuilder => SigmaDsl}
import sigma.util.Extensions.SigmaBooleanOps

import java.math.BigInteger
import sigma.{ContractsTestkit, SigmaDslBuilder, SigmaProp}
import sigma.{Box, ContractsTestkit, SigmaProp, VersionContext}
import sigmastate.interpreter.CErgoTreeEvaluator.DefaultProfiler
import sigmastate.interpreter.{CErgoTreeEvaluator, CostAccumulator}
import sigma.{ContractsTestkit, SigmaProp}

import scala.language.implicitConversions

Expand Down Expand Up @@ -85,18 +83,26 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers {
val accumulator = new CostAccumulator(
initialCost = JitCost(0),
costLimit = Some(JitCost.fromBlockCost(es.scriptCostLimitInEvaluator)))

val context = new CContext(
noInputs.toColl, noHeaders, dummyPreHeader,
Array[Box]().toColl, Array[Box]().toColl, 0, null, 0, null,
dummyPubkey.toColl, Colls.emptyColl, VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion)

val evaluator = new CErgoTreeEvaluator(
context = null,
context = context,
constants = ErgoTree.EmptyConstants,
coster = accumulator, DefaultProfiler, es)


val res = MethodCall(Global, SGlobalMethods.powHitMethod,
IndexedSeq(IntConstant(k), ByteArrayConstant(msg), ByteArrayConstant(nonce),
ByteArrayConstant(hbs), IntConstant(N)), Map.empty)
.evalTo[sigma.BigInt](Map.empty)(evaluator)
VersionContext.withVersions(VersionContext.V6SoftForkVersion, VersionContext.V6SoftForkVersion) {
val res = MethodCall(Global, SGlobalMethods.powHitMethod,
IndexedSeq(IntConstant(k), ByteArrayConstant(msg), ByteArrayConstant(nonce),
ByteArrayConstant(hbs), IntConstant(N)), Map.empty)
.evalTo[sigma.BigInt](Map.empty)(evaluator)

res should be (CBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344")))
res should be(CBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344")))
}
}

}
26 changes: 11 additions & 15 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -503,25 +503,22 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
val msg = x.slice(0, 7).toArray
val nonce = x.slice(7, 15).toArray
val h = x.slice(15, 19).toArray
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(8, msg, nonce, h, 255).bigInteger) },
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(32, msg, nonce, h, 1024 * 1024).bigInteger) },
"{ (x: Coll[Byte]) => val msg = x.slice(0,7); val nonce = x.slice(7,15); val h = x.slice(15,19); " +
"Global.powHit(32, msg, nonce, h, 1024 * 1024) }",
FuncValue(
Array((1, SByteArray)),
GT(
MethodCall.typed[Value[SBigInt.type]](
Global,
SGlobalMethods.powHitMethod,
Array(
IntConstant(8),
ValUse(1, SByteArray),
ValUse(1, SByteArray),
ValUse(1, SByteArray),
IntConstant(255)
),
Map()
MethodCall.typed[Value[SBigInt.type]](
Global,
SGlobalMethods.powHitMethod,
Array(
IntConstant(32),
Slice(ValUse(1, SByteArray), IntConstant(0), IntConstant(7)),
Slice(ValUse(1, SByteArray), IntConstant(7), IntConstant(15)),
Slice(ValUse(1, SByteArray), IntConstant(15), IntConstant(19)),
IntConstant(1048576)
),
BigIntConstant(CBigInt(new BigInteger("0", 16)))
Map()
)
),
sinceVersion = VersionContext.V6SoftForkVersion)
Expand All @@ -531,7 +528,6 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
val nonce = Base16.decode("000000000000002c").get
val h = Base16.decode("00000000").get
val x = Colls.fromArray(msg ++ nonce ++ h)

val hit = CBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344"))

verifyCases(
Expand Down

0 comments on commit 0d3f4be

Please sign in to comment.