github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/crypto/ed25519/ed25519.go (about)

     1  package ed25519
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/subtle"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"io"
     9  
    10  	"golang.org/x/crypto/ed25519"
    11  
    12  	"github.com/line/ostracon/crypto"
    13  	"github.com/line/ostracon/crypto/tmhash"
    14  	"github.com/line/ostracon/crypto/vrf"
    15  	tmjson "github.com/line/ostracon/libs/json"
    16  )
    17  
    18  //-------------------------------------
    19  
    20  var _ crypto.PrivKey = PrivKey{}
    21  
    22  const (
    23  	PrivKeyName = "tendermint/PrivKeyEd25519"
    24  	PubKeyName  = "tendermint/PubKeyEd25519"
    25  	// PubKeySize is is the size, in bytes, of public keys as used in this package.
    26  	PubKeySize = 32
    27  	// PrivateKeySize is the size, in bytes, of private keys as used in this package.
    28  	PrivateKeySize = 64
    29  	// SignatureSize of an Edwards25519 signature. Namely the size of a compressed
    30  	// Edwards25519 point, and a field element. Both of which are 32 bytes.
    31  	SignatureSize = 64
    32  	// SeedSize is the size, in bytes, of private key seeds. These are the
    33  	// private key representations used by RFC 8032.
    34  	SeedSize = 32
    35  
    36  	KeyType = "ed25519"
    37  )
    38  
    39  func init() {
    40  	tmjson.RegisterType(PubKey{}, PubKeyName)
    41  	tmjson.RegisterType(PrivKey{}, PrivKeyName)
    42  }
    43  
    44  // PrivKey implements crypto.PrivKey.
    45  type PrivKey []byte
    46  
    47  // Bytes returns the privkey byte format.
    48  func (privKey PrivKey) Bytes() []byte {
    49  	return []byte(privKey)
    50  }
    51  
    52  // Sign produces a signature on the provided message.
    53  // This assumes the privkey is wellformed in the golang format.
    54  // The first 32 bytes should be random,
    55  // corresponding to the normal ed25519 private key.
    56  // The latter 32 bytes should be the compressed public key.
    57  // If these conditions aren't met, Sign will panic or produce an
    58  // incorrect signature.
    59  func (privKey PrivKey) Sign(msg []byte) ([]byte, error) {
    60  	signatureBytes := ed25519.Sign(ed25519.PrivateKey(privKey), msg)
    61  	return signatureBytes, nil
    62  }
    63  
    64  // VRFProve generates a VRF Proof for given message to generate a verifiable random.
    65  func (privKey PrivKey) VRFProve(message []byte) (crypto.Proof, error) {
    66  	proof, err := vrf.Prove(privKey[:], message)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return crypto.Proof(proof[:]), nil
    71  }
    72  
    73  // PubKey gets the corresponding public key from the private key.
    74  //
    75  // Panics if the private key is not initialized.
    76  func (privKey PrivKey) PubKey() crypto.PubKey {
    77  	// If the latter 32 bytes of the privkey are all zero, privkey is not
    78  	// initialized.
    79  	initialized := false
    80  	for _, v := range privKey[32:] {
    81  		if v != 0 {
    82  			initialized = true
    83  			break
    84  		}
    85  	}
    86  
    87  	if !initialized {
    88  		panic("Expected ed25519 PrivKey to include concatenated pubkey bytes")
    89  	}
    90  
    91  	pubkeyBytes := make([]byte, PubKeySize)
    92  	copy(pubkeyBytes, privKey[32:])
    93  	return PubKey(pubkeyBytes)
    94  }
    95  
    96  // Equals - you probably don't need to use this.
    97  // Runs in constant time based on length of the keys.
    98  func (privKey PrivKey) Equals(other crypto.PrivKey) bool {
    99  	if otherEd, ok := other.(PrivKey); ok {
   100  		return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
   101  	}
   102  
   103  	return false
   104  }
   105  
   106  func (privKey PrivKey) Type() string {
   107  	return KeyType
   108  }
   109  
   110  // GenPrivKey generates a new ed25519 private key.
   111  // It uses OS randomness in conjunction with the current global random seed
   112  // in ostracon/libs/common to generate the private key.
   113  func GenPrivKey() PrivKey {
   114  	return genPrivKey(crypto.CReader())
   115  }
   116  
   117  // genPrivKey generates a new ed25519 private key using the provided reader.
   118  func genPrivKey(rand io.Reader) PrivKey {
   119  	seed := make([]byte, SeedSize)
   120  
   121  	_, err := io.ReadFull(rand, seed)
   122  	if err != nil {
   123  		panic(err)
   124  	}
   125  
   126  	return PrivKey(ed25519.NewKeyFromSeed(seed))
   127  }
   128  
   129  // GenPrivKeyFromSecret hashes the secret with SHA2, and uses
   130  // that 32 byte output to create the private key.
   131  // NOTE: secret should be the output of a KDF like bcrypt,
   132  // if it's derived from user input.
   133  func GenPrivKeyFromSecret(secret []byte) PrivKey {
   134  	seed := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes.
   135  
   136  	return PrivKey(ed25519.NewKeyFromSeed(seed))
   137  }
   138  
   139  //-------------------------------------
   140  
   141  var _ crypto.PubKey = PubKey{}
   142  
   143  // PubKeyEd25519 implements crypto.PubKey for the Ed25519 signature scheme.
   144  type PubKey []byte
   145  
   146  // Address is the SHA256-20 of the raw pubkey bytes.
   147  func (pubKey PubKey) Address() crypto.Address {
   148  	if len(pubKey) != PubKeySize {
   149  		panic("pubkey is incorrect size")
   150  	}
   151  	return crypto.Address(tmhash.SumTruncated(pubKey))
   152  }
   153  
   154  // Bytes returns the PubKey byte format.
   155  func (pubKey PubKey) Bytes() []byte {
   156  	return []byte(pubKey)
   157  }
   158  
   159  func (pubKey PubKey) VerifySignature(msg []byte, sig []byte) bool {
   160  	// make sure we use the same algorithm to sign
   161  	if len(sig) != SignatureSize {
   162  		return false
   163  	}
   164  
   165  	return ed25519.Verify(ed25519.PublicKey(pubKey), msg, sig)
   166  }
   167  
   168  func (pubKey PubKey) String() string {
   169  	return fmt.Sprintf("PubKeyEd25519{%X}", []byte(pubKey))
   170  }
   171  
   172  func (pubKey PubKey) Type() string {
   173  	return KeyType
   174  }
   175  
   176  // VRFVerify verifies that the given VRF Proof was generated from the message by the owner of this public key.
   177  func (pubKey PubKey) VRFVerify(proof crypto.Proof, message []byte) (crypto.Output, error) {
   178  	valid, err := vrf.Verify(pubKey[:], vrf.Proof(proof), message)
   179  	if err != nil {
   180  		return nil, fmt.Errorf("the specified proof is not a valid ed25519 proof: err: %s", err.Error())
   181  	}
   182  	if !valid {
   183  		return nil, fmt.Errorf("the specified Proof is not generated with this pair-key: %s",
   184  			hex.EncodeToString(proof))
   185  	}
   186  	output, err := vrf.ProofToHash(vrf.Proof(proof))
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	return crypto.Output(output), nil
   191  }
   192  
   193  func (pubKey PubKey) Equals(other crypto.PubKey) bool {
   194  	if otherEd, ok := other.(PubKey); ok {
   195  		return bytes.Equal(pubKey[:], otherEd[:])
   196  	}
   197  
   198  	return false
   199  }