You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package crypto
  2. /*
  3. #cgo CFLAGS: -g -m64 -O3 -fPIC -pthread
  4. #cgo LDFLAGS: -lcrypto
  5. #include <stdio.h>
  6. #include "ed25519.h"
  7. */
  8. import "C"
  9. import "unsafe"
  10. type Verify struct {
  11. Message []byte
  12. PubKey []byte
  13. Signature []byte
  14. Valid bool
  15. }
  16. func MakePubKey(privKey []byte) []byte {
  17. pubKey := [32]byte{}
  18. C.ed25519_publickey(
  19. (*C.uchar)(unsafe.Pointer(&privKey[0])),
  20. (*C.uchar)(unsafe.Pointer(&pubKey[0])),
  21. )
  22. return pubKey[:]
  23. }
  24. func SignMessage(message []byte, privKey []byte, pubKey []byte) []byte {
  25. sig := [64]byte{}
  26. C.ed25519_sign(
  27. (*C.uchar)(unsafe.Pointer(&message[0])), (C.size_t)(len(message)),
  28. (*C.uchar)(unsafe.Pointer(&privKey[0])),
  29. (*C.uchar)(unsafe.Pointer(&pubKey[0])),
  30. (*C.uchar)(unsafe.Pointer(&sig[0])),
  31. )
  32. return sig[:]
  33. }
  34. func VerifyBatch(verifys []*Verify) bool {
  35. count := len(verifys)
  36. msgs := make([]*byte, count)
  37. lens := make([]C.size_t, count)
  38. pubs := make([]*byte, count)
  39. sigs := make([]*byte, count)
  40. valids := make([]C.int, count)
  41. for i, v := range verifys {
  42. msgs[i] = (*byte)(unsafe.Pointer(&v.Message[0]))
  43. lens[i] = (C.size_t)(len(v.Message))
  44. pubs[i] = (*byte)(&v.PubKey[0])
  45. sigs[i] = (*byte)(&v.Signature[0])
  46. }
  47. count_ := (C.size_t)(count)
  48. msgs_ := (**C.uchar)(unsafe.Pointer(&msgs[0]))
  49. lens_ := (*C.size_t)(unsafe.Pointer(&lens[0]))
  50. pubs_ := (**C.uchar)(unsafe.Pointer(&pubs[0]))
  51. sigs_ := (**C.uchar)(unsafe.Pointer(&sigs[0]))
  52. res := C.ed25519_sign_open_batch(msgs_, lens_, pubs_, sigs_, count_, &valids[0])
  53. for i, valid := range valids {
  54. verifys[i].Valid = valid > 0
  55. }
  56. return res == 0
  57. }