3 * Arithmetic modulo 2^255 - 19
5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of secnet.
11 * See README for full list of copyright holders.
13 * secnet is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version d of the License, or
16 * (at your option) any later version.
18 * secnet is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * version 3 along with secnet; if not, see
25 * https://www.gnu.org/licenses/gpl.html.
27 * This file was originally part of Catacomb, but has been automatically
28 * modified for incorporation into secnet: see `import-catacomb-crypto'
31 * Catacomb is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU Library General Public License as
33 * published by the Free Software Foundation; either version 2 of the
34 * License, or (at your option) any later version.
36 * Catacomb is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU Library General Public License for more details.
41 * You should have received a copy of the GNU Library General Public
42 * License along with Catacomb; if not, write to the Free
43 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
47 #ifndef CATACOMB_F25519_H
48 #define CATACOMB_F25519_H
54 /*----- Header files ------------------------------------------------------*/
56 #include "fake-mLib-bits.h"
58 #ifndef CATACOMB_QFARITH_H
62 /*----- Data structures ---------------------------------------------------*/
68 typedef int32 f25519_piece
;
70 /*----- Functions provided ------------------------------------------------*/
72 /* --- @f25519_set@ --- *
74 * Arguments: @f25519 *z@ = where to write the result
75 * @int a@ = a small-ish constant
79 * Use: Sets @z@ to equal @a@.
82 extern void f25519_set(f25519 */
*x*/
, int /*a*/);
84 /* --- @f25519_load@ --- *
86 * Arguments: @f25519 *z@ = where to store the result
87 * @const octet xv[32]@ = source to read
91 * Use: Reads an element of %$\gf{2^{255} - 19}$% in external
92 * representation from @xv@ and stores it in @z@.
94 * External representation is little-endian base-256. Elements
95 * have multiple encodings, which are not produced by correct
96 * software; use of noncanonical encodings is not an error, and
97 * toleration of them is considered a performance feature.
99 * Some specifications, e.g., RFC7748, require the topmost bit
100 * (i.e., bit 7 of @wv[31]@) to be ignored. Callers
101 * implementing such specifications should clear the bit
102 * explicitly. (It's much easier for a caller who wants the bit
103 * to be ignored to clear it than for a caller who wants the bit
104 * to be significant to apply the necessary change by hand.)
107 extern void f25519_load(f25519 */
*z*/
, const octet
/*xv*/[32]);
109 /* --- @f25519_store@ --- *
111 * Arguments: @octet zv[32]@ = where to write the result
112 * @const f25519 *x@ = the field element to write
116 * Use: Stores a field element in the given octet vector in external
117 * representation. A canonical encoding is always stored, so,
118 * in particular, the top bit of @xv[31]@ is always left clear.
121 extern void f25519_store(octet
/*zv*/[32], const f25519 */
*x*/
);
123 /* --- @f25519_pick2@ --- *
125 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
126 * @const f25519 *x, *y@ = two operands
127 * @uint32 m@ = a mask
131 * Use: If @m@ is zero, set @z = y@; if @m@ is all-bits-set, then set
132 * @z = x@. If @m@ has some other value, then scramble @z@ in
136 extern void f25519_pick2(f25519 */
*z*/
, const f25519 */
*x*/
,
137 const f25519 */
*y*/
, uint32
/*m*/);
139 /* --- @f25519_pickn@ --- *
141 * Arguments: @f25519 *z@ = where to put the result
142 * @const f25519 *v@ = a table of entries
143 * @size_t n@ = the number of entries in @v@
144 * @size_t i@ = an index
148 * Use: If @0 <= i < n < 32@ then set @z = v[i]@. If @n >= 32@ then
149 * do something unhelpful; otherwise, if @i >= n@ then set @z@
153 extern void f25519_pickn(f25519 */
*z*/
, const f25519 */
*v*/
, size_t /*n*/,
156 /* --- @f25519_condswap@ --- *
158 * Arguments: @f25519 *x, *y@ = two operands
159 * @uint32 m@ = a mask
163 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
164 * exchange @x@ and @y@. If @m@ has some other value, then
165 * scramble @x@ and @y@ in an unhelpful way.
168 extern void f25519_condswap(f25519 */
*x*/
, f25519 */
*y*/
, uint32
/*m*/);
170 /* --- @f25519_add@ --- *
172 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
173 * @const f25519 *x, *y@ = two operands
177 * Use: Set @z@ to the sum %$x + y$%.
180 extern void f25519_add(f25519 */
*z*/
,
181 const f25519 */
*x*/
, const f25519 */
*y*/
);
183 /* --- @f25519_sub@ --- *
185 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
186 * @const f25519 *x, *y@ = two operands
190 * Use: Set @z@ to the difference %$x - y$%.
193 extern void f25519_sub(f25519 */
*z*/
,
194 const f25519 */
*x*/
, const f25519 */
*y*/
);
196 /* --- @f25519_neg@ --- *
198 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
199 * @const f25519 *x@ = an operand
206 extern void f25519_neg(f25519 */
*z*/
, const f25519 */
*x*/
);
208 /* --- @f25519_condneg@ --- *
210 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
211 * @const f25519 *x@ = an operand
212 * @uint32 m@ = a mask
216 * Use: If @m@ is zero, set @z = x@; if @m@ is all-bits-set, then set
217 * @z = -x@. If @m@ has some other value then scramble @z@ in
221 extern void f25519_condneg(f25519 */
*z*/
, const f25519 */
*x*/
, uint32
/*m*/);
223 /* --- @f25519_mulconst@ --- *
225 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
226 * @const f25519 *x@ = an operand
227 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
231 * Use: Set @z@ to the product %$a x$%.
234 extern void f25519_mulconst(f25519 */
*z*/
, const f25519 */
*x*/
, long /*a*/);
236 /* --- @f25519_mul@ --- *
238 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
239 * @const f25519 *x, *y@ = two operands
243 * Use: Set @z@ to the product %$x y$%.
246 extern void f25519_mul(f25519 */
*z*/
,
247 const f25519 */
*x*/
, const f25519 */
*y*/
);
249 /* --- @f25519_sqr@ --- *
251 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
252 * @const f25519 *x@ = an operand
256 * Use: Set @z@ to the square %$x^2$%.
259 extern void f25519_sqr(f25519 */
*z*/
, const f25519 */
*x*/
);
261 /* --- @f25519_inv@ --- *
263 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
264 * @const f25519 *x@ = an operand
268 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
269 * %$x = 0$% then @z@ is set to zero. This is considered a
273 extern void f25519_inv(f25519 */
*z*/
, const f25519 */
*x*/
);
275 /* --- @f25519_quosqrt@ --- *
277 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
278 * @const f25519 *x, *y@ = two operands
280 * Returns: Zero if successful, @-1@ if %$x/y$% is not a square.
282 * Use: Stores in @z@ the one of the square roots %$\pm\sqrt{x/y}$%.
283 * If %$x = y = 0% then the result is zero; if %$y = 0$% but %$x
284 * \ne 0$% then the operation fails. If you wanted a specific
285 * square root then you'll have to pick it yourself.
288 extern int f25519_quosqrt(f25519 */
*z*/
,
289 const f25519 */
*x*/
, const f25519 */
*y*/
);
291 /*----- That's all, folks -------------------------------------------------*/