In this paper, we consider linear codes which are linear complementary dual (LCD), a property introduced by Massey in 1992 . An LCD code is a linear code that has only the zero word in common with its dual. Recall that a linear code is an -subspace of , where is a finite field. Given such a code , its dual is . Hence, if is LCD, then and ; of course, if is not finite and instead has characteristic , then this naturally holds.
In 2015, Carlet and Guilley  demonstrated how LCD codes provide countermeasures to side-channel and fault-injection attacks. They use the observation (made by Massey) that a generator matrix of an LCD code has the property that is nonsingular; certainly, the same holds for a parity-check matrix of an LCD code , meaning is nonsingular. Suppose that data is masked as where . Since , there exists with
According to Carlet and Guilley, the countermeasure is degree secure where is the minimum distance of , and the greater the degree of the countermeasure, the harder it is to pass a successful SCA. To consider a fault injection attack, suppose is modified into where . Then for some . Detection amounts to distinguishing from . We have that
Notice that if and only if if and only if . Thus, fault not detected if . If , where is the minimum distance of , then fault is detected. Both of these applications motivate the need for LCD codes with large minimum distance.
Recently, it was shown that every linear code over with is equivalent to an LCD code, as demonstrated by Carlet, Mesnager, Tang, Qi, and Pellikaan . However, explicit constructions of LCD codes remain elusive. There have been results on the characterizations of LCD codes from particular families. Among them, there are some results for algebraic geometry codes, a particular family of evaluation code , cyclic codes , quasi-cyclic codes , and generalized Reed-Solomon codes .
In this paper, we consider LCD codes which are a special type of evaluation code, called a generalized affine Cartesian code. Generalized Reed-Solomon codes are a special case. Our results on generalized Reed-Solomon codes differ from those in 
in that we provide a characterization of which generalized Reed-Solomon codes are LCD and give explicit constructions, as opposed to determining the existence of such codes with a particular set of parameters; our results apply to codes over fields of any characteristic (as opposed to odd prime powers) as well.
A generalized affine Cartesian code is defined as follows. Let be a finite field with elements, and let be non-empty subsets of Set . Define the Cartesian product set
Let be a polynomial ring, let be the points of , and let be the
-vector space of all polynomials ofof degree less than where is a non-negative integer. Fix , and set . The evaluation map
defines a linear map of -vector spaces. The image of , denoted by , defines a linear code, called the generalized affine Cartesian evaluation code (Cartesian code for short) of degree associated to and The dimension and the length of are given by (dimension as -vector space) and respectively. The minimum distance of is given by
where is the number of non-zero entries of . Generalized affine Cartesian codes arise naturally as duals of affine Cartesian codes; this is seen in the computation by Beelen and Datta , though the codes are not mentioned by name. In this paper, we investigate them more fully.
Cartesian codes are special types of affine Reed-Muller codes in the sense of [26, p. 37] and a type of affine variety codes, which were defined in . Cartesian codes are a generalization of -ary Reed-Muller codes, which are Cartesian codes with
Cartesian codes have been studied in different works when the vector of ones: they appeared first time in  and then independently in . In , the authors study the basic parameters of Cartesian codes, they determine optimal weights for the case when is the product of two sets, and then present two list decoding algorithms. In  the authors study the vanishing ideal and then, using commutative algebra tools, for instance regularity, degree, Hilbert function, the authors determine the basic parameters of Cartesian codes in terms of the sizes of the components of the Cartesian set. In , the author shows some results on higher Hamming weights of Cartesian codes and gives a different proof for the minimum distance using the concepts of Gröbner basis and footprint of an ideal. In  the authors find several values for the second least weight of codewords, also known as the next-to-minimal Hamming weight. In  the authors find the generalized Hamming weights and the dual of Cartesian codes.
This paper is organized as follows. Section 2 details properties of generalized affine Cartesian codes, hereafter referred to as Cartesian codes. In Section 3, we provide a characterization of Cartesian codes which are LCD. Examples are found in Section 4, and our results are summarized in Section 5.
For all unexplained terminology and additional information, we refer to [11, 13, 27] for commutative algebra and the theory of Hilbert functions, [21, 26] for the theory of linear codes, and [8, 9, 12, 17, 19, 24, 25] for other families of evaluation codes, including several variations of Reed-Muller codes and projective versions of the Cartesian codes.
2 Properties of generalized affine Cartesian codes
In what follows, , the cardinality of for . An important characteristic for Cartesian codes and evaluation codes in general is the fact that we can use commutative algebra methods to study them. The reason is because the kernel of the evaluation map is precisely , where is the vanishing ideal of consisting of all polynomials of that vanish on . Thus, the algebra of is related to the basic parameters of . Observe the kernel of the evaluation map is independent of because every entry of is non-zero. In fact, the vanishing ideal of is given by [20, Lemma 2.3].
A monomial matrix is a square matrix with exactly one nonzero entry in each row and column. A monomial matrix can be written either in the form or the form , where is a diagonal matrix with nonzero entries at the diagonal and is a permutation matrix. Let and be codes of the same length over the field , and let be a generator matrix for Then and are monomially equivalent provided there is a monomial matrix over the same field so that is a generator matrix of . Monomially equivalent codes have the same length, dimension and minimum distance. For more properties of monomially equivalent codes, see  and references there.
Using properties of monomially equivalent codes along with [20, Theorems 3.1 and 3.8], we have the following result.
Let be a Cartesian code.
The length of is
The dimension of is (i.e. is surjective) if , and
The minimum distance of is 1 if , and for we have
where and are uniquely defined by with (if we understand that , and if then we set and ).
In light of Theorem 2.1, from now on we assume that .
The rest of this section is devoted to prove that the dual of the Cartesian code is where and is as described below. The dual of the Cartesian code the case when is the vector of ones, was previously found in .
Given a positive integer we define Let be the graded-lexicographic order on the set of monomials of This order is defined in the following way: if and only if or and the leftmost nonzero entry in is positive. From now on we fix the order . Denote the variables by For each define the polynomial
Notice that for evaluation purposes we can assume that for meaning
Indeed, if , then there exists such that By the division algorithm in [1, Theorem 1.5.9], there are in such that
where for and Then and Given this, moving forward, we make the assumption that .
Next we point out that the map is injective when for It is easy to see that for all implies However, and for forces for . As a result, meaning .
For each element of , define the polynomial
For , it is straightforward to check that if and only if In addition, where denotes the formal derivative of Writing in terms of the derivatives is convenient at times for computational purposes.
Given there exists a polynomial in such that and for Define the polynomial
Then and By definition of we have for Based on the injectivity of when we restrict to polynomials with , is the unique polynomial in such that and for
Using these ideas we are almost ready to find the dual of a Cartesian code. Just one more result.
Let . Then
Observe that the dimension of given in Theorem 2.1 (2) is the number of integer solutions of the following inequality
The number of integer solutions of the inequality
is the same that the number of integer solutions of the inequality
We come to the main result of this section.
Let be points of the Cartesian set The dual of is
where and is given by
Let be an element of such that for and let be an element of such that for By the division algorithm in [1, Theorem 1.5.9], there are in such that
where for and
Observe that for all Then
The coefficient of the monomial of degree on the right-hand side of (2.6) is given by:
By (2.5) so the coefficient of the monomial of degree on the left-side of (2.6) is Thus the dot product shown on (2.8) is As the left-hand side of the dot product given in (2.6) is an arbitrary element of , and right-hand side of the dot product of Equation (2.6) is an arbitrary element of , the proof is complete because where the last equality follows from Lemma 2.2. ∎
3 Finding LCD codes from Cartesian codes
In this section, we determine which Cartesian codes , where , are LCD. As a result, a number of explicit constructions for LCD codes are found.
3.1 Generalized Reed-Solomon codes (i.e., the case )
We start with the case when , meaning , so Observe that in this case the Cartesian code is the generalized Reed-Solomon code which is given by
Recall and for each element By (2.3),
We are interested in finding conditions on and such that is LCD. Observe that the Cartesian code is not LCD if and only if there are nonzero polynomials and such that , and
This holds if and only if
Define the polynomial associated to by
Notice that for all and . Moreover, and are coprime in . To see this, observe that if and only if whereas if . Since factors into linear terms over , and have no nonconstants common factors. Then we have the following result.
The Cartesian code is LCD if and only if for all nonzero polynomials with and we have
where is defined in (3.2).
Equation (3.1) holds if and only if divides . ∎
As and are coprime, by the Extended Euclidean Algorithm [28, Chapter 3], there exists a natural number , polynomials for and polynomials for such that
The following is the basis of our main results of this section.
Let be a Cartesian code and be the remainders from the Extended Euclidean Algorithm applied to polynomials and . Then, is LCD if and only if for all ,
We prove both implications via the contrapositives.
() Assume is not LCD. By Proposition 3.1, there are polynomials and in such that and
Let be the remainder such that and Observe which means now we just need to prove
Because and are coprime, Moreover it holds that
which implies Then by (3.4)
which completes the proof.
The following theorem is the main result of this section and a corollary of Proposition 3.2.
Let be a Cartesian code and be the remainders from the Extended Euclidean Algorithm applied to polynomials and . Then, is LCD if and only if
3.2 Affine Cartesian codes on components
Let be the points of the Cartesian set Now we are ready to determine whether a Cartesian code is LCD. By (2.3), the dual of is given by
where and is defined by Thus the Cartesian code is not LCD if and only if there are nonzero polynomials and such that
Equation (3.6) holds if and only if
The polynomial associated to is defined by
for all and
for all .
Moreover, if is an element of that satisfies 1. and 2., then We have the following characterization for LCD codes.
The Cartesian code is LCD if and only if for all nonzero polynomials with and we have
where is the polynomial associated to defined in (3.8)
Equation (3.7) holds if and only if ∎
Next, we focus on a special family of Cartesian codes.
For , write and let . The Cartesian vector is defined as a vector of length with