-
Notifications
You must be signed in to change notification settings - Fork 1
/
Generators.scala
60 lines (52 loc) · 1.83 KB
/
Generators.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
package scoin.ln
import scodec.bits.ByteVector
import scoin.{ByteVector32, Crypto}
import scoin.Crypto.{PublicKey, PrivateKey}
object Generators {
def fixSize(data: ByteVector): ByteVector32 = data.length match {
case 32 => ByteVector32(data)
case length if length < 32 => ByteVector32(data.padLeft(32))
}
def perCommitSecret(seed: ByteVector32, index: Long): PrivateKey = PrivateKey(
ShaChain.shaChainFromSeed(seed, 0xffffffffffffL - index)
)
def perCommitPoint(seed: ByteVector32, index: Long): PublicKey =
perCommitSecret(seed, index).publicKey
def derivePrivKey(
secret: PrivateKey,
perCommitPoint: PublicKey
): PrivateKey = {
// secretkey = basepoint-secret + SHA256(per-commitment-point || basepoint)
secret.add(
PrivateKey(Crypto.sha256(perCommitPoint.value ++ secret.publicKey.value))
)
}
def derivePubKey(
basePoint: PublicKey,
perCommitPoint: PublicKey
): PublicKey = {
// pubkey = basepoint + SHA256(per-commitment-point || basepoint)*G
val a = PrivateKey(Crypto.sha256(perCommitPoint.value ++ basePoint.value))
basePoint.add(a.publicKey)
}
def revocationPubKey(
basePoint: PublicKey,
perCommitPoint: PublicKey
): PublicKey = {
val a = PrivateKey(Crypto.sha256(basePoint.value ++ perCommitPoint.value))
val b = PrivateKey(Crypto.sha256(perCommitPoint.value ++ basePoint.value))
basePoint.multiply(a).add(perCommitPoint.multiply(b))
}
def revocationPrivKey(
secret: PrivateKey,
perCommitSecret: PrivateKey
): PrivateKey = {
val a = PrivateKey(
Crypto.sha256(secret.publicKey.value ++ perCommitSecret.publicKey.value)
)
val b = PrivateKey(
Crypto.sha256(perCommitSecret.publicKey.value ++ secret.publicKey.value)
)
secret.multiply(a).add(perCommitSecret.multiply(b))
}
}