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 method can produce bigint out of 256 bits scope #1045

Open
wants to merge 1 commit into
base: v6.0.0
Choose a base branch
from
Open
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 @@ -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()
}
}

}
Loading