Skip to content

Commit

Permalink
Add missing imports and use usePinned to guarantee proper unpinning
Browse files Browse the repository at this point in the history
  • Loading branch information
Traderjoe95 committed Aug 12, 2024
1 parent 6a66654 commit e785bde
Showing 1 changed file with 98 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,116 +1,194 @@
package com.ionspin.kotlin.crypto.ristretto255

import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.pin
import kotlinx.cinterop.usePinned
import libsodium.crypto_core_ristretto255_add
import libsodium.crypto_core_ristretto255_from_hash
import libsodium.crypto_core_ristretto255_is_valid_point
import libsodium.crypto_core_ristretto255_random
import libsodium.crypto_core_ristretto255_scalar_add
import libsodium.crypto_core_ristretto255_scalar_complement
import libsodium.crypto_core_ristretto255_scalar_invert
import libsodium.crypto_core_ristretto255_scalar_mul
import libsodium.crypto_core_ristretto255_scalar_negate
import libsodium.crypto_core_ristretto255_scalar_random
import libsodium.crypto_core_ristretto255_scalar_reduce
import libsodium.crypto_core_ristretto255_scalar_sub
import libsodium.crypto_core_ristretto255_sub
import libsodium.crypto_scalarmult_ristretto255
import libsodium.crypto_scalarmult_ristretto255_base


actual abstract class Ristretto255LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean {
return crypto_core_ristretto255_is_valid_point(encoded.pin().toPtr()) == 1
return encoded.usePinned { crypto_core_ristretto255_is_valid_point(it.toPtr()) == 1 }
}

actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)

crypto_core_ristretto255_add(result.pin().toPtr(), p.pin().toPtr(), q.pin().toPtr())
.ensureLibsodiumSuccess()
result.usePinned { resultPinned ->
p.usePinned { pPinned ->
q.usePinned { qPinned ->
crypto_core_ristretto255_add(resultPinned.toPtr(), pPinned.toPtr(), qPinned.toPtr())
.ensureLibsodiumSuccess()
}
}
}

return result
}

actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)

crypto_core_ristretto255_sub(result.pin().toPtr(), p.pin().toPtr(), q.pin().toPtr())
.ensureLibsodiumSuccess()
result.usePinned { resultPinned ->
p.usePinned { pPinned ->
q.usePinned { qPinned ->
crypto_core_ristretto255_sub(resultPinned.toPtr(), pPinned.toPtr(), qPinned.toPtr())
.ensureLibsodiumSuccess()
}
}
}

return result
}

actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)

crypto_core_ristretto255_from_hash(result.pin().toPtr(), hash.pin().toPtr())
result.usePinned { resultPinned ->
hash.usePinned { hashPinned ->
crypto_core_ristretto255_from_hash(resultPinned.toPtr(), hashPinned.toPtr())
}
}

return result
}

actual fun randomEncodedPoint(): UByteArray = UByteArray(crypto_core_ristretto255_BYTES).also {
crypto_core_ristretto255_random(it.pin().toPtr())
actual fun randomEncodedPoint(): UByteArray = UByteArray(crypto_core_ristretto255_BYTES).apply {
usePinned { crypto_core_ristretto255_random(it.toPtr()) }
}

actual fun randomEncodedScalar(): UByteArray = UByteArray(crypto_core_ristretto255_SCALARBYTES).also {
crypto_core_ristretto255_scalar_random(it.pin().toPtr())
actual fun randomEncodedScalar(): UByteArray = UByteArray(crypto_core_ristretto255_SCALARBYTES).apply {
usePinned { crypto_core_ristretto255_scalar_random(it.toPtr()) }
}

actual fun invert(scalar: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_invert(result.pin().toPtr(), scalar.pin().toPtr()).ensureLibsodiumSuccess()
result.usePinned { resultPinned ->
scalar.usePinned { scalarPinned ->
crypto_core_ristretto255_scalar_invert(resultPinned.toPtr(), scalarPinned.toPtr()).ensureLibsodiumSuccess()
}
}


return result
}

actual fun negate(scalar: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_negate(result.pin().toPtr(), scalar.pin().toPtr())
result.usePinned { resultPinned ->
scalar.usePinned { scalarPinned ->
crypto_core_ristretto255_scalar_negate(resultPinned.toPtr(), scalarPinned.toPtr())
}
}


return result
}

actual fun complement(scalar: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_complement(result.pin().toPtr(), scalar.pin().toPtr())
result.usePinned { resultPinned ->
scalar.usePinned { scalarPinned ->
crypto_core_ristretto255_scalar_complement(resultPinned.toPtr(), scalarPinned.toPtr())
}
}

return result
}

actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_add(result.pin().toPtr(), x.pin().toPtr(), y.pin().toPtr())
result.usePinned { resultPinned ->
x.usePinned { xPinned ->
y.usePinned { yPinned ->
crypto_core_ristretto255_scalar_add(resultPinned.toPtr(), xPinned.toPtr(), yPinned.toPtr())
}
}
}

return result
}

actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_sub(result.pin().toPtr(), x.pin().toPtr(), y.pin().toPtr())
result.usePinned { resultPinned ->
x.usePinned { xPinned ->
y.usePinned { yPinned ->
crypto_core_ristretto255_scalar_sub(resultPinned.toPtr(), xPinned.toPtr(), yPinned.toPtr())
}
}
}

return result
}

actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_mul(result.pin().toPtr(), x.pin().toPtr(), y.pin().toPtr())
result.usePinned { resultPinned ->
x.usePinned { xPinned ->
y.usePinned { yPinned ->
crypto_core_ristretto255_scalar_mul(resultPinned.toPtr(), xPinned.toPtr(), yPinned.toPtr())
}
}
}

return result
}

actual fun reduce(scalar: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_SCALARBYTES)

crypto_core_ristretto255_scalar_reduce(result.pin().toPtr(), scalar.pin().toPtr())
result.usePinned { resultPinned ->
scalar.usePinned { scalarPinned ->
crypto_core_ristretto255_scalar_reduce(resultPinned.toPtr(), scalarPinned.toPtr())
}
}

return result
}

actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)

crypto_scalarmult_ristretto255(result.pin().toPtr(), n.pin().toPtr(), p.pin().toPtr())
result.usePinned { resultPinned ->
n.usePinned { nPinned ->
p.usePinned { pPinned ->
crypto_scalarmult_ristretto255(resultPinned.toPtr(), nPinned.toPtr(), pPinned.toPtr())
}
}
}


return result
}

actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)

crypto_scalarmult_ristretto255_base(result.pin().toPtr(), n.pin().toPtr())
result.usePinned { resultPinned ->
n.usePinned { nPinned ->
crypto_scalarmult_ristretto255_base(resultPinned.toPtr(), nPinned.toPtr())
}
}

return result
}
Expand Down

0 comments on commit e785bde

Please sign in to comment.