//value for p and t, need p cong 3 mod 4
iv:=func< v, block,p | 
 CharacteristicVector(VectorSpace(GF(p),v), block) > ;
 Dim:=Dimension;
IV:=iv;
MW:=MinimumWeight; 
Z:=Integers();
//setting this for p:=7,t:=1;pp:=2; delete that line for other values

p:=7;t:=1;pp:=2;
q:=p^(2*t);
q1:=p^t;
q1,"=sqr root of q";
q1 mod 8,"=", q1, " mod 8";
f<w>:=GF(q);
u:=w^(p^t-1);
uu:=w^(q1+1);
FF:=[x:x in f];


s:=(q-1)/4;
C0:={w^(4*k): k in [0..s-1]};
pC0:={Position(FF,x):x in C0};
C1:={w*x:x in C0};
S:=C0 join C1;
#S,#C0,#C1;


Edg:={};
blox:=[];rblox:=[];
for j:=1 to q do
 ed:={};bl:={};rbl:={};
 bl:={Position(FF,FF[j]+y):y in S};
 rbl:=bl join {j};
 ed:={{j,i}:i in bl};
 blox:=Append(blox,bl);
 rblox:=Append(rblox,rbl);
 Edg:=Edg join ed;
end for;

gr:=Graph<q|Edg>;cgr:=Complement(gr);
des:=Design<1,q|blox>;
aud:=AutomorphismGroup(des);
ap:=pC0^aud;
//#pC0,{#(x meet y):x,y in ap};

"order aut gp des",Factorisation(Order(aud));
rdes:=Design<1,q|rblox>;
//"valence gr", Valence(gr);
"parameters (",q,(q-1)/2,(q-5)/4,(q-1)/4,")";
A:=AdjacencyMatrix(gr);
//eigs:=Eigenvalues(A);
//"eigenvalues",eigs;
C:=LinearCode(des,GF(pp));
RC:=LinearCode(rdes,GF(pp));
RC eq Dual(C), "that C is RLCD";
"dim C=",Dim(C); "dim RC=", Dim(RC);

"C is a [",q,Dim(C),MW(C),"]_2 code";

iblox:=[IV(q,blox[i],pp):i in [1..#blox]];
riblox:=[IV(q,rblox[i],pp):i in [1..#rblox]];

S:={1..q};
kk:=Z!(MW(C)/2 -1);
k:=Min(kk,4);k;

//this is setting no. of errors corrected max this value of k\le 4


 Sun:=[];Suv:=[];
 for m:=1 to k do
 ssm:=Setseq(Subsets(S,m));
 vsm:=[&+[riblox[j]:j in ssm[i]]: i in [1..#ssm]];
 Sun:=Append(Sun,ssm);
 Suv:=Append(Suv,vsm);
 end for;
 
m:=Random({1..k});m,"errors";

c:=Random(C);
r:=Random(Subsets(S,m));
vr:=IV(q,r,pp);
w:=c+vr;
ve:=&+[riblox[x]:x in Support(w)];
for i:=1 to k do
 a:=Position(Suv[i],ve);
  if a ne 0 then
  i,a,Sun[i][a];
  jj:=Sun[i][a];
  break i;
  end if;
 end for;

c eq (w+IV(q,jj,pp)), "that w decoded correctly";
