-
Notifications
You must be signed in to change notification settings - Fork 168
/
schnorr.go
183 lines (159 loc) · 4.9 KB
/
schnorr.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Package schnorr implements the vanilla Schnorr signature scheme.
See https://en.wikipedia.org/wiki/Schnorr_signature.
The only difference regarding the vanilla reference is the computation of the
response. This implementation adds the random component with the challenge times
private key while the Wikipedia article substracts them.
The resulting signature is compatible with EdDSA verification algorithm when
using the edwards25519 group, and by extension the CoSi verification algorithm.
*/
package schnorr
import (
"bytes"
"crypto/cipher"
"crypto/sha512"
"errors"
"fmt"
"go.dedis.ch/kyber/v4"
"go.dedis.ch/kyber/v4/sign"
)
// Suite represents the set of functionalities needed by the package schnorr.
type Suite interface {
kyber.Group
kyber.Random
}
type Scheme struct {
s Suite
}
func NewScheme(s Suite) sign.Scheme {
return &Scheme{s}
}
func (s *Scheme) NewKeyPair(random cipher.Stream) (kyber.Scalar, kyber.Point) {
priv := s.s.Scalar().Pick(random)
pub := s.s.Point().Mul(priv, nil)
return priv, pub
}
func (s *Scheme) Sign(private kyber.Scalar, msg []byte) ([]byte, error) {
return Sign(s.s, private, msg)
}
func (s *Scheme) Verify(public kyber.Point, msg, sig []byte) error {
return Verify(s.s, public, msg, sig)
}
// Sign creates a Sign signature from a msg and a private key. This
// signature can be verified with VerifySchnorr. It's also a valid EdDSA
// signature when using the edwards25519 Group.
func Sign(s Suite, private kyber.Scalar, msg []byte) ([]byte, error) {
var g kyber.Group = s
// create random secret k and public point commitment R
k := g.Scalar().Pick(s.RandomStream())
R := g.Point().Mul(k, nil)
// create hash(public || R || message)
public := g.Point().Mul(private, nil)
h, err := hash(g, public, R, msg)
if err != nil {
return nil, err
}
// compute response s = k + x*h
xh := g.Scalar().Mul(private, h)
S := g.Scalar().Add(k, xh)
// return R || s
var b bytes.Buffer
if _, err := R.MarshalTo(&b); err != nil {
return nil, err
}
if _, err := S.MarshalTo(&b); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// VerifyWithChecks uses a public key buffer, a message and a signature.
// It will return nil if sig is a valid signature for msg created by
// key public, or an error otherwise. Compared to `Verify`, it performs
// additional checks around the canonicality and ensures the public key
// does not have a small order when using `edwards25519` group.
func VerifyWithChecks(g kyber.Group, pub, msg, sig []byte) error {
type scalarCanCheckCanonical interface {
IsCanonical(b []byte) bool
}
type pointCanCheckCanonicalAndSmallOrder interface {
HasSmallOrder() bool
IsCanonical(b []byte) bool
}
R := g.Point()
s := g.Scalar()
pointSize := R.MarshalSize()
scalarSize := s.MarshalSize()
sigSize := scalarSize + pointSize
if len(sig) != sigSize {
return fmt.Errorf("schnorr: signature of invalid length %d instead of %d", len(sig), sigSize)
}
if err := R.UnmarshalBinary(sig[:pointSize]); err != nil {
return err
}
if p, ok := R.(pointCanCheckCanonicalAndSmallOrder); ok {
if !p.IsCanonical(sig[:pointSize]) {
return fmt.Errorf("point R is not canonical")
}
if p.HasSmallOrder() {
return fmt.Errorf("point R has small order")
}
}
if s, ok := g.Scalar().(scalarCanCheckCanonical); ok && !s.IsCanonical(sig[pointSize:]) {
return fmt.Errorf("signature is not canonical")
}
if sub, ok := R.(kyber.SubGroupElement); ok && !sub.IsInCorrectGroup() {
return fmt.Errorf("schnorr: point not in correct group")
}
if err := s.UnmarshalBinary(sig[pointSize:]); err != nil {
return err
}
public := g.Point()
err := public.UnmarshalBinary(pub)
if err != nil {
return fmt.Errorf("schnorr: error unmarshalling public key")
}
if p, ok := public.(pointCanCheckCanonicalAndSmallOrder); ok {
if !p.IsCanonical(pub) {
return fmt.Errorf("public key is not canonical")
}
if p.HasSmallOrder() {
return fmt.Errorf("public key has small order")
}
}
// recompute hash(public || R || msg)
h, err := hash(g, public, R, msg)
if err != nil {
return err
}
// compute S = g^s
S := g.Point().Mul(s, nil)
// compute RAh = R + A^h
Ah := g.Point().Mul(h, public)
RAs := g.Point().Add(R, Ah)
if !S.Equal(RAs) {
return errors.New("schnorr: invalid signature")
}
return nil
}
// Verify verifies a given Schnorr signature. It returns nil iff the
// given signature is valid.
func Verify(g kyber.Group, public kyber.Point, msg, sig []byte) error {
PBuf, err := public.MarshalBinary()
if err != nil {
return fmt.Errorf("error unmarshalling public key: %w", err)
}
return VerifyWithChecks(g, PBuf, msg, sig)
}
func hash(g kyber.Group, public, r kyber.Point, msg []byte) (kyber.Scalar, error) {
h := sha512.New()
if _, err := r.MarshalTo(h); err != nil {
return nil, err
}
if _, err := public.MarshalTo(h); err != nil {
return nil, err
}
if _, err := h.Write(msg); err != nil {
return nil, err
}
return g.Scalar().SetBytes(h.Sum(nil)), nil
}