-
Notifications
You must be signed in to change notification settings - Fork 1
/
goldilocks.go
93 lines (75 loc) · 1.85 KB
/
goldilocks.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
package mobileed448
import (
"bytes"
"strconv"
"github.com/core-coin/ed448"
)
func Ed448HDWalletsGenerateKey(seed string, index string) (string, error) {
sb, err := decodeBytes(seed)
if err != nil {
return "", err
}
iu, err := strconv.ParseUint(index, 10, 32)
if err != nil {
return "", err
}
m := ed448.SeedToExtendedPrivate([]uint8(sb))
k1 := ed448.ChildPrivateToPrivate(m, 0x80000000+44)
k2 := ed448.ChildPrivateToPrivate(k1, 0x80000000+654)
k3 := ed448.ChildPrivateToPrivate(k2, 0x80000000+0)
k4 := ed448.ChildPrivateToPrivate(k3, 0x80000000+0)
k5 := ed448.ChildPrivateToPrivate(k4, uint32(iu))
return encodeBytes(k5[57:]), nil
}
func Ed448GenerateKey(seed string) (string, error) {
sb, err := decodeBytes(seed)
if err != nil {
return "", err
}
privKey, err := ed448.Ed448GenerateKey(bytes.NewBuffer(sb))
if err != nil {
return "", err
}
return encodeBytes(privKey[:]), nil
}
func Ed448DerivePublicKey(privKey string) (string, error) {
pb, err := decodeBytes(privKey)
if err != nil {
return "", err
}
priv := ed448.BytesToPrivateKey(pb)
pub := ed448.Ed448DerivePublicKey(priv)
return encodeBytes(pub[:]), nil
}
func Ed448Sign(privKey, message string) (string, error) {
pb, err := decodeBytes(privKey)
if err != nil {
return "", err
}
priv := ed448.BytesToPrivateKey(pb)
msg, err := decodeBytes(message)
if err != nil {
return "", err
}
sig := ed448.Ed448Sign(priv, msg)
return encodeBytes(sig[:]), nil
}
func Ed448Verify(pubKey, signature, message string) (string, error) {
pb, err := decodeBytes(pubKey)
if err != nil {
return "false", err
}
pub := ed448.BytesToPublicKey(pb)
sig, err := decodeBytes(signature)
if err != nil {
return "false", err
}
msg, err := decodeBytes(message)
if err != nil {
return "false", err
}
if !ed448.Ed448Verify(pub, sig, msg) {
return "false", nil
}
return "true", nil
}