Squashed 'lib/bls-signatures/' content from commit b9a3dbf

git-subtree-dir: lib/bls-signatures
git-subtree-split: b9a3dbf02ef0d61d072a75d4456aa4afe0adb506
This commit is contained in:
Jakub Sztandera 2019-07-08 12:31:38 +02:00
commit ec306ba24d
4 changed files with 223 additions and 0 deletions

1
bls-signatures Submodule

@ -0,0 +1 @@
Subproject commit c2f28819039ce50f9a25429598a6d179fdaabe20

148
bls.go Normal file
View File

@ -0,0 +1,148 @@
package bls
import (
"unsafe"
)
// #cgo LDFLAGS: -L${SRCDIR}/lib -lbls_signatures
// #cgo pkg-config: ${SRCDIR}/lib/pkgconfig/libbls_signatures.pc
// #include "./include/libbls_signatures.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
resPtr := (*C.VerifyResponse)(unsafe.Pointer(C.verify(cSignaturePtr, cFlattenedDigestsPtr, cFlattenedDigestsLen, cFlattenedPublicKeysPtr, cFlattenedPublicKeysLen)))
defer C.destroy_verify_response(resPtr)
return resPtr.result > 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)))
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)))
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
}

46
bls_test.go Normal file
View File

@ -0,0 +1,46 @@
package bls
import (
"testing"
tf "github.com/filecoin-project/go-filecoin/testhelpers/testflags"
"github.com/stretchr/testify/assert"
)
func TestBLSSigningAndVerification(t *testing.T) {
tf.UnitTest(t)
// generate private keys
fooPrivateKey := PrivateKeyGenerate()
barPrivateKey := PrivateKeyGenerate()
// get the public keys for the private keys
fooPublicKey := PrivateKeyPublicKey(fooPrivateKey)
barPublicKey := PrivateKeyPublicKey(barPrivateKey)
// make messages to sign with the keys
fooMessage := Message("hello foo")
barMessage := Message("hello bar!")
// calculate the digests of the messages
fooDigest := Hash(fooMessage)
barDigest := Hash(barMessage)
// get the signature when signing the messages with the private keys
fooSignature := PrivateKeySign(fooPrivateKey, fooMessage)
barSignature := PrivateKeySign(barPrivateKey, barMessage)
// assert the foo message was signed with the foo key
assert.True(t, Verify(fooSignature, []Digest{fooDigest}, []PublicKey{fooPublicKey}))
// assert the bar message was signed with the bar key
assert.True(t, Verify(barSignature, []Digest{barDigest}, []PublicKey{barPublicKey}))
// assert the foo message was not signed by the bar key
assert.False(t, Verify(fooSignature, []Digest{fooDigest}, []PublicKey{barPublicKey}))
// assert the bar/foo message was not signed by the foo/bar key
assert.False(t, Verify(barSignature, []Digest{barDigest}, []PublicKey{fooPublicKey}))
assert.False(t, Verify(barSignature, []Digest{fooDigest}, []PublicKey{barPublicKey}))
assert.False(t, Verify(fooSignature, []Digest{barDigest}, []PublicKey{fooPublicKey}))
}

28
types.go Normal file
View File

@ -0,0 +1,28 @@
package bls
// SignatureBytes is the length of a BLS signature
const SignatureBytes = 96
// PrivateKeyBytes is the length of a BLS private key
const PrivateKeyBytes = 32
// PublicKeyBytes is the length of a BLS public key
const PublicKeyBytes = 48
// DigestBytes is the length of a BLS message hash/digest
const DigestBytes = 96
// Signature is a compressed affine
type Signature [SignatureBytes]byte
// PrivateKey is a compressed affine
type PrivateKey [PrivateKeyBytes]byte
// PublicKey is a compressed affine
type PublicKey [PublicKeyBytes]byte
// Message is a byte slice
type Message []byte
// Digest is a compressed affine
type Digest [DigestBytes]byte