...

Source file src/github.com/cloudflare/circl/sign/dilithium/mode3aes/dilithium.go

Documentation: github.com/cloudflare/circl/sign/dilithium/mode3aes

     1  // Code generated from modePkg.templ.go. DO NOT EDIT.
     2  
     3  // mode3aes implements the CRYSTALS-Dilithium signature scheme Dilithium3-AES
     4  // as submitted to round3 of the NIST PQC competition and described in
     5  //
     6  // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf
     7  package mode3aes
     8  
     9  import (
    10  	"crypto"
    11  	"errors"
    12  	"io"
    13  
    14  	"github.com/cloudflare/circl/sign/dilithium/internal/common"
    15  	"github.com/cloudflare/circl/sign/dilithium/mode3aes/internal"
    16  )
    17  
    18  const (
    19  	// Size of seed for NewKeyFromSeed
    20  	SeedSize = common.SeedSize
    21  
    22  	// Size of a packed PublicKey
    23  	PublicKeySize = internal.PublicKeySize
    24  
    25  	// Size of a packed PrivateKey
    26  	PrivateKeySize = internal.PrivateKeySize
    27  
    28  	// Size of a signature
    29  	SignatureSize = internal.SignatureSize
    30  )
    31  
    32  // PublicKey is the type of Dilithium3-AES public key
    33  type PublicKey internal.PublicKey
    34  
    35  // PrivateKey is the type of Dilithium3-AES private key
    36  type PrivateKey internal.PrivateKey
    37  
    38  // GenerateKey generates a public/private key pair using entropy from rand.
    39  // If rand is nil, crypto/rand.Reader will be used.
    40  func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) {
    41  	pk, sk, err := internal.GenerateKey(rand)
    42  	return (*PublicKey)(pk), (*PrivateKey)(sk), err
    43  }
    44  
    45  // NewKeyFromSeed derives a public/private key pair using the given seed.
    46  func NewKeyFromSeed(seed *[SeedSize]byte) (*PublicKey, *PrivateKey) {
    47  	pk, sk := internal.NewKeyFromSeed(seed)
    48  	return (*PublicKey)(pk), (*PrivateKey)(sk)
    49  }
    50  
    51  // SignTo signs the given message and writes the signature into signature.
    52  // It will panic if signature is not of length at least SignatureSize.
    53  func SignTo(sk *PrivateKey, msg []byte, signature []byte) {
    54  	internal.SignTo(
    55  		(*internal.PrivateKey)(sk),
    56  		msg,
    57  		signature,
    58  	)
    59  }
    60  
    61  // Verify checks whether the given signature by pk on msg is valid.
    62  func Verify(pk *PublicKey, msg []byte, signature []byte) bool {
    63  	return internal.Verify(
    64  		(*internal.PublicKey)(pk),
    65  		msg,
    66  		signature,
    67  	)
    68  }
    69  
    70  // Sets pk to the public key encoded in buf.
    71  func (pk *PublicKey) Unpack(buf *[PublicKeySize]byte) {
    72  	(*internal.PublicKey)(pk).Unpack(buf)
    73  }
    74  
    75  // Sets sk to the private key encoded in buf.
    76  func (sk *PrivateKey) Unpack(buf *[PrivateKeySize]byte) {
    77  	(*internal.PrivateKey)(sk).Unpack(buf)
    78  }
    79  
    80  // Packs the public key into buf.
    81  func (pk *PublicKey) Pack(buf *[PublicKeySize]byte) {
    82  	(*internal.PublicKey)(pk).Pack(buf)
    83  }
    84  
    85  // Packs the private key into buf.
    86  func (sk *PrivateKey) Pack(buf *[PrivateKeySize]byte) {
    87  	(*internal.PrivateKey)(sk).Pack(buf)
    88  }
    89  
    90  // Packs the public key.
    91  func (pk *PublicKey) Bytes() []byte {
    92  	var buf [PublicKeySize]byte
    93  	pk.Pack(&buf)
    94  	return buf[:]
    95  }
    96  
    97  // Packs the private key.
    98  func (sk *PrivateKey) Bytes() []byte {
    99  	var buf [PrivateKeySize]byte
   100  	sk.Pack(&buf)
   101  	return buf[:]
   102  }
   103  
   104  // Packs the public key.
   105  func (pk *PublicKey) MarshalBinary() ([]byte, error) {
   106  	return pk.Bytes(), nil
   107  }
   108  
   109  // Packs the private key.
   110  func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
   111  	return sk.Bytes(), nil
   112  }
   113  
   114  // Unpacks the public key from data.
   115  func (pk *PublicKey) UnmarshalBinary(data []byte) error {
   116  	if len(data) != PublicKeySize {
   117  		return errors.New("packed public key must be of mode3aes.PublicKeySize bytes")
   118  	}
   119  	var buf [PublicKeySize]byte
   120  	copy(buf[:], data)
   121  	pk.Unpack(&buf)
   122  	return nil
   123  }
   124  
   125  // Unpacks the private key from data.
   126  func (sk *PrivateKey) UnmarshalBinary(data []byte) error {
   127  	if len(data) != PrivateKeySize {
   128  		return errors.New("packed private key must be of mode3aes.PrivateKeySize bytes")
   129  	}
   130  	var buf [PrivateKeySize]byte
   131  	copy(buf[:], data)
   132  	sk.Unpack(&buf)
   133  	return nil
   134  }
   135  
   136  // Sign signs the given message.
   137  //
   138  // opts.HashFunc() must return zero, which can be achieved by passing
   139  // crypto.Hash(0) for opts.  rand is ignored.  Will only return an error
   140  // if opts.HashFunc() is non-zero.
   141  //
   142  // This function is used to make PrivateKey implement the crypto.Signer
   143  // interface.  The package-level SignTo function might be more convenient
   144  // to use.
   145  func (sk *PrivateKey) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) (
   146  	signature []byte, err error) {
   147  	var sig [SignatureSize]byte
   148  
   149  	if opts.HashFunc() != crypto.Hash(0) {
   150  		return nil, errors.New("dilithium: cannot sign hashed message")
   151  	}
   152  
   153  	SignTo(sk, msg, sig[:])
   154  	return sig[:], nil
   155  }
   156  
   157  // Computes the public key corresponding to this private key.
   158  //
   159  // Returns a *PublicKey.  The type crypto.PublicKey is used to make
   160  // PrivateKey implement the crypto.Signer interface.
   161  func (sk *PrivateKey) Public() crypto.PublicKey {
   162  	return (*PublicKey)((*internal.PrivateKey)(sk).Public())
   163  }
   164  
   165  // Equal returns whether the two private keys equal.
   166  func (sk *PrivateKey) Equal(other crypto.PrivateKey) bool {
   167  	castOther, ok := other.(*PrivateKey)
   168  	if !ok {
   169  		return false
   170  	}
   171  	return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(castOther))
   172  }
   173  
   174  // Equal returns whether the two public keys equal.
   175  func (pk *PublicKey) Equal(other crypto.PublicKey) bool {
   176  	castOther, ok := other.(*PublicKey)
   177  	if !ok {
   178  		return false
   179  	}
   180  	return (*internal.PublicKey)(pk).Equal((*internal.PublicKey)(castOther))
   181  }
   182  

View as plain text