Skip to content

Commit

Permalink
Migrate Context API to inline functions
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Dec 18, 2024
1 parent 020145b commit cf8e807
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 206 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fun main() {

state.addData(index = 0, 23452345254L)

KeccakP(state)
state.keccakP()

state.forEach {
println(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package org.kotlincrypto.sponges.benchmarks

import kotlinx.benchmark.*
import org.kotlincrypto.sponges.keccak.F1600
import org.kotlincrypto.sponges.keccak.KeccakP
import org.kotlincrypto.sponges.keccak.keccakP

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
Expand All @@ -34,5 +34,5 @@ open class KeccakPBenchmark {
}

@Benchmark
fun keccakP() { KeccakP(state) }
fun keccakP() { state.keccakP() }
}
21 changes: 21 additions & 0 deletions library/keccak/api/keccak.api
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
public final class org/kotlincrypto/sponges/keccak/F1600 : org/kotlincrypto/sponges/keccak/State {
public fun <init> ()V
public synthetic fun RC$org_kotlincrypto_sponges_keccak_jvm (I)Ljava/lang/Number;
public fun copy ()Lorg/kotlincrypto/sponges/keccak/F1600;
public synthetic fun copy ()Lorg/kotlincrypto/sponges/keccak/State;
public synthetic fun mixIn (Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
}

public final class org/kotlincrypto/sponges/keccak/F200 : org/kotlincrypto/sponges/keccak/State {
public fun <init> ()V
public synthetic fun RC$org_kotlincrypto_sponges_keccak_jvm (I)Ljava/lang/Number;
public fun copy ()Lorg/kotlincrypto/sponges/keccak/F200;
public synthetic fun copy ()Lorg/kotlincrypto/sponges/keccak/State;
public synthetic fun mixIn (Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
}

public final class org/kotlincrypto/sponges/keccak/F400 : org/kotlincrypto/sponges/keccak/State {
public fun <init> ()V
public synthetic fun RC$org_kotlincrypto_sponges_keccak_jvm (I)Ljava/lang/Number;
public fun copy ()Lorg/kotlincrypto/sponges/keccak/F400;
public synthetic fun copy ()Lorg/kotlincrypto/sponges/keccak/State;
public synthetic fun mixIn (Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
}

public final class org/kotlincrypto/sponges/keccak/F800 : org/kotlincrypto/sponges/keccak/State {
public fun <init> ()V
public synthetic fun RC$org_kotlincrypto_sponges_keccak_jvm (I)Ljava/lang/Number;
public fun copy ()Lorg/kotlincrypto/sponges/keccak/F800;
public synthetic fun copy ()Lorg/kotlincrypto/sponges/keccak/State;
public synthetic fun mixIn (Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
}

public final class org/kotlincrypto/sponges/keccak/KeccakPKt {
public static final fun KeccakP (Lorg/kotlincrypto/sponges/keccak/State;)V
public static final fun KeccakP (Lorg/kotlincrypto/sponges/keccak/State;B)V
public static synthetic fun KeccakP$default (Lorg/kotlincrypto/sponges/keccak/State;BILjava/lang/Object;)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F1600;)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F1600;B)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F200;)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F200;B)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F400;)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F400;B)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F800;)V
public static final fun keccakP (Lorg/kotlincrypto/sponges/keccak/F800;B)V
public static synthetic fun keccakP$default (Lorg/kotlincrypto/sponges/keccak/F1600;BILjava/lang/Object;)V
public static synthetic fun keccakP$default (Lorg/kotlincrypto/sponges/keccak/F200;BILjava/lang/Object;)V
public static synthetic fun keccakP$default (Lorg/kotlincrypto/sponges/keccak/F400;BILjava/lang/Object;)V
public static synthetic fun keccakP$default (Lorg/kotlincrypto/sponges/keccak/F800;BILjava/lang/Object;)V
}

public abstract class org/kotlincrypto/sponges/keccak/State : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
Expand All @@ -46,6 +66,7 @@ public abstract class org/kotlincrypto/sponges/keccak/State : java/util/Collecti
public final fun getSize ()I
public final fun isEmpty ()Z
public final fun iterator ()Ljava/util/Iterator;
protected abstract fun mixIn (Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
public fun remove (Ljava/lang/Object;)Z
public fun removeAll (Ljava/util/Collection;)Z
public fun removeIf (Ljava/util/function/Predicate;)Z
Expand Down
5 changes: 5 additions & 0 deletions library/keccak/api/keccak.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sealed class <#A: kotlin/Number, #B: org.kotlincrypto.sponges.keccak/State<#A, #
final val state // org.kotlincrypto.sponges.keccak/State.state|{}state[0]
final fun <get-state>(): kotlin/Array<#A> // org.kotlincrypto.sponges.keccak/State.state.<get-state>|<get-state>(){}[0]

abstract fun (#A).mixIn(#A): #A // org.kotlincrypto.sponges.keccak/State.mixIn|mixIn@1:0(1:0){}[0]
abstract fun copy(): #B // org.kotlincrypto.sponges.keccak/State.copy|copy(){}[0]
final fun addData(kotlin/Int, #A) // org.kotlincrypto.sponges.keccak/State.addData|addData(kotlin.Int;1:0){}[0]
final fun contains(#A): kotlin/Boolean // org.kotlincrypto.sponges.keccak/State.contains|contains(1:0){}[0]
Expand All @@ -52,4 +53,8 @@ sealed class <#A: kotlin/Number, #B: org.kotlincrypto.sponges.keccak/State<#A, #
final object Companion // org.kotlincrypto.sponges.keccak/State.Companion|null[0]
}

final fun (org.kotlincrypto.sponges.keccak/F1600).org.kotlincrypto.sponges.keccak/keccakP(kotlin/Byte =...) // org.kotlincrypto.sponges.keccak/keccakP|[email protected](kotlin.Byte){}[0]
final fun (org.kotlincrypto.sponges.keccak/F200).org.kotlincrypto.sponges.keccak/keccakP(kotlin/Byte =...) // org.kotlincrypto.sponges.keccak/keccakP|[email protected](kotlin.Byte){}[0]
final fun (org.kotlincrypto.sponges.keccak/F400).org.kotlincrypto.sponges.keccak/keccakP(kotlin/Byte =...) // org.kotlincrypto.sponges.keccak/keccakP|[email protected](kotlin.Byte){}[0]
final fun (org.kotlincrypto.sponges.keccak/F800).org.kotlincrypto.sponges.keccak/keccakP(kotlin/Byte =...) // org.kotlincrypto.sponges.keccak/keccakP|[email protected](kotlin.Byte){}[0]
final fun <#A: kotlin/Number> org.kotlincrypto.sponges.keccak/KeccakP(org.kotlincrypto.sponges.keccak/State<#A, *>, kotlin/Byte =...) // org.kotlincrypto.sponges.keccak/KeccakP|KeccakP(org.kotlincrypto.sponges.keccak.State<0:0,*>;kotlin.Byte){0§<kotlin.Number>}[0]
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,7 @@ public class F1600: State<Long, F1600> {
public constructor(): super(roundCount = 24, state = Array(P_LEN) { 0 })
private constructor(state: F1600): super(state.roundCount, state.state.copyOf())
public override fun copy(): F1600 = F1600(this)

protected override fun Long.mixIn(data: Long): Long = this xor data
@JvmSynthetic
internal override fun <T: Any?> withContext(block: Context<Long>.() -> T): T = block(F1600Context)

private object F1600Context: Context<Long> {
override fun and(a: Long, other: Long): Long = a and other
override fun inv(a: Long): Long = a.inv()
override fun xor(a: Long, other: Long): Long = a xor other
override fun rotateLeft(a: Long, n: Int): Long {
val bitCount = n % Long.SIZE_BITS
return (a shl bitCount) or (a ushr (Long.SIZE_BITS - bitCount))
}
override fun RC(index: Int): Long = RC[index]
}
internal override fun RC(index: Int): Long = RC[index]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
**/
package org.kotlincrypto.sponges.keccak

import kotlin.experimental.and
import kotlin.experimental.inv
import kotlin.experimental.xor
import kotlin.jvm.JvmSynthetic

Expand All @@ -27,22 +25,7 @@ public class F200: State<Byte, F200> {
public constructor(): super(roundCount = 18, state = Array(P_LEN) { 0 })
private constructor(state: F200): super(state.roundCount, state.state.copyOf())
public override fun copy(): F200 = F200(this)

protected override fun Byte.mixIn(data: Byte): Byte = this xor data
@JvmSynthetic
internal override fun <T: Any?> withContext(block: Context<Byte>.() -> T): T = block(F200Context)

private object F200Context: Context<Byte> {
private const val MASK: Int = 0xff

override fun and(a: Byte, other: Byte): Byte = a and other
override fun inv(a: Byte): Byte = a.inv()
override fun xor(a: Byte, other: Byte): Byte = a xor other
override fun rotateLeft(a: Byte, n: Int): Byte {
val bitCount = n % Byte.SIZE_BITS
return (
((a.toInt() and MASK) shl bitCount) or ((a.toInt() and MASK) ushr (Byte.SIZE_BITS - bitCount))
).toByte()
}
override fun RC(index: Int): Byte = RC[index].toByte()
}
internal override fun RC(index: Int): Byte = RC[index].toByte()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
**/
package org.kotlincrypto.sponges.keccak

import kotlin.experimental.and
import kotlin.experimental.inv
import kotlin.experimental.xor
import kotlin.jvm.JvmSynthetic

Expand All @@ -27,22 +25,7 @@ public class F400: State<Short, F400> {
public constructor(): super(roundCount = 20, state = Array(P_LEN) { 0 })
private constructor(state: F400): super(state.roundCount, state.state.copyOf())
public override fun copy(): F400 = F400(this)

protected override fun Short.mixIn(data: Short): Short = this xor data
@JvmSynthetic
internal override fun <T: Any?> withContext(block: Context<Short>.() -> T): T = block(F400Context)

private object F400Context: Context<Short> {
private const val MASK: Int = 0xffff

override fun and(a: Short, other: Short): Short = a and other
override fun inv(a: Short): Short = a.inv()
override fun xor(a: Short, other: Short): Short = a xor other
override fun rotateLeft(a: Short, n: Int): Short {
val bitCount = n % Short.SIZE_BITS
return (
((a.toInt() and MASK) shl bitCount) or ((a.toInt() and MASK) ushr (Short.SIZE_BITS - bitCount))
).toShort()
}
override fun RC(index: Int): Short = RC[index].toShort()
}
internal override fun RC(index: Int): Short = RC[index].toShort()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,7 @@ public class F800: State<Int, F800> {
public constructor(): super(roundCount = 22, state = Array(P_LEN) { 0 })
private constructor(state: F800): super(state.roundCount, state.state.copyOf())
public override fun copy(): F800 = F800(this)

protected override fun Int.mixIn(data: Int): Int = this xor data
@JvmSynthetic
internal override fun <T: Any?> withContext(block: Context<Int>.() -> T): T = block(F800Context)

private object F800Context: Context<Int> {
override fun and(a: Int, other: Int): Int = a and other
override fun inv(a: Int): Int = a.inv()
override fun xor(a: Int, other: Int): Int = a xor other
override fun rotateLeft(a: Int, n: Int): Int {
val bitCount = n % Int.SIZE_BITS
return (a shl bitCount) or (a ushr (Int.SIZE_BITS - bitCount))
}
override fun RC(index: Int): Int = RC[index].toInt()
}
internal override fun RC(index: Int): Int = RC[index].toInt()
}
Loading

0 comments on commit cf8e807

Please sign in to comment.