forked from Project-Arda/bgls
/
hash.go
265 lines (243 loc) · 6.51 KB
/
hash.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
// Copyright (C) 2018 Authors
// distributed under Apache 2.0 license
package curves
import (
"crypto/rand"
"math/big"
)
var zero = big.NewInt(0)
var one = big.NewInt(1)
var two = big.NewInt(2)
var three = big.NewInt(3)
var four = big.NewInt(4)
// 64 byte hash
func tryAndIncrement64(message []byte, hashfunc func(message []byte) [64]byte, curve CurveSystem) (px, py *big.Int) {
counter := []byte{byte(0)}
px = new(big.Int)
py = new(big.Int)
q := curve.GetG1Q()
for {
h := hashfunc(append(counter, message...))
counter[0]++
px.SetBytes(h[:48])
px.Mod(px, q)
ySqr := curve.g1XToYSquared(px)
root := calcQuadRes(ySqr, q)
rootSqr := new(big.Int).Exp(root, two, q)
if rootSqr.Cmp(ySqr) == 0 {
otherRoot := py.Sub(q, py)
// Set root to the canonical square root.
root, otherRoot = sortBigInts(root, otherRoot)
// Use the canonical root for py, unless the cofactor is one, in which case
// use an extra bit to determine parity.
py = root
if curve.getG1Cofactor().Cmp(one) == 0 {
signY := int(h[48]) % 2
if signY == 1 {
py = otherRoot
break
}
}
break
}
}
return
}
// Try and Increment hashing that is meant to comply with the standards we are using in the solidity contract.
// This is not recommended for use anywhere else.
func tryAndIncrementEvm(message []byte, hashfunc func(message []byte) [32]byte, curve CurveSystem) (px, py *big.Int) {
counter := []byte{byte(0)}
px = new(big.Int)
py = new(big.Int)
q := curve.GetG1Q()
for {
h := hashfunc(append(counter, message...))
counter[0]++
px.SetBytes(h[:32])
px.Mod(px, q)
ySqr := curve.g1XToYSquared(px)
root := calcQuadRes(ySqr, q)
rootSqr := new(big.Int).Exp(root, two, q)
if rootSqr.Cmp(ySqr) == 0 {
py = root
counter[0] = byte(255)
signY := hashfunc(append(counter, message...))[31] % 2
if signY == 1 {
py.Sub(q, py)
}
break
}
}
return
}
func sortBigInts(b1 *big.Int, b2 *big.Int) (*big.Int, *big.Int) {
if b1.Cmp(b2) > 0 {
return b2, b1
}
return b1, b2
}
func fouqueTibouchiG1(curve CurveSystem, t *big.Int, blind bool) (Point, bool) {
pt, ok := sw(curve, t, blind)
if !ok {
return nil, false
}
pt = pt.Mul(curve.getG1Cofactor())
return pt, true
}
// Shallue - van de Woestijne encoding
// from "Indifferentiable Hashing to Barreto–Naehrig Curves"
func sw(curve CurveSystem, t *big.Int, blind bool) (Point, bool) {
var x [3]*big.Int
b := curve.getG1B()
q := curve.GetG1Q()
rootNeg3, neg1SubRootNeg3 := curve.getFTHashParams()
//w = sqrt(-3)*t / (1 + b + t^2)
w := new(big.Int)
w.Exp(t, two, q)
w.Add(w, one)
w.Add(w, b)
w.ModInverse(w, q)
w.Mul(w, t)
w.Mod(w, q)
w.Mul(w, rootNeg3)
w.Mod(w, q)
alpha := int64(0)
beta := int64(0)
var i int
for i = 0; i < 3; i++ {
if i == 0 {
//x[0] = (-1 + sqrt(-3))/2 - t*w
x[0] = new(big.Int)
x[0].Mul(t, w)
x[0].Mod(x[0], q)
x[0].Sub(q, x[0])
x[0].Add(x[0], neg1SubRootNeg3)
x[0].Mod(x[0], q)
// If blinding isn't needed, utilize conditional branches.
alpha = chkPoint(x[0], curve, q, blind)
if !blind && alpha == 1 {
break
}
} else if i == 1 {
//x[1] = -1 - x[1]
x[1] = new(big.Int)
x[1].Neg(x[0])
x[1].Sub(x[1], one)
x[1].Mod(x[1], q)
beta = chkPoint(x[1], curve, q, blind)
if !blind && beta == 1 {
break
}
} else {
//x[2] = 1 + 1/w^2
x[2] = new(big.Int)
x[2].Exp(w, two, q)
x[2].ModInverse(x[2], q)
x[2].Add(x[2], one)
x[2].Mod(x[2], q)
break
}
}
//i = first x[i] such that (x^3 + b) is square
if blind {
i = int((((alpha - 1) * beta) + 3) % 3)
}
// TODO Add blinded form of this
y := calcQuadRes(curve.g1XToYSquared(x[i]), q)
if parity(y, q) != parity(t, q) {
y.Sub(q, y)
}
// Check is set to false since its guaranteed to be on the curve
return curve.MakeG1Point([]*big.Int{x[i], y}, false)
}
func parity(x *big.Int, q *big.Int) bool {
neg := new(big.Int).Sub(q, x)
return x.Cmp(neg) > 0
}
// Currently implementing first method from
// http://mathworld.wolfram.com/QuadraticResidue.html
// Experimentally, this seems to always return the canonical square root,
// however I haven't seen a proof of this.
func calcQuadRes(ySqr *big.Int, q *big.Int) *big.Int {
resMod4 := new(big.Int).Mod(q, four)
if resMod4.Cmp(three) == 0 {
k := new(big.Int).Sub(q, three)
k.Div(k, four)
exp := new(big.Int).Add(k, one)
result := new(big.Int)
result.Exp(ySqr, exp, q)
return result
}
// TODO: ADD CODE TO CALC QUADRATIC RESIDUE IN OTHER CASES
return zero
}
// Currently implementing method from Guide to Pairing Based Cryptography, Ch 5 algorithm 18.
// This in turn is cited from "Gora Adj and Francisco Rodriguez-Henriquez.
// Square root computation over even extension fields.
// IEEE Transactions on Computers, 63(11):2829-2841, 2014"
func calcComplexQuadRes(ySqr *complexNum, q *big.Int) *complexNum {
result := getComplexZero()
if ySqr.im.Cmp(zero) == 0 {
result.re = calcQuadRes(ySqr.re, q)
return result
}
lambda := new(big.Int).Exp(ySqr.re, two, q)
lambda.Add(lambda, new(big.Int).Exp(ySqr.im, two, q))
lambda = calcQuadRes(lambda, q)
invtwo := new(big.Int).ModInverse(two, q)
delta := new(big.Int).Add(ySqr.re, lambda)
delta.Mod(delta, q)
delta.Mul(delta, invtwo)
delta.Mod(delta, q)
if !isQuadRes(delta, q) {
delta = new(big.Int).Sub(ySqr.re, lambda)
delta.Mul(delta, invtwo)
delta.Mod(delta, q)
}
result.re = calcQuadRes(delta, q)
invRe := new(big.Int).ModInverse(result.re, q)
result.im.Mul(invRe, invtwo)
result.im.Mod(result.im, q)
result.im.Mul(result.im, ySqr.im)
result.im.Mod(result.im, q)
result.re.Mod(result.re, q)
return result
}
//generates a random member of Fq such that it is a square
func randSquare(q *big.Int) *big.Int {
var r, _ = rand.Int(rand.Reader, q)
return r.Exp(r, two, q)
}
// If blind is true, this blinds k with a random square in Fq,
// and then returns square root. This can be done to limit timing leakage.
// This returns the quadratic character of k.
func quadraticCharacter(k *big.Int, q *big.Int, blind bool) int64 {
r := k
if blind {
r = randSquare(q)
r.Mul(r, k)
r.Mod(r, q)
}
res := isQuadRes(r, q)
if res {
return 1
}
return -1
}
//checks that (x^3 + b) is a square in Fq
func chkPoint(x *big.Int, curve CurveSystem, q *big.Int, mask bool) int64 {
return quadraticCharacter(curve.g1XToYSquared(x), q, mask)
}
// Implement Eulers Criterion
func isQuadRes(a *big.Int, q *big.Int) bool {
if a.Cmp(zero) == 0 {
return true
}
fieldOrder := new(big.Int).Sub(q, one)
res := new(big.Int).Div(fieldOrder, two)
res.Exp(a, res, q)
if res.Cmp(one) == 0 {
return true
}
return false
}