-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzkscrypto.go
185 lines (164 loc) · 5.43 KB
/
zkscrypto.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
package zkscrypto
/*
#cgo LDFLAGS: -lzks-crypto
#include "zks_crypto.h"
*/
import "C"
import (
"encoding/hex"
"errors"
"unsafe"
)
var (
errSeedLen = errors.New("given seed is too short, length must be greater than 32")
errPrivateKey = errors.New("error on private key generation")
errPrivateKeyLen = errors.New("raw private key must be exactly 32 bytes")
errSignedMsgLen = errors.New("musig message length must not be larger than 92")
errSign = errors.New("error on sign message")
)
func init() {
C.zks_crypto_init()
}
/*
************************************************************************************************
Private key implementation
************************************************************************************************
*/
// NewPrivateKey generates private key from seed
func NewPrivateKey(seed []byte) (*PrivateKey, error) {
pointer := C.struct_ZksPrivateKey{}
rawSeed := C.CBytes(seed)
defer C.free(rawSeed)
result := C.zks_crypto_private_key_from_seed((*C.uint8_t)(rawSeed), C.size_t(len(seed)), &pointer)
if result != 0 {
switch result {
case 1:
return nil, errSeedLen
default:
return nil, errPrivateKey
}
}
data := unsafe.Pointer(&pointer.data)
return &PrivateKey{data: C.GoBytes(data, C.PRIVATE_KEY_LEN)}, nil
}
// NewPrivateKeyRaw create private key from raw bytes
func NewPrivateKeyRaw(pk []byte) (*PrivateKey, error) {
if len(pk) != C.PRIVATE_KEY_LEN {
return nil, errPrivateKeyLen
}
return &PrivateKey{data: pk}, nil
}
// GetBytes return private key raw bytes
func (pk *PrivateKey) GetBytes() []byte {
return pk.data
}
// Sign message with musig Schnorr signature scheme
func (pk *PrivateKey) Sign(message []byte) (*Signature, error) {
privateKeyC := C.struct_ZksPrivateKey{}
rawMessage := C.CBytes(message)
defer C.free(rawMessage)
for i := range pk.data {
privateKeyC.data[i] = C.uint8_t(pk.data[i])
}
signatureC := C.struct_ZksSignature{}
result := C.zks_crypto_sign_musig(&privateKeyC, (*C.uint8_t)(rawMessage), C.size_t(len(message)), &signatureC)
if result != 0 {
switch result {
case 1:
return nil, errSignedMsgLen
default:
return nil, errSign
}
}
data := unsafe.Pointer(&signatureC.data)
return &Signature{data: C.GoBytes(data, C.PACKED_SIGNATURE_LEN)}, nil
}
// PublicKey generates public key from private key
func (pk *PrivateKey) PublicKey() (*PublicKey, error) {
privateKeyC := C.struct_ZksPrivateKey{}
for i := range pk.data {
privateKeyC.data[i] = C.uint8_t(pk.data[i])
}
pointer := C.struct_ZksPackedPublicKey{}
result := C.zks_crypto_private_key_to_public_key(&privateKeyC, &pointer)
if result != 0 {
return nil, errors.New("error on public key generation")
}
data := unsafe.Pointer(&pointer.data)
return &PublicKey{data: C.GoBytes(data, C.PUBLIC_KEY_LEN)}, nil
}
// HexString creates a hex string representation of a private key
func (pk *PrivateKey) HexString() string {
if pk.data == nil || len(pk.data) == 0 {
return "0x"
}
return hex.EncodeToString(pk.data)
}
/*
************************************************************************************************
Public key implementation
************************************************************************************************
*/
// Hash generates hash from public key
func (pk *PublicKey) Hash() (*PublicKeyHash, error) {
publicKeyC := C.struct_ZksPackedPublicKey{}
for i := range pk.data {
publicKeyC.data[i] = C.uint8_t(pk.data[i])
}
pointer := C.struct_ZksPubkeyHash{}
result := C.zks_crypto_public_key_to_pubkey_hash(&publicKeyC, &pointer)
if result != 0 {
return nil, errors.New("Error on public key hash generation")
}
data := unsafe.Pointer(&pointer.data)
return &PublicKeyHash{data: C.GoBytes(data, C.PUBKEY_HASH_LEN)}, nil
}
// HexString creates a hex string representation of a public key
func (pk *PublicKey) HexString() string {
if pk.data == nil || len(pk.data) == 0 {
return "0x"
}
return hex.EncodeToString(pk.data)
}
/*
************************************************************************************************
ResqueHash implementation
************************************************************************************************
*/
// ResqueHashOrders generates hash from orders bytes
func ResqueHashOrders(orders []byte) *ResqueHash {
pointer := C.struct_ZksResqueHash{}
rawOrders := C.CBytes(orders)
defer C.free(rawOrders)
C.rescue_hash_orders((*C.uint8_t)(rawOrders), C.size_t(len(orders)), &pointer)
data := unsafe.Pointer(&pointer.data)
return &ResqueHash{data: C.GoBytes(data, C.RESCUE_HASH_LEN)}
}
// GetBytes return resque hash raw bytes
func (rh *ResqueHash) GetBytes() []byte {
return rh.data
}
/*
************************************************************************************************
Private key Hash implementation
************************************************************************************************
*/
// HexString creates a hex string representation of a public key hash
func (pk *PublicKeyHash) HexString() string {
if pk.data == nil || len(pk.data) == 0 {
return "0x"
}
return hex.EncodeToString(pk.data)
}
/*
************************************************************************************************
Signature implementation
************************************************************************************************
*/
// HexString creates a hex string representation of a signature
func (pk *Signature) HexString() string {
if pk.data == nil || len(pk.data) == 0 {
return "0x"
}
return hex.EncodeToString(pk.data)
}