"""Verify the certificate for Mandal and Mohan's Conjecture 1.28 using 2-by-2 matrices."""

T = 43_751_231_619_735_900_774_341
MODULUS = 4 * T


def multiply(a, b, m):
    return tuple(tuple(sum(a[i][k] * b[k][j] for k in range(2)) % m
                       for j in range(2)) for i in range(2))


def power(n, m):
    a, result = ((1, 1), (1, 0)), ((1, 0), (0, 1))
    while n:
        if n & 1:
            result = multiply(result, a, m)
        a = multiply(a, a, m)
        n //= 2
    return result


if __name__ == "__main__":
    matrix = power(T, MODULUS)
    assert matrix == ((139163915933195459769968, MODULUS - 3),
                      (MODULUS - 3, 139163915933195459769971))
    assert T % 2 == 1 and power(T, 24)[0][1] == 5
    smaller = 15_655_599_837_762_881
    assert power(smaller, 4 * smaller)[0][1] == 4 * smaller - 3
    assert power(smaller, 24)[0][1] == 13
    three_factors = 38_713 * 82_613 * 3_665_399
    assert three_factors == 11_722_668_338_515_531
    assert power(three_factors, 4 * three_factors) == (
        (10246444951041737, 46890673354062121),
        (46890673354062121, 10246444951041740),
    )
    assert 4 * three_factors - 3 == 46890673354062121
    assert power(three_factors, 24)[0][1] == 5
    print("t, 4t, F_t modulo 4t")
    for t in (three_factors, smaller, T):
        modulus = 4 * t
        residue = power(t, modulus)[0][1]
        assert t >= 5 and t % 2 == 1
        assert residue == modulus - 3
        print(t, modulus, residue, sep=", ")
