I think a[i] in both, LHS and RHS of constraint might cause an issue.
There can be multiple ways to address this. One way which strikes me first (might not be the most optimized one), is using a temporary array and substituting squared values in the main array.
class A;
rand byte unsigned a[];
rand byte unsigned temp[]; // temporary array
rand int unsigned a_size; // determines size of array
constraint c1{ foreach(a*)
{
a[i]==temp[i]*temp[i];
// temp[i] inside {[1:5]}; // use some range for which squares are required
unique {temp}; // use unique, for each unique entry in array
}
a.size == a_size;
temp.size == a_size; // size of temp must be same as main array size
a_size inside {[1:5]};
solve temp, a_size before a;
}
endclass
Or you can generate any random values in the main array and use [i]post-randomize* function to square each entry. This method won’t require any temporary array substitution:
class A;
rand byte unsigned a[];
rand int unsigned a_size;
constraint c1{ foreach(a[i])
{
a[i] inside {[1:5]}; // generate any values
}
a.size == a_size;
a_size inside {[1:5]};
solve a_size before a;
}
function void post_randomize();
foreach(a[i])
a[i] = a[i]*a[i]; // squaring here
endfunction