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.

99 lines
2.6 KiB

  1. package sr25519
  2. import (
  3. "crypto/subtle"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/tendermint/crypto"
  7. schnorrkel "github.com/ChainSafe/go-schnorrkel"
  8. )
  9. // PrivKeySr25519Size is the number of bytes in an Sr25519 private key.
  10. const PrivKeySr25519Size = 32
  11. // PrivKeySr25519 implements crypto.PrivKey.
  12. type PrivKeySr25519 [PrivKeySr25519Size]byte
  13. // Bytes marshals the privkey using amino encoding.
  14. func (privKey PrivKeySr25519) Bytes() []byte {
  15. return cdc.MustMarshalBinaryBare(privKey)
  16. }
  17. // Sign produces a signature on the provided message.
  18. func (privKey PrivKeySr25519) Sign(msg []byte) ([]byte, error) {
  19. secretKey := &(schnorrkel.SecretKey{})
  20. err := secretKey.Decode(privKey)
  21. if err != nil {
  22. return []byte{}, err
  23. }
  24. signingContext := schnorrkel.NewSigningContext([]byte{}, msg)
  25. sig, err := secretKey.Sign(signingContext)
  26. if err != nil {
  27. return []byte{}, err
  28. }
  29. sigBytes := sig.Encode()
  30. return sigBytes[:], nil
  31. }
  32. // PubKey gets the corresponding public key from the private key.
  33. func (privKey PrivKeySr25519) PubKey() crypto.PubKey {
  34. secretKey := &(schnorrkel.SecretKey{})
  35. err := secretKey.Decode(privKey)
  36. if err != nil {
  37. panic(fmt.Sprintf("Invalid private key: %v", err))
  38. }
  39. pubkey, _ := secretKey.Public()
  40. return PubKeySr25519(pubkey.Encode())
  41. }
  42. // Equals - you probably don't need to use this.
  43. // Runs in constant time based on length of the keys.
  44. func (privKey PrivKeySr25519) Equals(other crypto.PrivKey) bool {
  45. if otherEd, ok := other.(PrivKeySr25519); ok {
  46. return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
  47. } else {
  48. return false
  49. }
  50. }
  51. // GenPrivKey generates a new sr25519 private key.
  52. // It uses OS randomness in conjunction with the current global random seed
  53. // in tendermint/libs/common to generate the private key.
  54. func GenPrivKey() PrivKeySr25519 {
  55. return genPrivKey(crypto.CReader())
  56. }
  57. // genPrivKey generates a new sr25519 private key using the provided reader.
  58. func genPrivKey(rand io.Reader) PrivKeySr25519 {
  59. var seed [64]byte
  60. out := make([]byte, 64)
  61. _, err := io.ReadFull(rand, out)
  62. if err != nil {
  63. panic(err)
  64. }
  65. copy(seed[:], out)
  66. return schnorrkel.NewMiniSecretKey(seed).ExpandEd25519().Encode()
  67. }
  68. // GenPrivKeyFromSecret hashes the secret with SHA2, and uses
  69. // that 32 byte output to create the private key.
  70. // NOTE: secret should be the output of a KDF like bcrypt,
  71. // if it's derived from user input.
  72. func GenPrivKeyFromSecret(secret []byte) PrivKeySr25519 {
  73. seed := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  74. var bz [PrivKeySr25519Size]byte
  75. copy(bz[:], seed)
  76. privKey, _ := schnorrkel.NewMiniSecretKeyFromRaw(bz)
  77. return privKey.ExpandEd25519().Encode()
  78. }