-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
630 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
library/core/src/commonMain/kotlin/org/kotlincrypto/core/Counter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/* | ||
* Copyright (c) 2025 Matthew Nelson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
package org.kotlincrypto.core | ||
|
||
import org.kotlincrypto.core.Counter.Bit32.Companion.MAX_INCREMENT | ||
import org.kotlincrypto.core.Counter.Bit64.Companion.MAX_INCREMENT | ||
import kotlin.jvm.JvmField | ||
import kotlin.jvm.JvmName | ||
|
||
/** | ||
* Utility for counting things. | ||
* */ | ||
public sealed class Counter private constructor() { | ||
|
||
/** | ||
* A counter that utilizes 32-bit numbers providing a maximum count of 2^64. | ||
* */ | ||
public abstract class Bit32: Counter { | ||
|
||
public companion object { | ||
|
||
/** | ||
* The maximum value for which [Bit32.incrementBy] can be set to. | ||
* | ||
* 1024 * 1024 >> 1048576 | ||
* */ | ||
public const val MAX_INCREMENT: Int = 1024 * 1024 // Never decrease, only increase. | ||
} | ||
|
||
/** | ||
* The value to increment things by | ||
* */ | ||
@JvmField | ||
public val incrementBy: Int | ||
|
||
/** | ||
* The least significant bits of the number | ||
* */ | ||
@get:JvmName("lo") | ||
public var lo: Int | ||
private set | ||
|
||
/** | ||
* The most significant bits of the number | ||
* */ | ||
@get:JvmName("hi") | ||
public var hi: Int | ||
private set | ||
|
||
/** | ||
* Creates a new [Bit32] counter initialized to [lo] and [hi] | ||
* | ||
* @throws [IllegalArgumentException] when: | ||
* - [incrementBy] is less than or equal to 0 | ||
* - [incrementBy] is greater than [MAX_INCREMENT] | ||
* - [incrementBy] is not a factor of 8 | ||
* - [lo] is not a factor of [incrementBy] | ||
* */ | ||
public constructor(lo: Int, hi: Int, incrementBy: Int): super() { | ||
require(incrementBy > 0) { "incrementBy[$incrementBy] must be greater than 0" } | ||
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" } | ||
require(incrementBy % 8 == 0) { "incrementBy[$incrementBy] must be a factor of 8" } | ||
require(lo % incrementBy == 0) { "lo must be a factor of incrementBy[$incrementBy]" } | ||
|
||
this.incrementBy = incrementBy | ||
this.lo = lo | ||
this.hi = hi | ||
} | ||
|
||
/** | ||
* Creates a new [Bit32] counter initialized to 0, 0 | ||
* | ||
* @throws [IllegalArgumentException] when [incrementBy] is: | ||
* - Less than or equal to 0 | ||
* - Greater than [MAX_INCREMENT] | ||
* - Not a factor of 8 | ||
* */ | ||
public constructor(incrementBy: Int): this(0, 0, incrementBy) | ||
|
||
/** | ||
* Creates a clone of [other] | ||
* */ | ||
public constructor(other: Bit32): super() { | ||
this.incrementBy = other.incrementBy | ||
this.lo = other.lo | ||
this.hi = other.hi | ||
} | ||
|
||
protected override fun increment() { | ||
lo += incrementBy | ||
if (lo == 0) hi++ | ||
} | ||
|
||
protected override fun reset() { | ||
lo = 0 | ||
hi = 0 | ||
} | ||
} | ||
|
||
/** | ||
* A counter that utilizes 64-bit numbers providing a maximum count of 2^128. | ||
* */ | ||
public abstract class Bit64: Counter { | ||
|
||
public companion object { | ||
|
||
/** | ||
* The maximum value for which [Bit64.incrementBy] can be set to. | ||
* | ||
* @see [Bit32.MAX_INCREMENT] | ||
* */ | ||
public const val MAX_INCREMENT: Long = Bit32.MAX_INCREMENT.toLong() | ||
} | ||
|
||
/** | ||
* The value to increment things by | ||
* */ | ||
@JvmField | ||
public val incrementBy: Long | ||
|
||
/** | ||
* The least significant bits of the number | ||
* */ | ||
@get:JvmName("lo") | ||
public var lo: Long | ||
private set | ||
|
||
/** | ||
* The most significant bits of the number | ||
* */ | ||
@get:JvmName("hi") | ||
public var hi: Long | ||
private set | ||
|
||
/** | ||
* Creates a new [Bit64] counter initialized to [lo] and [hi] | ||
* | ||
* @throws [IllegalArgumentException] when: | ||
* - [incrementBy] is less than or equal to 0 | ||
* - [incrementBy] is greater than [MAX_INCREMENT] | ||
* - [incrementBy] is not a factor of 8 | ||
* - [lo] is not a factor of [incrementBy] | ||
* */ | ||
public constructor(lo: Long, hi: Long, incrementBy: Long): super() { | ||
require(incrementBy > 0L) { "incrementBy[$incrementBy] must be greater than 0" } | ||
require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" } | ||
require(incrementBy % 8 == 0L) { "incrementBy[$incrementBy] must be a factor of 8" } | ||
require(lo % incrementBy == 0L) { "lo must be a factor of incrementBy[$incrementBy]" } | ||
|
||
this.incrementBy = incrementBy | ||
this.lo = lo | ||
this.hi = hi | ||
} | ||
|
||
/** | ||
* Creates a new [Bit64] counter initialized to 0, 0 | ||
* | ||
* @throws [IllegalArgumentException] when [incrementBy] is: | ||
* - Less than or equal to 0 | ||
* - Greater than [MAX_INCREMENT] | ||
* - Not a factor of 8 | ||
* */ | ||
public constructor(incrementBy: Long): this(0, 0, incrementBy) | ||
|
||
/** | ||
* Creates a clone of [other] | ||
* */ | ||
public constructor(other: Bit64): super() { | ||
this.incrementBy = other.incrementBy | ||
this.lo = other.lo | ||
this.hi = other.hi | ||
} | ||
|
||
protected override fun increment() { | ||
lo += incrementBy | ||
if (lo == 0L) hi++ | ||
} | ||
|
||
protected override fun reset() { | ||
lo = 0L | ||
hi = 0L | ||
} | ||
} | ||
|
||
protected abstract fun increment() | ||
protected abstract fun reset() | ||
|
||
private val code = Any() | ||
|
||
/** @suppress */ | ||
public final override fun equals(other: Any?): Boolean = other is Counter && other.hashCode() == hashCode() | ||
/** @suppress */ | ||
public final override fun hashCode(): Int = 17 * 31 + code.hashCode() | ||
/** @suppress */ | ||
public final override fun toString(): String = when (this) { | ||
is Bit32 -> "Bit32[lo=$lo, hi=$hi, incrementBy=$incrementBy]" | ||
is Bit64 -> "Bit64[lo=$lo, hi=$hi, incrementBy=$incrementBy]" | ||
}.let { value -> "Counter.$value@${hashCode()}" } | ||
} |
Oops, something went wrong.