-
Notifications
You must be signed in to change notification settings - Fork 13
/
mixinnet_key.go
202 lines (175 loc) · 4.29 KB
/
mixinnet_key.go
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
package mixin
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"strconv"
"github.com/fox-one/mixin-sdk-go/edwards25519"
)
type (
Key [32]byte
)
func NewKey(randReader io.Reader) Key {
var (
seed = make([]byte, 64)
s = 0
)
for s < len(seed) {
n, _ := randReader.Read(seed[s:])
s += n
}
return NewKeyFromSeed(seed)
}
func NewKeyFromSeed(seed []byte) Key {
var key [32]byte
var src [64]byte
if len(seed) != len(src) {
panic(len(seed))
}
copy(src[:], seed)
edwards25519.ScReduce(&key, &src)
return key
}
func KeyFromString(s string) (Key, error) {
var key Key
b, err := hex.DecodeString(s)
if err != nil {
return key, err
}
if len(b) != len(key) {
return key, fmt.Errorf("invalid key size %d", len(b))
}
copy(key[:], b)
return key, nil
}
func (k Key) CheckKey() bool {
var point edwards25519.ExtendedGroupElement
tmp := [32]byte(k)
return point.FromBytes(&tmp)
}
func (k Key) CheckScalar() bool {
tmp := [32]byte(k)
return edwards25519.ScValid(&tmp)
}
func (k Key) Public() Key {
var point edwards25519.ExtendedGroupElement
tmp := [32]byte(k)
edwards25519.GeScalarMultBase(&point, &tmp)
point.ToBytes(&tmp)
return tmp
}
func (k Key) HasValue() bool {
zero := Key{}
return bytes.Compare(k[:], zero[:]) != 0
}
func (k Key) DeterministicHashDerive() Key {
seed := NewHash(k[:])
return NewKeyFromSeed(append(seed[:], seed[:]...))
}
func KeyMultPubPriv(pub, priv *Key) *Key {
if !pub.CheckKey() {
panic(pub.String())
}
if !priv.CheckScalar() {
panic(priv.String())
}
var point edwards25519.ExtendedGroupElement
var point2 edwards25519.ProjectiveGroupElement
tmp := [32]byte(*pub)
point.FromBytes(&tmp)
tmp = [32]byte(*priv)
edwards25519.GeScalarMult(&point2, &tmp, &point)
point2.ToBytes(&tmp)
key := Key(tmp)
return &key
}
func (k *Key) MultScalar(outputIndex int) *Key {
tmp := make([]byte, 12, 12)
length := binary.PutUvarint(tmp, uint64(outputIndex))
tmp = tmp[:length]
var src [64]byte
var buf bytes.Buffer
buf.Write(k[:])
buf.Write(tmp)
hash := NewHash(buf.Bytes())
copy(src[:32], hash[:])
hash = NewHash(hash[:])
copy(src[32:], hash[:])
key := NewKeyFromSeed(src[:])
return &key
}
func DeriveGhostPublicKey(r, A, B *Key, outputIndex int) *Key {
var point1, point2 edwards25519.ExtendedGroupElement
var point3 edwards25519.CachedGroupElement
var point4 edwards25519.CompletedGroupElement
var point5 edwards25519.ProjectiveGroupElement
tmp := [32]byte(*B)
point1.FromBytes(&tmp)
scalar := KeyMultPubPriv(A, r).MultScalar(outputIndex).HashScalar()
edwards25519.GeScalarMultBase(&point2, scalar)
point2.ToCached(&point3)
edwards25519.GeAdd(&point4, &point1, &point3)
point4.ToProjective(&point5)
point5.ToBytes(&tmp)
key := Key(tmp)
return &key
}
func DeriveGhostPrivateKey(R, a, b *Key, outputIndex int) *Key {
scalar := KeyMultPubPriv(R, a).MultScalar(outputIndex).HashScalar()
tmp := [32]byte(*b)
edwards25519.ScAdd(&tmp, &tmp, scalar)
key := Key(tmp)
return &key
}
func ViewGhostOutputKey(P, a, R *Key, outputIndex int) *Key {
var point1, point2 edwards25519.ExtendedGroupElement
var point3 edwards25519.CachedGroupElement
var point4 edwards25519.CompletedGroupElement
var point5 edwards25519.ProjectiveGroupElement
tmp := [32]byte(*P)
point1.FromBytes(&tmp)
scalar := KeyMultPubPriv(R, a).MultScalar(outputIndex).HashScalar()
edwards25519.GeScalarMultBase(&point2, scalar)
point2.ToCached(&point3)
edwards25519.GeSub(&point4, &point1, &point3)
point4.ToProjective(&point5)
point5.ToBytes(&tmp)
key := Key(tmp)
return &key
}
func (k Key) HashScalar() *[32]byte {
var out [32]byte
var src [64]byte
hash := NewHash(k[:])
copy(src[:32], hash[:])
hash = NewHash(hash[:])
copy(src[32:], hash[:])
edwards25519.ScReduce(&out, &src)
return &out
}
func (k Key) String() string {
return hex.EncodeToString(k[:])
}
func (k Key) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(k.String())), nil
}
func (k *Key) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(string(unquoted))
if err != nil {
if data, err = base64.StdEncoding.DecodeString(string(unquoted)); err != nil {
return err
}
}
if len(data) != len(k) {
return fmt.Errorf("invalid key length %d", len(data))
}
copy(k[:], data)
return nil
}