aboutsummaryrefslogtreecommitdiffstats
path: root/go/blscgo/bls.go
blob: 170146c0910da4a99c4acefb911b82384c16a8a6 (plain) (blame)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package blscgo

/*
#cgo CFLAGS:-I../../include -DBLS_MAX_OP_UNIT_SIZE=6
#cgo bn256 CFLAGS:-UBLS_MAX_OP_UNIT_SIZE -DBLS_MAX_OP_UNIT_SIZE=4
#cgo bn384 CFLAGS:-UBLS_MAX_OP_UNIT_SIZE -DBLS_MAX_OP_UNIT_SIZE=6
#cgo LDFLAGS:-lbls -lbls_if -lmcl -lgmp -lgmpxx -L../lib -L../../lib -L../../../mcl/lib -L../../mcl/lib  -lstdc++ -lcrypto
#include "bls_if.h"
*/
import "C"
import "fmt"
import "unsafe"

const CurveFp254BNb = 0
const CurveFp382_1 = 1
const CurveFp382_2 = 2

// Init --
func Init(curve int) {
    C.blsInit(C.int(curve), C.BLS_MAX_OP_UNIT_SIZE)
}

// getMaxOpUnitSize --
func GetMaxOpUnitSize() int {
    return int(C.BLS_MAX_OP_UNIT_SIZE)
}

// getOpUnitSize --
func GetOpUnitSize() int {
    return int(C.blsGetOpUnitSize())
}

// ID --
type ID struct {
    v [C.BLS_MAX_OP_UNIT_SIZE]C.uint64_t
}

// getPointer --
func (id *ID) getPointer() (p *C.blsId) {
    // #nosec
    return (*C.blsId)(unsafe.Pointer(&id.v[0]))
}

// String --
func (id *ID) String() string {
    buf := make([]byte, 1024)
    // #nosec
    n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if n == 0 {
        panic("implementation err. size of buf is small")
    }
    return string(buf[:n])
}

// SetStr --
func (id *ID) SetStr(s string) error {
    buf := []byte(s)
    // #nosec
    err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if err > 0 {
        return fmt.Errorf("bad string:%s", s)
    }
    return nil
}

// Set --
func (id *ID) Set(v []uint64) {
    expect := GetOpUnitSize()
    if len(v) != expect {
        panic(fmt.Errorf("bad size (%d), expected size %d", len(v), expect))
    }
    // #nosec
    C.blsIdSet(id.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
}

// SecretKey --
type SecretKey struct {
    v [C.BLS_MAX_OP_UNIT_SIZE]C.uint64_t
}

// getPointer --
func (sec *SecretKey) getPointer() (p *C.blsSecretKey) {
    // #nosec
    return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0]))
}

// String --
func (sec *SecretKey) String() string {
    buf := make([]byte, 1024)
    // #nosec
    n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if n == 0 {
        panic("implementation err. size of buf is small")
    }
    return string(buf[:n])
}

// SetStr -- The string passed in is a number and can be either hex or decimal
func (sec *SecretKey) SetStr(s string) error {
    buf := []byte(s)
    // #nosec
    err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if err > 0 {
        return fmt.Errorf("bad string:%s", s)
    }
    return nil
}

// SetArray --
func (sec *SecretKey) SetArray(v []uint64) {
    expect := GetOpUnitSize()
    if len(v) != expect {
        panic(fmt.Errorf("bad size (%d), expected size %d", len(v), expect))
    }
    // #nosec
    C.blsSecretKeySetArray(sec.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
}

// Init --
func (sec *SecretKey) Init() {
    C.blsSecretKeyInit(sec.getPointer())
}

// Add --
func (sec *SecretKey) Add(rhs *SecretKey) {
    C.blsSecretKeyAdd(sec.getPointer(), rhs.getPointer())
}

// GetMasterSecretKey --
func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) {
    msk = make([]SecretKey, k)
    msk[0] = *sec
    for i := 1; i < k; i++ {
        msk[i].Init()
    }
    return msk
}

// GetMasterPublicKey --
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
}

// Set --
func (sec *SecretKey) Set(msk []SecretKey, id *ID) {
    C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer())
}

// Recover --
func (sec *SecretKey) Recover(secVec []SecretKey, idVec []ID) {
    C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec)))
}

// GetPop --
func (sec *SecretKey) GetPop() (sign *Sign) {
    sign = new(Sign)
    C.blsSecretKeyGetPop(sec.getPointer(), sign.getPointer())
    return sign
}

// PublicKey --
type PublicKey struct {
    v [C.BLS_MAX_OP_UNIT_SIZE * 2 * 3]C.uint64_t
}

// getPointer --
func (pub *PublicKey) getPointer() (p *C.blsPublicKey) {
    // #nosec
    return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0]))
}

// String --
func (pub *PublicKey) String() string {
    buf := make([]byte, 1024)
    // #nosec
    n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if n == 0 {
        panic("implementation err. size of buf is small")
    }
    return string(buf[:n])
}

// SetStr --
func (pub *PublicKey) SetStr(s string) error {
    buf := []byte(s)
    // #nosec
    err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if err > 0 {
        return fmt.Errorf("bad string:%s", s)
    }
    return nil
}

// Add --
func (pub *PublicKey) Add(rhs *PublicKey) {
    C.blsPublicKeyAdd(pub.getPointer(), rhs.getPointer())
}

// Set --
func (pub *PublicKey) Set(mpk []PublicKey, id *ID) {
    C.blsPublicKeySet(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer())
}

// Recover --
func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) {
    C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec)))
}

// Sign  --
type Sign struct {
    v [C.BLS_MAX_OP_UNIT_SIZE * 3]C.uint64_t
}

// getPointer --
func (sign *Sign) getPointer() (p *C.blsSign) {
    // #nosec
    return (*C.blsSign)(unsafe.Pointer(&sign.v[0]))
}

// String --
func (sign *Sign) String() string {
    buf := make([]byte, 1024)
    // #nosec
    n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if n == 0 {
        panic("implementation err. size of buf is small")
    }
    return string(buf[:n])
}

// SetStr --
func (sign *Sign) SetStr(s string) error {
    buf := []byte(s)
    // #nosec
    err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    if err > 0 {
        return fmt.Errorf("bad string:%s", s)
    }
    return nil
}

// GetPublicKey --
func (sec *SecretKey) GetPublicKey() (pub *PublicKey) {
    pub = new(PublicKey)
    C.blsSecretKeyGetPublicKey(sec.getPointer(), pub.getPointer())
    return pub
}

// Sign --
func (sec *SecretKey) Sign(m string) (sign *Sign) {
    sign = new(Sign)
    buf := []byte(m)
    // #nosec
    C.blsSecretKeySign(sec.getPointer(), sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    return sign
}
// Constant Time Sign --
func (sec *SecretKey) SignCT(m string) (sign *Sign) {
    sign = new(Sign)
    buf := []byte(m)
    // #nosec
    C.blsSecretKeySignCT(sec.getPointer(), sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
    return sign
}

// Add --
func (sign *Sign) Add(rhs *Sign) {
    C.blsSignAdd(sign.getPointer(), rhs.getPointer())
}

// Recover --
func (sign *Sign) Recover(signVec []Sign, idVec []ID) {
    C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec)))
}

// Verify --
func (sign *Sign) Verify(pub *PublicKey, m string) bool {
    buf := []byte(m)
    // #nosec
    return C.blsSignVerify(sign.getPointer(), pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1
}

// VerifyPop --
func (sign *Sign) VerifyPop(pub *PublicKey) bool {
    return C.blsSignVerifyPop(sign.getPointer(), pub.getPointer()) == 1
}