Commit | Line | Data |
---|---|---|
ee39a683 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Arithmetic modulo 2^255 - 19 | |
4 | * | |
5 | * (c) 2017 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of Catacomb. | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
28 | #ifndef CATACOMB_F25519_H | |
29 | #define CATACOMB_F25519_H | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Header files ------------------------------------------------------*/ | |
36 | ||
37 | #include <mLib/bits.h> | |
38 | ||
39 | #ifndef CATACOMB_QFARITH_H | |
40 | # include "qfarith.h" | |
41 | #endif | |
42 | ||
43 | /*----- Data structures ---------------------------------------------------*/ | |
44 | ||
45 | typedef union { | |
46 | int32 p26[10]; | |
47 | int16 p10[26]; | |
48 | } f25519; | |
49 | ||
50 | #if !defined(F25519_IMPL) && defined(HAVE_INT64) | |
51 | # define F25519_IMPL 26 | |
52 | #endif | |
53 | ||
54 | #ifndef F25519_IMPL | |
55 | # define F25519_IMPL 10 | |
56 | #endif | |
57 | ||
58 | /*----- Functions provided ------------------------------------------------*/ | |
59 | ||
60 | /* --- @f25519_set@ --- * | |
61 | * | |
62 | * Arguments: @f25519 *z@ = where to write the result | |
63 | * @int a@ = a small-ish constant | |
64 | * | |
65 | * Returns: --- | |
66 | * | |
67 | * Use: Sets @z@ to equal @a@. | |
68 | */ | |
69 | ||
70 | extern void f25519_set(f25519 */*x*/, int /*a*/); | |
71 | ||
72 | /* --- @f25519_load@ --- * | |
73 | * | |
74 | * Arguments: @f25519 *z@ = where to store the result | |
75 | * @const octet xv[32]@ = source to read | |
76 | * | |
77 | * Returns: --- | |
78 | * | |
79 | * Use: Reads an element of %$\gf{2^{255} - 19}$% in external | |
80 | * representation from @xv@ and stores it in @z@. | |
81 | * | |
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. | |
86 | * | |
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.) | |
93 | */ | |
94 | ||
95 | extern void f25519_load(f25519 */*z*/, const octet /*xv*/[32]); | |
96 | ||
97 | /* --- @f25519_store@ --- * | |
98 | * | |
99 | * Arguments: @octet zv[32]@ = where to write the result | |
100 | * @const f25519 *x@ = the field element to write | |
101 | * | |
102 | * Returns: --- | |
103 | * | |
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. | |
107 | */ | |
108 | ||
109 | extern void f25519_store(octet /*zv*/[32], const f25519 */*x*/); | |
110 | ||
111 | /* --- @f25519_condswap@ --- * | |
112 | * | |
113 | * Arguments: @f25519 *x, *y@ = two operands | |
114 | * @uint32 m@ = a mask | |
115 | * | |
116 | * Returns: --- | |
117 | * | |
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. | |
121 | */ | |
122 | ||
123 | extern void f25519_condswap(f25519 */*x*/, f25519 */*y*/, uint32 /*m*/); | |
124 | ||
125 | /* --- @f25519_add@ --- * | |
126 | * | |
127 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) | |
128 | * @const f25519 *x, *y@ = two operands | |
129 | * | |
130 | * Returns: --- | |
131 | * | |
132 | * Use: Set @z@ to the sum %$x + y$%. | |
133 | */ | |
134 | ||
135 | extern void f25519_add(f25519 */*z*/, | |
136 | const f25519 */*x*/, const f25519 */*y*/); | |
137 | ||
138 | /* --- @f25519_sub@ --- * | |
139 | * | |
140 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) | |
141 | * @const f25519 *x, *y@ = two operands | |
142 | * | |
143 | * Returns: --- | |
144 | * | |
145 | * Use: Set @z@ to the difference %$x - y$%. | |
146 | */ | |
147 | ||
148 | extern void f25519_sub(f25519 */*z*/, | |
149 | const f25519 */*x*/, const f25519 */*y*/); | |
150 | ||
151 | /* --- @f25519_mulconst@ --- * | |
152 | * | |
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}$%. | |
156 | * | |
157 | * Returns: --- | |
158 | * | |
159 | * Use: Set @z@ to the product %$a x$%. | |
160 | */ | |
161 | ||
162 | extern void f25519_mulconst(f25519 */*z*/, const f25519 */*x*/, long /*a*/); | |
163 | ||
164 | /* --- @f25519_mul@ --- * | |
165 | * | |
166 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) | |
167 | * @const f25519 *x, *y@ = two operands | |
168 | * | |
169 | * Returns: --- | |
170 | * | |
171 | * Use: Set @z@ to the product %$x y$%. | |
172 | */ | |
173 | ||
174 | extern void f25519_mul(f25519 */*z*/, | |
175 | const f25519 */*x*/, const f25519 */*y*/); | |
176 | ||
177 | /* --- @f25519_sqr@ --- * | |
178 | * | |
179 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) | |
180 | * @const f25519 *x@ = an operand | |
181 | * | |
182 | * Returns: --- | |
183 | * | |
184 | * Use: Set @z@ to the square %$x^2$%. | |
185 | */ | |
186 | ||
187 | extern void f25519_sqr(f25519 */*z*/, const f25519 */*x*/); | |
188 | ||
189 | /* --- @f25519_inv@ --- * | |
190 | * | |
191 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@) | |
192 | * @const f25519 *x@ = an operand | |
193 | * | |
194 | * Returns: --- | |
195 | * | |
196 | * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If | |
197 | * %$x = 0$% then @z@ is set to zero. This is considered a | |
198 | * feature. | |
199 | */ | |
200 | ||
201 | extern void f25519_inv(f25519 */*z*/, const f25519 */*x*/); | |
202 | ||
203 | /*----- That's all, folks -------------------------------------------------*/ | |
204 | ||
205 | #ifdef __cplusplus | |
206 | } | |
207 | #endif | |
208 | ||
209 | #endif |