Add Ed25519 arithmetic and fix more bugs

This commit is contained in:
Johannes Leupold committed 2024-08-13 10:23:52 +02:00
1 parent e785bde585
commit fafa0aa0c5
10 files changed
+825 -21

No files matched your search

@@ -413,4 +413,65 @@ external object JsSodiumInterface {
//
// ---- Ristretto255 end ----
//
// ---- Ed25519 ----
@JsName("crypto_core_ed25519_is_valid_point")
fun crypto_core_ed25519_is_valid_point(p: Uint8Array): Boolean
@JsName("crypto_core_ed25519_random")
fun crypto_core_ed25519_random(): Uint8Array
@JsName("crypto_core_ed25519_from_hash")
fun crypto_core_ed25519_from_hash(r: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_from_uniform")
fun crypto_core_ed25519_from_uniform(r: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_add")
fun crypto_core_ed25519_add(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_sub")
fun crypto_core_ed25519_sub(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_random")
fun crypto_core_ed25519_scalar_random(): Uint8Array
@JsName("crypto_core_ed25519_scalar_reduce")
fun crypto_core_ed25519_scalar_reduce(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_invert")
fun crypto_core_ed25519_scalar_invert(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_negate")
fun crypto_core_ed25519_scalar_negate(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_complement")
fun crypto_core_ed25519_scalar_complement(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_add")
fun crypto_core_ed25519_scalar_add(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_sub")
fun crypto_core_ed25519_scalar_sub(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_mul")
fun crypto_core_ed25519_scalar_mul(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519")
fun crypto_scalarmult_ed25519(n: Uint8Array, p: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_noclamp")
fun crypto_scalarmult_ed25519_noclamp(n: Uint8Array, p: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_base")
fun crypto_scalarmult_ed25519_base(n: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_base_noclamp")
fun crypto_scalarmult_ed25519_base_noclamp(n: Uint8Array): Uint8Array
//
// ---- Ed25519 end ----
}
@@ -0,0 +1,112 @@
package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual abstract class Ed25519LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
getSodium().crypto_core_ed25519_is_valid_point(encoded.toUint8Array())
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_add(p.toUint8Array(), q.toUint8Array())
return result.toUByteArray()
}
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_sub(p.toUint8Array(), q.toUint8Array())
return result
}
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_from_hash(hash.toUint8Array())
return result.toUByteArray()
}
actual fun encodedPointFromUniform(uniform: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_from_uniform(hash.toUint8Array())
return result.toUByteArray()
}
actual fun randomEncodedPoint(): UByteArray {
val result = getSodium().crypto_core_ed25519_random()
return result.toUByteArray()
}
actual fun randomEncodedScalar(): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_random()
return result.toUByteArray()
}
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_invert(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_negate(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_complement(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_add(x.toUint8Array(), y.toUint8Array())
return result.toUByteArray()
}
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_sub(x.toUint8Array(), y.toUint8Array())
return result.toUByteArray()
}
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_mul(x.toUint8Array(), y.toUint8Array())
return result.toUByteArray()
}
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_reduce(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519(n.toUint8Array(), p.toUint8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationNoClamp(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_noclamp(n.toUint8Array(), p.toUint8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base(n.toUint8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBaseNoClamp(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base_noclamp(n.toUint8Array())
return result.toUByteArray()
}
}
@@ -38,19 +38,19 @@ actual abstract class Ristretto255LowLevel actual constructor() {
return result.toUByteArray()
}
actual fun invert(scalar: UByteArray): UByteArray {
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_invert(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun negate(scalar: UByteArray): UByteArray {
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_negate(scalar.toUint8Array())
return result.toUByteArray()
}
actual fun complement(scalar: UByteArray): UByteArray {
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_complement(scalar.toUint8Array())
return result.toUByteArray()
@@ -74,7 +74,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
return result.toUByteArray()
}
actual fun reduce(scalar: UByteArray): UByteArray {
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_reduce(scalar.toUint8Array())
return result.toUByteArray()