Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Global.decodeNBits can produce bigint out of 256 bits scope #1045

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion data/shared/src/main/scala/sigma/data/CSigmaDslBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class CSigmaDslBuilder extends SigmaDslBuilder { dsl =>
}

override def decodeNbits(l: Long): BigInt = {
CBigInt(NBitsUtils.decodeCompactBits(l).bigInteger)
// Result is limited to 256 bits with .toSignedBigIntValueExact
CBigInt(NBitsUtils.decodeCompactBits(l).bigInteger.toSignedBigIntValueExact)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import sigma.interpreter.ContextExtension.VarBinding
import sigmastate.interpreter.CErgoTreeEvaluator.{DefaultEvalSettings, currentEvaluator}
import sigmastate.interpreter.Interpreter._
import sigma.ast.Apply
import sigma.eval.EvalSettings
import sigma.eval.{EvalSettings, SigmaDsl}
import sigma.exceptions.InvalidType
import sigma.serialization.{ErgoTreeSerializer, SerializerException}
import sigma.serialization.{DataSerializer, ErgoTreeSerializer, SigmaByteWriter, SigmaSerializer, ValueSerializer}
Expand Down Expand Up @@ -3248,4 +3248,27 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Global.decodeNbits - result of more than 256 bits") {
val okValue = SigmaDsl.encodeNbits(CBigInt(new BigInteger("2").pow(255).subtract(BigInteger.ONE)))
val invalidValue = SigmaDsl.encodeNbits(CBigInt(new BigInteger("2").pow(256).subtract(BigInteger.ONE)))

def someTest(value: Long): Assertion = {
test("some", env, ext,
s"""{
| val target = Global.decodeNbits(${value}L)
| target != 0
|}""".stripMargin,
null
)
}

if (VersionContext.current.isV6SoftForkActivated) {
someTest(okValue)
// on JVM, InvocationTargetException wrapping (ArithmeticException: BigInteger out of 256 bit range) is thrown
an[Exception] should be thrownBy someTest(invalidValue)
} else {
an[sigma.validation.ValidationException] should be thrownBy someTest(okValue)
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have test cases for exact boundaries for the max value N:

  1. for value N it passes
  2. for N+1 it fails

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done something similar

}
Loading