/
hash_to_curve.go
262 lines (236 loc) · 8.02 KB
/
hash_to_curve.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
// Copyright 2020 ConsenSys AG
//
// 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 bw6633
import (
"math/big"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fp"
)
// hashToFp hashes msg to count prime field elements.
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-5.2
func hashToFp(msg, dst []byte, count int) ([]fp.Element, error) {
// 128 bits of security
// L = ceil((ceil(log2(p)) + k) / 8), where k is the security parameter = 128
L := 64
lenInBytes := count * L
pseudoRandomBytes, err := ecc.ExpandMsgXmd(msg, dst, lenInBytes)
if err != nil {
return nil, err
}
res := make([]fp.Element, count)
for i := 0; i < count; i++ {
res[i].SetBytes(pseudoRandomBytes[i*L : (i+1)*L])
}
return res, nil
}
// returns false if u>-u when seen as a bigInt
func sign0(u fp.Element) bool {
var a, b big.Int
u.ToBigIntRegular(&a)
u.Neg(&u)
u.ToBigIntRegular(&b)
return a.Cmp(&b) <= 0
}
// ----------------------------------------------------------------------------------------
// G1Affine
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-4.1
// Shallue and van de Woestijne method, works for any elliptic curve in Weierstrass curve
func svdwMapG1(u fp.Element) G1Affine {
var res G1Affine
// constants
// sage script to find z: https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#appendix-E.1
var z, c1, c2, c3, c4 fp.Element
z.SetString("20494478644167774678813387386538961497669590920908778075528754551012016751717791778743535050360001387419576570244406805463255765034468441182772056330021723098661967429339971741066259394985996")
c1.SetString("3")
c2.SetString("10247239322083887339406693693269480748834795460454389037764377275506008375858895889371767525180000693709788285122203402731627882517234220591386028165010861549330983714669985870533129697492999")
c3.SetString("4718799253676142777110053279330260062949551109654523745335489221344287444866578673338011692115447055772579777395725923509864056188435561217050822613773285813924493191312212418774289181159680")
c4.SetString("20494478644167774678813387386538961497669590920908778075528754551012016751717791778743535050360001387419576570244406805463255765034468441182772056330021723098661967429339971741066259394985993")
var tv1, tv2, tv3, tv4, one, x1, gx1, x2, gx2, x3, x, gx, y fp.Element
one.SetOne()
tv1.Square(&u).Mul(&tv1, &c1)
tv2.Add(&one, &tv1)
tv1.Sub(&one, &tv1)
tv3.Mul(&tv2, &tv1).Inverse(&tv3)
tv4.Mul(&u, &tv1)
tv4.Mul(&tv4, &tv3)
tv4.Mul(&tv4, &c3)
x1.Sub(&c2, &tv4)
gx1.Square(&x1)
// 12. gx1 = gx1 + A
gx1.Mul(&gx1, &x1)
gx1.Add(&gx1, &bCurveCoeff)
e1 := gx1.Legendre()
x2.Add(&c2, &tv4)
gx2.Square(&x2)
// 18. gx2 = gx2 + A
gx2.Mul(&gx2, &x2)
gx2.Add(&gx2, &bCurveCoeff)
E2 := gx2.Legendre() - e1 // 2 if is_square(gx2) AND NOT e1
x3.Square(&tv2)
x3.Mul(&x3, &tv3)
x3.Square(&x3)
x3.Mul(&x3, &c4)
x3.Add(&x3, &z)
if e1 == 1 {
x.Set(&x1)
} else {
x.Set(&x3)
}
if E2 == 2 {
x.Set(&x2)
}
gx.Square(&x)
// gx = gx + A
gx.Mul(&gx, &x)
gx.Add(&gx, &bCurveCoeff)
y.Sqrt(&gx)
e3 := sign0(u) && sign0(y)
if !e3 {
y.Neg(&y)
}
res.X.Set(&x)
res.Y.Set(&y)
return res
}
// MapToCurveG1Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-2.2.1
func MapToCurveG1Svdw(t fp.Element) G1Affine {
res := svdwMapG1(t)
res.ClearCofactor(&res)
return res
}
// EncodeToCurveG1Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-2.2.2
func EncodeToCurveG1Svdw(msg, dst []byte) (G1Affine, error) {
var res G1Affine
t, err := hashToFp(msg, dst, 1)
if err != nil {
return res, err
}
res = MapToCurveG1Svdw(t[0])
return res, nil
}
// HashToCurveG1Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-3
func HashToCurveG1Svdw(msg, dst []byte) (G1Affine, error) {
var res G1Affine
u, err := hashToFp(msg, dst, 2)
if err != nil {
return res, err
}
Q0 := MapToCurveG1Svdw(u[0])
Q1 := MapToCurveG1Svdw(u[1])
var _Q0, _Q1, _res G1Jac
_Q0.FromAffine(&Q0)
_Q1.FromAffine(&Q1)
_res.Set(&_Q1).AddAssign(&_Q0)
res.FromJacobian(&_res)
return res, nil
}
// ----------------------------------------------------------------------------------------
// G2Affine
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-4.1
// Shallue and van de Woestijne method, works for any elliptic curve in Weierstrass curve
func svdwMapG2(u fp.Element) G2Affine {
var res G2Affine
// constants
// sage script to find z: https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#appendix-E.1
var z, c1, c2, c3, c4 fp.Element
z.SetOne()
c1.SetString("9")
c2.SetString("10247239322083887339406693693269480748834795460454389037764377275506008375858895889371767525180000693709788285122203402731627882517234220591386028165010861549330983714669985870533129697492998")
c3.SetString("4098895705906800773620480056356439568405647282680108700646591795418032788291967668726767108634764703136596475278707263053005760418162253059108411150281824652143027659364481823596237914583914")
c4.SetString("20494478644167774678813387386538961497669590920908778075528754551012016751717791778743535050360001387419576570244406805463255765034468441182772056330021723098661967429339971741066259394985985")
var tv1, tv2, tv3, tv4, one, x1, gx1, x2, gx2, x3, x, gx, y fp.Element
one.SetOne()
tv1.Square(&u).Mul(&tv1, &c1)
tv2.Add(&one, &tv1)
tv1.Sub(&one, &tv1)
tv3.Mul(&tv2, &tv1).Inverse(&tv3)
tv4.Mul(&u, &tv1)
tv4.Mul(&tv4, &tv3)
tv4.Mul(&tv4, &c3)
x1.Sub(&c2, &tv4)
gx1.Square(&x1)
// 12. gx1 = gx1 + A
gx1.Mul(&gx1, &x1)
gx1.Add(&gx1, &bTwistCurveCoeff)
e1 := gx1.Legendre()
x2.Add(&c2, &tv4)
gx2.Square(&x2)
// 18. gx2 = gx2 + A
gx2.Mul(&gx2, &x2)
gx2.Add(&gx2, &bTwistCurveCoeff)
E2 := gx2.Legendre() - e1 // 2 if is_square(gx2) AND NOT e1
x3.Square(&tv2)
x3.Mul(&x3, &tv3)
x3.Square(&x3)
x3.Mul(&x3, &c4)
x3.Add(&x3, &z)
if e1 == 1 {
x.Set(&x1)
} else {
x.Set(&x3)
}
if E2 == 2 {
x.Set(&x2)
}
gx.Square(&x)
// gx = gx + A
gx.Mul(&gx, &x)
gx.Add(&gx, &bTwistCurveCoeff)
y.Sqrt(&gx)
e3 := sign0(u) && sign0(y)
if !e3 {
y.Neg(&y)
}
res.X.Set(&x)
res.Y.Set(&y)
return res
}
// MapToCurveG2Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-2.2.1
func MapToCurveG2Svdw(t fp.Element) G2Affine {
res := svdwMapG2(t)
res.ClearCofactor(&res)
return res
}
// EncodeToCurveG2Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-2.2.2
func EncodeToCurveG2Svdw(msg, dst []byte) (G2Affine, error) {
var res G2Affine
t, err := hashToFp(msg, dst, 1)
if err != nil {
return res, err
}
res = MapToCurveG2Svdw(t[0])
return res, nil
}
// HashToCurveG2Svdw maps an fp.Element to a point on the curve using the Shallue and van de Woestijne map
// https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-06#section-3
func HashToCurveG2Svdw(msg, dst []byte) (G2Affine, error) {
var res G2Affine
u, err := hashToFp(msg, dst, 2)
if err != nil {
return res, err
}
Q0 := MapToCurveG2Svdw(u[0])
Q1 := MapToCurveG2Svdw(u[1])
var _Q0, _Q1, _res G2Jac
_Q0.FromAffine(&Q0)
_Q1.FromAffine(&Q1)
_res.Set(&_Q1).AddAssign(&_Q0)
res.FromJacobian(&_res)
return res, nil
}