Skip to content

Commit

Permalink
Revert "0.0.4"
Browse files Browse the repository at this point in the history
This reverts commit a9d5fc9
  • Loading branch information
andreypfau committed Oct 22, 2022
1 parent a77c56a commit 9dcf3ad
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ dependencies {
<dependency>
<groupId>io.github.andreypfau</groupId>
<artifactId>curve25519-kotlin-jvm</artifactId>
<version>0.0.4</version>
<version>0.0.3</version>
</dependency>
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,24 @@ import io.github.andreypfau.curve25519.edwards.EdwardsPoint
import io.github.andreypfau.curve25519.internal.sha512
import io.github.andreypfau.curve25519.scalar.Scalar

class Ed25519PrivateKey constructor(
val data: ByteArray,
val offset: Int,
class Ed25519PrivateKey internal constructor(
internal val data: ByteArray
) {
constructor(rawData: ByteArray) : this(rawData.copyOf(SIZE_BYTES), 0)

init {
require(data.size - offset >= SIZE_BYTES) { "ed25519: bad length: ${data.size}" }
}

fun toByteArray(): ByteArray = toByteArray(ByteArray(Ed25519.PRIVATE_KEY_SIZE_BYTES))
fun toByteArray(destination: ByteArray, destinationOffset: Int = 0): ByteArray =
data.copyInto(destination, destinationOffset, offset, offset + SIZE_BYTES)
fun toByteArray(output: ByteArray, offset: Int = 0): ByteArray =
data.copyInto(output, offset)

fun seed(): ByteArray = seed(ByteArray(Ed25519.SEED_SIZE_BYTES))
fun seed(destination: ByteArray, destinationOffset: Int = 0): ByteArray {
data.copyInto(destination, destinationOffset, offset, offset + Ed25519.SEED_SIZE_BYTES)
return destination
fun seed(output: ByteArray, offset: Int = 0): ByteArray {
data.copyInto(output, offset, 0, Ed25519.SEED_SIZE_BYTES)
return output
}

fun publicKey(): Ed25519PublicKey =
Ed25519PublicKey(data, offset + Ed25519.SEED_SIZE_BYTES)
Ed25519PublicKey(data.copyOfRange(32, 64))

fun sign(message: ByteArray): ByteArray = sign(message, ByteArray(Ed25519.SIGNATURE_SIZE_BYTES))
fun sign(message: ByteArray, destination: ByteArray, destinationOffset: Int = 0): ByteArray {
fun sign(message: ByteArray, output: ByteArray, offset: Int = 0): ByteArray {
val extsk = sha512(data, 0, 32)
extsk[0] = (extsk[0].toInt() and 248).toByte()
extsk[31] = (extsk[31].toInt() and 127).toByte()
Expand All @@ -55,21 +48,17 @@ class Ed25519PrivateKey constructor(
s.add(s, r)

// S = (r + H(R,A,m)a) mod L
rCompressed.data.copyInto(destination, destinationOffset)
s.toByteArray(destination, destinationOffset + 32)
rCompressed.data.copyInto(output, offset)
s.toByteArray(output, offset + 32)

return destination
return output
}

fun sharedKey(
publicKey: Ed25519PublicKey
): ByteArray = sharedKey(publicKey, ByteArray(32))

fun sharedKey(
publicKey: Ed25519PublicKey,
destination: ByteArray,
destinationOffset: Int = 0
): ByteArray = Ed25519.sharedKey(this, publicKey, destination, destinationOffset)
output: ByteArray = ByteArray(32),
offset: Int = 0
): ByteArray = Ed25519.sharedKey(this, publicKey, output, offset)

companion object {
const val SIZE_BYTES = Ed25519.PRIVATE_KEY_SIZE_BYTES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@ import io.github.andreypfau.curve25519.internal.sha512
import io.github.andreypfau.curve25519.internal.varTimeDoubleScalarBaseMul
import io.github.andreypfau.curve25519.scalar.Scalar

class Ed25519PublicKey constructor(
val data: ByteArray,
val offset: Int
class Ed25519PublicKey internal constructor(
internal val data: ByteArray
) {
constructor(rawData: ByteArray) : this(rawData.copyOf(SIZE_BYTES), 0)

init {
require(data.size - offset >= SIZE_BYTES) { "ed25519: bad length: ${data.size}" }
}

fun verify(
message: ByteArray,
signature: ByteArray
): Boolean {
val aCompressed = CompressedEdwardsY(data, offset)
val aCompressed = CompressedEdwardsY(data)
val a = EdwardsPoint.from(aCompressed)

// hram = H(R,A,m)
val hash = sha512(signature.copyOfRange(0, 32) + data.copyOfRange(offset, offset + SIZE_BYTES) + message)
val hash = sha512(signature.copyOfRange(0, 32) + data + message)
val k = Scalar.fromWideByteArray(hash)
val s = Scalar.fromByteArray(signature, 32)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class Ed25519VerifyOptions(
// Unpack A.
val aCompressed = CompressedEdwardsY()
try {
aCompressed.set(publicKey.data, publicKey.offset)
aCompressed.set(publicKey.data)
a.set(aCompressed)
} catch (e: Exception) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import kotlin.experimental.xor
import kotlin.jvm.JvmStatic

class CompressedEdwardsY constructor(
val data: ByteArray,
val offset: Int
val data: ByteArray
) {
constructor() : this(ByteArray(SIZE_BYTES), 0)
constructor(data: ByteArray) : this(data.copyOf(SIZE_BYTES), 0)
constructor() : this(ByteArray(SIZE_BYTES))

fun set(src: ByteArray, srcOffset: Int = 0) {
src.copyInto(data, offset, srcOffset, srcOffset + SIZE_BYTES)
fun set(byteArray: ByteArray, offset: Int = 0) {
byteArray.copyInto(data, 0, offset, offset + SIZE_BYTES)
}

fun set(point: EdwardsPoint): CompressedEdwardsY = from(point, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.github.andreypfau.curve25519.internal.constantTimeEquals
import io.github.andreypfau.curve25519.internal.sha512
import io.github.andreypfau.curve25519.montgomery.MontgomeryPoint
import io.github.andreypfau.curve25519.scalar.Scalar
import kotlin.jvm.JvmStatic

object X25519 {
const val SCALAR_SIZE_BYTES = 32
Expand All @@ -19,7 +18,6 @@ object X25519 {
it[0] = 9
}

@JvmStatic
fun x25519(
scalar: ByteArray,
point: ByteArray = BASEPOINT,
Expand All @@ -35,20 +33,18 @@ object X25519 {
return output
}

@JvmStatic
fun toX25519(
publicKey: Ed25519PublicKey,
output: ByteArray = ByteArray(POINT_SIZE_BYTES),
offset: Int = 0
): ByteArray {
val aCompressed = CompressedEdwardsY(publicKey.data, publicKey.offset)
val aCompressed = CompressedEdwardsY(publicKey.data)
val a = EdwardsPoint.from(aCompressed)
val montA = MontgomeryPoint.from(a)
montA.data.copyInto(output, offset)
return output
}

@JvmStatic
fun toX25519(
privateKey: Ed25519PrivateKey,
output: ByteArray = ByteArray(SCALAR_SIZE_BYTES),
Expand Down

0 comments on commit 9dcf3ad

Please sign in to comment.