Skip to content

Commit

Permalink
Fix big integer creation from byte array, there was an unneccessary d…
Browse files Browse the repository at this point in the history
…owncast to int. Fixes #153
  • Loading branch information
ionspin committed Feb 9, 2021
1 parent 2c0dbc2 commit c7e0c99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ internal object BigInteger63Arithmetic : BigIntegerArithmetic {
ulongArray[ulongArray.size - 1] or
(trimmedSource[ulongsCount * 8 + i].toULong() shl ((ulongRest - 1) * 8 - i * 8))
}
val result = convertFrom64BitRepresentation(ulongArray.dropLastWhile { it.toUInt() == 0U }.toULongArray())
val result = convertFrom64BitRepresentation(ulongArray.dropLastWhile { it == 0UL }.toULongArray())
return result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

package com.ionspin.kotlin.bignum.integer.base63

import com.ionspin.kotlin.bignum.integer.Sign
import com.ionspin.kotlin.bignum.integer.util.fromTwosComplementByteArray
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
import com.ionspin.kotlin.bignum.integer.util.toTwosComplementByteArray
import java.math.BigInteger
import kotlin.random.Random
import kotlin.test.assertTrue
import org.junit.Assert
import org.junit.Test

/**
Expand Down Expand Up @@ -807,4 +809,14 @@ class ByteArrayConversionTest {
.joinToString(separator = "U, 0x", prefix = "0x", postfix = "U") { it.toString(16) }
})") { bigIntArray.contentEquals(javaBigIntArray) }
}

@Test
fun reportedIssueTest() {
val bytes = byteArrayOf(-128, 0, 0, 0, 0, 0, 0, 0)
val javaBig = BigInteger(1, bytes)
val kotlinBig = IonSpinBigInteger.fromByteArray(bytes, Sign.POSITIVE)
val javaBackToByteArray = javaBig.toByteArray().dropWhile { it == 0.toByte() }.toByteArray() // Java adds a leading zero byte
val bignumBackToByteArray = kotlinBig.toByteArray()
Assert.assertArrayEquals(javaBackToByteArray, bignumBackToByteArray)
}
}

0 comments on commit c7e0c99

Please sign in to comment.