Skip to content

Commit

Permalink
fix: linting issues + testing
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Ribó <[email protected]>
  • Loading branch information
elribonazo committed Sep 30, 2024
1 parent 93a10dc commit 8c8fd6c
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.hyperledger.identus.apollo.ed25519

import org.hyperledger.identus.apollo.base64.base64DecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlDecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import org.hyperledger.identus.apollo.utils.KMMEdKeyPair
import org.hyperledger.identus.apollo.utils.KMMEdPrivateKey
import org.hyperledger.identus.apollo.utils.KMMEdPublicKey
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class KMMEdKeyPairTest {
private val rawSK = "M7+EvGp9vjLyx5SQ1xiKhRUN+YQ0kjx6d21WgX8P1hE".base64DecodedBytes
private val rawSKEncoded = rawSK.base64UrlEncoded

@Test
fun ed_signature_verify() {
val message = "testing".encodeToByteArray()
val edsk = KMMEdPublicKey("z+a8g8i/3yajE/QDIxIF7sP1m/aLKTOfygIuSJanRAg".base64DecodedBytes)
val sig = "zZzeTBYjrpowc+skEMVQSVTzntzXvZGcbBbNY0gHKF6z+e40Q5G3o35lX4Mf0J8iRde/LAn77JdzIh6gQv7hAA".base64DecodedBytes
val verify = edsk.verify(message, sig)
assertTrue(verify)
}

@Test
fun testGenerateKeyPair() {
val keyPair = KMMEdKeyPair.generateKeyPair()
assertNotNull(keyPair)
assertNotNull(keyPair.privateKey)
assertNotNull(keyPair.publicKey)
}

@Test
fun testConstructorEncoded() {
val key = KMMEdPrivateKey(rawSKEncoded.base64UrlDecodedBytes)
assertTrue(key.raw contentEquals rawSK)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package org.hyperledger.identus.apollo.secp256k1

import fr.acinq.secp256k1.Secp256k1Native
import org.hyperledger.identus.apollo.utils.KMMEdKeyPair
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class Secp256k1Tests {

@Test
fun verifyValidPrivateKeyEd() {

val keypair = KMMEdKeyPair.generateKeyPair()
keypair.privateKey.raw
}



@Test
fun verifyValidPrivateKey() {
val secp256k1 = Secp256k1Native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ actual class KMMEdPublicKey(bytes: ByteArray) {
* @return Boolean
*/
actual fun verify(message: ByteArray, sig: ByteArray): Boolean {

return keyPair.verify(
Buffer.from(message),
sig.toHexString()


)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,92 @@

package org.hyperledger.identus.apollo.utils

import node.buffer.Buffer
import org.hyperledger.identus.apollo.base64.base64DecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlDecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class KMMEdKeyPairTests {
private val raw = arrayOf(234, 155, 38, 115, 124, 211, 171, 185, 149, 186, 77, 255, 240, 94, 209, 65, 63, 214, 168, 213, 146, 68, 68, 196, 167, 211, 183, 80, 14, 166, 239, 217)
private val rawBytes = Buffer.from(raw).toByteArray()
private val encoded = arrayOf(54, 112, 115, 109, 99, 51, 122, 84, 113, 55, 109, 86, 117, 107, 51, 95, 56, 70, 55, 82, 81, 84, 95, 87, 113, 78, 87, 83, 82, 69, 84, 69, 112, 57, 79, 51, 85, 65, 54, 109, 55, 57, 107)
private val encodedBytes = Buffer.from(encoded).toByteArray()
private val publicRaw = arrayOf(207, 230, 188, 131, 200, 191, 223, 38, 163, 19, 244, 3, 35, 18, 5, 238, 195, 245, 155, 246, 139, 41, 51, 159, 202, 2, 46, 72, 150, 167, 68, 8)
private val publicRawBytes = Buffer.from(publicRaw).toByteArray()
private val publicEncoded = arrayOf(122, 45, 97, 56, 103, 56, 105, 95, 51, 121, 97, 106, 69, 95, 81, 68, 73, 120, 73, 70, 55, 115, 80, 49, 109, 95, 97, 76, 75, 84, 79, 102, 121, 103, 73, 117, 83, 74, 97, 110, 82, 65, 103)
private val publicEncodedBytes = Buffer.from(publicEncoded).toByteArray()
val rawMessage = "Hello".encodeToByteArray()
val rawSK = "M7+EvGp9vjLyx5SQ1xiKhRUN+YQ0kjx6d21WgX8P1hE".base64DecodedBytes
val rawSKEncoded = rawSK.base64UrlEncoded
val rawPk = "6kXLIS4a3UAzHm/5XTcvjCTGoAQ+yqPTT0YsM76EeuQ".base64DecodedBytes
val rawPKEncoded = rawPk.base64UrlEncoded
val rawSig = "rQHC0fmOclPBFiPVJCK_WJB0NgTAxSqpggPwRotNdncKrhgM2eECtr3j7UBTBmDdPTmpXGwQvyhTRTG8cymeBA".base64UrlDecodedBytes

@Test
fun testGenerateKeyPair() {
val keyPair = KMMEdKeyPair.generateKeyPair()

assertNotNull(keyPair)
assertNotNull(keyPair.privateKey)
assertNotNull(keyPair.publicKey)
}

@Test
fun testConstructorRaw() {
val key = KMMEdPrivateKey(rawBytes)

assertTrue(key.raw.toByteArray() contentEquals rawBytes)
assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes)
val key = KMMEdPrivateKey(rawSK)
val rawKeyBytes = key.raw.toByteArray()
val rawKeyEncodedBytes = key.getEncoded().toByteArray().decodeToString()
assertTrue(rawKeyBytes contentEquals rawSK)
assertTrue(rawKeyEncodedBytes contentEquals rawSKEncoded)
}

@Test
fun testConstructorEncoded() {
val key = KMMEdPrivateKey(encodedBytes)

assertTrue(key.raw.toByteArray() contentEquals rawBytes)
assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes)
val key = KMMEdPrivateKey(rawSKEncoded.base64UrlDecodedBytes)
assertTrue(key.raw.toByteArray() contentEquals rawSK)
assertTrue(key.getEncoded().toByteArray().decodeToString() contentEquals rawSKEncoded)
}

@Test
fun testGetEncoded() {
val key = KMMEdPrivateKey(rawBytes)

assertTrue(key.getEncoded().toByteArray() contentEquals encodedBytes)
val key = KMMEdPrivateKey(rawSK)
assertTrue(key.getEncoded().toByteArray().decodeToString() contentEquals rawSKEncoded)
}


@Test
fun testPublicKey() {
val privateKey = KMMEdPrivateKey(rawBytes)
val privateKey = KMMEdPrivateKey(rawSK)
val publicKey = privateKey.publicKey()

assertTrue(publicKey.raw.toByteArray() contentEquals publicRawBytes)
assertTrue(publicKey.getEncoded().toByteArray() contentEquals publicEncodedBytes)
assertTrue(publicKey.raw.toByteArray() contentEquals rawPk)
assertTrue(publicKey.getEncoded().toByteArray().decodeToString() contentEquals rawPKEncoded)
}

@Test
fun testSignMessage() {
val keyPair = KMMEdKeyPair.generateKeyPair()
val message = "testing".encodeToByteArray()
val sig = keyPair.sign(message)

val sig = keyPair.sign(rawMessage)
assertNotNull(sig)
}


@Test
fun testSignMessageKnownValue() {
val privateKey = KMMEdPrivateKey(rawBytes)
val message = "testing".encodeToByteArray()
val sig = privateKey.sign(message)
val sigStr = Buffer.from(sig).toString()
val expectedBytes = byteArrayOf(67, 68, 57, 67, 68, 69, 52, 67, 49, 54, 50, 51, 65, 69, 57, 65, 51, 48, 55, 51, 69, 66, 50, 52, 49, 48, 67, 53, 53, 48, 52, 57, 53, 52, 70, 51, 57, 69, 68, 67, 68, 55, 66, 68, 57, 49, 57, 67, 54, 67, 49, 54, 67, 68, 54, 51, 52, 56, 48, 55, 50, 56, 53, 69, 66, 51, 70, 57, 69, 69, 51, 52, 52, 51, 57, 49, 66, 55, 65, 51, 55, 69, 54, 53, 53, 70, 56, 51, 49, 70, 68, 48, 57, 70, 50, 50, 52, 53, 68, 55, 66, 70, 50, 67, 48, 57, 70, 66, 69, 67, 57, 55, 55, 51, 50, 50, 49, 69, 65, 48, 52, 50, 70, 69, 69, 49, 48, 48)
val expectedStr = Buffer.from(expectedBytes).toString()

val privateKey = KMMEdPrivateKey(rawSK)
val sig = privateKey.sign(rawMessage)
assertNotNull(sig)
assertTrue(expectedBytes contentEquals sig)
assertTrue(expectedStr contentEquals sigStr)
assertTrue(sig contentEquals rawSig)
}

@Test
fun testVerifyMessage() {
val keyPair = KMMEdKeyPair.generateKeyPair()
val msgHash = "testing".encodeToByteArray()
val sig = keyPair.sign(msgHash)
val verified = keyPair.verify(msgHash, sig)

val sig = keyPair.sign(rawMessage)
val verified = keyPair.verify(rawMessage, sig)
assertTrue(verified)
}


@Test
fun testVerifyWithAnotherKeyPairFails() {
val keyPair = KMMEdKeyPair.generateKeyPair()
val msgHash = "testing".encodeToByteArray()
val sig = keyPair.sign(msgHash)

val sig = keyPair.sign(rawMessage)
val wrongKeyPair = KMMEdKeyPair.generateKeyPair()
val verified = wrongKeyPair.verify(msgHash, sig)

val verified = wrongKeyPair.verify(rawMessage, sig)
assertFalse(verified)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.hyperledger.identus.apollo.ed25519

import org.hyperledger.identus.apollo.base64.base64DecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlDecodedBytes
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import org.hyperledger.identus.apollo.utils.KMMEdKeyPair
import org.hyperledger.identus.apollo.utils.KMMEdPrivateKey
import org.hyperledger.identus.apollo.utils.KMMEdPublicKey
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class KMMEdKeyPairTest {
private val rawMessage = "Hello".encodeToByteArray()
private val rawSK = "M7+EvGp9vjLyx5SQ1xiKhRUN+YQ0kjx6d21WgX8P1hE".base64DecodedBytes
private val rawSKEncoded = rawSK.base64UrlEncoded
private val rawPk = "6kXLIS4a3UAzHm/5XTcvjCTGoAQ+yqPTT0YsM76EeuQ".base64DecodedBytes
private val rawPKEncoded = rawPk.base64UrlEncoded
private val rawSig = "rQHC0fmOclPBFiPVJCK_WJB0NgTAxSqpggPwRotNdncKrhgM2eECtr3j7UBTBmDdPTmpXGwQvyhTRTG8cymeBA".base64UrlDecodedBytes

@Test
fun ed_signature_verify() {
val message = "testing".encodeToByteArray()
val edsk = KMMEdPublicKey("z+a8g8i/3yajE/QDIxIF7sP1m/aLKTOfygIuSJanRAg".base64DecodedBytes)
val sig = "zZzeTBYjrpowc+skEMVQSVTzntzXvZGcbBbNY0gHKF6z+e40Q5G3o35lX4Mf0J8iRde/LAn77JdzIh6gQv7hAA".base64DecodedBytes
val verify = edsk.verify(message, sig)
assertTrue(verify)
}

@Test
fun testGenerateKeyPair() {
val keyPair = KMMEdKeyPair.generateKeyPair()
assertNotNull(keyPair)
assertNotNull(keyPair.privateKey)
assertNotNull(keyPair.publicKey)
}

@Test
fun testConstructorEncoded() {
val key = KMMEdPrivateKey(rawSKEncoded.base64UrlDecodedBytes)
assertTrue(key.raw contentEquals rawSK)
}

@Test
fun testSignMessageKnownValue() {
val privateKey = KMMEdPrivateKey(rawSK)
val sig = privateKey.sign(rawMessage)
assertNotNull(sig)
assertTrue(sig contentEquals rawSig)
}
}

0 comments on commit 8c8fd6c

Please sign in to comment.