forked from Consensys/gnark-crypto
/
endomorpism.go
190 lines (158 loc) · 5.07 KB
/
endomorpism.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
package bandersnatch
import (
"math"
"math/big"
"github.com/liyue201/gnark-crypto/ecc"
"github.com/liyue201/gnark-crypto/ecc/bls12-381/fr"
)
// phi endomorphism sqrt(-2) \in O(-8)
// (x,y,z)->\lambda*(x,y,z) s.t. \lamba^2 = -2 mod Order
func (p *PointProj) phi(p1 *PointProj) *PointProj {
initOnce.Do(initCurveParams)
var zz, yy, xy, f, g, h fr.Element
zz.Square(&p1.Z)
yy.Square(&p1.Y)
xy.Mul(&p1.X, &p1.Y)
f.Sub(&zz, &yy).Mul(&f, &curveParams.endo[1])
zz.Mul(&zz, &curveParams.endo[0])
g.Add(&yy, &zz).Mul(&g, &curveParams.endo[0])
h.Sub(&yy, &zz)
p.X.Mul(&f, &h)
p.Y.Mul(&g, &xy)
p.Z.Mul(&h, &xy)
return p
}
// ScalarMultiplication scalar multiplication (GLV) of a point
// p1 in projective coordinates with a scalar in big.Int
func (p *PointProj) scalarMulGLV(p1 *PointProj, scalar *big.Int) *PointProj {
initOnce.Do(initCurveParams)
var table [15]PointProj
var zero big.Int
var res PointProj
var k1, k2 fr.Element
res.setInfinity()
// table[b3b2b1b0-1] = b3b2*phi(p1) + b1b0*p1
table[0].Set(p1)
table[3].phi(p1)
// split the scalar, modifies +-p1, phi(p1) accordingly
k := ecc.SplitScalar(scalar, &curveParams.glvBasis)
if k[0].Cmp(&zero) == -1 {
k[0].Neg(&k[0])
table[0].Neg(&table[0])
}
if k[1].Cmp(&zero) == -1 {
k[1].Neg(&k[1])
table[3].Neg(&table[3])
}
// precompute table (2 bits sliding window)
// table[b3b2b1b0-1] = b3b2*phi(p1) + b1b0*p1 if b3b2b1b0 != 0
table[1].Double(&table[0])
table[2].Set(&table[1]).Add(&table[2], &table[0])
table[4].Set(&table[3]).Add(&table[4], &table[0])
table[5].Set(&table[3]).Add(&table[5], &table[1])
table[6].Set(&table[3]).Add(&table[6], &table[2])
table[7].Double(&table[3])
table[8].Set(&table[7]).Add(&table[8], &table[0])
table[9].Set(&table[7]).Add(&table[9], &table[1])
table[10].Set(&table[7]).Add(&table[10], &table[2])
table[11].Set(&table[7]).Add(&table[11], &table[3])
table[12].Set(&table[11]).Add(&table[12], &table[0])
table[13].Set(&table[11]).Add(&table[13], &table[1])
table[14].Set(&table[11]).Add(&table[14], &table[2])
// bounds on the lattice base vectors guarantee that k1, k2 are len(r)/2 bits long max
k1.SetBigInt(&k[0]).FromMont()
k2.SetBigInt(&k[1]).FromMont()
// loop starts from len(k1)/2 due to the bounds
// fr.Limbs == Order.limbs
for i := int(math.Ceil(fr.Limbs/2. - 1)); i >= 0; i-- {
mask := uint64(3) << 62
for j := 0; j < 32; j++ {
res.Double(&res).Double(&res)
b1 := (k1[i] & mask) >> (62 - 2*j)
b2 := (k2[i] & mask) >> (62 - 2*j)
if b1|b2 != 0 {
scalar := (b2<<2 | b1)
res.Add(&res, &table[scalar-1])
}
mask = mask >> 2
}
}
p.Set(&res)
return p
}
// phi endomorphism sqrt(-2) \in O(-8)
// (x,y,z)->\lambda*(x,y,z) s.t. \lamba^2 = -2 mod Order
func (p *PointExtended) phi(p1 *PointExtended) *PointExtended {
initOnce.Do(initCurveParams)
var zz, yy, xy, f, g, h fr.Element
zz.Square(&p1.Z)
yy.Square(&p1.Y)
xy.Mul(&p1.X, &p1.Y)
f.Sub(&zz, &yy).Mul(&f, &curveParams.endo[1])
zz.Mul(&zz, &curveParams.endo[0])
g.Add(&yy, &zz).Mul(&g, &curveParams.endo[0])
h.Sub(&yy, &zz)
p.X.Mul(&f, &h)
p.Y.Mul(&g, &xy)
p.Z.Mul(&h, &xy)
p.T.Mul(&f, &g)
return p
}
// ScalarMultiplication scalar multiplication (GLV) of a point
// p1 in projective coordinates with a scalar in big.Int
func (p *PointExtended) scalarMulGLV(p1 *PointExtended, scalar *big.Int) *PointExtended {
initOnce.Do(initCurveParams)
var table [15]PointExtended
var zero big.Int
var res PointExtended
var k1, k2 fr.Element
res.setInfinity()
// table[b3b2b1b0-1] = b3b2*phi(p1) + b1b0*p1
table[0].Set(p1)
table[3].phi(p1)
// split the scalar, modifies +-p1, phi(p1) accordingly
k := ecc.SplitScalar(scalar, &curveParams.glvBasis)
if k[0].Cmp(&zero) == -1 {
k[0].Neg(&k[0])
table[0].Neg(&table[0])
}
if k[1].Cmp(&zero) == -1 {
k[1].Neg(&k[1])
table[3].Neg(&table[3])
}
// precompute table (2 bits sliding window)
// table[b3b2b1b0-1] = b3b2*phi(p1) + b1b0*p1 if b3b2b1b0 != 0
table[1].Double(&table[0])
table[2].Set(&table[1]).Add(&table[2], &table[0])
table[4].Set(&table[3]).Add(&table[4], &table[0])
table[5].Set(&table[3]).Add(&table[5], &table[1])
table[6].Set(&table[3]).Add(&table[6], &table[2])
table[7].Double(&table[3])
table[8].Set(&table[7]).Add(&table[8], &table[0])
table[9].Set(&table[7]).Add(&table[9], &table[1])
table[10].Set(&table[7]).Add(&table[10], &table[2])
table[11].Set(&table[7]).Add(&table[11], &table[3])
table[12].Set(&table[11]).Add(&table[12], &table[0])
table[13].Set(&table[11]).Add(&table[13], &table[1])
table[14].Set(&table[11]).Add(&table[14], &table[2])
// bounds on the lattice base vectors guarantee that k1, k2 are len(r)/2 bits long max
k1.SetBigInt(&k[0]).FromMont()
k2.SetBigInt(&k[1]).FromMont()
// loop starts from len(k1)/2 due to the bounds
// fr.Limbs == Order.limbs
for i := int(math.Ceil(fr.Limbs/2. - 1)); i >= 0; i-- {
mask := uint64(3) << 62
for j := 0; j < 32; j++ {
res.Double(&res).Double(&res)
b1 := (k1[i] & mask) >> (62 - 2*j)
b2 := (k2[i] & mask) >> (62 - 2*j)
if b1|b2 != 0 {
scalar := (b2<<2 | b1)
res.Add(&res, &table[scalar-1])
}
mask = mask >> 2
}
}
p.Set(&res)
return p
}