This repository was archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbls.go
251 lines (208 loc) · 6.44 KB
/
bls.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
242
243
244
245
246
247
248
249
250
251
package bls
/*
#include "include/mcl/bls.h"
*/
import "C"
import "fmt"
import "unsafe"
import "log"
var bazel = "no"
func init() {
if bazel != "true" {
panic("this library must be built by bazel, but was not")
}
if err := initializeBLS(BLS12_381); err != nil {
log.Fatalf("Could not initialize BLS12-381 curve: %v", err)
}
}
// initializeBLS --
// call this function before calling all the other operations
// this function is not thread safe
func initializeBLS(curve int) error {
err := C.blsInit(C.int(curve), C.MCLBN_COMPILED_TIME_VAR)
if err != 0 {
return fmt.Errorf("ERR Init curve=%d", curve)
}
return nil
}
// SecretKey --
type SecretKey struct {
v fr
}
// getPointer --
func (sec *SecretKey) getPointer() (p *C.blsSecretKey) {
// #nosec
return (*C.blsSecretKey)(unsafe.Pointer(sec))
}
// LittleEndian returns the serialized, little-endian formatted byte slice
// of the secret key.
func (sec *SecretKey) LittleEndian() []byte {
return sec.v.serialize()
}
// SetLittleEndian sets a secret key based on a little-endian formatted
// byte slice.
func (sec *SecretKey) SetLittleEndian(buf []byte) error {
return sec.v.setLittleEndian(buf)
}
// SerializeToHexStr returns a hex string representation of a private key.
func (sec *SecretKey) SerializeToHexStr() string {
return sec.v.getString(IoSerializeHexStr)
}
// DeserializeHexStr deserializes a hex string into a private key.
func (sec *SecretKey) DeserializeHexStr(s string) error {
return sec.v.setString(s, IoSerializeHexStr)
}
// HexString returns the hex string of the private key.
func (sec *SecretKey) HexString() string {
return sec.v.getString(16)
}
// DecString returns a decimal string representation of the private key.
func (sec *SecretKey) DecString() string {
return sec.v.getString(10)
}
// SetHexString sets a private key based on a hex string.
func (sec *SecretKey) SetHexString(s string) error {
return sec.v.setString(s, 16)
}
// SetDecString sets a private key based on a decimal string.
func (sec *SecretKey) SetDecString(s string) error {
return sec.v.setString(s, 10)
}
// IsEqual compares two private keys.
func (sec *SecretKey) IsEqual(rhs *SecretKey) bool {
return sec.v.isEqual(&rhs.v)
}
// SetValue sets a private key's internal representation using an
// inputted number.
func (sec *SecretKey) SetValue(val int64) {
sec.v.setInt64(val)
}
// SetByCSPRNG sets a private key's internal representation using a
// cryptographically-secure, pseudorandom number generator.
func (sec *SecretKey) SetByCSPRNG() {
sec.v.setByCSPRNG()
}
// Add two private keys together.
func (sec *SecretKey) Add(rhs *SecretKey) {
frAdd(&sec.v, &sec.v, &rhs.v)
}
// GetMasterSecretKey creates a series of k secret keys and using a
// pseudorandom number generator and returns them as a slice.
func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) {
msk = make([]SecretKey, k)
msk[0] = *sec
for i := 1; i < k; i++ {
msk[i].SetByCSPRNG()
}
return msk
}
// GetMasterPublicKey returns a list of public keys for a slice of
// private keys.
func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) {
n := len(msk)
mpk = make([]PublicKey, n)
for i := 0; i < n; i++ {
mpk[i] = *msk[i].GetPublicKey()
}
return mpk
}
// PublicKey definition in the BLS scheme.
type PublicKey struct {
v g2
}
// getPointer --
func (pub *PublicKey) getPointer() (p *C.blsPublicKey) {
// #nosec
return (*C.blsPublicKey)(unsafe.Pointer(pub))
}
// Serialize a public key into a byte slice.
func (pub *PublicKey) Serialize() []byte {
return pub.v.serialize()
}
// Deserialize converts a byte slice into a public key.
func (pub *PublicKey) Deserialize(buf []byte) error {
return pub.v.deserialize(buf)
}
// SerializeToHexStr returns a hex string serialization of a public key.
func (pub *PublicKey) SerializeToHexStr() string {
return pub.v.getString(IoSerializeHexStr)
}
// DeserializeHexStr sets a public key from a hex string.
func (pub *PublicKey) DeserializeHexStr(s string) error {
return pub.v.setString(s, IoSerializeHexStr)
}
// HexString returns the hex string representation of a public key.
func (pub *PublicKey) HexString() string {
return pub.v.getString(16)
}
// SetHexString sets a public key from a hex string.
func (pub *PublicKey) SetHexString(s string) error {
return pub.v.setString(s, 16)
}
// IsEqual compares two BLS public keys.
func (pub *PublicKey) IsEqual(rhs *PublicKey) bool {
return pub.v.isEqual(&rhs.v)
}
// Add two BLS public keys together
func (pub *PublicKey) Add(rhs *PublicKey) {
g2Add(&pub.v, &pub.v, &rhs.v)
}
// Sign represents a signature in the BLS signature aggregation scheme.
type Sign struct {
v g1
}
// getPointer --
func (sign *Sign) getPointer() (p *C.blsSignature) {
// #nosec
return (*C.blsSignature)(unsafe.Pointer(sign))
}
// Serialize a signature into a byte array.
func (sign *Sign) Serialize() []byte {
return sign.v.serialize()
}
// Deserialize a signature from a byte array.
func (sign *Sign) Deserialize(buf []byte) error {
return sign.v.deserialize(buf)
}
// SerializeToHexStr serializes a signature into a hex string.
func (sign *Sign) SerializeToHexStr() string {
return sign.v.getString(IoSerializeHexStr)
}
// DeserializeHexStr creates a signature from a hex string serialization.
func (sign *Sign) DeserializeHexStr(s string) error {
return sign.v.setString(s, IoSerializeHexStr)
}
// HexString representation of a signature.
func (sign *Sign) HexString() string {
return sign.v.getString(16)
}
// SetHexString sets a signature from a hex string.
func (sign *Sign) SetHexString(s string) error {
return sign.v.setString(s, 16)
}
// IsEqual compares two signatures.
func (sign *Sign) IsEqual(rhs *Sign) bool {
return sign.v.isEqual(&rhs.v)
}
// GetPublicKey returns the public key corresponding to a BLS private key.
func (sec *SecretKey) GetPublicKey() (pub *PublicKey) {
pub = new(PublicKey)
C.blsGetPublicKey(pub.getPointer(), sec.getPointer())
return pub
}
// Sign a string message using a BLS private key in constant time.
func (sec *SecretKey) Sign(m []byte) (sign *Sign) {
sign = new(Sign)
// #nosec
C.blsSign(sign.getPointer(), sec.getPointer(), unsafe.Pointer(&m[0]), C.size_t(len(m)))
return sign
}
// Add two BLS signatures together.
func (sign *Sign) Add(rhs *Sign) {
C.blsSignatureAdd(sign.getPointer(), rhs.getPointer())
}
// Verify a signature using a BLS public key and a message string.
func (sign *Sign) Verify(pub *PublicKey, m []byte) bool {
// #nosec
return C.blsVerify(sign.getPointer(), pub.getPointer(), unsafe.Pointer(&m[0]), C.size_t(len(m))) == 1
}