-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathasymmetric.go
236 lines (177 loc) · 5.53 KB
/
asymmetric.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package libdisco
import (
"bytes"
"crypto/rand"
"encoding/hex"
"errors"
ristretto "github.com/gtank/ristretto255"
"golang.org/x/crypto/curve25519"
)
// The following code defines the X25519, chacha20poly1305, SHA-256 suite.
const (
dhLen = 32 // A constant specifying the size in bytes of public keys and DH outputs. For security reasons, dhLen must be 32 or greater.
skSize = 32 // a secret key is encoded as a 32 byte array.
)
// 4.1. DH functions
// TODO: store the KeyPair's parts in *[32]byte or []byte ?
// KeyPair contains a private and a public part, both of 32-byte.
// It can be generated via the GenerateKeyPair() function.
// The public part can also be extracted via the ExportPublicKey function.
type KeyPair struct {
PrivateKey [32]byte // must stay a [32]byte because of Serialize()
PublicKey [32]byte // must stay a [32]byte because of Serialize()
}
// GenerateKeypair creates a X25519 static keyPair out of a private key. If privateKey is nil the function generates a random key pair.
func GenerateKeypair(privateKey *[32]byte) *KeyPair {
var keyPair KeyPair
if privateKey != nil {
copy(keyPair.PrivateKey[:], privateKey[:])
} else {
if _, err := rand.Read(keyPair.PrivateKey[:]); err != nil {
panic(err)
}
}
curve25519.ScalarBaseMult(&keyPair.PublicKey, &keyPair.PrivateKey)
return &keyPair
}
// ExportPublicKey returns the public part in hex format of a static key pair.
func (kp KeyPair) ExportPublicKey() string {
return hex.EncodeToString(kp.PublicKey[:])
}
func dh(keyPair KeyPair, publicKey [32]byte) (shared [32]byte) {
curve25519.ScalarMult(&shared, &keyPair.PrivateKey, &publicKey)
return
}
// The following code implements the Schnorrkel variant of Schnorr signatures
// over ristretto255.
// This implementation was picked from https://github.com/w3f/schnorrkel
// SigningKeypair uses deterministic Schnorr with strobe
type SigningKeypair struct {
SecretKey ristretto.Scalar
PublicKey ristretto.Element
}
// Signature represents a schnorrkel signature
type Signature struct {
R ristretto.Element
S ristretto.Scalar
}
// Decode a schnorrkel signature from a bytearray.
// ref: https://github.com/w3f/schnorrkel/blob/master/src/sign.rs#L100
func (s *Signature) Decode(sigBytes [64]byte) error {
err := s.R.Decode(sigBytes[:32])
if err != nil {
return err
}
sigBytes[63] &= 127
err = s.S.Decode(sigBytes[32:])
if err != nil {
return err
}
return nil
}
// Encode a signature as a bytearray.
// see: https://github.com/w3f/schnorrkel/blob/master/src/sign.rs#L77
func (s *Signature) Encode() [64]byte {
var sigBytes [64]byte
rBytes := s.R.Encode(nil)
copy(sigBytes[:32], rBytes)
sBytes := s.S.Encode(nil)
copy(sigBytes[32:], sBytes)
sigBytes[63] |= 128
return sigBytes
}
// GenerateSigningKeypair for schnorr signatures.
func GenerateSigningKeypair() (SigningKeypair, error) {
var sigpair SigningKeypair
// Generate a schnorr keypair
var publicKey ristretto.Element
secretKey, err := newRandomScalar()
if err != nil {
return sigpair, err
}
sigpair.PublicKey = *publicKey.ScalarBaseMult(&secretKey)
sigpair.SecretKey = secretKey
return sigpair, nil
}
// ExportPublicKey returns a hexstring encoding of the signing keypair.
func (kp SigningKeypair) ExportPublicKey() string {
return hex.EncodeToString(kp.SecretKey.Encode(nil)) + hex.EncodeToString(kp.PublicKey.Encode(nil))
}
// Sign a message using a deterministic nonce
func (kp SigningKeypair) Sign(message []byte) Signature {
// choose a deterministic nonce
var kBytes [64]byte
// preallocate and avoid append allocations
var buf = make([]byte, 0, skSize+len(message))
buf = append(buf, kp.SecretKey.Encode(nil)...)
buf = append(buf, message...)
// buf ownership passed to reusable buffer
var reusableBuffer = bytes.NewBuffer(buf)
// the output should be 64 byte per ristretto255 rfc
// ref : https://tools.ietf.org/html/draft-hdevalence-cfrg-ristretto-00
copy(kBytes[:], Hash(reusableBuffer.Bytes(), 64))
// cleanup
reusableBuffer.Reset()
var k ristretto.Scalar
var R ristretto.Element
k.FromUniformBytes(kBytes[:])
R.ScalarBaseMult(&k)
var e ristretto.Scalar
var x ristretto.Scalar
var s ristretto.Scalar
// e = H(R||message)
reusableBuffer.Write(R.Encode(nil))
reusableBuffer.Write(message)
h := Hash(reusableBuffer.Bytes(), 64)
e.FromUniformBytes(h)
reusableBuffer.Reset()
// x = sk*e
x.Multiply(&kp.SecretKey, &e)
// s = k + x
s.Add(&k, &x)
sig := Signature{R, s}
return sig
}
// Verify a signature
func (kp SigningKeypair) Verify(message []byte, signature Signature) error {
// Verifying a signature of the form R,s
// Decoding the signature
var R ristretto.Element
var s ristretto.Scalar
R = signature.R
s = signature.S
ev := Hash(append(R.Encode(nil), message...), 64)
var k ristretto.Scalar
k.FromUniformBytes(ev)
var Rp ristretto.Element
Rp.ScalarBaseMult(&s)
var ky ristretto.Element
ky.ScalarMult(&k, &kp.PublicKey)
Rp.Subtract(&Rp, &ky)
if Rp.Equal(&R) != 1 {
return errors.New("failed to verify signature")
}
return nil
}
// newRandomScalar generates a random ristretto scalar using crypto/rand.
func newRandomScalar() (ristretto.Scalar, error) {
var buf [64]byte
var s ristretto.Scalar
n, err := rand.Read(buf[:])
if n != 64 || err != nil {
return s, err
}
s.FromUniformBytes(buf[:])
return s, nil
}
// newRandomElement generates a random ristretto point using crypto/rand.
func newRandomElement() (ristretto.Element, error) {
var buf [64]byte
var P ristretto.Element
n, err := rand.Read(buf[:])
if n != 64 || err != nil {
return P, err
}
P.FromUniformBytes(buf[:])
return P, nil
}