From 90feb762db40ea90d56f9f44056a430197d4f569 Mon Sep 17 00:00:00 2001 From: kildishevps Date: Thu, 23 Jan 2025 00:35:54 +0300 Subject: [PATCH] Rebase and adapting new functions to wasmJs --- .../kotlin/crypto/JsSodiumInterface.kt | 107 ++++++++++++++++++ .../kotlin/crypto/ed25519/Ed25519LowLevel.kt | 106 +++++++++++++++++ .../ristretto255/Ristretto255LowLevel.kt | 94 +++++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ed25519/Ed25519LowLevel.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ristretto255/Ristretto255LowLevel.kt diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index 8260758..e766fa5 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -366,5 +366,112 @@ external object JsSodiumInterface: JsAny { // ---- Scalar multiplication end ---- + // + // ---- Ristretto255 ---- + @JsName("crypto_core_ristretto255_is_valid_point") + fun crypto_core_ristretto255_is_valid_point(p: Uint8Array): Boolean + + @JsName("crypto_core_ristretto255_random") + fun crypto_core_ristretto255_random(): Uint8Array + + @JsName("crypto_core_ristretto255_from_hash") + fun crypto_core_ristretto255_from_hash(r: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_add") + fun crypto_core_ristretto255_add(p: Uint8Array, q: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_sub") + fun crypto_core_ristretto255_sub(p: Uint8Array, q: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_random") + fun crypto_core_ristretto255_scalar_random(): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_reduce") + fun crypto_core_ristretto255_scalar_reduce(s: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_invert") + fun crypto_core_ristretto255_scalar_invert(s: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_negate") + fun crypto_core_ristretto255_scalar_negate(s: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_complement") + fun crypto_core_ristretto255_scalar_complement(s: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_add") + fun crypto_core_ristretto255_scalar_add(x: Uint8Array, y: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_sub") + fun crypto_core_ristretto255_scalar_sub(x: Uint8Array, y: Uint8Array): Uint8Array + + @JsName("crypto_core_ristretto255_scalar_mul") + fun crypto_core_ristretto255_scalar_mul(x: Uint8Array, y: Uint8Array): Uint8Array + + @JsName("crypto_scalarmult_ristretto255") + fun crypto_scalarmult_ristretto255(n: Uint8Array, p: Uint8Array): Uint8Array + + @JsName("crypto_scalarmult_ristretto255_base") + fun crypto_scalarmult_ristretto255_base(n: Uint8Array): Uint8Array + + // + // ---- 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_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 ---- } diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ed25519/Ed25519LowLevel.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ed25519/Ed25519LowLevel.kt new file mode 100644 index 0000000..4e33907 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ed25519/Ed25519LowLevel.kt @@ -0,0 +1,106 @@ +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 object Ed25519LowLevel { + 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.toUByteArray() + } + + actual fun pointFromUniform(uniform: UByteArray): UByteArray { + val result = getSodium().crypto_core_ed25519_from_uniform(uniform.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun randomPoint(): UByteArray { + val result = getSodium().crypto_core_ed25519_random() + + return result.toUByteArray() + } + + actual fun randomScalar(): 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() + } +} \ No newline at end of file diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ristretto255/Ristretto255LowLevel.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ristretto255/Ristretto255LowLevel.kt new file mode 100644 index 0000000..6e40b1c --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/ristretto255/Ristretto255LowLevel.kt @@ -0,0 +1,94 @@ +package com.ionspin.kotlin.crypto.ristretto255 + +import com.ionspin.kotlin.crypto.getSodium +import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray +import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array + +actual object Ristretto255LowLevel { + actual fun isValidPoint(encoded: UByteArray): Boolean = + getSodium().crypto_core_ristretto255_is_valid_point(encoded.toUInt8Array()) + + actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_add(p.toUInt8Array(), q.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_sub(p.toUInt8Array(), q.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun pointFromHash(hash: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_from_hash(hash.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun randomPoint(): UByteArray { + val result = getSodium().crypto_core_ristretto255_random() + + return result.toUByteArray() + } + + actual fun randomScalar(): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_random() + + return result.toUByteArray() + } + + actual fun invertScalar(scalar: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_invert(scalar.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun negateScalar(scalar: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_negate(scalar.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun complementScalar(scalar: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_complement(scalar.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_add(x.toUInt8Array(), y.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_sub(x.toUInt8Array(), y.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_mul(x.toUInt8Array(), y.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun reduceScalar(scalar: UByteArray): UByteArray { + val result = getSodium().crypto_core_ristretto255_scalar_reduce(scalar.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray { + val result = getSodium().crypto_scalarmult_ristretto255(n.toUInt8Array(), p.toUInt8Array()) + + return result.toUByteArray() + } + + actual fun scalarMultiplicationBase(n: UByteArray): UByteArray { + val result = getSodium().crypto_scalarmult_ristretto255_base(n.toUInt8Array()) + + return result.toUByteArray() + } +} \ No newline at end of file