Browse Source

fixed crypto

pull/9/head
Jae Kwon 11 years ago
parent
commit
55a9031e91
2 changed files with 44 additions and 9 deletions
  1. +9
    -9
      crypto/ed25519.go
  2. +35
    -0
      crypto/ed25519_test.go

+ 9
- 9
crypto/ed25519.go View File

@ -11,13 +11,13 @@ import "C"
import "unsafe"
type Verify struct {
Message []byte
PubKey []byte
Sig []byte
Valid bool
Message []byte
PubKey []byte
Signature []byte
Valid bool
}
func makeKeypair(privKey []byte) []byte {
func MakePubKey(privKey []byte) []byte {
pubKey := [32]byte{}
C.ed25519_publickey(
(*C.uchar)(unsafe.Pointer(&privKey[0])),
@ -26,7 +26,7 @@ func makeKeypair(privKey []byte) []byte {
return pubKey[:]
}
func signMessage(message []byte, privKey []byte, pubKey []byte) []byte {
func SignMessage(message []byte, privKey []byte, pubKey []byte) []byte {
sig := [64]byte{}
C.ed25519_sign(
(*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[:]
}
func verifyBatch(verifys []Verify) bool {
func VerifyBatch(verifys []*Verify) bool {
count := len(verifys)
@ -51,14 +51,14 @@ func verifyBatch(verifys []Verify) bool {
msgs[i] = (*byte)(unsafe.Pointer(&v.Message[0]))
lens[i] = (C.size_t)(len(v.Message))
pubs[i] = (*byte)(&v.PubKey[0])
sigs[i] = (*byte)(&v.Sig[0])
sigs[i] = (*byte)(&v.Signature[0])
}
count_ := (C.size_t)(count)
msgs_ := (**C.uchar)(unsafe.Pointer(&msgs[0]))
lens_ := (*C.size_t)(unsafe.Pointer(&lens[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])


+ 35
- 0
crypto/ed25519_test.go View 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…
Cancel
Save