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

Simplifying DifficultySerializer.decodeMPI #2143

Merged
merged 4 commits into from
Apr 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object DifficultySerializer extends ErgoSerializer[NBits] {
if (size >= 1) bytes(4) = ((compact >> 16) & 0xFF).toByte
if (size >= 2) bytes(5) = ((compact >> 8) & 0xFF).toByte
if (size >= 3) bytes(6) = (compact & 0xFF).toByte
decodeMPI(bytes, hasLength = true)
decodeMPI(bytes)
}

/**
Expand Down Expand Up @@ -78,19 +78,13 @@ object DifficultySerializer extends ErgoSerializer[NBits] {
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
* a 4 byte big endian length field, followed by the stated number of bytes representing
* the number in big endian format (with a sign bit).
*
* @param hasLength can be set to false if the given array is missing the 4 byte length field
*/
@SuppressWarnings(Array("NullAssignment"))
private def decodeMPI(mpi: Array[Byte], hasLength: Boolean): BigInteger = {
var buf: Array[Byte] = null // scalastyle:ignore
if (hasLength) {
val length: Int = readUint32BE(mpi).toInt
buf = new Array[Byte](length)
System.arraycopy(mpi, 4, buf, 0, length)
} else {
buf = mpi
}
private def decodeMPI(mpi: Array[Byte]): BigInteger = {

val length: Int = readUint32BE(mpi).toInt
val buf = new Array[Byte](length)
System.arraycopy(mpi, 4, buf, 0, length)

if (buf.length == 0) {
BigInteger.ZERO
} else {
Expand Down
Loading