@@ -14,10 +14,6 @@ import scala.util.Try
14
14
// removed, then this tests should be deleted too
15
15
class CryptoTestsSpec extends AnyWordSpec {
16
16
17
- // public key encoding/decoding (compressed and uncompressed)
18
- // private key encoding/decoding
19
- // Signature validation
20
-
21
17
" cryptoUtils library" should {
22
18
23
19
// HASHING
@@ -88,7 +84,7 @@ class CryptoTestsSpec extends AnyWordSpec {
88
84
}
89
85
90
86
// 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 {
92
88
val secpPublicKey = CryptoTestUtils .generateKeyPair().publicKey
93
89
val sdkPubKey = EC .INSTANCE .generateKeyPair().getPublicKey
94
90
@@ -104,20 +100,56 @@ class CryptoTestsSpec extends AnyWordSpec {
104
100
uSecp.toVector mustBe parsedSecpKey.getEncoded.toVector
105
101
}
106
102
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)
112
113
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
114
117
}
115
118
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
121
153
}
122
154
123
155
" Must generate the same key from different encodings" in {
0 commit comments