2 * f25519.h: arithmetic modulo 2^255 - 19
5 * This file is Free Software. It has been modified to as part of its
6 * incorporation into secnet.
8 * Copyright 2017 Mark Wooding
10 * You may redistribute this file and/or modify it under the terms of
11 * the permissive licence shown below.
13 * You may redistribute secnet as a whole and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3, or (at your option) any
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see
25 * https://www.gnu.org/licenses/gpl.html.
28 * Imported from Catacomb, and lightly modified for Secnet (2017-04-30):
30 * * Use `fake-mLib-bits.h' in place of the real <mLib/bits.h>.
32 * * Remove the 16/32-bit implementation, since C99 always has 64-bit
35 * * Disable some of the operations which aren't needed for X25519.
36 * (They're used for Ed25519, which we don't need.)
38 * The file's original comment headers are preserved below.
42 * Arithmetic modulo 2^255 - 19
44 * (c) 2017 Straylight/Edgeware
47 /*----- Licensing notice --------------------------------------------------*
49 * This file is part of Catacomb.
51 * Catacomb is free software; you can redistribute it and/or modify
52 * it under the terms of the GNU Library General Public License as
53 * published by the Free Software Foundation; either version 2 of the
54 * License, or (at your option) any later version.
56 * Catacomb is distributed in the hope that it will be useful,
57 * but WITHOUT ANY WARRANTY; without even the implied warranty of
58 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 * GNU Library General Public License for more details.
61 * You should have received a copy of the GNU Library General Public
62 * License along with Catacomb; if not, write to the Free
63 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
67 #ifndef CATACOMB_F25519_H
68 #define CATACOMB_F25519_H
74 /*----- Header files ------------------------------------------------------*/
76 #include "fake-mLib-bits.h"
78 #ifndef CATACOMB_QFARITH_H
82 /*----- Data structures ---------------------------------------------------*/
88 /*----- Functions provided ------------------------------------------------*/
90 #define F25519_TRIM_X25519
92 /* --- @f25519_set@ --- *
94 * Arguments: @f25519 *z@ = where to write the result
95 * @int a@ = a small-ish constant
99 * Use: Sets @z@ to equal @a@.
102 extern void f25519_set(f25519 */*x*/, int /*a*/);
104 /* --- @f25519_load@ --- *
106 * Arguments: @f25519 *z@ = where to store the result
107 * @const octet xv[32]@ = source to read
111 * Use: Reads an element of %$\gf{2^{255} - 19}$% in external
112 * representation from @xv@ and stores it in @z@.
114 * External representation is little-endian base-256. Elements
115 * have multiple encodings, which are not produced by correct
116 * software; use of noncanonical encodings is not an error, and
117 * toleration of them is considered a performance feature.
119 * Some specifications, e.g., RFC7748, require the topmost bit
120 * (i.e., bit 7 of @wv[31]@) to be ignored. Callers
121 * implementing such specifications should clear the bit
122 * explicitly. (It's much easier for a caller who wants the bit
123 * to be ignored to clear it than for a caller who wants the bit
124 * to be significant to apply the necessary change by hand.)
127 extern void f25519_load(f25519 */*z*/, const octet /*xv*/[32]);
129 /* --- @f25519_store@ --- *
131 * Arguments: @octet zv[32]@ = where to write the result
132 * @const f25519 *x@ = the field element to write
136 * Use: Stores a field element in the given octet vector in external
137 * representation. A canonical encoding is always stored, so,
138 * in particular, the top bit of @xv[31]@ is always left clear.
141 extern void f25519_store(octet /*zv*/[32], const f25519 */*x*/);
143 #ifndef F25519_TRIM_X25519
145 /* --- @f25519_pick2@ --- *
147 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
148 * @const f25519 *x, *y@ = two operands
149 * @uint32 m@ = a mask
153 * Use: If @m@ is zero, set @z = y@; if @m@ is all-bits-set, then set
154 * @z = x@. If @m@ has some other value, then scramble @z@ in
158 extern void f25519_pick2(f25519 */*z*/, const f25519 */*x*/,
159 const f25519 */*y*/, uint32 /*m*/);
161 /* --- @f25519_pickn@ --- *
163 * Arguments: @f25519 *z@ = where to put the result
164 * @const f25519 *v@ = a table of entries
165 * @size_t n@ = the number of entries in @v@
166 * @size_t i@ = an index
170 * Use: If @0 <= i < n < 32@ then set @z = v[i]@. If @n >= 32@ then
171 * do something unhelpful; otherwise, if @i >= n@ then set @z@
175 extern void f25519_pickn(f25519 */*z*/, const f25519 */*v*/, size_t /*n*/,
180 /* --- @f25519_condswap@ --- *
182 * Arguments: @f25519 *x, *y@ = two operands
183 * @uint32 m@ = a mask
187 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
188 * exchange @x@ and @y@. If @m@ has some other value, then
189 * scramble @x@ and @y@ in an unhelpful way.
192 extern void f25519_condswap(f25519 */*x*/, f25519 */*y*/, uint32 /*m*/);
194 /* --- @f25519_add@ --- *
196 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
197 * @const f25519 *x, *y@ = two operands
201 * Use: Set @z@ to the sum %$x + y$%.
204 extern void f25519_add(f25519 */*z*/,
205 const f25519 */*x*/, const f25519 */*y*/);
207 /* --- @f25519_sub@ --- *
209 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
210 * @const f25519 *x, *y@ = two operands
214 * Use: Set @z@ to the difference %$x - y$%.
217 extern void f25519_sub(f25519 */*z*/,
218 const f25519 */*x*/, const f25519 */*y*/);
220 #ifndef F25519_TRIM_X25519
222 /* --- @f25519_neg@ --- *
224 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
225 * @const f25519 *x@ = an operand
232 extern void f25519_neg(f25519 */*z*/, const f25519 */*x*/);
234 /* --- @f25519_condneg@ --- *
236 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
237 * @const f25519 *x@ = an operand
238 * @uint32 m@ = a mask
242 * Use: If @m@ is zero, set @z = x@; if @m@ is all-bits-set, then set
243 * @z = -x@. If @m@ has some other value then scramble @z@ in
247 extern void f25519_condneg(f25519 */*z*/, const f25519 */*x*/, uint32 /*m*/);
251 /* --- @f25519_mulconst@ --- *
253 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
254 * @const f25519 *x@ = an operand
255 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
259 * Use: Set @z@ to the product %$a x$%.
262 extern void f25519_mulconst(f25519 */*z*/, const f25519 */*x*/, long /*a*/);
264 /* --- @f25519_mul@ --- *
266 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
267 * @const f25519 *x, *y@ = two operands
271 * Use: Set @z@ to the product %$x y$%.
274 extern void f25519_mul(f25519 */*z*/,
275 const f25519 */*x*/, const f25519 */*y*/);
277 /* --- @f25519_sqr@ --- *
279 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
280 * @const f25519 *x@ = an operand
284 * Use: Set @z@ to the square %$x^2$%.
287 extern void f25519_sqr(f25519 */*z*/, const f25519 */*x*/);
289 /* --- @f25519_inv@ --- *
291 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
292 * @const f25519 *x@ = an operand
296 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
297 * %$x = 0$% then @z@ is set to zero. This is considered a
301 extern void f25519_inv(f25519 */*z*/, const f25519 */*x*/);
303 #ifndef F25519_TRIM_X25519
305 /* --- @f25519_quosqrt@ --- *
307 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
308 * @const f25519 *x, *y@ = two operands
310 * Returns: Zero if successful, @-1@ if %$x/y$% is not a square.
312 * Use: Stores in @z@ the one of the square roots %$\pm\sqrt{x/y}$%.
313 * If %$x = y = 0% then the result is zero; if %$y = 0$% but %$x
314 * \ne 0$% then the operation fails. If you wanted a specific
315 * square root then you'll have to pick it yourself.
318 extern int f25519_quosqrt(f25519 */*z*/,
319 const f25519 */*x*/, const f25519 */*y*/);
323 /*----- That's all, folks -------------------------------------------------*/