mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-26 07:12:16 +00:00
fixed crypto
This commit is contained in:
parent
e9e130b4ad
commit
55a9031e91
@ -13,11 +13,11 @@ import "unsafe"
|
|||||||
type Verify struct {
|
type Verify struct {
|
||||||
Message []byte
|
Message []byte
|
||||||
PubKey []byte
|
PubKey []byte
|
||||||
Sig []byte
|
Signature []byte
|
||||||
Valid bool
|
Valid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeKeypair(privKey []byte) []byte {
|
func MakePubKey(privKey []byte) []byte {
|
||||||
pubKey := [32]byte{}
|
pubKey := [32]byte{}
|
||||||
C.ed25519_publickey(
|
C.ed25519_publickey(
|
||||||
(*C.uchar)(unsafe.Pointer(&privKey[0])),
|
(*C.uchar)(unsafe.Pointer(&privKey[0])),
|
||||||
@ -26,7 +26,7 @@ func makeKeypair(privKey []byte) []byte {
|
|||||||
return pubKey[:]
|
return pubKey[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func signMessage(message []byte, privKey []byte, pubKey []byte) []byte {
|
func SignMessage(message []byte, privKey []byte, pubKey []byte) []byte {
|
||||||
sig := [64]byte{}
|
sig := [64]byte{}
|
||||||
C.ed25519_sign(
|
C.ed25519_sign(
|
||||||
(*C.uchar)(unsafe.Pointer(&message[0])), (C.size_t)(len(message)),
|
(*C.uchar)(unsafe.Pointer(&message[0])), (C.size_t)(len(message)),
|
||||||
@ -37,7 +37,7 @@ func signMessage(message []byte, privKey []byte, pubKey []byte) []byte {
|
|||||||
return sig[:]
|
return sig[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyBatch(verifys []Verify) bool {
|
func VerifyBatch(verifys []*Verify) bool {
|
||||||
|
|
||||||
count := len(verifys)
|
count := len(verifys)
|
||||||
|
|
||||||
@ -51,14 +51,14 @@ func verifyBatch(verifys []Verify) bool {
|
|||||||
msgs[i] = (*byte)(unsafe.Pointer(&v.Message[0]))
|
msgs[i] = (*byte)(unsafe.Pointer(&v.Message[0]))
|
||||||
lens[i] = (C.size_t)(len(v.Message))
|
lens[i] = (C.size_t)(len(v.Message))
|
||||||
pubs[i] = (*byte)(&v.PubKey[0])
|
pubs[i] = (*byte)(&v.PubKey[0])
|
||||||
sigs[i] = (*byte)(&v.Sig[0])
|
sigs[i] = (*byte)(&v.Signature[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
count_ := (C.size_t)(count)
|
count_ := (C.size_t)(count)
|
||||||
msgs_ := (**C.uchar)(unsafe.Pointer(&msgs[0]))
|
msgs_ := (**C.uchar)(unsafe.Pointer(&msgs[0]))
|
||||||
lens_ := (*C.size_t)(unsafe.Pointer(&lens[0]))
|
lens_ := (*C.size_t)(unsafe.Pointer(&lens[0]))
|
||||||
pubs_ := (**C.uchar)(unsafe.Pointer(&pubs[0]))
|
pubs_ := (**C.uchar)(unsafe.Pointer(&pubs[0]))
|
||||||
sigs_ := (**C.uchar)(unsafe.Pointer(&pubs[0]))
|
sigs_ := (**C.uchar)(unsafe.Pointer(&sigs[0]))
|
||||||
|
|
||||||
res := C.ed25519_sign_open_batch(msgs_, lens_, pubs_, sigs_, count_, &valids[0])
|
res := C.ed25519_sign_open_batch(msgs_, lens_, pubs_, sigs_, count_, &valids[0])
|
||||||
|
|
||||||
|
35
crypto/ed25519_test.go
Normal file
35
crypto/ed25519_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"crypto/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSign(t *testing.T) {
|
||||||
|
privKey := make([]byte, 32)
|
||||||
|
_, err := rand.Read(privKey)
|
||||||
|
if err != nil { t.Fatal(err) }
|
||||||
|
pubKey := MakePubKey(privKey)
|
||||||
|
signature := SignMessage([]byte("hello"), privKey, pubKey)
|
||||||
|
|
||||||
|
v1 := &Verify{
|
||||||
|
Message: []byte("hello"),
|
||||||
|
PubKey: pubKey,
|
||||||
|
Signature: signature,
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := VerifyBatch([]*Verify{v1, v1, v1, v1})
|
||||||
|
if ok != true { t.Fatal("Expected ok == true") }
|
||||||
|
if v1.Valid != true { t.Fatal("Expected v1.Valid to be true") }
|
||||||
|
|
||||||
|
v2 := &Verify{
|
||||||
|
Message: []byte{0x73},
|
||||||
|
PubKey: pubKey,
|
||||||
|
Signature: signature,
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = VerifyBatch([]*Verify{v1, v1, v1, v2})
|
||||||
|
if ok != false { t.Fatal("Expected ok == false") }
|
||||||
|
if v1.Valid != true { t.Fatal("Expected v1.Valid to be true") }
|
||||||
|
if v2.Valid != false { t.Fatal("Expected v2.Valid to be true") }
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user