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