package simulator;

public class Colossus {
    // Assumes that the patterns for CHI1 and CHI2 are correct.
    public static void runDoubleDelta12(Tunny t, byte[] cipher, int len) {
        int bestCounter = 0;
        byte[] dechi = new byte[len];
        for (int p1 = 0; p1 < Tunny.CHI_PERIOD[Tunny.CHI1]; p1++) {
            t.startPositions.chi[Tunny.CHI1] = p1;
            for (int p2 = 0; p2 < Tunny.CHI_PERIOD[Tunny.CHI2]; p2++) {
                t.startPositions.chi[Tunny.CHI2] = p2;
                t.dechi(dechi, len, cipher);
                int counterDelta12Dot = 0;
                for (int i = 0; i < len - 1; i++) {
                    int deltaDechi = dechi[i] ^ dechi[i + 1];
                    int deltaChiBit1 = deltaDechi & 0x1;
                    int deltaChiBit2 = (deltaDechi >> 1) & 0x1;
                    counterDelta12Dot += ((deltaChiBit1 ^ deltaChiBit2) == 0x0) ? 1 : 0;
                }
                if (counterDelta12Dot > bestCounter) {
                    bestCounter = counterDelta12Dot;
                    System.out.printf("Best counter value: %,6d CHI1 position: %2d CHI2 position: %2d\n", bestCounter, p1, p2);
                }
            }
        }
    }
}
