Debian Codegraph @

edwards25519/elligator.go

raw symbols SCIP
1 package edwards25519
2
3 import (
4 "fmt"
5 )
6
7 // Represents a point (s, t) on the Jacobi quartic associated to
8 // the Edwards curve
9 type JacobiPoint struct {
10 S, T FieldElement
11 }
12
13 // Computes the at most 8 positive FieldElements f such that p == elligator2(f).
14 // Assumes p is even.
15 //
16 // Returns a bitmask of which elements in fes are set.
17 func (p *ExtendedPoint) RistrettoElligator2Inverse(fes *[8]FieldElement) uint8 {
18 var setMask uint8
19 var jcs [4]JacobiPoint
20 var jc JacobiPoint
21
22 // Elligator2 computes a Point from a FieldElement in two steps: first
23 // it computes a (s,t) on the Jacobi quartic and then computes the
24 // corresponding even point on the Edwards curve.
25 //
26 // We invert in three steps. Any Ristretto point has four representatives
27 // as even Edwards points. For each of those even Edwards points,
28 // there are two points on the Jacobi quartic that map to it.
29 // Each of those eight points on the Jacobi quartic might have an
30 // Elligator2 preimage.
31 //
32 // Essentially we first loop over the four representatives of our point,
33 // then for each of them consider both points on the Jacobi quartic and
34 // check whether they have an inverse under Elligator2. We take the
35 // following shortcut though.
36 //
37 // We can compute two Jacobi quartic points for (x,y) and (-x,-y)
38 // at the same time. The four Jacobi quartic points are two of
39 // such pairs.
40
41 p.ToJacobiQuarticRistretto(&jcs)
42
43 for j := 0; j < 4; j++ {
44 setMask |= uint8(jcs[j].elligator2Inverse(&fes[2*j]) << uint(2*j))
45 jc.Dual(&jcs[j])
46 setMask |= uint8(jc.elligator2Inverse(&fes[2*j+1]) << uint(2*j+1))
47 }
48
49 return setMask
50 }
51
52 // Find a point on the Jacobi quartic associated to each of the four
53 // points Ristretto equivalent to p.
54 //
55 // There is one exception: for (0,-1) there is no point on the quartic and
56 // so we repeat one on the quartic equivalent to (0,1).
57 func (p *ExtendedPoint) ToJacobiQuarticRistretto(qs *[4]JacobiPoint) *ExtendedPoint {
58 var X2, Y2, Z2, Y4, ZmY, ZpY, Z2mY2, gamma, den, sOverX, spOverXp, tmp FieldElement
59
60 X2.Square(&p.X) // X^2
61 Y2.Square(&p.Y) // Y^2
62 Y4.Square(&Y2) // Y^4
63 Z2.Square(&p.Z) // Z^2
64 ZmY.sub(&p.Z, &p.Y) // Z - Y
65 ZpY.add(&p.Z, &p.Y) // Z + Y
66 Z2mY2.sub(&Z2, &Y2) // Z^2 - Y^2
67
68 // gamma := 1/sqrt( Y^4 X^2 (Z^2 - Y^2) )
69 gamma.Mul(&Y4, &X2)
70 gamma.Mul(&gamma, &Z2mY2)
71 gamma.InvSqrt(&gamma)
72
73 // den := gamma * Y^2
74 den.Mul(&gamma, &Y2)
75
76 // sOverX := den * (Z - Y)
77 // spOverXp := den * (Z + Y)
78 sOverX.Mul(&den, &ZmY)
79 spOverXp.Mul(&den, &ZpY)
80
81 // s_0 := sOverX * X
82 // s_1 := -spOverXp * X
83 qs[0].S.Mul(&sOverX, &p.X)
84 tmp.Mul(&spOverXp, &p.X)
85 qs[1].S.Neg(&tmp)
86
87 // t_0 := 2/sqrt(-d-1) * Z * sOverX
88 // t_1 := 2/sqrt(-d-1) * Z * spOverXp
89 tmp.Mul(&feDoubleInvSqrtMinusDMinusOne, &p.Z)
90 qs[0].T.Mul(&tmp, &sOverX)
91 qs[1].T.Mul(&tmp, &spOverXp)
92
93 // den = 1/sqrt(1+d) (Y^2 - Z^2) gamma
94 den.Neg(&Z2mY2)
95 den.Mul(&den, &feInvSqrt1pD)
96 den.Mul(&den, &gamma)
97
98 // Same as before, but with the substitution (X, Y, Z) = (Y, X, i*Z)
99 var iZ, iZmX, iZpX, sOverY, spOverYp FieldElement
100 iZ.Mul(&feI, &p.Z) // iZ
101 iZmX.sub(&iZ, &p.X) // iZ - X
102 iZpX.add(&iZ, &p.X) // iZ + X
103
104 // sOverY := den * (iZ - Y)
105 // spOverYp := den * (iZ + Y)
106 sOverY.Mul(&den, &iZmX)
107 spOverYp.Mul(&den, &iZpX)
108
109 // s_2 := sOverY * Y
110 // s_3 := -spOverYp * Y
111 qs[2].S.Mul(&sOverY, &p.Y)
112 tmp.Mul(&spOverYp, &p.Y)
113 qs[3].S.Neg(&tmp)
114
115 // t_2 := 2/sqrt(-d-1) * i*Z * sOverY
116 // t_3 := 2/sqrt(-d-1) * i*Z * spOverYp
117 tmp.Mul(&feDoubleInvSqrtMinusDMinusOne, &iZ)
118 qs[2].T.Mul(&tmp, &sOverY)
119 qs[3].T.Mul(&tmp, &spOverYp)
120
121 // Special case: X=0 or Y=0. Then return
122 //
123 // (0,1) (1,2i/sqrt(-d-1) (-1,2i/sqrt(-d-1))
124 //
125 // Note that if X=0 or Y=0, then s_i = t_i = 0.
126 XorYisZero := 1 - (p.X.IsNonZeroI() & p.Y.IsNonZeroI())
127 qs[0].T.ConditionalSet(&feOne, XorYisZero)
128 qs[1].T.ConditionalSet(&feOne, XorYisZero)
129 qs[2].T.ConditionalSet(&feDoubleIInvSqrtMinusDMinusOne, XorYisZero)
130 qs[3].T.ConditionalSet(&feDoubleIInvSqrtMinusDMinusOne, XorYisZero)
131 qs[2].S.ConditionalSet(&feOne, XorYisZero)
132 qs[3].S.ConditionalSet(&feMinusOne, XorYisZero)
133
134 return p
135 }
136
137 func (p *JacobiPoint) Dual(q *JacobiPoint) *JacobiPoint {
138 p.S.Neg(&q.S)
139 p.T.Neg(&q.T)
140
141 return p
142 }
143
144 // Elligator2 is defined in two steps: first a field element is converted
145 // to a point (s,t) on the Jacobi quartic associated to the Edwards curve.
146 // Then this point is mapped to a point on the Edwards curve.
147 // This function computes a field element that is mapped to a given (s,t)
148 // with Elligator2 if it exists.
149 //
150 // Returns 1 if a preimage is found and 0 if none exists.
151 func (p *JacobiPoint) elligator2Inverse(fe *FieldElement) int {
152 var x, y, a, a2, S2, S4, invSqiY, negS2, out FieldElement
153
154 // Special case: s = 0. If s is zero, either t = 1 or t = -1.
155 // If t=1, then sqrt(i*d) is the preimage. Otherwise it's 0.
156
157 sNonZero := p.S.IsNonZeroI()
158 tEqualsOne := p.T.EqualsI(&feOne)
159 out.Set(&feZero)
160 out.ConditionalSet(&feSqrtID, tEqualsOne)
161
162 ret := 1 - sNonZero
163 done := 1 - sNonZero
164
165 // a := (t+1) (d+1)/(d-1)
166 a.add(&p.T, &feOne)
167 a.Mul(&a, &feDp1OverDm1)
168 a2.Square(&a)
169
170 // y := 1/sqrt(i (s^4 - a^2)).
171 S2.Square(&p.S)
172 S4.Square(&S2)
173 invSqiY.sub(&S4, &a2)
174
175 // there is no preimage of the square root of i*(s^4-a^2) does not exist
176 sq := y.InvSqrtI(&invSqiY)
177 ret |= 1 - sq
178 done |= sq
179
180 // x := (a + sign(s)*s^2) y
181 negS2.Neg(&S2)
182 S2.ConditionalSet(&negS2, p.S.IsNegativeI())
183 x.add(&a, &S2)
184 x.Mul(&x, &y)
185
186 // fe := abs(x)
187 x.Abs(&x)
188 out.ConditionalSet(&x, 1-done)
189 fe.Set(&out)
190
191 return int(ret)
192 }
193
194 // Set p to the point corresponding to the given point (s,t) on the
195 // associated Jacobi quartic.
196 func (p *CompletedPoint) SetJacobiQuartic(jc *JacobiPoint) *CompletedPoint {
197 var s2 FieldElement
198 s2.Square(&jc.S)
199
200 // Set x to s * 2/sqrt(-d-1)
201 p.X.Mul(&jc.S, &feDoubleInvSqrtMinusDMinusOne)
202
203 // Set z to t
204 p.Z.Set(&jc.T)
205
206 // Set y to 1-s^2
207 p.Y.sub(&feOne, &s2)
208
209 // Set t to 1+s^2
210 p.T.add(&feOne, &s2)
211 return p
212 }
213
214 // Set p to the curvepoint corresponding to r0 via Mike Hamburg's variation
215 // on Elligator2 for Ristretto. Returns p.
216 func (p *CompletedPoint) SetRistrettoElligator2(r0 *FieldElement) *CompletedPoint {
217 var r, rPlusD, rPlusOne, D, N, ND, sqrt, twiddle, sgn FieldElement
218 var rSubOne, r0i, sNeg FieldElement
219 var jc JacobiPoint
220
221 var b int32
222
223 // r := i * r0^2
224 r0i.Mul(r0, &feI)
225 r.Mul(r0, &r0i)
226
227 // D := -((d*r)+1) * (r + d)
228 rPlusD.add(&feD, &r)
229 D.Mul(&feD, &r)
230 D.add(&D, &feOne)
231 D.Mul(&D, &rPlusD)
232 D.Neg(&D)
233
234 // N := -(d^2 - 1)(r + 1)
235 rPlusOne.add(&r, &feOne)
236 N.Mul(&feOneMinusDSquared, &rPlusOne)
237
238 // sqrt is the inverse square root of N*D or of i*N*D.
239 // b=1 iff n1 is square.
240 ND.Mul(&N, &D)
241
242 b = sqrt.InvSqrtI(&ND)
243 sqrt.Abs(&sqrt)
244
245 twiddle.SetOne()
246 twiddle.ConditionalSet(&r0i, 1-b)
247 sgn.SetOne()
248 sgn.ConditionalSet(&feMinusOne, 1-b)
249 sqrt.Mul(&sqrt, &twiddle)
250
251 // s = N * sqrt * twiddle
252 jc.S.Mul(&sqrt, &N)
253
254 // t = -sgn * sqrt * s * (r-1) * (d-1)^2 - 1
255 jc.T.Neg(&sgn)
256 jc.T.Mul(&sqrt, &jc.T)
257 jc.T.Mul(&jc.S, &jc.T)
258 jc.T.Mul(&feDMinusOneSquared, &jc.T)
259 rSubOne.sub(&r, &feOne)
260 jc.T.Mul(&rSubOne, &jc.T)
261 jc.T.sub(&jc.T, &feOne)
262
263 sNeg.Neg(&jc.S)
264 jc.S.ConditionalSet(&sNeg, equal30(jc.S.IsNegativeI(), b))
265 return p.SetJacobiQuartic(&jc)
266 }
267
268 // WARNING This operation is not constant-time. Do not use for cryptography
269 //
270 // unless you're sure this is not an issue.
271 func (p *JacobiPoint) String() string {
272 return fmt.Sprintf("JacobiPoint(%v, %v)", p.S, p.T)
273 }