<commit_before>package sigmastate.utxo

import org.ergoplatform.ErgoLikeContext
import scorex.util.encode.Base16
import scorex.crypto.hash.Blake2b256
import sigmastate.Values._
import sigmastate._
import sigmastate.lang.Terms._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeTestInterpreter, SigmaTestingCommons}
import sigmastate.lang.exceptions.OptionUnwrapNone

class ContextEnrichingSpecification extends SigmaTestingCommons {
  implicit lazy val IR = new TestingIRContext
  property("context enriching mixed w. crypto") {
    val prover = new ContextEnrichingTestProvingInterpreter
    val preimage = prover.contextExtenders(1).value.asInstanceOf[Array[Byte]]
    val pubkey = prover.dlogSecrets.head.publicImage

    val env = Map("blake" -> Blake2b256(preimage), "pubkey" -> pubkey)
    val compiledScript = compileWithCosting(env,
      """{
        |  pubkey && blake2b256(getVar[Coll[Byte]](1).get) == blake
        |}
      """.stripMargin).asSigmaProp
    val prop = SigmaAnd(
      pubkey,
      EQ(CalcBlake2b256(GetVarByteArray(1).get), ByteArrayConstant(Blake2b256(preimage))).toSigmaProp
    )
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(compiledScript, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    verifier.verify(env, compiledScript, ctx, pr.proof, fakeMessage).map(_._1).getOrElse(false) shouldBe false //context w/out extensions
    verifier.verify(env, compiledScript, ctxv, pr.proof, fakeMessage).get._1 shouldBe true
  }

  property("context enriching mixed w. crypto 2") {
    val prover = new ContextEnrichingTestProvingInterpreter
    val preimage1 = prover.contextExtenders(1).value.asInstanceOf[Array[Byte]]
    val preimage2 = prover.contextExtenders(2).value.asInstanceOf[Array[Byte]]
    val pubkey = prover.dlogSecrets.head.publicImage

    val env = Map("blake" -> Blake2b256(preimage1 ++ preimage2), "pubkey" -> pubkey)
    val compiledScript = compileWithCosting(env,
      """{
        |  pubkey && blake2b256(getVar[Coll[Byte]](1).get ++ getVar[Coll[Byte]](2).get) == blake
        |}
      """.stripMargin).asSigmaProp

    val prop = SigmaAnd(
      pubkey,
      EQ(
        CalcBlake2b256(Append(GetVarByteArray(1).get, GetVarByteArray(2).get)),
        ByteArrayConstant(Blake2b256(preimage1 ++ preimage2))
      ).toSigmaProp
    )
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(compiledScript, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    verifier.verify(env, compiledScript, ctx, pr.proof, fakeMessage).map(_._1).getOrElse(false) shouldBe false //context w/out extensions
    verifier.verify(env, compiledScript, ctxv, pr.proof, fakeMessage).get._1 shouldBe true
  }

  // todo: fix broken XOR for byte arrays
  // in https://github.com/ScorexFoundation/sigmastate-interpreter/issues/324
  ignore("prover enriching context - xor") {
    val v1 = Base16.decode("abcdef7865").get
    val k1 = 21: Byte

    val v2 = Base16.decode("10abdca345").get
    val k2 = 22: Byte

    val r = Base16.decode("bb6633db20").get

    val prover = new ContextEnrichingTestProvingInterpreter()
      .withContextExtender(k1, ByteArrayConstant(v1))
      .withContextExtender(k2, ByteArrayConstant(v2))

    val env = Map("k1" -> k1.toInt, "k2" -> k2.toInt, "r" -> r)
    val compiledScript = compileWithCosting(env,
      """{
        |  (getVar[Coll[Byte]](k1).get | getVar[Coll[Byte]](k2).get) == r
        |}
      """.stripMargin).asBoolValue.toSigmaProp

    val prop = EQ(Xor(GetVarByteArray(k1).get, GetVarByteArray(k2).get), ByteArrayConstant(r)).toSigmaProp
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(prop, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    //context w/out extensions
    assertExceptionThrown(
      verifier.verify(env, prop, ctx, pr.proof, fakeMessage).get,
      rootCause(_).isInstanceOf[NoSuchElementException]<commit_msg>xor test modified to 419 and 420<commit_after>package sigmastate.utxo

import org.ergoplatform.ErgoLikeContext
import scorex.util.encode.Base16
import scorex.crypto.hash.Blake2b256
import sigmastate.Values._
import sigmastate._
import sigmastate.lang.Terms._
import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeTestInterpreter, SigmaTestingCommons}


class ContextEnrichingSpecification extends SigmaTestingCommons {

  implicit lazy val IR: TestingIRContext = new TestingIRContext

  property("context enriching mixed w. crypto") {
    val prover = new ContextEnrichingTestProvingInterpreter
    val preimage = prover.contextExtenders(1).value.asInstanceOf[Array[Byte]]
    val pubkey = prover.dlogSecrets.head.publicImage

    val env = Map("blake" -> Blake2b256(preimage), "pubkey" -> pubkey)
    val compiledScript = compileWithCosting(env,
      """{
        |  pubkey && blake2b256(getVar[Coll[Byte]](1).get) == blake
        |}
      """.stripMargin).asSigmaProp
    val prop = SigmaAnd(
      pubkey,
      EQ(CalcBlake2b256(GetVarByteArray(1).get), ByteArrayConstant(Blake2b256(preimage))).toSigmaProp
    )
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(compiledScript, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    verifier.verify(env, compiledScript, ctx, pr.proof, fakeMessage).map(_._1).getOrElse(false) shouldBe false //context w/out extensions
    verifier.verify(env, compiledScript, ctxv, pr.proof, fakeMessage).get._1 shouldBe true
  }

  property("context enriching mixed w. crypto 2") {
    val prover = new ContextEnrichingTestProvingInterpreter
    val preimage1 = prover.contextExtenders(1).value.asInstanceOf[Array[Byte]]
    val preimage2 = prover.contextExtenders(2).value.asInstanceOf[Array[Byte]]
    val pubkey = prover.dlogSecrets.head.publicImage

    val env = Map("blake" -> Blake2b256(preimage1 ++ preimage2), "pubkey" -> pubkey)
    val compiledScript = compileWithCosting(env,
      """{
        |  pubkey && blake2b256(getVar[Coll[Byte]](1).get ++ getVar[Coll[Byte]](2).get) == blake
        |}
      """.stripMargin).asSigmaProp

    val prop = SigmaAnd(
      pubkey,
      EQ(
        CalcBlake2b256(Append(GetVarByteArray(1).get, GetVarByteArray(2).get)),
        ByteArrayConstant(Blake2b256(preimage1 ++ preimage2))
      ).toSigmaProp
    )
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(compiledScript, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    verifier.verify(env, compiledScript, ctx, pr.proof, fakeMessage).map(_._1).getOrElse(false) shouldBe false //context w/out extensions
    verifier.verify(env, compiledScript, ctxv, pr.proof, fakeMessage).get._1 shouldBe true
  }

  // todo: ignored because of https://github.com/ScorexFoundation/sigmastate-interpreter/issues/419
  // todo: and https://github.com/ScorexFoundation/sigmastate-interpreter/issues/420
  ignore("prover enriching context - xor") {
    val v1 = Base16.decode("abcdef7865").get
    val k1 = 21: Byte

    val v2 = Base16.decode("10abdca345").get
    val k2 = 22: Byte

    val r = Base16.decode("bb6633db20").get

    val prover = new ContextEnrichingTestProvingInterpreter()
      .withContextExtender(k1, ByteArrayConstant(v1))
      .withContextExtender(k2, ByteArrayConstant(v2))

    val env = Map("k1" -> k1.toInt, "k2" -> k2.toInt, "r" -> r)
    val compiledScript = compileWithCosting(env,
      """{
        |
        |  // def Xor(c1: Coll[Byte], c2: Coll[Byte]): Coll[Byte] = c1.zipWith(c2, { (x, y) => x ^ y })
        |
        |  def Xor(c1: Coll[Byte], c2: Coll[Byte]): Coll[Byte] = c1.zip(c2).map({ (t : (Byte, Byte)) => t._1 ^ t._2 })
        |
        |  Xor(getVar[Coll[Byte]](k1).get, getVar[Coll[Byte]](k2).get) == r
        |}
      """.stripMargin).asBoolValue.toSigmaProp

    val prop = EQ(Xor(GetVarByteArray(k1).get, GetVarByteArray(k2).get), ByteArrayConstant(r)).toSigmaProp
    compiledScript shouldBe prop

    val ctx = ErgoLikeContext.dummy(fakeSelf)
    val pr = prover.prove(prop, ctx, fakeMessage).get

    val ctxv = ctx.withExtension(pr.extension)

    val verifier = new ErgoLikeTestInterpreter
    //context w/out extensions
    assertExceptionThrown(
      verifier.verify(env, prop, ctx, pr.proof, fakeMessage).get,
      rootCause(_).isInstanceOf[NoSuchElementException]
{'fraction_non_alphanumeric': 0.11775465874013948, 'fraction_numerical': 0.02549445524179719, 'mean_word_length': 3.909090909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 3, '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': '533025', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2876, 'n_words': 554}