# Polynomial Multiplication & other high-level operations on hardware backend

**URL:** <https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336>\
**Category:** OpenFHE Hardware Acceleration\
**Created:** [February 6, 2023, 4:34am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336 "2023-02-06T04:34:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![homer\_g](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@homer\_g](https://openfhe.discourse.group/u/homer_g)\
**Post date:** [February 6, 2023, 4:34am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/1 "2023-02-06T04:34:50Z")

</div>

I would like to implement polynomial multiplication and other higher-level operations using a hardware-integrated backend implemented through the hardware abstraction layer.

I investigated the current backends that are provided (HEXL). From what I was able to understand from **mubintvecnathexl.cpp** , the backend is able to execute mostly lower-level operations such as Modular Multiplication (ModMul).

In **benativehexl-math-impl.cpp,** I found a template for Polynomial Multiplication (PolynomialMultiplication). However after running some examples, it seems that the function is not called during the execution of a program that contains for example an EvalMult() operation. I was only able to see PolynomialMultiplication() be executed within **unittests** , which I believe only test for correctness.

Ideally I would want the backend be able to process a “customized” polynomial multiplication operation instead of low-level ModMults.

Is there a way to accomplish this? If so, could you please point me to the right direction?

Thank you very much.

---

<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:** [February 7, 2023, 12:16am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/2 "2023-02-07T00:16:12Z")

</div>

The `PolynomialMultiplication()` you are referring to is only used for correctness checks. It implements the schoolbook polynomial multiplication of complexity N^2, where N is the input polynomial length. We do not recommend using this algorithm in FHE implementation.

For your purpose, you are advised to implement the faster version of polynomial multiplication that uses NTT and runs in N\log\_2{}N. At a high level, this algorithm does the following:  
Given polynomials a and b:

1. A=NTT(a), forward NTT
2. B=NTT(b), forward NTT
3. C=A \bigodot B, where \bigodot is point-wise modular multiplication
4. c=INTT(C), inverse NTT

A concrete example illustrating this algorithm is provided in UnitTest `CRT_polynomial_mult_small` which can be found [here](https://github.com/openfheorg/openfhe-development/blob/main/src/core/unittest/UnitTestTransform.cpp#L105).

Note that OpenFHE uses the described algorithm above extensively in the low-level implementations of the different schemes. Sometimes the input would be already in the NTT representation (which means we do not need to invoke steps 1 and 2), other times we do not need to convert the product into the coefficient representation (that is, we do not need to invoke step 4). For that reason, we advise implementing polynomial multiplication from the low-level operations it is composed of such as NTT, INTT, and ModMul, which should be provided by your accelerator.

---

<div class="post-metadata">

**Author:** ![homer\_g](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@homer\_g](https://openfhe.discourse.group/u/homer_g)\
**Post date:** [February 7, 2023, 8:47am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/3 "2023-02-07T08:47:38Z")

</div>

Hello Caesar, thank you for your reply.  
I am more concerned about the technical aspect of the process.

Could you point out which method is invoked in the regular execution when openFHE tries to perform polynomial multiplication using 2 polynomials?

Ideally I would like to have my custom backend execute a polynomial multiplication operation of the form poly\_mult(A,B).  
What is the procedure I would have to follow to accomplish this?  
Thank you.

---

<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:** [February 7, 2023, 2:40pm UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/4 "2023-02-07T14:40:32Z")

</div>

First, I do not see why there would be a need to implement `poly_mult(A, B)` as an atomic operation. In OpenFHE, we provide a complete set of low-level operations that can be used to compose any ring operation that might be required by the supported FHE schemes. Assuming your accelerator implements these low-level operations, you should be able to implement `poly_mult(A, B)` or potentially any other ring operation from the low-level operations.

If you still want to implement a new custom method, you may want to follow the guidelines below. For the sake of illustration, let’s assume you are interested in adding a new custom method to `DCRTPoly`. In order to provide your own implementation of the new custom method,  
you will need to do the following:

1. declare your method in `dcrtpoly.h`.
2. declare your method in `dcrtpoly-interface.h`.
3. declare your method in `<accelerator-name>dcrtpoly.h`, check `hexldcrtpoly.h` for an example.
4. make sure you provide the implementation in `<accelerator-name>dcrtpoly.cpp` check `hexldcrtpoly.cpp` for an example.

As a guiding example, you can trace the evolution of any of the methods declared in `hexldcrtpoly.h` and do the same for your custom method.

Lastly, I would like to note that polynomial multiplication is implemented in `dcrtpoly.cpp` as [Times](https://github.com/openfheorg/openfhe-development/blob/main/src/core/lib/lattice/hal/default/dcrtpoly.cpp#L862). If you are interested in implementing `poly_mult`, I recommend using this method.

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 8, 2023, 7:17pm UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/5 "2023-02-08T19:17:59Z")

</div>

Hi @homer_g.  
the Current HAL layer is insufficient for any PCI based accelerator, and also stops at the lowest math and transform level – it needs to be augmented up to the lattice layer in order to really accelerate commercial level FHE workloads. We plan to do this under our DARPA program.  
You are actually the vanguard some of this by being the first one to investigate what is and isn’t there in the HAL for a PCI interfaced accelerator.

I think we need some definitions here.

what do you call a poly?  
and tell me exactly what your poly\_mult(a,b) would do, and how you see it used in FHE?

Because in OpenFHE our poly’s are basically 1) all in RNS form and are called DCRTpoly 2) components of other things such as cipertexts and keys.  
Rarely do you just multiply two DCRTpolynomials hence the reason there isn’t an obvious stand alone call. @Caesar points out Times but that is an elementwise hadamard multiply of all the towers in a DCRTpoly and not a “multiplication of two polynomials” which requires a convolution of their coefficients. The code that @Caesar pointed out is what is used for that as direct convolution is N^2 vs NlogN and N is LARGE.

I would suggest maybe you provide us with a list of what your expected acceleration primitives are and then we may be able to suggest similar points in the OpenFHE code. Basically you have a small number of primitives and a huge number of OpenFHE functions, so wouldn’t that make sense?

---

<div class="post-metadata">

**Author:** ![adki](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@adki](https://openfhe.discourse.group/u/adki)\
**Post date:** [February 17, 2023, 6:58am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/6 "2023-02-17T06:58:53Z")

</div>

I wonder if there are any further discussion?

A few days before I did asked any comment on following question.  
“How to use custom NTT/CRT library” [How to use custom NTT/CRT library](https://openfhe.discourse.group/t/how-to-use-custom-ntt-crt-library/360)

Basically, it is the same question how to offload (plug-in) the other implementation of NTT/CRT routines on the OpenFHE framework.

---

<div class="post-metadata">

**Author:** ![Minnow](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Minnow](https://openfhe.discourse.group/u/Minnow)\
**Post date:** [June 6, 2024, 6:34am UTC](https://openfhe.discourse.group/t/polynomial-multiplication-other-high-level-operations-on-hardware-backend/336/7 "2024-06-06T06:34:55Z")

</div>

Hello, is there any information about the progress on the HAL layer including lattice layer? Will this progress added to the further version of OpenFHE?  
Thanks very much.
