# General NxN matrix multiplication

Hello Zama Community!

I am wondering if it is possible to do general NxN matrix mutltiplication in either Concrete-Numpy or Concrete-ML .

I have seen the documentation of concrete-numpy, and it was clear from the documentation that concrete-numpy that it only apparently supports matrix multiplication of 2x2 matrices and atmost one has to be encrypted. On the other hand, it seems from Quantization of Neural Networks for Fully Homomorphic Encryption that Concrete-ML might support some large matrix multiplication for at least integer based matrices.

To be very clear, I need very elementary matrix multiplication of say about matrices less than 50x50 in dimension.

Concrete is an excellent tool and attending Benoit’s talk at PPML '22 in Copenhagen last week was a very educating experience.

Looking forward to hearing from the experts,

danish

2 Likes

Hello @dalvi

I am going to answer for CN team since they are in vacation.

Yes, M * N matrix multiplication is supported in Concrete-Numpy (and linear layers are supported in Concrete-ML). Just use np.matmul. When we say “Only 2D matrix multiplication is supported for now”, 2D means “two dimensions”, ie M * N, by opposition to larger cases like M * N * N’ (numpy.matmul — NumPy v1.23 Manual).

(And thanks for the words about the PPML presentation, quotes like that make us very proud)

Cheers

Dear Benoit,

Thank you for your response. Indeed, I was confused by the equivocation of the word dimension.

Also one more question in this context, I wish to square an encrypted matrix but since we necessarily compelled to have one matrix in the product as unencrypted, I cannot see how may I do that.

danish

So, if you really mean squaring M, ie compute M * M, it is not possible currently, and I guess it will not be possible for some time, since it implies multiplying cipher texts together, which is not available in below Concrete library at the moment. On the opposite, if you mean applying the square function on all the elements of the matrix, it is doable with a PBS (but I imagine you’re speaking about M * M).

Could I ask you what you are trying to achieve?

Or better, if nothing sentive, you could share with us the numpy function that you want to turn into FHE and we’ll try to help you to

Dear Benoit,

Thank you for your response.

I am indeed referring to M * M.

It will be sufficient to know that for my application, I do need to be able to compute powers of a matrix, and any arbitrary although they will be always less than N-1, N is are the dimensions of the square matrix N x N.

I am myself thinking about some mathematical tricks by which I can compute A^2 , but have not reached anywhere as of yet.

danish

Hey @dalvi,

In the next release this summer, you will be able to do `M ** 2`.

For the time being, you can manually create a `LookupTable` like this one `table = hnp.LookupTable([i ** 2 for i in range(max_expected_value_of_m)])` and use it like so `table[M]`.

Let me know if you need anything else!
Umut

I might have misunderstood.

If you were asking the following question:

``````I have an encrypted tensor `x` of shape (M, M) and an encrypted tensor `y` of shape (M, M), can I do element-wise multiplication of `x` and `y` (i.e., `x * y` in numpy)?
``````

Then, the answer is you cannot for the time being.
Umut

Or if you meant `x @ y` (i.e., `np.matmul(x, y)`) that’s not supported for the time being as well.

Dear Umut,

Thank you for your exhaustive response, and apologies for my belated reaction. It is indeed true that I meant `np.matmul(x, y))`. I am also not sure what you mean by `M **2`. Is this suppose to mean squaring the entries in the matrix in place?

It therefore seems that that matrix multiplication , which is the linear algebraic matrix multiplication, in a homomorphic fashion, is not very much supported in Zama. It is understandable as perhaps concrete-numpy is in its initial stages and has already made a large way to incorporate functions which might be rather more useful for ML algorithms, though many ML algorithms also require matrix mutliplication.

Thank you for your time and response, I will seek your help if there is anything else that I would need.

Dear @dalvi,

Yes, it was for squaring the entries in the matrix, I thought that’s what you wanted to do when I saw `M * M`.

The main blocker for encrypted matrix multiplication is the multiplication of two encrypted scalars. We’re working on bringing support for it but I cannot provide an estimate on when it’ll be available.

You’re welcome, don’t hesitate to ask anything 