-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ED25519): ED25519 jsMain implementation (#76)
- Loading branch information
Showing
20 changed files
with
240 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...yption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/Ed25519KeyPairGeneration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import kotlin.js.ExperimentalJsExport | ||
import kotlin.js.JsExport | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
interface Ed25519KeyPairGeneration { | ||
fun generateEd25519KeyPair(): KMMEdKeyPair | ||
fun generateKeyPair(): KMMEdKeyPair | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...tric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
public expect class KMMEdPrivateKey | ||
public expect class KMMEdPrivateKey { | ||
fun sign(message: ByteArray): ByteArray | ||
} |
4 changes: 3 additions & 1 deletion
4
...etric-encryption/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPublicKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
public expect class KMMEdPublicKey | ||
public expect class KMMEdPublicKey { | ||
fun verify(message: ByteArray, sig: ByteArray): Boolean | ||
} |
51 changes: 51 additions & 0 deletions
51
...yption/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPairTestsIgnored.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import kotlin.test.Ignore | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
// un-ignore when JVM implementation is done and remove jsTest version of these tests | ||
@Ignore | ||
class KMMEdKeyPairTestsIgnored { | ||
@Test | ||
fun testGenerateKeyPair() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
|
||
assertNotNull(keyPair) | ||
assertNotNull(keyPair.privateKey) | ||
assertNotNull(keyPair.publicKey) | ||
} | ||
|
||
@Test | ||
fun testSignMessage() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val message = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(message) | ||
|
||
assertNotNull(sig) | ||
} | ||
|
||
@Test | ||
fun testVerifyMessage() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val msgHash = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(msgHash) | ||
val verified = keyPair.verify(msgHash, sig) | ||
|
||
assertTrue(verified) | ||
} | ||
|
||
@Test | ||
fun testVerifyWithAnotherKeyPairFails() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val msgHash = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(msgHash) | ||
|
||
val wrongKeyPair = KMMEdKeyPair.generateKeyPair() | ||
val verified = wrongKeyPair.verify(msgHash, sig) | ||
|
||
assertFalse(verified) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 20 additions & 6 deletions
26
...-asymmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPair.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import io.iohk.atala.prism.apollo.utils.external.rand | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual class KMMEdKeyPair actual constructor( | ||
actual val privateKey: KMMEdPrivateKey, | ||
actual val publicKey: KMMEdPublicKey | ||
) { | ||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
actual fun sign(message: ByteArray): ByteArray { | ||
return privateKey.sign(message) | ||
} | ||
|
||
actual fun verify(message: ByteArray, sig: ByteArray): Boolean { | ||
return publicKey.verify(message, sig) | ||
} | ||
|
||
actual companion object : Ed25519KeyPairGeneration { | ||
override fun generateEd25519KeyPair(): KMMEdKeyPair { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
override fun generateKeyPair(): KMMEdKeyPair { | ||
val ed25519 = eddsa("ed25519") | ||
val rnd = rand(32) | ||
val secret = Buffer.from(rnd).toByteArray() | ||
val keypair = ed25519.keyFromSecret(secret) | ||
val public = keypair.getPublic() | ||
|
||
return KMMEdKeyPair(KMMEdPrivateKey(secret), KMMEdPublicKey(public)) | ||
} | ||
} | ||
} |
20 changes: 17 additions & 3 deletions
20
...ymmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
actual class KMMEdPrivateKey { | ||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual class KMMEdPrivateKey(val raw: ByteArray) { | ||
private val keyPair: eddsa.KeyPair | ||
|
||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
val ed25519 = eddsa("ed25519") | ||
|
||
keyPair = ed25519.keyFromSecret(raw) | ||
} | ||
|
||
actual fun sign(message: ByteArray): ByteArray { | ||
val sig = keyPair.sign(Buffer.from(message)) | ||
|
||
return sig.toHex().encodeToByteArray() | ||
} | ||
} |
18 changes: 15 additions & 3 deletions
18
...symmetric-encryption/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPublicKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
actual class KMMEdPublicKey { | ||
import io.iohk.atala.prism.apollo.utils.external.eddsa | ||
import node.buffer.Buffer | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual class KMMEdPublicKey(val raw: ByteArray) { | ||
private val keyPair: eddsa.KeyPair | ||
|
||
init { | ||
// TODO: we will use this lib for JS https://github.com/indutny/elliptic | ||
throw NotImplementedError("Ed25519 is yet to be implemented in JS") | ||
val ed25519 = eddsa("ed25519") | ||
|
||
keyPair = ed25519.keyFromPublic(raw) | ||
} | ||
|
||
actual fun verify(message: ByteArray, sig: ByteArray): Boolean { | ||
return keyPair.verify(Buffer.from(message), sig.decodeToString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...metric-encryption/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdKeyPairTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.iohk.atala.prism.apollo.utils | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
class KMMEdKeyPairTests { | ||
@Test | ||
fun testGenerateKeyPair() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
|
||
assertNotNull(keyPair) | ||
assertNotNull(keyPair.privateKey) | ||
assertNotNull(keyPair.publicKey) | ||
} | ||
|
||
@Test | ||
fun testSignMessage() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val message = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(message) | ||
|
||
assertNotNull(sig) | ||
} | ||
|
||
@Test | ||
fun testVerifyMessage() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val msgHash = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(msgHash) | ||
val verified = keyPair.verify(msgHash, sig) | ||
|
||
assertTrue(verified) | ||
} | ||
|
||
@Test | ||
fun testVerifyWithAnotherKeyPairFails() { | ||
val keyPair = KMMEdKeyPair.generateKeyPair() | ||
val msgHash = "testing".encodeToByteArray() | ||
val sig = keyPair.sign(msgHash) | ||
|
||
val wrongKeyPair = KMMEdKeyPair.generateKeyPair() | ||
val verified = wrongKeyPair.verify(msgHash, sig) | ||
|
||
assertFalse(verified) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.