Skip to content

Commit

Permalink
ATL-6926: Correct typo and add tests
Browse files Browse the repository at this point in the history
This commit adds the final tests to validate the cryptography
implementation that replaces the SDK
  • Loading branch information
EzequielPostan committed Apr 23, 2024
1 parent 72598aa commit 800757f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ object CryptoUtils {

trait SecpPrivateKey {
private[crypto] def bytes: Array[Byte]
private[crypto] def privateLey: PrivateKey
private[crypto] def privateKey: PrivateKey
def getEncoded: Array[Byte]
}
private[crypto] case class SecpPrivateKeyImpl(bytes: Array[Byte]) extends SecpPrivateKey {
override def getEncoded: Array[Byte] = {
privateLey
privateKey
.asInstanceOf[ECPrivateKey]
.getD
.toByteArray
.dropWhile(_ == 0)
}

override def privateLey: PrivateKey = {
override def privateKey: PrivateKey = {
val ecParameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1")
val ecNamedCurveSpec: ECParameterSpec = new ECNamedCurveSpec(
ecParameterSpec.getName,
Expand All @@ -85,7 +85,7 @@ object CryptoUtils {
object SecpECDSA {
def signBytes(msg: Array[Byte], privateKey: SecpPrivateKey): SecpECDSASignature = {
val signer = Signature.getInstance("SHA256withECDSA", provider)
signer.initSign(privateKey.privateLey)
signer.initSign(privateKey.privateKey)
signer.update(msg)
SecpECDSASignatureImpl(signer.sign())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import scala.util.Try
// removed, then this tests should be deleted too
class CryptoTestsSpec extends AnyWordSpec {

// public key encoding/decoding (compressed and uncompressed)
// private key encoding/decoding
// Signature validation

"cryptoUtils library" should {

// HASHING
Expand Down Expand Up @@ -88,7 +84,7 @@ class CryptoTestsSpec extends AnyWordSpec {
}

// PUBLIC KEY ENCODING / DECODING
"public key uncompressed encoding us compatible with SDK" in {
"public key uncompressed encoding / decoding is compatible with SDK" in {
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey

Expand All @@ -104,20 +100,56 @@ class CryptoTestsSpec extends AnyWordSpec {
uSecp.toVector mustBe parsedSecpKey.getEncoded.toVector
}

"private key encoding decoding" in {
val pair = EC.INSTANCE.generateKeyPair()
val privK = pair.getPrivateKey
val encodedPvKey = privK.getEncoded
val secp = SecpPrivateKey.unsafefromBytesCompressed(encodedPvKey)
"public key compressed encoding / decoding is compatible with SDK" in {
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey

val secp = secpPublicKey.compressed
val sdk = sdkPubKey.getEncodedCompressed

// we parse the keys in the opposite library
val parsedSDKKey = SecpPublicKey.unsafeToSecpPublicKeyFromCompressed(sdk.toVector)
val parsedSecpKey = EC.INSTANCE.toPublicKeyFromCompressed(secp)

encodedPvKey.toVector mustBe secp.getEncoded.toVector
// we compare the encodings
sdk.toVector mustBe parsedSDKKey.compressed.toVector
secp.toVector mustBe parsedSecpKey.getEncodedCompressed.toVector
}

"encoded uncompressed should be the same as SDK" in {
val pair = EC.INSTANCE.generateKeyPair()
val compressedPub = pair.getPublicKey.getEncodedCompressed
val secpKeyFromCompressed = SecpPublicKey.unsafeToSecpPublicKeyFromCompressed(compressedPub.toVector)
pair.getPublicKey.getEncoded.toVector mustBe secpKeyFromCompressed.unCompressed.toVector
"public key coordinates encoding / decoding is compatible with SDK" in {
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey

val secpX = secpPublicKey.x
val secpY = secpPublicKey.y
val sdkX = sdkPubKey.getCurvePoint.getX.bytes()
val sdkY = sdkPubKey.getCurvePoint.getY.bytes()

// we parse the keys in the opposite library
val parsedSDKKey = SecpPublicKey.unsafeToSecpPublicKeyFromByteCoordinates(sdkX, sdkY)
val parsedSecpKey = EC.INSTANCE.toPublicKeyFromByteCoordinates(secpX, secpY)

// we compare the encodings
sdkX.toVector mustBe parsedSDKKey.x.toVector
sdkY.toVector mustBe parsedSDKKey.y.toVector
secpX.toVector mustBe parsedSecpKey.getCurvePoint.getX.bytes().toVector
secpY.toVector mustBe parsedSecpKey.getCurvePoint.getY.bytes().toVector
}

"private key encoding / decoding is compatible with SDK" in {
val secpPrivateKey = CryptoTestUtils.generateKeyPair().privateKey
val sdkPrivate = EC.INSTANCE.generateKeyPair().getPrivateKey

val uSecp = secpPrivateKey.getEncoded
val uSdk = sdkPrivate.getEncoded

// we parse the keys in the opposite library
val parsedSDKKey = SecpPrivateKey.unsafefromBytesCompressed(uSdk)
val parsedSecpKey = EC.INSTANCE.toPrivateKeyFromBytes(uSecp)

// we compare the encodings
uSdk.toVector mustBe parsedSDKKey.getEncoded.toVector
uSecp.toVector mustBe parsedSecpKey.getEncoded.toVector
}

"Must generate the same key from different encodings" in {
Expand Down

0 comments on commit 800757f

Please sign in to comment.