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_condswap@ --- *
113 * Arguments: @f25519 *x, *y@ = two operands
114 * @uint32 m@ = a mask
118 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
119 * exchange @x@ and @y@. If @m@ has some other value, then
120 * scramble @x@ and @y@ in an unhelpful way.
123 extern void f25519_condswap(f25519 */
*x*/
, f25519 */
*y*/
, uint32
/*m*/);
125 /* --- @f25519_add@ --- *
127 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
128 * @const f25519 *x, *y@ = two operands
132 * Use: Set @z@ to the sum %$x + y$%.
135 extern void f25519_add(f25519 */
*z*/
,
136 const f25519 */
*x*/
, const f25519 */
*y*/
);
138 /* --- @f25519_sub@ --- *
140 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
141 * @const f25519 *x, *y@ = two operands
145 * Use: Set @z@ to the difference %$x - y$%.
148 extern void f25519_sub(f25519 */
*z*/
,
149 const f25519 */
*x*/
, const f25519 */
*y*/
);
151 /* --- @f25519_mulconst@ --- *
153 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
154 * @const f25519 *x@ = an operand
155 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
159 * Use: Set @z@ to the product %$a x$%.
162 extern void f25519_mulconst(f25519 */
*z*/
, const f25519 */
*x*/
, long /*a*/);
164 /* --- @f25519_mul@ --- *
166 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
167 * @const f25519 *x, *y@ = two operands
171 * Use: Set @z@ to the product %$x y$%.
174 extern void f25519_mul(f25519 */
*z*/
,
175 const f25519 */
*x*/
, const f25519 */
*y*/
);
177 /* --- @f25519_sqr@ --- *
179 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
180 * @const f25519 *x@ = an operand
184 * Use: Set @z@ to the square %$x^2$%.
187 extern void f25519_sqr(f25519 */
*z*/
, const f25519 */
*x*/
);
189 /* --- @f25519_inv@ --- *
191 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
192 * @const f25519 *x@ = an operand
196 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
197 * %$x = 0$% then @z@ is set to zero. This is considered a
201 extern void f25519_inv(f25519 */
*z*/
, const f25519 */
*x*/
);
203 /*----- That's all, folks -------------------------------------------------*/