ellswift.md (27589B)
1 # ElligatorSwift for secp256k1 explained 2 3 In this document we explain how the `ellswift` module implementation is related to the 4 construction in the 5 ["SwiftEC: Shallue–van de Woestijne Indifferentiable Function To Elliptic Curves"](https://eprint.iacr.org/2022/759) 6 paper by Jorge Chávez-Saab, Francisco Rodríguez-Henríquez, and Mehdi Tibouchi. 7 8 * [1. Introduction](#1-introduction) 9 * [2. The decoding function](#2-the-decoding-function) 10 + [2.1 Decoding for `secp256k1`](#21-decoding-for-secp256k1) 11 * [3. The encoding function](#3-the-encoding-function) 12 + [3.1 Switching to *v, w* coordinates](#31-switching-to-v-w-coordinates) 13 + [3.2 Avoiding computing all inverses](#32-avoiding-computing-all-inverses) 14 + [3.3 Finding the inverse](#33-finding-the-inverse) 15 + [3.4 Dealing with special cases](#34-dealing-with-special-cases) 16 + [3.5 Encoding for `secp256k1`](#35-encoding-for-secp256k1) 17 * [4. Encoding and decoding full *(x, y)* coordinates](#4-encoding-and-decoding-full-x-y-coordinates) 18 + [4.1 Full *(x, y)* coordinates for `secp256k1`](#41-full-x-y-coordinates-for-secp256k1) 19 20 ## 1. Introduction 21 22 The `ellswift` module effectively introduces a new 64-byte public key format, with the property 23 that (uniformly random) public keys can be encoded as 64-byte arrays which are computationally 24 indistinguishable from uniform byte arrays. The module provides functions to convert public keys 25 from and to this format, as well as convenience functions for key generation and ECDH that operate 26 directly on ellswift-encoded keys. 27 28 The encoding consists of the concatenation of two (32-byte big endian) encoded field elements $u$ 29 and $t.$ Together they encode an x-coordinate on the curve $x$, or (see further) a full point $(x, y)$ on 30 the curve. 31 32 **Decoding** consists of decoding the field elements $u$ and $t$ (values above the field size $p$ 33 are taken modulo $p$), and then evaluating $F_u(t)$, which for every $u$ and $t$ results in a valid 34 x-coordinate on the curve. The functions $F_u$ will be defined in [Section 2](#2-the-decoding-function). 35 36 **Encoding** a given $x$ coordinate is conceptually done as follows: 37 * Loop: 38 * Pick a uniformly random field element $u.$ 39 * Compute the set $L = F_u^{-1}(x)$ of $t$ values for which $F_u(t) = x$, which may have up to *8* elements. 40 * With probability $1 - \dfrac{\\#L}{8}$, restart the loop. 41 * Select a uniformly random $t \in L$ and return $(u, t).$ 42 43 This is the *ElligatorSwift* algorithm, here given for just x-coordinates. An extension to full 44 $(x, y)$ points will be given in [Section 4](#4-encoding-and-decoding-full-x-y-coordinates). 45 The algorithm finds a uniformly random $(u, t)$ among (almost all) those 46 for which $F_u(t) = x.$ Section 3.2 in the paper proves that the number of such encodings for 47 almost all x-coordinates on the curve (all but at most 39) is close to two times the field size 48 (specifically, it lies in the range $2q \pm (22\sqrt{q} + O(1))$, where $q$ is the size of the field). 49 50 ## 2. The decoding function 51 52 First some definitions: 53 * $\mathbb{F}$ is the finite field of size $q$, of characteristic 5 or more, and $q \equiv 1 \mod 3.$ 54 * For `secp256k1`, $q = 2^{256} - 2^{32} - 977$, which satisfies that requirement. 55 * Let $E$ be the elliptic curve of points $(x, y) \in \mathbb{F}^2$ for which $y^2 = x^3 + ax + b$, with $a$ and $b$ 56 public constants, for which $\Delta_E = -16(4a^3 + 27b^2)$ is a square, and at least one of $(-b \pm \sqrt{-3 \Delta_E} / 36)/2$ is a square. 57 This implies that the order of $E$ is either odd, or a multiple of *4*. 58 If $a=0$, this condition is always fulfilled. 59 * For `secp256k1`, $a=0$ and $b=7.$ 60 * Let the function $g(x) = x^3 + ax + b$, so the $E$ curve equation is also $y^2 = g(x).$ 61 * Let the function $h(x) = 3x^3 + 4a.$ 62 * Define $V$ as the set of solutions $(x_1, x_2, x_3, z)$ to $z^2 = g(x_1)g(x_2)g(x_3).$ 63 * Define $S_u$ as the set of solutions $(X, Y)$ to $X^2 + h(u)Y^2 = -g(u)$ and $Y \neq 0.$ 64 * $P_u$ is a function from $\mathbb{F}$ to $S_u$ that will be defined below. 65 * $\psi_u$ is a function from $S_u$ to $V$ that will be defined below. 66 67 **Note**: In the paper: 68 * $F_u$ corresponds to $F_{0,u}$ there. 69 * $P_u(t)$ is called $P$ there. 70 * All $S_u$ sets together correspond to $S$ there. 71 * All $\psi_u$ functions together (operating on elements of $S$) correspond to $\psi$ there. 72 73 Note that for $V$, the left hand side of the equation $z^2$ is square, and thus the right 74 hand must also be square. As multiplying non-squares results in a square in $\mathbb{F}$, 75 out of the three right-hand side factors an even number must be non-squares. 76 This implies that exactly *1* or exactly *3* out of 77 $\\{g(x_1), g(x_2), g(x_3)\\}$ must be square, and thus that for any $(x_1,x_2,x_3,z) \in V$, 78 at least one of $\\{x_1, x_2, x_3\\}$ must be a valid x-coordinate on $E.$ There is one exception 79 to this, namely when $z=0$, but even then one of the three values is a valid x-coordinate. 80 81 **Define** the decoding function $F_u(t)$ as: 82 * Let $(x_1, x_2, x_3, z) = \psi_u(P_u(t)).$ 83 * Return the first element $x$ of $(x_3, x_2, x_1)$ which is a valid x-coordinate on $E$ (i.e., $g(x)$ is square). 84 85 $P_u(t) = (X(u, t), Y(u, t))$, where: 86 87 $$ 88 \begin{array}{lcl} 89 X(u, t) & = & \left\\{\begin{array}{ll} 90 \dfrac{g(u) - t^2}{2t} & a = 0 \\ 91 \dfrac{g(u) + h(u)(Y_0(u) - X_0(u)t)^2}{X_0(u)(1 + h(u)t^2)} & a \neq 0 92 \end{array}\right. \\ 93 Y(u, t) & = & \left\\{\begin{array}{ll} 94 \dfrac{X(u, t) + t}{u \sqrt{-3}} = \dfrac{g(u) + t^2}{2tu\sqrt{-3}} & a = 0 \\ 95 Y_0(u) + t(X(u, t) - X_0(u)) & a \neq 0 96 \end{array}\right. 97 \end{array} 98 $$ 99 100 $P_u(t)$ is defined: 101 * For $a=0$, unless: 102 * $u = 0$ or $t = 0$ (division by zero) 103 * $g(u) = -t^2$ (would give $Y=0$). 104 * For $a \neq 0$, unless: 105 * $X_0(u) = 0$ or $h(u)t^2 = -1$ (division by zero) 106 * $Y_0(u) (1 - h(u)t^2) = 2X_0(u)t$ (would give $Y=0$). 107 108 The functions $X_0(u)$ and $Y_0(u)$ are defined in Appendix A of the paper, and depend on various properties of $E.$ 109 110 The function $\psi_u$ is the same for all curves: $\psi_u(X, Y) = (x_1, x_2, x_3, z)$, where: 111 112 $$ 113 \begin{array}{lcl} 114 x_1 & = & \dfrac{X}{2Y} - \dfrac{u}{2} && \\ 115 x_2 & = & -\dfrac{X}{2Y} - \dfrac{u}{2} && \\ 116 x_3 & = & u + 4Y^2 && \\ 117 z & = & \dfrac{g(x_3)}{2Y}(u^2 + ux_1 + x_1^2 + a) = \dfrac{-g(u)g(x_3)}{8Y^3} 118 \end{array} 119 $$ 120 121 ### 2.1 Decoding for `secp256k1` 122 123 Put together and specialized for $a=0$ curves, decoding $(u, t)$ to an x-coordinate is: 124 125 **Define** $F_u(t)$ as: 126 * Let $X = \dfrac{u^3 + b - t^2}{2t}.$ 127 * Let $Y = \dfrac{X + t}{u\sqrt{-3}}.$ 128 * Return the first $x$ in $(u + 4Y^2, \dfrac{-X}{2Y} - \dfrac{u}{2}, \dfrac{X}{2Y} - \dfrac{u}{2})$ for which $g(x)$ is square. 129 130 To make sure that every input decodes to a valid x-coordinate, we remap the inputs in case 131 $P_u$ is not defined (when $u=0$, $t=0$, or $g(u) = -t^2$): 132 133 **Define** $F_u(t)$ as: 134 * Let $u'=u$ if $u \neq 0$; $1$ otherwise (guaranteeing $u' \neq 0$). 135 * Let $t'=t$ if $t \neq 0$; $1$ otherwise (guaranteeing $t' \neq 0$). 136 * Let $t''=t'$ if $g(u') \neq -t'^2$; $2t'$ otherwise (guaranteeing $t'' \neq 0$ and $g(u') \neq -t''^2$). 137 * Let $X = \dfrac{u'^3 + b - t''^2}{2t''}.$ 138 * Let $Y = \dfrac{X + t''}{u'\sqrt{-3}}.$ 139 * Return the first $x$ in $(u' + 4Y^2, \dfrac{-X}{2Y} - \dfrac{u'}{2}, \dfrac{X}{2Y} - \dfrac{u'}{2})$ for which $x^3 + b$ is square. 140 141 The choices here are not strictly necessary. Just returning a fixed constant in any of the undefined cases would suffice, 142 but the approach here is simple enough and gives fairly uniform output even in these cases. 143 144 **Note**: in the paper these conditions result in $\infty$ as output, due to the use of projective coordinates there. 145 We wish to avoid the need for callers to deal with this special case. 146 147 This is implemented in `haskellsecp256k1_v0_1_0_ellswift_xswiftec_frac_var` (which decodes to an x-coordinate represented as a fraction), and 148 in `haskellsecp256k1_v0_1_0_ellswift_xswiftec_var` (which outputs the actual x-coordinate). 149 150 ## 3. The encoding function 151 152 To implement $F_u^{-1}(x)$, the function to find the set of inverses $t$ for which $F_u(t) = x$, we have to reverse the process: 153 * Find all the $(X, Y) \in S_u$ that could have given rise to $x$, through the $x_1$, $x_2$, or $x_3$ formulas in $\psi_u.$ 154 * Map those $(X, Y)$ solutions to $t$ values using $P_u^{-1}(X, Y).$ 155 * For each of the found $t$ values, verify that $F_u(t) = x.$ 156 * Return the remaining $t$ values. 157 158 The function $P_u^{-1}$, which finds $t$ given $(X, Y) \in S_u$, is significantly simpler than $P_u:$ 159 160 $$ 161 P_u^{-1}(X, Y) = \left\\{\begin{array}{ll} 162 Yu\sqrt{-3} - X & a = 0 \\ 163 \dfrac{Y-Y_0(u)}{X-X_0(u)} & a \neq 0 \land X \neq X_0(u) \\ 164 \dfrac{-X_0(u)}{h(u)Y_0(u)} & a \neq 0 \land X = X_0(u) \land Y = Y_0(u) 165 \end{array}\right. 166 $$ 167 168 The third step above, verifying that $F_u(t) = x$, is necessary because for the $(X, Y)$ values found through the $x_1$ and $x_2$ expressions, 169 it is possible that decoding through $\psi_u(X, Y)$ yields a valid $x_3$ on the curve, which would take precedence over the 170 $x_1$ or $x_2$ decoding. These $(X, Y)$ solutions must be rejected. 171 172 Since we know that exactly one or exactly three out of $\\{x_1, x_2, x_3\\}$ are valid x-coordinates for any $t$, 173 the case where either $x_1$ or $x_2$ is valid and in addition also $x_3$ is valid must mean that all three are valid. 174 This means that instead of checking whether $x_3$ is on the curve, it is also possible to check whether the other one out of 175 $x_1$ and $x_2$ is on the curve. This is significantly simpler, as it turns out. 176 177 Observe that $\psi_u$ guarantees that $x_1 + x_2 = -u.$ So given either $x = x_1$ or $x = x_2$, the other one of the two can be computed as 178 $-u - x.$ Thus, when encoding $x$ through the $x_1$ or $x_2$ expressions, one can simply check whether $g(-u-x)$ is a square, 179 and if so, not include the corresponding $t$ values in the returned set. As this does not need $X$, $Y$, or $t$, this condition can be determined 180 before those values are computed. 181 182 It is not possible that an encoding found through the $x_1$ expression decodes to a different valid x-coordinate using $x_2$ (which would 183 take precedence), for the same reason: if both $x_1$ and $x_2$ decodings were valid, $x_3$ would be valid as well, and thus take 184 precedence over both. Because of this, the $g(-u-x)$ being square test for $x_1$ and $x_2$ is the only test necessary to guarantee the found $t$ 185 values round-trip back to the input $x$ correctly. This is the reason for choosing the $(x_3, x_2, x_1)$ precedence order in the decoder; 186 any order which does not place $x_3$ first requires more complicated round-trip checks in the encoder. 187 188 ### 3.1 Switching to *v, w* coordinates 189 190 Before working out the formulas for all this, we switch to different variables for $S_u.$ Let $v = (X/Y - u)/2$, and 191 $w = 2Y.$ Or in the other direction, $X = w(u/2 + v)$ and $Y = w/2:$ 192 * $S_u'$ becomes the set of $(v, w)$ for which $w^2 (u^2 + uv + v^2 + a) = -g(u)$ and $w \neq 0.$ 193 * For $a=0$ curves, $P_u^{-1}$ can be stated for $(v,w)$ as $P_u^{'-1}(v, w) = w\left(\frac{\sqrt{-3}-1}{2}u - v\right).$ 194 * $\psi_u$ can be stated for $(v, w)$ as $\psi_u'(v, w) = (x_1, x_2, x_3, z)$, where 195 196 $$ 197 \begin{array}{lcl} 198 x_1 & = & v \\ 199 x_2 & = & -u - v \\ 200 x_3 & = & u + w^2 \\ 201 z & = & \dfrac{g(x_3)}{w}(u^2 + uv + v^2 + a) = \dfrac{-g(u)g(x_3)}{w^3} 202 \end{array} 203 $$ 204 205 We can now write the expressions for finding $(v, w)$ given $x$ explicitly, by solving each of the $\\{x_1, x_2, x_3\\}$ 206 expressions for $v$ or $w$, and using the $S_u'$ equation to find the other variable: 207 * Assuming $x = x_1$, we find $v = x$ and $w = \pm\sqrt{-g(u)/(u^2 + uv + v^2 + a)}$ (two solutions). 208 * Assuming $x = x_2$, we find $v = -u-x$ and $w = \pm\sqrt{-g(u)/(u^2 + uv + v^2 + a)}$ (two solutions). 209 * Assuming $x = x_3$, we find $w = \pm\sqrt{x-u}$ and $v = -u/2 \pm \sqrt{-w^2(4g(u) + w^2h(u))}/(2w^2)$ (four solutions). 210 211 ### 3.2 Avoiding computing all inverses 212 213 The *ElligatorSwift* algorithm as stated in Section 1 requires the computation of $L = F_u^{-1}(x)$ (the 214 set of all $t$ such that $(u, t)$ decode to $x$) in full. This is unnecessary. 215 216 Observe that the procedure of restarting with probability $(1 - \frac{\\#L}{8})$ and otherwise returning a 217 uniformly random element from $L$ is actually equivalent to always padding $L$ with $\bot$ values up to length 8, 218 picking a uniformly random element from that, restarting whenever $\bot$ is picked: 219 220 **Define** *ElligatorSwift(x)* as: 221 * Loop: 222 * Pick a uniformly random field element $u.$ 223 * Compute the set $L = F_u^{-1}(x).$ 224 * Let $T$ be the 8-element vector consisting of the elements of $L$, plus $8 - \\#L$ times $\\{\bot\\}.$ 225 * Select a uniformly random $t \in T.$ 226 * If $t \neq \bot$, return $(u, t)$; restart loop otherwise. 227 228 Now notice that the order of elements in $T$ does not matter, as all we do is pick a uniformly 229 random element in it, so we do not need to have all $\bot$ values at the end. 230 As we have 8 distinct formulas for finding $(v, w)$ (taking the variants due to $\pm$ into account), 231 we can associate every index in $T$ with exactly one of those formulas, making sure that: 232 * Formulas that yield no solutions (due to division by zero or non-existing square roots) or invalid solutions are made to return $\bot.$ 233 * For the $x_1$ and $x_2$ cases, if $g(-u-x)$ is a square, $\bot$ is returned instead (the round-trip check). 234 * In case multiple formulas would return the same non- $\bot$ result, all but one of those must be turned into $\bot$ to avoid biasing those. 235 236 The last condition above only occurs with negligible probability for cryptographically-sized curves, but is interesting 237 to take into account as it allows exhaustive testing in small groups. See [Section 3.4](#34-dealing-with-special-cases) 238 for an analysis of all the negligible cases. 239 240 If we define $T = (G_{0,u}(x), G_{1,u}(x), \ldots, G_{7,u}(x))$, with each $G_{i,u}$ matching one of the formulas, 241 the loop can be simplified to only compute one of the inverses instead of all of them: 242 243 **Define** *ElligatorSwift(x)* as: 244 * Loop: 245 * Pick a uniformly random field element $u.$ 246 * Pick a uniformly random integer $c$ in $[0,8).$ 247 * Let $t = G_{c,u}(x).$ 248 * If $t \neq \bot$, return $(u, t)$; restart loop otherwise. 249 250 This is implemented in `haskellsecp256k1_v0_1_0_ellswift_xelligatorswift_var`. 251 252 ### 3.3 Finding the inverse 253 254 To implement $G_{c,u}$, we map $c=0$ to the $x_1$ formula, $c=1$ to the $x_2$ formula, and $c=2$ and $c=3$ to the $x_3$ formula. 255 Those are then repeated as $c=4$ through $c=7$ for the other sign of $w$ (noting that in each formula, $w$ is a square root of some expression). 256 Ignoring the negligible cases, we get: 257 258 **Define** $G_{c,u}(x)$ as: 259 * If $c \in \\{0, 1, 4, 5\\}$ (for $x_1$ and $x_2$ formulas): 260 * If $g(-u-x)$ is square, return $\bot$ (as $x_3$ would be valid and take precedence). 261 * If $c \in \\{0, 4\\}$ (the $x_1$ formula) let $v = x$, otherwise let $v = -u-x$ (the $x_2$ formula) 262 * Let $s = -g(u)/(u^2 + uv + v^2 + a)$ (using $s = w^2$ in what follows). 263 * Otherwise, when $c \in \\{2, 3, 6, 7\\}$ (for $x_3$ formulas): 264 * Let $s = x-u.$ 265 * Let $r = \sqrt{-s(4g(u) + sh(u))}.$ 266 * Let $v = (r/s - u)/2$ if $c \in \\{3, 7\\}$; $(-r/s - u)/2$ otherwise. 267 * Let $w = \sqrt{s}.$ 268 * Depending on $c:$ 269 * If $c \in \\{0, 1, 2, 3\\}:$ return $P_u^{'-1}(v, w).$ 270 * If $c \in \\{4, 5, 6, 7\\}:$ return $P_u^{'-1}(v, -w).$ 271 272 Whenever a square root of a non-square is taken, $\bot$ is returned; for both square roots this happens with roughly 273 50% on random inputs. Similarly, when a division by 0 would occur, $\bot$ is returned as well; this will only happen 274 with negligible probability. A division by 0 in the first branch in fact cannot occur at all, because $u^2 + uv + v^2 + a = 0$ 275 implies $g(-u-x) = g(x)$ which would mean the $g(-u-x)$ is square condition has triggered 276 and $\bot$ would have been returned already. 277 278 **Note**: In the paper, the $case$ variable corresponds roughly to the $c$ above, but only takes on 4 possible values (1 to 4). 279 The conditional negation of $w$ at the end is done randomly, which is equivalent, but makes testing harder. We choose to 280 have the $G_{c,u}$ be deterministic, and capture all choices in $c.$ 281 282 Now observe that the $c \in \\{1, 5\\}$ and $c \in \\{3, 7\\}$ conditions effectively perform the same $v \rightarrow -u-v$ 283 transformation. Furthermore, that transformation has no effect on $s$ in the first branch 284 as $u^2 + ux + x^2 + a = u^2 + u(-u-x) + (-u-x)^2 + a.$ Thus we can extract it out and move it down: 285 286 **Define** $G_{c,u}(x)$ as: 287 * If $c \in \\{0, 1, 4, 5\\}:$ 288 * If $g(-u-x)$ is square, return $\bot.$ 289 * Let $s = -g(u)/(u^2 + ux + x^2 + a).$ 290 * Let $v = x.$ 291 * Otherwise, when $c \in \\{2, 3, 6, 7\\}:$ 292 * Let $s = x-u.$ 293 * Let $r = \sqrt{-s(4g(u) + sh(u))}.$ 294 * Let $v = (r/s - u)/2.$ 295 * Let $w = \sqrt{s}.$ 296 * Depending on $c:$ 297 * If $c \in \\{0, 2\\}:$ return $P_u^{'-1}(v, w).$ 298 * If $c \in \\{1, 3\\}:$ return $P_u^{'-1}(-u-v, w).$ 299 * If $c \in \\{4, 6\\}:$ return $P_u^{'-1}(v, -w).$ 300 * If $c \in \\{5, 7\\}:$ return $P_u^{'-1}(-u-v, -w).$ 301 302 This shows there will always be exactly 0, 4, or 8 $t$ values for a given $(u, x)$ input. 303 There can be 0, 1, or 2 $(v, w)$ pairs before invoking $P_u^{'-1}$, and each results in 4 distinct $t$ values. 304 305 ### 3.4 Dealing with special cases 306 307 As mentioned before there are a few cases to deal with which only happen in a negligibly small subset of inputs. 308 For cryptographically sized fields, if only random inputs are going to be considered, it is unnecessary to deal with these. Still, for completeness 309 we analyse them here. They generally fall into two categories: cases in which the encoder would produce $t$ values that 310 do not decode back to $x$ (or at least cannot guarantee that they do), and cases in which the encoder might produce the same 311 $t$ value for multiple $c$ inputs (thereby biasing that encoding): 312 313 * In the branch for $x_1$ and $x_2$ (where $c \in \\{0, 1, 4, 5\\}$): 314 * When $g(u) = 0$, we would have $s=w=Y=0$, which is not on $S_u.$ This is only possible on even-ordered curves. 315 Excluding this also removes the one condition under which the simplified check for $x_3$ on the curve 316 fails (namely when $g(x_1)=g(x_2)=0$ but $g(x_3)$ is not square). 317 This does exclude some valid encodings: when both $g(u)=0$ and $u^2+ux+x^2+a=0$ (also implying $g(x)=0$), 318 the $S_u'$ equation degenerates to $0 = 0$, and many valid $t$ values may exist. Yet, these cannot be targeted uniformly by the 319 encoder anyway as there will generally be more than 8. 320 * When $g(x) = 0$, the same $t$ would be produced as in the $x_3$ branch (where $c \in \\{2, 3, 6, 7\\}$) which we give precedence 321 as it can deal with $g(u)=0$. 322 This is again only possible on even-ordered curves. 323 * In the branch for $x_3$ (where $c \in \\{2, 3, 6, 7\\}$): 324 * When $s=0$, a division by zero would occur. 325 * When $v = -u-v$ and $c \in \\{3, 7\\}$, the same $t$ would be returned as in the $c \in \\{2, 6\\}$ cases. 326 It is equivalent to checking whether $r=0$. 327 This cannot occur in the $x_1$ or $x_2$ branches, as it would trigger the $g(-u-x)$ is square condition. 328 A similar concern for $w = -w$ does not exist, as $w=0$ is already impossible in both branches: in the first 329 it requires $g(u)=0$ which is already outlawed on even-ordered curves and impossible on others; in the second it would trigger division by zero. 330 * Curve-specific special cases also exist that need to be rejected, because they result in $(u,t)$ which is invalid to the decoder, or because of division by zero in the encoder: 331 * For $a=0$ curves, when $u=0$ or when $t=0$. The latter can only be reached by the encoder when $g(u)=0$, which requires an even-ordered curve. 332 * For $a \neq 0$ curves, when $X_0(u)=0$, when $h(u)t^2 = -1$, or when $w(u + 2v) = 2X_0(u)$ while also either $w \neq 2Y_0(u)$ or $h(u)=0$. 333 334 **Define** a version of $G_{c,u}(x)$ which deals with all these cases: 335 * If $a=0$ and $u=0$, return $\bot.$ 336 * If $a \neq 0$ and $X_0(u)=0$, return $\bot.$ 337 * If $c \in \\{0, 1, 4, 5\\}:$ 338 * If $g(u) = 0$ or $g(x) = 0$, return $\bot$ (even curves only). 339 * If $g(-u-x)$ is square, return $\bot.$ 340 * Let $s = -g(u)/(u^2 + ux + x^2 + a)$ (cannot cause division by zero). 341 * Let $v = x.$ 342 * Otherwise, when $c \in \\{2, 3, 6, 7\\}:$ 343 * Let $s = x-u.$ 344 * Let $r = \sqrt{-s(4g(u) + sh(u))}$; return $\bot$ if not square. 345 * If $c \in \\{3, 7\\}$ and $r=0$, return $\bot.$ 346 * If $s = 0$, return $\bot.$ 347 * Let $v = (r/s - u)/2.$ 348 * Let $w = \sqrt{s}$; return $\bot$ if not square. 349 * If $a \neq 0$ and $w(u+2v) = 2X_0(u)$ and either $w \neq 2Y_0(u)$ or $h(u) = 0$, return $\bot.$ 350 * Depending on $c:$ 351 * If $c \in \\{0, 2\\}$, let $t = P_u^{'-1}(v, w).$ 352 * If $c \in \\{1, 3\\}$, let $t = P_u^{'-1}(-u-v, w).$ 353 * If $c \in \\{4, 6\\}$, let $t = P_u^{'-1}(v, -w).$ 354 * If $c \in \\{5, 7\\}$, let $t = P_u^{'-1}(-u-v, -w).$ 355 * If $a=0$ and $t=0$, return $\bot$ (even curves only). 356 * If $a \neq 0$ and $h(u)t^2 = -1$, return $\bot.$ 357 * Return $t.$ 358 359 Given any $u$, using this algorithm over all $x$ and $c$ values, every $t$ value will be reached exactly once, 360 for an $x$ for which $F_u(t) = x$ holds, except for these cases that will not be reached: 361 * All cases where $P_u(t)$ is not defined: 362 * For $a=0$ curves, when $u=0$, $t=0$, or $g(u) = -t^2.$ 363 * For $a \neq 0$ curves, when $h(u)t^2 = -1$, $X_0(u) = 0$, or $Y_0(u) (1 - h(u) t^2) = 2X_0(u)t.$ 364 * When $g(u)=0$, the potentially many $t$ values that decode to an $x$ satisfying $g(x)=0$ using the $x_2$ formula. These were excluded by the $g(u)=0$ condition in the $c \in \\{0, 1, 4, 5\\}$ branch. 365 366 These cases form a negligible subset of all $(u, t)$ for cryptographically sized curves. 367 368 ### 3.5 Encoding for `secp256k1` 369 370 Specialized for odd-ordered $a=0$ curves: 371 372 **Define** $G_{c,u}(x)$ as: 373 * If $u=0$, return $\bot.$ 374 * If $c \in \\{0, 1, 4, 5\\}:$ 375 * If $(-u-x)^3 + b$ is square, return $\bot$ 376 * Let $s = -(u^3 + b)/(u^2 + ux + x^2)$ (cannot cause division by 0). 377 * Let $v = x.$ 378 * Otherwise, when $c \in \\{2, 3, 6, 7\\}:$ 379 * Let $s = x-u.$ 380 * Let $r = \sqrt{-s(4(u^3 + b) + 3su^2)}$; return $\bot$ if not square. 381 * If $c \in \\{3, 7\\}$ and $r=0$, return $\bot.$ 382 * If $s = 0$, return $\bot.$ 383 * Let $v = (r/s - u)/2.$ 384 * Let $w = \sqrt{s}$; return $\bot$ if not square. 385 * Depending on $c:$ 386 * If $c \in \\{0, 2\\}:$ return $w(\frac{\sqrt{-3}-1}{2}u - v).$ 387 * If $c \in \\{1, 3\\}:$ return $w(\frac{\sqrt{-3}+1}{2}u + v).$ 388 * If $c \in \\{4, 6\\}:$ return $w(\frac{-\sqrt{-3}+1}{2}u + v).$ 389 * If $c \in \\{5, 7\\}:$ return $w(\frac{-\sqrt{-3}-1}{2}u - v).$ 390 391 This is implemented in `haskellsecp256k1_v0_1_0_ellswift_xswiftec_inv_var`. 392 393 And the x-only ElligatorSwift encoding algorithm is still: 394 395 **Define** *ElligatorSwift(x)* as: 396 * Loop: 397 * Pick a uniformly random field element $u.$ 398 * Pick a uniformly random integer $c$ in $[0,8).$ 399 * Let $t = G_{c,u}(x).$ 400 * If $t \neq \bot$, return $(u, t)$; restart loop otherwise. 401 402 Note that this logic does not take the remapped $u=0$, $t=0$, and $g(u) = -t^2$ cases into account; it just avoids them. 403 While it is not impossible to make the encoder target them, this would increase the maximum number of $t$ values for a given $(u, x)$ 404 combination beyond 8, and thereby slow down the ElligatorSwift loop proportionally, for a negligible gain in uniformity. 405 406 ## 4. Encoding and decoding full *(x, y)* coordinates 407 408 So far we have only addressed encoding and decoding x-coordinates, but in some cases an encoding 409 for full points with $(x, y)$ coordinates is desirable. It is possible to encode this information 410 in $t$ as well. 411 412 Note that for any $(X, Y) \in S_u$, $(\pm X, \pm Y)$ are all on $S_u.$ Moreover, all of these are 413 mapped to the same x-coordinate. Negating $X$ or negating $Y$ just results in $x_1$ and $x_2$ 414 being swapped, and does not affect $x_3.$ This will not change the outcome x-coordinate as the order 415 of $x_1$ and $x_2$ only matters if both were to be valid, and in that case $x_3$ would be used instead. 416 417 Still, these four $(X, Y)$ combinations all correspond to distinct $t$ values, so we can encode 418 the sign of the y-coordinate in the sign of $X$ or the sign of $Y.$ They correspond to the 419 four distinct $P_u^{'-1}$ calls in the definition of $G_{u,c}.$ 420 421 **Note**: In the paper, the sign of the y coordinate is encoded in a separately-coded bit. 422 423 To encode the sign of $y$ in the sign of $Y:$ 424 425 **Define** *Decode(u, t)* for full $(x, y)$ as: 426 * Let $(X, Y) = P_u(t).$ 427 * Let $x$ be the first value in $(u + 4Y^2, \frac{-X}{2Y} - \frac{u}{2}, \frac{X}{2Y} - \frac{u}{2})$ for which $g(x)$ is square. 428 * Let $y = \sqrt{g(x)}.$ 429 * If $sign(y) = sign(Y)$, return $(x, y)$; otherwise return $(x, -y).$ 430 431 And encoding would be done using a $G_{c,u}(x, y)$ function defined as: 432 433 **Define** $G_{c,u}(x, y)$ as: 434 * If $c \in \\{0, 1\\}:$ 435 * If $g(u) = 0$ or $g(x) = 0$, return $\bot$ (even curves only). 436 * If $g(-u-x)$ is square, return $\bot.$ 437 * Let $s = -g(u)/(u^2 + ux + x^2 + a)$ (cannot cause division by zero). 438 * Let $v = x.$ 439 * Otherwise, when $c \in \\{2, 3\\}:$ 440 * Let $s = x-u.$ 441 * Let $r = \sqrt{-s(4g(u) + sh(u))}$; return $\bot$ if not square. 442 * If $c = 3$ and $r = 0$, return $\bot.$ 443 * Let $v = (r/s - u)/2.$ 444 * Let $w = \sqrt{s}$; return $\bot$ if not square. 445 * Let $w' = w$ if $sign(w/2) = sign(y)$; $-w$ otherwise. 446 * Depending on $c:$ 447 * If $c \in \\{0, 2\\}:$ return $P_u^{'-1}(v, w').$ 448 * If $c \in \\{1, 3\\}:$ return $P_u^{'-1}(-u-v, w').$ 449 450 Note that $c$ now only ranges $[0,4)$, as the sign of $w'$ is decided based on that of $y$, rather than on $c.$ 451 This change makes some valid encodings unreachable: when $y = 0$ and $sign(Y) \neq sign(0)$. 452 453 In the above logic, $sign$ can be implemented in several ways, such as parity of the integer representation 454 of the input field element (for prime-sized fields) or the quadratic residuosity (for fields where 455 $-1$ is not square). The choice does not matter, as long as it only takes on two possible values, and for $x \neq 0$ it holds that $sign(x) \neq sign(-x)$. 456 457 ### 4.1 Full *(x, y)* coordinates for `secp256k1` 458 459 For $a=0$ curves, there is another option. Note that for those, 460 the $P_u(t)$ function translates negations of $t$ to negations of (both) $X$ and $Y.$ Thus, we can use $sign(t)$ to 461 encode the y-coordinate directly. Combined with the earlier remapping to guarantee all inputs land on the curve, we get 462 as decoder: 463 464 **Define** *Decode(u, t)* as: 465 * Let $u'=u$ if $u \neq 0$; $1$ otherwise. 466 * Let $t'=t$ if $t \neq 0$; $1$ otherwise. 467 * Let $t''=t'$ if $u'^3 + b + t'^2 \neq 0$; $2t'$ otherwise. 468 * Let $X = \dfrac{u'^3 + b - t''^2}{2t''}.$ 469 * Let $Y = \dfrac{X + t''}{u'\sqrt{-3}}.$ 470 * Let $x$ be the first element of $(u' + 4Y^2, \frac{-X}{2Y} - \frac{u'}{2}, \frac{X}{2Y} - \frac{u'}{2})$ for which $g(x)$ is square. 471 * Let $y = \sqrt{g(x)}.$ 472 * Return $(x, y)$ if $sign(y) = sign(t)$; $(x, -y)$ otherwise. 473 474 This is implemented in `haskellsecp256k1_v0_1_0_ellswift_swiftec_var`. The used $sign(x)$ function is the parity of $x$ when represented as in integer in $[0,q).$ 475 476 The corresponding encoder would invoke the x-only one, but negating the output $t$ if $sign(t) \neq sign(y).$ 477 478 This is implemented in `haskellsecp256k1_v0_1_0_ellswift_elligatorswift_var`. 479 480 Note that this is only intended for encoding points where both the x-coordinate and y-coordinate are unpredictable. When encoding x-only points 481 where the y-coordinate is implicitly even (or implicitly square, or implicitly in $[0,q/2]$), the encoder in 482 [Section 3.5](#35-encoding-for-secp256k1) must be used, or a bias is reintroduced that undoes all the benefit of using ElligatorSwift 483 in the first place.