Add Ed25519 arithmetic and fix more bugs
This commit is contained in:
1 parent
e785bde585
commit
fafa0aa0c5
10 files changed
+825
-21
No files matched your search
+42
@@ -1329,4 +1329,46 @@ interface JnaLibsodiumInterface : Library {
|
||||
|
||||
//
|
||||
// // ---- Ristretto255 end ----
|
||||
|
||||
//
|
||||
// // ---- Ed25519 ----
|
||||
|
||||
fun crypto_core_ed25519_is_valid_point(p: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_random(p: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_from_hash(p: ByteArray, r: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_from_uniform(p: ByteArray, r: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_add(r: ByteArray, p: ByteArray, q: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_sub(r: ByteArray, p: ByteArray, q: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_scalar_random(r: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_reduce(r: ByteArray, s: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_invert(recip: ByteArray, s: ByteArray): Int
|
||||
|
||||
fun crypto_core_ed25519_scalar_negate(neg: ByteArray, s: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_complement(comp: ByteArray, s: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_add(z: ByteArray, x: ByteArray, y: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_sub(z: ByteArray, x: ByteArray, y: ByteArray)
|
||||
|
||||
fun crypto_core_ed25519_scalar_mul(z: ByteArray, x: ByteArray, y: ByteArray)
|
||||
|
||||
fun crypto_scalarmult_ed25519(q: ByteArray, n: ByteArray, p: ByteArray): Int
|
||||
|
||||
fun crypto_scalarmult_ed25519_noclamp(q: ByteArray, n: ByteArray, p: ByteArray): Int
|
||||
|
||||
fun crypto_scalarmult_ed25519_base(q: ByteArray, n: ByteArray): Int
|
||||
|
||||
fun crypto_scalarmult_ed25519_base_noclamp(q: ByteArray, n: ByteArray): Int
|
||||
|
||||
//
|
||||
// // ---- Ed25519 end ----
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
package com.ionspin.kotlin.crypto.ed25519
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import kotlin.UByteArray
|
||||
|
||||
actual abstract class Ed25519LowLevel actual constructor() {
|
||||
actual fun isValidPoint(encoded: UByteArray): Boolean =
|
||||
sodiumJna.crypto_core_ed25519_is_valid_point(encoded.asByteArray()) == 1
|
||||
|
||||
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_add(result.asByteArray(), p.asByteArray(), q.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_sub(result.asByteArray(), p.asByteArray(), q.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_from_hash(result.asByteArray(), hash.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun encodedPointFromUniform(uniform: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_from_uniform(result.asByteArray(), uniform.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun randomEncodedPoint(): UByteArray = UByteArray(crypto_core_ed25519_BYTES).also {
|
||||
sodiumJna.crypto_core_ed25519_random(it.asByteArray())
|
||||
}
|
||||
|
||||
actual fun randomEncodedScalar(): UByteArray = UByteArray(crypto_core_ed25519_SCALARBYTES).also {
|
||||
sodiumJna.crypto_core_ed25519_scalar_random(it.asByteArray())
|
||||
}
|
||||
|
||||
actual fun invertScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_invert(result.asByteArray(), scalar.asByteArray()).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun negateScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_negate(result.asByteArray(), scalar.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun complementScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_complement(result.asByteArray(), scalar.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_add(result.asByteArray(), x.asByteArray(), y.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_sub(result.asByteArray(), x.asByteArray(), y.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_mul(result.asByteArray(), x.asByteArray(), y.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun reduceScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ed25519_scalar_reduce(result.asByteArray(), scalar.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ed25519(result.asByteArray(), n.asByteArray(), p.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun scalarMultiplicationNoClamp(n: UByteArray, p: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ed25519_noclamp(result.asByteArray(), n.asByteArray(), p.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ed25519_base(result.asByteArray(), n.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun scalarMultiplicationBaseNoClamp(n: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ed25519_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ed25519_base_noclamp(result.asByteArray(), n.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -42,7 +42,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
sodiumJna.crypto_core_ristretto255_scalar_random(it.asByteArray())
|
||||
}
|
||||
|
||||
actual fun invert(scalar: UByteArray): UByteArray {
|
||||
actual fun invertScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ristretto255_scalar_invert(result.asByteArray(), scalar.asByteArray()).ensureLibsodiumSuccess()
|
||||
@@ -50,7 +50,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun negate(scalar: UByteArray): UByteArray {
|
||||
actual fun negateScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ristretto255_scalar_negate(result.asByteArray(), scalar.asByteArray())
|
||||
@@ -58,7 +58,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun complement(scalar: UByteArray): UByteArray {
|
||||
actual fun complementScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ristretto255_scalar_complement(result.asByteArray(), scalar.asByteArray())
|
||||
@@ -90,7 +90,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun reduce(scalar: UByteArray): UByteArray {
|
||||
actual fun reduceScalar(scalar: UByteArray): UByteArray {
|
||||
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)
|
||||
|
||||
sodiumJna.crypto_core_ristretto255_scalar_reduce(result.asByteArray(), scalar.asByteArray())
|
||||
@@ -102,6 +102,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
val result = UByteArray(crypto_core_ristretto255_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ristretto255(result.asByteArray(), n.asByteArray(), p.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -110,6 +111,7 @@ actual abstract class Ristretto255LowLevel actual constructor() {
|
||||
val result = UByteArray(crypto_core_ristretto255_BYTES)
|
||||
|
||||
sodiumJna.crypto_scalarmult_ristretto255_base(result.asByteArray(), n.asByteArray())
|
||||
.ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user