style: apply spotless formatting to ChaCha20/Poly1305 sources

https://claude.ai/code/session_01YHBwqnb3Z7Q7PX31tJ2G3i
This commit is contained in:
Claude
2026-03-23 17:57:31 +00:00
parent d586a0dc25
commit 2e1bcf3e52
4 changed files with 149 additions and 87 deletions
@@ -48,10 +48,14 @@ object ChaCha20Core {
c: Int, c: Int,
d: Int, d: Int,
) { ) {
state[a] += state[b]; state[d] = (state[d] xor state[a]).rotateLeft(16) state[a] += state[b]
state[c] += state[d]; state[b] = (state[b] xor state[c]).rotateLeft(12) state[d] = (state[d] xor state[a]).rotateLeft(16)
state[a] += state[b]; state[d] = (state[d] xor state[a]).rotateLeft(8) state[c] += state[d]
state[c] += state[d]; state[b] = (state[b] xor state[c]).rotateLeft(7) state[b] = (state[b] xor state[c]).rotateLeft(12)
state[a] += state[b]
state[d] = (state[d] xor state[a]).rotateLeft(8)
state[c] += state[d]
state[b] = (state[b] xor state[c]).rotateLeft(7)
} }
/** /**
@@ -84,12 +84,24 @@ object Poly1305 {
// Partial reduction mod 2^130 - 5 // Partial reduction mod 2^130 - 5
var c: Long var c: Long
c = d0 ushr 26; h0 = d0 and 0x3ffffff; h1 = d1 + c c = d0 ushr 26
c = h1 ushr 26; h1 = h1 and 0x3ffffff; h2 = d2 + c h0 = d0 and 0x3ffffff
c = h2 ushr 26; h2 = h2 and 0x3ffffff; h3 = d3 + c h1 = d1 + c
c = h3 ushr 26; h3 = h3 and 0x3ffffff; h4 = d4 + c c = h1 ushr 26
c = h4 ushr 26; h4 = h4 and 0x3ffffff; h0 += c * 5 h1 = h1 and 0x3ffffff
c = h0 ushr 26; h0 = h0 and 0x3ffffff; h1 += c h2 = d2 + c
c = h2 ushr 26
h2 = h2 and 0x3ffffff
h3 = d3 + c
c = h3 ushr 26
h3 = h3 and 0x3ffffff
h4 = d4 + c
c = h4 ushr 26
h4 = h4 and 0x3ffffff
h0 += c * 5
c = h0 ushr 26
h0 = h0 and 0x3ffffff
h1 += c
} }
// Process remaining bytes (if any) // Process remaining bytes (if any)
@@ -111,27 +123,57 @@ object Poly1305 {
val d4 = h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0 val d4 = h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0
var c: Long var c: Long
c = d0 ushr 26; h0 = d0 and 0x3ffffff; h1 = d1 + c c = d0 ushr 26
c = h1 ushr 26; h1 = h1 and 0x3ffffff; h2 = d2 + c h0 = d0 and 0x3ffffff
c = h2 ushr 26; h2 = h2 and 0x3ffffff; h3 = d3 + c h1 = d1 + c
c = h3 ushr 26; h3 = h3 and 0x3ffffff; h4 = d4 + c c = h1 ushr 26
c = h4 ushr 26; h4 = h4 and 0x3ffffff; h0 += c * 5 h1 = h1 and 0x3ffffff
c = h0 ushr 26; h0 = h0 and 0x3ffffff; h1 += c h2 = d2 + c
c = h2 ushr 26
h2 = h2 and 0x3ffffff
h3 = d3 + c
c = h3 ushr 26
h3 = h3 and 0x3ffffff
h4 = d4 + c
c = h4 ushr 26
h4 = h4 and 0x3ffffff
h0 += c * 5
c = h0 ushr 26
h0 = h0 and 0x3ffffff
h1 += c
} }
// Final reduction: fully carry and reduce mod 2^130 - 5 // Final reduction: fully carry and reduce mod 2^130 - 5
var c: Long var c: Long
c = h1 ushr 26; h1 = h1 and 0x3ffffff; h2 += c c = h1 ushr 26
c = h2 ushr 26; h2 = h2 and 0x3ffffff; h3 += c h1 = h1 and 0x3ffffff
c = h3 ushr 26; h3 = h3 and 0x3ffffff; h4 += c h2 += c
c = h4 ushr 26; h4 = h4 and 0x3ffffff; h0 += c * 5 c = h2 ushr 26
c = h0 ushr 26; h0 = h0 and 0x3ffffff; h1 += c h2 = h2 and 0x3ffffff
h3 += c
c = h3 ushr 26
h3 = h3 and 0x3ffffff
h4 += c
c = h4 ushr 26
h4 = h4 and 0x3ffffff
h0 += c * 5
c = h0 ushr 26
h0 = h0 and 0x3ffffff
h1 += c
// Compute h + -(2^130 - 5) = h - 2^130 + 5 // Compute h + -(2^130 - 5) = h - 2^130 + 5
var g0 = h0 + 5; c = g0 ushr 26; g0 = g0 and 0x3ffffff var g0 = h0 + 5
var g1 = h1 + c; c = g1 ushr 26; g1 = g1 and 0x3ffffff c = g0 ushr 26
var g2 = h2 + c; c = g2 ushr 26; g2 = g2 and 0x3ffffff g0 = g0 and 0x3ffffff
var g3 = h3 + c; c = g3 ushr 26; g3 = g3 and 0x3ffffff var g1 = h1 + c
c = g1 ushr 26
g1 = g1 and 0x3ffffff
var g2 = h2 + c
c = g2 ushr 26
g2 = g2 and 0x3ffffff
var g3 = h3 + c
c = g3 ushr 26
g3 = g3 and 0x3ffffff
var g4 = h4 + c - (1L shl 26) var g4 = h4 + c - (1L shl 26)
// Select h if g4 is negative (bit 63 set), else select g // Select h if g4 is negative (bit 63 set), else select g
@@ -160,9 +202,12 @@ object Poly1305 {
val s2Long = key.leToUInt(24) val s2Long = key.leToUInt(24)
val s3Long = key.leToUInt(28) val s3Long = key.leToUInt(28)
f0 += s0; c = f0 ushr 32 f0 += s0
f1 += s1Long + c; c = f1 ushr 32 c = f0 ushr 32
f2 += s2Long + c; c = f2 ushr 32 f1 += s1Long + c
c = f1 ushr 32
f2 += s2Long + c
c = f2 ushr 32
f3 += s3Long + c f3 += s3Long + c
// Output as little-endian bytes // Output as little-endian bytes
@@ -31,7 +31,11 @@ import kotlin.test.assertFailsWith
*/ */
class ChaCha20CoreTest { class ChaCha20CoreTest {
private fun hex(s: String): ByteArray = private fun hex(s: String): ByteArray =
s.replace(" ", "").chunked(2).map { it.toInt(16).toByte() }.toByteArray() s
.replace(" ", "")
.chunked(2)
.map { it.toInt(16).toByte() }
.toByteArray()
private fun ByteArray.toHex(): String = joinToString("") { "%02x".format(it) } private fun ByteArray.toHex(): String = joinToString("") { "%02x".format(it) }
@@ -25,13 +25,17 @@ import kotlin.test.assertContentEquals
/** Test vectors from libsodium xchacha20.c */ /** Test vectors from libsodium xchacha20.c */
class XChaCha20Test { class XChaCha20Test {
private fun hex(s: String): ByteArray = private fun hex(s: String): ByteArray = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
// HChaCha20 test vectors from libsodium // HChaCha20 test vectors from libsodium
data class HChaCha20TV(val key: String, val input: String, val expected: String) data class HChaCha20TV(
val key: String,
val input: String,
val expected: String,
)
private val hChaCha20Vectors = listOf( private val hChaCha20Vectors =
listOf(
HChaCha20TV( HChaCha20TV(
"24f11cce8a1b3d61e441561a696c1c1b7e173d084fd4812425435a8896a013dc", "24f11cce8a1b3d61e441561a696c1c1b7e173d084fd4812425435a8896a013dc",
"d9660c5900ae19ddad28d6e06e45fe5e", "d9660c5900ae19ddad28d6e06e45fe5e",
@@ -68,9 +72,14 @@ class XChaCha20Test {
} }
// XChaCha20 stream test vectors from libsodium // XChaCha20 stream test vectors from libsodium
data class XChaCha20TV(val key: String, val nonce: String, val expected: String) data class XChaCha20TV(
val key: String,
val nonce: String,
val expected: String,
)
private val xChaCha20Vectors = listOf( private val xChaCha20Vectors =
listOf(
XChaCha20TV( XChaCha20TV(
"79c99798ac67300bbb2704c95c341e3245f3dcb21761b98e52ff45b24f304fc4", "79c99798ac67300bbb2704c95c341e3245f3dcb21761b98e52ff45b24f304fc4",
"b33ffd3096479bcfbc9aee49417688a0a2554f8d95389419", "b33ffd3096479bcfbc9aee49417688a0a2554f8d95389419",