-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoPlatform.scala
319 lines (281 loc) · 8.53 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package scoin
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.math.BigInteger
import java.security.SecureRandom
import scala.util.Try
import scala.language.implicitConversions
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
import scala.scalajs.js.typedarray.Uint8Array
import scodec.bits.ByteVector
private[scoin] trait CryptoPlatform {
import Crypto._
private val secureRandom = new SecureRandom
def randomBytes(length: Int): ByteVector = {
val buffer = new Array[Byte](length)
secureRandom.nextBytes(buffer)
ByteVector.view(buffer)
}
def G = PublicKey(
ByteVector.view(new Point(Curve.Gx, Curve.Gy).toRawBytes(true))
)
def N = bigint2biginteger(Curve.n)
private implicit def bigint2biginteger(x: js.BigInt): BigInteger =
new BigInteger(x.toString(10), 10)
private implicit def bytevector2biginteger(x: ByteVector): BigInteger =
new BigInteger(x.toHex, 16)
private val zero = BigInteger.valueOf(0)
private val one = BigInteger.valueOf(1)
private[scoin] class PrivateKeyPlatform(value: ByteVector32) {
def add(that: PrivateKey): PrivateKey = PrivateKey(
ByteVector32(
ByteVector.view(
Secp256k1Utils.privateAdd(
value.bytes.toUint8Array,
that.value.bytes.toUint8Array
)
)
)
)
def subtract(that: PrivateKey): PrivateKey = PrivateKey(
ByteVector32(
ByteVector.view(
Secp256k1Utils.privateAdd(
value.bytes.toUint8Array,
Secp256k1Utils.privateNegate(that.value.bytes.toUint8Array)
)
)
)
)
def multiply(that: PrivateKey): PrivateKey =
PrivateKey(
ByteVector32(
ByteVector.fromValidHex(
Secp256k1Utils
.mod(
js.BigInt(
bytevector2biginteger(value.bytes)
.multiply(bytevector2biginteger(that.value.bytes))
.toString(10)
)
)
.toString(16)
)
)
)
def publicKey: PublicKey = PublicKey(
ByteVector.view(Secp256k1.getPublicKey(value.bytes.toUint8Array))
)
}
private[scoin] class PublicKeyPlatform(value: ByteVector) {
lazy val point = Point.fromHex(value.toUint8Array)
def add(that: PublicKey): PublicKey = PublicKey(
ByteVector.view(
point.add(that.asInstanceOf[PublicKeyPlatform].point).toRawBytes(true)
)
)
def add(that: PrivateKey): PublicKey =
PublicKey(
ByteVector.view(
Secp256k1Utils.pointAddScalar(
value.toUint8Array,
that.value.toUint8Array
)
)
)
def subtract(that: PublicKey): PublicKey = PublicKey(
ByteVector.view(
point.add(that.asInstanceOf[PublicKeyPlatform].point).toRawBytes(true)
)
)
def multiply(that: PrivateKey): PublicKey = PublicKey(
ByteVector.view(point.multiply(that.value.toUint8Array).toRawBytes(true))
)
def toUncompressedBin: ByteVector = ByteVector.view(point.toRawBytes(false))
}
def sha1(input: ByteVector): ByteVector =
ByteVector.fromValidHex(
HashJS.sha1().update(input.toHex, "hex").digest("hex")
)
def sha256(input: ByteVector): ByteVector32 =
ByteVector32(
ByteVector.fromValidHex(
HashJS.sha256().update(input.toHex, "hex").digest("hex")
)
)
def sha512(input: ByteVector): ByteVector =
ByteVector.fromValidHex(
HashJS.sha512().update(input.toHex, "hex").digest("hex")
)
def hmac512(key: ByteVector, data: ByteVector): ByteVector =
ByteVector.fromValidHex(
HashJS
.hmac(HashJS.sha512, key.toHex, "hex")
.update(data.toHex, "hex")
.digest("hex")
)
def hmac256(key: ByteVector, data: ByteVector): ByteVector32 =
ByteVector32(
ByteVector.fromValidHex(
HashJS
.hmac(HashJS.sha256, key.toHex, "hex")
.update(data.toHex, "hex")
.digest("hex")
)
)
def ripemd160(input: ByteVector): ByteVector =
ByteVector.fromValidHex(
HashJS.ripemd160().update(input.toHex, "hex").digest("hex")
)
/** @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) &&
Try(Point.fromHex(key.toUint8Array).assertValidity())
.map(_ => true)
.getOrElse(false)
def compact2der(signature: ByteVector64): ByteVector = {
val (r, s) = decodeSignatureCompact(signature)
signatureToDER(r, s)
}
def verifySignature(
data: Array[Byte],
signature: Array[Byte],
publicKey: PublicKey
): Boolean =
Secp256k1.verify(
ByteVector.view(signature).toUint8Array,
ByteVector.view(data).toUint8Array,
publicKey.value.toUint8Array
)
def sign(data: Array[Byte], privateKey: PrivateKey): ByteVector64 =
ByteVector64(
ByteVector.view(
Secp256k1.signSync(
ByteVector.view(data).toUint8Array,
privateKey.value.bytes.toUint8Array,
js.Dictionary(("der" -> false))
)
)
)
/** 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(
ByteVector.view(
Secp256k1.recoverPublicKey(
message.toUint8Array,
signature.bytes.toUint8Array,
recoveryId,
true
)
)
)
}
@js.native
@JSGlobal
object Secp256k1 extends js.Object {
def getPublicKey(
privateKey: Uint8Array,
compressed: Boolean = true
): Uint8Array = js.native
def signSync(
msgHash: Uint8Array,
privateKey: Uint8Array,
options: js.Dictionary[Boolean]
): Uint8Array = js.native
def verify(
sig: Uint8Array,
msgHash: Uint8Array,
publicKey: Uint8Array
): Boolean =
js.native
def recoverPublicKey(
msgHash: Uint8Array,
sig: Uint8Array,
rec: Integer,
compressed: Boolean
): Uint8Array = js.native
}
@js.native
@JSGlobal
object Curve extends js.Object {
def Gx: js.BigInt = js.native
def Gy: js.BigInt = js.native
def n: js.BigInt = js.native
}
@js.native
@JSGlobal
object Point extends js.Object {
def fromHex(bytes: Uint8Array): Point = js.native
}
@js.native
@JSGlobal
class Point(x: js.BigInt, y: js.BigInt) extends js.Object {
def negate(): Point = js.native
def add(point: Point): Point = js.native
def subtract(point: Point): Point = js.native
def multiply(scalar: Uint8Array): Point = js.native
def toRawBytes(compressed: Boolean): Uint8Array = js.native
def assertValidity(): Unit = js.native
}
@js.native
@JSGlobal
object Secp256k1Utils extends js.Object {
def privateNegate(privateKey: Uint8Array): Uint8Array = js.native
def privateAdd(privateKey: Uint8Array, tweak: Uint8Array): Uint8Array =
js.native
def pointAddScalar(point: Uint8Array, tweak: Uint8Array): Uint8Array =
js.native
def mod(number: js.BigInt): js.BigInt = js.native
}
object monkeyPatch {
def sha256Sync(msg: Uint8Array): Uint8Array =
ByteVector
.fromValidHex(
HashJS.sha256().update(ByteVector.view(msg).toHex, "hex").digest("hex")
)
.toUint8Array
def hmacSha256Sync(key: Uint8Array, msg: Uint8Array): Uint8Array =
ByteVector
.fromValidHex(
HashJS
.hmac(HashJS.sha256, ByteVector.view(key).toHex, "hex")
.update(ByteVector.view(msg).toHex, "hex")
.digest("hex")
)
.toUint8Array
Secp256k1Utils.asInstanceOf[js.Dynamic].sha256Sync = sha256Sync
Secp256k1Utils.asInstanceOf[js.Dynamic].hmacSha256Sync = hmacSha256Sync
}
@js.native
@JSGlobal
object HashJS extends js.Object {
def sha1(): Hash = js.native
def sha256(): Hash = js.native
def sha512(): Hash = js.native
def ripemd160(): Hash = js.native
def hmac(hash: () => Hash, key: String, enc: String): Hash = js.native
}
@js.native
trait Hash extends js.Object {
def update(msg: String, enc: String): Hash = js.native
def digest(enc: String): String = js.native
}