forked from filecoin-project/filecoin-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbls.go
155 lines (124 loc) · 4.74 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
//+build cgo
package ffi
import (
"unsafe"
)
// #cgo LDFLAGS: ${SRCDIR}/libfilecoin.a
// #cgo pkg-config: ${SRCDIR}/filecoin.pc
// #include "./filecoin.h"
import "C"
// Hash computes the digest of a message
func Hash(message Message) Digest {
// prep request
cMessage := C.CBytes(message)
defer C.free(cMessage)
cMessagePtr := (*C.uchar)(cMessage)
cMessageLen := C.size_t(len(message))
// call method
resPtr := (*C.HashResponse)(unsafe.Pointer(C.hash(cMessagePtr, cMessageLen)))
defer C.destroy_hash_response(resPtr)
// prep response
var digest Digest
digestSlice := C.GoBytes(unsafe.Pointer(&resPtr.digest), DigestBytes) // nolint: staticcheck
copy(digest[:], digestSlice)
return digest
}
// Verify verifies that a signature is the aggregated signature of digests - pubkeys
func Verify(signature *Signature, digests []Digest, publicKeys []PublicKey) bool {
// prep data
flattenedDigests := make([]byte, DigestBytes*len(digests))
for idx, digest := range digests {
copy(flattenedDigests[(DigestBytes*idx):(DigestBytes*(1+idx))], digest[:])
}
flattenedPublicKeys := make([]byte, PublicKeyBytes*len(publicKeys))
for idx, publicKey := range publicKeys {
copy(flattenedPublicKeys[(PublicKeyBytes*idx):(PublicKeyBytes*(1+idx))], publicKey[:])
}
// prep request
cSignature := C.CBytes(signature[:])
defer C.free(cSignature)
cSignaturePtr := (*C.uchar)(cSignature)
cFlattenedDigests := C.CBytes(flattenedDigests)
defer C.free(cFlattenedDigests)
cFlattenedDigestsPtr := (*C.uint8_t)(cFlattenedDigests)
cFlattenedDigestsLen := C.size_t(len(flattenedDigests))
cFlattenedPublicKeys := C.CBytes(flattenedPublicKeys)
defer C.free(cFlattenedPublicKeys)
cFlattenedPublicKeysPtr := (*C.uint8_t)(cFlattenedPublicKeys)
cFlattenedPublicKeysLen := C.size_t(len(flattenedPublicKeys))
// call method
res := (C.int)(C.verify(cSignaturePtr, cFlattenedDigestsPtr, cFlattenedDigestsLen, cFlattenedPublicKeysPtr, cFlattenedPublicKeysLen))
return res > 0
}
// Aggregate aggregates signatures together into a new signature
func Aggregate(signatures []Signature) *Signature {
// prep data
flattenedSignatures := make([]byte, SignatureBytes*len(signatures))
for idx, sig := range signatures {
copy(flattenedSignatures[(SignatureBytes*idx):(SignatureBytes*(1+idx))], sig[:])
}
// prep request
cFlattenedSignatures := C.CBytes(flattenedSignatures)
defer C.free(cFlattenedSignatures)
cFlattenedSignaturesPtr := (*C.uint8_t)(cFlattenedSignatures)
cFlattenedSignaturesLen := C.size_t(len(flattenedSignatures))
// call method
resPtr := (*C.AggregateResponse)(unsafe.Pointer(C.aggregate(cFlattenedSignaturesPtr, cFlattenedSignaturesLen)))
if resPtr == nil {
return nil
}
defer C.destroy_aggregate_response(resPtr)
// prep response
var signature Signature
signatureSlice := C.GoBytes(unsafe.Pointer(&resPtr.signature), SignatureBytes) // nolint: staticcheck
copy(signature[:], signatureSlice)
return &signature
}
// PrivateKeyGenerate generates a private key
func PrivateKeyGenerate() PrivateKey {
// call method
resPtr := (*C.PrivateKeyGenerateResponse)(unsafe.Pointer(C.private_key_generate()))
defer C.destroy_private_key_generate_response(resPtr)
// prep response
var privateKey PrivateKey
privateKeySlice := C.GoBytes(unsafe.Pointer(&resPtr.private_key), PrivateKeyBytes) // nolint: staticcheck
copy(privateKey[:], privateKeySlice)
return privateKey
}
// PrivateKeySign signs a message
func PrivateKeySign(privateKey PrivateKey, message Message) *Signature {
// prep request
cPrivateKey := C.CBytes(privateKey[:])
defer C.free(cPrivateKey)
cPrivateKeyPtr := (*C.uchar)(cPrivateKey)
cMessage := C.CBytes(message)
defer C.free(cMessage)
cMessagePtr := (*C.uchar)(cMessage)
cMessageLen := C.size_t(len(message))
// call method
resPtr := (*C.PrivateKeySignResponse)(unsafe.Pointer(C.private_key_sign(cPrivateKeyPtr, cMessagePtr, cMessageLen)))
if resPtr == nil {
return nil
}
defer C.destroy_private_key_sign_response(resPtr)
// prep response
var signature Signature
signatureSlice := C.GoBytes(unsafe.Pointer(&resPtr.signature), SignatureBytes) // nolint: staticcheck
copy(signature[:], signatureSlice)
return &signature
}
// PrivateKeyPublicKey gets the public key for a private key
func PrivateKeyPublicKey(privateKey PrivateKey) PublicKey {
// prep request
cPrivateKey := C.CBytes(privateKey[:])
defer C.free(cPrivateKey)
cPrivateKeyPtr := (*C.uchar)(cPrivateKey)
// call method
resPtr := (*C.PrivateKeyPublicKeyResponse)(unsafe.Pointer(C.private_key_public_key(cPrivateKeyPtr))) // nolint: staticcheck
defer C.destroy_private_key_public_key_response(resPtr)
// prep response
var publicKey PublicKey
publicKeySlice := C.GoBytes(unsafe.Pointer(&resPtr.public_key), PublicKeyBytes) // nolint: staticcheck
copy(publicKey[:], publicKeySlice)
return publicKey
}