# How to reduce Multiplication Depth in CKKS

**URL:** <https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637>\
**Category:** FHE Questions\
**Created:** [June 15, 2023, 12:45pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637 "2023-06-15T12:45:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 15, 2023, 12:45pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/1 "2023-06-15T12:45:51Z")

</div>

Hi,  
I want to do secure inference of the simple neural network. where client hold the neural network that is the weight of the neural network and user hold the input.

So I am tring to implement an neural network using arbitrary weight vector. so while doing that each layer of the neural network needs an matrix vector multiplication(Considerding it as n number of innerproduct) followed by an activation function on the output vector of matrix-vector multiplication. I can see that multiplication depth used by the n innerproduct is 2 and by the activation function is 3. So each layer on total use 5 multiplication depth. How can I reduce the number of multiplication depth?

For the innerproduct I am doing pliantext-ciphertext multiplication.  
For evaluating the activation function I used polynomial approximation with an polynomial of degree 3.

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [June 15, 2023, 4:39pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/2 "2023-06-15T16:39:08Z")

</div>

Sounds to me like you don’t have many options. One trick the FHE community uses is to pack things in specific formats to reduce the number of encrypted multiplications. I cant say for sure without looking at your code, data format, etc, but it’s sounding like you’ve gotten it as small as you can get it. You’ll probably have to use bootstrapping at this point

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [June 15, 2023, 9:46pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/3 "2023-06-15T21:46:00Z")

</div>

Explain why the multiplicative depth of the inner product is 2. It should be 1, I believe.  
Also, what is the degree of your approximation polynomial? If you are unsure of the degree, please provide your polynomial.

---

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 16, 2023, 10:54am UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/4 "2023-06-16T10:54:10Z")

</div>

The degree of the approximate polynomial is 3  
I am not getting the fact why the inner product takes two multiplication depths. Is there any way to reduce it?

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [June 16, 2023, 2:25pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/5 "2023-06-16T14:25:50Z")

</div>

Can you share a minimal working example of your issue? I can’t help without examining some code.

---

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 16, 2023, 4:05pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/6 "2023-06-16T16:05:22Z")

</div>

```auto
void innerProduct(Ciphertext<DCRTPoly>& output, const Ciphertext<DCRTPoly>& input1, const Plaintext& input2, CryptoContext<DCRTPoly>& cc, int i, bool accumulate=true){
	
    auto cMul = cc->EvalMult(input1, input2);
    auto ctemp2= cMul;
	
    for(int i=1; i<=log(8)+1; i++)
    {
      	auto ctemp1 = cc->EvalRotate(cMul, i);
	ctemp2 = cc->EvalAdd(ctemp2, ctemp1);
    }
    auto ctemp1=cc->EvalRotate(ctemp2, 4);
    ctemp2=cc->EvalAdd(ctemp2, ctemp1);

    // apply the mask
    std::vector<double> x1 = {0, 0, 0, 0, 0, 0,0, 0};
    x1[i]=1;
    
    // put the result in output
    Plaintext mask = cc->MakeCKKSPackedPlaintext(x1);
    if (accumulate){
    	output += cc->EvalMult(ctemp2, mask);
    }else{
    	output = cc->EvalMult(ctemp2, mask);
    }
}

```

Here is the inner product function. It takes two multiplicative depths. can I reduce it any how?

---

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 16, 2023, 4:07pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/7 "2023-06-16T16:07:11Z")

</div>

```auto
void EvalLogisticExample(Ciphertext<DCRTPoly>& output, const Ciphertext<DCRTPoly>& input1, CryptoContext<DCRTPoly>& cc, double a, double b) {

    uint32_t polyDegree = 3;
    double lowerBound = -a;
    double upperBound = b;
    
    //output = cc->EvalLogistic(input1, lowerBound, upperBound, polyDegree);
    output = cc->EvalChebyshevFunction([](double x) -> double { if (x < 0) return 0; else return x; }, input1, lowerBound,
                                            upperBound, polyDegree);
}

```

Here is the function evaluation on a vector (ciphertext). can this be modified such that it takes less multiplicative depth? now it takes 3 multiplicative depth.

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [June 16, 2023, 8:37pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/8 "2023-06-16T20:37:24Z")

</div>

> Here is the inner product function. It takes two multiplicative depths. can I reduce it any how?

We have an internal [EvalInnerProduct](https://openfhe-development.readthedocs.io/en/latest/api/classlbcrypto_1_1AdvancedSHEBase.html?highlight=inner%20product#_CPPv4NK8lbcrypto15AdvancedSHEBase16EvalInnerProductE15ConstCiphertextI7ElementE15ConstCiphertextI7ElementE5usintRKNSt3mapI5usint7EvalKeyI7ElementEEEK7EvalKeyI7ElementE) Is there a reason you’re choosing to not use it?

> Chebyshev multiplicative depth

This doesn’t answer your question, but you should read through [OpenFHE Lattice Cryptography Library - Arbitrary Smooth Function Evaluation](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/FUNCTION_EVALUATION.md#how-to-choose-multiplicative-depth) if you haven’t already done so.

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [June 17, 2023, 12:03am UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/9 "2023-06-17T00:03:34Z")

</div>

It takes two levels because you have a homomorphic multiplication and then you also multiply by a mask to clear all values except for one of the slots. It is the multiplication by a mask that takes the second level. You could rearrange the encoding so that after EvalMult and EvalSum you get the same values in all slots (of a subring element) if this is what you desire (depends on what the desired output should be). The `EvalInnerProduct` in OpenFHE does not multiply by a mask, and hence costs only 1 level.

---

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 22, 2023, 9:19am UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/10 "2023-06-22T09:19:28Z")

</div>

Okay.  
Is there any way of parallel processing so that the program takes less time like what we can do during the “make” of the library?

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [June 22, 2023, 9:56pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/11 "2023-06-22T21:56:54Z")

</div>

It looks like you are asking a new question. Are you asking about multithreading support in OpenFHE? Please open a new topic for this.

---

<div class="post-metadata">

**Author:** ![meamit](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@meamit](https://openfhe.discourse.group/u/meamit)\
**Post date:** [June 22, 2023, 10:09pm UTC](https://openfhe.discourse.group/t/how-to-reduce-multiplication-depth-in-ckks/637/12 "2023-06-22T22:09:10Z")

</div>

Yes.  
Okay, I am doing that.
