-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxoodooRound.cpp
More file actions
executable file
·1826 lines (1685 loc) · 60.2 KB
/
Copy pathxoodooRound.cpp
File metadata and controls
executable file
·1826 lines (1685 loc) · 60.2 KB
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
//
// xoodooRound.cpp
//
// Created by Haochen on 05/15/21.
//
#include <cstdlib>
#include <chrono>
#include <ctime>
#include "xoodooRound.h"
using namespace std::chrono;
using namespace XOODOOSAT;
/**
* @brief Construct a new XoodooRound object
*
* @param analy_mode int, 0 for differential, 1 for linear
* @param rounds int, number of rounds
* @param weight int, the weight bound
* @param thread int, number of threads
* @param mode int, 0 for <=weight bound, 1 for =weight bound, 2 for >=weight bound
*/
XoodooRound::XoodooRound(int analy_mode, int rounds, int weight, int thread, int mode) {
// set parameter
analysis_mode = analy_mode; // 0 for differential, 1 for linear
AS_weight_num = weight; // weight bound
AS_state_num = rounds; // how many rounds of AS need to be considered in weight calculation
AS_mode = mode; // 0 for <=weight bound, 1 for =weight bound, 2 for >=weight bound
round_num = rounds; // number of rounds
// trail core a1,b1,a2,b2,...
// number of states in the trail core
core_state_num = 2 * (round_num - 1);
// total number of AS in all states
AS_var_num = X * Z * AS_state_num;
// number of AS in one state(node)
AS_node_var_num = X * Z;
// total number of bits in all states
round_var_num = var_num * 2;
// path to AS cnf file
objFilePath = default_obj_file_path;
thread_num = thread;
theta_order = compute_Theta_Order();
solver.set_num_threads(thread_num);// threads to use
/*
X=4,Y=3,Z=32
for 3 round trail cores:
(0-383) a1
(384,767) b1
(768,1151) a2
(1152,1535) b2
each AS has X*Z=128 varibles
AS_a1 (1536, 1663)
AS_a2 (1664, 1791)
AS_b2 (1792, 1919)
*/
solver.new_vars(var_num*core_state_num);// add vars
rho_plane[0] = 1; // which plane to rho (plane 1 and 2)
rho_plane[1] = 2;
theta_L1[0] = 1; // 1st lshift in theta
theta_L1[1] = 5;
theta_L2[0] = 1; // 2nd lshift in theta
theta_L2[1] = 14;
rhoW_L1[0] = 1; // 1st lshift in rhoW
rhoW_L1[1] = 0;
rhoW_L2[0] = 0; // 2nd lshift in rhoW
rhoW_L2[1] = 11;
rhoE_L1[0] = 0; // 1st lshift in rhoE
rhoE_L1[1] = 1;
rhoE_L2[0] = 2; // 2nd lshift in rhoE
rhoE_L2[1] = 8;
// generate the input-output relation of transformations(rhoe, rhow, theta)
gen_RhoE_T(RhoE_Relation, inverse_RhoE_Relation);
gen_RhoW_T(RhoW_Relation, inverse_RhoW_Relation);
gen_Theta_T(Theta_Relation, transpose_Theta_Relation);
// generate chi cnf
gen_xoodoo_Chi_cnf(Chi_Relation);
// generate AS weight cnf
gen_xoodoo_AS_cnf(AS_Relation);
// generate AS weight bound cnf
gen_obj_T(obj, solver, AS_weight_num, AS_var_num, AS_mode);
}
/**
* @brief compute the order of theta transformation
*
* @return unsigned int, the order
*/
unsigned int XoodooRound::compute_Theta_Order() {
// xoodoo X=4
unsigned int oddPartSizeX = X;
// 2^i, begin with 2^0=1
unsigned int powerTwoPartSizeX = 1;
// powerTwoPartSizeX=2^i, where i is the lowest 1 in X
// here X=4=0b100, so i=2, powerTwoPartSizeX = 4
// oddPartSizeX = X >> i = 1
while ((oddPartSizeX & 1) == 0) {
oddPartSizeX >>= 1;
powerTwoPartSizeX <<= 1;
}
// order = Z = 32
unsigned int order = Z;
if (powerTwoPartSizeX > order) order = powerTwoPartSizeX;
// since oddPartSizeX = 1, order = Z
switch (oddPartSizeX) {
case 1:
break;
case 3:
order *= 3;
break;
case 5:
order *= 15;
break;
case 7:
order *= 7;
break;
throw ((string)"X is not a power of two times, 3, 5 or 7");
}
return order;
}
/**
* @brief theta transformation
*
* @param A tXoodooState&
*/
void XoodooRound::theta(tXoodooState &A) {
unsigned int x, y;
vector<tXoodooLane> P(X, 0), E(X, 0);
// compute the parity
for (x = 0; x < X; x++) {
for (y = 0; y < Y; y++)
P[x] ^= A[indexXY(x, y)];
}
// compute E = P<<(1,5) ^ P<<(1,14)
for (x = 0; x < X; x++)
E[x] = ROLxoo(P[(x + X - theta_L1[0]) % X], theta_L1[1]) ^ ROLxoo(P[(x + X - theta_L2[0]) % X], theta_L2[1]);
// finally output = input ^ E
for (x = 0; x < X; x++)
for (y = 0; y < Y; y++)
A[indexXY(x, y)] ^= E[x];
}
/**
* @brief transpose theta transformation
*
* @param A tXoodooState&
*/
void XoodooRound::transposetheta(tXoodooState &A) {
unsigned int x, y;
vector<tXoodooLane> P(X, 0), E(X, 0);
// compute the parity
for (x = 0; x < X; x++) {
for (y = 0; y < Y; y++)
P[x] ^= A[indexXY(x, y)];
}
// compute E = P>>(1,5) ^ P>>(1,14)
for (x = 0; x < X; x++)
E[x] = RORxoo(P[(x + X + theta_L1[0]) % X], theta_L1[1]) ^ RORxoo(P[(x + X + theta_L2[0]) % X], theta_L2[1]);
// finally output = input ^ E
for (x = 0; x < X; x++)
for (y = 0; y < Y; y++)
A[indexXY(x, y)] ^= E[x];
}
/**
* @brief the inverse of theta transformation
*
* @param A tXoodooState&
*/
void XoodooRound::inversetheta(tXoodooState &A) {
unsigned int x, y;
vector<tXoodooLane> P(X, 0);
unsigned int exponent = theta_order - 1;
unsigned int powerTwo = 1;
// compute the parity
for (x = 0; x < X; x++) {
for (y = 0; y < Y; y++)
P[x] ^= A[indexXY(x, y)];
}
vector<tXoodooLane> E(P);
// the loop will run theta_order times
do {
if ((exponent & powerTwo) != 0) {
vector<tXoodooLane> tmp(E);
for (x = 0; x < X; x++)
E[x] = tmp[x] ^ ROLxoo(tmp[(x + X - theta_L1[0] * powerTwo) % X], (theta_L1[1] * powerTwo) % Z) ^ ROLxoo(tmp[(x + X - theta_L2[0] * powerTwo) % X], (theta_L2[1] * powerTwo) % Z);
}
powerTwo <<= 1;
} while (powerTwo <= exponent);
// add the parity
for (x = 0; x < X; x++)
E[x] ^= P[x];
for (x = 0; x < X; x++)
for (y = 0; y < Y; y++)
A[indexXY(x, y)] ^= E[x];
}
/**
* @brief rho west transformation
*
* @param A tXoodooState&
*/
void XoodooRound::rhoW(tXoodooState &A) {
unsigned int x, y;
tXoodooState tempA(A);
y = rho_plane[0];//A1
for (x = 0; x < X; x++)
A[indexXY(x, y)] = ROLxoo(tempA[indexXY(x + X - rhoW_L1[0], y)], rhoW_L1[1]);//(1,0)
y = rho_plane[1];//A2
for (x = 0; x < X; x++)
A[indexXY(x, y)] = ROLxoo(tempA[indexXY(x + X - rhoW_L2[0], y)], rhoW_L2[1]);//(0,11)
}
/**
* @brief the inverse of rho west transformation
*
* @param A tXoodooState&
*/
void XoodooRound::inverserhoW(tXoodooState &A) {
unsigned int x, y;
tXoodooState tempA(A);
y = rho_plane[0];//A1
for (x = 0; x < X; x++)
A[indexXY(x, y)] = RORxoo(tempA[indexXY(x + X + rhoW_L1[0], y)], rhoW_L1[1]);//(1,0)
y = rho_plane[1];//A2
for (x = 0; x < X; x++)
A[indexXY(x, y)] = RORxoo(tempA[indexXY(x + X + rhoW_L2[0], y)], rhoW_L2[1]);//(0,11)
}
/**
* @brief rho east transformation
*
* @param A tXoodooState&
*/
void XoodooRound::rhoE(tXoodooState &A) {
unsigned int x, y;
tXoodooState tempA(A);
y = rho_plane[0];//A1
for (x = 0; x < X; x++)
A[indexXY(x, y)] = ROLxoo(tempA[indexXY(x + X - rhoE_L1[0], y)], rhoE_L1[1]);//(0,1)
y = rho_plane[1];//A2
for (x = 0; x < X; x++)
A[indexXY(x, y)] = ROLxoo(tempA[indexXY(x + X - rhoE_L2[0], y)], rhoE_L2[1]);//(2,8)
}
/**
* @brief the inverse of rho east transformation
*
* @param A tXoodooState&
*/
void XoodooRound::inverserhoE(tXoodooState &A) {
unsigned int x, y;
tXoodooState tempA(A);
y = rho_plane[0];//A1
for (x = 0; x < X; x++)
A[indexXY(x, y)] = RORxoo(tempA[indexXY(x + X + rhoE_L1[0], y)], rhoE_L1[1]);//(0,1)
y = rho_plane[1];//A2
for (x = 0; x < X; x++)
A[indexXY(x, y)] = RORxoo(tempA[indexXY(x + X + rhoE_L2[0], y)], rhoE_L2[1]);//(2,8)
}
/**
* @brief chi transformation
*
* @param A tXoodooState&
*/
void XoodooRound::chi(tXoodooState &A) {
unsigned int x, y;
vector<tXoodooLane> B(Y, 0);
for (x = 0; x < X; x++) {
for (y = 0; y < Y; y++)
B[y] = A[indexXY(x, y)] ^ ((~A[indexXY(x, y + 1)]) & A[indexXY(x, y + 2)]);
for (y = 0; y < Y; y++)
A[indexXY(x, y)] = B[y];
}
}
/**
* @brief lambda transformation, including all linear transformations
*
* @param A tXoodooState
* @return tXoodooState
*/
tXoodooState XoodooRound::lambda(tXoodooState A) {
rhoE(A);
theta(A);
rhoW(A);
return A;
}
/**
* @brief transpose lambda transformation, including all linear transformations
*
* @param A tXoodooState
* @return tXoodooState
*/
tXoodooState XoodooRound::transposelambda(tXoodooState A) {
inverserhoW(A);
transposetheta(A);
inverserhoE(A);
return A;
}
/**
* @brief teh inverse of lambda transformation, including all linear transformations
*
* @param A tXoodooState
* @return tXoodooState
*/
tXoodooState XoodooRound::inverselambda(tXoodooState A) {
inverserhoW(A);
inversetheta(A);
inverserhoE(A);
return A;
}
/**
* @brief Bitmap to tXoodooState
*
* @param A input const Bitmap&
* @param B output tXoodooState&
*/
void XoodooRound::Bit2XooState(const Bitmap &A, tXoodooState &B) {
B.assign(X*Y, 0);
unsigned int x, y, z;
for (y = 0; y < Y; y++) {
// set the lane to 0
for (x = 0; x < X; x++) {
B[X*y + x] = 0;
}
// convert a tXoodooLane(32 bits) at a time
for (z = 0; z < Z; z++) {
for (x = 0; x < X; x++) {
B[X*y + x] |= ((tXoodooLane)(A[Z*X*y + Z * x + z]) << (z)); // 32*(4y+x) + z
}
}
}
}
/**
* @brief tXoodooState to Bitmap
*
* @param A input const tXoodooState&
* @param B output Bitmap&
*/
void XoodooRound::XooState2Bit(const tXoodooState &A, Bitmap &B) {
B.assign(var_num, 0);
unsigned int x, y, z;
// convert a bit at a time
for (y = 0; y < Y; y++) {
for (z = 0; z < Z; z++) {
for (x = 0; x < X; x++) {
B[Z*X*y + Z * x + z] = ((A[X*y + x] >> (z)) & 0x1); // 32*(4y+x) + z
}
}
}
}
/**
* @brief Bitmap to State
*
* @param A input const Bitmap&
* @param B output State&
*/
void XoodooRound::Bit2State(const Bitmap &A, State &B) {
B.clear();
// only store the bits that are active
for (unsigned int i = 0; i < var_num; i++) {
if (A[i] == 1) B.push_back(i);
}
}
/**
* @brief State to Bitmap
*
* @param A input const State&
* @param B output Bitmap&
*/
void XoodooRound::State2Bit(const State &A, Bitmap &B) {
B.assign(var_num, 0);
// active bits set to 1, others are 0(by default)
for (int i = 0; i < A.size(); i++) {
B[A[i]] = 1;
}
}
/**
* @brief Bitmap to StateColumn
*
* @param A input const Bitmap&
* @param B output StateColumn&
*/
void XoodooRound::Bit2StateColumn(const Bitmap &A, StateColumn &B) {
B.assign(X*Z, 0);
// calculate each column
for (int z = 0; z < Z; z++) {
for (int x = 0; x < X; x++) {
unsigned int col = 0;
for (int y = 0; y < Y; y++) {
col += (A[Z*X*y + Z * x + z] << y);
}
B[Z*x + z] = col;
}
}
}
/**
* @brief StateColumn to Bitmap
*
* @param A input const StateColumn&
* @param B output Bitmap&
*/
void XoodooRound::StateColumn2Bit(const StateColumn &A, Bitmap &B) {
B.assign(var_num, 0);
for (int z = 0; z < Z; z++) {
for (int x = 0; x < X; x++) {
unsigned int col = A[Z*x + z];
// convert a column at a time
for (int y = 0; y < Y; y++) {
B[Z*X*y + Z * x + z] = (col >> y) & 0x1;
}
}
}
}
/**
* @brief Bitmap to vector<tXoodooLane>
*
* @param A input const Bitmap&
* @param B output vector<tXoodooLane>&
*/
void XoodooRound::Bit2Plane(const Bitmap &A, vector<tXoodooLane> &B) {//32*x + z
B.assign(X, 0);
unsigned int x, z;
for (x = 0; x < X; x++) {
B[x] = 0;
}
for (z = 0; z < Z; z++) {
for (x = 0; x < X; x++) {
B[x] |= ((tXoodooLane)(A[Z*x + z]) << (z));
}
}
}
/**
* @brief vector<tXoodooLane> to Bitmap
*
* @param A input const vector<tXoodooLane>&
* @param B output Bitmap&
*/
void XoodooRound::Plane2Bit(const vector<tXoodooLane> &A, Bitmap &B) {//32*x + z
unsigned int x, z;
for (z = 0; z < Z; z++) {
for (x = 0; x < X; x++) {
B[Z*x + z] = ((A[x] >> (z)) & 0x1);
}
}
}
/**
* @brief State to tXoodooState
*
* @param A input const State&
* @param B output tXoodooState&
*/
void XoodooRound::State2XooState(const State &A, tXoodooState &B) {
Bitmap BitA;
State2Bit(A, BitA);
Bit2XooState(BitA, B);
}
/**
* @brief tXoodooState to State
*
* @param A input const tXoodooState&
* @param B output State&
*/
void XoodooRound::XooState2State(const tXoodooState &A, State &B) {
Bitmap BitA(var_num, 0);
XooState2Bit(A, BitA);
Bit2State(BitA, B);
}
/**
* @brief State to StateColumn
*
* @param A input const State&
* @param B output StateColumn&
*/
void XoodooRound::State2StateColumn(const State &A, StateColumn &B) {
Bitmap BitA;
State2Bit(A, BitA);
Bit2StateColumn(BitA, B);
}
/**
* @brief StateColumn to State
*
* @param A input const StateColumn&
* @param B output State&
*/
void XoodooRound::StateColumn2State(const StateColumn &A, State &B) {
Bitmap BitA(var_num, 0);
StateColumn2Bit(A, BitA);
Bit2State(BitA, B);
}
/**
* @brief tXoodooState to StateColumn
*
* @param A input const tXoodooState&
* @param B output StateColumn&
*/
void XoodooRound::XooState2StateColumn(const tXoodooState &A, StateColumn &B) {
Bitmap BitA(var_num, 0);
XooState2Bit(A, BitA);
Bit2StateColumn(BitA, B);
}
/**
* @brief StateColumn to tXoodooState
*
* @param A input const StateColumn&
* @param B output tXoodooState&
*/
void XoodooRound::StateColumn2XooState(const StateColumn &A, tXoodooState &B) {
Bitmap BitA;
StateColumn2Bit(A, BitA);
Bit2XooState(BitA, B);
}
/**
* @brief given a tXoodooState, calculate its weight
*
* @param A(const tXoodooState&) input state
* @return int
*/
int XoodooRound::caculateXooStateWeight(const tXoodooState &A) {
int weight = 0;
for (int x = 0; x < X; x++) {
for (int z = 0; z < Z; z++) {
for (int y = 0; y < Y; y++) {
// check each column for active bits
if ((A[indexXY(x, y)] >> (z)) & 0x1) {
// found an active bit, weight++
++weight;
// after weight++, exclude this column(using "break")
break;
}
}
}
}
return weight;
}
/**
* @brief given a StateColumn, calculate its weight
*
* @param A (const StateColumn &) input state in column form, A[i] is a column
* @return int
*/
int XoodooRound::caculateStateColumnWeight(const StateColumn &A) {
int weight = 0;
for (int x = 0; x < X; x++) {
for (int z = 0; z < Z; z++) {
if (A[Z*x + z]) ++weight;
}
}
return weight;
}
/**
* @brief State << (dx, dz)
*
* @param dx_dz const vector<int> &, (dx, dz), shift bits
* @param A const State &, state to shift
* @return State
*/
State XoodooRound::ShiftXZ(const vector<int> &dx_dz, const State &A) {
// first convert State to tXoodooState
tXoodooState stateA(X*Y, 0);
State2XooState(A, stateA);
tXoodooState shiftA(stateA);
for (int x = 0; x < X; x++) {
for (int y = 0; y < Y; y++) {
// shift (dx,dz)
shiftA[indexXY(x, y)] = ROLxoo(stateA[indexXY(x + X - dx_dz[0], y)], dx_dz[1]);
}
}
State res;
// convert tXoodooState to State
XooState2State(shiftA, res);
return res;
}
/**
* @brief check if State A and B are symmetry (shift (x,z))
*
* @param A const State &
* @param B const State &
* @return bool
*/
bool XoodooRound::StateEqualAfterShift(const State &A, const State &B) {//A, B is sorted
if (A.size() != B.size()) return false;
if (A == B) return true;
// shift all possible (x,z)
for (int dx = 0; dx < X; dx++) {
for (int dz = 0; dz < Z; dz++) {
State shiftA = ShiftXZ({ dx,dz }, A);
if (shiftA == B) return true;
}
}
return false;
}
/**
* @brief check if State A < B
* value of A = \sum_{i=0}^{383} pow(2, i)*bits_of_A[i]
* A, B are sorted
*
* @param A const State &
* @param B const State &
* @return bool
*/
bool XoodooRound::isSmaller(const State &A, const State &B) {
// start from the largest bit of A and B
int i = A.size() - 1, j = B.size() - 1;
while (i >= 0 && j >= 0) {
// if largest of A<B
if (A[i] < B[j]) return true;
// if largest of A>B
else if (A[i] > B[j]) return false;
// if largest of A=B, get the second largest...
else {
i--;
j--;
}
}
// if all bits are equal, check if there A has more active bits than B
return i < j;
}
/**
* @brief by shifting (x,z), shift State A to its smallest symmetry State, A is sorted
*
* @param A const State &
* @return {int, int} dx_dz, the shift position corresponding to the smallest symmetry state
*/
vector<int> XoodooRound::genSmallestState(const State &A) {
vector<int> dx_dz = { 0, 0 };
State smallest(A);
// shift all possible (x,z)
for (int dx = 0; dx < X; dx++) {
for (int dz = 0; dz < Z; dz++) {
State shiftA = ShiftXZ({ dx,dz }, A);
// after shift (dx,dz), get the smaller state
if (isSmaller(shiftA, smallest)) {
dx_dz = { dx,dz };
smallest = shiftA;
}
}
}
return dx_dz;
}
/**
* @brief print State A
*
* @param fout ostream&
* @param A const State&
*/
void XoodooRound::display(ostream& fout, const State &A) {
// State column map, 0<=tempA[i]<=7
vector<unsigned int> tempA(X*Z, 0);
// for each active bit in A, add to the correspond column
for (int i = 0; i < A.size(); i++) {
// calculate the y index of the bit
int y;
y = A[i] / (X*Z);
assert(y < Y);
// add to the correspond column
tempA[A[i] - y * X*Z] |= ((0x1) << y);
}
// print each column
for (int x = 0; x < X; x++) {
for (int z = 0; z < Z; z++) {
// replace 0 by .
if (tempA[Z*x + z] == 0) fout << ".";
else fout << tempA[Z*x + z];
}
fout << endl;
}
}
/**
* @brief print tXoodooState A
*
* @param fout ostream&
* @param A const tXoodooState &
*/
void XoodooRound::displayXooState(ostream& fout, const tXoodooState &A) {
State tmp;
XooState2State(A, tmp);
display(fout, tmp);
}
/**
* @brief generate RhoW and inverse_RhoW's input-output bit relation
*
* @param RhoW_index map<unsigned int, unsigned int>&, 384 size map, {input bit, output bit}
* @param inverse_RhoW_index map<unsigned int, unsigned int>&, 384 size map, {input bit, output bit}
*/
void XoodooRound::gen_RhoW_T(map<unsigned int, unsigned int>& RhoW_index, map<unsigned int, unsigned int>& inverse_RhoW_index) {
Bitmap BitA(var_num, 0);
tXoodooState temp(X*Y, 0);
unsigned int i, j;
for (i = 0; i < BitA.size(); i++) {
// only set the input bit to 1
// the other input bits are 0
// then only the corresponding output bit is 1
// the other output bits are 0
BitA[i] = 1;
// convert to tXoodooState
Bit2XooState(BitA, temp);
// RhoW
rhoW(temp);
// convert back to Bitmap
XooState2Bit(temp, BitA);
// find the corresponding output bit, which is 1
for (j = 0; j < BitA.size(); j++) {
if (BitA[j] == 1) {
// log the input-output bit relation
// i is the input
// j is the output
RhoW_index.insert(pair<unsigned int, unsigned int>(i, j));
// inverse is (j, i)
inverse_RhoW_index.insert(pair<unsigned int, unsigned int>(j, i));
}
// set all bits back to 0
BitA[j] = 0;
}
}
return;
}
/**
* @brief generate RhoE and inverse_RhoE's input-output bit relation
*
* @param RhoE_index map<unsigned int, unsigned int>&
* @param inverse_RhoE_index map<unsigned int, unsigned int>&
*/
void XoodooRound::gen_RhoE_T(map<unsigned int, unsigned int>& RhoE_index, map<unsigned int, unsigned int>& inverse_RhoE_index) {
Bitmap BitA(var_num, 0);
tXoodooState temp(X*Y, 0);
unsigned int i, j;
for (i = 0; i < BitA.size(); i++) {
// only set the input bit to 1
// the other input bits are 0
// then only the corresponding output bit is 1
// the other output bits are 0
BitA[i] = 1;
// convert to tXoodooState
Bit2XooState(BitA, temp);
// RhoE
rhoE(temp);
// convert back to Bitmap
XooState2Bit(temp, BitA);
// find the corresponding output bit, which is 1
for (j = 0; j < BitA.size(); j++) {
if (BitA[j] == 1) {
// log the input-output bit relation
// i is the input
// j is the output
RhoE_index.insert(pair<unsigned int, unsigned int>(i, j));
// inverse is (j, i)
inverse_RhoE_index.insert(pair<unsigned int, unsigned int>(j, i));
}
// set all bits back to 0
BitA[j] = 0;
}
}
return;
}
/**
* @brief generate Theta and transpose Thetas input-output bit relation
* an output bit of Theta is related to 7 input bits of Theta
* a[x,y,z] = a[x,y,z] ^ \sum_{y=0}^{2} a[x-1,y,z-5] ^ \sum_{y=0}^{2} a[x-1,y,z-14]
*
* @param relation vector<vector<unsigned int>>&
* @param transpose_relation vector<vector<unsigned int>>&
*/
void XoodooRound::gen_Theta_T(vector<vector<unsigned int>>& relation, vector<vector<unsigned int>>& transpose_relation) {
// 4*32=128 columns
vector<vector<unsigned int>> column;
// each column[(x,z)] contains 3 bit indexes of y, i.e. (x + y * 4)*32 + z
for (unsigned int x = 0; x < X; x++) {
for (unsigned int z = 0; z < Z; z++) {
vector<unsigned int> aColumn = {};
for (int y = 0; y < Y; y++) {
aColumn.push_back((x + y * X)*Z + z);
}
column.push_back(aColumn);
}
}
for (int x = 0; x < X; x++) {
for (int y = 0; y < Y; y++) {
for (int z = 0; z < Z; z++) {
vector<unsigned int> temp, temp_transpose;
// output bit a[x,y,z]
temp.push_back(Z*(x + X * y) + z);
temp_transpose.push_back(Z*(x + X * y) + z);
// input bit a[x,y,z]
temp.push_back(Z*(x + X * y) + z);
temp_transpose.push_back(Z*(x + X * y) + z);
// input bit \sum_{y=0}^{2} a[x-1,y,z-5] ^ \sum_{y=0}^{2} a[x-1,y,z-14]
for (int i = 0; i < Y; i++) {
temp.push_back(column[Z*((x + X - theta_L1[0]) % X) + ((z + Z - theta_L1[1]) % Z)][i]);
temp.push_back(column[Z*((x + X - theta_L2[0]) % X) + ((z + Z - theta_L2[1]) % Z)][i]);
temp_transpose.push_back(column[Z*((x + X + theta_L1[0]) % X) + ((z + Z + theta_L1[1]) % Z)][i]);
temp_transpose.push_back(column[Z*((x + X + theta_L2[0]) % X) + ((z + Z + theta_L2[1]) % Z)][i]);
}
relation.push_back(temp);
transpose_relation.push_back(temp_transpose);
}
}
}
}
/**
* @brief combine str cnf with to_string(AS_var_num)
*
* @param cnf_num string &
*/
void XoodooRound::gen_extend_AS_cnf_num(string &cnf_num) {
cnf_num += to_string(AS_var_num) + " ";
/*if(round_num == 2) {
cnf_num += to_string(AS_node_var_num) + " ";
}*/
}
/**
* @brief ban solution States and its symmetry States
*
* @param Solver SATSolver &
* @param A const map<State, int>&, A.first is the State to ban, A.second is the var offset
*/
void XoodooRound::ban_solution(SATSolver &Solver, const map<State, int> &A) {
// check if the State is 0
bool zero = true;
for (auto iter = A.begin(); iter != A.end();iter++) {
zero = zero && (iter->first.size() == 0);
}
// shift all possible (dx, dz)
for (int dx = 0; dx < X; dx++) {
for (int dz = 0; dz < Z; dz++) {
vector<Lit> ban_solutions;
for (auto iter = A.begin(); iter != A.end();iter++) {
// ban all shifted symmetric States
State shiftA = ShiftXZ({ dx,dz }, iter->first);
Bitmap BitA(var_num, 0);
State2Bit(shiftA, BitA);
for (uint32_t var = 0; var < BitA.size(); var++) {
ban_solutions.push_back(Lit(var + iter->second, (BitA[var] == 1) ? true : false));
}
}
Solver.add_clause(ban_solutions);
ban_solutions.clear();
// if the State is 0, no need to shift since 0 << (x,z) = 0 for all (x,z)
if (zero) return;
}
}
}
/**
* @brief write a solution to a file in a readable form
*
* @param pathname const string
* @param weight const int
* @param solution_count const int
* @param solution const States&, a solution, containing r States, r is the round number
*/
void XoodooRound::write_result(const string pathname, const int weight, const int solution_count, const States &solution) {
ofstream out(pathname, ios::out | ios::app);
// write weight
out << "weight " << weight << " solution " << solution_count << endl;
// write ai
for (int i = 0; i < solution.size() - 1; i++) {
out << "a" << i + 1 << ":" << endl;
display(out, solution[i]);
out << endl;
}
// write b_{r-1}
out << "b" << solution.size() - 1 << ":" << endl;
display(out, solution[solution.size() - 1]);
out << "\n" << endl;
out.close();
}
/**
* @brief calculate the weight of the Solver's solution
*
* @param Solver SATSolver &
* @param rounds int
* @param core_var_num int
* @return int
*/
int XoodooRound::get_weight(SATSolver &Solver, int rounds, int core_var_num) {
// get each State weight(a1,a2,...)
vector<int> as_weight(rounds, 0);
for (int i = 0; i < AS_node_var_num; i++) {
for (int j = 0; j < rounds; j++) {
if (Solver.get_model()[i + core_var_num + AS_node_var_num * j] == l_True) {
as_weight[j]++;
}
}
}
// get total weight
int weight = 0;
/*if(round_num == 2) weight = as_weight[0]*2 + as_weight[1];
else {
for(int j=0; j<rounds; j++) weight += as_weight[j];
}*/
for (int j = 0; j < rounds; j++) weight += as_weight[j];
return weight;
}
/**
* @brief solve all solutions by iteratively banning solved solutions and write solutions to a file
*
* @param Solver SATSolver &
* @param pathname const string
* @param rounds int
* @param base_offset int
* @param core_var_num int
* @param assumption const vector<Lit>&, you can solve with an assumption
*/
void XoodooRound::solve_and_output(SATSolver &Solver, const string pathname, int rounds, int base_offset, int core_var_num, const vector<Lit> &assumption) {
int counting = 0;
map<int, int> solution_counts;// map<weight, solution_counts>
while (true) {
lbool ret;
// solve the cnf
if (assumption.size() == 0) {
ret = Solver.solve();
}
else {
cout << "got a assumption" << endl;
ret = Solver.solve(&assumption);
}
// print date and time
time_point<system_clock> start = system_clock::now();
auto st = system_clock::to_time_t(start);
struct tm* stm = localtime(&st);
cout << '\n' << stm->tm_mon + 1 << " " << stm->tm_mday << " " << stm->tm_hour << ":" << stm->tm_min << ":" << stm->tm_sec << endl;
// if unsat, then all solutions found
if (ret != l_True) {
assert(ret == l_False);
cout << "reach end" << endl;
cout << "counting: " << counting << endl;
exit(0);
}
// start processing solution
// get weight
int weight = get_weight(Solver, rounds, core_var_num);
// get result in States format