# Speed up performance of nested function computations

I'm trying to create a model for intensive computations for a homomorphic encryption scheme. The core of my problems is a series of function calls

h00=Function[PadLeft[IntegerDigits[Mod[(#1[]-#1[][])*(#2[]-#2[][]),q],2],wrdLen+1]];

hi0=Function[Table[PadLeft[IntegerDigits[Mod[#1[]*(#2[][]-#2[])-#1[]*#2[],q],2][[u]],wrdLen+1],{u,2,n+1}]];

h0j=Function[Table[PadLeft[IntegerDigits[Mod[#1[][]*#2[],q],2][[i]],wrdLen+1],{i,2,n+1}]];

hij=Function[Table[PadLeft[IntegerDigits[Table[Mod[#1[][[i]]*#2[][[j]],q],{i,2,n+1},{j,2,n+1}],2][[u]][[v]],wrdLen+1],{u,1,n},{v,1,n}]];
hijτ=Function[Join[{Catenate[{{h00[#1,#2]},h0j[#1,#2]}]},Transpose[Catenate[{{hi0[#1,#2]},Transpose[hij[#1,#2]]}]]]];

SHMult=Function[Catenate[{{Mod[Sum[hijτ[#1,#2][[i]][[j]][[wrdLen+2-τ]]*a[][[i]][[j]][[τ]],{i,1,n+1},{j,1,n+1},{τ,1,wrdLen+1}],q]},{Mod[Sum[hijτ[#1,#2][[i]][[j]][[wrdLen+2-τ]]*k[][[i]][[j]][[τ]],{i,1,n+1},{j,1,n+1},{τ,1,wrdLen+1}],q]}}]];


Function SHMult calls all previous functions and depending on the number of simulations this might be repeated a lot of times. Basically, what it does is that it creates a big square matrix hijτ where h00 produces the element {0, 0}, hi0 produces all other elements of the first column, h0j produces all other elements of the first row, and hij produces a submatrix with all other elements of the matrix.

Not also that the indices for i, j, τ might be also large; for example a 256 x 256 matrix of 32-bit values. As a first step I tried to use Compile instead of Function for all these functions with the parallelization option given, but I got errors. Also I tried ParallelTable instead of Table, but I got an error saying that ParallelTable can't be used in a nested environment.

Is there any way to optimize this somehow?

• What is wrdLen? How did you try to call compile? – Feyre Mar 24 '17 at 11:20
• It is a constant defined before calling the functions. I just didn't define it to simplify the question. For compile I set Compile[{x,y}...,Option] and replaced the #1, #2 in the function definitions with x and y. – Jimakos Mar 24 '17 at 14:31