github.com/Finschia/ostracon@v1.1.5/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  	"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
    11  	vrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
    12  
    13  	"github.com/Finschia/ostracon/crypto"
    14  	"github.com/Finschia/ostracon/crypto/tmhash"
    15  	tmjson "github.com/Finschia/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 := vrf.Prove(ed25519.PrivateKey(privKey[:]), message)
    67  	return proof, nil
    68  }
    69  
    70  // PubKey gets the corresponding public key from the private key.
    71  //
    72  // Panics if the private key is not initialized.
    73  func (privKey PrivKey) PubKey() crypto.PubKey {
    74  	// If the latter 32 bytes of the privkey are all zero, privkey is not
    75  	// initialized.
    76  	initialized := false
    77  	for _, v := range privKey[32:] {
    78  		if v != 0 {
    79  			initialized = true
    80  			break
    81  		}
    82  	}
    83  
    84  	if !initialized {
    85  		panic("Expected ed25519 PrivKey to include concatenated pubkey bytes")
    86  	}
    87  
    88  	pubkeyBytes := make([]byte, PubKeySize)
    89  	copy(pubkeyBytes, privKey[32:])
    90  	return PubKey(pubkeyBytes)
    91  }
    92  
    93  // Equals - you probably don't need to use this.
    94  // Runs in constant time based on length of the keys.
    95  func (privKey PrivKey) Equals(other crypto.PrivKey) bool {
    96  	if otherEd, ok := other.(PrivKey); ok {
    97  		return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
    98  	}
    99  
   100  	return false
   101  }
   102  
   103  func (privKey PrivKey) Type() string {
   104  	return KeyType
   105  }
   106  
   107  // GenPrivKey generates a new ed25519 private key.
   108  // It uses OS randomness in conjunction with the current global random seed
   109  // in ostracon/libs/common to generate the private key.
   110  func GenPrivKey() PrivKey {
   111  	return genPrivKey(crypto.CReader())
   112  }
   113  
   114  // genPrivKey generates a new ed25519 private key using the provided reader.
   115  func genPrivKey(rand io.Reader) PrivKey {
   116  	seed := make([]byte, SeedSize)
   117  
   118  	_, err := io.ReadFull(rand, seed)
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  
   123  	return PrivKey(ed25519.NewKeyFromSeed(seed))
   124  }
   125  
   126  // GenPrivKeyFromSecret hashes the secret with SHA2, and uses
   127  // that 32 byte output to create the private key.
   128  // NOTE: secret should be the output of a KDF like bcrypt,
   129  // if it's derived from user input.
   130  func GenPrivKeyFromSecret(secret []byte) PrivKey {
   131  	seed := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes.
   132  
   133  	return PrivKey(ed25519.NewKeyFromSeed(seed))
   134  }
   135  
   136  //-------------------------------------
   137  
   138  var _ crypto.PubKey = PubKey{}
   139  
   140  // PubKeyEd25519 implements crypto.PubKey for the Ed25519 signature scheme.
   141  type PubKey []byte
   142  
   143  // Address is the SHA256-20 of the raw pubkey bytes.
   144  func (pubKey PubKey) Address() crypto.Address {
   145  	if len(pubKey) != PubKeySize {
   146  		panic("pubkey is incorrect size")
   147  	}
   148  	return crypto.Address(tmhash.SumTruncated(pubKey))
   149  }
   150  
   151  // Bytes returns the PubKey byte format.
   152  func (pubKey PubKey) Bytes() []byte {
   153  	return []byte(pubKey)
   154  }
   155  
   156  func (pubKey PubKey) VerifySignature(msg []byte, sig []byte) bool {
   157  	// make sure we use the same algorithm to sign
   158  	if len(sig) != SignatureSize {
   159  		return false
   160  	}
   161  
   162  	return ed25519.Verify(ed25519.PublicKey(pubKey), msg, sig)
   163  }
   164  
   165  func (pubKey PubKey) String() string {
   166  	return fmt.Sprintf("PubKeyEd25519{%X}", []byte(pubKey))
   167  }
   168  
   169  func (pubKey PubKey) Type() string {
   170  	return KeyType
   171  }
   172  
   173  // VRFVerify guarantees that the public key is validated such that the "full uniqueness" and
   174  // "full collision" properties are satisfied.
   175  // The internal function of VRFVerify is implemented based on the IETF draft.
   176  // See sections 3.1 and 3.2 here https://datatracker.ietf.org/doc/draft-irtf-cfrg-vrf/.
   177  func (pubKey PubKey) VRFVerify(proof []byte, message []byte) (crypto.Output, error) {
   178  	isValid, hash := VRFVerify(ed25519.PublicKey(pubKey), proof, message)
   179  	if !isValid {
   180  		return nil, fmt.Errorf("either Public Key or Proof is an invalid value.: err: %s",
   181  			hex.EncodeToString(proof))
   182  	}
   183  	return hash, nil
   184  }
   185  
   186  func (pubKey PubKey) Equals(other crypto.PubKey) bool {
   187  	if otherEd, ok := other.(PubKey); ok {
   188  		return bytes.Equal(pubKey[:], otherEd[:])
   189  	}
   190  
   191  	return false
   192  }