From f671d617261161b6ea9843897a5c3302e619554f Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Fri, 3 Jan 2025 07:07:12 -0500 Subject: [PATCH] Do not process most significant bits if 0 (#89) --- gradle.properties | 4 +- .../kotlin/org/kotlincrypto/core/xof/Xof.kt | 37 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index 9754aad..b611902 100644 --- a/gradle.properties +++ b/gradle.properties @@ -30,10 +30,10 @@ POM_DEVELOPER_ID=KotlinCrypto POM_DEVELOPER_NAME=Kotlin Crypto POM_DEVELOPER_URL=https://github.com/KotlinCrypto/ -VERSION_NAME=1.0.0-SNAPSHOT +VERSION_NAME=1.0.0-alpha01-SNAPSHOT # 0.1.0-alpha01 = 00 01 00 11 # 0.1.0-beta01 = 00 01 00 21 # 0.1.0-rc01 = 00 01 00 31 # 0.1.0 = 00 01 00 99 # 1.1.0 = 01 01 00 99 -VERSION_CODE=01000099 +VERSION_CODE=01000011 diff --git a/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt b/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt index 70226e5..a552425 100644 --- a/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt +++ b/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt @@ -238,22 +238,31 @@ public sealed class Xof: Algorithm, Copyable>, Resettabl @JvmStatic private fun encode(lo: Int, hi: Int, left: Boolean): ByteArray { - if (lo == 0 && hi == 0) { - // If it's zero, return early - return if (left) byteArrayOf(1, 0) else byteArrayOf(0, 1) + val a = if (hi == 0) { + if (lo == 0) { + // If it's zero, return early + return if (left) byteArrayOf(1, 0) else byteArrayOf(0, 1) + } + + byteArrayOf( + (lo ushr 24).toByte(), + (lo ushr 16).toByte(), + (lo ushr 8).toByte(), + (lo ).toByte(), + ) + } else { + byteArrayOf( + (hi ushr 24).toByte(), + (hi ushr 16).toByte(), + (hi ushr 8).toByte(), + (hi ).toByte(), + (lo ushr 24).toByte(), + (lo ushr 16).toByte(), + (lo ushr 8).toByte(), + (lo ).toByte(), + ) } - val a = byteArrayOf( - (hi ushr 24).toByte(), - (hi ushr 16).toByte(), - (hi ushr 8).toByte(), - (hi ).toByte(), - (lo ushr 24).toByte(), - (lo ushr 16).toByte(), - (lo ushr 8).toByte(), - (lo ).toByte(), - ) - // Find index of first non-zero byte var i = 0 while (i < a.size && a[i] == ZERO) {