// generateBarriers creates the encoding barriers between rounds that compute ShiftRows and re-encodes data. func generateBarriers(rs *common.RandomSource, out *Construction, inputMask, outputMask, sr *matrix.Matrix) { // Generate the ShiftRows and re-encoding matrices. out.ShiftRows[0] = MaskSwap(rs, 16, 0).Compose(*sr).Compose(*inputMask) for round := 1; round < 10; round++ { out.ShiftRows[round] = MaskSwap(rs, 16, round).Compose(*sr).Compose(MaskSwap(rs, 32, round-1)) } // We need to apply a final matrix transformation to convert the double-level encoding to a block-level one. out.FinalMask = outputMask.Compose(MaskSwap(rs, 32, 9)) }
// FindAtilde calculates a non-trivial matrix Atilde s.t. L <- Atilde = Atilde <- D(beta), where // L = A_i <- D(beta) <- A_i^(-1) func FindAtilde(constr *chow.Construction, L matrix.Matrix) matrix.Matrix { beta := CharToBeta[FindCharacteristic(L)] D, _ := DecomposeAffineEncoding(encoding.ByteMultiplication(beta)) x := L.RightStretch().Add(D.LeftStretch()).NullSpace() m := matrix.Matrix(make([]matrix.Row, len(x))) for i, e := range x { m[i] = matrix.Row{e} } return m }
func (constr *Construction) SubByte(e byte) byte { // AES S-Box m := matrix.Matrix{ // Linear component. matrix.Row{0xF1}, // 0b11110001 matrix.Row{0xE3}, // 0b11100011 matrix.Row{0xC7}, // 0b11000111 matrix.Row{0x8F}, // 0b10001111 matrix.Row{0x1F}, // 0b00011111 matrix.Row{0x3E}, // 0b00111110 matrix.Row{0x7C}, // 0b01111100 matrix.Row{0xF8}, // 0b11111000 } a := byte(0x63) // 0b01100011 - Affine component. return m.Mul(matrix.Row{byte(number.ByteFieldElem(e).Invert())})[0] ^ a }
func (constr *Construction) UnSubByte(e byte) byte { // AES Inverse S-Box m := matrix.Matrix{ matrix.Row{0xA4}, matrix.Row{0x49}, matrix.Row{0x92}, matrix.Row{0x25}, matrix.Row{0x4a}, matrix.Row{0x94}, matrix.Row{0x29}, matrix.Row{0x52}, } a := byte(0x63) invVal := m.Mul(matrix.Row{e ^ a})[0] return byte(number.ByteFieldElem(invVal).Invert()) }
func matrixMul(m *matrix.Matrix, dst, src []byte) { res := m.Mul(matrix.Row(src[:])) copy(dst, res) }