Skip to content

Commit

Permalink
UnsignedBigInt result type for Global.powHit
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Jan 10, 2025
1 parent d13dd4d commit d6c0242
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ trait SigmaDslBuilder {
def xor(l: Coll[Byte], r: Coll[Byte]): Coll[Byte]

/** Calculates value of a custom Autolykos 2 hash function */
def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): BigInt
def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): UnsignedBigInt

/** Deserializes provided `bytes` into a value of type `T`. **/
def deserializeTo[T](bytes: Coll[Byte])(implicit cT: RType[T]): T
Expand Down
9 changes: 4 additions & 5 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import sigma.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIs
import sigma.data.NumericOps.BigIntIsExactIntegral
import sigma.data.OverloadHack.Overloaded1
import sigma.data.UnsignedBigIntNumericOps.UnsignedBigIntIsExactIntegral
import sigma.data.{DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.data.{CBigInt, DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.data.{CBigInt, CUnsignedBigInt, DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.eval.{CostDetails, ErgoTreeEvaluator, TracedCost}
import sigma.pow.Autolykos2PowValidation
import sigma.reflection.RClass
Expand Down Expand Up @@ -1944,7 +1943,7 @@ 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), methodId = 8,
this, "powHit", SFunc(Array(SGlobal, SInt, SByteArray, SByteArray, SByteArray, SInt), SUnsignedBigInt), methodId = 8,
PowHitCostKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall,
Expand All @@ -1957,10 +1956,10 @@ 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 = {
(implicit E: ErgoTreeEvaluator): UnsignedBigInt = {
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)
CUnsignedBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(k, msg.toArray, nonce.toArray, h.toArray, N).bigInteger)
}

private val deserializeCostKind = PerItemCost(baseCost = JitCost(30), perChunkCost = JitCost(20), chunkSize = 32)
Expand Down
4 changes: 2 additions & 2 deletions data/shared/src/main/scala/sigma/data/CSigmaDslBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ class CSigmaDslBuilder extends SigmaDslBuilder { dsl =>
Colls.fromArray(w.toBytes)
}

override def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): BigInt = {
override def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): UnsignedBigInt = {
val bi = Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(k, msg.toArray, nonce.toArray, h.toArray, N)
this.BigInt(bi.bigInteger)
this.UnsignedBigInt(bi.bigInteger)
}


Expand Down
2 changes: 1 addition & 1 deletion docs/LangSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ There are some functions which do not belong to other types, thus they put under
* @param nonce - used to pad the message to get Proof-of-Work hash function output with desirable properties
* @param h - PoW protocol specific padding for table uniqueness (e.g. block height in Ergo)
*/
def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): BigInt
def powHit(k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int): UnsignedBigInt
/** Deserializes provided `bytes` into a value of type `T`. **/
def deserializeTo[T](bytes: Coll[Byte])(implicit cT: RType[T]): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scorex.util.encode.Base16
import sigma.Extensions.ArrayOps
import sigma.ast.{ByteArrayConstant, IntConstant}
import sigma.crypto.SecP256K1Group
import sigma.data.{CBigInt, TrivialProp}
import sigma.data.{CBigInt, CUnsignedBigInt, TrivialProp}
import sigma.eval.SigmaDsl
import sigma.util.Extensions.SigmaBooleanOps
import sigma.util.NBitsUtils
Expand Down Expand Up @@ -108,7 +108,7 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers {
val hbs = Colls.fromArray(Base16.decode("00000000").get)
val N = 1024 * 1024

SigmaDsl.powHit(k, msg, nonce, hbs, N) shouldBe CBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344"))
SigmaDsl.powHit(k, msg, nonce, hbs, N) shouldBe CUnsignedBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344"))

val es = CErgoTreeEvaluator.DefaultEvalSettings
val accumulator = new CostAccumulator(
Expand All @@ -129,9 +129,9 @@ class BasicOpsTests extends AnyFunSuite with ContractsTestkit with Matchers {
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)
.evalTo[sigma.UnsignedBigInt](Map.empty)(evaluator)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ import scalan._
def xor(l: Ref[Coll[Byte]], r: Ref[Coll[Byte]]): Ref[Coll[Byte]]
def encodeNbits(bi: Ref[BigInt]): Ref[Long]
def decodeNbits(l: Ref[Long]): Ref[BigInt]
def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[BigInt];
def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[UnsignedBigInt];
def serialize[T](value: Ref[T]): Ref[Coll[Byte]]
def fromBigEndianBytes[T](bytes: Ref[Coll[Byte]])(implicit cT: Elem[T]): Ref[T]
def deserializeTo[T](bytes: Ref[Coll[Byte]])(implicit cT: Elem[T]): Ref[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2294,11 +2294,11 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
}


override def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[BigInt] = {
asRep[BigInt](mkMethodCall(self,
override def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[UnsignedBigInt] = {
asRep[UnsignedBigInt](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("powHit", classOf[Sym], classOf[Sym], classOf[Sym], classOf[Sym], classOf[Sym]),
Array[AnyRef](k, msg, nonce, h, N),
true, false, element[BigInt]))
true, false, element[UnsignedBigInt](UnsignedBigInt.unsignedBigIntElement)))
}

override def encodeNbits(bi: Ref[BigInt]): Ref[Long] = {
Expand Down Expand Up @@ -2474,11 +2474,11 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, true, element[Coll[Byte]]))
}

def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[BigInt] = {
asRep[BigInt](mkMethodCall(source,
def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[UnsignedBigInt] = {
asRep[UnsignedBigInt](mkMethodCall(source,
SigmaDslBuilderClass.getMethod("powHit", classOf[Sym], classOf[Sym], classOf[Sym], classOf[Sym], classOf[Sym]),
Array[AnyRef](k, msg, nonce, h, N),
true, true, element[BigInt]))
true, true, element[UnsignedBigInt](UnsignedBigInt.unsignedBigIntElement)))
}

def serialize[T](value: Ref[T]): Ref[Coll[Byte]] = {
Expand Down
6 changes: 3 additions & 3 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1468,12 +1468,12 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
}

property("Global.powHit") {
def powHit: Feature[Coll[Byte], sigma.BigInt] = newFeature(
def powHit: Feature[Coll[Byte], sigma.UnsignedBigInt] = newFeature(
{ (x: Coll[Byte]) =>
val msg = x.slice(0, 7).toArray
val nonce = x.slice(7, 15).toArray
val h = x.slice(15, 19).toArray
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessageWithChecks(32, msg, nonce, h, 1024 * 1024).bigInteger)
CUnsignedBigInt(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) }",
Expand All @@ -1499,7 +1499,7 @@ 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"))
val hit = CUnsignedBigInt(new BigInteger("326674862673836209462483453386286740270338859283019276168539876024851191344"))

verifyCases(
Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class TestingInterpreterSpecification extends CompilerTestingCommons
val source =
"""
|{
| val b: BigInt = bigInt("1157920892373161954235709850086879078528375642790749043826051631415181614943")
| val b = unsignedBigInt("1157920892373161954235709850086879078528375642790749043826051631415181614943")
| val k = 32
| val N = 1024 * 1024
| val msg = fromBase16("0a101b8c6a4f2e")
Expand Down

0 comments on commit d6c0242

Please sign in to comment.