Skip to content

Commit 800757f

Browse files
ATL-6926: Correct typo and add tests
This commit adds the final tests to validate the cryptography implementation that replaces the SDK
1 parent 72598aa commit 800757f

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

node/src/main/scala/io/iohk/atala/prism/node/crypto/CryptoUtils.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ object CryptoUtils {
5555

5656
trait SecpPrivateKey {
5757
private[crypto] def bytes: Array[Byte]
58-
private[crypto] def privateLey: PrivateKey
58+
private[crypto] def privateKey: PrivateKey
5959
def getEncoded: Array[Byte]
6060
}
6161
private[crypto] case class SecpPrivateKeyImpl(bytes: Array[Byte]) extends SecpPrivateKey {
6262
override def getEncoded: Array[Byte] = {
63-
privateLey
63+
privateKey
6464
.asInstanceOf[ECPrivateKey]
6565
.getD
6666
.toByteArray
6767
.dropWhile(_ == 0)
6868
}
6969

70-
override def privateLey: PrivateKey = {
70+
override def privateKey: PrivateKey = {
7171
val ecParameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1")
7272
val ecNamedCurveSpec: ECParameterSpec = new ECNamedCurveSpec(
7373
ecParameterSpec.getName,
@@ -85,7 +85,7 @@ object CryptoUtils {
8585
object SecpECDSA {
8686
def signBytes(msg: Array[Byte], privateKey: SecpPrivateKey): SecpECDSASignature = {
8787
val signer = Signature.getInstance("SHA256withECDSA", provider)
88-
signer.initSign(privateKey.privateLey)
88+
signer.initSign(privateKey.privateKey)
8989
signer.update(msg)
9090
SecpECDSASignatureImpl(signer.sign())
9191
}

node/src/test/scala/io/iohk/atala/prism/node/crypto/CryptoTestsSpec.scala

+48-16
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import scala.util.Try
1414
// removed, then this tests should be deleted too
1515
class CryptoTestsSpec extends AnyWordSpec {
1616

17-
// public key encoding/decoding (compressed and uncompressed)
18-
// private key encoding/decoding
19-
// Signature validation
20-
2117
"cryptoUtils library" should {
2218

2319
// HASHING
@@ -88,7 +84,7 @@ class CryptoTestsSpec extends AnyWordSpec {
8884
}
8985

9086
// PUBLIC KEY ENCODING / DECODING
91-
"public key uncompressed encoding us compatible with SDK" in {
87+
"public key uncompressed encoding / decoding is compatible with SDK" in {
9288
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
9389
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey
9490

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

107-
"private key encoding decoding" in {
108-
val pair = EC.INSTANCE.generateKeyPair()
109-
val privK = pair.getPrivateKey
110-
val encodedPvKey = privK.getEncoded
111-
val secp = SecpPrivateKey.unsafefromBytesCompressed(encodedPvKey)
103+
"public key compressed encoding / decoding is compatible with SDK" in {
104+
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
105+
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey
106+
107+
val secp = secpPublicKey.compressed
108+
val sdk = sdkPubKey.getEncodedCompressed
109+
110+
// we parse the keys in the opposite library
111+
val parsedSDKKey = SecpPublicKey.unsafeToSecpPublicKeyFromCompressed(sdk.toVector)
112+
val parsedSecpKey = EC.INSTANCE.toPublicKeyFromCompressed(secp)
112113

113-
encodedPvKey.toVector mustBe secp.getEncoded.toVector
114+
// we compare the encodings
115+
sdk.toVector mustBe parsedSDKKey.compressed.toVector
116+
secp.toVector mustBe parsedSecpKey.getEncodedCompressed.toVector
114117
}
115118

116-
"encoded uncompressed should be the same as SDK" in {
117-
val pair = EC.INSTANCE.generateKeyPair()
118-
val compressedPub = pair.getPublicKey.getEncodedCompressed
119-
val secpKeyFromCompressed = SecpPublicKey.unsafeToSecpPublicKeyFromCompressed(compressedPub.toVector)
120-
pair.getPublicKey.getEncoded.toVector mustBe secpKeyFromCompressed.unCompressed.toVector
119+
"public key coordinates encoding / decoding is compatible with SDK" in {
120+
val secpPublicKey = CryptoTestUtils.generateKeyPair().publicKey
121+
val sdkPubKey = EC.INSTANCE.generateKeyPair().getPublicKey
122+
123+
val secpX = secpPublicKey.x
124+
val secpY = secpPublicKey.y
125+
val sdkX = sdkPubKey.getCurvePoint.getX.bytes()
126+
val sdkY = sdkPubKey.getCurvePoint.getY.bytes()
127+
128+
// we parse the keys in the opposite library
129+
val parsedSDKKey = SecpPublicKey.unsafeToSecpPublicKeyFromByteCoordinates(sdkX, sdkY)
130+
val parsedSecpKey = EC.INSTANCE.toPublicKeyFromByteCoordinates(secpX, secpY)
131+
132+
// we compare the encodings
133+
sdkX.toVector mustBe parsedSDKKey.x.toVector
134+
sdkY.toVector mustBe parsedSDKKey.y.toVector
135+
secpX.toVector mustBe parsedSecpKey.getCurvePoint.getX.bytes().toVector
136+
secpY.toVector mustBe parsedSecpKey.getCurvePoint.getY.bytes().toVector
137+
}
138+
139+
"private key encoding / decoding is compatible with SDK" in {
140+
val secpPrivateKey = CryptoTestUtils.generateKeyPair().privateKey
141+
val sdkPrivate = EC.INSTANCE.generateKeyPair().getPrivateKey
142+
143+
val uSecp = secpPrivateKey.getEncoded
144+
val uSdk = sdkPrivate.getEncoded
145+
146+
// we parse the keys in the opposite library
147+
val parsedSDKKey = SecpPrivateKey.unsafefromBytesCompressed(uSdk)
148+
val parsedSecpKey = EC.INSTANCE.toPrivateKeyFromBytes(uSecp)
149+
150+
// we compare the encodings
151+
uSdk.toVector mustBe parsedSDKKey.getEncoded.toVector
152+
uSecp.toVector mustBe parsedSecpKey.getEncoded.toVector
121153
}
122154

123155
"Must generate the same key from different encodings" in {

0 commit comments

Comments
 (0)