-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoPlatform.scala
260 lines (234 loc) · 6.41 KB
/
CryptoPlatform.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package scoin
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.math.BigInteger
import scala.scalanative.unsigned._
import scodec.bits.ByteVector
import sha256.{hash => sha256sum}
import sha512.{hash => sha512sum}
import hmac256.{hmac => hmac256sum}
import hmac512.{hmac => hmac512sum}
import ripemd160.{hash => ripemd160sum}
import secp256k1.Secp256k1
private[scoin] trait CryptoPlatform {
import Crypto._
def randomBytes(length: Int): ByteVector = {
ByteVector(
(1 to (length.toDouble / 32).ceil.toInt).iterator
.map(_ => secp256k1.createPrivateKey().value.map(_.toByte))
.reduce(_ ++ _)
.take(length)
)
}
def G = PublicKey(
ByteVector(
Secp256k1.G.value.toArray.map[Byte](_.toByte)
)
)
def N: BigInteger = Secp256k1.N
private[scoin] class PrivateKeyPlatform(value: ByteVector32) {
lazy val underlying =
secp256k1.loadPrivateKey(value.toArray.map(_.toUByte)).toOption.get
def add(that: PrivateKey): PrivateKey =
PrivateKey(
ByteVector32(
ByteVector(
underlying
.add(that.value.toArray.map(_.toUByte))
.value
.map(_.toByte)
)
)
)
def subtract(that: PrivateKey): PrivateKey =
PrivateKey(
ByteVector32(
ByteVector(
underlying
.add(
secp256k1
.loadPrivateKey(that.value.toArray.map(_.toUByte))
.toOption
.get
.negate()
.value
)
.value
.map(_.toByte)
)
)
)
def multiply(that: PrivateKey): PrivateKey =
PrivateKey(
ByteVector32(
ByteVector(
underlying
.multiply(that.value.toArray.map(_.toUByte))
.value
.map(_.toByte)
)
)
)
def publicKey: PublicKey =
PublicKey(
ByteVector(underlying.publicKey().value.map(_.toByte))
)
}
private[scoin] class PublicKeyPlatform(value: ByteVector) {
lazy val underlying =
secp256k1.loadPublicKey(value.toArray.map(_.toUByte)).toOption.get
def add(that: PublicKey): PublicKey =
PublicKey(
ByteVector(
underlying
.add(
that.value.toArray.map(_.toUByte)
)
.value
.map(_.toByte)
)
)
def add(that: PrivateKey): PublicKey =
PublicKey(
ByteVector(
underlying
.add(
that.value.toArray.map(_.toUByte)
)
.value
.map(_.toByte)
)
)
def subtract(that: PublicKey): PublicKey =
PublicKey(
ByteVector(
underlying
.add(
secp256k1
.loadPrivateKey(that.value.toArray.map(_.toUByte))
.toOption
.get
.negate()
.value
.toArray
.map(_.toUByte)
)
.value
.map(_.toByte)
)
)
def multiply(that: PrivateKey): PublicKey =
PublicKey(
ByteVector(
underlying
.multiply(that.value.toArray.map(_.toUByte))
.value
.map(_.toByte)
)
)
def toUncompressedBin: ByteVector = ByteVector(
underlying.toUncompressed().map(_.toByte)
)
}
def sha1(x: ByteVector): ByteVector32 =
throw new NotImplementedError("not implemented")
def sha256(x: ByteVector): ByteVector32 =
ByteVector32(
ByteVector(
sha256sum(x.toArray.map[UByte](_.toUByte))
.map[Byte](_.toByte)
)
)
def sha512(x: ByteVector): ByteVector =
ByteVector(
sha512sum(x.toArray.map[UByte](_.toUByte)).map[Byte](_.toByte)
)
def hmac512(key: ByteVector, data: ByteVector): ByteVector =
ByteVector(
hmac512sum(
key.toArray.map[UByte](_.toUByte),
data.toArray.map[UByte](_.toUByte)
)
.map[Byte](_.toByte)
)
def hmac256(key: ByteVector, message: ByteVector): ByteVector32 =
ByteVector32(
ByteVector(
hmac256sum(
key.toArray.map[UByte](_.toUByte),
message.toArray.map[UByte](_.toUByte)
)
.map[Byte](_.toByte)
)
)
def ripemd160(input: ByteVector): ByteVector =
ByteVector(
ripemd160sum(input.toArray.map[UByte](_.toUByte))
.map[Byte](_.toByte)
)
/** @param key
* serialized public key
* @return
* true if the key is valid. This check is much more expensive than its lax
* version since here we check that the public key is a valid point on the
* secp256k1 curve
*/
def isPubKeyValidStrict(key: ByteVector): Boolean = isPubKeyValidLax(key) &&
secp256k1.loadPublicKey(key.toArray.map(_.toUByte)).toOption.isDefined
// copied from noble-secp256k1
def compact2der(signature: ByteVector64): ByteVector = {
val (r, s) = decodeSignatureCompact(signature)
signatureToDER(r, s)
}
def verifySignature(
data: Array[Byte],
signature: Array[Byte],
publicKey: PublicKey
): Boolean =
publicKey.underlying
.verify(data.map(_.toUByte), signature.map(_.toUByte))
.toOption
.getOrElse(false)
def sign(data: Array[Byte], privateKey: PrivateKey): ByteVector64 =
ByteVector64(
ByteVector(
privateKey.underlying
.sign(data.map(_.toUByte))
.toOption
.get
.map(_.toByte)
)
)
/** Recover public keys from a signature and the message that was signed. This
* method will return 2 public keys, and the signature can be verified with
* both, but only one of them matches that private key that was used to
* generate the signature.
*
* @param signature
* signature
* @param message
* message that was signed
* @return
* a recovered public key
*/
def recoverPublicKey(
signature: ByteVector64,
message: ByteVector,
recoveryId: Int
): PublicKey =
PublicKey(
ByteVector64(
ByteVector(
secp256k1
.recoverPublicKey(
message.toArray.map(_.toUByte),
signature.bytes.toArray.map(_.toUByte),
recoveryId
)
.toOption
.get
.value
.map(_.toByte)
)
)
)
}