<commit_before>package scapi.sigma

import java.math.BigInteger
import java.security.SecureRandom

import edu.biu.scapi.primitives.dlog.bc.BcDlogECFp
import edu.biu.scapi.primitives.dlog.{DlogGroup, ECElementSendableData, GroupElement}
import org.bouncycastle.util.BigIntegers
import scapi.sigma.rework._
import scorex.core.serialization.Serializer
import scorex.core.transaction.state.SecretCompanion
import sigmastate.SigmaProposition.PropositionCode
import sigmastate.utxo.CostTable.Cost
import sigmastate.{EcPointFunctions, SigmaProofOfKnowledgeTree}

import scala.concurrent.Future
import scala.util.Try

object DLogProtocol {


  trait DLogSigmaProtocol extends SigmaProtocol[DLogSigmaProtocol]{
    override type A = FirstDLogProverMessage
    override type Z = SecondDLogProverMessage
  }

  case class ProveDlog(h: GroupElement)
    extends SigmaProtocolCommonInput[DLogSigmaProtocol]
      with SigmaProofOfKnowledgeTree[DLogSigmaProtocol, DLogProverInput] {

    override val cost: Int = Cost.Dlog

    override type M = this.type
    override val code: PropositionCode = ProveDlog.Code

    override lazy val dlogGroup: DlogGroup = ProveDlog.dlogGroup
    override val soundness: Int = 256

    override lazy val bytes: Array[Byte] = {
      val gw = h.generateSendableData().asInstanceOf[ECElementSendableData]
      val gwx = gw.getX.toByteArray
      val gwy = gw.getY.toByteArray
      gwx ++ gwy
    }
  }

  object ProveDlog {
    val Code: PropositionCode = 102: Byte

    lazy val dlogGroup: DlogGroup = new BcDlogECFp()

    def fromBytes(bytes: Array[Byte]): ProveDlog = {
      val (x, y) = EcPointFunctions.decodeBigIntPair(bytes).get
      val xy = new ECElementSendableData(x, y)
      val h = dlogGroup.reconstructElement(true, xy)
      ProveDlog(h)
    }
  }


  case class DLogProverInput(w: BigInteger)(implicit val dlogGroup: DlogGroup, soundness: Int)
    extends SigmaProtocolPrivateInput[DLogSigmaProtocol] {

    override type S = DLogProverInput
    override type PK = ProveDlog

    override def companion: SecretCompanion[DLogProverInput] = ???

    override lazy val publicImage: ProveDlog = {
      val g = dlogGroup.getGenerator
      ProveDlog(dlogGroup.exponentiate(g, w))
    }

    override type M = DLogProverInput<commit_msg>ProveDlog takes GroupElementLeaf as parameter<commit_after>package scapi.sigma

import java.math.BigInteger
import java.security.SecureRandom

import edu.biu.scapi.primitives.dlog.bc.BcDlogECFp
import edu.biu.scapi.primitives.dlog.{DlogGroup, ECElementSendableData, GroupElement}
import org.bouncycastle.util.BigIntegers
import scapi.sigma.rework._
import scorex.core.serialization.Serializer
import scorex.core.transaction.state.SecretCompanion
import sigmastate.SigmaProposition.PropositionCode
import sigmastate.utxo.CostTable.Cost
import sigmastate.{EcPointFunctions, GroupElementConstant, GroupElementLeaf, SigmaProofOfKnowledgeTree}

import scala.concurrent.Future
import scala.util.Try

object DLogProtocol {


  trait DLogSigmaProtocol extends SigmaProtocol[DLogSigmaProtocol]{
    override type A = FirstDLogProverMessage
    override type Z = SecondDLogProverMessage
  }

  case class ProveDlog(value: GroupElementLeaf)
    extends SigmaProtocolCommonInput[DLogSigmaProtocol]
      with SigmaProofOfKnowledgeTree[DLogSigmaProtocol, DLogProverInput] {

    override val cost: Int = Cost.Dlog

    override type M = this.type
    override val code: PropositionCode = ProveDlog.Code

    override lazy val dlogGroup: DlogGroup = ProveDlog.dlogGroup
    override val soundness: Int = 256

    //todo: fix, we should consider that class parameter could be not evaluated
    lazy val h = value.asInstanceOf[GroupElementConstant].value

    override lazy val bytes: Array[Byte] = {
      val gw = h.generateSendableData().asInstanceOf[ECElementSendableData]
      val gwx = gw.getX.toByteArray
      val gwy = gw.getY.toByteArray
      gwx ++ gwy
    }
  }

  object ProveDlog {

    def apply(h: GroupElement): ProveDlog = ProveDlog(GroupElementConstant(h))

    val Code: PropositionCode = 102: Byte

    lazy val dlogGroup: DlogGroup = new BcDlogECFp()

    def fromBytes(bytes: Array[Byte]): ProveDlog = {
      val (x, y) = EcPointFunctions.decodeBigIntPair(bytes).get
      val xy = new ECElementSendableData(x, y)
      val h = dlogGroup.reconstructElement(true, xy)
      ProveDlog(h)
    }
  }


  case class DLogProverInput(w: BigInteger)(implicit val dlogGroup: DlogGroup, soundness: Int)
    extends SigmaProtocolPrivateInput[DLogSigmaProtocol] {

    override type S = DLogProverInput
    override type PK = ProveDlog

    override def companion: SecretCompanion[DLogProverInput] = ???

    override lazy val publicImage: ProveDlog = {
      val g = dlogGroup.getGenerator
      ProveDlog(dlogGroup.exponentiate(g, w))
    }

    override type M = DLogProverInput
{'fraction_non_alphanumeric': 0.0741962077493817, 'fraction_numerical': 0.00247320692497939, 'mean_word_length': 4.246486486486487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '244606', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1533, 'n_words': 352}