Berlekamp algorithm