1 /* Autogenerated: src/ExtractionOCaml/unsaturated_solinas --static 25519 5 '2^255 - 19' 64 carry_mul carry_square carry add sub opp selectznz to_bytes from_bytes carry_scmul121666 */
2 /* curve description: 25519 */
3 /* requested operations: carry_mul, carry_square, carry, add, sub, opp, selectznz, to_bytes, from_bytes, carry_scmul121666 */
4 /* n = 5 (from "5") */
5 /* s-c = 2^255 - [(1, 19)] (from "2^255 - 19") */
6 /* machine_wordsize = 64 (from "64") */
7
8 /* Computed values: */
9 /* carry_chain = [0, 1, 2, 3, 4, 0, 1] */
10
11 #include <stdint.h>
12 typedef unsigned char fiat_25519_uint1;
13 typedef signed char fiat_25519_int1;
14 typedef signed __int128 fiat_25519_int128;
15 typedef unsigned __int128 fiat_25519_uint128;
16
17 #if (-1 & 3) != 3
18 #error "This code only works on a two's complement system"
19 #endif
20
21
22 /*
23 * The function fiat_25519_addcarryx_u51 is an addition with carry.
24 * Postconditions:
25 * out1 = (arg1 + arg2 + arg3) mod 2^51
26 * out2 = ⌊(arg1 + arg2 + arg3) / 2^51⌋
27 *
28 * Input Bounds:
29 * arg1: [0x0 ~> 0x1]
30 * arg2: [0x0 ~> 0x7ffffffffffff]
31 * arg3: [0x0 ~> 0x7ffffffffffff]
32 * Output Bounds:
33 * out1: [0x0 ~> 0x7ffffffffffff]
34 * out2: [0x0 ~> 0x1]
35 */
fiat_25519_addcarryx_u51(uint64_t * out1,fiat_25519_uint1 * out2,fiat_25519_uint1 arg1,uint64_t arg2,uint64_t arg3)36 static void fiat_25519_addcarryx_u51(uint64_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
37 uint64_t x1 = ((arg1 + arg2) + arg3);
38 uint64_t x2 = (x1 & UINT64_C(0x7ffffffffffff));
39 fiat_25519_uint1 x3 = (fiat_25519_uint1)(x1 >> 51);
40 *out1 = x2;
41 *out2 = x3;
42 }
43
44 /*
45 * The function fiat_25519_subborrowx_u51 is a subtraction with borrow.
46 * Postconditions:
47 * out1 = (-arg1 + arg2 + -arg3) mod 2^51
48 * out2 = -⌊(-arg1 + arg2 + -arg3) / 2^51⌋
49 *
50 * Input Bounds:
51 * arg1: [0x0 ~> 0x1]
52 * arg2: [0x0 ~> 0x7ffffffffffff]
53 * arg3: [0x0 ~> 0x7ffffffffffff]
54 * Output Bounds:
55 * out1: [0x0 ~> 0x7ffffffffffff]
56 * out2: [0x0 ~> 0x1]
57 */
fiat_25519_subborrowx_u51(uint64_t * out1,fiat_25519_uint1 * out2,fiat_25519_uint1 arg1,uint64_t arg2,uint64_t arg3)58 static void fiat_25519_subborrowx_u51(uint64_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
59 int64_t x1 = ((int64_t)(arg2 - (int64_t)arg1) - (int64_t)arg3);
60 fiat_25519_int1 x2 = (fiat_25519_int1)(x1 >> 51);
61 uint64_t x3 = (x1 & UINT64_C(0x7ffffffffffff));
62 *out1 = x3;
63 *out2 = (fiat_25519_uint1)(0x0 - x2);
64 }
65
66 /*
67 * The function fiat_25519_cmovznz_u64 is a single-word conditional move.
68 * Postconditions:
69 * out1 = (if arg1 = 0 then arg2 else arg3)
70 *
71 * Input Bounds:
72 * arg1: [0x0 ~> 0x1]
73 * arg2: [0x0 ~> 0xffffffffffffffff]
74 * arg3: [0x0 ~> 0xffffffffffffffff]
75 * Output Bounds:
76 * out1: [0x0 ~> 0xffffffffffffffff]
77 */
fiat_25519_cmovznz_u64(uint64_t * out1,fiat_25519_uint1 arg1,uint64_t arg2,uint64_t arg3)78 static void fiat_25519_cmovznz_u64(uint64_t* out1, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
79 fiat_25519_uint1 x1 = (!(!arg1));
80 uint64_t x2 = ((fiat_25519_int1)(0x0 - x1) & UINT64_C(0xffffffffffffffff));
81 // Note this line has been patched from the synthesized code to add value
82 // barriers.
83 //
84 // Clang recognizes this pattern as a select. While it usually transforms it
85 // to a cmov, it sometimes further transforms it into a branch, which we do
86 // not want.
87 uint64_t x3 = ((value_barrier_u64(x2) & arg3) | (value_barrier_u64(~x2) & arg2));
88 *out1 = x3;
89 }
90
91 /*
92 * The function fiat_25519_carry_mul multiplies two field elements and reduces the result.
93 * Postconditions:
94 * eval out1 mod m = (eval arg1 * eval arg2) mod m
95 *
96 * Input Bounds:
97 * arg1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
98 * arg2: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
99 * Output Bounds:
100 * out1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
101 */
fiat_25519_carry_mul(uint64_t out1[5],const uint64_t arg1[5],const uint64_t arg2[5])102 static void fiat_25519_carry_mul(uint64_t out1[5], const uint64_t arg1[5], const uint64_t arg2[5]) {
103 fiat_25519_uint128 x1 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[4]) * UINT8_C(0x13)));
104 fiat_25519_uint128 x2 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[3]) * UINT8_C(0x13)));
105 fiat_25519_uint128 x3 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[2]) * UINT8_C(0x13)));
106 fiat_25519_uint128 x4 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[1]) * UINT8_C(0x13)));
107 fiat_25519_uint128 x5 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[4]) * UINT8_C(0x13)));
108 fiat_25519_uint128 x6 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[3]) * UINT8_C(0x13)));
109 fiat_25519_uint128 x7 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[2]) * UINT8_C(0x13)));
110 fiat_25519_uint128 x8 = ((fiat_25519_uint128)(arg1[2]) * ((arg2[4]) * UINT8_C(0x13)));
111 fiat_25519_uint128 x9 = ((fiat_25519_uint128)(arg1[2]) * ((arg2[3]) * UINT8_C(0x13)));
112 fiat_25519_uint128 x10 = ((fiat_25519_uint128)(arg1[1]) * ((arg2[4]) * UINT8_C(0x13)));
113 fiat_25519_uint128 x11 = ((fiat_25519_uint128)(arg1[4]) * (arg2[0]));
114 fiat_25519_uint128 x12 = ((fiat_25519_uint128)(arg1[3]) * (arg2[1]));
115 fiat_25519_uint128 x13 = ((fiat_25519_uint128)(arg1[3]) * (arg2[0]));
116 fiat_25519_uint128 x14 = ((fiat_25519_uint128)(arg1[2]) * (arg2[2]));
117 fiat_25519_uint128 x15 = ((fiat_25519_uint128)(arg1[2]) * (arg2[1]));
118 fiat_25519_uint128 x16 = ((fiat_25519_uint128)(arg1[2]) * (arg2[0]));
119 fiat_25519_uint128 x17 = ((fiat_25519_uint128)(arg1[1]) * (arg2[3]));
120 fiat_25519_uint128 x18 = ((fiat_25519_uint128)(arg1[1]) * (arg2[2]));
121 fiat_25519_uint128 x19 = ((fiat_25519_uint128)(arg1[1]) * (arg2[1]));
122 fiat_25519_uint128 x20 = ((fiat_25519_uint128)(arg1[1]) * (arg2[0]));
123 fiat_25519_uint128 x21 = ((fiat_25519_uint128)(arg1[0]) * (arg2[4]));
124 fiat_25519_uint128 x22 = ((fiat_25519_uint128)(arg1[0]) * (arg2[3]));
125 fiat_25519_uint128 x23 = ((fiat_25519_uint128)(arg1[0]) * (arg2[2]));
126 fiat_25519_uint128 x24 = ((fiat_25519_uint128)(arg1[0]) * (arg2[1]));
127 fiat_25519_uint128 x25 = ((fiat_25519_uint128)(arg1[0]) * (arg2[0]));
128 fiat_25519_uint128 x26 = (x25 + (x10 + (x9 + (x7 + x4))));
129 uint64_t x27 = (uint64_t)(x26 >> 51);
130 uint64_t x28 = (uint64_t)(x26 & UINT64_C(0x7ffffffffffff));
131 fiat_25519_uint128 x29 = (x21 + (x17 + (x14 + (x12 + x11))));
132 fiat_25519_uint128 x30 = (x22 + (x18 + (x15 + (x13 + x1))));
133 fiat_25519_uint128 x31 = (x23 + (x19 + (x16 + (x5 + x2))));
134 fiat_25519_uint128 x32 = (x24 + (x20 + (x8 + (x6 + x3))));
135 fiat_25519_uint128 x33 = (x27 + x32);
136 uint64_t x34 = (uint64_t)(x33 >> 51);
137 uint64_t x35 = (uint64_t)(x33 & UINT64_C(0x7ffffffffffff));
138 fiat_25519_uint128 x36 = (x34 + x31);
139 uint64_t x37 = (uint64_t)(x36 >> 51);
140 uint64_t x38 = (uint64_t)(x36 & UINT64_C(0x7ffffffffffff));
141 fiat_25519_uint128 x39 = (x37 + x30);
142 uint64_t x40 = (uint64_t)(x39 >> 51);
143 uint64_t x41 = (uint64_t)(x39 & UINT64_C(0x7ffffffffffff));
144 fiat_25519_uint128 x42 = (x40 + x29);
145 uint64_t x43 = (uint64_t)(x42 >> 51);
146 uint64_t x44 = (uint64_t)(x42 & UINT64_C(0x7ffffffffffff));
147 uint64_t x45 = (x43 * UINT8_C(0x13));
148 uint64_t x46 = (x28 + x45);
149 uint64_t x47 = (x46 >> 51);
150 uint64_t x48 = (x46 & UINT64_C(0x7ffffffffffff));
151 uint64_t x49 = (x47 + x35);
152 fiat_25519_uint1 x50 = (fiat_25519_uint1)(x49 >> 51);
153 uint64_t x51 = (x49 & UINT64_C(0x7ffffffffffff));
154 uint64_t x52 = (x50 + x38);
155 out1[0] = x48;
156 out1[1] = x51;
157 out1[2] = x52;
158 out1[3] = x41;
159 out1[4] = x44;
160 }
161
162 /*
163 * The function fiat_25519_carry_square squares a field element and reduces the result.
164 * Postconditions:
165 * eval out1 mod m = (eval arg1 * eval arg1) mod m
166 *
167 * Input Bounds:
168 * arg1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
169 * Output Bounds:
170 * out1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
171 */
fiat_25519_carry_square(uint64_t out1[5],const uint64_t arg1[5])172 static void fiat_25519_carry_square(uint64_t out1[5], const uint64_t arg1[5]) {
173 uint64_t x1 = ((arg1[4]) * UINT8_C(0x13));
174 uint64_t x2 = (x1 * 0x2);
175 uint64_t x3 = ((arg1[4]) * 0x2);
176 uint64_t x4 = ((arg1[3]) * UINT8_C(0x13));
177 uint64_t x5 = (x4 * 0x2);
178 uint64_t x6 = ((arg1[3]) * 0x2);
179 uint64_t x7 = ((arg1[2]) * 0x2);
180 uint64_t x8 = ((arg1[1]) * 0x2);
181 fiat_25519_uint128 x9 = ((fiat_25519_uint128)(arg1[4]) * x1);
182 fiat_25519_uint128 x10 = ((fiat_25519_uint128)(arg1[3]) * x2);
183 fiat_25519_uint128 x11 = ((fiat_25519_uint128)(arg1[3]) * x4);
184 fiat_25519_uint128 x12 = ((fiat_25519_uint128)(arg1[2]) * x2);
185 fiat_25519_uint128 x13 = ((fiat_25519_uint128)(arg1[2]) * x5);
186 fiat_25519_uint128 x14 = ((fiat_25519_uint128)(arg1[2]) * (arg1[2]));
187 fiat_25519_uint128 x15 = ((fiat_25519_uint128)(arg1[1]) * x2);
188 fiat_25519_uint128 x16 = ((fiat_25519_uint128)(arg1[1]) * x6);
189 fiat_25519_uint128 x17 = ((fiat_25519_uint128)(arg1[1]) * x7);
190 fiat_25519_uint128 x18 = ((fiat_25519_uint128)(arg1[1]) * (arg1[1]));
191 fiat_25519_uint128 x19 = ((fiat_25519_uint128)(arg1[0]) * x3);
192 fiat_25519_uint128 x20 = ((fiat_25519_uint128)(arg1[0]) * x6);
193 fiat_25519_uint128 x21 = ((fiat_25519_uint128)(arg1[0]) * x7);
194 fiat_25519_uint128 x22 = ((fiat_25519_uint128)(arg1[0]) * x8);
195 fiat_25519_uint128 x23 = ((fiat_25519_uint128)(arg1[0]) * (arg1[0]));
196 fiat_25519_uint128 x24 = (x23 + (x15 + x13));
197 uint64_t x25 = (uint64_t)(x24 >> 51);
198 uint64_t x26 = (uint64_t)(x24 & UINT64_C(0x7ffffffffffff));
199 fiat_25519_uint128 x27 = (x19 + (x16 + x14));
200 fiat_25519_uint128 x28 = (x20 + (x17 + x9));
201 fiat_25519_uint128 x29 = (x21 + (x18 + x10));
202 fiat_25519_uint128 x30 = (x22 + (x12 + x11));
203 fiat_25519_uint128 x31 = (x25 + x30);
204 uint64_t x32 = (uint64_t)(x31 >> 51);
205 uint64_t x33 = (uint64_t)(x31 & UINT64_C(0x7ffffffffffff));
206 fiat_25519_uint128 x34 = (x32 + x29);
207 uint64_t x35 = (uint64_t)(x34 >> 51);
208 uint64_t x36 = (uint64_t)(x34 & UINT64_C(0x7ffffffffffff));
209 fiat_25519_uint128 x37 = (x35 + x28);
210 uint64_t x38 = (uint64_t)(x37 >> 51);
211 uint64_t x39 = (uint64_t)(x37 & UINT64_C(0x7ffffffffffff));
212 fiat_25519_uint128 x40 = (x38 + x27);
213 uint64_t x41 = (uint64_t)(x40 >> 51);
214 uint64_t x42 = (uint64_t)(x40 & UINT64_C(0x7ffffffffffff));
215 uint64_t x43 = (x41 * UINT8_C(0x13));
216 uint64_t x44 = (x26 + x43);
217 uint64_t x45 = (x44 >> 51);
218 uint64_t x46 = (x44 & UINT64_C(0x7ffffffffffff));
219 uint64_t x47 = (x45 + x33);
220 fiat_25519_uint1 x48 = (fiat_25519_uint1)(x47 >> 51);
221 uint64_t x49 = (x47 & UINT64_C(0x7ffffffffffff));
222 uint64_t x50 = (x48 + x36);
223 out1[0] = x46;
224 out1[1] = x49;
225 out1[2] = x50;
226 out1[3] = x39;
227 out1[4] = x42;
228 }
229
230 /*
231 * The function fiat_25519_carry reduces a field element.
232 * Postconditions:
233 * eval out1 mod m = eval arg1 mod m
234 *
235 * Input Bounds:
236 * arg1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
237 * Output Bounds:
238 * out1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
239 */
fiat_25519_carry(uint64_t out1[5],const uint64_t arg1[5])240 static void fiat_25519_carry(uint64_t out1[5], const uint64_t arg1[5]) {
241 uint64_t x1 = (arg1[0]);
242 uint64_t x2 = ((x1 >> 51) + (arg1[1]));
243 uint64_t x3 = ((x2 >> 51) + (arg1[2]));
244 uint64_t x4 = ((x3 >> 51) + (arg1[3]));
245 uint64_t x5 = ((x4 >> 51) + (arg1[4]));
246 uint64_t x6 = ((x1 & UINT64_C(0x7ffffffffffff)) + ((x5 >> 51) * UINT8_C(0x13)));
247 uint64_t x7 = ((fiat_25519_uint1)(x6 >> 51) + (x2 & UINT64_C(0x7ffffffffffff)));
248 uint64_t x8 = (x6 & UINT64_C(0x7ffffffffffff));
249 uint64_t x9 = (x7 & UINT64_C(0x7ffffffffffff));
250 uint64_t x10 = ((fiat_25519_uint1)(x7 >> 51) + (x3 & UINT64_C(0x7ffffffffffff)));
251 uint64_t x11 = (x4 & UINT64_C(0x7ffffffffffff));
252 uint64_t x12 = (x5 & UINT64_C(0x7ffffffffffff));
253 out1[0] = x8;
254 out1[1] = x9;
255 out1[2] = x10;
256 out1[3] = x11;
257 out1[4] = x12;
258 }
259
260 /*
261 * The function fiat_25519_add adds two field elements.
262 * Postconditions:
263 * eval out1 mod m = (eval arg1 + eval arg2) mod m
264 *
265 * Input Bounds:
266 * arg1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
267 * arg2: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
268 * Output Bounds:
269 * out1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
270 */
fiat_25519_add(uint64_t out1[5],const uint64_t arg1[5],const uint64_t arg2[5])271 static void fiat_25519_add(uint64_t out1[5], const uint64_t arg1[5], const uint64_t arg2[5]) {
272 uint64_t x1 = ((arg1[0]) + (arg2[0]));
273 uint64_t x2 = ((arg1[1]) + (arg2[1]));
274 uint64_t x3 = ((arg1[2]) + (arg2[2]));
275 uint64_t x4 = ((arg1[3]) + (arg2[3]));
276 uint64_t x5 = ((arg1[4]) + (arg2[4]));
277 out1[0] = x1;
278 out1[1] = x2;
279 out1[2] = x3;
280 out1[3] = x4;
281 out1[4] = x5;
282 }
283
284 /*
285 * The function fiat_25519_sub subtracts two field elements.
286 * Postconditions:
287 * eval out1 mod m = (eval arg1 - eval arg2) mod m
288 *
289 * Input Bounds:
290 * arg1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
291 * arg2: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
292 * Output Bounds:
293 * out1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
294 */
fiat_25519_sub(uint64_t out1[5],const uint64_t arg1[5],const uint64_t arg2[5])295 static void fiat_25519_sub(uint64_t out1[5], const uint64_t arg1[5], const uint64_t arg2[5]) {
296 uint64_t x1 = ((UINT64_C(0xfffffffffffda) + (arg1[0])) - (arg2[0]));
297 uint64_t x2 = ((UINT64_C(0xffffffffffffe) + (arg1[1])) - (arg2[1]));
298 uint64_t x3 = ((UINT64_C(0xffffffffffffe) + (arg1[2])) - (arg2[2]));
299 uint64_t x4 = ((UINT64_C(0xffffffffffffe) + (arg1[3])) - (arg2[3]));
300 uint64_t x5 = ((UINT64_C(0xffffffffffffe) + (arg1[4])) - (arg2[4]));
301 out1[0] = x1;
302 out1[1] = x2;
303 out1[2] = x3;
304 out1[3] = x4;
305 out1[4] = x5;
306 }
307
308 /*
309 * The function fiat_25519_opp negates a field element.
310 * Postconditions:
311 * eval out1 mod m = -eval arg1 mod m
312 *
313 * Input Bounds:
314 * arg1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
315 * Output Bounds:
316 * out1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
317 */
fiat_25519_opp(uint64_t out1[5],const uint64_t arg1[5])318 static void fiat_25519_opp(uint64_t out1[5], const uint64_t arg1[5]) {
319 uint64_t x1 = (UINT64_C(0xfffffffffffda) - (arg1[0]));
320 uint64_t x2 = (UINT64_C(0xffffffffffffe) - (arg1[1]));
321 uint64_t x3 = (UINT64_C(0xffffffffffffe) - (arg1[2]));
322 uint64_t x4 = (UINT64_C(0xffffffffffffe) - (arg1[3]));
323 uint64_t x5 = (UINT64_C(0xffffffffffffe) - (arg1[4]));
324 out1[0] = x1;
325 out1[1] = x2;
326 out1[2] = x3;
327 out1[3] = x4;
328 out1[4] = x5;
329 }
330
331 /*
332 * The function fiat_25519_selectznz is a multi-limb conditional select.
333 * Postconditions:
334 * eval out1 = (if arg1 = 0 then eval arg2 else eval arg3)
335 *
336 * Input Bounds:
337 * arg1: [0x0 ~> 0x1]
338 * arg2: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
339 * arg3: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
340 * Output Bounds:
341 * out1: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
342 */
fiat_25519_selectznz(uint64_t out1[5],fiat_25519_uint1 arg1,const uint64_t arg2[5],const uint64_t arg3[5])343 static void fiat_25519_selectznz(uint64_t out1[5], fiat_25519_uint1 arg1, const uint64_t arg2[5], const uint64_t arg3[5]) {
344 uint64_t x1;
345 fiat_25519_cmovznz_u64(&x1, arg1, (arg2[0]), (arg3[0]));
346 uint64_t x2;
347 fiat_25519_cmovznz_u64(&x2, arg1, (arg2[1]), (arg3[1]));
348 uint64_t x3;
349 fiat_25519_cmovznz_u64(&x3, arg1, (arg2[2]), (arg3[2]));
350 uint64_t x4;
351 fiat_25519_cmovznz_u64(&x4, arg1, (arg2[3]), (arg3[3]));
352 uint64_t x5;
353 fiat_25519_cmovznz_u64(&x5, arg1, (arg2[4]), (arg3[4]));
354 out1[0] = x1;
355 out1[1] = x2;
356 out1[2] = x3;
357 out1[3] = x4;
358 out1[4] = x5;
359 }
360
361 /*
362 * The function fiat_25519_to_bytes serializes a field element to bytes in little-endian order.
363 * Postconditions:
364 * out1 = map (λ x, ⌊((eval arg1 mod m) mod 2^(8 * (x + 1))) / 2^(8 * x)⌋) [0..31]
365 *
366 * Input Bounds:
367 * arg1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
368 * Output Bounds:
369 * out1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
370 */
fiat_25519_to_bytes(uint8_t out1[32],const uint64_t arg1[5])371 static void fiat_25519_to_bytes(uint8_t out1[32], const uint64_t arg1[5]) {
372 uint64_t x1;
373 fiat_25519_uint1 x2;
374 fiat_25519_subborrowx_u51(&x1, &x2, 0x0, (arg1[0]), UINT64_C(0x7ffffffffffed));
375 uint64_t x3;
376 fiat_25519_uint1 x4;
377 fiat_25519_subborrowx_u51(&x3, &x4, x2, (arg1[1]), UINT64_C(0x7ffffffffffff));
378 uint64_t x5;
379 fiat_25519_uint1 x6;
380 fiat_25519_subborrowx_u51(&x5, &x6, x4, (arg1[2]), UINT64_C(0x7ffffffffffff));
381 uint64_t x7;
382 fiat_25519_uint1 x8;
383 fiat_25519_subborrowx_u51(&x7, &x8, x6, (arg1[3]), UINT64_C(0x7ffffffffffff));
384 uint64_t x9;
385 fiat_25519_uint1 x10;
386 fiat_25519_subborrowx_u51(&x9, &x10, x8, (arg1[4]), UINT64_C(0x7ffffffffffff));
387 uint64_t x11;
388 fiat_25519_cmovznz_u64(&x11, x10, 0x0, UINT64_C(0xffffffffffffffff));
389 uint64_t x12;
390 fiat_25519_uint1 x13;
391 fiat_25519_addcarryx_u51(&x12, &x13, 0x0, x1, (x11 & UINT64_C(0x7ffffffffffed)));
392 uint64_t x14;
393 fiat_25519_uint1 x15;
394 fiat_25519_addcarryx_u51(&x14, &x15, x13, x3, (x11 & UINT64_C(0x7ffffffffffff)));
395 uint64_t x16;
396 fiat_25519_uint1 x17;
397 fiat_25519_addcarryx_u51(&x16, &x17, x15, x5, (x11 & UINT64_C(0x7ffffffffffff)));
398 uint64_t x18;
399 fiat_25519_uint1 x19;
400 fiat_25519_addcarryx_u51(&x18, &x19, x17, x7, (x11 & UINT64_C(0x7ffffffffffff)));
401 uint64_t x20;
402 fiat_25519_uint1 x21;
403 fiat_25519_addcarryx_u51(&x20, &x21, x19, x9, (x11 & UINT64_C(0x7ffffffffffff)));
404 uint64_t x22 = (x20 << 4);
405 uint64_t x23 = (x18 * (uint64_t)0x2);
406 uint64_t x24 = (x16 << 6);
407 uint64_t x25 = (x14 << 3);
408 uint64_t x26 = (x12 >> 8);
409 uint8_t x27 = (uint8_t)(x12 & UINT8_C(0xff));
410 uint64_t x28 = (x26 >> 8);
411 uint8_t x29 = (uint8_t)(x26 & UINT8_C(0xff));
412 uint64_t x30 = (x28 >> 8);
413 uint8_t x31 = (uint8_t)(x28 & UINT8_C(0xff));
414 uint64_t x32 = (x30 >> 8);
415 uint8_t x33 = (uint8_t)(x30 & UINT8_C(0xff));
416 uint64_t x34 = (x32 >> 8);
417 uint8_t x35 = (uint8_t)(x32 & UINT8_C(0xff));
418 uint8_t x36 = (uint8_t)(x34 >> 8);
419 uint8_t x37 = (uint8_t)(x34 & UINT8_C(0xff));
420 uint64_t x38 = (x36 + x25);
421 uint64_t x39 = (x38 >> 8);
422 uint8_t x40 = (uint8_t)(x38 & UINT8_C(0xff));
423 uint64_t x41 = (x39 >> 8);
424 uint8_t x42 = (uint8_t)(x39 & UINT8_C(0xff));
425 uint64_t x43 = (x41 >> 8);
426 uint8_t x44 = (uint8_t)(x41 & UINT8_C(0xff));
427 uint64_t x45 = (x43 >> 8);
428 uint8_t x46 = (uint8_t)(x43 & UINT8_C(0xff));
429 uint64_t x47 = (x45 >> 8);
430 uint8_t x48 = (uint8_t)(x45 & UINT8_C(0xff));
431 uint8_t x49 = (uint8_t)(x47 >> 8);
432 uint8_t x50 = (uint8_t)(x47 & UINT8_C(0xff));
433 uint64_t x51 = (x49 + x24);
434 uint64_t x52 = (x51 >> 8);
435 uint8_t x53 = (uint8_t)(x51 & UINT8_C(0xff));
436 uint64_t x54 = (x52 >> 8);
437 uint8_t x55 = (uint8_t)(x52 & UINT8_C(0xff));
438 uint64_t x56 = (x54 >> 8);
439 uint8_t x57 = (uint8_t)(x54 & UINT8_C(0xff));
440 uint64_t x58 = (x56 >> 8);
441 uint8_t x59 = (uint8_t)(x56 & UINT8_C(0xff));
442 uint64_t x60 = (x58 >> 8);
443 uint8_t x61 = (uint8_t)(x58 & UINT8_C(0xff));
444 uint64_t x62 = (x60 >> 8);
445 uint8_t x63 = (uint8_t)(x60 & UINT8_C(0xff));
446 fiat_25519_uint1 x64 = (fiat_25519_uint1)(x62 >> 8);
447 uint8_t x65 = (uint8_t)(x62 & UINT8_C(0xff));
448 uint64_t x66 = (x64 + x23);
449 uint64_t x67 = (x66 >> 8);
450 uint8_t x68 = (uint8_t)(x66 & UINT8_C(0xff));
451 uint64_t x69 = (x67 >> 8);
452 uint8_t x70 = (uint8_t)(x67 & UINT8_C(0xff));
453 uint64_t x71 = (x69 >> 8);
454 uint8_t x72 = (uint8_t)(x69 & UINT8_C(0xff));
455 uint64_t x73 = (x71 >> 8);
456 uint8_t x74 = (uint8_t)(x71 & UINT8_C(0xff));
457 uint64_t x75 = (x73 >> 8);
458 uint8_t x76 = (uint8_t)(x73 & UINT8_C(0xff));
459 uint8_t x77 = (uint8_t)(x75 >> 8);
460 uint8_t x78 = (uint8_t)(x75 & UINT8_C(0xff));
461 uint64_t x79 = (x77 + x22);
462 uint64_t x80 = (x79 >> 8);
463 uint8_t x81 = (uint8_t)(x79 & UINT8_C(0xff));
464 uint64_t x82 = (x80 >> 8);
465 uint8_t x83 = (uint8_t)(x80 & UINT8_C(0xff));
466 uint64_t x84 = (x82 >> 8);
467 uint8_t x85 = (uint8_t)(x82 & UINT8_C(0xff));
468 uint64_t x86 = (x84 >> 8);
469 uint8_t x87 = (uint8_t)(x84 & UINT8_C(0xff));
470 uint64_t x88 = (x86 >> 8);
471 uint8_t x89 = (uint8_t)(x86 & UINT8_C(0xff));
472 uint8_t x90 = (uint8_t)(x88 >> 8);
473 uint8_t x91 = (uint8_t)(x88 & UINT8_C(0xff));
474 out1[0] = x27;
475 out1[1] = x29;
476 out1[2] = x31;
477 out1[3] = x33;
478 out1[4] = x35;
479 out1[5] = x37;
480 out1[6] = x40;
481 out1[7] = x42;
482 out1[8] = x44;
483 out1[9] = x46;
484 out1[10] = x48;
485 out1[11] = x50;
486 out1[12] = x53;
487 out1[13] = x55;
488 out1[14] = x57;
489 out1[15] = x59;
490 out1[16] = x61;
491 out1[17] = x63;
492 out1[18] = x65;
493 out1[19] = x68;
494 out1[20] = x70;
495 out1[21] = x72;
496 out1[22] = x74;
497 out1[23] = x76;
498 out1[24] = x78;
499 out1[25] = x81;
500 out1[26] = x83;
501 out1[27] = x85;
502 out1[28] = x87;
503 out1[29] = x89;
504 out1[30] = x91;
505 out1[31] = x90;
506 }
507
508 /*
509 * The function fiat_25519_from_bytes deserializes a field element from bytes in little-endian order.
510 * Postconditions:
511 * eval out1 mod m = bytes_eval arg1 mod m
512 *
513 * Input Bounds:
514 * arg1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
515 * Output Bounds:
516 * out1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
517 */
fiat_25519_from_bytes(uint64_t out1[5],const uint8_t arg1[32])518 static void fiat_25519_from_bytes(uint64_t out1[5], const uint8_t arg1[32]) {
519 uint64_t x1 = ((uint64_t)(arg1[31]) << 44);
520 uint64_t x2 = ((uint64_t)(arg1[30]) << 36);
521 uint64_t x3 = ((uint64_t)(arg1[29]) << 28);
522 uint64_t x4 = ((uint64_t)(arg1[28]) << 20);
523 uint64_t x5 = ((uint64_t)(arg1[27]) << 12);
524 uint64_t x6 = ((uint64_t)(arg1[26]) << 4);
525 uint64_t x7 = ((uint64_t)(arg1[25]) << 47);
526 uint64_t x8 = ((uint64_t)(arg1[24]) << 39);
527 uint64_t x9 = ((uint64_t)(arg1[23]) << 31);
528 uint64_t x10 = ((uint64_t)(arg1[22]) << 23);
529 uint64_t x11 = ((uint64_t)(arg1[21]) << 15);
530 uint64_t x12 = ((uint64_t)(arg1[20]) << 7);
531 uint64_t x13 = ((uint64_t)(arg1[19]) << 50);
532 uint64_t x14 = ((uint64_t)(arg1[18]) << 42);
533 uint64_t x15 = ((uint64_t)(arg1[17]) << 34);
534 uint64_t x16 = ((uint64_t)(arg1[16]) << 26);
535 uint64_t x17 = ((uint64_t)(arg1[15]) << 18);
536 uint64_t x18 = ((uint64_t)(arg1[14]) << 10);
537 uint64_t x19 = ((uint64_t)(arg1[13]) << 2);
538 uint64_t x20 = ((uint64_t)(arg1[12]) << 45);
539 uint64_t x21 = ((uint64_t)(arg1[11]) << 37);
540 uint64_t x22 = ((uint64_t)(arg1[10]) << 29);
541 uint64_t x23 = ((uint64_t)(arg1[9]) << 21);
542 uint64_t x24 = ((uint64_t)(arg1[8]) << 13);
543 uint64_t x25 = ((uint64_t)(arg1[7]) << 5);
544 uint64_t x26 = ((uint64_t)(arg1[6]) << 48);
545 uint64_t x27 = ((uint64_t)(arg1[5]) << 40);
546 uint64_t x28 = ((uint64_t)(arg1[4]) << 32);
547 uint64_t x29 = ((uint64_t)(arg1[3]) << 24);
548 uint64_t x30 = ((uint64_t)(arg1[2]) << 16);
549 uint64_t x31 = ((uint64_t)(arg1[1]) << 8);
550 uint8_t x32 = (arg1[0]);
551 uint64_t x33 = (x32 + (x31 + (x30 + (x29 + (x28 + (x27 + x26))))));
552 uint8_t x34 = (uint8_t)(x33 >> 51);
553 uint64_t x35 = (x33 & UINT64_C(0x7ffffffffffff));
554 uint64_t x36 = (x6 + (x5 + (x4 + (x3 + (x2 + x1)))));
555 uint64_t x37 = (x12 + (x11 + (x10 + (x9 + (x8 + x7)))));
556 uint64_t x38 = (x19 + (x18 + (x17 + (x16 + (x15 + (x14 + x13))))));
557 uint64_t x39 = (x25 + (x24 + (x23 + (x22 + (x21 + x20)))));
558 uint64_t x40 = (x34 + x39);
559 uint8_t x41 = (uint8_t)(x40 >> 51);
560 uint64_t x42 = (x40 & UINT64_C(0x7ffffffffffff));
561 uint64_t x43 = (x41 + x38);
562 uint8_t x44 = (uint8_t)(x43 >> 51);
563 uint64_t x45 = (x43 & UINT64_C(0x7ffffffffffff));
564 uint64_t x46 = (x44 + x37);
565 uint8_t x47 = (uint8_t)(x46 >> 51);
566 uint64_t x48 = (x46 & UINT64_C(0x7ffffffffffff));
567 uint64_t x49 = (x47 + x36);
568 out1[0] = x35;
569 out1[1] = x42;
570 out1[2] = x45;
571 out1[3] = x48;
572 out1[4] = x49;
573 }
574
575 /*
576 * The function fiat_25519_carry_scmul_121666 multiplies a field element by 121666 and reduces the result.
577 * Postconditions:
578 * eval out1 mod m = (121666 * eval arg1) mod m
579 *
580 * Input Bounds:
581 * arg1: [[0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664], [0x0 ~> 0x1a666666666664]]
582 * Output Bounds:
583 * out1: [[0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc], [0x0 ~> 0x8cccccccccccc]]
584 */
fiat_25519_carry_scmul_121666(uint64_t out1[5],const uint64_t arg1[5])585 static void fiat_25519_carry_scmul_121666(uint64_t out1[5], const uint64_t arg1[5]) {
586 fiat_25519_uint128 x1 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[4]));
587 fiat_25519_uint128 x2 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[3]));
588 fiat_25519_uint128 x3 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[2]));
589 fiat_25519_uint128 x4 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[1]));
590 fiat_25519_uint128 x5 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[0]));
591 uint64_t x6 = (uint64_t)(x5 >> 51);
592 uint64_t x7 = (uint64_t)(x5 & UINT64_C(0x7ffffffffffff));
593 fiat_25519_uint128 x8 = (x6 + x4);
594 uint64_t x9 = (uint64_t)(x8 >> 51);
595 uint64_t x10 = (uint64_t)(x8 & UINT64_C(0x7ffffffffffff));
596 fiat_25519_uint128 x11 = (x9 + x3);
597 uint64_t x12 = (uint64_t)(x11 >> 51);
598 uint64_t x13 = (uint64_t)(x11 & UINT64_C(0x7ffffffffffff));
599 fiat_25519_uint128 x14 = (x12 + x2);
600 uint64_t x15 = (uint64_t)(x14 >> 51);
601 uint64_t x16 = (uint64_t)(x14 & UINT64_C(0x7ffffffffffff));
602 fiat_25519_uint128 x17 = (x15 + x1);
603 uint64_t x18 = (uint64_t)(x17 >> 51);
604 uint64_t x19 = (uint64_t)(x17 & UINT64_C(0x7ffffffffffff));
605 uint64_t x20 = (x18 * UINT8_C(0x13));
606 uint64_t x21 = (x7 + x20);
607 fiat_25519_uint1 x22 = (fiat_25519_uint1)(x21 >> 51);
608 uint64_t x23 = (x21 & UINT64_C(0x7ffffffffffff));
609 uint64_t x24 = (x22 + x10);
610 fiat_25519_uint1 x25 = (fiat_25519_uint1)(x24 >> 51);
611 uint64_t x26 = (x24 & UINT64_C(0x7ffffffffffff));
612 uint64_t x27 = (x25 + x13);
613 out1[0] = x23;
614 out1[1] = x26;
615 out1[2] = x27;
616 out1[3] = x16;
617 out1[4] = x19;
618 }
619
620