2 import java
.math
.BigInteger
;
5 * Generates stress-tests for garbling
7 * @author Roman Elizarov
9 public class gen_stress_garbling
{
10 private boolean FIND_LONGEST_LOOP
= false;
12 static class Field
implements Cloneable
{
22 for (int i
= 0; i
< r
; i
++) {
23 for (int j
= 0; j
< c
; j
++) {
29 void garbleLeft(int i
, int j
) {
31 a
[i
][j
] = a
[i
][j
+ 1];
32 a
[i
][j
+ 1] = a
[i
+ 1][j
+ 1];
33 a
[i
+ 1][j
+ 1] = a
[i
+ 1][j
];
37 void garbleRight(int i
, int j
) {
39 a
[i
][j
] = a
[i
+ 1][j
];
40 a
[i
+ 1][j
] = a
[i
+ 1][j
+ 1];
41 a
[i
+ 1][j
+ 1] = a
[i
][j
+ 1];
46 Random rnd
= new Random();
47 long besttime
= System
.currentTimeMillis();
48 BigInteger besttotal
= BigInteger
.ZERO
;
50 public static void main(String
[] args
) {
51 new gen_stress_garbling().go();
56 generateAndAnalyzeOne();
60 private void generateAndAnalyzeOne() {
61 int r
= 290 + rnd
.nextInt(11);
62 int c
= 290 + rnd
.nextInt(11);
63 int pl
= rnd
.nextInt(101);
64 int pr
= rnd
.nextInt(101 - pl
);
65 int seed
= rnd
.nextInt();
66 char[][] map
= genRandom(r
, c
, pl
, pr
, seed
);
67 Field f
= new Field(r
, c
);
68 garbleFieldOnce(f
, map
);
69 BigInteger total
= findLoopLength(f
);
70 if (total
.compareTo(besttotal
) > 0) {
71 long time
= System
.currentTimeMillis();
73 System
.out
.printf("-- Found %d with r=%d, c=%d, pl=%d, pr=%d, seed=%d in %d ms%n",
74 total
, r
, c
, pl
, pr
, seed
, time
- besttime
);
80 private BigInteger
findLoopLength(Field f
) {
81 // find transposition of one garbling
85 int[] transpose
= new int[k
];
86 for (int i
= 0; i
< r
; i
++) {
87 for (int j
= 0; j
< c
; j
++) {
88 transpose
[f
.a
[i
][j
]] = i
* c
+ j
;
91 // split transposition into loops
92 int[] lnum
= new int[k
];
93 int[] llen
= new int[k
];
95 Arrays
.fill(lnum
, -1);
96 for (int p
= 0; p
< k
; p
++) {
103 } while (lnum
[p
] < 0);
108 BigInteger total
= BigInteger
.ONE
;
109 for (int q
= 0; q
< lc
; q
++) {
110 if (FIND_LONGEST_LOOP
)
111 total
= llen
[q
] > total
.longValue() ? BigInteger
.valueOf(llen
[q
]) : total
;
113 total
= nok(total
, BigInteger
.valueOf(llen
[q
]));
118 private BigInteger
nok(BigInteger a
, BigInteger b
) {
119 return a
.divide(a
.gcd(b
)).multiply(b
);
122 private static char[][] genRandom(int r
, int c
, int pl
, int pr
, int seed
) {
123 char[][] map
= new char[r
- 1][c
- 1];
124 Random rnd
= new Random(seed
);
125 for (int i
= 0; i
< r
- 1; i
++) {
126 for (int j
= 0; j
< c
- 1; j
++) {
127 int p
= rnd
.nextInt(100);
130 else if (p
< pl
+ pr
)
139 private void garbleFieldOnce(Field f
, char[][] map
) {
140 for (int i
= 0; i
< f
.r
- 1; i
++) {
141 for (int j
= 0; j
< f
.c
- 1; j
++) {
143 case 'L': f
.garbleLeft(i
, j
); break;
144 case 'R': f
.garbleRight(i
, j
); break;
150 private void printMap(char[][] map
) {
151 for (char[] row
: map
) {
152 System
.out
.println(new String(row
));