// FindPermutation is called on the first affine layer. It returns the permutation matrix corresponding to the row // permutation that has occured to the first layer. func (al *affineLayer) findPermutation() matrix.Matrix { permed := (*al).BlockLinear.Forwards unpermed := matrix.Matrix{} for i := 0; i < 8; i++ { for pos := 0; pos < 16; pos++ { if h := permed[8*pos].Height(); 16*i <= h && h < 16*(i+1) { unpermed = append(unpermed, permed[8*pos:8*(pos+1)]...) } } } unpermed, _ = unpermed.Invert() perm := permed.Compose(unpermed) return perm }
// findMatrix finds an invertible matrix in a basis. func findMatrix(basis []matrix.Row) matrix.Matrix { im := matrix.NewIncrementalMatrix(64) for _, row := range basis { im.Add(row) } size := im.Size() for i := 0; i < size; i++ { row := im.Row(i) cand := matrix.Matrix{} for _, v := range row { cand = append(cand, matrix.Row{v}) } if _, ok := cand.Invert(); ok { return cand } } panic("Couldn't find an invertible matrix in the given basis!") }
matrix.Row{0x00, 0x8f, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x8f, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x00, 0x68, 0x1f, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x21, 0x3e, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x42, 0x7c, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x84, 0xf8, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0xf1, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x09, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0xe3, 0x00, 0x00, 0x00, 0x00, 0xe3, 0x00, 0x00, 0x00, 0x00, 0x09, 0xea, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0xc7, 0x00, 0x00, 0x00, 0x00, 0xc7, 0x00, 0x00, 0x00, 0x00, 0xe3, 0x24, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x8f, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xb0, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x77, 0x68, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x21, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x42, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x84, 0x00, 0x00, 0x00}, } var unRound, _ = round.Invert() var lastRound = matrix.Matrix{ matrix.Row{0xf1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0xe3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0xc7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x8f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x00, 0x00, 0x00, 0x00, 0xc7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, matrix.Row{0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},