Skip to content

Commit

Permalink
powHit impl #1
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Apr 16, 2024
1 parent 6abf794 commit 1d9f80a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
18 changes: 16 additions & 2 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package sigma.ast

import org.ergoplatform._
import org.ergoplatform.validation._
import sigma.{VersionContext, _}
import sigma.{Coll, VersionContext, _}
import sigma.ast.SCollection.{SBooleanArray, SBoxArray, SByteArray, SByteArray2, SHeaderArray}
import sigma.ast.SMethod.{MethodCallIrBuilder, MethodCostFunc, javaMethodOf}
import sigma.ast.SType.TypeCode
import sigma.ast.syntax.{SValue, ValueOps}
import sigma.data.OverloadHack.Overloaded1
import sigma.data.{DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.data.{CBigInt, DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.eval.{CostDetails, ErgoTreeEvaluator, TracedCost}
import sigma.pow.Autolykos2PowValidation
import sigma.reflection.RClass
import sigma.serialization.CoreByteWriter.ArgInfo
import sigma.utils.SparseArrayContainer
Expand Down Expand Up @@ -1511,6 +1512,19 @@ case object SGlobalMethods extends MonoTypeMethods {
.withInfo(Xor, "Byte-wise XOR of two collections of bytes",
ArgInfo("left", "left operand"), ArgInfo("right", "right operand"))

lazy val checkPoWMethod = SMethod(
this, "powHit", SFunc(Array(SGlobal, SInt, SByteArray, SByteArray, SByteArray, SInt), SBigInt), 2, Xor.costKind) // todo: cost
.withIRInfo(MethodCallIrBuilder)
.withInfo(Xor, "Byte-wise XOR of two collections of bytes",
ArgInfo("left", "left operand"), ArgInfo("right", "right operand"))

def powHit_eval(mc: MethodCall, G: SigmaDslBuilder, k: Int, msg: Coll[Byte], nonce: Coll[Byte], h: Coll[Byte], N: Int)
(implicit E: ErgoTreeEvaluator): BigInt = {
E.addSeqCost(Xor.costKind, msg.length, Xor.opDesc) { () => // todo: proper costing
CBigInt(Autolykos2PowValidation.hitForVersion2ForMessage(k, msg.toArray, nonce.toArray, h.toArray, N).bigInteger)
}
}

/** Implements evaluation of Global.xor method call ErgoTree node.
* Called via reflection based on naming convention.
* @see SMethod.evalMethod, Xor.eval, Xor.xorWithCosting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ object Autolykos2PowValidation {
/**
* Generate element of Autolykos equation.
*/
private def genElementV2(m: Array[Byte],
pk: Array[Byte], // not used in v2
w: Array[Byte], // not used in v2
indexBytes: Array[Byte],
heightBytes: => Array[Byte] // not used in v1
): BigInt = {
private def genElementV2(indexBytes: Array[Byte], heightBytes: => Array[Byte]): BigInt = {
// Autolykos v. 2: H(j|h|M) (line 5 from the Algo 2 of the spec)
toBigInt(hash(Bytes.concat(indexBytes, heightBytes, M)).drop(1))
}
Expand All @@ -60,7 +55,7 @@ object Autolykos2PowValidation {

val indexes = genIndexes(k, seed, N)
//pk and w not used in v2
val elems = indexes.map(idx => genElementV2(msg, null, null, Ints.toByteArray(idx), h))
val elems = indexes.map(idx => genElementV2(Ints.toByteArray(idx), h))
val f2 = elems.sum

// sum as byte array is always about 32 bytes
Expand Down
3 changes: 2 additions & 1 deletion sc/shared/src/main/scala/special/sigma/SigmaDslUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ package sigma {
def decodePoint(encoded: Ref[Coll[Byte]]): Ref[GroupElement];
/** This method will be used in v6.0 to handle CreateAvlTree operation in GraphBuilding */
def avlTree(operationFlags: Ref[Byte], digest: Ref[Coll[Byte]], keyLength: Ref[Int], valueLengthOpt: Ref[WOption[Int]]): Ref[AvlTree];
def xor(l: Ref[Coll[Byte]], r: Ref[Coll[Byte]]): Ref[Coll[Byte]]
def xor(l: Ref[Coll[Byte]], r: Ref[Coll[Byte]]): Ref[Coll[Byte]];
def powHit(k: Ref[Int], msg: Ref[Coll[Byte]], nonce: Ref[Coll[Byte]], h: Ref[Coll[Byte]], N: Ref[Int]): Ref[BigInt];
};
trait CostModelCompanion;
trait BigIntCompanion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,13 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
Array[AnyRef](l, r),
true, false, element[Coll[Byte]]))
}

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

implicit object LiftableSigmaDslBuilder
Expand Down

0 comments on commit 1d9f80a

Please sign in to comment.