Algorithm 2 -> Simon library  converted.