From d917cf0033e416a603dc5e2eaaf348941c59396c Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 6 Feb 2024 12:57:40 +0100 Subject: [PATCH] Return an array containing 0 when big integer is zero, to be more consistent. Fixes #280 --- .../bignum/integer/base63/array/BigInteger63Arithmetic.kt | 3 +++ .../bignum/integer/base63/ByteArrayConversionTest.kt | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/base63/array/BigInteger63Arithmetic.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/base63/array/BigInteger63Arithmetic.kt index 18c94a7e..397710f0 100644 --- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/base63/array/BigInteger63Arithmetic.kt +++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/base63/array/BigInteger63Arithmetic.kt @@ -2228,6 +2228,9 @@ internal object BigInteger63Arithmetic : BigIntegerArithmetic { override fun toUByteArray( operand: ULongArray ): UByteArray { + if (operand == ZERO) { + return UByteArray(1) { 0U } + } val as64Bit = convertTo64BitRepresentation(operand).reversedArray() val result = UByteArray(as64Bit.size * 8) for (i in 0 until as64Bit.size) { diff --git a/bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/ByteArrayConversionTest.kt b/bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/ByteArrayConversionTest.kt index e84c2cc5..e9d3a7ba 100644 --- a/bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/ByteArrayConversionTest.kt +++ b/bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/ByteArrayConversionTest.kt @@ -826,4 +826,11 @@ class ByteArrayConversionTest { val kotlinBigBytes = IonSpinBigInteger.parseString("21000").toByteArray() Assert.assertArrayEquals(javaBigBytes, kotlinBigBytes) } + + @Test + fun zeroToByteArray() { + val javaBigBytes = BigInteger("0").toByteArray() + val kotlinBigBytes = IonSpinBigInteger.parseString("0").toByteArray() + Assert.assertArrayEquals(javaBigBytes, kotlinBigBytes) + } }