...

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

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

     1  // Code generated from mode.templ.go. DO NOT EDIT.
     2  
     3  package dilithium
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  
     9  	"github.com/cloudflare/circl/sign/dilithium/internal/common"
    10  	"github.com/cloudflare/circl/sign/dilithium/mode2"
    11  )
    12  
    13  // implMode2 implements the mode.Mode interface for Dilithium2.
    14  type implMode2 struct{}
    15  
    16  // Mode2 is Dilithium in mode "Dilithium2".
    17  var Mode2 Mode = &implMode2{}
    18  
    19  func (m *implMode2) GenerateKey(rand io.Reader) (
    20  	PublicKey, PrivateKey, error) {
    21  	return mode2.GenerateKey(rand)
    22  }
    23  
    24  func (m *implMode2) NewKeyFromSeed(seed []byte) (PublicKey,
    25  	PrivateKey) {
    26  	if len(seed) != common.SeedSize {
    27  		panic(fmt.Sprintf("seed must be of length %d", common.SeedSize))
    28  	}
    29  	seedBuf := [common.SeedSize]byte{}
    30  	copy(seedBuf[:], seed)
    31  	return mode2.NewKeyFromSeed(&seedBuf)
    32  }
    33  
    34  func (m *implMode2) Sign(sk PrivateKey, msg []byte) []byte {
    35  	isk := sk.(*mode2.PrivateKey)
    36  	ret := [mode2.SignatureSize]byte{}
    37  	mode2.SignTo(isk, msg, ret[:])
    38  	return ret[:]
    39  }
    40  
    41  func (m *implMode2) Verify(pk PublicKey, msg []byte, signature []byte) bool {
    42  	ipk := pk.(*mode2.PublicKey)
    43  	return mode2.Verify(ipk, msg, signature)
    44  }
    45  
    46  func (m *implMode2) PublicKeyFromBytes(data []byte) PublicKey {
    47  	var ret mode2.PublicKey
    48  	if len(data) != mode2.PublicKeySize {
    49  		panic("packed public key must be of mode2.PublicKeySize bytes")
    50  	}
    51  	var buf [mode2.PublicKeySize]byte
    52  	copy(buf[:], data)
    53  	ret.Unpack(&buf)
    54  	return &ret
    55  }
    56  
    57  func (m *implMode2) PrivateKeyFromBytes(data []byte) PrivateKey {
    58  	var ret mode2.PrivateKey
    59  	if len(data) != mode2.PrivateKeySize {
    60  		panic("packed public key must be of mode2.PrivateKeySize bytes")
    61  	}
    62  	var buf [mode2.PrivateKeySize]byte
    63  	copy(buf[:], data)
    64  	ret.Unpack(&buf)
    65  	return &ret
    66  }
    67  
    68  func (m *implMode2) SeedSize() int {
    69  	return common.SeedSize
    70  }
    71  
    72  func (m *implMode2) PublicKeySize() int {
    73  	return mode2.PublicKeySize
    74  }
    75  
    76  func (m *implMode2) PrivateKeySize() int {
    77  	return mode2.PrivateKeySize
    78  }
    79  
    80  func (m *implMode2) SignatureSize() int {
    81  	return mode2.SignatureSize
    82  }
    83  
    84  func (m *implMode2) Name() string {
    85  	return "Dilithium2"
    86  }
    87  
    88  func init() {
    89  	modes["Dilithium2"] = Mode2
    90  }
    91  

View as plain text