...
1
2
3
4
5
6
7
8
9
10
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
24 SeedSize = common.SeedSize
25
26
27 PublicKeySize = internal.PublicKeySize
28
29
30 PrivateKeySize = internal.PrivateKeySize
31
32
33 SignatureSize = internal.SignatureSize
34 )
35
36
37 type PublicKey internal.PublicKey
38
39
40 type PrivateKey internal.PrivateKey
41
42
43
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
50 func NewKeyFromSeed(seed *[SeedSize]byte) (*PublicKey, *PrivateKey) {
51 pk, sk := internal.NewKeyFromSeed(seed)
52 return (*PublicKey)(pk), (*PrivateKey)(sk)
53 }
54
55
56
57 func SignTo(sk *PrivateKey, msg []byte, signature []byte) {
58 internal.SignTo(
59 (*internal.PrivateKey)(sk),
60 msg,
61 signature,
62 )
63 }
64
65
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
75 func (pk *PublicKey) Unpack(buf *[PublicKeySize]byte) {
76 (*internal.PublicKey)(pk).Unpack(buf)
77 }
78
79
80 func (sk *PrivateKey) Unpack(buf *[PrivateKeySize]byte) {
81 (*internal.PrivateKey)(sk).Unpack(buf)
82 }
83
84
85 func (pk *PublicKey) Pack(buf *[PublicKeySize]byte) {
86 (*internal.PublicKey)(pk).Pack(buf)
87 }
88
89
90 func (sk *PrivateKey) Pack(buf *[PrivateKeySize]byte) {
91 (*internal.PrivateKey)(sk).Pack(buf)
92 }
93
94
95 func (pk *PublicKey) Bytes() []byte {
96 var buf [PublicKeySize]byte
97 pk.Pack(&buf)
98 return buf[:]
99 }
100
101
102 func (sk *PrivateKey) Bytes() []byte {
103 var buf [PrivateKeySize]byte
104 sk.Pack(&buf)
105 return buf[:]
106 }
107
108
109 func (pk *PublicKey) MarshalBinary() ([]byte, error) {
110 return pk.Bytes(), nil
111 }
112
113
114 func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
115 return sk.Bytes(), nil
116 }
117
118
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
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
141
142
143
144
145
146
147
148
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
162
163
164
165 func (sk *PrivateKey) Public() crypto.PublicKey {
166 return (*PublicKey)((*internal.PrivateKey)(sk).Public())
167 }
168
169
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
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