-
Notifications
You must be signed in to change notification settings - Fork 428
/
Copy patheddsa.go
271 lines (251 loc) · 8.08 KB
/
eddsa.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package eddsa
import (
"errors"
"github.com/consensys/gnark/logger"
"github.com/consensys/gnark/std/hash"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/algebra/native/twistededwards"
tedwards "github.com/consensys/gnark-crypto/ecc/twistededwards"
edwardsbls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/twistededwards"
edwardsbls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/twistededwards"
edwardsbls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315/twistededwards"
edwardsbls24317 "github.com/consensys/gnark-crypto/ecc/bls24-317/twistededwards"
edwardsbn254 "github.com/consensys/gnark-crypto/ecc/bn254/twistededwards"
edwardsbw6633 "github.com/consensys/gnark-crypto/ecc/bw6-633/twistededwards"
edwardsbw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/twistededwards"
)
// PublicKey stores an eddsa public key (to be used in gnark circuit)
type PublicKey struct {
A twistededwards.Point
}
// Signature stores a signature (to be used in gnark circuit)
// An EdDSA signature is a tuple (R,S) where R is a point on the twisted Edwards curve
// and S a scalar. Since the base field of the twisted Edwards is Fr, the number of points
// N on the Edwards is < r+1+2sqrt(r)+2 (since the curve has 2 points of multiplicity 2).
// The subgroup l used in eddsa is <1/2N, so the reduction
// mod l ensures S < r, therefore there is no risk of overflow.
type Signature struct {
R twistededwards.Point
S frontend.Variable
}
// Verify checks that an eddsa signature verifies for the message msg and
// public key pk provided using MiMC hash function.
// cf https://en.wikipedia.org/wiki/EdDSA
func Verify(curve twistededwards.Curve, sig Signature, msg frontend.Variable, pubKey PublicKey, hash hash.FieldHasher) error {
isValid, err := SignIsValid(curve, sig, msg, pubKey, hash)
if err != nil {
return err
}
curve.API().AssertIsEqual(isValid, 1)
return nil
}
// SignIsValid returns 1 if the signature sig verifies an eddsa signature
// using MiMC hash function for the message msg and public key pk or 0 if not.
// cf https://en.wikipedia.org/wiki/EdDSA
func SignIsValid(curve twistededwards.Curve, sig Signature, msg frontend.Variable, pubKey PublicKey, hash hash.FieldHasher) (frontend.Variable, error) {
// compute H(R, A, M)
hash.Write(sig.R.X)
hash.Write(sig.R.Y)
hash.Write(pubKey.A.X)
hash.Write(pubKey.A.Y)
hash.Write(msg)
hRAM := hash.Sum()
base := twistededwards.Point{
X: curve.Params().Base[0],
Y: curve.Params().Base[1],
}
//[S]G-[H(R,A,M)]*A
_A := curve.Neg(pubKey.A)
Q := curve.DoubleBaseScalarMul(base, _A, sig.S, hRAM)
// check if Q is on the curve, if not multiply by 0
isOnCurve := curve.IsOnCurve(Q)
Q = curve.ScalarMul(Q, isOnCurve)
//[S]G-[H(R,A,M)]*A-R
Q = curve.Add(curve.Neg(Q), sig.R)
// [cofactor]*(lhs-rhs)
log := logger.Logger()
if !curve.Params().Cofactor.IsUint64() {
err := errors.New("invalid cofactor")
log.Err(err).Str("cofactor", curve.Params().Cofactor.String()).Send()
return nil, err
}
cofactor := curve.Params().Cofactor.Uint64()
switch cofactor {
case 4:
Q = curve.Double(curve.Double(Q))
case 8:
Q = curve.Double(curve.Double(curve.Double(Q)))
default:
log.Warn().Str("cofactor", curve.Params().Cofactor.String()).Msg("curve cofactor is not implemented")
}
zeroX := curve.API().IsZero(Q.X)
oneY := curve.API().IsZero(curve.API().Sub(Q.Y, 1))
expectedPoint := curve.API().And(zeroX, oneY)
return curve.API().And(isOnCurve, expectedPoint), nil
}
// Assign is a helper to assigned a compressed binary public key representation into its uncompressed form
func (p *PublicKey) Assign(curveID tedwards.ID, buf []byte) {
ax, ay, err := parsePoint(curveID, buf)
if err != nil {
panic(err)
}
p.A.X = ax
p.A.Y = ay
}
// Assign is a helper to assigned a compressed binary signature representation into its uncompressed form
func (s *Signature) Assign(curveID tedwards.ID, buf []byte) {
rx, ry, S, err := parseSignature(curveID, buf)
if err != nil {
panic(err)
}
s.R.X = rx
s.R.Y = ry
s.S = S
}
// parseSignature parses a compressed binary signature into uncompressed R.X, R.Y and S
func parseSignature(curveID tedwards.ID, buf []byte) ([]byte, []byte, []byte, error) {
var pointbn254 edwardsbn254.PointAffine
var pointbls12381 edwardsbls12381.PointAffine
var pointbls12377 edwardsbls12377.PointAffine
var pointbw6761 edwardsbw6761.PointAffine
var pointbls24315 edwardsbls24315.PointAffine
var pointbls24317 edwardsbls24317.PointAffine
var pointbw6633 edwardsbw6633.PointAffine
switch curveID {
case tedwards.BN254:
if _, err := pointbn254.SetBytes(buf[:32]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[32:]
return a, b, s, nil
case tedwards.BLS12_381:
if _, err := pointbls12381.SetBytes(buf[:32]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[32:]
return a, b, s, nil
case tedwards.BLS12_377:
if _, err := pointbls12377.SetBytes(buf[:32]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[32:]
return a, b, s, nil
case tedwards.BW6_761:
if _, err := pointbw6761.SetBytes(buf[:48]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[48:]
return a, b, s, nil
case tedwards.BLS24_317:
if _, err := pointbls24317.SetBytes(buf[:32]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[32:]
return a, b, s, nil
case tedwards.BLS24_315:
if _, err := pointbls24315.SetBytes(buf[:32]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[32:]
return a, b, s, nil
case tedwards.BW6_633:
if _, err := pointbw6633.SetBytes(buf[:40]); err != nil {
return nil, nil, nil, err
}
a, b, err := parsePoint(curveID, buf)
if err != nil {
return nil, nil, nil, err
}
s := buf[40:]
return a, b, s, nil
default:
panic("not implemented")
}
}
// parsePoint parses a compressed binary point into uncompressed P.X and P.Y
func parsePoint(curveID tedwards.ID, buf []byte) ([]byte, []byte, error) {
var pointbn254 edwardsbn254.PointAffine
var pointbls12381 edwardsbls12381.PointAffine
var pointbls12377 edwardsbls12377.PointAffine
var pointbw6761 edwardsbw6761.PointAffine
var pointbls24315 edwardsbls24315.PointAffine
var pointbls24317 edwardsbls24317.PointAffine
var pointbw6633 edwardsbw6633.PointAffine
switch curveID {
case tedwards.BN254:
if _, err := pointbn254.SetBytes(buf[:32]); err != nil {
return nil, nil, err
}
a := pointbn254.X.Bytes()
b := pointbn254.Y.Bytes()
return a[:], b[:], nil
case tedwards.BLS12_381:
if _, err := pointbls12381.SetBytes(buf[:32]); err != nil {
return nil, nil, err
}
a := pointbls12381.X.Bytes()
b := pointbls12381.Y.Bytes()
return a[:], b[:], nil
case tedwards.BLS12_377:
if _, err := pointbls12377.SetBytes(buf[:32]); err != nil {
return nil, nil, err
}
a := pointbls12377.X.Bytes()
b := pointbls12377.Y.Bytes()
return a[:], b[:], nil
case tedwards.BW6_761:
if _, err := pointbw6761.SetBytes(buf[:48]); err != nil {
return nil, nil, err
}
a := pointbw6761.X.Bytes()
b := pointbw6761.Y.Bytes()
return a[:], b[:], nil
case tedwards.BLS24_317:
if _, err := pointbls24317.SetBytes(buf[:32]); err != nil {
return nil, nil, err
}
a := pointbls24317.X.Bytes()
b := pointbls24317.Y.Bytes()
return a[:], b[:], nil
case tedwards.BLS24_315:
if _, err := pointbls24315.SetBytes(buf[:32]); err != nil {
return nil, nil, err
}
a := pointbls24315.X.Bytes()
b := pointbls24315.Y.Bytes()
return a[:], b[:], nil
case tedwards.BW6_633:
if _, err := pointbw6633.SetBytes(buf[:40]); err != nil {
return nil, nil, err
}
a := pointbw6633.X.Bytes()
b := pointbw6633.Y.Bytes()
return a[:], b[:], nil
default:
panic("not implemented")
}
}