summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/verified/Hacl_Poly1305_32.c
blob: 246a41af391a9b6385f036279316cb1a503b9a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* Copyright 2016-2017 INRIA and Microsoft Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Hacl_Poly1305_32.h"

inline static void
Hacl_Bignum_Modulo_reduce(uint32_t *b)
{
    uint32_t b0 = b[0U];
    b[0U] = (b0 << (uint32_t)2U) + b0;
}

inline static void
Hacl_Bignum_Modulo_carry_top(uint32_t *b)
{
    uint32_t b4 = b[4U];
    uint32_t b0 = b[0U];
    uint32_t b4_26 = b4 >> (uint32_t)26U;
    b[4U] = b4 & (uint32_t)0x3ffffffU;
    b[0U] = (b4_26 << (uint32_t)2U) + b4_26 + b0;
}

inline static void
Hacl_Bignum_Modulo_carry_top_wide(uint64_t *b)
{
    uint64_t b4 = b[4U];
    uint64_t b0 = b[0U];
    uint64_t b4_ = b4 & (uint64_t)(uint32_t)0x3ffffffU;
    uint32_t b4_26 = (uint32_t)(b4 >> (uint32_t)26U);
    uint64_t b0_ = b0 + (uint64_t)((b4_26 << (uint32_t)2U) + b4_26);
    b[4U] = b4_;
    b[0U] = b0_;
}

inline static void
Hacl_Bignum_Fproduct_copy_from_wide_(uint32_t *output, uint64_t *input)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)5U; i = i + (uint32_t)1U) {
        uint64_t xi = input[i];
        output[i] = (uint32_t)xi;
    }
}

inline static void
Hacl_Bignum_Fproduct_sum_scalar_multiplication_(uint64_t *output, uint32_t *input, uint32_t s)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)5U; i = i + (uint32_t)1U) {
        uint64_t xi = output[i];
        uint32_t yi = input[i];
        uint64_t x_wide = (uint64_t)yi;
        uint64_t y_wide = (uint64_t)s;
        output[i] = xi + x_wide * y_wide;
    }
}

inline static void
Hacl_Bignum_Fproduct_carry_wide_(uint64_t *tmp)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)4U; i = i + (uint32_t)1U) {
        uint32_t ctr = i;
        uint64_t tctr = tmp[ctr];
        uint64_t tctrp1 = tmp[ctr + (uint32_t)1U];
        uint32_t r0 = (uint32_t)tctr & (uint32_t)0x3ffffffU;
        uint64_t c = tctr >> (uint32_t)26U;
        tmp[ctr] = (uint64_t)r0;
        tmp[ctr + (uint32_t)1U] = tctrp1 + c;
    }
}

inline static void
Hacl_Bignum_Fproduct_carry_limb_(uint32_t *tmp)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)4U; i = i + (uint32_t)1U) {
        uint32_t ctr = i;
        uint32_t tctr = tmp[ctr];
        uint32_t tctrp1 = tmp[ctr + (uint32_t)1U];
        uint32_t r0 = tctr & (uint32_t)0x3ffffffU;
        uint32_t c = tctr >> (uint32_t)26U;
        tmp[ctr] = r0;
        tmp[ctr + (uint32_t)1U] = tctrp1 + c;
    }
}

inline static void
Hacl_Bignum_Fmul_shift_reduce(uint32_t *output)
{
    uint32_t tmp = output[4U];
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)4U; i = i + (uint32_t)1U) {
        uint32_t ctr = (uint32_t)5U - i - (uint32_t)1U;
        uint32_t z = output[ctr - (uint32_t)1U];
        output[ctr] = z;
    }
    output[0U] = tmp;
    Hacl_Bignum_Modulo_reduce(output);
}

static void
Hacl_Bignum_Fmul_mul_shift_reduce_(uint64_t *output, uint32_t *input, uint32_t *input2)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)4U; i = i + (uint32_t)1U) {
        uint32_t input2i = input2[i];
        Hacl_Bignum_Fproduct_sum_scalar_multiplication_(output, input, input2i);
        Hacl_Bignum_Fmul_shift_reduce(input);
    }
    uint32_t i = (uint32_t)4U;
    uint32_t input2i = input2[i];
    Hacl_Bignum_Fproduct_sum_scalar_multiplication_(output, input, input2i);
}

inline static void
Hacl_Bignum_Fmul_fmul(uint32_t *output, uint32_t *input, uint32_t *input2)
{
    uint32_t tmp[5U] = { 0U };
    memcpy(tmp, input, (uint32_t)5U * sizeof input[0U]);
    uint64_t t[5U] = { 0U };
    Hacl_Bignum_Fmul_mul_shift_reduce_(t, tmp, input2);
    Hacl_Bignum_Fproduct_carry_wide_(t);
    Hacl_Bignum_Modulo_carry_top_wide(t);
    Hacl_Bignum_Fproduct_copy_from_wide_(output, t);
    uint32_t i0 = output[0U];
    uint32_t i1 = output[1U];
    uint32_t i0_ = i0 & (uint32_t)0x3ffffffU;
    uint32_t i1_ = i1 + (i0 >> (uint32_t)26U);
    output[0U] = i0_;
    output[1U] = i1_;
}

inline static void
Hacl_Bignum_AddAndMultiply_add_and_multiply(uint32_t *acc, uint32_t *block, uint32_t *r)
{
    for (uint32_t i = (uint32_t)0U; i < (uint32_t)5U; i = i + (uint32_t)1U) {
        uint32_t xi = acc[i];
        uint32_t yi = block[i];
        acc[i] = xi + yi;
    }
    Hacl_Bignum_Fmul_fmul(acc, acc, r);
}

inline static void
Hacl_Impl_Poly1305_32_poly1305_update(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m)
{
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut0 = st;
    uint32_t *h = scrut0.h;
    uint32_t *acc = h;
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *r = scrut.r;
    uint32_t *r5 = r;
    uint32_t tmp[5U] = { 0U };
    uint8_t *s0 = m;
    uint8_t *s1 = m + (uint32_t)3U;
    uint8_t *s2 = m + (uint32_t)6U;
    uint8_t *s3 = m + (uint32_t)9U;
    uint8_t *s4 = m + (uint32_t)12U;
    uint32_t i0 = load32_le(s0);
    uint32_t i1 = load32_le(s1);
    uint32_t i2 = load32_le(s2);
    uint32_t i3 = load32_le(s3);
    uint32_t i4 = load32_le(s4);
    uint32_t r0 = i0 & (uint32_t)0x3ffffffU;
    uint32_t r1 = i1 >> (uint32_t)2U & (uint32_t)0x3ffffffU;
    uint32_t r2 = i2 >> (uint32_t)4U & (uint32_t)0x3ffffffU;
    uint32_t r3 = i3 >> (uint32_t)6U & (uint32_t)0x3ffffffU;
    uint32_t r4 = i4 >> (uint32_t)8U;
    tmp[0U] = r0;
    tmp[1U] = r1;
    tmp[2U] = r2;
    tmp[3U] = r3;
    tmp[4U] = r4;
    uint32_t b4 = tmp[4U];
    uint32_t b4_ = (uint32_t)0x1000000U | b4;
    tmp[4U] = b4_;
    Hacl_Bignum_AddAndMultiply_add_and_multiply(acc, tmp, r5);
}

inline static void
Hacl_Impl_Poly1305_32_poly1305_process_last_block_(
    uint8_t *block,
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint64_t rem_)
{
    uint32_t tmp[5U] = { 0U };
    uint8_t *s0 = block;
    uint8_t *s1 = block + (uint32_t)3U;
    uint8_t *s2 = block + (uint32_t)6U;
    uint8_t *s3 = block + (uint32_t)9U;
    uint8_t *s4 = block + (uint32_t)12U;
    uint32_t i0 = load32_le(s0);
    uint32_t i1 = load32_le(s1);
    uint32_t i2 = load32_le(s2);
    uint32_t i3 = load32_le(s3);
    uint32_t i4 = load32_le(s4);
    uint32_t r0 = i0 & (uint32_t)0x3ffffffU;
    uint32_t r1 = i1 >> (uint32_t)2U & (uint32_t)0x3ffffffU;
    uint32_t r2 = i2 >> (uint32_t)4U & (uint32_t)0x3ffffffU;
    uint32_t r3 = i3 >> (uint32_t)6U & (uint32_t)0x3ffffffU;
    uint32_t r4 = i4 >> (uint32_t)8U;
    tmp[0U] = r0;
    tmp[1U] = r1;
    tmp[2U] = r2;
    tmp[3U] = r3;
    tmp[4U] = r4;
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut0 = st;
    uint32_t *h = scrut0.h;
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *r = scrut.r;
    Hacl_Bignum_AddAndMultiply_add_and_multiply(h, tmp, r);
}

inline static void
Hacl_Impl_Poly1305_32_poly1305_process_last_block(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint64_t rem_)
{
    uint8_t zero1 = (uint8_t)0U;
    KRML_CHECK_SIZE(zero1, (uint32_t)16U);
    uint8_t block[16U];
    for (uint32_t _i = 0U; _i < (uint32_t)16U; ++_i)
        block[_i] = zero1;
    uint32_t i0 = (uint32_t)rem_;
    uint32_t i = (uint32_t)rem_;
    memcpy(block, m, i * sizeof m[0U]);
    block[i0] = (uint8_t)1U;
    Hacl_Impl_Poly1305_32_poly1305_process_last_block_(block, st, m, rem_);
}

static void
Hacl_Impl_Poly1305_32_poly1305_last_pass(uint32_t *acc)
{
    Hacl_Bignum_Fproduct_carry_limb_(acc);
    Hacl_Bignum_Modulo_carry_top(acc);
    uint32_t t0 = acc[0U];
    uint32_t t10 = acc[1U];
    uint32_t t20 = acc[2U];
    uint32_t t30 = acc[3U];
    uint32_t t40 = acc[4U];
    uint32_t t1_ = t10 + (t0 >> (uint32_t)26U);
    uint32_t mask_261 = (uint32_t)0x3ffffffU;
    uint32_t t0_ = t0 & mask_261;
    uint32_t t2_ = t20 + (t1_ >> (uint32_t)26U);
    uint32_t t1__ = t1_ & mask_261;
    uint32_t t3_ = t30 + (t2_ >> (uint32_t)26U);
    uint32_t t2__ = t2_ & mask_261;
    uint32_t t4_ = t40 + (t3_ >> (uint32_t)26U);
    uint32_t t3__ = t3_ & mask_261;
    acc[0U] = t0_;
    acc[1U] = t1__;
    acc[2U] = t2__;
    acc[3U] = t3__;
    acc[4U] = t4_;
    Hacl_Bignum_Modulo_carry_top(acc);
    uint32_t t00 = acc[0U];
    uint32_t t1 = acc[1U];
    uint32_t t2 = acc[2U];
    uint32_t t3 = acc[3U];
    uint32_t t4 = acc[4U];
    uint32_t t1_0 = t1 + (t00 >> (uint32_t)26U);
    uint32_t t0_0 = t00 & (uint32_t)0x3ffffffU;
    uint32_t t2_0 = t2 + (t1_0 >> (uint32_t)26U);
    uint32_t t1__0 = t1_0 & (uint32_t)0x3ffffffU;
    uint32_t t3_0 = t3 + (t2_0 >> (uint32_t)26U);
    uint32_t t2__0 = t2_0 & (uint32_t)0x3ffffffU;
    uint32_t t4_0 = t4 + (t3_0 >> (uint32_t)26U);
    uint32_t t3__0 = t3_0 & (uint32_t)0x3ffffffU;
    acc[0U] = t0_0;
    acc[1U] = t1__0;
    acc[2U] = t2__0;
    acc[3U] = t3__0;
    acc[4U] = t4_0;
    Hacl_Bignum_Modulo_carry_top(acc);
    uint32_t i0 = acc[0U];
    uint32_t i1 = acc[1U];
    uint32_t i0_ = i0 & (uint32_t)0x3ffffffU;
    uint32_t i1_ = i1 + (i0 >> (uint32_t)26U);
    acc[0U] = i0_;
    acc[1U] = i1_;
    uint32_t a0 = acc[0U];
    uint32_t a1 = acc[1U];
    uint32_t a2 = acc[2U];
    uint32_t a3 = acc[3U];
    uint32_t a4 = acc[4U];
    uint32_t mask0 = FStar_UInt32_gte_mask(a0, (uint32_t)0x3fffffbU);
    uint32_t mask1 = FStar_UInt32_eq_mask(a1, (uint32_t)0x3ffffffU);
    uint32_t mask2 = FStar_UInt32_eq_mask(a2, (uint32_t)0x3ffffffU);
    uint32_t mask3 = FStar_UInt32_eq_mask(a3, (uint32_t)0x3ffffffU);
    uint32_t mask4 = FStar_UInt32_eq_mask(a4, (uint32_t)0x3ffffffU);
    uint32_t mask = (((mask0 & mask1) & mask2) & mask3) & mask4;
    uint32_t a0_ = a0 - ((uint32_t)0x3fffffbU & mask);
    uint32_t a1_ = a1 - ((uint32_t)0x3ffffffU & mask);
    uint32_t a2_ = a2 - ((uint32_t)0x3ffffffU & mask);
    uint32_t a3_ = a3 - ((uint32_t)0x3ffffffU & mask);
    uint32_t a4_ = a4 - ((uint32_t)0x3ffffffU & mask);
    acc[0U] = a0_;
    acc[1U] = a1_;
    acc[2U] = a2_;
    acc[3U] = a3_;
    acc[4U] = a4_;
}

static Hacl_Impl_Poly1305_32_State_poly1305_state
Hacl_Impl_Poly1305_32_mk_state(uint32_t *r, uint32_t *h)
{
    return ((Hacl_Impl_Poly1305_32_State_poly1305_state){.r = r, .h = h });
}

static void
Hacl_Standalone_Poly1305_32_poly1305_blocks(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint64_t len1)
{
    if (!(len1 == (uint64_t)0U)) {
        uint8_t *block = m;
        uint8_t *tail1 = m + (uint32_t)16U;
        Hacl_Impl_Poly1305_32_poly1305_update(st, block);
        uint64_t len2 = len1 - (uint64_t)1U;
        Hacl_Standalone_Poly1305_32_poly1305_blocks(st, tail1, len2);
    }
}

static void
Hacl_Standalone_Poly1305_32_poly1305_partial(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *input,
    uint64_t len1,
    uint8_t *kr)
{
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *r = scrut.r;
    uint32_t *x0 = r;
    FStar_UInt128_t k1 = load128_le(kr);
    FStar_UInt128_t
        k_clamped =
            FStar_UInt128_logand(k1,
                                 FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)0x0ffffffc0ffffffcU),
                                                                              (uint32_t)64U),
                                                     FStar_UInt128_uint64_to_uint128((uint64_t)0x0ffffffc0fffffffU)));
    uint32_t r0 = (uint32_t)FStar_UInt128_uint128_to_uint64(k_clamped) & (uint32_t)0x3ffffffU;
    uint32_t
        r1 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)26U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r2 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)52U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r3 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)78U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r4 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)104U)) & (uint32_t)0x3ffffffU;
    x0[0U] = r0;
    x0[1U] = r1;
    x0[2U] = r2;
    x0[3U] = r3;
    x0[4U] = r4;
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut0 = st;
    uint32_t *h = scrut0.h;
    uint32_t *x00 = h;
    x00[0U] = (uint32_t)0U;
    x00[1U] = (uint32_t)0U;
    x00[2U] = (uint32_t)0U;
    x00[3U] = (uint32_t)0U;
    x00[4U] = (uint32_t)0U;
    Hacl_Standalone_Poly1305_32_poly1305_blocks(st, input, len1);
}

static void
Hacl_Standalone_Poly1305_32_poly1305_complete(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint64_t len1,
    uint8_t *k1)
{
    uint8_t *kr = k1;
    uint64_t len16 = len1 >> (uint32_t)4U;
    uint64_t rem16 = len1 & (uint64_t)0xfU;
    uint8_t *part_input = m;
    uint8_t *last_block = m + (uint32_t)((uint64_t)16U * len16);
    Hacl_Standalone_Poly1305_32_poly1305_partial(st, part_input, len16, kr);
    if (!(rem16 == (uint64_t)0U))
        Hacl_Impl_Poly1305_32_poly1305_process_last_block(st, last_block, rem16);
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *h = scrut.h;
    uint32_t *acc = h;
    Hacl_Impl_Poly1305_32_poly1305_last_pass(acc);
}

static void
Hacl_Standalone_Poly1305_32_crypto_onetimeauth_(
    uint8_t *output,
    uint8_t *input,
    uint64_t len1,
    uint8_t *k1)
{
    uint32_t buf[10U] = { 0U };
    uint32_t *r = buf;
    uint32_t *h = buf + (uint32_t)5U;
    Hacl_Impl_Poly1305_32_State_poly1305_state st = Hacl_Impl_Poly1305_32_mk_state(r, h);
    uint8_t *key_s = k1 + (uint32_t)16U;
    Hacl_Standalone_Poly1305_32_poly1305_complete(st, input, len1, k1);
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *h5 = scrut.h;
    uint32_t *acc = h5;
    FStar_UInt128_t k_ = load128_le(key_s);
    uint32_t h0 = acc[0U];
    uint32_t h1 = acc[1U];
    uint32_t h2 = acc[2U];
    uint32_t h3 = acc[3U];
    uint32_t h4 = acc[4U];
    FStar_UInt128_t
        acc_ =
            FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h4),
                                                         (uint32_t)104U),
                                FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h3),
                                                                             (uint32_t)78U),
                                                    FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h2),
                                                                                                 (uint32_t)52U),
                                                                        FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h1),
                                                                                                                     (uint32_t)26U),
                                                                                            FStar_UInt128_uint64_to_uint128((uint64_t)h0)))));
    FStar_UInt128_t mac_ = FStar_UInt128_add_mod(acc_, k_);
    store128_le(output, mac_);
}

static void
Hacl_Standalone_Poly1305_32_crypto_onetimeauth(
    uint8_t *output,
    uint8_t *input,
    uint64_t len1,
    uint8_t *k1)
{
    Hacl_Standalone_Poly1305_32_crypto_onetimeauth_(output, input, len1, k1);
}

void *
Hacl_Poly1305_32_op_String_Access(FStar_Monotonic_HyperStack_mem h, uint8_t *b)
{
    return (void *)(uint8_t)0U;
}

Hacl_Impl_Poly1305_32_State_poly1305_state
Hacl_Poly1305_32_mk_state(uint32_t *r, uint32_t *acc)
{
    return Hacl_Impl_Poly1305_32_mk_state(r, acc);
}

void
Hacl_Poly1305_32_init(Hacl_Impl_Poly1305_32_State_poly1305_state st, uint8_t *k1)
{
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *r = scrut.r;
    uint32_t *x0 = r;
    FStar_UInt128_t k10 = load128_le(k1);
    FStar_UInt128_t
        k_clamped =
            FStar_UInt128_logand(k10,
                                 FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)0x0ffffffc0ffffffcU),
                                                                              (uint32_t)64U),
                                                     FStar_UInt128_uint64_to_uint128((uint64_t)0x0ffffffc0fffffffU)));
    uint32_t r0 = (uint32_t)FStar_UInt128_uint128_to_uint64(k_clamped) & (uint32_t)0x3ffffffU;
    uint32_t
        r1 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)26U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r2 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)52U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r3 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)78U)) & (uint32_t)0x3ffffffU;
    uint32_t
        r4 =
            (uint32_t)FStar_UInt128_uint128_to_uint64(FStar_UInt128_shift_right(k_clamped, (uint32_t)104U)) & (uint32_t)0x3ffffffU;
    x0[0U] = r0;
    x0[1U] = r1;
    x0[2U] = r2;
    x0[3U] = r3;
    x0[4U] = r4;
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut0 = st;
    uint32_t *h = scrut0.h;
    uint32_t *x00 = h;
    x00[0U] = (uint32_t)0U;
    x00[1U] = (uint32_t)0U;
    x00[2U] = (uint32_t)0U;
    x00[3U] = (uint32_t)0U;
    x00[4U] = (uint32_t)0U;
}

void *Hacl_Poly1305_32_empty_log = (void *)(uint8_t)0U;

void
Hacl_Poly1305_32_update_block(Hacl_Impl_Poly1305_32_State_poly1305_state st, uint8_t *m)
{
    Hacl_Impl_Poly1305_32_poly1305_update(st, m);
}

void
Hacl_Poly1305_32_update(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint32_t len1)
{
    if (!(len1 == (uint32_t)0U)) {
        uint8_t *block = m;
        uint8_t *m_ = m + (uint32_t)16U;
        uint32_t len2 = len1 - (uint32_t)1U;
        Hacl_Poly1305_32_update_block(st, block);
        Hacl_Poly1305_32_update(st, m_, len2);
    }
}

void
Hacl_Poly1305_32_update_last(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *m,
    uint32_t len1)
{
    if (!((uint64_t)len1 == (uint64_t)0U))
        Hacl_Impl_Poly1305_32_poly1305_process_last_block(st, m, (uint64_t)len1);
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *h = scrut.h;
    uint32_t *acc = h;
    Hacl_Impl_Poly1305_32_poly1305_last_pass(acc);
}

void
Hacl_Poly1305_32_finish(
    Hacl_Impl_Poly1305_32_State_poly1305_state st,
    uint8_t *mac,
    uint8_t *k1)
{
    Hacl_Impl_Poly1305_32_State_poly1305_state scrut = st;
    uint32_t *h = scrut.h;
    uint32_t *acc = h;
    FStar_UInt128_t k_ = load128_le(k1);
    uint32_t h0 = acc[0U];
    uint32_t h1 = acc[1U];
    uint32_t h2 = acc[2U];
    uint32_t h3 = acc[3U];
    uint32_t h4 = acc[4U];
    FStar_UInt128_t
        acc_ =
            FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h4),
                                                         (uint32_t)104U),
                                FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h3),
                                                                             (uint32_t)78U),
                                                    FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h2),
                                                                                                 (uint32_t)52U),
                                                                        FStar_UInt128_logor(FStar_UInt128_shift_left(FStar_UInt128_uint64_to_uint128((uint64_t)h1),
                                                                                                                     (uint32_t)26U),
                                                                                            FStar_UInt128_uint64_to_uint128((uint64_t)h0)))));
    FStar_UInt128_t mac_ = FStar_UInt128_add_mod(acc_, k_);
    store128_le(mac, mac_);
}

void
Hacl_Poly1305_32_crypto_onetimeauth(
    uint8_t *output,
    uint8_t *input,
    uint64_t len1,
    uint8_t *k1)
{
    Hacl_Standalone_Poly1305_32_crypto_onetimeauth(output, input, len1, k1);
}