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
58 /*----- Functions provided ------------------------------------------------*/
60 /* --- @f25519_set@ --- *
62 * Arguments: @f25519 *z@ = where to write the result
63 * @int a@ = a small-ish constant
67 * Use: Sets @z@ to equal @a@.
70 extern void f25519_set(f25519 */
*x*/
, int /*a*/);
72 /* --- @f25519_load@ --- *
74 * Arguments: @f25519 *z@ = where to store the result
75 * @const octet xv[32]@ = source to read
79 * Use: Reads an element of %$\gf{2^{255} - 19}$% in external
80 * representation from @xv@ and stores it in @z@.
82 * External representation is little-endian base-256. Elements
83 * have multiple encodings, which are not produced by correct
84 * software; use of noncanonical encodings is not an error, and
85 * toleration of them is considered a performance feature.
87 * Some specifications, e.g., RFC7748, require the topmost bit
88 * (i.e., bit 7 of @wv[31]@) to be ignored. Callers
89 * implementing such specifications should clear the bit
90 * explicitly. (It's much easier for a caller who wants the bit
91 * to be ignored to clear it than for a caller who wants the bit
92 * to be significant to apply the necessary change by hand.)
95 extern void f25519_load(f25519 */
*z*/
, const octet
/*xv*/[32]);
97 /* --- @f25519_store@ --- *
99 * Arguments: @octet zv[32]@ = where to write the result
100 * @const f25519 *x@ = the field element to write
104 * Use: Stores a field element in the given octet vector in external
105 * representation. A canonical encoding is always stored, so,
106 * in particular, the top bit of @xv[31]@ is always left clear.
109 extern void f25519_store(octet
/*zv*/[32], const f25519 */
*x*/
);
111 /* --- @f25519_pick2@ --- *
113 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
114 * @const f25519 *x, *y@ = two operands
115 * @uint32 m@ = a mask
119 * Use: If @m@ is zero, set @z = y@; if @m@ is all-bits-set, then set
120 * @z = x@. If @m@ has some other value, then scramble @z@ in
124 extern void f25519_pick2(f25519 */
*z*/
, const f25519 */
*x*/
,
125 const f25519 */
*y*/
, uint32
/*m*/);
127 /* --- @f25519_pickn@ --- *
129 * Arguments: @f25519 *z@ = where to put the result
130 * @const f25519 *v@ = a table of entries
131 * @size_t n@ = the number of entries in @v@
132 * @size_t i@ = an index
136 * Use: If @0 <= i < n < 32@ then set @z = v[i]@. If @n >= 32@ then
137 * do something unhelpful; otherwise, if @i >= n@ then set @z@
141 extern void f25519_pickn(f25519 */
*z*/
, const f25519 */
*v*/
, size_t /*n*/,
144 /* --- @f25519_condswap@ --- *
146 * Arguments: @f25519 *x, *y@ = two operands
147 * @uint32 m@ = a mask
151 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
152 * exchange @x@ and @y@. If @m@ has some other value, then
153 * scramble @x@ and @y@ in an unhelpful way.
156 extern void f25519_condswap(f25519 */
*x*/
, f25519 */
*y*/
, uint32
/*m*/);
158 /* --- @f25519_add@ --- *
160 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
161 * @const f25519 *x, *y@ = two operands
165 * Use: Set @z@ to the sum %$x + y$%.
168 extern void f25519_add(f25519 */
*z*/
,
169 const f25519 */
*x*/
, const f25519 */
*y*/
);
171 /* --- @f25519_sub@ --- *
173 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
174 * @const f25519 *x, *y@ = two operands
178 * Use: Set @z@ to the difference %$x - y$%.
181 extern void f25519_sub(f25519 */
*z*/
,
182 const f25519 */
*x*/
, const f25519 */
*y*/
);
184 /* --- @f25519_neg@ --- *
186 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
187 * @const f25519 *x@ = an operand
194 extern void f25519_neg(f25519 */
*z*/
, const f25519 */
*x*/
);
196 /* --- @f25519_condneg@ --- *
198 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
199 * @const f25519 *x@ = an operand
200 * @uint32 m@ = a mask
204 * Use: If @m@ is zero, set @z = x@; if @m@ is all-bits-set, then set
205 * @z = -x@. If @m@ has some other value then scramble @z@ in
209 extern void f25519_condneg(f25519 */
*z*/
, const f25519 */
*x*/
, uint32
/*m*/);
211 /* --- @f25519_mulconst@ --- *
213 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
214 * @const f25519 *x@ = an operand
215 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
219 * Use: Set @z@ to the product %$a x$%.
222 extern void f25519_mulconst(f25519 */
*z*/
, const f25519 */
*x*/
, long /*a*/);
224 /* --- @f25519_mul@ --- *
226 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
227 * @const f25519 *x, *y@ = two operands
231 * Use: Set @z@ to the product %$x y$%.
234 extern void f25519_mul(f25519 */
*z*/
,
235 const f25519 */
*x*/
, const f25519 */
*y*/
);
237 /* --- @f25519_sqr@ --- *
239 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
240 * @const f25519 *x@ = an operand
244 * Use: Set @z@ to the square %$x^2$%.
247 extern void f25519_sqr(f25519 */
*z*/
, const f25519 */
*x*/
);
249 /* --- @f25519_inv@ --- *
251 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
252 * @const f25519 *x@ = an operand
256 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
257 * %$x = 0$% then @z@ is set to zero. This is considered a
261 extern void f25519_inv(f25519 */
*z*/
, const f25519 */
*x*/
);
263 /* --- @f25519_quosqrt@ --- *
265 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
266 * @const f25519 *x, *y@ = two operands
268 * Returns: Zero if successful, @-1@ if %$x/y$% is not a square.
270 * Use: Stores in @z@ the one of the square roots %$\pm\sqrt{x/y}$%.
271 * If %$x = y = 0% then the result is zero; if %$y = 0$% but %$x
272 * \ne 0$% then the operation fails. If you wanted a specific
273 * square root then you'll have to pick it yourself.
276 extern int f25519_quosqrt(f25519 */
*z*/
,
277 const f25519 */
*x*/
, const f25519 */
*y*/
);
279 /*----- That's all, folks -------------------------------------------------*/