<commit_before>package scorex.crypto.authds.skiplist

import com.google.common.primitives.Ints
import play.api.libs.json._
import scorex.crypto.authds.AuthData
import scorex.crypto.encode.Base58
import scorex.crypto.hash.{CommutativeHash, CryptographicHash}

import scala.annotation.tailrec
import scala.util.Try

sealed trait SLProof extends SLProofI

/**
 *
 * @param e
 * @param l
 * @param r - None for MaxSlElement, Some for others
 */
case class SLNonExistenceProof(e: SLElement, l: SLExistenceProof, r: Option[SLExistenceProof]) extends SLProof {
  lazy val bytes: Array[Byte] = {
    val eSize = Ints.toByteArray(e.bytes.length)
    val leftSize = Ints.toByteArray(l.bytes.length)
    val rightSize = Ints.toByteArray(r.map(r => r.bytes.length).getOrElse(0))

    Array(0: Byte) ++ eSize ++ leftSize ++ rightSize ++ e.bytes ++ l.bytes ++ r.map(_.bytes).getOrElse(Array())
  }

  override def isEmpty: Boolean = true

  override def check[HF <: CommutativeHash[_]](rootHash: Digest)(implicit hf: HF): Boolean = {
    val linked: Boolean = r match {
      case None => l.proof.hashes.head sameElements hf(MaxSLElement.bytes)
      case Some(rp) => l.leftNeighborTo(rp)
    }
    val rightCheck = r.map(rp => e < rp.e && rp.check(rootHash)).getOrElse(true)

    linked && e > l.e && l.check(rootHash)
  }
}

/**
 * @param e - element to proof
 * @param proof - skiplist path, complementary to data block
 */
case class SLExistenceProof(e: SLElement, proof: SLPath) extends SLProof {

  def leftNeighborTo[HF <: CommutativeHash[_]](that: SLExistenceProof)(implicit hf: HF): Boolean = {
    val tower = proof.hashes.head sameElements hf(that.e.bytes)
    val nonTower = proof.hashes.head sameElements hf(hf(that.e.bytes), that.proof.hashes.head)
    tower || nonTower
  }<commit_msg>Calculate non-tower check only ehrn tower check fails<commit_after>package scorex.crypto.authds.skiplist

import com.google.common.primitives.Ints
import play.api.libs.json._
import scorex.crypto.authds.AuthData
import scorex.crypto.encode.Base58
import scorex.crypto.hash.{CommutativeHash, CryptographicHash}

import scala.annotation.tailrec
import scala.util.Try

sealed trait SLProof extends SLProofI

/**
 *
 * @param e
 * @param l
 * @param r - None for MaxSlElement, Some for others
 */
case class SLNonExistenceProof(e: SLElement, l: SLExistenceProof, r: Option[SLExistenceProof]) extends SLProof {
  lazy val bytes: Array[Byte] = {
    val eSize = Ints.toByteArray(e.bytes.length)
    val leftSize = Ints.toByteArray(l.bytes.length)
    val rightSize = Ints.toByteArray(r.map(r => r.bytes.length).getOrElse(0))

    Array(0: Byte) ++ eSize ++ leftSize ++ rightSize ++ e.bytes ++ l.bytes ++ r.map(_.bytes).getOrElse(Array())
  }

  override def isEmpty: Boolean = true

  override def check[HF <: CommutativeHash[_]](rootHash: Digest)(implicit hf: HF): Boolean = {
    val linked: Boolean = r match {
      case None => l.proof.hashes.head sameElements hf(MaxSLElement.bytes)
      case Some(rp) => l.leftNeighborTo(rp)
    }
    val rightCheck = r.map(rp => e < rp.e && rp.check(rootHash)).getOrElse(true)

    linked && e > l.e && l.check(rootHash)
  }
}

/**
 * @param e - element to proof
 * @param proof - skiplist path, complementary to data block
 */
case class SLExistenceProof(e: SLElement, proof: SLPath) extends SLProof {

  def leftNeighborTo[HF <: CommutativeHash[_]](that: SLExistenceProof)(implicit hf: HF): Boolean = {
    val tower = proof.hashes.head sameElements hf(that.e.bytes)
    lazy val nonTower = proof.hashes.head sameElements hf(hf(that.e.bytes), that.proof.hashes.head)
    tower || nonTower
  }
{'fraction_non_alphanumeric': 0.12800663533314902, 'fraction_numerical': 0.002211777716339508, 'mean_word_length': 4.067226890756302, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '6099911', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1235, 'n_words': 294}