-
Notifications
You must be signed in to change notification settings - Fork 12
/
mixinnet_crypto.go
179 lines (153 loc) · 3.25 KB
/
mixinnet_crypto.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
package mixin
import (
"encoding/hex"
"fmt"
"strconv"
"golang.org/x/crypto/sha3"
)
const (
Operator0 = 0x00
Operator64 = 0x40
OperatorSum = 0xfe
OperatorCmp = 0xff
)
type (
Script []uint8
Hash [32]byte
Key [32]byte
Signature [64]byte
)
// Script
func NewThresholdScript(threshold uint8) Script {
return Script{OperatorCmp, OperatorSum, threshold}
}
func (s Script) VerifyFormat() error {
if len(s) != 3 {
return fmt.Errorf("invalid script %d", len(s))
}
if s[0] != OperatorCmp || s[1] != OperatorSum {
return fmt.Errorf("invalid script %d %d", s[0], s[1])
}
return nil
}
func (s Script) Validate(sum int) error {
err := s.VerifyFormat()
if err != nil {
return err
}
if sum < int(s[2]) {
return fmt.Errorf("invalid signature keys %d %d", sum, s[2])
}
return nil
}
func (s Script) String() string {
return hex.EncodeToString(s[:])
}
func (s Script) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(s.String())), nil
}
func (s *Script) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(string(unquoted))
if err != nil {
return err
}
*s = data
return nil
}
// Hash
func NewHash(data []byte) Hash {
return Hash(sha3.Sum256(data))
}
func HashFromString(src string) (Hash, error) {
var hash Hash
data, err := hex.DecodeString(src)
if err != nil {
return hash, err
}
if len(data) != len(hash) {
return hash, fmt.Errorf("invalid hash length %d", len(data))
}
copy(hash[:], data)
return hash, nil
}
func (h Hash) String() string {
return hex.EncodeToString(h[:])
}
func (h Hash) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(h.String())), nil
}
func (h *Hash) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(string(unquoted))
if err != nil {
return err
}
if len(data) != len(h) {
return fmt.Errorf("invalid hash length %d", len(data))
}
copy(h[:], data)
return nil
}
// 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) 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 {
return err
}
if len(data) != len(k) {
return fmt.Errorf("invalid key length %d", len(data))
}
copy(k[:], data)
return nil
}
// Signature
func (s Signature) String() string {
return hex.EncodeToString(s[:])
}
func (s Signature) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(s.String())), nil
}
func (s *Signature) UnmarshalJSON(b []byte) error {
unquoted, err := strconv.Unquote(string(b))
if err != nil {
return err
}
data, err := hex.DecodeString(string(unquoted))
if err != nil {
return err
}
if len(data) != len(s) {
return fmt.Errorf("invalid signature length %d", len(data))
}
copy(s[:], data)
return nil
}