Skip to content

Commit 16210ac

Browse files
committed
Update to final FIPS 204 standard.
Remove old mode API and hook Dilithium and ML-DSA into generic signature API. Expose support for randomized ML-DSA mode. (It is not exposed in the generic signature API.) Remove old AES variants of Dilithium. Does not add the HashML-DSA variants (yet).
1 parent 72ac15a commit 16210ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1861
-9836
lines changed

sign/dilithium/dilithium.go

-84
Original file line numberDiff line numberDiff line change
@@ -25,87 +25,3 @@
2525
// Dilithium3 with Ed448. These packages are a drop in replacements for the
2626
// mode subpackages of this package.
2727
package dilithium
28-
29-
import (
30-
"crypto"
31-
"io"
32-
)
33-
34-
// PublicKey is a Dilithium public key.
35-
//
36-
// The structure contains values precomputed during unpacking/key generation
37-
// and is therefore significantly larger than a packed public key.
38-
type PublicKey interface {
39-
// Packs public key
40-
Bytes() []byte
41-
}
42-
43-
// PrivateKey is a Dilithium private key.
44-
//
45-
// The structure contains values precomputed during unpacking/key generation
46-
// and is therefore significantly larger than a packed private key.
47-
type PrivateKey interface {
48-
// Packs private key
49-
Bytes() []byte
50-
51-
crypto.Signer
52-
}
53-
54-
// Mode is a certain configuration of the Dilithium signature scheme.
55-
type Mode interface {
56-
// GenerateKey generates a public/private key pair using entropy from rand.
57-
// If rand is nil, crypto/rand.Reader will be used.
58-
GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error)
59-
60-
// NewKeyFromSeed derives a public/private key pair using the given seed.
61-
// Panics if len(seed) != SeedSize()
62-
NewKeyFromSeed(seed []byte) (PublicKey, PrivateKey)
63-
64-
// Sign signs the given message and returns the signature.
65-
// It will panic if sk has not been generated for this mode.
66-
Sign(sk PrivateKey, msg []byte) []byte
67-
68-
// Verify checks whether the given signature by pk on msg is valid.
69-
// It will panic if pk is of the wrong mode.
70-
Verify(pk PublicKey, msg []byte, signature []byte) bool
71-
72-
// Unpacks a public key. Panics if the buffer is not of PublicKeySize()
73-
// length. Precomputes values to speed up subsequent calls to Verify.
74-
PublicKeyFromBytes([]byte) PublicKey
75-
76-
// Unpacks a private key. Panics if the buffer is not
77-
// of PrivateKeySize() length. Precomputes values to speed up subsequent
78-
// calls to Sign(To).
79-
PrivateKeyFromBytes([]byte) PrivateKey
80-
81-
// SeedSize returns the size of the seed for NewKeyFromSeed
82-
SeedSize() int
83-
84-
// PublicKeySize returns the size of a packed PublicKey
85-
PublicKeySize() int
86-
87-
// PrivateKeySize returns the size of a packed PrivateKey
88-
PrivateKeySize() int
89-
90-
// SignatureSize returns the size of a signature
91-
SignatureSize() int
92-
93-
// Name returns the name of this mode
94-
Name() string
95-
}
96-
97-
var modes = make(map[string]Mode)
98-
99-
// ModeNames returns the list of supported modes.
100-
func ModeNames() []string {
101-
names := []string{}
102-
for name := range modes {
103-
names = append(names, name)
104-
}
105-
return names
106-
}
107-
108-
// ModeByName returns the mode with the given name or nil when not supported.
109-
func ModeByName(name string) Mode {
110-
return modes[name]
111-
}

sign/dilithium/dilithium_test.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/hex"
55
"testing"
66

7+
"github.com/cloudflare/circl/sign/schemes"
8+
79
"github.com/cloudflare/circl/internal/sha3"
810
)
911

@@ -34,29 +36,26 @@ func TestNewKeyFromSeed(t *testing.T) {
3436
"Dilithium5", "3956d812a7961af6e5dad16af15c736c",
3537
"665388291aa01e12e7f94bdc7769db18",
3638
},
37-
{
38-
"Dilithium2-AES", "8466a752b0a09e63e42f66d3174a6471",
39-
"c3f8e705a0d8dfd489b98b205670f393",
40-
},
41-
{
42-
"Dilithium3-AES", "2bb713ba7cb15f3ebf05c4c1fbb1b03c",
43-
"eb2bd8d98630835a3b18594ac436368b",
44-
},
45-
{
46-
"Dilithium5-AES", "a613a08b564ee8717ba4f5ccfddc2693",
47-
"2f541bf6fedd12854d06a6b80090932a",
48-
},
4939
} {
5040
t.Run(tc.name, func(t *testing.T) {
51-
mode := ModeByName(tc.name)
41+
mode := schemes.ByName(tc.name)
5242
if mode == nil {
5343
t.Fatal()
5444
}
5545
var seed [32]byte
56-
pk, sk := mode.NewKeyFromSeed(seed[:])
46+
pk, sk := mode.DeriveKey(seed[:])
47+
48+
ppk, err := pk.MarshalBinary()
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
psk, err := sk.MarshalBinary()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
5756

58-
pkh := hexHash(pk.Bytes())
59-
skh := hexHash(sk.Bytes())
57+
pkh := hexHash(ppk)
58+
skh := hexHash(psk)
6059
if pkh != tc.epk {
6160
t.Fatalf("%s expected pk %s, got %s", tc.name, tc.epk, pkh)
6261
}

sign/dilithium/example_test.go

-56
This file was deleted.

sign/dilithium/gen.go

+9-76
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ func (m Mode) Mode() string {
5252
return strings.ReplaceAll(m.Name, "-", "")
5353
}
5454

55-
return strings.ReplaceAll(strings.ReplaceAll(m.Name,
56-
"Dilithium", "Mode"), "-AES", "AES")
57-
}
58-
59-
func (m Mode) UseAES() bool {
60-
return strings.HasSuffix(m.Name, "-AES")
55+
return strings.ReplaceAll(m.Name, "Dilithium", "Mode")
6156
}
6257

6358
func (m Mode) NIST() bool {
@@ -79,19 +74,6 @@ var (
7974
TRSize: 32,
8075
CTildeSize: 32,
8176
},
82-
{
83-
Name: "Dilithium2-AES",
84-
K: 4,
85-
L: 4,
86-
Eta: 2,
87-
DoubleEtaBits: 3,
88-
Omega: 80,
89-
Tau: 39,
90-
Gamma1Bits: 17,
91-
Gamma2: (params.Q - 1) / 88,
92-
TRSize: 32,
93-
CTildeSize: 32,
94-
},
9577
{
9678
Name: "Dilithium3",
9779
K: 6,
@@ -105,19 +87,6 @@ var (
10587
TRSize: 32,
10688
CTildeSize: 32,
10789
},
108-
{
109-
Name: "Dilithium3-AES",
110-
K: 6,
111-
L: 5,
112-
Eta: 4,
113-
DoubleEtaBits: 4,
114-
Omega: 55,
115-
Tau: 49,
116-
Gamma1Bits: 19,
117-
Gamma2: (params.Q - 1) / 32,
118-
TRSize: 32,
119-
CTildeSize: 32,
120-
},
12190
{
12291
Name: "Dilithium5",
12392
K: 8,
@@ -131,19 +100,6 @@ var (
131100
TRSize: 32,
132101
CTildeSize: 32,
133102
},
134-
{
135-
Name: "Dilithium5-AES",
136-
K: 8,
137-
L: 7,
138-
Eta: 2,
139-
DoubleEtaBits: 3,
140-
Omega: 75,
141-
Tau: 60,
142-
Gamma1Bits: 19,
143-
Gamma2: (params.Q - 1) / 32,
144-
TRSize: 32,
145-
CTildeSize: 32,
146-
},
147103
{
148104
Name: "ML-DSA-44",
149105
K: 4,
@@ -189,7 +145,6 @@ var (
189145

190146
func main() {
191147
generateModePackageFiles()
192-
generateModeToplevelFiles()
193148
generateParamsFiles()
194149
generateSourceFiles()
195150
}
@@ -227,9 +182,9 @@ func generateParamsFiles() {
227182
}
228183
}
229184

230-
// Generates modeX.go from templates/mode.templ.go
231-
func generateModeToplevelFiles() {
232-
tl, err := template.ParseFiles("templates/mode.templ.go")
185+
// Generates modeX/dilithium.go from templates/pkg.templ.go
186+
func generateModePackageFiles() {
187+
tl, err := template.ParseFiles("templates/pkg.templ.go")
233188
if err != nil {
234189
panic(err)
235190
}
@@ -241,38 +196,16 @@ func generateModeToplevelFiles() {
241196
panic(err)
242197
}
243198

244-
res := string(buf.Bytes())
245-
offset := strings.Index(res, TemplateWarning)
246-
if offset == -1 {
247-
panic("Missing template warning in mode.templ.go")
248-
}
249-
err = os.WriteFile(mode.Pkg()+".go", []byte(res[offset:]), 0o644)
199+
res, err := format.Source(buf.Bytes())
250200
if err != nil {
251-
panic(err)
252-
}
253-
}
254-
}
255-
256-
// Generates modeX/dilithium.go from templates/modePkg.templ.go
257-
func generateModePackageFiles() {
258-
tl, err := template.ParseFiles("templates/modePkg.templ.go")
259-
if err != nil {
260-
panic(err)
261-
}
262-
263-
for _, mode := range Modes {
264-
buf := new(bytes.Buffer)
265-
err := tl.Execute(buf, mode)
266-
if err != nil {
267-
panic(err)
201+
panic("error formating code")
268202
}
269203

270-
res := string(buf.Bytes())
271-
offset := strings.Index(res, TemplateWarning)
204+
offset := strings.Index(string(res), TemplateWarning)
272205
if offset == -1 {
273-
panic("Missing template warning in modePkg.templ.go")
206+
panic("Missing template warning in pkg.templ.go")
274207
}
275-
err = os.WriteFile(mode.PkgPath()+"/dilithium.go", []byte(res[offset:]), 0o644)
208+
err = os.WriteFile(mode.PkgPath()+"/dilithium.go", res[offset:], 0o644)
276209
if err != nil {
277210
panic(err)
278211
}

0 commit comments

Comments
 (0)