Skip to content

Commit

Permalink
Merge pull request #142 from avianlabs/guillermo/allow-for-half-secre…
Browse files Browse the repository at this point in the history
…t-keys

allow for half secret keys
  • Loading branch information
wiyarmir authored Oct 15, 2024
2 parents f5e976d + 76355f4 commit f5fd389
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 20 deletions.
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version=0.2.1
# Kotlin
#kotlin.code.style=official
kotlin.js.compiler=ir
kotlin.incremental.multiplatform=true
kotlin.mpp.stability.nowarn=true
android.useAndroidX=true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.avianlabs.solana.tweetnacl.ed25519

import net.avianlabs.solana.tweetnacl.TweetNaCl
import net.avianlabs.solana.tweetnacl.TweetNaCl.Signature.Companion.PUBLIC_KEY_BYTES
import net.avianlabs.solana.tweetnacl.TweetNaCl.Signature.Companion.SECRET_KEY_BYTES
import net.avianlabs.solana.tweetnacl.vendor.decodeBase58

public data class Ed25519Keypair(
Expand Down Expand Up @@ -31,10 +29,16 @@ public data class Ed25519Keypair(
TweetNaCl.Signature.sign(message = message, secretKey = secretKey)

public companion object {
public fun fromSecretKeyBytes(bytes: ByteArray): Ed25519Keypair {
require(bytes.size == SECRET_KEY_BYTES) { "Invalid key length: ${bytes.size}" }
val publicKey = PublicKey(bytes.sliceArray(PUBLIC_KEY_BYTES until SECRET_KEY_BYTES))
return Ed25519Keypair(publicKey, bytes.copyOf())
public fun fromSecretKeyBytes(bytes: ByteArray): Ed25519Keypair = when (bytes.size) {
// [secretKey(32)]
32 -> TweetNaCl.Signature.generateKey(bytes + ByteArray(32))
// [secretKey(32)|publicKey(32)]
64 -> {
val publicKey = PublicKey(bytes.sliceArray(32 until 64))
Ed25519Keypair(publicKey, bytes.copyOf())
}

else -> error("Invalid key length: ${bytes.size}")
}

public fun fromBase58(base58: String): Ed25519Keypair =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.avianlabs.solana.tweetnacl

import net.avianlabs.solana.tweetnacl.ed25519.Ed25519Keypair
import net.avianlabs.solana.tweetnacl.vendor.decodeBase58
import kotlin.test.Test
import kotlin.test.assertEquals

class Ed25519Test {
private val secretKey =
"ftqmzZS6Va5xuyCdks47WZd1D8FpXZaPGqL3JE39814pReiEuTAvJpr8PxkUxm9wHKHTsfN8TGk44hhoEdQDYrD".decodeBase58()

@Test
fun test_chopping_and_restoring() {
val chopped = secretKey.take(32).toByteArray()

val restored = Ed25519Keypair.fromSecretKeyBytes(chopped).secretKey

@OptIn(ExperimentalStdlibApi::class)
assertEquals(
secretKey.toHexString(),
restored.toHexString(),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
extern int is_on_curve(const unsigned char *);
extern int is_on_curve(const unsigned char *);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@file:OptIn(ExperimentalForeignApi::class)

package net.avianlabs.solana.tweetnacl

import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.get

internal fun CPointer<UByteVar>.toByteArray(length: Int): ByteArray {
val nativeBytes = this
val bytes = ByteArray(length)
var index = 0
while (index < length) {
bytes[index] = nativeBytes[index].toByte()
++index
}
return bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,3 @@ internal actual fun secretBoxInternal(secretKey: ByteArray): TweetNaCl.SecretBox
}.drop(crypto_secretbox_xsalsa20poly1305_tweet_ZEROBYTES).toByteArray()
}

private fun CPointer<UByteVar>.toByteArray(length: Int): ByteArray {
val nativeBytes = this
val bytes = ByteArray(length)
var index = 0
while (index < length) {
bytes[index] = nativeBytes[index].toByte()
++index
}
return bytes
}
6 changes: 4 additions & 2 deletions tweetnacl-multiplatform/vendor/tweetnacl/tweetnacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,11 @@ int crypto_sign_open(u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)

// curve additions

int is_on_curve(const u8 p[32]) {
int is_on_curve(const u8 p[32])
{
gf q[4];
return unpackneg(q,p);

return unpackneg(q, p);
}

// END curve additions

0 comments on commit f5fd389

Please sign in to comment.