forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eddsa.go
253 lines (229 loc) · 7.24 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
/*
Copyright © 2020 ConsenSys
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package eddsa provides a ZKP-circuit function to verify a EdDSA signature.
package eddsa
import (
"errors"
"github.com/consensys/gnark-crypto/ecc"
"github.com/nume-crypto/gnark/logger"
"github.com/nume-crypto/gnark/std/hash"
"github.com/nume-crypto/gnark/frontend"
"github.com/nume-crypto/gnark/std/algebra/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"
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 verifies an eddsa signature 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.Hash) 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)
curve.AssertIsOnCurve(Q)
//[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 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")
}
curve.API().AssertIsEqual(Q.X, 0)
curve.API().AssertIsEqual(Q.Y, 1)
return nil
}
// Assign is a helper to assigned a compressed binary public key representation into its uncompressed form
func (p *PublicKey) Assign(curveID ecc.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 ecc.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 ecc.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 pointbw6633 edwardsbw6633.PointAffine
switch curveID {
case ecc.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 ecc.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 ecc.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 ecc.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 ecc.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 ecc.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 ecc.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 pointbw6633 edwardsbw6633.PointAffine
switch curveID {
case ecc.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 ecc.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 ecc.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 ecc.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 ecc.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 ecc.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")
}
}