-
Notifications
You must be signed in to change notification settings - Fork 26
/
mpc_lowmc.c
1229 lines (1089 loc) · 59.5 KB
/
mpc_lowmc.c
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "mpc_lowmc.h"
#include "mzd_additional.h"
#if !defined(_MSC_VER)
#include <stdalign.h>
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#if defined(WITH_OPT)
#include "simd.h"
#endif
#if defined(WITH_LOWMC_128_128_20)
#include "lowmc_128_128_20.h"
#endif
#if defined(WITH_LOWMC_129_129_4)
#include "lowmc_129_129_4.h"
#endif
#if defined(WITH_LOWMC_192_192_4)
#include "lowmc_192_192_4.h"
#endif
#if defined(WITH_LOWMC_192_192_30)
#include "lowmc_192_192_30.h"
#endif
#if defined(WITH_LOWMC_256_256_38)
#include "lowmc_256_256_38.h"
#endif
#if defined(WITH_LOWMC_255_255_4)
#include "lowmc_255_255_4.h"
#endif
#define MPC_LOOP_CONST(function, result, first, second, sc) \
do { \
for (unsigned int e = 0; e < (sc); ++e) { \
function((result)[e], (first)[e], (second)); \
} \
} while (0)
#define MPC_LOOP_SHARED(function, result, first, second, sc) \
do { \
for (unsigned int o = 0; o < (sc); ++o) { \
function((result)[o], (first)[o], (second)[o]); \
} \
} while (0)
#define MPC_LOOP_SHARED_1(function, result, first, sc) \
do { \
for (unsigned int o = 0; o < (sc); ++o) { \
function((result)[o], (first)[o]); \
} \
} while (0)
#define MPC_LOOP_CONST_C_0(function, result, first, second, sc) \
function((result)[0], (first)[0], (second))
#define MPC_LOOP_CONST_C_ch(function, result, first, second, sc, c) \
do { \
if (!(c)) { \
MPC_LOOP_CONST_C_0(function, result, first, second, sc); \
} else if ((c) == (sc)) { \
function((result)[(sc)-1], first[(sc)-1], (second)); \
} \
} while (0)
// clang-format off
#if defined(WITH_LOWMC_128_128_20) || defined(WITH_LOWMC_192_192_30) || defined(WITH_LOWMC_256_256_38)
// clang-format on
/* MPC Sbox implementation for partical Sbox */
static void mpc_and_uint64(uint64_t* res, uint64_t const* first, uint64_t const* second,
uint64_t const* r, view_t* view, unsigned viewshift) {
for (unsigned m = 0; m < SC_PROOF; ++m) {
const unsigned j = (m + 1) % SC_PROOF;
uint64_t tmp1 = second[m] ^ second[j];
uint64_t tmp2 = first[j] & second[m];
tmp1 = tmp1 & first[m];
tmp1 = tmp1 ^ tmp2;
tmp2 = r[m] ^ r[j];
res[m] = tmp1 = tmp1 ^ tmp2;
if (viewshift) {
tmp1 = tmp1 >> viewshift;
view->t[m] = view->t[m] ^ tmp1;
} else {
// on first call (viewshift == 0), view->t[0..2] == 0
view->t[m] = tmp1;
}
}
}
static void mpc_and_verify_uint64(uint64_t* res, uint64_t const* first, uint64_t const* second,
uint64_t const* r, view_t* view, uint64_t const mask,
unsigned viewshift) {
for (unsigned m = 0; m < (SC_VERIFY - 1); ++m) {
const unsigned j = (m + 1);
uint64_t tmp1 = second[m] ^ second[j];
uint64_t tmp2 = first[j] & second[m];
tmp1 = tmp1 & first[m];
tmp1 = tmp1 ^ tmp2;
tmp2 = r[m] ^ r[j];
res[m] = tmp1 = tmp1 ^ tmp2;
if (viewshift || m) {
tmp1 = tmp1 >> viewshift;
view->t[m] = view->t[m] ^ tmp1;
} else {
// on first call (viewshift == 0), view->t[0] == 0
view->t[m] = tmp1;
}
}
const uint64_t rsc = view->t[SC_VERIFY - 1] << viewshift;
res[SC_VERIFY - 1] = rsc & mask;
}
#define bitsliced_step_1_uint64_10(sc) \
uint64_t r0m[sc]; \
uint64_t r0s[sc]; \
uint64_t r1m[sc]; \
uint64_t r1s[sc]; \
uint64_t r2m[sc]; \
uint64_t x0s[sc]; \
uint64_t x1s[sc]; \
uint64_t x2m[sc]; \
do { \
for (unsigned int m = 0; m < (sc); ++m) { \
const uint64_t inm = in[m]; \
const uint64_t rvecm = rvec[m]; \
\
x0s[m] = (inm & MASK_X0I) << 2; \
x1s[m] = (inm & MASK_X1I) << 1; \
x2m[m] = inm & MASK_X2I; \
\
r0m[m] = rvecm & MASK_X0I; \
r1m[m] = rvecm & MASK_X1I; \
r2m[m] = rvecm & MASK_X2I; \
\
r0s[m] = r0m[m] << 2; \
r1s[m] = r1m[m] << 1; \
} \
} while (0)
#define bitsliced_step_2_uint64_10(sc) \
do { \
for (unsigned int m = 0; m < (sc); ++m) { \
const uint64_t tmp1 = r2m[m] ^ x0s[m]; \
const uint64_t tmp2 = x0s[m] ^ x1s[m]; \
const uint64_t tmp3 = tmp2 ^ r1m[m]; \
const uint64_t tmp4 = tmp2 ^ r0m[m] ^ x2m[m]; \
\
in[m] = (in[m] & MASK_MASK) ^ (tmp4) ^ (tmp1 >> 2) ^ (tmp3 >> 1); \
} \
} while (0)
static void mpc_sbox_prove_uint64_10(uint64_t* in, view_t* view, uint64_t const* rvec) {
bitsliced_step_1_uint64_10(SC_PROOF);
mpc_and_uint64(r0m, x0s, x1s, r2m, view, 0);
mpc_and_uint64(r2m, x1s, x2m, r1s, view, 1);
mpc_and_uint64(r1m, x0s, x2m, r0s, view, 2);
bitsliced_step_2_uint64_10(SC_PROOF - 1);
}
static void mpc_sbox_verify_uint64_10(uint64_t* in, view_t* view, uint64_t const* rvec) {
bitsliced_step_1_uint64_10(SC_VERIFY);
mpc_and_verify_uint64(r0m, x0s, x1s, r2m, view, MASK_X2I, 0);
mpc_and_verify_uint64(r2m, x1s, x2m, r1s, view, MASK_X2I, 1);
mpc_and_verify_uint64(r1m, x0s, x2m, r0s, view, MASK_X2I, 2);
bitsliced_step_2_uint64_10(SC_VERIFY);
}
#endif
/* MPC Sbox implementation for full instances */
#if !defined(NO_UINT64_FALLBACK)
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_192_192_4)
static void mpc_and_uint64_192(mzd_local_t* res, const mzd_local_t* first,
const mzd_local_t* second, const mzd_local_t* r, view_t* view,
unsigned viewshift) {
mzd_local_t tmp = {{0}};
for (unsigned int m = 0; m < SC_PROOF; ++m) {
const unsigned int j = (m + 1) % SC_PROOF;
// f[m] & s[m]
mzd_and_uint64_192(&res[m], &first[m], &second[m]);
// f[m + 1] & s[m]
mzd_and_uint64_192(&tmp, &first[j], &second[m]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
// f[m] & s[m + 1]
mzd_and_uint64_192(&tmp, &first[m], &second[j]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
// ... ^ r[m] ^ r[m + 1]
mzd_xor_uint64_192(&tmp, &r[m], &r[j]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
if (viewshift) {
mzd_shift_right_uint64_192(&tmp, &res[m], viewshift);
mzd_xor_uint64_192(&view->s[m], &view->s[m], &tmp);
} else {
// on first call (viewshift == 0), view->t[0..2] == 0
mzd_copy_uint64_192(&view->s[m], &res[m]);
}
}
}
static void mpc_and_verify_uint64_192(mzd_local_t* res, const mzd_local_t* first,
const mzd_local_t* second, const mzd_local_t* r, view_t* view,
const mzd_local_t* mask, unsigned viewshift) {
mzd_local_t tmp = {{0}};
for (unsigned int m = 0; m < (SC_VERIFY - 1); ++m) {
const unsigned int j = m + 1;
mzd_and_uint64_192(&res[m], &first[m], &second[m]);
mzd_and_uint64_192(&tmp, &first[j], &second[m]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
mzd_and_uint64_192(&tmp, &first[m], &second[j]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
mzd_xor_uint64_192(&tmp, &r[m], &r[j]);
mzd_xor_uint64_192(&res[m], &res[m], &tmp);
if (viewshift) {
mzd_shift_right_uint64_192(&tmp, &res[m], viewshift);
mzd_xor_uint64_192(&view->s[m], &view->s[m], &tmp);
} else {
// on first call (viewshift == 0), view->s[0] == 0
mzd_copy_uint64_192(&view->s[m], &res[m]);
}
}
if (viewshift) {
mzd_shift_left_uint64_192(&tmp, &view->s[SC_VERIFY - 1], viewshift);
mzd_and_uint64_192(&res[SC_VERIFY - 1], &tmp, mask);
} else {
mzd_and_uint64_192(&res[SC_VERIFY - 1], &view->s[SC_VERIFY - 1], mask);
}
}
#endif
#if defined(WITH_LOWMC_255_255_4)
static void mpc_and_uint64_256(mzd_local_t* res, const mzd_local_t* first,
const mzd_local_t* second, const mzd_local_t* r, view_t* view,
unsigned viewshift) {
mzd_local_t tmp = {{0}};
for (unsigned int m = 0; m < SC_PROOF; ++m) {
const unsigned int j = (m + 1) % SC_PROOF;
// f[m] & s[m]
mzd_and_uint64_256(&res[m], &first[m], &second[m]);
// f[m + 1] & s[m]
mzd_and_uint64_256(&tmp, &first[j], &second[m]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
// f[m] & s[m + 1]
mzd_and_uint64_256(&tmp, &first[m], &second[j]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
// ... ^ r[m] ^ r[m + 1]
mzd_xor_uint64_256(&tmp, &r[m], &r[j]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
if (viewshift) {
mzd_shift_right_uint64_256(&tmp, &res[m], viewshift);
mzd_xor_uint64_256(&view->s[m], &view->s[m], &tmp);
} else {
// on first call (viewshift == 0), view->t[0..2] == 0
mzd_copy_uint64_256(&view->s[m], &res[m]);
}
}
}
static void mpc_and_verify_uint64_256(mzd_local_t* res, const mzd_local_t* first,
const mzd_local_t* second, const mzd_local_t* r, view_t* view,
const mzd_local_t* mask, unsigned viewshift) {
mzd_local_t tmp = {{0}};
for (unsigned int m = 0; m < (SC_VERIFY - 1); ++m) {
const unsigned int j = m + 1;
mzd_and_uint64_256(&res[m], &first[m], &second[m]);
mzd_and_uint64_256(&tmp, &first[j], &second[m]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
mzd_and_uint64_256(&tmp, &first[m], &second[j]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
mzd_xor_uint64_256(&tmp, &r[m], &r[j]);
mzd_xor_uint64_256(&res[m], &res[m], &tmp);
if (viewshift) {
mzd_shift_right_uint64_256(&tmp, &res[m], viewshift);
mzd_xor_uint64_256(&view->s[m], &view->s[m], &tmp);
} else {
// on first call (viewshift == 0), view->s[0] == 0
mzd_copy_uint64_256(&view->s[m], &res[m]);
}
}
if (viewshift) {
mzd_shift_left_uint64_256(&tmp, &view->s[SC_VERIFY - 1], viewshift);
mzd_and_uint64_256(&res[SC_VERIFY - 1], &tmp, mask);
} else {
mzd_and_uint64_256(&res[SC_VERIFY - 1], &view->s[SC_VERIFY - 1], mask);
}
}
#endif
#define bitsliced_step_1(sc, AND, ROL, MASK_A, MASK_B, MASK_C) \
mzd_local_t x2m[sc] = {{{0}}}; \
mzd_local_t r0m[sc] = {{{0}}}, r1m[sc] = {{{0}}}, r2m[sc] = {{{0}}}; \
mzd_local_t x0s[sc] = {{{0}}}, x1s[sc] = {{{0}}}, r0s[sc] = {{{0}}}, r1s[sc] = {{{0}}}; \
\
for (unsigned int m = 0; m < (sc); ++m) { \
AND(&x0s[m], &in[m], MASK_A); \
AND(&x1s[m], &in[m], MASK_B); \
AND(&x2m[m], &in[m], MASK_C); \
\
ROL(&x0s[m], &x0s[m], 2); \
ROL(&x1s[m], &x1s[m], 1); \
\
AND(&r0m[m], &rvec->s[m], MASK_A); \
AND(&r1m[m], &rvec->s[m], MASK_B); \
AND(&r2m[m], &rvec->s[m], MASK_C); \
\
ROL(&r0s[m], &r0m[m], 2); \
ROL(&r1s[m], &r1m[m], 1); \
}
#define bitsliced_step_2(sc, XOR, ROR) \
for (unsigned int m = 0; m < sc; ++m) { \
XOR(&r2m[m], &r2m[m], &x0s[m]); \
XOR(&x0s[m], &x0s[m], &x1s[m]); \
XOR(&r1m[m], &r1m[m], &x0s[m]); \
XOR(&r0m[m], &r0m[m], &x0s[m]); \
XOR(&r0m[m], &r0m[m], &x2m[m]); \
\
ROR(&x0s[m], &r2m[m], 2); \
ROR(&x1s[m], &r1m[m], 1); \
\
XOR(&x0s[m], &x0s[m], &r0m[m]); \
XOR(&out[m], &x0s[m], &x1s[m]); \
}
#if defined(WITH_LOWMC_129_129_4)
static void mpc_sbox_prove_uint64_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_PROOF, mzd_and_uint64_192, mzd_shift_left_uint64_192, mask_129_129_43_a,
mask_129_129_43_b, mask_129_129_43_c);
// a & b
mpc_and_uint64_192(r0m, x0s, x1s, r2m, view, 0);
// b & c
mpc_and_uint64_192(r2m, x1s, x2m, r1s, view, 1);
// c & a
mpc_and_uint64_192(r1m, x0s, x2m, r0s, view, 2);
bitsliced_step_2(SC_PROOF, mzd_xor_uint64_192, mzd_shift_right_uint64_192);
}
static void mpc_sbox_verify_uint64_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_VERIFY, mzd_and_uint64_192, mzd_shift_left_uint64_192, mask_129_129_43_a,
mask_129_129_43_b, mask_129_129_43_c);
// a & b
mpc_and_verify_uint64_192(r0m, x0s, x1s, r2m, view, mask_129_129_43_c, 0);
// b & c
mpc_and_verify_uint64_192(r2m, x1s, x2m, r1s, view, mask_129_129_43_c, 1);
// c & a
mpc_and_verify_uint64_192(r1m, x0s, x2m, r0s, view, mask_129_129_43_c, 2);
bitsliced_step_2(SC_VERIFY, mzd_xor_uint64_192, mzd_shift_right_uint64_192);
}
#endif
#if defined(WITH_LOWMC_192_192_4)
static void mpc_sbox_prove_uint64_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_PROOF, mzd_and_uint64_192, mzd_shift_left_uint64_192, mask_192_192_64_a,
mask_192_192_64_b, mask_192_192_64_c);
// a & b
mpc_and_uint64_192(r0m, x0s, x1s, r2m, view, 0);
// b & c
mpc_and_uint64_192(r2m, x1s, x2m, r1s, view, 1);
// c & a
mpc_and_uint64_192(r1m, x0s, x2m, r0s, view, 2);
bitsliced_step_2(SC_PROOF, mzd_xor_uint64_192, mzd_shift_right_uint64_192);
}
static void mpc_sbox_verify_uint64_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_VERIFY, mzd_and_uint64_192, mzd_shift_left_uint64_192, mask_192_192_64_a,
mask_192_192_64_b, mask_192_192_64_c);
// a & b
mpc_and_verify_uint64_192(r0m, x0s, x1s, r2m, view, mask_192_192_64_c, 0);
// b & c
mpc_and_verify_uint64_192(r2m, x1s, x2m, r1s, view, mask_192_192_64_c, 1);
// c & a
mpc_and_verify_uint64_192(r1m, x0s, x2m, r0s, view, mask_192_192_64_c, 2);
bitsliced_step_2(SC_VERIFY, mzd_xor_uint64_192, mzd_shift_right_uint64_192);
}
#endif
#if defined(WITH_LOWMC_255_255_4)
static void mpc_sbox_prove_uint64_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_PROOF, mzd_and_uint64_256, mzd_shift_left_uint64_256, mask_255_255_85_a,
mask_255_255_85_b, mask_255_255_85_c);
// a & b
mpc_and_uint64_256(r0m, x0s, x1s, r2m, view, 0);
// b & c
mpc_and_uint64_256(r2m, x1s, x2m, r1s, view, 1);
// c & a
mpc_and_uint64_256(r1m, x0s, x2m, r0s, view, 2);
bitsliced_step_2(SC_PROOF, mzd_xor_uint64_256, mzd_shift_right_uint64_256);
}
static void mpc_sbox_verify_uint64_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
bitsliced_step_1(SC_VERIFY, mzd_and_uint64_256, mzd_shift_left_uint64_256, mask_255_255_85_a,
mask_255_255_85_b, mask_255_255_85_c);
// a & b
mpc_and_verify_uint64_256(r0m, x0s, x1s, r2m, view, mask_255_255_85_c, 0);
// b & c
mpc_and_verify_uint64_256(r2m, x1s, x2m, r1s, view, mask_255_255_85_c, 1);
// c & a
mpc_and_verify_uint64_256(r1m, x0s, x2m, r0s, view, mask_255_255_85_c, 2);
bitsliced_step_2(SC_VERIFY, mzd_xor_uint64_256, mzd_shift_right_uint64_256);
}
#endif
#endif /* NO_UINT_FALLBACK */
#if defined(WITH_OPT)
#define NROLR(a, b, c) \
do { \
a[0] = b[0]; \
a[1] = b[1]; \
(void)c; \
} while (0)
/* requires IN and RVEC to be defined */
#define bitsliced_mm256_step_1(sc, MASK_A, MASK_B, MASK_C) \
word256 r0m[sc]; \
word256 r0s[sc]; \
word256 r1m[sc]; \
word256 r1s[sc]; \
word256 r2m[sc]; \
word256 x0s[sc]; \
word256 x1s[sc]; \
word256 x2m[sc]; \
do { \
for (unsigned int m = 0; m < (sc); ++m) { \
word256 tmp = mm256_load(IN(m)); \
x0s[m] = mm256_rotate_left(mm256_and(tmp, MASK_A), 2); \
x1s[m] = mm256_rotate_left(mm256_and(tmp, MASK_B), 1); \
x2m[m] = mm256_and(tmp, MASK_C); \
\
tmp = mm256_load(RVEC(m)); \
r0s[m] = mm256_rotate_left(mm256_and(tmp, MASK_A), 2); \
r1s[m] = mm256_rotate_left(mm256_and(tmp, MASK_B), 1); \
r2m[m] = mm256_and(tmp, MASK_C); \
} \
} while (0)
#define bitsliced_mm256_step_2(sc) \
do { \
for (unsigned int m = 0; m < sc; ++m) { \
r2m[m] = mm256_xor(r2m[m], x0s[m]); \
x0s[m] = mm256_xor(x0s[m], x1s[m]); \
r1m[m] = mm256_xor(x0s[m], r1m[m]); \
r0m[m] = mm256_xor(x0s[m], r0m[m]); \
\
x0s[m] = mm256_rotate_right(r2m[m], 2); \
x1s[m] = mm256_rotate_right(r1m[m], 1); \
\
mm256_store(OUT(m), mm256_xor(mm256_xor(r0m[m], x2m[m]), mm256_xor(x0s[m], x1s[m]))); \
} \
} while (0)
#define mpc_mm256_and_def(res, first, second, r, viewshift) \
do { \
for (unsigned int m = 0; m < SC_PROOF; ++m) { \
const unsigned int j = (m + 1) % SC_PROOF; \
\
word256 tmp1 = mm256_xor(mm256_and(first[m], second[m]), mm256_and(first[j], second[m])); \
word256 tmp2 = mm256_xor(mm256_xor(r[m], r[j]), mm256_and(first[m], second[j])); \
res[m] = mm256_xor(tmp1, tmp2); \
if (viewshift) { \
mm256_store(VIEW(m), \
mm256_xor(mm256_rotate_right(res[m], viewshift), mm256_load(VIEW(m)))); \
} else { \
mm256_store(VIEW(m), res[m]); \
} \
} \
} while (0)
#define mpc_mm256_and_verify_def(res, first, second, r, MASK, viewshift) \
do { \
for (unsigned int m = 0; m < (SC_VERIFY - 1); ++m) { \
const unsigned int j = m + 1; \
\
word256 tmp1 = mm256_xor(mm256_and(first[m], second[m]), mm256_and(first[j], second[m])); \
word256 tmp2 = mm256_xor(mm256_xor(r[m], r[j]), mm256_and(first[m], second[j])); \
res[m] = mm256_xor(tmp1, tmp2); \
if (viewshift) { \
mm256_store(VIEW(m), \
mm256_xor(mm256_rotate_right(res[m], viewshift), mm256_load(VIEW(m)))); \
} else { \
mm256_store(VIEW(m), res[m]); \
} \
} \
if (viewshift) { \
const word256 tmp = mm256_load(VIEW(SC_VERIFY - 1)); \
res[SC_VERIFY - 1] = mm256_and(mm256_rotate_left(tmp, viewshift), MASK); \
} else { \
res[SC_VERIFY - 1] = mm256_and(mm256_load(VIEW(SC_VERIFY - 1)), MASK); \
} \
} while (0)
#if defined(WITH_SSE2) || defined(WITH_NEON)
#define bitsliced_mm128_256_step_1(sc, MASK_A, MASK_B, MASK_C) \
word128 r0m[sc][2]; \
word128 r0s[sc][2]; \
word128 r1m[sc][2]; \
word128 r1s[sc][2]; \
word128 r2m[sc][2]; \
word128 x0s[sc][2]; \
word128 x1s[sc][2]; \
word128 x2m[sc][2]; \
do { \
word128 tmp[2], bitm_a[2], bitm_b[2], bitm_c[2]; \
bitm_a[0] = mm128_load(&MASK_A->w64[0]); \
bitm_a[1] = mm128_load(&MASK_A->w64[2]); \
bitm_b[0] = mm128_load(&MASK_B->w64[0]); \
bitm_b[1] = mm128_load(&MASK_B->w64[2]); \
bitm_c[0] = mm128_load(&MASK_C->w64[0]); \
bitm_c[1] = mm128_load(&MASK_C->w64[2]); \
for (unsigned int m = 0; m < (sc); ++m) { \
tmp[0] = mm128_load(&IN(m)[0]); \
tmp[1] = mm128_load(&IN(m)[2]); \
mm128_and_256(x0s[m], tmp, bitm_a); \
mm128_and_256(x1s[m], tmp, bitm_b); \
mm128_and_256(x2m[m], tmp, bitm_c); \
\
mm128_shift_left_256(x0s[m], x0s[m], 2); \
mm128_shift_left_256(x1s[m], x1s[m], 1); \
\
tmp[0] = mm128_load(&RVEC(m)[0]); \
tmp[1] = mm128_load(&RVEC(m)[2]); \
mm128_and_256(r0m[m], tmp, bitm_a); \
mm128_and_256(r1m[m], tmp, bitm_b); \
mm128_and_256(r2m[m], tmp, bitm_c); \
\
mm128_shift_left_256(r0s[m], r0m[m], 2); \
mm128_shift_left_256(r1s[m], r1m[m], 1); \
} \
} while (0)
#define bitsliced_mm128_256_step_2(sc) \
do { \
for (unsigned int m = 0; m < sc; ++m) { \
mm128_xor_256(r2m[m], r2m[m], x0s[m]); \
mm128_xor_256(x0s[m], x0s[m], x1s[m]); \
mm128_xor_256(r1m[m], x0s[m], r1m[m]); \
mm128_xor_256(r0m[m], x0s[m], r0m[m]); \
mm128_xor_256(r0m[m], r0m[m], x2m[m]); \
\
mm128_shift_right_256(x0s[m], r2m[m], 2); \
mm128_shift_right_256(x1s[m], r1m[m], 1); \
\
mm128_xor_256(x0s[m], x0s[m], x1s[m]); \
mm128_xor_256(r0m[m], r0m[m], x0s[m]); \
mm128_store(&OUT(m)[0], r0m[m][0]); \
mm128_store(&OUT(m)[2], r0m[m][1]); \
} \
} while (0)
#define mpc_mm128_256_and_def(ROR, res, first, second, r, viewshift) \
do { \
for (unsigned int m = 0; m < SC_PROOF; ++m) { \
const unsigned int j = (m + 1) % SC_PROOF; \
word128 tmp1[2], tmp2[2]; \
\
mm128_and_256(tmp1, first[m], second[m]); \
mm128_and_256(tmp2, first[j], second[m]); \
mm128_xor_256(res[m], tmp1, tmp2); \
mm128_and_256(tmp1, first[m], second[j]); \
mm128_xor_256(res[m], res[m], tmp1); \
mm128_xor_256(tmp2, r[m], r[j]); \
mm128_xor_256(res[m], res[m], tmp2); \
if (viewshift) { \
ROR(tmp1, res[m], viewshift); \
tmp2[0] = mm128_load(&VIEW(m)[0]); \
tmp2[1] = mm128_load(&VIEW(m)[2]); \
mm128_xor_256(tmp2, tmp1, tmp2); \
mm128_store(&VIEW(m)[0], tmp2[0]); \
mm128_store(&VIEW(m)[2], tmp2[1]); \
} else { \
mm128_store(&VIEW(m)[0], res[m][0]); \
mm128_store(&VIEW(m)[2], res[m][1]); \
} \
} \
} while (0)
#define mpc_mm128_256_and_verify_def(ROL, ROR, res, first, second, r, MASK, viewshift) \
do { \
word128 tmp1[2], tmp2[2]; \
for (unsigned int m = 0; m < (SC_VERIFY - 1); ++m) { \
const unsigned int j = (m + 1) % SC_PROOF; \
\
mm128_and_256(tmp1, first[m], second[m]); \
mm128_and_256(tmp2, first[j], second[m]); \
mm128_xor_256(res[m], tmp1, tmp2); \
mm128_and_256(tmp1, first[m], second[j]); \
mm128_xor_256(res[m], res[m], tmp1); \
mm128_xor_256(tmp2, r[m], r[j]); \
mm128_xor_256(res[m], res[m], tmp2); \
if (viewshift) { \
ROR(tmp1, res[m], viewshift); \
tmp2[0] = mm128_load(&VIEW(m)[0]); \
tmp2[1] = mm128_load(&VIEW(m)[2]); \
mm128_xor_256(tmp2, tmp1, tmp2); \
mm128_store(&VIEW(m)[0], tmp2[0]); \
mm128_store(&VIEW(m)[2], tmp2[1]); \
} else { \
mm128_store(&VIEW(m)[0], res[m][0]); \
mm128_store(&VIEW(m)[2], res[m][1]); \
} \
} \
\
tmp2[0] = mm128_load(&VIEW(SC_VERIFY - 1)[0]); \
tmp2[1] = mm128_load(&VIEW(SC_VERIFY - 1)[2]); \
tmp1[0] = mm128_load(&MASK->w64[0]); \
tmp1[1] = mm128_load(&MASK->w64[2]); \
if (viewshift) { \
ROL(tmp2, tmp2, viewshift); \
mm128_and_256(res[SC_VERIFY - 1], tmp2, tmp1); \
} else { \
mm128_and_256(res[SC_VERIFY - 1], tmp2, tmp1); \
} \
} while (0)
#define IN(m) in[m].w64
#define OUT(m) out[m].w64
#define RVEC(m) rvec->s[m].w64
#define VIEW(m) view->s[m].w64
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_192_192_4) || defined(WITH_LOWMC_255_255_4)
ATTR_TARGET_S128
static inline void mpc_sbox_prove_s128_256(mzd_local_t* out, const mzd_local_t* in, view_t* view,
const rvec_t* rvec, const mzd_local_t* mask_a,
const mzd_local_t* mask_b, const mzd_local_t* mask_c) {
bitsliced_mm128_256_step_1(SC_PROOF, mask_a, mask_b, mask_c);
// a & b
mpc_mm128_256_and_def(NROLR, r0m, x0s, x1s, r2m, 0);
// b & c
mpc_mm128_256_and_def(mm128_shift_right_256, r2m, x1s, x2m, r1s, 1);
// c & a
mpc_mm128_256_and_def(mm128_shift_right_256, r1m, x0s, x2m, r0s, 2);
bitsliced_mm128_256_step_2(SC_PROOF);
}
ATTR_TARGET_S128
static inline void mpc_sbox_verify_s128_256(mzd_local_t* out, const mzd_local_t* in, view_t* view,
const rvec_t* rvec, const mzd_local_t* mask_a,
const mzd_local_t* mask_b, const mzd_local_t* mask_c) {
bitsliced_mm128_256_step_1(SC_VERIFY, mask_a, mask_b, mask_c);
// a & b
mpc_mm128_256_and_verify_def(NROLR, NROLR, r0m, x0s, x1s, r2m, mask_c, 0);
// b & c
mpc_mm128_256_and_verify_def(mm128_shift_left_256, mm128_shift_right_256, r2m, x1s, x2m, r1s,
mask_c, 1);
// c & a
mpc_mm128_256_and_verify_def(mm128_shift_left_256, mm128_shift_right_256, r1m, x0s, x2m, r0s,
mask_c, 2);
bitsliced_mm128_256_step_2(SC_VERIFY);
}
#endif
#if defined(WITH_LOWMC_129_129_4)
ATTR_TARGET_S128
static void mpc_sbox_prove_s128_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s128_256(out, in, view, rvec, mask_129_129_43_a, mask_129_129_43_b,
mask_129_129_43_c);
}
ATTR_TARGET_S128
static void mpc_sbox_verify_s128_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s128_256(out, in, view, rvec, mask_129_129_43_a, mask_129_129_43_b,
mask_129_129_43_c);
}
#endif
#if defined(WITH_LOWMC_192_192_4)
ATTR_TARGET_S128
static void mpc_sbox_prove_s128_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s128_256(out, in, view, rvec, mask_192_192_64_a, mask_192_192_64_b,
mask_192_192_64_c);
}
ATTR_TARGET_S128
static void mpc_sbox_verify_s128_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s128_256(out, in, view, rvec, mask_192_192_64_a, mask_192_192_64_b,
mask_192_192_64_c);
}
#endif
#if defined(WITH_LOWMC_255_255_4)
ATTR_TARGET_S128
static void mpc_sbox_prove_s128_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s128_256(out, in, view, rvec, mask_255_255_85_a, mask_255_255_85_b,
mask_255_255_85_c);
}
ATTR_TARGET_S128
static void mpc_sbox_verify_s128_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s128_256(out, in, view, rvec, mask_255_255_85_a, mask_255_255_85_b,
mask_255_255_85_c);
}
#endif
#undef IN
#undef OUT
#undef RVEC
#undef VIEW
#endif /* WITH_SSE2 || WITH_NEON */
#if defined(WITH_AVX2)
#define IN(m) in[m].w64
#define OUT(m) out[m].w64
#define RVEC(m) rvec->s[m].w64
#define VIEW(m) view->s[m].w64
#if defined(WITH_LOWMC_129_129_4) || defined(WITH_LOWMC_192_192_4) || defined(WITH_LOWMC_255_255_4)
ATTR_TARGET_AVX2
static inline void mpc_sbox_prove_s256_256(mzd_local_t* out, const mzd_local_t* in, view_t* view,
const rvec_t* rvec, const word256 mask_a,
const word256 mask_b, const word256 mask_c) {
bitsliced_mm256_step_1(SC_PROOF, mask_a, mask_b, mask_c);
// a & b
mpc_mm256_and_def(r0m, x0s, x1s, r2m, 0);
// b & c
mpc_mm256_and_def(r2m, x1s, x2m, r1s, 1);
// c & a
mpc_mm256_and_def(r1m, x0s, x2m, r0s, 2);
bitsliced_mm256_step_2(SC_PROOF);
}
ATTR_TARGET_AVX2
static void mpc_sbox_verify_s256_256(mzd_local_t* out, const mzd_local_t* in, view_t* view,
const rvec_t* rvec, const word256 mask_a, const word256 mask_b,
const word256 mask_c) {
bitsliced_mm256_step_1(SC_VERIFY, mask_a, mask_b, mask_c);
// a & b
mpc_mm256_and_verify_def(r0m, x0s, x1s, r2m, mask_c, 0);
// b & c
mpc_mm256_and_verify_def(r2m, x1s, x2m, r1s, mask_c, 1);
// c & a
mpc_mm256_and_verify_def(r1m, x0s, x2m, r0s, mask_c, 2);
bitsliced_mm256_step_2(SC_VERIFY);
}
#endif
#if defined(WITH_LOWMC_129_129_4)
ATTR_TARGET_AVX2
static void mpc_sbox_prove_s256_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s256_256(out, in, view, rvec,
mm256_set_4(MASK_129_129_43_A_0, MASK_129_129_43_A_1, MASK_129_129_43_A_2,
MASK_129_129_43_A_3),
mm256_set_4(MASK_129_129_43_B_0, MASK_129_129_43_B_1, MASK_129_129_43_B_2,
MASK_129_129_43_B_3),
mm256_set_4(MASK_129_129_43_C_0, MASK_129_129_43_C_1, MASK_129_129_43_C_2,
MASK_129_129_43_C_3));
}
ATTR_TARGET_AVX2
static void mpc_sbox_verify_s256_lowmc_129_129_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s256_256(out, in, view, rvec,
mm256_set_4(MASK_129_129_43_A_0, MASK_129_129_43_A_1,
MASK_129_129_43_A_2, MASK_129_129_43_A_3),
mm256_set_4(MASK_129_129_43_B_0, MASK_129_129_43_B_1,
MASK_129_129_43_B_2, MASK_129_129_43_B_3),
mm256_set_4(MASK_129_129_43_C_0, MASK_129_129_43_C_1,
MASK_129_129_43_C_2, MASK_129_129_43_C_3));
}
#endif
#if defined(WITH_LOWMC_192_192_4)
ATTR_TARGET_AVX2
static void mpc_sbox_prove_s256_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s256_256(out, in, view, rvec,
mm256_set_4(MASK_192_192_64_A_0, MASK_192_192_64_A_1, MASK_192_192_64_A_2,
MASK_192_192_64_A_3),
mm256_set_4(MASK_192_192_64_B_0, MASK_192_192_64_B_1, MASK_192_192_64_B_2,
MASK_192_192_64_B_3),
mm256_set_4(MASK_192_192_64_C_0, MASK_192_192_64_C_1, MASK_192_192_64_C_2,
MASK_192_192_64_C_3));
}
ATTR_TARGET_AVX2
static void mpc_sbox_verify_s256_lowmc_192_192_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s256_256(out, in, view, rvec,
mm256_set_4(MASK_192_192_64_A_0, MASK_192_192_64_A_1,
MASK_192_192_64_A_2, MASK_192_192_64_A_3),
mm256_set_4(MASK_192_192_64_B_0, MASK_192_192_64_B_1,
MASK_192_192_64_B_2, MASK_192_192_64_B_3),
mm256_set_4(MASK_192_192_64_C_0, MASK_192_192_64_C_1,
MASK_192_192_64_C_2, MASK_192_192_64_C_3));
}
#endif
#if defined(WITH_LOWMC_255_255_4)
ATTR_TARGET_AVX2
static void mpc_sbox_prove_s256_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_prove_s256_256(out, in, view, rvec,
mm256_set_4(MASK_255_255_85_A_0, MASK_255_255_85_A_1, MASK_255_255_85_A_2,
MASK_255_255_85_A_3),
mm256_set_4(MASK_255_255_85_B_0, MASK_255_255_85_B_1, MASK_255_255_85_B_2,
MASK_255_255_85_B_3),
mm256_set_4(MASK_255_255_85_C_0, MASK_255_255_85_C_1, MASK_255_255_85_C_2,
MASK_255_255_85_C_3));
}
ATTR_TARGET_AVX2
static void mpc_sbox_verify_s256_lowmc_255_255_4(mzd_local_t* out, const mzd_local_t* in,
view_t* view, const rvec_t* rvec) {
mpc_sbox_verify_s256_256(out, in, view, rvec,
mm256_set_4(MASK_255_255_85_A_0, MASK_255_255_85_A_1,
MASK_255_255_85_A_2, MASK_255_255_85_A_3),
mm256_set_4(MASK_255_255_85_B_0, MASK_255_255_85_B_1,
MASK_255_255_85_B_2, MASK_255_255_85_B_3),
mm256_set_4(MASK_255_255_85_C_0, MASK_255_255_85_C_1,
MASK_255_255_85_C_2, MASK_255_255_85_C_3));
}
#endif
#endif /* WITH_AVX2*/
#endif /* WITH_OPT */
/* TODO: get rid of the copies */
#define SBOX(sbox, y, x, views, rvec, n, shares, shares2) \
{ \
mzd_local_t tmp[shares]; \
for (unsigned int count = 0; count < shares; ++count) { \
memcpy(tmp[count].w64, CONST_BLOCK(x[count], 0)->w64, sizeof(mzd_local_t)); \
} \
sbox(tmp, tmp, views, rvec); \
for (unsigned int count = 0; count < shares; ++count) { \
memcpy(BLOCK(y[count], 0)->w64, tmp[count].w64, sizeof(mzd_local_t)); \
} \
} \
while (0)
#define SBOX_uint64(sbox, y, x, views, rvec, n, shares, shares2) \
do { \
uint64_t in[shares]; \
for (unsigned int count = 0; count < shares; ++count) { \
in[count] = CONST_BLOCK(x[count], 0)->w64[(n) / (sizeof(word) * 8) - 1]; \
} \
sbox(in, views, rvec->t); \
for (unsigned int count = 0; count < shares2; ++count) { \
memcpy(BLOCK(y[count], 0)->w64, CONST_BLOCK(x[count], 0)->w64, \
((n) / (sizeof(word) * 8) - 1) * sizeof(word)); \
BLOCK(y[count], 0)->w64[(n) / (sizeof(word) * 8) - 1] = in[count]; \
} \
} while (0)
#if !defined(NO_UINT64_FALLBACK)
#define IMPL uint64
// uint64 based implementation
#include "lowmc_128_128_20_fns_uint64.h"
#include "mpc_lowmc.c.i"
#include "lowmc_129_129_4_fns_uint64.h"
#include "mpc_lowmc.c.i"
#include "lowmc_192_192_30_fns_uint64.h"
#include "mpc_lowmc.c.i"
#include "lowmc_192_192_4_fns_uint64.h"
#include "mpc_lowmc.c.i"
#include "lowmc_256_256_38_fns_uint64.h"
#include "mpc_lowmc.c.i"
#include "lowmc_255_255_4_fns_uint64.h"
#include "mpc_lowmc.c.i"
#endif
#if defined(WITH_OPT)
#if defined(WITH_SSE2) || defined(WITH_NEON)
#define FN_ATTR ATTR_TARGET_S128
#undef IMPL
#define IMPL s128
// L1 using SSE2/NEON
#include "lowmc_128_128_20_fns_s128.h"
#include "mpc_lowmc.c.i"
#include "lowmc_129_129_4_fns_s128.h"
#include "mpc_lowmc.c.i"
// L3 using SSE2/NEON
#include "lowmc_192_192_30_fns_s128.h"
#include "mpc_lowmc.c.i"
#include "lowmc_192_192_4_fns_s128.h"
#include "mpc_lowmc.c.i"
// L5 using SSE2/NEON
#include "lowmc_256_256_38_fns_s128.h"
#include "mpc_lowmc.c.i"
#include "lowmc_255_255_4_fns_s128.h"
#include "mpc_lowmc.c.i"
#undef FN_ATTR
#endif
#if defined(WITH_AVX2)
#define FN_ATTR ATTR_TARGET_AVX2
#undef IMPL
#define IMPL s256
// L1 using AVX2
#include "lowmc_128_128_20_fns_s256.h"
#include "mpc_lowmc.c.i"
#include "lowmc_129_129_4_fns_s256.h"
#include "mpc_lowmc.c.i"
// L3 using AVX2
#include "lowmc_192_192_30_fns_s256.h"
#include "mpc_lowmc.c.i"
#include "lowmc_192_192_4_fns_s256.h"
#include "mpc_lowmc.c.i"
// L5 using AVX2
#include "lowmc_256_256_38_fns_s256.h"
#include "mpc_lowmc.c.i"
#include "lowmc_255_255_4_fns_s256.h"
#include "mpc_lowmc.c.i"
#undef FN_ATTR
#endif
#endif