Skip to content

Commit

Permalink
Deprecate the KyberAgreement class to streamline encapsulation and de…
Browse files Browse the repository at this point in the history
…capsulation
  • Loading branch information
ronhombre committed Aug 30, 2024
1 parent 61167b7 commit 8187ba1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/asia/hombre/kyber/KyberAgreement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlin.jvm.JvmSynthetic
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
@Deprecated("This class is redundant. It will be removed in 1.0.0 release.")
class KyberAgreement(private val decapsulationKey: KyberDecapsulationKey) {
/**
* The [KyberParameter] of this agreement.
Expand Down Expand Up @@ -267,6 +268,10 @@ class KyberAgreement(private val decapsulationKey: KyberDecapsulationKey) {
*/
@JvmStatic
@JsName("encapsulate")
@Deprecated("KyberAgreement is a redundant class.",
ReplaceWith("kyberEncapsulationKey.encapsulate()",
"asia.hombre.kyber.KyberEncapsulationKey.encapsulate"),
DeprecationLevel.WARNING)
fun encapsulate(kyberEncapsulationKey: KyberEncapsulationKey): KyberEncapsulationResult {
return encapsulate(kyberEncapsulationKey, SecureRandom().nextBytesOf(KyberConstants.N_BYTES))
}
Expand All @@ -281,6 +286,11 @@ class KyberAgreement(private val decapsulationKey: KyberDecapsulationKey) {
* @return [ByteArray] - The generated Secret Key, which is the same one generated by the sender.
*/
@JsName("decapsulate")
@Deprecated("KyberAgreement is a redundant class.",
ReplaceWith("kyberCipherText.decapsulate(kyberDecapsulationKey)",
"asia.hombre.kyber.KyberCipherText.decapsulate",
"asia.hombre.kyber.KyberDecapsulationKey"),
DeprecationLevel.WARNING)
fun decapsulate(kyberCipherText: KyberCipherText): ByteArray {
return Companion.decapsulate(secureDecapsulationKey, kyberCipherText)
}
Expand Down
12 changes: 12 additions & 0 deletions src/commonMain/kotlin/asia/hombre/kyber/KyberCipherText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ class KyberCipherText internal constructor( //TODO: Copy parameter variables
return KyberCipherText(this.parameter, encodedCoefficients.copyOf(), encodedTerms.copyOf())
}

/**
* Decapsulates this [KyberCipherText] and recovers the Secret Key.
*
* This method is the ML-KEM.Decaps() specified in NIST FIPS 203.
*
* @param decapsulationKey your [KyberDecapsulationKey].
* @return [ByteArray] - The generated Secret Key, which is the same one generated by the sender.
*/
fun decapsulate(decapsulationKey: KyberDecapsulationKey): ByteArray {
return KyberAgreement.decapsulate(decapsulationKey, this)
}

/**
* Convert [KyberCipherText] into a String.
*
Expand Down
12 changes: 12 additions & 0 deletions src/commonMain/kotlin/asia/hombre/kyber/KyberEncapsulationKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package asia.hombre.kyber

import asia.hombre.kyber.exceptions.UnsupportedKyberVariantException
import asia.hombre.kyber.interfaces.KyberKEMKey
import org.kotlincrypto.SecureRandom
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
import kotlin.js.ExperimentalJsExport
Expand Down Expand Up @@ -137,6 +138,17 @@ class KyberEncapsulationKey internal constructor(
return KyberEncapsulationKey(key.copy())
}

/**
* Encapsulates this [KyberEncapsulationKey] into a [KyberCipherText] and generates a Secret Key.
*
* This method is the ML-KEM.Encaps() specified in NIST FIPS 203.
*
* @return [KyberEncapsulationResult] - Contains the Cipher Text and the generated Secret Key.
*/
fun encapsulate(): KyberEncapsulationResult {
return KyberAgreement.encapsulate(this, SecureRandom().nextBytesOf(KyberConstants.N_BYTES))
}

/**
* Convert [KyberEncapsulationKey] into a String.
*
Expand Down

0 comments on commit 8187ba1

Please sign in to comment.