3 * Arithmetic modulo 2^255 - 19
5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_F25519_H
29 #define CATACOMB_F25519_H
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
39 #ifndef CATACOMB_QFARITH_H
43 /*----- Data structures ---------------------------------------------------*/
50 #if !defined(F25519_IMPL) && defined(HAVE_INT64)
51 # define F25519_IMPL 26
55 # define F25519_IMPL 10
59 typedef int32 f25519_piece
;
62 typedef int16 f25519_piece
;
65 /*----- Functions provided ------------------------------------------------*/
67 /* --- @f25519_set@ --- *
69 * Arguments: @f25519 *z@ = where to write the result
70 * @int a@ = a small-ish constant
74 * Use: Sets @z@ to equal @a@.
77 extern void f25519_set(f25519 */
*x*/
, int /*a*/);
79 /* --- @f25519_load@ --- *
81 * Arguments: @f25519 *z@ = where to store the result
82 * @const octet xv[32]@ = source to read
86 * Use: Reads an element of %$\gf{2^{255} - 19}$% in external
87 * representation from @xv@ and stores it in @z@.
89 * External representation is little-endian base-256. Elements
90 * have multiple encodings, which are not produced by correct
91 * software; use of noncanonical encodings is not an error, and
92 * toleration of them is considered a performance feature.
94 * Some specifications, e.g., RFC7748, require the topmost bit
95 * (i.e., bit 7 of @wv[31]@) to be ignored. Callers
96 * implementing such specifications should clear the bit
97 * explicitly. (It's much easier for a caller who wants the bit
98 * to be ignored to clear it than for a caller who wants the bit
99 * to be significant to apply the necessary change by hand.)
102 extern void f25519_load(f25519 */
*z*/
, const octet
/*xv*/[32]);
104 /* --- @f25519_store@ --- *
106 * Arguments: @octet zv[32]@ = where to write the result
107 * @const f25519 *x@ = the field element to write
111 * Use: Stores a field element in the given octet vector in external
112 * representation. A canonical encoding is always stored, so,
113 * in particular, the top bit of @xv[31]@ is always left clear.
116 extern void f25519_store(octet
/*zv*/[32], const f25519 */
*x*/
);
118 /* --- @f25519_pick2@ --- *
120 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
121 * @const f25519 *x, *y@ = two operands
122 * @uint32 m@ = a mask
126 * Use: If @m@ is zero, set @z = y@; if @m@ is all-bits-set, then set
127 * @z = x@. If @m@ has some other value, then scramble @z@ in
131 extern void f25519_pick2(f25519 */
*z*/
, const f25519 */
*x*/
,
132 const f25519 */
*y*/
, uint32
/*m*/);
134 /* --- @f25519_pickn@ --- *
136 * Arguments: @f25519 *z@ = where to put the result
137 * @const f25519 *v@ = a table of entries
138 * @size_t n@ = the number of entries in @v@
139 * @size_t i@ = an index
143 * Use: If @0 <= i < n < 32@ then set @z = v[i]@. If @n >= 32@ then
144 * do something unhelpful; otherwise, if @i >= n@ then set @z@
148 extern void f25519_pickn(f25519 */
*z*/
, const f25519 */
*v*/
, size_t /*n*/,
151 /* --- @f25519_condswap@ --- *
153 * Arguments: @f25519 *x, *y@ = two operands
154 * @uint32 m@ = a mask
158 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
159 * exchange @x@ and @y@. If @m@ has some other value, then
160 * scramble @x@ and @y@ in an unhelpful way.
163 extern void f25519_condswap(f25519 */
*x*/
, f25519 */
*y*/
, uint32
/*m*/);
165 /* --- @f25519_add@ --- *
167 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
168 * @const f25519 *x, *y@ = two operands
172 * Use: Set @z@ to the sum %$x + y$%.
175 extern void f25519_add(f25519 */
*z*/
,
176 const f25519 */
*x*/
, const f25519 */
*y*/
);
178 /* --- @f25519_sub@ --- *
180 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
181 * @const f25519 *x, *y@ = two operands
185 * Use: Set @z@ to the difference %$x - y$%.
188 extern void f25519_sub(f25519 */
*z*/
,
189 const f25519 */
*x*/
, const f25519 */
*y*/
);
191 /* --- @f25519_neg@ --- *
193 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
194 * @const f25519 *x@ = an operand
201 extern void f25519_neg(f25519 */
*z*/
, const f25519 */
*x*/
);
203 /* --- @f25519_condneg@ --- *
205 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
206 * @const f25519 *x@ = an operand
207 * @uint32 m@ = a mask
211 * Use: If @m@ is zero, set @z = x@; if @m@ is all-bits-set, then set
212 * @z = -x@. If @m@ has some other value then scramble @z@ in
216 extern void f25519_condneg(f25519 */
*z*/
, const f25519 */
*x*/
, uint32
/*m*/);
218 /* --- @f25519_mulconst@ --- *
220 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
221 * @const f25519 *x@ = an operand
222 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
226 * Use: Set @z@ to the product %$a x$%.
229 extern void f25519_mulconst(f25519 */
*z*/
, const f25519 */
*x*/
, long /*a*/);
231 /* --- @f25519_mul@ --- *
233 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
234 * @const f25519 *x, *y@ = two operands
238 * Use: Set @z@ to the product %$x y$%.
241 extern void f25519_mul(f25519 */
*z*/
,
242 const f25519 */
*x*/
, const f25519 */
*y*/
);
244 /* --- @f25519_sqr@ --- *
246 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
247 * @const f25519 *x@ = an operand
251 * Use: Set @z@ to the square %$x^2$%.
254 extern void f25519_sqr(f25519 */
*z*/
, const f25519 */
*x*/
);
256 /* --- @f25519_inv@ --- *
258 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
259 * @const f25519 *x@ = an operand
263 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
264 * %$x = 0$% then @z@ is set to zero. This is considered a
268 extern void f25519_inv(f25519 */
*z*/
, const f25519 */
*x*/
);
270 /* --- @f25519_quosqrt@ --- *
272 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
273 * @const f25519 *x, *y@ = two operands
275 * Returns: Zero if successful, @-1@ if %$x/y$% is not a square.
277 * Use: Stores in @z@ the one of the square roots %$\pm\sqrt{x/y}$%.
278 * If %$x = y = 0% then the result is zero; if %$y = 0$% but %$x
279 * \ne 0$% then the operation fails. If you wanted a specific
280 * square root then you'll have to pick it yourself.
283 extern int f25519_quosqrt(f25519 */
*z*/
,
284 const f25519 */
*x*/
, const f25519 */
*y*/
);
286 /*----- That's all, folks -------------------------------------------------*/