...

Source file src/github.com/cloudflare/circl/sign/dilithium/templates/modePkg.templ.go

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

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

View as plain text