Skip to content

Commit

Permalink
close #1044 : fix for Global.decodeNBits method can produce bigint ou…
Browse files Browse the repository at this point in the history
…t of 256 bits scope
  • Loading branch information
kushti committed Jan 6, 2025
1 parent d13dd4d commit aa61236
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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 @@ -3200,4 +3200,23 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Global.decodeNbits - result of more than 256 bits") {
def someTest(): Assertion = {
test("some", env, ext,
s"""{
| val target = Global.decodeNbits(${Long.MaxValue}L) // result is 2031 bits long
| target != 0
|}""".stripMargin,
null
)
}

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

}

0 comments on commit aa61236

Please sign in to comment.