1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build go1.13
6// +build go1.13
7
8// Package ed25519 implements the Ed25519 signature algorithm. See
9// https://ed25519.cr.yp.to/.
10//
11// These functions are also compatible with the “Ed25519” function defined in
12// RFC 8032. However, unlike RFC 8032's formulation, this package's private key
13// representation includes a public key suffix to make multiple signing
14// operations with the same key more efficient. This package refers to the RFC
15// 8032 private key as the “seed”.
16//
17// Beginning with Go 1.13, the functionality of this package was moved to the
18// standard library as crypto/ed25519. This package only acts as a compatibility
19// wrapper.
20package ed25519
21
22import (
23	"crypto/ed25519"
24	"io"
25)
26
27const (
28	// PublicKeySize is the size, in bytes, of public keys as used in this package.
29	PublicKeySize = 32
30	// PrivateKeySize is the size, in bytes, of private keys as used in this package.
31	PrivateKeySize = 64
32	// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
33	SignatureSize = 64
34	// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
35	SeedSize = 32
36)
37
38// PublicKey is the type of Ed25519 public keys.
39//
40// This type is an alias for crypto/ed25519's PublicKey type.
41// See the crypto/ed25519 package for the methods on this type.
42type PublicKey = ed25519.PublicKey
43
44// PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
45//
46// This type is an alias for crypto/ed25519's PrivateKey type.
47// See the crypto/ed25519 package for the methods on this type.
48type PrivateKey = ed25519.PrivateKey
49
50// GenerateKey generates a public/private key pair using entropy from rand.
51// If rand is nil, crypto/rand.Reader will be used.
52func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
53	return ed25519.GenerateKey(rand)
54}
55
56// NewKeyFromSeed calculates a private key from a seed. It will panic if
57// len(seed) is not SeedSize. This function is provided for interoperability
58// with RFC 8032. RFC 8032's private keys correspond to seeds in this
59// package.
60func NewKeyFromSeed(seed []byte) PrivateKey {
61	return ed25519.NewKeyFromSeed(seed)
62}
63
64// Sign signs the message with privateKey and returns a signature. It will
65// panic if len(privateKey) is not PrivateKeySize.
66func Sign(privateKey PrivateKey, message []byte) []byte {
67	return ed25519.Sign(privateKey, message)
68}
69
70// Verify reports whether sig is a valid signature of message by publicKey. It
71// will panic if len(publicKey) is not PublicKeySize.
72func Verify(publicKey PublicKey, message, sig []byte) bool {
73	return ed25519.Verify(publicKey, message, sig)
74}
75