-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchaincodes.go
241 lines (177 loc) · 4.7 KB
/
chaincodes.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
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
package ed448
import (
"fmt"
"io"
// "github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/sha3"
)
type ExtendedPrivate [114]uint8
type ExtendedPublic [114]uint8
func SHA512Hash(password, salt []uint8) []uint8 {
return pbkdf2.Key(password, salt, 2048, 57, sha3.New512)
}
func concatenateAndHex(prefix uint8, key []uint8, index uint32, salt []uint8) []uint8 {
if len(key) != 57 {
panic("wrong slice length")
}
var p [62]uint8
p[0] = prefix
copy(p[1:58], key[:])
for i := 58; i < 62; i++ {
p[i] = uint8(index & 0xff)
index >>= 8
}
return SHA512Hash(p[:], salt)
}
func addTwoSecrets(secKey1 []uint8, secKey2 []uint8) []uint8 {
if len(secKey1) != 57 {
panic("wrong slice length")
}
if len(secKey2) != 57 {
panic("wrong slice length")
}
var secKey [57]uint8
var count uint16 = 0
for i := 0; i < 57; i++ {
count += uint16(secKey1[i]) + uint16(secKey2[i])
secKey[i] = uint8(count & 0xff)
count >>= 8
}
return secKey[:]
}
func addTwoPublic(pub1 PublicKey, pub2 PublicKey) PublicKey {
var pub PublicKey
p := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
p1 := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
p2 := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
if !p1.EdDSADecode(pub1[:]) {
panic("Point is not on the curve!")
}
if !p2.EdDSADecode(pub2[:]) {
panic("Point is not on the curve!")
}
p.Add(p1, p2)
r := p.EdDSAEncode()
copy(pub[:], r[:])
return pub
}
func shiftPublic(pub1 PublicKey, shift []uint8) PublicKey {
r := NewScalar(shift[:])
r.Halve(r)
r.Halve(r)
p2 := PrecomputedScalarMul(r)
var pub PublicKey
p := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
p1 := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
if !p1.EdDSADecode(pub1[:]) {
panic("Point is not on the curve!")
}
p.Add(p1, p2)
t := p.EdDSAEncode()
copy(pub[:], t[:])
return pub
}
func clampTemplate(t []uint8) {
if len(t) != 57 {
panic("wrong slice length")
}
t[56] = 0
t[55] = 0
t[54] = 0
t[53] = 0
t[0] &= 0xfc
}
func SeedToExtendedPrivate(s []uint8) ExtendedPrivate {
var p ExtendedPrivate
if len(s) != 64 {
panic("Seed must be 64 bytes")
}
t := SHA512Hash(s, []uint8("mnemonicforthechain"))
copy (p[:57], t)
t = SHA512Hash(s, []uint8("mnemonicforthekey"))
copy (p[57:], t)
p[113] |= 0x80 // Set key type identifier
p[112] |= 0x80 // EdDSA standard
p[112] &= 0xbf // Set to keep previous =1 during generation new accounts
return p
}
func (s ExtendedPrivate) getPublic() PublicKey {
var secret PrivateKey
copy(secret[:], s[57:])
var public PublicKey = SecretToPublic(secret)
var zero [57]uint8
copy(secret[:], zero[:])
return public
}
func ExtendedPrivateToPublic(s ExtendedPrivate) ExtendedPublic {
var pub ExtendedPublic
copy(pub[:57], s[:57])
p := s.getPublic()
copy(pub[57:], p[:])
return pub
}
func ChildPrivateToPrivate(s ExtendedPrivate, index uint32) ExtendedPrivate {
var child ExtendedPrivate
if index >= 0x80000000 {
hex := concatenateAndHex(1, s[57:], index, s[:57])
copy(child[:57], hex[:])
var zero [57]uint8
copy(hex[:], zero[:])
hex = concatenateAndHex(0, s[57:], index, s[:57])
clampTemplate(hex)
var a []uint8 = addTwoSecrets(s[57:], hex)
copy(child[57:], a[:])
copy(hex[:], zero[:])
copy(a[:], zero[:])
return child
} else {
var pub PublicKey = s.getPublic()
hex := concatenateAndHex(3, pub[:], index, s[:57])
copy(child[:57], hex[:])
var zero [57]uint8
copy(hex[:], zero[:])
hex = concatenateAndHex(2, pub[:], index, s[:57])
clampTemplate(hex)
var a []uint8 = addTwoSecrets(s[57:], hex)
copy(child[57:], a[:])
copy(hex[:], zero[:])
copy(a[:], zero[:])
return child
}
}
func ChildPrivateToPublic(s ExtendedPrivate, index uint32) ExtendedPublic {
var s1 ExtendedPrivate = ChildPrivateToPrivate(s, index)
var p ExtendedPublic = ExtendedPrivateToPublic(s1)
var zero [114]uint8
copy(s1[:], zero[:])
return p
}
func ChildPublicToPublic(pub ExtendedPublic, index uint32) ExtendedPublic {
if index >= 0x80000000 {
panic("wrong index value")
}
var child ExtendedPublic
hex := concatenateAndHex(3, pub[57:], index, pub[:57])
copy(child[:57], hex[:])
hex = concatenateAndHex(2, pub[57:], index, pub[:57])
clampTemplate(hex)
/* var s1 PrivateKey
copy(s1[:], hex[:])
a2 := publicWithoutClamp(s1)*/
var a1 PublicKey
copy(a1[:], pub[57:])
var a PublicKey = shiftPublic(a1, hex[:])
copy(child[57:], a[:])
return child
}
func GenerateSeed(reader io.Reader) ([]uint8, error) {
seed := new([64]uint8)
n, err := io.ReadFull(reader, seed[:])
if err != nil {
return seed[:], err
} else if n != 64 {
return seed[:], fmt.Errorf("not 64 random bytes")
}
return seed[:], nil
}